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