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