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