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) { |
| 615 | case LY_TYPE_ENUM: |
| 616 | for (i = 0; i < type->info.enums.count; i++) { |
| 617 | lydict_remove(ctx, type->info.enums.list[i].name); |
| 618 | lydict_remove(ctx, type->info.enums.list[i].dsc); |
| 619 | lydict_remove(ctx, type->info.enums.list[i].ref); |
| 620 | } |
| 621 | free(type->info.enums.list); |
| 622 | break; |
| 623 | default: |
| 624 | /* TODO */ |
| 625 | break; |
| 626 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 627 | } |
| 628 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 629 | struct ly_tpdf * |
| 630 | 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] | 631 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 632 | struct ly_tpdf *result; |
| 633 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 634 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 635 | if (!size) { |
| 636 | return NULL; |
| 637 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 638 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 639 | result = calloc(size, sizeof *result); |
| 640 | for (i = 0; i < size; i++) { |
| 641 | result[i].name = lydict_insert(ctx, old[i].name, 0); |
| 642 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 643 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 644 | result[i].flags = old[i].flags; |
| 645 | result[i].module = old[i].module; |
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 | ly_type_dup(ctx, &(result[i].type), &(old[i].type)); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 648 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 649 | result[i].dflt = lydict_insert(ctx, old[i].dflt, 0); |
| 650 | result[i].units = lydict_insert(ctx, old[i].units, 0); |
| 651 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 652 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 653 | return result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 654 | } |
| 655 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 656 | void |
| 657 | ly_tpdf_free(struct ly_ctx *ctx, struct ly_tpdf *tpdf) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 658 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 659 | assert(ctx); |
| 660 | if (!tpdf) { |
| 661 | return; |
| 662 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 663 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 664 | lydict_remove(ctx, tpdf->name); |
| 665 | lydict_remove(ctx, tpdf->dsc); |
| 666 | lydict_remove(ctx, tpdf->ref); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 667 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 668 | ly_type_free(ctx, &tpdf->type); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 669 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 670 | lydict_remove(ctx, tpdf->units); |
| 671 | lydict_remove(ctx, tpdf->dflt); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 672 | } |
| 673 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 674 | struct ly_must * |
| 675 | 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] | 676 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 677 | struct ly_must *result; |
| 678 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 679 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 680 | if (!size) { |
| 681 | return NULL; |
| 682 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 683 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 684 | result = calloc(size, sizeof *result); |
| 685 | for (i = 0; i < size; i++) { |
| 686 | result[i].cond = lydict_insert(ctx, old[i].cond, 0); |
| 687 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 688 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 689 | result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0); |
| 690 | result[i].emsg = lydict_insert(ctx, old[i].emsg, 0); |
| 691 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 692 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 693 | return result; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 694 | } |
| 695 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 696 | void |
| 697 | ly_must_free(struct ly_ctx *ctx, struct ly_must *must) |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 698 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 699 | assert(ctx); |
| 700 | if (!must) { |
| 701 | return; |
| 702 | } |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 703 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 704 | lydict_remove(ctx, must->cond); |
| 705 | lydict_remove(ctx, must->dsc); |
| 706 | lydict_remove(ctx, must->ref); |
| 707 | lydict_remove(ctx, must->eapptag); |
| 708 | lydict_remove(ctx, must->emsg); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 709 | } |
| 710 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 711 | struct ly_augment * |
| 712 | 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] | 713 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 714 | struct ly_augment *new = NULL; |
| 715 | int i = -1; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 716 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 717 | if (!size) { |
| 718 | return NULL; |
| 719 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 720 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 721 | new = calloc(size, sizeof *new); |
| 722 | for (i = 0; i < size; i++) { |
| 723 | new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0); |
| 724 | new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0); |
| 725 | new[i].ref = lydict_insert(module->ctx, old[i].ref, 0); |
| 726 | new[i].flags = old[i].flags; |
| 727 | /* .target = NULL; .nodetype = 0 */ |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 728 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 729 | new[i].parent = parent; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 730 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 731 | /* copy the definition of augment nodes */ |
| 732 | if (old[i].child) { |
| 733 | new[i].child = (struct ly_mnode *)lyxml_dup_elem(module->ctx, (struct lyxml_elem *)old[i].child, NULL, 1); |
| 734 | } |
| 735 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 736 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 737 | return new; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 738 | } |
| 739 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 740 | struct ly_refine * |
| 741 | 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] | 742 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 743 | struct ly_refine *result; |
| 744 | int i; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 745 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 746 | if (!size) { |
| 747 | return NULL; |
| 748 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 749 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 750 | result = calloc(size, sizeof *result); |
| 751 | for (i = 0; i < size; i++) { |
| 752 | result[i].target = lydict_insert(ctx, old[i].target, 0); |
| 753 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 754 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 755 | result[i].flags = old[i].flags; |
| 756 | result[i].target_type = old[i].target_type; |
| 757 | result[i].must_size = old[i].must_size; |
| 758 | result[i].must = ly_must_dup(ctx, old[i].must, old[i].must_size); |
| 759 | if (result[i].target_type & (LY_NODE_LEAF | LY_NODE_CHOICE)) { |
| 760 | result[i].mod.dflt = lydict_insert(ctx, old[i].mod.dflt, 0); |
| 761 | } else if (result[i].target_type == LY_NODE_CONTAINER) { |
| 762 | result[i].mod.presence = lydict_insert(ctx, old[i].mod.presence, 0); |
| 763 | } else if (result[i].target_type & (LY_NODE_LIST | LY_NODE_LEAFLIST)) { |
| 764 | result[i].mod.list = old[i].mod.list; |
| 765 | } |
| 766 | } |
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 | return result; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 769 | } |
| 770 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 771 | void |
| 772 | ly_ident_free(struct ly_ctx *ctx, struct ly_ident *ident) |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 773 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 774 | struct ly_ident_der *der; |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 775 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 776 | assert(ctx); |
| 777 | if (!ident) { |
| 778 | return; |
| 779 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 780 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 781 | /* |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 782 | * if caller free only a single data model which is used (its identity is |
| 783 | * reference from identity in another module), this silly freeing can lead |
| 784 | * to segmentation fault. But without noting if the module is used by some |
| 785 | * other, it cannot be solved. |
Radek Krejci | a339069 | 2015-06-16 14:13:31 +0200 | [diff] [blame] | 786 | * |
| 787 | * Possible solution is to not allow caller to remove particular schema |
| 788 | * from the context. |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 789 | */ |
| 790 | while (ident->der) { |
| 791 | der = ident->der; |
| 792 | ident->der = der->next; |
| 793 | free(der); |
| 794 | } |
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 | lydict_remove(ctx, ident->name); |
| 797 | lydict_remove(ctx, ident->dsc); |
| 798 | lydict_remove(ctx, ident->ref); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 799 | |
| 800 | } |
| 801 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 802 | void |
| 803 | ly_grp_free(struct ly_ctx *ctx, struct ly_mnode_grp *grp) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 804 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 805 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 806 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 807 | /* handle only specific parts for LY_NODE_GROUPING */ |
| 808 | for (i = 0; i < grp->tpdf_size; i++) { |
| 809 | ly_tpdf_free(ctx, &grp->tpdf[i]); |
| 810 | } |
| 811 | free(grp->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 812 | } |
| 813 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 814 | void |
| 815 | ly_anyxml_free(struct ly_ctx *ctx, struct ly_mnode_anyxml *anyxml) |
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 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 818 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 819 | for (i = 0; i < anyxml->must_size; i++) { |
| 820 | ly_must_free(ctx, &anyxml->must[i]); |
| 821 | } |
| 822 | free(anyxml->must); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 823 | } |
| 824 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 825 | void |
| 826 | ly_leaf_free(struct ly_ctx *ctx, struct ly_mnode_leaf *leaf) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 827 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 828 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 829 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 830 | for (i = 0; i < leaf->must_size; i++) { |
| 831 | ly_must_free(ctx, &leaf->must[i]); |
| 832 | } |
| 833 | free(leaf->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 834 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 835 | ly_type_free(ctx, &leaf->type); |
| 836 | lydict_remove(ctx, leaf->units); |
| 837 | lydict_remove(ctx, leaf->dflt); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 838 | } |
| 839 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 840 | void |
| 841 | ly_leaflist_free(struct ly_ctx *ctx, struct ly_mnode_leaflist *llist) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 842 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 843 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 844 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 845 | for (i = 0; i < llist->must_size; i++) { |
| 846 | ly_must_free(ctx, &llist->must[i]); |
| 847 | } |
| 848 | free(llist->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 849 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 850 | ly_type_free(ctx, &llist->type); |
| 851 | lydict_remove(ctx, llist->units); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 852 | } |
| 853 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 854 | void |
| 855 | ly_list_free(struct ly_ctx *ctx, struct ly_mnode_list *list) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 856 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 857 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 858 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 859 | /* handle only specific parts for LY_NODE_LIST */ |
| 860 | for (i = 0; i < list->tpdf_size; i++) { |
| 861 | ly_tpdf_free(ctx, &list->tpdf[i]); |
| 862 | } |
| 863 | free(list->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 864 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 865 | for (i = 0; i < list->must_size; i++) { |
| 866 | ly_must_free(ctx, &list->must[i]); |
| 867 | } |
| 868 | free(list->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 869 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 870 | for (i = 0; i < list->unique_size; i++) { |
| 871 | free(list->unique[i].leafs); |
| 872 | } |
| 873 | free(list->unique); |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 874 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 875 | free(list->keys); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 876 | } |
| 877 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 878 | void |
| 879 | ly_container_free(struct ly_ctx *ctx, struct ly_mnode_container *cont) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 880 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 881 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 882 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 883 | /* handle only specific parts for LY_NODE_CONTAINER */ |
| 884 | lydict_remove(ctx, cont->presence); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 885 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 886 | for (i = 0; i < cont->tpdf_size; i++) { |
| 887 | ly_tpdf_free(ctx, &cont->tpdf[i]); |
| 888 | } |
| 889 | free(cont->tpdf); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 890 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 891 | for (i = 0; i < cont->must_size; i++) { |
| 892 | ly_must_free(ctx, &cont->must[i]); |
| 893 | } |
| 894 | free(cont->must); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 895 | } |
| 896 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 897 | void |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 898 | ly_augment_free(struct ly_ctx *ctx, struct ly_augment *aug) |
| 899 | { |
| 900 | lydict_remove(ctx, aug->target_name); |
| 901 | lydict_remove(ctx, aug->dsc); |
| 902 | lydict_remove(ctx, aug->ref); |
| 903 | |
| 904 | lyxml_free_elem(ctx, (struct lyxml_elem *)aug->child); |
| 905 | } |
| 906 | |
| 907 | void |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 908 | ly_uses_free(struct ly_ctx *ctx, struct ly_mnode_uses *uses) |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 909 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 910 | int i, j; |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 911 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 912 | for (i = 0; i < uses->refine_size; i++) { |
| 913 | lydict_remove(ctx, uses->refine[i].target); |
| 914 | lydict_remove(ctx, uses->refine[i].dsc); |
| 915 | lydict_remove(ctx, uses->refine[i].ref); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 916 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 917 | for (j = 0; j < uses->refine[j].must_size; j++) { |
| 918 | ly_must_free(ctx, &uses->refine[i].must[j]); |
| 919 | } |
| 920 | free(uses->refine[i].must); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 921 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 922 | if (uses->refine[i].target_type & (LY_NODE_LEAF | LY_NODE_CHOICE)) { |
| 923 | lydict_remove(ctx, uses->refine[i].mod.dflt); |
| 924 | } else if (uses->refine[i].target_type & LY_NODE_CONTAINER) { |
| 925 | lydict_remove(ctx, uses->refine[i].mod.presence); |
| 926 | } |
| 927 | } |
| 928 | free(uses->refine); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 929 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 930 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 931 | ly_augment_free(ctx, &uses->augment[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 932 | } |
| 933 | free(uses->augment); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 934 | |
| 935 | } |
| 936 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 937 | void |
| 938 | ly_mnode_free(struct ly_mnode *node) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 939 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 940 | struct ly_ctx *ctx; |
| 941 | struct ly_mnode *sub, *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 942 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 943 | if (!node) { |
| 944 | return; |
| 945 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 946 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 947 | assert(node->module); |
| 948 | assert(node->module->ctx); |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 949 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 950 | ctx = node->module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 951 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 952 | /* common part */ |
| 953 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
| 954 | ly_mnode_free(sub); |
| 955 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 956 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 957 | lydict_remove(ctx, node->name); |
| 958 | lydict_remove(ctx, node->dsc); |
| 959 | lydict_remove(ctx, node->ref); |
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 | /* specific part */ |
| 962 | switch (node->nodetype) { |
| 963 | case LY_NODE_CONTAINER: |
| 964 | ly_container_free(ctx, (struct ly_mnode_container *)node); |
| 965 | break; |
| 966 | case LY_NODE_CHOICE: |
| 967 | break; |
| 968 | case LY_NODE_LEAF: |
| 969 | ly_leaf_free(ctx, (struct ly_mnode_leaf *)node); |
| 970 | break; |
| 971 | case LY_NODE_LEAFLIST: |
| 972 | ly_leaflist_free(ctx, (struct ly_mnode_leaflist *)node); |
| 973 | break; |
| 974 | case LY_NODE_LIST: |
| 975 | ly_list_free(ctx, (struct ly_mnode_list *)node); |
| 976 | break; |
| 977 | case LY_NODE_ANYXML: |
| 978 | ly_anyxml_free(ctx, (struct ly_mnode_anyxml *)node); |
| 979 | break; |
| 980 | case LY_NODE_USES: |
| 981 | ly_uses_free(ctx, (struct ly_mnode_uses *)node); |
| 982 | break; |
| 983 | case LY_NODE_CASE: |
| 984 | case LY_NODE_AUGMENT: |
| 985 | /* do nothing */ |
| 986 | break; |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 987 | case LY_NODE_GROUPING: |
| 988 | case LY_NODE_RPC: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 989 | case LY_NODE_INPUT: |
| 990 | case LY_NODE_OUTPUT: |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 991 | case LY_NODE_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 992 | ly_grp_free(ctx, (struct ly_mnode_grp *)node); |
| 993 | break; |
| 994 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 995 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 996 | /* again common part */ |
| 997 | ly_mnode_unlink(node); |
| 998 | free(node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 999 | } |
| 1000 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1001 | static void |
| 1002 | module_free_common(struct ly_module *module) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1003 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1004 | struct ly_ctx *ctx; |
| 1005 | struct ly_mnode *mnode; |
| 1006 | unsigned int i; |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 1007 | int j, l; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1008 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1009 | assert(module->ctx); |
| 1010 | ctx = module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1011 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 1012 | /* as first step, free the imported modules */ |
| 1013 | for (i = 0; i < module->imp_size; i++) { |
| 1014 | /* get the imported module from the context and then free, |
| 1015 | * this check is necessary because the imported module can |
| 1016 | * be already removed |
| 1017 | */ |
| 1018 | l = ctx->models.used; |
| 1019 | for (j = 0; j < l; j++) { |
| 1020 | if (ctx->models.list[j] == module->imp[i].module) { |
| 1021 | ly_module_free(module->imp[i].module); |
| 1022 | break; |
| 1023 | } |
| 1024 | } |
| 1025 | } |
| 1026 | free(module->imp); |
| 1027 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1028 | while (module->data) { |
| 1029 | mnode = module->data; |
| 1030 | module->data = mnode; |
| 1031 | ly_mnode_free(mnode); |
| 1032 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1033 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1034 | lydict_remove(ctx, module->dsc); |
| 1035 | lydict_remove(ctx, module->ref); |
| 1036 | lydict_remove(ctx, module->org); |
| 1037 | lydict_remove(ctx, module->contact); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1038 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1039 | for (i = 0; i < module->rev_size; i++) { |
| 1040 | lydict_remove(ctx, module->rev[i].dsc); |
| 1041 | lydict_remove(ctx, module->rev[i].ref); |
| 1042 | } |
| 1043 | free(module->rev); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1044 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1045 | for (i = 0; i < module->ident_size; i++) { |
| 1046 | ly_ident_free(ctx, &module->ident[i]); |
| 1047 | } |
| 1048 | module->ident_size = 0; |
| 1049 | free(module->ident); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1050 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1051 | for (i = 0; i < module->tpdf_size; i++) { |
| 1052 | ly_tpdf_free(ctx, &module->tpdf[i]); |
| 1053 | } |
| 1054 | free(module->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1055 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1056 | for (i = 0; i < module->inc_size; i++) { |
| 1057 | ly_submodule_free(module->inc[i].submodule); |
| 1058 | } |
| 1059 | free(module->inc); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1060 | |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 1061 | for (i = 0; i < module->augment_size; i++) { |
| 1062 | ly_augment_free(ctx, &module->augment[i]); |
| 1063 | } |
| 1064 | free(module->augment); |
| 1065 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1066 | lydict_remove(ctx, module->name); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1067 | } |
| 1068 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1069 | void |
| 1070 | ly_submodule_free(struct ly_submodule *submodule) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1071 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1072 | if (!submodule) { |
| 1073 | return; |
| 1074 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1075 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1076 | submodule->inc_size = 0; |
| 1077 | free(submodule->inc); |
| 1078 | submodule->inc = NULL; |
Radek Krejci | f388693 | 2015-06-04 17:36:06 +0200 | [diff] [blame] | 1079 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1080 | /* common part with struct ly_module */ |
| 1081 | module_free_common((struct ly_module *)submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1082 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1083 | /* no specific items to free */ |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1084 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1085 | free(submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1086 | } |
| 1087 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1088 | struct ly_mnode * |
| 1089 | 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] | 1090 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1091 | struct ly_mnode *retval = NULL, *aux, *child; |
| 1092 | struct ly_ctx *ctx = module->ctx; |
| 1093 | int i, j; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1094 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1095 | struct ly_mnode_container *cont; |
| 1096 | struct ly_mnode_container *cont_orig = (struct ly_mnode_container *)mnode; |
| 1097 | struct ly_mnode_choice *choice; |
| 1098 | struct ly_mnode_choice *choice_orig = (struct ly_mnode_choice *)mnode; |
| 1099 | struct ly_mnode_leaf *leaf; |
| 1100 | struct ly_mnode_leaf *leaf_orig = (struct ly_mnode_leaf *)mnode; |
| 1101 | struct ly_mnode_leaflist *llist; |
| 1102 | struct ly_mnode_leaflist *llist_orig = (struct ly_mnode_leaflist *)mnode; |
| 1103 | struct ly_mnode_list *list; |
| 1104 | struct ly_mnode_list *list_orig = (struct ly_mnode_list *)mnode; |
| 1105 | struct ly_mnode_anyxml *anyxml; |
| 1106 | struct ly_mnode_anyxml *anyxml_orig = (struct ly_mnode_anyxml *)mnode; |
| 1107 | struct ly_mnode_uses *uses; |
| 1108 | struct ly_mnode_uses *uses_orig = (struct ly_mnode_uses *)mnode; |
| 1109 | struct ly_mnode_grp *mix; |
| 1110 | struct ly_mnode_grp *mix_orig = (struct ly_mnode_grp *)mnode; |
| 1111 | struct ly_mnode_case *cs; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1112 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1113 | /* we cannot just duplicate memory since the strings are stored in |
| 1114 | * dictionary and we need to update dictionary counters. |
| 1115 | */ |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1116 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1117 | switch (mnode->nodetype) { |
| 1118 | case LY_NODE_CONTAINER: |
| 1119 | cont = calloc(1, sizeof *cont); |
| 1120 | retval = (struct ly_mnode *)cont; |
| 1121 | break; |
| 1122 | |
| 1123 | case LY_NODE_CHOICE: |
| 1124 | choice = calloc(1, sizeof *choice); |
| 1125 | retval = (struct ly_mnode *)choice; |
| 1126 | break; |
| 1127 | |
| 1128 | case LY_NODE_LEAF: |
| 1129 | leaf = calloc(1, sizeof *leaf); |
| 1130 | retval = (struct ly_mnode *)leaf; |
| 1131 | break; |
| 1132 | |
| 1133 | case LY_NODE_LEAFLIST: |
| 1134 | llist = calloc(1, sizeof *llist); |
| 1135 | retval = (struct ly_mnode *)llist; |
| 1136 | break; |
| 1137 | |
| 1138 | case LY_NODE_LIST: |
| 1139 | list = calloc(1, sizeof *list); |
| 1140 | retval = (struct ly_mnode *)list; |
| 1141 | break; |
| 1142 | |
| 1143 | case LY_NODE_ANYXML: |
| 1144 | anyxml = calloc(1, sizeof *anyxml); |
| 1145 | retval = (struct ly_mnode *)anyxml; |
| 1146 | break; |
| 1147 | |
| 1148 | case LY_NODE_USES: |
| 1149 | uses = calloc(1, sizeof *uses); |
| 1150 | retval = (struct ly_mnode *)uses; |
| 1151 | break; |
| 1152 | |
| 1153 | case LY_NODE_CASE: |
| 1154 | cs = calloc(1, sizeof *cs); |
| 1155 | retval = (struct ly_mnode *)cs; |
| 1156 | break; |
| 1157 | |
| 1158 | /* exact same structure */ |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 1159 | case LY_NODE_GROUPING: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 1160 | case LY_NODE_RPC: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 1161 | case LY_NODE_INPUT: |
| 1162 | case LY_NODE_OUTPUT: |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 1163 | case LY_NODE_NOTIF: |
| 1164 | mix = calloc(1, sizeof *mix); |
| 1165 | retval = (struct ly_mnode *)mix; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 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 | default: |
| 1169 | goto error; |
| 1170 | } |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame] | 1171 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1172 | /* |
| 1173 | * duplicate generic part of the structure |
| 1174 | */ |
| 1175 | retval->name = lydict_insert(ctx, mnode->name, 0); |
| 1176 | retval->dsc = lydict_insert(ctx, mnode->dsc, 0); |
| 1177 | retval->ref = lydict_insert(ctx, mnode->ref, 0); |
| 1178 | retval->flags = mnode->flags; |
| 1179 | if (!(retval->flags & LY_NODE_CONFIG_MASK)) { |
| 1180 | /* set parent's config flag */ |
| 1181 | retval->flags |= flags & LY_NODE_CONFIG_MASK; |
| 1182 | } |
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 | retval->module = module; |
| 1185 | retval->nodetype = mnode->nodetype; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1186 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1187 | retval->prev = retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1188 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1189 | retval->feature = NULL; /* TODO */ |
| 1190 | retval->when = NULL; /* TODO */ |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1191 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1192 | if (recursive) { |
| 1193 | /* go recursively */ |
| 1194 | LY_TREE_FOR(mnode->child, child) { |
| 1195 | aux = ly_mnode_dup(module, child, retval->flags, 1, line); |
| 1196 | if (!aux || ly_mnode_addchild(retval, aux)) { |
| 1197 | goto error; |
| 1198 | } |
| 1199 | } |
| 1200 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1201 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1202 | /* |
| 1203 | * duplicate specific part of the structure |
| 1204 | */ |
| 1205 | switch (mnode->nodetype) { |
| 1206 | case LY_NODE_CONTAINER: |
| 1207 | cont->presence = lydict_insert(ctx, cont_orig->presence, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1208 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1209 | cont->must_size = cont_orig->must_size; |
| 1210 | cont->tpdf_size = cont_orig->tpdf_size; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1211 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1212 | cont->must = ly_must_dup(ctx, cont_orig->must, cont->must_size); |
| 1213 | cont->tpdf = ly_tpdf_dup(ctx, cont_orig->tpdf, cont->tpdf_size); |
| 1214 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1215 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1216 | case LY_NODE_CHOICE: |
| 1217 | if (choice->dflt) { |
| 1218 | LY_TREE_FOR(choice->child, child) { |
| 1219 | if (child->name == choice_orig->dflt->name) { |
| 1220 | choice->dflt = child; |
| 1221 | break; |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1226 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1227 | case LY_NODE_LEAF: |
| 1228 | ly_type_dup(ctx, &(leaf->type), &(leaf_orig->type)); |
| 1229 | leaf->units = lydict_insert(ctx, leaf_orig->units, 0); |
| 1230 | leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1231 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1232 | leaf->must_size = leaf_orig->must_size; |
| 1233 | leaf->must = ly_must_dup(ctx, leaf_orig->must, leaf->must_size); |
| 1234 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1235 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1236 | case LY_NODE_LEAFLIST: |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1237 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1238 | ly_type_dup(ctx, &(llist->type), &(llist_orig->type)); |
| 1239 | llist->units = lydict_insert(ctx, llist_orig->units, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1240 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1241 | llist->min = llist_orig->min; |
| 1242 | llist->max = llist_orig->max; |
| 1243 | |
| 1244 | llist->must_size = llist_orig->must_size; |
| 1245 | llist->must = ly_must_dup(ctx, llist_orig->must, llist->must_size); |
| 1246 | break; |
| 1247 | |
| 1248 | case LY_NODE_LIST: |
| 1249 | list->min = list_orig->min; |
| 1250 | list->max = list_orig->max; |
| 1251 | |
| 1252 | list->must_size = list_orig->must_size; |
| 1253 | list->tpdf_size = list_orig->tpdf_size; |
| 1254 | list->keys_size = list_orig->keys_size; |
| 1255 | list->unique_size = list_orig->unique_size; |
| 1256 | |
| 1257 | list->must = ly_must_dup(ctx, list_orig->must, list->must_size); |
| 1258 | list->tpdf = ly_tpdf_dup(ctx, list_orig->tpdf, list->tpdf_size); |
| 1259 | |
| 1260 | if (list->keys_size) { |
| 1261 | list->keys = calloc(list->keys_size, sizeof *list->keys); |
| 1262 | for (i = 0; i < list->keys_size; i++) { |
| 1263 | list->keys[i] = find_leaf(retval, list_orig->keys[i]->name, 0); |
| 1264 | } |
| 1265 | } |
| 1266 | if (list->unique_size) { |
| 1267 | list->unique = calloc(list->unique_size, sizeof *list->unique); |
| 1268 | for (i = 0; i < list->unique_size; i++) { |
| 1269 | list->unique[i].leafs = calloc(list->unique[i].leafs_size, sizeof *list->unique[i].leafs); |
| 1270 | for (j = 0; j < list->unique[i].leafs_size; j++) { |
| 1271 | list->unique[i].leafs[j] = find_leaf(retval, list_orig->unique[i].leafs[j]->name, 0); |
| 1272 | } |
| 1273 | } |
| 1274 | } |
| 1275 | break; |
| 1276 | |
| 1277 | case LY_NODE_ANYXML: |
| 1278 | anyxml->must_size = anyxml_orig->must_size; |
| 1279 | anyxml->must = ly_must_dup(ctx, anyxml_orig->must, anyxml->must_size); |
| 1280 | break; |
| 1281 | |
| 1282 | case LY_NODE_USES: |
| 1283 | uses->grp = uses_orig->grp; |
| 1284 | uses->refine_size = uses_orig->refine_size; |
| 1285 | uses->refine = ly_refine_dup(ctx, uses_orig->refine, uses_orig->refine_size); |
| 1286 | uses->augment_size = uses_orig->augment_size; |
| 1287 | uses->augment = ly_augment_dup(module, (struct ly_mnode *)uses, uses_orig->augment, uses_orig->augment_size); |
| 1288 | if (resolve_uses(uses, line)) { |
| 1289 | goto error; |
| 1290 | } |
| 1291 | break; |
| 1292 | |
| 1293 | case LY_NODE_CASE: |
| 1294 | /* nothing to do */ |
| 1295 | break; |
| 1296 | |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 1297 | case LY_NODE_GROUPING: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 1298 | case LY_NODE_RPC: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 1299 | case LY_NODE_INPUT: |
| 1300 | case LY_NODE_OUTPUT: |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 1301 | case LY_NODE_NOTIF: |
| 1302 | mix->tpdf_size = mix_orig->tpdf_size; |
| 1303 | mix->tpdf = ly_tpdf_dup(ctx, mix_orig->tpdf, mix->tpdf_size); |
Radek Krejci | a01e543 | 2015-06-16 10:35:25 +0200 | [diff] [blame] | 1304 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1305 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1306 | default: |
| 1307 | /* LY_NODE_NOTIF */ |
| 1308 | goto error; |
| 1309 | } |
| 1310 | |
| 1311 | return retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1312 | |
| 1313 | error: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1314 | LOGDBG("error: %s:%d", __FILE__, __LINE__); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1315 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1316 | ly_mnode_free(retval); |
| 1317 | return NULL; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1318 | } |
| 1319 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1320 | API void |
| 1321 | ly_module_free(struct ly_module *module) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1322 | { |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 1323 | struct ly_ctx *ctx; |
| 1324 | int i; |
| 1325 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1326 | if (!module) { |
| 1327 | return; |
| 1328 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1329 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 1330 | /* remove schema from the context */ |
| 1331 | ctx = module->ctx; |
| 1332 | if (ctx->models.used) { |
| 1333 | for (i = 0; i < ctx->models.used; i++) { |
| 1334 | if (ctx->models.list[i] == module) { |
| 1335 | /* replace the position in the list by the last module in the list */ |
| 1336 | ctx->models.used--; |
| 1337 | ctx->models.list[i] = ctx->models.list[ctx->models.used]; |
| 1338 | ctx->models.list[ctx->models.used] = NULL; |
| 1339 | /* we are done */ |
| 1340 | break; |
| 1341 | } |
| 1342 | } |
| 1343 | } |
| 1344 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1345 | /* common part with struct ly_submodule */ |
| 1346 | module_free_common(module); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1347 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1348 | /* specific items to free */ |
| 1349 | lydict_remove(module->ctx, module->ns); |
| 1350 | lydict_remove(module->ctx, module->prefix); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1351 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1352 | free(module); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1353 | } |