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 | */ |
| 21 | |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 22 | #include <assert.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <sys/stat.h> |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 26 | #include <string.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 27 | |
| 28 | #include "common.h" |
| 29 | #include "context.h" |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 30 | #include "parser.h" |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 31 | #include "xml.h" |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 32 | #include "tree_internal.h" |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 33 | |
| 34 | void ly_submodule_free(struct ly_submodule *submodule); |
| 35 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 36 | static const char * |
| 37 | strnodetype(LY_NODE_TYPE type) |
Radek Krejci | b7155b5 | 2015-06-10 17:03:01 +0200 | [diff] [blame] | 38 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 39 | switch (type) { |
| 40 | case LY_NODE_AUGMENT: |
| 41 | return "augment"; |
| 42 | case LY_NODE_CONTAINER: |
| 43 | return "container"; |
| 44 | case LY_NODE_CHOICE: |
| 45 | return "choice"; |
| 46 | case LY_NODE_LEAF: |
| 47 | return "leaf"; |
| 48 | case LY_NODE_LEAFLIST: |
| 49 | return "leaf-list"; |
| 50 | case LY_NODE_LIST: |
| 51 | return "list"; |
| 52 | case LY_NODE_ANYXML: |
| 53 | return "anyxml"; |
| 54 | case LY_NODE_USES: |
| 55 | return "uses"; |
| 56 | case LY_NODE_GROUPING: |
| 57 | return "grouping"; |
| 58 | case LY_NODE_CASE: |
| 59 | return "case"; |
| 60 | case LY_NODE_INPUT: |
| 61 | return "input"; |
| 62 | case LY_NODE_OUTPUT: |
| 63 | return "output"; |
| 64 | case LY_NODE_NOTIF: |
| 65 | return "notification"; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 66 | case LY_NODE_RPC: |
| 67 | return "rpc"; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 68 | } |
Radek Krejci | b7155b5 | 2015-06-10 17:03:01 +0200 | [diff] [blame] | 69 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 70 | return NULL; |
Radek Krejci | b7155b5 | 2015-06-10 17:03:01 +0200 | [diff] [blame] | 71 | } |
| 72 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 73 | struct ly_mnode_leaf * |
| 74 | find_leaf(struct ly_mnode *parent, const char *name, int len) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 75 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 76 | struct ly_mnode *child; |
| 77 | struct ly_mnode_leaf *result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 78 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 79 | if (!len) { |
| 80 | len = strlen(name); |
| 81 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 82 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 83 | LY_TREE_FOR(parent->child, child) { |
| 84 | switch (child->nodetype) { |
| 85 | case LY_NODE_LEAF: |
| 86 | /* direct check */ |
| 87 | if (child->name == name || (!strncmp(child->name, name, len) && !child->name[len])) { |
| 88 | return (struct ly_mnode_leaf *)child; |
| 89 | } |
| 90 | break; |
| 91 | case LY_NODE_USES: |
| 92 | /* search recursively */ |
| 93 | result = find_leaf(child, name, len); |
| 94 | if (result) { |
| 95 | return result; |
| 96 | } |
| 97 | break; |
| 98 | default: |
| 99 | /* ignore */ |
| 100 | break; |
| 101 | } |
| 102 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 103 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 104 | return NULL; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 105 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 106 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 107 | void |
| 108 | ly_mnode_unlink(struct ly_mnode *node) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 109 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 110 | struct ly_mnode *parent, *first; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 111 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 112 | if (!node) { |
| 113 | return; |
| 114 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 115 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 116 | /* unlink from data model if necessary */ |
| 117 | if (node->module) { |
| 118 | if (node->module->data == node) { |
| 119 | node->module->data = node->next; |
Radek Krejci | 2118196 | 2015-06-30 14:11:00 +0200 | [diff] [blame] | 120 | } else if (node->module->rpc == node) { |
| 121 | node->module->rpc = node->next; |
| 122 | } else if (node->module->notif == node) { |
| 123 | node->module->notif = node->next; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 124 | } |
| 125 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 126 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 127 | /* store pointers to important nodes */ |
| 128 | parent = node->parent; |
| 129 | if (parent && !parent->nodetype) { |
| 130 | /* handle augments - first, unlink it from the augment parent ... */ |
| 131 | if (parent->child == node) { |
| 132 | parent->child = node->next; |
| 133 | } |
| 134 | /* and then continue with the target parent */ |
| 135 | parent = ((struct ly_augment *)parent)->target; |
| 136 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 137 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 138 | /* unlink from parent */ |
| 139 | if (parent) { |
| 140 | if (parent->child == node) { |
| 141 | parent->child = node->next; |
| 142 | } |
| 143 | node->parent = NULL; |
| 144 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 145 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 146 | /* unlink from siblings */ |
| 147 | if (node->prev == node) { |
| 148 | /* there are no more siblings */ |
| 149 | return; |
| 150 | } |
| 151 | if (node->next) { |
| 152 | node->next->prev = node->prev; |
| 153 | } else { |
| 154 | /* unlinking the last element */ |
| 155 | if (parent) { |
| 156 | first = parent->child; |
| 157 | } else { |
| 158 | first = node; |
| 159 | while (node->prev->next) { |
| 160 | first = node->prev; |
| 161 | } |
| 162 | } |
| 163 | first->prev = node->prev; |
| 164 | } |
| 165 | if (node->prev->next) { |
| 166 | node->prev->next = node->next; |
| 167 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 168 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 169 | /* clean up the unlinked element */ |
| 170 | node->next = NULL; |
| 171 | node->prev = node; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Add child model node at the end of the parent's child list. |
| 176 | * If the child is connected somewhere (has a parent), it is completely |
| 177 | * unlinked and none of the following conditions applies. |
| 178 | * If the child has prev sibling(s), they are ignored (child is added at the |
| 179 | * end of the child list). |
| 180 | * If the child has next sibling(s), all of them are connected with the parent. |
| 181 | */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 182 | int |
| 183 | ly_mnode_addchild(struct ly_mnode *parent, struct ly_mnode *child) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 184 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 185 | struct ly_mnode *last; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 186 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 187 | assert(parent); |
| 188 | assert(child); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 189 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 190 | /* checks */ |
| 191 | switch (parent->nodetype) { |
| 192 | case LY_NODE_CONTAINER: |
| 193 | case LY_NODE_LIST: |
| 194 | case LY_NODE_GROUPING: |
| 195 | case LY_NODE_USES: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 196 | case LY_NODE_INPUT: |
| 197 | case LY_NODE_OUTPUT: |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 198 | case LY_NODE_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 199 | if (!(child->nodetype & |
| 200 | (LY_NODE_ANYXML | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_GROUPING | LY_NODE_LEAF | |
| 201 | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_USES))) { |
| 202 | LOGVAL(VE_SPEC, 0, "Unexpected substatement \"%s\" in \"%s\" (%s).", |
| 203 | strnodetype(child->nodetype), strnodetype(parent->nodetype), parent->name); |
| 204 | return EXIT_FAILURE; |
| 205 | } |
| 206 | break; |
| 207 | case LY_NODE_CHOICE: |
| 208 | if (!(child->nodetype & |
| 209 | (LY_NODE_ANYXML | LY_NODE_CASE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST))) { |
| 210 | LOGVAL(VE_SPEC, 0, "Unexpected substatement \"%s\" in \"choice\" %s.", |
| 211 | strnodetype(child->nodetype), parent->name); |
| 212 | return EXIT_FAILURE; |
| 213 | } |
| 214 | break; |
| 215 | case LY_NODE_CASE: |
| 216 | if (!(child->nodetype & |
| 217 | (LY_NODE_ANYXML | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_USES))) { |
| 218 | LOGVAL(VE_SPEC, 0, "Unexpected substatement \"%s\" in \"case\" %s.", |
| 219 | strnodetype(child->nodetype), parent->name); |
| 220 | return EXIT_FAILURE; |
| 221 | } |
| 222 | break; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 223 | case LY_NODE_RPC: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 224 | if (!(child->nodetype & (LY_NODE_INPUT | LY_NODE_OUTPUT | LY_NODE_GROUPING))) { |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 225 | LOGVAL(VE_SPEC, 0, "Unexpected substatement \"%s\" in \"rpc\" %s.", |
| 226 | strnodetype(child->nodetype), parent->name); |
| 227 | return EXIT_FAILURE; |
| 228 | } |
| 229 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 230 | case LY_NODE_LEAF: |
| 231 | case LY_NODE_LEAFLIST: |
| 232 | case LY_NODE_ANYXML: |
| 233 | LOGVAL(VE_SPEC, 0, "The \"%s\" statement (%s) cannot have any substatement.", |
| 234 | strnodetype(parent->nodetype), parent->name); |
| 235 | return EXIT_FAILURE; |
| 236 | case LY_NODE_AUGMENT: |
| 237 | LOGVAL(VE_SPEC, 0, "Internal error (%s:%d)", __FILE__, __LINE__); |
| 238 | return EXIT_FAILURE; |
| 239 | } |
Radek Krejci | b7155b5 | 2015-06-10 17:03:01 +0200 | [diff] [blame] | 240 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 241 | if (child->parent) { |
| 242 | ly_mnode_unlink(child); |
| 243 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 244 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 245 | if (!parent->child) { |
| 246 | /* the only/first child of the parent */ |
| 247 | parent->child = child; |
| 248 | child->parent = parent; |
| 249 | last = child; |
| 250 | } else { |
| 251 | /* add a new child at the end of parent's child list */ |
| 252 | last = parent->child->prev; |
| 253 | last->next = child; |
| 254 | child->prev = last; |
| 255 | } |
| 256 | while (last->next) { |
| 257 | last = last->next; |
| 258 | last->parent = parent; |
| 259 | } |
| 260 | parent->child->prev = last; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 261 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 262 | return EXIT_SUCCESS; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 263 | } |
| 264 | |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 265 | /* |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 266 | * id - schema-nodeid |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 267 | * |
| 268 | * node_type - LY_NODE_AUGMENT (searches also RPCs and notifications) |
| 269 | * - LY_NODE_USES (the caller is actually either an augment or refine in a uses, only |
| 270 | * descendant-schema-nodeid allowed, ".." not allowed) |
| 271 | * - LY_NODE_CHOICE (search only start->child, only descendant-schema-nodeid allowed) |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 272 | */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 273 | struct ly_mnode * |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 274 | resolve_schema_nodeid(const char *id, struct ly_mnode *start, struct ly_module *mod, LY_NODE_TYPE node_type) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 275 | { |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 276 | const char *name, *prefix, *ptr; |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 277 | struct ly_mnode *sibling; |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 278 | struct ly_submodule *sub_mod; |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 279 | uint32_t i, j, nam_len, pref_len; |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 280 | /* 0 - in module, 1 - in 1st submodule, 2 - in second submodule, ... */ |
| 281 | uint8_t in_submod; |
| 282 | /* 0 - in data, 1 - in RPCs, 2 - in notifications */ |
| 283 | uint8_t in_mod_part; |
| 284 | int found; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 285 | |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 286 | assert(mod); |
| 287 | assert(id); |
| 288 | |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 289 | if (id[0] == '/') { |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 290 | if (node_type & (LY_NODE_USES | LY_NODE_CHOICE)) { |
| 291 | return NULL; |
| 292 | } |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 293 | ptr = strchr(id+1, '/'); |
| 294 | prefix = id+1; |
| 295 | } else { |
| 296 | ptr = strchr(id, '/'); |
| 297 | prefix = id; |
| 298 | } |
| 299 | pref_len = (ptr ? (unsigned)(ptr-prefix) : strlen(prefix)); |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 300 | |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 301 | ptr = strnchr(prefix, ':', pref_len); |
| 302 | /* there is prefix */ |
| 303 | if (ptr) { |
| 304 | nam_len = (pref_len-(ptr-prefix))-1; |
| 305 | pref_len = ptr-prefix; |
| 306 | name = ptr+1; |
| 307 | |
| 308 | /* no prefix used */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 309 | } else { |
| 310 | name = prefix; |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 311 | nam_len = pref_len; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 312 | prefix = NULL; |
| 313 | } |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 314 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 315 | /* absolute-schema-nodeid */ |
| 316 | if (id[0] == '/') { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 317 | start = NULL; |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 318 | found = 0; |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 319 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 320 | /* it is not the local prefix */ |
Michal Vasko | 3b2b041 | 2015-07-01 12:15:02 +0200 | [diff] [blame] | 321 | if (prefix && ((pref_len != strlen(mod->prefix)) || strncmp(prefix, mod->prefix, pref_len))) { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 322 | /* check imports */ |
| 323 | for (i = 0; i < mod->imp_size; i++) { |
Michal Vasko | 3b2b041 | 2015-07-01 12:15:02 +0200 | [diff] [blame] | 324 | if ((pref_len == strlen(mod->imp[i].prefix)) && !strncmp(mod->imp[i].prefix, prefix, pref_len)) { |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 325 | mod = mod->imp[i].module; |
| 326 | start = mod->data; |
| 327 | found = 1; |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 328 | break; |
| 329 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 330 | } |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 331 | |
| 332 | /* no match - check include imports */ |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 333 | if (!found) { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 334 | for (i = 0; i < mod->inc_size; i++) { |
| 335 | sub_mod = mod->inc[i].submodule; |
| 336 | for (j = 0; j < sub_mod->imp_size; j++) { |
Michal Vasko | 3b2b041 | 2015-07-01 12:15:02 +0200 | [diff] [blame] | 337 | if ((pref_len == strlen(sub_mod->imp[j].prefix)) |
| 338 | && !strncmp(sub_mod->imp[j].prefix, prefix, pref_len)) { |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 339 | mod = sub_mod->imp[j].module; |
| 340 | start = mod->data; |
| 341 | found = 1; |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 342 | break; |
| 343 | } |
| 344 | } |
| 345 | |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 346 | if (found) { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 347 | break; |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 352 | /* no match */ |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 353 | if (!found) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 354 | return NULL; |
| 355 | } |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 356 | |
Michal Vasko | 3ea4299 | 2015-06-22 10:51:02 +0200 | [diff] [blame] | 357 | /* it is likely the local prefix (checked later) */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 358 | } else { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 359 | start = mod->data; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 360 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 361 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 362 | /* descendant-schema-nodeid */ |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 363 | } else { |
| 364 | assert(start); |
| 365 | start = start->child; |
| 366 | } |
| 367 | |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 368 | in_submod = 0; |
| 369 | in_mod_part = 0; |
| 370 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 371 | while (1) { |
| 372 | if (!strcmp(name, ".")) { |
| 373 | /* this node - start does not change */ |
| 374 | } else if (!strcmp(name, "..")) { |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 375 | /* ".." is not allowed in refines and augments in uses, there is no need for it there */ |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 376 | if (!start || (node_type == LY_NODE_USES)) { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 377 | return NULL; |
| 378 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 379 | start = start->parent; |
| 380 | } else { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 381 | sibling = NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 382 | LY_TREE_FOR(start, sibling) { |
| 383 | /* match */ |
Michal Vasko | 3ea4299 | 2015-06-22 10:51:02 +0200 | [diff] [blame] | 384 | if (!strncmp(name, sibling->name, nam_len) |
| 385 | && ((sibling->name[nam_len] == '/') || (sibling->name[nam_len] == '\0'))) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 386 | /* prefix check, it's not our own */ |
Michal Vasko | 3b2b041 | 2015-07-01 12:15:02 +0200 | [diff] [blame] | 387 | if (prefix && ((pref_len != strlen(sibling->module->prefix)) |
| 388 | || strncmp(prefix, sibling->module->prefix, pref_len))) { |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 389 | |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 390 | /* in choice and the prefix is not ours, error for sure */ |
| 391 | if (node_type == LY_NODE_CHOICE) { |
| 392 | return NULL; |
| 393 | } |
| 394 | |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 395 | /* import prefix check */ |
| 396 | for (i = 0; i < sibling->module->imp_size; i++) { |
Michal Vasko | 3b2b041 | 2015-07-01 12:15:02 +0200 | [diff] [blame] | 397 | if ((pref_len == strlen(sibling->module->imp[i].prefix)) |
| 398 | && !strncmp(sibling->module->imp[i].prefix, prefix, pref_len) |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 399 | && (sibling->module->imp[i].module == sibling->module)) { |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 400 | break; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /* import prefix check failed */ |
| 405 | if (i == sibling->module->imp_size) { |
| 406 | /* include import prefix check */ |
| 407 | for (i = 0; i < sibling->module->inc_size; i++) { |
| 408 | sub_mod = sibling->module->inc[i].submodule; |
| 409 | for (j = 0; j < sub_mod->imp_size; j++) { |
Michal Vasko | 3b2b041 | 2015-07-01 12:15:02 +0200 | [diff] [blame] | 410 | if ((pref_len == strlen(sub_mod->imp[j].prefix)) |
| 411 | && !strncmp(sub_mod->imp[j].prefix, prefix, pref_len) |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 412 | && (sub_mod->imp[j].module == sibling->module)) { |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 413 | break; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | if (j < sub_mod->imp_size) { |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /* include import prefix check failed too - definite fail */ |
| 423 | if (i == sibling->module->inc_size) { |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 424 | return NULL; |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | /* the result node? */ |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 430 | ptr = name+nam_len; |
| 431 | if (!ptr[0]) { |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 432 | return sibling; |
| 433 | } |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 434 | assert(ptr[0] == '/'); |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 435 | |
| 436 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 437 | if (!sibling->parent || (sibling->parent->nodetype != LY_NODE_CHOICE) |
| 438 | || (sibling->nodetype == LY_NODE_CASE)) { |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 439 | start = sibling->child; |
| 440 | } |
| 441 | break; |
| 442 | } |
| 443 | } |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 444 | |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 445 | /* we did not find the case in direct siblings */ |
| 446 | if (node_type == LY_NODE_CHOICE) { |
| 447 | return NULL; |
| 448 | } |
| 449 | |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 450 | /* no match */ |
| 451 | if (!sibling) { |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 452 | /* on augment search also RPCs and notifications, if we are in top-level */ |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 453 | if ((node_type == LY_NODE_AUGMENT) && (!start || !start->parent)) { |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 454 | /* we have searched all the data nodes */ |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 455 | if (in_mod_part == 0) { |
| 456 | if (!in_submod) { |
| 457 | start = mod->rpc; |
| 458 | } else { |
| 459 | start = mod->inc[in_submod-1].submodule->rpc; |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 460 | } |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 461 | in_mod_part = 1; |
| 462 | continue; |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 463 | } |
| 464 | /* we have searched all the RPCs */ |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 465 | if (in_mod_part == 1) { |
| 466 | if (!in_submod) { |
| 467 | start = mod->notif; |
| 468 | } else { |
| 469 | start = mod->inc[in_submod-1].submodule->notif; |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 470 | } |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 471 | in_mod_part = 2; |
| 472 | continue; |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 473 | } |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 474 | /* we have searched all the notifications, nothing else to search in this module */ |
| 475 | } |
| 476 | |
| 477 | /* are we done with the included submodules as well? */ |
| 478 | if (in_submod-1 == mod->inc_size) { |
Michal Vasko | 86b8445 | 2015-06-22 10:54:05 +0200 | [diff] [blame] | 479 | return NULL; |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 480 | } |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 481 | |
| 482 | /* we aren't, check the next one */ |
| 483 | ++in_submod; |
| 484 | in_mod_part = 0; |
| 485 | start = mod->inc[in_submod-1].submodule->data; |
| 486 | continue; |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
Michal Vasko | a158c68 | 2015-06-17 09:25:38 +0200 | [diff] [blame] | 490 | assert((*(name+nam_len) == '/') || (*(name+nam_len) == '\0')); |
| 491 | |
| 492 | /* make prefix point to the next node name */ |
| 493 | prefix = name+nam_len; |
| 494 | if (!prefix[0]) { |
| 495 | return start; |
| 496 | } |
| 497 | ++prefix; |
| 498 | |
| 499 | /* parse prefix and node name */ |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 500 | ptr = strchr(prefix, '/'); |
| 501 | pref_len = (ptr ? (unsigned)(ptr-prefix) : strlen(prefix)); |
| 502 | ptr = strnchr(prefix, ':', pref_len); |
| 503 | |
| 504 | /* there is prefix */ |
| 505 | if (ptr) { |
Michal Vasko | a158c68 | 2015-06-17 09:25:38 +0200 | [diff] [blame] | 506 | nam_len = (pref_len-(ptr-prefix))-1; |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 507 | pref_len = ptr-prefix; |
| 508 | name = ptr+1; |
| 509 | |
| 510 | /* no prefix used */ |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 511 | } else { |
| 512 | name = prefix; |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 513 | nam_len = pref_len; |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 514 | prefix = NULL; |
| 515 | } |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* cannot get here */ |
| 519 | return NULL; |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 520 | } |
| 521 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 522 | API struct ly_module * |
| 523 | ly_module_read(struct ly_ctx *ctx, const char *data, LY_MINFORMAT format) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 524 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 525 | if (!ctx || !data) { |
| 526 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 527 | return NULL; |
| 528 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 529 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 530 | switch (format) { |
| 531 | case LY_IN_YIN: |
| 532 | return yin_read_module(ctx, data); |
| 533 | case LY_IN_YANG: |
| 534 | default: |
| 535 | /* TODO */ |
| 536 | return NULL; |
| 537 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 538 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 539 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 540 | } |
| 541 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 542 | struct ly_submodule * |
| 543 | ly_submodule_read(struct ly_module *module, const char *data, LY_MINFORMAT format) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 544 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 545 | assert(module); |
| 546 | assert(data); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 547 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 548 | switch (format) { |
| 549 | case LY_IN_YIN: |
| 550 | return yin_read_submodule(module, data); |
| 551 | case LY_IN_YANG: |
| 552 | default: |
| 553 | /* TODO */ |
| 554 | return NULL; |
| 555 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 556 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 557 | return NULL; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 558 | } |
| 559 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 560 | API struct ly_module * |
| 561 | ly_module_read_fd(struct ly_ctx *ctx, int fd, LY_MINFORMAT format) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 562 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 563 | struct ly_module *module; |
| 564 | struct stat sb; |
| 565 | char *addr; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 566 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 567 | if (!ctx || fd < 0) { |
| 568 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 569 | return NULL; |
| 570 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 571 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 572 | /* |
| 573 | * TODO |
| 574 | * This is just a temporary solution to make working automatic search for |
| 575 | * imported modules. This doesn't work e.g. for streams (stdin) |
| 576 | */ |
| 577 | fstat(fd, &sb); |
| 578 | addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 579 | module = ly_module_read(ctx, addr, format); |
| 580 | munmap(addr, sb.st_size); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 581 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 582 | return module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 583 | } |
| 584 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 585 | struct ly_submodule * |
| 586 | ly_submodule_read_fd(struct ly_module *module, int fd, LY_MINFORMAT format) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 587 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 588 | struct ly_submodule *submodule; |
| 589 | struct stat sb; |
| 590 | char *addr; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 591 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 592 | assert(module); |
| 593 | assert(fd >= 0); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 594 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 595 | /* |
| 596 | * TODO |
| 597 | * This is just a temporary solution to make working automatic search for |
| 598 | * imported modules. This doesn't work e.g. for streams (stdin) |
| 599 | */ |
| 600 | fstat(fd, &sb); |
| 601 | addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 602 | /* TODO addr error check */ |
| 603 | submodule = ly_submodule_read(module, addr, format); |
| 604 | munmap(addr, sb.st_size); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 605 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 606 | return submodule; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 607 | |
| 608 | } |
| 609 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 610 | struct ly_restr * |
| 611 | ly_restr_dup(struct ly_ctx *ctx, struct ly_restr *old, int size) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 612 | { |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 613 | struct ly_restr *result; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 614 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 615 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 616 | if (!size) { |
| 617 | return NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 618 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 619 | |
| 620 | result = calloc(size, sizeof *result); |
| 621 | for (i = 0; i < size; i++) { |
| 622 | result[i].expr = lydict_insert(ctx, old[i].expr, 0); |
| 623 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 624 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 625 | result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0); |
| 626 | result[i].emsg = lydict_insert(ctx, old[i].emsg, 0); |
| 627 | } |
| 628 | |
| 629 | return result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 630 | } |
| 631 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 632 | void |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 633 | ly_restr_free(struct ly_ctx *ctx, struct ly_restr *restr) |
| 634 | { |
| 635 | assert(ctx); |
| 636 | if (!restr) { |
| 637 | return; |
| 638 | } |
| 639 | |
| 640 | lydict_remove(ctx, restr->expr); |
| 641 | lydict_remove(ctx, restr->dsc); |
| 642 | lydict_remove(ctx, restr->ref); |
| 643 | lydict_remove(ctx, restr->eapptag); |
| 644 | lydict_remove(ctx, restr->emsg); |
| 645 | } |
| 646 | |
| 647 | void |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 648 | ly_type_dup(struct ly_ctx *ctx, struct ly_type *new, struct ly_type *old) |
| 649 | { |
| 650 | int i; |
| 651 | |
| 652 | new->prefix = lydict_insert(ctx, old->prefix, 0); |
| 653 | new->base = old->base; |
| 654 | new->der = old->der; |
| 655 | |
| 656 | switch (new->base) { |
| 657 | case LY_TYPE_BINARY: |
Radek Krejci | 425adf0 | 2015-06-26 16:23:28 +0200 | [diff] [blame] | 658 | if (old->info.binary.length) { |
| 659 | new->info.binary.length = ly_restr_dup(ctx, old->info.binary.length, 1); |
| 660 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 661 | break; |
| 662 | |
| 663 | case LY_TYPE_BITS: |
| 664 | new->info.bits.count = old->info.bits.count; |
| 665 | if (new->info.bits.count) { |
| 666 | new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit); |
| 667 | for (i = 0; i < new->info.bits.count; i++) { |
| 668 | new->info.bits.bit[i].name = lydict_insert(ctx, old->info.bits.bit[i].name, 0); |
| 669 | new->info.bits.bit[i].dsc = lydict_insert(ctx, old->info.bits.bit[i].dsc, 0); |
| 670 | new->info.bits.bit[i].ref = lydict_insert(ctx, old->info.bits.bit[i].ref, 0); |
| 671 | new->info.bits.bit[i].status = old->info.bits.bit[i].status; |
| 672 | new->info.bits.bit[i].pos = old->info.bits.bit[i].pos; |
| 673 | } |
| 674 | } |
| 675 | break; |
| 676 | |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 677 | case LY_TYPE_DEC64: |
| 678 | new->info.dec64.dig = old->info.dec64.dig; |
| 679 | if (old->info.dec64.range) { |
| 680 | new->info.dec64.range = ly_restr_dup(ctx, old->info.dec64.range, 1); |
| 681 | } |
| 682 | break; |
| 683 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 684 | case LY_TYPE_ENUM: |
| 685 | new->info.enums.count = old->info.enums.count; |
| 686 | if (new->info.enums.count) { |
| 687 | new->info.enums.list = calloc(new->info.enums.count, sizeof *new->info.enums.list); |
| 688 | for (i = 0; i < new->info.enums.count; i++) { |
| 689 | new->info.enums.list[i].name = lydict_insert(ctx, old->info.enums.list[i].name, 0); |
| 690 | new->info.enums.list[i].dsc = lydict_insert(ctx, old->info.enums.list[i].dsc, 0); |
| 691 | new->info.enums.list[i].ref = lydict_insert(ctx, old->info.enums.list[i].ref, 0); |
| 692 | new->info.enums.list[i].status = old->info.enums.list[i].status; |
| 693 | new->info.enums.list[i].value = old->info.enums.list[i].value; |
| 694 | } |
| 695 | } |
| 696 | break; |
| 697 | |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 698 | case LY_TYPE_IDENT: |
| 699 | new->info.ident.ref = old->info.ident.ref; |
| 700 | break; |
| 701 | |
Radek Krejci | af35142 | 2015-06-19 14:49:38 +0200 | [diff] [blame] | 702 | case LY_TYPE_INST: |
| 703 | new->info.inst.req = old->info.inst.req; |
| 704 | break; |
| 705 | |
Radek Krejci | f286013 | 2015-06-20 12:37:20 +0200 | [diff] [blame] | 706 | case LY_TYPE_INT8: |
| 707 | case LY_TYPE_INT16: |
| 708 | case LY_TYPE_INT32: |
| 709 | case LY_TYPE_INT64: |
| 710 | case LY_TYPE_UINT8: |
| 711 | case LY_TYPE_UINT16: |
| 712 | case LY_TYPE_UINT32: |
| 713 | case LY_TYPE_UINT64: |
Radek Krejci | 425adf0 | 2015-06-26 16:23:28 +0200 | [diff] [blame] | 714 | if (old->info.num.range) { |
| 715 | new->info.num.range = ly_restr_dup(ctx, old->info.num.range, 1); |
| 716 | } |
Radek Krejci | f286013 | 2015-06-20 12:37:20 +0200 | [diff] [blame] | 717 | break; |
| 718 | |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 719 | case LY_TYPE_LEAFREF: |
| 720 | new->info.lref.path = lydict_insert(ctx, old->info.lref.path, 0); |
| 721 | break; |
| 722 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 723 | case LY_TYPE_STRING: |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 724 | if (old->info.str.length) { |
| 725 | new->info.str.length = ly_restr_dup(ctx, old->info.str.length, 1); |
| 726 | } |
| 727 | new->info.str.patterns = ly_restr_dup(ctx, old->info.str.patterns, old->info.str.pat_count); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 728 | break; |
| 729 | |
| 730 | default: |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 731 | /* TODO - remove default to make sure that all types are covered */ |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 732 | break; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | void |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 737 | ly_type_free(struct ly_ctx *ctx, struct ly_type *type) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 738 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 739 | int i; |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 740 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 741 | assert(ctx); |
| 742 | if (!type) { |
| 743 | return; |
| 744 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 745 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 746 | lydict_remove(ctx, type->prefix); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 747 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 748 | switch (type->base) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 749 | case LY_TYPE_BINARY: |
| 750 | ly_restr_free(ctx, type->info.binary.length); |
| 751 | free(type->info.binary.length); |
| 752 | break; |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 753 | case LY_TYPE_BITS: |
| 754 | for (i = 0; i < type->info.bits.count; i++) { |
| 755 | lydict_remove(ctx, type->info.bits.bit[i].name); |
| 756 | lydict_remove(ctx, type->info.bits.bit[i].dsc); |
| 757 | lydict_remove(ctx, type->info.bits.bit[i].ref); |
| 758 | } |
| 759 | free(type->info.bits.bit); |
| 760 | break; |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 761 | |
| 762 | case LY_TYPE_DEC64: |
| 763 | ly_restr_free(ctx, type->info.dec64.range); |
| 764 | free(type->info.dec64.range); |
| 765 | break; |
| 766 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 767 | case LY_TYPE_ENUM: |
| 768 | for (i = 0; i < type->info.enums.count; i++) { |
| 769 | lydict_remove(ctx, type->info.enums.list[i].name); |
| 770 | lydict_remove(ctx, type->info.enums.list[i].dsc); |
| 771 | lydict_remove(ctx, type->info.enums.list[i].ref); |
| 772 | } |
| 773 | free(type->info.enums.list); |
| 774 | break; |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 775 | |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 776 | case LY_TYPE_INT8: |
| 777 | case LY_TYPE_INT16: |
| 778 | case LY_TYPE_INT32: |
| 779 | case LY_TYPE_INT64: |
| 780 | case LY_TYPE_UINT8: |
| 781 | case LY_TYPE_UINT16: |
| 782 | case LY_TYPE_UINT32: |
| 783 | case LY_TYPE_UINT64: |
| 784 | ly_restr_free(ctx, type->info.num.range); |
| 785 | free(type->info.num.range); |
| 786 | break; |
| 787 | |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 788 | case LY_TYPE_LEAFREF: |
| 789 | lydict_remove(ctx, type->info.lref.path); |
| 790 | break; |
| 791 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 792 | case LY_TYPE_STRING: |
| 793 | ly_restr_free(ctx, type->info.str.length); |
| 794 | free(type->info.str.length); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 795 | for (i = 0; i < type->info.str.pat_count; i++) { |
| 796 | ly_restr_free(ctx, &type->info.str.patterns[i]); |
| 797 | } |
| 798 | free(type->info.str.patterns); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 799 | break; |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 800 | |
| 801 | case LY_TYPE_IDENT: |
| 802 | /* nothing to do */ |
| 803 | break; |
| 804 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 805 | default: |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 806 | /* TODO - remove default to make sure that all types are covered */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 807 | break; |
| 808 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 809 | } |
| 810 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 811 | struct ly_tpdf * |
| 812 | ly_tpdf_dup(struct ly_ctx *ctx, struct ly_tpdf *old, int size) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 813 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 814 | struct ly_tpdf *result; |
| 815 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 816 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 817 | if (!size) { |
| 818 | return NULL; |
| 819 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 820 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 821 | result = calloc(size, sizeof *result); |
| 822 | for (i = 0; i < size; i++) { |
| 823 | result[i].name = lydict_insert(ctx, old[i].name, 0); |
| 824 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 825 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 826 | result[i].flags = old[i].flags; |
| 827 | result[i].module = old[i].module; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 828 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 829 | ly_type_dup(ctx, &(result[i].type), &(old[i].type)); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 830 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 831 | result[i].dflt = lydict_insert(ctx, old[i].dflt, 0); |
| 832 | result[i].units = lydict_insert(ctx, old[i].units, 0); |
| 833 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 834 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 835 | return result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 836 | } |
| 837 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 838 | void |
| 839 | ly_tpdf_free(struct ly_ctx *ctx, struct ly_tpdf *tpdf) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 840 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 841 | assert(ctx); |
| 842 | if (!tpdf) { |
| 843 | return; |
| 844 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 845 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 846 | lydict_remove(ctx, tpdf->name); |
| 847 | lydict_remove(ctx, tpdf->dsc); |
| 848 | lydict_remove(ctx, tpdf->ref); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 849 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 850 | ly_type_free(ctx, &tpdf->type); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 851 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 852 | lydict_remove(ctx, tpdf->units); |
| 853 | lydict_remove(ctx, tpdf->dflt); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 854 | } |
| 855 | |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 856 | struct ly_when * |
| 857 | ly_when_dup(struct ly_ctx *ctx, struct ly_when *old) |
| 858 | { |
| 859 | struct ly_when *new; |
| 860 | |
| 861 | if (!old) { |
| 862 | return NULL; |
| 863 | } |
| 864 | |
| 865 | new = calloc(1, sizeof *new); |
| 866 | new->cond = lydict_insert(ctx, old->cond, 0); |
| 867 | new->dsc = lydict_insert(ctx, old->dsc, 0); |
| 868 | new->ref = lydict_insert(ctx, old->ref, 0); |
| 869 | |
| 870 | return new; |
| 871 | } |
| 872 | |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 873 | void |
| 874 | ly_when_free(struct ly_ctx *ctx, struct ly_when *w) |
| 875 | { |
| 876 | if (!w) { |
| 877 | return; |
| 878 | } |
| 879 | |
| 880 | lydict_remove(ctx, w->cond); |
| 881 | lydict_remove(ctx, w->dsc); |
| 882 | lydict_remove(ctx, w->ref); |
| 883 | |
| 884 | free(w); |
| 885 | } |
| 886 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 887 | struct ly_augment * |
| 888 | ly_augment_dup(struct ly_module *module, struct ly_mnode *parent, struct ly_augment *old, int size) |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 889 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 890 | struct ly_augment *new = NULL; |
| 891 | int i = -1; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 892 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 893 | if (!size) { |
| 894 | return NULL; |
| 895 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 896 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 897 | new = calloc(size, sizeof *new); |
| 898 | for (i = 0; i < size; i++) { |
| 899 | new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0); |
| 900 | new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0); |
| 901 | new[i].ref = lydict_insert(module->ctx, old[i].ref, 0); |
| 902 | new[i].flags = old[i].flags; |
| 903 | /* .target = NULL; .nodetype = 0 */ |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 904 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 905 | new[i].parent = parent; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 906 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 907 | /* copy the definition of augment nodes */ |
| 908 | if (old[i].child) { |
| 909 | new[i].child = (struct ly_mnode *)lyxml_dup_elem(module->ctx, (struct lyxml_elem *)old[i].child, NULL, 1); |
| 910 | } |
| 911 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 912 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 913 | return new; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 914 | } |
| 915 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 916 | struct ly_refine * |
| 917 | ly_refine_dup(struct ly_ctx *ctx, struct ly_refine *old, int size) |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 918 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 919 | struct ly_refine *result; |
| 920 | int i; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 921 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 922 | if (!size) { |
| 923 | return NULL; |
| 924 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 925 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 926 | result = calloc(size, sizeof *result); |
| 927 | for (i = 0; i < size; i++) { |
| 928 | result[i].target = lydict_insert(ctx, old[i].target, 0); |
| 929 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 930 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 931 | result[i].flags = old[i].flags; |
| 932 | result[i].target_type = old[i].target_type; |
| 933 | result[i].must_size = old[i].must_size; |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 934 | result[i].must = ly_restr_dup(ctx, old[i].must, old[i].must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 935 | if (result[i].target_type & (LY_NODE_LEAF | LY_NODE_CHOICE)) { |
| 936 | result[i].mod.dflt = lydict_insert(ctx, old[i].mod.dflt, 0); |
| 937 | } else if (result[i].target_type == LY_NODE_CONTAINER) { |
| 938 | result[i].mod.presence = lydict_insert(ctx, old[i].mod.presence, 0); |
| 939 | } else if (result[i].target_type & (LY_NODE_LIST | LY_NODE_LEAFLIST)) { |
| 940 | result[i].mod.list = old[i].mod.list; |
| 941 | } |
| 942 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 943 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 944 | return result; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 945 | } |
| 946 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 947 | void |
| 948 | ly_ident_free(struct ly_ctx *ctx, struct ly_ident *ident) |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 949 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 950 | struct ly_ident_der *der; |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 951 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 952 | assert(ctx); |
| 953 | if (!ident) { |
| 954 | return; |
| 955 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 956 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 957 | /* |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 958 | * if caller free only a single data model which is used (its identity is |
| 959 | * reference from identity in another module), this silly freeing can lead |
| 960 | * to segmentation fault. But without noting if the module is used by some |
| 961 | * other, it cannot be solved. |
Radek Krejci | a339069 | 2015-06-16 14:13:31 +0200 | [diff] [blame] | 962 | * |
| 963 | * Possible solution is to not allow caller to remove particular schema |
| 964 | * from the context. |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 965 | */ |
| 966 | while (ident->der) { |
| 967 | der = ident->der; |
| 968 | ident->der = der->next; |
| 969 | free(der); |
| 970 | } |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 971 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 972 | lydict_remove(ctx, ident->name); |
| 973 | lydict_remove(ctx, ident->dsc); |
| 974 | lydict_remove(ctx, ident->ref); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 975 | |
| 976 | } |
| 977 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 978 | void |
| 979 | ly_grp_free(struct ly_ctx *ctx, struct ly_mnode_grp *grp) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 980 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 981 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 982 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 983 | /* handle only specific parts for LY_NODE_GROUPING */ |
| 984 | for (i = 0; i < grp->tpdf_size; i++) { |
| 985 | ly_tpdf_free(ctx, &grp->tpdf[i]); |
| 986 | } |
| 987 | free(grp->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 988 | } |
| 989 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 990 | void |
| 991 | ly_anyxml_free(struct ly_ctx *ctx, struct ly_mnode_anyxml *anyxml) |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 992 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 993 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 994 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 995 | for (i = 0; i < anyxml->must_size; i++) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 996 | ly_restr_free(ctx, &anyxml->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 997 | } |
| 998 | free(anyxml->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 999 | |
| 1000 | ly_when_free(ctx, anyxml->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1001 | } |
| 1002 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1003 | void |
| 1004 | ly_leaf_free(struct ly_ctx *ctx, struct ly_mnode_leaf *leaf) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1005 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1006 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1007 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1008 | for (i = 0; i < leaf->must_size; i++) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1009 | ly_restr_free(ctx, &leaf->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1010 | } |
| 1011 | free(leaf->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1012 | |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1013 | ly_when_free(ctx, leaf->when); |
| 1014 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1015 | ly_type_free(ctx, &leaf->type); |
| 1016 | lydict_remove(ctx, leaf->units); |
| 1017 | lydict_remove(ctx, leaf->dflt); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1018 | } |
| 1019 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1020 | void |
| 1021 | ly_leaflist_free(struct ly_ctx *ctx, struct ly_mnode_leaflist *llist) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1022 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1023 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1024 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1025 | for (i = 0; i < llist->must_size; i++) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1026 | ly_restr_free(ctx, &llist->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1027 | } |
| 1028 | free(llist->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1029 | |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1030 | ly_when_free(ctx, llist->when); |
| 1031 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1032 | ly_type_free(ctx, &llist->type); |
| 1033 | lydict_remove(ctx, llist->units); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1034 | } |
| 1035 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1036 | void |
| 1037 | ly_list_free(struct ly_ctx *ctx, struct ly_mnode_list *list) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1038 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1039 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1040 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1041 | /* handle only specific parts for LY_NODE_LIST */ |
| 1042 | for (i = 0; i < list->tpdf_size; i++) { |
| 1043 | ly_tpdf_free(ctx, &list->tpdf[i]); |
| 1044 | } |
| 1045 | free(list->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1046 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1047 | for (i = 0; i < list->must_size; i++) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1048 | ly_restr_free(ctx, &list->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1049 | } |
| 1050 | free(list->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1051 | |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1052 | ly_when_free(ctx, list->when); |
| 1053 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1054 | for (i = 0; i < list->unique_size; i++) { |
| 1055 | free(list->unique[i].leafs); |
| 1056 | } |
| 1057 | free(list->unique); |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1058 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1059 | free(list->keys); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1060 | } |
| 1061 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1062 | void |
| 1063 | ly_container_free(struct ly_ctx *ctx, struct ly_mnode_container *cont) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1064 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1065 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1066 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1067 | /* handle only specific parts for LY_NODE_CONTAINER */ |
| 1068 | lydict_remove(ctx, cont->presence); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1069 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1070 | for (i = 0; i < cont->tpdf_size; i++) { |
| 1071 | ly_tpdf_free(ctx, &cont->tpdf[i]); |
| 1072 | } |
| 1073 | free(cont->tpdf); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1074 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1075 | for (i = 0; i < cont->must_size; i++) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1076 | ly_restr_free(ctx, &cont->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1077 | } |
| 1078 | free(cont->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1079 | |
| 1080 | ly_when_free(ctx, cont->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1081 | } |
| 1082 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1083 | void |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1084 | ly_feature_free(struct ly_ctx *ctx, struct ly_feature *f) |
| 1085 | { |
| 1086 | lydict_remove(ctx, f->name); |
| 1087 | lydict_remove(ctx, f->dsc); |
| 1088 | lydict_remove(ctx, f->ref); |
| 1089 | free(f->features); |
| 1090 | } |
| 1091 | |
| 1092 | void |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 1093 | ly_augment_free(struct ly_ctx *ctx, struct ly_augment *aug) |
| 1094 | { |
| 1095 | lydict_remove(ctx, aug->target_name); |
| 1096 | lydict_remove(ctx, aug->dsc); |
| 1097 | lydict_remove(ctx, aug->ref); |
| 1098 | |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1099 | ly_when_free(ctx, aug->when); |
| 1100 | |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 1101 | lyxml_free_elem(ctx, (struct lyxml_elem *)aug->child); |
| 1102 | } |
| 1103 | |
| 1104 | void |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1105 | ly_uses_free(struct ly_ctx *ctx, struct ly_mnode_uses *uses) |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1106 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1107 | int i, j; |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1108 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1109 | for (i = 0; i < uses->refine_size; i++) { |
| 1110 | lydict_remove(ctx, uses->refine[i].target); |
| 1111 | lydict_remove(ctx, uses->refine[i].dsc); |
| 1112 | lydict_remove(ctx, uses->refine[i].ref); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1113 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1114 | for (j = 0; j < uses->refine[j].must_size; j++) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1115 | ly_restr_free(ctx, &uses->refine[i].must[j]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1116 | } |
| 1117 | free(uses->refine[i].must); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1118 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1119 | if (uses->refine[i].target_type & (LY_NODE_LEAF | LY_NODE_CHOICE)) { |
| 1120 | lydict_remove(ctx, uses->refine[i].mod.dflt); |
| 1121 | } else if (uses->refine[i].target_type & LY_NODE_CONTAINER) { |
| 1122 | lydict_remove(ctx, uses->refine[i].mod.presence); |
| 1123 | } |
| 1124 | } |
| 1125 | free(uses->refine); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1126 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1127 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 1128 | ly_augment_free(ctx, &uses->augment[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1129 | } |
| 1130 | free(uses->augment); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1131 | |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1132 | ly_when_free(ctx, uses->when); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1133 | } |
| 1134 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1135 | void |
| 1136 | ly_mnode_free(struct ly_mnode *node) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1137 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1138 | struct ly_ctx *ctx; |
| 1139 | struct ly_mnode *sub, *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1140 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1141 | if (!node) { |
| 1142 | return; |
| 1143 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1144 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1145 | assert(node->module); |
| 1146 | assert(node->module->ctx); |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1147 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1148 | ctx = node->module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1149 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1150 | /* common part */ |
| 1151 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
| 1152 | ly_mnode_free(sub); |
| 1153 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1154 | free(node->features); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1155 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1156 | lydict_remove(ctx, node->name); |
| 1157 | lydict_remove(ctx, node->dsc); |
| 1158 | lydict_remove(ctx, node->ref); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1159 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1160 | /* specific part */ |
| 1161 | switch (node->nodetype) { |
| 1162 | case LY_NODE_CONTAINER: |
| 1163 | ly_container_free(ctx, (struct ly_mnode_container *)node); |
| 1164 | break; |
| 1165 | case LY_NODE_CHOICE: |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1166 | ly_when_free(ctx, ((struct ly_mnode_choice *)node)->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1167 | break; |
| 1168 | case LY_NODE_LEAF: |
| 1169 | ly_leaf_free(ctx, (struct ly_mnode_leaf *)node); |
| 1170 | break; |
| 1171 | case LY_NODE_LEAFLIST: |
| 1172 | ly_leaflist_free(ctx, (struct ly_mnode_leaflist *)node); |
| 1173 | break; |
| 1174 | case LY_NODE_LIST: |
| 1175 | ly_list_free(ctx, (struct ly_mnode_list *)node); |
| 1176 | break; |
| 1177 | case LY_NODE_ANYXML: |
| 1178 | ly_anyxml_free(ctx, (struct ly_mnode_anyxml *)node); |
| 1179 | break; |
| 1180 | case LY_NODE_USES: |
| 1181 | ly_uses_free(ctx, (struct ly_mnode_uses *)node); |
| 1182 | break; |
| 1183 | case LY_NODE_CASE: |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1184 | ly_when_free(ctx, ((struct ly_mnode_case *)node)->when); |
| 1185 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1186 | case LY_NODE_AUGMENT: |
| 1187 | /* do nothing */ |
| 1188 | break; |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 1189 | case LY_NODE_GROUPING: |
| 1190 | case LY_NODE_RPC: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1191 | case LY_NODE_INPUT: |
| 1192 | case LY_NODE_OUTPUT: |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 1193 | case LY_NODE_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1194 | ly_grp_free(ctx, (struct ly_mnode_grp *)node); |
| 1195 | break; |
| 1196 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1197 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1198 | /* again common part */ |
| 1199 | ly_mnode_unlink(node); |
| 1200 | free(node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1201 | } |
| 1202 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1203 | static void |
| 1204 | module_free_common(struct ly_module *module) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1205 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1206 | struct ly_ctx *ctx; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1207 | unsigned int i; |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 1208 | int j, l; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1209 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1210 | assert(module->ctx); |
| 1211 | ctx = module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1212 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 1213 | /* as first step, free the imported modules */ |
| 1214 | for (i = 0; i < module->imp_size; i++) { |
| 1215 | /* get the imported module from the context and then free, |
| 1216 | * this check is necessary because the imported module can |
| 1217 | * be already removed |
| 1218 | */ |
| 1219 | l = ctx->models.used; |
| 1220 | for (j = 0; j < l; j++) { |
| 1221 | if (ctx->models.list[j] == module->imp[i].module) { |
| 1222 | ly_module_free(module->imp[i].module); |
| 1223 | break; |
| 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | free(module->imp); |
| 1228 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1229 | while (module->data) { |
Radek Krejci | 2118196 | 2015-06-30 14:11:00 +0200 | [diff] [blame] | 1230 | ly_mnode_free(module->data); |
| 1231 | } |
| 1232 | while (module->rpc) { |
| 1233 | ly_mnode_free(module->rpc); |
| 1234 | } |
| 1235 | while (module->notif) { |
| 1236 | ly_mnode_free(module->notif); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1237 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1238 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1239 | lydict_remove(ctx, module->dsc); |
| 1240 | lydict_remove(ctx, module->ref); |
| 1241 | lydict_remove(ctx, module->org); |
| 1242 | lydict_remove(ctx, module->contact); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1243 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1244 | for (i = 0; i < module->rev_size; i++) { |
| 1245 | lydict_remove(ctx, module->rev[i].dsc); |
| 1246 | lydict_remove(ctx, module->rev[i].ref); |
| 1247 | } |
| 1248 | free(module->rev); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1249 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1250 | for (i = 0; i < module->ident_size; i++) { |
| 1251 | ly_ident_free(ctx, &module->ident[i]); |
| 1252 | } |
| 1253 | module->ident_size = 0; |
| 1254 | free(module->ident); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1255 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1256 | for (i = 0; i < module->tpdf_size; i++) { |
| 1257 | ly_tpdf_free(ctx, &module->tpdf[i]); |
| 1258 | } |
| 1259 | free(module->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1260 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1261 | for (i = 0; i < module->inc_size; i++) { |
| 1262 | ly_submodule_free(module->inc[i].submodule); |
| 1263 | } |
| 1264 | free(module->inc); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1265 | |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 1266 | for (i = 0; i < module->augment_size; i++) { |
| 1267 | ly_augment_free(ctx, &module->augment[i]); |
| 1268 | } |
| 1269 | free(module->augment); |
| 1270 | |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1271 | for (i = 0; i < module->features_size; i++) { |
| 1272 | ly_feature_free(ctx, &module->features[i]); |
| 1273 | } |
| 1274 | free(module->features); |
| 1275 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1276 | lydict_remove(ctx, module->name); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1277 | } |
| 1278 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1279 | void |
| 1280 | ly_submodule_free(struct ly_submodule *submodule) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1281 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1282 | if (!submodule) { |
| 1283 | return; |
| 1284 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1285 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1286 | submodule->inc_size = 0; |
| 1287 | free(submodule->inc); |
| 1288 | submodule->inc = NULL; |
Radek Krejci | f388693 | 2015-06-04 17:36:06 +0200 | [diff] [blame] | 1289 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1290 | /* common part with struct ly_module */ |
| 1291 | module_free_common((struct ly_module *)submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1292 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1293 | /* no specific items to free */ |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1294 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1295 | free(submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1296 | } |
| 1297 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1298 | struct ly_mnode * |
| 1299 | ly_mnode_dup(struct ly_module *module, struct ly_mnode *mnode, uint8_t flags, int recursive, unsigned int line) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1300 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1301 | struct ly_mnode *retval = NULL, *aux, *child; |
| 1302 | struct ly_ctx *ctx = module->ctx; |
| 1303 | int i, j; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1304 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1305 | struct ly_mnode_container *cont; |
| 1306 | struct ly_mnode_container *cont_orig = (struct ly_mnode_container *)mnode; |
| 1307 | struct ly_mnode_choice *choice; |
| 1308 | struct ly_mnode_choice *choice_orig = (struct ly_mnode_choice *)mnode; |
| 1309 | struct ly_mnode_leaf *leaf; |
| 1310 | struct ly_mnode_leaf *leaf_orig = (struct ly_mnode_leaf *)mnode; |
| 1311 | struct ly_mnode_leaflist *llist; |
| 1312 | struct ly_mnode_leaflist *llist_orig = (struct ly_mnode_leaflist *)mnode; |
| 1313 | struct ly_mnode_list *list; |
| 1314 | struct ly_mnode_list *list_orig = (struct ly_mnode_list *)mnode; |
| 1315 | struct ly_mnode_anyxml *anyxml; |
| 1316 | struct ly_mnode_anyxml *anyxml_orig = (struct ly_mnode_anyxml *)mnode; |
| 1317 | struct ly_mnode_uses *uses; |
| 1318 | struct ly_mnode_uses *uses_orig = (struct ly_mnode_uses *)mnode; |
| 1319 | struct ly_mnode_grp *mix; |
| 1320 | struct ly_mnode_grp *mix_orig = (struct ly_mnode_grp *)mnode; |
| 1321 | struct ly_mnode_case *cs; |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1322 | struct ly_mnode_case *cs_orig = (struct ly_mnode_case *)mnode; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1323 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1324 | /* we cannot just duplicate memory since the strings are stored in |
| 1325 | * dictionary and we need to update dictionary counters. |
| 1326 | */ |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1327 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1328 | switch (mnode->nodetype) { |
| 1329 | case LY_NODE_CONTAINER: |
| 1330 | cont = calloc(1, sizeof *cont); |
| 1331 | retval = (struct ly_mnode *)cont; |
| 1332 | break; |
| 1333 | |
| 1334 | case LY_NODE_CHOICE: |
| 1335 | choice = calloc(1, sizeof *choice); |
| 1336 | retval = (struct ly_mnode *)choice; |
| 1337 | break; |
| 1338 | |
| 1339 | case LY_NODE_LEAF: |
| 1340 | leaf = calloc(1, sizeof *leaf); |
| 1341 | retval = (struct ly_mnode *)leaf; |
| 1342 | break; |
| 1343 | |
| 1344 | case LY_NODE_LEAFLIST: |
| 1345 | llist = calloc(1, sizeof *llist); |
| 1346 | retval = (struct ly_mnode *)llist; |
| 1347 | break; |
| 1348 | |
| 1349 | case LY_NODE_LIST: |
| 1350 | list = calloc(1, sizeof *list); |
| 1351 | retval = (struct ly_mnode *)list; |
| 1352 | break; |
| 1353 | |
| 1354 | case LY_NODE_ANYXML: |
| 1355 | anyxml = calloc(1, sizeof *anyxml); |
| 1356 | retval = (struct ly_mnode *)anyxml; |
| 1357 | break; |
| 1358 | |
| 1359 | case LY_NODE_USES: |
| 1360 | uses = calloc(1, sizeof *uses); |
| 1361 | retval = (struct ly_mnode *)uses; |
| 1362 | break; |
| 1363 | |
| 1364 | case LY_NODE_CASE: |
| 1365 | cs = calloc(1, sizeof *cs); |
| 1366 | retval = (struct ly_mnode *)cs; |
| 1367 | break; |
| 1368 | |
| 1369 | /* exact same structure */ |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 1370 | case LY_NODE_GROUPING: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 1371 | case LY_NODE_RPC: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 1372 | case LY_NODE_INPUT: |
| 1373 | case LY_NODE_OUTPUT: |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 1374 | case LY_NODE_NOTIF: |
| 1375 | mix = calloc(1, sizeof *mix); |
| 1376 | retval = (struct ly_mnode *)mix; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 1377 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1378 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1379 | default: |
| 1380 | goto error; |
| 1381 | } |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame] | 1382 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1383 | /* |
| 1384 | * duplicate generic part of the structure |
| 1385 | */ |
| 1386 | retval->name = lydict_insert(ctx, mnode->name, 0); |
| 1387 | retval->dsc = lydict_insert(ctx, mnode->dsc, 0); |
| 1388 | retval->ref = lydict_insert(ctx, mnode->ref, 0); |
| 1389 | retval->flags = mnode->flags; |
| 1390 | if (!(retval->flags & LY_NODE_CONFIG_MASK)) { |
| 1391 | /* set parent's config flag */ |
| 1392 | retval->flags |= flags & LY_NODE_CONFIG_MASK; |
| 1393 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1394 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1395 | retval->module = module; |
| 1396 | retval->nodetype = mnode->nodetype; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1397 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1398 | retval->prev = retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1399 | |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1400 | retval->features_size = mnode->features_size; |
| 1401 | retval->features = calloc(retval->features_size, sizeof *retval->features); |
| 1402 | memcpy(retval->features, mnode->features, retval->features_size * sizeof *retval->features); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1403 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1404 | if (recursive) { |
| 1405 | /* go recursively */ |
| 1406 | LY_TREE_FOR(mnode->child, child) { |
| 1407 | aux = ly_mnode_dup(module, child, retval->flags, 1, line); |
| 1408 | if (!aux || ly_mnode_addchild(retval, aux)) { |
| 1409 | goto error; |
| 1410 | } |
| 1411 | } |
| 1412 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1413 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1414 | /* |
| 1415 | * duplicate specific part of the structure |
| 1416 | */ |
| 1417 | switch (mnode->nodetype) { |
| 1418 | case LY_NODE_CONTAINER: |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1419 | cont->when = ly_when_dup(ctx, cont_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1420 | cont->presence = lydict_insert(ctx, cont_orig->presence, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1421 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1422 | cont->must_size = cont_orig->must_size; |
| 1423 | cont->tpdf_size = cont_orig->tpdf_size; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1424 | |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1425 | cont->must = ly_restr_dup(ctx, cont_orig->must, cont->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1426 | cont->tpdf = ly_tpdf_dup(ctx, cont_orig->tpdf, cont->tpdf_size); |
| 1427 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1428 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1429 | case LY_NODE_CHOICE: |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1430 | choice->when = ly_when_dup(ctx, choice_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1431 | if (choice->dflt) { |
| 1432 | LY_TREE_FOR(choice->child, child) { |
| 1433 | if (child->name == choice_orig->dflt->name) { |
| 1434 | choice->dflt = child; |
| 1435 | break; |
| 1436 | } |
| 1437 | } |
| 1438 | } |
| 1439 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1440 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1441 | case LY_NODE_LEAF: |
| 1442 | ly_type_dup(ctx, &(leaf->type), &(leaf_orig->type)); |
| 1443 | leaf->units = lydict_insert(ctx, leaf_orig->units, 0); |
| 1444 | leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1445 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1446 | leaf->must_size = leaf_orig->must_size; |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1447 | leaf->must = ly_restr_dup(ctx, leaf_orig->must, leaf->must_size); |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1448 | |
| 1449 | leaf->when = ly_when_dup(ctx, leaf_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1450 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1451 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1452 | case LY_NODE_LEAFLIST: |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1453 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1454 | ly_type_dup(ctx, &(llist->type), &(llist_orig->type)); |
| 1455 | llist->units = lydict_insert(ctx, llist_orig->units, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1456 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1457 | llist->min = llist_orig->min; |
| 1458 | llist->max = llist_orig->max; |
| 1459 | |
| 1460 | llist->must_size = llist_orig->must_size; |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1461 | llist->must = ly_restr_dup(ctx, llist_orig->must, llist->must_size); |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1462 | |
| 1463 | llist->when = ly_when_dup(ctx, llist_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1464 | break; |
| 1465 | |
| 1466 | case LY_NODE_LIST: |
| 1467 | list->min = list_orig->min; |
| 1468 | list->max = list_orig->max; |
| 1469 | |
| 1470 | list->must_size = list_orig->must_size; |
| 1471 | list->tpdf_size = list_orig->tpdf_size; |
| 1472 | list->keys_size = list_orig->keys_size; |
| 1473 | list->unique_size = list_orig->unique_size; |
| 1474 | |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1475 | list->must = ly_restr_dup(ctx, list_orig->must, list->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1476 | list->tpdf = ly_tpdf_dup(ctx, list_orig->tpdf, list->tpdf_size); |
| 1477 | |
| 1478 | if (list->keys_size) { |
| 1479 | list->keys = calloc(list->keys_size, sizeof *list->keys); |
| 1480 | for (i = 0; i < list->keys_size; i++) { |
| 1481 | list->keys[i] = find_leaf(retval, list_orig->keys[i]->name, 0); |
| 1482 | } |
| 1483 | } |
| 1484 | if (list->unique_size) { |
| 1485 | list->unique = calloc(list->unique_size, sizeof *list->unique); |
| 1486 | for (i = 0; i < list->unique_size; i++) { |
| 1487 | list->unique[i].leafs = calloc(list->unique[i].leafs_size, sizeof *list->unique[i].leafs); |
| 1488 | for (j = 0; j < list->unique[i].leafs_size; j++) { |
| 1489 | list->unique[i].leafs[j] = find_leaf(retval, list_orig->unique[i].leafs[j]->name, 0); |
| 1490 | } |
| 1491 | } |
| 1492 | } |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1493 | list->when = ly_when_dup(ctx, list_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1494 | break; |
| 1495 | |
| 1496 | case LY_NODE_ANYXML: |
| 1497 | anyxml->must_size = anyxml_orig->must_size; |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1498 | anyxml->must = ly_restr_dup(ctx, anyxml_orig->must, anyxml->must_size); |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1499 | anyxml->when = ly_when_dup(ctx, anyxml_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1500 | break; |
| 1501 | |
| 1502 | case LY_NODE_USES: |
| 1503 | uses->grp = uses_orig->grp; |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1504 | uses->when = ly_when_dup(ctx, uses_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1505 | uses->refine_size = uses_orig->refine_size; |
| 1506 | uses->refine = ly_refine_dup(ctx, uses_orig->refine, uses_orig->refine_size); |
| 1507 | uses->augment_size = uses_orig->augment_size; |
| 1508 | uses->augment = ly_augment_dup(module, (struct ly_mnode *)uses, uses_orig->augment, uses_orig->augment_size); |
| 1509 | if (resolve_uses(uses, line)) { |
| 1510 | goto error; |
| 1511 | } |
| 1512 | break; |
| 1513 | |
| 1514 | case LY_NODE_CASE: |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1515 | cs->when = ly_when_dup(ctx, cs_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1516 | break; |
| 1517 | |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 1518 | case LY_NODE_GROUPING: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 1519 | case LY_NODE_RPC: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 1520 | case LY_NODE_INPUT: |
| 1521 | case LY_NODE_OUTPUT: |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 1522 | case LY_NODE_NOTIF: |
| 1523 | mix->tpdf_size = mix_orig->tpdf_size; |
| 1524 | mix->tpdf = ly_tpdf_dup(ctx, mix_orig->tpdf, mix->tpdf_size); |
Radek Krejci | a01e543 | 2015-06-16 10:35:25 +0200 | [diff] [blame] | 1525 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1526 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1527 | default: |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1528 | /* LY_NODE_AUGMENT */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1529 | goto error; |
| 1530 | } |
| 1531 | |
| 1532 | return retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1533 | |
| 1534 | error: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1535 | LOGDBG("error: %s:%d", __FILE__, __LINE__); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1536 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1537 | ly_mnode_free(retval); |
| 1538 | return NULL; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1539 | } |
| 1540 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1541 | API void |
| 1542 | ly_module_free(struct ly_module *module) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1543 | { |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 1544 | struct ly_ctx *ctx; |
| 1545 | int i; |
| 1546 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1547 | if (!module) { |
| 1548 | return; |
| 1549 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1550 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 1551 | /* remove schema from the context */ |
| 1552 | ctx = module->ctx; |
| 1553 | if (ctx->models.used) { |
| 1554 | for (i = 0; i < ctx->models.used; i++) { |
| 1555 | if (ctx->models.list[i] == module) { |
| 1556 | /* replace the position in the list by the last module in the list */ |
| 1557 | ctx->models.used--; |
| 1558 | ctx->models.list[i] = ctx->models.list[ctx->models.used]; |
| 1559 | ctx->models.list[ctx->models.used] = NULL; |
| 1560 | /* we are done */ |
| 1561 | break; |
| 1562 | } |
| 1563 | } |
| 1564 | } |
| 1565 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1566 | /* common part with struct ly_submodule */ |
| 1567 | module_free_common(module); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1568 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1569 | /* specific items to free */ |
| 1570 | lydict_remove(module->ctx, module->ns); |
| 1571 | lydict_remove(module->ctx, module->prefix); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1572 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1573 | free(module); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1574 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 1575 | |
| 1576 | /* |
| 1577 | * op: 1 - enable, 0 - disable |
| 1578 | */ |
| 1579 | static int |
| 1580 | ly_features_change(struct ly_module *module, const char *name, int op) |
| 1581 | { |
| 1582 | int all = 0; |
| 1583 | int i, j, k; |
| 1584 | |
| 1585 | if (!module || !name || !strlen(name)) { |
| 1586 | return EXIT_FAILURE; |
| 1587 | } |
| 1588 | |
| 1589 | if (!strcmp(name, "*")) { |
| 1590 | /* enable all */ |
| 1591 | all = 1; |
| 1592 | } |
| 1593 | |
| 1594 | /* module itself */ |
| 1595 | for (i = 0; i < module->features_size; i++) { |
| 1596 | if (all || !strcmp(module->features[i].name, name)) { |
| 1597 | if (op) { |
| 1598 | module->features[i].flags |= LY_NODE_FENABLED; |
| 1599 | /* enable referenced features (recursion) */ |
| 1600 | for (k = 0; k < module->features[i].features_size; k++) { |
| 1601 | ly_features_change(module->features[i].features[k]->module, |
| 1602 | module->features[i].features[k]->name, op); |
| 1603 | } |
| 1604 | } else { |
| 1605 | module->features[i].flags &= ~LY_NODE_FENABLED; |
| 1606 | } |
| 1607 | if (!all) { |
| 1608 | return EXIT_SUCCESS; |
| 1609 | } |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | /* submodules */ |
| 1614 | for (j = 0; j < module->inc_size; j++) { |
| 1615 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
| 1616 | if (all || !strcmp(module->inc[j].submodule->features[i].name, name)) { |
| 1617 | if (op) { |
| 1618 | module->inc[j].submodule->features[i].flags |= LY_NODE_FENABLED; |
| 1619 | } else { |
| 1620 | module->inc[j].submodule->features[i].flags &= ~LY_NODE_FENABLED; |
| 1621 | } |
| 1622 | if (!all) { |
| 1623 | return EXIT_SUCCESS; |
| 1624 | } |
| 1625 | } |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | if (all) { |
| 1630 | return EXIT_SUCCESS; |
| 1631 | } else { |
| 1632 | return EXIT_FAILURE; |
| 1633 | } |
| 1634 | } |
| 1635 | |
| 1636 | API int |
| 1637 | ly_features_enable(struct ly_module *module, const char *name) |
| 1638 | { |
| 1639 | return ly_features_change(module, name, 1); |
| 1640 | } |
| 1641 | |
| 1642 | API int |
| 1643 | ly_features_disable(struct ly_module *module, const char *name) |
| 1644 | { |
| 1645 | return ly_features_change(module, name, 1); |
| 1646 | } |