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