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 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 49 | if (!node) { |
| 50 | return NULL; |
| 51 | } |
| 52 | |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 53 | check: |
| 54 | if (node->nodetype != LYS_INPUT && node->nodetype != LYS_OUTPUT) { |
| 55 | /* input/output does not have if-feature, so skip them */ |
| 56 | |
| 57 | /* check local if-features */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 58 | for (i = 0; i < node->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 59 | if (!resolve_iffeature(&node->iffeature[i])) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 60 | return node; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if (!recursive) { |
| 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | /* go through parents */ |
| 70 | if (node->nodetype == LYS_AUGMENT) { |
| 71 | /* go to parent actually means go to the target node */ |
| 72 | node = ((struct lys_node_augment *)node)->target; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 73 | } else if (node->parent) { |
| 74 | node = node->parent; |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 75 | } else { |
| 76 | return NULL; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 77 | } |
| 78 | |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 79 | if (recursive == 2) { |
| 80 | /* continue only if the node cannot have a data instance */ |
| 81 | if (node->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST)) { |
| 82 | return NULL; |
| 83 | } |
| 84 | } |
| 85 | goto check; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 88 | int |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 89 | lys_get_sibling(const struct lys_node *siblings, const char *mod_name, int mod_name_len, const char *name, |
| 90 | int nam_len, LYS_NODE type, const struct lys_node **ret) |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 91 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 92 | const struct lys_node *node, *parent = NULL; |
| 93 | const struct lys_module *mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 94 | const char *node_mod_name; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 95 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 96 | assert(siblings && mod_name && name); |
Michal Vasko | 165dc4a | 2015-10-23 09:44:27 +0200 | [diff] [blame] | 97 | assert(!(type & (LYS_USES | LYS_GROUPING))); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 98 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 99 | /* fill the lengths in case the caller is so indifferent */ |
| 100 | if (!mod_name_len) { |
| 101 | mod_name_len = strlen(mod_name); |
| 102 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 103 | if (!nam_len) { |
| 104 | nam_len = strlen(name); |
| 105 | } |
| 106 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 107 | while (siblings && (siblings->nodetype == LYS_USES)) { |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 108 | siblings = siblings->child; |
| 109 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 110 | if (!siblings) { |
| 111 | /* unresolved uses */ |
| 112 | return EXIT_FAILURE; |
| 113 | } |
| 114 | |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 115 | if (siblings->nodetype == LYS_GROUPING) { |
| 116 | for (node = siblings; (node->nodetype == LYS_GROUPING) && (node->prev != siblings); node = node->prev); |
| 117 | if (node->nodetype == LYS_GROUPING) { |
| 118 | /* we went through all the siblings, only groupings there - no valid sibling */ |
| 119 | return EXIT_FAILURE; |
| 120 | } |
| 121 | /* update siblings to be valid */ |
| 122 | siblings = node; |
| 123 | } |
| 124 | |
Michal Vasko | 4cf7dba | 2016-10-14 09:42:01 +0200 | [diff] [blame] | 125 | /* set parent correctly */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 126 | parent = lys_parent(siblings); |
Michal Vasko | 4cf7dba | 2016-10-14 09:42:01 +0200 | [diff] [blame] | 127 | |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 128 | /* go up all uses */ |
| 129 | while (parent && (parent->nodetype == LYS_USES)) { |
| 130 | parent = lys_parent(parent); |
Michal Vasko | 4cf7dba | 2016-10-14 09:42:01 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 133 | if (!parent) { |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 134 | /* handle situation when there is a top-level uses referencing a foreign grouping */ |
| 135 | for (node = siblings; lys_parent(node) && (node->nodetype == LYS_USES); node = lys_parent(node)); |
| 136 | mod = lys_node_module(node); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 139 | /* try to find the node */ |
| 140 | node = NULL; |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 141 | while ((node = lys_getnext(node, parent, mod, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 142 | if (!type || (node->nodetype & type)) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 143 | /* module name comparison */ |
| 144 | node_mod_name = lys_node_module(node)->name; |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 145 | 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] | 146 | continue; |
| 147 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 148 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 149 | /* direct name check */ |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 150 | 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] | 151 | if (ret) { |
| 152 | *ret = node; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 153 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 154 | return EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 155 | } |
| 156 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | return EXIT_FAILURE; |
| 160 | } |
| 161 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 162 | int |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 163 | lys_get_data_sibling(const struct lys_module *mod, const struct lys_node *siblings, const char *name, int nam_len, |
| 164 | LYS_NODE type, const struct lys_node **ret) |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 165 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 166 | const struct lys_node *node; |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 167 | |
| 168 | assert(siblings && name); |
| 169 | assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))); |
| 170 | |
| 171 | /* find the beginning */ |
| 172 | while (siblings->prev->next) { |
| 173 | siblings = siblings->prev; |
| 174 | } |
| 175 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 176 | if (!mod) { |
| 177 | mod = siblings->module; |
| 178 | } |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 179 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 180 | /* try to find the node */ |
| 181 | node = NULL; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 182 | while ((node = lys_getnext(node, lys_parent(siblings), mod, 0))) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 183 | if (!type || (node->nodetype & type)) { |
| 184 | /* module check */ |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 185 | if (lys_node_module(node) != lys_main_module(mod)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 186 | continue; |
| 187 | } |
| 188 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 189 | /* direct name check */ |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 190 | if (!strncmp(node->name, name, nam_len) && !node->name[nam_len]) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 191 | if (ret) { |
| 192 | *ret = node; |
| 193 | } |
| 194 | return EXIT_SUCCESS; |
| 195 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 196 | } |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | return EXIT_FAILURE; |
| 200 | } |
| 201 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 202 | API const struct lys_node * |
| 203 | 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] | 204 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 205 | const struct lys_node *next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 206 | |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 207 | if (!last) { |
| 208 | /* first call */ |
| 209 | |
| 210 | /* get know where to start */ |
| 211 | if (parent) { |
| 212 | /* schema subtree */ |
| 213 | next = last = parent->child; |
| 214 | } else { |
| 215 | /* top level data */ |
| 216 | assert(module); |
| 217 | next = last = module->data; |
| 218 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 219 | } else if ((last->nodetype == LYS_USES) && (options & LYS_GETNEXT_INTOUSES) && last->child) { |
| 220 | /* continue with uses content */ |
| 221 | next = last->child; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 222 | } else { |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 223 | /* continue after the last returned value */ |
| 224 | next = last->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | repeat: |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 228 | while (next && (next->nodetype == LYS_GROUPING)) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 229 | if (options & LYS_GETNEXT_WITHGROUPING) { |
| 230 | return next; |
| 231 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 232 | next = next->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 235 | if (!next) { /* cover case when parent is augment */ |
| 236 | if (!last || last->parent == parent || lys_parent(last) == parent) { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 237 | /* no next element */ |
| 238 | return NULL; |
| 239 | } |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 240 | last = lys_parent(last); |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 241 | next = last->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 242 | goto repeat; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 243 | } else { |
| 244 | last = next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | switch (next->nodetype) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 248 | case LYS_INPUT: |
| 249 | case LYS_OUTPUT: |
| 250 | if (options & LYS_GETNEXT_WITHINOUT) { |
| 251 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 252 | } else if (next->child) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 253 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 254 | } else { |
| 255 | next = next->next; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 256 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 257 | goto repeat; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 258 | |
Michal Vasko | a5835e9 | 2015-10-20 15:07:39 +0200 | [diff] [blame] | 259 | case LYS_CASE: |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 260 | if (options & LYS_GETNEXT_WITHCASE) { |
| 261 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 262 | } else if (next->child) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 263 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 264 | } else { |
| 265 | next = next->next; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 266 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 267 | goto repeat; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 268 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 269 | case LYS_USES: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 270 | /* go into */ |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 271 | if (options & LYS_GETNEXT_WITHUSES) { |
| 272 | return next; |
| 273 | } else if (next->child) { |
| 274 | next = next->child; |
| 275 | } else { |
| 276 | next = next->next; |
| 277 | } |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 278 | goto repeat; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 279 | |
Radek Krejci | dfcae7d | 2015-10-20 17:13:01 +0200 | [diff] [blame] | 280 | case LYS_RPC: |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 281 | case LYS_ACTION: |
Radek Krejci | dfcae7d | 2015-10-20 17:13:01 +0200 | [diff] [blame] | 282 | case LYS_NOTIF: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 283 | case LYS_LEAF: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 284 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 285 | case LYS_ANYDATA: |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 286 | case LYS_LIST: |
| 287 | case LYS_LEAFLIST: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 288 | return next; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 289 | |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 290 | case LYS_CONTAINER: |
| 291 | if (!((struct lys_node_container *)next)->presence && (options & LYS_GETNEXT_INTONPCONT)) { |
| 292 | if (next->child) { |
| 293 | /* go into */ |
| 294 | next = next->child; |
| 295 | } else { |
| 296 | next = next->next; |
| 297 | } |
| 298 | goto repeat; |
| 299 | } else { |
| 300 | return next; |
| 301 | } |
| 302 | |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 303 | case LYS_CHOICE: |
| 304 | if (options & LYS_GETNEXT_WITHCHOICE) { |
| 305 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 306 | } else if (next->child) { |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 307 | /* go into */ |
| 308 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 309 | } else { |
| 310 | next = next->next; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 311 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 312 | goto repeat; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 313 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 314 | default: |
| 315 | /* we should not be here */ |
| 316 | return NULL; |
| 317 | } |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 318 | |
| 319 | |
| 320 | } |
| 321 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 322 | static void |
| 323 | lys_extension_instances_free(struct ly_ctx *ctx, struct lys_ext_instance **e, unsigned int size) |
| 324 | { |
| 325 | unsigned int i; |
| 326 | |
| 327 | if (!size || !e || !(*e)) { |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | for (i = 0; i < size; i++) { |
| 332 | if (!e[i]) { |
| 333 | continue; |
| 334 | } |
| 335 | |
Radek Krejci | 80056d5 | 2017-01-05 13:13:33 +0100 | [diff] [blame] | 336 | if (e[i]->flags & LYEXT_OPT_INHERIT) { |
| 337 | /* no free, this is just a shadow copy of the original extension instance */ |
| 338 | } else { |
| 339 | lys_extension_instances_free(ctx, e[i]->ext, e[i]->ext_size); |
| 340 | lydict_remove(ctx, e[i]->arg_value); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 341 | } |
| 342 | free(e[i]); |
| 343 | } |
| 344 | free(e); |
| 345 | } |
| 346 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 347 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 348 | lys_node_unlink(struct lys_node *node) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 349 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 350 | struct lys_node *parent, *first; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 351 | struct lys_module *main_module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 352 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 353 | if (!node) { |
| 354 | return; |
| 355 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 356 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 357 | /* unlink from data model if necessary */ |
| 358 | if (node->module) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 359 | /* get main module with data tree */ |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 360 | main_module = lys_node_module(node); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 361 | if (main_module->data == node) { |
| 362 | main_module->data = node->next; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 363 | } |
| 364 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 365 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 366 | /* store pointers to important nodes */ |
| 367 | parent = node->parent; |
Michal Vasko | 3a9943b | 2015-09-23 11:33:50 +0200 | [diff] [blame] | 368 | if (parent && (parent->nodetype == LYS_AUGMENT)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 369 | /* handle augments - first, unlink it from the augment parent ... */ |
| 370 | if (parent->child == node) { |
| 371 | parent->child = node->next; |
| 372 | } |
Radek Krejci | fec2e14 | 2017-01-05 15:19:03 +0100 | [diff] [blame] | 373 | |
| 374 | if (parent->flags & LYS_NOTAPPLIED) { |
| 375 | /* data are not connected in the target, so we cannot continue with the target as a parent */ |
| 376 | parent = NULL; |
| 377 | } else { |
| 378 | /* data are connected in target, so we will continue with the target as a parent */ |
| 379 | parent = ((struct lys_node_augment *)parent)->target; |
| 380 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 381 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 382 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 383 | /* unlink from parent */ |
| 384 | if (parent) { |
| 385 | if (parent->child == node) { |
| 386 | parent->child = node->next; |
| 387 | } |
| 388 | node->parent = NULL; |
| 389 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 390 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 391 | /* unlink from siblings */ |
| 392 | if (node->prev == node) { |
| 393 | /* there are no more siblings */ |
| 394 | return; |
| 395 | } |
| 396 | if (node->next) { |
| 397 | node->next->prev = node->prev; |
| 398 | } else { |
| 399 | /* unlinking the last element */ |
| 400 | if (parent) { |
| 401 | first = parent->child; |
| 402 | } else { |
| 403 | first = node; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 404 | while (first->prev->next) { |
Michal Vasko | 276f96b | 2015-09-23 11:34:28 +0200 | [diff] [blame] | 405 | first = first->prev; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | first->prev = node->prev; |
| 409 | } |
| 410 | if (node->prev->next) { |
| 411 | node->prev->next = node->next; |
| 412 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 413 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 414 | /* clean up the unlinked element */ |
| 415 | node->next = NULL; |
| 416 | node->prev = node; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 417 | } |
| 418 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 419 | struct lys_node_grp * |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 420 | lys_find_grouping_up(const char *name, struct lys_node *start) |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 421 | { |
| 422 | struct lys_node *par_iter, *iter, *stop; |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 423 | |
| 424 | for (par_iter = start; par_iter; par_iter = par_iter->parent) { |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 425 | /* top-level augment, look into module (uses augment is handled correctly below) */ |
| 426 | if (par_iter->parent && !par_iter->parent->parent && (par_iter->parent->nodetype == LYS_AUGMENT)) { |
| 427 | par_iter = par_iter->parent->module->data; |
| 428 | if (!par_iter) { |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 429 | break; |
| 430 | } |
| 431 | } |
| 432 | |
Michal Vasko | 6f929da | 2015-10-02 16:23:25 +0200 | [diff] [blame] | 433 | 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] | 434 | continue; |
| 435 | } |
| 436 | |
| 437 | for (iter = par_iter, stop = NULL; iter; iter = iter->prev) { |
| 438 | if (!stop) { |
| 439 | stop = par_iter; |
| 440 | } else if (iter == stop) { |
| 441 | break; |
| 442 | } |
| 443 | if (iter->nodetype != LYS_GROUPING) { |
| 444 | continue; |
| 445 | } |
| 446 | |
Radek Krejci | f8426a7 | 2015-10-31 23:14:03 +0100 | [diff] [blame] | 447 | if (!strcmp(name, iter->name)) { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 448 | return (struct lys_node_grp *)iter; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 453 | return NULL; |
| 454 | } |
| 455 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 456 | /* |
| 457 | * get next grouping in the root's subtree, in the |
| 458 | * first call, tha last is NULL |
| 459 | */ |
| 460 | static struct lys_node_grp * |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 461 | 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] | 462 | { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 463 | struct lys_node *last = (struct lys_node *)lastgrp; |
| 464 | struct lys_node *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 465 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 466 | assert(root); |
| 467 | |
| 468 | if (!last) { |
| 469 | last = root; |
| 470 | } |
| 471 | |
| 472 | while (1) { |
| 473 | if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) { |
| 474 | next = last->child; |
| 475 | } else { |
| 476 | next = NULL; |
| 477 | } |
| 478 | if (!next) { |
| 479 | if (last == root) { |
| 480 | /* we are done */ |
| 481 | return NULL; |
| 482 | } |
| 483 | |
| 484 | /* no children, go to siblings */ |
| 485 | next = last->next; |
| 486 | } |
| 487 | while (!next) { |
| 488 | /* go back through parents */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 489 | if (lys_parent(last) == root) { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 490 | /* we are done */ |
| 491 | return NULL; |
| 492 | } |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 493 | next = last->next; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 494 | last = lys_parent(last); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | if (next->nodetype == LYS_GROUPING) { |
| 498 | return (struct lys_node_grp *)next; |
| 499 | } |
| 500 | |
| 501 | last = next; |
| 502 | } |
| 503 | } |
| 504 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 505 | /* logs directly */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 506 | int |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 507 | lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module) |
| 508 | { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 509 | struct lys_node *start, *stop, *iter; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 510 | struct lys_node_grp *grp; |
| 511 | int down; |
| 512 | |
| 513 | assert(node); |
| 514 | |
| 515 | if (!parent) { |
| 516 | assert(module); |
| 517 | } else { |
| 518 | module = parent->module; |
| 519 | } |
| 520 | |
| 521 | switch (node->nodetype) { |
| 522 | case LYS_GROUPING: |
| 523 | /* 6.2.1, rule 6 */ |
| 524 | if (parent) { |
| 525 | if (parent->child) { |
| 526 | down = 1; |
| 527 | start = parent->child; |
| 528 | } else { |
| 529 | down = 0; |
| 530 | start = parent; |
| 531 | } |
| 532 | } else { |
| 533 | down = 1; |
| 534 | start = module->data; |
| 535 | } |
| 536 | /* go up */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 537 | if (lys_find_grouping_up(node->name, start)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 538 | LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name); |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 539 | return EXIT_FAILURE; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 540 | } |
| 541 | /* go down, because grouping can be defined after e.g. container in which is collision */ |
| 542 | if (down) { |
| 543 | for (iter = start, stop = NULL; iter; iter = iter->prev) { |
| 544 | if (!stop) { |
| 545 | stop = start; |
| 546 | } else if (iter == stop) { |
| 547 | break; |
| 548 | } |
| 549 | if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) { |
| 550 | continue; |
| 551 | } |
| 552 | |
| 553 | grp = NULL; |
| 554 | while ((grp = lys_get_next_grouping(grp, iter))) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 555 | if (ly_strequal(node->name, grp->name, 1)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 556 | LOGVAL(LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 557 | return EXIT_FAILURE; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | break; |
| 563 | case LYS_LEAF: |
| 564 | case LYS_LEAFLIST: |
| 565 | case LYS_LIST: |
| 566 | case LYS_CONTAINER: |
| 567 | case LYS_CHOICE: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 568 | case LYS_ANYDATA: |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 569 | /* 6.2.1, rule 7 */ |
| 570 | if (parent) { |
| 571 | iter = parent; |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 572 | while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE | LYS_AUGMENT))) { |
| 573 | if (iter->nodetype == LYS_AUGMENT) { |
| 574 | if (((struct lys_node_augment *)iter)->target) { |
| 575 | /* augment is resolved, go up */ |
| 576 | iter = ((struct lys_node_augment *)iter)->target; |
| 577 | continue; |
| 578 | } |
| 579 | /* augment is not resolved, this is the final parent */ |
| 580 | break; |
| 581 | } |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 582 | iter = iter->parent; |
| 583 | } |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 584 | |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 585 | if (!iter) { |
| 586 | stop = NULL; |
| 587 | iter = module->data; |
| 588 | } else { |
| 589 | stop = iter; |
| 590 | iter = iter->child; |
| 591 | } |
| 592 | } else { |
| 593 | stop = NULL; |
| 594 | iter = module->data; |
| 595 | } |
| 596 | while (iter) { |
| 597 | if (iter->nodetype & (LYS_USES | LYS_CASE)) { |
| 598 | iter = iter->child; |
| 599 | continue; |
| 600 | } |
| 601 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 602 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CONTAINER | LYS_CHOICE | LYS_ANYDATA)) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 603 | if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 604 | LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, strnodetype(node->nodetype), node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 605 | return EXIT_FAILURE; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | /* special case for choice - we must check the choice's name as |
| 610 | * well as the names of nodes under the choice |
| 611 | */ |
| 612 | if (iter->nodetype == LYS_CHOICE) { |
| 613 | iter = iter->child; |
| 614 | continue; |
| 615 | } |
| 616 | |
| 617 | /* go to siblings */ |
| 618 | if (!iter->next) { |
| 619 | /* no sibling, go to parent's sibling */ |
| 620 | do { |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 621 | /* for parent LYS_AUGMENT */ |
| 622 | if (iter->parent == stop) { |
| 623 | iter = stop; |
| 624 | break; |
| 625 | } |
| 626 | iter = lys_parent(iter); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 627 | if (iter && iter->next) { |
| 628 | break; |
| 629 | } |
| 630 | } while (iter != stop); |
| 631 | |
| 632 | if (iter == stop) { |
| 633 | break; |
| 634 | } |
| 635 | } |
| 636 | iter = iter->next; |
| 637 | } |
| 638 | break; |
| 639 | case LYS_CASE: |
| 640 | /* 6.2.1, rule 8 */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 641 | if (parent) { |
| 642 | start = parent->child; |
| 643 | } else { |
| 644 | start = module->data; |
| 645 | } |
| 646 | |
| 647 | LY_TREE_FOR(start, iter) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 648 | if (!(iter->nodetype & (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) { |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 649 | continue; |
| 650 | } |
| 651 | |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 652 | if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 653 | LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "case", node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 654 | return EXIT_FAILURE; |
| 655 | } |
| 656 | } |
| 657 | break; |
| 658 | default: |
| 659 | /* no check needed */ |
| 660 | break; |
| 661 | } |
| 662 | |
| 663 | return EXIT_SUCCESS; |
| 664 | } |
| 665 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 666 | /* logs directly */ |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 667 | int |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 668 | lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child) |
| 669 | { |
Radek Krejci | 9272055 | 2015-10-05 15:28:27 +0200 | [diff] [blame] | 670 | struct lys_node *iter; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 671 | struct lys_node_inout *in, *out, *inout; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 672 | int type; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 673 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 674 | assert(child); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 675 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 676 | if (parent) { |
| 677 | type = parent->nodetype; |
| 678 | module = parent->module; |
| 679 | } else { |
| 680 | assert(module); |
Radek Krejci | fe3a138 | 2016-11-04 10:30:11 +0100 | [diff] [blame] | 681 | assert(!(child->nodetype & (LYS_INPUT | LYS_OUTPUT))); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 682 | type = 0; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 683 | } |
| 684 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 685 | /* checks */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 686 | switch (type) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 687 | case LYS_CONTAINER: |
| 688 | case LYS_LIST: |
| 689 | case LYS_GROUPING: |
Radek Krejci | 9693540 | 2016-11-04 16:27:28 +0100 | [diff] [blame] | 690 | case LYS_USES: |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 691 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 692 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF | |
Michal Vasko | b15cae2 | 2016-09-15 09:40:56 +0200 | [diff] [blame] | 693 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) { |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 694 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
| 695 | return EXIT_FAILURE; |
| 696 | } |
| 697 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 698 | case LYS_INPUT: |
| 699 | case LYS_OUTPUT: |
| 700 | case LYS_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 701 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 702 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 703 | LYS_LEAFLIST | LYS_LIST | LYS_USES))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 704 | 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] | 705 | return EXIT_FAILURE; |
| 706 | } |
| 707 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 708 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 709 | if (!(child->nodetype & |
Pavol Vican | 47a1b0a | 2016-09-20 10:18:24 +0200 | [diff] [blame] | 710 | (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 711 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 712 | return EXIT_FAILURE; |
| 713 | } |
| 714 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 715 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 716 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 717 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_USES))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 718 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 719 | return EXIT_FAILURE; |
| 720 | } |
| 721 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 722 | case LYS_RPC: |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 723 | case LYS_ACTION: |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 724 | if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 725 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc"); |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 726 | return EXIT_FAILURE; |
| 727 | } |
| 728 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 729 | case LYS_LEAF: |
| 730 | case LYS_LEAFLIST: |
| 731 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 732 | case LYS_ANYDATA: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 733 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
| 734 | 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] | 735 | strnodetype(parent->nodetype)); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 736 | return EXIT_FAILURE; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 737 | case LYS_AUGMENT: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 738 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 739 | (LYS_ANYDATA | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 740 | | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 741 | 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] | 742 | return EXIT_FAILURE; |
| 743 | } |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 744 | break; |
| 745 | case LYS_UNKNOWN: |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 746 | /* top level */ |
| 747 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 748 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 749 | | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 750 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "(sub)module"); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 751 | return EXIT_FAILURE; |
| 752 | } |
| 753 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 754 | break; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | /* check identifier uniqueness */ |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 758 | if (lys_check_id(child, parent, module)) { |
| 759 | return EXIT_FAILURE; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 760 | } |
Radek Krejci | b7155b5 | 2015-06-10 17:03:01 +0200 | [diff] [blame] | 761 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 762 | if (child->parent) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 763 | lys_node_unlink(child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 764 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 765 | |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 766 | if (child->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 767 | /* replace the implicit input/output node */ |
| 768 | if (child->nodetype == LYS_OUTPUT) { |
| 769 | inout = (struct lys_node_inout *)parent->child->next; |
| 770 | } else { /* LYS_INPUT */ |
| 771 | inout = (struct lys_node_inout *)parent->child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 772 | parent->child = child; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 773 | } |
| 774 | if (inout->next) { |
| 775 | child->next = inout->next; |
| 776 | inout->next->prev = child; |
| 777 | inout->next = NULL; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 778 | } else { |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 779 | parent->child->prev = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 780 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 781 | child->prev = inout->prev; |
| 782 | if (inout->prev->next) { |
| 783 | inout->prev->next = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 784 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 785 | inout->prev = (struct lys_node *)inout; |
| 786 | child->parent = parent; |
| 787 | inout->parent = NULL; |
| 788 | lys_node_free((struct lys_node *)inout, NULL, 0); |
| 789 | } else { |
| 790 | /* connect the child correctly */ |
| 791 | if (!parent) { |
| 792 | if (module->data) { |
| 793 | module->data->prev->next = child; |
| 794 | child->prev = module->data->prev; |
| 795 | module->data->prev = child; |
| 796 | } else { |
| 797 | module->data = child; |
| 798 | } |
| 799 | } else { |
| 800 | if (!parent->child) { |
| 801 | /* the only/first child of the parent */ |
| 802 | parent->child = child; |
| 803 | child->parent = parent; |
| 804 | iter = child; |
| 805 | } else { |
| 806 | /* add a new child at the end of parent's child list */ |
| 807 | iter = parent->child->prev; |
| 808 | iter->next = child; |
| 809 | child->prev = iter; |
| 810 | } |
| 811 | while (iter->next) { |
| 812 | iter = iter->next; |
| 813 | iter->parent = parent; |
| 814 | } |
| 815 | parent->child->prev = iter; |
| 816 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 817 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 818 | |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 819 | /* check config value (but ignore them in groupings and augments) */ |
| 820 | for (iter = parent; iter && !(iter->nodetype & (LYS_GROUPING | LYS_AUGMENT)); iter = iter->parent); |
| 821 | if (parent && !iter) { |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 822 | for (iter = child; iter && !(iter->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); iter = iter->parent); |
| 823 | if (!iter && (parent->flags & LYS_CONFIG_R) && (child->flags & LYS_CONFIG_W)) { |
| 824 | LOGVAL(LYE_INARG, LY_VLOG_LYS, child, "true", "config"); |
| 825 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, child, "State nodes cannot have configuration nodes as children."); |
| 826 | return EXIT_FAILURE; |
| 827 | } |
| 828 | } |
| 829 | |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 830 | /* propagate information about status data presence */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 831 | if ((child->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA)) && |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 832 | (child->flags & LYS_INCL_STATUS)) { |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 833 | for(iter = parent; iter; iter = lys_parent(iter)) { |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 834 | /* store it only into container or list - the only data inner nodes */ |
| 835 | if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
| 836 | if (iter->flags & LYS_INCL_STATUS) { |
| 837 | /* done, someone else set it already from here */ |
| 838 | break; |
| 839 | } |
| 840 | /* set flag about including status data */ |
| 841 | iter->flags |= LYS_INCL_STATUS; |
| 842 | } |
| 843 | } |
| 844 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 845 | |
| 846 | /* create implicit input/output nodes to have available them as possible target for augment */ |
| 847 | if (child->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 848 | in = calloc(1, sizeof *in); |
| 849 | in->nodetype = LYS_INPUT; |
| 850 | in->name = lydict_insert(child->module->ctx, "input", 5); |
| 851 | out = calloc(1, sizeof *out); |
| 852 | out->name = lydict_insert(child->module->ctx, "output", 6); |
| 853 | out->nodetype = LYS_OUTPUT; |
| 854 | in->module = out->module = child->module; |
| 855 | in->parent = out->parent = child; |
| 856 | in->flags = out->flags = LYS_IMPLICIT; |
| 857 | in->next = (struct lys_node *)out; |
| 858 | in->prev = (struct lys_node *)out; |
| 859 | out->prev = (struct lys_node *)in; |
| 860 | child->child = (struct lys_node *)in; |
| 861 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 862 | return EXIT_SUCCESS; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 863 | } |
| 864 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 865 | static const struct lys_module * |
| 866 | 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] | 867 | { |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 868 | char *enlarged_data = NULL; |
Radek Krejci | 0b5805d | 2015-08-13 09:38:02 +0200 | [diff] [blame] | 869 | struct lys_module *mod = NULL; |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 870 | unsigned int len; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 871 | |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 872 | ly_err_clean(1); |
Radek Krejci | f347abc | 2016-06-22 10:18:47 +0200 | [diff] [blame] | 873 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 874 | if (!ctx || !data) { |
| 875 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 876 | return NULL; |
| 877 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 878 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 879 | if (!internal && format == LYS_IN_YANG) { |
| 880 | /* enlarge data by 2 bytes for flex */ |
| 881 | len = strlen(data); |
| 882 | enlarged_data = malloc((len + 2) * sizeof *enlarged_data); |
| 883 | if (!enlarged_data) { |
| 884 | LOGMEM; |
| 885 | return NULL; |
| 886 | } |
| 887 | memcpy(enlarged_data, data, len); |
| 888 | enlarged_data[len] = enlarged_data[len + 1] = '\0'; |
| 889 | data = enlarged_data; |
| 890 | } |
| 891 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 892 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 893 | case LYS_IN_YIN: |
Radek Krejci | ff4874d | 2016-03-07 12:30:50 +0100 | [diff] [blame] | 894 | mod = yin_read_module(ctx, data, NULL, 1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 895 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 896 | case LYS_IN_YANG: |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 897 | mod = yang_read_module(ctx, data, 0, NULL, 1); |
| 898 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 899 | default: |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 900 | LOGERR(LY_EINVAL, "Invalid schema input format."); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 901 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 902 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 903 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 904 | free(enlarged_data); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 905 | return mod; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 906 | } |
| 907 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 908 | API const struct lys_module * |
| 909 | lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format) |
| 910 | { |
| 911 | return lys_parse_mem_(ctx, data, format, 0); |
| 912 | } |
| 913 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 914 | struct lys_submodule * |
| 915 | 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] | 916 | { |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 917 | struct lys_submodule *submod = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 918 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 919 | assert(module); |
| 920 | assert(data); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 921 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 922 | /* get the main module */ |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 923 | module = lys_main_module(module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 924 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 925 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 926 | case LYS_IN_YIN: |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 927 | submod = yin_read_submodule(module, data, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 928 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 929 | case LYS_IN_YANG: |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 930 | submod = yang_read_submodule(module, data, 0, unres); |
| 931 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 932 | default: |
Radek Krejci | 90a550a | 2016-04-13 16:00:58 +0200 | [diff] [blame] | 933 | assert(0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 934 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 935 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 936 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 937 | return submod; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 938 | } |
| 939 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 940 | API const struct lys_module * |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 941 | lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format) |
| 942 | { |
| 943 | int fd; |
| 944 | const struct lys_module *ret; |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 945 | const char *rev, *dot, *filename; |
| 946 | size_t len; |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 947 | |
| 948 | if (!ctx || !path) { |
| 949 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 950 | return NULL; |
| 951 | } |
| 952 | |
| 953 | fd = open(path, O_RDONLY); |
| 954 | if (fd == -1) { |
| 955 | LOGERR(LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)); |
| 956 | return NULL; |
| 957 | } |
| 958 | |
| 959 | ret = lys_parse_fd(ctx, fd, format); |
| 960 | close(fd); |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 961 | |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 962 | if (!ret) { |
| 963 | /* error */ |
| 964 | return NULL; |
| 965 | } |
| 966 | |
| 967 | /* check that name and revision match filename */ |
| 968 | filename = strrchr(path, '/'); |
| 969 | if (!filename) { |
| 970 | filename = path; |
| 971 | } else { |
| 972 | filename++; |
| 973 | } |
| 974 | rev = strchr(filename, '@'); |
| 975 | dot = strrchr(filename, '.'); |
| 976 | |
| 977 | /* name */ |
| 978 | len = strlen(ret->name); |
| 979 | if (strncmp(filename, ret->name, len) || |
| 980 | ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) { |
Radek Krejci | c0c8230 | 2016-10-25 14:21:33 +0200 | [diff] [blame] | 981 | LOGWRN("File name \"%s\" does not match module name \"%s\".", filename, ret->name); |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 982 | } |
| 983 | if (rev) { |
| 984 | len = dot - ++rev; |
| 985 | if (!ret->rev_size || len != 10 || strncmp(ret->rev[0].date, rev, len)) { |
| 986 | LOGWRN("File name \"%s\" does not match module revision \"%s\".", filename, |
| 987 | ret->rev_size ? ret->rev[0].date : "none"); |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | if (!ret->filepath) { |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 992 | /* store URI */ |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 993 | ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0); |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 994 | } |
| 995 | |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 996 | return ret; |
| 997 | } |
| 998 | |
| 999 | API const struct lys_module * |
| 1000 | lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format) |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1001 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1002 | const struct lys_module *module; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1003 | struct stat sb; |
| 1004 | char *addr; |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1005 | char buf[PATH_MAX]; |
| 1006 | int len; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1007 | |
| 1008 | if (!ctx || fd < 0) { |
| 1009 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1010 | return NULL; |
| 1011 | } |
| 1012 | |
Radek Krejci | 10a833c | 2015-12-16 15:28:37 +0100 | [diff] [blame] | 1013 | if (fstat(fd, &sb) == -1) { |
| 1014 | LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno)); |
| 1015 | return NULL; |
| 1016 | } |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1017 | if (!S_ISREG(sb.st_mode)) { |
| 1018 | LOGERR(LY_EINVAL, "Invalid parameter, input file is not a regular file"); |
| 1019 | return NULL; |
| 1020 | } |
| 1021 | |
Michal Vasko | 164d901 | 2016-04-01 10:16:59 +0200 | [diff] [blame] | 1022 | if (!sb.st_size) { |
| 1023 | LOGERR(LY_EINVAL, "File empty."); |
| 1024 | return NULL; |
| 1025 | } |
| 1026 | |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1027 | 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] | 1028 | if (addr == MAP_FAILED) { |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1029 | LOGERR(LY_EMEM, "Map file into memory failed (%s()).",__func__); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1030 | return NULL; |
| 1031 | } |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1032 | module = lys_parse_mem_(ctx, addr, format, 1); |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1033 | munmap(addr, sb.st_size + 2); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1034 | |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1035 | if (module && !module->filepath) { |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1036 | /* get URI if there is /proc */ |
| 1037 | addr = NULL; |
Radek Krejci | 15412ca | 2016-03-03 11:16:52 +0100 | [diff] [blame] | 1038 | if (asprintf(&addr, "/proc/self/fd/%d", fd) != -1) { |
| 1039 | if ((len = readlink(addr, buf, PATH_MAX - 1)) > 0) { |
| 1040 | ((struct lys_module *)module)->filepath = lydict_insert(ctx, buf, len); |
| 1041 | } |
| 1042 | free(addr); |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1043 | } |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1044 | } |
| 1045 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1046 | return module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1047 | } |
| 1048 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1049 | struct lys_submodule * |
| 1050 | 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] | 1051 | { |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1052 | struct lys_submodule *submodule; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1053 | struct stat sb; |
| 1054 | char *addr; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1055 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1056 | assert(module); |
| 1057 | assert(fd >= 0); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1058 | |
Radek Krejci | 10a833c | 2015-12-16 15:28:37 +0100 | [diff] [blame] | 1059 | if (fstat(fd, &sb) == -1) { |
| 1060 | LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno)); |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1061 | return NULL; |
Radek Krejci | 10a833c | 2015-12-16 15:28:37 +0100 | [diff] [blame] | 1062 | } |
Michal Vasko | 164d901 | 2016-04-01 10:16:59 +0200 | [diff] [blame] | 1063 | |
| 1064 | if (!sb.st_size) { |
| 1065 | LOGERR(LY_EINVAL, "File empty."); |
| 1066 | return NULL; |
| 1067 | } |
| 1068 | |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1069 | 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] | 1070 | if (addr == MAP_FAILED) { |
| 1071 | LOGERR(LY_EMEM,"Map file into memory failed (%s()).",__func__); |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1072 | return NULL; |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1073 | } |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1074 | submodule = lys_submodule_parse(module, addr, format, unres); |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1075 | munmap(addr, sb.st_size + 2); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1076 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1077 | return submodule; |
| 1078 | |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1079 | } |
| 1080 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1081 | static struct lys_restr * |
| 1082 | 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] | 1083 | { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1084 | struct lys_restr *result; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1085 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1086 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1087 | if (!size) { |
| 1088 | return NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1089 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1090 | |
| 1091 | result = calloc(size, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1092 | if (!result) { |
Radek Krejci | aa2a893 | 2016-02-17 15:03:14 +0100 | [diff] [blame] | 1093 | LOGMEM; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1094 | return NULL; |
| 1095 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1096 | for (i = 0; i < size; i++) { |
| 1097 | result[i].expr = lydict_insert(ctx, old[i].expr, 0); |
| 1098 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 1099 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 1100 | result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0); |
| 1101 | result[i].emsg = lydict_insert(ctx, old[i].emsg, 0); |
| 1102 | } |
| 1103 | |
| 1104 | return result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1105 | } |
| 1106 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1107 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1108 | lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr) |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1109 | { |
| 1110 | assert(ctx); |
| 1111 | if (!restr) { |
| 1112 | return; |
| 1113 | } |
| 1114 | |
Radek Krejci | fccd144 | 2017-01-16 10:26:57 +0100 | [diff] [blame^] | 1115 | lys_extension_instances_free(ctx, restr->ext, restr->ext_size); |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1116 | lydict_remove(ctx, restr->expr); |
| 1117 | lydict_remove(ctx, restr->dsc); |
| 1118 | lydict_remove(ctx, restr->ref); |
| 1119 | lydict_remove(ctx, restr->eapptag); |
| 1120 | lydict_remove(ctx, restr->emsg); |
| 1121 | } |
| 1122 | |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 1123 | static void |
| 1124 | lys_iffeature_free(struct lys_iffeature *iffeature, uint8_t iffeature_size) |
| 1125 | { |
| 1126 | uint8_t i; |
| 1127 | |
| 1128 | for (i = 0; i < iffeature_size; ++i) { |
| 1129 | free(iffeature[i].expr); |
| 1130 | free(iffeature[i].features); |
| 1131 | } |
| 1132 | free(iffeature); |
| 1133 | } |
| 1134 | |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1135 | static int |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1136 | 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] | 1137 | LY_DATA_TYPE base, int tpdftype, struct unres_schema *unres) |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1138 | { |
| 1139 | int i; |
| 1140 | |
| 1141 | switch (base) { |
| 1142 | case LY_TYPE_BINARY: |
| 1143 | if (old->info.binary.length) { |
| 1144 | new->info.binary.length = lys_restr_dup(mod->ctx, old->info.binary.length, 1); |
| 1145 | } |
| 1146 | break; |
| 1147 | |
| 1148 | case LY_TYPE_BITS: |
| 1149 | new->info.bits.count = old->info.bits.count; |
| 1150 | if (new->info.bits.count) { |
| 1151 | new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit); |
| 1152 | if (!new->info.bits.bit) { |
| 1153 | LOGMEM; |
| 1154 | return -1; |
| 1155 | } |
| 1156 | for (i = 0; i < new->info.bits.count; i++) { |
| 1157 | new->info.bits.bit[i].name = lydict_insert(mod->ctx, old->info.bits.bit[i].name, 0); |
| 1158 | new->info.bits.bit[i].dsc = lydict_insert(mod->ctx, old->info.bits.bit[i].dsc, 0); |
| 1159 | new->info.bits.bit[i].ref = lydict_insert(mod->ctx, old->info.bits.bit[i].ref, 0); |
| 1160 | new->info.bits.bit[i].flags = old->info.bits.bit[i].flags; |
| 1161 | new->info.bits.bit[i].pos = old->info.bits.bit[i].pos; |
| 1162 | } |
| 1163 | } |
| 1164 | break; |
| 1165 | |
| 1166 | case LY_TYPE_DEC64: |
| 1167 | new->info.dec64.dig = old->info.dec64.dig; |
Radek Krejci | 8c3b4b6 | 2016-06-17 14:32:12 +0200 | [diff] [blame] | 1168 | new->info.dec64.div = old->info.dec64.div; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1169 | if (old->info.dec64.range) { |
| 1170 | new->info.dec64.range = lys_restr_dup(mod->ctx, old->info.dec64.range, 1); |
| 1171 | } |
| 1172 | break; |
| 1173 | |
| 1174 | case LY_TYPE_ENUM: |
| 1175 | new->info.enums.count = old->info.enums.count; |
| 1176 | if (new->info.enums.count) { |
| 1177 | new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm); |
| 1178 | if (!new->info.enums.enm) { |
| 1179 | LOGMEM; |
| 1180 | return -1; |
| 1181 | } |
| 1182 | for (i = 0; i < new->info.enums.count; i++) { |
| 1183 | new->info.enums.enm[i].name = lydict_insert(mod->ctx, old->info.enums.enm[i].name, 0); |
| 1184 | new->info.enums.enm[i].dsc = lydict_insert(mod->ctx, old->info.enums.enm[i].dsc, 0); |
| 1185 | new->info.enums.enm[i].ref = lydict_insert(mod->ctx, old->info.enums.enm[i].ref, 0); |
| 1186 | new->info.enums.enm[i].flags = old->info.enums.enm[i].flags; |
| 1187 | new->info.enums.enm[i].value = old->info.enums.enm[i].value; |
| 1188 | } |
| 1189 | } |
| 1190 | break; |
| 1191 | |
| 1192 | case LY_TYPE_IDENT: |
Michal Vasko | affc37f | 2016-10-10 18:05:03 +0000 | [diff] [blame] | 1193 | new->info.ident.count = old->info.ident.count; |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 1194 | if (old->info.ident.count) { |
| 1195 | new->info.ident.ref = malloc(old->info.ident.count * sizeof *new->info.ident.ref); |
| 1196 | if (!new->info.ident.ref) { |
| 1197 | LOGMEM; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1198 | return -1; |
| 1199 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 1200 | memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref); |
| 1201 | } else { |
| 1202 | /* there can be several unresolved base identities, duplicate them all */ |
| 1203 | i = -1; |
| 1204 | while ((i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF)) != -1) { |
| 1205 | if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i]) == -1) { |
| 1206 | return -1; |
| 1207 | } |
| 1208 | --i; |
| 1209 | } |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1210 | } |
| 1211 | break; |
| 1212 | |
| 1213 | case LY_TYPE_INST: |
| 1214 | new->info.inst.req = old->info.inst.req; |
| 1215 | break; |
| 1216 | |
| 1217 | case LY_TYPE_INT8: |
| 1218 | case LY_TYPE_INT16: |
| 1219 | case LY_TYPE_INT32: |
| 1220 | case LY_TYPE_INT64: |
| 1221 | case LY_TYPE_UINT8: |
| 1222 | case LY_TYPE_UINT16: |
| 1223 | case LY_TYPE_UINT32: |
| 1224 | case LY_TYPE_UINT64: |
| 1225 | if (old->info.num.range) { |
| 1226 | new->info.num.range = lys_restr_dup(mod->ctx, old->info.num.range, 1); |
| 1227 | } |
| 1228 | break; |
| 1229 | |
| 1230 | case LY_TYPE_LEAFREF: |
| 1231 | if (old->info.lref.path) { |
| 1232 | 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] | 1233 | 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] | 1234 | return -1; |
| 1235 | } |
| 1236 | } |
| 1237 | break; |
| 1238 | |
| 1239 | case LY_TYPE_STRING: |
| 1240 | if (old->info.str.length) { |
| 1241 | new->info.str.length = lys_restr_dup(mod->ctx, old->info.str.length, 1); |
| 1242 | } |
| 1243 | new->info.str.patterns = lys_restr_dup(mod->ctx, old->info.str.patterns, old->info.str.pat_count); |
| 1244 | new->info.str.pat_count = old->info.str.pat_count; |
| 1245 | break; |
| 1246 | |
| 1247 | case LY_TYPE_UNION: |
| 1248 | new->info.uni.count = old->info.uni.count; |
| 1249 | if (new->info.uni.count) { |
| 1250 | new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types); |
| 1251 | if (!new->info.uni.types) { |
| 1252 | LOGMEM; |
| 1253 | return -1; |
| 1254 | } |
| 1255 | for (i = 0; i < new->info.uni.count; i++) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1256 | 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] | 1257 | return -1; |
| 1258 | } |
| 1259 | } |
| 1260 | } |
| 1261 | break; |
| 1262 | |
| 1263 | default: |
| 1264 | /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */ |
| 1265 | break; |
| 1266 | } |
| 1267 | return EXIT_SUCCESS; |
| 1268 | } |
| 1269 | |
| 1270 | struct yang_type * |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1271 | lys_yang_type_dup(struct lys_module *module, struct lys_node *parent, struct yang_type *old, struct lys_type *type, |
| 1272 | int tpdftype, struct unres_schema *unres) |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1273 | { |
| 1274 | struct yang_type *new; |
| 1275 | |
| 1276 | new = calloc(1, sizeof *new); |
| 1277 | if (!new) { |
| 1278 | LOGMEM; |
| 1279 | return NULL; |
| 1280 | } |
| 1281 | new->flags = old->flags; |
| 1282 | new->base = old->base; |
Pavol Vican | 6658629 | 2016-04-07 11:38:52 +0200 | [diff] [blame] | 1283 | new->name = lydict_insert(module->ctx, old->name, 0); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1284 | new->type = type; |
| 1285 | if (!new->name) { |
| 1286 | LOGMEM; |
| 1287 | goto error; |
| 1288 | } |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1289 | if (type_dup(module, parent, type, old->type, new->base, tpdftype, unres)) { |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1290 | new->type->base = new->base; |
| 1291 | lys_type_free(module->ctx, new->type); |
| 1292 | memset(&new->type->info, 0, sizeof new->type->info); |
| 1293 | goto error; |
| 1294 | } |
| 1295 | return new; |
| 1296 | |
| 1297 | error: |
| 1298 | free(new); |
| 1299 | return NULL; |
| 1300 | } |
| 1301 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1302 | /* |
| 1303 | * duplicate extension instance |
| 1304 | */ |
| 1305 | static struct lys_ext_instance ** |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1306 | lys_ext_dup(struct lys_module *mod, struct lys_ext_instance **orig, unsigned int size, |
| 1307 | void *parent, LYEXT_PAR parent_type, struct unres_schema *unres) |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1308 | { |
| 1309 | int i; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1310 | unsigned int u = 0; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1311 | struct lys_ext_instance **result; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1312 | struct unres_ext *info, *info_orig; |
| 1313 | |
| 1314 | assert(size); |
| 1315 | assert(!orig); |
| 1316 | assert(!(*orig)); |
| 1317 | |
| 1318 | result = calloc(size, sizeof *result); |
| 1319 | for (u = 0; u < size; u++) { |
| 1320 | if (orig[u]) { |
| 1321 | /* resolved extension instance, just duplicate it */ |
| 1322 | result[u]->def = orig[u]->def; |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 1323 | switch(lys_ext_instance_type(orig[u])) { |
| 1324 | case LYEXT_FLAG: |
| 1325 | result[u] = malloc(sizeof(struct lys_ext_instance)); |
| 1326 | break; |
| 1327 | case LYEXT_ERR: |
| 1328 | LOGINT; |
| 1329 | break; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1330 | } |
Radek Krejci | 0aa821a | 2016-12-08 11:21:35 +0100 | [diff] [blame] | 1331 | /* generic part */ |
| 1332 | result[u]->arg_value = lydict_insert(mod->ctx, orig[u]->arg_value, 0); |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1333 | result[u]->parent = parent; |
| 1334 | result[u]->parent_type = parent_type; |
Radek Krejci | 80c6b44 | 2017-01-04 16:21:13 +0100 | [diff] [blame] | 1335 | result[u]->substmt = orig[u]->substmt; |
| 1336 | result[u]->substmt_index = orig[u]->substmt_index; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1337 | } else { |
| 1338 | /* original extension is not yet resolved, so duplicate it in unres */ |
| 1339 | i = unres_schema_find(unres, -1, orig, UNRES_EXT); |
| 1340 | if (i == -1) { |
| 1341 | /* extension not found in unres */ |
| 1342 | LOGINT; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1343 | goto error; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1344 | } |
| 1345 | info_orig = unres->str_snode[i]; |
| 1346 | info = malloc(sizeof *info); |
| 1347 | info->datatype = info_orig->datatype; |
| 1348 | if (info->datatype == LYS_IN_YIN) { |
| 1349 | info->data.yin = lyxml_dup_elem(mod->ctx, info_orig->data.yin, NULL, 1); |
| 1350 | } /* else TODO YANG */ |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1351 | info->parent = parent; |
| 1352 | info->parent_type = parent_type; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1353 | if (unres_schema_add_node(mod, unres, &result[u], UNRES_EXT, (struct lys_node *)info) == -1) { |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1354 | goto error; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1355 | } |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | return result; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1360 | |
| 1361 | error: |
| 1362 | lys_extension_instances_free(mod->ctx, result, u); |
| 1363 | return NULL; |
| 1364 | } |
| 1365 | |
| 1366 | API const void * |
| 1367 | lys_ext_instance_substmt(const struct lys_ext_instance *ext) |
| 1368 | { |
| 1369 | if (!ext) { |
| 1370 | return NULL; |
| 1371 | } |
| 1372 | |
| 1373 | switch (ext->substmt) { |
| 1374 | case LYEXT_SUBSTMT_SELF: |
| 1375 | case LYEXT_SUBSTMT_MODIFIER: |
| 1376 | case LYEXT_SUBSTMT_VERSION: |
| 1377 | return NULL; |
| 1378 | case LYEXT_SUBSTMT_ARGUMENT: |
| 1379 | if (ext->parent_type == LYEXT_PAR_EXT) { |
| 1380 | return ((struct lys_ext_instance*)ext->parent)->arg_value; |
| 1381 | } |
| 1382 | break; |
| 1383 | case LYEXT_SUBSTMT_BASE: |
| 1384 | if (ext->parent_type == LYEXT_PAR_TYPE) { |
| 1385 | return ((struct lys_type*)ext->parent)->info.ident.ref[ext->substmt_index]; |
| 1386 | } else if (ext->parent_type == LYEXT_PAR_IDENT) { |
| 1387 | return ((struct lys_ident*)ext->parent)->base[ext->substmt_index]; |
| 1388 | } |
| 1389 | break; |
| 1390 | case LYEXT_SUBSTMT_BELONGSTO: |
| 1391 | if (ext->parent_type == LYEXT_PAR_MODULE && ((struct lys_module*)ext->parent)->type) { |
| 1392 | return ((struct lys_submodule*)ext->parent)->belongsto; |
| 1393 | } |
| 1394 | break; |
| 1395 | case LYEXT_SUBSTMT_CONFIG: |
| 1396 | case LYEXT_SUBSTMT_MANDATORY: |
| 1397 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 1398 | return &((struct lys_node*)ext->parent)->flags; |
| 1399 | } else if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
| 1400 | return &((struct lys_deviate*)ext->parent)->flags; |
| 1401 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 1402 | return &((struct lys_refine*)ext->parent)->flags; |
| 1403 | } |
| 1404 | break; |
| 1405 | case LYEXT_SUBSTMT_CONTACT: |
| 1406 | if (ext->parent_type == LYEXT_PAR_MODULE) { |
| 1407 | return ((struct lys_module*)ext->parent)->contact; |
| 1408 | } |
| 1409 | break; |
| 1410 | case LYEXT_SUBSTMT_DEFAULT: |
| 1411 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 1412 | switch (((struct lys_node*)ext->parent)->nodetype) { |
| 1413 | case LYS_LEAF: |
| 1414 | case LYS_LEAFLIST: |
| 1415 | /* in case of leaf, the index is supposed to be 0, so it will return the |
| 1416 | * correct pointer despite the leaf structure does not have dflt as array */ |
| 1417 | return ((struct lys_node_leaflist*)ext->parent)->dflt[ext->substmt_index]; |
| 1418 | case LYS_CHOICE: |
| 1419 | return ((struct lys_node_choice*)ext->parent)->dflt; |
| 1420 | default: |
| 1421 | /* internal error */ |
| 1422 | break; |
| 1423 | } |
| 1424 | } else if (ext->parent_type == LYEXT_PAR_TPDF) { |
| 1425 | return ((struct lys_tpdf*)ext->parent)->dflt; |
| 1426 | } else if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
| 1427 | return ((struct lys_deviate*)ext->parent)->dflt[ext->substmt_index]; |
| 1428 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 1429 | return &((struct lys_refine*)ext->parent)->dflt[ext->substmt_index]; |
| 1430 | } |
| 1431 | break; |
| 1432 | case LYEXT_SUBSTMT_DESCRIPTION: |
| 1433 | switch (ext->parent_type) { |
| 1434 | case LYEXT_PAR_NODE: |
| 1435 | return ((struct lys_node*)ext->parent)->dsc; |
| 1436 | case LYEXT_PAR_MODULE: |
| 1437 | return ((struct lys_module*)ext->parent)->dsc; |
| 1438 | case LYEXT_PAR_IMPORT: |
| 1439 | return ((struct lys_import*)ext->parent)->dsc; |
| 1440 | case LYEXT_PAR_INCLUDE: |
| 1441 | return ((struct lys_include*)ext->parent)->dsc; |
| 1442 | case LYEXT_PAR_EXT: |
| 1443 | return ((struct lys_ext*)ext->parent)->dsc; |
| 1444 | case LYEXT_PAR_FEATURE: |
| 1445 | return ((struct lys_feature*)ext->parent)->dsc; |
| 1446 | case LYEXT_PAR_TPDF: |
| 1447 | return ((struct lys_tpdf*)ext->parent)->dsc; |
| 1448 | case LYEXT_PAR_TYPE_BIT: |
| 1449 | return ((struct lys_type_bit*)ext->parent)->dsc; |
| 1450 | case LYEXT_PAR_TYPE_ENUM: |
| 1451 | return ((struct lys_type_enum*)ext->parent)->dsc; |
| 1452 | case LYEXT_PAR_MUST: |
| 1453 | case LYEXT_PAR_RANGE: |
| 1454 | case LYEXT_PAR_LENGTH: |
| 1455 | case LYEXT_PAR_PATTERN: |
| 1456 | return ((struct lys_restr*)ext->parent)->dsc; |
| 1457 | case LYEXT_PAR_WHEN: |
| 1458 | return ((struct lys_when*)ext->parent)->dsc; |
| 1459 | case LYEXT_PAR_IDENT: |
| 1460 | return ((struct lys_ident*)ext->parent)->dsc; |
| 1461 | case LYEXT_PAR_DEVIATION: |
| 1462 | return ((struct lys_deviation*)ext->parent)->dsc; |
| 1463 | case LYEXT_PAR_REVISION: |
| 1464 | return ((struct lys_revision*)ext->parent)->dsc; |
| 1465 | case LYEXT_PAR_REFINE: |
| 1466 | return ((struct lys_refine*)ext->parent)->dsc; |
| 1467 | default: |
| 1468 | break; |
| 1469 | } |
| 1470 | break; |
| 1471 | case LYEXT_SUBSTMT_ERRTAG: |
| 1472 | if (ext->parent_type == LYEXT_PAR_MUST || ext->parent_type == LYEXT_PAR_RANGE || |
| 1473 | ext->parent_type == LYEXT_PAR_LENGTH || ext->parent_type == LYEXT_PAR_PATTERN) { |
| 1474 | return ((struct lys_restr*)ext->parent)->eapptag; |
| 1475 | } |
| 1476 | break; |
| 1477 | case LYEXT_SUBSTMT_ERRMSG: |
| 1478 | if (ext->parent_type == LYEXT_PAR_MUST || ext->parent_type == LYEXT_PAR_RANGE || |
| 1479 | ext->parent_type == LYEXT_PAR_LENGTH || ext->parent_type == LYEXT_PAR_PATTERN) { |
| 1480 | return ((struct lys_restr*)ext->parent)->emsg; |
| 1481 | } |
| 1482 | break; |
| 1483 | case LYEXT_SUBSTMT_DIGITS: |
| 1484 | if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_DEC64) { |
| 1485 | return &((struct lys_type*)ext->parent)->info.dec64.dig; |
| 1486 | } |
| 1487 | break; |
| 1488 | case LYEXT_SUBSTMT_KEY: |
| 1489 | if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) { |
| 1490 | return ((struct lys_node_list*)ext->parent)->keys; |
| 1491 | } |
| 1492 | break; |
| 1493 | case LYEXT_SUBSTMT_MAX: |
| 1494 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 1495 | if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) { |
| 1496 | return &((struct lys_node_list*)ext->parent)->max; |
| 1497 | } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) { |
| 1498 | return &((struct lys_node_leaflist*)ext->parent)->max; |
| 1499 | } |
| 1500 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 1501 | return &((struct lys_refine*)ext->parent)->mod.list.max; |
| 1502 | } |
| 1503 | break; |
| 1504 | case LYEXT_SUBSTMT_MIN: |
| 1505 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 1506 | if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) { |
| 1507 | return &((struct lys_node_list*)ext->parent)->min; |
| 1508 | } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) { |
| 1509 | return &((struct lys_node_leaflist*)ext->parent)->min; |
| 1510 | } |
| 1511 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 1512 | return &((struct lys_refine*)ext->parent)->mod.list.min; |
| 1513 | } |
| 1514 | break; |
| 1515 | case LYEXT_SUBSTMT_NAMESPACE: |
| 1516 | if (ext->parent_type == LYEXT_PAR_MODULE && !((struct lys_module*)ext->parent)->type) { |
| 1517 | return ((struct lys_module*)ext->parent)->ns; |
| 1518 | } |
| 1519 | break; |
| 1520 | case LYEXT_SUBSTMT_ORDEREDBY: |
| 1521 | if (ext->parent_type == LYEXT_PAR_NODE && |
| 1522 | (((struct lys_node*)ext->parent)->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 1523 | return &((struct lys_node_list*)ext->parent)->flags; |
| 1524 | } |
| 1525 | break; |
| 1526 | case LYEXT_SUBSTMT_ORGANIZATION: |
| 1527 | if (ext->parent_type == LYEXT_PAR_MODULE) { |
| 1528 | return ((struct lys_module*)ext->parent)->org; |
| 1529 | } |
| 1530 | break; |
| 1531 | case LYEXT_SUBSTMT_PATH: |
| 1532 | if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) { |
| 1533 | return ((struct lys_type*)ext->parent)->info.lref.path; |
| 1534 | } |
| 1535 | break; |
| 1536 | case LYEXT_SUBSTMT_POSITION: |
| 1537 | if (ext->parent_type == LYEXT_PAR_TYPE_BIT) { |
| 1538 | return &((struct lys_type_bit*)ext->parent)->pos; |
| 1539 | } |
| 1540 | break; |
| 1541 | case LYEXT_SUBSTMT_PREFIX: |
| 1542 | if (ext->parent_type == LYEXT_PAR_MODULE) { |
| 1543 | /* covers also lys_submodule */ |
| 1544 | return ((struct lys_module*)ext->parent)->prefix; |
| 1545 | } else if (ext->parent_type == LYEXT_PAR_IMPORT) { |
| 1546 | return ((struct lys_import*)ext->parent)->prefix; |
| 1547 | } |
| 1548 | break; |
| 1549 | case LYEXT_SUBSTMT_PRESENCE: |
| 1550 | if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_CONTAINER) { |
| 1551 | return ((struct lys_node_container*)ext->parent)->presence; |
| 1552 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 1553 | return ((struct lys_refine*)ext->parent)->mod.presence; |
| 1554 | } |
| 1555 | break; |
| 1556 | case LYEXT_SUBSTMT_REFERENCE: |
| 1557 | switch (ext->parent_type) { |
| 1558 | case LYEXT_PAR_NODE: |
| 1559 | return ((struct lys_node*)ext->parent)->ref; |
| 1560 | case LYEXT_PAR_MODULE: |
| 1561 | return ((struct lys_module*)ext->parent)->ref; |
| 1562 | case LYEXT_PAR_IMPORT: |
| 1563 | return ((struct lys_import*)ext->parent)->ref; |
| 1564 | case LYEXT_PAR_INCLUDE: |
| 1565 | return ((struct lys_include*)ext->parent)->ref; |
| 1566 | case LYEXT_PAR_EXT: |
| 1567 | return ((struct lys_ext*)ext->parent)->ref; |
| 1568 | case LYEXT_PAR_FEATURE: |
| 1569 | return ((struct lys_feature*)ext->parent)->ref; |
| 1570 | case LYEXT_PAR_TPDF: |
| 1571 | return ((struct lys_tpdf*)ext->parent)->ref; |
| 1572 | case LYEXT_PAR_TYPE_BIT: |
| 1573 | return ((struct lys_type_bit*)ext->parent)->ref; |
| 1574 | case LYEXT_PAR_TYPE_ENUM: |
| 1575 | return ((struct lys_type_enum*)ext->parent)->ref; |
| 1576 | case LYEXT_PAR_MUST: |
| 1577 | case LYEXT_PAR_RANGE: |
| 1578 | case LYEXT_PAR_LENGTH: |
| 1579 | case LYEXT_PAR_PATTERN: |
| 1580 | return ((struct lys_restr*)ext->parent)->ref; |
| 1581 | case LYEXT_PAR_WHEN: |
| 1582 | return ((struct lys_when*)ext->parent)->ref; |
| 1583 | case LYEXT_PAR_IDENT: |
| 1584 | return ((struct lys_ident*)ext->parent)->ref; |
| 1585 | case LYEXT_PAR_DEVIATION: |
| 1586 | return ((struct lys_deviation*)ext->parent)->ref; |
| 1587 | case LYEXT_PAR_REVISION: |
| 1588 | return ((struct lys_revision*)ext->parent)->ref; |
| 1589 | case LYEXT_PAR_REFINE: |
| 1590 | return ((struct lys_refine*)ext->parent)->ref; |
| 1591 | default: |
| 1592 | break; |
| 1593 | } |
| 1594 | break; |
| 1595 | case LYEXT_SUBSTMT_REQINST: |
| 1596 | if (ext->parent_type == LYEXT_PAR_TYPE) { |
| 1597 | if (((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) { |
| 1598 | return &((struct lys_type*)ext->parent)->info.lref.req; |
| 1599 | } else if (((struct lys_type*)ext->parent)->base == LY_TYPE_INST) { |
| 1600 | return &((struct lys_type*)ext->parent)->info.inst.req; |
| 1601 | } |
| 1602 | } |
| 1603 | break; |
| 1604 | case LYEXT_SUBSTMT_REVISIONDATE: |
| 1605 | if (ext->parent_type == LYEXT_PAR_IMPORT) { |
| 1606 | return ((struct lys_import*)ext->parent)->rev; |
| 1607 | } else if (ext->parent_type == LYEXT_PAR_INCLUDE) { |
| 1608 | return ((struct lys_include*)ext->parent)->rev; |
| 1609 | } |
| 1610 | break; |
| 1611 | case LYEXT_SUBSTMT_STATUS: |
| 1612 | switch (ext->parent_type) { |
| 1613 | case LYEXT_PAR_NODE: |
| 1614 | case LYEXT_PAR_IDENT: |
| 1615 | case LYEXT_PAR_TPDF: |
| 1616 | case LYEXT_PAR_EXT: |
| 1617 | case LYEXT_PAR_FEATURE: |
| 1618 | case LYEXT_PAR_TYPE_ENUM: |
| 1619 | case LYEXT_PAR_TYPE_BIT: |
| 1620 | /* in all structures the flags member is at the same offset */ |
| 1621 | return &((struct lys_node*)ext->parent)->flags; |
| 1622 | default: |
| 1623 | break; |
| 1624 | } |
| 1625 | break; |
| 1626 | case LYEXT_SUBSTMT_UNIQUE: |
| 1627 | if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
| 1628 | return &((struct lys_deviate*)ext->parent)->unique[ext->substmt_index]; |
| 1629 | } else if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) { |
| 1630 | return &((struct lys_node_list*)ext->parent)->unique[ext->substmt_index]; |
| 1631 | } |
| 1632 | break; |
| 1633 | case LYEXT_SUBSTMT_UNITS: |
| 1634 | if (ext->parent_type == LYEXT_PAR_NODE && |
| 1635 | (((struct lys_node*)ext->parent)->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 1636 | /* units is at the same offset in both lys_node_leaf and lys_node_leaflist */ |
| 1637 | return ((struct lys_node_leaf*)ext->parent)->units; |
| 1638 | } else if (ext->parent_type == LYEXT_PAR_TPDF) { |
| 1639 | return ((struct lys_tpdf*)ext->parent)->units; |
| 1640 | } else if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
| 1641 | return ((struct lys_deviate*)ext->parent)->units; |
| 1642 | } |
| 1643 | break; |
| 1644 | case LYEXT_SUBSTMT_VALUE: |
| 1645 | if (ext->parent_type == LYEXT_PAR_TYPE_ENUM) { |
| 1646 | return &((struct lys_type_enum*)ext->parent)->value; |
| 1647 | } |
| 1648 | break; |
| 1649 | case LYEXT_SUBSTMT_YINELEM: |
| 1650 | if (ext->parent_type == LYEXT_PAR_EXT) { |
| 1651 | return &((struct lys_ext*)ext->parent)->flags; |
| 1652 | } |
| 1653 | break; |
| 1654 | } |
| 1655 | LOGINT; |
| 1656 | return NULL; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1657 | } |
| 1658 | |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1659 | static int |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1660 | 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] | 1661 | int tpdftype, struct unres_schema *unres) |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1662 | { |
| 1663 | int i; |
| 1664 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1665 | new->module_name = lydict_insert(mod->ctx, old->module_name, 0); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1666 | new->base = old->base; |
| 1667 | new->der = old->der; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1668 | new->parent = (struct lys_tpdf *)parent; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1669 | if (old->ext_size) { |
| 1670 | new->ext_size = old->ext_size; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1671 | new->ext = lys_ext_dup(mod, old->ext, old->ext_size, new->parent, LYEXT_PAR_TPDF, unres); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1672 | if (!new->ext) { |
| 1673 | return -1; |
| 1674 | } |
| 1675 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1676 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 1677 | i = unres_schema_find(unres, -1, old, tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1678 | if (i != -1) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 1679 | /* HACK (serious one) for unres */ |
| 1680 | /* nothing else we can do but duplicate it immediately */ |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1681 | if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1682 | 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] | 1683 | } else { |
| 1684 | new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1); |
| 1685 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1686 | /* all these unres additions can fail even though they did not before */ |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1687 | if (!new->der || unres_schema_add_node(mod, unres, new, |
Michal Vasko | 5d63140 | 2016-07-21 13:15:15 +0200 | [diff] [blame] | 1688 | tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER, parent) == -1) { |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1689 | return -1; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 1690 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1691 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1692 | } |
| 1693 | |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1694 | return type_dup(mod, parent, new, old, new->base, tpdftype, unres); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1698 | lys_type_free(struct ly_ctx *ctx, struct lys_type *type) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1699 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1700 | int i; |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1701 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1702 | assert(ctx); |
| 1703 | if (!type) { |
| 1704 | return; |
| 1705 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1706 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1707 | lydict_remove(ctx, type->module_name); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1708 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1709 | lys_extension_instances_free(ctx, type->ext, type->ext_size); |
| 1710 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1711 | switch (type->base) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1712 | case LY_TYPE_BINARY: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1713 | lys_restr_free(ctx, type->info.binary.length); |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1714 | free(type->info.binary.length); |
| 1715 | break; |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 1716 | case LY_TYPE_BITS: |
| 1717 | for (i = 0; i < type->info.bits.count; i++) { |
| 1718 | lydict_remove(ctx, type->info.bits.bit[i].name); |
| 1719 | lydict_remove(ctx, type->info.bits.bit[i].dsc); |
| 1720 | lydict_remove(ctx, type->info.bits.bit[i].ref); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 1721 | 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] | 1722 | } |
| 1723 | free(type->info.bits.bit); |
| 1724 | break; |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1725 | |
| 1726 | case LY_TYPE_DEC64: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1727 | lys_restr_free(ctx, type->info.dec64.range); |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1728 | free(type->info.dec64.range); |
| 1729 | break; |
| 1730 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1731 | case LY_TYPE_ENUM: |
| 1732 | for (i = 0; i < type->info.enums.count; i++) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1733 | lydict_remove(ctx, type->info.enums.enm[i].name); |
| 1734 | lydict_remove(ctx, type->info.enums.enm[i].dsc); |
| 1735 | lydict_remove(ctx, type->info.enums.enm[i].ref); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 1736 | 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] | 1737 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1738 | free(type->info.enums.enm); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1739 | break; |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1740 | |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 1741 | case LY_TYPE_INT8: |
| 1742 | case LY_TYPE_INT16: |
| 1743 | case LY_TYPE_INT32: |
| 1744 | case LY_TYPE_INT64: |
| 1745 | case LY_TYPE_UINT8: |
| 1746 | case LY_TYPE_UINT16: |
| 1747 | case LY_TYPE_UINT32: |
| 1748 | case LY_TYPE_UINT64: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1749 | lys_restr_free(ctx, type->info.num.range); |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 1750 | free(type->info.num.range); |
| 1751 | break; |
| 1752 | |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1753 | case LY_TYPE_LEAFREF: |
| 1754 | lydict_remove(ctx, type->info.lref.path); |
| 1755 | break; |
| 1756 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1757 | case LY_TYPE_STRING: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1758 | lys_restr_free(ctx, type->info.str.length); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1759 | free(type->info.str.length); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1760 | for (i = 0; i < type->info.str.pat_count; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1761 | lys_restr_free(ctx, &type->info.str.patterns[i]); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1762 | } |
| 1763 | free(type->info.str.patterns); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1764 | break; |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1765 | |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1766 | case LY_TYPE_UNION: |
| 1767 | for (i = 0; i < type->info.uni.count; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1768 | lys_type_free(ctx, &type->info.uni.types[i]); |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1769 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1770 | free(type->info.uni.types); |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1771 | break; |
| 1772 | |
Michal Vasko | d328219 | 2016-09-05 11:27:57 +0200 | [diff] [blame] | 1773 | case LY_TYPE_IDENT: |
| 1774 | free(type->info.ident.ref); |
| 1775 | break; |
| 1776 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1777 | default: |
Michal Vasko | d328219 | 2016-09-05 11:27:57 +0200 | [diff] [blame] | 1778 | /* nothing to do for LY_TYPE_INST, LY_TYPE_BOOL, LY_TYPE_EMPTY */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1779 | break; |
| 1780 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1781 | } |
| 1782 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1783 | static void |
| 1784 | lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1785 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1786 | assert(ctx); |
| 1787 | if (!tpdf) { |
| 1788 | return; |
| 1789 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1790 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1791 | lydict_remove(ctx, tpdf->name); |
| 1792 | lydict_remove(ctx, tpdf->dsc); |
| 1793 | lydict_remove(ctx, tpdf->ref); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1794 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1795 | lys_type_free(ctx, &tpdf->type); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1796 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1797 | lydict_remove(ctx, tpdf->units); |
| 1798 | lydict_remove(ctx, tpdf->dflt); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1799 | |
| 1800 | lys_extension_instances_free(ctx, tpdf->ext, tpdf->ext_size); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1801 | } |
| 1802 | |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1803 | static struct lys_tpdf * |
| 1804 | lys_tpdf_dup(struct lys_module *mod, struct lys_node *parent, struct lys_tpdf *old, int size, struct unres_schema *unres) |
| 1805 | { |
| 1806 | struct lys_tpdf *result; |
| 1807 | int i, j; |
| 1808 | |
| 1809 | if (!size) { |
| 1810 | return NULL; |
| 1811 | } |
| 1812 | |
| 1813 | result = calloc(size, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1814 | if (!result) { |
| 1815 | LOGMEM; |
| 1816 | return NULL; |
| 1817 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1818 | for (i = 0; i < size; i++) { |
| 1819 | result[i].name = lydict_insert(mod->ctx, old[i].name, 0); |
| 1820 | result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0); |
| 1821 | result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0); |
| 1822 | result[i].flags = old[i].flags; |
| 1823 | result[i].module = old[i].module; |
| 1824 | |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1825 | 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] | 1826 | for (j = 0; j <= i; ++j) { |
| 1827 | lys_tpdf_free(mod->ctx, &result[j]); |
| 1828 | } |
| 1829 | free(result); |
| 1830 | return NULL; |
| 1831 | } |
| 1832 | |
| 1833 | result[i].dflt = lydict_insert(mod->ctx, old[i].dflt, 0); |
| 1834 | result[i].units = lydict_insert(mod->ctx, old[i].units, 0); |
| 1835 | } |
| 1836 | |
| 1837 | return result; |
| 1838 | } |
| 1839 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1840 | static struct lys_when * |
| 1841 | lys_when_dup(struct ly_ctx *ctx, struct lys_when *old) |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1842 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1843 | struct lys_when *new; |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1844 | |
| 1845 | if (!old) { |
| 1846 | return NULL; |
| 1847 | } |
| 1848 | |
| 1849 | new = calloc(1, sizeof *new); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1850 | if (!new) { |
| 1851 | LOGMEM; |
| 1852 | return NULL; |
| 1853 | } |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1854 | new->cond = lydict_insert(ctx, old->cond, 0); |
| 1855 | new->dsc = lydict_insert(ctx, old->dsc, 0); |
| 1856 | new->ref = lydict_insert(ctx, old->ref, 0); |
| 1857 | |
| 1858 | return new; |
| 1859 | } |
| 1860 | |
Michal Vasko | 0308dd6 | 2015-10-07 09:14:40 +0200 | [diff] [blame] | 1861 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1862 | lys_when_free(struct ly_ctx *ctx, struct lys_when *w) |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1863 | { |
| 1864 | if (!w) { |
| 1865 | return; |
| 1866 | } |
| 1867 | |
| 1868 | lydict_remove(ctx, w->cond); |
| 1869 | lydict_remove(ctx, w->dsc); |
| 1870 | lydict_remove(ctx, w->ref); |
| 1871 | |
| 1872 | free(w); |
| 1873 | } |
| 1874 | |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1875 | static void |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1876 | 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] | 1877 | { |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 1878 | struct lys_node *next, *sub; |
| 1879 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1880 | /* children from a resolved augment are freed under the target node */ |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 1881 | if (!aug->target || (aug->flags & LYS_NOTAPPLIED)) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1882 | LY_TREE_FOR_SAFE(aug->child, next, sub) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1883 | lys_node_free(sub, private_destructor, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1884 | } |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 1885 | } |
| 1886 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1887 | lydict_remove(ctx, aug->target_name); |
| 1888 | lydict_remove(ctx, aug->dsc); |
| 1889 | lydict_remove(ctx, aug->ref); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1890 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1891 | lys_iffeature_free(aug->iffeature, aug->iffeature_size); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1892 | lys_extension_instances_free(ctx, aug->ext, aug->ext_size); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1893 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1894 | lys_when_free(ctx, aug->when); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1895 | } |
| 1896 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1897 | static struct lys_node_augment * |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1898 | 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] | 1899 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1900 | struct lys_node_augment *new = NULL; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1901 | struct lys_node *old_child, *new_child; |
| 1902 | int i; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1903 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1904 | if (!size) { |
| 1905 | return NULL; |
| 1906 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1907 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1908 | new = calloc(size, sizeof *new); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1909 | if (!new) { |
| 1910 | LOGMEM; |
| 1911 | return NULL; |
| 1912 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1913 | for (i = 0; i < size; i++) { |
| 1914 | new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0); |
| 1915 | new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0); |
| 1916 | new[i].ref = lydict_insert(module->ctx, old[i].ref, 0); |
| 1917 | new[i].flags = old[i].flags; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1918 | new[i].module = old[i].module; |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 1919 | new[i].nodetype = old[i].nodetype; |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1920 | |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1921 | /* this must succeed, it was already resolved once */ |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1922 | if (resolve_augment_schema_nodeid(new[i].target_name, parent->child, NULL, 1, |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1923 | (const struct lys_node **)&new[i].target)) { |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1924 | LOGINT; |
| 1925 | free(new); |
| 1926 | return NULL; |
| 1927 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1928 | new[i].parent = parent; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1929 | |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1930 | /* Correct the augment nodes. |
| 1931 | * This function can only be called from lys_node_dup() with uses |
| 1932 | * being the node duplicated, so we must have a case of grouping |
| 1933 | * with a uses with augments. The augmented nodes have already been |
| 1934 | * copied and everything is almost fine except their parent is wrong |
Michal Vasko | a5900c5 | 2015-09-24 13:17:11 +0200 | [diff] [blame] | 1935 | * (it was set to their actual data parent, not an augment), and |
| 1936 | * the new augment does not have child pointer to its augment nodes, |
| 1937 | * so we just correct it. |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1938 | */ |
| 1939 | LY_TREE_FOR(new[i].target->child, new_child) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 1940 | if (ly_strequal(new_child->name, old[i].child->name, 1)) { |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1941 | break; |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1942 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1943 | } |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1944 | assert(new_child); |
Michal Vasko | a5900c5 | 2015-09-24 13:17:11 +0200 | [diff] [blame] | 1945 | new[i].child = new_child; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1946 | LY_TREE_FOR(old[i].child, old_child) { |
| 1947 | /* all augment nodes were connected as siblings, there can be no more after this */ |
| 1948 | if (old_child->parent != (struct lys_node *)&old[i]) { |
| 1949 | break; |
| 1950 | } |
| 1951 | |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 1952 | assert(ly_strequal(old_child->name, new_child->name, 1)); |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1953 | |
| 1954 | new_child->parent = (struct lys_node *)&new[i]; |
| 1955 | new_child = new_child->next; |
| 1956 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1957 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1958 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1959 | return new; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1960 | } |
| 1961 | |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 1962 | static const char ** |
| 1963 | lys_dflt_dup(struct ly_ctx *ctx, const char **old, int size) |
| 1964 | { |
| 1965 | int i; |
| 1966 | const char **result; |
| 1967 | |
| 1968 | if (!size) { |
| 1969 | return NULL; |
| 1970 | } |
| 1971 | |
| 1972 | result = calloc(size, sizeof *result); |
| 1973 | if (!result) { |
| 1974 | LOGMEM; |
| 1975 | return NULL; |
| 1976 | } |
| 1977 | |
| 1978 | for (i = 0; i < size; i++) { |
| 1979 | result[i] = lydict_insert(ctx, old[i], 0); |
| 1980 | } |
| 1981 | return result; |
| 1982 | } |
| 1983 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1984 | static struct lys_refine * |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 1985 | 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] | 1986 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1987 | struct lys_refine *result; |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 1988 | int i; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1989 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1990 | if (!size) { |
| 1991 | return NULL; |
| 1992 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1993 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1994 | result = calloc(size, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1995 | if (!result) { |
| 1996 | LOGMEM; |
| 1997 | return NULL; |
| 1998 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1999 | for (i = 0; i < size; i++) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2000 | 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] | 2001 | result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0); |
| 2002 | result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2003 | result[i].flags = old[i].flags; |
| 2004 | result[i].target_type = old[i].target_type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2005 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2006 | result[i].must_size = old[i].must_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2007 | 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] | 2008 | |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 2009 | result[i].dflt_size = old[i].dflt_size; |
| 2010 | result[i].dflt = lys_dflt_dup(mod->ctx, old[i].dflt, old[i].dflt_size); |
| 2011 | |
| 2012 | if (result[i].target_type == LYS_CONTAINER) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2013 | 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] | 2014 | } else if (result[i].target_type & (LYS_LIST | LYS_LEAFLIST)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2015 | result[i].mod.list = old[i].mod.list; |
| 2016 | } |
| 2017 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 2018 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2019 | return result; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 2020 | } |
| 2021 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2022 | static void |
| 2023 | lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident) |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2024 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2025 | assert(ctx); |
| 2026 | if (!ident) { |
| 2027 | return; |
| 2028 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2029 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 2030 | free(ident->base); |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 2031 | ly_set_free(ident->der); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2032 | lydict_remove(ctx, ident->name); |
| 2033 | lydict_remove(ctx, ident->dsc); |
| 2034 | lydict_remove(ctx, ident->ref); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 2035 | lys_iffeature_free(ident->iffeature, ident->iffeature_size); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2036 | lys_extension_instances_free(ctx, ident->ext, ident->ext_size); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2037 | |
| 2038 | } |
| 2039 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2040 | static void |
| 2041 | lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2042 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2043 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2044 | |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2045 | /* handle only specific parts for LYS_GROUPING */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2046 | for (i = 0; i < grp->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2047 | lys_tpdf_free(ctx, &grp->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2048 | } |
| 2049 | free(grp->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2050 | } |
| 2051 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2052 | static void |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2053 | lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io) |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2054 | { |
| 2055 | int i; |
| 2056 | |
| 2057 | /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */ |
| 2058 | for (i = 0; i < io->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2059 | lys_tpdf_free(ctx, &io->tpdf[i]); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2060 | } |
| 2061 | free(io->tpdf); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2062 | |
| 2063 | for (i = 0; i < io->must_size; i++) { |
| 2064 | lys_restr_free(ctx, &io->must[i]); |
| 2065 | } |
| 2066 | free(io->must); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2067 | } |
| 2068 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2069 | static void |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2070 | lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif) |
| 2071 | { |
| 2072 | int i; |
| 2073 | |
| 2074 | for (i = 0; i < notif->must_size; i++) { |
| 2075 | lys_restr_free(ctx, ¬if->must[i]); |
| 2076 | } |
| 2077 | free(notif->must); |
| 2078 | |
| 2079 | for (i = 0; i < notif->tpdf_size; i++) { |
| 2080 | lys_tpdf_free(ctx, ¬if->tpdf[i]); |
| 2081 | } |
| 2082 | free(notif->tpdf); |
| 2083 | } |
| 2084 | static void |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2085 | lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml) |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2086 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2087 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2088 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2089 | for (i = 0; i < anyxml->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2090 | lys_restr_free(ctx, &anyxml->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2091 | } |
| 2092 | free(anyxml->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2093 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2094 | lys_when_free(ctx, anyxml->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2095 | } |
| 2096 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2097 | static void |
| 2098 | lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2099 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2100 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2101 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 2102 | /* leafref backlinks */ |
| 2103 | ly_set_free((struct ly_set *)leaf->backlinks); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2104 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2105 | for (i = 0; i < leaf->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2106 | lys_restr_free(ctx, &leaf->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2107 | } |
| 2108 | free(leaf->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2109 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2110 | lys_when_free(ctx, leaf->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2111 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2112 | lys_type_free(ctx, &leaf->type); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2113 | lydict_remove(ctx, leaf->units); |
| 2114 | lydict_remove(ctx, leaf->dflt); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2115 | } |
| 2116 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2117 | static void |
| 2118 | lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2119 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2120 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2121 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2122 | if (llist->backlinks) { |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2123 | /* leafref backlinks */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2124 | ly_set_free(llist->backlinks); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2125 | } |
| 2126 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2127 | for (i = 0; i < llist->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2128 | lys_restr_free(ctx, &llist->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2129 | } |
| 2130 | free(llist->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2131 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2132 | for (i = 0; i < llist->dflt_size; i++) { |
| 2133 | lydict_remove(ctx, llist->dflt[i]); |
| 2134 | } |
| 2135 | free(llist->dflt); |
| 2136 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2137 | lys_when_free(ctx, llist->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2138 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2139 | lys_type_free(ctx, &llist->type); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2140 | lydict_remove(ctx, llist->units); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2141 | } |
| 2142 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2143 | static void |
| 2144 | lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2145 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2146 | int i, j; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2147 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2148 | /* handle only specific parts for LY_NODE_LIST */ |
| 2149 | for (i = 0; i < list->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2150 | lys_tpdf_free(ctx, &list->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2151 | } |
| 2152 | free(list->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2153 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2154 | for (i = 0; i < list->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2155 | lys_restr_free(ctx, &list->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2156 | } |
| 2157 | free(list->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2158 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2159 | lys_when_free(ctx, list->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2160 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2161 | for (i = 0; i < list->unique_size; i++) { |
Michal Vasko | f5e940a | 2016-06-07 09:39:26 +0200 | [diff] [blame] | 2162 | for (j = 0; j < list->unique[i].expr_size; j++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2163 | lydict_remove(ctx, list->unique[i].expr[j]); |
| 2164 | } |
| 2165 | free(list->unique[i].expr); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2166 | } |
| 2167 | free(list->unique); |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 2168 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2169 | free(list->keys); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2170 | } |
| 2171 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2172 | static void |
| 2173 | lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2174 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2175 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2176 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2177 | /* handle only specific parts for LY_NODE_CONTAINER */ |
| 2178 | lydict_remove(ctx, cont->presence); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2179 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2180 | for (i = 0; i < cont->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2181 | lys_tpdf_free(ctx, &cont->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2182 | } |
| 2183 | free(cont->tpdf); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2184 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2185 | for (i = 0; i < cont->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2186 | lys_restr_free(ctx, &cont->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2187 | } |
| 2188 | free(cont->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2189 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2190 | lys_when_free(ctx, cont->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2191 | } |
| 2192 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2193 | static void |
| 2194 | lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f) |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2195 | { |
| 2196 | lydict_remove(ctx, f->name); |
| 2197 | lydict_remove(ctx, f->dsc); |
| 2198 | lydict_remove(ctx, f->ref); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2199 | lys_iffeature_free(f->iffeature, f->iffeature_size); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 2200 | ly_set_free(f->depfeatures); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2201 | lys_extension_instances_free(ctx, f->ext, f->ext_size); |
| 2202 | } |
| 2203 | |
| 2204 | static void |
| 2205 | lys_extension_free(struct ly_ctx *ctx, struct lys_ext *e) |
| 2206 | { |
| 2207 | lydict_remove(ctx, e->name); |
| 2208 | lydict_remove(ctx, e->dsc); |
| 2209 | lydict_remove(ctx, e->ref); |
| 2210 | lydict_remove(ctx, e->argument); |
| 2211 | lys_extension_instances_free(ctx, e->ext, e->ext_size); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2212 | } |
| 2213 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2214 | static void |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2215 | lys_deviation_free(struct lys_module *module, struct lys_deviation *dev) |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2216 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2217 | int i, j, k; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2218 | struct ly_ctx *ctx; |
| 2219 | struct lys_node *next, *elem; |
| 2220 | |
| 2221 | ctx = module->ctx; |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2222 | |
| 2223 | lydict_remove(ctx, dev->target_name); |
| 2224 | lydict_remove(ctx, dev->dsc); |
| 2225 | lydict_remove(ctx, dev->ref); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2226 | lys_extension_instances_free(ctx, dev->ext, dev->ext_size); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2227 | |
Pavol Vican | 64d0b76 | 2016-08-25 10:44:59 +0200 | [diff] [blame] | 2228 | if (!dev->deviate) { |
| 2229 | return ; |
| 2230 | } |
| 2231 | |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2232 | /* the module was freed, but we only need the context from orig_node, use ours */ |
| 2233 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 2234 | /* it's actually a node subtree, we need to update modules on all the nodes :-/ */ |
| 2235 | LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) { |
| 2236 | elem->module = module; |
| 2237 | |
| 2238 | LY_TREE_DFS_END(dev->orig_node, next, elem); |
| 2239 | } |
| 2240 | lys_node_free(dev->orig_node, NULL, 0); |
| 2241 | } else { |
| 2242 | /* it's just a shallow copy, freeing one node */ |
| 2243 | dev->orig_node->module = module; |
| 2244 | lys_node_free(dev->orig_node, NULL, 1); |
| 2245 | } |
| 2246 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2247 | for (i = 0; i < dev->deviate_size; i++) { |
Radek Krejci | d5a5c28 | 2016-08-15 15:38:08 +0200 | [diff] [blame] | 2248 | for (j = 0; j < dev->deviate[i].dflt_size; j++) { |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2249 | lydict_remove(ctx, dev->deviate[i].dflt[j]); |
Radek Krejci | d5a5c28 | 2016-08-15 15:38:08 +0200 | [diff] [blame] | 2250 | } |
| 2251 | free(dev->deviate[i].dflt); |
| 2252 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2253 | lydict_remove(ctx, dev->deviate[i].units); |
| 2254 | |
| 2255 | if (dev->deviate[i].mod == LY_DEVIATE_DEL) { |
| 2256 | for (j = 0; j < dev->deviate[i].must_size; j++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2257 | lys_restr_free(ctx, &dev->deviate[i].must[j]); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2258 | } |
| 2259 | free(dev->deviate[i].must); |
| 2260 | |
| 2261 | for (j = 0; j < dev->deviate[i].unique_size; j++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2262 | for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) { |
| 2263 | lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]); |
| 2264 | } |
Michal Vasko | 0238ccf | 2016-03-07 15:02:18 +0100 | [diff] [blame] | 2265 | free(dev->deviate[i].unique[j].expr); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2266 | } |
| 2267 | free(dev->deviate[i].unique); |
| 2268 | } |
| 2269 | } |
| 2270 | free(dev->deviate); |
| 2271 | } |
| 2272 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2273 | static void |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2274 | 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] | 2275 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2276 | int i, j; |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2277 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2278 | for (i = 0; i < uses->refine_size; i++) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2279 | lydict_remove(ctx, uses->refine[i].target_name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2280 | lydict_remove(ctx, uses->refine[i].dsc); |
| 2281 | lydict_remove(ctx, uses->refine[i].ref); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2282 | |
Michal Vasko | a275c0a | 2015-09-24 09:55:42 +0200 | [diff] [blame] | 2283 | for (j = 0; j < uses->refine[i].must_size; j++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2284 | lys_restr_free(ctx, &uses->refine[i].must[j]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2285 | } |
| 2286 | free(uses->refine[i].must); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2287 | |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 2288 | for (j = 0; j < uses->refine[i].dflt_size; j++) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 2289 | lydict_remove(ctx, uses->refine[i].dflt[j]); |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 2290 | } |
| 2291 | free(uses->refine[i].dflt); |
| 2292 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2293 | lys_extension_instances_free(ctx, uses->refine[i].ext, uses->refine[i].ext_size); |
| 2294 | |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 2295 | if (uses->refine[i].target_type & LYS_CONTAINER) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2296 | lydict_remove(ctx, uses->refine[i].mod.presence); |
| 2297 | } |
| 2298 | } |
| 2299 | free(uses->refine); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2300 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2301 | for (i = 0; i < uses->augment_size; i++) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2302 | lys_augment_free(ctx, &uses->augment[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2303 | } |
| 2304 | free(uses->augment); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2305 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2306 | lys_when_free(ctx, uses->when); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2307 | } |
| 2308 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2309 | void |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2310 | 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] | 2311 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2312 | struct ly_ctx *ctx; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2313 | struct lys_node *sub, *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2314 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2315 | if (!node) { |
| 2316 | return; |
| 2317 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2318 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2319 | assert(node->module); |
| 2320 | assert(node->module->ctx); |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2321 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2322 | ctx = node->module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2323 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2324 | /* remove private object */ |
Mislav Novakovic | b5529e5 | 2016-02-29 11:42:43 +0100 | [diff] [blame] | 2325 | if (node->priv && private_destructor) { |
| 2326 | private_destructor(node, node->priv); |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2327 | } |
| 2328 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2329 | /* common part */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2330 | lydict_remove(ctx, node->name); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2331 | if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2332 | lys_iffeature_free(node->iffeature, node->iffeature_size); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2333 | lydict_remove(ctx, node->dsc); |
| 2334 | lydict_remove(ctx, node->ref); |
| 2335 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2336 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2337 | if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2338 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2339 | lys_node_free(sub, private_destructor, 0); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2340 | } |
| 2341 | } |
| 2342 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2343 | lys_extension_instances_free(ctx, node->ext, node->ext_size); |
| 2344 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2345 | /* specific part */ |
| 2346 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2347 | case LYS_CONTAINER: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2348 | lys_container_free(ctx, (struct lys_node_container *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2349 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2350 | case LYS_CHOICE: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2351 | lys_when_free(ctx, ((struct lys_node_choice *)node)->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2352 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2353 | case LYS_LEAF: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2354 | lys_leaf_free(ctx, (struct lys_node_leaf *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2355 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2356 | case LYS_LEAFLIST: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2357 | lys_leaflist_free(ctx, (struct lys_node_leaflist *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2358 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2359 | case LYS_LIST: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2360 | lys_list_free(ctx, (struct lys_node_list *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2361 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2362 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2363 | case LYS_ANYDATA: |
| 2364 | lys_anydata_free(ctx, (struct lys_node_anydata *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2365 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2366 | case LYS_USES: |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2367 | lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2368 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2369 | case LYS_CASE: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2370 | lys_when_free(ctx, ((struct lys_node_case *)node)->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2371 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2372 | case LYS_AUGMENT: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2373 | /* do nothing */ |
| 2374 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2375 | case LYS_GROUPING: |
| 2376 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2377 | case LYS_ACTION: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2378 | lys_grp_free(ctx, (struct lys_node_grp *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2379 | break; |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2380 | case LYS_NOTIF: |
| 2381 | lys_notif_free(ctx, (struct lys_node_notif *)node); |
| 2382 | break; |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2383 | case LYS_INPUT: |
| 2384 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2385 | lys_inout_free(ctx, (struct lys_node_inout *)node); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2386 | break; |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 2387 | case LYS_UNKNOWN: |
| 2388 | LOGINT; |
| 2389 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2390 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2391 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2392 | /* again common part */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2393 | lys_node_unlink(node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2394 | free(node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2395 | } |
| 2396 | |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 2397 | API struct lys_module * |
| 2398 | lys_implemented_module(const struct lys_module *mod) |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 2399 | { |
| 2400 | struct ly_ctx *ctx; |
| 2401 | int i; |
| 2402 | |
| 2403 | if (!mod || mod->implemented) { |
| 2404 | /* invalid argument or the module itself is implemented */ |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 2405 | return (struct lys_module *)mod; |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | ctx = mod->ctx; |
| 2409 | for (i = 0; i < ctx->models.used; i++) { |
| 2410 | if (!ctx->models.list[i]->implemented) { |
| 2411 | continue; |
| 2412 | } |
| 2413 | |
| 2414 | if (ly_strequal(mod->name, ctx->models.list[i]->name, 1)) { |
| 2415 | /* we have some revision of the module implemented */ |
| 2416 | return ctx->models.list[i]; |
| 2417 | } |
| 2418 | } |
| 2419 | |
| 2420 | /* we have no revision of the module implemented, return the module itself, |
| 2421 | * it is up to the caller to set the module implemented when needed */ |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 2422 | return (struct lys_module *)mod; |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | const struct lys_module * |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2426 | lys_get_import_module_ns(const struct lys_module *module, const char *ns) |
| 2427 | { |
| 2428 | int i; |
| 2429 | |
| 2430 | assert(module && ns); |
| 2431 | |
Radek Krejci | c789d69 | 2017-01-11 11:31:14 +0100 | [diff] [blame] | 2432 | if (module->type) { |
| 2433 | /* the module is actually submodule and to get the namespace, we need the main module */ |
| 2434 | if (ly_strequal(((struct lys_submodule *)module)->belongsto->ns, ns, 0)) { |
| 2435 | return ((struct lys_submodule *)module)->belongsto; |
| 2436 | } |
| 2437 | } else { |
| 2438 | /* modul's own namespace */ |
| 2439 | if (ly_strequal(module->ns, ns, 0)) { |
| 2440 | return module; |
| 2441 | } |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | /* imported modules */ |
| 2445 | for (i = 0; i < module->imp_size; ++i) { |
| 2446 | if (ly_strequal(module->imp[i].module->ns, ns, 0)) { |
| 2447 | return module->imp[i].module; |
| 2448 | } |
| 2449 | } |
| 2450 | |
| 2451 | return NULL; |
| 2452 | } |
| 2453 | |
| 2454 | const struct lys_module * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2455 | 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] | 2456 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2457 | const struct lys_module *main_module; |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 2458 | char *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2459 | int i; |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2460 | |
Michal Vasko | a7789a8 | 2016-02-11 15:42:55 +0100 | [diff] [blame] | 2461 | assert(!prefix || !name); |
| 2462 | |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2463 | if (prefix && !pref_len) { |
| 2464 | pref_len = strlen(prefix); |
| 2465 | } |
| 2466 | if (name && !name_len) { |
| 2467 | name_len = strlen(name); |
| 2468 | } |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 2469 | |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 2470 | main_module = lys_main_module(module); |
Michal Vasko | a7789a8 | 2016-02-11 15:42:55 +0100 | [diff] [blame] | 2471 | |
| 2472 | /* module own prefix, submodule own prefix, (sub)module own name */ |
| 2473 | if ((!prefix || (!module->type && !strncmp(main_module->prefix, prefix, pref_len) && !main_module->prefix[pref_len]) |
| 2474 | || (module->type && !strncmp(module->prefix, prefix, pref_len) && !module->prefix[pref_len])) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2475 | && (!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] | 2476 | return main_module; |
Michal Vasko | d8da86b | 2015-10-21 13:35:37 +0200 | [diff] [blame] | 2477 | } |
| 2478 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 2479 | /* standard import */ |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 2480 | for (i = 0; i < module->imp_size; ++i) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2481 | if ((!prefix || (!strncmp(module->imp[i].prefix, prefix, pref_len) && !module->imp[i].prefix[pref_len])) |
| 2482 | && (!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] | 2483 | return module->imp[i].module; |
| 2484 | } |
| 2485 | } |
| 2486 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 2487 | /* module required by a foreign grouping, deviation, or submodule */ |
| 2488 | if (name) { |
| 2489 | str = strndup(name, name_len); |
| 2490 | if (!str) { |
| 2491 | LOGMEM; |
| 2492 | return NULL; |
| 2493 | } |
| 2494 | main_module = ly_ctx_get_module(module->ctx, str, NULL); |
| 2495 | free(str); |
| 2496 | return main_module; |
| 2497 | } |
| 2498 | |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 2499 | return NULL; |
| 2500 | } |
| 2501 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2502 | /* free_int_mods - flag whether to free the internal modules as well */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2503 | static void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2504 | 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] | 2505 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2506 | struct ly_ctx *ctx; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2507 | struct lys_node *next, *iter; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2508 | unsigned int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2509 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2510 | assert(module->ctx); |
| 2511 | ctx = module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2512 | |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2513 | /* just free the import array, imported modules will stay in the context */ |
Radek Krejci | 225376f | 2016-02-16 17:36:22 +0100 | [diff] [blame] | 2514 | for (i = 0; i < module->imp_size; i++) { |
Michal Vasko | a99c163 | 2016-06-06 16:23:52 +0200 | [diff] [blame] | 2515 | lydict_remove(ctx, module->imp[i].prefix); |
Michal Vasko | 8bfe381 | 2016-07-27 13:37:52 +0200 | [diff] [blame] | 2516 | lydict_remove(ctx, module->imp[i].dsc); |
| 2517 | lydict_remove(ctx, module->imp[i].ref); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2518 | lys_extension_instances_free(ctx, module->imp[i].ext, module->imp[i].ext_size); |
Radek Krejci | 225376f | 2016-02-16 17:36:22 +0100 | [diff] [blame] | 2519 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2520 | free(module->imp); |
| 2521 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2522 | /* submodules don't have data tree, the data nodes |
| 2523 | * are placed in the main module altogether */ |
| 2524 | if (!module->type) { |
| 2525 | LY_TREE_FOR_SAFE(module->data, next, iter) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2526 | lys_node_free(iter, private_destructor, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2527 | } |
Radek Krejci | 2118196 | 2015-06-30 14:11:00 +0200 | [diff] [blame] | 2528 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2529 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2530 | lydict_remove(ctx, module->dsc); |
| 2531 | lydict_remove(ctx, module->ref); |
| 2532 | lydict_remove(ctx, module->org); |
| 2533 | lydict_remove(ctx, module->contact); |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 2534 | lydict_remove(ctx, module->filepath); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2535 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2536 | /* revisions */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2537 | for (i = 0; i < module->rev_size; i++) { |
| 2538 | lydict_remove(ctx, module->rev[i].dsc); |
| 2539 | lydict_remove(ctx, module->rev[i].ref); |
| 2540 | } |
| 2541 | free(module->rev); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2542 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2543 | /* identities */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2544 | for (i = 0; i < module->ident_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2545 | lys_ident_free(ctx, &module->ident[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2546 | } |
| 2547 | module->ident_size = 0; |
| 2548 | free(module->ident); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2549 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2550 | /* typedefs */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2551 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2552 | lys_tpdf_free(ctx, &module->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2553 | } |
| 2554 | free(module->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2555 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2556 | /* extension instances */ |
| 2557 | lys_extension_instances_free(ctx, module->ext, module->ext_size); |
| 2558 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2559 | /* include */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2560 | for (i = 0; i < module->inc_size; i++) { |
Michal Vasko | 8bfe381 | 2016-07-27 13:37:52 +0200 | [diff] [blame] | 2561 | lydict_remove(ctx, module->inc[i].dsc); |
| 2562 | lydict_remove(ctx, module->inc[i].ref); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2563 | lys_extension_instances_free(ctx, module->inc[i].ext, module->inc[i].ext_size); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2564 | /* complete submodule free is done only from main module since |
| 2565 | * submodules propagate their includes to the main module */ |
| 2566 | if (!module->type) { |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2567 | lys_submodule_free(module->inc[i].submodule, private_destructor); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2568 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2569 | } |
| 2570 | free(module->inc); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2571 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2572 | /* augment */ |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2573 | for (i = 0; i < module->augment_size; i++) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2574 | lys_augment_free(ctx, &module->augment[i], private_destructor); |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2575 | } |
| 2576 | free(module->augment); |
| 2577 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2578 | /* features */ |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2579 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2580 | lys_feature_free(ctx, &module->features[i]); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2581 | } |
| 2582 | free(module->features); |
| 2583 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2584 | /* deviations */ |
| 2585 | for (i = 0; i < module->deviation_size; i++) { |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2586 | lys_deviation_free(module, &module->deviation[i]); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2587 | } |
| 2588 | free(module->deviation); |
| 2589 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2590 | /* extensions */ |
| 2591 | for (i = 0; i < module->extensions_size; i++) { |
| 2592 | lys_extension_free(ctx, &module->extensions[i]); |
| 2593 | } |
| 2594 | free(module->extensions); |
| 2595 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2596 | lydict_remove(ctx, module->name); |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 2597 | lydict_remove(ctx, module->prefix); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2598 | } |
| 2599 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2600 | void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2601 | 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] | 2602 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2603 | if (!submodule) { |
| 2604 | return; |
| 2605 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2606 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2607 | /* common part with struct ly_module */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2608 | module_free_common((struct lys_module *)submodule, private_destructor); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2609 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2610 | /* no specific items to free */ |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2611 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2612 | free(submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2613 | } |
| 2614 | |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 2615 | static int |
| 2616 | ingrouping(const struct lys_node *node) |
| 2617 | { |
| 2618 | const struct lys_node *iter = node; |
| 2619 | assert(node); |
| 2620 | |
| 2621 | for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter)); |
| 2622 | if (!iter) { |
| 2623 | return 0; |
| 2624 | } else { |
| 2625 | return 1; |
| 2626 | } |
| 2627 | } |
| 2628 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2629 | /* |
| 2630 | * final: 0 - do not change config flags; 1 - inherit config flags from the parent; 2 - remove config flags |
| 2631 | */ |
| 2632 | static struct lys_node * |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 2633 | lys_node_dup_recursion(struct lys_module *module, struct lys_node *parent, const struct lys_node *node, |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2634 | struct unres_schema *unres, int shallow, int finalize) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2635 | { |
Radek Krejci | 7b9f566 | 2016-11-07 16:28:37 +0100 | [diff] [blame] | 2636 | struct lys_node *retval = NULL, *iter, *p; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2637 | struct ly_ctx *ctx = module->ctx; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2638 | int i, j, rc; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2639 | unsigned int size, size1, size2; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 2640 | struct unres_list_uniq *unique_info; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2641 | uint16_t flags; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2642 | |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2643 | struct lys_node_container *cont = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2644 | struct lys_node_container *cont_orig = (struct lys_node_container *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2645 | struct lys_node_choice *choice = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2646 | struct lys_node_choice *choice_orig = (struct lys_node_choice *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2647 | struct lys_node_leaf *leaf = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2648 | struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2649 | struct lys_node_leaflist *llist = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2650 | struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2651 | struct lys_node_list *list = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2652 | struct lys_node_list *list_orig = (struct lys_node_list *)node; |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2653 | struct lys_node_anydata *any = NULL; |
| 2654 | struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2655 | struct lys_node_uses *uses = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2656 | struct lys_node_uses *uses_orig = (struct lys_node_uses *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2657 | struct lys_node_grp *grp = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2658 | struct lys_node_grp *grp_orig = (struct lys_node_grp *)node; |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2659 | struct lys_node_rpc_action *rpc = NULL; |
| 2660 | struct lys_node_rpc_action *rpc_orig = (struct lys_node_rpc_action *)node; |
| 2661 | struct lys_node_inout *io = NULL; |
| 2662 | struct lys_node_inout *io_orig = (struct lys_node_inout *)node; |
| 2663 | struct lys_node_rpc_action *ntf = NULL; |
| 2664 | 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] | 2665 | struct lys_node_case *cs = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2666 | struct lys_node_case *cs_orig = (struct lys_node_case *)node; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2667 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2668 | /* we cannot just duplicate memory since the strings are stored in |
| 2669 | * dictionary and we need to update dictionary counters. |
| 2670 | */ |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2671 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2672 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2673 | case LYS_CONTAINER: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2674 | cont = calloc(1, sizeof *cont); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2675 | retval = (struct lys_node *)cont; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2676 | break; |
| 2677 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2678 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2679 | choice = calloc(1, sizeof *choice); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2680 | retval = (struct lys_node *)choice; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2681 | break; |
| 2682 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2683 | case LYS_LEAF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2684 | leaf = calloc(1, sizeof *leaf); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2685 | retval = (struct lys_node *)leaf; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2686 | break; |
| 2687 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2688 | case LYS_LEAFLIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2689 | llist = calloc(1, sizeof *llist); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2690 | retval = (struct lys_node *)llist; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2691 | break; |
| 2692 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2693 | case LYS_LIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2694 | list = calloc(1, sizeof *list); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2695 | retval = (struct lys_node *)list; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2696 | break; |
| 2697 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2698 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2699 | case LYS_ANYDATA: |
| 2700 | any = calloc(1, sizeof *any); |
| 2701 | retval = (struct lys_node *)any; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2702 | break; |
| 2703 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2704 | case LYS_USES: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2705 | uses = calloc(1, sizeof *uses); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2706 | retval = (struct lys_node *)uses; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2707 | break; |
| 2708 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2709 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2710 | cs = calloc(1, sizeof *cs); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2711 | retval = (struct lys_node *)cs; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2712 | break; |
| 2713 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2714 | case LYS_GROUPING: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2715 | grp = calloc(1, sizeof *grp); |
| 2716 | retval = (struct lys_node *)grp; |
| 2717 | break; |
| 2718 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2719 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2720 | case LYS_ACTION: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2721 | rpc = calloc(1, sizeof *rpc); |
| 2722 | retval = (struct lys_node *)rpc; |
| 2723 | break; |
| 2724 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2725 | case LYS_INPUT: |
| 2726 | case LYS_OUTPUT: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2727 | io = calloc(1, sizeof *io); |
| 2728 | retval = (struct lys_node *)io; |
| 2729 | break; |
| 2730 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2731 | case LYS_NOTIF: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2732 | ntf = calloc(1, sizeof *ntf); |
| 2733 | retval = (struct lys_node *)ntf; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2734 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2735 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2736 | default: |
Michal Vasko | d23ce59 | 2015-08-06 09:55:37 +0200 | [diff] [blame] | 2737 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2738 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2739 | } |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame] | 2740 | |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2741 | if (!retval) { |
| 2742 | LOGMEM; |
| 2743 | return NULL; |
| 2744 | } |
| 2745 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2746 | /* |
| 2747 | * duplicate generic part of the structure |
| 2748 | */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2749 | retval->name = lydict_insert(ctx, node->name, 0); |
| 2750 | retval->dsc = lydict_insert(ctx, node->dsc, 0); |
| 2751 | retval->ref = lydict_insert(ctx, node->ref, 0); |
| 2752 | retval->flags = node->flags; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2753 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2754 | retval->module = module; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2755 | retval->nodetype = node->nodetype; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2756 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2757 | retval->prev = retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2758 | |
Radek Krejci | 0621404 | 2016-11-04 16:25:58 +0100 | [diff] [blame] | 2759 | if (node->iffeature_size) { |
| 2760 | retval->iffeature_size = node->iffeature_size; |
| 2761 | retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature); |
| 2762 | if (!retval->iffeature) { |
| 2763 | LOGMEM; |
| 2764 | goto error; |
| 2765 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2766 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2767 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2768 | if (!shallow) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 2769 | for (i = 0; i < node->iffeature_size; ++i) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2770 | resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2); |
| 2771 | if (size1) { |
| 2772 | /* there is something to duplicate */ |
| 2773 | |
| 2774 | /* duplicate compiled expression */ |
| 2775 | size = (size1 / 4) + (size1 % 4) ? 1 : 0; |
| 2776 | retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr); |
| 2777 | memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr); |
| 2778 | |
| 2779 | /* list of feature pointer must be updated to point to the resulting tree */ |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 2780 | retval->iffeature[i].features = calloc(size2, sizeof *retval->iffeature[i].features); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2781 | for (j = 0; (unsigned int)j < size2; j++) { |
| 2782 | rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT, |
| 2783 | &retval->iffeature[i].features[j]); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 2784 | if (rc == EXIT_FAILURE) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2785 | /* feature is resolved in origin, so copy it |
| 2786 | * - duplication is used for instantiating groupings |
| 2787 | * and if-feature inside grouping is supposed to be |
| 2788 | * resolved inside the original grouping, so we want |
| 2789 | * to keep pointers to features from the grouping |
| 2790 | * context */ |
| 2791 | retval->iffeature[i].features[j] = node->iffeature[i].features[j]; |
| 2792 | } else if (rc == -1) { |
| 2793 | goto error; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 2794 | } /* else unres was duplicated */ |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2795 | } |
| 2796 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2797 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2798 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2799 | /* inherit config flags */ |
Radek Krejci | 7b9f566 | 2016-11-07 16:28:37 +0100 | [diff] [blame] | 2800 | p = parent; |
| 2801 | do { |
| 2802 | for (iter = p; iter && (iter->nodetype == LYS_USES); iter = iter->parent); |
| 2803 | } while (iter && iter->nodetype == LYS_AUGMENT && (p = lys_parent(iter))); |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2804 | if (iter) { |
| 2805 | flags = iter->flags & LYS_CONFIG_MASK; |
| 2806 | } else { |
| 2807 | /* default */ |
| 2808 | flags = LYS_CONFIG_W; |
| 2809 | } |
| 2810 | |
| 2811 | switch (finalize) { |
| 2812 | case 1: |
| 2813 | /* inherit config flags */ |
| 2814 | if (retval->flags & LYS_CONFIG_SET) { |
| 2815 | /* skip nodes with an explicit config value */ |
| 2816 | if ((flags & LYS_CONFIG_R) && (retval->flags & LYS_CONFIG_W)) { |
| 2817 | LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, "true", "config"); |
| 2818 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, retval, "State nodes cannot have configuration nodes as children."); |
| 2819 | goto error; |
| 2820 | } |
| 2821 | break; |
| 2822 | } |
| 2823 | |
| 2824 | if (retval->nodetype != LYS_USES) { |
| 2825 | retval->flags = (retval->flags & ~LYS_CONFIG_MASK) | flags; |
| 2826 | } |
| 2827 | break; |
| 2828 | case 2: |
| 2829 | /* erase config flags */ |
| 2830 | retval->flags &= ~LYS_CONFIG_MASK; |
| 2831 | retval->flags &= ~LYS_CONFIG_SET; |
| 2832 | break; |
| 2833 | } |
| 2834 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2835 | /* connect it to the parent */ |
| 2836 | if (lys_node_addchild(parent, retval->module, retval)) { |
| 2837 | goto error; |
| 2838 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2839 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2840 | /* go recursively */ |
| 2841 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2842 | LY_TREE_FOR(node->child, iter) { |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 2843 | if (!lys_node_dup_recursion(module, retval, iter, unres, 0, finalize)) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2844 | goto error; |
| 2845 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2846 | } |
| 2847 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2848 | |
| 2849 | if (finalize == 1) { |
| 2850 | /* check that configuration lists have keys |
| 2851 | * - we really want to check keys_size in original node, because the keys are |
| 2852 | * not yet resolved here, it is done below in nodetype specific part */ |
| 2853 | if ((retval->nodetype == LYS_LIST) && (retval->flags & LYS_CONFIG_W) |
| 2854 | && !((struct lys_node_list *)node)->keys_size) { |
| 2855 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list"); |
| 2856 | goto error; |
| 2857 | } |
| 2858 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2859 | } else { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 2860 | memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2861 | } |
| 2862 | |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2863 | /* |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2864 | * duplicate specific part of the structure |
| 2865 | */ |
| 2866 | switch (node->nodetype) { |
| 2867 | case LYS_CONTAINER: |
| 2868 | if (cont_orig->when) { |
| 2869 | cont->when = lys_when_dup(ctx, cont_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2870 | } |
| 2871 | cont->presence = lydict_insert(ctx, cont_orig->presence, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2872 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2873 | cont->must_size = cont_orig->must_size; |
| 2874 | cont->tpdf_size = cont_orig->tpdf_size; |
| 2875 | |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2876 | cont->must = lys_restr_dup(ctx, cont_orig->must, cont->must_size); |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2877 | 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] | 2878 | break; |
| 2879 | |
| 2880 | case LYS_CHOICE: |
| 2881 | if (choice_orig->when) { |
| 2882 | choice->when = lys_when_dup(ctx, choice_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2883 | } |
| 2884 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2885 | if (!shallow) { |
| 2886 | if (choice_orig->dflt) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2887 | rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0, |
| 2888 | LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST, |
| 2889 | (const struct lys_node **)&choice->dflt); |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2890 | if (rc) { |
| 2891 | if (rc == EXIT_FAILURE) { |
| 2892 | LOGINT; |
| 2893 | } |
| 2894 | goto error; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2895 | } |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2896 | } else { |
| 2897 | /* useless to check return value, we don't know whether |
| 2898 | * there really wasn't any default defined or it just hasn't |
| 2899 | * been resolved, we just hope for the best :) |
| 2900 | */ |
| 2901 | unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2902 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2903 | } else { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2904 | choice->dflt = choice_orig->dflt; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2905 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2906 | break; |
| 2907 | |
| 2908 | case LYS_LEAF: |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 2909 | 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] | 2910 | goto error; |
| 2911 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2912 | leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0); |
| 2913 | |
| 2914 | if (leaf_orig->dflt) { |
| 2915 | leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2916 | if (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT, |
| 2917 | (struct lys_node *)(&leaf->dflt)) == -1) { |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2918 | goto error; |
| 2919 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | leaf->must_size = leaf_orig->must_size; |
| 2923 | leaf->must = lys_restr_dup(ctx, leaf_orig->must, leaf->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2924 | |
| 2925 | if (leaf_orig->when) { |
| 2926 | leaf->when = lys_when_dup(ctx, leaf_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2927 | } |
| 2928 | break; |
| 2929 | |
| 2930 | case LYS_LEAFLIST: |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 2931 | 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] | 2932 | goto error; |
| 2933 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2934 | llist->units = lydict_insert(module->ctx, llist_orig->units, 0); |
| 2935 | |
| 2936 | llist->min = llist_orig->min; |
| 2937 | llist->max = llist_orig->max; |
| 2938 | |
| 2939 | llist->must_size = llist_orig->must_size; |
| 2940 | llist->must = lys_restr_dup(ctx, llist_orig->must, llist->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2941 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2942 | llist->dflt_size = llist_orig->dflt_size; |
| 2943 | llist->dflt = malloc(llist->dflt_size * sizeof *llist->dflt); |
| 2944 | for (i = 0; i < llist->dflt_size; i++) { |
| 2945 | llist->dflt[i] = lydict_insert(ctx, llist_orig->dflt[i], 0); |
| 2946 | if (unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DFLT, |
| 2947 | (struct lys_node *)(&llist->dflt[i])) == -1) { |
| 2948 | goto error; |
| 2949 | } |
| 2950 | } |
| 2951 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2952 | if (llist_orig->when) { |
| 2953 | llist->when = lys_when_dup(ctx, llist_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2954 | } |
| 2955 | break; |
| 2956 | |
| 2957 | case LYS_LIST: |
| 2958 | list->min = list_orig->min; |
| 2959 | list->max = list_orig->max; |
| 2960 | |
| 2961 | list->must_size = list_orig->must_size; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2962 | list->must = lys_restr_dup(ctx, list_orig->must, list->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2963 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2964 | list->tpdf_size = list_orig->tpdf_size; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2965 | 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] | 2966 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2967 | list->keys_size = list_orig->keys_size; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2968 | if (list->keys_size) { |
| 2969 | list->keys = calloc(list->keys_size, sizeof *list->keys); |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 2970 | list->keys_str = lydict_insert(ctx, list_orig->keys_str, 0); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2971 | if (!list->keys) { |
| 2972 | LOGMEM; |
| 2973 | goto error; |
| 2974 | } |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 2975 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2976 | if (!shallow) { |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 2977 | /* the keys are going to be resolved only if the list is instantiated in data tree, not just |
| 2978 | * in another grouping */ |
| 2979 | for (iter = parent; iter && iter->nodetype != LYS_GROUPING; iter = iter->parent); |
| 2980 | if (!iter && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) { |
| 2981 | goto error; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2982 | } |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 2983 | } else { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2984 | memcpy(list->keys, list_orig->keys, list->keys_size * sizeof *list->keys); |
Radek Krejci | a01e543 | 2015-06-16 10:35:25 +0200 | [diff] [blame] | 2985 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2986 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2987 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2988 | list->unique_size = list_orig->unique_size; |
| 2989 | list->unique = malloc(list->unique_size * sizeof *list->unique); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2990 | if (!list->unique) { |
| 2991 | LOGMEM; |
| 2992 | goto error; |
| 2993 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2994 | for (i = 0; i < list->unique_size; ++i) { |
| 2995 | list->unique[i].expr_size = list_orig->unique[i].expr_size; |
| 2996 | 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] | 2997 | if (!list->unique[i].expr) { |
| 2998 | LOGMEM; |
| 2999 | goto error; |
| 3000 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3001 | for (j = 0; j < list->unique[i].expr_size; j++) { |
| 3002 | list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0); |
| 3003 | |
| 3004 | /* if it stays in unres list, duplicate it also there */ |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 3005 | unique_info = malloc(sizeof *unique_info); |
| 3006 | unique_info->list = (struct lys_node *)list; |
| 3007 | unique_info->expr = list->unique[i].expr[j]; |
| 3008 | unique_info->trg_type = &list->unique[i].trg_type; |
| 3009 | unres_schema_dup(module, unres, &list_orig, UNRES_LIST_UNIQ, unique_info); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3010 | } |
| 3011 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 3012 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3013 | if (list_orig->when) { |
| 3014 | list->when = lys_when_dup(ctx, list_orig->when); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 3015 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 3016 | break; |
| 3017 | |
| 3018 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3019 | case LYS_ANYDATA: |
| 3020 | any->must_size = any_orig->must_size; |
| 3021 | any->must = lys_restr_dup(ctx, any_orig->must, any->must_size); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3022 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3023 | if (any_orig->when) { |
| 3024 | any->when = lys_when_dup(ctx, any_orig->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3025 | } |
| 3026 | break; |
| 3027 | |
| 3028 | case LYS_USES: |
| 3029 | uses->grp = uses_orig->grp; |
| 3030 | |
| 3031 | if (uses_orig->when) { |
| 3032 | uses->when = lys_when_dup(ctx, uses_orig->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3033 | } |
| 3034 | |
| 3035 | uses->refine_size = uses_orig->refine_size; |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 3036 | 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] | 3037 | uses->augment_size = uses_orig->augment_size; |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3038 | if (!shallow) { |
| 3039 | uses->augment = lys_augment_dup(module, (struct lys_node *)uses, uses_orig->augment, uses_orig->augment_size); |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 3040 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 3041 | if (!uses->grp || ((uint8_t*)&uses->grp->flags)[1]) { |
| 3042 | #else |
| 3043 | if (!uses->grp || ((uint8_t*)&uses->grp->flags)[0]) { |
| 3044 | #endif |
Michal Vasko | cda5171 | 2016-05-19 15:22:11 +0200 | [diff] [blame] | 3045 | assert(!uses->child); |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3046 | if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL) == -1) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3047 | goto error; |
| 3048 | } |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3049 | } |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3050 | } else { |
| 3051 | memcpy(uses->augment, uses_orig->augment, uses->augment_size * sizeof *uses->augment); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3052 | } |
| 3053 | break; |
| 3054 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 3055 | case LYS_CASE: |
| 3056 | if (cs_orig->when) { |
| 3057 | cs->when = lys_when_dup(ctx, cs_orig->when); |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 3058 | } |
| 3059 | break; |
| 3060 | |
| 3061 | case LYS_GROUPING: |
| 3062 | grp->tpdf_size = grp_orig->tpdf_size; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3063 | 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] | 3064 | break; |
| 3065 | |
Radek Krejci | 9693540 | 2016-11-04 16:27:28 +0100 | [diff] [blame] | 3066 | case LYS_ACTION: |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 3067 | case LYS_RPC: |
| 3068 | rpc->tpdf_size = rpc_orig->tpdf_size; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3069 | 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] | 3070 | break; |
| 3071 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3072 | case LYS_INPUT: |
| 3073 | case LYS_OUTPUT: |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3074 | io->tpdf_size = io_orig->tpdf_size; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3075 | 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] | 3076 | break; |
| 3077 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3078 | case LYS_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3079 | ntf->tpdf_size = ntf_orig->tpdf_size; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3080 | 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] | 3081 | break; |
| 3082 | |
Radek Krejci | 9693540 | 2016-11-04 16:27:28 +0100 | [diff] [blame] | 3083 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3084 | default: |
| 3085 | /* LY_NODE_AUGMENT */ |
| 3086 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3087 | goto error; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3088 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3089 | |
| 3090 | return retval; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3091 | |
| 3092 | error: |
| 3093 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3094 | lys_node_free(retval, NULL, 0); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3095 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3096 | } |
| 3097 | |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 3098 | int |
| 3099 | lys_has_xpath(const struct lys_node *node) |
| 3100 | { |
| 3101 | assert(node); |
| 3102 | |
| 3103 | switch (node->nodetype) { |
| 3104 | case LYS_AUGMENT: |
| 3105 | if (((struct lys_node_augment *)node)->when) { |
| 3106 | return 1; |
| 3107 | } |
| 3108 | break; |
| 3109 | case LYS_CASE: |
| 3110 | if (((struct lys_node_case *)node)->when) { |
| 3111 | return 1; |
| 3112 | } |
| 3113 | break; |
| 3114 | case LYS_CHOICE: |
| 3115 | if (((struct lys_node_choice *)node)->when) { |
| 3116 | return 1; |
| 3117 | } |
| 3118 | break; |
| 3119 | case LYS_ANYDATA: |
| 3120 | if (((struct lys_node_anydata *)node)->when || ((struct lys_node_anydata *)node)->must_size) { |
| 3121 | return 1; |
| 3122 | } |
| 3123 | break; |
| 3124 | case LYS_LEAF: |
| 3125 | if (((struct lys_node_leaf *)node)->when || ((struct lys_node_leaf *)node)->must_size) { |
| 3126 | return 1; |
| 3127 | } |
| 3128 | break; |
| 3129 | case LYS_LEAFLIST: |
| 3130 | if (((struct lys_node_leaflist *)node)->when || ((struct lys_node_leaflist *)node)->must_size) { |
| 3131 | return 1; |
| 3132 | } |
| 3133 | break; |
| 3134 | case LYS_LIST: |
| 3135 | if (((struct lys_node_list *)node)->when || ((struct lys_node_list *)node)->must_size) { |
| 3136 | return 1; |
| 3137 | } |
| 3138 | break; |
| 3139 | case LYS_CONTAINER: |
| 3140 | if (((struct lys_node_container *)node)->when || ((struct lys_node_container *)node)->must_size) { |
| 3141 | return 1; |
| 3142 | } |
| 3143 | break; |
| 3144 | case LYS_INPUT: |
| 3145 | case LYS_OUTPUT: |
| 3146 | if (((struct lys_node_inout *)node)->must_size) { |
| 3147 | return 1; |
| 3148 | } |
| 3149 | break; |
| 3150 | case LYS_NOTIF: |
| 3151 | if (((struct lys_node_notif *)node)->must_size) { |
| 3152 | return 1; |
| 3153 | } |
| 3154 | break; |
| 3155 | case LYS_USES: |
| 3156 | if (((struct lys_node_uses *)node)->when) { |
| 3157 | return 1; |
| 3158 | } |
| 3159 | break; |
| 3160 | default: |
| 3161 | /* does not have XPath */ |
| 3162 | break; |
| 3163 | } |
| 3164 | |
| 3165 | return 0; |
| 3166 | } |
| 3167 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3168 | struct lys_node * |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 3169 | lys_node_dup(struct lys_module *module, struct lys_node *parent, const struct lys_node *node, |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3170 | struct unres_schema *unres, int shallow) |
| 3171 | { |
| 3172 | struct lys_node *p = NULL; |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 3173 | int finalize = 0; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3174 | struct lys_node *result, *iter, *next; |
| 3175 | |
| 3176 | if (!shallow) { |
| 3177 | /* get know where in schema tre we are to know what should be done during instantiation of the grouping */ |
| 3178 | for (p = parent; |
| 3179 | p && !(p->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC | LYS_ACTION | LYS_GROUPING)); |
| 3180 | p = lys_parent(p)); |
| 3181 | finalize = p ? ((p->nodetype == LYS_GROUPING) ? 0 : 2) : 1; |
| 3182 | } |
| 3183 | |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 3184 | result = lys_node_dup_recursion(module, parent, node, unres, shallow, finalize); |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3185 | if (finalize) { |
| 3186 | /* check xpath expressions in the instantiated tree */ |
| 3187 | for (iter = next = parent->child; iter; iter = next) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 3188 | if (lys_has_xpath(iter) && unres_schema_add_node(module, unres, iter, UNRES_XPATH, NULL) == -1) { |
Radek Krejci | 3c48d04 | 2016-11-07 14:42:36 +0100 | [diff] [blame] | 3189 | /* invalid xpath */ |
| 3190 | return NULL; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3191 | } |
| 3192 | |
| 3193 | /* select next item */ |
| 3194 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA | LYS_GROUPING)) { |
| 3195 | /* child exception for leafs, leaflists and anyxml without children, ignore groupings */ |
| 3196 | next = NULL; |
| 3197 | } else { |
| 3198 | next = iter->child; |
| 3199 | } |
| 3200 | if (!next) { |
| 3201 | /* no children, try siblings */ |
| 3202 | next = iter->next; |
| 3203 | } |
| 3204 | while (!next) { |
| 3205 | /* parent is already processed, go to its sibling */ |
| 3206 | iter = lys_parent(iter); |
| 3207 | if (iter == parent) { |
| 3208 | /* we are done, no next element to process */ |
| 3209 | break; |
| 3210 | } |
| 3211 | next = iter->next; |
| 3212 | } |
| 3213 | } |
| 3214 | } |
| 3215 | |
| 3216 | return result; |
| 3217 | } |
| 3218 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 3219 | void |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3220 | lys_node_switch(struct lys_node *dst, struct lys_node *src) |
| 3221 | { |
| 3222 | struct lys_node *child; |
| 3223 | |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 3224 | 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] | 3225 | |
| 3226 | /* sibling next */ |
Michal Vasko | 8328da8 | 2016-08-25 09:27:22 +0200 | [diff] [blame] | 3227 | if (dst->prev->next) { |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3228 | dst->prev->next = src; |
| 3229 | } |
| 3230 | |
| 3231 | /* sibling prev */ |
| 3232 | if (dst->next) { |
| 3233 | dst->next->prev = src; |
Michal Vasko | 8328da8 | 2016-08-25 09:27:22 +0200 | [diff] [blame] | 3234 | } else { |
| 3235 | for (child = dst->prev; child->prev->next; child = child->prev); |
| 3236 | child->prev = src; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | /* next */ |
| 3240 | src->next = dst->next; |
| 3241 | dst->next = NULL; |
| 3242 | |
| 3243 | /* prev */ |
| 3244 | if (dst->prev != dst) { |
| 3245 | src->prev = dst->prev; |
| 3246 | } |
| 3247 | dst->prev = dst; |
| 3248 | |
| 3249 | /* parent child */ |
| 3250 | if (dst->parent && (dst->parent->child == dst)) { |
| 3251 | dst->parent->child = src; |
| 3252 | } |
| 3253 | |
| 3254 | /* parent */ |
| 3255 | src->parent = dst->parent; |
| 3256 | dst->parent = NULL; |
| 3257 | |
| 3258 | /* child parent */ |
| 3259 | LY_TREE_FOR(dst->child, child) { |
| 3260 | if (child->parent == dst) { |
| 3261 | child->parent = src; |
| 3262 | } |
| 3263 | } |
| 3264 | |
| 3265 | /* child */ |
| 3266 | src->child = dst->child; |
| 3267 | dst->child = NULL; |
| 3268 | } |
| 3269 | |
| 3270 | void |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 3271 | 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] | 3272 | { |
| 3273 | struct ly_ctx *ctx; |
| 3274 | int i; |
| 3275 | |
| 3276 | if (!module) { |
| 3277 | return; |
| 3278 | } |
| 3279 | |
| 3280 | /* remove schema from the context */ |
| 3281 | ctx = module->ctx; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 3282 | if (remove_from_ctx && ctx->models.used) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3283 | for (i = 0; i < ctx->models.used; i++) { |
| 3284 | if (ctx->models.list[i] == module) { |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 3285 | /* move all the models to not change the order in the list */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3286 | ctx->models.used--; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 3287 | 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] | 3288 | ctx->models.list[ctx->models.used] = NULL; |
| 3289 | /* we are done */ |
| 3290 | break; |
| 3291 | } |
| 3292 | } |
| 3293 | } |
| 3294 | |
| 3295 | /* common part with struct ly_submodule */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 3296 | module_free_common(module, private_destructor); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3297 | |
| 3298 | /* specific items to free */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 3299 | lydict_remove(ctx, module->ns); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3300 | |
| 3301 | free(module); |
| 3302 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3303 | |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 3304 | static void |
| 3305 | lys_features_disable_recursive(struct lys_feature *f) |
| 3306 | { |
| 3307 | unsigned int i; |
| 3308 | struct lys_feature *depf; |
| 3309 | |
| 3310 | /* disable the feature */ |
| 3311 | f->flags &= ~LYS_FENABLED; |
| 3312 | |
| 3313 | /* by disabling feature we have to disable also all features that depends on this feature */ |
| 3314 | if (f->depfeatures) { |
| 3315 | for (i = 0; i < f->depfeatures->number; i++) { |
| 3316 | depf = (struct lys_feature *)f->depfeatures->set.g[i]; |
| 3317 | if (depf->flags & LYS_FENABLED) { |
| 3318 | lys_features_disable_recursive(depf); |
| 3319 | } |
| 3320 | } |
| 3321 | } |
| 3322 | } |
| 3323 | |
| 3324 | |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3325 | /* |
| 3326 | * op: 1 - enable, 0 - disable |
| 3327 | */ |
| 3328 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3329 | 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] | 3330 | { |
| 3331 | int all = 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3332 | int i, j, k; |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3333 | int progress, faili, failj, failk; |
| 3334 | |
| 3335 | uint8_t fsize; |
| 3336 | struct lys_feature *f; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3337 | |
| 3338 | if (!module || !name || !strlen(name)) { |
| 3339 | return EXIT_FAILURE; |
| 3340 | } |
| 3341 | |
| 3342 | if (!strcmp(name, "*")) { |
| 3343 | /* enable all */ |
| 3344 | all = 1; |
| 3345 | } |
| 3346 | |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3347 | progress = failk = 1; |
| 3348 | while (progress && failk) { |
| 3349 | for (i = -1, failk = progress = 0; i < module->inc_size; i++) { |
| 3350 | if (i == -1) { |
| 3351 | fsize = module->features_size; |
| 3352 | f = module->features; |
| 3353 | } else { |
| 3354 | fsize = module->inc[i].submodule->features_size; |
| 3355 | f = module->inc[i].submodule->features; |
| 3356 | } |
| 3357 | |
| 3358 | for (j = 0; j < fsize; j++) { |
| 3359 | if (all || !strcmp(f[j].name, name)) { |
Michal Vasko | 34c3b55 | 2016-11-21 09:07:43 +0100 | [diff] [blame] | 3360 | if ((op && (f[j].flags & LYS_FENABLED)) || (!op && !(f[j].flags & LYS_FENABLED))) { |
| 3361 | if (all) { |
| 3362 | /* skip already set features */ |
| 3363 | continue; |
| 3364 | } else { |
| 3365 | /* feature already set correctly */ |
| 3366 | return EXIT_SUCCESS; |
| 3367 | } |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3368 | } |
| 3369 | |
| 3370 | if (op) { |
| 3371 | /* check referenced features if they are enabled */ |
| 3372 | for (k = 0; k < f[j].iffeature_size; k++) { |
| 3373 | if (!resolve_iffeature(&f[j].iffeature[k])) { |
| 3374 | if (all) { |
| 3375 | faili = i; |
| 3376 | failj = j; |
| 3377 | failk = k + 1; |
| 3378 | break; |
| 3379 | } else { |
| 3380 | LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.", |
| 3381 | f[j].name, k + 1); |
| 3382 | return EXIT_FAILURE; |
| 3383 | } |
| 3384 | } |
| 3385 | } |
| 3386 | |
| 3387 | if (k == f[j].iffeature_size) { |
| 3388 | /* the last check passed, do the change */ |
| 3389 | f[j].flags |= LYS_FENABLED; |
| 3390 | progress++; |
| 3391 | } |
| 3392 | } else { |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 3393 | lys_features_disable_recursive(&f[j]); |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3394 | progress++; |
| 3395 | } |
| 3396 | if (!all) { |
| 3397 | /* stop in case changing a single feature */ |
| 3398 | return EXIT_SUCCESS; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3399 | } |
| 3400 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3401 | } |
| 3402 | } |
| 3403 | } |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3404 | if (failk) { |
| 3405 | /* print info about the last failing feature */ |
| 3406 | LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.", |
| 3407 | faili == -1 ? module->features[failj].name : module->inc[faili].submodule->features[failj].name, failk); |
| 3408 | return EXIT_FAILURE; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3409 | } |
| 3410 | |
| 3411 | if (all) { |
| 3412 | return EXIT_SUCCESS; |
| 3413 | } else { |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3414 | /* the specified feature not found */ |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3415 | return EXIT_FAILURE; |
| 3416 | } |
| 3417 | } |
| 3418 | |
| 3419 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3420 | lys_features_enable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3421 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3422 | return lys_features_change(module, feature, 1); |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3423 | } |
| 3424 | |
| 3425 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3426 | lys_features_disable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3427 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3428 | return lys_features_change(module, feature, 0); |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3429 | } |
| 3430 | |
| 3431 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3432 | lys_features_state(const struct lys_module *module, const char *feature) |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3433 | { |
| 3434 | int i, j; |
| 3435 | |
| 3436 | if (!module || !feature) { |
| 3437 | return -1; |
| 3438 | } |
| 3439 | |
| 3440 | /* search for the specified feature */ |
| 3441 | /* module itself */ |
| 3442 | for (i = 0; i < module->features_size; i++) { |
| 3443 | if (!strcmp(feature, module->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3444 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3445 | return 1; |
| 3446 | } else { |
| 3447 | return 0; |
| 3448 | } |
| 3449 | } |
| 3450 | } |
| 3451 | |
| 3452 | /* submodules */ |
| 3453 | for (j = 0; j < module->inc_size; j++) { |
| 3454 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
| 3455 | if (!strcmp(feature, module->inc[j].submodule->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3456 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3457 | return 1; |
| 3458 | } else { |
| 3459 | return 0; |
| 3460 | } |
| 3461 | } |
| 3462 | } |
| 3463 | } |
| 3464 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3465 | /* feature definition not found */ |
| 3466 | return -1; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3467 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3468 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3469 | API const char ** |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3470 | lys_features_list(const struct lys_module *module, uint8_t **states) |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3471 | { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3472 | const char **result = NULL; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3473 | int i, j; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3474 | unsigned int count; |
| 3475 | |
| 3476 | if (!module) { |
| 3477 | return NULL; |
| 3478 | } |
| 3479 | |
| 3480 | count = module->features_size; |
| 3481 | for (i = 0; i < module->inc_size; i++) { |
| 3482 | count += module->inc[i].submodule->features_size; |
| 3483 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3484 | result = malloc((count + 1) * sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3485 | if (!result) { |
| 3486 | LOGMEM; |
| 3487 | return NULL; |
| 3488 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3489 | if (states) { |
| 3490 | *states = malloc((count + 1) * sizeof **states); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3491 | if (!(*states)) { |
| 3492 | LOGMEM; |
| 3493 | free(result); |
| 3494 | return NULL; |
| 3495 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3496 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3497 | count = 0; |
| 3498 | |
| 3499 | /* module itself */ |
| 3500 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3501 | result[count] = module->features[i].name; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3502 | if (states) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3503 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3504 | (*states)[count] = 1; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3505 | } else { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3506 | (*states)[count] = 0; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3507 | } |
| 3508 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3509 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3510 | } |
| 3511 | |
| 3512 | /* submodules */ |
| 3513 | for (j = 0; j < module->inc_size; j++) { |
| 3514 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3515 | result[count] = module->inc[j].submodule->features[i].name; |
Radek Krejci | 374b94e | 2015-08-13 09:44:22 +0200 | [diff] [blame] | 3516 | if (states) { |
| 3517 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
| 3518 | (*states)[count] = 1; |
| 3519 | } else { |
| 3520 | (*states)[count] = 0; |
| 3521 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3522 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3523 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3524 | } |
| 3525 | } |
| 3526 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3527 | /* terminating NULL byte */ |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3528 | result[count] = NULL; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3529 | |
| 3530 | return result; |
| 3531 | } |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3532 | |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 3533 | API struct lys_module * |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 3534 | lys_node_module(const struct lys_node *node) |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3535 | { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 3536 | if (!node) { |
| 3537 | return NULL; |
| 3538 | } |
| 3539 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3540 | return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module; |
| 3541 | } |
| 3542 | |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 3543 | API struct lys_module * |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 3544 | lys_main_module(const struct lys_module *module) |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 3545 | { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 3546 | if (!module) { |
| 3547 | return NULL; |
| 3548 | } |
| 3549 | |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 3550 | return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module); |
| 3551 | } |
| 3552 | |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3553 | API struct lys_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3554 | lys_parent(const struct lys_node *node) |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3555 | { |
| 3556 | if (!node || !node->parent) { |
| 3557 | return NULL; |
| 3558 | } |
| 3559 | |
| 3560 | if (node->parent->nodetype == LYS_AUGMENT) { |
| 3561 | return ((struct lys_node_augment *)node->parent)->target; |
| 3562 | } |
| 3563 | |
| 3564 | return node->parent; |
| 3565 | } |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3566 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 3567 | API void * |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3568 | lys_set_private(const struct lys_node *node, void *priv) |
| 3569 | { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 3570 | void *prev; |
| 3571 | |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3572 | if (!node) { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 3573 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 3574 | return NULL; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3575 | } |
| 3576 | |
Mislav Novakovic | b5529e5 | 2016-02-29 11:42:43 +0100 | [diff] [blame] | 3577 | prev = node->priv; |
| 3578 | ((struct lys_node *)node)->priv = priv; |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 3579 | |
| 3580 | return prev; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3581 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3582 | |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3583 | int |
| 3584 | lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref) |
| 3585 | { |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 3586 | struct lys_node_leaf *iter = leafref_target; |
| 3587 | |
Michal Vasko | 48a573d | 2016-07-01 11:46:02 +0200 | [diff] [blame] | 3588 | if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3589 | LOGINT; |
| 3590 | return -1; |
| 3591 | } |
| 3592 | |
Pavol Vican | 9317515 | 2016-08-30 15:34:44 +0200 | [diff] [blame] | 3593 | /* check for config flag */ |
| 3594 | if ((leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) { |
| 3595 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, leafref, |
| 3596 | "The %s is config but refers to a non-config %s.", |
| 3597 | strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype)); |
| 3598 | return -1; |
| 3599 | } |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 3600 | /* check for cycles */ |
| 3601 | while (iter && iter->type.base == LY_TYPE_LEAFREF) { |
| 3602 | if ((void *)iter == (void *)leafref) { |
| 3603 | /* cycle detected */ |
| 3604 | LOGVAL(LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref); |
| 3605 | return -1; |
| 3606 | } |
| 3607 | iter = iter->type.info.lref.target; |
| 3608 | } |
| 3609 | |
| 3610 | /* create fake child - the ly_set structure to hold the list of |
Michal Vasko | 48a573d | 2016-07-01 11:46:02 +0200 | [diff] [blame] | 3611 | * leafrefs referencing the leaf(-list) */ |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 3612 | if (!leafref_target->backlinks) { |
| 3613 | leafref_target->backlinks = (void*)ly_set_new(); |
| 3614 | if (!leafref_target->backlinks) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3615 | LOGMEM; |
| 3616 | return -1; |
| 3617 | } |
| 3618 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3619 | ly_set_add(leafref_target->backlinks, leafref, 0); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3620 | |
| 3621 | return 0; |
| 3622 | } |
| 3623 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3624 | /* not needed currently */ |
| 3625 | #if 0 |
| 3626 | |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3627 | static const char * |
| 3628 | lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len) |
| 3629 | { |
| 3630 | struct lys_module *prev_mod; |
| 3631 | uint32_t str_len, mod_len, buf_idx; |
| 3632 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3633 | if (!(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3634 | LOGINT; |
| 3635 | return NULL; |
| 3636 | } |
| 3637 | |
| 3638 | buf_idx = buf_len - 1; |
| 3639 | buf[buf_idx] = '\0'; |
| 3640 | |
| 3641 | while (node) { |
| 3642 | if (lys_parent(node)) { |
| 3643 | prev_mod = lys_node_module(lys_parent(node)); |
| 3644 | } else { |
| 3645 | prev_mod = NULL; |
| 3646 | } |
| 3647 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3648 | if (node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3649 | str_len = strlen(node->name); |
| 3650 | |
| 3651 | if (prev_mod != node->module) { |
| 3652 | mod_len = strlen(node->module->name); |
| 3653 | } else { |
| 3654 | mod_len = 0; |
| 3655 | } |
| 3656 | |
| 3657 | if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) { |
| 3658 | LOGINT; |
| 3659 | return NULL; |
| 3660 | } |
| 3661 | |
| 3662 | buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len; |
| 3663 | |
| 3664 | buf[buf_idx] = '/'; |
| 3665 | if (mod_len) { |
| 3666 | memcpy(buf + buf_idx + 1, node->module->name, mod_len); |
| 3667 | buf[buf_idx + 1 + mod_len] = ':'; |
| 3668 | } |
| 3669 | memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len); |
| 3670 | } |
| 3671 | |
| 3672 | node = lys_parent(node); |
| 3673 | } |
| 3674 | |
| 3675 | return buf + buf_idx; |
| 3676 | } |
| 3677 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3678 | #endif |
| 3679 | |
| 3680 | API struct ly_set * |
Michal Vasko | f06fb5b | 2016-09-08 10:05:56 +0200 | [diff] [blame] | 3681 | lys_find_xpath(const struct lys_node *node, const char *expr, int options) |
Michal Vasko | 46a4bf9 | 2016-09-08 08:23:49 +0200 | [diff] [blame] | 3682 | { |
| 3683 | struct lyxp_set set; |
| 3684 | struct ly_set *ret_set; |
| 3685 | uint32_t i; |
Michal Vasko | db1da03 | 2016-09-08 10:07:38 +0200 | [diff] [blame] | 3686 | int opts; |
Michal Vasko | 46a4bf9 | 2016-09-08 08:23:49 +0200 | [diff] [blame] | 3687 | |
| 3688 | if (!node || !expr) { |
| 3689 | ly_errno = LY_EINVAL; |
| 3690 | return NULL; |
| 3691 | } |
| 3692 | |
| 3693 | memset(&set, 0, sizeof set); |
| 3694 | |
Michal Vasko | db1da03 | 2016-09-08 10:07:38 +0200 | [diff] [blame] | 3695 | opts = LYXP_SNODE; |
| 3696 | if (options & LYS_FIND_OUTPUT) { |
| 3697 | opts |= LYXP_SNODE_OUTPUT; |
| 3698 | } |
| 3699 | |
Michal Vasko | 46a4bf9 | 2016-09-08 08:23:49 +0200 | [diff] [blame] | 3700 | /* node and nodetype won't matter at all since it is absolute */ |
Michal Vasko | db1da03 | 2016-09-08 10:07:38 +0200 | [diff] [blame] | 3701 | if (lyxp_atomize(expr, node, LYXP_NODE_ELEM, &set, opts)) { |
Michal Vasko | 46a4bf9 | 2016-09-08 08:23:49 +0200 | [diff] [blame] | 3702 | free(set.val.snodes); |
Radek Krejci | 9ee20c3 | 2016-11-09 00:04:13 +0100 | [diff] [blame] | 3703 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "Resolving XPath expression \"%s\" failed.", expr); |
Michal Vasko | 46a4bf9 | 2016-09-08 08:23:49 +0200 | [diff] [blame] | 3704 | return NULL; |
| 3705 | } |
| 3706 | |
| 3707 | ret_set = ly_set_new(); |
| 3708 | |
| 3709 | for (i = 0; i < set.used; ++i) { |
| 3710 | if (!set.val.snodes[i].in_ctx) { |
| 3711 | continue; |
| 3712 | } |
| 3713 | assert(set.val.snodes[i].in_ctx == 1); |
| 3714 | |
| 3715 | switch (set.val.snodes[i].type) { |
| 3716 | case LYXP_NODE_ELEM: |
| 3717 | if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) { |
| 3718 | ly_set_free(ret_set); |
| 3719 | free(set.val.snodes); |
| 3720 | return NULL; |
| 3721 | } |
| 3722 | break; |
| 3723 | default: |
| 3724 | /* ignore roots, text and attr should not ever appear */ |
| 3725 | break; |
| 3726 | } |
| 3727 | } |
| 3728 | |
| 3729 | free(set.val.snodes); |
| 3730 | return ret_set; |
| 3731 | } |
| 3732 | |
| 3733 | API struct ly_set * |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3734 | lys_xpath_atomize(const struct lys_node *cur_snode, enum lyxp_node_type cur_snode_type, const char *expr, int options) |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3735 | { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3736 | struct lyxp_set set; |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3737 | struct ly_set *ret_set; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3738 | uint32_t i; |
| 3739 | |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 3740 | if (!cur_snode || !expr) { |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3741 | return NULL; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3742 | } |
| 3743 | |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 3744 | /* adjust the root */ |
| 3745 | if ((cur_snode_type == LYXP_NODE_ROOT) || (cur_snode_type == LYXP_NODE_ROOT_CONFIG)) { |
| 3746 | do { |
| 3747 | cur_snode = lys_getnext(NULL, NULL, lys_node_module(cur_snode), 0); |
| 3748 | } while ((cur_snode_type == LYXP_NODE_ROOT_CONFIG) && (cur_snode->flags & LYS_CONFIG_R)); |
| 3749 | } |
| 3750 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3751 | memset(&set, 0, sizeof set); |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3752 | |
| 3753 | if (options & LYXP_MUST) { |
| 3754 | options &= ~LYXP_MUST; |
| 3755 | options |= LYXP_SNODE_MUST; |
| 3756 | } else if (options & LYXP_WHEN) { |
| 3757 | options &= ~LYXP_WHEN; |
| 3758 | options |= LYXP_SNODE_WHEN; |
| 3759 | } else { |
| 3760 | options |= LYXP_SNODE; |
| 3761 | } |
| 3762 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3763 | if (lyxp_atomize(expr, cur_snode, cur_snode_type, &set, options)) { |
| 3764 | free(set.val.snodes); |
Radek Krejci | 9ee20c3 | 2016-11-09 00:04:13 +0100 | [diff] [blame] | 3765 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, cur_snode, "Resolving XPath expression \"%s\" failed.", expr); |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3766 | return NULL; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3767 | } |
| 3768 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3769 | ret_set = ly_set_new(); |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3770 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3771 | for (i = 0; i < set.used; ++i) { |
| 3772 | switch (set.val.snodes[i].type) { |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3773 | case LYXP_NODE_ELEM: |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3774 | if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) { |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3775 | ly_set_free(ret_set); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3776 | free(set.val.snodes); |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3777 | return NULL; |
| 3778 | } |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3779 | break; |
| 3780 | default: |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3781 | /* ignore roots, text and attr should not ever appear */ |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3782 | break; |
| 3783 | } |
| 3784 | } |
| 3785 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3786 | free(set.val.snodes); |
| 3787 | return ret_set; |
| 3788 | } |
| 3789 | |
| 3790 | API struct ly_set * |
| 3791 | lys_node_xpath_atomize(const struct lys_node *node, int options) |
| 3792 | { |
| 3793 | const struct lys_node *next, *elem, *parent, *tmp; |
| 3794 | struct lyxp_set set; |
| 3795 | struct ly_set *ret_set; |
| 3796 | uint16_t i; |
| 3797 | |
| 3798 | if (!node) { |
| 3799 | return NULL; |
| 3800 | } |
| 3801 | |
| 3802 | for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent)); |
| 3803 | if (!parent) { |
| 3804 | /* not in input, output, or notification */ |
| 3805 | return NULL; |
| 3806 | } |
| 3807 | |
| 3808 | ret_set = ly_set_new(); |
Radek Krejci | d9af8d2 | 2016-09-07 18:44:26 +0200 | [diff] [blame] | 3809 | if (!ret_set) { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3810 | return NULL; |
| 3811 | } |
| 3812 | |
| 3813 | LY_TREE_DFS_BEGIN(node, next, elem) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3814 | if ((options & LYXP_NO_LOCAL) && !(elem->flags & LYS_XPATH_DEP)) { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3815 | /* elem has no dependencies from other subtrees and local nodes get discarded */ |
| 3816 | goto next_iter; |
| 3817 | } |
| 3818 | |
| 3819 | if (lyxp_node_atomize(elem, &set)) { |
| 3820 | ly_set_free(ret_set); |
| 3821 | free(set.val.snodes); |
| 3822 | return NULL; |
| 3823 | } |
| 3824 | |
| 3825 | for (i = 0; i < set.used; ++i) { |
| 3826 | switch (set.val.snodes[i].type) { |
| 3827 | case LYXP_NODE_ELEM: |
| 3828 | if (options & LYXP_NO_LOCAL) { |
| 3829 | for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp)); |
| 3830 | if (tmp) { |
| 3831 | /* in local subtree, discard */ |
| 3832 | break; |
| 3833 | } |
| 3834 | } |
| 3835 | if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) { |
| 3836 | ly_set_free(ret_set); |
| 3837 | free(set.val.snodes); |
| 3838 | return NULL; |
| 3839 | } |
| 3840 | break; |
| 3841 | default: |
| 3842 | /* ignore roots, text and attr should not ever appear */ |
| 3843 | break; |
| 3844 | } |
| 3845 | } |
| 3846 | |
| 3847 | free(set.val.snodes); |
| 3848 | if (!(options & LYXP_RECURSIVE)) { |
| 3849 | break; |
| 3850 | } |
| 3851 | next_iter: |
| 3852 | LY_TREE_DFS_END(node, next, elem); |
| 3853 | } |
| 3854 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3855 | return ret_set; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3856 | } |
| 3857 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3858 | static void |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3859 | 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] | 3860 | { |
| 3861 | int ret; |
| 3862 | char *parent_path; |
| 3863 | struct lys_node *target; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3864 | |
Pavol Vican | 64d0b76 | 2016-08-25 10:44:59 +0200 | [diff] [blame] | 3865 | if (!dev->deviate) { |
| 3866 | return ; |
| 3867 | } |
| 3868 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3869 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 3870 | if (dev->orig_node) { |
| 3871 | /* removing not-supported deviation ... */ |
| 3872 | if (strrchr(dev->target_name, '/') != dev->target_name) { |
| 3873 | /* ... from a parent */ |
| 3874 | parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name); |
| 3875 | |
| 3876 | target = NULL; |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 3877 | ret = resolve_augment_schema_nodeid(parent_path, NULL, target_module, 1, |
| 3878 | (const struct lys_node **)&target); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3879 | free(parent_path); |
| 3880 | if (ret || !target) { |
| 3881 | LOGINT; |
| 3882 | return; |
| 3883 | } |
| 3884 | |
| 3885 | lys_node_addchild(target, NULL, dev->orig_node); |
| 3886 | } else { |
| 3887 | /* ... from top-level data */ |
| 3888 | lys_node_addchild(NULL, (struct lys_module *)target_module, dev->orig_node); |
| 3889 | } |
| 3890 | |
| 3891 | dev->orig_node = NULL; |
| 3892 | } else { |
| 3893 | /* adding not-supported deviation */ |
| 3894 | target = NULL; |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 3895 | ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, 1, |
| 3896 | (const struct lys_node **)&target); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3897 | if (ret || !target) { |
| 3898 | LOGINT; |
| 3899 | return; |
| 3900 | } |
| 3901 | |
| 3902 | lys_node_unlink(target); |
| 3903 | dev->orig_node = target; |
| 3904 | } |
| 3905 | } else { |
| 3906 | target = NULL; |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 3907 | ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, 1, |
| 3908 | (const struct lys_node **)&target); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3909 | if (ret || !target) { |
| 3910 | LOGINT; |
| 3911 | return; |
| 3912 | } |
| 3913 | |
| 3914 | lys_node_switch(target, dev->orig_node); |
| 3915 | dev->orig_node = target; |
| 3916 | } |
| 3917 | } |
| 3918 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3919 | /* temporarily removes or applies deviations, updates module deviation flag accordingly */ |
| 3920 | void |
| 3921 | lys_switch_deviations(struct lys_module *module) |
| 3922 | { |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3923 | uint32_t i = 0, j; |
| 3924 | const struct lys_module *mod; |
| 3925 | const char *ptr; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3926 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3927 | if (module->deviated) { |
| 3928 | while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) { |
| 3929 | if (mod == module) { |
| 3930 | continue; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3931 | } |
| 3932 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3933 | for (j = 0; j < mod->deviation_size; ++j) { |
| 3934 | ptr = strstr(mod->deviation[j].target_name, module->name); |
| 3935 | if (ptr && ptr[strlen(module->name)] == ':') { |
| 3936 | lys_switch_deviation(&mod->deviation[j], module); |
| 3937 | } |
| 3938 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3939 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3940 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3941 | if (module->deviated == 2) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3942 | module->deviated = 1; |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3943 | } else { |
| 3944 | module->deviated = 2; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3945 | } |
| 3946 | } |
| 3947 | } |
| 3948 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 3949 | static void |
| 3950 | apply_dev(struct lys_deviation *dev, const struct lys_module *module) |
| 3951 | { |
| 3952 | lys_switch_deviation(dev, module); |
| 3953 | |
| 3954 | assert(dev->orig_node); |
| 3955 | lys_node_module(dev->orig_node)->deviated = 1; |
| 3956 | } |
| 3957 | |
| 3958 | static void |
| 3959 | apply_aug(struct lys_node_augment *augment) |
| 3960 | { |
| 3961 | struct lys_node *last; |
| 3962 | |
| 3963 | assert(augment->target && (augment->flags & LYS_NOTAPPLIED)); |
| 3964 | |
| 3965 | /* reconnect augmenting data into the target - add them to the target child list */ |
| 3966 | if (augment->target->child) { |
| 3967 | last = augment->target->child->prev; |
| 3968 | last->next = augment->child; |
| 3969 | augment->target->child->prev = augment->child->prev; |
| 3970 | augment->child->prev = last; |
| 3971 | } else { |
| 3972 | augment->target->child = augment->child; |
| 3973 | } |
| 3974 | |
| 3975 | /* remove the flag about not applicability */ |
| 3976 | augment->flags &= ~LYS_NOTAPPLIED; |
| 3977 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3978 | |
| 3979 | void |
| 3980 | lys_sub_module_apply_devs_augs(struct lys_module *module) |
| 3981 | { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 3982 | uint8_t u, v; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3983 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 3984 | /* remove applied deviations */ |
| 3985 | for (u = 0; u < module->deviation_size; ++u) { |
| 3986 | apply_dev(&module->deviation[u], module); |
| 3987 | } |
| 3988 | /* remove applied augments */ |
| 3989 | for (u = 0; u < module->augment_size; ++u) { |
| 3990 | apply_aug(&module->augment[u]); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3991 | } |
| 3992 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 3993 | /* remove deviation and augments defined in submodules */ |
| 3994 | for (v = 0; v < module->inc_size; ++v) { |
| 3995 | for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) { |
| 3996 | apply_dev(&module->inc[v].submodule->deviation[u], module); |
| 3997 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3998 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 3999 | for (u = 0; u < module->inc[v].submodule->augment_size; ++u) { |
| 4000 | apply_aug(&module->inc[v].submodule->augment[u]); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4001 | } |
| 4002 | } |
| 4003 | } |
| 4004 | |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4005 | static void |
| 4006 | remove_dev(struct lys_deviation *dev, const struct lys_module *module) |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4007 | { |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4008 | uint32_t idx = 0, j; |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 4009 | const struct lys_module *mod; |
| 4010 | struct lys_module *target_mod; |
| 4011 | const char *ptr; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4012 | |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4013 | if (dev->orig_node) { |
| 4014 | target_mod = lys_node_module(dev->orig_node); |
| 4015 | } else { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4016 | LOGINT; |
| 4017 | return; |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4018 | } |
| 4019 | lys_switch_deviation(dev, module); |
| 4020 | |
| 4021 | /* clear the deviation flag if possible */ |
| 4022 | while ((mod = ly_ctx_get_module_iter(module->ctx, &idx))) { |
| 4023 | if ((mod == module) || (mod == target_mod)) { |
| 4024 | continue; |
Pavol Vican | 64d0b76 | 2016-08-25 10:44:59 +0200 | [diff] [blame] | 4025 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4026 | |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4027 | for (j = 0; j < mod->deviation_size; ++j) { |
| 4028 | ptr = strstr(mod->deviation[j].target_name, target_mod->name); |
| 4029 | if (ptr && (ptr[strlen(target_mod->name)] == ':')) { |
| 4030 | /* some other module deviation targets the inspected module, flag remains */ |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 4031 | break; |
| 4032 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4033 | } |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 4034 | |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4035 | if (j < mod->deviation_size) { |
| 4036 | break; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4037 | } |
| 4038 | } |
| 4039 | |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4040 | if (!mod) { |
| 4041 | target_mod->deviated = 0; |
| 4042 | } |
| 4043 | } |
| 4044 | |
| 4045 | static void |
| 4046 | remove_aug(struct lys_node_augment *augment) |
| 4047 | { |
| 4048 | struct lys_node *last, *elem; |
| 4049 | |
| 4050 | if (!augment->target) { |
| 4051 | /* skip not resolved augments */ |
| 4052 | return; |
| 4053 | } |
| 4054 | |
| 4055 | elem = augment->child; |
| 4056 | if (elem) { |
| 4057 | LY_TREE_FOR(elem, last) { |
| 4058 | if (!last->next || (last->next->parent != (struct lys_node *)augment)) { |
| 4059 | break; |
| 4060 | } |
| 4061 | } |
| 4062 | /* elem is first augment child, last is the last child */ |
| 4063 | |
| 4064 | /* parent child ptr */ |
| 4065 | if (augment->target->child == elem) { |
| 4066 | augment->target->child = last->next; |
| 4067 | } |
| 4068 | |
| 4069 | /* parent child next ptr */ |
| 4070 | if (elem->prev->next) { |
| 4071 | elem->prev->next = last->next; |
| 4072 | } |
| 4073 | |
| 4074 | /* parent child prev ptr */ |
| 4075 | if (last->next) { |
| 4076 | last->next->prev = elem->prev; |
| 4077 | } else if (augment->target->child) { |
| 4078 | augment->target->child->prev = elem->prev; |
| 4079 | } |
| 4080 | |
| 4081 | /* update augment children themselves */ |
| 4082 | elem->prev = last; |
| 4083 | last->next = NULL; |
| 4084 | } |
| 4085 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4086 | /* augment->target still keeps the resolved target, but for lys_augment_free() |
| 4087 | * we have to keep information that this augment is not applied to free its data */ |
| 4088 | augment->flags |= LYS_NOTAPPLIED; |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4089 | } |
| 4090 | |
| 4091 | void |
| 4092 | lys_sub_module_remove_devs_augs(struct lys_module *module) |
| 4093 | { |
| 4094 | uint8_t u, v; |
| 4095 | |
| 4096 | /* remove applied deviations */ |
| 4097 | for (u = 0; u < module->deviation_size; ++u) { |
| 4098 | remove_dev(&module->deviation[u], module); |
| 4099 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4100 | /* remove applied augments */ |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4101 | for (u = 0; u < module->augment_size; ++u) { |
| 4102 | remove_aug(&module->augment[u]); |
| 4103 | } |
| 4104 | |
| 4105 | /* remove deviation and augments defined in submodules */ |
| 4106 | for (v = 0; v < module->inc_size; ++v) { |
| 4107 | for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) { |
| 4108 | remove_dev(&module->inc[v].submodule->deviation[u], module); |
Radek Krejci | dbc1526 | 2016-06-16 14:58:29 +0200 | [diff] [blame] | 4109 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4110 | |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4111 | for (u = 0; u < module->inc[v].submodule->augment_size; ++u) { |
| 4112 | remove_aug(&module->inc[v].submodule->augment[u]); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4113 | } |
| 4114 | } |
| 4115 | } |
| 4116 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4117 | static int |
| 4118 | lys_set_implemented_recursion(struct lys_module *module, struct unres_schema *unres) |
| 4119 | { |
| 4120 | struct lys_node *root, *next, *node; |
| 4121 | uint8_t i; |
| 4122 | |
| 4123 | for (i = 0; i < module->augment_size; i++) { |
| 4124 | /* apply augment */ |
Michal Vasko | a1911b9 | 2016-09-19 14:57:34 +0200 | [diff] [blame] | 4125 | if (!module->augment[i].target |
| 4126 | && (unres_schema_add_node(module, unres, &module->augment[i], UNRES_AUGMENT, NULL) == -1)) { |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4127 | return -1; |
| 4128 | } |
| 4129 | } |
| 4130 | LY_TREE_FOR(module->data, root) { |
| 4131 | /* handle leafrefs and recursively change the implemented flags in the leafref targets */ |
| 4132 | LY_TREE_DFS_BEGIN(root, next, node) { |
| 4133 | if (node->nodetype == LYS_GROUPING) { |
| 4134 | goto nextsibling; |
| 4135 | } |
| 4136 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4137 | if (((struct lys_node_leaf *)node)->type.base == LY_TYPE_LEAFREF) { |
| 4138 | if (unres_schema_add_node(module, unres, &((struct lys_node_leaf *)node)->type, |
| 4139 | UNRES_TYPE_LEAFREF, node) == -1) { |
| 4140 | return EXIT_FAILURE; |
| 4141 | } |
| 4142 | } |
| 4143 | } |
| 4144 | |
| 4145 | /* modified LY_TREE_DFS_END */ |
| 4146 | next = node->child; |
| 4147 | /* child exception for leafs, leaflists and anyxml without children */ |
| 4148 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 4149 | next = NULL; |
| 4150 | } |
| 4151 | if (!next) { |
| 4152 | nextsibling: |
| 4153 | /* no children */ |
| 4154 | if (node == root) { |
| 4155 | /* we are done, root has no children */ |
| 4156 | break; |
| 4157 | } |
| 4158 | /* try siblings */ |
| 4159 | next = node->next; |
| 4160 | } |
| 4161 | while (!next) { |
| 4162 | /* parent is already processed, go to its sibling */ |
| 4163 | node = lys_parent(node); |
| 4164 | /* no siblings, go back through parents */ |
| 4165 | if (lys_parent(node) == lys_parent(root)) { |
| 4166 | /* we are done, no next element to process */ |
| 4167 | break; |
| 4168 | } |
| 4169 | next = node->next; |
| 4170 | } |
| 4171 | } |
| 4172 | } |
| 4173 | |
| 4174 | return EXIT_SUCCESS; |
| 4175 | } |
| 4176 | |
| 4177 | API int |
| 4178 | lys_set_implemented(const struct lys_module *module) |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4179 | { |
| 4180 | struct ly_ctx *ctx; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4181 | struct unres_schema *unres; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4182 | int i, j, disabled = 0; |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4183 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4184 | if (!module) { |
| 4185 | ly_errno = LY_EINVAL; |
| 4186 | return EXIT_FAILURE; |
| 4187 | } |
| 4188 | |
| 4189 | module = lys_main_module(module); |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4190 | |
| 4191 | if (module->disabled) { |
| 4192 | disabled = 1; |
| 4193 | lys_set_enabled(module); |
| 4194 | } |
| 4195 | |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4196 | if (module->implemented) { |
| 4197 | return EXIT_SUCCESS; |
| 4198 | } |
| 4199 | |
| 4200 | ctx = module->ctx; |
| 4201 | |
| 4202 | for (i = 0; i < ctx->models.used; ++i) { |
| 4203 | if (module == ctx->models.list[i]) { |
| 4204 | continue; |
| 4205 | } |
| 4206 | |
| 4207 | if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) { |
| 4208 | LOGERR(LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name); |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4209 | if (disabled) { |
| 4210 | /* set it back disabled */ |
| 4211 | lys_set_disabled(module); |
| 4212 | } |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4213 | return EXIT_FAILURE; |
| 4214 | } |
| 4215 | } |
| 4216 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4217 | unres = calloc(1, sizeof *unres); |
| 4218 | if (!unres) { |
| 4219 | LOGMEM; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4220 | if (disabled) { |
| 4221 | /* set it back disabled */ |
| 4222 | lys_set_disabled(module); |
| 4223 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4224 | return EXIT_FAILURE; |
| 4225 | } |
| 4226 | /* recursively make the module implemented */ |
| 4227 | ((struct lys_module *)module)->implemented = 1; |
| 4228 | if (lys_set_implemented_recursion((struct lys_module *)module, unres)) { |
| 4229 | goto error; |
| 4230 | } |
| 4231 | /* process augments in submodules */ |
| 4232 | for (i = 0; i < module->inc_size; ++i) { |
| 4233 | if (!module->inc[i].submodule) { |
| 4234 | continue; |
| 4235 | } |
| 4236 | for (j = 0; j < module->inc[i].submodule->augment_size; j++) { |
| 4237 | /* apply augment */ |
Radek Krejci | c8c2253 | 2016-11-09 15:11:24 +0100 | [diff] [blame] | 4238 | if (!module->inc[i].submodule->augment[j].target |
| 4239 | && (unres_schema_add_node((struct lys_module *)module->inc[j].submodule, unres, |
| 4240 | &module->inc[i].submodule->augment[j], UNRES_AUGMENT, NULL) == -1)) { |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4241 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4242 | goto error; |
| 4243 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4244 | } |
| 4245 | } |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4246 | /* try again resolve augments in other modules possibly augmenting this one, |
| 4247 | * since we have just enabled it |
| 4248 | */ |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4249 | /* resolve rest of unres items */ |
| 4250 | if (unres->count && resolve_unres_schema((struct lys_module *)module, unres)) { |
| 4251 | goto error; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4252 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4253 | unres_schema_free((struct lys_module *)module, &unres); |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4254 | |
| 4255 | return EXIT_SUCCESS; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4256 | |
| 4257 | error: |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4258 | |
| 4259 | if (disabled) { |
| 4260 | /* set it back disabled */ |
| 4261 | lys_set_disabled(module); |
| 4262 | } |
| 4263 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4264 | ((struct lys_module *)module)->implemented = 0; |
| 4265 | unres_schema_free((struct lys_module *)module, &unres); |
| 4266 | return EXIT_FAILURE; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4267 | } |
| 4268 | |
| 4269 | void |
| 4270 | lys_submodule_module_data_free(struct lys_submodule *submodule) |
| 4271 | { |
| 4272 | struct lys_node *next, *elem; |
| 4273 | |
| 4274 | /* remove parsed data */ |
| 4275 | LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) { |
| 4276 | if (elem->module == (struct lys_module *)submodule) { |
| 4277 | lys_node_free(elem, NULL, 0); |
| 4278 | } |
| 4279 | } |
| 4280 | } |
Radek Krejci | a1c33bf | 2016-09-07 12:38:49 +0200 | [diff] [blame] | 4281 | |
| 4282 | int |
| 4283 | lys_is_key(struct lys_node_list *list, struct lys_node_leaf *leaf) |
| 4284 | { |
| 4285 | uint8_t i; |
| 4286 | |
| 4287 | for (i = 0; i < list->keys_size; i++) { |
| 4288 | if (list->keys[i] == leaf) { |
| 4289 | return i + 1; |
| 4290 | } |
| 4291 | } |
| 4292 | |
| 4293 | return 0; |
| 4294 | } |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4295 | |
| 4296 | API char * |
| 4297 | lys_path(const struct lys_node *node) |
| 4298 | { |
| 4299 | char *buf_backup = NULL, *buf = ly_buf(), *result = NULL; |
| 4300 | uint16_t index = LY_BUF_SIZE - 1; |
| 4301 | |
| 4302 | if (!node) { |
| 4303 | LOGERR(LY_EINVAL, "%s: NULL node parameter", __func__); |
| 4304 | return NULL; |
| 4305 | } |
| 4306 | |
| 4307 | /* backup the shared internal buffer */ |
| 4308 | if (ly_buf_used && buf[0]) { |
| 4309 | buf_backup = strndup(buf, LY_BUF_SIZE - 1); |
| 4310 | } |
| 4311 | ly_buf_used++; |
| 4312 | |
| 4313 | /* build the path */ |
| 4314 | buf[index] = '\0'; |
Michal Vasko | 5efa25c | 2017-01-10 11:34:30 +0100 | [diff] [blame] | 4315 | ly_vlog_build_path_reverse(LY_VLOG_LYS, node, buf, &index, 0); |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4316 | result = strdup(&buf[index]); |
| 4317 | |
| 4318 | /* restore the shared internal buffer */ |
| 4319 | if (buf_backup) { |
| 4320 | strcpy(buf, buf_backup); |
| 4321 | free(buf_backup); |
| 4322 | } |
| 4323 | ly_buf_used--; |
| 4324 | |
| 4325 | return result; |
| 4326 | } |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 4327 | |