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 | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 23 | #include <ctype.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <sys/mman.h> |
| 26 | #include <sys/stat.h> |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 27 | #include <string.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 28 | |
| 29 | #include "common.h" |
| 30 | #include "context.h" |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 31 | #include "parser.h" |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 32 | #include "xml.h" |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 33 | #include "tree_internal.h" |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 34 | |
| 35 | void ly_submodule_free(struct ly_submodule *submodule); |
| 36 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 37 | struct ly_mnode_leaf * |
| 38 | find_leaf(struct ly_mnode *parent, const char *name, int len) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 39 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 40 | struct ly_mnode *child; |
| 41 | struct ly_mnode_leaf *result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 42 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 43 | if (!len) { |
| 44 | len = strlen(name); |
| 45 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 46 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 47 | LY_TREE_FOR(parent->child, child) { |
| 48 | switch (child->nodetype) { |
| 49 | case LY_NODE_LEAF: |
| 50 | /* direct check */ |
| 51 | if (child->name == name || (!strncmp(child->name, name, len) && !child->name[len])) { |
| 52 | return (struct ly_mnode_leaf *)child; |
| 53 | } |
| 54 | break; |
| 55 | case LY_NODE_USES: |
| 56 | /* search recursively */ |
| 57 | result = find_leaf(child, name, len); |
| 58 | if (result) { |
| 59 | return result; |
| 60 | } |
| 61 | break; |
| 62 | default: |
| 63 | /* ignore */ |
| 64 | break; |
| 65 | } |
| 66 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 67 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 68 | return NULL; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 69 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 70 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 71 | void |
| 72 | ly_mnode_unlink(struct ly_mnode *node) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 73 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 74 | struct ly_mnode *parent, *first; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 75 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 76 | if (!node) { |
| 77 | return; |
| 78 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 79 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 80 | /* unlink from data model if necessary */ |
| 81 | if (node->module) { |
| 82 | if (node->module->data == node) { |
| 83 | node->module->data = node->next; |
Radek Krejci | 2118196 | 2015-06-30 14:11:00 +0200 | [diff] [blame] | 84 | } else if (node->module->rpc == node) { |
| 85 | node->module->rpc = node->next; |
| 86 | } else if (node->module->notif == node) { |
| 87 | node->module->notif = node->next; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 88 | } |
| 89 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 90 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 91 | /* store pointers to important nodes */ |
| 92 | parent = node->parent; |
| 93 | if (parent && !parent->nodetype) { |
| 94 | /* handle augments - first, unlink it from the augment parent ... */ |
| 95 | if (parent->child == node) { |
| 96 | parent->child = node->next; |
| 97 | } |
| 98 | /* and then continue with the target parent */ |
| 99 | parent = ((struct ly_augment *)parent)->target; |
| 100 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 101 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 102 | /* unlink from parent */ |
| 103 | if (parent) { |
| 104 | if (parent->child == node) { |
| 105 | parent->child = node->next; |
| 106 | } |
| 107 | node->parent = NULL; |
| 108 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 109 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 110 | /* unlink from siblings */ |
| 111 | if (node->prev == node) { |
| 112 | /* there are no more siblings */ |
| 113 | return; |
| 114 | } |
| 115 | if (node->next) { |
| 116 | node->next->prev = node->prev; |
| 117 | } else { |
| 118 | /* unlinking the last element */ |
| 119 | if (parent) { |
| 120 | first = parent->child; |
| 121 | } else { |
| 122 | first = node; |
| 123 | while (node->prev->next) { |
| 124 | first = node->prev; |
| 125 | } |
| 126 | } |
| 127 | first->prev = node->prev; |
| 128 | } |
| 129 | if (node->prev->next) { |
| 130 | node->prev->next = node->next; |
| 131 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 132 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 133 | /* clean up the unlinked element */ |
| 134 | node->next = NULL; |
| 135 | node->prev = node; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Add child model node at the end of the parent's child list. |
| 140 | * If the child is connected somewhere (has a parent), it is completely |
| 141 | * unlinked and none of the following conditions applies. |
| 142 | * If the child has prev sibling(s), they are ignored (child is added at the |
| 143 | * end of the child list). |
| 144 | * If the child has next sibling(s), all of them are connected with the parent. |
| 145 | */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 146 | int |
| 147 | ly_mnode_addchild(struct ly_mnode *parent, struct ly_mnode *child) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 148 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 149 | struct ly_mnode *last; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 150 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 151 | assert(parent); |
| 152 | assert(child); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 153 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 154 | /* checks */ |
| 155 | switch (parent->nodetype) { |
| 156 | case LY_NODE_CONTAINER: |
| 157 | case LY_NODE_LIST: |
| 158 | case LY_NODE_GROUPING: |
| 159 | case LY_NODE_USES: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 160 | case LY_NODE_INPUT: |
| 161 | case LY_NODE_OUTPUT: |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 162 | case LY_NODE_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 163 | if (!(child->nodetype & |
| 164 | (LY_NODE_ANYXML | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_GROUPING | LY_NODE_LEAF | |
| 165 | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_USES))) { |
| 166 | LOGVAL(VE_SPEC, 0, "Unexpected substatement \"%s\" in \"%s\" (%s).", |
| 167 | strnodetype(child->nodetype), strnodetype(parent->nodetype), parent->name); |
| 168 | return EXIT_FAILURE; |
| 169 | } |
| 170 | break; |
| 171 | case LY_NODE_CHOICE: |
| 172 | if (!(child->nodetype & |
| 173 | (LY_NODE_ANYXML | LY_NODE_CASE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST))) { |
| 174 | LOGVAL(VE_SPEC, 0, "Unexpected substatement \"%s\" in \"choice\" %s.", |
| 175 | strnodetype(child->nodetype), parent->name); |
| 176 | return EXIT_FAILURE; |
| 177 | } |
| 178 | break; |
| 179 | case LY_NODE_CASE: |
| 180 | if (!(child->nodetype & |
| 181 | (LY_NODE_ANYXML | LY_NODE_CHOICE | LY_NODE_CONTAINER | LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_LIST | LY_NODE_USES))) { |
| 182 | LOGVAL(VE_SPEC, 0, "Unexpected substatement \"%s\" in \"case\" %s.", |
| 183 | strnodetype(child->nodetype), parent->name); |
| 184 | return EXIT_FAILURE; |
| 185 | } |
| 186 | break; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 187 | case LY_NODE_RPC: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 188 | if (!(child->nodetype & (LY_NODE_INPUT | LY_NODE_OUTPUT | LY_NODE_GROUPING))) { |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 189 | LOGVAL(VE_SPEC, 0, "Unexpected substatement \"%s\" in \"rpc\" %s.", |
| 190 | strnodetype(child->nodetype), parent->name); |
| 191 | return EXIT_FAILURE; |
| 192 | } |
| 193 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 194 | case LY_NODE_LEAF: |
| 195 | case LY_NODE_LEAFLIST: |
| 196 | case LY_NODE_ANYXML: |
| 197 | LOGVAL(VE_SPEC, 0, "The \"%s\" statement (%s) cannot have any substatement.", |
| 198 | strnodetype(parent->nodetype), parent->name); |
| 199 | return EXIT_FAILURE; |
| 200 | case LY_NODE_AUGMENT: |
| 201 | LOGVAL(VE_SPEC, 0, "Internal error (%s:%d)", __FILE__, __LINE__); |
| 202 | return EXIT_FAILURE; |
| 203 | } |
Radek Krejci | b7155b5 | 2015-06-10 17:03:01 +0200 | [diff] [blame] | 204 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 205 | if (child->parent) { |
| 206 | ly_mnode_unlink(child); |
| 207 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 208 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 209 | if (!parent->child) { |
| 210 | /* the only/first child of the parent */ |
| 211 | parent->child = child; |
| 212 | child->parent = parent; |
| 213 | last = child; |
| 214 | } else { |
| 215 | /* add a new child at the end of parent's child list */ |
| 216 | last = parent->child->prev; |
| 217 | last->next = child; |
| 218 | child->prev = last; |
| 219 | } |
| 220 | while (last->next) { |
| 221 | last = last->next; |
| 222 | last->parent = parent; |
| 223 | } |
| 224 | parent->child->prev = last; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 225 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 226 | return EXIT_SUCCESS; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 229 | static struct ly_module * |
| 230 | find_import_in_includes_recursive(struct ly_module *mod, const char *prefix, uint32_t pref_len) |
| 231 | { |
| 232 | int i, j; |
| 233 | struct ly_submodule *sub_mod; |
| 234 | struct ly_module *ret; |
| 235 | |
| 236 | for (i = 0; i < mod->inc_size; i++) { |
| 237 | sub_mod = mod->inc[i].submodule; |
| 238 | for (j = 0; j < sub_mod->imp_size; j++) { |
| 239 | if ((pref_len == strlen(sub_mod->imp[j].prefix)) |
| 240 | && !strncmp(sub_mod->imp[j].prefix, prefix, pref_len)) { |
| 241 | return sub_mod->imp[j].module; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | for (i = 0; i < mod->inc_size; i++) { |
| 247 | ret = find_import_in_includes_recursive((struct ly_module *)mod->inc[i].submodule, prefix, pref_len); |
| 248 | if (ret) { |
| 249 | return ret; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | static struct ly_module * |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 257 | find_prefixed_module(struct ly_module *mod, const char *prefix, uint32_t pref_len) |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 258 | { |
| 259 | int i; |
| 260 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 261 | /* module itself */ |
| 262 | if (!strncmp(mod->prefix, prefix, pref_len) && mod->prefix[pref_len] == '\0') { |
| 263 | return mod; |
| 264 | } |
| 265 | |
| 266 | /* imported modules */ |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 267 | for (i = 0; i < mod->imp_size; i++) { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 268 | if (!strncmp(mod->imp[i].prefix, prefix, pref_len) && mod->imp[i].prefix[pref_len] == '\0') { |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 269 | return mod->imp[i].module; |
| 270 | } |
| 271 | } |
| 272 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 273 | /* imports in includes */ |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 274 | return find_import_in_includes_recursive(mod, prefix, pref_len); |
| 275 | } |
| 276 | |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 277 | /* |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 278 | * id - schema-nodeid |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 279 | * |
| 280 | * node_type - LY_NODE_AUGMENT (searches also RPCs and notifications) |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 281 | * - LY_NODE_USES (only descendant-schema-nodeid allowed, ".." not allowed) |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 282 | * - LY_NODE_CHOICE (search only start->child, only descendant-schema-nodeid allowed) |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 283 | */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 284 | struct ly_mnode * |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 285 | 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] | 286 | { |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 287 | const char *name, *prefix, *ptr; |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 288 | struct ly_mnode *sibling; |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 289 | uint32_t nam_len, pref_len; |
| 290 | struct ly_module *prefix_mod, *start_mod; |
| 291 | /* 0 - in module, 1 - in 1st submodule, 2 - in 2nd submodule, ... */ |
| 292 | uint8_t in_submod = 0; |
| 293 | /* 0 - in data, 1 - in RPCs, 2 - in notifications (relevant only with LY_NODE_AUGMENT) */ |
| 294 | uint8_t in_mod_part = 0; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 295 | |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 296 | assert(mod); |
| 297 | assert(id); |
| 298 | |
Michal Vasko | 92f4b2e | 2015-07-03 14:19:49 +0200 | [diff] [blame] | 299 | if (id[strlen(id)-1] == '/') { |
| 300 | LOGERR(LY_EINVAL, "%s: Path ending with '/' is not valid.", __func__); |
| 301 | return NULL; |
| 302 | } |
| 303 | |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 304 | if (id[0] == '/') { |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 305 | if (node_type & (LY_NODE_USES | LY_NODE_CHOICE)) { |
| 306 | return NULL; |
| 307 | } |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 308 | ptr = strchr(id+1, '/'); |
| 309 | prefix = id+1; |
| 310 | } else { |
| 311 | ptr = strchr(id, '/'); |
| 312 | prefix = id; |
| 313 | } |
| 314 | pref_len = (ptr ? (unsigned)(ptr-prefix) : strlen(prefix)); |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 315 | |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 316 | ptr = strnchr(prefix, ':', pref_len); |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 317 | /* there is a prefix */ |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 318 | if (ptr) { |
| 319 | nam_len = (pref_len-(ptr-prefix))-1; |
| 320 | pref_len = ptr-prefix; |
| 321 | name = ptr+1; |
| 322 | |
| 323 | /* no prefix used */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 324 | } else { |
| 325 | name = prefix; |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 326 | nam_len = pref_len; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 327 | prefix = NULL; |
| 328 | } |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 329 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 330 | /* absolute-schema-nodeid */ |
| 331 | if (id[0] == '/') { |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 332 | if (prefix) { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 333 | start_mod = find_prefixed_module(mod, prefix, pref_len); |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 334 | if (!start_mod) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 335 | return NULL; |
| 336 | } |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 337 | start = start_mod->data; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 338 | } else { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 339 | start = mod->data; |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 340 | start_mod = mod; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 341 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 342 | /* descendant-schema-nodeid */ |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 343 | } else { |
| 344 | assert(start); |
| 345 | start = start->child; |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 346 | start_mod = start->module; |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 347 | } |
| 348 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 349 | while (1) { |
| 350 | if (!strcmp(name, ".")) { |
| 351 | /* this node - start does not change */ |
| 352 | } else if (!strcmp(name, "..")) { |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 353 | /* ".." 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] | 354 | if (!start || (node_type == LY_NODE_USES)) { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 355 | return NULL; |
| 356 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 357 | start = start->parent; |
| 358 | } else { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 359 | sibling = NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 360 | LY_TREE_FOR(start, sibling) { |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 361 | /* name match */ |
Michal Vasko | 69b4291 | 2015-07-03 13:43:15 +0200 | [diff] [blame] | 362 | if ((sibling->name && !strncmp(name, sibling->name, nam_len) && (strlen(sibling->name) == nam_len)) |
| 363 | || (!strncmp(name, "input", 5) && (nam_len == 5) && (sibling->nodetype == LY_NODE_INPUT)) |
| 364 | || (!strncmp(name, "output", 6) && (nam_len == 6) && (sibling->nodetype == LY_NODE_OUTPUT))) { |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 365 | |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 366 | /* prefix match check */ |
| 367 | if (prefix) { |
| 368 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 369 | prefix_mod = find_prefixed_module(mod, prefix, pref_len); |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 370 | if (!prefix_mod) { |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 371 | return NULL; |
| 372 | } |
| 373 | |
Radek Krejci | a7f4a0e | 2015-07-02 19:13:35 +0200 | [diff] [blame] | 374 | if (!sibling->module->type) { |
| 375 | if (prefix_mod != sibling->module) { |
| 376 | continue; |
| 377 | } |
| 378 | } else { |
| 379 | if (prefix_mod != ((struct ly_submodule *)sibling->module)->belongsto) { |
| 380 | continue; |
| 381 | } |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
| 385 | /* the result node? */ |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 386 | ptr = name+nam_len; |
| 387 | if (!ptr[0]) { |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 388 | return sibling; |
| 389 | } |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 390 | assert(ptr[0] == '/'); |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 391 | |
| 392 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 393 | if (!sibling->parent || (sibling->parent->nodetype != LY_NODE_CHOICE) |
| 394 | || (sibling->nodetype == LY_NODE_CASE)) { |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 395 | start = sibling->child; |
| 396 | } |
| 397 | break; |
| 398 | } |
| 399 | } |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 400 | |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 401 | /* we did not find the case in direct siblings */ |
| 402 | if (node_type == LY_NODE_CHOICE) { |
| 403 | return NULL; |
| 404 | } |
| 405 | |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 406 | /* no match */ |
| 407 | if (!sibling) { |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 408 | /* 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] | 409 | if ((node_type == LY_NODE_AUGMENT) && (!start || !start->parent)) { |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 410 | /* we have searched all the data nodes */ |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 411 | if (in_mod_part == 0) { |
| 412 | if (!in_submod) { |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 413 | start = start_mod->rpc; |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 414 | } else { |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 415 | start = start_mod->inc[in_submod-1].submodule->rpc; |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 416 | } |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 417 | in_mod_part = 1; |
| 418 | continue; |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 419 | } |
| 420 | /* we have searched all the RPCs */ |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 421 | if (in_mod_part == 1) { |
| 422 | if (!in_submod) { |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 423 | start = start_mod->notif; |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 424 | } else { |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 425 | start = start_mod->inc[in_submod-1].submodule->notif; |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 426 | } |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 427 | in_mod_part = 2; |
| 428 | continue; |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 429 | } |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 430 | /* we have searched all the notifications, nothing else to search in this module */ |
| 431 | } |
| 432 | |
| 433 | /* are we done with the included submodules as well? */ |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 434 | if (in_submod == start_mod->inc_size) { |
Michal Vasko | 86b8445 | 2015-06-22 10:54:05 +0200 | [diff] [blame] | 435 | return NULL; |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 436 | } |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 437 | |
| 438 | /* we aren't, check the next one */ |
| 439 | ++in_submod; |
| 440 | in_mod_part = 0; |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 441 | start = start_mod->inc[in_submod-1].submodule->data; |
Michal Vasko | af989e6 | 2015-06-29 14:57:45 +0200 | [diff] [blame] | 442 | continue; |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
Michal Vasko | 3bf80ca | 2015-07-02 17:19:28 +0200 | [diff] [blame] | 446 | /* we found our submodule */ |
| 447 | if (in_submod) { |
| 448 | start_mod = (struct ly_module *)start_mod->inc[in_submod-1].submodule; |
| 449 | in_submod = 0; |
| 450 | } |
Michal Vasko | db7f229 | 2015-07-02 11:52:13 +0200 | [diff] [blame] | 451 | |
Michal Vasko | a158c68 | 2015-06-17 09:25:38 +0200 | [diff] [blame] | 452 | assert((*(name+nam_len) == '/') || (*(name+nam_len) == '\0')); |
| 453 | |
| 454 | /* make prefix point to the next node name */ |
| 455 | prefix = name+nam_len; |
Michal Vasko | a158c68 | 2015-06-17 09:25:38 +0200 | [diff] [blame] | 456 | ++prefix; |
Michal Vasko | 92f4b2e | 2015-07-03 14:19:49 +0200 | [diff] [blame] | 457 | assert(prefix[0]); |
Michal Vasko | a158c68 | 2015-06-17 09:25:38 +0200 | [diff] [blame] | 458 | |
| 459 | /* parse prefix and node name */ |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 460 | ptr = strchr(prefix, '/'); |
| 461 | pref_len = (ptr ? (unsigned)(ptr-prefix) : strlen(prefix)); |
| 462 | ptr = strnchr(prefix, ':', pref_len); |
| 463 | |
| 464 | /* there is prefix */ |
| 465 | if (ptr) { |
Michal Vasko | a158c68 | 2015-06-17 09:25:38 +0200 | [diff] [blame] | 466 | nam_len = (pref_len-(ptr-prefix))-1; |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 467 | pref_len = ptr-prefix; |
| 468 | name = ptr+1; |
| 469 | |
| 470 | /* no prefix used */ |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 471 | } else { |
| 472 | name = prefix; |
Michal Vasko | a7d0be6 | 2015-06-16 10:43:49 +0200 | [diff] [blame] | 473 | nam_len = pref_len; |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 474 | prefix = NULL; |
| 475 | } |
Michal Vasko | 25880b4 | 2015-06-12 10:16:33 +0200 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /* cannot get here */ |
| 479 | return NULL; |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 480 | } |
| 481 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 482 | static int |
| 483 | is_identifier(char start, char c) |
| 484 | { |
| 485 | if (start == c) { |
| 486 | /* first letter */ |
| 487 | if (isalpha(start) || c == '_') { |
| 488 | return 1; |
| 489 | } |
| 490 | } else { |
| 491 | /* checking inner letter */ |
| 492 | if (isalnum(c) || c == '_' || c == '-' || c == '.') { |
| 493 | return 1; |
| 494 | } |
| 495 | } |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | static int |
| 500 | match_data_nodeid(const char *id, struct lyd_node *data, struct lyd_node *parent, struct lyd_node **result) |
| 501 | { |
| 502 | int i = 0, j; |
| 503 | const char *name; |
| 504 | struct ly_module *mod; |
| 505 | struct lyd_node *node, *start; |
| 506 | |
| 507 | *result = NULL; |
| 508 | |
| 509 | /* [prefix:]identifier */ |
| 510 | name = id; |
| 511 | while(is_identifier(name[0], name[i])) { |
| 512 | i++; |
| 513 | } |
| 514 | if (name[i] == ':') { |
| 515 | /* we have prefix, find appropriate module */ |
| 516 | if (!i) { |
| 517 | /* syntax error */ |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | mod = find_prefixed_module(data->schema->module, name, i); |
| 522 | if (!mod) { |
| 523 | /* invalid prefix */ |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | /* now get the identifier */ |
| 528 | name = &id[i + 1]; |
| 529 | j = 0; |
| 530 | while(is_identifier(name[0], name[j])) { |
| 531 | j++; |
| 532 | } |
| 533 | if (!j) { |
| 534 | i = 0; |
| 535 | } |
| 536 | } else { |
| 537 | /* no prefix, module is the same as of current node */ |
| 538 | mod = data->schema->module; |
| 539 | j = i; |
| 540 | } |
| 541 | if (!i) { |
| 542 | /* syntax error */ |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | if (parent) { |
| 547 | start = parent->child; |
| 548 | } else { |
| 549 | /* get data root */ |
| 550 | for (start = data; start->parent; start = start->parent); |
| 551 | } |
| 552 | LY_TREE_FOR(start, node) { |
| 553 | if (node->schema->module == mod && !strncmp(node->schema->name, name, j)) { |
| 554 | /* matching target */ |
| 555 | *result = node; |
| 556 | break; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | if (!*result) { |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | return i; |
| 565 | } |
| 566 | |
| 567 | /* return number of processed bytes in id */ |
| 568 | static int |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 569 | resolve_data_nodeid(const char *id, struct lyd_node *data, struct leafref_instid **parents) |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 570 | { |
| 571 | int i = 0, j, flag; |
| 572 | struct ly_module *mod; |
| 573 | const char *name; |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 574 | struct leafref_instid *item, *par_iter; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 575 | struct lyd_node *node, *root = NULL; |
| 576 | |
| 577 | /* [prefix:]identifier */ |
| 578 | name = id; |
| 579 | while(is_identifier(name[0], name[i])) { |
| 580 | i++; |
| 581 | } |
| 582 | if (name[i] == ':') { |
| 583 | /* we have prefix, find appropriate module */ |
| 584 | if (!i) { |
| 585 | /* syntax error */ |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | mod = find_prefixed_module(data->schema->module, name, i); |
| 590 | if (!mod) { |
| 591 | /* invalid prefix */ |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | /* now get the identifier */ |
| 596 | name = &id[++i]; |
| 597 | j = 0; |
| 598 | while(is_identifier(name[0], name[j])) { |
| 599 | j++; |
| 600 | } |
| 601 | if (!j) { |
| 602 | i = 0; |
| 603 | } else { |
| 604 | i += j; |
| 605 | } |
| 606 | } else { |
| 607 | /* no prefix, module is the same as of current node */ |
| 608 | mod = data->schema->module; |
| 609 | j = i; |
| 610 | } |
| 611 | if (!i) { |
| 612 | /* syntax error */ |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | if (!*parents) { |
| 617 | *parents = malloc(sizeof **parents); |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 618 | (*parents)->dnode = NULL; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 619 | (*parents)->next = NULL; |
| 620 | |
| 621 | /* find root data element */ |
| 622 | for (root = data; root->parent; root = root->parent); |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 623 | /* make sure it's first */ |
| 624 | for (; root->prev->next; root = root->prev); |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 625 | } |
| 626 | for (par_iter = *parents; par_iter; par_iter = par_iter->next) { |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 627 | if (par_iter->dnode && (par_iter->dnode->schema->nodetype & (LY_NODE_LEAF | LY_NODE_LEAFLIST))) { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 628 | /* skip */ |
| 629 | continue; |
| 630 | } |
| 631 | flag = 0; |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 632 | LY_TREE_FOR(par_iter->dnode ? par_iter->dnode->child : root, node) { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 633 | if (node->schema->module == mod && !strncmp(node->schema->name, name, j)) { |
| 634 | /* matching target */ |
| 635 | if (!flag) { |
| 636 | /* replace leafref instead of the current parent */ |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 637 | par_iter->dnode = node; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 638 | flag = 1; |
| 639 | } else { |
| 640 | /* multiple matching, so create new leafref structure */ |
| 641 | item = malloc(sizeof *item); |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 642 | item->dnode = node; |
Michal Vasko | 63ee3d4 | 2015-07-17 10:30:50 +0200 | [diff] [blame] | 643 | item->next = par_iter->next; |
| 644 | par_iter->next = item; |
| 645 | par_iter = par_iter->next; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | return i; |
| 652 | } |
| 653 | |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 654 | int |
| 655 | resolve_path(struct lyd_node *data, const char *path, struct leafref_instid **ret) |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 656 | { |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 657 | struct leafref_instid *riter = NULL, *raux; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 658 | struct ly_mnode_leaf *schema = (struct ly_mnode_leaf *)data->schema; |
| 659 | struct lyd_node *pathnode = NULL; |
| 660 | int i, j; |
| 661 | char *p = strdup(path); |
| 662 | char *name; |
| 663 | struct lyd_node *pred_source, *pred_target; |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 664 | *ret = NULL; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 665 | |
| 666 | i = 0; |
| 667 | if (p[0] == '/') { |
| 668 | /* absolute path, start with '/' */ |
| 669 | i = 0; |
| 670 | |
| 671 | } else { |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 672 | *ret = calloc(1, sizeof **ret); |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 673 | while(!strncmp(&p[i], "../", 3)) { |
| 674 | /* relative path */ |
| 675 | i += 3; |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 676 | if (!*ret) { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 677 | /* error, too many .. */ |
| 678 | LOGVAL(DE_INVAL, 0, p, schema->name); |
| 679 | goto error; |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 680 | } else if (!(*ret)->dnode) { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 681 | /* first .. */ |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 682 | (*ret)->dnode = data->parent; |
| 683 | } else if (!(*ret)->dnode->parent) { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 684 | /* we are in root */ |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 685 | free(*ret); |
| 686 | *ret = NULL; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 687 | } else { |
| 688 | /* multiple .. */ |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 689 | (*ret)->dnode = (*ret)->dnode->parent; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | if (!i) { |
| 693 | /* neither absolute or relative p */ |
| 694 | LOGVAL(DE_INVAL, 0, p, schema->name); |
| 695 | goto error; |
| 696 | } |
| 697 | /* start with '/' */ |
| 698 | i--; |
| 699 | } |
| 700 | |
| 701 | /* searching for nodeset */ |
| 702 | for (; p[i]; ) { |
| 703 | if (p[i] == '/') { |
| 704 | i++; |
| 705 | |
| 706 | /* node identifier */ |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 707 | j = resolve_data_nodeid(&p[i], data, ret); |
| 708 | if (!j || !*ret) { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 709 | goto error; |
| 710 | } |
| 711 | |
| 712 | i += j; |
| 713 | if (p[i] == '[') { |
| 714 | /* we have predicate, so the current results must be lists */ |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 715 | for (raux = NULL, riter = *ret; riter; ) { |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 716 | if (riter->dnode->schema->nodetype == LY_NODE_LIST && |
| 717 | ((struct ly_mnode_list *)riter->dnode->schema)->keys) { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 718 | /* leafref is ok, continue check with next leafref */ |
| 719 | raux = riter; |
| 720 | riter = riter->next; |
| 721 | continue; |
| 722 | } |
| 723 | |
| 724 | /* does not fulfill conditions, remove leafref record */ |
| 725 | if (raux) { |
| 726 | raux->next = riter->next; |
| 727 | free(riter); |
| 728 | riter = raux->next; |
| 729 | } else { |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 730 | *ret = riter->next; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 731 | free(riter); |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 732 | riter = *ret; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 733 | } |
| 734 | } |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 735 | if (!*ret) { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 736 | /* no matching node */ |
| 737 | } |
| 738 | } |
| 739 | } else if (p[i] == '[') { |
| 740 | /* predicate */ |
| 741 | i++; |
| 742 | while(isspace(p[i])) { |
| 743 | i++; |
| 744 | } |
| 745 | |
| 746 | /* [prefix:]identifier */ |
| 747 | name = &p[i]; /* use name later */ |
| 748 | while (isalnum(p[i]) || p[i] == '_' || p[i] == '-' || p[i] == '.' || p[i] == ':') { |
| 749 | i++; |
| 750 | } |
| 751 | |
| 752 | /* *WSP "=" *WSP */ |
| 753 | while (isspace(p[i])) { |
| 754 | p[i] = '\0'; |
| 755 | i++; |
| 756 | } |
| 757 | if (p[i] != '=') { |
| 758 | /* error */ |
| 759 | } |
| 760 | p[i] = '\0'; |
| 761 | i++; |
| 762 | while (isspace(p[i])) { |
| 763 | i++; |
| 764 | } |
| 765 | |
| 766 | /* path-key-expr (pred_source) */ |
| 767 | /* current-function-invocation */ |
| 768 | if (strncmp(&p[i], "current()", 9)) { |
| 769 | /* error */ |
| 770 | } |
| 771 | pred_source = data; |
| 772 | i += 9; |
| 773 | /* *WSP */ |
| 774 | while (isspace(p[i])) { |
| 775 | i++; |
| 776 | } |
| 777 | /* rel-path-keyexpr */ |
| 778 | while(p[i] != ']') { |
| 779 | /* "/" *WSP */ |
| 780 | if (p[i] != '/') { |
| 781 | /* error */ |
| 782 | } |
| 783 | i++; |
| 784 | while (isspace(p[i])) { |
| 785 | i++; |
| 786 | } |
| 787 | |
| 788 | if (!strncmp("..", &p[i], 2)) { |
| 789 | /* 1*(".." *WSP */ |
| 790 | i += 2; |
| 791 | while (isspace(p[i])) { |
| 792 | i++; |
| 793 | } |
| 794 | pred_source = pred_source->parent; |
| 795 | } else { |
| 796 | /* node-identifier *WSP */ |
| 797 | j = match_data_nodeid(&p[i], data, pred_source, &pred_source); |
| 798 | if (!j) { |
| 799 | /* error */ |
| 800 | } |
| 801 | i += j; |
| 802 | while (isspace(p[i])) { |
| 803 | i++; |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | i++; |
| 808 | |
| 809 | if (pred_source == data) { |
| 810 | /* something is wrong */ |
| 811 | } |
| 812 | |
| 813 | /* find match between target and source nodes */ |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 814 | for (raux = NULL, riter = *ret; riter; ) { |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 815 | pathnode = riter->dnode; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 816 | |
| 817 | /* get target */ |
| 818 | if (!match_data_nodeid(name, data, pathnode, &pred_target)) { |
| 819 | /* error */ |
| 820 | } |
| 821 | |
| 822 | if (pred_target->schema->nodetype != pred_source->schema->nodetype) { |
| 823 | goto remove_leafref; |
| 824 | } |
| 825 | |
| 826 | if (((struct ly_mnode_leaf *)pred_target->schema)->type.base != ((struct ly_mnode_leaf *)pred_source->schema)->type.base) { |
| 827 | goto remove_leafref; |
| 828 | } |
| 829 | |
| 830 | if (((struct lyd_node_leaf *)pred_target)->value_str != ((struct lyd_node_leaf *)pred_source)->value_str) { |
| 831 | goto remove_leafref; |
| 832 | } |
| 833 | |
| 834 | /* leafref is ok, continue check with next leafref */ |
| 835 | raux = riter; |
| 836 | riter = riter->next; |
| 837 | continue; |
| 838 | |
| 839 | remove_leafref: |
| 840 | |
| 841 | /* does not fulfill conditions, remove leafref record */ |
| 842 | if (raux) { |
| 843 | raux->next = riter->next; |
| 844 | free(riter); |
| 845 | riter = raux->next; |
| 846 | } else { |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 847 | *ret = riter->next; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 848 | free(riter); |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 849 | riter = *ret; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | } else { |
| 853 | /* syntax error */ |
| 854 | goto error; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | free(p); |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 859 | return 0; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 860 | |
| 861 | error: |
| 862 | |
| 863 | free(p); |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 864 | while (*ret) { |
| 865 | raux = (*ret)->next; |
| 866 | free(*ret); |
| 867 | *ret = raux; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 868 | } |
| 869 | |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 870 | return 1; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 871 | } |
Michal Vasko | 5d34dea | 2015-07-16 12:08:24 +0200 | [diff] [blame] | 872 | |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 873 | int |
| 874 | resolve_instid(struct lyd_node *data, const char *path, int path_len, struct leafref_instid **ret) |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 875 | { |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 876 | struct leafref_instid *riter = NULL, *raux; |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 877 | struct lyd_node *pathnode = NULL; |
Michal Vasko | f1b9c36 | 2015-07-17 10:32:02 +0200 | [diff] [blame] | 878 | int i, j, cur_idx, idx = -1; |
Michal Vasko | c82396e | 2015-07-17 15:31:25 +0200 | [diff] [blame] | 879 | char *p = strndup(path, path_len); |
Michal Vasko | f1b9c36 | 2015-07-17 10:32:02 +0200 | [diff] [blame] | 880 | char *name, *value = NULL; |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 881 | struct lyd_node *pred_target; |
| 882 | |
| 883 | i = 0; |
| 884 | if (p[0] != '/') { |
| 885 | /* error */ |
| 886 | } |
| 887 | |
| 888 | /* searching for nodeset */ |
| 889 | while (p[i]) { |
| 890 | if (p[i] == '/') { |
| 891 | i++; |
| 892 | |
| 893 | /* node identifier */ |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 894 | j = resolve_data_nodeid(&p[i], data, ret); |
| 895 | if (!j || !*ret) { |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 896 | goto error; |
| 897 | } |
| 898 | |
| 899 | i += j; |
| 900 | if (p[i] == '[') { |
Michal Vasko | f1b9c36 | 2015-07-17 10:32:02 +0200 | [diff] [blame] | 901 | /* we have predicate, so the current results must be list or leaf-list */ |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 902 | for (raux = NULL, riter = *ret; riter; ) { |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 903 | if ((riter->dnode->schema->nodetype == LY_NODE_LIST && |
| 904 | ((struct ly_mnode_list *)riter->dnode->schema)->keys) |
| 905 | || (riter->dnode->schema->nodetype == LY_NODE_LEAFLIST)) { |
| 906 | /* instid is ok, continue check with next instid */ |
| 907 | raux = riter; |
| 908 | riter = riter->next; |
| 909 | continue; |
| 910 | } |
| 911 | |
| 912 | /* does not fulfill conditions, remove inst record */ |
| 913 | if (raux) { |
| 914 | raux->next = riter->next; |
| 915 | free(riter); |
| 916 | riter = raux->next; |
| 917 | } else { |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 918 | *ret = riter->next; |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 919 | free(riter); |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 920 | riter = *ret; |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 921 | } |
| 922 | } |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 923 | if (!*ret) { |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 924 | /* no matching node */ |
| 925 | } |
| 926 | } |
| 927 | } else if (p[i] == '[') { |
| 928 | /* predicate */ |
| 929 | i++; |
| 930 | while(isspace(p[i])) { |
| 931 | i++; |
| 932 | } |
| 933 | |
| 934 | /* [prefix:]identifier */ |
| 935 | name = &p[i]; /* use name later */ |
| 936 | while (isalnum(p[i]) || p[i] == '_' || p[i] == '-' || p[i] == '.' || p[i] == ':') { |
| 937 | i++; |
| 938 | } |
| 939 | |
Michal Vasko | f1b9c36 | 2015-07-17 10:32:02 +0200 | [diff] [blame] | 940 | /* *WSP */ |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 941 | while (isspace(p[i])) { |
| 942 | p[i] = '\0'; |
| 943 | i++; |
| 944 | } |
Michal Vasko | f1b9c36 | 2015-07-17 10:32:02 +0200 | [diff] [blame] | 945 | |
| 946 | /* name is position */ |
| 947 | if (isdigit(name[0])) { |
| 948 | if (p[i] != ']') { |
| 949 | /* error */ |
| 950 | } |
| 951 | p[i] = '\0'; |
| 952 | ++i; |
| 953 | idx = atoi(name); |
| 954 | goto resolve_predicate; |
| 955 | } |
| 956 | |
| 957 | /* "=" *WSP */ |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 958 | if (p[i] != '=') { |
| 959 | /* error */ |
| 960 | } |
| 961 | p[i] = '\0'; |
| 962 | i++; |
| 963 | while (isspace(p[i])) { |
| 964 | i++; |
| 965 | } |
| 966 | |
| 967 | /* (DQUOTE/SQUOTE) string (DQUOTE/SQUOTE) */ |
| 968 | if ((p[i] != '\"') && (p[i] != '\'')) { |
| 969 | /* error */ |
| 970 | } |
| 971 | |
| 972 | value = &p[i+1]; |
Michal Vasko | 8ed7530 | 2015-07-17 11:05:14 +0200 | [diff] [blame] | 973 | if (!value[0]) { |
| 974 | /* error */ |
| 975 | } |
| 976 | |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 977 | j = i; |
| 978 | i++; |
| 979 | |
| 980 | while (p[j] != p[i]) { |
| 981 | if (p[i] == '\0') { |
| 982 | /* error */ |
| 983 | } |
| 984 | i++; |
| 985 | } |
| 986 | |
| 987 | p[i] = '\0'; |
| 988 | i++; |
| 989 | |
| 990 | /* *WSP */ |
| 991 | while (isspace(p[i])) { |
| 992 | i++; |
| 993 | } |
| 994 | |
| 995 | if (p[i] != ']') { |
| 996 | /* error */ |
| 997 | } |
| 998 | i++; |
| 999 | |
Michal Vasko | f1b9c36 | 2015-07-17 10:32:02 +0200 | [diff] [blame] | 1000 | resolve_predicate: |
| 1001 | |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 1002 | /* find match between target and source nodes */ |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 1003 | for (cur_idx = 0, raux = NULL, riter = *ret; riter; ++cur_idx) { |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 1004 | pathnode = riter->dnode; |
| 1005 | |
| 1006 | /* get target */ |
Michal Vasko | f1b9c36 | 2015-07-17 10:32:02 +0200 | [diff] [blame] | 1007 | if (!strcmp(name, ".") || !value) { |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 1008 | pred_target = pathnode; |
| 1009 | } else if (!match_data_nodeid(name, data, pathnode, &pred_target)) { |
| 1010 | /* error */ |
| 1011 | } |
| 1012 | |
| 1013 | /* check that we have the correct type */ |
| 1014 | if (!strcmp(name, ".")) { |
| 1015 | if (pathnode->schema->nodetype != LY_NODE_LEAFLIST) { |
| 1016 | goto remove_leafref; |
| 1017 | } |
Michal Vasko | f1b9c36 | 2015-07-17 10:32:02 +0200 | [diff] [blame] | 1018 | } else if (value) { |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 1019 | if (pathnode->schema->nodetype != LY_NODE_LIST) { |
| 1020 | goto remove_leafref; |
| 1021 | } |
| 1022 | } |
| 1023 | |
Michal Vasko | f1b9c36 | 2015-07-17 10:32:02 +0200 | [diff] [blame] | 1024 | if ((value && strcmp(((struct lyd_node_leaf *)pred_target)->value_str, value)) || (!value && (idx != cur_idx))) { |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 1025 | goto remove_leafref; |
| 1026 | } |
| 1027 | |
| 1028 | /* leafref is ok, continue check with next leafref */ |
| 1029 | raux = riter; |
| 1030 | riter = riter->next; |
| 1031 | continue; |
| 1032 | |
| 1033 | remove_leafref: |
| 1034 | |
| 1035 | /* does not fulfill conditions, remove leafref record */ |
| 1036 | if (raux) { |
| 1037 | raux->next = riter->next; |
| 1038 | free(riter); |
| 1039 | riter = raux->next; |
| 1040 | } else { |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 1041 | *ret = riter->next; |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 1042 | free(riter); |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 1043 | riter = *ret; |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | } else { |
| 1047 | /* syntax error */ |
| 1048 | goto error; |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | free(p); |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 1053 | return 0; |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 1054 | |
| 1055 | error: |
| 1056 | |
| 1057 | free(p); |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 1058 | while (*ret) { |
| 1059 | raux = (*ret)->next; |
| 1060 | free(*ret); |
| 1061 | *ret = raux; |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 1062 | } |
| 1063 | |
Michal Vasko | 3f1098b | 2015-07-20 15:16:43 +0200 | [diff] [blame] | 1064 | return 1; |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 1065 | } |
| 1066 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1067 | API struct ly_module * |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1068 | lys_parse(struct ly_ctx *ctx, const char *data, LY_MINFORMAT format) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1069 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1070 | if (!ctx || !data) { |
| 1071 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1072 | return NULL; |
| 1073 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1074 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1075 | switch (format) { |
| 1076 | case LY_IN_YIN: |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1077 | return yin_read_module(ctx, data, 1); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1078 | case LY_IN_YANG: |
| 1079 | default: |
| 1080 | /* TODO */ |
| 1081 | return NULL; |
| 1082 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1083 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1084 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1085 | } |
| 1086 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1087 | struct ly_submodule * |
Michal Vasko | d8aa32d | 2015-07-24 11:50:01 +0200 | [diff] [blame] | 1088 | ly_submodule_read(struct ly_module *module, const char *data, LY_MINFORMAT format, int implement) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1089 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1090 | assert(module); |
| 1091 | assert(data); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1092 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1093 | switch (format) { |
| 1094 | case LY_IN_YIN: |
Michal Vasko | d8aa32d | 2015-07-24 11:50:01 +0200 | [diff] [blame] | 1095 | return yin_read_submodule(module, data, implement); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1096 | case LY_IN_YANG: |
| 1097 | default: |
| 1098 | /* TODO */ |
| 1099 | return NULL; |
| 1100 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1101 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1102 | return NULL; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1103 | } |
| 1104 | |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1105 | struct ly_module * |
| 1106 | lys_read_import(struct ly_ctx *ctx, int fd, LY_MINFORMAT format) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1107 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1108 | struct ly_module *module; |
| 1109 | struct stat sb; |
| 1110 | char *addr; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1111 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1112 | if (!ctx || fd < 0) { |
| 1113 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1114 | return NULL; |
| 1115 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1116 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1117 | /* |
| 1118 | * TODO |
| 1119 | * This is just a temporary solution to make working automatic search for |
| 1120 | * imported modules. This doesn't work e.g. for streams (stdin) |
| 1121 | */ |
| 1122 | fstat(fd, &sb); |
| 1123 | addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1124 | |
| 1125 | switch (format) { |
| 1126 | case LY_IN_YIN: |
| 1127 | module = yin_read_module(ctx, addr, 0); |
| 1128 | break; |
| 1129 | case LY_IN_YANG: |
| 1130 | default: |
| 1131 | /* TODO */ |
| 1132 | munmap(addr, sb.st_size); |
| 1133 | return NULL; |
| 1134 | } |
| 1135 | munmap(addr, sb.st_size); |
| 1136 | |
| 1137 | return module; |
| 1138 | } |
| 1139 | |
| 1140 | API struct ly_module * |
| 1141 | lys_read(struct ly_ctx *ctx, int fd, LY_MINFORMAT format) |
| 1142 | { |
| 1143 | struct ly_module *module; |
| 1144 | struct stat sb; |
| 1145 | char *addr; |
| 1146 | |
| 1147 | if (!ctx || fd < 0) { |
| 1148 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1149 | return NULL; |
| 1150 | } |
| 1151 | |
| 1152 | /* |
| 1153 | * TODO |
| 1154 | * This is just a temporary solution to make working automatic search for |
| 1155 | * imported modules. This doesn't work e.g. for streams (stdin) |
| 1156 | */ |
| 1157 | fstat(fd, &sb); |
| 1158 | addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 1159 | module = lys_parse(ctx, addr, format); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1160 | munmap(addr, sb.st_size); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1161 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1162 | return module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1163 | } |
| 1164 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1165 | struct ly_submodule * |
Michal Vasko | d8aa32d | 2015-07-24 11:50:01 +0200 | [diff] [blame] | 1166 | ly_submodule_read_fd(struct ly_module *module, int fd, LY_MINFORMAT format, int implement) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1167 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1168 | struct ly_submodule *submodule; |
| 1169 | struct stat sb; |
| 1170 | char *addr; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1171 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1172 | assert(module); |
| 1173 | assert(fd >= 0); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1174 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1175 | /* |
| 1176 | * TODO |
| 1177 | * This is just a temporary solution to make working automatic search for |
| 1178 | * imported modules. This doesn't work e.g. for streams (stdin) |
| 1179 | */ |
| 1180 | fstat(fd, &sb); |
| 1181 | addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 1182 | /* TODO addr error check */ |
Michal Vasko | d8aa32d | 2015-07-24 11:50:01 +0200 | [diff] [blame] | 1183 | submodule = ly_submodule_read(module, addr, format, implement); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1184 | munmap(addr, sb.st_size); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1185 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1186 | return submodule; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1187 | |
| 1188 | } |
| 1189 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1190 | struct ly_restr * |
| 1191 | 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] | 1192 | { |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1193 | struct ly_restr *result; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1194 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1195 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1196 | if (!size) { |
| 1197 | return NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1198 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1199 | |
| 1200 | result = calloc(size, sizeof *result); |
| 1201 | for (i = 0; i < size; i++) { |
| 1202 | result[i].expr = lydict_insert(ctx, old[i].expr, 0); |
| 1203 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 1204 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 1205 | result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0); |
| 1206 | result[i].emsg = lydict_insert(ctx, old[i].emsg, 0); |
| 1207 | } |
| 1208 | |
| 1209 | return result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1210 | } |
| 1211 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1212 | void |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1213 | ly_restr_free(struct ly_ctx *ctx, struct ly_restr *restr) |
| 1214 | { |
| 1215 | assert(ctx); |
| 1216 | if (!restr) { |
| 1217 | return; |
| 1218 | } |
| 1219 | |
| 1220 | lydict_remove(ctx, restr->expr); |
| 1221 | lydict_remove(ctx, restr->dsc); |
| 1222 | lydict_remove(ctx, restr->ref); |
| 1223 | lydict_remove(ctx, restr->eapptag); |
| 1224 | lydict_remove(ctx, restr->emsg); |
| 1225 | } |
| 1226 | |
| 1227 | void |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1228 | ly_type_dup(struct ly_ctx *ctx, struct ly_type *new, struct ly_type *old) |
| 1229 | { |
| 1230 | int i; |
| 1231 | |
| 1232 | new->prefix = lydict_insert(ctx, old->prefix, 0); |
| 1233 | new->base = old->base; |
| 1234 | new->der = old->der; |
| 1235 | |
| 1236 | switch (new->base) { |
| 1237 | case LY_TYPE_BINARY: |
Radek Krejci | 425adf0 | 2015-06-26 16:23:28 +0200 | [diff] [blame] | 1238 | if (old->info.binary.length) { |
| 1239 | new->info.binary.length = ly_restr_dup(ctx, old->info.binary.length, 1); |
| 1240 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1241 | break; |
| 1242 | |
| 1243 | case LY_TYPE_BITS: |
| 1244 | new->info.bits.count = old->info.bits.count; |
| 1245 | if (new->info.bits.count) { |
| 1246 | new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit); |
| 1247 | for (i = 0; i < new->info.bits.count; i++) { |
| 1248 | new->info.bits.bit[i].name = lydict_insert(ctx, old->info.bits.bit[i].name, 0); |
| 1249 | new->info.bits.bit[i].dsc = lydict_insert(ctx, old->info.bits.bit[i].dsc, 0); |
| 1250 | new->info.bits.bit[i].ref = lydict_insert(ctx, old->info.bits.bit[i].ref, 0); |
| 1251 | new->info.bits.bit[i].status = old->info.bits.bit[i].status; |
| 1252 | new->info.bits.bit[i].pos = old->info.bits.bit[i].pos; |
| 1253 | } |
| 1254 | } |
| 1255 | break; |
| 1256 | |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1257 | case LY_TYPE_DEC64: |
| 1258 | new->info.dec64.dig = old->info.dec64.dig; |
| 1259 | if (old->info.dec64.range) { |
| 1260 | new->info.dec64.range = ly_restr_dup(ctx, old->info.dec64.range, 1); |
| 1261 | } |
| 1262 | break; |
| 1263 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1264 | case LY_TYPE_ENUM: |
| 1265 | new->info.enums.count = old->info.enums.count; |
| 1266 | if (new->info.enums.count) { |
| 1267 | new->info.enums.list = calloc(new->info.enums.count, sizeof *new->info.enums.list); |
| 1268 | for (i = 0; i < new->info.enums.count; i++) { |
| 1269 | new->info.enums.list[i].name = lydict_insert(ctx, old->info.enums.list[i].name, 0); |
| 1270 | new->info.enums.list[i].dsc = lydict_insert(ctx, old->info.enums.list[i].dsc, 0); |
| 1271 | new->info.enums.list[i].ref = lydict_insert(ctx, old->info.enums.list[i].ref, 0); |
| 1272 | new->info.enums.list[i].status = old->info.enums.list[i].status; |
| 1273 | new->info.enums.list[i].value = old->info.enums.list[i].value; |
| 1274 | } |
| 1275 | } |
| 1276 | break; |
| 1277 | |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1278 | case LY_TYPE_IDENT: |
| 1279 | new->info.ident.ref = old->info.ident.ref; |
| 1280 | break; |
| 1281 | |
Radek Krejci | af35142 | 2015-06-19 14:49:38 +0200 | [diff] [blame] | 1282 | case LY_TYPE_INST: |
| 1283 | new->info.inst.req = old->info.inst.req; |
| 1284 | break; |
| 1285 | |
Radek Krejci | f286013 | 2015-06-20 12:37:20 +0200 | [diff] [blame] | 1286 | case LY_TYPE_INT8: |
| 1287 | case LY_TYPE_INT16: |
| 1288 | case LY_TYPE_INT32: |
| 1289 | case LY_TYPE_INT64: |
| 1290 | case LY_TYPE_UINT8: |
| 1291 | case LY_TYPE_UINT16: |
| 1292 | case LY_TYPE_UINT32: |
| 1293 | case LY_TYPE_UINT64: |
Radek Krejci | 425adf0 | 2015-06-26 16:23:28 +0200 | [diff] [blame] | 1294 | if (old->info.num.range) { |
| 1295 | new->info.num.range = ly_restr_dup(ctx, old->info.num.range, 1); |
| 1296 | } |
Radek Krejci | f286013 | 2015-06-20 12:37:20 +0200 | [diff] [blame] | 1297 | break; |
| 1298 | |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1299 | case LY_TYPE_LEAFREF: |
| 1300 | new->info.lref.path = lydict_insert(ctx, old->info.lref.path, 0); |
| 1301 | break; |
| 1302 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1303 | case LY_TYPE_STRING: |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1304 | if (old->info.str.length) { |
| 1305 | new->info.str.length = ly_restr_dup(ctx, old->info.str.length, 1); |
| 1306 | } |
| 1307 | 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] | 1308 | break; |
| 1309 | |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1310 | case LY_TYPE_UNION: |
| 1311 | new->info.uni.count = old->info.uni.count; |
| 1312 | if (new->info.uni.count) { |
| 1313 | new->info.uni.type = calloc(new->info.uni.count, sizeof *new->info.uni.type); |
| 1314 | for (i = 0; i < new->info.uni.count; i++) { |
| 1315 | ly_type_dup(ctx, &(new->info.uni.type[i]), &(old->info.uni.type[i])); |
| 1316 | } |
| 1317 | } |
| 1318 | break; |
| 1319 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1320 | default: |
Radek Krejci | c7c8553 | 2015-07-02 10:16:54 +0200 | [diff] [blame] | 1321 | /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */ |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1322 | break; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | void |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1327 | ly_type_free(struct ly_ctx *ctx, struct ly_type *type) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1328 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1329 | int i; |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1330 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1331 | assert(ctx); |
| 1332 | if (!type) { |
| 1333 | return; |
| 1334 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1335 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1336 | lydict_remove(ctx, type->prefix); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1337 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1338 | switch (type->base) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1339 | case LY_TYPE_BINARY: |
| 1340 | ly_restr_free(ctx, type->info.binary.length); |
| 1341 | free(type->info.binary.length); |
| 1342 | break; |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 1343 | case LY_TYPE_BITS: |
| 1344 | for (i = 0; i < type->info.bits.count; i++) { |
| 1345 | lydict_remove(ctx, type->info.bits.bit[i].name); |
| 1346 | lydict_remove(ctx, type->info.bits.bit[i].dsc); |
| 1347 | lydict_remove(ctx, type->info.bits.bit[i].ref); |
| 1348 | } |
| 1349 | free(type->info.bits.bit); |
| 1350 | break; |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1351 | |
| 1352 | case LY_TYPE_DEC64: |
| 1353 | ly_restr_free(ctx, type->info.dec64.range); |
| 1354 | free(type->info.dec64.range); |
| 1355 | break; |
| 1356 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1357 | case LY_TYPE_ENUM: |
| 1358 | for (i = 0; i < type->info.enums.count; i++) { |
| 1359 | lydict_remove(ctx, type->info.enums.list[i].name); |
| 1360 | lydict_remove(ctx, type->info.enums.list[i].dsc); |
| 1361 | lydict_remove(ctx, type->info.enums.list[i].ref); |
| 1362 | } |
| 1363 | free(type->info.enums.list); |
| 1364 | break; |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1365 | |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 1366 | case LY_TYPE_INT8: |
| 1367 | case LY_TYPE_INT16: |
| 1368 | case LY_TYPE_INT32: |
| 1369 | case LY_TYPE_INT64: |
| 1370 | case LY_TYPE_UINT8: |
| 1371 | case LY_TYPE_UINT16: |
| 1372 | case LY_TYPE_UINT32: |
| 1373 | case LY_TYPE_UINT64: |
| 1374 | ly_restr_free(ctx, type->info.num.range); |
| 1375 | free(type->info.num.range); |
| 1376 | break; |
| 1377 | |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1378 | case LY_TYPE_LEAFREF: |
| 1379 | lydict_remove(ctx, type->info.lref.path); |
| 1380 | break; |
| 1381 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1382 | case LY_TYPE_STRING: |
| 1383 | ly_restr_free(ctx, type->info.str.length); |
| 1384 | free(type->info.str.length); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1385 | for (i = 0; i < type->info.str.pat_count; i++) { |
| 1386 | ly_restr_free(ctx, &type->info.str.patterns[i]); |
| 1387 | } |
| 1388 | free(type->info.str.patterns); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1389 | break; |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1390 | |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1391 | case LY_TYPE_UNION: |
| 1392 | for (i = 0; i < type->info.uni.count; i++) { |
| 1393 | ly_type_free(ctx, &type->info.uni.type[i]); |
| 1394 | } |
| 1395 | free(type->info.uni.type); |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1396 | break; |
| 1397 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1398 | default: |
Radek Krejci | c7c8553 | 2015-07-02 10:16:54 +0200 | [diff] [blame] | 1399 | /* 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] | 1400 | break; |
| 1401 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1402 | } |
| 1403 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1404 | struct ly_tpdf * |
| 1405 | 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] | 1406 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1407 | struct ly_tpdf *result; |
| 1408 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1409 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1410 | if (!size) { |
| 1411 | return NULL; |
| 1412 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1413 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1414 | result = calloc(size, sizeof *result); |
| 1415 | for (i = 0; i < size; i++) { |
| 1416 | result[i].name = lydict_insert(ctx, old[i].name, 0); |
| 1417 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 1418 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 1419 | result[i].flags = old[i].flags; |
| 1420 | result[i].module = old[i].module; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1421 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1422 | ly_type_dup(ctx, &(result[i].type), &(old[i].type)); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1423 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1424 | result[i].dflt = lydict_insert(ctx, old[i].dflt, 0); |
| 1425 | result[i].units = lydict_insert(ctx, old[i].units, 0); |
| 1426 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1427 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1428 | return result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1429 | } |
| 1430 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1431 | void |
| 1432 | ly_tpdf_free(struct ly_ctx *ctx, struct ly_tpdf *tpdf) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1433 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1434 | assert(ctx); |
| 1435 | if (!tpdf) { |
| 1436 | return; |
| 1437 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1438 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1439 | lydict_remove(ctx, tpdf->name); |
| 1440 | lydict_remove(ctx, tpdf->dsc); |
| 1441 | lydict_remove(ctx, tpdf->ref); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1442 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1443 | ly_type_free(ctx, &tpdf->type); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1444 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1445 | lydict_remove(ctx, tpdf->units); |
| 1446 | lydict_remove(ctx, tpdf->dflt); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1447 | } |
| 1448 | |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1449 | struct ly_when * |
| 1450 | ly_when_dup(struct ly_ctx *ctx, struct ly_when *old) |
| 1451 | { |
| 1452 | struct ly_when *new; |
| 1453 | |
| 1454 | if (!old) { |
| 1455 | return NULL; |
| 1456 | } |
| 1457 | |
| 1458 | new = calloc(1, sizeof *new); |
| 1459 | new->cond = lydict_insert(ctx, old->cond, 0); |
| 1460 | new->dsc = lydict_insert(ctx, old->dsc, 0); |
| 1461 | new->ref = lydict_insert(ctx, old->ref, 0); |
| 1462 | |
| 1463 | return new; |
| 1464 | } |
| 1465 | |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1466 | void |
| 1467 | ly_when_free(struct ly_ctx *ctx, struct ly_when *w) |
| 1468 | { |
| 1469 | if (!w) { |
| 1470 | return; |
| 1471 | } |
| 1472 | |
| 1473 | lydict_remove(ctx, w->cond); |
| 1474 | lydict_remove(ctx, w->dsc); |
| 1475 | lydict_remove(ctx, w->ref); |
| 1476 | |
| 1477 | free(w); |
| 1478 | } |
| 1479 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1480 | struct ly_augment * |
| 1481 | 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] | 1482 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1483 | struct ly_augment *new = NULL; |
| 1484 | int i = -1; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1485 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1486 | if (!size) { |
| 1487 | return NULL; |
| 1488 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1489 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1490 | new = calloc(size, sizeof *new); |
| 1491 | for (i = 0; i < size; i++) { |
| 1492 | new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0); |
| 1493 | new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0); |
| 1494 | new[i].ref = lydict_insert(module->ctx, old[i].ref, 0); |
| 1495 | new[i].flags = old[i].flags; |
| 1496 | /* .target = NULL; .nodetype = 0 */ |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1497 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1498 | new[i].parent = parent; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1499 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1500 | /* copy the definition of augment nodes */ |
| 1501 | if (old[i].child) { |
| 1502 | new[i].child = (struct ly_mnode *)lyxml_dup_elem(module->ctx, (struct lyxml_elem *)old[i].child, NULL, 1); |
| 1503 | } |
| 1504 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1505 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1506 | return new; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1507 | } |
| 1508 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1509 | struct ly_refine * |
| 1510 | 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] | 1511 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1512 | struct ly_refine *result; |
| 1513 | int i; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1514 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1515 | if (!size) { |
| 1516 | return NULL; |
| 1517 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1518 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1519 | result = calloc(size, sizeof *result); |
| 1520 | for (i = 0; i < size; i++) { |
| 1521 | result[i].target = lydict_insert(ctx, old[i].target, 0); |
| 1522 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 1523 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 1524 | result[i].flags = old[i].flags; |
| 1525 | result[i].target_type = old[i].target_type; |
| 1526 | result[i].must_size = old[i].must_size; |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1527 | 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] | 1528 | if (result[i].target_type & (LY_NODE_LEAF | LY_NODE_CHOICE)) { |
| 1529 | result[i].mod.dflt = lydict_insert(ctx, old[i].mod.dflt, 0); |
| 1530 | } else if (result[i].target_type == LY_NODE_CONTAINER) { |
| 1531 | result[i].mod.presence = lydict_insert(ctx, old[i].mod.presence, 0); |
| 1532 | } else if (result[i].target_type & (LY_NODE_LIST | LY_NODE_LEAFLIST)) { |
| 1533 | result[i].mod.list = old[i].mod.list; |
| 1534 | } |
| 1535 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1536 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1537 | return result; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1538 | } |
| 1539 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1540 | void |
| 1541 | ly_ident_free(struct ly_ctx *ctx, struct ly_ident *ident) |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1542 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1543 | struct ly_ident_der *der; |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1544 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1545 | assert(ctx); |
| 1546 | if (!ident) { |
| 1547 | return; |
| 1548 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1549 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1550 | /* |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1551 | * if caller free only a single data model which is used (its identity is |
| 1552 | * reference from identity in another module), this silly freeing can lead |
| 1553 | * to segmentation fault. But without noting if the module is used by some |
| 1554 | * other, it cannot be solved. |
Radek Krejci | a339069 | 2015-06-16 14:13:31 +0200 | [diff] [blame] | 1555 | * |
| 1556 | * Possible solution is to not allow caller to remove particular schema |
| 1557 | * from the context. |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1558 | */ |
| 1559 | while (ident->der) { |
| 1560 | der = ident->der; |
| 1561 | ident->der = der->next; |
| 1562 | free(der); |
| 1563 | } |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1564 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1565 | lydict_remove(ctx, ident->name); |
| 1566 | lydict_remove(ctx, ident->dsc); |
| 1567 | lydict_remove(ctx, ident->ref); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1568 | |
| 1569 | } |
| 1570 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1571 | void |
| 1572 | ly_grp_free(struct ly_ctx *ctx, struct ly_mnode_grp *grp) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1573 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1574 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1575 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1576 | /* handle only specific parts for LY_NODE_GROUPING */ |
| 1577 | for (i = 0; i < grp->tpdf_size; i++) { |
| 1578 | ly_tpdf_free(ctx, &grp->tpdf[i]); |
| 1579 | } |
| 1580 | free(grp->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1581 | } |
| 1582 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1583 | void |
| 1584 | ly_anyxml_free(struct ly_ctx *ctx, struct ly_mnode_anyxml *anyxml) |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1585 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1586 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1587 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1588 | for (i = 0; i < anyxml->must_size; i++) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1589 | ly_restr_free(ctx, &anyxml->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1590 | } |
| 1591 | free(anyxml->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1592 | |
| 1593 | ly_when_free(ctx, anyxml->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1594 | } |
| 1595 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1596 | void |
| 1597 | ly_leaf_free(struct ly_ctx *ctx, struct ly_mnode_leaf *leaf) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1598 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1599 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1600 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1601 | for (i = 0; i < leaf->must_size; i++) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1602 | ly_restr_free(ctx, &leaf->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1603 | } |
| 1604 | free(leaf->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1605 | |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1606 | ly_when_free(ctx, leaf->when); |
| 1607 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1608 | ly_type_free(ctx, &leaf->type); |
| 1609 | lydict_remove(ctx, leaf->units); |
| 1610 | lydict_remove(ctx, leaf->dflt); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1611 | } |
| 1612 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1613 | void |
| 1614 | ly_leaflist_free(struct ly_ctx *ctx, struct ly_mnode_leaflist *llist) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1615 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1616 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1617 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1618 | for (i = 0; i < llist->must_size; i++) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1619 | ly_restr_free(ctx, &llist->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1620 | } |
| 1621 | free(llist->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1622 | |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1623 | ly_when_free(ctx, llist->when); |
| 1624 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1625 | ly_type_free(ctx, &llist->type); |
| 1626 | lydict_remove(ctx, llist->units); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1627 | } |
| 1628 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1629 | void |
| 1630 | ly_list_free(struct ly_ctx *ctx, struct ly_mnode_list *list) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1631 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1632 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1633 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1634 | /* handle only specific parts for LY_NODE_LIST */ |
| 1635 | for (i = 0; i < list->tpdf_size; i++) { |
| 1636 | ly_tpdf_free(ctx, &list->tpdf[i]); |
| 1637 | } |
| 1638 | free(list->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1639 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1640 | for (i = 0; i < list->must_size; i++) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1641 | ly_restr_free(ctx, &list->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1642 | } |
| 1643 | free(list->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1644 | |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1645 | ly_when_free(ctx, list->when); |
| 1646 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1647 | for (i = 0; i < list->unique_size; i++) { |
| 1648 | free(list->unique[i].leafs); |
| 1649 | } |
| 1650 | free(list->unique); |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1651 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1652 | free(list->keys); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1653 | } |
| 1654 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1655 | void |
| 1656 | ly_container_free(struct ly_ctx *ctx, struct ly_mnode_container *cont) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1657 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1658 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1659 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1660 | /* handle only specific parts for LY_NODE_CONTAINER */ |
| 1661 | lydict_remove(ctx, cont->presence); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1662 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1663 | for (i = 0; i < cont->tpdf_size; i++) { |
| 1664 | ly_tpdf_free(ctx, &cont->tpdf[i]); |
| 1665 | } |
| 1666 | free(cont->tpdf); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1667 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1668 | for (i = 0; i < cont->must_size; i++) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1669 | ly_restr_free(ctx, &cont->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1670 | } |
| 1671 | free(cont->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1672 | |
| 1673 | ly_when_free(ctx, cont->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1674 | } |
| 1675 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1676 | void |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1677 | ly_feature_free(struct ly_ctx *ctx, struct ly_feature *f) |
| 1678 | { |
| 1679 | lydict_remove(ctx, f->name); |
| 1680 | lydict_remove(ctx, f->dsc); |
| 1681 | lydict_remove(ctx, f->ref); |
| 1682 | free(f->features); |
| 1683 | } |
| 1684 | |
| 1685 | void |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1686 | ly_deviation_free(struct ly_ctx *ctx, struct ly_deviation *dev) |
| 1687 | { |
| 1688 | int i, j; |
| 1689 | |
| 1690 | lydict_remove(ctx, dev->target_name); |
| 1691 | lydict_remove(ctx, dev->dsc); |
| 1692 | lydict_remove(ctx, dev->ref); |
| 1693 | |
| 1694 | for (i = 0; i < dev->deviate_size; i++) { |
| 1695 | lydict_remove(ctx, dev->deviate[i].dflt); |
| 1696 | lydict_remove(ctx, dev->deviate[i].units); |
| 1697 | |
| 1698 | if (dev->deviate[i].mod == LY_DEVIATE_DEL) { |
| 1699 | for (j = 0; j < dev->deviate[i].must_size; j++) { |
| 1700 | ly_restr_free(ctx, &dev->deviate[i].must[j]); |
| 1701 | } |
| 1702 | free(dev->deviate[i].must); |
| 1703 | |
| 1704 | for (j = 0; j < dev->deviate[i].unique_size; j++) { |
| 1705 | free(dev->deviate[j].unique[j].leafs); |
| 1706 | } |
| 1707 | free(dev->deviate[i].unique); |
| 1708 | } |
| 1709 | } |
| 1710 | free(dev->deviate); |
| 1711 | } |
| 1712 | |
| 1713 | void |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 1714 | ly_augment_free(struct ly_ctx *ctx, struct ly_augment *aug) |
| 1715 | { |
| 1716 | lydict_remove(ctx, aug->target_name); |
| 1717 | lydict_remove(ctx, aug->dsc); |
| 1718 | lydict_remove(ctx, aug->ref); |
| 1719 | |
Michal Vasko | 25b3961 | 2015-07-07 16:11:07 +0200 | [diff] [blame] | 1720 | free(aug->features); |
| 1721 | |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1722 | ly_when_free(ctx, aug->when); |
| 1723 | |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 1724 | lyxml_free_elem(ctx, (struct lyxml_elem *)aug->child); |
| 1725 | } |
| 1726 | |
| 1727 | void |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1728 | ly_uses_free(struct ly_ctx *ctx, struct ly_mnode_uses *uses) |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1729 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1730 | int i, j; |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1731 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1732 | for (i = 0; i < uses->refine_size; i++) { |
| 1733 | lydict_remove(ctx, uses->refine[i].target); |
| 1734 | lydict_remove(ctx, uses->refine[i].dsc); |
| 1735 | lydict_remove(ctx, uses->refine[i].ref); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1736 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1737 | for (j = 0; j < uses->refine[j].must_size; j++) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1738 | ly_restr_free(ctx, &uses->refine[i].must[j]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1739 | } |
| 1740 | free(uses->refine[i].must); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1741 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1742 | if (uses->refine[i].target_type & (LY_NODE_LEAF | LY_NODE_CHOICE)) { |
| 1743 | lydict_remove(ctx, uses->refine[i].mod.dflt); |
| 1744 | } else if (uses->refine[i].target_type & LY_NODE_CONTAINER) { |
| 1745 | lydict_remove(ctx, uses->refine[i].mod.presence); |
| 1746 | } |
| 1747 | } |
| 1748 | free(uses->refine); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1749 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1750 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 1751 | ly_augment_free(ctx, &uses->augment[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1752 | } |
| 1753 | free(uses->augment); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1754 | |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1755 | ly_when_free(ctx, uses->when); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1756 | } |
| 1757 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1758 | void |
| 1759 | ly_mnode_free(struct ly_mnode *node) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1760 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1761 | struct ly_ctx *ctx; |
| 1762 | struct ly_mnode *sub, *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1763 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1764 | if (!node) { |
| 1765 | return; |
| 1766 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1767 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1768 | assert(node->module); |
| 1769 | assert(node->module->ctx); |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1770 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1771 | ctx = node->module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1772 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1773 | /* common part */ |
| 1774 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
| 1775 | ly_mnode_free(sub); |
| 1776 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1777 | free(node->features); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1778 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1779 | lydict_remove(ctx, node->name); |
| 1780 | lydict_remove(ctx, node->dsc); |
| 1781 | lydict_remove(ctx, node->ref); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1782 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1783 | /* specific part */ |
| 1784 | switch (node->nodetype) { |
| 1785 | case LY_NODE_CONTAINER: |
| 1786 | ly_container_free(ctx, (struct ly_mnode_container *)node); |
| 1787 | break; |
| 1788 | case LY_NODE_CHOICE: |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1789 | ly_when_free(ctx, ((struct ly_mnode_choice *)node)->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1790 | break; |
| 1791 | case LY_NODE_LEAF: |
| 1792 | ly_leaf_free(ctx, (struct ly_mnode_leaf *)node); |
| 1793 | break; |
| 1794 | case LY_NODE_LEAFLIST: |
| 1795 | ly_leaflist_free(ctx, (struct ly_mnode_leaflist *)node); |
| 1796 | break; |
| 1797 | case LY_NODE_LIST: |
| 1798 | ly_list_free(ctx, (struct ly_mnode_list *)node); |
| 1799 | break; |
| 1800 | case LY_NODE_ANYXML: |
| 1801 | ly_anyxml_free(ctx, (struct ly_mnode_anyxml *)node); |
| 1802 | break; |
| 1803 | case LY_NODE_USES: |
| 1804 | ly_uses_free(ctx, (struct ly_mnode_uses *)node); |
| 1805 | break; |
| 1806 | case LY_NODE_CASE: |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1807 | ly_when_free(ctx, ((struct ly_mnode_case *)node)->when); |
| 1808 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1809 | case LY_NODE_AUGMENT: |
| 1810 | /* do nothing */ |
| 1811 | break; |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 1812 | case LY_NODE_GROUPING: |
| 1813 | case LY_NODE_RPC: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1814 | case LY_NODE_INPUT: |
| 1815 | case LY_NODE_OUTPUT: |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 1816 | case LY_NODE_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1817 | ly_grp_free(ctx, (struct ly_mnode_grp *)node); |
| 1818 | break; |
| 1819 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1820 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1821 | /* again common part */ |
| 1822 | ly_mnode_unlink(node); |
| 1823 | free(node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1824 | } |
| 1825 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1826 | static void |
| 1827 | module_free_common(struct ly_module *module) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1828 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1829 | struct ly_ctx *ctx; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1830 | unsigned int i; |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 1831 | int j, l; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1832 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1833 | assert(module->ctx); |
| 1834 | ctx = module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1835 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 1836 | /* as first step, free the imported modules */ |
| 1837 | for (i = 0; i < module->imp_size; i++) { |
| 1838 | /* get the imported module from the context and then free, |
| 1839 | * this check is necessary because the imported module can |
| 1840 | * be already removed |
| 1841 | */ |
| 1842 | l = ctx->models.used; |
| 1843 | for (j = 0; j < l; j++) { |
| 1844 | if (ctx->models.list[j] == module->imp[i].module) { |
Radek Krejci | 912da45 | 2015-07-29 14:10:06 +0200 | [diff] [blame] | 1845 | lys_free(module->imp[i].module); |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 1846 | break; |
| 1847 | } |
| 1848 | } |
| 1849 | } |
| 1850 | free(module->imp); |
| 1851 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1852 | while (module->data) { |
Radek Krejci | 2118196 | 2015-06-30 14:11:00 +0200 | [diff] [blame] | 1853 | ly_mnode_free(module->data); |
| 1854 | } |
| 1855 | while (module->rpc) { |
| 1856 | ly_mnode_free(module->rpc); |
| 1857 | } |
| 1858 | while (module->notif) { |
| 1859 | ly_mnode_free(module->notif); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1860 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1861 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1862 | lydict_remove(ctx, module->dsc); |
| 1863 | lydict_remove(ctx, module->ref); |
| 1864 | lydict_remove(ctx, module->org); |
| 1865 | lydict_remove(ctx, module->contact); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1866 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1867 | /* revisions */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1868 | for (i = 0; i < module->rev_size; i++) { |
| 1869 | lydict_remove(ctx, module->rev[i].dsc); |
| 1870 | lydict_remove(ctx, module->rev[i].ref); |
| 1871 | } |
| 1872 | free(module->rev); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1873 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1874 | /* identities */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1875 | for (i = 0; i < module->ident_size; i++) { |
| 1876 | ly_ident_free(ctx, &module->ident[i]); |
| 1877 | } |
| 1878 | module->ident_size = 0; |
| 1879 | free(module->ident); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1880 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1881 | /* typedefs */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1882 | for (i = 0; i < module->tpdf_size; i++) { |
| 1883 | ly_tpdf_free(ctx, &module->tpdf[i]); |
| 1884 | } |
| 1885 | free(module->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1886 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1887 | /* include */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1888 | for (i = 0; i < module->inc_size; i++) { |
| 1889 | ly_submodule_free(module->inc[i].submodule); |
| 1890 | } |
| 1891 | free(module->inc); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1892 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1893 | /* augment */ |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 1894 | for (i = 0; i < module->augment_size; i++) { |
| 1895 | ly_augment_free(ctx, &module->augment[i]); |
| 1896 | } |
| 1897 | free(module->augment); |
| 1898 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1899 | /* features */ |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1900 | for (i = 0; i < module->features_size; i++) { |
| 1901 | ly_feature_free(ctx, &module->features[i]); |
| 1902 | } |
| 1903 | free(module->features); |
| 1904 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1905 | /* deviations */ |
| 1906 | for (i = 0; i < module->deviation_size; i++) { |
| 1907 | ly_deviation_free(ctx, &module->deviation[i]); |
| 1908 | } |
| 1909 | free(module->deviation); |
| 1910 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1911 | lydict_remove(ctx, module->name); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1912 | } |
| 1913 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1914 | void |
| 1915 | ly_submodule_free(struct ly_submodule *submodule) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1916 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1917 | if (!submodule) { |
| 1918 | return; |
| 1919 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1920 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1921 | submodule->inc_size = 0; |
| 1922 | free(submodule->inc); |
| 1923 | submodule->inc = NULL; |
Radek Krejci | f388693 | 2015-06-04 17:36:06 +0200 | [diff] [blame] | 1924 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1925 | /* common part with struct ly_module */ |
| 1926 | module_free_common((struct ly_module *)submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1927 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1928 | /* no specific items to free */ |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1929 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1930 | free(submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1931 | } |
| 1932 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1933 | struct ly_mnode * |
| 1934 | 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] | 1935 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1936 | struct ly_mnode *retval = NULL, *aux, *child; |
| 1937 | struct ly_ctx *ctx = module->ctx; |
| 1938 | int i, j; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1939 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1940 | struct ly_mnode_container *cont; |
| 1941 | struct ly_mnode_container *cont_orig = (struct ly_mnode_container *)mnode; |
| 1942 | struct ly_mnode_choice *choice; |
| 1943 | struct ly_mnode_choice *choice_orig = (struct ly_mnode_choice *)mnode; |
| 1944 | struct ly_mnode_leaf *leaf; |
| 1945 | struct ly_mnode_leaf *leaf_orig = (struct ly_mnode_leaf *)mnode; |
| 1946 | struct ly_mnode_leaflist *llist; |
| 1947 | struct ly_mnode_leaflist *llist_orig = (struct ly_mnode_leaflist *)mnode; |
| 1948 | struct ly_mnode_list *list; |
| 1949 | struct ly_mnode_list *list_orig = (struct ly_mnode_list *)mnode; |
| 1950 | struct ly_mnode_anyxml *anyxml; |
| 1951 | struct ly_mnode_anyxml *anyxml_orig = (struct ly_mnode_anyxml *)mnode; |
| 1952 | struct ly_mnode_uses *uses; |
| 1953 | struct ly_mnode_uses *uses_orig = (struct ly_mnode_uses *)mnode; |
| 1954 | struct ly_mnode_grp *mix; |
| 1955 | struct ly_mnode_grp *mix_orig = (struct ly_mnode_grp *)mnode; |
| 1956 | struct ly_mnode_case *cs; |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1957 | struct ly_mnode_case *cs_orig = (struct ly_mnode_case *)mnode; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1958 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1959 | /* we cannot just duplicate memory since the strings are stored in |
| 1960 | * dictionary and we need to update dictionary counters. |
| 1961 | */ |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1962 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1963 | switch (mnode->nodetype) { |
| 1964 | case LY_NODE_CONTAINER: |
| 1965 | cont = calloc(1, sizeof *cont); |
| 1966 | retval = (struct ly_mnode *)cont; |
| 1967 | break; |
| 1968 | |
| 1969 | case LY_NODE_CHOICE: |
| 1970 | choice = calloc(1, sizeof *choice); |
| 1971 | retval = (struct ly_mnode *)choice; |
| 1972 | break; |
| 1973 | |
| 1974 | case LY_NODE_LEAF: |
| 1975 | leaf = calloc(1, sizeof *leaf); |
| 1976 | retval = (struct ly_mnode *)leaf; |
| 1977 | break; |
| 1978 | |
| 1979 | case LY_NODE_LEAFLIST: |
| 1980 | llist = calloc(1, sizeof *llist); |
| 1981 | retval = (struct ly_mnode *)llist; |
| 1982 | break; |
| 1983 | |
| 1984 | case LY_NODE_LIST: |
| 1985 | list = calloc(1, sizeof *list); |
| 1986 | retval = (struct ly_mnode *)list; |
| 1987 | break; |
| 1988 | |
| 1989 | case LY_NODE_ANYXML: |
| 1990 | anyxml = calloc(1, sizeof *anyxml); |
| 1991 | retval = (struct ly_mnode *)anyxml; |
| 1992 | break; |
| 1993 | |
| 1994 | case LY_NODE_USES: |
| 1995 | uses = calloc(1, sizeof *uses); |
| 1996 | retval = (struct ly_mnode *)uses; |
| 1997 | break; |
| 1998 | |
| 1999 | case LY_NODE_CASE: |
| 2000 | cs = calloc(1, sizeof *cs); |
| 2001 | retval = (struct ly_mnode *)cs; |
| 2002 | break; |
| 2003 | |
| 2004 | /* exact same structure */ |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 2005 | case LY_NODE_GROUPING: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2006 | case LY_NODE_RPC: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2007 | case LY_NODE_INPUT: |
| 2008 | case LY_NODE_OUTPUT: |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 2009 | case LY_NODE_NOTIF: |
| 2010 | mix = calloc(1, sizeof *mix); |
| 2011 | retval = (struct ly_mnode *)mix; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2012 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2013 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2014 | default: |
| 2015 | goto error; |
| 2016 | } |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame] | 2017 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2018 | /* |
| 2019 | * duplicate generic part of the structure |
| 2020 | */ |
| 2021 | retval->name = lydict_insert(ctx, mnode->name, 0); |
| 2022 | retval->dsc = lydict_insert(ctx, mnode->dsc, 0); |
| 2023 | retval->ref = lydict_insert(ctx, mnode->ref, 0); |
| 2024 | retval->flags = mnode->flags; |
| 2025 | if (!(retval->flags & LY_NODE_CONFIG_MASK)) { |
| 2026 | /* set parent's config flag */ |
| 2027 | retval->flags |= flags & LY_NODE_CONFIG_MASK; |
| 2028 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2029 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2030 | retval->module = module; |
| 2031 | retval->nodetype = mnode->nodetype; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2032 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2033 | retval->prev = retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2034 | |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2035 | retval->features_size = mnode->features_size; |
| 2036 | retval->features = calloc(retval->features_size, sizeof *retval->features); |
| 2037 | memcpy(retval->features, mnode->features, retval->features_size * sizeof *retval->features); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2038 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2039 | if (recursive) { |
| 2040 | /* go recursively */ |
| 2041 | LY_TREE_FOR(mnode->child, child) { |
| 2042 | aux = ly_mnode_dup(module, child, retval->flags, 1, line); |
| 2043 | if (!aux || ly_mnode_addchild(retval, aux)) { |
| 2044 | goto error; |
| 2045 | } |
| 2046 | } |
| 2047 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2048 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2049 | /* |
| 2050 | * duplicate specific part of the structure |
| 2051 | */ |
| 2052 | switch (mnode->nodetype) { |
| 2053 | case LY_NODE_CONTAINER: |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2054 | cont->when = ly_when_dup(ctx, cont_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2055 | cont->presence = lydict_insert(ctx, cont_orig->presence, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2056 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2057 | cont->must_size = cont_orig->must_size; |
| 2058 | cont->tpdf_size = cont_orig->tpdf_size; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2059 | |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 2060 | cont->must = ly_restr_dup(ctx, cont_orig->must, cont->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2061 | cont->tpdf = ly_tpdf_dup(ctx, cont_orig->tpdf, cont->tpdf_size); |
| 2062 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2063 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2064 | case LY_NODE_CHOICE: |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2065 | choice->when = ly_when_dup(ctx, choice_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2066 | if (choice->dflt) { |
| 2067 | LY_TREE_FOR(choice->child, child) { |
| 2068 | if (child->name == choice_orig->dflt->name) { |
| 2069 | choice->dflt = child; |
| 2070 | break; |
| 2071 | } |
| 2072 | } |
| 2073 | } |
| 2074 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2075 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2076 | case LY_NODE_LEAF: |
| 2077 | ly_type_dup(ctx, &(leaf->type), &(leaf_orig->type)); |
| 2078 | leaf->units = lydict_insert(ctx, leaf_orig->units, 0); |
| 2079 | leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2080 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2081 | leaf->must_size = leaf_orig->must_size; |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 2082 | leaf->must = ly_restr_dup(ctx, leaf_orig->must, leaf->must_size); |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2083 | |
| 2084 | leaf->when = ly_when_dup(ctx, leaf_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2085 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2086 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2087 | case LY_NODE_LEAFLIST: |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2088 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2089 | ly_type_dup(ctx, &(llist->type), &(llist_orig->type)); |
| 2090 | llist->units = lydict_insert(ctx, llist_orig->units, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2091 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2092 | llist->min = llist_orig->min; |
| 2093 | llist->max = llist_orig->max; |
| 2094 | |
| 2095 | llist->must_size = llist_orig->must_size; |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 2096 | llist->must = ly_restr_dup(ctx, llist_orig->must, llist->must_size); |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2097 | |
| 2098 | llist->when = ly_when_dup(ctx, llist_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2099 | break; |
| 2100 | |
| 2101 | case LY_NODE_LIST: |
| 2102 | list->min = list_orig->min; |
| 2103 | list->max = list_orig->max; |
| 2104 | |
| 2105 | list->must_size = list_orig->must_size; |
| 2106 | list->tpdf_size = list_orig->tpdf_size; |
| 2107 | list->keys_size = list_orig->keys_size; |
| 2108 | list->unique_size = list_orig->unique_size; |
| 2109 | |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 2110 | list->must = ly_restr_dup(ctx, list_orig->must, list->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2111 | list->tpdf = ly_tpdf_dup(ctx, list_orig->tpdf, list->tpdf_size); |
| 2112 | |
| 2113 | if (list->keys_size) { |
| 2114 | list->keys = calloc(list->keys_size, sizeof *list->keys); |
| 2115 | for (i = 0; i < list->keys_size; i++) { |
| 2116 | list->keys[i] = find_leaf(retval, list_orig->keys[i]->name, 0); |
| 2117 | } |
| 2118 | } |
| 2119 | if (list->unique_size) { |
| 2120 | list->unique = calloc(list->unique_size, sizeof *list->unique); |
| 2121 | for (i = 0; i < list->unique_size; i++) { |
| 2122 | list->unique[i].leafs = calloc(list->unique[i].leafs_size, sizeof *list->unique[i].leafs); |
| 2123 | for (j = 0; j < list->unique[i].leafs_size; j++) { |
| 2124 | list->unique[i].leafs[j] = find_leaf(retval, list_orig->unique[i].leafs[j]->name, 0); |
| 2125 | } |
| 2126 | } |
| 2127 | } |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2128 | list->when = ly_when_dup(ctx, list_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2129 | break; |
| 2130 | |
| 2131 | case LY_NODE_ANYXML: |
| 2132 | anyxml->must_size = anyxml_orig->must_size; |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 2133 | anyxml->must = ly_restr_dup(ctx, anyxml_orig->must, anyxml->must_size); |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2134 | anyxml->when = ly_when_dup(ctx, anyxml_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2135 | break; |
| 2136 | |
| 2137 | case LY_NODE_USES: |
| 2138 | uses->grp = uses_orig->grp; |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2139 | uses->when = ly_when_dup(ctx, uses_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2140 | uses->refine_size = uses_orig->refine_size; |
| 2141 | uses->refine = ly_refine_dup(ctx, uses_orig->refine, uses_orig->refine_size); |
| 2142 | uses->augment_size = uses_orig->augment_size; |
| 2143 | uses->augment = ly_augment_dup(module, (struct ly_mnode *)uses, uses_orig->augment, uses_orig->augment_size); |
| 2144 | if (resolve_uses(uses, line)) { |
| 2145 | goto error; |
| 2146 | } |
| 2147 | break; |
| 2148 | |
| 2149 | case LY_NODE_CASE: |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2150 | cs->when = ly_when_dup(ctx, cs_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2151 | break; |
| 2152 | |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 2153 | case LY_NODE_GROUPING: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2154 | case LY_NODE_RPC: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2155 | case LY_NODE_INPUT: |
| 2156 | case LY_NODE_OUTPUT: |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 2157 | case LY_NODE_NOTIF: |
| 2158 | mix->tpdf_size = mix_orig->tpdf_size; |
| 2159 | mix->tpdf = ly_tpdf_dup(ctx, mix_orig->tpdf, mix->tpdf_size); |
Radek Krejci | a01e543 | 2015-06-16 10:35:25 +0200 | [diff] [blame] | 2160 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2161 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2162 | default: |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2163 | /* LY_NODE_AUGMENT */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2164 | goto error; |
| 2165 | } |
| 2166 | |
| 2167 | return retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2168 | |
| 2169 | error: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2170 | LOGDBG("error: %s:%d", __FILE__, __LINE__); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2171 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2172 | ly_mnode_free(retval); |
| 2173 | return NULL; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2174 | } |
| 2175 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2176 | API void |
Radek Krejci | 912da45 | 2015-07-29 14:10:06 +0200 | [diff] [blame] | 2177 | lys_free(struct ly_module *module) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2178 | { |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2179 | struct ly_ctx *ctx; |
| 2180 | int i; |
| 2181 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2182 | if (!module) { |
| 2183 | return; |
| 2184 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2185 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2186 | /* remove schema from the context */ |
| 2187 | ctx = module->ctx; |
| 2188 | if (ctx->models.used) { |
| 2189 | for (i = 0; i < ctx->models.used; i++) { |
| 2190 | if (ctx->models.list[i] == module) { |
| 2191 | /* replace the position in the list by the last module in the list */ |
| 2192 | ctx->models.used--; |
| 2193 | ctx->models.list[i] = ctx->models.list[ctx->models.used]; |
| 2194 | ctx->models.list[ctx->models.used] = NULL; |
| 2195 | /* we are done */ |
| 2196 | break; |
| 2197 | } |
| 2198 | } |
| 2199 | } |
| 2200 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2201 | /* common part with struct ly_submodule */ |
| 2202 | module_free_common(module); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2203 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2204 | /* specific items to free */ |
| 2205 | lydict_remove(module->ctx, module->ns); |
| 2206 | lydict_remove(module->ctx, module->prefix); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2207 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2208 | free(module); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2209 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2210 | |
| 2211 | /* |
| 2212 | * op: 1 - enable, 0 - disable |
| 2213 | */ |
| 2214 | static int |
| 2215 | ly_features_change(struct ly_module *module, const char *name, int op) |
| 2216 | { |
| 2217 | int all = 0; |
| 2218 | int i, j, k; |
| 2219 | |
| 2220 | if (!module || !name || !strlen(name)) { |
| 2221 | return EXIT_FAILURE; |
| 2222 | } |
| 2223 | |
| 2224 | if (!strcmp(name, "*")) { |
| 2225 | /* enable all */ |
| 2226 | all = 1; |
| 2227 | } |
| 2228 | |
| 2229 | /* module itself */ |
| 2230 | for (i = 0; i < module->features_size; i++) { |
| 2231 | if (all || !strcmp(module->features[i].name, name)) { |
| 2232 | if (op) { |
| 2233 | module->features[i].flags |= LY_NODE_FENABLED; |
| 2234 | /* enable referenced features (recursion) */ |
| 2235 | for (k = 0; k < module->features[i].features_size; k++) { |
| 2236 | ly_features_change(module->features[i].features[k]->module, |
| 2237 | module->features[i].features[k]->name, op); |
| 2238 | } |
| 2239 | } else { |
| 2240 | module->features[i].flags &= ~LY_NODE_FENABLED; |
| 2241 | } |
| 2242 | if (!all) { |
| 2243 | return EXIT_SUCCESS; |
| 2244 | } |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | /* submodules */ |
| 2249 | for (j = 0; j < module->inc_size; j++) { |
| 2250 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
| 2251 | if (all || !strcmp(module->inc[j].submodule->features[i].name, name)) { |
| 2252 | if (op) { |
| 2253 | module->inc[j].submodule->features[i].flags |= LY_NODE_FENABLED; |
| 2254 | } else { |
| 2255 | module->inc[j].submodule->features[i].flags &= ~LY_NODE_FENABLED; |
| 2256 | } |
| 2257 | if (!all) { |
| 2258 | return EXIT_SUCCESS; |
| 2259 | } |
| 2260 | } |
| 2261 | } |
| 2262 | } |
| 2263 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2264 | /* TODO submodules of submodules ... */ |
| 2265 | |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2266 | if (all) { |
| 2267 | return EXIT_SUCCESS; |
| 2268 | } else { |
| 2269 | return EXIT_FAILURE; |
| 2270 | } |
| 2271 | } |
| 2272 | |
| 2273 | API int |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2274 | lys_features_enable(struct ly_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2275 | { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2276 | return ly_features_change(module, feature, 1); |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | API int |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2280 | lys_features_disable(struct ly_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2281 | { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2282 | return ly_features_change(module, feature, 0); |
| 2283 | } |
| 2284 | |
| 2285 | API int |
| 2286 | lys_features_state(struct ly_module *module, const char *feature) |
| 2287 | { |
| 2288 | int i, j; |
| 2289 | |
| 2290 | if (!module || !feature) { |
| 2291 | return -1; |
| 2292 | } |
| 2293 | |
| 2294 | /* search for the specified feature */ |
| 2295 | /* module itself */ |
| 2296 | for (i = 0; i < module->features_size; i++) { |
| 2297 | if (!strcmp(feature, module->features[i].name)) { |
| 2298 | if (module->features[i].flags & LY_NODE_FENABLED) { |
| 2299 | return 1; |
| 2300 | } else { |
| 2301 | return 0; |
| 2302 | } |
| 2303 | } |
| 2304 | } |
| 2305 | |
| 2306 | /* submodules */ |
| 2307 | for (j = 0; j < module->inc_size; j++) { |
| 2308 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
| 2309 | if (!strcmp(feature, module->inc[j].submodule->features[i].name)) { |
| 2310 | if (module->inc[j].submodule->features[i].flags & LY_NODE_FENABLED) { |
| 2311 | return 1; |
| 2312 | } else { |
| 2313 | return 0; |
| 2314 | } |
| 2315 | } |
| 2316 | } |
| 2317 | } |
| 2318 | |
| 2319 | /* TODO submodules of submodules ... */ |
| 2320 | |
| 2321 | /* feature definition not found */ |
| 2322 | return -1; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2323 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2324 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2325 | API const char ** |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2326 | lys_features_list(struct ly_module *module, uint8_t **states) |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2327 | { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2328 | const char **result = NULL; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2329 | int i, j; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2330 | unsigned int count; |
| 2331 | |
| 2332 | if (!module) { |
| 2333 | return NULL; |
| 2334 | } |
| 2335 | |
| 2336 | count = module->features_size; |
| 2337 | for (i = 0; i < module->inc_size; i++) { |
| 2338 | count += module->inc[i].submodule->features_size; |
| 2339 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2340 | result = malloc((count + 1) * sizeof *result); |
| 2341 | if (states) { |
| 2342 | *states = malloc((count + 1) * sizeof **states); |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2343 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2344 | count = 0; |
| 2345 | |
| 2346 | /* module itself */ |
| 2347 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2348 | result[count] = module->features[i].name; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2349 | if (states) { |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2350 | if (module->features[i].flags & LY_NODE_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2351 | (*states)[count] = 1; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2352 | } else { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2353 | (*states)[count] = 0; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2354 | } |
| 2355 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2356 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | /* submodules */ |
| 2360 | for (j = 0; j < module->inc_size; j++) { |
| 2361 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2362 | result[count] = module->inc[j].submodule->features[i].name; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2363 | if (module->inc[j].submodule->features[i].flags & LY_NODE_FENABLED) { |
| 2364 | (*states)[count] = 1; |
| 2365 | } else { |
| 2366 | (*states)[count] = 0; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2367 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2368 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2369 | } |
| 2370 | } |
| 2371 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2372 | /* TODO submodules of submodules ... */ |
| 2373 | |
| 2374 | /* terminating NULL byte */ |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2375 | result[count] = NULL; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2376 | |
| 2377 | return result; |
| 2378 | } |
Radek Krejci | b20c62d | 2015-07-07 17:07:14 +0200 | [diff] [blame] | 2379 | |
Radek Krejci | e3d724f | 2015-07-10 15:32:42 +0200 | [diff] [blame] | 2380 | static struct ly_ident * |
| 2381 | find_base_ident_sub(struct ly_module *module, struct ly_ident *ident, const char *basename) |
| 2382 | { |
| 2383 | unsigned int i, j; |
| 2384 | struct ly_ident *base_iter = NULL; |
| 2385 | struct ly_ident_der *der; |
| 2386 | |
| 2387 | /* search module */ |
| 2388 | for (i = 0; i < module->ident_size; i++) { |
| 2389 | if (!strcmp(basename, module->ident[i].name)) { |
| 2390 | |
| 2391 | if (!ident) { |
| 2392 | /* just search for type, so do not modify anything, just return |
| 2393 | * the base identity pointer |
| 2394 | */ |
| 2395 | return &module->ident[i]; |
| 2396 | } |
| 2397 | |
| 2398 | /* we are resolving identity definition, so now update structures */ |
| 2399 | ident->base = base_iter = &module->ident[i]; |
| 2400 | |
| 2401 | break; |
| 2402 | } |
| 2403 | } |
| 2404 | |
| 2405 | /* search submodules */ |
| 2406 | if (!base_iter) { |
| 2407 | for (j = 0; j < module->inc_size; j++) { |
| 2408 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 2409 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
| 2410 | |
| 2411 | if (!ident) { |
| 2412 | return &module->inc[j].submodule->ident[i]; |
| 2413 | } |
| 2414 | |
| 2415 | ident->base = base_iter = &module->inc[j].submodule->ident[i]; |
| 2416 | break; |
| 2417 | } |
| 2418 | } |
| 2419 | } |
| 2420 | } |
| 2421 | |
| 2422 | /* we found it somewhere */ |
| 2423 | if (base_iter) { |
| 2424 | while (base_iter) { |
| 2425 | for (der = base_iter->der; der && der->next; der = der->next); |
| 2426 | if (der) { |
| 2427 | der->next = malloc(sizeof *der); |
| 2428 | der = der->next; |
| 2429 | } else { |
| 2430 | ident->base->der = der = malloc(sizeof *der); |
| 2431 | } |
| 2432 | der->next = NULL; |
| 2433 | der->ident = ident; |
| 2434 | |
| 2435 | base_iter = base_iter->base; |
| 2436 | } |
| 2437 | return ident->base; |
| 2438 | } |
| 2439 | |
| 2440 | return NULL; |
| 2441 | } |
| 2442 | |
| 2443 | struct ly_ident * |
| 2444 | find_base_ident(struct ly_module *module, struct ly_ident *ident, const char *basename, int line, const char* parent) |
| 2445 | { |
| 2446 | const char *name; |
| 2447 | int prefix_len = 0; |
| 2448 | int i, found = 0; |
| 2449 | struct ly_ident *result; |
| 2450 | |
| 2451 | /* search for the base identity */ |
| 2452 | name = strchr(basename, ':'); |
| 2453 | if (name) { |
| 2454 | /* set name to correct position after colon */ |
| 2455 | prefix_len = name - basename; |
| 2456 | name++; |
| 2457 | |
| 2458 | if (!strncmp(basename, module->prefix, prefix_len) && !module->prefix[prefix_len]) { |
| 2459 | /* prefix refers to the current module, ignore it */ |
| 2460 | prefix_len = 0; |
| 2461 | } |
| 2462 | } else { |
| 2463 | name = basename; |
| 2464 | } |
| 2465 | |
| 2466 | if (prefix_len) { |
| 2467 | /* get module where to search */ |
| 2468 | for (i = 0; i < module->imp_size; i++) { |
| 2469 | if (!strncmp(module->imp[i].prefix, basename, prefix_len) |
| 2470 | && !module->imp[i].prefix[prefix_len]) { |
| 2471 | module = module->imp[i].module; |
| 2472 | found = 1; |
| 2473 | break; |
| 2474 | } |
| 2475 | } |
| 2476 | if (!found) { |
| 2477 | /* identity refers unknown data model */ |
| 2478 | LOGVAL(VE_INPREFIX, line, basename); |
| 2479 | return NULL; |
| 2480 | } |
| 2481 | } else { |
| 2482 | /* search in submodules */ |
| 2483 | for (i = 0; i < module->inc_size; i++) { |
| 2484 | result = find_base_ident_sub((struct ly_module *)module->inc[i].submodule, ident, name); |
| 2485 | if (result) { |
| 2486 | return result; |
| 2487 | } |
| 2488 | } |
| 2489 | } |
| 2490 | |
| 2491 | /* search in the identified module */ |
| 2492 | result = find_base_ident_sub(module, ident, name); |
| 2493 | if (!result) { |
| 2494 | LOGVAL(VE_INARG, line, basename, parent); |
| 2495 | } |
| 2496 | |
| 2497 | return result; |
| 2498 | } |
| 2499 | |
| 2500 | struct ly_ident * |
| 2501 | find_identityref(struct ly_ident *base, const char *name, const char *ns) |
| 2502 | { |
| 2503 | struct ly_ident_der *der; |
| 2504 | |
| 2505 | if (!base || !name || !ns) { |
| 2506 | return NULL; |
| 2507 | } |
| 2508 | |
| 2509 | for(der = base->der; der; der = der->next) { |
| 2510 | if (!strcmp(der->ident->name, name) && ns == der->ident->module->ns) { |
| 2511 | /* we have match */ |
| 2512 | return der->ident; |
| 2513 | } |
| 2514 | } |
| 2515 | |
| 2516 | /* not found */ |
| 2517 | return NULL; |
| 2518 | } |
| 2519 | |
Radek Krejci | b20c62d | 2015-07-07 17:07:14 +0200 | [diff] [blame] | 2520 | API struct lyd_node * |
Radek Krejci | 912da45 | 2015-07-29 14:10:06 +0200 | [diff] [blame] | 2521 | lyd_parse(struct ly_ctx *ctx, const char *data, LY_DFORMAT format) |
Radek Krejci | b20c62d | 2015-07-07 17:07:14 +0200 | [diff] [blame] | 2522 | { |
| 2523 | if (!ctx || !data) { |
| 2524 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 2525 | return NULL; |
| 2526 | } |
| 2527 | |
| 2528 | switch (format) { |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 2529 | case LY_DATA_XML: |
Radek Krejci | b20c62d | 2015-07-07 17:07:14 +0200 | [diff] [blame] | 2530 | return xml_read_data(ctx, data); |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 2531 | case LY_DATA_JSON: |
Radek Krejci | b20c62d | 2015-07-07 17:07:14 +0200 | [diff] [blame] | 2532 | default: |
| 2533 | /* TODO */ |
| 2534 | return NULL; |
| 2535 | } |
| 2536 | |
| 2537 | return NULL; |
| 2538 | } |
| 2539 | |
Radek Krejci | 0e1d1a6 | 2015-07-31 11:17:01 +0200 | [diff] [blame] | 2540 | API void |
Radek Krejci | 912da45 | 2015-07-29 14:10:06 +0200 | [diff] [blame] | 2541 | lyd_free(struct lyd_node *node) |
Radek Krejci | b20c62d | 2015-07-07 17:07:14 +0200 | [diff] [blame] | 2542 | { |
| 2543 | struct lyd_node *next, *child; |
| 2544 | |
Radek Krejci | 52f791c | 2015-07-15 11:14:17 +0200 | [diff] [blame] | 2545 | if (!node) { |
| 2546 | return; |
| 2547 | } |
| 2548 | |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 2549 | if (!(node->schema->nodetype & (LY_NODE_LEAF | LY_NODE_LEAFLIST | LY_NODE_ANYXML))) { |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 2550 | /* free children */ |
| 2551 | LY_TREE_FOR_SAFE(node->child, next, child) { |
Radek Krejci | 912da45 | 2015-07-29 14:10:06 +0200 | [diff] [blame] | 2552 | lyd_free(child); |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 2553 | } |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 2554 | } else if (node->schema->nodetype == LY_NODE_ANYXML) { |
Radek Krejci | 9566b09 | 2015-07-31 11:18:15 +0200 | [diff] [blame^] | 2555 | lyxml_free_elem(node->schema->module->ctx, ((struct lyd_node_anyxml *)node)->value); |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 2556 | } else { |
| 2557 | /* free value */ |
| 2558 | switch(((struct lyd_node_leaf *)node)->value_type) { |
| 2559 | case LY_TYPE_BINARY: |
| 2560 | case LY_TYPE_STRING: |
| 2561 | lydict_remove(node->schema->module->ctx, ((struct lyd_node_leaf *)node)->value.string); |
| 2562 | break; |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 2563 | case LY_TYPE_BITS: |
| 2564 | if (((struct lyd_node_leaf *)node)->value.bit) { |
| 2565 | free(((struct lyd_node_leaf *)node)->value.bit); |
| 2566 | } |
| 2567 | break; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 2568 | default: |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 2569 | /* TODO nothing needed : LY_TYPE_BOOL, LY_TYPE_DEC64*/ |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 2570 | break; |
| 2571 | } |
Radek Krejci | b20c62d | 2015-07-07 17:07:14 +0200 | [diff] [blame] | 2572 | } |
| 2573 | |
| 2574 | if (node->prev->next) { |
| 2575 | node->prev->next = node->next; |
| 2576 | } else if (node->parent) { |
| 2577 | /* first node */ |
| 2578 | node->parent->child = node->next; |
| 2579 | } |
| 2580 | if (node->next) { |
| 2581 | node->next->prev = node->prev; |
| 2582 | } |
| 2583 | |
| 2584 | free(node); |
| 2585 | } |
Michal Vasko | f54f2f8 | 2015-07-17 13:12:05 +0200 | [diff] [blame] | 2586 | |
Radek Krejci | 2dbed4c | 2015-07-20 13:02:04 +0200 | [diff] [blame] | 2587 | API int |
Radek Krejci | 9566b09 | 2015-07-31 11:18:15 +0200 | [diff] [blame^] | 2588 | lyd_is_last(struct lyd_node *node) |
Radek Krejci | 2dbed4c | 2015-07-20 13:02:04 +0200 | [diff] [blame] | 2589 | { |
| 2590 | struct lyd_node *n; |
| 2591 | |
| 2592 | if (!node->next) { |
| 2593 | return 1; |
| 2594 | } |
| 2595 | |
| 2596 | for (n = node->next; n; n = n->next) { |
| 2597 | switch (n->schema->nodetype) { |
| 2598 | case LY_NODE_LIST: |
| 2599 | if (!((struct lyd_node_list *)n)->lprev) { |
| 2600 | return 0; |
| 2601 | } |
| 2602 | break; |
| 2603 | case LY_NODE_LEAFLIST: |
| 2604 | if (!((struct lyd_node_leaflist *)n)->lprev) { |
| 2605 | return 0; |
| 2606 | } |
| 2607 | break; |
| 2608 | default: |
| 2609 | return 0; |
| 2610 | } |
| 2611 | } |
| 2612 | return 1; |
| 2613 | } |