Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 2 | * @file tree_schema.c |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 4 | * @brief Manipulation with libyang schema data structures |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 13 | */ |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 14 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 16 | |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 19 | #include <limits.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <sys/mman.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 22 | #include <sys/types.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 23 | #include <sys/stat.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 24 | #include <fcntl.h> |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 25 | #include <string.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | #include <errno.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" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 32 | #include "resolve.h" |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 33 | #include "xml.h" |
Michal Vasko | fc5744d | 2015-10-22 12:09:34 +0200 | [diff] [blame] | 34 | #include "xml_internal.h" |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 35 | #include "tree_internal.h" |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 36 | #include "validation.h" |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 37 | #include "parser_yang.h" |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 38 | |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 39 | static int |
| 40 | lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old, |
| 41 | struct unres_schema *unres); |
| 42 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 43 | API const struct lys_feature * |
| 44 | lys_is_disabled(const struct lys_node *node, int recursive) |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 45 | { |
| 46 | int i; |
| 47 | |
| 48 | check: |
| 49 | if (node->nodetype != LYS_INPUT && node->nodetype != LYS_OUTPUT) { |
| 50 | /* input/output does not have if-feature, so skip them */ |
| 51 | |
| 52 | /* check local if-features */ |
| 53 | for (i = 0; i < node->features_size; i++) { |
| 54 | if (!(node->features[i]->flags & LYS_FENABLED)) { |
| 55 | return node->features[i]; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if (!recursive) { |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | /* go through parents */ |
| 65 | if (node->nodetype == LYS_AUGMENT) { |
| 66 | /* go to parent actually means go to the target node */ |
| 67 | node = ((struct lys_node_augment *)node)->target; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 68 | } else if (node->parent) { |
| 69 | node = node->parent; |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 70 | } else { |
| 71 | return NULL; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 74 | if (recursive == 2) { |
| 75 | /* continue only if the node cannot have a data instance */ |
| 76 | if (node->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST)) { |
| 77 | return NULL; |
| 78 | } |
| 79 | } |
| 80 | goto check; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 83 | int |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 84 | lys_get_sibling(const struct lys_node *siblings, const char *mod_name, int mod_name_len, const char *name, |
| 85 | int nam_len, LYS_NODE type, const struct lys_node **ret) |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 86 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 87 | const struct lys_node *node, *parent = NULL; |
| 88 | const struct lys_module *mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 89 | const char *node_mod_name; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 90 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 91 | assert(siblings && mod_name && name); |
Michal Vasko | 165dc4a | 2015-10-23 09:44:27 +0200 | [diff] [blame] | 92 | assert(!(type & (LYS_USES | LYS_GROUPING))); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 93 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 94 | /* fill the lengths in case the caller is so indifferent */ |
| 95 | if (!mod_name_len) { |
| 96 | mod_name_len = strlen(mod_name); |
| 97 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 98 | if (!nam_len) { |
| 99 | nam_len = strlen(name); |
| 100 | } |
| 101 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 102 | /* set mod correctly */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 103 | parent = lys_parent(siblings); |
| 104 | if (!parent) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 105 | mod = lys_node_module(siblings); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 108 | /* try to find the node */ |
| 109 | node = NULL; |
| 110 | while ((node = lys_getnext(node, parent, mod, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) { |
| 111 | if (!type || (node->nodetype & type)) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 112 | /* module name comparison */ |
| 113 | node_mod_name = lys_node_module(node)->name; |
| 114 | if ((node_mod_name != mod_name) && (strncmp(node_mod_name, mod_name, mod_name_len) || node_mod_name[mod_name_len])) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 115 | continue; |
| 116 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 117 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 118 | /* direct name check */ |
| 119 | if ((node->name == name) || (!strncmp(node->name, name, nam_len) && !node->name[nam_len])) { |
| 120 | if (ret) { |
| 121 | *ret = node; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 122 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 123 | return EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 124 | } |
| 125 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | return EXIT_FAILURE; |
| 129 | } |
| 130 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 131 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 132 | lys_get_data_sibling(const struct lys_module *mod, const struct lys_node *siblings, const char *name, LYS_NODE type, |
| 133 | const struct lys_node **ret) |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 134 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 135 | const struct lys_node *node; |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 136 | |
| 137 | assert(siblings && name); |
| 138 | assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))); |
| 139 | |
| 140 | /* find the beginning */ |
| 141 | while (siblings->prev->next) { |
| 142 | siblings = siblings->prev; |
| 143 | } |
| 144 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 145 | if (!mod) { |
| 146 | mod = siblings->module; |
| 147 | } |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 148 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 149 | /* try to find the node */ |
| 150 | node = NULL; |
| 151 | while ((node = lys_getnext(node, siblings->parent, mod, 0))) { |
| 152 | if (!type || (node->nodetype & type)) { |
| 153 | /* module check */ |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 154 | if (lys_node_module(node) != lys_main_module(mod)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 155 | continue; |
| 156 | } |
| 157 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 158 | /* direct name check */ |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 159 | if (ly_strequal(node->name, name, 0)) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 160 | if (ret) { |
| 161 | *ret = node; |
| 162 | } |
| 163 | return EXIT_SUCCESS; |
| 164 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 165 | } |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | return EXIT_FAILURE; |
| 169 | } |
| 170 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 171 | API const struct lys_node * |
| 172 | lys_getnext(const struct lys_node *last, const struct lys_node *parent, const struct lys_module *module, int options) |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 173 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 174 | const struct lys_node *next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 175 | |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 176 | if (!last) { |
| 177 | /* first call */ |
| 178 | |
| 179 | /* get know where to start */ |
| 180 | if (parent) { |
| 181 | /* schema subtree */ |
| 182 | next = last = parent->child; |
| 183 | } else { |
| 184 | /* top level data */ |
| 185 | assert(module); |
| 186 | next = last = module->data; |
| 187 | } |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 188 | } else { |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 189 | /* continue after the last returned value */ |
| 190 | next = last->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | repeat: |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 194 | while (next && (next->nodetype == LYS_GROUPING)) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 195 | if (options & LYS_GETNEXT_WITHGROUPING) { |
| 196 | return next; |
| 197 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 198 | next = next->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Radek Krejci | c4ccbd9 | 2016-01-07 13:13:33 +0100 | [diff] [blame] | 201 | if (!next) { |
Radek Krejci | 63318e8 | 2016-02-12 09:43:12 +0100 | [diff] [blame] | 202 | if (!last || lys_parent(last) == parent) { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 203 | /* no next element */ |
| 204 | return NULL; |
| 205 | } |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 206 | last = lys_parent(last); |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 207 | next = last->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 208 | goto repeat; |
| 209 | } |
| 210 | |
| 211 | switch (next->nodetype) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 212 | case LYS_INPUT: |
| 213 | case LYS_OUTPUT: |
| 214 | if (options & LYS_GETNEXT_WITHINOUT) { |
| 215 | return next; |
| 216 | } else { |
| 217 | next = next->child; |
| 218 | goto repeat; |
| 219 | } |
| 220 | break; |
| 221 | |
Michal Vasko | a5835e9 | 2015-10-20 15:07:39 +0200 | [diff] [blame] | 222 | case LYS_CASE: |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 223 | if (options & LYS_GETNEXT_WITHCASE) { |
| 224 | return next; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 225 | } else { |
| 226 | next = next->child; |
| 227 | goto repeat; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 228 | } |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 229 | break; |
| 230 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 231 | case LYS_USES: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 232 | /* go into */ |
| 233 | next = next->child; |
| 234 | goto repeat; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 235 | |
Radek Krejci | dfcae7d | 2015-10-20 17:13:01 +0200 | [diff] [blame] | 236 | case LYS_RPC: |
| 237 | case LYS_NOTIF: |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 238 | case LYS_CONTAINER: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 239 | case LYS_LEAF: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 240 | case LYS_ANYXML: |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 241 | case LYS_LIST: |
| 242 | case LYS_LEAFLIST: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 243 | return next; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 244 | |
| 245 | case LYS_CHOICE: |
| 246 | if (options & LYS_GETNEXT_WITHCHOICE) { |
| 247 | return next; |
| 248 | } else { |
| 249 | /* go into */ |
| 250 | next = next->child; |
| 251 | goto repeat; |
| 252 | } |
| 253 | break; |
| 254 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 255 | default: |
| 256 | /* we should not be here */ |
| 257 | return NULL; |
| 258 | } |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 259 | |
| 260 | |
| 261 | } |
| 262 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 263 | static const struct lys_node * |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 264 | check_mand_getnext(const struct lys_node *last, const struct lys_node *parent, const struct lys_module *module) |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 265 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 266 | const struct lys_node *next; |
| 267 | |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 268 | next = lys_getnext(last, parent, module, LYS_GETNEXT_WITHCHOICE); |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 269 | |
Radek Krejci | 4b6c211 | 2015-10-06 12:48:34 +0200 | [diff] [blame] | 270 | repeat: |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 271 | if (next && next->nodetype == LYS_CONTAINER) { |
| 272 | if (((struct lys_node_container *)next)->presence) { |
| 273 | /* mandatory elements under the non-existing presence |
| 274 | * container are not mandatory - 7.6.5, rule 1 */ |
| 275 | next = next->next; |
| 276 | } else { |
| 277 | /* go into */ |
| 278 | next = next->child; |
| 279 | } |
| 280 | goto repeat; |
| 281 | } |
| 282 | |
| 283 | return next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 284 | } |
| 285 | |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 286 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 287 | check_mand_check(const struct lys_node *node, const struct lys_node *stop, const struct lyd_node *data) |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 288 | { |
Radek Krejci | 7531aa2 | 2016-04-12 13:52:19 +0200 | [diff] [blame] | 289 | const struct lys_node *siter = NULL, *missing_parent = NULL; |
Radek Krejci | 96e2085 | 2016-04-11 17:51:26 +0200 | [diff] [blame] | 290 | struct lys_node *parent = NULL; |
Radek Krejci | 7531aa2 | 2016-04-12 13:52:19 +0200 | [diff] [blame] | 291 | const struct lyd_node *diter = NULL; |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 292 | struct ly_set *set = NULL; |
Michal Vasko | f610fd4 | 2016-04-19 10:38:20 +0200 | [diff] [blame] | 293 | unsigned int i, toplevel = (stop && stop->nodetype != LYS_OUTPUT) ? 0 : 1; |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 294 | uint32_t minmax, min, max; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 295 | |
Radek Krejci | 7531aa2 | 2016-04-12 13:52:19 +0200 | [diff] [blame] | 296 | if (data) { |
| 297 | /* go to the correct data level */ |
| 298 | for (parent = lys_parent(node); parent && parent != stop; parent = lys_parent(parent)) { |
| 299 | /* 7.6.5, rule 1 (presence container), checking presence |
| 300 | * is not needed since it is done in check_mand_getnext() |
| 301 | */ |
| 302 | |
| 303 | if (parent->nodetype != LYS_CONTAINER) { |
| 304 | /* not interested in LYS_USES, LYS_CASE or LYS_CHOICE, |
| 305 | * because they are not instantiated in data tree */ |
| 306 | continue; |
| 307 | } |
| 308 | /* add the parent to the list for searching in data tree */ |
| 309 | if (!set) { |
| 310 | set = ly_set_new(); |
| 311 | } |
| 312 | /* ignore return - memory error is logged and we will |
| 313 | * check at least the rest of nodes we have */ |
| 314 | (void)ly_set_add(set, parent); |
| 315 | } |
| 316 | if (set) { |
| 317 | for (i = set->number; i > 0; ) { |
| 318 | i--; |
| 319 | LY_TREE_FOR(toplevel ? data : data->child, diter) { |
| 320 | if (diter->schema == set->set.s[i]) { |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | if (!diter) { |
| 325 | /* instance not found */ |
| 326 | missing_parent = set->set.s[i]; |
Radek Krejci | 29ac4f9 | 2016-04-12 15:05:53 +0200 | [diff] [blame] | 327 | if (toplevel) { |
| 328 | data = NULL; |
| 329 | } |
Radek Krejci | 7531aa2 | 2016-04-12 13:52:19 +0200 | [diff] [blame] | 330 | break; |
| 331 | } |
| 332 | data = diter; |
| 333 | toplevel = 0; |
| 334 | if (data->validity == LYD_VAL_OK) { |
| 335 | /* already checked */ |
| 336 | ly_set_free(set); |
| 337 | return EXIT_SUCCESS; |
| 338 | } |
| 339 | } |
| 340 | ly_set_free(set); |
| 341 | } |
| 342 | } else { |
| 343 | missing_parent = node; |
| 344 | } |
| 345 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 346 | if (node->flags & LYS_MAND_TRUE) { |
Radek Krejci | 7531aa2 | 2016-04-12 13:52:19 +0200 | [diff] [blame] | 347 | if (missing_parent) { |
Radek Krejci | 29ac4f9 | 2016-04-12 15:05:53 +0200 | [diff] [blame] | 348 | LOGVAL(LYE_MISSELEM, LY_VLOG_LYD, data, node->name, |
| 349 | (lys_parent(node) ? lys_parent(node)->name : lys_node_module(node)->name)); |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 350 | return EXIT_FAILURE; |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 351 | } |
Radek Krejci | 7531aa2 | 2016-04-12 13:52:19 +0200 | [diff] [blame] | 352 | |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 353 | switch (node->nodetype) { |
| 354 | case LYS_LEAF: |
| 355 | case LYS_ANYXML: |
| 356 | case LYS_CHOICE: |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 357 | if (lys_parent(node) && lys_parent(node)->nodetype == LYS_CASE) { |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 358 | /* 7.6.5, rule 2 */ |
| 359 | /* 7.9.4, rule 1 */ |
Radek Krejci | 7531aa2 | 2016-04-12 13:52:19 +0200 | [diff] [blame] | 360 | |
| 361 | /* try to find the node's siblings in data */ |
| 362 | LY_TREE_FOR(toplevel ? data : data->child, diter) { |
| 363 | LY_TREE_FOR(lys_parent(node)->child, siter) { |
| 364 | if (siter == diter->schema) { |
| 365 | /* some sibling exists, rule applies */ |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 366 | break; |
| 367 | } |
| 368 | } |
Radek Krejci | 7531aa2 | 2016-04-12 13:52:19 +0200 | [diff] [blame] | 369 | if (siter) { |
| 370 | break; |
| 371 | } |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 372 | } |
| 373 | if (!siter) { |
| 374 | /* no sibling exists */ |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 375 | return EXIT_SUCCESS; |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 376 | } |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 377 | } |
Radek Krejci | 50c5b0e | 2015-08-21 15:43:45 +0200 | [diff] [blame] | 378 | |
Radek Krejci | 96e2085 | 2016-04-11 17:51:26 +0200 | [diff] [blame] | 379 | if (node->nodetype == LYS_CHOICE) { |
| 380 | siter = NULL; |
Radek Krejci | 7531aa2 | 2016-04-12 13:52:19 +0200 | [diff] [blame] | 381 | LY_TREE_FOR(toplevel ? data : data->child, diter) { |
Radek Krejci | 96e2085 | 2016-04-11 17:51:26 +0200 | [diff] [blame] | 382 | while ((siter = lys_getnext(siter, node, NULL, 0))) { |
| 383 | if (diter->schema == siter) { |
| 384 | return EXIT_SUCCESS; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | } else { |
Radek Krejci | 7531aa2 | 2016-04-12 13:52:19 +0200 | [diff] [blame] | 389 | LY_TREE_FOR(toplevel ? data : data->child, diter) { |
Radek Krejci | 96e2085 | 2016-04-11 17:51:26 +0200 | [diff] [blame] | 390 | if (diter->schema == node) { |
| 391 | return EXIT_SUCCESS; |
| 392 | } |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 393 | } |
| 394 | } |
Radek Krejci | 50c5b0e | 2015-08-21 15:43:45 +0200 | [diff] [blame] | 395 | |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 396 | /* instance not found */ |
| 397 | /* 7.6.5, rule 3 (or 2) */ |
| 398 | /* 7.9.4, rule 2 */ |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 399 | if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 29ac4f9 | 2016-04-12 15:05:53 +0200 | [diff] [blame] | 400 | LOGVAL(LYE_NOMANDCHOICE, LY_VLOG_LYD, toplevel ? NULL : data, node->name); |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 401 | } else { |
Radek Krejci | 29ac4f9 | 2016-04-12 15:05:53 +0200 | [diff] [blame] | 402 | LOGVAL(LYE_MISSELEM, LY_VLOG_LYD, toplevel ? NULL : data, node->name, |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 403 | (lys_parent(node) ? lys_parent(node)->name : lys_node_module(node)->name)); |
| 404 | } |
Radek Krejci | 7531aa2 | 2016-04-12 13:52:19 +0200 | [diff] [blame] | 405 | break; |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 406 | default: |
| 407 | /* error */ |
Radek Krejci | 7531aa2 | 2016-04-12 13:52:19 +0200 | [diff] [blame] | 408 | LOGINT; |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 409 | break; |
| 410 | } |
Radek Krejci | 7531aa2 | 2016-04-12 13:52:19 +0200 | [diff] [blame] | 411 | return EXIT_FAILURE; |
| 412 | |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 413 | } else if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 414 | /* search for number of instances */ |
| 415 | minmax = 0; |
Radek Krejci | 7531aa2 | 2016-04-12 13:52:19 +0200 | [diff] [blame] | 416 | if (!missing_parent) { |
| 417 | LY_TREE_FOR(toplevel ? data : data->child, diter) { |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 418 | if (diter->schema == node) { |
| 419 | minmax++; |
Michal Vasko | 05ed842 | 2016-04-12 11:38:24 +0200 | [diff] [blame] | 420 | /* remember the last instance, we will use it in the log message */ |
| 421 | data = diter; |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 422 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | /* check the specified constraints */ |
| 427 | if (node->nodetype == LYS_LIST) { |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 428 | min = ((struct lys_node_list *)node)->min; |
| 429 | max = ((struct lys_node_list *)node)->max; |
| 430 | } else { |
| 431 | min = ((struct lys_node_leaflist *)node)->min; |
| 432 | max = ((struct lys_node_leaflist *)node)->max; |
| 433 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 434 | |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 435 | if (min && (minmax < min)) { |
Radek Krejci | 29ac4f9 | 2016-04-12 15:05:53 +0200 | [diff] [blame] | 436 | LOGVAL(LYE_NOMIN, LY_VLOG_LYD, toplevel ? NULL : data, node->name); |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 437 | return EXIT_FAILURE; |
| 438 | } |
| 439 | if (max && (minmax > max)) { |
Radek Krejci | 29ac4f9 | 2016-04-12 15:05:53 +0200 | [diff] [blame] | 440 | LOGVAL(LYE_NOMAX, LY_VLOG_LYD, toplevel ? NULL : data, node->name); |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 441 | return EXIT_FAILURE; |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 442 | } |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 443 | } |
| 444 | |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 445 | return EXIT_SUCCESS; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 448 | int |
Radek Krejci | 763122e | 2016-04-05 16:35:33 +0200 | [diff] [blame] | 449 | ly_check_mandatory(const struct lyd_node *data, const struct lys_node *schema, int status) |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 450 | { |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 451 | const struct lys_node *siter, *saux, *saux2, *parent = NULL, *parent2; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 452 | const struct lyd_node *diter; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 453 | int found; |
| 454 | |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 455 | assert(data || schema); |
| 456 | |
Radek Krejci | 7531aa2 | 2016-04-12 13:52:19 +0200 | [diff] [blame] | 457 | if (schema) { |
| 458 | /* schema is preferred regardless the data */ |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 459 | siter = schema; |
Radek Krejci | 7531aa2 | 2016-04-12 13:52:19 +0200 | [diff] [blame] | 460 | } else { |
| 461 | /* !schema && data */ |
Radek Krejci | e2f1221 | 2016-02-12 13:50:22 +0100 | [diff] [blame] | 462 | schema = data->schema; |
| 463 | siter = data->schema->child; |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 464 | } |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 465 | |
| 466 | repeat: |
| 467 | while (siter) { |
Radek Krejci | 763122e | 2016-04-05 16:35:33 +0200 | [diff] [blame] | 468 | if (lys_is_disabled(siter, 2) || (!status && (siter->flags & LYS_CONFIG_R))) { |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 469 | siter = siter->next; |
| 470 | continue; |
| 471 | } |
| 472 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 473 | switch (siter->nodetype) { |
| 474 | case LYS_CONTAINER: |
| 475 | case LYS_LEAF: |
| 476 | case LYS_ANYXML: |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 477 | case LYS_LIST: |
| 478 | case LYS_LEAFLIST: |
| 479 | /* check if there is some mandatory node; first test the siter itself ... */ |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 480 | if (check_mand_check(siter, lys_parent(siter), data)) { |
| 481 | return EXIT_FAILURE; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 482 | } |
| 483 | /* ... and then the subtree */ |
Radek Krejci | cf82e93 | 2016-04-11 17:52:31 +0200 | [diff] [blame] | 484 | if (siter->nodetype == LYS_CONTAINER && !((struct lys_node_container *)siter)->presence) { |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 485 | saux = NULL; |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 486 | while ((saux = check_mand_getnext(saux, siter, NULL))) { |
Radek Krejci | e8da2e0 | 2016-04-11 17:53:34 +0200 | [diff] [blame] | 487 | if ((status || (saux->flags & LYS_CONFIG_W)) && check_mand_check(saux, lys_parent(siter), data)) { |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 488 | return EXIT_FAILURE; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | } |
| 492 | siter = siter->next; |
| 493 | break; |
| 494 | case LYS_CHOICE: |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 495 | /* search for instance */ |
| 496 | saux = siter; |
| 497 | siter = siter->child; |
| 498 | found = 0; |
| 499 | parent2 = NULL; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 500 | repeat_choice: |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 501 | while (siter && data) { |
Radek Krejci | 763122e | 2016-04-05 16:35:33 +0200 | [diff] [blame] | 502 | if (lys_is_disabled(siter, 2) || (!status && (siter->flags & LYS_CONFIG_R))) { |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 503 | siter = siter->next; |
| 504 | continue; |
| 505 | } |
| 506 | |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 507 | switch (siter->nodetype) { |
| 508 | case LYS_CONTAINER: |
| 509 | case LYS_LEAF: |
| 510 | case LYS_LEAFLIST: |
| 511 | case LYS_LIST: |
| 512 | case LYS_ANYXML: |
| 513 | LY_TREE_FOR(data->child, diter) { |
| 514 | if (diter->schema == siter) { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 515 | break; |
| 516 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 517 | } |
| 518 | if (diter) { |
| 519 | /* got instance */ |
| 520 | /* check presence of mandatory siblings */ |
Radek Krejci | 65d0a65 | 2015-08-27 13:09:42 +0200 | [diff] [blame] | 521 | if (parent2 && parent2->nodetype == LYS_CASE) { |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 522 | saux2 = NULL; |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 523 | while ((saux2 = check_mand_getnext(saux2, parent2, NULL))) { |
Radek Krejci | 7531aa2 | 2016-04-12 13:52:19 +0200 | [diff] [blame] | 524 | if (check_mand_check(saux2, lys_parent(saux), data)) { |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 525 | return EXIT_FAILURE; |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | } |
| 529 | siter = parent2 = NULL; |
| 530 | found = 1; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 531 | break; |
| 532 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 533 | siter = siter->next; |
| 534 | break; |
| 535 | case LYS_CASE: |
| 536 | case LYS_CHOICE: |
| 537 | case LYS_USES: |
| 538 | /* go into */ |
Radek Krejci | 37bda00 | 2015-08-27 11:23:56 +0200 | [diff] [blame] | 539 | if (!parent2) { |
| 540 | parent2 = siter; |
| 541 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 542 | siter = siter->child; |
| 543 | break; |
| 544 | case LYS_AUGMENT: |
| 545 | case LYS_GROUPING: |
| 546 | /* skip */ |
| 547 | siter = siter->next; |
| 548 | break; |
| 549 | default: |
| 550 | /* error */ |
| 551 | break; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 552 | } |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 555 | if (parent2) { |
| 556 | siter = parent2->next; |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 557 | if (lys_parent(parent2) == saux) { |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 558 | parent2 = NULL; |
| 559 | } else { |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 560 | parent2 = lys_parent(parent2); |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 561 | } |
| 562 | goto repeat_choice; |
| 563 | } |
| 564 | |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 565 | if (!found && (saux->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 7531aa2 | 2016-04-12 13:52:19 +0200 | [diff] [blame] | 566 | LOGVAL(LYE_MISSELEM, LY_VLOG_LYD, data, saux->name, |
| 567 | (lys_parent(saux) ? lys_parent(saux)->name : lys_node_module(saux)->name)); |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 568 | return EXIT_FAILURE; |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /* go to next */ |
| 572 | siter = saux->next; |
| 573 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 574 | break; |
| 575 | case LYS_USES: |
| 576 | case LYS_CASE: |
Michal Vasko | f610fd4 | 2016-04-19 10:38:20 +0200 | [diff] [blame] | 577 | case LYS_INPUT: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 578 | /* go into */ |
| 579 | parent = siter; |
| 580 | siter = siter->child; |
| 581 | break; |
| 582 | default: |
| 583 | /* can ignore, go to next */ |
| 584 | siter = siter->next; |
| 585 | break; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | if (parent) { |
| 590 | siter = parent->next; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 591 | if (lys_parent(parent) == schema) { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 592 | parent = NULL; |
| 593 | } else { |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 594 | parent = lys_parent(parent); |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 595 | } |
| 596 | goto repeat; |
| 597 | } |
| 598 | |
Michal Vasko | 84f821a | 2016-04-11 11:03:42 +0200 | [diff] [blame] | 599 | return EXIT_SUCCESS; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 600 | } |
| 601 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 602 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 603 | lys_node_unlink(struct lys_node *node) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 604 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 605 | struct lys_node *parent, *first; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 606 | struct lys_module *main_module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 607 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 608 | if (!node) { |
| 609 | return; |
| 610 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 611 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 612 | /* unlink from data model if necessary */ |
| 613 | if (node->module) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 614 | /* get main module with data tree */ |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 615 | main_module = lys_node_module(node); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 616 | if (main_module->data == node) { |
| 617 | main_module->data = node->next; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 618 | } |
| 619 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 620 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 621 | /* store pointers to important nodes */ |
| 622 | parent = node->parent; |
Michal Vasko | 3a9943b | 2015-09-23 11:33:50 +0200 | [diff] [blame] | 623 | if (parent && (parent->nodetype == LYS_AUGMENT)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 624 | /* handle augments - first, unlink it from the augment parent ... */ |
| 625 | if (parent->child == node) { |
| 626 | parent->child = node->next; |
| 627 | } |
| 628 | /* and then continue with the target parent */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 629 | parent = ((struct lys_node_augment *)parent)->target; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 630 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 631 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 632 | /* unlink from parent */ |
| 633 | if (parent) { |
| 634 | if (parent->child == node) { |
| 635 | parent->child = node->next; |
| 636 | } |
| 637 | node->parent = NULL; |
| 638 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 639 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 640 | /* unlink from siblings */ |
| 641 | if (node->prev == node) { |
| 642 | /* there are no more siblings */ |
| 643 | return; |
| 644 | } |
| 645 | if (node->next) { |
| 646 | node->next->prev = node->prev; |
| 647 | } else { |
| 648 | /* unlinking the last element */ |
| 649 | if (parent) { |
| 650 | first = parent->child; |
| 651 | } else { |
| 652 | first = node; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 653 | while (first->prev->next) { |
Michal Vasko | 276f96b | 2015-09-23 11:34:28 +0200 | [diff] [blame] | 654 | first = first->prev; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | first->prev = node->prev; |
| 658 | } |
| 659 | if (node->prev->next) { |
| 660 | node->prev->next = node->next; |
| 661 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 662 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 663 | /* clean up the unlinked element */ |
| 664 | node->next = NULL; |
| 665 | node->prev = node; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 666 | } |
| 667 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 668 | struct lys_node_grp * |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 669 | lys_find_grouping_up(const char *name, struct lys_node *start) |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 670 | { |
| 671 | struct lys_node *par_iter, *iter, *stop; |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 672 | |
| 673 | for (par_iter = start; par_iter; par_iter = par_iter->parent) { |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 674 | /* top-level augment, look into module (uses augment is handled correctly below) */ |
| 675 | if (par_iter->parent && !par_iter->parent->parent && (par_iter->parent->nodetype == LYS_AUGMENT)) { |
| 676 | par_iter = par_iter->parent->module->data; |
| 677 | if (!par_iter) { |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 678 | break; |
| 679 | } |
| 680 | } |
| 681 | |
Michal Vasko | 6f929da | 2015-10-02 16:23:25 +0200 | [diff] [blame] | 682 | if (par_iter->parent && (par_iter->parent->nodetype & (LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_USES))) { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 683 | continue; |
| 684 | } |
| 685 | |
| 686 | for (iter = par_iter, stop = NULL; iter; iter = iter->prev) { |
| 687 | if (!stop) { |
| 688 | stop = par_iter; |
| 689 | } else if (iter == stop) { |
| 690 | break; |
| 691 | } |
| 692 | if (iter->nodetype != LYS_GROUPING) { |
| 693 | continue; |
| 694 | } |
| 695 | |
Radek Krejci | f8426a7 | 2015-10-31 23:14:03 +0100 | [diff] [blame] | 696 | if (!strcmp(name, iter->name)) { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 697 | return (struct lys_node_grp *)iter; |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 702 | return NULL; |
| 703 | } |
| 704 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 705 | /* |
| 706 | * get next grouping in the root's subtree, in the |
| 707 | * first call, tha last is NULL |
| 708 | */ |
| 709 | static struct lys_node_grp * |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 710 | lys_get_next_grouping(struct lys_node_grp *lastgrp, struct lys_node *root) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 711 | { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 712 | struct lys_node *last = (struct lys_node *)lastgrp; |
| 713 | struct lys_node *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 714 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 715 | assert(root); |
| 716 | |
| 717 | if (!last) { |
| 718 | last = root; |
| 719 | } |
| 720 | |
| 721 | while (1) { |
| 722 | if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) { |
| 723 | next = last->child; |
| 724 | } else { |
| 725 | next = NULL; |
| 726 | } |
| 727 | if (!next) { |
| 728 | if (last == root) { |
| 729 | /* we are done */ |
| 730 | return NULL; |
| 731 | } |
| 732 | |
| 733 | /* no children, go to siblings */ |
| 734 | next = last->next; |
| 735 | } |
| 736 | while (!next) { |
| 737 | /* go back through parents */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 738 | if (lys_parent(last) == root) { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 739 | /* we are done */ |
| 740 | return NULL; |
| 741 | } |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 742 | next = last->next; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 743 | last = lys_parent(last); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | if (next->nodetype == LYS_GROUPING) { |
| 747 | return (struct lys_node_grp *)next; |
| 748 | } |
| 749 | |
| 750 | last = next; |
| 751 | } |
| 752 | } |
| 753 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 754 | /* logs directly */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 755 | int |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 756 | lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module) |
| 757 | { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 758 | struct lys_node *start, *stop, *iter; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 759 | struct lys_node_grp *grp; |
| 760 | int down; |
| 761 | |
| 762 | assert(node); |
| 763 | |
| 764 | if (!parent) { |
| 765 | assert(module); |
| 766 | } else { |
| 767 | module = parent->module; |
| 768 | } |
| 769 | |
| 770 | switch (node->nodetype) { |
| 771 | case LYS_GROUPING: |
| 772 | /* 6.2.1, rule 6 */ |
| 773 | if (parent) { |
| 774 | if (parent->child) { |
| 775 | down = 1; |
| 776 | start = parent->child; |
| 777 | } else { |
| 778 | down = 0; |
| 779 | start = parent; |
| 780 | } |
| 781 | } else { |
| 782 | down = 1; |
| 783 | start = module->data; |
| 784 | } |
| 785 | /* go up */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 786 | if (lys_find_grouping_up(node->name, start)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 787 | LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name); |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 788 | return EXIT_FAILURE; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 789 | } |
| 790 | /* go down, because grouping can be defined after e.g. container in which is collision */ |
| 791 | if (down) { |
| 792 | for (iter = start, stop = NULL; iter; iter = iter->prev) { |
| 793 | if (!stop) { |
| 794 | stop = start; |
| 795 | } else if (iter == stop) { |
| 796 | break; |
| 797 | } |
| 798 | if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) { |
| 799 | continue; |
| 800 | } |
| 801 | |
| 802 | grp = NULL; |
| 803 | while ((grp = lys_get_next_grouping(grp, iter))) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 804 | if (ly_strequal(node->name, grp->name, 1)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 805 | LOGVAL(LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 806 | return EXIT_FAILURE; |
| 807 | } |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | break; |
| 812 | case LYS_LEAF: |
| 813 | case LYS_LEAFLIST: |
| 814 | case LYS_LIST: |
| 815 | case LYS_CONTAINER: |
| 816 | case LYS_CHOICE: |
| 817 | case LYS_ANYXML: |
| 818 | /* 6.2.1, rule 7 */ |
| 819 | if (parent) { |
| 820 | iter = parent; |
| 821 | while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE))) { |
| 822 | iter = iter->parent; |
| 823 | } |
| 824 | if (!iter) { |
| 825 | stop = NULL; |
| 826 | iter = module->data; |
| 827 | } else { |
| 828 | stop = iter; |
| 829 | iter = iter->child; |
| 830 | } |
| 831 | } else { |
| 832 | stop = NULL; |
| 833 | iter = module->data; |
| 834 | } |
| 835 | while (iter) { |
| 836 | if (iter->nodetype & (LYS_USES | LYS_CASE)) { |
| 837 | iter = iter->child; |
| 838 | continue; |
| 839 | } |
| 840 | |
| 841 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CONTAINER | LYS_CHOICE | LYS_ANYXML)) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 842 | if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 843 | LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, strnodetype(node->nodetype), node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 844 | return EXIT_FAILURE; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /* special case for choice - we must check the choice's name as |
| 849 | * well as the names of nodes under the choice |
| 850 | */ |
| 851 | if (iter->nodetype == LYS_CHOICE) { |
| 852 | iter = iter->child; |
| 853 | continue; |
| 854 | } |
| 855 | |
| 856 | /* go to siblings */ |
| 857 | if (!iter->next) { |
| 858 | /* no sibling, go to parent's sibling */ |
| 859 | do { |
| 860 | iter = iter->parent; |
| 861 | if (iter && iter->next) { |
| 862 | break; |
| 863 | } |
| 864 | } while (iter != stop); |
| 865 | |
| 866 | if (iter == stop) { |
| 867 | break; |
| 868 | } |
| 869 | } |
| 870 | iter = iter->next; |
| 871 | } |
| 872 | break; |
| 873 | case LYS_CASE: |
| 874 | /* 6.2.1, rule 8 */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 875 | if (parent) { |
| 876 | start = parent->child; |
| 877 | } else { |
| 878 | start = module->data; |
| 879 | } |
| 880 | |
| 881 | LY_TREE_FOR(start, iter) { |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 882 | if (!(iter->nodetype & (LYS_ANYXML | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) { |
| 883 | continue; |
| 884 | } |
| 885 | |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 886 | if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 887 | LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "case", node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 888 | return EXIT_FAILURE; |
| 889 | } |
| 890 | } |
| 891 | break; |
| 892 | default: |
| 893 | /* no check needed */ |
| 894 | break; |
| 895 | } |
| 896 | |
| 897 | return EXIT_SUCCESS; |
| 898 | } |
| 899 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 900 | /* logs directly */ |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 901 | int |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 902 | lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child) |
| 903 | { |
Radek Krejci | 9272055 | 2015-10-05 15:28:27 +0200 | [diff] [blame] | 904 | struct lys_node *iter; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 905 | int type; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 906 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 907 | assert(child); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 908 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 909 | if (parent) { |
| 910 | type = parent->nodetype; |
| 911 | module = parent->module; |
| 912 | } else { |
| 913 | assert(module); |
| 914 | type = 0; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 915 | } |
| 916 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 917 | /* checks */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 918 | switch (type) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 919 | case LYS_CONTAINER: |
| 920 | case LYS_LIST: |
| 921 | case LYS_GROUPING: |
| 922 | case LYS_USES: |
| 923 | case LYS_INPUT: |
| 924 | case LYS_OUTPUT: |
| 925 | case LYS_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 926 | if (!(child->nodetype & |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 927 | (LYS_ANYXML | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF | |
| 928 | LYS_LEAFLIST | LYS_LIST | LYS_USES))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 929 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 930 | return EXIT_FAILURE; |
| 931 | } |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 932 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 933 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 934 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 935 | if (!(child->nodetype & |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 936 | (LYS_ANYXML | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 937 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 938 | return EXIT_FAILURE; |
| 939 | } |
| 940 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 941 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 942 | if (!(child->nodetype & |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 943 | (LYS_ANYXML | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_USES))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 944 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 945 | return EXIT_FAILURE; |
| 946 | } |
| 947 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 948 | case LYS_RPC: |
| 949 | if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 950 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc"); |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 951 | return EXIT_FAILURE; |
| 952 | } |
| 953 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 954 | case LYS_LEAF: |
| 955 | case LYS_LEAFLIST: |
| 956 | case LYS_ANYXML: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 957 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
| 958 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, NULL, "The \"%s\" statement cannot have any data substatement.", |
Michal Vasko | 6ea3e36 | 2016-03-11 10:25:36 +0100 | [diff] [blame] | 959 | strnodetype(parent->nodetype)); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 960 | return EXIT_FAILURE; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 961 | case LYS_AUGMENT: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 962 | if (!(child->nodetype & |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 963 | (LYS_ANYXML | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF |
| 964 | | LYS_LEAFLIST | LYS_LIST | LYS_USES))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 965 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 966 | return EXIT_FAILURE; |
| 967 | } |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 968 | break; |
| 969 | case LYS_UNKNOWN: |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 970 | /* top level */ |
| 971 | if (!(child->nodetype & |
| 972 | (LYS_ANYXML | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING |
| 973 | | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 974 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "(sub)module"); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 975 | return EXIT_FAILURE; |
| 976 | } |
| 977 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 978 | break; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | /* check identifier uniqueness */ |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 982 | if (lys_check_id(child, parent, module)) { |
| 983 | return EXIT_FAILURE; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 984 | } |
Radek Krejci | b7155b5 | 2015-06-10 17:03:01 +0200 | [diff] [blame] | 985 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 986 | if (child->parent) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 987 | lys_node_unlink(child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 988 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 989 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 990 | if (!parent) { |
Radek Krejci | 9272055 | 2015-10-05 15:28:27 +0200 | [diff] [blame] | 991 | if (module->data) { |
| 992 | module->data->prev->next = child; |
| 993 | child->prev = module->data->prev; |
| 994 | module->data->prev = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 995 | } else { |
Radek Krejci | 9272055 | 2015-10-05 15:28:27 +0200 | [diff] [blame] | 996 | module->data = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 997 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 998 | } else { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 999 | if (!parent->child) { |
| 1000 | /* the only/first child of the parent */ |
| 1001 | parent->child = child; |
| 1002 | child->parent = parent; |
| 1003 | iter = child; |
| 1004 | } else { |
| 1005 | /* add a new child at the end of parent's child list */ |
| 1006 | iter = parent->child->prev; |
| 1007 | iter->next = child; |
| 1008 | child->prev = iter; |
| 1009 | } |
| 1010 | while (iter->next) { |
| 1011 | iter = iter->next; |
| 1012 | iter->parent = parent; |
| 1013 | } |
| 1014 | parent->child->prev = iter; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1015 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1016 | |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 1017 | /* propagate information about status data presence */ |
| 1018 | if ((child->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML)) && |
| 1019 | (child->flags & LYS_INCL_STATUS)) { |
| 1020 | for(iter = parent; iter; iter = iter->parent) { |
| 1021 | /* store it only into container or list - the only data inner nodes */ |
| 1022 | if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
| 1023 | if (iter->flags & LYS_INCL_STATUS) { |
| 1024 | /* done, someone else set it already from here */ |
| 1025 | break; |
| 1026 | } |
| 1027 | /* set flag about including status data */ |
| 1028 | iter->flags |= LYS_INCL_STATUS; |
| 1029 | } |
| 1030 | } |
| 1031 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1032 | return EXIT_SUCCESS; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1033 | } |
| 1034 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1035 | static const struct lys_module * |
| 1036 | lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int internal) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1037 | { |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1038 | char *enlarged_data = NULL; |
Radek Krejci | 0b5805d | 2015-08-13 09:38:02 +0200 | [diff] [blame] | 1039 | struct lys_module *mod = NULL; |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1040 | unsigned int len; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1041 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1042 | if (!ctx || !data) { |
| 1043 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1044 | return NULL; |
| 1045 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1046 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1047 | if (!internal && format == LYS_IN_YANG) { |
| 1048 | /* enlarge data by 2 bytes for flex */ |
| 1049 | len = strlen(data); |
| 1050 | enlarged_data = malloc((len + 2) * sizeof *enlarged_data); |
| 1051 | if (!enlarged_data) { |
| 1052 | LOGMEM; |
| 1053 | return NULL; |
| 1054 | } |
| 1055 | memcpy(enlarged_data, data, len); |
| 1056 | enlarged_data[len] = enlarged_data[len + 1] = '\0'; |
| 1057 | data = enlarged_data; |
| 1058 | } |
| 1059 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1060 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1061 | case LYS_IN_YIN: |
Radek Krejci | ff4874d | 2016-03-07 12:30:50 +0100 | [diff] [blame] | 1062 | mod = yin_read_module(ctx, data, NULL, 1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1063 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1064 | case LYS_IN_YANG: |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1065 | mod = yang_read_module(ctx, data, 0, NULL, 1); |
| 1066 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1067 | default: |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1068 | LOGERR(LY_EINVAL, "Invalid schema input format."); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1069 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1070 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1071 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1072 | free(enlarged_data); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1073 | return mod; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1074 | } |
| 1075 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1076 | API const struct lys_module * |
| 1077 | lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format) |
| 1078 | { |
| 1079 | return lys_parse_mem_(ctx, data, format, 0); |
| 1080 | } |
| 1081 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1082 | struct lys_submodule * |
| 1083 | lys_submodule_parse(struct lys_module *module, const char *data, LYS_INFORMAT format, struct unres_schema *unres) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1084 | { |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1085 | struct lys_submodule *submod = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1086 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1087 | assert(module); |
| 1088 | assert(data); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1089 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1090 | /* get the main module */ |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 1091 | module = lys_main_module(module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1092 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1093 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1094 | case LYS_IN_YIN: |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1095 | submod = yin_read_submodule(module, data, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1096 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1097 | case LYS_IN_YANG: |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1098 | submod = yang_read_submodule(module, data, 0, unres); |
| 1099 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1100 | default: |
Radek Krejci | 90a550a | 2016-04-13 16:00:58 +0200 | [diff] [blame] | 1101 | assert(0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1102 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1103 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1104 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1105 | return submod; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1106 | } |
| 1107 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1108 | API const struct lys_module * |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1109 | lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format) |
| 1110 | { |
| 1111 | int fd; |
| 1112 | const struct lys_module *ret; |
| 1113 | |
| 1114 | if (!ctx || !path) { |
| 1115 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1116 | return NULL; |
| 1117 | } |
| 1118 | |
| 1119 | fd = open(path, O_RDONLY); |
| 1120 | if (fd == -1) { |
| 1121 | LOGERR(LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)); |
| 1122 | return NULL; |
| 1123 | } |
| 1124 | |
| 1125 | ret = lys_parse_fd(ctx, fd, format); |
| 1126 | close(fd); |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 1127 | |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1128 | if (ret && !ret->filepath) { |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 1129 | /* store URI */ |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1130 | ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0); |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 1131 | } |
| 1132 | |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1133 | return ret; |
| 1134 | } |
| 1135 | |
| 1136 | API const struct lys_module * |
| 1137 | lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format) |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1138 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1139 | const struct lys_module *module; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1140 | struct stat sb; |
| 1141 | char *addr; |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1142 | char buf[PATH_MAX]; |
| 1143 | int len; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1144 | |
| 1145 | if (!ctx || fd < 0) { |
| 1146 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1147 | return NULL; |
| 1148 | } |
| 1149 | |
Radek Krejci | 10a833c | 2015-12-16 15:28:37 +0100 | [diff] [blame] | 1150 | if (fstat(fd, &sb) == -1) { |
| 1151 | LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno)); |
| 1152 | return NULL; |
| 1153 | } |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1154 | if (!S_ISREG(sb.st_mode)) { |
| 1155 | LOGERR(LY_EINVAL, "Invalid parameter, input file is not a regular file"); |
| 1156 | return NULL; |
| 1157 | } |
| 1158 | |
Michal Vasko | 164d901 | 2016-04-01 10:16:59 +0200 | [diff] [blame] | 1159 | if (!sb.st_size) { |
| 1160 | LOGERR(LY_EINVAL, "File empty."); |
| 1161 | return NULL; |
| 1162 | } |
| 1163 | |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1164 | addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1165 | if (addr == MAP_FAILED) { |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1166 | LOGERR(LY_EMEM, "Map file into memory failed (%s()).",__func__); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1167 | return NULL; |
| 1168 | } |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1169 | module = lys_parse_mem_(ctx, addr, format, 1); |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1170 | munmap(addr, sb.st_size + 2); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1171 | |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1172 | if (module && !module->filepath) { |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1173 | /* get URI if there is /proc */ |
| 1174 | addr = NULL; |
Radek Krejci | 15412ca | 2016-03-03 11:16:52 +0100 | [diff] [blame] | 1175 | if (asprintf(&addr, "/proc/self/fd/%d", fd) != -1) { |
| 1176 | if ((len = readlink(addr, buf, PATH_MAX - 1)) > 0) { |
| 1177 | ((struct lys_module *)module)->filepath = lydict_insert(ctx, buf, len); |
| 1178 | } |
| 1179 | free(addr); |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1180 | } |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1181 | } |
| 1182 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1183 | return module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1184 | } |
| 1185 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1186 | struct lys_submodule * |
| 1187 | lys_submodule_read(struct lys_module *module, int fd, LYS_INFORMAT format, struct unres_schema *unres) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1188 | { |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1189 | struct lys_submodule *submodule; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1190 | struct stat sb; |
| 1191 | char *addr; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1192 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1193 | assert(module); |
| 1194 | assert(fd >= 0); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1195 | |
Radek Krejci | 10a833c | 2015-12-16 15:28:37 +0100 | [diff] [blame] | 1196 | if (fstat(fd, &sb) == -1) { |
| 1197 | LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno)); |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1198 | return NULL; |
Radek Krejci | 10a833c | 2015-12-16 15:28:37 +0100 | [diff] [blame] | 1199 | } |
Michal Vasko | 164d901 | 2016-04-01 10:16:59 +0200 | [diff] [blame] | 1200 | |
| 1201 | if (!sb.st_size) { |
| 1202 | LOGERR(LY_EINVAL, "File empty."); |
| 1203 | return NULL; |
| 1204 | } |
| 1205 | |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1206 | addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1207 | if (addr == MAP_FAILED) { |
| 1208 | LOGERR(LY_EMEM,"Map file into memory failed (%s()).",__func__); |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1209 | return NULL; |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1210 | } |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1211 | submodule = lys_submodule_parse(module, addr, format, unres); |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1212 | munmap(addr, sb.st_size + 2); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1213 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1214 | return submodule; |
| 1215 | |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1216 | } |
| 1217 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1218 | static struct lys_restr * |
| 1219 | lys_restr_dup(struct ly_ctx *ctx, struct lys_restr *old, int size) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1220 | { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1221 | struct lys_restr *result; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1222 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1223 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1224 | if (!size) { |
| 1225 | return NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1226 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1227 | |
| 1228 | result = calloc(size, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1229 | if (!result) { |
Radek Krejci | aa2a893 | 2016-02-17 15:03:14 +0100 | [diff] [blame] | 1230 | LOGMEM; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1231 | return NULL; |
| 1232 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1233 | for (i = 0; i < size; i++) { |
| 1234 | result[i].expr = lydict_insert(ctx, old[i].expr, 0); |
| 1235 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 1236 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 1237 | result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0); |
| 1238 | result[i].emsg = lydict_insert(ctx, old[i].emsg, 0); |
| 1239 | } |
| 1240 | |
| 1241 | return result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1242 | } |
| 1243 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1244 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1245 | lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr) |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1246 | { |
| 1247 | assert(ctx); |
| 1248 | if (!restr) { |
| 1249 | return; |
| 1250 | } |
| 1251 | |
| 1252 | lydict_remove(ctx, restr->expr); |
| 1253 | lydict_remove(ctx, restr->dsc); |
| 1254 | lydict_remove(ctx, restr->ref); |
| 1255 | lydict_remove(ctx, restr->eapptag); |
| 1256 | lydict_remove(ctx, restr->emsg); |
| 1257 | } |
| 1258 | |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1259 | static int |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1260 | type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old, |
| 1261 | LY_DATA_TYPE base, struct unres_schema *unres) |
| 1262 | { |
| 1263 | int i; |
| 1264 | |
| 1265 | switch (base) { |
| 1266 | case LY_TYPE_BINARY: |
| 1267 | if (old->info.binary.length) { |
| 1268 | new->info.binary.length = lys_restr_dup(mod->ctx, old->info.binary.length, 1); |
| 1269 | } |
| 1270 | break; |
| 1271 | |
| 1272 | case LY_TYPE_BITS: |
| 1273 | new->info.bits.count = old->info.bits.count; |
| 1274 | if (new->info.bits.count) { |
| 1275 | new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit); |
| 1276 | if (!new->info.bits.bit) { |
| 1277 | LOGMEM; |
| 1278 | return -1; |
| 1279 | } |
| 1280 | for (i = 0; i < new->info.bits.count; i++) { |
| 1281 | new->info.bits.bit[i].name = lydict_insert(mod->ctx, old->info.bits.bit[i].name, 0); |
| 1282 | new->info.bits.bit[i].dsc = lydict_insert(mod->ctx, old->info.bits.bit[i].dsc, 0); |
| 1283 | new->info.bits.bit[i].ref = lydict_insert(mod->ctx, old->info.bits.bit[i].ref, 0); |
| 1284 | new->info.bits.bit[i].flags = old->info.bits.bit[i].flags; |
| 1285 | new->info.bits.bit[i].pos = old->info.bits.bit[i].pos; |
| 1286 | } |
| 1287 | } |
| 1288 | break; |
| 1289 | |
| 1290 | case LY_TYPE_DEC64: |
| 1291 | new->info.dec64.dig = old->info.dec64.dig; |
| 1292 | if (old->info.dec64.range) { |
| 1293 | new->info.dec64.range = lys_restr_dup(mod->ctx, old->info.dec64.range, 1); |
| 1294 | } |
| 1295 | break; |
| 1296 | |
| 1297 | case LY_TYPE_ENUM: |
| 1298 | new->info.enums.count = old->info.enums.count; |
| 1299 | if (new->info.enums.count) { |
| 1300 | new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm); |
| 1301 | if (!new->info.enums.enm) { |
| 1302 | LOGMEM; |
| 1303 | return -1; |
| 1304 | } |
| 1305 | for (i = 0; i < new->info.enums.count; i++) { |
| 1306 | new->info.enums.enm[i].name = lydict_insert(mod->ctx, old->info.enums.enm[i].name, 0); |
| 1307 | new->info.enums.enm[i].dsc = lydict_insert(mod->ctx, old->info.enums.enm[i].dsc, 0); |
| 1308 | new->info.enums.enm[i].ref = lydict_insert(mod->ctx, old->info.enums.enm[i].ref, 0); |
| 1309 | new->info.enums.enm[i].flags = old->info.enums.enm[i].flags; |
| 1310 | new->info.enums.enm[i].value = old->info.enums.enm[i].value; |
| 1311 | } |
| 1312 | } |
| 1313 | break; |
| 1314 | |
| 1315 | case LY_TYPE_IDENT: |
| 1316 | if (old->info.ident.ref) { |
| 1317 | new->info.ident.ref = old->info.ident.ref; |
| 1318 | } else { |
| 1319 | i = unres_schema_find(unres, old, UNRES_TYPE_IDENTREF); |
| 1320 | if (i > -1 && unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i])) { |
| 1321 | return -1; |
| 1322 | } |
| 1323 | } |
| 1324 | break; |
| 1325 | |
| 1326 | case LY_TYPE_INST: |
| 1327 | new->info.inst.req = old->info.inst.req; |
| 1328 | break; |
| 1329 | |
| 1330 | case LY_TYPE_INT8: |
| 1331 | case LY_TYPE_INT16: |
| 1332 | case LY_TYPE_INT32: |
| 1333 | case LY_TYPE_INT64: |
| 1334 | case LY_TYPE_UINT8: |
| 1335 | case LY_TYPE_UINT16: |
| 1336 | case LY_TYPE_UINT32: |
| 1337 | case LY_TYPE_UINT64: |
| 1338 | if (old->info.num.range) { |
| 1339 | new->info.num.range = lys_restr_dup(mod->ctx, old->info.num.range, 1); |
| 1340 | } |
| 1341 | break; |
| 1342 | |
| 1343 | case LY_TYPE_LEAFREF: |
| 1344 | if (old->info.lref.path) { |
| 1345 | new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0); |
| 1346 | if (unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent)) { |
| 1347 | return -1; |
| 1348 | } |
| 1349 | } |
| 1350 | break; |
| 1351 | |
| 1352 | case LY_TYPE_STRING: |
| 1353 | if (old->info.str.length) { |
| 1354 | new->info.str.length = lys_restr_dup(mod->ctx, old->info.str.length, 1); |
| 1355 | } |
| 1356 | new->info.str.patterns = lys_restr_dup(mod->ctx, old->info.str.patterns, old->info.str.pat_count); |
| 1357 | new->info.str.pat_count = old->info.str.pat_count; |
| 1358 | break; |
| 1359 | |
| 1360 | case LY_TYPE_UNION: |
| 1361 | new->info.uni.count = old->info.uni.count; |
| 1362 | if (new->info.uni.count) { |
| 1363 | new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types); |
| 1364 | if (!new->info.uni.types) { |
| 1365 | LOGMEM; |
| 1366 | return -1; |
| 1367 | } |
| 1368 | for (i = 0; i < new->info.uni.count; i++) { |
| 1369 | if (lys_type_dup(mod, parent, &(new->info.uni.types[i]), &(old->info.uni.types[i]), unres)) { |
| 1370 | return -1; |
| 1371 | } |
| 1372 | } |
| 1373 | } |
| 1374 | break; |
| 1375 | |
| 1376 | default: |
| 1377 | /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */ |
| 1378 | break; |
| 1379 | } |
| 1380 | return EXIT_SUCCESS; |
| 1381 | } |
| 1382 | |
| 1383 | struct yang_type * |
| 1384 | lys_yang_type_dup(struct lys_module *module, struct lys_node *parent, struct yang_type *old, struct lys_type *type, struct unres_schema *unres) |
| 1385 | { |
| 1386 | struct yang_type *new; |
| 1387 | |
| 1388 | new = calloc(1, sizeof *new); |
| 1389 | if (!new) { |
| 1390 | LOGMEM; |
| 1391 | return NULL; |
| 1392 | } |
| 1393 | new->flags = old->flags; |
| 1394 | new->base = old->base; |
Pavol Vican | 6658629 | 2016-04-07 11:38:52 +0200 | [diff] [blame] | 1395 | new->name = lydict_insert(module->ctx, old->name, 0); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1396 | new->type = type; |
| 1397 | if (!new->name) { |
| 1398 | LOGMEM; |
| 1399 | goto error; |
| 1400 | } |
| 1401 | if (type_dup(module, parent, type, old->type, new->base, unres)) { |
| 1402 | new->type->base = new->base; |
| 1403 | lys_type_free(module->ctx, new->type); |
| 1404 | memset(&new->type->info, 0, sizeof new->type->info); |
| 1405 | goto error; |
| 1406 | } |
| 1407 | return new; |
| 1408 | |
| 1409 | error: |
| 1410 | free(new); |
| 1411 | return NULL; |
| 1412 | } |
| 1413 | |
| 1414 | static int |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1415 | lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old, |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1416 | struct unres_schema *unres) |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1417 | { |
| 1418 | int i; |
| 1419 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1420 | new->module_name = lydict_insert(mod->ctx, old->module_name, 0); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1421 | new->base = old->base; |
| 1422 | new->der = old->der; |
| 1423 | |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 1424 | i = unres_schema_find(unres, old, UNRES_TYPE_DER); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1425 | if (i != -1) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 1426 | /* HACK (serious one) for unres */ |
| 1427 | /* nothing else we can do but duplicate it immediately */ |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1428 | if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) { |
| 1429 | new->der = (struct lys_tpdf *)lys_yang_type_dup(mod, parent, (struct yang_type *)old->der, new, unres); |
| 1430 | } else { |
| 1431 | new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1); |
| 1432 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1433 | new->parent = (struct lys_tpdf *)parent; |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1434 | /* all these unres additions can fail even though they did not before */ |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1435 | if (!new->der || unres_schema_add_node(mod, unres, new, UNRES_TYPE_DER, parent)) { |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1436 | return -1; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 1437 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1438 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1439 | } |
| 1440 | |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1441 | return type_dup(mod, parent, new, old, new->base, unres); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1445 | lys_type_free(struct ly_ctx *ctx, struct lys_type *type) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1446 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1447 | int i; |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1448 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1449 | assert(ctx); |
| 1450 | if (!type) { |
| 1451 | return; |
| 1452 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1453 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1454 | lydict_remove(ctx, type->module_name); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1455 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1456 | switch (type->base) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1457 | case LY_TYPE_BINARY: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1458 | lys_restr_free(ctx, type->info.binary.length); |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1459 | free(type->info.binary.length); |
| 1460 | break; |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 1461 | case LY_TYPE_BITS: |
| 1462 | for (i = 0; i < type->info.bits.count; i++) { |
| 1463 | lydict_remove(ctx, type->info.bits.bit[i].name); |
| 1464 | lydict_remove(ctx, type->info.bits.bit[i].dsc); |
| 1465 | lydict_remove(ctx, type->info.bits.bit[i].ref); |
| 1466 | } |
| 1467 | free(type->info.bits.bit); |
| 1468 | break; |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1469 | |
| 1470 | case LY_TYPE_DEC64: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1471 | lys_restr_free(ctx, type->info.dec64.range); |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1472 | free(type->info.dec64.range); |
| 1473 | break; |
| 1474 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1475 | case LY_TYPE_ENUM: |
| 1476 | for (i = 0; i < type->info.enums.count; i++) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1477 | lydict_remove(ctx, type->info.enums.enm[i].name); |
| 1478 | lydict_remove(ctx, type->info.enums.enm[i].dsc); |
| 1479 | lydict_remove(ctx, type->info.enums.enm[i].ref); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1480 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1481 | free(type->info.enums.enm); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1482 | break; |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1483 | |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 1484 | case LY_TYPE_INT8: |
| 1485 | case LY_TYPE_INT16: |
| 1486 | case LY_TYPE_INT32: |
| 1487 | case LY_TYPE_INT64: |
| 1488 | case LY_TYPE_UINT8: |
| 1489 | case LY_TYPE_UINT16: |
| 1490 | case LY_TYPE_UINT32: |
| 1491 | case LY_TYPE_UINT64: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1492 | lys_restr_free(ctx, type->info.num.range); |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 1493 | free(type->info.num.range); |
| 1494 | break; |
| 1495 | |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1496 | case LY_TYPE_LEAFREF: |
| 1497 | lydict_remove(ctx, type->info.lref.path); |
| 1498 | break; |
| 1499 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1500 | case LY_TYPE_STRING: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1501 | lys_restr_free(ctx, type->info.str.length); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1502 | free(type->info.str.length); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1503 | for (i = 0; i < type->info.str.pat_count; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1504 | lys_restr_free(ctx, &type->info.str.patterns[i]); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1505 | } |
| 1506 | free(type->info.str.patterns); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1507 | break; |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1508 | |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1509 | case LY_TYPE_UNION: |
| 1510 | for (i = 0; i < type->info.uni.count; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1511 | lys_type_free(ctx, &type->info.uni.types[i]); |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1512 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1513 | free(type->info.uni.types); |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1514 | break; |
| 1515 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1516 | default: |
Radek Krejci | c7c8553 | 2015-07-02 10:16:54 +0200 | [diff] [blame] | 1517 | /* 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] | 1518 | break; |
| 1519 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1520 | } |
| 1521 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1522 | static void |
| 1523 | lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1524 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1525 | assert(ctx); |
| 1526 | if (!tpdf) { |
| 1527 | return; |
| 1528 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1529 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1530 | lydict_remove(ctx, tpdf->name); |
| 1531 | lydict_remove(ctx, tpdf->dsc); |
| 1532 | lydict_remove(ctx, tpdf->ref); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1533 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1534 | lys_type_free(ctx, &tpdf->type); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1535 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1536 | lydict_remove(ctx, tpdf->units); |
| 1537 | lydict_remove(ctx, tpdf->dflt); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1538 | } |
| 1539 | |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1540 | static struct lys_tpdf * |
| 1541 | lys_tpdf_dup(struct lys_module *mod, struct lys_node *parent, struct lys_tpdf *old, int size, struct unres_schema *unres) |
| 1542 | { |
| 1543 | struct lys_tpdf *result; |
| 1544 | int i, j; |
| 1545 | |
| 1546 | if (!size) { |
| 1547 | return NULL; |
| 1548 | } |
| 1549 | |
| 1550 | result = calloc(size, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1551 | if (!result) { |
| 1552 | LOGMEM; |
| 1553 | return NULL; |
| 1554 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1555 | for (i = 0; i < size; i++) { |
| 1556 | result[i].name = lydict_insert(mod->ctx, old[i].name, 0); |
| 1557 | result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0); |
| 1558 | result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0); |
| 1559 | result[i].flags = old[i].flags; |
| 1560 | result[i].module = old[i].module; |
| 1561 | |
| 1562 | if (lys_type_dup(mod, parent, &(result[i].type), &(old[i].type), unres)) { |
| 1563 | for (j = 0; j <= i; ++j) { |
| 1564 | lys_tpdf_free(mod->ctx, &result[j]); |
| 1565 | } |
| 1566 | free(result); |
| 1567 | return NULL; |
| 1568 | } |
| 1569 | |
| 1570 | result[i].dflt = lydict_insert(mod->ctx, old[i].dflt, 0); |
| 1571 | result[i].units = lydict_insert(mod->ctx, old[i].units, 0); |
| 1572 | } |
| 1573 | |
| 1574 | return result; |
| 1575 | } |
| 1576 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1577 | static struct lys_when * |
| 1578 | lys_when_dup(struct ly_ctx *ctx, struct lys_when *old) |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1579 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1580 | struct lys_when *new; |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1581 | |
| 1582 | if (!old) { |
| 1583 | return NULL; |
| 1584 | } |
| 1585 | |
| 1586 | new = calloc(1, sizeof *new); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1587 | if (!new) { |
| 1588 | LOGMEM; |
| 1589 | return NULL; |
| 1590 | } |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1591 | new->cond = lydict_insert(ctx, old->cond, 0); |
| 1592 | new->dsc = lydict_insert(ctx, old->dsc, 0); |
| 1593 | new->ref = lydict_insert(ctx, old->ref, 0); |
| 1594 | |
| 1595 | return new; |
| 1596 | } |
| 1597 | |
Michal Vasko | 0308dd6 | 2015-10-07 09:14:40 +0200 | [diff] [blame] | 1598 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1599 | lys_when_free(struct ly_ctx *ctx, struct lys_when *w) |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1600 | { |
| 1601 | if (!w) { |
| 1602 | return; |
| 1603 | } |
| 1604 | |
| 1605 | lydict_remove(ctx, w->cond); |
| 1606 | lydict_remove(ctx, w->dsc); |
| 1607 | lydict_remove(ctx, w->ref); |
| 1608 | |
| 1609 | free(w); |
| 1610 | } |
| 1611 | |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1612 | static void |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1613 | lys_augment_free(struct ly_ctx *ctx, struct lys_node_augment *aug, void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1614 | { |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 1615 | struct lys_node *next, *sub; |
| 1616 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1617 | /* children from a resolved augment are freed under the target node */ |
Michal Vasko | d2fbade | 2016-05-02 17:14:00 +0200 | [diff] [blame] | 1618 | if (!aug->target) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1619 | LY_TREE_FOR_SAFE(aug->child, next, sub) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1620 | lys_node_free(sub, private_destructor, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1621 | } |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 1622 | } |
| 1623 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1624 | lydict_remove(ctx, aug->target_name); |
| 1625 | lydict_remove(ctx, aug->dsc); |
| 1626 | lydict_remove(ctx, aug->ref); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1627 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1628 | free(aug->features); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1629 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1630 | lys_when_free(ctx, aug->when); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1631 | } |
| 1632 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1633 | static struct lys_node_augment * |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1634 | lys_augment_dup(struct lys_module *module, struct lys_node *parent, struct lys_node_augment *old, int size) |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1635 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1636 | struct lys_node_augment *new = NULL; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1637 | struct lys_node *old_child, *new_child; |
| 1638 | int i; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1639 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1640 | if (!size) { |
| 1641 | return NULL; |
| 1642 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1643 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1644 | new = calloc(size, sizeof *new); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1645 | if (!new) { |
| 1646 | LOGMEM; |
| 1647 | return NULL; |
| 1648 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1649 | for (i = 0; i < size; i++) { |
| 1650 | new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0); |
| 1651 | new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0); |
| 1652 | new[i].ref = lydict_insert(module->ctx, old[i].ref, 0); |
| 1653 | new[i].flags = old[i].flags; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1654 | new[i].module = old[i].module; |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 1655 | new[i].nodetype = old[i].nodetype; |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1656 | |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1657 | /* this must succeed, it was already resolved once */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1658 | if (resolve_augment_schema_nodeid(new[i].target_name, parent->child, NULL, |
| 1659 | (const struct lys_node **)&new[i].target)) { |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1660 | LOGINT; |
| 1661 | free(new); |
| 1662 | return NULL; |
| 1663 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1664 | new[i].parent = parent; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1665 | |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1666 | /* Correct the augment nodes. |
| 1667 | * This function can only be called from lys_node_dup() with uses |
| 1668 | * being the node duplicated, so we must have a case of grouping |
| 1669 | * with a uses with augments. The augmented nodes have already been |
| 1670 | * copied and everything is almost fine except their parent is wrong |
Michal Vasko | a5900c5 | 2015-09-24 13:17:11 +0200 | [diff] [blame] | 1671 | * (it was set to their actual data parent, not an augment), and |
| 1672 | * the new augment does not have child pointer to its augment nodes, |
| 1673 | * so we just correct it. |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1674 | */ |
| 1675 | LY_TREE_FOR(new[i].target->child, new_child) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 1676 | if (ly_strequal(new_child->name, old[i].child->name, 1)) { |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1677 | break; |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1678 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1679 | } |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1680 | assert(new_child); |
Michal Vasko | a5900c5 | 2015-09-24 13:17:11 +0200 | [diff] [blame] | 1681 | new[i].child = new_child; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1682 | LY_TREE_FOR(old[i].child, old_child) { |
| 1683 | /* all augment nodes were connected as siblings, there can be no more after this */ |
| 1684 | if (old_child->parent != (struct lys_node *)&old[i]) { |
| 1685 | break; |
| 1686 | } |
| 1687 | |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 1688 | assert(ly_strequal(old_child->name, new_child->name, 1)); |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1689 | |
| 1690 | new_child->parent = (struct lys_node *)&new[i]; |
| 1691 | new_child = new_child->next; |
| 1692 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1693 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1694 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1695 | return new; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1696 | } |
| 1697 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1698 | static struct lys_refine * |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 1699 | lys_refine_dup(struct lys_module *mod, struct lys_refine *old, int size) |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1700 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1701 | struct lys_refine *result; |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 1702 | int i; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1703 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1704 | if (!size) { |
| 1705 | return NULL; |
| 1706 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1707 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1708 | result = calloc(size, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1709 | if (!result) { |
| 1710 | LOGMEM; |
| 1711 | return NULL; |
| 1712 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1713 | for (i = 0; i < size; i++) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1714 | result[i].target_name = lydict_insert(mod->ctx, old[i].target_name, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1715 | result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0); |
| 1716 | result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1717 | result[i].flags = old[i].flags; |
| 1718 | result[i].target_type = old[i].target_type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1719 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1720 | result[i].must_size = old[i].must_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1721 | result[i].must = lys_restr_dup(mod->ctx, old[i].must, old[i].must_size); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1722 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1723 | if (result[i].target_type & (LYS_LEAF | LYS_CHOICE)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1724 | result[i].mod.dflt = lydict_insert(mod->ctx, old[i].mod.dflt, 0); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1725 | } else if (result[i].target_type == LYS_CONTAINER) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1726 | result[i].mod.presence = lydict_insert(mod->ctx, old[i].mod.presence, 0); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1727 | } else if (result[i].target_type & (LYS_LIST | LYS_LEAFLIST)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1728 | result[i].mod.list = old[i].mod.list; |
| 1729 | } |
| 1730 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1731 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1732 | return result; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1733 | } |
| 1734 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1735 | static void |
| 1736 | lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident) |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1737 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1738 | assert(ctx); |
| 1739 | if (!ident) { |
| 1740 | return; |
| 1741 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1742 | |
Radek Krejci | 1b61d0e | 2016-04-15 13:55:44 +0200 | [diff] [blame] | 1743 | free(ident->der); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1744 | lydict_remove(ctx, ident->name); |
| 1745 | lydict_remove(ctx, ident->dsc); |
| 1746 | lydict_remove(ctx, ident->ref); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1747 | |
| 1748 | } |
| 1749 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1750 | static void |
| 1751 | lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1752 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1753 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1754 | |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1755 | /* handle only specific parts for LYS_GROUPING */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1756 | for (i = 0; i < grp->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1757 | lys_tpdf_free(ctx, &grp->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1758 | } |
| 1759 | free(grp->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1760 | } |
| 1761 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1762 | static void |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1763 | lys_rpc_inout_free(struct ly_ctx *ctx, struct lys_node_rpc_inout *io) |
| 1764 | { |
| 1765 | int i; |
| 1766 | |
| 1767 | /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */ |
| 1768 | for (i = 0; i < io->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1769 | lys_tpdf_free(ctx, &io->tpdf[i]); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1770 | } |
| 1771 | free(io->tpdf); |
| 1772 | } |
| 1773 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1774 | static void |
| 1775 | lys_anyxml_free(struct ly_ctx *ctx, struct lys_node_anyxml *anyxml) |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1776 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1777 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1778 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1779 | for (i = 0; i < anyxml->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1780 | lys_restr_free(ctx, &anyxml->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1781 | } |
| 1782 | free(anyxml->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1783 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1784 | lys_when_free(ctx, anyxml->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1785 | } |
| 1786 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1787 | static void |
| 1788 | lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1789 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1790 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1791 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1792 | if (leaf->child) { |
| 1793 | /* leafref backlinks */ |
| 1794 | ly_set_free((struct ly_set *)leaf->child); |
| 1795 | } |
| 1796 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1797 | for (i = 0; i < leaf->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1798 | lys_restr_free(ctx, &leaf->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1799 | } |
| 1800 | free(leaf->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1801 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1802 | lys_when_free(ctx, leaf->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1803 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1804 | lys_type_free(ctx, &leaf->type); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1805 | lydict_remove(ctx, leaf->units); |
| 1806 | lydict_remove(ctx, leaf->dflt); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1807 | } |
| 1808 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1809 | static void |
| 1810 | lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1811 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1812 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1813 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1814 | if (llist->child) { |
| 1815 | /* leafref backlinks */ |
| 1816 | ly_set_free((struct ly_set *)llist->child); |
| 1817 | } |
| 1818 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1819 | for (i = 0; i < llist->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1820 | lys_restr_free(ctx, &llist->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1821 | } |
| 1822 | free(llist->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1823 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1824 | lys_when_free(ctx, llist->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1825 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1826 | lys_type_free(ctx, &llist->type); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1827 | lydict_remove(ctx, llist->units); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1828 | } |
| 1829 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1830 | static void |
| 1831 | lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1832 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1833 | int i, j; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1834 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1835 | /* handle only specific parts for LY_NODE_LIST */ |
| 1836 | for (i = 0; i < list->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1837 | lys_tpdf_free(ctx, &list->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1838 | } |
| 1839 | free(list->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1840 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1841 | for (i = 0; i < list->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1842 | lys_restr_free(ctx, &list->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1843 | } |
| 1844 | free(list->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1845 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1846 | lys_when_free(ctx, list->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1847 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1848 | for (i = 0; i < list->unique_size; i++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1849 | for (j = 0; j > list->unique[i].expr_size; j++) { |
| 1850 | lydict_remove(ctx, list->unique[i].expr[j]); |
| 1851 | } |
| 1852 | free(list->unique[i].expr); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1853 | } |
| 1854 | free(list->unique); |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1855 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1856 | free(list->keys); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1857 | } |
| 1858 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1859 | static void |
| 1860 | lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1861 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1862 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1863 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1864 | /* handle only specific parts for LY_NODE_CONTAINER */ |
| 1865 | lydict_remove(ctx, cont->presence); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1866 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1867 | for (i = 0; i < cont->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1868 | lys_tpdf_free(ctx, &cont->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1869 | } |
| 1870 | free(cont->tpdf); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1871 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1872 | for (i = 0; i < cont->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1873 | lys_restr_free(ctx, &cont->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1874 | } |
| 1875 | free(cont->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1876 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1877 | lys_when_free(ctx, cont->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1878 | } |
| 1879 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1880 | static void |
| 1881 | lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f) |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1882 | { |
| 1883 | lydict_remove(ctx, f->name); |
| 1884 | lydict_remove(ctx, f->dsc); |
| 1885 | lydict_remove(ctx, f->ref); |
| 1886 | free(f->features); |
| 1887 | } |
| 1888 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1889 | static void |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 1890 | lys_deviation_free(struct lys_module *module, struct lys_deviation *dev) |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1891 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1892 | int i, j, k; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 1893 | struct ly_ctx *ctx; |
| 1894 | struct lys_node *next, *elem; |
| 1895 | |
| 1896 | ctx = module->ctx; |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1897 | |
| 1898 | lydict_remove(ctx, dev->target_name); |
| 1899 | lydict_remove(ctx, dev->dsc); |
| 1900 | lydict_remove(ctx, dev->ref); |
| 1901 | |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 1902 | /* the module was freed, but we only need the context from orig_node, use ours */ |
| 1903 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 1904 | /* it's actually a node subtree, we need to update modules on all the nodes :-/ */ |
| 1905 | LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) { |
| 1906 | elem->module = module; |
| 1907 | |
| 1908 | LY_TREE_DFS_END(dev->orig_node, next, elem); |
| 1909 | } |
| 1910 | lys_node_free(dev->orig_node, NULL, 0); |
| 1911 | } else { |
| 1912 | /* it's just a shallow copy, freeing one node */ |
| 1913 | dev->orig_node->module = module; |
| 1914 | lys_node_free(dev->orig_node, NULL, 1); |
| 1915 | } |
| 1916 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1917 | for (i = 0; i < dev->deviate_size; i++) { |
| 1918 | lydict_remove(ctx, dev->deviate[i].dflt); |
| 1919 | lydict_remove(ctx, dev->deviate[i].units); |
| 1920 | |
| 1921 | if (dev->deviate[i].mod == LY_DEVIATE_DEL) { |
| 1922 | for (j = 0; j < dev->deviate[i].must_size; j++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1923 | lys_restr_free(ctx, &dev->deviate[i].must[j]); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1924 | } |
| 1925 | free(dev->deviate[i].must); |
| 1926 | |
| 1927 | for (j = 0; j < dev->deviate[i].unique_size; j++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1928 | for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) { |
| 1929 | lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]); |
| 1930 | } |
Michal Vasko | 0238ccf | 2016-03-07 15:02:18 +0100 | [diff] [blame] | 1931 | free(dev->deviate[i].unique[j].expr); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1932 | } |
| 1933 | free(dev->deviate[i].unique); |
| 1934 | } |
| 1935 | } |
| 1936 | free(dev->deviate); |
| 1937 | } |
| 1938 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1939 | static void |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1940 | lys_uses_free(struct ly_ctx *ctx, struct lys_node_uses *uses, void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1941 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1942 | int i, j; |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1943 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1944 | for (i = 0; i < uses->refine_size; i++) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1945 | lydict_remove(ctx, uses->refine[i].target_name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1946 | lydict_remove(ctx, uses->refine[i].dsc); |
| 1947 | lydict_remove(ctx, uses->refine[i].ref); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1948 | |
Michal Vasko | a275c0a | 2015-09-24 09:55:42 +0200 | [diff] [blame] | 1949 | for (j = 0; j < uses->refine[i].must_size; j++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1950 | lys_restr_free(ctx, &uses->refine[i].must[j]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1951 | } |
| 1952 | free(uses->refine[i].must); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1953 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1954 | if (uses->refine[i].target_type & (LYS_LEAF | LYS_CHOICE)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1955 | lydict_remove(ctx, uses->refine[i].mod.dflt); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1956 | } else if (uses->refine[i].target_type & LYS_CONTAINER) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1957 | lydict_remove(ctx, uses->refine[i].mod.presence); |
| 1958 | } |
| 1959 | } |
| 1960 | free(uses->refine); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1961 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1962 | for (i = 0; i < uses->augment_size; i++) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1963 | lys_augment_free(ctx, &uses->augment[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1964 | } |
| 1965 | free(uses->augment); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1966 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1967 | lys_when_free(ctx, uses->when); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1968 | } |
| 1969 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1970 | void |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1971 | lys_node_free(struct lys_node *node, void (*private_destructor)(const struct lys_node *node, void *priv), int shallow) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1972 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1973 | struct ly_ctx *ctx; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1974 | struct lys_node *sub, *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1975 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1976 | if (!node) { |
| 1977 | return; |
| 1978 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1979 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1980 | assert(node->module); |
| 1981 | assert(node->module->ctx); |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1982 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1983 | ctx = node->module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1984 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1985 | /* remove private object */ |
Mislav Novakovic | b5529e5 | 2016-02-29 11:42:43 +0100 | [diff] [blame] | 1986 | if (node->priv && private_destructor) { |
| 1987 | private_destructor(node, node->priv); |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1988 | } |
| 1989 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1990 | /* common part */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1991 | lydict_remove(ctx, node->name); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1992 | if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 1993 | free(node->features); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1994 | lydict_remove(ctx, node->dsc); |
| 1995 | lydict_remove(ctx, node->ref); |
| 1996 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1997 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1998 | if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1999 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2000 | lys_node_free(sub, private_destructor, 0); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2001 | } |
| 2002 | } |
| 2003 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2004 | /* specific part */ |
| 2005 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2006 | case LYS_CONTAINER: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2007 | lys_container_free(ctx, (struct lys_node_container *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2008 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2009 | case LYS_CHOICE: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2010 | lys_when_free(ctx, ((struct lys_node_choice *)node)->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2011 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2012 | case LYS_LEAF: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2013 | lys_leaf_free(ctx, (struct lys_node_leaf *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2014 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2015 | case LYS_LEAFLIST: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2016 | lys_leaflist_free(ctx, (struct lys_node_leaflist *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2017 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2018 | case LYS_LIST: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2019 | lys_list_free(ctx, (struct lys_node_list *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2020 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2021 | case LYS_ANYXML: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2022 | lys_anyxml_free(ctx, (struct lys_node_anyxml *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2023 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2024 | case LYS_USES: |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2025 | lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2026 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2027 | case LYS_CASE: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2028 | lys_when_free(ctx, ((struct lys_node_case *)node)->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2029 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2030 | case LYS_AUGMENT: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2031 | /* do nothing */ |
| 2032 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2033 | case LYS_GROUPING: |
| 2034 | case LYS_RPC: |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2035 | case LYS_NOTIF: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2036 | lys_grp_free(ctx, (struct lys_node_grp *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2037 | break; |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2038 | |
| 2039 | case LYS_INPUT: |
| 2040 | case LYS_OUTPUT: |
| 2041 | lys_rpc_inout_free(ctx, (struct lys_node_rpc_inout *)node); |
| 2042 | break; |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 2043 | case LYS_UNKNOWN: |
| 2044 | LOGINT; |
| 2045 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2046 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2047 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2048 | /* again common part */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2049 | lys_node_unlink(node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2050 | free(node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2051 | } |
| 2052 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2053 | const struct lys_module * |
| 2054 | lys_get_import_module(const struct lys_module *module, const char *prefix, int pref_len, const char *name, int name_len) |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 2055 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2056 | const struct lys_module *main_module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2057 | int i; |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2058 | |
Michal Vasko | a7789a8 | 2016-02-11 15:42:55 +0100 | [diff] [blame] | 2059 | assert(!prefix || !name); |
| 2060 | |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2061 | if (prefix && !pref_len) { |
| 2062 | pref_len = strlen(prefix); |
| 2063 | } |
| 2064 | if (name && !name_len) { |
| 2065 | name_len = strlen(name); |
| 2066 | } |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 2067 | |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 2068 | main_module = lys_main_module(module); |
Michal Vasko | a7789a8 | 2016-02-11 15:42:55 +0100 | [diff] [blame] | 2069 | |
| 2070 | /* module own prefix, submodule own prefix, (sub)module own name */ |
| 2071 | if ((!prefix || (!module->type && !strncmp(main_module->prefix, prefix, pref_len) && !main_module->prefix[pref_len]) |
| 2072 | || (module->type && !strncmp(module->prefix, prefix, pref_len) && !module->prefix[pref_len])) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2073 | && (!name || (!strncmp(main_module->name, name, name_len) && !main_module->name[name_len]))) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2074 | return main_module; |
Michal Vasko | d8da86b | 2015-10-21 13:35:37 +0200 | [diff] [blame] | 2075 | } |
| 2076 | |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 2077 | for (i = 0; i < module->imp_size; ++i) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2078 | if ((!prefix || (!strncmp(module->imp[i].prefix, prefix, pref_len) && !module->imp[i].prefix[pref_len])) |
| 2079 | && (!name || (!strncmp(module->imp[i].module->name, name, name_len) && !module->imp[i].module->name[name_len]))) { |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 2080 | return module->imp[i].module; |
| 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | return NULL; |
| 2085 | } |
| 2086 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2087 | /* free_int_mods - flag whether to free the internal modules as well */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2088 | static void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2089 | module_free_common(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2090 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2091 | struct ly_ctx *ctx; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2092 | struct lys_node *next, *iter; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2093 | unsigned int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2094 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2095 | assert(module->ctx); |
| 2096 | ctx = module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2097 | |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2098 | /* just free the import array, imported modules will stay in the context */ |
Radek Krejci | 225376f | 2016-02-16 17:36:22 +0100 | [diff] [blame] | 2099 | for (i = 0; i < module->imp_size; i++) { |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 2100 | if (!module->imp[i].external) { |
| 2101 | lydict_remove(ctx, module->imp[i].prefix); |
| 2102 | } |
Radek Krejci | 225376f | 2016-02-16 17:36:22 +0100 | [diff] [blame] | 2103 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2104 | free(module->imp); |
| 2105 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2106 | /* submodules don't have data tree, the data nodes |
| 2107 | * are placed in the main module altogether */ |
| 2108 | if (!module->type) { |
| 2109 | LY_TREE_FOR_SAFE(module->data, next, iter) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2110 | lys_node_free(iter, private_destructor, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2111 | } |
Radek Krejci | 2118196 | 2015-06-30 14:11:00 +0200 | [diff] [blame] | 2112 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2113 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2114 | lydict_remove(ctx, module->dsc); |
| 2115 | lydict_remove(ctx, module->ref); |
| 2116 | lydict_remove(ctx, module->org); |
| 2117 | lydict_remove(ctx, module->contact); |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 2118 | lydict_remove(ctx, module->filepath); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2119 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2120 | /* revisions */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2121 | for (i = 0; i < module->rev_size; i++) { |
| 2122 | lydict_remove(ctx, module->rev[i].dsc); |
| 2123 | lydict_remove(ctx, module->rev[i].ref); |
| 2124 | } |
| 2125 | free(module->rev); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2126 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2127 | /* identities */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2128 | for (i = 0; i < module->ident_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2129 | lys_ident_free(ctx, &module->ident[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2130 | } |
| 2131 | module->ident_size = 0; |
| 2132 | free(module->ident); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2133 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2134 | /* typedefs */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2135 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2136 | lys_tpdf_free(ctx, &module->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2137 | } |
| 2138 | free(module->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2139 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2140 | /* include */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2141 | for (i = 0; i < module->inc_size; i++) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2142 | /* complete submodule free is done only from main module since |
| 2143 | * submodules propagate their includes to the main module */ |
| 2144 | if (!module->type) { |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2145 | lys_submodule_free(module->inc[i].submodule, private_destructor); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2146 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2147 | } |
| 2148 | free(module->inc); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2149 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2150 | /* augment */ |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2151 | for (i = 0; i < module->augment_size; i++) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2152 | lys_augment_free(ctx, &module->augment[i], private_destructor); |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2153 | } |
| 2154 | free(module->augment); |
| 2155 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2156 | /* features */ |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2157 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2158 | lys_feature_free(ctx, &module->features[i]); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2159 | } |
| 2160 | free(module->features); |
| 2161 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2162 | /* deviations */ |
| 2163 | for (i = 0; i < module->deviation_size; i++) { |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2164 | lys_deviation_free(module, &module->deviation[i]); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2165 | } |
| 2166 | free(module->deviation); |
| 2167 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2168 | lydict_remove(ctx, module->name); |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 2169 | lydict_remove(ctx, module->prefix); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2170 | } |
| 2171 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2172 | void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2173 | lys_submodule_free(struct lys_submodule *submodule, void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2174 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2175 | if (!submodule) { |
| 2176 | return; |
| 2177 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2178 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2179 | /* common part with struct ly_module */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2180 | module_free_common((struct lys_module *)submodule, private_destructor); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2181 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2182 | /* no specific items to free */ |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2183 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2184 | free(submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2185 | } |
| 2186 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2187 | struct lys_node * |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2188 | lys_node_dup(struct lys_module *module, struct lys_node *parent, const struct lys_node *node, uint8_t flags, |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2189 | uint8_t nacm, struct unres_schema *unres, int shallow) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2190 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2191 | struct lys_node *retval = NULL, *child; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2192 | struct ly_ctx *ctx = module->ctx; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2193 | int i, j, rc; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2194 | |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2195 | struct lys_node_container *cont = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2196 | struct lys_node_container *cont_orig = (struct lys_node_container *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2197 | struct lys_node_choice *choice = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2198 | struct lys_node_choice *choice_orig = (struct lys_node_choice *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2199 | struct lys_node_leaf *leaf = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2200 | struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2201 | struct lys_node_leaflist *llist = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2202 | struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2203 | struct lys_node_list *list = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2204 | struct lys_node_list *list_orig = (struct lys_node_list *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2205 | struct lys_node_anyxml *anyxml = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2206 | struct lys_node_anyxml *anyxml_orig = (struct lys_node_anyxml *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2207 | struct lys_node_uses *uses = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2208 | struct lys_node_uses *uses_orig = (struct lys_node_uses *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2209 | struct lys_node_grp *grp = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2210 | struct lys_node_grp *grp_orig = (struct lys_node_grp *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2211 | struct lys_node_rpc *rpc = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2212 | struct lys_node_rpc *rpc_orig = (struct lys_node_rpc *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2213 | struct lys_node_rpc_inout *io = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2214 | struct lys_node_rpc_inout *io_orig = (struct lys_node_rpc_inout *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2215 | struct lys_node_rpc *ntf = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2216 | struct lys_node_rpc *ntf_orig = (struct lys_node_rpc *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2217 | struct lys_node_case *cs = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2218 | struct lys_node_case *cs_orig = (struct lys_node_case *)node; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2219 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2220 | /* we cannot just duplicate memory since the strings are stored in |
| 2221 | * dictionary and we need to update dictionary counters. |
| 2222 | */ |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2223 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2224 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2225 | case LYS_CONTAINER: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2226 | cont = calloc(1, sizeof *cont); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2227 | retval = (struct lys_node *)cont; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2228 | break; |
| 2229 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2230 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2231 | choice = calloc(1, sizeof *choice); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2232 | retval = (struct lys_node *)choice; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2233 | break; |
| 2234 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2235 | case LYS_LEAF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2236 | leaf = calloc(1, sizeof *leaf); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2237 | retval = (struct lys_node *)leaf; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2238 | break; |
| 2239 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2240 | case LYS_LEAFLIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2241 | llist = calloc(1, sizeof *llist); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2242 | retval = (struct lys_node *)llist; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2243 | break; |
| 2244 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2245 | case LYS_LIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2246 | list = calloc(1, sizeof *list); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2247 | retval = (struct lys_node *)list; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2248 | break; |
| 2249 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2250 | case LYS_ANYXML: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2251 | anyxml = calloc(1, sizeof *anyxml); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2252 | retval = (struct lys_node *)anyxml; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2253 | break; |
| 2254 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2255 | case LYS_USES: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2256 | uses = calloc(1, sizeof *uses); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2257 | retval = (struct lys_node *)uses; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2258 | break; |
| 2259 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2260 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2261 | cs = calloc(1, sizeof *cs); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2262 | retval = (struct lys_node *)cs; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2263 | break; |
| 2264 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2265 | case LYS_GROUPING: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2266 | grp = calloc(1, sizeof *grp); |
| 2267 | retval = (struct lys_node *)grp; |
| 2268 | break; |
| 2269 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2270 | case LYS_RPC: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2271 | rpc = calloc(1, sizeof *rpc); |
| 2272 | retval = (struct lys_node *)rpc; |
| 2273 | break; |
| 2274 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2275 | case LYS_INPUT: |
| 2276 | case LYS_OUTPUT: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2277 | io = calloc(1, sizeof *io); |
| 2278 | retval = (struct lys_node *)io; |
| 2279 | break; |
| 2280 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2281 | case LYS_NOTIF: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2282 | ntf = calloc(1, sizeof *ntf); |
| 2283 | retval = (struct lys_node *)ntf; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2284 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2285 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2286 | default: |
Michal Vasko | d23ce59 | 2015-08-06 09:55:37 +0200 | [diff] [blame] | 2287 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2288 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2289 | } |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame] | 2290 | |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2291 | if (!retval) { |
| 2292 | LOGMEM; |
| 2293 | return NULL; |
| 2294 | } |
| 2295 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2296 | /* |
| 2297 | * duplicate generic part of the structure |
| 2298 | */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2299 | retval->name = lydict_insert(ctx, node->name, 0); |
| 2300 | retval->dsc = lydict_insert(ctx, node->dsc, 0); |
| 2301 | retval->ref = lydict_insert(ctx, node->ref, 0); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 2302 | retval->nacm = nacm; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2303 | retval->flags = node->flags; |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2304 | if (!(retval->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2305 | /* set parent's config flag */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2306 | retval->flags |= flags & LYS_CONFIG_MASK; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2307 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2308 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2309 | retval->module = module; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2310 | retval->nodetype = node->nodetype; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2311 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2312 | retval->prev = retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2313 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2314 | retval->features_size = node->features_size; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2315 | retval->features = calloc(retval->features_size, sizeof *retval->features); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2316 | if (!retval->features) { |
| 2317 | LOGMEM; |
| 2318 | goto error; |
| 2319 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2320 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2321 | if (!shallow) { |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2322 | for (i = 0; i < node->features_size; ++i) { |
| 2323 | retval->features[i] = (struct lys_feature *)retval; |
| 2324 | if (unres_schema_dup(module, unres, &node->features[i], UNRES_IFFEAT, &retval->features[i])) { |
| 2325 | retval->features[i] = node->features[i]; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2326 | } |
| 2327 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2328 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2329 | /* connect it to the parent */ |
| 2330 | if (lys_node_addchild(parent, retval->module, retval)) { |
| 2331 | goto error; |
| 2332 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2333 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2334 | /* go recursively */ |
| 2335 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2336 | LY_TREE_FOR(node->child, child) { |
| 2337 | if (!lys_node_dup(module, retval, child, retval->flags, retval->nacm, unres, 0)) { |
| 2338 | goto error; |
| 2339 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2340 | } |
| 2341 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2342 | } else { |
| 2343 | memcpy(retval->features, node->features, retval->features_size * sizeof *retval->features); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2344 | } |
| 2345 | |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2346 | /* |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2347 | * duplicate specific part of the structure |
| 2348 | */ |
| 2349 | switch (node->nodetype) { |
| 2350 | case LYS_CONTAINER: |
| 2351 | if (cont_orig->when) { |
| 2352 | cont->when = lys_when_dup(ctx, cont_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2353 | } |
| 2354 | cont->presence = lydict_insert(ctx, cont_orig->presence, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2355 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2356 | cont->must_size = cont_orig->must_size; |
| 2357 | cont->tpdf_size = cont_orig->tpdf_size; |
| 2358 | |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2359 | cont->must = lys_restr_dup(ctx, cont_orig->must, cont->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2360 | cont->tpdf = lys_tpdf_dup(module, node->parent, cont_orig->tpdf, cont->tpdf_size, unres); |
| 2361 | break; |
| 2362 | |
| 2363 | case LYS_CHOICE: |
| 2364 | if (choice_orig->when) { |
| 2365 | choice->when = lys_when_dup(ctx, choice_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2366 | } |
| 2367 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2368 | if (!shallow) { |
| 2369 | if (choice_orig->dflt) { |
| 2370 | rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0, LYS_ANYXML |
| 2371 | | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST |
| 2372 | | LYS_LIST, (const struct lys_node **)&choice->dflt); |
| 2373 | if (rc) { |
| 2374 | if (rc == EXIT_FAILURE) { |
| 2375 | LOGINT; |
| 2376 | } |
| 2377 | goto error; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2378 | } |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2379 | } else { |
| 2380 | /* useless to check return value, we don't know whether |
| 2381 | * there really wasn't any default defined or it just hasn't |
| 2382 | * been resolved, we just hope for the best :) |
| 2383 | */ |
| 2384 | unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2385 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2386 | } else { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2387 | choice->dflt = choice_orig->dflt; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2388 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2389 | break; |
| 2390 | |
| 2391 | case LYS_LEAF: |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 2392 | if (lys_type_dup(module, node->parent, &(leaf->type), &(leaf_orig->type), unres)) { |
| 2393 | goto error; |
| 2394 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2395 | leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0); |
| 2396 | |
| 2397 | if (leaf_orig->dflt) { |
| 2398 | leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0); |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2399 | if (unres_schema_add_str(module, unres, &leaf->type, UNRES_TYPE_DFLT, leaf->dflt) == -1) { |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2400 | goto error; |
| 2401 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | leaf->must_size = leaf_orig->must_size; |
| 2405 | leaf->must = lys_restr_dup(ctx, leaf_orig->must, leaf->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2406 | |
| 2407 | if (leaf_orig->when) { |
| 2408 | leaf->when = lys_when_dup(ctx, leaf_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2409 | } |
| 2410 | break; |
| 2411 | |
| 2412 | case LYS_LEAFLIST: |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 2413 | if (lys_type_dup(module, node->parent, &(llist->type), &(llist_orig->type), unres)) { |
| 2414 | goto error; |
| 2415 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2416 | llist->units = lydict_insert(module->ctx, llist_orig->units, 0); |
| 2417 | |
| 2418 | llist->min = llist_orig->min; |
| 2419 | llist->max = llist_orig->max; |
| 2420 | |
| 2421 | llist->must_size = llist_orig->must_size; |
| 2422 | llist->must = lys_restr_dup(ctx, llist_orig->must, llist->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2423 | |
| 2424 | if (llist_orig->when) { |
| 2425 | llist->when = lys_when_dup(ctx, llist_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2426 | } |
| 2427 | break; |
| 2428 | |
| 2429 | case LYS_LIST: |
| 2430 | list->min = list_orig->min; |
| 2431 | list->max = list_orig->max; |
| 2432 | |
| 2433 | list->must_size = list_orig->must_size; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2434 | list->must = lys_restr_dup(ctx, list_orig->must, list->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2435 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2436 | list->tpdf_size = list_orig->tpdf_size; |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 2437 | list->tpdf = lys_tpdf_dup(module, node->parent, list_orig->tpdf, list->tpdf_size, unres); |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2438 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2439 | list->keys_size = list_orig->keys_size; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2440 | if (list->keys_size) { |
| 2441 | list->keys = calloc(list->keys_size, sizeof *list->keys); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2442 | if (!list->keys) { |
| 2443 | LOGMEM; |
| 2444 | goto error; |
| 2445 | } |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 2446 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2447 | if (!shallow) { |
| 2448 | /* we managed to resolve it before, resolve it again manually */ |
| 2449 | if (list_orig->keys[0]) { |
| 2450 | for (i = 0; i < list->keys_size; ++i) { |
| 2451 | rc = lys_get_sibling(list->child, lys_node_module(retval)->name, 0, list_orig->keys[i]->name, 0, LYS_LEAF, |
| 2452 | (const struct lys_node **)&list->keys[i]); |
| 2453 | if (rc) { |
| 2454 | if (rc == EXIT_FAILURE) { |
| 2455 | LOGINT; |
| 2456 | } |
| 2457 | goto error; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2458 | } |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2459 | } |
| 2460 | /* it was not resolved yet, add unres copy */ |
| 2461 | } else { |
| 2462 | if (unres_schema_dup(module, unres, list_orig, UNRES_LIST_KEYS, list)) { |
| 2463 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2464 | goto error; |
| 2465 | } |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2466 | } |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 2467 | } else { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2468 | memcpy(list->keys, list_orig->keys, list->keys_size * sizeof *list->keys); |
Radek Krejci | a01e543 | 2015-06-16 10:35:25 +0200 | [diff] [blame] | 2469 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2470 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2471 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2472 | list->unique_size = list_orig->unique_size; |
| 2473 | list->unique = malloc(list->unique_size * sizeof *list->unique); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2474 | if (!list->unique) { |
| 2475 | LOGMEM; |
| 2476 | goto error; |
| 2477 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2478 | for (i = 0; i < list->unique_size; ++i) { |
| 2479 | list->unique[i].expr_size = list_orig->unique[i].expr_size; |
| 2480 | list->unique[i].expr = malloc(list->unique[i].expr_size * sizeof *list->unique[i].expr); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2481 | if (!list->unique[i].expr) { |
| 2482 | LOGMEM; |
| 2483 | goto error; |
| 2484 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2485 | for (j = 0; j < list->unique[i].expr_size; j++) { |
| 2486 | list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0); |
| 2487 | |
| 2488 | /* if it stays in unres list, duplicate it also there */ |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 2489 | unres_schema_dup(module, unres, &list_orig->unique[i], UNRES_LIST_UNIQ, &list->unique[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2490 | } |
| 2491 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2492 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2493 | if (list_orig->when) { |
| 2494 | list->when = lys_when_dup(ctx, list_orig->when); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2495 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2496 | break; |
| 2497 | |
| 2498 | case LYS_ANYXML: |
| 2499 | anyxml->must_size = anyxml_orig->must_size; |
| 2500 | anyxml->must = lys_restr_dup(ctx, anyxml_orig->must, anyxml->must_size); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2501 | |
| 2502 | if (anyxml_orig->when) { |
| 2503 | anyxml->when = lys_when_dup(ctx, anyxml_orig->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2504 | } |
| 2505 | break; |
| 2506 | |
| 2507 | case LYS_USES: |
| 2508 | uses->grp = uses_orig->grp; |
| 2509 | |
| 2510 | if (uses_orig->when) { |
| 2511 | uses->when = lys_when_dup(ctx, uses_orig->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | uses->refine_size = uses_orig->refine_size; |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 2515 | uses->refine = lys_refine_dup(module, uses_orig->refine, uses_orig->refine_size); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2516 | uses->augment_size = uses_orig->augment_size; |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2517 | if (!shallow) { |
| 2518 | uses->augment = lys_augment_dup(module, (struct lys_node *)uses, uses_orig->augment, uses_orig->augment_size); |
| 2519 | if (!uses->child) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2520 | if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL) == -1) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2521 | goto error; |
| 2522 | } |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2523 | } |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2524 | } else { |
| 2525 | memcpy(uses->augment, uses_orig->augment, uses->augment_size * sizeof *uses->augment); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2526 | } |
| 2527 | break; |
| 2528 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2529 | case LYS_CASE: |
| 2530 | if (cs_orig->when) { |
| 2531 | cs->when = lys_when_dup(ctx, cs_orig->when); |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2532 | } |
| 2533 | break; |
| 2534 | |
| 2535 | case LYS_GROUPING: |
| 2536 | grp->tpdf_size = grp_orig->tpdf_size; |
| 2537 | grp->tpdf = lys_tpdf_dup(module, node->parent, grp_orig->tpdf, grp->tpdf_size, unres); |
| 2538 | break; |
| 2539 | |
| 2540 | case LYS_RPC: |
| 2541 | rpc->tpdf_size = rpc_orig->tpdf_size; |
| 2542 | rpc->tpdf = lys_tpdf_dup(module, node->parent, rpc_orig->tpdf, rpc->tpdf_size, unres); |
| 2543 | break; |
| 2544 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2545 | case LYS_INPUT: |
| 2546 | case LYS_OUTPUT: |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2547 | io->tpdf_size = io_orig->tpdf_size; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2548 | io->tpdf = lys_tpdf_dup(module, node->parent, io_orig->tpdf, io->tpdf_size, unres); |
| 2549 | break; |
| 2550 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2551 | case LYS_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2552 | ntf->tpdf_size = ntf_orig->tpdf_size; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2553 | ntf->tpdf = lys_tpdf_dup(module, node->parent, ntf_orig->tpdf, ntf->tpdf_size, unres); |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2554 | break; |
| 2555 | |
| 2556 | default: |
| 2557 | /* LY_NODE_AUGMENT */ |
| 2558 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2559 | goto error; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2560 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2561 | |
| 2562 | return retval; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2563 | |
| 2564 | error: |
| 2565 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2566 | lys_node_free(retval, NULL, 0); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2567 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2568 | } |
| 2569 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2570 | void |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2571 | lys_node_switch(struct lys_node *dst, struct lys_node *src) |
| 2572 | { |
| 2573 | struct lys_node *child; |
| 2574 | |
| 2575 | assert((dst->module == src->module) && (dst->name == src->name) && (dst->nodetype == src->nodetype)); |
| 2576 | |
| 2577 | /* sibling next */ |
| 2578 | if (dst->prev != dst) { |
| 2579 | dst->prev->next = src; |
| 2580 | } |
| 2581 | |
| 2582 | /* sibling prev */ |
| 2583 | if (dst->next) { |
| 2584 | dst->next->prev = src; |
| 2585 | } |
| 2586 | |
| 2587 | /* parent child prev */ |
| 2588 | if (!dst->next && dst->parent) { |
| 2589 | dst->parent->child->prev = src; |
| 2590 | } |
| 2591 | |
| 2592 | /* next */ |
| 2593 | src->next = dst->next; |
| 2594 | dst->next = NULL; |
| 2595 | |
| 2596 | /* prev */ |
| 2597 | if (dst->prev != dst) { |
| 2598 | src->prev = dst->prev; |
| 2599 | } |
| 2600 | dst->prev = dst; |
| 2601 | |
| 2602 | /* parent child */ |
| 2603 | if (dst->parent && (dst->parent->child == dst)) { |
| 2604 | dst->parent->child = src; |
| 2605 | } |
| 2606 | |
| 2607 | /* parent */ |
| 2608 | src->parent = dst->parent; |
| 2609 | dst->parent = NULL; |
| 2610 | |
| 2611 | /* child parent */ |
| 2612 | LY_TREE_FOR(dst->child, child) { |
| 2613 | if (child->parent == dst) { |
| 2614 | child->parent = src; |
| 2615 | } |
| 2616 | } |
| 2617 | |
| 2618 | /* child */ |
| 2619 | src->child = dst->child; |
| 2620 | dst->child = NULL; |
| 2621 | } |
| 2622 | |
| 2623 | void |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2624 | lys_free(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv), int remove_from_ctx) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2625 | { |
| 2626 | struct ly_ctx *ctx; |
| 2627 | int i; |
| 2628 | |
| 2629 | if (!module) { |
| 2630 | return; |
| 2631 | } |
| 2632 | |
| 2633 | /* remove schema from the context */ |
| 2634 | ctx = module->ctx; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2635 | if (remove_from_ctx && ctx->models.used) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2636 | for (i = 0; i < ctx->models.used; i++) { |
| 2637 | if (ctx->models.list[i] == module) { |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2638 | /* move all the models to not change the order in the list */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2639 | ctx->models.used--; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2640 | memmove(&ctx->models.list[i], ctx->models.list[i + 1], (ctx->models.used - i) * sizeof *ctx->models.list); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2641 | ctx->models.list[ctx->models.used] = NULL; |
| 2642 | /* we are done */ |
| 2643 | break; |
| 2644 | } |
| 2645 | } |
| 2646 | } |
| 2647 | |
| 2648 | /* common part with struct ly_submodule */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2649 | module_free_common(module, private_destructor); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2650 | |
| 2651 | /* specific items to free */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2652 | lydict_remove(ctx, module->ns); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2653 | |
| 2654 | free(module); |
| 2655 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2656 | |
| 2657 | /* |
| 2658 | * op: 1 - enable, 0 - disable |
| 2659 | */ |
| 2660 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2661 | lys_features_change(const struct lys_module *module, const char *name, int op) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2662 | { |
| 2663 | int all = 0; |
| 2664 | int i, j, k; |
| 2665 | |
| 2666 | if (!module || !name || !strlen(name)) { |
| 2667 | return EXIT_FAILURE; |
| 2668 | } |
| 2669 | |
| 2670 | if (!strcmp(name, "*")) { |
| 2671 | /* enable all */ |
| 2672 | all = 1; |
| 2673 | } |
| 2674 | |
| 2675 | /* module itself */ |
| 2676 | for (i = 0; i < module->features_size; i++) { |
| 2677 | if (all || !strcmp(module->features[i].name, name)) { |
| 2678 | if (op) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2679 | module->features[i].flags |= LYS_FENABLED; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2680 | /* enable referenced features (recursion) */ |
| 2681 | for (k = 0; k < module->features[i].features_size; k++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2682 | lys_features_change(module->features[i].features[k]->module, |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2683 | module->features[i].features[k]->name, op); |
| 2684 | } |
| 2685 | } else { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2686 | module->features[i].flags &= ~LYS_FENABLED; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2687 | } |
| 2688 | if (!all) { |
| 2689 | return EXIT_SUCCESS; |
| 2690 | } |
| 2691 | } |
| 2692 | } |
| 2693 | |
| 2694 | /* submodules */ |
| 2695 | for (j = 0; j < module->inc_size; j++) { |
| 2696 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
| 2697 | if (all || !strcmp(module->inc[j].submodule->features[i].name, name)) { |
| 2698 | if (op) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2699 | module->inc[j].submodule->features[i].flags |= LYS_FENABLED; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2700 | } else { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2701 | module->inc[j].submodule->features[i].flags &= ~LYS_FENABLED; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2702 | } |
| 2703 | if (!all) { |
| 2704 | return EXIT_SUCCESS; |
| 2705 | } |
| 2706 | } |
| 2707 | } |
| 2708 | } |
| 2709 | |
| 2710 | if (all) { |
| 2711 | return EXIT_SUCCESS; |
| 2712 | } else { |
| 2713 | return EXIT_FAILURE; |
| 2714 | } |
| 2715 | } |
| 2716 | |
| 2717 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2718 | lys_features_enable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2719 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2720 | return lys_features_change(module, feature, 1); |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2721 | } |
| 2722 | |
| 2723 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2724 | lys_features_disable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2725 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2726 | return lys_features_change(module, feature, 0); |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2730 | lys_features_state(const struct lys_module *module, const char *feature) |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2731 | { |
| 2732 | int i, j; |
| 2733 | |
| 2734 | if (!module || !feature) { |
| 2735 | return -1; |
| 2736 | } |
| 2737 | |
| 2738 | /* search for the specified feature */ |
| 2739 | /* module itself */ |
| 2740 | for (i = 0; i < module->features_size; i++) { |
| 2741 | if (!strcmp(feature, module->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2742 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2743 | return 1; |
| 2744 | } else { |
| 2745 | return 0; |
| 2746 | } |
| 2747 | } |
| 2748 | } |
| 2749 | |
| 2750 | /* submodules */ |
| 2751 | for (j = 0; j < module->inc_size; j++) { |
| 2752 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
| 2753 | if (!strcmp(feature, module->inc[j].submodule->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2754 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2755 | return 1; |
| 2756 | } else { |
| 2757 | return 0; |
| 2758 | } |
| 2759 | } |
| 2760 | } |
| 2761 | } |
| 2762 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2763 | /* feature definition not found */ |
| 2764 | return -1; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2765 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2766 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2767 | API const char ** |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2768 | lys_features_list(const struct lys_module *module, uint8_t **states) |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2769 | { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2770 | const char **result = NULL; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2771 | int i, j; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2772 | unsigned int count; |
| 2773 | |
| 2774 | if (!module) { |
| 2775 | return NULL; |
| 2776 | } |
| 2777 | |
| 2778 | count = module->features_size; |
| 2779 | for (i = 0; i < module->inc_size; i++) { |
| 2780 | count += module->inc[i].submodule->features_size; |
| 2781 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2782 | result = malloc((count + 1) * sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2783 | if (!result) { |
| 2784 | LOGMEM; |
| 2785 | return NULL; |
| 2786 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2787 | if (states) { |
| 2788 | *states = malloc((count + 1) * sizeof **states); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2789 | if (!(*states)) { |
| 2790 | LOGMEM; |
| 2791 | free(result); |
| 2792 | return NULL; |
| 2793 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2794 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2795 | count = 0; |
| 2796 | |
| 2797 | /* module itself */ |
| 2798 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2799 | result[count] = module->features[i].name; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2800 | if (states) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2801 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2802 | (*states)[count] = 1; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2803 | } else { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2804 | (*states)[count] = 0; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2805 | } |
| 2806 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2807 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | /* submodules */ |
| 2811 | for (j = 0; j < module->inc_size; j++) { |
| 2812 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2813 | result[count] = module->inc[j].submodule->features[i].name; |
Radek Krejci | 374b94e | 2015-08-13 09:44:22 +0200 | [diff] [blame] | 2814 | if (states) { |
| 2815 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
| 2816 | (*states)[count] = 1; |
| 2817 | } else { |
| 2818 | (*states)[count] = 0; |
| 2819 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2820 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2821 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2822 | } |
| 2823 | } |
| 2824 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2825 | /* terminating NULL byte */ |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2826 | result[count] = NULL; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2827 | |
| 2828 | return result; |
| 2829 | } |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 2830 | |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 2831 | API struct lys_module * |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 2832 | lys_node_module(const struct lys_node *node) |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2833 | { |
| 2834 | return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module; |
| 2835 | } |
| 2836 | |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 2837 | API struct lys_module * |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 2838 | lys_main_module(const struct lys_module *module) |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 2839 | { |
| 2840 | return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module); |
| 2841 | } |
| 2842 | |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 2843 | API struct lys_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2844 | lys_parent(const struct lys_node *node) |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 2845 | { |
| 2846 | if (!node || !node->parent) { |
| 2847 | return NULL; |
| 2848 | } |
| 2849 | |
| 2850 | if (node->parent->nodetype == LYS_AUGMENT) { |
| 2851 | return ((struct lys_node_augment *)node->parent)->target; |
| 2852 | } |
| 2853 | |
| 2854 | return node->parent; |
| 2855 | } |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2856 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2857 | API void * |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2858 | lys_set_private(const struct lys_node *node, void *priv) |
| 2859 | { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2860 | void *prev; |
| 2861 | |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2862 | if (!node) { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2863 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 2864 | return NULL; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2865 | } |
| 2866 | |
Mislav Novakovic | b5529e5 | 2016-02-29 11:42:43 +0100 | [diff] [blame] | 2867 | prev = node->priv; |
| 2868 | ((struct lys_node *)node)->priv = priv; |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2869 | |
| 2870 | return prev; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2871 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2872 | |
| 2873 | static void |
| 2874 | lys_switch_deviation(struct lys_deviation *dev, struct lys_module *dev_module) |
| 2875 | { |
| 2876 | int ret; |
| 2877 | char *parent_path; |
| 2878 | struct lys_node *target; |
| 2879 | const struct lys_module *target_module; |
| 2880 | |
| 2881 | target_module = lys_get_import_module(dev_module, NULL, 0, dev->target_name + 1, |
| 2882 | strcspn(dev->target_name, ":") - 1); |
| 2883 | |
| 2884 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 2885 | if (dev->orig_node) { |
| 2886 | /* removing not-supported deviation ... */ |
| 2887 | if (strrchr(dev->target_name, '/') != dev->target_name) { |
| 2888 | /* ... from a parent */ |
| 2889 | parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name); |
| 2890 | |
| 2891 | target = NULL; |
| 2892 | ret = resolve_augment_schema_nodeid(parent_path, NULL, target_module, (const struct lys_node **)&target); |
| 2893 | free(parent_path); |
| 2894 | if (ret || !target) { |
| 2895 | LOGINT; |
| 2896 | return; |
| 2897 | } |
| 2898 | |
| 2899 | lys_node_addchild(target, NULL, dev->orig_node); |
| 2900 | } else { |
| 2901 | /* ... from top-level data */ |
| 2902 | lys_node_addchild(NULL, (struct lys_module *)target_module, dev->orig_node); |
| 2903 | } |
| 2904 | |
| 2905 | dev->orig_node = NULL; |
| 2906 | } else { |
| 2907 | /* adding not-supported deviation */ |
| 2908 | target = NULL; |
| 2909 | ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, (const struct lys_node **)&target); |
| 2910 | if (ret || !target) { |
| 2911 | LOGINT; |
| 2912 | return; |
| 2913 | } |
| 2914 | |
| 2915 | lys_node_unlink(target); |
| 2916 | dev->orig_node = target; |
| 2917 | } |
| 2918 | } else { |
| 2919 | target = NULL; |
| 2920 | ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, (const struct lys_node **)&target); |
| 2921 | if (ret || !target) { |
| 2922 | LOGINT; |
| 2923 | return; |
| 2924 | } |
| 2925 | |
| 2926 | lys_node_switch(target, dev->orig_node); |
| 2927 | dev->orig_node = target; |
| 2928 | } |
| 2929 | } |
| 2930 | |
| 2931 | void |
| 2932 | lys_deviation_add_ext_imports(struct lys_module *dev_target_module, struct lys_module *dev_module) |
| 2933 | { |
| 2934 | int i, j; |
| 2935 | |
| 2936 | /* mark the target module as deviated */ |
| 2937 | dev_target_module->deviated = 1; |
| 2938 | |
| 2939 | /* copy our imports to the deviated module (deviations may need them to work) */ |
| 2940 | for (i = 0; i < dev_module->imp_size; ++i) { |
| 2941 | for (j = 0; j < dev_target_module->imp_size; ++j) { |
| 2942 | if (dev_module->imp[i].module == dev_target_module->imp[j].module) { |
| 2943 | break; |
| 2944 | } |
| 2945 | } |
| 2946 | |
| 2947 | if (j < dev_target_module->imp_size) { |
| 2948 | /* import is already there */ |
| 2949 | continue; |
| 2950 | } |
| 2951 | |
| 2952 | /* copy the import, mark it as external */ |
| 2953 | ++dev_target_module->imp_size; |
| 2954 | dev_target_module->imp = ly_realloc(dev_target_module->imp, dev_target_module->imp_size * sizeof *dev_target_module->imp); |
| 2955 | if (!dev_target_module->imp) { |
| 2956 | LOGMEM; |
| 2957 | return; |
| 2958 | } |
| 2959 | dev_target_module->imp[dev_target_module->imp_size - 1].module = dev_module->imp[i].module; |
| 2960 | dev_target_module->imp[dev_target_module->imp_size - 1].prefix = lydict_insert(dev_module->ctx, dev_module->imp[i].prefix, 0); |
| 2961 | memcpy(dev_target_module->imp[dev_target_module->imp_size - 1].rev, dev_module->imp[i].rev, LY_REV_SIZE); |
| 2962 | dev_target_module->imp[dev_target_module->imp_size - 1].external = 1; |
| 2963 | } |
| 2964 | |
| 2965 | /* copy ourselves to the deviated module as a special import (if we haven't yet, there could be more deviations of the same module) */ |
| 2966 | for (i = 0; i < dev_target_module->imp_size; ++i) { |
| 2967 | if (dev_target_module->imp[i].module == dev_module) { |
| 2968 | break; |
| 2969 | } |
| 2970 | } |
| 2971 | |
| 2972 | if (i == dev_target_module->imp_size) { |
| 2973 | ++dev_target_module->imp_size; |
| 2974 | dev_target_module->imp = ly_realloc(dev_target_module->imp, dev_target_module->imp_size * sizeof *dev_target_module->imp); |
| 2975 | if (!dev_target_module->imp) { |
| 2976 | LOGMEM; |
| 2977 | return; |
| 2978 | } |
| 2979 | dev_target_module->imp[dev_target_module->imp_size - 1].module = dev_module; |
| 2980 | dev_target_module->imp[dev_target_module->imp_size - 1].prefix = lydict_insert(dev_module->ctx, dev_module->prefix, 0); |
| 2981 | if (dev_module->rev_size) { |
| 2982 | memcpy(dev_target_module->imp[dev_target_module->imp_size - 1].rev, dev_module->rev[0].date, LY_REV_SIZE); |
| 2983 | } else { |
| 2984 | memset(dev_target_module->imp[dev_target_module->imp_size - 1].rev, 0, LY_REV_SIZE); |
| 2985 | } |
| 2986 | dev_target_module->imp[dev_target_module->imp_size - 1].external = 2; |
| 2987 | } else { |
| 2988 | /* it could have been added by another deviating module that imported this deviating module */ |
| 2989 | dev_target_module->imp[i].external = 2; |
| 2990 | } |
| 2991 | } |
| 2992 | |
| 2993 | /* temporarily removes or applies deviations, updates module deviation flag accordingly */ |
| 2994 | void |
| 2995 | lys_switch_deviations(struct lys_module *module) |
| 2996 | { |
| 2997 | uint8_t i, j, changes = 0; |
| 2998 | |
| 2999 | for (i = 0; i < module->imp_size; ++i) { |
| 3000 | if (module->imp[i].external == 2) { |
| 3001 | for (j = 0; j < module->imp[i].module->deviation_size; ++j) { |
| 3002 | lys_switch_deviation(&module->imp[i].module->deviation[j], module->imp[i].module); |
| 3003 | } |
| 3004 | |
| 3005 | changes = 1; |
| 3006 | } |
| 3007 | } |
| 3008 | |
| 3009 | if (changes) { |
| 3010 | if (module->deviated) { |
| 3011 | module->deviated = 0; |
| 3012 | } else { |
| 3013 | module->deviated = 1; |
| 3014 | } |
| 3015 | } |
| 3016 | } |
| 3017 | |
| 3018 | /* not needed currently, but tested and working */ |
| 3019 | #if 0 |
| 3020 | |
| 3021 | void |
| 3022 | lys_sub_module_apply_devs_augs(struct lys_module *module) |
| 3023 | { |
| 3024 | int i; |
| 3025 | struct lys_node_augment *aug; |
| 3026 | struct lys_node *last; |
| 3027 | |
| 3028 | /* re-apply deviations */ |
| 3029 | for (i = 0; i < module->deviation_size; ++i) { |
| 3030 | lys_switch_deviation(&module->deviation[i], module); |
| 3031 | assert(module->deviation[i].orig_node); |
| 3032 | lys_deviation_add_ext_imports(lys_node_module(module->deviation[i].orig_node), module); |
| 3033 | } |
| 3034 | |
| 3035 | /* re-apply augments */ |
| 3036 | for (i = 0; i < module->augment_size; ++i) { |
| 3037 | aug = &module->augment[i]; |
| 3038 | assert(aug->target); |
| 3039 | |
| 3040 | /* reconnect augmenting data into the target - add them to the target child list */ |
| 3041 | if (aug->target->child) { |
| 3042 | last = aug->target->child->prev; |
| 3043 | last->next = aug->child; |
| 3044 | aug->target->child->prev = aug->child->prev; |
| 3045 | aug->child->prev = last; |
| 3046 | } else { |
| 3047 | aug->target->child = aug->child; |
| 3048 | } |
| 3049 | } |
| 3050 | } |
| 3051 | |
| 3052 | #endif |
| 3053 | |
| 3054 | void |
| 3055 | lys_sub_module_remove_devs_augs(struct lys_module *module) |
| 3056 | { |
| 3057 | int i, j, flag; |
| 3058 | struct lys_node *last, *elem; |
| 3059 | struct lys_module *orig_mod; |
| 3060 | |
| 3061 | /* remove applied deviations */ |
| 3062 | for (i = 0; i < module->deviation_size; ++i) { |
| 3063 | lys_switch_deviation(&module->deviation[i], module); |
| 3064 | |
| 3065 | /* remove our deviation import, clear deviated flag is possible */ |
| 3066 | orig_mod = lys_node_module(module->deviation[i].orig_node); |
| 3067 | flag = 0; |
| 3068 | for (j = 0; j < orig_mod->imp_size; ++j) { |
| 3069 | if (orig_mod->imp[j].external == 2) { |
| 3070 | if (orig_mod->imp[j].module == lys_main_module(module)) { |
| 3071 | /* our deviation import, remove it */ |
| 3072 | --orig_mod->imp_size; |
| 3073 | if (j < orig_mod->imp_size) { |
| 3074 | memcpy(&orig_mod->imp[j], &orig_mod->imp[j + 1], (orig_mod->imp_size - j) * sizeof *orig_mod->imp); |
| 3075 | } |
| 3076 | --j; |
| 3077 | } else { |
| 3078 | /* some other deviation, we cannot clear the deviated flag */ |
| 3079 | flag = 1; |
| 3080 | } |
| 3081 | } |
| 3082 | } |
| 3083 | if (!flag) { |
| 3084 | /* it's safe to clear the deviated flag */ |
| 3085 | orig_mod->deviated = 0; |
| 3086 | } |
| 3087 | } |
| 3088 | |
| 3089 | /* remove applied augments */ |
| 3090 | for (i = 0; i < module->augment_size; ++i) { |
| 3091 | assert(module->augment[i].target); |
| 3092 | |
| 3093 | elem = module->augment[i].child; |
| 3094 | if (elem) { |
| 3095 | LY_TREE_FOR(elem, last) { |
| 3096 | if (!last->next || (last->next->parent != (struct lys_node *)&module->augment[i])) { |
| 3097 | break; |
| 3098 | } |
| 3099 | } |
| 3100 | /* elem is first augment child, last is the last child */ |
| 3101 | |
| 3102 | /* parent child ptr */ |
| 3103 | if (module->augment[i].target->child == elem) { |
| 3104 | module->augment[i].target->child = last->next; |
| 3105 | } |
| 3106 | |
| 3107 | /* parent child next ptr */ |
| 3108 | if (elem->prev->next) { |
| 3109 | elem->prev->next = last->next; |
| 3110 | } |
| 3111 | |
| 3112 | /* parent child prev ptr */ |
| 3113 | if (last->next) { |
| 3114 | last->next->prev = elem->prev; |
| 3115 | } else if (module->augment[i].target->child) { |
| 3116 | module->augment[i].target->child->prev = elem->prev; |
| 3117 | } |
| 3118 | |
| 3119 | /* update augment children themselves */ |
| 3120 | elem->prev = last; |
| 3121 | last->next = NULL; |
| 3122 | } |
| 3123 | } |
| 3124 | } |
| 3125 | |
| 3126 | void |
| 3127 | lys_sub_module_set_dev_aug_target_implement(struct lys_module *module) |
| 3128 | { |
| 3129 | int i; |
| 3130 | struct lys_module *trg_mod; |
| 3131 | |
| 3132 | for (i = 0; i < module->deviation_size; ++i) { |
| 3133 | assert(module->deviation[i].orig_node); |
| 3134 | trg_mod = lys_node_module(module->deviation[i].orig_node); |
| 3135 | if (!trg_mod->implemented) { |
| 3136 | trg_mod->implemented = 1; |
| 3137 | } |
| 3138 | } |
| 3139 | |
| 3140 | for (i = 0; i < module->augment_size; ++i) { |
| 3141 | assert(module->augment[i].target); |
| 3142 | trg_mod = lys_node_module(module->augment[i].target); |
| 3143 | if (!trg_mod->implemented) { |
| 3144 | trg_mod->implemented = 1; |
| 3145 | } |
| 3146 | } |
| 3147 | } |
| 3148 | |
| 3149 | void |
| 3150 | lys_submodule_module_data_free(struct lys_submodule *submodule) |
| 3151 | { |
| 3152 | struct lys_node *next, *elem; |
| 3153 | |
| 3154 | /* remove parsed data */ |
| 3155 | LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) { |
| 3156 | if (elem->module == (struct lys_module *)submodule) { |
| 3157 | lys_node_free(elem, NULL, 0); |
| 3158 | } |
| 3159 | } |
| 3160 | } |