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); |
| 1173 | module = lys_parse(ctx, addr, format); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1174 | munmap(addr, sb.st_size); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1175 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1176 | return module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1177 | } |
| 1178 | |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1179 | struct lys_submodule * |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1180 | 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] | 1181 | { |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1182 | struct lys_submodule *submodule; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1183 | struct stat sb; |
| 1184 | char *addr; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1185 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1186 | assert(module); |
| 1187 | assert(fd >= 0); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1188 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1189 | /* |
| 1190 | * TODO |
| 1191 | * This is just a temporary solution to make working automatic search for |
| 1192 | * imported modules. This doesn't work e.g. for streams (stdin) |
| 1193 | */ |
| 1194 | fstat(fd, &sb); |
| 1195 | addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 1196 | /* TODO addr error check */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1197 | submodule = lys_submodule_parse(module, addr, format, implement); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1198 | munmap(addr, sb.st_size); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1199 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1200 | return submodule; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1201 | |
| 1202 | } |
| 1203 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1204 | static struct lys_restr * |
| 1205 | 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] | 1206 | { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1207 | struct lys_restr *result; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1208 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1209 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1210 | if (!size) { |
| 1211 | return NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1212 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1213 | |
| 1214 | result = calloc(size, sizeof *result); |
| 1215 | for (i = 0; i < size; i++) { |
| 1216 | result[i].expr = lydict_insert(ctx, old[i].expr, 0); |
| 1217 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 1218 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 1219 | result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0); |
| 1220 | result[i].emsg = lydict_insert(ctx, old[i].emsg, 0); |
| 1221 | } |
| 1222 | |
| 1223 | return result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1224 | } |
| 1225 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1226 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1227 | lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr) |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1228 | { |
| 1229 | assert(ctx); |
| 1230 | if (!restr) { |
| 1231 | return; |
| 1232 | } |
| 1233 | |
| 1234 | lydict_remove(ctx, restr->expr); |
| 1235 | lydict_remove(ctx, restr->dsc); |
| 1236 | lydict_remove(ctx, restr->ref); |
| 1237 | lydict_remove(ctx, restr->eapptag); |
| 1238 | lydict_remove(ctx, restr->emsg); |
| 1239 | } |
| 1240 | |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1241 | static int |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1242 | 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] | 1243 | struct unres_schema *unres) |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1244 | { |
| 1245 | int i; |
| 1246 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1247 | new->module_name = lydict_insert(mod->ctx, old->module_name, 0); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1248 | new->base = old->base; |
| 1249 | new->der = old->der; |
| 1250 | |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 1251 | i = unres_schema_find(unres, old, UNRES_TYPE_DER); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1252 | if (i != -1) { |
| 1253 | /* HACK for unres */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1254 | new->der = (struct lys_tpdf *)parent; |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1255 | /* all these unres additions can fail even though they did not before */ |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 1256 | 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] | 1257 | return -1; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 1258 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1259 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1260 | } |
| 1261 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1262 | switch (new->base) { |
| 1263 | case LY_TYPE_BINARY: |
Radek Krejci | 425adf0 | 2015-06-26 16:23:28 +0200 | [diff] [blame] | 1264 | if (old->info.binary.length) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1265 | 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] | 1266 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1267 | break; |
| 1268 | |
| 1269 | case LY_TYPE_BITS: |
| 1270 | new->info.bits.count = old->info.bits.count; |
| 1271 | if (new->info.bits.count) { |
| 1272 | new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit); |
| 1273 | for (i = 0; i < new->info.bits.count; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1274 | new->info.bits.bit[i].name = lydict_insert(mod->ctx, old->info.bits.bit[i].name, 0); |
| 1275 | new->info.bits.bit[i].dsc = lydict_insert(mod->ctx, old->info.bits.bit[i].dsc, 0); |
| 1276 | new->info.bits.bit[i].ref = lydict_insert(mod->ctx, old->info.bits.bit[i].ref, 0); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1277 | new->info.bits.bit[i].status = old->info.bits.bit[i].status; |
| 1278 | new->info.bits.bit[i].pos = old->info.bits.bit[i].pos; |
| 1279 | } |
| 1280 | } |
| 1281 | break; |
| 1282 | |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1283 | case LY_TYPE_DEC64: |
| 1284 | new->info.dec64.dig = old->info.dec64.dig; |
| 1285 | if (old->info.dec64.range) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1286 | 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] | 1287 | } |
| 1288 | break; |
| 1289 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1290 | case LY_TYPE_ENUM: |
| 1291 | new->info.enums.count = old->info.enums.count; |
| 1292 | if (new->info.enums.count) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1293 | 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] | 1294 | for (i = 0; i < new->info.enums.count; i++) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1295 | new->info.enums.enm[i].name = lydict_insert(mod->ctx, old->info.enums.enm[i].name, 0); |
| 1296 | new->info.enums.enm[i].dsc = lydict_insert(mod->ctx, old->info.enums.enm[i].dsc, 0); |
| 1297 | new->info.enums.enm[i].ref = lydict_insert(mod->ctx, old->info.enums.enm[i].ref, 0); |
| 1298 | new->info.enums.enm[i].status = old->info.enums.enm[i].status; |
| 1299 | new->info.enums.enm[i].value = old->info.enums.enm[i].value; |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | break; |
| 1303 | |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1304 | case LY_TYPE_IDENT: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1305 | if (old->info.ident.ref) { |
| 1306 | new->info.ident.ref = old->info.ident.ref; |
| 1307 | } else { |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 1308 | i = unres_schema_find(unres, old, UNRES_TYPE_IDENTREF); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1309 | assert(i != -1); |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 1310 | 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] | 1311 | return -1; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 1312 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1313 | } |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1314 | break; |
| 1315 | |
Radek Krejci | af35142 | 2015-06-19 14:49:38 +0200 | [diff] [blame] | 1316 | case LY_TYPE_INST: |
| 1317 | new->info.inst.req = old->info.inst.req; |
| 1318 | break; |
| 1319 | |
Radek Krejci | f286013 | 2015-06-20 12:37:20 +0200 | [diff] [blame] | 1320 | case LY_TYPE_INT8: |
| 1321 | case LY_TYPE_INT16: |
| 1322 | case LY_TYPE_INT32: |
| 1323 | case LY_TYPE_INT64: |
| 1324 | case LY_TYPE_UINT8: |
| 1325 | case LY_TYPE_UINT16: |
| 1326 | case LY_TYPE_UINT32: |
| 1327 | case LY_TYPE_UINT64: |
Radek Krejci | 425adf0 | 2015-06-26 16:23:28 +0200 | [diff] [blame] | 1328 | if (old->info.num.range) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1329 | 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] | 1330 | } |
Radek Krejci | f286013 | 2015-06-20 12:37:20 +0200 | [diff] [blame] | 1331 | break; |
| 1332 | |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1333 | case LY_TYPE_LEAFREF: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1334 | 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] | 1335 | 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] | 1336 | return -1; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 1337 | } |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1338 | break; |
| 1339 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1340 | case LY_TYPE_STRING: |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1341 | if (old->info.str.length) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1342 | 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] | 1343 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1344 | 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] | 1345 | break; |
| 1346 | |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1347 | case LY_TYPE_UNION: |
| 1348 | new->info.uni.count = old->info.uni.count; |
| 1349 | if (new->info.uni.count) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1350 | 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] | 1351 | for (i = 0; i < new->info.uni.count; i++) { |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1352 | if (lys_type_dup(mod, parent, &(new->info.uni.types[i]), &(old->info.uni.types[i]), unres)) { |
| 1353 | return -1; |
| 1354 | } |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1355 | } |
| 1356 | } |
| 1357 | break; |
| 1358 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1359 | default: |
Radek Krejci | c7c8553 | 2015-07-02 10:16:54 +0200 | [diff] [blame] | 1360 | /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */ |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1361 | break; |
| 1362 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1363 | |
| 1364 | return EXIT_SUCCESS; |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1368 | lys_type_free(struct ly_ctx *ctx, struct lys_type *type) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1369 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1370 | int i; |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1371 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1372 | assert(ctx); |
| 1373 | if (!type) { |
| 1374 | return; |
| 1375 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1376 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1377 | lydict_remove(ctx, type->module_name); |
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 | switch (type->base) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1380 | case LY_TYPE_BINARY: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1381 | lys_restr_free(ctx, type->info.binary.length); |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1382 | free(type->info.binary.length); |
| 1383 | break; |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 1384 | case LY_TYPE_BITS: |
| 1385 | for (i = 0; i < type->info.bits.count; i++) { |
| 1386 | lydict_remove(ctx, type->info.bits.bit[i].name); |
| 1387 | lydict_remove(ctx, type->info.bits.bit[i].dsc); |
| 1388 | lydict_remove(ctx, type->info.bits.bit[i].ref); |
| 1389 | } |
| 1390 | free(type->info.bits.bit); |
| 1391 | break; |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1392 | |
| 1393 | case LY_TYPE_DEC64: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1394 | lys_restr_free(ctx, type->info.dec64.range); |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1395 | free(type->info.dec64.range); |
| 1396 | break; |
| 1397 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1398 | case LY_TYPE_ENUM: |
| 1399 | for (i = 0; i < type->info.enums.count; i++) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1400 | lydict_remove(ctx, type->info.enums.enm[i].name); |
| 1401 | lydict_remove(ctx, type->info.enums.enm[i].dsc); |
| 1402 | lydict_remove(ctx, type->info.enums.enm[i].ref); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1403 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1404 | free(type->info.enums.enm); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1405 | break; |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1406 | |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 1407 | case LY_TYPE_INT8: |
| 1408 | case LY_TYPE_INT16: |
| 1409 | case LY_TYPE_INT32: |
| 1410 | case LY_TYPE_INT64: |
| 1411 | case LY_TYPE_UINT8: |
| 1412 | case LY_TYPE_UINT16: |
| 1413 | case LY_TYPE_UINT32: |
| 1414 | case LY_TYPE_UINT64: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1415 | lys_restr_free(ctx, type->info.num.range); |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 1416 | free(type->info.num.range); |
| 1417 | break; |
| 1418 | |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1419 | case LY_TYPE_LEAFREF: |
| 1420 | lydict_remove(ctx, type->info.lref.path); |
| 1421 | break; |
| 1422 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1423 | case LY_TYPE_STRING: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1424 | lys_restr_free(ctx, type->info.str.length); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1425 | free(type->info.str.length); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1426 | for (i = 0; i < type->info.str.pat_count; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1427 | lys_restr_free(ctx, &type->info.str.patterns[i]); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1428 | } |
| 1429 | free(type->info.str.patterns); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1430 | break; |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1431 | |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1432 | case LY_TYPE_UNION: |
| 1433 | for (i = 0; i < type->info.uni.count; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1434 | lys_type_free(ctx, &type->info.uni.types[i]); |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1435 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1436 | free(type->info.uni.types); |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1437 | break; |
| 1438 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1439 | default: |
Radek Krejci | c7c8553 | 2015-07-02 10:16:54 +0200 | [diff] [blame] | 1440 | /* 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] | 1441 | break; |
| 1442 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1443 | } |
| 1444 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1445 | static void |
| 1446 | lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1447 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1448 | assert(ctx); |
| 1449 | if (!tpdf) { |
| 1450 | return; |
| 1451 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1452 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1453 | lydict_remove(ctx, tpdf->name); |
| 1454 | lydict_remove(ctx, tpdf->dsc); |
| 1455 | lydict_remove(ctx, tpdf->ref); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1456 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1457 | lys_type_free(ctx, &tpdf->type); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1458 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1459 | lydict_remove(ctx, tpdf->units); |
| 1460 | lydict_remove(ctx, tpdf->dflt); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1461 | } |
| 1462 | |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1463 | static struct lys_tpdf * |
| 1464 | lys_tpdf_dup(struct lys_module *mod, struct lys_node *parent, struct lys_tpdf *old, int size, struct unres_schema *unres) |
| 1465 | { |
| 1466 | struct lys_tpdf *result; |
| 1467 | int i, j; |
| 1468 | |
| 1469 | if (!size) { |
| 1470 | return NULL; |
| 1471 | } |
| 1472 | |
| 1473 | result = calloc(size, sizeof *result); |
| 1474 | for (i = 0; i < size; i++) { |
| 1475 | result[i].name = lydict_insert(mod->ctx, old[i].name, 0); |
| 1476 | result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0); |
| 1477 | result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0); |
| 1478 | result[i].flags = old[i].flags; |
| 1479 | result[i].module = old[i].module; |
| 1480 | |
| 1481 | if (lys_type_dup(mod, parent, &(result[i].type), &(old[i].type), unres)) { |
| 1482 | for (j = 0; j <= i; ++j) { |
| 1483 | lys_tpdf_free(mod->ctx, &result[j]); |
| 1484 | } |
| 1485 | free(result); |
| 1486 | return NULL; |
| 1487 | } |
| 1488 | |
| 1489 | result[i].dflt = lydict_insert(mod->ctx, old[i].dflt, 0); |
| 1490 | result[i].units = lydict_insert(mod->ctx, old[i].units, 0); |
| 1491 | } |
| 1492 | |
| 1493 | return result; |
| 1494 | } |
| 1495 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1496 | static struct lys_when * |
| 1497 | lys_when_dup(struct ly_ctx *ctx, struct lys_when *old) |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1498 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1499 | struct lys_when *new; |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1500 | |
| 1501 | if (!old) { |
| 1502 | return NULL; |
| 1503 | } |
| 1504 | |
| 1505 | new = calloc(1, sizeof *new); |
| 1506 | new->cond = lydict_insert(ctx, old->cond, 0); |
| 1507 | new->dsc = lydict_insert(ctx, old->dsc, 0); |
| 1508 | new->ref = lydict_insert(ctx, old->ref, 0); |
| 1509 | |
| 1510 | return new; |
| 1511 | } |
| 1512 | |
Michal Vasko | 0308dd6 | 2015-10-07 09:14:40 +0200 | [diff] [blame] | 1513 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1514 | lys_when_free(struct ly_ctx *ctx, struct lys_when *w) |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1515 | { |
| 1516 | if (!w) { |
| 1517 | return; |
| 1518 | } |
| 1519 | |
| 1520 | lydict_remove(ctx, w->cond); |
| 1521 | lydict_remove(ctx, w->dsc); |
| 1522 | lydict_remove(ctx, w->ref); |
| 1523 | |
| 1524 | free(w); |
| 1525 | } |
| 1526 | |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1527 | static void |
| 1528 | lys_augment_free(struct ly_ctx *ctx, struct lys_node_augment aug) |
| 1529 | { |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 1530 | struct lys_node *next, *sub; |
| 1531 | |
| 1532 | /* children from a resolved uses */ |
| 1533 | LY_TREE_FOR_SAFE(aug.child, next, sub) { |
| 1534 | lys_node_free(sub); |
| 1535 | } |
| 1536 | |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1537 | lydict_remove(ctx, aug.target_name); |
| 1538 | lydict_remove(ctx, aug.dsc); |
| 1539 | lydict_remove(ctx, aug.ref); |
| 1540 | |
| 1541 | free(aug.features); |
| 1542 | |
| 1543 | lys_when_free(ctx, aug.when); |
| 1544 | |
Michal Vasko | 7d356a5 | 2015-08-19 15:06:31 +0200 | [diff] [blame] | 1545 | /* Do not free the children, they were appended somewhere and their |
| 1546 | * new parent will take care of them. |
| 1547 | */ |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1548 | } |
| 1549 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1550 | static struct lys_node_augment * |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1551 | 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] | 1552 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1553 | struct lys_node_augment *new = NULL; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1554 | struct lys_node *old_child, *new_child; |
| 1555 | int i; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1556 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1557 | if (!size) { |
| 1558 | return NULL; |
| 1559 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1560 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1561 | new = calloc(size, sizeof *new); |
| 1562 | for (i = 0; i < size; i++) { |
| 1563 | new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0); |
| 1564 | new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0); |
| 1565 | new[i].ref = lydict_insert(module->ctx, old[i].ref, 0); |
| 1566 | new[i].flags = old[i].flags; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1567 | new[i].module = old[i].module; |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 1568 | new[i].nodetype = old[i].nodetype; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1569 | /* this must succeed, it was already resolved once */ |
| 1570 | if (resolve_schema_nodeid(new[i].target_name, parent->child, new[i].module, LYS_AUGMENT, &new[i].target)) { |
| 1571 | LOGINT; |
| 1572 | free(new); |
| 1573 | return NULL; |
| 1574 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1575 | new[i].parent = parent; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1576 | |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1577 | /* Correct the augment nodes. |
| 1578 | * This function can only be called from lys_node_dup() with uses |
| 1579 | * being the node duplicated, so we must have a case of grouping |
| 1580 | * with a uses with augments. The augmented nodes have already been |
| 1581 | * copied and everything is almost fine except their parent is wrong |
Michal Vasko | a5900c5 | 2015-09-24 13:17:11 +0200 | [diff] [blame] | 1582 | * (it was set to their actual data parent, not an augment), and |
| 1583 | * the new augment does not have child pointer to its augment nodes, |
| 1584 | * so we just correct it. |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1585 | */ |
| 1586 | LY_TREE_FOR(new[i].target->child, new_child) { |
| 1587 | if (new_child->name == old[i].child->name) { |
| 1588 | break; |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1589 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1590 | } |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1591 | assert(new_child); |
Michal Vasko | a5900c5 | 2015-09-24 13:17:11 +0200 | [diff] [blame] | 1592 | new[i].child = new_child; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1593 | LY_TREE_FOR(old[i].child, old_child) { |
| 1594 | /* all augment nodes were connected as siblings, there can be no more after this */ |
| 1595 | if (old_child->parent != (struct lys_node *)&old[i]) { |
| 1596 | break; |
| 1597 | } |
| 1598 | |
| 1599 | assert(old_child->name == new_child->name); |
| 1600 | |
| 1601 | new_child->parent = (struct lys_node *)&new[i]; |
| 1602 | new_child = new_child->next; |
| 1603 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1604 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1605 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1606 | return new; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1607 | } |
| 1608 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1609 | static struct lys_refine * |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 1610 | 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] | 1611 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1612 | struct lys_refine *result; |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 1613 | int i; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1614 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1615 | if (!size) { |
| 1616 | return NULL; |
| 1617 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1618 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1619 | result = calloc(size, sizeof *result); |
| 1620 | for (i = 0; i < size; i++) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1621 | 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] | 1622 | result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0); |
| 1623 | result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1624 | result[i].flags = old[i].flags; |
| 1625 | result[i].target_type = old[i].target_type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1626 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1627 | result[i].must_size = old[i].must_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1628 | 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] | 1629 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1630 | if (result[i].target_type & (LYS_LEAF | LYS_CHOICE)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1631 | 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] | 1632 | } else if (result[i].target_type == LYS_CONTAINER) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1633 | 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] | 1634 | } else if (result[i].target_type & (LYS_LIST | LYS_LEAFLIST)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1635 | result[i].mod.list = old[i].mod.list; |
| 1636 | } |
| 1637 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1638 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1639 | return result; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1640 | } |
| 1641 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1642 | static void |
| 1643 | lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident) |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1644 | { |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 1645 | struct lys_ident_der *der; |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1646 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1647 | assert(ctx); |
| 1648 | if (!ident) { |
| 1649 | return; |
| 1650 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1651 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1652 | /* |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1653 | * if caller free only a single data model which is used (its identity is |
| 1654 | * reference from identity in another module), this silly freeing can lead |
| 1655 | * to segmentation fault. But without noting if the module is used by some |
| 1656 | * other, it cannot be solved. |
Radek Krejci | a339069 | 2015-06-16 14:13:31 +0200 | [diff] [blame] | 1657 | * |
| 1658 | * Possible solution is to not allow caller to remove particular schema |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1659 | * from the context. This is the current approach. |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1660 | */ |
| 1661 | while (ident->der) { |
| 1662 | der = ident->der; |
| 1663 | ident->der = der->next; |
| 1664 | free(der); |
| 1665 | } |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1666 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1667 | lydict_remove(ctx, ident->name); |
| 1668 | lydict_remove(ctx, ident->dsc); |
| 1669 | lydict_remove(ctx, ident->ref); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1670 | |
| 1671 | } |
| 1672 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1673 | static void |
| 1674 | lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1675 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1676 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1677 | |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1678 | /* handle only specific parts for LYS_GROUPING */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1679 | for (i = 0; i < grp->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1680 | lys_tpdf_free(ctx, &grp->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1681 | } |
| 1682 | free(grp->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1683 | } |
| 1684 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1685 | static void |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1686 | lys_rpc_inout_free(struct ly_ctx *ctx, struct lys_node_rpc_inout *io) |
| 1687 | { |
| 1688 | int i; |
| 1689 | |
| 1690 | /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */ |
| 1691 | for (i = 0; i < io->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1692 | lys_tpdf_free(ctx, &io->tpdf[i]); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1693 | } |
| 1694 | free(io->tpdf); |
| 1695 | } |
| 1696 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1697 | static void |
| 1698 | lys_anyxml_free(struct ly_ctx *ctx, struct lys_node_anyxml *anyxml) |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1699 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1700 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1701 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1702 | for (i = 0; i < anyxml->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1703 | lys_restr_free(ctx, &anyxml->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1704 | } |
| 1705 | free(anyxml->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1706 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1707 | lys_when_free(ctx, anyxml->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1708 | } |
| 1709 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1710 | static void |
| 1711 | lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1712 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1713 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1714 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1715 | for (i = 0; i < leaf->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1716 | lys_restr_free(ctx, &leaf->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1717 | } |
| 1718 | free(leaf->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1719 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1720 | lys_when_free(ctx, leaf->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1721 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1722 | lys_type_free(ctx, &leaf->type); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1723 | lydict_remove(ctx, leaf->units); |
| 1724 | lydict_remove(ctx, leaf->dflt); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1725 | } |
| 1726 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1727 | static void |
| 1728 | lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1729 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1730 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1731 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1732 | for (i = 0; i < llist->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1733 | lys_restr_free(ctx, &llist->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1734 | } |
| 1735 | free(llist->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1736 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1737 | lys_when_free(ctx, llist->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1738 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1739 | lys_type_free(ctx, &llist->type); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1740 | lydict_remove(ctx, llist->units); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1741 | } |
| 1742 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1743 | static void |
| 1744 | lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1745 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame^] | 1746 | int i, j; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1747 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1748 | /* handle only specific parts for LY_NODE_LIST */ |
| 1749 | for (i = 0; i < list->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1750 | lys_tpdf_free(ctx, &list->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1751 | } |
| 1752 | free(list->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1753 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1754 | for (i = 0; i < list->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1755 | lys_restr_free(ctx, &list->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1756 | } |
| 1757 | free(list->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1758 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1759 | lys_when_free(ctx, list->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1760 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1761 | for (i = 0; i < list->unique_size; i++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame^] | 1762 | for (j = 0; j > list->unique[i].expr_size; j++) { |
| 1763 | lydict_remove(ctx, list->unique[i].expr[j]); |
| 1764 | } |
| 1765 | free(list->unique[i].expr); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1766 | } |
| 1767 | free(list->unique); |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1768 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1769 | free(list->keys); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1770 | } |
| 1771 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1772 | static void |
| 1773 | lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1774 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1775 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1776 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1777 | /* handle only specific parts for LY_NODE_CONTAINER */ |
| 1778 | lydict_remove(ctx, cont->presence); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1779 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1780 | for (i = 0; i < cont->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1781 | lys_tpdf_free(ctx, &cont->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1782 | } |
| 1783 | free(cont->tpdf); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1784 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1785 | for (i = 0; i < cont->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1786 | lys_restr_free(ctx, &cont->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1787 | } |
| 1788 | free(cont->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1789 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1790 | lys_when_free(ctx, cont->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1791 | } |
| 1792 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1793 | static void |
| 1794 | lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f) |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1795 | { |
| 1796 | lydict_remove(ctx, f->name); |
| 1797 | lydict_remove(ctx, f->dsc); |
| 1798 | lydict_remove(ctx, f->ref); |
| 1799 | free(f->features); |
| 1800 | } |
| 1801 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1802 | static void |
| 1803 | lys_deviation_free(struct ly_ctx *ctx, struct lys_deviation *dev) |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1804 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame^] | 1805 | int i, j, k; |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1806 | |
| 1807 | lydict_remove(ctx, dev->target_name); |
| 1808 | lydict_remove(ctx, dev->dsc); |
| 1809 | lydict_remove(ctx, dev->ref); |
| 1810 | |
| 1811 | for (i = 0; i < dev->deviate_size; i++) { |
| 1812 | lydict_remove(ctx, dev->deviate[i].dflt); |
| 1813 | lydict_remove(ctx, dev->deviate[i].units); |
| 1814 | |
| 1815 | if (dev->deviate[i].mod == LY_DEVIATE_DEL) { |
| 1816 | for (j = 0; j < dev->deviate[i].must_size; j++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1817 | lys_restr_free(ctx, &dev->deviate[i].must[j]); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1818 | } |
| 1819 | free(dev->deviate[i].must); |
| 1820 | |
| 1821 | for (j = 0; j < dev->deviate[i].unique_size; j++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame^] | 1822 | for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) { |
| 1823 | lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]); |
| 1824 | } |
| 1825 | free(dev->deviate[j].unique[j].expr); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1826 | } |
| 1827 | free(dev->deviate[i].unique); |
| 1828 | } |
| 1829 | } |
| 1830 | free(dev->deviate); |
| 1831 | } |
| 1832 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1833 | static void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1834 | lys_uses_free(struct ly_ctx *ctx, struct lys_node_uses *uses) |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1835 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1836 | int i, j; |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1837 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1838 | for (i = 0; i < uses->refine_size; i++) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1839 | lydict_remove(ctx, uses->refine[i].target_name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1840 | lydict_remove(ctx, uses->refine[i].dsc); |
| 1841 | lydict_remove(ctx, uses->refine[i].ref); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1842 | |
Michal Vasko | a275c0a | 2015-09-24 09:55:42 +0200 | [diff] [blame] | 1843 | for (j = 0; j < uses->refine[i].must_size; j++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1844 | lys_restr_free(ctx, &uses->refine[i].must[j]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1845 | } |
| 1846 | free(uses->refine[i].must); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1847 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1848 | if (uses->refine[i].target_type & (LYS_LEAF | LYS_CHOICE)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1849 | lydict_remove(ctx, uses->refine[i].mod.dflt); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1850 | } else if (uses->refine[i].target_type & LYS_CONTAINER) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1851 | lydict_remove(ctx, uses->refine[i].mod.presence); |
| 1852 | } |
| 1853 | } |
| 1854 | free(uses->refine); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1855 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1856 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1857 | lys_augment_free(ctx, uses->augment[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1858 | } |
| 1859 | free(uses->augment); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1860 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1861 | lys_when_free(ctx, uses->when); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1862 | } |
| 1863 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1864 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1865 | lys_node_free(struct lys_node *node) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1866 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1867 | struct ly_ctx *ctx; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1868 | struct lys_node *sub, *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1869 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1870 | if (!node) { |
| 1871 | return; |
| 1872 | } |
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 | assert(node->module); |
| 1875 | assert(node->module->ctx); |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1876 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1877 | ctx = node->module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1878 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1879 | /* common part */ |
| 1880 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1881 | lys_node_free(sub); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1882 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1883 | |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1884 | if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 1885 | free(node->features); |
| 1886 | lydict_remove(ctx, node->name); |
| 1887 | lydict_remove(ctx, node->dsc); |
| 1888 | lydict_remove(ctx, node->ref); |
| 1889 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1890 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1891 | /* specific part */ |
| 1892 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1893 | case LYS_CONTAINER: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1894 | lys_container_free(ctx, (struct lys_node_container *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1895 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1896 | case LYS_CHOICE: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1897 | lys_when_free(ctx, ((struct lys_node_choice *)node)->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1898 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1899 | case LYS_LEAF: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1900 | lys_leaf_free(ctx, (struct lys_node_leaf *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1901 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1902 | case LYS_LEAFLIST: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1903 | lys_leaflist_free(ctx, (struct lys_node_leaflist *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1904 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1905 | case LYS_LIST: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1906 | lys_list_free(ctx, (struct lys_node_list *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1907 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1908 | case LYS_ANYXML: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1909 | lys_anyxml_free(ctx, (struct lys_node_anyxml *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1910 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1911 | case LYS_USES: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1912 | lys_uses_free(ctx, (struct lys_node_uses *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1913 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1914 | case LYS_CASE: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1915 | lys_when_free(ctx, ((struct lys_node_case *)node)->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1916 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1917 | case LYS_AUGMENT: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1918 | /* do nothing */ |
| 1919 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1920 | case LYS_GROUPING: |
| 1921 | case LYS_RPC: |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1922 | case LYS_NOTIF: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1923 | lys_grp_free(ctx, (struct lys_node_grp *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1924 | break; |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1925 | |
| 1926 | case LYS_INPUT: |
| 1927 | case LYS_OUTPUT: |
| 1928 | lys_rpc_inout_free(ctx, (struct lys_node_rpc_inout *)node); |
| 1929 | break; |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 1930 | case LYS_UNKNOWN: |
| 1931 | LOGINT; |
| 1932 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1933 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1934 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1935 | /* again common part */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1936 | lys_node_unlink(node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1937 | free(node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1938 | } |
| 1939 | |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 1940 | struct lys_module * |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 1941 | 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] | 1942 | { |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 1943 | int i, match; |
| 1944 | |
| 1945 | assert(prefix || name); |
| 1946 | if (prefix && !pref_len) { |
| 1947 | pref_len = strlen(prefix); |
| 1948 | } |
| 1949 | if (name && !name_len) { |
| 1950 | name_len = strlen(name); |
| 1951 | } |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 1952 | |
Michal Vasko | d8da86b | 2015-10-21 13:35:37 +0200 | [diff] [blame] | 1953 | if ((!prefix || (!strncmp(module->prefix, prefix, pref_len) && !module->prefix[pref_len])) |
| 1954 | && (!name || (!strncmp(module->name, name, name_len) && !module->name[name_len]))) { |
| 1955 | return module; |
| 1956 | } |
| 1957 | |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 1958 | for (i = 0; i < module->imp_size; ++i) { |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 1959 | match = 0; |
| 1960 | if (!prefix || (!strncmp(module->imp[i].prefix, prefix, pref_len) && !module->imp[i].prefix[pref_len])) { |
| 1961 | match = 1; |
| 1962 | } |
| 1963 | if (match && (!name |
| 1964 | || (!strncmp(module->imp[i].module->name, name, name_len) && !module->imp[i].module->name[name_len]))) { |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 1965 | return module->imp[i].module; |
| 1966 | } |
| 1967 | } |
| 1968 | |
| 1969 | return NULL; |
| 1970 | } |
| 1971 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 1972 | /* free_int_mods - flag whether to free the internal modules as well */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1973 | static void |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 1974 | module_free_common(struct lys_module *module, int free_int_mods) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1975 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1976 | struct ly_ctx *ctx; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1977 | unsigned int i; |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 1978 | int j, l; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1979 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1980 | assert(module->ctx); |
| 1981 | ctx = module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1982 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 1983 | /* as first step, free the imported modules */ |
| 1984 | for (i = 0; i < module->imp_size; i++) { |
Michal Vasko | a76ee15 | 2015-08-17 15:38:22 +0200 | [diff] [blame] | 1985 | /* do not free internal modules */ |
Michal Vasko | 18bd09b | 2015-08-19 15:07:00 +0200 | [diff] [blame] | 1986 | if (!free_int_mods) { |
| 1987 | for (j = 0; j < int_mods.count; ++j) { |
Michal Vasko | d83c03a | 2015-08-21 09:09:24 +0200 | [diff] [blame] | 1988 | 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] | 1989 | && module->imp[i].module->rev |
| 1990 | && !strcmp(int_mods.modules[j].revision, module->imp[i].module->rev[0].date)) { |
| 1991 | break; |
| 1992 | } |
Michal Vasko | a76ee15 | 2015-08-17 15:38:22 +0200 | [diff] [blame] | 1993 | } |
Michal Vasko | 18bd09b | 2015-08-19 15:07:00 +0200 | [diff] [blame] | 1994 | if (j < int_mods.count) { |
| 1995 | continue; |
| 1996 | } |
Michal Vasko | a76ee15 | 2015-08-17 15:38:22 +0200 | [diff] [blame] | 1997 | } |
| 1998 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 1999 | /* get the imported module from the context and then free, |
| 2000 | * this check is necessary because the imported module can |
| 2001 | * be already removed |
| 2002 | */ |
| 2003 | l = ctx->models.used; |
| 2004 | for (j = 0; j < l; j++) { |
| 2005 | if (ctx->models.list[j] == module->imp[i].module) { |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2006 | lys_free(module->imp[i].module, free_int_mods); |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2007 | break; |
| 2008 | } |
| 2009 | } |
| 2010 | } |
| 2011 | free(module->imp); |
| 2012 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2013 | while (module->data) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2014 | lys_node_free(module->data); |
Radek Krejci | 2118196 | 2015-06-30 14:11:00 +0200 | [diff] [blame] | 2015 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2016 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2017 | lydict_remove(ctx, module->dsc); |
| 2018 | lydict_remove(ctx, module->ref); |
| 2019 | lydict_remove(ctx, module->org); |
| 2020 | lydict_remove(ctx, module->contact); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2021 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2022 | /* revisions */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2023 | for (i = 0; i < module->rev_size; i++) { |
| 2024 | lydict_remove(ctx, module->rev[i].dsc); |
| 2025 | lydict_remove(ctx, module->rev[i].ref); |
| 2026 | } |
| 2027 | free(module->rev); |
Radek 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 | /* identities */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2030 | for (i = 0; i < module->ident_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2031 | lys_ident_free(ctx, &module->ident[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2032 | } |
| 2033 | module->ident_size = 0; |
| 2034 | free(module->ident); |
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 | /* typedefs */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2037 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2038 | lys_tpdf_free(ctx, &module->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2039 | } |
| 2040 | free(module->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2041 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2042 | /* include */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2043 | for (i = 0; i < module->inc_size; i++) { |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2044 | lys_submodule_free(module->inc[i].submodule, free_int_mods); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2045 | } |
| 2046 | free(module->inc); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2047 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2048 | /* augment */ |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2049 | for (i = 0; i < module->augment_size; i++) { |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2050 | lys_augment_free(ctx, module->augment[i]); |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2051 | } |
| 2052 | free(module->augment); |
| 2053 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2054 | /* features */ |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2055 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2056 | lys_feature_free(ctx, &module->features[i]); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2057 | } |
| 2058 | free(module->features); |
| 2059 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2060 | /* deviations */ |
| 2061 | for (i = 0; i < module->deviation_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2062 | lys_deviation_free(ctx, &module->deviation[i]); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2063 | } |
| 2064 | free(module->deviation); |
| 2065 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2066 | lydict_remove(ctx, module->name); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2067 | } |
| 2068 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2069 | void |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2070 | lys_submodule_free(struct lys_submodule *submodule, int free_int_mods) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2071 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2072 | if (!submodule) { |
| 2073 | return; |
| 2074 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2075 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2076 | submodule->inc_size = 0; |
| 2077 | free(submodule->inc); |
| 2078 | submodule->inc = NULL; |
Radek Krejci | f388693 | 2015-06-04 17:36:06 +0200 | [diff] [blame] | 2079 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2080 | /* common part with struct ly_module */ |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2081 | module_free_common((struct lys_module *)submodule, free_int_mods); |
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 | /* no specific items to free */ |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2084 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2085 | free(submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2086 | } |
| 2087 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2088 | struct lys_node * |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 2089 | 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] | 2090 | struct unres_schema *unres) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2091 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2092 | struct lys_node *retval = NULL, *aux, *child; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2093 | struct ly_ctx *ctx = module->ctx; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2094 | int i, j, rc; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2095 | |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2096 | struct lys_node_container *cont = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2097 | struct lys_node_container *cont_orig = (struct lys_node_container *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2098 | struct lys_node_choice *choice = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2099 | struct lys_node_choice *choice_orig = (struct lys_node_choice *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2100 | struct lys_node_leaf *leaf = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2101 | struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2102 | struct lys_node_leaflist *llist = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2103 | struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2104 | struct lys_node_list *list = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2105 | struct lys_node_list *list_orig = (struct lys_node_list *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2106 | struct lys_node_anyxml *anyxml = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2107 | struct lys_node_anyxml *anyxml_orig = (struct lys_node_anyxml *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2108 | struct lys_node_uses *uses = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2109 | struct lys_node_uses *uses_orig = (struct lys_node_uses *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2110 | struct lys_node_grp *grp = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2111 | struct lys_node_grp *grp_orig = (struct lys_node_grp *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2112 | struct lys_node_rpc *rpc = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2113 | struct lys_node_rpc *rpc_orig = (struct lys_node_rpc *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2114 | struct lys_node_rpc_inout *io = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2115 | 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] | 2116 | struct lys_node_rpc *ntf = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2117 | struct lys_node_rpc *ntf_orig = (struct lys_node_rpc *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2118 | struct lys_node_case *cs = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2119 | struct lys_node_case *cs_orig = (struct lys_node_case *)node; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2120 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2121 | /* we cannot just duplicate memory since the strings are stored in |
| 2122 | * dictionary and we need to update dictionary counters. |
| 2123 | */ |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2124 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2125 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2126 | case LYS_CONTAINER: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2127 | cont = calloc(1, sizeof *cont); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2128 | retval = (struct lys_node *)cont; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2129 | break; |
| 2130 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2131 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2132 | choice = calloc(1, sizeof *choice); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2133 | retval = (struct lys_node *)choice; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2134 | break; |
| 2135 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2136 | case LYS_LEAF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2137 | leaf = calloc(1, sizeof *leaf); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2138 | retval = (struct lys_node *)leaf; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2139 | break; |
| 2140 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2141 | case LYS_LEAFLIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2142 | llist = calloc(1, sizeof *llist); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2143 | retval = (struct lys_node *)llist; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2144 | break; |
| 2145 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2146 | case LYS_LIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2147 | list = calloc(1, sizeof *list); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2148 | retval = (struct lys_node *)list; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2149 | break; |
| 2150 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2151 | case LYS_ANYXML: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2152 | anyxml = calloc(1, sizeof *anyxml); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2153 | retval = (struct lys_node *)anyxml; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2154 | break; |
| 2155 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2156 | case LYS_USES: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2157 | uses = calloc(1, sizeof *uses); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2158 | retval = (struct lys_node *)uses; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2159 | break; |
| 2160 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2161 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2162 | cs = calloc(1, sizeof *cs); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2163 | retval = (struct lys_node *)cs; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2164 | break; |
| 2165 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2166 | case LYS_GROUPING: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2167 | grp = calloc(1, sizeof *grp); |
| 2168 | retval = (struct lys_node *)grp; |
| 2169 | break; |
| 2170 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2171 | case LYS_RPC: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2172 | rpc = calloc(1, sizeof *rpc); |
| 2173 | retval = (struct lys_node *)rpc; |
| 2174 | break; |
| 2175 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2176 | case LYS_INPUT: |
| 2177 | case LYS_OUTPUT: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2178 | io = calloc(1, sizeof *io); |
| 2179 | retval = (struct lys_node *)io; |
| 2180 | break; |
| 2181 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2182 | case LYS_NOTIF: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2183 | ntf = calloc(1, sizeof *ntf); |
| 2184 | retval = (struct lys_node *)ntf; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2185 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2186 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2187 | default: |
Michal Vasko | d23ce59 | 2015-08-06 09:55:37 +0200 | [diff] [blame] | 2188 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2189 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2190 | } |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame] | 2191 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2192 | /* |
| 2193 | * duplicate generic part of the structure |
| 2194 | */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2195 | retval->name = lydict_insert(ctx, node->name, 0); |
| 2196 | retval->dsc = lydict_insert(ctx, node->dsc, 0); |
| 2197 | retval->ref = lydict_insert(ctx, node->ref, 0); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 2198 | retval->nacm = nacm; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2199 | retval->flags = node->flags; |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2200 | if (!(retval->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2201 | /* set parent's config flag */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2202 | retval->flags |= flags & LYS_CONFIG_MASK; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2203 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2204 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2205 | retval->module = module; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2206 | retval->nodetype = node->nodetype; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2207 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2208 | retval->prev = retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2209 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2210 | retval->features_size = node->features_size; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2211 | retval->features = calloc(retval->features_size, sizeof *retval->features); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2212 | for (i = 0; i < node->features_size; ++i) { |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 2213 | 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] | 2214 | retval->features[i] = node->features[i]; |
Michal Vasko | d23ce59 | 2015-08-06 09:55:37 +0200 | [diff] [blame] | 2215 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2216 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2217 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2218 | if (recursive) { |
| 2219 | /* go recursively */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2220 | LY_TREE_FOR(node->child, child) { |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 2221 | aux = lys_node_dup(module, child, retval->flags, retval->nacm, 1, unres); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 2222 | if (!aux || lys_node_addchild(retval, NULL, aux)) { |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2223 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2224 | } |
| 2225 | } |
| 2226 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2227 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2228 | /* |
| 2229 | * duplicate specific part of the structure |
| 2230 | */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2231 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2232 | case LYS_CONTAINER: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2233 | if (cont_orig->when) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2234 | cont->when = lys_when_dup(ctx, cont_orig->when); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2235 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2236 | cont->presence = lydict_insert(ctx, cont_orig->presence, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2237 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2238 | cont->must_size = cont_orig->must_size; |
| 2239 | cont->tpdf_size = cont_orig->tpdf_size; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2240 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2241 | cont->must = lys_restr_dup(ctx, cont_orig->must, cont->must_size); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2242 | 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] | 2243 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2244 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2245 | case LYS_CHOICE: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2246 | if (choice_orig->when) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2247 | choice->when = lys_when_dup(ctx, choice_orig->when); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | if (choice_orig->dflt) { |
Michal Vasko | 165dc4a | 2015-10-23 09:44:27 +0200 | [diff] [blame] | 2251 | 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] | 2252 | | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2253 | | LYS_LIST, &choice->dflt); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2254 | if (rc) { |
| 2255 | if (rc == EXIT_FAILURE) { |
| 2256 | LOGINT; |
| 2257 | } |
| 2258 | goto error; |
| 2259 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2260 | } else { |
Michal Vasko | c68a2a2 | 2015-08-06 09:57:24 +0200 | [diff] [blame] | 2261 | /* useless to check return value, we don't know whether |
| 2262 | * there really wasn't any default defined or it just hasn't |
| 2263 | * been resolved, we just hope for the best :) |
| 2264 | */ |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 2265 | unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2266 | } |
| 2267 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2268 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2269 | case LYS_LEAF: |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 2270 | if (lys_type_dup(module, node->parent, &(leaf->type), &(leaf_orig->type), unres)) { |
| 2271 | goto error; |
| 2272 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2273 | leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0); |
| 2274 | |
| 2275 | if (leaf_orig->dflt) { |
| 2276 | leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0); |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 2277 | 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] | 2278 | goto error; |
| 2279 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2280 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2281 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2282 | leaf->must_size = leaf_orig->must_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2283 | leaf->must = lys_restr_dup(ctx, leaf_orig->must, leaf->must_size); |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2284 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2285 | if (leaf_orig->when) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2286 | leaf->when = lys_when_dup(ctx, leaf_orig->when); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2287 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2288 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2289 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2290 | case LYS_LEAFLIST: |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 2291 | if (lys_type_dup(module, node->parent, &(llist->type), &(llist_orig->type), unres)) { |
| 2292 | goto error; |
| 2293 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2294 | llist->units = lydict_insert(module->ctx, llist_orig->units, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2295 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2296 | llist->min = llist_orig->min; |
| 2297 | llist->max = llist_orig->max; |
| 2298 | |
| 2299 | llist->must_size = llist_orig->must_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2300 | llist->must = lys_restr_dup(ctx, llist_orig->must, llist->must_size); |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2301 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2302 | if (llist_orig->when) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2303 | llist->when = lys_when_dup(ctx, llist_orig->when); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2304 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2305 | break; |
| 2306 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2307 | case LYS_LIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2308 | list->min = list_orig->min; |
| 2309 | list->max = list_orig->max; |
| 2310 | |
| 2311 | list->must_size = list_orig->must_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2312 | list->must = lys_restr_dup(ctx, list_orig->must, list->must_size); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2313 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame^] | 2314 | list->tpdf_size = list_orig->tpdf_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2315 | 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] | 2316 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame^] | 2317 | list->keys_size = list_orig->keys_size; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2318 | if (list->keys_size) { |
| 2319 | list->keys = calloc(list->keys_size, sizeof *list->keys); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2320 | |
| 2321 | /* we managed to resolve it before, resolve it again manually */ |
| 2322 | if (list_orig->keys[0]) { |
| 2323 | for (i = 0; i < list->keys_size; ++i) { |
Michal Vasko | 165dc4a | 2015-10-23 09:44:27 +0200 | [diff] [blame] | 2324 | rc = lys_get_sibling(list->module, list->child, NULL, 0, list_orig->keys[i]->name, 0, LYS_LEAF, |
| 2325 | (struct lys_node **)&list->keys[i]); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2326 | if (rc) { |
| 2327 | if (rc == EXIT_FAILURE) { |
| 2328 | LOGINT; |
| 2329 | } |
| 2330 | goto error; |
| 2331 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2332 | } |
| 2333 | /* it was not resolved yet, add unres copy */ |
| 2334 | } else { |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 2335 | if (unres_schema_dup(module, unres, list_orig, UNRES_LIST_KEYS, list)) { |
Michal Vasko | d23ce59 | 2015-08-06 09:55:37 +0200 | [diff] [blame] | 2336 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2337 | goto error; |
Michal Vasko | d23ce59 | 2015-08-06 09:55:37 +0200 | [diff] [blame] | 2338 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2339 | } |
| 2340 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2341 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame^] | 2342 | list->unique_size = list_orig->unique_size; |
| 2343 | list->unique = malloc(list->unique_size * sizeof *list->unique); |
| 2344 | for (i = 0; i < list->unique_size; ++i) { |
| 2345 | list->unique[i].expr_size = list_orig->unique[i].expr_size; |
| 2346 | list->unique[i].expr = malloc(list->unique[i].expr_size * sizeof *list->unique[i].expr); |
| 2347 | for (j = 0; j < list->unique[i].expr_size; j++) { |
| 2348 | list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0); |
| 2349 | |
| 2350 | /* if it stays in unres list, duplicate it also there */ |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 2351 | 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] | 2352 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2353 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2354 | |
| 2355 | if (list_orig->when) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2356 | list->when = lys_when_dup(ctx, list_orig->when); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2357 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2358 | break; |
| 2359 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2360 | case LYS_ANYXML: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2361 | anyxml->must_size = anyxml_orig->must_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2362 | anyxml->must = lys_restr_dup(ctx, anyxml_orig->must, anyxml->must_size); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2363 | |
| 2364 | if (anyxml_orig->when) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2365 | anyxml->when = lys_when_dup(ctx, anyxml_orig->when); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2366 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2367 | break; |
| 2368 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2369 | case LYS_USES: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2370 | uses->grp = uses_orig->grp; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2371 | |
| 2372 | if (uses_orig->when) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2373 | uses->when = lys_when_dup(ctx, uses_orig->when); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2374 | } |
| 2375 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2376 | uses->refine_size = uses_orig->refine_size; |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 2377 | 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] | 2378 | uses->augment_size = uses_orig->augment_size; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 2379 | 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] | 2380 | if (!uses->child) { |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 2381 | 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] | 2382 | goto error; |
| 2383 | } |
Michal Vasko | f8c128d | 2015-08-06 15:25:28 +0200 | [diff] [blame] | 2384 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2385 | break; |
| 2386 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2387 | case LYS_CASE: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2388 | if (cs_orig->when) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2389 | cs->when = lys_when_dup(ctx, cs_orig->when); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2390 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2391 | break; |
| 2392 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2393 | case LYS_GROUPING: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2394 | grp->tpdf_size = grp_orig->tpdf_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2395 | 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] | 2396 | break; |
| 2397 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2398 | case LYS_RPC: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2399 | rpc->tpdf_size = rpc_orig->tpdf_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2400 | 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] | 2401 | break; |
| 2402 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2403 | case LYS_INPUT: |
| 2404 | case LYS_OUTPUT: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2405 | io->tpdf_size = io_orig->tpdf_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2406 | 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] | 2407 | break; |
| 2408 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2409 | case LYS_NOTIF: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2410 | ntf->tpdf_size = ntf_orig->tpdf_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2411 | 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] | 2412 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2413 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2414 | default: |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2415 | /* LY_NODE_AUGMENT */ |
Michal Vasko | d23ce59 | 2015-08-06 09:55:37 +0200 | [diff] [blame] | 2416 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2417 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2418 | } |
| 2419 | |
| 2420 | return retval; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2421 | |
| 2422 | error: |
| 2423 | |
| 2424 | lys_node_free(retval); |
| 2425 | return NULL; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2426 | } |
| 2427 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2428 | void |
| 2429 | lys_free(struct lys_module *module, int free_int_mods) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2430 | { |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2431 | struct ly_ctx *ctx; |
| 2432 | int i; |
| 2433 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2434 | if (!module) { |
| 2435 | return; |
| 2436 | } |
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 | /* remove schema from the context */ |
| 2439 | ctx = module->ctx; |
| 2440 | if (ctx->models.used) { |
| 2441 | for (i = 0; i < ctx->models.used; i++) { |
| 2442 | if (ctx->models.list[i] == module) { |
| 2443 | /* replace the position in the list by the last module in the list */ |
| 2444 | ctx->models.used--; |
| 2445 | ctx->models.list[i] = ctx->models.list[ctx->models.used]; |
| 2446 | ctx->models.list[ctx->models.used] = NULL; |
| 2447 | /* we are done */ |
| 2448 | break; |
| 2449 | } |
| 2450 | } |
| 2451 | } |
| 2452 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2453 | /* common part with struct ly_submodule */ |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2454 | module_free_common(module, free_int_mods); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2455 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2456 | /* specific items to free */ |
| 2457 | lydict_remove(module->ctx, module->ns); |
| 2458 | lydict_remove(module->ctx, module->prefix); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2459 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2460 | free(module); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2461 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2462 | |
| 2463 | /* |
| 2464 | * op: 1 - enable, 0 - disable |
| 2465 | */ |
| 2466 | static int |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2467 | lys_features_change(struct lys_module *module, const char *name, int op) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2468 | { |
| 2469 | int all = 0; |
| 2470 | int i, j, k; |
| 2471 | |
| 2472 | if (!module || !name || !strlen(name)) { |
| 2473 | return EXIT_FAILURE; |
| 2474 | } |
| 2475 | |
| 2476 | if (!strcmp(name, "*")) { |
| 2477 | /* enable all */ |
| 2478 | all = 1; |
| 2479 | } |
| 2480 | |
| 2481 | /* module itself */ |
| 2482 | for (i = 0; i < module->features_size; i++) { |
| 2483 | if (all || !strcmp(module->features[i].name, name)) { |
| 2484 | if (op) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2485 | module->features[i].flags |= LYS_FENABLED; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2486 | /* enable referenced features (recursion) */ |
| 2487 | for (k = 0; k < module->features[i].features_size; k++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2488 | lys_features_change(module->features[i].features[k]->module, |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2489 | module->features[i].features[k]->name, op); |
| 2490 | } |
| 2491 | } else { |
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 | } |
| 2494 | if (!all) { |
| 2495 | return EXIT_SUCCESS; |
| 2496 | } |
| 2497 | } |
| 2498 | } |
| 2499 | |
| 2500 | /* submodules */ |
| 2501 | for (j = 0; j < module->inc_size; j++) { |
| 2502 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
| 2503 | if (all || !strcmp(module->inc[j].submodule->features[i].name, name)) { |
| 2504 | if (op) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2505 | module->inc[j].submodule->features[i].flags |= LYS_FENABLED; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2506 | } else { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2507 | module->inc[j].submodule->features[i].flags &= ~LYS_FENABLED; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2508 | } |
| 2509 | if (!all) { |
| 2510 | return EXIT_SUCCESS; |
| 2511 | } |
| 2512 | } |
| 2513 | } |
| 2514 | } |
| 2515 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2516 | /* TODO submodules of submodules ... */ |
| 2517 | |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2518 | if (all) { |
| 2519 | return EXIT_SUCCESS; |
| 2520 | } else { |
| 2521 | return EXIT_FAILURE; |
| 2522 | } |
| 2523 | } |
| 2524 | |
| 2525 | API int |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2526 | lys_features_enable(struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2527 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2528 | return lys_features_change(module, feature, 1); |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2529 | } |
| 2530 | |
| 2531 | API int |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2532 | lys_features_disable(struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2533 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2534 | return lys_features_change(module, feature, 0); |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2535 | } |
| 2536 | |
| 2537 | API int |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2538 | lys_features_state(struct lys_module *module, const char *feature) |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2539 | { |
| 2540 | int i, j; |
| 2541 | |
| 2542 | if (!module || !feature) { |
| 2543 | return -1; |
| 2544 | } |
| 2545 | |
| 2546 | /* search for the specified feature */ |
| 2547 | /* module itself */ |
| 2548 | for (i = 0; i < module->features_size; i++) { |
| 2549 | if (!strcmp(feature, module->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2550 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2551 | return 1; |
| 2552 | } else { |
| 2553 | return 0; |
| 2554 | } |
| 2555 | } |
| 2556 | } |
| 2557 | |
| 2558 | /* submodules */ |
| 2559 | for (j = 0; j < module->inc_size; j++) { |
| 2560 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
| 2561 | if (!strcmp(feature, module->inc[j].submodule->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2562 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2563 | return 1; |
| 2564 | } else { |
| 2565 | return 0; |
| 2566 | } |
| 2567 | } |
| 2568 | } |
| 2569 | } |
| 2570 | |
| 2571 | /* TODO submodules of submodules ... */ |
| 2572 | |
| 2573 | /* feature definition not found */ |
| 2574 | return -1; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2575 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2576 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2577 | API const char ** |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2578 | lys_features_list(struct lys_module *module, uint8_t **states) |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2579 | { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2580 | const char **result = NULL; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2581 | int i, j; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2582 | unsigned int count; |
| 2583 | |
| 2584 | if (!module) { |
| 2585 | return NULL; |
| 2586 | } |
| 2587 | |
| 2588 | count = module->features_size; |
| 2589 | for (i = 0; i < module->inc_size; i++) { |
| 2590 | count += module->inc[i].submodule->features_size; |
| 2591 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2592 | result = malloc((count + 1) * sizeof *result); |
| 2593 | if (states) { |
| 2594 | *states = malloc((count + 1) * sizeof **states); |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2595 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2596 | count = 0; |
| 2597 | |
| 2598 | /* module itself */ |
| 2599 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2600 | result[count] = module->features[i].name; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2601 | if (states) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2602 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2603 | (*states)[count] = 1; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2604 | } else { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2605 | (*states)[count] = 0; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2606 | } |
| 2607 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2608 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | /* submodules */ |
| 2612 | for (j = 0; j < module->inc_size; j++) { |
| 2613 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2614 | result[count] = module->inc[j].submodule->features[i].name; |
Radek Krejci | 374b94e | 2015-08-13 09:44:22 +0200 | [diff] [blame] | 2615 | if (states) { |
| 2616 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
| 2617 | (*states)[count] = 1; |
| 2618 | } else { |
| 2619 | (*states)[count] = 0; |
| 2620 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2621 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2622 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2623 | } |
| 2624 | } |
| 2625 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2626 | /* TODO submodules of submodules ... */ |
| 2627 | |
| 2628 | /* terminating NULL byte */ |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2629 | result[count] = NULL; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2630 | |
| 2631 | return result; |
| 2632 | } |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 2633 | |
| 2634 | API struct lys_node * |
| 2635 | lys_parent(struct lys_node *node) |
| 2636 | { |
| 2637 | if (!node || !node->parent) { |
| 2638 | return NULL; |
| 2639 | } |
| 2640 | |
| 2641 | if (node->parent->nodetype == LYS_AUGMENT) { |
| 2642 | return ((struct lys_node_augment *)node->parent)->target; |
| 2643 | } |
| 2644 | |
| 2645 | return node->parent; |
| 2646 | } |