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