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