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