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