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; |
| 120 | } |
| 121 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 122 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 123 | /* store pointers to important nodes */ |
| 124 | parent = node->parent; |
| 125 | if (parent && !parent->nodetype) { |
| 126 | /* handle augments - first, unlink it from the augment parent ... */ |
| 127 | if (parent->child == node) { |
| 128 | parent->child = node->next; |
| 129 | } |
| 130 | /* and then continue with the target parent */ |
| 131 | parent = ((struct ly_augment *)parent)->target; |
| 132 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 133 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 134 | /* unlink from parent */ |
| 135 | if (parent) { |
| 136 | if (parent->child == node) { |
| 137 | parent->child = node->next; |
| 138 | } |
| 139 | node->parent = NULL; |
| 140 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 141 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 142 | /* unlink from siblings */ |
| 143 | if (node->prev == node) { |
| 144 | /* there are no more siblings */ |
| 145 | return; |
| 146 | } |
| 147 | if (node->next) { |
| 148 | node->next->prev = node->prev; |
| 149 | } else { |
| 150 | /* unlinking the last element */ |
| 151 | if (parent) { |
| 152 | first = parent->child; |
| 153 | } else { |
| 154 | first = node; |
| 155 | while (node->prev->next) { |
| 156 | first = node->prev; |
| 157 | } |
| 158 | } |
| 159 | first->prev = node->prev; |
| 160 | } |
| 161 | if (node->prev->next) { |
| 162 | node->prev->next = node->next; |
| 163 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 164 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 165 | /* clean up the unlinked element */ |
| 166 | node->next = NULL; |
| 167 | node->prev = node; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Add child model node at the end of the parent's child list. |
| 172 | * If the child is connected somewhere (has a parent), it is completely |
| 173 | * unlinked and none of the following conditions applies. |
| 174 | * If the child has prev sibling(s), they are ignored (child is added at the |
| 175 | * end of the child list). |
| 176 | * If the child has next sibling(s), all of them are connected with the parent. |
| 177 | */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 178 | int |
| 179 | ly_mnode_addchild(struct ly_mnode *parent, struct ly_mnode *child) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 180 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 181 | struct ly_mnode *last; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 182 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 183 | assert(parent); |
| 184 | assert(child); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 185 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 186 | /* checks */ |
| 187 | switch (parent->nodetype) { |
| 188 | case LY_NODE_CONTAINER: |
| 189 | case LY_NODE_LIST: |
| 190 | case LY_NODE_GROUPING: |
| 191 | case LY_NODE_USES: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 192 | case LY_NODE_INPUT: |
| 193 | case LY_NODE_OUTPUT: |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 194 | case LY_NODE_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 195 | if (!(child->nodetype & |
| 196 | (LY_NODE_ANYXML | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_GROUPING | LY_NODE_LEAF | |
| 197 | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_USES))) { |
| 198 | LOGVAL(VE_SPEC, 0, "Unexpected substatement \"%s\" in \"%s\" (%s).", |
| 199 | strnodetype(child->nodetype), strnodetype(parent->nodetype), parent->name); |
| 200 | return EXIT_FAILURE; |
| 201 | } |
| 202 | break; |
| 203 | case LY_NODE_CHOICE: |
| 204 | if (!(child->nodetype & |
| 205 | (LY_NODE_ANYXML | LY_NODE_CASE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST))) { |
| 206 | LOGVAL(VE_SPEC, 0, "Unexpected substatement \"%s\" in \"choice\" %s.", |
| 207 | strnodetype(child->nodetype), parent->name); |
| 208 | return EXIT_FAILURE; |
| 209 | } |
| 210 | break; |
| 211 | case LY_NODE_CASE: |
| 212 | if (!(child->nodetype & |
| 213 | (LY_NODE_ANYXML | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_USES))) { |
| 214 | LOGVAL(VE_SPEC, 0, "Unexpected substatement \"%s\" in \"case\" %s.", |
| 215 | strnodetype(child->nodetype), parent->name); |
| 216 | return EXIT_FAILURE; |
| 217 | } |
| 218 | break; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 219 | case LY_NODE_RPC: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 220 | if (!(child->nodetype & (LY_NODE_INPUT | LY_NODE_OUTPUT | LY_NODE_GROUPING))) { |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 221 | LOGVAL(VE_SPEC, 0, "Unexpected substatement \"%s\" in \"rpc\" %s.", |
| 222 | strnodetype(child->nodetype), parent->name); |
| 223 | return EXIT_FAILURE; |
| 224 | } |
| 225 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 226 | case LY_NODE_LEAF: |
| 227 | case LY_NODE_LEAFLIST: |
| 228 | case LY_NODE_ANYXML: |
| 229 | LOGVAL(VE_SPEC, 0, "The \"%s\" statement (%s) cannot have any substatement.", |
| 230 | strnodetype(parent->nodetype), parent->name); |
| 231 | return EXIT_FAILURE; |
| 232 | case LY_NODE_AUGMENT: |
| 233 | LOGVAL(VE_SPEC, 0, "Internal error (%s:%d)", __FILE__, __LINE__); |
| 234 | return EXIT_FAILURE; |
| 235 | } |
Radek Krejci | b7155b5 | 2015-06-10 17:03:01 +0200 | [diff] [blame] | 236 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 237 | if (child->parent) { |
| 238 | ly_mnode_unlink(child); |
| 239 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 240 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 241 | if (!parent->child) { |
| 242 | /* the only/first child of the parent */ |
| 243 | parent->child = child; |
| 244 | child->parent = parent; |
| 245 | last = child; |
| 246 | } else { |
| 247 | /* add a new child at the end of parent's child list */ |
| 248 | last = parent->child->prev; |
| 249 | last->next = child; |
| 250 | child->prev = last; |
| 251 | } |
| 252 | while (last->next) { |
| 253 | last = last->next; |
| 254 | last->parent = parent; |
| 255 | } |
| 256 | parent->child->prev = last; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 257 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 258 | return EXIT_SUCCESS; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 261 | /* |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 262 | * id - schema-nodeid |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 263 | */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 264 | struct ly_mnode * |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 265 | resolve_schema_nodeid(const char *id, struct ly_mnode *start, struct ly_module *mod) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 266 | { |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 267 | const char *name, *prefix, *ptr; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 268 | struct ly_mnode *sibling; |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 269 | struct ly_submodule *sub_mod; |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 270 | uint32_t i, j, nam_len, pref_len; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 271 | |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 272 | assert(mod); |
| 273 | assert(id); |
| 274 | |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 275 | if (id[0] == '/') { |
| 276 | ptr = strchr(id+1, '/'); |
| 277 | prefix = id+1; |
| 278 | } else { |
| 279 | ptr = strchr(id, '/'); |
| 280 | prefix = id; |
| 281 | } |
| 282 | pref_len = (ptr ? (unsigned)(ptr-prefix) : strlen(prefix)); |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 283 | |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 284 | ptr = strnchr(prefix, ':', pref_len); |
| 285 | /* there is prefix */ |
| 286 | if (ptr) { |
| 287 | nam_len = (pref_len-(ptr-prefix))-1; |
| 288 | pref_len = ptr-prefix; |
| 289 | name = ptr+1; |
| 290 | |
| 291 | /* no prefix used */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 292 | } else { |
| 293 | name = prefix; |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 294 | nam_len = pref_len; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 295 | prefix = NULL; |
| 296 | } |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 297 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 298 | /* absolute-schema-nodeid */ |
| 299 | if (id[0] == '/') { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 300 | start = NULL; |
| 301 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 302 | /* it is not the local prefix */ |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 303 | if (prefix && strncmp(prefix, mod->prefix, pref_len)) { |
| 304 | /* check imports */ |
| 305 | for (i = 0; i < mod->imp_size; i++) { |
| 306 | if (!strncmp(mod->imp[i].prefix, prefix, pref_len)) { |
| 307 | start = mod->imp[i].module->data; |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 308 | break; |
| 309 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 310 | } |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 311 | |
| 312 | /* no match - check include imports */ |
| 313 | if (!start) { |
| 314 | for (i = 0; i < mod->inc_size; i++) { |
| 315 | sub_mod = mod->inc[i].submodule; |
| 316 | for (j = 0; j < sub_mod->imp_size; j++) { |
| 317 | if (!strncmp(sub_mod->imp[j].prefix, prefix, pref_len)) { |
| 318 | start = sub_mod->imp[j].module->data; |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | if (start) { |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 329 | /* no match */ |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 330 | if (!start) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 331 | return NULL; |
| 332 | } |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 333 | |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 334 | /* it is likely the local prefix */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 335 | } else { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 336 | start = mod->data; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 337 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 338 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 339 | /* descendant-schema-nodeid */ |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 340 | } else { |
| 341 | assert(start); |
| 342 | start = start->child; |
| 343 | } |
| 344 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 345 | while (1) { |
| 346 | if (!strcmp(name, ".")) { |
| 347 | /* this node - start does not change */ |
| 348 | } else if (!strcmp(name, "..")) { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 349 | if (!start) { |
| 350 | return NULL; |
| 351 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 352 | start = start->parent; |
| 353 | } else { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 354 | sibling = NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 355 | LY_TREE_FOR(start, sibling) { |
| 356 | /* match */ |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 357 | if (!strncmp(name, sibling->name, nam_len)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 358 | /* prefix check, it's not our own */ |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 359 | if (prefix && strncmp(sibling->module->prefix, prefix, pref_len)) { |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 360 | |
| 361 | /* import prefix check */ |
| 362 | for (i = 0; i < sibling->module->imp_size; i++) { |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 363 | if (!strncmp(sibling->module->imp[i].prefix, prefix, pref_len) |
| 364 | && (sibling->module->imp[i].module == sibling->module)) { |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 365 | break; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /* import prefix check failed */ |
| 370 | if (i == sibling->module->imp_size) { |
| 371 | /* include import prefix check */ |
| 372 | for (i = 0; i < sibling->module->inc_size; i++) { |
| 373 | sub_mod = sibling->module->inc[i].submodule; |
| 374 | for (j = 0; j < sub_mod->imp_size; j++) { |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 375 | if (!strncmp(sub_mod->imp[j].prefix, prefix, pref_len) |
| 376 | && (sub_mod->imp[j].module == sibling->module)) { |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 377 | break; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | if (j < sub_mod->imp_size) { |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | /* include import prefix check failed too - definite fail */ |
| 387 | if (i == sibling->module->inc_size) { |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 388 | return NULL; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /* the result node? */ |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 394 | ptr = name+nam_len; |
| 395 | if (!ptr[0]) { |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 396 | return sibling; |
| 397 | } |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 398 | assert(ptr[0] == '/'); |
| 399 | prefix = ptr+1; |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 400 | |
| 401 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 402 | if (!sibling->parent || (sibling->parent->nodetype != LY_NODE_CHOICE) |
| 403 | || (sibling->nodetype == LY_NODE_CASE)) { |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 404 | start = sibling->child; |
| 405 | } |
| 406 | break; |
| 407 | } |
| 408 | } |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 409 | |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 410 | /* no match */ |
| 411 | if (!sibling) { |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 412 | return NULL; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /* parse prefix */ |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 417 | ptr = strchr(prefix, '/'); |
| 418 | pref_len = (ptr ? (unsigned)(ptr-prefix) : strlen(prefix)); |
| 419 | ptr = strnchr(prefix, ':', pref_len); |
| 420 | |
| 421 | /* there is prefix */ |
| 422 | if (ptr) { |
| 423 | nam_len = pref_len-((ptr-prefix)-1); |
| 424 | pref_len = ptr-prefix; |
| 425 | name = ptr+1; |
| 426 | |
| 427 | /* no prefix used */ |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 428 | } else { |
| 429 | name = prefix; |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 430 | nam_len = pref_len; |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 431 | prefix = NULL; |
| 432 | } |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /* cannot get here */ |
| 436 | return NULL; |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 439 | API struct ly_module * |
| 440 | 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] | 441 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 442 | if (!ctx || !data) { |
| 443 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 444 | return NULL; |
| 445 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 446 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 447 | switch (format) { |
| 448 | case LY_IN_YIN: |
| 449 | return yin_read_module(ctx, data); |
| 450 | case LY_IN_YANG: |
| 451 | default: |
| 452 | /* TODO */ |
| 453 | return NULL; |
| 454 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 455 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 456 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 457 | } |
| 458 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 459 | struct ly_submodule * |
| 460 | 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] | 461 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 462 | assert(module); |
| 463 | assert(data); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 464 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 465 | switch (format) { |
| 466 | case LY_IN_YIN: |
| 467 | return yin_read_submodule(module, data); |
| 468 | case LY_IN_YANG: |
| 469 | default: |
| 470 | /* TODO */ |
| 471 | return NULL; |
| 472 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 473 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 474 | return NULL; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 475 | } |
| 476 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 477 | API struct ly_module * |
| 478 | 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] | 479 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 480 | struct ly_module *module; |
| 481 | struct stat sb; |
| 482 | char *addr; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 483 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 484 | if (!ctx || fd < 0) { |
| 485 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 486 | return NULL; |
| 487 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 488 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 489 | /* |
| 490 | * TODO |
| 491 | * This is just a temporary solution to make working automatic search for |
| 492 | * imported modules. This doesn't work e.g. for streams (stdin) |
| 493 | */ |
| 494 | fstat(fd, &sb); |
| 495 | addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 496 | module = ly_module_read(ctx, addr, format); |
| 497 | munmap(addr, sb.st_size); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 498 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 499 | return module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 500 | } |
| 501 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 502 | struct ly_submodule * |
| 503 | 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] | 504 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 505 | struct ly_submodule *submodule; |
| 506 | struct stat sb; |
| 507 | char *addr; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 508 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 509 | assert(module); |
| 510 | assert(fd >= 0); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 511 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 512 | /* |
| 513 | * TODO |
| 514 | * This is just a temporary solution to make working automatic search for |
| 515 | * imported modules. This doesn't work e.g. for streams (stdin) |
| 516 | */ |
| 517 | fstat(fd, &sb); |
| 518 | addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 519 | /* TODO addr error check */ |
| 520 | submodule = ly_submodule_read(module, addr, format); |
| 521 | munmap(addr, sb.st_size); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 522 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 523 | return submodule; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 524 | |
| 525 | } |
| 526 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 527 | void |
| 528 | ly_type_dup(struct ly_ctx *ctx, struct ly_type *new, struct ly_type *old) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 529 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 530 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 531 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 532 | new->prefix = lydict_insert(ctx, old->prefix, 0); |
| 533 | new->base = old->base; |
| 534 | new->der = old->der; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 535 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 536 | switch (new->base) { |
| 537 | case LY_TYPE_ENUM: |
| 538 | new->info.enums.count = old->info.enums.count; |
| 539 | if (new->info.enums.count) { |
| 540 | new->info.enums.list = calloc(new->info.enums.count, sizeof *new->info.enums.list); |
| 541 | for (i = 0; i < new->info.enums.count; i++) { |
| 542 | new->info.enums.list[i].name = lydict_insert(ctx, old->info.enums.list[i].name, 0); |
| 543 | new->info.enums.list[i].dsc = lydict_insert(ctx, old->info.enums.list[i].dsc, 0); |
| 544 | new->info.enums.list[i].ref = lydict_insert(ctx, old->info.enums.list[i].ref, 0); |
| 545 | } |
| 546 | } |
| 547 | break; |
| 548 | default: |
| 549 | /* TODO */ |
| 550 | break; |
| 551 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 552 | } |
| 553 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 554 | void |
| 555 | ly_type_free(struct ly_ctx *ctx, struct ly_type *type) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 556 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 557 | int i; |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 558 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 559 | assert(ctx); |
| 560 | if (!type) { |
| 561 | return; |
| 562 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 563 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 564 | lydict_remove(ctx, type->prefix); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 565 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 566 | switch (type->base) { |
| 567 | case LY_TYPE_ENUM: |
| 568 | for (i = 0; i < type->info.enums.count; i++) { |
| 569 | lydict_remove(ctx, type->info.enums.list[i].name); |
| 570 | lydict_remove(ctx, type->info.enums.list[i].dsc); |
| 571 | lydict_remove(ctx, type->info.enums.list[i].ref); |
| 572 | } |
| 573 | free(type->info.enums.list); |
| 574 | break; |
| 575 | default: |
| 576 | /* TODO */ |
| 577 | break; |
| 578 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 579 | } |
| 580 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 581 | struct ly_tpdf * |
| 582 | 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] | 583 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 584 | struct ly_tpdf *result; |
| 585 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 586 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 587 | if (!size) { |
| 588 | return NULL; |
| 589 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 590 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 591 | result = calloc(size, sizeof *result); |
| 592 | for (i = 0; i < size; i++) { |
| 593 | result[i].name = lydict_insert(ctx, old[i].name, 0); |
| 594 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 595 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 596 | result[i].flags = old[i].flags; |
| 597 | result[i].module = old[i].module; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 598 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 599 | ly_type_dup(ctx, &(result[i].type), &(old[i].type)); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 600 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 601 | result[i].dflt = lydict_insert(ctx, old[i].dflt, 0); |
| 602 | result[i].units = lydict_insert(ctx, old[i].units, 0); |
| 603 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 604 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 605 | return result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 606 | } |
| 607 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 608 | void |
| 609 | ly_tpdf_free(struct ly_ctx *ctx, struct ly_tpdf *tpdf) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 610 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 611 | assert(ctx); |
| 612 | if (!tpdf) { |
| 613 | return; |
| 614 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 615 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 616 | lydict_remove(ctx, tpdf->name); |
| 617 | lydict_remove(ctx, tpdf->dsc); |
| 618 | lydict_remove(ctx, tpdf->ref); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 619 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 620 | ly_type_free(ctx, &tpdf->type); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 621 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 622 | lydict_remove(ctx, tpdf->units); |
| 623 | lydict_remove(ctx, tpdf->dflt); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 624 | } |
| 625 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 626 | struct ly_must * |
| 627 | ly_must_dup(struct ly_ctx *ctx, struct ly_must *old, int size) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 628 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 629 | struct ly_must *result; |
| 630 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 631 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 632 | if (!size) { |
| 633 | return NULL; |
| 634 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 635 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 636 | result = calloc(size, sizeof *result); |
| 637 | for (i = 0; i < size; i++) { |
| 638 | result[i].cond = lydict_insert(ctx, old[i].cond, 0); |
| 639 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 640 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 641 | result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0); |
| 642 | result[i].emsg = lydict_insert(ctx, old[i].emsg, 0); |
| 643 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 644 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 645 | return result; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 646 | } |
| 647 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 648 | void |
| 649 | ly_must_free(struct ly_ctx *ctx, struct ly_must *must) |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 650 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 651 | assert(ctx); |
| 652 | if (!must) { |
| 653 | return; |
| 654 | } |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 655 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 656 | lydict_remove(ctx, must->cond); |
| 657 | lydict_remove(ctx, must->dsc); |
| 658 | lydict_remove(ctx, must->ref); |
| 659 | lydict_remove(ctx, must->eapptag); |
| 660 | lydict_remove(ctx, must->emsg); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 661 | } |
| 662 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 663 | struct ly_augment * |
| 664 | 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] | 665 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 666 | struct ly_augment *new = NULL; |
| 667 | int i = -1; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 668 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 669 | if (!size) { |
| 670 | return NULL; |
| 671 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 672 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 673 | new = calloc(size, sizeof *new); |
| 674 | for (i = 0; i < size; i++) { |
| 675 | new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0); |
| 676 | new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0); |
| 677 | new[i].ref = lydict_insert(module->ctx, old[i].ref, 0); |
| 678 | new[i].flags = old[i].flags; |
| 679 | /* .target = NULL; .nodetype = 0 */ |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 680 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 681 | new[i].parent = parent; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 682 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 683 | /* copy the definition of augment nodes */ |
| 684 | if (old[i].child) { |
| 685 | new[i].child = (struct ly_mnode *)lyxml_dup_elem(module->ctx, (struct lyxml_elem *)old[i].child, NULL, 1); |
| 686 | } |
| 687 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 688 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 689 | return new; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 690 | } |
| 691 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 692 | struct ly_refine * |
| 693 | 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] | 694 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 695 | struct ly_refine *result; |
| 696 | int i; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 697 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 698 | if (!size) { |
| 699 | return NULL; |
| 700 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 701 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 702 | result = calloc(size, sizeof *result); |
| 703 | for (i = 0; i < size; i++) { |
| 704 | result[i].target = lydict_insert(ctx, old[i].target, 0); |
| 705 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 706 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 707 | result[i].flags = old[i].flags; |
| 708 | result[i].target_type = old[i].target_type; |
| 709 | result[i].must_size = old[i].must_size; |
| 710 | result[i].must = ly_must_dup(ctx, old[i].must, old[i].must_size); |
| 711 | if (result[i].target_type & (LY_NODE_LEAF | LY_NODE_CHOICE)) { |
| 712 | result[i].mod.dflt = lydict_insert(ctx, old[i].mod.dflt, 0); |
| 713 | } else if (result[i].target_type == LY_NODE_CONTAINER) { |
| 714 | result[i].mod.presence = lydict_insert(ctx, old[i].mod.presence, 0); |
| 715 | } else if (result[i].target_type & (LY_NODE_LIST | LY_NODE_LEAFLIST)) { |
| 716 | result[i].mod.list = old[i].mod.list; |
| 717 | } |
| 718 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 719 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 720 | return result; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 721 | } |
| 722 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 723 | void |
| 724 | ly_ident_free(struct ly_ctx *ctx, struct ly_ident *ident) |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 725 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 726 | struct ly_ident_der *der; |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 727 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 728 | assert(ctx); |
| 729 | if (!ident) { |
| 730 | return; |
| 731 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 732 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 733 | /* |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 734 | * if caller free only a single data model which is used (its identity is |
| 735 | * reference from identity in another module), this silly freeing can lead |
| 736 | * to segmentation fault. But without noting if the module is used by some |
| 737 | * other, it cannot be solved. |
Radek Krejci | a339069 | 2015-06-16 14:13:31 +0200 | [diff] [blame] | 738 | * |
| 739 | * Possible solution is to not allow caller to remove particular schema |
| 740 | * from the context. |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 741 | */ |
| 742 | while (ident->der) { |
| 743 | der = ident->der; |
| 744 | ident->der = der->next; |
| 745 | free(der); |
| 746 | } |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 747 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 748 | lydict_remove(ctx, ident->name); |
| 749 | lydict_remove(ctx, ident->dsc); |
| 750 | lydict_remove(ctx, ident->ref); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 751 | |
| 752 | } |
| 753 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 754 | void |
| 755 | ly_grp_free(struct ly_ctx *ctx, struct ly_mnode_grp *grp) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 756 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 757 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 758 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 759 | /* handle only specific parts for LY_NODE_GROUPING */ |
| 760 | for (i = 0; i < grp->tpdf_size; i++) { |
| 761 | ly_tpdf_free(ctx, &grp->tpdf[i]); |
| 762 | } |
| 763 | free(grp->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 764 | } |
| 765 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 766 | void |
| 767 | ly_anyxml_free(struct ly_ctx *ctx, struct ly_mnode_anyxml *anyxml) |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 768 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 769 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 770 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 771 | for (i = 0; i < anyxml->must_size; i++) { |
| 772 | ly_must_free(ctx, &anyxml->must[i]); |
| 773 | } |
| 774 | free(anyxml->must); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 775 | } |
| 776 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 777 | void |
| 778 | ly_leaf_free(struct ly_ctx *ctx, struct ly_mnode_leaf *leaf) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 779 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 780 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 781 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 782 | for (i = 0; i < leaf->must_size; i++) { |
| 783 | ly_must_free(ctx, &leaf->must[i]); |
| 784 | } |
| 785 | free(leaf->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 786 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 787 | ly_type_free(ctx, &leaf->type); |
| 788 | lydict_remove(ctx, leaf->units); |
| 789 | lydict_remove(ctx, leaf->dflt); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 790 | } |
| 791 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 792 | void |
| 793 | ly_leaflist_free(struct ly_ctx *ctx, struct ly_mnode_leaflist *llist) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 794 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 795 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 796 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 797 | for (i = 0; i < llist->must_size; i++) { |
| 798 | ly_must_free(ctx, &llist->must[i]); |
| 799 | } |
| 800 | free(llist->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 801 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 802 | ly_type_free(ctx, &llist->type); |
| 803 | lydict_remove(ctx, llist->units); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 804 | } |
| 805 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 806 | void |
| 807 | ly_list_free(struct ly_ctx *ctx, struct ly_mnode_list *list) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 808 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 809 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 810 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 811 | /* handle only specific parts for LY_NODE_LIST */ |
| 812 | for (i = 0; i < list->tpdf_size; i++) { |
| 813 | ly_tpdf_free(ctx, &list->tpdf[i]); |
| 814 | } |
| 815 | free(list->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 816 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 817 | for (i = 0; i < list->must_size; i++) { |
| 818 | ly_must_free(ctx, &list->must[i]); |
| 819 | } |
| 820 | free(list->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 821 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 822 | for (i = 0; i < list->unique_size; i++) { |
| 823 | free(list->unique[i].leafs); |
| 824 | } |
| 825 | free(list->unique); |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 826 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 827 | free(list->keys); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 828 | } |
| 829 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 830 | void |
| 831 | ly_container_free(struct ly_ctx *ctx, struct ly_mnode_container *cont) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 832 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 833 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 834 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 835 | /* handle only specific parts for LY_NODE_CONTAINER */ |
| 836 | lydict_remove(ctx, cont->presence); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 837 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 838 | for (i = 0; i < cont->tpdf_size; i++) { |
| 839 | ly_tpdf_free(ctx, &cont->tpdf[i]); |
| 840 | } |
| 841 | free(cont->tpdf); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 842 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 843 | for (i = 0; i < cont->must_size; i++) { |
| 844 | ly_must_free(ctx, &cont->must[i]); |
| 845 | } |
| 846 | free(cont->must); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 847 | } |
| 848 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 849 | void |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 850 | ly_augment_free(struct ly_ctx *ctx, struct ly_augment *aug) |
| 851 | { |
| 852 | lydict_remove(ctx, aug->target_name); |
| 853 | lydict_remove(ctx, aug->dsc); |
| 854 | lydict_remove(ctx, aug->ref); |
| 855 | |
| 856 | lyxml_free_elem(ctx, (struct lyxml_elem *)aug->child); |
| 857 | } |
| 858 | |
| 859 | void |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 860 | ly_uses_free(struct ly_ctx *ctx, struct ly_mnode_uses *uses) |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 861 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 862 | int i, j; |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 863 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 864 | for (i = 0; i < uses->refine_size; i++) { |
| 865 | lydict_remove(ctx, uses->refine[i].target); |
| 866 | lydict_remove(ctx, uses->refine[i].dsc); |
| 867 | lydict_remove(ctx, uses->refine[i].ref); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 868 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 869 | for (j = 0; j < uses->refine[j].must_size; j++) { |
| 870 | ly_must_free(ctx, &uses->refine[i].must[j]); |
| 871 | } |
| 872 | free(uses->refine[i].must); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 873 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 874 | if (uses->refine[i].target_type & (LY_NODE_LEAF | LY_NODE_CHOICE)) { |
| 875 | lydict_remove(ctx, uses->refine[i].mod.dflt); |
| 876 | } else if (uses->refine[i].target_type & LY_NODE_CONTAINER) { |
| 877 | lydict_remove(ctx, uses->refine[i].mod.presence); |
| 878 | } |
| 879 | } |
| 880 | free(uses->refine); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 881 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 882 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 883 | ly_augment_free(ctx, &uses->augment[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 884 | } |
| 885 | free(uses->augment); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 886 | |
| 887 | } |
| 888 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 889 | void |
| 890 | ly_mnode_free(struct ly_mnode *node) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 891 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 892 | struct ly_ctx *ctx; |
| 893 | struct ly_mnode *sub, *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 894 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 895 | if (!node) { |
| 896 | return; |
| 897 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 898 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 899 | assert(node->module); |
| 900 | assert(node->module->ctx); |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 901 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 902 | ctx = node->module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 903 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 904 | /* common part */ |
| 905 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
| 906 | ly_mnode_free(sub); |
| 907 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 908 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 909 | lydict_remove(ctx, node->name); |
| 910 | lydict_remove(ctx, node->dsc); |
| 911 | lydict_remove(ctx, node->ref); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 912 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 913 | /* specific part */ |
| 914 | switch (node->nodetype) { |
| 915 | case LY_NODE_CONTAINER: |
| 916 | ly_container_free(ctx, (struct ly_mnode_container *)node); |
| 917 | break; |
| 918 | case LY_NODE_CHOICE: |
| 919 | break; |
| 920 | case LY_NODE_LEAF: |
| 921 | ly_leaf_free(ctx, (struct ly_mnode_leaf *)node); |
| 922 | break; |
| 923 | case LY_NODE_LEAFLIST: |
| 924 | ly_leaflist_free(ctx, (struct ly_mnode_leaflist *)node); |
| 925 | break; |
| 926 | case LY_NODE_LIST: |
| 927 | ly_list_free(ctx, (struct ly_mnode_list *)node); |
| 928 | break; |
| 929 | case LY_NODE_ANYXML: |
| 930 | ly_anyxml_free(ctx, (struct ly_mnode_anyxml *)node); |
| 931 | break; |
| 932 | case LY_NODE_USES: |
| 933 | ly_uses_free(ctx, (struct ly_mnode_uses *)node); |
| 934 | break; |
| 935 | case LY_NODE_CASE: |
| 936 | case LY_NODE_AUGMENT: |
| 937 | /* do nothing */ |
| 938 | break; |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 939 | case LY_NODE_GROUPING: |
| 940 | case LY_NODE_RPC: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 941 | case LY_NODE_INPUT: |
| 942 | case LY_NODE_OUTPUT: |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 943 | case LY_NODE_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 944 | ly_grp_free(ctx, (struct ly_mnode_grp *)node); |
| 945 | break; |
| 946 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 947 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 948 | /* again common part */ |
| 949 | ly_mnode_unlink(node); |
| 950 | free(node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 951 | } |
| 952 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 953 | static void |
| 954 | module_free_common(struct ly_module *module) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 955 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 956 | struct ly_ctx *ctx; |
| 957 | struct ly_mnode *mnode; |
| 958 | unsigned int i; |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame^] | 959 | int j, l; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 960 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 961 | assert(module->ctx); |
| 962 | ctx = module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 963 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame^] | 964 | /* as first step, free the imported modules */ |
| 965 | for (i = 0; i < module->imp_size; i++) { |
| 966 | /* get the imported module from the context and then free, |
| 967 | * this check is necessary because the imported module can |
| 968 | * be already removed |
| 969 | */ |
| 970 | l = ctx->models.used; |
| 971 | for (j = 0; j < l; j++) { |
| 972 | if (ctx->models.list[j] == module->imp[i].module) { |
| 973 | ly_module_free(module->imp[i].module); |
| 974 | break; |
| 975 | } |
| 976 | } |
| 977 | } |
| 978 | free(module->imp); |
| 979 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 980 | while (module->data) { |
| 981 | mnode = module->data; |
| 982 | module->data = mnode; |
| 983 | ly_mnode_free(mnode); |
| 984 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 985 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 986 | lydict_remove(ctx, module->dsc); |
| 987 | lydict_remove(ctx, module->ref); |
| 988 | lydict_remove(ctx, module->org); |
| 989 | lydict_remove(ctx, module->contact); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 990 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 991 | for (i = 0; i < module->rev_size; i++) { |
| 992 | lydict_remove(ctx, module->rev[i].dsc); |
| 993 | lydict_remove(ctx, module->rev[i].ref); |
| 994 | } |
| 995 | free(module->rev); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 996 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 997 | for (i = 0; i < module->ident_size; i++) { |
| 998 | ly_ident_free(ctx, &module->ident[i]); |
| 999 | } |
| 1000 | module->ident_size = 0; |
| 1001 | free(module->ident); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1002 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1003 | for (i = 0; i < module->tpdf_size; i++) { |
| 1004 | ly_tpdf_free(ctx, &module->tpdf[i]); |
| 1005 | } |
| 1006 | free(module->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1007 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1008 | for (i = 0; i < module->inc_size; i++) { |
| 1009 | ly_submodule_free(module->inc[i].submodule); |
| 1010 | } |
| 1011 | free(module->inc); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1012 | |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 1013 | for (i = 0; i < module->augment_size; i++) { |
| 1014 | ly_augment_free(ctx, &module->augment[i]); |
| 1015 | } |
| 1016 | free(module->augment); |
| 1017 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1018 | lydict_remove(ctx, module->name); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1019 | } |
| 1020 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1021 | void |
| 1022 | ly_submodule_free(struct ly_submodule *submodule) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1023 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1024 | if (!submodule) { |
| 1025 | return; |
| 1026 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1027 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1028 | submodule->inc_size = 0; |
| 1029 | free(submodule->inc); |
| 1030 | submodule->inc = NULL; |
Radek Krejci | f388693 | 2015-06-04 17:36:06 +0200 | [diff] [blame] | 1031 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1032 | /* common part with struct ly_module */ |
| 1033 | module_free_common((struct ly_module *)submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1034 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1035 | /* no specific items to free */ |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1036 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1037 | free(submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1038 | } |
| 1039 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1040 | struct ly_mnode * |
| 1041 | 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] | 1042 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1043 | struct ly_mnode *retval = NULL, *aux, *child; |
| 1044 | struct ly_ctx *ctx = module->ctx; |
| 1045 | int i, j; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1046 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1047 | struct ly_mnode_container *cont; |
| 1048 | struct ly_mnode_container *cont_orig = (struct ly_mnode_container *)mnode; |
| 1049 | struct ly_mnode_choice *choice; |
| 1050 | struct ly_mnode_choice *choice_orig = (struct ly_mnode_choice *)mnode; |
| 1051 | struct ly_mnode_leaf *leaf; |
| 1052 | struct ly_mnode_leaf *leaf_orig = (struct ly_mnode_leaf *)mnode; |
| 1053 | struct ly_mnode_leaflist *llist; |
| 1054 | struct ly_mnode_leaflist *llist_orig = (struct ly_mnode_leaflist *)mnode; |
| 1055 | struct ly_mnode_list *list; |
| 1056 | struct ly_mnode_list *list_orig = (struct ly_mnode_list *)mnode; |
| 1057 | struct ly_mnode_anyxml *anyxml; |
| 1058 | struct ly_mnode_anyxml *anyxml_orig = (struct ly_mnode_anyxml *)mnode; |
| 1059 | struct ly_mnode_uses *uses; |
| 1060 | struct ly_mnode_uses *uses_orig = (struct ly_mnode_uses *)mnode; |
| 1061 | struct ly_mnode_grp *mix; |
| 1062 | struct ly_mnode_grp *mix_orig = (struct ly_mnode_grp *)mnode; |
| 1063 | struct ly_mnode_case *cs; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1064 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1065 | /* we cannot just duplicate memory since the strings are stored in |
| 1066 | * dictionary and we need to update dictionary counters. |
| 1067 | */ |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1068 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1069 | switch (mnode->nodetype) { |
| 1070 | case LY_NODE_CONTAINER: |
| 1071 | cont = calloc(1, sizeof *cont); |
| 1072 | retval = (struct ly_mnode *)cont; |
| 1073 | break; |
| 1074 | |
| 1075 | case LY_NODE_CHOICE: |
| 1076 | choice = calloc(1, sizeof *choice); |
| 1077 | retval = (struct ly_mnode *)choice; |
| 1078 | break; |
| 1079 | |
| 1080 | case LY_NODE_LEAF: |
| 1081 | leaf = calloc(1, sizeof *leaf); |
| 1082 | retval = (struct ly_mnode *)leaf; |
| 1083 | break; |
| 1084 | |
| 1085 | case LY_NODE_LEAFLIST: |
| 1086 | llist = calloc(1, sizeof *llist); |
| 1087 | retval = (struct ly_mnode *)llist; |
| 1088 | break; |
| 1089 | |
| 1090 | case LY_NODE_LIST: |
| 1091 | list = calloc(1, sizeof *list); |
| 1092 | retval = (struct ly_mnode *)list; |
| 1093 | break; |
| 1094 | |
| 1095 | case LY_NODE_ANYXML: |
| 1096 | anyxml = calloc(1, sizeof *anyxml); |
| 1097 | retval = (struct ly_mnode *)anyxml; |
| 1098 | break; |
| 1099 | |
| 1100 | case LY_NODE_USES: |
| 1101 | uses = calloc(1, sizeof *uses); |
| 1102 | retval = (struct ly_mnode *)uses; |
| 1103 | break; |
| 1104 | |
| 1105 | case LY_NODE_CASE: |
| 1106 | cs = calloc(1, sizeof *cs); |
| 1107 | retval = (struct ly_mnode *)cs; |
| 1108 | break; |
| 1109 | |
| 1110 | /* exact same structure */ |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 1111 | case LY_NODE_GROUPING: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 1112 | case LY_NODE_RPC: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 1113 | case LY_NODE_INPUT: |
| 1114 | case LY_NODE_OUTPUT: |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 1115 | case LY_NODE_NOTIF: |
| 1116 | mix = calloc(1, sizeof *mix); |
| 1117 | retval = (struct ly_mnode *)mix; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 1118 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1119 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1120 | default: |
| 1121 | goto error; |
| 1122 | } |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame] | 1123 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1124 | /* |
| 1125 | * duplicate generic part of the structure |
| 1126 | */ |
| 1127 | retval->name = lydict_insert(ctx, mnode->name, 0); |
| 1128 | retval->dsc = lydict_insert(ctx, mnode->dsc, 0); |
| 1129 | retval->ref = lydict_insert(ctx, mnode->ref, 0); |
| 1130 | retval->flags = mnode->flags; |
| 1131 | if (!(retval->flags & LY_NODE_CONFIG_MASK)) { |
| 1132 | /* set parent's config flag */ |
| 1133 | retval->flags |= flags & LY_NODE_CONFIG_MASK; |
| 1134 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1135 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1136 | retval->module = module; |
| 1137 | retval->nodetype = mnode->nodetype; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1138 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1139 | retval->prev = retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1140 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1141 | retval->feature = NULL; /* TODO */ |
| 1142 | retval->when = NULL; /* TODO */ |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1143 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1144 | if (recursive) { |
| 1145 | /* go recursively */ |
| 1146 | LY_TREE_FOR(mnode->child, child) { |
| 1147 | aux = ly_mnode_dup(module, child, retval->flags, 1, line); |
| 1148 | if (!aux || ly_mnode_addchild(retval, aux)) { |
| 1149 | goto error; |
| 1150 | } |
| 1151 | } |
| 1152 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1153 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1154 | /* |
| 1155 | * duplicate specific part of the structure |
| 1156 | */ |
| 1157 | switch (mnode->nodetype) { |
| 1158 | case LY_NODE_CONTAINER: |
| 1159 | cont->presence = lydict_insert(ctx, cont_orig->presence, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1160 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1161 | cont->must_size = cont_orig->must_size; |
| 1162 | cont->tpdf_size = cont_orig->tpdf_size; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1163 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1164 | cont->must = ly_must_dup(ctx, cont_orig->must, cont->must_size); |
| 1165 | cont->tpdf = ly_tpdf_dup(ctx, cont_orig->tpdf, cont->tpdf_size); |
| 1166 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1167 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1168 | case LY_NODE_CHOICE: |
| 1169 | if (choice->dflt) { |
| 1170 | LY_TREE_FOR(choice->child, child) { |
| 1171 | if (child->name == choice_orig->dflt->name) { |
| 1172 | choice->dflt = child; |
| 1173 | break; |
| 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1178 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1179 | case LY_NODE_LEAF: |
| 1180 | ly_type_dup(ctx, &(leaf->type), &(leaf_orig->type)); |
| 1181 | leaf->units = lydict_insert(ctx, leaf_orig->units, 0); |
| 1182 | leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1183 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1184 | leaf->must_size = leaf_orig->must_size; |
| 1185 | leaf->must = ly_must_dup(ctx, leaf_orig->must, leaf->must_size); |
| 1186 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1187 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1188 | case LY_NODE_LEAFLIST: |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1189 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1190 | ly_type_dup(ctx, &(llist->type), &(llist_orig->type)); |
| 1191 | llist->units = lydict_insert(ctx, llist_orig->units, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1192 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1193 | llist->min = llist_orig->min; |
| 1194 | llist->max = llist_orig->max; |
| 1195 | |
| 1196 | llist->must_size = llist_orig->must_size; |
| 1197 | llist->must = ly_must_dup(ctx, llist_orig->must, llist->must_size); |
| 1198 | break; |
| 1199 | |
| 1200 | case LY_NODE_LIST: |
| 1201 | list->min = list_orig->min; |
| 1202 | list->max = list_orig->max; |
| 1203 | |
| 1204 | list->must_size = list_orig->must_size; |
| 1205 | list->tpdf_size = list_orig->tpdf_size; |
| 1206 | list->keys_size = list_orig->keys_size; |
| 1207 | list->unique_size = list_orig->unique_size; |
| 1208 | |
| 1209 | list->must = ly_must_dup(ctx, list_orig->must, list->must_size); |
| 1210 | list->tpdf = ly_tpdf_dup(ctx, list_orig->tpdf, list->tpdf_size); |
| 1211 | |
| 1212 | if (list->keys_size) { |
| 1213 | list->keys = calloc(list->keys_size, sizeof *list->keys); |
| 1214 | for (i = 0; i < list->keys_size; i++) { |
| 1215 | list->keys[i] = find_leaf(retval, list_orig->keys[i]->name, 0); |
| 1216 | } |
| 1217 | } |
| 1218 | if (list->unique_size) { |
| 1219 | list->unique = calloc(list->unique_size, sizeof *list->unique); |
| 1220 | for (i = 0; i < list->unique_size; i++) { |
| 1221 | list->unique[i].leafs = calloc(list->unique[i].leafs_size, sizeof *list->unique[i].leafs); |
| 1222 | for (j = 0; j < list->unique[i].leafs_size; j++) { |
| 1223 | list->unique[i].leafs[j] = find_leaf(retval, list_orig->unique[i].leafs[j]->name, 0); |
| 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | break; |
| 1228 | |
| 1229 | case LY_NODE_ANYXML: |
| 1230 | anyxml->must_size = anyxml_orig->must_size; |
| 1231 | anyxml->must = ly_must_dup(ctx, anyxml_orig->must, anyxml->must_size); |
| 1232 | break; |
| 1233 | |
| 1234 | case LY_NODE_USES: |
| 1235 | uses->grp = uses_orig->grp; |
| 1236 | uses->refine_size = uses_orig->refine_size; |
| 1237 | uses->refine = ly_refine_dup(ctx, uses_orig->refine, uses_orig->refine_size); |
| 1238 | uses->augment_size = uses_orig->augment_size; |
| 1239 | uses->augment = ly_augment_dup(module, (struct ly_mnode *)uses, uses_orig->augment, uses_orig->augment_size); |
| 1240 | if (resolve_uses(uses, line)) { |
| 1241 | goto error; |
| 1242 | } |
| 1243 | break; |
| 1244 | |
| 1245 | case LY_NODE_CASE: |
| 1246 | /* nothing to do */ |
| 1247 | break; |
| 1248 | |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 1249 | case LY_NODE_GROUPING: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 1250 | case LY_NODE_RPC: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 1251 | case LY_NODE_INPUT: |
| 1252 | case LY_NODE_OUTPUT: |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 1253 | case LY_NODE_NOTIF: |
| 1254 | mix->tpdf_size = mix_orig->tpdf_size; |
| 1255 | mix->tpdf = ly_tpdf_dup(ctx, mix_orig->tpdf, mix->tpdf_size); |
Radek Krejci | a01e543 | 2015-06-16 10:35:25 +0200 | [diff] [blame] | 1256 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1257 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1258 | default: |
| 1259 | /* LY_NODE_NOTIF */ |
| 1260 | goto error; |
| 1261 | } |
| 1262 | |
| 1263 | return retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1264 | |
| 1265 | error: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1266 | LOGDBG("error: %s:%d", __FILE__, __LINE__); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1267 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1268 | ly_mnode_free(retval); |
| 1269 | return NULL; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1270 | } |
| 1271 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1272 | API void |
| 1273 | ly_module_free(struct ly_module *module) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1274 | { |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame^] | 1275 | struct ly_ctx *ctx; |
| 1276 | int i; |
| 1277 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1278 | if (!module) { |
| 1279 | return; |
| 1280 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1281 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame^] | 1282 | /* remove schema from the context */ |
| 1283 | ctx = module->ctx; |
| 1284 | if (ctx->models.used) { |
| 1285 | for (i = 0; i < ctx->models.used; i++) { |
| 1286 | if (ctx->models.list[i] == module) { |
| 1287 | /* replace the position in the list by the last module in the list */ |
| 1288 | ctx->models.used--; |
| 1289 | ctx->models.list[i] = ctx->models.list[ctx->models.used]; |
| 1290 | ctx->models.list[ctx->models.used] = NULL; |
| 1291 | /* we are done */ |
| 1292 | break; |
| 1293 | } |
| 1294 | } |
| 1295 | } |
| 1296 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1297 | /* common part with struct ly_submodule */ |
| 1298 | module_free_common(module); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1299 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1300 | /* specific items to free */ |
| 1301 | lydict_remove(module->ctx, module->ns); |
| 1302 | lydict_remove(module->ctx, module->prefix); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1303 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1304 | free(module); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1305 | } |