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; |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 821 | while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE | LYS_AUGMENT))) { |
| 822 | if (iter->nodetype == LYS_AUGMENT) { |
| 823 | if (((struct lys_node_augment *)iter)->target) { |
| 824 | /* augment is resolved, go up */ |
| 825 | iter = ((struct lys_node_augment *)iter)->target; |
| 826 | continue; |
| 827 | } |
| 828 | /* augment is not resolved, this is the final parent */ |
| 829 | break; |
| 830 | } |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 831 | iter = iter->parent; |
| 832 | } |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 833 | |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 834 | if (!iter) { |
| 835 | stop = NULL; |
| 836 | iter = module->data; |
| 837 | } else { |
| 838 | stop = iter; |
| 839 | iter = iter->child; |
| 840 | } |
| 841 | } else { |
| 842 | stop = NULL; |
| 843 | iter = module->data; |
| 844 | } |
| 845 | while (iter) { |
| 846 | if (iter->nodetype & (LYS_USES | LYS_CASE)) { |
| 847 | iter = iter->child; |
| 848 | continue; |
| 849 | } |
| 850 | |
| 851 | 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] | 852 | if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 853 | LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, strnodetype(node->nodetype), node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 854 | return EXIT_FAILURE; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | /* special case for choice - we must check the choice's name as |
| 859 | * well as the names of nodes under the choice |
| 860 | */ |
| 861 | if (iter->nodetype == LYS_CHOICE) { |
| 862 | iter = iter->child; |
| 863 | continue; |
| 864 | } |
| 865 | |
| 866 | /* go to siblings */ |
| 867 | if (!iter->next) { |
| 868 | /* no sibling, go to parent's sibling */ |
| 869 | do { |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 870 | /* for parent LYS_AUGMENT */ |
| 871 | if (iter->parent == stop) { |
| 872 | iter = stop; |
| 873 | break; |
| 874 | } |
| 875 | iter = lys_parent(iter); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 876 | if (iter && iter->next) { |
| 877 | break; |
| 878 | } |
| 879 | } while (iter != stop); |
| 880 | |
| 881 | if (iter == stop) { |
| 882 | break; |
| 883 | } |
| 884 | } |
| 885 | iter = iter->next; |
| 886 | } |
| 887 | break; |
| 888 | case LYS_CASE: |
| 889 | /* 6.2.1, rule 8 */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 890 | if (parent) { |
| 891 | start = parent->child; |
| 892 | } else { |
| 893 | start = module->data; |
| 894 | } |
| 895 | |
| 896 | LY_TREE_FOR(start, iter) { |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 897 | if (!(iter->nodetype & (LYS_ANYXML | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) { |
| 898 | continue; |
| 899 | } |
| 900 | |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 901 | if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 902 | LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "case", node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 903 | return EXIT_FAILURE; |
| 904 | } |
| 905 | } |
| 906 | break; |
| 907 | default: |
| 908 | /* no check needed */ |
| 909 | break; |
| 910 | } |
| 911 | |
| 912 | return EXIT_SUCCESS; |
| 913 | } |
| 914 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 915 | /* logs directly */ |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 916 | int |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 917 | lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child) |
| 918 | { |
Radek Krejci | 9272055 | 2015-10-05 15:28:27 +0200 | [diff] [blame] | 919 | struct lys_node *iter; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 920 | int type; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 921 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 922 | assert(child); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 923 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 924 | if (parent) { |
| 925 | type = parent->nodetype; |
| 926 | module = parent->module; |
| 927 | } else { |
| 928 | assert(module); |
| 929 | type = 0; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 930 | } |
| 931 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 932 | /* checks */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 933 | switch (type) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 934 | case LYS_CONTAINER: |
| 935 | case LYS_LIST: |
| 936 | case LYS_GROUPING: |
| 937 | case LYS_USES: |
| 938 | case LYS_INPUT: |
| 939 | case LYS_OUTPUT: |
| 940 | case LYS_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 941 | if (!(child->nodetype & |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 942 | (LYS_ANYXML | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF | |
| 943 | 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), strnodetype(parent->nodetype)); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 945 | return EXIT_FAILURE; |
| 946 | } |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 947 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 948 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 949 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 950 | if (!(child->nodetype & |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 951 | (LYS_ANYXML | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 952 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 953 | return EXIT_FAILURE; |
| 954 | } |
| 955 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 956 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 957 | if (!(child->nodetype & |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 958 | (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] | 959 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 960 | return EXIT_FAILURE; |
| 961 | } |
| 962 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 963 | case LYS_RPC: |
| 964 | if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 965 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc"); |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 966 | return EXIT_FAILURE; |
| 967 | } |
| 968 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 969 | case LYS_LEAF: |
| 970 | case LYS_LEAFLIST: |
| 971 | case LYS_ANYXML: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 972 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
| 973 | 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] | 974 | strnodetype(parent->nodetype)); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 975 | return EXIT_FAILURE; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 976 | case LYS_AUGMENT: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 977 | if (!(child->nodetype & |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 978 | (LYS_ANYXML | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF |
| 979 | | LYS_LEAFLIST | LYS_LIST | LYS_USES))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 980 | 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] | 981 | return EXIT_FAILURE; |
| 982 | } |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 983 | break; |
| 984 | case LYS_UNKNOWN: |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 985 | /* top level */ |
| 986 | if (!(child->nodetype & |
| 987 | (LYS_ANYXML | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING |
| 988 | | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 989 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "(sub)module"); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 990 | return EXIT_FAILURE; |
| 991 | } |
| 992 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 993 | break; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /* check identifier uniqueness */ |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 997 | if (lys_check_id(child, parent, module)) { |
| 998 | return EXIT_FAILURE; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 999 | } |
Radek Krejci | b7155b5 | 2015-06-10 17:03:01 +0200 | [diff] [blame] | 1000 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1001 | if (child->parent) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1002 | lys_node_unlink(child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1003 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1004 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 1005 | if (!parent) { |
Radek Krejci | 9272055 | 2015-10-05 15:28:27 +0200 | [diff] [blame] | 1006 | if (module->data) { |
| 1007 | module->data->prev->next = child; |
| 1008 | child->prev = module->data->prev; |
| 1009 | module->data->prev = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 1010 | } else { |
Radek Krejci | 9272055 | 2015-10-05 15:28:27 +0200 | [diff] [blame] | 1011 | module->data = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 1012 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1013 | } else { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 1014 | if (!parent->child) { |
| 1015 | /* the only/first child of the parent */ |
| 1016 | parent->child = child; |
| 1017 | child->parent = parent; |
| 1018 | iter = child; |
| 1019 | } else { |
| 1020 | /* add a new child at the end of parent's child list */ |
| 1021 | iter = parent->child->prev; |
| 1022 | iter->next = child; |
| 1023 | child->prev = iter; |
| 1024 | } |
| 1025 | while (iter->next) { |
| 1026 | iter = iter->next; |
| 1027 | iter->parent = parent; |
| 1028 | } |
| 1029 | parent->child->prev = iter; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1030 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1031 | |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 1032 | /* propagate information about status data presence */ |
| 1033 | if ((child->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML)) && |
| 1034 | (child->flags & LYS_INCL_STATUS)) { |
| 1035 | for(iter = parent; iter; iter = iter->parent) { |
| 1036 | /* store it only into container or list - the only data inner nodes */ |
| 1037 | if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
| 1038 | if (iter->flags & LYS_INCL_STATUS) { |
| 1039 | /* done, someone else set it already from here */ |
| 1040 | break; |
| 1041 | } |
| 1042 | /* set flag about including status data */ |
| 1043 | iter->flags |= LYS_INCL_STATUS; |
| 1044 | } |
| 1045 | } |
| 1046 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1047 | return EXIT_SUCCESS; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1048 | } |
| 1049 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1050 | static const struct lys_module * |
| 1051 | 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] | 1052 | { |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1053 | char *enlarged_data = NULL; |
Radek Krejci | 0b5805d | 2015-08-13 09:38:02 +0200 | [diff] [blame] | 1054 | struct lys_module *mod = NULL; |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1055 | unsigned int len; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1056 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1057 | if (!ctx || !data) { |
| 1058 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1059 | return NULL; |
| 1060 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1061 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1062 | if (!internal && format == LYS_IN_YANG) { |
| 1063 | /* enlarge data by 2 bytes for flex */ |
| 1064 | len = strlen(data); |
| 1065 | enlarged_data = malloc((len + 2) * sizeof *enlarged_data); |
| 1066 | if (!enlarged_data) { |
| 1067 | LOGMEM; |
| 1068 | return NULL; |
| 1069 | } |
| 1070 | memcpy(enlarged_data, data, len); |
| 1071 | enlarged_data[len] = enlarged_data[len + 1] = '\0'; |
| 1072 | data = enlarged_data; |
| 1073 | } |
| 1074 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1075 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1076 | case LYS_IN_YIN: |
Radek Krejci | ff4874d | 2016-03-07 12:30:50 +0100 | [diff] [blame] | 1077 | mod = yin_read_module(ctx, data, NULL, 1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1078 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1079 | case LYS_IN_YANG: |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1080 | mod = yang_read_module(ctx, data, 0, NULL, 1); |
| 1081 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1082 | default: |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1083 | LOGERR(LY_EINVAL, "Invalid schema input format."); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1084 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1085 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1086 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1087 | free(enlarged_data); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1088 | return mod; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1089 | } |
| 1090 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1091 | API const struct lys_module * |
| 1092 | lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format) |
| 1093 | { |
| 1094 | return lys_parse_mem_(ctx, data, format, 0); |
| 1095 | } |
| 1096 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1097 | struct lys_submodule * |
| 1098 | 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] | 1099 | { |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1100 | struct lys_submodule *submod = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1101 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1102 | assert(module); |
| 1103 | assert(data); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1104 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1105 | /* get the main module */ |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 1106 | module = lys_main_module(module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1107 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1108 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1109 | case LYS_IN_YIN: |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1110 | submod = yin_read_submodule(module, data, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1111 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1112 | case LYS_IN_YANG: |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1113 | submod = yang_read_submodule(module, data, 0, unres); |
| 1114 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1115 | default: |
Radek Krejci | 90a550a | 2016-04-13 16:00:58 +0200 | [diff] [blame] | 1116 | assert(0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1117 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1118 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1119 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1120 | return submod; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1121 | } |
| 1122 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1123 | API const struct lys_module * |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1124 | lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format) |
| 1125 | { |
| 1126 | int fd; |
| 1127 | const struct lys_module *ret; |
| 1128 | |
| 1129 | if (!ctx || !path) { |
| 1130 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1131 | return NULL; |
| 1132 | } |
| 1133 | |
| 1134 | fd = open(path, O_RDONLY); |
| 1135 | if (fd == -1) { |
| 1136 | LOGERR(LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)); |
| 1137 | return NULL; |
| 1138 | } |
| 1139 | |
| 1140 | ret = lys_parse_fd(ctx, fd, format); |
| 1141 | close(fd); |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 1142 | |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1143 | if (ret && !ret->filepath) { |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 1144 | /* store URI */ |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1145 | ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0); |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 1146 | } |
| 1147 | |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1148 | return ret; |
| 1149 | } |
| 1150 | |
| 1151 | API const struct lys_module * |
| 1152 | lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format) |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1153 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1154 | const struct lys_module *module; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1155 | struct stat sb; |
| 1156 | char *addr; |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1157 | char buf[PATH_MAX]; |
| 1158 | int len; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1159 | |
| 1160 | if (!ctx || fd < 0) { |
| 1161 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1162 | return NULL; |
| 1163 | } |
| 1164 | |
Radek Krejci | 10a833c | 2015-12-16 15:28:37 +0100 | [diff] [blame] | 1165 | if (fstat(fd, &sb) == -1) { |
| 1166 | LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno)); |
| 1167 | return NULL; |
| 1168 | } |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1169 | if (!S_ISREG(sb.st_mode)) { |
| 1170 | LOGERR(LY_EINVAL, "Invalid parameter, input file is not a regular file"); |
| 1171 | return NULL; |
| 1172 | } |
| 1173 | |
Michal Vasko | 164d901 | 2016-04-01 10:16:59 +0200 | [diff] [blame] | 1174 | if (!sb.st_size) { |
| 1175 | LOGERR(LY_EINVAL, "File empty."); |
| 1176 | return NULL; |
| 1177 | } |
| 1178 | |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1179 | 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] | 1180 | if (addr == MAP_FAILED) { |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1181 | LOGERR(LY_EMEM, "Map file into memory failed (%s()).",__func__); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1182 | return NULL; |
| 1183 | } |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1184 | module = lys_parse_mem_(ctx, addr, format, 1); |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1185 | munmap(addr, sb.st_size + 2); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1186 | |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1187 | if (module && !module->filepath) { |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1188 | /* get URI if there is /proc */ |
| 1189 | addr = NULL; |
Radek Krejci | 15412ca | 2016-03-03 11:16:52 +0100 | [diff] [blame] | 1190 | if (asprintf(&addr, "/proc/self/fd/%d", fd) != -1) { |
| 1191 | if ((len = readlink(addr, buf, PATH_MAX - 1)) > 0) { |
| 1192 | ((struct lys_module *)module)->filepath = lydict_insert(ctx, buf, len); |
| 1193 | } |
| 1194 | free(addr); |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1195 | } |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1196 | } |
| 1197 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1198 | return module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1199 | } |
| 1200 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1201 | struct lys_submodule * |
| 1202 | 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] | 1203 | { |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1204 | struct lys_submodule *submodule; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1205 | struct stat sb; |
| 1206 | char *addr; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1207 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1208 | assert(module); |
| 1209 | assert(fd >= 0); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1210 | |
Radek Krejci | 10a833c | 2015-12-16 15:28:37 +0100 | [diff] [blame] | 1211 | if (fstat(fd, &sb) == -1) { |
| 1212 | LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno)); |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1213 | return NULL; |
Radek Krejci | 10a833c | 2015-12-16 15:28:37 +0100 | [diff] [blame] | 1214 | } |
Michal Vasko | 164d901 | 2016-04-01 10:16:59 +0200 | [diff] [blame] | 1215 | |
| 1216 | if (!sb.st_size) { |
| 1217 | LOGERR(LY_EINVAL, "File empty."); |
| 1218 | return NULL; |
| 1219 | } |
| 1220 | |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1221 | 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] | 1222 | if (addr == MAP_FAILED) { |
| 1223 | LOGERR(LY_EMEM,"Map file into memory failed (%s()).",__func__); |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1224 | return NULL; |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1225 | } |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1226 | submodule = lys_submodule_parse(module, addr, format, unres); |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1227 | munmap(addr, sb.st_size + 2); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1228 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1229 | return submodule; |
| 1230 | |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1231 | } |
| 1232 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1233 | static struct lys_restr * |
| 1234 | 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] | 1235 | { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1236 | struct lys_restr *result; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1237 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1238 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1239 | if (!size) { |
| 1240 | return NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1241 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1242 | |
| 1243 | result = calloc(size, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1244 | if (!result) { |
Radek Krejci | aa2a893 | 2016-02-17 15:03:14 +0100 | [diff] [blame] | 1245 | LOGMEM; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1246 | return NULL; |
| 1247 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1248 | for (i = 0; i < size; i++) { |
| 1249 | result[i].expr = lydict_insert(ctx, old[i].expr, 0); |
| 1250 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 1251 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 1252 | result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0); |
| 1253 | result[i].emsg = lydict_insert(ctx, old[i].emsg, 0); |
| 1254 | } |
| 1255 | |
| 1256 | return result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1257 | } |
| 1258 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1259 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1260 | lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr) |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1261 | { |
| 1262 | assert(ctx); |
| 1263 | if (!restr) { |
| 1264 | return; |
| 1265 | } |
| 1266 | |
| 1267 | lydict_remove(ctx, restr->expr); |
| 1268 | lydict_remove(ctx, restr->dsc); |
| 1269 | lydict_remove(ctx, restr->ref); |
| 1270 | lydict_remove(ctx, restr->eapptag); |
| 1271 | lydict_remove(ctx, restr->emsg); |
| 1272 | } |
| 1273 | |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1274 | static int |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1275 | type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old, |
| 1276 | LY_DATA_TYPE base, struct unres_schema *unres) |
| 1277 | { |
| 1278 | int i; |
| 1279 | |
| 1280 | switch (base) { |
| 1281 | case LY_TYPE_BINARY: |
| 1282 | if (old->info.binary.length) { |
| 1283 | new->info.binary.length = lys_restr_dup(mod->ctx, old->info.binary.length, 1); |
| 1284 | } |
| 1285 | break; |
| 1286 | |
| 1287 | case LY_TYPE_BITS: |
| 1288 | new->info.bits.count = old->info.bits.count; |
| 1289 | if (new->info.bits.count) { |
| 1290 | new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit); |
| 1291 | if (!new->info.bits.bit) { |
| 1292 | LOGMEM; |
| 1293 | return -1; |
| 1294 | } |
| 1295 | for (i = 0; i < new->info.bits.count; i++) { |
| 1296 | new->info.bits.bit[i].name = lydict_insert(mod->ctx, old->info.bits.bit[i].name, 0); |
| 1297 | new->info.bits.bit[i].dsc = lydict_insert(mod->ctx, old->info.bits.bit[i].dsc, 0); |
| 1298 | new->info.bits.bit[i].ref = lydict_insert(mod->ctx, old->info.bits.bit[i].ref, 0); |
| 1299 | new->info.bits.bit[i].flags = old->info.bits.bit[i].flags; |
| 1300 | new->info.bits.bit[i].pos = old->info.bits.bit[i].pos; |
| 1301 | } |
| 1302 | } |
| 1303 | break; |
| 1304 | |
| 1305 | case LY_TYPE_DEC64: |
| 1306 | new->info.dec64.dig = old->info.dec64.dig; |
| 1307 | if (old->info.dec64.range) { |
| 1308 | new->info.dec64.range = lys_restr_dup(mod->ctx, old->info.dec64.range, 1); |
| 1309 | } |
| 1310 | break; |
| 1311 | |
| 1312 | case LY_TYPE_ENUM: |
| 1313 | new->info.enums.count = old->info.enums.count; |
| 1314 | if (new->info.enums.count) { |
| 1315 | new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm); |
| 1316 | if (!new->info.enums.enm) { |
| 1317 | LOGMEM; |
| 1318 | return -1; |
| 1319 | } |
| 1320 | for (i = 0; i < new->info.enums.count; i++) { |
| 1321 | new->info.enums.enm[i].name = lydict_insert(mod->ctx, old->info.enums.enm[i].name, 0); |
| 1322 | new->info.enums.enm[i].dsc = lydict_insert(mod->ctx, old->info.enums.enm[i].dsc, 0); |
| 1323 | new->info.enums.enm[i].ref = lydict_insert(mod->ctx, old->info.enums.enm[i].ref, 0); |
| 1324 | new->info.enums.enm[i].flags = old->info.enums.enm[i].flags; |
| 1325 | new->info.enums.enm[i].value = old->info.enums.enm[i].value; |
| 1326 | } |
| 1327 | } |
| 1328 | break; |
| 1329 | |
| 1330 | case LY_TYPE_IDENT: |
| 1331 | if (old->info.ident.ref) { |
| 1332 | new->info.ident.ref = old->info.ident.ref; |
| 1333 | } else { |
| 1334 | i = unres_schema_find(unres, old, UNRES_TYPE_IDENTREF); |
| 1335 | if (i > -1 && unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i])) { |
| 1336 | return -1; |
| 1337 | } |
| 1338 | } |
| 1339 | break; |
| 1340 | |
| 1341 | case LY_TYPE_INST: |
| 1342 | new->info.inst.req = old->info.inst.req; |
| 1343 | break; |
| 1344 | |
| 1345 | case LY_TYPE_INT8: |
| 1346 | case LY_TYPE_INT16: |
| 1347 | case LY_TYPE_INT32: |
| 1348 | case LY_TYPE_INT64: |
| 1349 | case LY_TYPE_UINT8: |
| 1350 | case LY_TYPE_UINT16: |
| 1351 | case LY_TYPE_UINT32: |
| 1352 | case LY_TYPE_UINT64: |
| 1353 | if (old->info.num.range) { |
| 1354 | new->info.num.range = lys_restr_dup(mod->ctx, old->info.num.range, 1); |
| 1355 | } |
| 1356 | break; |
| 1357 | |
| 1358 | case LY_TYPE_LEAFREF: |
| 1359 | if (old->info.lref.path) { |
| 1360 | new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0); |
| 1361 | if (unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent)) { |
| 1362 | return -1; |
| 1363 | } |
| 1364 | } |
| 1365 | break; |
| 1366 | |
| 1367 | case LY_TYPE_STRING: |
| 1368 | if (old->info.str.length) { |
| 1369 | new->info.str.length = lys_restr_dup(mod->ctx, old->info.str.length, 1); |
| 1370 | } |
| 1371 | new->info.str.patterns = lys_restr_dup(mod->ctx, old->info.str.patterns, old->info.str.pat_count); |
| 1372 | new->info.str.pat_count = old->info.str.pat_count; |
| 1373 | break; |
| 1374 | |
| 1375 | case LY_TYPE_UNION: |
| 1376 | new->info.uni.count = old->info.uni.count; |
| 1377 | if (new->info.uni.count) { |
| 1378 | new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types); |
| 1379 | if (!new->info.uni.types) { |
| 1380 | LOGMEM; |
| 1381 | return -1; |
| 1382 | } |
| 1383 | for (i = 0; i < new->info.uni.count; i++) { |
| 1384 | if (lys_type_dup(mod, parent, &(new->info.uni.types[i]), &(old->info.uni.types[i]), unres)) { |
| 1385 | return -1; |
| 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | break; |
| 1390 | |
| 1391 | default: |
| 1392 | /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */ |
| 1393 | break; |
| 1394 | } |
| 1395 | return EXIT_SUCCESS; |
| 1396 | } |
| 1397 | |
| 1398 | struct yang_type * |
| 1399 | lys_yang_type_dup(struct lys_module *module, struct lys_node *parent, struct yang_type *old, struct lys_type *type, struct unres_schema *unres) |
| 1400 | { |
| 1401 | struct yang_type *new; |
| 1402 | |
| 1403 | new = calloc(1, sizeof *new); |
| 1404 | if (!new) { |
| 1405 | LOGMEM; |
| 1406 | return NULL; |
| 1407 | } |
| 1408 | new->flags = old->flags; |
| 1409 | new->base = old->base; |
Pavol Vican | 6658629 | 2016-04-07 11:38:52 +0200 | [diff] [blame] | 1410 | new->name = lydict_insert(module->ctx, old->name, 0); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1411 | new->type = type; |
| 1412 | if (!new->name) { |
| 1413 | LOGMEM; |
| 1414 | goto error; |
| 1415 | } |
| 1416 | if (type_dup(module, parent, type, old->type, new->base, unres)) { |
| 1417 | new->type->base = new->base; |
| 1418 | lys_type_free(module->ctx, new->type); |
| 1419 | memset(&new->type->info, 0, sizeof new->type->info); |
| 1420 | goto error; |
| 1421 | } |
| 1422 | return new; |
| 1423 | |
| 1424 | error: |
| 1425 | free(new); |
| 1426 | return NULL; |
| 1427 | } |
| 1428 | |
| 1429 | static int |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1430 | 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] | 1431 | struct unres_schema *unres) |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1432 | { |
| 1433 | int i; |
| 1434 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1435 | new->module_name = lydict_insert(mod->ctx, old->module_name, 0); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1436 | new->base = old->base; |
| 1437 | new->der = old->der; |
| 1438 | |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 1439 | i = unres_schema_find(unres, old, UNRES_TYPE_DER); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1440 | if (i != -1) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 1441 | /* HACK (serious one) for unres */ |
| 1442 | /* nothing else we can do but duplicate it immediately */ |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1443 | if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) { |
| 1444 | new->der = (struct lys_tpdf *)lys_yang_type_dup(mod, parent, (struct yang_type *)old->der, new, unres); |
| 1445 | } else { |
| 1446 | new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1); |
| 1447 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1448 | new->parent = (struct lys_tpdf *)parent; |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1449 | /* all these unres additions can fail even though they did not before */ |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1450 | 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] | 1451 | return -1; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 1452 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1453 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1454 | } |
| 1455 | |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1456 | return type_dup(mod, parent, new, old, new->base, unres); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1460 | lys_type_free(struct ly_ctx *ctx, struct lys_type *type) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1461 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1462 | int i; |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1463 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1464 | assert(ctx); |
| 1465 | if (!type) { |
| 1466 | return; |
| 1467 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1468 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1469 | lydict_remove(ctx, type->module_name); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1470 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1471 | switch (type->base) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1472 | case LY_TYPE_BINARY: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1473 | lys_restr_free(ctx, type->info.binary.length); |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1474 | free(type->info.binary.length); |
| 1475 | break; |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 1476 | case LY_TYPE_BITS: |
| 1477 | for (i = 0; i < type->info.bits.count; i++) { |
| 1478 | lydict_remove(ctx, type->info.bits.bit[i].name); |
| 1479 | lydict_remove(ctx, type->info.bits.bit[i].dsc); |
| 1480 | lydict_remove(ctx, type->info.bits.bit[i].ref); |
| 1481 | } |
| 1482 | free(type->info.bits.bit); |
| 1483 | break; |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1484 | |
| 1485 | case LY_TYPE_DEC64: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1486 | lys_restr_free(ctx, type->info.dec64.range); |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1487 | free(type->info.dec64.range); |
| 1488 | break; |
| 1489 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1490 | case LY_TYPE_ENUM: |
| 1491 | for (i = 0; i < type->info.enums.count; i++) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1492 | lydict_remove(ctx, type->info.enums.enm[i].name); |
| 1493 | lydict_remove(ctx, type->info.enums.enm[i].dsc); |
| 1494 | lydict_remove(ctx, type->info.enums.enm[i].ref); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1495 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1496 | free(type->info.enums.enm); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1497 | break; |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1498 | |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 1499 | case LY_TYPE_INT8: |
| 1500 | case LY_TYPE_INT16: |
| 1501 | case LY_TYPE_INT32: |
| 1502 | case LY_TYPE_INT64: |
| 1503 | case LY_TYPE_UINT8: |
| 1504 | case LY_TYPE_UINT16: |
| 1505 | case LY_TYPE_UINT32: |
| 1506 | case LY_TYPE_UINT64: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1507 | lys_restr_free(ctx, type->info.num.range); |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 1508 | free(type->info.num.range); |
| 1509 | break; |
| 1510 | |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1511 | case LY_TYPE_LEAFREF: |
| 1512 | lydict_remove(ctx, type->info.lref.path); |
| 1513 | break; |
| 1514 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1515 | case LY_TYPE_STRING: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1516 | lys_restr_free(ctx, type->info.str.length); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1517 | free(type->info.str.length); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1518 | for (i = 0; i < type->info.str.pat_count; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1519 | lys_restr_free(ctx, &type->info.str.patterns[i]); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1520 | } |
| 1521 | free(type->info.str.patterns); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1522 | break; |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1523 | |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1524 | case LY_TYPE_UNION: |
| 1525 | for (i = 0; i < type->info.uni.count; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1526 | lys_type_free(ctx, &type->info.uni.types[i]); |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1527 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1528 | free(type->info.uni.types); |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1529 | break; |
| 1530 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1531 | default: |
Radek Krejci | c7c8553 | 2015-07-02 10:16:54 +0200 | [diff] [blame] | 1532 | /* 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] | 1533 | break; |
| 1534 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1535 | } |
| 1536 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1537 | static void |
| 1538 | lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1539 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1540 | assert(ctx); |
| 1541 | if (!tpdf) { |
| 1542 | return; |
| 1543 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1544 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1545 | lydict_remove(ctx, tpdf->name); |
| 1546 | lydict_remove(ctx, tpdf->dsc); |
| 1547 | lydict_remove(ctx, tpdf->ref); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1548 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1549 | lys_type_free(ctx, &tpdf->type); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1550 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1551 | lydict_remove(ctx, tpdf->units); |
| 1552 | lydict_remove(ctx, tpdf->dflt); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1553 | } |
| 1554 | |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1555 | static struct lys_tpdf * |
| 1556 | lys_tpdf_dup(struct lys_module *mod, struct lys_node *parent, struct lys_tpdf *old, int size, struct unres_schema *unres) |
| 1557 | { |
| 1558 | struct lys_tpdf *result; |
| 1559 | int i, j; |
| 1560 | |
| 1561 | if (!size) { |
| 1562 | return NULL; |
| 1563 | } |
| 1564 | |
| 1565 | result = calloc(size, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1566 | if (!result) { |
| 1567 | LOGMEM; |
| 1568 | return NULL; |
| 1569 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1570 | for (i = 0; i < size; i++) { |
| 1571 | result[i].name = lydict_insert(mod->ctx, old[i].name, 0); |
| 1572 | result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0); |
| 1573 | result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0); |
| 1574 | result[i].flags = old[i].flags; |
| 1575 | result[i].module = old[i].module; |
| 1576 | |
| 1577 | if (lys_type_dup(mod, parent, &(result[i].type), &(old[i].type), unres)) { |
| 1578 | for (j = 0; j <= i; ++j) { |
| 1579 | lys_tpdf_free(mod->ctx, &result[j]); |
| 1580 | } |
| 1581 | free(result); |
| 1582 | return NULL; |
| 1583 | } |
| 1584 | |
| 1585 | result[i].dflt = lydict_insert(mod->ctx, old[i].dflt, 0); |
| 1586 | result[i].units = lydict_insert(mod->ctx, old[i].units, 0); |
| 1587 | } |
| 1588 | |
| 1589 | return result; |
| 1590 | } |
| 1591 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1592 | static struct lys_when * |
| 1593 | lys_when_dup(struct ly_ctx *ctx, struct lys_when *old) |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1594 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1595 | struct lys_when *new; |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1596 | |
| 1597 | if (!old) { |
| 1598 | return NULL; |
| 1599 | } |
| 1600 | |
| 1601 | new = calloc(1, sizeof *new); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1602 | if (!new) { |
| 1603 | LOGMEM; |
| 1604 | return NULL; |
| 1605 | } |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1606 | new->cond = lydict_insert(ctx, old->cond, 0); |
| 1607 | new->dsc = lydict_insert(ctx, old->dsc, 0); |
| 1608 | new->ref = lydict_insert(ctx, old->ref, 0); |
| 1609 | |
| 1610 | return new; |
| 1611 | } |
| 1612 | |
Michal Vasko | 0308dd6 | 2015-10-07 09:14:40 +0200 | [diff] [blame] | 1613 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1614 | lys_when_free(struct ly_ctx *ctx, struct lys_when *w) |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1615 | { |
| 1616 | if (!w) { |
| 1617 | return; |
| 1618 | } |
| 1619 | |
| 1620 | lydict_remove(ctx, w->cond); |
| 1621 | lydict_remove(ctx, w->dsc); |
| 1622 | lydict_remove(ctx, w->ref); |
| 1623 | |
| 1624 | free(w); |
| 1625 | } |
| 1626 | |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1627 | static void |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1628 | 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] | 1629 | { |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 1630 | struct lys_node *next, *sub; |
| 1631 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1632 | /* children from a resolved augment are freed under the target node */ |
Michal Vasko | d2fbade | 2016-05-02 17:14:00 +0200 | [diff] [blame] | 1633 | if (!aug->target) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1634 | LY_TREE_FOR_SAFE(aug->child, next, sub) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1635 | lys_node_free(sub, private_destructor, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1636 | } |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 1637 | } |
| 1638 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1639 | lydict_remove(ctx, aug->target_name); |
| 1640 | lydict_remove(ctx, aug->dsc); |
| 1641 | lydict_remove(ctx, aug->ref); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1642 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1643 | free(aug->features); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1644 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1645 | lys_when_free(ctx, aug->when); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1646 | } |
| 1647 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1648 | static struct lys_node_augment * |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1649 | 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] | 1650 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1651 | struct lys_node_augment *new = NULL; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1652 | struct lys_node *old_child, *new_child; |
| 1653 | int i; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1654 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1655 | if (!size) { |
| 1656 | return NULL; |
| 1657 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1658 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1659 | new = calloc(size, sizeof *new); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1660 | if (!new) { |
| 1661 | LOGMEM; |
| 1662 | return NULL; |
| 1663 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1664 | for (i = 0; i < size; i++) { |
| 1665 | new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0); |
| 1666 | new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0); |
| 1667 | new[i].ref = lydict_insert(module->ctx, old[i].ref, 0); |
| 1668 | new[i].flags = old[i].flags; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1669 | new[i].module = old[i].module; |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 1670 | new[i].nodetype = old[i].nodetype; |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1671 | |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1672 | /* this must succeed, it was already resolved once */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1673 | if (resolve_augment_schema_nodeid(new[i].target_name, parent->child, NULL, |
| 1674 | (const struct lys_node **)&new[i].target)) { |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1675 | LOGINT; |
| 1676 | free(new); |
| 1677 | return NULL; |
| 1678 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1679 | new[i].parent = parent; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1680 | |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1681 | /* Correct the augment nodes. |
| 1682 | * This function can only be called from lys_node_dup() with uses |
| 1683 | * being the node duplicated, so we must have a case of grouping |
| 1684 | * with a uses with augments. The augmented nodes have already been |
| 1685 | * copied and everything is almost fine except their parent is wrong |
Michal Vasko | a5900c5 | 2015-09-24 13:17:11 +0200 | [diff] [blame] | 1686 | * (it was set to their actual data parent, not an augment), and |
| 1687 | * the new augment does not have child pointer to its augment nodes, |
| 1688 | * so we just correct it. |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1689 | */ |
| 1690 | LY_TREE_FOR(new[i].target->child, new_child) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 1691 | if (ly_strequal(new_child->name, old[i].child->name, 1)) { |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1692 | break; |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1693 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1694 | } |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1695 | assert(new_child); |
Michal Vasko | a5900c5 | 2015-09-24 13:17:11 +0200 | [diff] [blame] | 1696 | new[i].child = new_child; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1697 | LY_TREE_FOR(old[i].child, old_child) { |
| 1698 | /* all augment nodes were connected as siblings, there can be no more after this */ |
| 1699 | if (old_child->parent != (struct lys_node *)&old[i]) { |
| 1700 | break; |
| 1701 | } |
| 1702 | |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 1703 | assert(ly_strequal(old_child->name, new_child->name, 1)); |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1704 | |
| 1705 | new_child->parent = (struct lys_node *)&new[i]; |
| 1706 | new_child = new_child->next; |
| 1707 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1708 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1709 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1710 | return new; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1711 | } |
| 1712 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1713 | static struct lys_refine * |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 1714 | 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] | 1715 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1716 | struct lys_refine *result; |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 1717 | int i; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1718 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1719 | if (!size) { |
| 1720 | return NULL; |
| 1721 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1722 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1723 | result = calloc(size, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1724 | if (!result) { |
| 1725 | LOGMEM; |
| 1726 | return NULL; |
| 1727 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1728 | for (i = 0; i < size; i++) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1729 | 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] | 1730 | result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0); |
| 1731 | result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1732 | result[i].flags = old[i].flags; |
| 1733 | result[i].target_type = old[i].target_type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1734 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1735 | result[i].must_size = old[i].must_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1736 | 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] | 1737 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1738 | if (result[i].target_type & (LYS_LEAF | LYS_CHOICE)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1739 | 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] | 1740 | } else if (result[i].target_type == LYS_CONTAINER) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1741 | 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] | 1742 | } else if (result[i].target_type & (LYS_LIST | LYS_LEAFLIST)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1743 | result[i].mod.list = old[i].mod.list; |
| 1744 | } |
| 1745 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1746 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1747 | return result; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1748 | } |
| 1749 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1750 | static void |
| 1751 | lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident) |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1752 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1753 | assert(ctx); |
| 1754 | if (!ident) { |
| 1755 | return; |
| 1756 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1757 | |
Radek Krejci | 1b61d0e | 2016-04-15 13:55:44 +0200 | [diff] [blame] | 1758 | free(ident->der); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1759 | lydict_remove(ctx, ident->name); |
| 1760 | lydict_remove(ctx, ident->dsc); |
| 1761 | lydict_remove(ctx, ident->ref); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1762 | |
| 1763 | } |
| 1764 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1765 | static void |
| 1766 | lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1767 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1768 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1769 | |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1770 | /* handle only specific parts for LYS_GROUPING */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1771 | for (i = 0; i < grp->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1772 | lys_tpdf_free(ctx, &grp->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1773 | } |
| 1774 | free(grp->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1775 | } |
| 1776 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1777 | static void |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1778 | lys_rpc_inout_free(struct ly_ctx *ctx, struct lys_node_rpc_inout *io) |
| 1779 | { |
| 1780 | int i; |
| 1781 | |
| 1782 | /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */ |
| 1783 | for (i = 0; i < io->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1784 | lys_tpdf_free(ctx, &io->tpdf[i]); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1785 | } |
| 1786 | free(io->tpdf); |
| 1787 | } |
| 1788 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1789 | static void |
| 1790 | lys_anyxml_free(struct ly_ctx *ctx, struct lys_node_anyxml *anyxml) |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1791 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1792 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1793 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1794 | for (i = 0; i < anyxml->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1795 | lys_restr_free(ctx, &anyxml->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1796 | } |
| 1797 | free(anyxml->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1798 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1799 | lys_when_free(ctx, anyxml->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1800 | } |
| 1801 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1802 | static void |
| 1803 | lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1804 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1805 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1806 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1807 | if (leaf->child) { |
| 1808 | /* leafref backlinks */ |
| 1809 | ly_set_free((struct ly_set *)leaf->child); |
| 1810 | } |
| 1811 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1812 | for (i = 0; i < leaf->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1813 | lys_restr_free(ctx, &leaf->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1814 | } |
| 1815 | free(leaf->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1816 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1817 | lys_when_free(ctx, leaf->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1818 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1819 | lys_type_free(ctx, &leaf->type); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1820 | lydict_remove(ctx, leaf->units); |
| 1821 | lydict_remove(ctx, leaf->dflt); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1822 | } |
| 1823 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1824 | static void |
| 1825 | lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1826 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1827 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1828 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1829 | if (llist->child) { |
| 1830 | /* leafref backlinks */ |
| 1831 | ly_set_free((struct ly_set *)llist->child); |
| 1832 | } |
| 1833 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1834 | for (i = 0; i < llist->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1835 | lys_restr_free(ctx, &llist->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1836 | } |
| 1837 | free(llist->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1838 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1839 | lys_when_free(ctx, llist->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1840 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1841 | lys_type_free(ctx, &llist->type); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1842 | lydict_remove(ctx, llist->units); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1843 | } |
| 1844 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1845 | static void |
| 1846 | lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1847 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1848 | int i, j; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1849 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1850 | /* handle only specific parts for LY_NODE_LIST */ |
| 1851 | for (i = 0; i < list->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1852 | lys_tpdf_free(ctx, &list->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1853 | } |
| 1854 | free(list->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1855 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1856 | for (i = 0; i < list->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1857 | lys_restr_free(ctx, &list->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1858 | } |
| 1859 | free(list->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1860 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1861 | lys_when_free(ctx, list->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1862 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1863 | for (i = 0; i < list->unique_size; i++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1864 | for (j = 0; j > list->unique[i].expr_size; j++) { |
| 1865 | lydict_remove(ctx, list->unique[i].expr[j]); |
| 1866 | } |
| 1867 | free(list->unique[i].expr); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1868 | } |
| 1869 | free(list->unique); |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1870 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1871 | free(list->keys); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1872 | } |
| 1873 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1874 | static void |
| 1875 | lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1876 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1877 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1878 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1879 | /* handle only specific parts for LY_NODE_CONTAINER */ |
| 1880 | lydict_remove(ctx, cont->presence); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1881 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1882 | for (i = 0; i < cont->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1883 | lys_tpdf_free(ctx, &cont->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1884 | } |
| 1885 | free(cont->tpdf); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1886 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1887 | for (i = 0; i < cont->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1888 | lys_restr_free(ctx, &cont->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1889 | } |
| 1890 | free(cont->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1891 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1892 | lys_when_free(ctx, cont->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1893 | } |
| 1894 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1895 | static void |
| 1896 | lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f) |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1897 | { |
| 1898 | lydict_remove(ctx, f->name); |
| 1899 | lydict_remove(ctx, f->dsc); |
| 1900 | lydict_remove(ctx, f->ref); |
| 1901 | free(f->features); |
| 1902 | } |
| 1903 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1904 | static void |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 1905 | lys_deviation_free(struct lys_module *module, struct lys_deviation *dev) |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1906 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1907 | int i, j, k; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 1908 | struct ly_ctx *ctx; |
| 1909 | struct lys_node *next, *elem; |
| 1910 | |
| 1911 | ctx = module->ctx; |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1912 | |
| 1913 | lydict_remove(ctx, dev->target_name); |
| 1914 | lydict_remove(ctx, dev->dsc); |
| 1915 | lydict_remove(ctx, dev->ref); |
| 1916 | |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 1917 | /* the module was freed, but we only need the context from orig_node, use ours */ |
| 1918 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 1919 | /* it's actually a node subtree, we need to update modules on all the nodes :-/ */ |
| 1920 | LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) { |
| 1921 | elem->module = module; |
| 1922 | |
| 1923 | LY_TREE_DFS_END(dev->orig_node, next, elem); |
| 1924 | } |
| 1925 | lys_node_free(dev->orig_node, NULL, 0); |
| 1926 | } else { |
| 1927 | /* it's just a shallow copy, freeing one node */ |
| 1928 | dev->orig_node->module = module; |
| 1929 | lys_node_free(dev->orig_node, NULL, 1); |
| 1930 | } |
| 1931 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1932 | for (i = 0; i < dev->deviate_size; i++) { |
| 1933 | lydict_remove(ctx, dev->deviate[i].dflt); |
| 1934 | lydict_remove(ctx, dev->deviate[i].units); |
| 1935 | |
| 1936 | if (dev->deviate[i].mod == LY_DEVIATE_DEL) { |
| 1937 | for (j = 0; j < dev->deviate[i].must_size; j++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1938 | lys_restr_free(ctx, &dev->deviate[i].must[j]); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1939 | } |
| 1940 | free(dev->deviate[i].must); |
| 1941 | |
| 1942 | for (j = 0; j < dev->deviate[i].unique_size; j++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1943 | for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) { |
| 1944 | lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]); |
| 1945 | } |
Michal Vasko | 0238ccf | 2016-03-07 15:02:18 +0100 | [diff] [blame] | 1946 | free(dev->deviate[i].unique[j].expr); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1947 | } |
| 1948 | free(dev->deviate[i].unique); |
| 1949 | } |
| 1950 | } |
| 1951 | free(dev->deviate); |
| 1952 | } |
| 1953 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1954 | static void |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1955 | 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] | 1956 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1957 | int i, j; |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1958 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1959 | for (i = 0; i < uses->refine_size; i++) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1960 | lydict_remove(ctx, uses->refine[i].target_name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1961 | lydict_remove(ctx, uses->refine[i].dsc); |
| 1962 | lydict_remove(ctx, uses->refine[i].ref); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1963 | |
Michal Vasko | a275c0a | 2015-09-24 09:55:42 +0200 | [diff] [blame] | 1964 | for (j = 0; j < uses->refine[i].must_size; j++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1965 | lys_restr_free(ctx, &uses->refine[i].must[j]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1966 | } |
| 1967 | free(uses->refine[i].must); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1968 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1969 | if (uses->refine[i].target_type & (LYS_LEAF | LYS_CHOICE)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1970 | lydict_remove(ctx, uses->refine[i].mod.dflt); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1971 | } else if (uses->refine[i].target_type & LYS_CONTAINER) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1972 | lydict_remove(ctx, uses->refine[i].mod.presence); |
| 1973 | } |
| 1974 | } |
| 1975 | free(uses->refine); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1976 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1977 | for (i = 0; i < uses->augment_size; i++) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1978 | lys_augment_free(ctx, &uses->augment[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1979 | } |
| 1980 | free(uses->augment); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1981 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1982 | lys_when_free(ctx, uses->when); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1983 | } |
| 1984 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1985 | void |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1986 | 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] | 1987 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1988 | struct ly_ctx *ctx; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1989 | struct lys_node *sub, *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1990 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1991 | if (!node) { |
| 1992 | return; |
| 1993 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1994 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1995 | assert(node->module); |
| 1996 | assert(node->module->ctx); |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1997 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1998 | ctx = node->module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1999 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2000 | /* remove private object */ |
Mislav Novakovic | b5529e5 | 2016-02-29 11:42:43 +0100 | [diff] [blame] | 2001 | if (node->priv && private_destructor) { |
| 2002 | private_destructor(node, node->priv); |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2003 | } |
| 2004 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2005 | /* common part */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2006 | lydict_remove(ctx, node->name); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2007 | if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 2008 | free(node->features); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2009 | lydict_remove(ctx, node->dsc); |
| 2010 | lydict_remove(ctx, node->ref); |
| 2011 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2012 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2013 | if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2014 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2015 | lys_node_free(sub, private_destructor, 0); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2016 | } |
| 2017 | } |
| 2018 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2019 | /* specific part */ |
| 2020 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2021 | case LYS_CONTAINER: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2022 | lys_container_free(ctx, (struct lys_node_container *)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_CHOICE: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2025 | lys_when_free(ctx, ((struct lys_node_choice *)node)->when); |
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_LEAF: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2028 | lys_leaf_free(ctx, (struct lys_node_leaf *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2029 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2030 | case LYS_LEAFLIST: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2031 | lys_leaflist_free(ctx, (struct lys_node_leaflist *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2032 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2033 | case LYS_LIST: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2034 | lys_list_free(ctx, (struct lys_node_list *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2035 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2036 | case LYS_ANYXML: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2037 | lys_anyxml_free(ctx, (struct lys_node_anyxml *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2038 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2039 | case LYS_USES: |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2040 | lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2041 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2042 | case LYS_CASE: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2043 | lys_when_free(ctx, ((struct lys_node_case *)node)->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2044 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2045 | case LYS_AUGMENT: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2046 | /* do nothing */ |
| 2047 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2048 | case LYS_GROUPING: |
| 2049 | case LYS_RPC: |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2050 | case LYS_NOTIF: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2051 | lys_grp_free(ctx, (struct lys_node_grp *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2052 | break; |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2053 | |
| 2054 | case LYS_INPUT: |
| 2055 | case LYS_OUTPUT: |
| 2056 | lys_rpc_inout_free(ctx, (struct lys_node_rpc_inout *)node); |
| 2057 | break; |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 2058 | case LYS_UNKNOWN: |
| 2059 | LOGINT; |
| 2060 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2061 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2062 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2063 | /* again common part */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2064 | lys_node_unlink(node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2065 | free(node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2066 | } |
| 2067 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2068 | const struct lys_module * |
| 2069 | 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] | 2070 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2071 | const struct lys_module *main_module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2072 | int i; |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2073 | |
Michal Vasko | a7789a8 | 2016-02-11 15:42:55 +0100 | [diff] [blame] | 2074 | assert(!prefix || !name); |
| 2075 | |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2076 | if (prefix && !pref_len) { |
| 2077 | pref_len = strlen(prefix); |
| 2078 | } |
| 2079 | if (name && !name_len) { |
| 2080 | name_len = strlen(name); |
| 2081 | } |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 2082 | |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 2083 | main_module = lys_main_module(module); |
Michal Vasko | a7789a8 | 2016-02-11 15:42:55 +0100 | [diff] [blame] | 2084 | |
| 2085 | /* module own prefix, submodule own prefix, (sub)module own name */ |
| 2086 | if ((!prefix || (!module->type && !strncmp(main_module->prefix, prefix, pref_len) && !main_module->prefix[pref_len]) |
| 2087 | || (module->type && !strncmp(module->prefix, prefix, pref_len) && !module->prefix[pref_len])) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2088 | && (!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] | 2089 | return main_module; |
Michal Vasko | d8da86b | 2015-10-21 13:35:37 +0200 | [diff] [blame] | 2090 | } |
| 2091 | |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 2092 | for (i = 0; i < module->imp_size; ++i) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2093 | if ((!prefix || (!strncmp(module->imp[i].prefix, prefix, pref_len) && !module->imp[i].prefix[pref_len])) |
| 2094 | && (!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] | 2095 | return module->imp[i].module; |
| 2096 | } |
| 2097 | } |
| 2098 | |
| 2099 | return NULL; |
| 2100 | } |
| 2101 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2102 | /* free_int_mods - flag whether to free the internal modules as well */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2103 | static void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2104 | 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] | 2105 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2106 | struct ly_ctx *ctx; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2107 | struct lys_node *next, *iter; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2108 | unsigned int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2109 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2110 | assert(module->ctx); |
| 2111 | ctx = module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2112 | |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2113 | /* just free the import array, imported modules will stay in the context */ |
Radek Krejci | 225376f | 2016-02-16 17:36:22 +0100 | [diff] [blame] | 2114 | for (i = 0; i < module->imp_size; i++) { |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 2115 | if (!module->imp[i].external) { |
| 2116 | lydict_remove(ctx, module->imp[i].prefix); |
| 2117 | } |
Radek Krejci | 225376f | 2016-02-16 17:36:22 +0100 | [diff] [blame] | 2118 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2119 | free(module->imp); |
| 2120 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2121 | /* submodules don't have data tree, the data nodes |
| 2122 | * are placed in the main module altogether */ |
| 2123 | if (!module->type) { |
| 2124 | LY_TREE_FOR_SAFE(module->data, next, iter) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2125 | lys_node_free(iter, private_destructor, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2126 | } |
Radek Krejci | 2118196 | 2015-06-30 14:11:00 +0200 | [diff] [blame] | 2127 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2128 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2129 | lydict_remove(ctx, module->dsc); |
| 2130 | lydict_remove(ctx, module->ref); |
| 2131 | lydict_remove(ctx, module->org); |
| 2132 | lydict_remove(ctx, module->contact); |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 2133 | lydict_remove(ctx, module->filepath); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2134 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2135 | /* revisions */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2136 | for (i = 0; i < module->rev_size; i++) { |
| 2137 | lydict_remove(ctx, module->rev[i].dsc); |
| 2138 | lydict_remove(ctx, module->rev[i].ref); |
| 2139 | } |
| 2140 | free(module->rev); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2141 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2142 | /* identities */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2143 | for (i = 0; i < module->ident_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2144 | lys_ident_free(ctx, &module->ident[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2145 | } |
| 2146 | module->ident_size = 0; |
| 2147 | free(module->ident); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2148 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2149 | /* typedefs */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2150 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2151 | lys_tpdf_free(ctx, &module->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2152 | } |
| 2153 | free(module->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2154 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2155 | /* include */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2156 | for (i = 0; i < module->inc_size; i++) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2157 | /* complete submodule free is done only from main module since |
| 2158 | * submodules propagate their includes to the main module */ |
| 2159 | if (!module->type) { |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2160 | lys_submodule_free(module->inc[i].submodule, private_destructor); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2161 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2162 | } |
| 2163 | free(module->inc); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2164 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2165 | /* augment */ |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2166 | for (i = 0; i < module->augment_size; i++) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2167 | lys_augment_free(ctx, &module->augment[i], private_destructor); |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2168 | } |
| 2169 | free(module->augment); |
| 2170 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2171 | /* features */ |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2172 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2173 | lys_feature_free(ctx, &module->features[i]); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2174 | } |
| 2175 | free(module->features); |
| 2176 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2177 | /* deviations */ |
| 2178 | for (i = 0; i < module->deviation_size; i++) { |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2179 | lys_deviation_free(module, &module->deviation[i]); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2180 | } |
| 2181 | free(module->deviation); |
| 2182 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2183 | lydict_remove(ctx, module->name); |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 2184 | lydict_remove(ctx, module->prefix); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2185 | } |
| 2186 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2187 | void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2188 | 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] | 2189 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2190 | if (!submodule) { |
| 2191 | return; |
| 2192 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2193 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2194 | /* common part with struct ly_module */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2195 | module_free_common((struct lys_module *)submodule, private_destructor); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2196 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2197 | /* no specific items to free */ |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2198 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2199 | free(submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2200 | } |
| 2201 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2202 | struct lys_node * |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2203 | 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] | 2204 | uint8_t nacm, struct unres_schema *unres, int shallow) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2205 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2206 | struct lys_node *retval = NULL, *child; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2207 | struct ly_ctx *ctx = module->ctx; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2208 | int i, j, rc; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2209 | |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2210 | struct lys_node_container *cont = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2211 | struct lys_node_container *cont_orig = (struct lys_node_container *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2212 | struct lys_node_choice *choice = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2213 | struct lys_node_choice *choice_orig = (struct lys_node_choice *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2214 | struct lys_node_leaf *leaf = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2215 | struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2216 | struct lys_node_leaflist *llist = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2217 | struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2218 | struct lys_node_list *list = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2219 | struct lys_node_list *list_orig = (struct lys_node_list *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2220 | struct lys_node_anyxml *anyxml = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2221 | struct lys_node_anyxml *anyxml_orig = (struct lys_node_anyxml *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2222 | struct lys_node_uses *uses = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2223 | struct lys_node_uses *uses_orig = (struct lys_node_uses *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2224 | struct lys_node_grp *grp = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2225 | struct lys_node_grp *grp_orig = (struct lys_node_grp *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2226 | struct lys_node_rpc *rpc = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2227 | struct lys_node_rpc *rpc_orig = (struct lys_node_rpc *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2228 | struct lys_node_rpc_inout *io = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2229 | 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] | 2230 | struct lys_node_rpc *ntf = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2231 | struct lys_node_rpc *ntf_orig = (struct lys_node_rpc *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2232 | struct lys_node_case *cs = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2233 | struct lys_node_case *cs_orig = (struct lys_node_case *)node; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2234 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2235 | /* we cannot just duplicate memory since the strings are stored in |
| 2236 | * dictionary and we need to update dictionary counters. |
| 2237 | */ |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2238 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2239 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2240 | case LYS_CONTAINER: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2241 | cont = calloc(1, sizeof *cont); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2242 | retval = (struct lys_node *)cont; |
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_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2246 | choice = calloc(1, sizeof *choice); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2247 | retval = (struct lys_node *)choice; |
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_LEAF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2251 | leaf = calloc(1, sizeof *leaf); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2252 | retval = (struct lys_node *)leaf; |
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_LEAFLIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2256 | llist = calloc(1, sizeof *llist); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2257 | retval = (struct lys_node *)llist; |
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_LIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2261 | list = calloc(1, sizeof *list); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2262 | retval = (struct lys_node *)list; |
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_ANYXML: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2266 | anyxml = calloc(1, sizeof *anyxml); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2267 | retval = (struct lys_node *)anyxml; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2268 | break; |
| 2269 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2270 | case LYS_USES: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2271 | uses = calloc(1, sizeof *uses); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2272 | retval = (struct lys_node *)uses; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2273 | break; |
| 2274 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2275 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2276 | cs = calloc(1, sizeof *cs); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2277 | retval = (struct lys_node *)cs; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2278 | break; |
| 2279 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2280 | case LYS_GROUPING: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2281 | grp = calloc(1, sizeof *grp); |
| 2282 | retval = (struct lys_node *)grp; |
| 2283 | break; |
| 2284 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2285 | case LYS_RPC: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2286 | rpc = calloc(1, sizeof *rpc); |
| 2287 | retval = (struct lys_node *)rpc; |
| 2288 | break; |
| 2289 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2290 | case LYS_INPUT: |
| 2291 | case LYS_OUTPUT: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2292 | io = calloc(1, sizeof *io); |
| 2293 | retval = (struct lys_node *)io; |
| 2294 | break; |
| 2295 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2296 | case LYS_NOTIF: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2297 | ntf = calloc(1, sizeof *ntf); |
| 2298 | retval = (struct lys_node *)ntf; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2299 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2300 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2301 | default: |
Michal Vasko | d23ce59 | 2015-08-06 09:55:37 +0200 | [diff] [blame] | 2302 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2303 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2304 | } |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame] | 2305 | |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2306 | if (!retval) { |
| 2307 | LOGMEM; |
| 2308 | return NULL; |
| 2309 | } |
| 2310 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2311 | /* |
| 2312 | * duplicate generic part of the structure |
| 2313 | */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2314 | retval->name = lydict_insert(ctx, node->name, 0); |
| 2315 | retval->dsc = lydict_insert(ctx, node->dsc, 0); |
| 2316 | retval->ref = lydict_insert(ctx, node->ref, 0); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 2317 | retval->nacm = nacm; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2318 | retval->flags = node->flags; |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2319 | if (!(retval->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2320 | /* set parent's config flag */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2321 | retval->flags |= flags & LYS_CONFIG_MASK; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2322 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2323 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2324 | retval->module = module; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2325 | retval->nodetype = node->nodetype; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2326 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2327 | retval->prev = retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2328 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2329 | retval->features_size = node->features_size; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2330 | retval->features = calloc(retval->features_size, sizeof *retval->features); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2331 | if (!retval->features) { |
| 2332 | LOGMEM; |
| 2333 | goto error; |
| 2334 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2335 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2336 | if (!shallow) { |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2337 | for (i = 0; i < node->features_size; ++i) { |
| 2338 | retval->features[i] = (struct lys_feature *)retval; |
| 2339 | if (unres_schema_dup(module, unres, &node->features[i], UNRES_IFFEAT, &retval->features[i])) { |
| 2340 | retval->features[i] = node->features[i]; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2341 | } |
| 2342 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2343 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2344 | /* connect it to the parent */ |
| 2345 | if (lys_node_addchild(parent, retval->module, retval)) { |
| 2346 | goto error; |
| 2347 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2348 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2349 | /* go recursively */ |
| 2350 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2351 | LY_TREE_FOR(node->child, child) { |
| 2352 | if (!lys_node_dup(module, retval, child, retval->flags, retval->nacm, unres, 0)) { |
| 2353 | goto error; |
| 2354 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2355 | } |
| 2356 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2357 | } else { |
| 2358 | memcpy(retval->features, node->features, retval->features_size * sizeof *retval->features); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2359 | } |
| 2360 | |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2361 | /* |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2362 | * duplicate specific part of the structure |
| 2363 | */ |
| 2364 | switch (node->nodetype) { |
| 2365 | case LYS_CONTAINER: |
| 2366 | if (cont_orig->when) { |
| 2367 | cont->when = lys_when_dup(ctx, cont_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2368 | } |
| 2369 | cont->presence = lydict_insert(ctx, cont_orig->presence, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2370 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2371 | cont->must_size = cont_orig->must_size; |
| 2372 | cont->tpdf_size = cont_orig->tpdf_size; |
| 2373 | |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2374 | cont->must = lys_restr_dup(ctx, cont_orig->must, cont->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2375 | cont->tpdf = lys_tpdf_dup(module, node->parent, cont_orig->tpdf, cont->tpdf_size, unres); |
| 2376 | break; |
| 2377 | |
| 2378 | case LYS_CHOICE: |
| 2379 | if (choice_orig->when) { |
| 2380 | choice->when = lys_when_dup(ctx, choice_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2381 | } |
| 2382 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2383 | if (!shallow) { |
| 2384 | if (choice_orig->dflt) { |
| 2385 | rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0, LYS_ANYXML |
| 2386 | | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST |
| 2387 | | LYS_LIST, (const struct lys_node **)&choice->dflt); |
| 2388 | if (rc) { |
| 2389 | if (rc == EXIT_FAILURE) { |
| 2390 | LOGINT; |
| 2391 | } |
| 2392 | goto error; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2393 | } |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2394 | } else { |
| 2395 | /* useless to check return value, we don't know whether |
| 2396 | * there really wasn't any default defined or it just hasn't |
| 2397 | * been resolved, we just hope for the best :) |
| 2398 | */ |
| 2399 | unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2400 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2401 | } else { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2402 | choice->dflt = choice_orig->dflt; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2403 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2404 | break; |
| 2405 | |
| 2406 | case LYS_LEAF: |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 2407 | if (lys_type_dup(module, node->parent, &(leaf->type), &(leaf_orig->type), unres)) { |
| 2408 | goto error; |
| 2409 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2410 | leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0); |
| 2411 | |
| 2412 | if (leaf_orig->dflt) { |
| 2413 | leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0); |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2414 | 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] | 2415 | goto error; |
| 2416 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | leaf->must_size = leaf_orig->must_size; |
| 2420 | leaf->must = lys_restr_dup(ctx, leaf_orig->must, leaf->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2421 | |
| 2422 | if (leaf_orig->when) { |
| 2423 | leaf->when = lys_when_dup(ctx, leaf_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2424 | } |
| 2425 | break; |
| 2426 | |
| 2427 | case LYS_LEAFLIST: |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 2428 | if (lys_type_dup(module, node->parent, &(llist->type), &(llist_orig->type), unres)) { |
| 2429 | goto error; |
| 2430 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2431 | llist->units = lydict_insert(module->ctx, llist_orig->units, 0); |
| 2432 | |
| 2433 | llist->min = llist_orig->min; |
| 2434 | llist->max = llist_orig->max; |
| 2435 | |
| 2436 | llist->must_size = llist_orig->must_size; |
| 2437 | llist->must = lys_restr_dup(ctx, llist_orig->must, llist->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2438 | |
| 2439 | if (llist_orig->when) { |
| 2440 | llist->when = lys_when_dup(ctx, llist_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2441 | } |
| 2442 | break; |
| 2443 | |
| 2444 | case LYS_LIST: |
| 2445 | list->min = list_orig->min; |
| 2446 | list->max = list_orig->max; |
| 2447 | |
| 2448 | list->must_size = list_orig->must_size; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2449 | list->must = lys_restr_dup(ctx, list_orig->must, list->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2450 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2451 | list->tpdf_size = list_orig->tpdf_size; |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 2452 | 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] | 2453 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2454 | list->keys_size = list_orig->keys_size; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2455 | if (list->keys_size) { |
| 2456 | list->keys = calloc(list->keys_size, sizeof *list->keys); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2457 | if (!list->keys) { |
| 2458 | LOGMEM; |
| 2459 | goto error; |
| 2460 | } |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 2461 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2462 | if (!shallow) { |
| 2463 | /* we managed to resolve it before, resolve it again manually */ |
| 2464 | if (list_orig->keys[0]) { |
| 2465 | for (i = 0; i < list->keys_size; ++i) { |
| 2466 | rc = lys_get_sibling(list->child, lys_node_module(retval)->name, 0, list_orig->keys[i]->name, 0, LYS_LEAF, |
| 2467 | (const struct lys_node **)&list->keys[i]); |
| 2468 | if (rc) { |
| 2469 | if (rc == EXIT_FAILURE) { |
| 2470 | LOGINT; |
| 2471 | } |
| 2472 | goto error; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2473 | } |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2474 | } |
| 2475 | /* it was not resolved yet, add unres copy */ |
| 2476 | } else { |
| 2477 | if (unres_schema_dup(module, unres, list_orig, UNRES_LIST_KEYS, list)) { |
| 2478 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2479 | goto error; |
| 2480 | } |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2481 | } |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 2482 | } else { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2483 | memcpy(list->keys, list_orig->keys, list->keys_size * sizeof *list->keys); |
Radek Krejci | a01e543 | 2015-06-16 10:35:25 +0200 | [diff] [blame] | 2484 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2485 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2486 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2487 | list->unique_size = list_orig->unique_size; |
| 2488 | list->unique = malloc(list->unique_size * sizeof *list->unique); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2489 | if (!list->unique) { |
| 2490 | LOGMEM; |
| 2491 | goto error; |
| 2492 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2493 | for (i = 0; i < list->unique_size; ++i) { |
| 2494 | list->unique[i].expr_size = list_orig->unique[i].expr_size; |
| 2495 | 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] | 2496 | if (!list->unique[i].expr) { |
| 2497 | LOGMEM; |
| 2498 | goto error; |
| 2499 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2500 | for (j = 0; j < list->unique[i].expr_size; j++) { |
| 2501 | list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0); |
| 2502 | |
| 2503 | /* if it stays in unres list, duplicate it also there */ |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 2504 | 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] | 2505 | } |
| 2506 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2507 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2508 | if (list_orig->when) { |
| 2509 | list->when = lys_when_dup(ctx, list_orig->when); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2510 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2511 | break; |
| 2512 | |
| 2513 | case LYS_ANYXML: |
| 2514 | anyxml->must_size = anyxml_orig->must_size; |
| 2515 | anyxml->must = lys_restr_dup(ctx, anyxml_orig->must, anyxml->must_size); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2516 | |
| 2517 | if (anyxml_orig->when) { |
| 2518 | anyxml->when = lys_when_dup(ctx, anyxml_orig->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2519 | } |
| 2520 | break; |
| 2521 | |
| 2522 | case LYS_USES: |
| 2523 | uses->grp = uses_orig->grp; |
| 2524 | |
| 2525 | if (uses_orig->when) { |
| 2526 | uses->when = lys_when_dup(ctx, uses_orig->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | uses->refine_size = uses_orig->refine_size; |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 2530 | 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] | 2531 | uses->augment_size = uses_orig->augment_size; |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2532 | if (!shallow) { |
| 2533 | uses->augment = lys_augment_dup(module, (struct lys_node *)uses, uses_orig->augment, uses_orig->augment_size); |
| 2534 | if (!uses->child) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2535 | if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL) == -1) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2536 | goto error; |
| 2537 | } |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2538 | } |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2539 | } else { |
| 2540 | memcpy(uses->augment, uses_orig->augment, uses->augment_size * sizeof *uses->augment); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2541 | } |
| 2542 | break; |
| 2543 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2544 | case LYS_CASE: |
| 2545 | if (cs_orig->when) { |
| 2546 | cs->when = lys_when_dup(ctx, cs_orig->when); |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2547 | } |
| 2548 | break; |
| 2549 | |
| 2550 | case LYS_GROUPING: |
| 2551 | grp->tpdf_size = grp_orig->tpdf_size; |
| 2552 | grp->tpdf = lys_tpdf_dup(module, node->parent, grp_orig->tpdf, grp->tpdf_size, unres); |
| 2553 | break; |
| 2554 | |
| 2555 | case LYS_RPC: |
| 2556 | rpc->tpdf_size = rpc_orig->tpdf_size; |
| 2557 | rpc->tpdf = lys_tpdf_dup(module, node->parent, rpc_orig->tpdf, rpc->tpdf_size, unres); |
| 2558 | break; |
| 2559 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2560 | case LYS_INPUT: |
| 2561 | case LYS_OUTPUT: |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2562 | io->tpdf_size = io_orig->tpdf_size; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2563 | io->tpdf = lys_tpdf_dup(module, node->parent, io_orig->tpdf, io->tpdf_size, unres); |
| 2564 | break; |
| 2565 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2566 | case LYS_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2567 | ntf->tpdf_size = ntf_orig->tpdf_size; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2568 | 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] | 2569 | break; |
| 2570 | |
| 2571 | default: |
| 2572 | /* LY_NODE_AUGMENT */ |
| 2573 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2574 | goto error; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2575 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2576 | |
| 2577 | return retval; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2578 | |
| 2579 | error: |
| 2580 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2581 | lys_node_free(retval, NULL, 0); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2582 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2583 | } |
| 2584 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2585 | void |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2586 | lys_node_switch(struct lys_node *dst, struct lys_node *src) |
| 2587 | { |
| 2588 | struct lys_node *child; |
| 2589 | |
| 2590 | assert((dst->module == src->module) && (dst->name == src->name) && (dst->nodetype == src->nodetype)); |
| 2591 | |
| 2592 | /* sibling next */ |
| 2593 | if (dst->prev != dst) { |
| 2594 | dst->prev->next = src; |
| 2595 | } |
| 2596 | |
| 2597 | /* sibling prev */ |
| 2598 | if (dst->next) { |
| 2599 | dst->next->prev = src; |
| 2600 | } |
| 2601 | |
| 2602 | /* parent child prev */ |
| 2603 | if (!dst->next && dst->parent) { |
| 2604 | dst->parent->child->prev = src; |
| 2605 | } |
| 2606 | |
| 2607 | /* next */ |
| 2608 | src->next = dst->next; |
| 2609 | dst->next = NULL; |
| 2610 | |
| 2611 | /* prev */ |
| 2612 | if (dst->prev != dst) { |
| 2613 | src->prev = dst->prev; |
| 2614 | } |
| 2615 | dst->prev = dst; |
| 2616 | |
| 2617 | /* parent child */ |
| 2618 | if (dst->parent && (dst->parent->child == dst)) { |
| 2619 | dst->parent->child = src; |
| 2620 | } |
| 2621 | |
| 2622 | /* parent */ |
| 2623 | src->parent = dst->parent; |
| 2624 | dst->parent = NULL; |
| 2625 | |
| 2626 | /* child parent */ |
| 2627 | LY_TREE_FOR(dst->child, child) { |
| 2628 | if (child->parent == dst) { |
| 2629 | child->parent = src; |
| 2630 | } |
| 2631 | } |
| 2632 | |
| 2633 | /* child */ |
| 2634 | src->child = dst->child; |
| 2635 | dst->child = NULL; |
| 2636 | } |
| 2637 | |
| 2638 | void |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2639 | 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] | 2640 | { |
| 2641 | struct ly_ctx *ctx; |
| 2642 | int i; |
| 2643 | |
| 2644 | if (!module) { |
| 2645 | return; |
| 2646 | } |
| 2647 | |
| 2648 | /* remove schema from the context */ |
| 2649 | ctx = module->ctx; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2650 | if (remove_from_ctx && ctx->models.used) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2651 | for (i = 0; i < ctx->models.used; i++) { |
| 2652 | if (ctx->models.list[i] == module) { |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2653 | /* move all the models to not change the order in the list */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2654 | ctx->models.used--; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2655 | 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] | 2656 | ctx->models.list[ctx->models.used] = NULL; |
| 2657 | /* we are done */ |
| 2658 | break; |
| 2659 | } |
| 2660 | } |
| 2661 | } |
| 2662 | |
| 2663 | /* common part with struct ly_submodule */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2664 | module_free_common(module, private_destructor); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2665 | |
| 2666 | /* specific items to free */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2667 | lydict_remove(ctx, module->ns); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2668 | |
| 2669 | free(module); |
| 2670 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2671 | |
| 2672 | /* |
| 2673 | * op: 1 - enable, 0 - disable |
| 2674 | */ |
| 2675 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2676 | 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] | 2677 | { |
| 2678 | int all = 0; |
| 2679 | int i, j, k; |
| 2680 | |
| 2681 | if (!module || !name || !strlen(name)) { |
| 2682 | return EXIT_FAILURE; |
| 2683 | } |
| 2684 | |
| 2685 | if (!strcmp(name, "*")) { |
| 2686 | /* enable all */ |
| 2687 | all = 1; |
| 2688 | } |
| 2689 | |
| 2690 | /* module itself */ |
| 2691 | for (i = 0; i < module->features_size; i++) { |
| 2692 | if (all || !strcmp(module->features[i].name, name)) { |
| 2693 | if (op) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2694 | module->features[i].flags |= LYS_FENABLED; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2695 | /* enable referenced features (recursion) */ |
| 2696 | for (k = 0; k < module->features[i].features_size; k++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2697 | lys_features_change(module->features[i].features[k]->module, |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2698 | module->features[i].features[k]->name, op); |
| 2699 | } |
| 2700 | } else { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2701 | module->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 | /* submodules */ |
| 2710 | for (j = 0; j < module->inc_size; j++) { |
| 2711 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
| 2712 | if (all || !strcmp(module->inc[j].submodule->features[i].name, name)) { |
| 2713 | if (op) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2714 | module->inc[j].submodule->features[i].flags |= LYS_FENABLED; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2715 | } else { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2716 | module->inc[j].submodule->features[i].flags &= ~LYS_FENABLED; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2717 | } |
| 2718 | if (!all) { |
| 2719 | return EXIT_SUCCESS; |
| 2720 | } |
| 2721 | } |
| 2722 | } |
| 2723 | } |
| 2724 | |
| 2725 | if (all) { |
| 2726 | return EXIT_SUCCESS; |
| 2727 | } else { |
| 2728 | return EXIT_FAILURE; |
| 2729 | } |
| 2730 | } |
| 2731 | |
| 2732 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2733 | lys_features_enable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2734 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2735 | return lys_features_change(module, feature, 1); |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2739 | lys_features_disable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2740 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2741 | return lys_features_change(module, feature, 0); |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2742 | } |
| 2743 | |
| 2744 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2745 | lys_features_state(const struct lys_module *module, const char *feature) |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2746 | { |
| 2747 | int i, j; |
| 2748 | |
| 2749 | if (!module || !feature) { |
| 2750 | return -1; |
| 2751 | } |
| 2752 | |
| 2753 | /* search for the specified feature */ |
| 2754 | /* module itself */ |
| 2755 | for (i = 0; i < module->features_size; i++) { |
| 2756 | if (!strcmp(feature, module->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2757 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2758 | return 1; |
| 2759 | } else { |
| 2760 | return 0; |
| 2761 | } |
| 2762 | } |
| 2763 | } |
| 2764 | |
| 2765 | /* submodules */ |
| 2766 | for (j = 0; j < module->inc_size; j++) { |
| 2767 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
| 2768 | if (!strcmp(feature, module->inc[j].submodule->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2769 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2770 | return 1; |
| 2771 | } else { |
| 2772 | return 0; |
| 2773 | } |
| 2774 | } |
| 2775 | } |
| 2776 | } |
| 2777 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2778 | /* feature definition not found */ |
| 2779 | return -1; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2780 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2781 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2782 | API const char ** |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2783 | lys_features_list(const struct lys_module *module, uint8_t **states) |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2784 | { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2785 | const char **result = NULL; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2786 | int i, j; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2787 | unsigned int count; |
| 2788 | |
| 2789 | if (!module) { |
| 2790 | return NULL; |
| 2791 | } |
| 2792 | |
| 2793 | count = module->features_size; |
| 2794 | for (i = 0; i < module->inc_size; i++) { |
| 2795 | count += module->inc[i].submodule->features_size; |
| 2796 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2797 | result = malloc((count + 1) * sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2798 | if (!result) { |
| 2799 | LOGMEM; |
| 2800 | return NULL; |
| 2801 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2802 | if (states) { |
| 2803 | *states = malloc((count + 1) * sizeof **states); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2804 | if (!(*states)) { |
| 2805 | LOGMEM; |
| 2806 | free(result); |
| 2807 | return NULL; |
| 2808 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2809 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2810 | count = 0; |
| 2811 | |
| 2812 | /* module itself */ |
| 2813 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2814 | result[count] = module->features[i].name; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2815 | if (states) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2816 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2817 | (*states)[count] = 1; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2818 | } else { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2819 | (*states)[count] = 0; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2820 | } |
| 2821 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2822 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2823 | } |
| 2824 | |
| 2825 | /* submodules */ |
| 2826 | for (j = 0; j < module->inc_size; j++) { |
| 2827 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2828 | result[count] = module->inc[j].submodule->features[i].name; |
Radek Krejci | 374b94e | 2015-08-13 09:44:22 +0200 | [diff] [blame] | 2829 | if (states) { |
| 2830 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
| 2831 | (*states)[count] = 1; |
| 2832 | } else { |
| 2833 | (*states)[count] = 0; |
| 2834 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2835 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2836 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2837 | } |
| 2838 | } |
| 2839 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2840 | /* terminating NULL byte */ |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2841 | result[count] = NULL; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2842 | |
| 2843 | return result; |
| 2844 | } |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 2845 | |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 2846 | API struct lys_module * |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 2847 | lys_node_module(const struct lys_node *node) |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2848 | { |
| 2849 | return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module; |
| 2850 | } |
| 2851 | |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 2852 | API struct lys_module * |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 2853 | lys_main_module(const struct lys_module *module) |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 2854 | { |
| 2855 | return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module); |
| 2856 | } |
| 2857 | |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 2858 | API struct lys_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2859 | lys_parent(const struct lys_node *node) |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 2860 | { |
| 2861 | if (!node || !node->parent) { |
| 2862 | return NULL; |
| 2863 | } |
| 2864 | |
| 2865 | if (node->parent->nodetype == LYS_AUGMENT) { |
| 2866 | return ((struct lys_node_augment *)node->parent)->target; |
| 2867 | } |
| 2868 | |
| 2869 | return node->parent; |
| 2870 | } |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2871 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2872 | API void * |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2873 | lys_set_private(const struct lys_node *node, void *priv) |
| 2874 | { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2875 | void *prev; |
| 2876 | |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2877 | if (!node) { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2878 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 2879 | return NULL; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2880 | } |
| 2881 | |
Mislav Novakovic | b5529e5 | 2016-02-29 11:42:43 +0100 | [diff] [blame] | 2882 | prev = node->priv; |
| 2883 | ((struct lys_node *)node)->priv = priv; |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2884 | |
| 2885 | return prev; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2886 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2887 | |
| 2888 | static void |
| 2889 | lys_switch_deviation(struct lys_deviation *dev, struct lys_module *dev_module) |
| 2890 | { |
| 2891 | int ret; |
| 2892 | char *parent_path; |
| 2893 | struct lys_node *target; |
| 2894 | const struct lys_module *target_module; |
| 2895 | |
| 2896 | target_module = lys_get_import_module(dev_module, NULL, 0, dev->target_name + 1, |
| 2897 | strcspn(dev->target_name, ":") - 1); |
| 2898 | |
| 2899 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 2900 | if (dev->orig_node) { |
| 2901 | /* removing not-supported deviation ... */ |
| 2902 | if (strrchr(dev->target_name, '/') != dev->target_name) { |
| 2903 | /* ... from a parent */ |
| 2904 | parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name); |
| 2905 | |
| 2906 | target = NULL; |
| 2907 | ret = resolve_augment_schema_nodeid(parent_path, NULL, target_module, (const struct lys_node **)&target); |
| 2908 | free(parent_path); |
| 2909 | if (ret || !target) { |
| 2910 | LOGINT; |
| 2911 | return; |
| 2912 | } |
| 2913 | |
| 2914 | lys_node_addchild(target, NULL, dev->orig_node); |
| 2915 | } else { |
| 2916 | /* ... from top-level data */ |
| 2917 | lys_node_addchild(NULL, (struct lys_module *)target_module, dev->orig_node); |
| 2918 | } |
| 2919 | |
| 2920 | dev->orig_node = NULL; |
| 2921 | } else { |
| 2922 | /* adding not-supported deviation */ |
| 2923 | target = NULL; |
| 2924 | ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, (const struct lys_node **)&target); |
| 2925 | if (ret || !target) { |
| 2926 | LOGINT; |
| 2927 | return; |
| 2928 | } |
| 2929 | |
| 2930 | lys_node_unlink(target); |
| 2931 | dev->orig_node = target; |
| 2932 | } |
| 2933 | } else { |
| 2934 | target = NULL; |
| 2935 | ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, (const struct lys_node **)&target); |
| 2936 | if (ret || !target) { |
| 2937 | LOGINT; |
| 2938 | return; |
| 2939 | } |
| 2940 | |
| 2941 | lys_node_switch(target, dev->orig_node); |
| 2942 | dev->orig_node = target; |
| 2943 | } |
| 2944 | } |
| 2945 | |
| 2946 | void |
| 2947 | lys_deviation_add_ext_imports(struct lys_module *dev_target_module, struct lys_module *dev_module) |
| 2948 | { |
| 2949 | int i, j; |
| 2950 | |
| 2951 | /* mark the target module as deviated */ |
| 2952 | dev_target_module->deviated = 1; |
| 2953 | |
| 2954 | /* copy our imports to the deviated module (deviations may need them to work) */ |
| 2955 | for (i = 0; i < dev_module->imp_size; ++i) { |
| 2956 | for (j = 0; j < dev_target_module->imp_size; ++j) { |
| 2957 | if (dev_module->imp[i].module == dev_target_module->imp[j].module) { |
| 2958 | break; |
| 2959 | } |
| 2960 | } |
| 2961 | |
| 2962 | if (j < dev_target_module->imp_size) { |
| 2963 | /* import is already there */ |
| 2964 | continue; |
| 2965 | } |
| 2966 | |
| 2967 | /* copy the import, mark it as external */ |
| 2968 | ++dev_target_module->imp_size; |
| 2969 | dev_target_module->imp = ly_realloc(dev_target_module->imp, dev_target_module->imp_size * sizeof *dev_target_module->imp); |
| 2970 | if (!dev_target_module->imp) { |
| 2971 | LOGMEM; |
| 2972 | return; |
| 2973 | } |
| 2974 | dev_target_module->imp[dev_target_module->imp_size - 1].module = dev_module->imp[i].module; |
| 2975 | dev_target_module->imp[dev_target_module->imp_size - 1].prefix = lydict_insert(dev_module->ctx, dev_module->imp[i].prefix, 0); |
| 2976 | memcpy(dev_target_module->imp[dev_target_module->imp_size - 1].rev, dev_module->imp[i].rev, LY_REV_SIZE); |
| 2977 | dev_target_module->imp[dev_target_module->imp_size - 1].external = 1; |
| 2978 | } |
| 2979 | |
| 2980 | /* copy ourselves to the deviated module as a special import (if we haven't yet, there could be more deviations of the same module) */ |
| 2981 | for (i = 0; i < dev_target_module->imp_size; ++i) { |
| 2982 | if (dev_target_module->imp[i].module == dev_module) { |
| 2983 | break; |
| 2984 | } |
| 2985 | } |
| 2986 | |
| 2987 | if (i == dev_target_module->imp_size) { |
| 2988 | ++dev_target_module->imp_size; |
| 2989 | dev_target_module->imp = ly_realloc(dev_target_module->imp, dev_target_module->imp_size * sizeof *dev_target_module->imp); |
| 2990 | if (!dev_target_module->imp) { |
| 2991 | LOGMEM; |
| 2992 | return; |
| 2993 | } |
| 2994 | dev_target_module->imp[dev_target_module->imp_size - 1].module = dev_module; |
| 2995 | dev_target_module->imp[dev_target_module->imp_size - 1].prefix = lydict_insert(dev_module->ctx, dev_module->prefix, 0); |
| 2996 | if (dev_module->rev_size) { |
| 2997 | memcpy(dev_target_module->imp[dev_target_module->imp_size - 1].rev, dev_module->rev[0].date, LY_REV_SIZE); |
| 2998 | } else { |
| 2999 | memset(dev_target_module->imp[dev_target_module->imp_size - 1].rev, 0, LY_REV_SIZE); |
| 3000 | } |
| 3001 | dev_target_module->imp[dev_target_module->imp_size - 1].external = 2; |
| 3002 | } else { |
| 3003 | /* it could have been added by another deviating module that imported this deviating module */ |
| 3004 | dev_target_module->imp[i].external = 2; |
| 3005 | } |
| 3006 | } |
| 3007 | |
| 3008 | /* temporarily removes or applies deviations, updates module deviation flag accordingly */ |
| 3009 | void |
| 3010 | lys_switch_deviations(struct lys_module *module) |
| 3011 | { |
| 3012 | uint8_t i, j, changes = 0; |
| 3013 | |
| 3014 | for (i = 0; i < module->imp_size; ++i) { |
| 3015 | if (module->imp[i].external == 2) { |
| 3016 | for (j = 0; j < module->imp[i].module->deviation_size; ++j) { |
| 3017 | lys_switch_deviation(&module->imp[i].module->deviation[j], module->imp[i].module); |
| 3018 | } |
| 3019 | |
| 3020 | changes = 1; |
| 3021 | } |
| 3022 | } |
| 3023 | |
| 3024 | if (changes) { |
| 3025 | if (module->deviated) { |
| 3026 | module->deviated = 0; |
| 3027 | } else { |
| 3028 | module->deviated = 1; |
| 3029 | } |
| 3030 | } |
| 3031 | } |
| 3032 | |
| 3033 | /* not needed currently, but tested and working */ |
| 3034 | #if 0 |
| 3035 | |
| 3036 | void |
| 3037 | lys_sub_module_apply_devs_augs(struct lys_module *module) |
| 3038 | { |
| 3039 | int i; |
| 3040 | struct lys_node_augment *aug; |
| 3041 | struct lys_node *last; |
| 3042 | |
| 3043 | /* re-apply deviations */ |
| 3044 | for (i = 0; i < module->deviation_size; ++i) { |
| 3045 | lys_switch_deviation(&module->deviation[i], module); |
| 3046 | assert(module->deviation[i].orig_node); |
| 3047 | lys_deviation_add_ext_imports(lys_node_module(module->deviation[i].orig_node), module); |
| 3048 | } |
| 3049 | |
| 3050 | /* re-apply augments */ |
| 3051 | for (i = 0; i < module->augment_size; ++i) { |
| 3052 | aug = &module->augment[i]; |
| 3053 | assert(aug->target); |
| 3054 | |
| 3055 | /* reconnect augmenting data into the target - add them to the target child list */ |
| 3056 | if (aug->target->child) { |
| 3057 | last = aug->target->child->prev; |
| 3058 | last->next = aug->child; |
| 3059 | aug->target->child->prev = aug->child->prev; |
| 3060 | aug->child->prev = last; |
| 3061 | } else { |
| 3062 | aug->target->child = aug->child; |
| 3063 | } |
| 3064 | } |
| 3065 | } |
| 3066 | |
| 3067 | #endif |
| 3068 | |
| 3069 | void |
| 3070 | lys_sub_module_remove_devs_augs(struct lys_module *module) |
| 3071 | { |
| 3072 | int i, j, flag; |
| 3073 | struct lys_node *last, *elem; |
| 3074 | struct lys_module *orig_mod; |
| 3075 | |
| 3076 | /* remove applied deviations */ |
| 3077 | for (i = 0; i < module->deviation_size; ++i) { |
| 3078 | lys_switch_deviation(&module->deviation[i], module); |
| 3079 | |
| 3080 | /* remove our deviation import, clear deviated flag is possible */ |
| 3081 | orig_mod = lys_node_module(module->deviation[i].orig_node); |
| 3082 | flag = 0; |
| 3083 | for (j = 0; j < orig_mod->imp_size; ++j) { |
| 3084 | if (orig_mod->imp[j].external == 2) { |
| 3085 | if (orig_mod->imp[j].module == lys_main_module(module)) { |
| 3086 | /* our deviation import, remove it */ |
| 3087 | --orig_mod->imp_size; |
| 3088 | if (j < orig_mod->imp_size) { |
| 3089 | memcpy(&orig_mod->imp[j], &orig_mod->imp[j + 1], (orig_mod->imp_size - j) * sizeof *orig_mod->imp); |
| 3090 | } |
| 3091 | --j; |
| 3092 | } else { |
| 3093 | /* some other deviation, we cannot clear the deviated flag */ |
| 3094 | flag = 1; |
| 3095 | } |
| 3096 | } |
| 3097 | } |
| 3098 | if (!flag) { |
| 3099 | /* it's safe to clear the deviated flag */ |
| 3100 | orig_mod->deviated = 0; |
| 3101 | } |
| 3102 | } |
| 3103 | |
| 3104 | /* remove applied augments */ |
| 3105 | for (i = 0; i < module->augment_size; ++i) { |
| 3106 | assert(module->augment[i].target); |
| 3107 | |
| 3108 | elem = module->augment[i].child; |
| 3109 | if (elem) { |
| 3110 | LY_TREE_FOR(elem, last) { |
| 3111 | if (!last->next || (last->next->parent != (struct lys_node *)&module->augment[i])) { |
| 3112 | break; |
| 3113 | } |
| 3114 | } |
| 3115 | /* elem is first augment child, last is the last child */ |
| 3116 | |
| 3117 | /* parent child ptr */ |
| 3118 | if (module->augment[i].target->child == elem) { |
| 3119 | module->augment[i].target->child = last->next; |
| 3120 | } |
| 3121 | |
| 3122 | /* parent child next ptr */ |
| 3123 | if (elem->prev->next) { |
| 3124 | elem->prev->next = last->next; |
| 3125 | } |
| 3126 | |
| 3127 | /* parent child prev ptr */ |
| 3128 | if (last->next) { |
| 3129 | last->next->prev = elem->prev; |
| 3130 | } else if (module->augment[i].target->child) { |
| 3131 | module->augment[i].target->child->prev = elem->prev; |
| 3132 | } |
| 3133 | |
| 3134 | /* update augment children themselves */ |
| 3135 | elem->prev = last; |
| 3136 | last->next = NULL; |
| 3137 | } |
Michal Vasko | b8f7132 | 2016-05-03 11:39:56 +0200 | [diff] [blame^] | 3138 | |
| 3139 | /* needs to be NULL for lys_augment_free() to free the children */ |
| 3140 | module->augment[i].target = NULL; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3141 | } |
| 3142 | } |
| 3143 | |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 3144 | int |
| 3145 | lys_module_set_implement(struct lys_module *module) |
| 3146 | { |
| 3147 | struct ly_ctx *ctx; |
| 3148 | int i; |
| 3149 | |
| 3150 | if (module->implemented) { |
| 3151 | return EXIT_SUCCESS; |
| 3152 | } |
| 3153 | |
| 3154 | ctx = module->ctx; |
| 3155 | |
| 3156 | for (i = 0; i < ctx->models.used; ++i) { |
| 3157 | if (module == ctx->models.list[i]) { |
| 3158 | continue; |
| 3159 | } |
| 3160 | |
| 3161 | if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) { |
| 3162 | LOGERR(LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name); |
| 3163 | return EXIT_FAILURE; |
| 3164 | } |
| 3165 | } |
| 3166 | |
| 3167 | module->implemented = 1; |
| 3168 | return EXIT_SUCCESS; |
| 3169 | } |
| 3170 | |
| 3171 | int |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3172 | lys_sub_module_set_dev_aug_target_implement(struct lys_module *module) |
| 3173 | { |
| 3174 | int i; |
| 3175 | struct lys_module *trg_mod; |
| 3176 | |
| 3177 | for (i = 0; i < module->deviation_size; ++i) { |
| 3178 | assert(module->deviation[i].orig_node); |
| 3179 | trg_mod = lys_node_module(module->deviation[i].orig_node); |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 3180 | if (lys_module_set_implement(trg_mod)) { |
| 3181 | return EXIT_FAILURE; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3182 | } |
| 3183 | } |
| 3184 | |
| 3185 | for (i = 0; i < module->augment_size; ++i) { |
| 3186 | assert(module->augment[i].target); |
| 3187 | trg_mod = lys_node_module(module->augment[i].target); |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 3188 | if (lys_module_set_implement(trg_mod)) { |
| 3189 | return EXIT_FAILURE; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3190 | } |
| 3191 | } |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 3192 | |
| 3193 | return EXIT_SUCCESS; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | void |
| 3197 | lys_submodule_module_data_free(struct lys_submodule *submodule) |
| 3198 | { |
| 3199 | struct lys_node *next, *elem; |
| 3200 | |
| 3201 | /* remove parsed data */ |
| 3202 | LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) { |
| 3203 | if (elem->module == (struct lys_module *)submodule) { |
| 3204 | lys_node_free(elem, NULL, 0); |
| 3205 | } |
| 3206 | } |
| 3207 | } |