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 | * |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 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 | |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 17 | #ifdef __APPLE__ |
| 18 | # include <sys/param.h> |
| 19 | #endif |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 20 | #include <assert.h> |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 21 | #include <ctype.h> |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 22 | #include <limits.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <sys/mman.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 25 | #include <sys/types.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 26 | #include <sys/stat.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 27 | #include <fcntl.h> |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 28 | #include <string.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | #include <errno.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 31 | |
| 32 | #include "common.h" |
| 33 | #include "context.h" |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 34 | #include "parser.h" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 35 | #include "resolve.h" |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 36 | #include "xml.h" |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 37 | #include "xpath.h" |
Michal Vasko | fc5744d | 2015-10-22 12:09:34 +0200 | [diff] [blame] | 38 | #include "xml_internal.h" |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 39 | #include "tree_internal.h" |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 40 | #include "validation.h" |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 41 | #include "parser_yang.h" |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 42 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 43 | 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] | 44 | int in_grp, int shallow, struct unres_schema *unres); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 45 | |
Radek Krejci | 65aca41 | 2018-01-24 11:23:06 +0100 | [diff] [blame] | 46 | API const struct lys_node_list * |
| 47 | lys_is_key(const struct lys_node_leaf *node, uint8_t *index) |
| 48 | { |
| 49 | struct lys_node *parent = (struct lys_node *)node; |
| 50 | struct lys_node_list *list; |
| 51 | uint8_t i; |
| 52 | |
| 53 | if (!node || node->nodetype != LYS_LEAF) { |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | do { |
| 58 | parent = lys_parent(parent); |
| 59 | } while (parent && parent->nodetype == LYS_USES); |
| 60 | |
| 61 | if (!parent || parent->nodetype != LYS_LIST) { |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | list = (struct lys_node_list*)parent; |
| 66 | for (i = 0; i < list->keys_size; i++) { |
| 67 | if (list->keys[i] == node) { |
| 68 | if (index) { |
| 69 | (*index) = i; |
| 70 | } |
| 71 | return list; |
| 72 | } |
| 73 | } |
| 74 | return NULL; |
| 75 | } |
| 76 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 77 | API const struct lys_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 78 | lys_is_disabled(const struct lys_node *node, int recursive) |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 79 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 80 | int i; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 81 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 82 | if (!node) { |
| 83 | return NULL; |
| 84 | } |
| 85 | |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 86 | check: |
| 87 | if (node->nodetype != LYS_INPUT && node->nodetype != LYS_OUTPUT) { |
| 88 | /* input/output does not have if-feature, so skip them */ |
| 89 | |
| 90 | /* check local if-features */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 91 | for (i = 0; i < node->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 92 | if (!resolve_iffeature(&node->iffeature[i])) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 93 | return node; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if (!recursive) { |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | /* go through parents */ |
| 103 | if (node->nodetype == LYS_AUGMENT) { |
| 104 | /* go to parent actually means go to the target node */ |
| 105 | node = ((struct lys_node_augment *)node)->target; |
Michal Vasko | 1754919 | 2017-03-13 10:19:33 +0100 | [diff] [blame] | 106 | if (!node) { |
| 107 | /* unresolved augment, let's say it's enabled */ |
| 108 | return NULL; |
| 109 | } |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 110 | } else if (node->parent) { |
| 111 | node = node->parent; |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 112 | } else { |
| 113 | return NULL; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 116 | if (recursive == 2) { |
| 117 | /* continue only if the node cannot have a data instance */ |
| 118 | if (node->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST)) { |
| 119 | return NULL; |
| 120 | } |
| 121 | } |
| 122 | goto check; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 125 | int |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 126 | lys_get_sibling(const struct lys_node *siblings, const char *mod_name, int mod_name_len, const char *name, |
| 127 | int nam_len, LYS_NODE type, const struct lys_node **ret) |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 128 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 129 | const struct lys_node *node, *parent = NULL; |
| 130 | const struct lys_module *mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 131 | const char *node_mod_name; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 132 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 133 | assert(siblings && mod_name && name); |
Michal Vasko | 165dc4a | 2015-10-23 09:44:27 +0200 | [diff] [blame] | 134 | assert(!(type & (LYS_USES | LYS_GROUPING))); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 135 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 136 | /* fill the lengths in case the caller is so indifferent */ |
| 137 | if (!mod_name_len) { |
| 138 | mod_name_len = strlen(mod_name); |
| 139 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 140 | if (!nam_len) { |
| 141 | nam_len = strlen(name); |
| 142 | } |
| 143 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 144 | while (siblings && (siblings->nodetype == LYS_USES)) { |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 145 | siblings = siblings->child; |
| 146 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 147 | if (!siblings) { |
| 148 | /* unresolved uses */ |
| 149 | return EXIT_FAILURE; |
| 150 | } |
| 151 | |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 152 | if (siblings->nodetype == LYS_GROUPING) { |
| 153 | for (node = siblings; (node->nodetype == LYS_GROUPING) && (node->prev != siblings); node = node->prev); |
| 154 | if (node->nodetype == LYS_GROUPING) { |
| 155 | /* we went through all the siblings, only groupings there - no valid sibling */ |
| 156 | return EXIT_FAILURE; |
| 157 | } |
| 158 | /* update siblings to be valid */ |
| 159 | siblings = node; |
| 160 | } |
| 161 | |
Michal Vasko | 4cf7dba | 2016-10-14 09:42:01 +0200 | [diff] [blame] | 162 | /* set parent correctly */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 163 | parent = lys_parent(siblings); |
Michal Vasko | 4cf7dba | 2016-10-14 09:42:01 +0200 | [diff] [blame] | 164 | |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 165 | /* go up all uses */ |
| 166 | while (parent && (parent->nodetype == LYS_USES)) { |
| 167 | parent = lys_parent(parent); |
Michal Vasko | 4cf7dba | 2016-10-14 09:42:01 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 170 | if (!parent) { |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 171 | /* handle situation when there is a top-level uses referencing a foreign grouping */ |
| 172 | for (node = siblings; lys_parent(node) && (node->nodetype == LYS_USES); node = lys_parent(node)); |
| 173 | mod = lys_node_module(node); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 176 | /* try to find the node */ |
| 177 | node = NULL; |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 178 | 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] | 179 | if (!type || (node->nodetype & type)) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 180 | /* module name comparison */ |
| 181 | node_mod_name = lys_node_module(node)->name; |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 182 | 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] | 183 | continue; |
| 184 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 185 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 186 | /* direct name check */ |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 187 | 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] | 188 | if (ret) { |
| 189 | *ret = node; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 190 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 191 | return EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 192 | } |
| 193 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | return EXIT_FAILURE; |
| 197 | } |
| 198 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 199 | int |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 200 | lys_getnext_data(const struct lys_module *mod, const struct lys_node *parent, const char *name, int nam_len, |
| 201 | LYS_NODE type, const struct lys_node **ret) |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 202 | { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 203 | const struct lys_node *node; |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 204 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 205 | assert((mod || parent) && name); |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 206 | assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))); |
| 207 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 208 | if (!mod) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 209 | mod = lys_node_module(parent); |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 210 | } |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 211 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 212 | /* try to find the node */ |
| 213 | node = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 214 | while ((node = lys_getnext(node, parent, mod, 0))) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 215 | if (!type || (node->nodetype & type)) { |
| 216 | /* module check */ |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 217 | if (lys_node_module(node) != lys_main_module(mod)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 218 | continue; |
| 219 | } |
| 220 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 221 | /* direct name check */ |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 222 | if (!strncmp(node->name, name, nam_len) && !node->name[nam_len]) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 223 | if (ret) { |
| 224 | *ret = node; |
| 225 | } |
| 226 | return EXIT_SUCCESS; |
| 227 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 228 | } |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return EXIT_FAILURE; |
| 232 | } |
| 233 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 234 | API const struct lys_node * |
| 235 | 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] | 236 | { |
Michal Vasko | 5a9c24b | 2017-03-13 09:25:58 +0100 | [diff] [blame] | 237 | const struct lys_node *next, *aug_parent; |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 238 | struct lys_node **snode; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 239 | |
Michal Vasko | 75702cd | 2018-02-12 11:27:09 +0100 | [diff] [blame] | 240 | if ((!parent && !module) || (module && module->type) || (parent && (parent->nodetype == LYS_USES) && !(options & LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 241 | LOGARG; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 242 | return NULL; |
| 243 | } |
| 244 | |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 245 | if (!last) { |
| 246 | /* first call */ |
| 247 | |
| 248 | /* get know where to start */ |
| 249 | if (parent) { |
| 250 | /* schema subtree */ |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 251 | snode = lys_child(parent, LYS_UNKNOWN); |
Michal Vasko | 5a9c24b | 2017-03-13 09:25:58 +0100 | [diff] [blame] | 252 | /* do not return anything if the augment does not have any children */ |
Radek Krejci | bb08db3 | 2017-07-03 11:29:17 +0200 | [diff] [blame] | 253 | if (!snode || !(*snode) || ((parent->nodetype == LYS_AUGMENT) && ((*snode)->parent != parent))) { |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 254 | return NULL; |
| 255 | } |
| 256 | next = last = *snode; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 257 | } else { |
| 258 | /* top level data */ |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 259 | if (!(options & LYS_GETNEXT_NOSTATECHECK) && (module->disabled || !module->implemented)) { |
| 260 | /* nothing to return from a disabled/imported module */ |
| 261 | return NULL; |
| 262 | } |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 263 | next = last = module->data; |
| 264 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 265 | } else if ((last->nodetype == LYS_USES) && (options & LYS_GETNEXT_INTOUSES) && last->child) { |
| 266 | /* continue with uses content */ |
| 267 | next = last->child; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 268 | } else { |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 269 | /* continue after the last returned value */ |
| 270 | next = last->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | repeat: |
Michal Vasko | 5a9c24b | 2017-03-13 09:25:58 +0100 | [diff] [blame] | 274 | if (parent && (parent->nodetype == LYS_AUGMENT) && next) { |
| 275 | /* do not return anything outside the parent augment */ |
| 276 | aug_parent = next->parent; |
| 277 | do { |
| 278 | while (aug_parent && (aug_parent->nodetype != LYS_AUGMENT)) { |
| 279 | aug_parent = aug_parent->parent; |
| 280 | } |
| 281 | if (aug_parent) { |
| 282 | if (aug_parent == parent) { |
| 283 | break; |
| 284 | } |
| 285 | aug_parent = ((struct lys_node_augment *)aug_parent)->target; |
| 286 | } |
| 287 | |
| 288 | } while (aug_parent); |
| 289 | if (!aug_parent) { |
| 290 | return NULL; |
| 291 | } |
| 292 | } |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 293 | while (next && (next->nodetype == LYS_GROUPING)) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 294 | if (options & LYS_GETNEXT_WITHGROUPING) { |
| 295 | return next; |
| 296 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 297 | next = next->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 300 | if (!next) { /* cover case when parent is augment */ |
| 301 | if (!last || last->parent == parent || lys_parent(last) == parent) { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 302 | /* no next element */ |
| 303 | return NULL; |
| 304 | } |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 305 | last = lys_parent(last); |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 306 | next = last->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 307 | goto repeat; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 308 | } else { |
| 309 | last = next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 310 | } |
| 311 | |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 312 | if (!(options & LYS_GETNEXT_NOSTATECHECK) && lys_is_disabled(next, 0)) { |
| 313 | next = next->next; |
| 314 | goto repeat; |
| 315 | } |
| 316 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 317 | switch (next->nodetype) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 318 | case LYS_INPUT: |
| 319 | case LYS_OUTPUT: |
| 320 | if (options & LYS_GETNEXT_WITHINOUT) { |
| 321 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 322 | } else if (next->child) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 323 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 324 | } else { |
| 325 | next = next->next; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 326 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 327 | goto repeat; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 328 | |
Michal Vasko | a5835e9 | 2015-10-20 15:07:39 +0200 | [diff] [blame] | 329 | case LYS_CASE: |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 330 | if (options & LYS_GETNEXT_WITHCASE) { |
| 331 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 332 | } else if (next->child) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 333 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 334 | } else { |
| 335 | next = next->next; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 336 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 337 | goto repeat; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 338 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 339 | case LYS_USES: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 340 | /* go into */ |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 341 | if (options & LYS_GETNEXT_WITHUSES) { |
| 342 | return next; |
| 343 | } else if (next->child) { |
| 344 | next = next->child; |
| 345 | } else { |
| 346 | next = next->next; |
| 347 | } |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 348 | goto repeat; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 349 | |
Radek Krejci | dfcae7d | 2015-10-20 17:13:01 +0200 | [diff] [blame] | 350 | case LYS_RPC: |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 351 | case LYS_ACTION: |
Radek Krejci | dfcae7d | 2015-10-20 17:13:01 +0200 | [diff] [blame] | 352 | case LYS_NOTIF: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 353 | case LYS_LEAF: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 354 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 355 | case LYS_ANYDATA: |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 356 | case LYS_LIST: |
| 357 | case LYS_LEAFLIST: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 358 | return next; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 359 | |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 360 | case LYS_CONTAINER: |
| 361 | if (!((struct lys_node_container *)next)->presence && (options & LYS_GETNEXT_INTONPCONT)) { |
| 362 | if (next->child) { |
| 363 | /* go into */ |
| 364 | next = next->child; |
| 365 | } else { |
| 366 | next = next->next; |
| 367 | } |
| 368 | goto repeat; |
| 369 | } else { |
| 370 | return next; |
| 371 | } |
| 372 | |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 373 | case LYS_CHOICE: |
| 374 | if (options & LYS_GETNEXT_WITHCHOICE) { |
| 375 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 376 | } else if (next->child) { |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 377 | /* go into */ |
| 378 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 379 | } else { |
| 380 | next = next->next; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 381 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 382 | goto repeat; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 383 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 384 | default: |
| 385 | /* we should not be here */ |
| 386 | return NULL; |
| 387 | } |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 388 | } |
| 389 | |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 390 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 391 | lys_node_unlink(struct lys_node *node) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 392 | { |
Michal Vasko | f75b277 | 2018-03-14 09:55:33 +0100 | [diff] [blame] | 393 | struct lys_node *parent, *first, **pp = NULL; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 394 | struct lys_module *main_module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 395 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 396 | if (!node) { |
| 397 | return; |
| 398 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 399 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 400 | /* unlink from data model if necessary */ |
| 401 | if (node->module) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 402 | /* get main module with data tree */ |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 403 | main_module = lys_node_module(node); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 404 | if (main_module->data == node) { |
| 405 | main_module->data = node->next; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 406 | } |
| 407 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 408 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 409 | /* store pointers to important nodes */ |
| 410 | parent = node->parent; |
Michal Vasko | 3a9943b | 2015-09-23 11:33:50 +0200 | [diff] [blame] | 411 | if (parent && (parent->nodetype == LYS_AUGMENT)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 412 | /* handle augments - first, unlink it from the augment parent ... */ |
| 413 | if (parent->child == node) { |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 414 | parent->child = (node->next && node->next->parent == parent) ? node->next : NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 415 | } |
Radek Krejci | fec2e14 | 2017-01-05 15:19:03 +0100 | [diff] [blame] | 416 | |
| 417 | if (parent->flags & LYS_NOTAPPLIED) { |
| 418 | /* data are not connected in the target, so we cannot continue with the target as a parent */ |
| 419 | parent = NULL; |
| 420 | } else { |
| 421 | /* data are connected in target, so we will continue with the target as a parent */ |
| 422 | parent = ((struct lys_node_augment *)parent)->target; |
| 423 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 424 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 425 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 426 | /* unlink from parent */ |
| 427 | if (parent) { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 428 | if (parent->nodetype == LYS_EXT) { |
| 429 | pp = (struct lys_node **)lys_ext_complex_get_substmt(lys_snode2stmt(node->nodetype), |
| 430 | (struct lys_ext_instance_complex*)parent, NULL); |
| 431 | if (*pp == node) { |
| 432 | *pp = node->next; |
| 433 | } |
| 434 | } else if (parent->child == node) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 435 | parent->child = node->next; |
| 436 | } |
| 437 | node->parent = NULL; |
| 438 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 439 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 440 | /* unlink from siblings */ |
| 441 | if (node->prev == node) { |
| 442 | /* there are no more siblings */ |
| 443 | return; |
| 444 | } |
| 445 | if (node->next) { |
| 446 | node->next->prev = node->prev; |
| 447 | } else { |
| 448 | /* unlinking the last element */ |
| 449 | if (parent) { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 450 | if (parent->nodetype == LYS_EXT) { |
| 451 | first = *(struct lys_node **)pp; |
| 452 | } else { |
| 453 | first = parent->child; |
| 454 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 455 | } else { |
| 456 | first = node; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 457 | while (first->prev->next) { |
Michal Vasko | 276f96b | 2015-09-23 11:34:28 +0200 | [diff] [blame] | 458 | first = first->prev; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | first->prev = node->prev; |
| 462 | } |
| 463 | if (node->prev->next) { |
| 464 | node->prev->next = node->next; |
| 465 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 466 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 467 | /* clean up the unlinked element */ |
| 468 | node->next = NULL; |
| 469 | node->prev = node; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 472 | struct lys_node_grp * |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 473 | lys_find_grouping_up(const char *name, struct lys_node *start) |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 474 | { |
| 475 | struct lys_node *par_iter, *iter, *stop; |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 476 | |
| 477 | for (par_iter = start; par_iter; par_iter = par_iter->parent) { |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 478 | /* top-level augment, look into module (uses augment is handled correctly below) */ |
| 479 | 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] | 480 | par_iter = lys_main_module(par_iter->parent->module)->data; |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 481 | if (!par_iter) { |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 482 | break; |
| 483 | } |
| 484 | } |
| 485 | |
Michal Vasko | 38a0041 | 2018-03-14 10:01:50 +0100 | [diff] [blame^] | 486 | if (par_iter->nodetype == LYS_EXT) { |
| 487 | /* we are in a top-level extension, search grouping in top-level groupings */ |
| 488 | par_iter = lys_main_module(par_iter->module)->data; |
| 489 | if (!par_iter) { |
| 490 | /* not connected yet, wait */ |
| 491 | return NULL; |
| 492 | } |
| 493 | } else 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] | 494 | continue; |
| 495 | } |
| 496 | |
| 497 | for (iter = par_iter, stop = NULL; iter; iter = iter->prev) { |
| 498 | if (!stop) { |
| 499 | stop = par_iter; |
| 500 | } else if (iter == stop) { |
| 501 | break; |
| 502 | } |
| 503 | if (iter->nodetype != LYS_GROUPING) { |
| 504 | continue; |
| 505 | } |
| 506 | |
Radek Krejci | f8426a7 | 2015-10-31 23:14:03 +0100 | [diff] [blame] | 507 | if (!strcmp(name, iter->name)) { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 508 | return (struct lys_node_grp *)iter; |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 513 | return NULL; |
| 514 | } |
| 515 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 516 | /* |
| 517 | * get next grouping in the root's subtree, in the |
| 518 | * first call, tha last is NULL |
| 519 | */ |
| 520 | static struct lys_node_grp * |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 521 | 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] | 522 | { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 523 | struct lys_node *last = (struct lys_node *)lastgrp; |
| 524 | struct lys_node *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 525 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 526 | assert(root); |
| 527 | |
| 528 | if (!last) { |
| 529 | last = root; |
| 530 | } |
| 531 | |
| 532 | while (1) { |
| 533 | if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) { |
| 534 | next = last->child; |
| 535 | } else { |
| 536 | next = NULL; |
| 537 | } |
| 538 | if (!next) { |
| 539 | if (last == root) { |
| 540 | /* we are done */ |
| 541 | return NULL; |
| 542 | } |
| 543 | |
| 544 | /* no children, go to siblings */ |
| 545 | next = last->next; |
| 546 | } |
| 547 | while (!next) { |
| 548 | /* go back through parents */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 549 | if (lys_parent(last) == root) { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 550 | /* we are done */ |
| 551 | return NULL; |
| 552 | } |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 553 | next = last->next; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 554 | last = lys_parent(last); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | if (next->nodetype == LYS_GROUPING) { |
| 558 | return (struct lys_node_grp *)next; |
| 559 | } |
| 560 | |
| 561 | last = next; |
| 562 | } |
| 563 | } |
| 564 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 565 | /* logs directly */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 566 | int |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 567 | lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module) |
| 568 | { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 569 | struct lys_node *start, *stop, *iter; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 570 | struct lys_node_grp *grp; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 571 | int down, up; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 572 | |
| 573 | assert(node); |
| 574 | |
| 575 | if (!parent) { |
| 576 | assert(module); |
| 577 | } else { |
| 578 | module = parent->module; |
| 579 | } |
Radek Krejci | 115fa88 | 2017-03-01 16:15:07 +0100 | [diff] [blame] | 580 | module = lys_main_module(module); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 581 | |
| 582 | switch (node->nodetype) { |
| 583 | case LYS_GROUPING: |
| 584 | /* 6.2.1, rule 6 */ |
| 585 | if (parent) { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 586 | start = *lys_child(parent, LYS_GROUPING); |
| 587 | if (!start) { |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 588 | down = 0; |
| 589 | start = parent; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 590 | } else { |
| 591 | down = 1; |
| 592 | } |
| 593 | if (parent->nodetype == LYS_EXT) { |
| 594 | up = 0; |
| 595 | } else { |
| 596 | up = 1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 597 | } |
| 598 | } else { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 599 | down = up = 1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 600 | start = module->data; |
| 601 | } |
| 602 | /* go up */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 603 | if (up && lys_find_grouping_up(node->name, start)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 604 | LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name); |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 605 | return EXIT_FAILURE; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 606 | } |
| 607 | /* go down, because grouping can be defined after e.g. container in which is collision */ |
| 608 | if (down) { |
| 609 | for (iter = start, stop = NULL; iter; iter = iter->prev) { |
| 610 | if (!stop) { |
| 611 | stop = start; |
| 612 | } else if (iter == stop) { |
| 613 | break; |
| 614 | } |
| 615 | if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) { |
| 616 | continue; |
| 617 | } |
| 618 | |
| 619 | grp = NULL; |
| 620 | while ((grp = lys_get_next_grouping(grp, iter))) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 621 | if (ly_strequal(node->name, grp->name, 1)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 622 | LOGVAL(module->ctx, LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 623 | return EXIT_FAILURE; |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | break; |
| 629 | case LYS_LEAF: |
| 630 | case LYS_LEAFLIST: |
| 631 | case LYS_LIST: |
| 632 | case LYS_CONTAINER: |
| 633 | case LYS_CHOICE: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 634 | case LYS_ANYDATA: |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 635 | /* 6.2.1, rule 7 */ |
| 636 | if (parent) { |
| 637 | iter = parent; |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 638 | while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE | LYS_AUGMENT))) { |
| 639 | if (iter->nodetype == LYS_AUGMENT) { |
| 640 | if (((struct lys_node_augment *)iter)->target) { |
| 641 | /* augment is resolved, go up */ |
| 642 | iter = ((struct lys_node_augment *)iter)->target; |
| 643 | continue; |
| 644 | } |
| 645 | /* augment is not resolved, this is the final parent */ |
| 646 | break; |
| 647 | } |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 648 | iter = iter->parent; |
| 649 | } |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 650 | |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 651 | if (!iter) { |
| 652 | stop = NULL; |
| 653 | iter = module->data; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 654 | } else if (iter->nodetype == LYS_EXT) { |
| 655 | stop = iter; |
PavolVican | 9d61d40 | 2018-02-05 15:52:48 +0100 | [diff] [blame] | 656 | iter = (struct lys_node *)lys_child(iter, node->nodetype); |
| 657 | if (iter) { |
| 658 | iter = *(struct lys_node **)iter; |
| 659 | } |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 660 | } else { |
| 661 | stop = iter; |
| 662 | iter = iter->child; |
| 663 | } |
| 664 | } else { |
| 665 | stop = NULL; |
| 666 | iter = module->data; |
| 667 | } |
| 668 | while (iter) { |
| 669 | if (iter->nodetype & (LYS_USES | LYS_CASE)) { |
| 670 | iter = iter->child; |
| 671 | continue; |
| 672 | } |
| 673 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 674 | 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] | 675 | if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 676 | LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, strnodetype(node->nodetype), node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 677 | return EXIT_FAILURE; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | /* special case for choice - we must check the choice's name as |
| 682 | * well as the names of nodes under the choice |
| 683 | */ |
| 684 | if (iter->nodetype == LYS_CHOICE) { |
| 685 | iter = iter->child; |
| 686 | continue; |
| 687 | } |
| 688 | |
| 689 | /* go to siblings */ |
| 690 | if (!iter->next) { |
| 691 | /* no sibling, go to parent's sibling */ |
| 692 | do { |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 693 | /* for parent LYS_AUGMENT */ |
| 694 | if (iter->parent == stop) { |
| 695 | iter = stop; |
| 696 | break; |
| 697 | } |
| 698 | iter = lys_parent(iter); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 699 | if (iter && iter->next) { |
| 700 | break; |
| 701 | } |
| 702 | } while (iter != stop); |
| 703 | |
| 704 | if (iter == stop) { |
| 705 | break; |
| 706 | } |
| 707 | } |
| 708 | iter = iter->next; |
| 709 | } |
| 710 | break; |
| 711 | case LYS_CASE: |
| 712 | /* 6.2.1, rule 8 */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 713 | if (parent) { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 714 | start = *lys_child(parent, LYS_CASE); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 715 | } else { |
| 716 | start = module->data; |
| 717 | } |
| 718 | |
| 719 | LY_TREE_FOR(start, iter) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 720 | 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] | 721 | continue; |
| 722 | } |
| 723 | |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 724 | if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 725 | LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, "case", node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 726 | return EXIT_FAILURE; |
| 727 | } |
| 728 | } |
| 729 | break; |
| 730 | default: |
| 731 | /* no check needed */ |
| 732 | break; |
| 733 | } |
| 734 | |
| 735 | return EXIT_SUCCESS; |
| 736 | } |
| 737 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 738 | /* logs directly */ |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 739 | int |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 740 | lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child) |
| 741 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 742 | struct ly_ctx *ctx = child->module->ctx; |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 743 | struct lys_node *iter, **pchild; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 744 | struct lys_node_inout *in, *out, *inout; |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 745 | struct lys_node_case *c; |
| 746 | int type, shortcase = 0; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 747 | void *p; |
| 748 | struct lyext_substmt *info = NULL; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 749 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 750 | assert(child); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 751 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 752 | if (parent) { |
| 753 | type = parent->nodetype; |
| 754 | module = parent->module; |
| 755 | } else { |
| 756 | assert(module); |
Radek Krejci | fe3a138 | 2016-11-04 10:30:11 +0100 | [diff] [blame] | 757 | assert(!(child->nodetype & (LYS_INPUT | LYS_OUTPUT))); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 758 | type = 0; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 759 | } |
| 760 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 761 | /* checks */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 762 | switch (type) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 763 | case LYS_CONTAINER: |
| 764 | case LYS_LIST: |
| 765 | case LYS_GROUPING: |
Radek Krejci | 9693540 | 2016-11-04 16:27:28 +0100 | [diff] [blame] | 766 | case LYS_USES: |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 767 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 768 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF | |
Michal Vasko | b15cae2 | 2016-09-15 09:40:56 +0200 | [diff] [blame] | 769 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 770 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 771 | return EXIT_FAILURE; |
| 772 | } |
| 773 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 774 | case LYS_INPUT: |
| 775 | case LYS_OUTPUT: |
| 776 | case LYS_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 777 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 778 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 779 | LYS_LEAFLIST | LYS_LIST | LYS_USES))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 780 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 781 | return EXIT_FAILURE; |
| 782 | } |
| 783 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 784 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 785 | if (!(child->nodetype & |
Pavol Vican | 47a1b0a | 2016-09-20 10:18:24 +0200 | [diff] [blame] | 786 | (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 787 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 788 | return EXIT_FAILURE; |
| 789 | } |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 790 | if (child->nodetype != LYS_CASE) { |
| 791 | shortcase = 1; |
| 792 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 793 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 794 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 795 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 796 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_USES))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 797 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 798 | return EXIT_FAILURE; |
| 799 | } |
| 800 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 801 | case LYS_RPC: |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 802 | case LYS_ACTION: |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 803 | if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 804 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc"); |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 805 | return EXIT_FAILURE; |
| 806 | } |
| 807 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 808 | case LYS_LEAF: |
| 809 | case LYS_LEAFLIST: |
| 810 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 811 | case LYS_ANYDATA: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 812 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
| 813 | LOGVAL(ctx, 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] | 814 | strnodetype(parent->nodetype)); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 815 | return EXIT_FAILURE; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 816 | case LYS_AUGMENT: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 817 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 818 | (LYS_ANYDATA | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 819 | | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 820 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 821 | return EXIT_FAILURE; |
| 822 | } |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 823 | break; |
| 824 | case LYS_UNKNOWN: |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 825 | /* top level */ |
| 826 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 827 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 828 | | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 829 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "(sub)module"); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 830 | return EXIT_FAILURE; |
| 831 | } |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 832 | break; |
| 833 | case LYS_EXT: |
| 834 | /* plugin-defined */ |
| 835 | p = lys_ext_complex_get_substmt(lys_snode2stmt(child->nodetype), (struct lys_ext_instance_complex*)parent, &info); |
| 836 | if (!p) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 837 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 838 | ((struct lys_ext_instance_complex*)parent)->def->name); |
| 839 | return EXIT_FAILURE; |
| 840 | } |
| 841 | /* TODO check cardinality */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 842 | break; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /* check identifier uniqueness */ |
Michal Vasko | 15a4337 | 2017-09-25 14:12:42 +0200 | [diff] [blame] | 846 | if (!(module->ctx->models.flags & LY_CTX_TRUSTED) && lys_check_id(child, parent, module)) { |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 847 | return EXIT_FAILURE; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 848 | } |
Radek Krejci | b7155b5 | 2015-06-10 17:03:01 +0200 | [diff] [blame] | 849 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 850 | if (child->parent) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 851 | lys_node_unlink(child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 852 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 853 | |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 854 | if ((child->nodetype & (LYS_INPUT | LYS_OUTPUT)) && parent->nodetype != LYS_EXT) { |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 855 | /* replace the implicit input/output node */ |
| 856 | if (child->nodetype == LYS_OUTPUT) { |
| 857 | inout = (struct lys_node_inout *)parent->child->next; |
| 858 | } else { /* LYS_INPUT */ |
| 859 | inout = (struct lys_node_inout *)parent->child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 860 | parent->child = child; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 861 | } |
| 862 | if (inout->next) { |
| 863 | child->next = inout->next; |
| 864 | inout->next->prev = child; |
| 865 | inout->next = NULL; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 866 | } else { |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 867 | parent->child->prev = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 868 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 869 | child->prev = inout->prev; |
| 870 | if (inout->prev->next) { |
| 871 | inout->prev->next = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 872 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 873 | inout->prev = (struct lys_node *)inout; |
| 874 | child->parent = parent; |
| 875 | inout->parent = NULL; |
| 876 | lys_node_free((struct lys_node *)inout, NULL, 0); |
| 877 | } else { |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 878 | if (shortcase) { |
| 879 | /* create the implicit case to allow it to serve as a target of the augments, |
| 880 | * it won't be printed, but it will be present in the tree */ |
| 881 | c = calloc(1, sizeof *c); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 882 | LY_CHECK_ERR_RETURN(!c, LOGMEM(ctx), EXIT_FAILURE); |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 883 | c->name = lydict_insert(module->ctx, child->name, 0); |
| 884 | c->flags = LYS_IMPLICIT; |
| 885 | c->module = module; |
| 886 | c->nodetype = LYS_CASE; |
| 887 | c->prev = (struct lys_node*)c; |
| 888 | lys_node_addchild(parent, module, (struct lys_node*)c); |
| 889 | parent = (struct lys_node*)c; |
| 890 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 891 | /* connect the child correctly */ |
| 892 | if (!parent) { |
| 893 | if (module->data) { |
| 894 | module->data->prev->next = child; |
| 895 | child->prev = module->data->prev; |
| 896 | module->data->prev = child; |
| 897 | } else { |
| 898 | module->data = child; |
| 899 | } |
| 900 | } else { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 901 | pchild = lys_child(parent, child->nodetype); |
| 902 | assert(pchild); |
| 903 | |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 904 | child->parent = parent; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 905 | if (!(*pchild)) { |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 906 | /* the only/first child of the parent */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 907 | *pchild = child; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 908 | iter = child; |
| 909 | } else { |
| 910 | /* add a new child at the end of parent's child list */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 911 | iter = (*pchild)->prev; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 912 | iter->next = child; |
| 913 | child->prev = iter; |
| 914 | } |
| 915 | while (iter->next) { |
| 916 | iter = iter->next; |
| 917 | iter->parent = parent; |
| 918 | } |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 919 | (*pchild)->prev = iter; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 920 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 921 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 922 | |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 923 | /* check config value (but ignore them in groupings and augments) */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 924 | 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] | 925 | if (parent && !iter) { |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 926 | for (iter = child; iter && !(iter->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); iter = iter->parent); |
| 927 | if (!iter && (parent->flags & LYS_CONFIG_R) && (child->flags & LYS_CONFIG_W)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 928 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, child, "true", "config"); |
| 929 | LOGVAL(ctx, 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] | 930 | return EXIT_FAILURE; |
| 931 | } |
| 932 | } |
| 933 | |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 934 | /* propagate information about status data presence */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 935 | 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] | 936 | (child->flags & LYS_INCL_STATUS)) { |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 937 | for(iter = parent; iter; iter = lys_parent(iter)) { |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 938 | /* store it only into container or list - the only data inner nodes */ |
| 939 | if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
| 940 | if (iter->flags & LYS_INCL_STATUS) { |
| 941 | /* done, someone else set it already from here */ |
| 942 | break; |
| 943 | } |
| 944 | /* set flag about including status data */ |
| 945 | iter->flags |= LYS_INCL_STATUS; |
| 946 | } |
| 947 | } |
| 948 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 949 | |
| 950 | /* create implicit input/output nodes to have available them as possible target for augment */ |
Radek Krejci | 6025123 | 2017-08-24 17:13:08 +0200 | [diff] [blame] | 951 | if ((child->nodetype & (LYS_RPC | LYS_ACTION)) && !child->child) { |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 952 | in = calloc(1, sizeof *in); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 953 | out = calloc(1, sizeof *out); |
| 954 | if (!in || !out) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 955 | LOGMEM(ctx); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 956 | free(in); |
| 957 | free(out); |
| 958 | return EXIT_FAILURE; |
| 959 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 960 | in->nodetype = LYS_INPUT; |
| 961 | in->name = lydict_insert(child->module->ctx, "input", 5); |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 962 | out->nodetype = LYS_OUTPUT; |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 963 | out->name = lydict_insert(child->module->ctx, "output", 6); |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 964 | in->module = out->module = child->module; |
| 965 | in->parent = out->parent = child; |
| 966 | in->flags = out->flags = LYS_IMPLICIT; |
| 967 | in->next = (struct lys_node *)out; |
| 968 | in->prev = (struct lys_node *)out; |
| 969 | out->prev = (struct lys_node *)in; |
| 970 | child->child = (struct lys_node *)in; |
| 971 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 972 | return EXIT_SUCCESS; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 973 | } |
| 974 | |
Michal Vasko | 2924566 | 2017-04-18 15:56:31 +0200 | [diff] [blame] | 975 | const struct lys_module * |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 976 | lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const char *revision, int internal, int implement) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 977 | { |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 978 | char *enlarged_data = NULL; |
Radek Krejci | 0b5805d | 2015-08-13 09:38:02 +0200 | [diff] [blame] | 979 | struct lys_module *mod = NULL; |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 980 | unsigned int len; |
Radek Krejci | f347abc | 2016-06-22 10:18:47 +0200 | [diff] [blame] | 981 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 982 | if (!ctx || !data) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 983 | LOGARG; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 984 | return NULL; |
| 985 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 986 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 987 | if (!internal && format == LYS_IN_YANG) { |
| 988 | /* enlarge data by 2 bytes for flex */ |
| 989 | len = strlen(data); |
| 990 | enlarged_data = malloc((len + 2) * sizeof *enlarged_data); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 991 | LY_CHECK_ERR_RETURN(!enlarged_data, LOGMEM(ctx), NULL); |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 992 | memcpy(enlarged_data, data, len); |
| 993 | enlarged_data[len] = enlarged_data[len + 1] = '\0'; |
| 994 | data = enlarged_data; |
| 995 | } |
| 996 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 997 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 998 | case LYS_IN_YIN: |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 999 | mod = yin_read_module(ctx, data, revision, implement); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1000 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1001 | case LYS_IN_YANG: |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1002 | mod = yang_read_module(ctx, data, 0, revision, implement); |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1003 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1004 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1005 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1006 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1007 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1008 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1009 | free(enlarged_data); |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 1010 | |
| 1011 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 1012 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 1013 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 1014 | * the anotation definitions available in the internal schema structure. There is another hack in schema |
| 1015 | * printers to do not print this internally added annotation. */ |
| 1016 | if (mod && ly_strequal(mod->name, "ietf-netconf", 0)) { |
Radek Krejci | 5b19066 | 2017-04-13 08:56:14 +0200 | [diff] [blame] | 1017 | if (lyp_add_ietf_netconf_annotations(mod)) { |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 1018 | lys_free(mod, NULL, 1, 1); |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 1019 | return NULL; |
| 1020 | } |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 1021 | } |
| 1022 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1023 | return mod; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1026 | API const struct lys_module * |
| 1027 | lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format) |
| 1028 | { |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1029 | return lys_parse_mem_(ctx, data, format, NULL, 0, 1); |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1030 | } |
| 1031 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1032 | struct lys_submodule * |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1033 | 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] | 1034 | { |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1035 | char *enlarged_data = NULL; |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1036 | struct lys_submodule *submod = NULL; |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1037 | unsigned int len; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1038 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1039 | assert(module); |
| 1040 | assert(data); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1041 | |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1042 | if (format == LYS_IN_YANG) { |
| 1043 | /* enlarge data by 2 bytes for flex */ |
| 1044 | len = strlen(data); |
| 1045 | enlarged_data = malloc((len + 2) * sizeof *enlarged_data); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1046 | LY_CHECK_ERR_RETURN(!enlarged_data, LOGMEM(module->ctx), NULL); |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1047 | memcpy(enlarged_data, data, len); |
| 1048 | enlarged_data[len] = enlarged_data[len + 1] = '\0'; |
| 1049 | data = enlarged_data; |
| 1050 | } |
| 1051 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1052 | /* get the main module */ |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 1053 | module = lys_main_module(module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1054 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1055 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1056 | case LYS_IN_YIN: |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1057 | submod = yin_read_submodule(module, data, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1058 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1059 | case LYS_IN_YANG: |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1060 | submod = yang_read_submodule(module, data, 0, unres); |
| 1061 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1062 | default: |
Radek Krejci | 90a550a | 2016-04-13 16:00:58 +0200 | [diff] [blame] | 1063 | assert(0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1064 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1065 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1066 | |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1067 | free(enlarged_data); |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1068 | return submod; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1069 | } |
| 1070 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1071 | API const struct lys_module * |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1072 | lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format) |
| 1073 | { |
| 1074 | int fd; |
| 1075 | const struct lys_module *ret; |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 1076 | const char *rev, *dot, *filename; |
| 1077 | size_t len; |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1078 | |
| 1079 | if (!ctx || !path) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1080 | LOGARG; |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1081 | return NULL; |
| 1082 | } |
| 1083 | |
| 1084 | fd = open(path, O_RDONLY); |
| 1085 | if (fd == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1086 | LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)); |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1087 | return NULL; |
| 1088 | } |
| 1089 | |
| 1090 | ret = lys_parse_fd(ctx, fd, format); |
| 1091 | close(fd); |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 1092 | |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 1093 | if (!ret) { |
| 1094 | /* error */ |
| 1095 | return NULL; |
| 1096 | } |
| 1097 | |
| 1098 | /* check that name and revision match filename */ |
| 1099 | filename = strrchr(path, '/'); |
| 1100 | if (!filename) { |
| 1101 | filename = path; |
| 1102 | } else { |
| 1103 | filename++; |
| 1104 | } |
| 1105 | rev = strchr(filename, '@'); |
| 1106 | dot = strrchr(filename, '.'); |
| 1107 | |
| 1108 | /* name */ |
| 1109 | len = strlen(ret->name); |
| 1110 | if (strncmp(filename, ret->name, len) || |
| 1111 | ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1112 | LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, ret->name); |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 1113 | } |
| 1114 | if (rev) { |
| 1115 | len = dot - ++rev; |
| 1116 | if (!ret->rev_size || len != 10 || strncmp(ret->rev[0].date, rev, len)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1117 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 1118 | ret->rev_size ? ret->rev[0].date : "none"); |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | if (!ret->filepath) { |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 1123 | /* store URI */ |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1124 | ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0); |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 1125 | } |
| 1126 | |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1127 | return ret; |
| 1128 | } |
| 1129 | |
| 1130 | API const struct lys_module * |
| 1131 | lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format) |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1132 | { |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1133 | return lys_parse_fd_(ctx, fd, format, NULL, 1); |
| 1134 | } |
| 1135 | |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1136 | static void |
| 1137 | lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd) |
| 1138 | { |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1139 | #ifdef __APPLE__ |
| 1140 | char path[MAXPATHLEN]; |
| 1141 | #else |
Michal Vasko | 25601f3 | 2018-02-16 09:41:46 +0100 | [diff] [blame] | 1142 | int len; |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1143 | char path[PATH_MAX], proc_path[32]; |
| 1144 | #endif |
| 1145 | |
| 1146 | #ifdef __APPLE__ |
| 1147 | if (fcntl(fd, F_GETPATH, path) != -1) { |
| 1148 | *filename = lydict_insert(ctx, path, 0); |
| 1149 | } |
| 1150 | #else |
| 1151 | /* get URI if there is /proc */ |
| 1152 | sprintf(proc_path, "/proc/self/fd/%d", fd); |
| 1153 | if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) { |
| 1154 | *filename = lydict_insert(ctx, path, len); |
| 1155 | } |
| 1156 | #endif |
| 1157 | } |
| 1158 | |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1159 | const struct lys_module * |
| 1160 | lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const char *revision, int implement) |
| 1161 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1162 | const struct lys_module *module; |
Radek Krejci | 0fb1150 | 2017-01-31 16:45:42 +0100 | [diff] [blame] | 1163 | size_t length; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1164 | char *addr; |
| 1165 | |
| 1166 | if (!ctx || fd < 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1167 | LOGARG; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1168 | return NULL; |
| 1169 | } |
| 1170 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1171 | if (lyp_mmap(ctx, fd, format == LYS_IN_YANG ? 1 : 0, &length, (void **)&addr)) { |
| 1172 | LOGERR(ctx, LY_ESYS, "Mapping file descriptor into memory failed (%s()).", __func__); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1173 | return NULL; |
Radek Krejci | 10c216a | 2017-02-01 10:36:00 +0100 | [diff] [blame] | 1174 | } else if (!addr) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1175 | LOGERR(ctx, LY_EINVAL, "Empty schema file."); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1176 | return NULL; |
| 1177 | } |
Radek Krejci | 0fb1150 | 2017-01-31 16:45:42 +0100 | [diff] [blame] | 1178 | |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1179 | module = lys_parse_mem_(ctx, addr, format, revision, 1, implement); |
Radek Krejci | 0fb1150 | 2017-01-31 16:45:42 +0100 | [diff] [blame] | 1180 | lyp_munmap(addr, length); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1181 | |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1182 | if (module && !module->filepath) { |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1183 | lys_parse_set_filename(ctx, (const char **)&module->filepath, fd); |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1184 | } |
| 1185 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1186 | return module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1187 | } |
| 1188 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1189 | struct lys_submodule * |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1190 | 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] | 1191 | { |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1192 | struct lys_submodule *submodule; |
Radek Krejci | 0fb1150 | 2017-01-31 16:45:42 +0100 | [diff] [blame] | 1193 | size_t length; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1194 | char *addr; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1195 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1196 | assert(module); |
| 1197 | assert(fd >= 0); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1198 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1199 | if (lyp_mmap(module->ctx, fd, format == LYS_IN_YANG ? 1 : 0, &length, (void **)&addr)) { |
| 1200 | LOGERR(module->ctx, LY_ESYS, "Mapping file descriptor into memory failed (%s()).", __func__); |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1201 | return NULL; |
Radek Krejci | 10c216a | 2017-02-01 10:36:00 +0100 | [diff] [blame] | 1202 | } else if (!addr) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1203 | LOGERR(module->ctx, LY_EINVAL, "Empty submodule schema file."); |
Michal Vasko | 2e7241e | 2016-02-15 16:06:34 +0100 | [diff] [blame] | 1204 | return NULL; |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1205 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1206 | |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1207 | /* get the main module */ |
| 1208 | module = lys_main_module(module); |
| 1209 | |
| 1210 | switch (format) { |
| 1211 | case LYS_IN_YIN: |
| 1212 | submodule = yin_read_submodule(module, addr, unres); |
| 1213 | break; |
| 1214 | case LYS_IN_YANG: |
| 1215 | submodule = yang_read_submodule(module, addr, 0, unres); |
| 1216 | break; |
| 1217 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1218 | LOGINT(module->ctx); |
Michal Vasko | 85d4152 | 2017-02-24 09:49:16 +0100 | [diff] [blame] | 1219 | return NULL; |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1220 | } |
| 1221 | |
Radek Krejci | c645a3a | 2017-01-31 16:59:00 +0100 | [diff] [blame] | 1222 | lyp_munmap(addr, length); |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1223 | |
| 1224 | if (submodule && !submodule->filepath) { |
| 1225 | lys_parse_set_filename(module->ctx, (const char **)&submodule->filepath, fd); |
| 1226 | } |
| 1227 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1228 | return submodule; |
| 1229 | |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1230 | } |
| 1231 | |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 1232 | int |
| 1233 | lys_ext_iter(struct lys_ext_instance **ext, uint8_t ext_size, uint8_t start, LYEXT_SUBSTMT substmt) |
| 1234 | { |
| 1235 | unsigned int u; |
| 1236 | |
| 1237 | for (u = start; u < ext_size; u++) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1238 | if (ext[u]->insubstmt == substmt) { |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 1239 | return u; |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | return -1; |
| 1244 | } |
| 1245 | |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1246 | /* |
| 1247 | * duplicate extension instance |
| 1248 | */ |
| 1249 | int |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1250 | lys_ext_dup(struct ly_ctx *ctx, struct lys_module *mod, struct lys_ext_instance **orig, uint8_t size, void *parent, |
| 1251 | LYEXT_PAR parent_type, struct lys_ext_instance ***new, int shallow, struct unres_schema *unres) |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1252 | { |
| 1253 | int i; |
| 1254 | uint8_t u = 0; |
| 1255 | struct lys_ext_instance **result; |
| 1256 | struct unres_ext *info, *info_orig; |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 1257 | size_t len; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1258 | |
| 1259 | assert(new); |
| 1260 | |
| 1261 | if (!size) { |
| 1262 | if (orig) { |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1263 | LOGINT(ctx); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1264 | return EXIT_FAILURE; |
| 1265 | } |
| 1266 | (*new) = NULL; |
| 1267 | return EXIT_SUCCESS; |
| 1268 | } |
| 1269 | |
| 1270 | (*new) = result = calloc(size, sizeof *result); |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1271 | LY_CHECK_ERR_RETURN(!result, LOGMEM(ctx), EXIT_FAILURE); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1272 | for (u = 0; u < size; u++) { |
| 1273 | if (orig[u]) { |
| 1274 | /* resolved extension instance, just duplicate it */ |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 1275 | switch(orig[u]->ext_type) { |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1276 | case LYEXT_FLAG: |
| 1277 | result[u] = malloc(sizeof(struct lys_ext_instance)); |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1278 | LY_CHECK_ERR_GOTO(!result[u], LOGMEM(ctx), error); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1279 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 1280 | case LYEXT_COMPLEX: |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 1281 | len = ((struct lyext_plugin_complex*)orig[u]->def->plugin)->instance_size; |
| 1282 | result[u] = calloc(1, len); |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1283 | LY_CHECK_ERR_GOTO(!result[u], LOGMEM(ctx), error); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1284 | |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1285 | ((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] | 1286 | /* TODO duplicate data in extension instance content */ |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 1287 | memcpy((void*)result[u] + sizeof(**orig), (void*)orig[u] + sizeof(**orig), len - sizeof(**orig)); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 1288 | break; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1289 | } |
| 1290 | /* generic part */ |
| 1291 | result[u]->def = orig[u]->def; |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 1292 | result[u]->flags = LYEXT_OPT_CONTENT; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1293 | result[u]->arg_value = lydict_insert(ctx, orig[u]->arg_value, 0); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1294 | result[u]->parent = parent; |
| 1295 | result[u]->parent_type = parent_type; |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1296 | result[u]->insubstmt = orig[u]->insubstmt; |
| 1297 | result[u]->insubstmt_index = orig[u]->insubstmt_index; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 1298 | result[u]->ext_type = orig[u]->ext_type; |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 1299 | result[u]->priv = NULL; |
| 1300 | result[u]->nodetype = LYS_EXT; |
| 1301 | result[u]->module = mod; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1302 | |
| 1303 | /* extensions */ |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1304 | result[u]->ext_size = orig[u]->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1305 | if (lys_ext_dup(ctx, mod, orig[u]->ext, orig[u]->ext_size, result[u], |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1306 | LYEXT_PAR_EXTINST, &result[u]->ext, shallow, unres)) { |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1307 | goto error; |
| 1308 | } |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1309 | |
| 1310 | /* in case of shallow copy (duplication for deviation), duplicate only the link to private data |
| 1311 | * in a new copy, otherwise (grouping instantiation) do not duplicate the private data */ |
| 1312 | if (shallow) { |
| 1313 | result[u]->priv = orig[u]->priv; |
| 1314 | } |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1315 | } else { |
| 1316 | /* original extension is not yet resolved, so duplicate it in unres */ |
| 1317 | i = unres_schema_find(unres, -1, &orig, UNRES_EXT); |
| 1318 | if (i == -1) { |
| 1319 | /* extension not found in unres */ |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1320 | LOGINT(ctx); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1321 | goto error; |
| 1322 | } |
| 1323 | info_orig = unres->str_snode[i]; |
| 1324 | info = malloc(sizeof *info); |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1325 | LY_CHECK_ERR_GOTO(!info, LOGMEM(ctx), error); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1326 | info->datatype = info_orig->datatype; |
| 1327 | if (info->datatype == LYS_IN_YIN) { |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1328 | info->data.yin = lyxml_dup_elem(ctx, info_orig->data.yin, NULL, 1); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1329 | } /* else TODO YANG */ |
| 1330 | info->parent = parent; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 1331 | info->mod = mod; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1332 | info->parent_type = parent_type; |
| 1333 | info->ext_index = u; |
| 1334 | if (unres_schema_add_node(info->mod, unres, new, UNRES_EXT, (struct lys_node *)info) == -1) { |
| 1335 | goto error; |
| 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | return EXIT_SUCCESS; |
| 1341 | |
| 1342 | error: |
| 1343 | (*new) = NULL; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1344 | lys_extension_instances_free(ctx, result, u, NULL); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1345 | return EXIT_FAILURE; |
| 1346 | } |
| 1347 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1348 | static struct lys_restr * |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1349 | 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] | 1350 | { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1351 | struct lys_restr *result; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1352 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1353 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1354 | if (!size) { |
| 1355 | return NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1356 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1357 | |
| 1358 | result = calloc(size, sizeof *result); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1359 | LY_CHECK_ERR_RETURN(!result, LOGMEM(mod->ctx), NULL); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1360 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1361 | for (i = 0; i < size; i++) { |
Radek Krejci | 77f22b2 | 2017-01-17 15:23:03 +0100 | [diff] [blame] | 1362 | result[i].ext_size = old[i].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1363 | lys_ext_dup(mod->ctx, 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] | 1364 | result[i].expr = lydict_insert(mod->ctx, old[i].expr, 0); |
| 1365 | result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0); |
| 1366 | result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0); |
| 1367 | result[i].eapptag = lydict_insert(mod->ctx, old[i].eapptag, 0); |
| 1368 | result[i].emsg = lydict_insert(mod->ctx, old[i].emsg, 0); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | return result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1372 | } |
| 1373 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1374 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1375 | lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr, |
| 1376 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1377 | { |
| 1378 | assert(ctx); |
| 1379 | if (!restr) { |
| 1380 | return; |
| 1381 | } |
| 1382 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1383 | lys_extension_instances_free(ctx, restr->ext, restr->ext_size, private_destructor); |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1384 | lydict_remove(ctx, restr->expr); |
| 1385 | lydict_remove(ctx, restr->dsc); |
| 1386 | lydict_remove(ctx, restr->ref); |
| 1387 | lydict_remove(ctx, restr->eapptag); |
| 1388 | lydict_remove(ctx, restr->emsg); |
| 1389 | } |
| 1390 | |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 1391 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1392 | lys_iffeature_free(struct ly_ctx *ctx, struct lys_iffeature *iffeature, uint8_t iffeature_size, |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 1393 | int shallow, void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 1394 | { |
| 1395 | uint8_t i; |
| 1396 | |
| 1397 | for (i = 0; i < iffeature_size; ++i) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1398 | lys_extension_instances_free(ctx, iffeature[i].ext, iffeature[i].ext_size, private_destructor); |
Michal Vasko | 15a4337 | 2017-09-25 14:12:42 +0200 | [diff] [blame] | 1399 | if (!shallow) { |
Frank Rimpler | 2a503f5 | 2017-09-12 15:21:18 +0000 | [diff] [blame] | 1400 | free(iffeature[i].expr); |
| 1401 | free(iffeature[i].features); |
| 1402 | } |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 1403 | } |
| 1404 | free(iffeature); |
| 1405 | } |
| 1406 | |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1407 | static int |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1408 | type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old, |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1409 | 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] | 1410 | { |
| 1411 | int i; |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1412 | unsigned int u; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1413 | |
| 1414 | switch (base) { |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1415 | case LY_TYPE_BINARY: |
| 1416 | if (old->info.binary.length) { |
| 1417 | new->info.binary.length = lys_restr_dup(mod, old->info.binary.length, 1, shallow, unres); |
| 1418 | } |
| 1419 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1420 | |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1421 | case LY_TYPE_BITS: |
| 1422 | new->info.bits.count = old->info.bits.count; |
| 1423 | if (new->info.bits.count) { |
| 1424 | new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1425 | LY_CHECK_ERR_RETURN(!new->info.bits.bit, LOGMEM(mod->ctx), -1); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1426 | |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1427 | for (u = 0; u < new->info.bits.count; u++) { |
| 1428 | new->info.bits.bit[u].name = lydict_insert(mod->ctx, old->info.bits.bit[u].name, 0); |
| 1429 | new->info.bits.bit[u].dsc = lydict_insert(mod->ctx, old->info.bits.bit[u].dsc, 0); |
| 1430 | new->info.bits.bit[u].ref = lydict_insert(mod->ctx, old->info.bits.bit[u].ref, 0); |
| 1431 | new->info.bits.bit[u].flags = old->info.bits.bit[u].flags; |
| 1432 | new->info.bits.bit[u].pos = old->info.bits.bit[u].pos; |
| 1433 | new->info.bits.bit[u].ext_size = old->info.bits.bit[u].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1434 | if (lys_ext_dup(mod->ctx, mod, old->info.bits.bit[u].ext, old->info.bits.bit[u].ext_size, |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1435 | &new->info.bits.bit[u], LYEXT_PAR_TYPE_BIT, |
| 1436 | &new->info.bits.bit[u].ext, shallow, unres)) { |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1437 | return -1; |
| 1438 | } |
| 1439 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1440 | } |
| 1441 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1442 | |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1443 | case LY_TYPE_DEC64: |
| 1444 | new->info.dec64.dig = old->info.dec64.dig; |
| 1445 | new->info.dec64.div = old->info.dec64.div; |
| 1446 | if (old->info.dec64.range) { |
| 1447 | new->info.dec64.range = lys_restr_dup(mod, old->info.dec64.range, 1, shallow, unres); |
| 1448 | } |
| 1449 | break; |
| 1450 | |
| 1451 | case LY_TYPE_ENUM: |
| 1452 | new->info.enums.count = old->info.enums.count; |
| 1453 | if (new->info.enums.count) { |
| 1454 | new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1455 | LY_CHECK_ERR_RETURN(!new->info.enums.enm, LOGMEM(mod->ctx), -1); |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1456 | |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1457 | for (u = 0; u < new->info.enums.count; u++) { |
| 1458 | new->info.enums.enm[u].name = lydict_insert(mod->ctx, old->info.enums.enm[u].name, 0); |
| 1459 | new->info.enums.enm[u].dsc = lydict_insert(mod->ctx, old->info.enums.enm[u].dsc, 0); |
| 1460 | new->info.enums.enm[u].ref = lydict_insert(mod->ctx, old->info.enums.enm[u].ref, 0); |
| 1461 | new->info.enums.enm[u].flags = old->info.enums.enm[u].flags; |
| 1462 | new->info.enums.enm[u].value = old->info.enums.enm[u].value; |
| 1463 | new->info.enums.enm[u].ext_size = old->info.enums.enm[u].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1464 | if (lys_ext_dup(mod->ctx, mod, old->info.enums.enm[u].ext, old->info.enums.enm[u].ext_size, |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1465 | &new->info.enums.enm[u], LYEXT_PAR_TYPE_ENUM, |
| 1466 | &new->info.enums.enm[u].ext, shallow, unres)) { |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1467 | return -1; |
| 1468 | } |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1469 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1470 | } |
| 1471 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1472 | |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1473 | case LY_TYPE_IDENT: |
| 1474 | new->info.ident.count = old->info.ident.count; |
| 1475 | if (old->info.ident.count) { |
| 1476 | new->info.ident.ref = malloc(old->info.ident.count * sizeof *new->info.ident.ref); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1477 | LY_CHECK_ERR_RETURN(!new->info.ident.ref, LOGMEM(mod->ctx), -1); |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1478 | memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref); |
| 1479 | } else { |
| 1480 | /* there can be several unresolved base identities, duplicate them all */ |
| 1481 | i = -1; |
| 1482 | do { |
| 1483 | i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF); |
| 1484 | if (i != -1) { |
| 1485 | if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i]) == -1) { |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1486 | return -1; |
| 1487 | } |
| 1488 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1489 | --i; |
| 1490 | } while (i > -1); |
| 1491 | } |
| 1492 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1493 | |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1494 | case LY_TYPE_INST: |
| 1495 | new->info.inst.req = old->info.inst.req; |
| 1496 | break; |
| 1497 | |
| 1498 | case LY_TYPE_INT8: |
| 1499 | case LY_TYPE_INT16: |
| 1500 | case LY_TYPE_INT32: |
| 1501 | case LY_TYPE_INT64: |
| 1502 | case LY_TYPE_UINT8: |
| 1503 | case LY_TYPE_UINT16: |
| 1504 | case LY_TYPE_UINT32: |
| 1505 | case LY_TYPE_UINT64: |
| 1506 | if (old->info.num.range) { |
| 1507 | new->info.num.range = lys_restr_dup(mod, old->info.num.range, 1, shallow, unres); |
| 1508 | } |
| 1509 | break; |
| 1510 | |
| 1511 | case LY_TYPE_LEAFREF: |
| 1512 | if (old->info.lref.path) { |
| 1513 | new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0); |
| 1514 | if (!in_grp && unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent) == -1) { |
| 1515 | return -1; |
| 1516 | } |
| 1517 | } |
| 1518 | break; |
| 1519 | |
| 1520 | case LY_TYPE_STRING: |
| 1521 | if (old->info.str.length) { |
| 1522 | new->info.str.length = lys_restr_dup(mod, old->info.str.length, 1, shallow, unres); |
| 1523 | } |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 1524 | if (old->info.str.pat_count) { |
| 1525 | new->info.str.patterns = lys_restr_dup(mod, old->info.str.patterns, old->info.str.pat_count, shallow, unres); |
| 1526 | new->info.str.pat_count = old->info.str.pat_count; |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 1527 | #ifdef LY_ENABLED_CACHE |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 1528 | if (!in_grp) { |
| 1529 | new->info.str.patterns_pcre = malloc(new->info.str.pat_count * 2 * sizeof *new->info.str.patterns_pcre); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1530 | LY_CHECK_ERR_RETURN(!new->info.str.patterns_pcre, LOGMEM(mod->ctx), -1); |
Radek Krejci | a4c107d | 2017-10-27 14:19:26 +0200 | [diff] [blame] | 1531 | for (u = 0; u < new->info.str.pat_count; u++) { |
Michal Vasko | a26db30 | 2018-02-14 15:22:10 +0100 | [diff] [blame] | 1532 | if (lyp_precompile_pattern(mod->ctx, &new->info.str.patterns[u].expr[1], |
Radek Krejci | a4c107d | 2017-10-27 14:19:26 +0200 | [diff] [blame] | 1533 | (pcre**)&new->info.str.patterns_pcre[2 * u], |
| 1534 | (pcre_extra**)&new->info.str.patterns_pcre[2 * u + 1])) { |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 1535 | free(new->info.str.patterns_pcre); |
| 1536 | new->info.str.patterns_pcre = NULL; |
| 1537 | return -1; |
| 1538 | } |
| 1539 | } |
| 1540 | } |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 1541 | #endif |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 1542 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1543 | break; |
| 1544 | |
| 1545 | case LY_TYPE_UNION: |
| 1546 | new->info.uni.has_ptr_type = old->info.uni.has_ptr_type; |
| 1547 | new->info.uni.count = old->info.uni.count; |
| 1548 | if (new->info.uni.count) { |
| 1549 | new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1550 | LY_CHECK_ERR_RETURN(!new->info.uni.types, LOGMEM(mod->ctx), -1); |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1551 | |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1552 | for (u = 0; u < new->info.uni.count; u++) { |
| 1553 | if (lys_type_dup(mod, parent, &(new->info.uni.types[u]), &(old->info.uni.types[u]), in_grp, |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1554 | shallow, unres)) { |
| 1555 | return -1; |
| 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | break; |
| 1560 | |
| 1561 | default: |
| 1562 | /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */ |
| 1563 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1564 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1565 | |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1566 | return EXIT_SUCCESS; |
| 1567 | } |
| 1568 | |
| 1569 | struct yang_type * |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1570 | 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] | 1571 | int in_grp, int shallow, struct unres_schema *unres) |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1572 | { |
| 1573 | struct yang_type *new; |
| 1574 | |
| 1575 | new = calloc(1, sizeof *new); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1576 | LY_CHECK_ERR_RETURN(!new, LOGMEM(module->ctx), NULL); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1577 | new->flags = old->flags; |
| 1578 | new->base = old->base; |
Pavol Vican | 6658629 | 2016-04-07 11:38:52 +0200 | [diff] [blame] | 1579 | new->name = lydict_insert(module->ctx, old->name, 0); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1580 | new->type = type; |
| 1581 | if (!new->name) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1582 | LOGMEM(module->ctx); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1583 | goto error; |
| 1584 | } |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1585 | 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] | 1586 | new->type->base = new->base; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1587 | lys_type_free(module->ctx, new->type, NULL); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1588 | memset(&new->type->info, 0, sizeof new->type->info); |
| 1589 | goto error; |
| 1590 | } |
| 1591 | return new; |
| 1592 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1593 | error: |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1594 | free(new); |
| 1595 | return NULL; |
| 1596 | } |
| 1597 | |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1598 | int |
| 1599 | lys_copy_union_leafrefs(struct lys_module *mod, struct lys_node *parent, struct lys_type *type, struct lys_type *prev_new, |
| 1600 | struct unres_schema *unres) |
| 1601 | { |
| 1602 | struct lys_type new; |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1603 | unsigned int i, top_type; |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1604 | struct lys_ext_instance **ext; |
| 1605 | uint8_t ext_size; |
| 1606 | void *reloc; |
| 1607 | |
| 1608 | if (!prev_new) { |
| 1609 | /* this is the "top-level" type, meaning it is a real type and no typedef directly above */ |
| 1610 | top_type = 1; |
| 1611 | |
| 1612 | memset(&new, 0, sizeof new); |
| 1613 | |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1614 | new.base = type->base; |
| 1615 | new.parent = (struct lys_tpdf *)parent; |
| 1616 | |
| 1617 | prev_new = &new; |
| 1618 | } else { |
| 1619 | /* this is not top-level type, just a type of a typedef */ |
| 1620 | top_type = 0; |
| 1621 | } |
| 1622 | |
Radek Krejci | 9c5cb6d | 2017-08-09 11:15:23 +0200 | [diff] [blame] | 1623 | assert(type->der); |
| 1624 | if (type->der->module) { |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1625 | /* typedef, skip it, but keep the extensions */ |
| 1626 | ext_size = type->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1627 | if (lys_ext_dup(mod->ctx, mod, type->ext, type->ext_size, prev_new, LYEXT_PAR_TYPE, &ext, 0, unres)) { |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1628 | return -1; |
| 1629 | } |
| 1630 | if (prev_new->ext) { |
| 1631 | reloc = realloc(prev_new->ext, (prev_new->ext_size + ext_size) * sizeof *prev_new->ext); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1632 | LY_CHECK_ERR_RETURN(!reloc, LOGMEM(mod->ctx), -1); |
Radek Krejci | 70379e2 | 2017-08-09 11:21:07 +0200 | [diff] [blame] | 1633 | prev_new->ext = reloc; |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1634 | |
| 1635 | memcpy(prev_new->ext + prev_new->ext_size, ext, ext_size * sizeof *ext); |
| 1636 | free(ext); |
| 1637 | |
| 1638 | prev_new->ext_size += ext_size; |
| 1639 | } else { |
| 1640 | prev_new->ext = ext; |
| 1641 | prev_new->ext_size = ext_size; |
| 1642 | } |
| 1643 | |
| 1644 | if (lys_copy_union_leafrefs(mod, parent, &type->der->type, prev_new, unres)) { |
| 1645 | return -1; |
| 1646 | } |
| 1647 | } else { |
| 1648 | /* type, just make a deep copy */ |
| 1649 | switch (type->base) { |
| 1650 | case LY_TYPE_UNION: |
| 1651 | prev_new->info.uni.has_ptr_type = type->info.uni.has_ptr_type; |
| 1652 | prev_new->info.uni.count = type->info.uni.count; |
| 1653 | /* this cannot be a typedef anymore */ |
| 1654 | assert(prev_new->info.uni.count); |
| 1655 | |
| 1656 | prev_new->info.uni.types = calloc(prev_new->info.uni.count, sizeof *prev_new->info.uni.types); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1657 | LY_CHECK_ERR_RETURN(!prev_new->info.uni.types, LOGMEM(mod->ctx), -1); |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1658 | |
| 1659 | for (i = 0; i < prev_new->info.uni.count; i++) { |
| 1660 | if (lys_copy_union_leafrefs(mod, parent, &(type->info.uni.types[i]), &(prev_new->info.uni.types[i]), unres)) { |
| 1661 | return -1; |
| 1662 | } |
| 1663 | } |
| 1664 | |
| 1665 | prev_new->der = type->der; |
| 1666 | break; |
| 1667 | default: |
| 1668 | if (lys_type_dup(mod, parent, prev_new, type, 0, 0, unres)) { |
| 1669 | return -1; |
| 1670 | } |
| 1671 | break; |
| 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | if (top_type) { |
| 1676 | memcpy(type, prev_new, sizeof *type); |
| 1677 | } |
| 1678 | return EXIT_SUCCESS; |
| 1679 | } |
| 1680 | |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1681 | API const void * |
| 1682 | lys_ext_instance_substmt(const struct lys_ext_instance *ext) |
| 1683 | { |
| 1684 | if (!ext) { |
| 1685 | return NULL; |
| 1686 | } |
| 1687 | |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1688 | switch (ext->insubstmt) { |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1689 | case LYEXT_SUBSTMT_SELF: |
| 1690 | case LYEXT_SUBSTMT_MODIFIER: |
| 1691 | case LYEXT_SUBSTMT_VERSION: |
| 1692 | return NULL; |
| 1693 | case LYEXT_SUBSTMT_ARGUMENT: |
| 1694 | if (ext->parent_type == LYEXT_PAR_EXT) { |
| 1695 | return ((struct lys_ext_instance*)ext->parent)->arg_value; |
| 1696 | } |
| 1697 | break; |
| 1698 | case LYEXT_SUBSTMT_BASE: |
| 1699 | if (ext->parent_type == LYEXT_PAR_TYPE) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1700 | return ((struct lys_type*)ext->parent)->info.ident.ref[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1701 | } else if (ext->parent_type == LYEXT_PAR_IDENT) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1702 | return ((struct lys_ident*)ext->parent)->base[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1703 | } |
| 1704 | break; |
| 1705 | case LYEXT_SUBSTMT_BELONGSTO: |
| 1706 | if (ext->parent_type == LYEXT_PAR_MODULE && ((struct lys_module*)ext->parent)->type) { |
| 1707 | return ((struct lys_submodule*)ext->parent)->belongsto; |
| 1708 | } |
| 1709 | break; |
| 1710 | case LYEXT_SUBSTMT_CONFIG: |
| 1711 | case LYEXT_SUBSTMT_MANDATORY: |
| 1712 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 1713 | return &((struct lys_node*)ext->parent)->flags; |
| 1714 | } else if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
| 1715 | return &((struct lys_deviate*)ext->parent)->flags; |
| 1716 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 1717 | return &((struct lys_refine*)ext->parent)->flags; |
| 1718 | } |
| 1719 | break; |
| 1720 | case LYEXT_SUBSTMT_CONTACT: |
| 1721 | if (ext->parent_type == LYEXT_PAR_MODULE) { |
| 1722 | return ((struct lys_module*)ext->parent)->contact; |
| 1723 | } |
| 1724 | break; |
| 1725 | case LYEXT_SUBSTMT_DEFAULT: |
| 1726 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 1727 | switch (((struct lys_node*)ext->parent)->nodetype) { |
| 1728 | case LYS_LEAF: |
| 1729 | case LYS_LEAFLIST: |
| 1730 | /* in case of leaf, the index is supposed to be 0, so it will return the |
| 1731 | * correct pointer despite the leaf structure does not have dflt as array */ |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1732 | return ((struct lys_node_leaflist*)ext->parent)->dflt[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1733 | case LYS_CHOICE: |
| 1734 | return ((struct lys_node_choice*)ext->parent)->dflt; |
| 1735 | default: |
| 1736 | /* internal error */ |
| 1737 | break; |
| 1738 | } |
| 1739 | } else if (ext->parent_type == LYEXT_PAR_TPDF) { |
| 1740 | return ((struct lys_tpdf*)ext->parent)->dflt; |
| 1741 | } else if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1742 | return ((struct lys_deviate*)ext->parent)->dflt[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1743 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1744 | return &((struct lys_refine*)ext->parent)->dflt[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1745 | } |
| 1746 | break; |
| 1747 | case LYEXT_SUBSTMT_DESCRIPTION: |
| 1748 | switch (ext->parent_type) { |
| 1749 | case LYEXT_PAR_NODE: |
| 1750 | return ((struct lys_node*)ext->parent)->dsc; |
| 1751 | case LYEXT_PAR_MODULE: |
| 1752 | return ((struct lys_module*)ext->parent)->dsc; |
| 1753 | case LYEXT_PAR_IMPORT: |
| 1754 | return ((struct lys_import*)ext->parent)->dsc; |
| 1755 | case LYEXT_PAR_INCLUDE: |
| 1756 | return ((struct lys_include*)ext->parent)->dsc; |
| 1757 | case LYEXT_PAR_EXT: |
| 1758 | return ((struct lys_ext*)ext->parent)->dsc; |
| 1759 | case LYEXT_PAR_FEATURE: |
| 1760 | return ((struct lys_feature*)ext->parent)->dsc; |
| 1761 | case LYEXT_PAR_TPDF: |
| 1762 | return ((struct lys_tpdf*)ext->parent)->dsc; |
| 1763 | case LYEXT_PAR_TYPE_BIT: |
| 1764 | return ((struct lys_type_bit*)ext->parent)->dsc; |
| 1765 | case LYEXT_PAR_TYPE_ENUM: |
| 1766 | return ((struct lys_type_enum*)ext->parent)->dsc; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1767 | case LYEXT_PAR_RESTR: |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1768 | return ((struct lys_restr*)ext->parent)->dsc; |
| 1769 | case LYEXT_PAR_WHEN: |
| 1770 | return ((struct lys_when*)ext->parent)->dsc; |
| 1771 | case LYEXT_PAR_IDENT: |
| 1772 | return ((struct lys_ident*)ext->parent)->dsc; |
| 1773 | case LYEXT_PAR_DEVIATION: |
| 1774 | return ((struct lys_deviation*)ext->parent)->dsc; |
| 1775 | case LYEXT_PAR_REVISION: |
| 1776 | return ((struct lys_revision*)ext->parent)->dsc; |
| 1777 | case LYEXT_PAR_REFINE: |
| 1778 | return ((struct lys_refine*)ext->parent)->dsc; |
| 1779 | default: |
| 1780 | break; |
| 1781 | } |
| 1782 | break; |
| 1783 | case LYEXT_SUBSTMT_ERRTAG: |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1784 | if (ext->parent_type == LYEXT_PAR_RESTR) { |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1785 | return ((struct lys_restr*)ext->parent)->eapptag; |
| 1786 | } |
| 1787 | break; |
| 1788 | case LYEXT_SUBSTMT_ERRMSG: |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1789 | if (ext->parent_type == LYEXT_PAR_RESTR) { |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1790 | return ((struct lys_restr*)ext->parent)->emsg; |
| 1791 | } |
| 1792 | break; |
| 1793 | case LYEXT_SUBSTMT_DIGITS: |
| 1794 | if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_DEC64) { |
| 1795 | return &((struct lys_type*)ext->parent)->info.dec64.dig; |
| 1796 | } |
| 1797 | break; |
| 1798 | case LYEXT_SUBSTMT_KEY: |
| 1799 | if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) { |
| 1800 | return ((struct lys_node_list*)ext->parent)->keys; |
| 1801 | } |
| 1802 | break; |
| 1803 | case LYEXT_SUBSTMT_MAX: |
| 1804 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 1805 | if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) { |
| 1806 | return &((struct lys_node_list*)ext->parent)->max; |
| 1807 | } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) { |
| 1808 | return &((struct lys_node_leaflist*)ext->parent)->max; |
| 1809 | } |
| 1810 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 1811 | return &((struct lys_refine*)ext->parent)->mod.list.max; |
| 1812 | } |
| 1813 | break; |
| 1814 | case LYEXT_SUBSTMT_MIN: |
| 1815 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 1816 | if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) { |
| 1817 | return &((struct lys_node_list*)ext->parent)->min; |
| 1818 | } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) { |
| 1819 | return &((struct lys_node_leaflist*)ext->parent)->min; |
| 1820 | } |
| 1821 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 1822 | return &((struct lys_refine*)ext->parent)->mod.list.min; |
| 1823 | } |
| 1824 | break; |
| 1825 | case LYEXT_SUBSTMT_NAMESPACE: |
| 1826 | if (ext->parent_type == LYEXT_PAR_MODULE && !((struct lys_module*)ext->parent)->type) { |
| 1827 | return ((struct lys_module*)ext->parent)->ns; |
| 1828 | } |
| 1829 | break; |
| 1830 | case LYEXT_SUBSTMT_ORDEREDBY: |
| 1831 | if (ext->parent_type == LYEXT_PAR_NODE && |
| 1832 | (((struct lys_node*)ext->parent)->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 1833 | return &((struct lys_node_list*)ext->parent)->flags; |
| 1834 | } |
| 1835 | break; |
| 1836 | case LYEXT_SUBSTMT_ORGANIZATION: |
| 1837 | if (ext->parent_type == LYEXT_PAR_MODULE) { |
| 1838 | return ((struct lys_module*)ext->parent)->org; |
| 1839 | } |
| 1840 | break; |
| 1841 | case LYEXT_SUBSTMT_PATH: |
| 1842 | if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) { |
| 1843 | return ((struct lys_type*)ext->parent)->info.lref.path; |
| 1844 | } |
| 1845 | break; |
| 1846 | case LYEXT_SUBSTMT_POSITION: |
| 1847 | if (ext->parent_type == LYEXT_PAR_TYPE_BIT) { |
| 1848 | return &((struct lys_type_bit*)ext->parent)->pos; |
| 1849 | } |
| 1850 | break; |
| 1851 | case LYEXT_SUBSTMT_PREFIX: |
| 1852 | if (ext->parent_type == LYEXT_PAR_MODULE) { |
| 1853 | /* covers also lys_submodule */ |
| 1854 | return ((struct lys_module*)ext->parent)->prefix; |
| 1855 | } else if (ext->parent_type == LYEXT_PAR_IMPORT) { |
| 1856 | return ((struct lys_import*)ext->parent)->prefix; |
| 1857 | } |
| 1858 | break; |
| 1859 | case LYEXT_SUBSTMT_PRESENCE: |
| 1860 | if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_CONTAINER) { |
| 1861 | return ((struct lys_node_container*)ext->parent)->presence; |
| 1862 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 1863 | return ((struct lys_refine*)ext->parent)->mod.presence; |
| 1864 | } |
| 1865 | break; |
| 1866 | case LYEXT_SUBSTMT_REFERENCE: |
| 1867 | switch (ext->parent_type) { |
| 1868 | case LYEXT_PAR_NODE: |
| 1869 | return ((struct lys_node*)ext->parent)->ref; |
| 1870 | case LYEXT_PAR_MODULE: |
| 1871 | return ((struct lys_module*)ext->parent)->ref; |
| 1872 | case LYEXT_PAR_IMPORT: |
| 1873 | return ((struct lys_import*)ext->parent)->ref; |
| 1874 | case LYEXT_PAR_INCLUDE: |
| 1875 | return ((struct lys_include*)ext->parent)->ref; |
| 1876 | case LYEXT_PAR_EXT: |
| 1877 | return ((struct lys_ext*)ext->parent)->ref; |
| 1878 | case LYEXT_PAR_FEATURE: |
| 1879 | return ((struct lys_feature*)ext->parent)->ref; |
| 1880 | case LYEXT_PAR_TPDF: |
| 1881 | return ((struct lys_tpdf*)ext->parent)->ref; |
| 1882 | case LYEXT_PAR_TYPE_BIT: |
| 1883 | return ((struct lys_type_bit*)ext->parent)->ref; |
| 1884 | case LYEXT_PAR_TYPE_ENUM: |
| 1885 | return ((struct lys_type_enum*)ext->parent)->ref; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1886 | case LYEXT_PAR_RESTR: |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1887 | return ((struct lys_restr*)ext->parent)->ref; |
| 1888 | case LYEXT_PAR_WHEN: |
| 1889 | return ((struct lys_when*)ext->parent)->ref; |
| 1890 | case LYEXT_PAR_IDENT: |
| 1891 | return ((struct lys_ident*)ext->parent)->ref; |
| 1892 | case LYEXT_PAR_DEVIATION: |
| 1893 | return ((struct lys_deviation*)ext->parent)->ref; |
| 1894 | case LYEXT_PAR_REVISION: |
| 1895 | return ((struct lys_revision*)ext->parent)->ref; |
| 1896 | case LYEXT_PAR_REFINE: |
| 1897 | return ((struct lys_refine*)ext->parent)->ref; |
| 1898 | default: |
| 1899 | break; |
| 1900 | } |
| 1901 | break; |
Radek Krejci | be33639 | 2017-02-07 10:54:24 +0100 | [diff] [blame] | 1902 | case LYEXT_SUBSTMT_REQINSTANCE: |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1903 | if (ext->parent_type == LYEXT_PAR_TYPE) { |
| 1904 | if (((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) { |
| 1905 | return &((struct lys_type*)ext->parent)->info.lref.req; |
| 1906 | } else if (((struct lys_type*)ext->parent)->base == LY_TYPE_INST) { |
| 1907 | return &((struct lys_type*)ext->parent)->info.inst.req; |
| 1908 | } |
| 1909 | } |
| 1910 | break; |
| 1911 | case LYEXT_SUBSTMT_REVISIONDATE: |
| 1912 | if (ext->parent_type == LYEXT_PAR_IMPORT) { |
| 1913 | return ((struct lys_import*)ext->parent)->rev; |
| 1914 | } else if (ext->parent_type == LYEXT_PAR_INCLUDE) { |
| 1915 | return ((struct lys_include*)ext->parent)->rev; |
| 1916 | } |
| 1917 | break; |
| 1918 | case LYEXT_SUBSTMT_STATUS: |
| 1919 | switch (ext->parent_type) { |
| 1920 | case LYEXT_PAR_NODE: |
| 1921 | case LYEXT_PAR_IDENT: |
| 1922 | case LYEXT_PAR_TPDF: |
| 1923 | case LYEXT_PAR_EXT: |
| 1924 | case LYEXT_PAR_FEATURE: |
| 1925 | case LYEXT_PAR_TYPE_ENUM: |
| 1926 | case LYEXT_PAR_TYPE_BIT: |
| 1927 | /* in all structures the flags member is at the same offset */ |
| 1928 | return &((struct lys_node*)ext->parent)->flags; |
| 1929 | default: |
| 1930 | break; |
| 1931 | } |
| 1932 | break; |
| 1933 | case LYEXT_SUBSTMT_UNIQUE: |
| 1934 | if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1935 | return &((struct lys_deviate*)ext->parent)->unique[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1936 | } 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] | 1937 | return &((struct lys_node_list*)ext->parent)->unique[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1938 | } |
| 1939 | break; |
| 1940 | case LYEXT_SUBSTMT_UNITS: |
| 1941 | if (ext->parent_type == LYEXT_PAR_NODE && |
| 1942 | (((struct lys_node*)ext->parent)->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 1943 | /* units is at the same offset in both lys_node_leaf and lys_node_leaflist */ |
| 1944 | return ((struct lys_node_leaf*)ext->parent)->units; |
| 1945 | } else if (ext->parent_type == LYEXT_PAR_TPDF) { |
| 1946 | return ((struct lys_tpdf*)ext->parent)->units; |
| 1947 | } else if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
| 1948 | return ((struct lys_deviate*)ext->parent)->units; |
| 1949 | } |
| 1950 | break; |
| 1951 | case LYEXT_SUBSTMT_VALUE: |
| 1952 | if (ext->parent_type == LYEXT_PAR_TYPE_ENUM) { |
| 1953 | return &((struct lys_type_enum*)ext->parent)->value; |
| 1954 | } |
| 1955 | break; |
| 1956 | case LYEXT_SUBSTMT_YINELEM: |
| 1957 | if (ext->parent_type == LYEXT_PAR_EXT) { |
| 1958 | return &((struct lys_ext*)ext->parent)->flags; |
| 1959 | } |
| 1960 | break; |
| 1961 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1962 | LOGINT(ext->module->ctx); |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1963 | return NULL; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1964 | } |
| 1965 | |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1966 | static int |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1967 | 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] | 1968 | int in_grp, int shallow, struct unres_schema *unres) |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1969 | { |
| 1970 | int i; |
| 1971 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1972 | new->base = old->base; |
| 1973 | new->der = old->der; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1974 | new->parent = (struct lys_tpdf *)parent; |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 1975 | new->ext_size = old->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1976 | if (lys_ext_dup(mod->ctx, 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] | 1977 | return -1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1978 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1979 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 1980 | i = unres_schema_find(unres, -1, old, UNRES_TYPE_DER); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1981 | if (i != -1) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 1982 | /* HACK (serious one) for unres */ |
| 1983 | /* nothing else we can do but duplicate it immediately */ |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1984 | if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1985 | new->der = (struct lys_tpdf *)lys_yang_type_dup(mod, parent, (struct yang_type *)old->der, new, in_grp, |
| 1986 | shallow, unres); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1987 | } else { |
| 1988 | new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1); |
| 1989 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1990 | /* all these unres additions can fail even though they did not before */ |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 1991 | 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] | 1992 | return -1; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 1993 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1994 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1995 | } |
| 1996 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1997 | 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] | 1998 | } |
| 1999 | |
| 2000 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2001 | lys_type_free(struct ly_ctx *ctx, struct lys_type *type, |
| 2002 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2003 | { |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 2004 | unsigned int i; |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2005 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2006 | assert(ctx); |
| 2007 | if (!type) { |
| 2008 | return; |
| 2009 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2010 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2011 | lys_extension_instances_free(ctx, type->ext, type->ext_size, private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2012 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2013 | switch (type->base) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 2014 | case LY_TYPE_BINARY: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2015 | lys_restr_free(ctx, type->info.binary.length, private_destructor); |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 2016 | free(type->info.binary.length); |
| 2017 | break; |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 2018 | case LY_TYPE_BITS: |
| 2019 | for (i = 0; i < type->info.bits.count; i++) { |
| 2020 | lydict_remove(ctx, type->info.bits.bit[i].name); |
| 2021 | lydict_remove(ctx, type->info.bits.bit[i].dsc); |
| 2022 | lydict_remove(ctx, type->info.bits.bit[i].ref); |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2023 | lys_iffeature_free(ctx, type->info.bits.bit[i].iffeature, type->info.bits.bit[i].iffeature_size, 0, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2024 | private_destructor); |
| 2025 | lys_extension_instances_free(ctx, type->info.bits.bit[i].ext, type->info.bits.bit[i].ext_size, |
| 2026 | private_destructor); |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 2027 | } |
| 2028 | free(type->info.bits.bit); |
| 2029 | break; |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 2030 | |
| 2031 | case LY_TYPE_DEC64: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2032 | lys_restr_free(ctx, type->info.dec64.range, private_destructor); |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 2033 | free(type->info.dec64.range); |
| 2034 | break; |
| 2035 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2036 | case LY_TYPE_ENUM: |
| 2037 | for (i = 0; i < type->info.enums.count; i++) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2038 | lydict_remove(ctx, type->info.enums.enm[i].name); |
| 2039 | lydict_remove(ctx, type->info.enums.enm[i].dsc); |
| 2040 | lydict_remove(ctx, type->info.enums.enm[i].ref); |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2041 | lys_iffeature_free(ctx, type->info.enums.enm[i].iffeature, type->info.enums.enm[i].iffeature_size, 0, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2042 | private_destructor); |
| 2043 | lys_extension_instances_free(ctx, type->info.enums.enm[i].ext, type->info.enums.enm[i].ext_size, |
| 2044 | private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2045 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2046 | free(type->info.enums.enm); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2047 | break; |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 2048 | |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 2049 | case LY_TYPE_INT8: |
| 2050 | case LY_TYPE_INT16: |
| 2051 | case LY_TYPE_INT32: |
| 2052 | case LY_TYPE_INT64: |
| 2053 | case LY_TYPE_UINT8: |
| 2054 | case LY_TYPE_UINT16: |
| 2055 | case LY_TYPE_UINT32: |
| 2056 | case LY_TYPE_UINT64: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2057 | lys_restr_free(ctx, type->info.num.range, private_destructor); |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 2058 | free(type->info.num.range); |
| 2059 | break; |
| 2060 | |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 2061 | case LY_TYPE_LEAFREF: |
| 2062 | lydict_remove(ctx, type->info.lref.path); |
| 2063 | break; |
| 2064 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 2065 | case LY_TYPE_STRING: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2066 | lys_restr_free(ctx, type->info.str.length, private_destructor); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 2067 | free(type->info.str.length); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 2068 | for (i = 0; i < type->info.str.pat_count; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2069 | lys_restr_free(ctx, &type->info.str.patterns[i], private_destructor); |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 2070 | #ifdef LY_ENABLED_CACHE |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 2071 | if (type->info.str.patterns_pcre) { |
| 2072 | pcre_free((pcre*)type->info.str.patterns_pcre[2 * i]); |
| 2073 | pcre_free_study((pcre_extra*)type->info.str.patterns_pcre[2 * i + 1]); |
| 2074 | } |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 2075 | #endif |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 2076 | } |
| 2077 | free(type->info.str.patterns); |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 2078 | #ifdef LY_ENABLED_CACHE |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 2079 | free(type->info.str.patterns_pcre); |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 2080 | #endif |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 2081 | break; |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 2082 | |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 2083 | case LY_TYPE_UNION: |
| 2084 | for (i = 0; i < type->info.uni.count; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2085 | lys_type_free(ctx, &type->info.uni.types[i], private_destructor); |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 2086 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2087 | free(type->info.uni.types); |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 2088 | break; |
| 2089 | |
Michal Vasko | d328219 | 2016-09-05 11:27:57 +0200 | [diff] [blame] | 2090 | case LY_TYPE_IDENT: |
| 2091 | free(type->info.ident.ref); |
| 2092 | break; |
| 2093 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2094 | default: |
Michal Vasko | d328219 | 2016-09-05 11:27:57 +0200 | [diff] [blame] | 2095 | /* 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] | 2096 | break; |
| 2097 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2098 | } |
| 2099 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2100 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2101 | lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf, |
| 2102 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2103 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2104 | assert(ctx); |
| 2105 | if (!tpdf) { |
| 2106 | return; |
| 2107 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2108 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2109 | lydict_remove(ctx, tpdf->name); |
| 2110 | lydict_remove(ctx, tpdf->dsc); |
| 2111 | lydict_remove(ctx, tpdf->ref); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2112 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2113 | lys_type_free(ctx, &tpdf->type, private_destructor); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2114 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2115 | lydict_remove(ctx, tpdf->units); |
| 2116 | lydict_remove(ctx, tpdf->dflt); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2117 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2118 | lys_extension_instances_free(ctx, tpdf->ext, tpdf->ext_size, private_destructor); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2119 | } |
| 2120 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2121 | static struct lys_when * |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2122 | 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] | 2123 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2124 | struct lys_when *new; |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2125 | |
| 2126 | if (!old) { |
| 2127 | return NULL; |
| 2128 | } |
| 2129 | |
| 2130 | new = calloc(1, sizeof *new); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2131 | LY_CHECK_ERR_RETURN(!new, LOGMEM(mod->ctx), NULL); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 2132 | new->cond = lydict_insert(mod->ctx, old->cond, 0); |
| 2133 | new->dsc = lydict_insert(mod->ctx, old->dsc, 0); |
| 2134 | new->ref = lydict_insert(mod->ctx, old->ref, 0); |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 2135 | new->ext_size = old->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 2136 | lys_ext_dup(mod->ctx, 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] | 2137 | |
| 2138 | return new; |
| 2139 | } |
| 2140 | |
Michal Vasko | 0308dd6 | 2015-10-07 09:14:40 +0200 | [diff] [blame] | 2141 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2142 | lys_when_free(struct ly_ctx *ctx, struct lys_when *w, |
| 2143 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2144 | { |
| 2145 | if (!w) { |
| 2146 | return; |
| 2147 | } |
| 2148 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2149 | lys_extension_instances_free(ctx, w->ext, w->ext_size, private_destructor); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2150 | lydict_remove(ctx, w->cond); |
| 2151 | lydict_remove(ctx, w->dsc); |
| 2152 | lydict_remove(ctx, w->ref); |
| 2153 | |
| 2154 | free(w); |
| 2155 | } |
| 2156 | |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2157 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2158 | lys_augment_free(struct ly_ctx *ctx, struct lys_node_augment *aug, |
| 2159 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2160 | { |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 2161 | struct lys_node *next, *sub; |
| 2162 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2163 | /* children from a resolved augment are freed under the target node */ |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 2164 | if (!aug->target || (aug->flags & LYS_NOTAPPLIED)) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2165 | LY_TREE_FOR_SAFE(aug->child, next, sub) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2166 | lys_node_free(sub, private_destructor, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2167 | } |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 2168 | } |
| 2169 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2170 | lydict_remove(ctx, aug->target_name); |
| 2171 | lydict_remove(ctx, aug->dsc); |
| 2172 | lydict_remove(ctx, aug->ref); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2173 | |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2174 | lys_iffeature_free(ctx, aug->iffeature, aug->iffeature_size, 0, private_destructor); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2175 | lys_extension_instances_free(ctx, aug->ext, aug->ext_size, private_destructor); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2176 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2177 | lys_when_free(ctx, aug->when, private_destructor); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +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_ident_free(struct ly_ctx *ctx, struct lys_ident *ident, |
| 2182 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2183 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2184 | assert(ctx); |
| 2185 | if (!ident) { |
| 2186 | return; |
| 2187 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2188 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 2189 | free(ident->base); |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 2190 | ly_set_free(ident->der); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2191 | lydict_remove(ctx, ident->name); |
| 2192 | lydict_remove(ctx, ident->dsc); |
| 2193 | lydict_remove(ctx, ident->ref); |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2194 | lys_iffeature_free(ctx, ident->iffeature, ident->iffeature_size, 0, private_destructor); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2195 | lys_extension_instances_free(ctx, ident->ext, ident->ext_size, private_destructor); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2196 | |
| 2197 | } |
| 2198 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2199 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2200 | lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp, |
| 2201 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2202 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2203 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2204 | |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2205 | /* handle only specific parts for LYS_GROUPING */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2206 | for (i = 0; i < grp->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2207 | lys_tpdf_free(ctx, &grp->tpdf[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2208 | } |
| 2209 | free(grp->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2210 | } |
| 2211 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2212 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2213 | lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io, |
| 2214 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2215 | { |
| 2216 | int i; |
| 2217 | |
| 2218 | /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */ |
| 2219 | for (i = 0; i < io->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2220 | lys_tpdf_free(ctx, &io->tpdf[i], private_destructor); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2221 | } |
| 2222 | free(io->tpdf); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2223 | |
| 2224 | for (i = 0; i < io->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2225 | lys_restr_free(ctx, &io->must[i], private_destructor); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2226 | } |
| 2227 | free(io->must); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2228 | } |
| 2229 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2230 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2231 | lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif, |
| 2232 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2233 | { |
| 2234 | int i; |
| 2235 | |
| 2236 | for (i = 0; i < notif->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2237 | lys_restr_free(ctx, ¬if->must[i], private_destructor); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2238 | } |
| 2239 | free(notif->must); |
| 2240 | |
| 2241 | for (i = 0; i < notif->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2242 | lys_tpdf_free(ctx, ¬if->tpdf[i], private_destructor); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2243 | } |
| 2244 | free(notif->tpdf); |
| 2245 | } |
| 2246 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2247 | lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml, |
| 2248 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2249 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2250 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2251 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2252 | for (i = 0; i < anyxml->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2253 | lys_restr_free(ctx, &anyxml->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2254 | } |
| 2255 | free(anyxml->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2256 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2257 | lys_when_free(ctx, anyxml->when, private_destructor); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2258 | } |
| 2259 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2260 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2261 | lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf, |
| 2262 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2263 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2264 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2265 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 2266 | /* leafref backlinks */ |
| 2267 | ly_set_free((struct ly_set *)leaf->backlinks); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2268 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2269 | for (i = 0; i < leaf->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2270 | lys_restr_free(ctx, &leaf->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2271 | } |
| 2272 | free(leaf->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2273 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2274 | lys_when_free(ctx, leaf->when, private_destructor); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2275 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2276 | lys_type_free(ctx, &leaf->type, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2277 | lydict_remove(ctx, leaf->units); |
| 2278 | lydict_remove(ctx, leaf->dflt); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2279 | } |
| 2280 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2281 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2282 | lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist, |
| 2283 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2284 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2285 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2286 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2287 | if (llist->backlinks) { |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2288 | /* leafref backlinks */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2289 | ly_set_free(llist->backlinks); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2290 | } |
| 2291 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2292 | for (i = 0; i < llist->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2293 | lys_restr_free(ctx, &llist->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2294 | } |
| 2295 | free(llist->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2296 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2297 | for (i = 0; i < llist->dflt_size; i++) { |
| 2298 | lydict_remove(ctx, llist->dflt[i]); |
| 2299 | } |
| 2300 | free(llist->dflt); |
| 2301 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2302 | lys_when_free(ctx, llist->when, private_destructor); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2303 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2304 | lys_type_free(ctx, &llist->type, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2305 | lydict_remove(ctx, llist->units); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2306 | } |
| 2307 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2308 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2309 | lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list, |
| 2310 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2311 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2312 | int i, j; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2313 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2314 | /* handle only specific parts for LY_NODE_LIST */ |
Michal Vasko | aaeab1d | 2018-02-16 09:36:46 +0100 | [diff] [blame] | 2315 | lys_when_free(ctx, list->when, private_destructor); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2316 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2317 | for (i = 0; i < list->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2318 | lys_restr_free(ctx, &list->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2319 | } |
| 2320 | free(list->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2321 | |
Michal Vasko | aaeab1d | 2018-02-16 09:36:46 +0100 | [diff] [blame] | 2322 | for (i = 0; i < list->tpdf_size; i++) { |
| 2323 | lys_tpdf_free(ctx, &list->tpdf[i], private_destructor); |
| 2324 | } |
| 2325 | free(list->tpdf); |
| 2326 | |
| 2327 | free(list->keys); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2328 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2329 | for (i = 0; i < list->unique_size; i++) { |
Michal Vasko | f5e940a | 2016-06-07 09:39:26 +0200 | [diff] [blame] | 2330 | for (j = 0; j < list->unique[i].expr_size; j++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2331 | lydict_remove(ctx, list->unique[i].expr[j]); |
| 2332 | } |
| 2333 | free(list->unique[i].expr); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2334 | } |
| 2335 | free(list->unique); |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 2336 | |
Michal Vasko | aaeab1d | 2018-02-16 09:36:46 +0100 | [diff] [blame] | 2337 | lydict_remove(ctx, list->keys_str); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2338 | } |
| 2339 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2340 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2341 | lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont, |
| 2342 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2343 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2344 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2345 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2346 | /* handle only specific parts for LY_NODE_CONTAINER */ |
| 2347 | lydict_remove(ctx, cont->presence); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2348 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2349 | for (i = 0; i < cont->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2350 | lys_tpdf_free(ctx, &cont->tpdf[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2351 | } |
| 2352 | free(cont->tpdf); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2353 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2354 | for (i = 0; i < cont->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2355 | lys_restr_free(ctx, &cont->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2356 | } |
| 2357 | free(cont->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2358 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2359 | lys_when_free(ctx, cont->when, private_destructor); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2360 | } |
| 2361 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2362 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2363 | lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f, |
| 2364 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2365 | { |
| 2366 | lydict_remove(ctx, f->name); |
| 2367 | lydict_remove(ctx, f->dsc); |
| 2368 | lydict_remove(ctx, f->ref); |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2369 | lys_iffeature_free(ctx, f->iffeature, f->iffeature_size, 0, private_destructor); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 2370 | ly_set_free(f->depfeatures); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2371 | lys_extension_instances_free(ctx, f->ext, f->ext_size, private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2375 | lys_extension_free(struct ly_ctx *ctx, struct lys_ext *e, |
| 2376 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2377 | { |
| 2378 | lydict_remove(ctx, e->name); |
| 2379 | lydict_remove(ctx, e->dsc); |
| 2380 | lydict_remove(ctx, e->ref); |
| 2381 | lydict_remove(ctx, e->argument); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2382 | lys_extension_instances_free(ctx, e->ext, e->ext_size, private_destructor); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2383 | } |
| 2384 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2385 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2386 | lys_deviation_free(struct lys_module *module, struct lys_deviation *dev, |
| 2387 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2388 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2389 | int i, j, k; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2390 | struct ly_ctx *ctx; |
| 2391 | struct lys_node *next, *elem; |
| 2392 | |
| 2393 | ctx = module->ctx; |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2394 | |
| 2395 | lydict_remove(ctx, dev->target_name); |
| 2396 | lydict_remove(ctx, dev->dsc); |
| 2397 | lydict_remove(ctx, dev->ref); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2398 | lys_extension_instances_free(ctx, dev->ext, dev->ext_size, private_destructor); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2399 | |
Pavol Vican | 64d0b76 | 2016-08-25 10:44:59 +0200 | [diff] [blame] | 2400 | if (!dev->deviate) { |
| 2401 | return ; |
| 2402 | } |
| 2403 | |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2404 | /* the module was freed, but we only need the context from orig_node, use ours */ |
| 2405 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 2406 | /* it's actually a node subtree, we need to update modules on all the nodes :-/ */ |
| 2407 | LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) { |
| 2408 | elem->module = module; |
| 2409 | |
| 2410 | LY_TREE_DFS_END(dev->orig_node, next, elem); |
| 2411 | } |
| 2412 | lys_node_free(dev->orig_node, NULL, 0); |
| 2413 | } else { |
| 2414 | /* it's just a shallow copy, freeing one node */ |
| 2415 | dev->orig_node->module = module; |
| 2416 | lys_node_free(dev->orig_node, NULL, 1); |
| 2417 | } |
| 2418 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2419 | for (i = 0; i < dev->deviate_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2420 | 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] | 2421 | |
Radek Krejci | d5a5c28 | 2016-08-15 15:38:08 +0200 | [diff] [blame] | 2422 | for (j = 0; j < dev->deviate[i].dflt_size; j++) { |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2423 | lydict_remove(ctx, dev->deviate[i].dflt[j]); |
Radek Krejci | d5a5c28 | 2016-08-15 15:38:08 +0200 | [diff] [blame] | 2424 | } |
| 2425 | free(dev->deviate[i].dflt); |
| 2426 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2427 | lydict_remove(ctx, dev->deviate[i].units); |
| 2428 | |
| 2429 | if (dev->deviate[i].mod == LY_DEVIATE_DEL) { |
| 2430 | for (j = 0; j < dev->deviate[i].must_size; j++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2431 | lys_restr_free(ctx, &dev->deviate[i].must[j], private_destructor); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2432 | } |
| 2433 | free(dev->deviate[i].must); |
| 2434 | |
| 2435 | for (j = 0; j < dev->deviate[i].unique_size; j++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2436 | for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) { |
| 2437 | lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]); |
| 2438 | } |
Michal Vasko | 0238ccf | 2016-03-07 15:02:18 +0100 | [diff] [blame] | 2439 | free(dev->deviate[i].unique[j].expr); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2440 | } |
| 2441 | free(dev->deviate[i].unique); |
| 2442 | } |
| 2443 | } |
| 2444 | free(dev->deviate); |
| 2445 | } |
| 2446 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2447 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2448 | lys_uses_free(struct ly_ctx *ctx, struct lys_node_uses *uses, |
| 2449 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2450 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2451 | int i, j; |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2452 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2453 | for (i = 0; i < uses->refine_size; i++) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2454 | lydict_remove(ctx, uses->refine[i].target_name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2455 | lydict_remove(ctx, uses->refine[i].dsc); |
| 2456 | lydict_remove(ctx, uses->refine[i].ref); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2457 | |
Radek Krejci | fde04bd | 2017-09-13 16:38:38 +0200 | [diff] [blame] | 2458 | lys_iffeature_free(ctx, uses->refine[i].iffeature, uses->refine[i].iffeature_size, 0, private_destructor); |
| 2459 | |
Michal Vasko | a275c0a | 2015-09-24 09:55:42 +0200 | [diff] [blame] | 2460 | for (j = 0; j < uses->refine[i].must_size; j++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2461 | lys_restr_free(ctx, &uses->refine[i].must[j], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2462 | } |
| 2463 | free(uses->refine[i].must); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2464 | |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 2465 | for (j = 0; j < uses->refine[i].dflt_size; j++) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 2466 | lydict_remove(ctx, uses->refine[i].dflt[j]); |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 2467 | } |
| 2468 | free(uses->refine[i].dflt); |
| 2469 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2470 | 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] | 2471 | |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 2472 | if (uses->refine[i].target_type & LYS_CONTAINER) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2473 | lydict_remove(ctx, uses->refine[i].mod.presence); |
| 2474 | } |
| 2475 | } |
| 2476 | free(uses->refine); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2477 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2478 | for (i = 0; i < uses->augment_size; i++) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2479 | lys_augment_free(ctx, &uses->augment[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2480 | } |
| 2481 | free(uses->augment); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2482 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2483 | lys_when_free(ctx, uses->when, private_destructor); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2484 | } |
| 2485 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2486 | void |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2487 | 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] | 2488 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2489 | struct ly_ctx *ctx; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2490 | struct lys_node *sub, *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2491 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2492 | if (!node) { |
| 2493 | return; |
| 2494 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2495 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2496 | assert(node->module); |
| 2497 | assert(node->module->ctx); |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2498 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2499 | ctx = node->module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2500 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2501 | /* remove private object */ |
Mislav Novakovic | b5529e5 | 2016-02-29 11:42:43 +0100 | [diff] [blame] | 2502 | if (node->priv && private_destructor) { |
| 2503 | private_destructor(node, node->priv); |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2504 | } |
| 2505 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2506 | /* common part */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2507 | lydict_remove(ctx, node->name); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2508 | if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2509 | lys_iffeature_free(ctx, node->iffeature, node->iffeature_size, shallow, private_destructor); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2510 | lydict_remove(ctx, node->dsc); |
| 2511 | lydict_remove(ctx, node->ref); |
| 2512 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2513 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2514 | if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2515 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2516 | lys_node_free(sub, private_destructor, 0); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2517 | } |
| 2518 | } |
| 2519 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2520 | lys_extension_instances_free(ctx, node->ext, node->ext_size, private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2521 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2522 | /* specific part */ |
| 2523 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2524 | case LYS_CONTAINER: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2525 | lys_container_free(ctx, (struct lys_node_container *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2526 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2527 | case LYS_CHOICE: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2528 | lys_when_free(ctx, ((struct lys_node_choice *)node)->when, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2529 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2530 | case LYS_LEAF: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2531 | lys_leaf_free(ctx, (struct lys_node_leaf *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2532 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2533 | case LYS_LEAFLIST: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2534 | lys_leaflist_free(ctx, (struct lys_node_leaflist *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2535 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2536 | case LYS_LIST: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2537 | lys_list_free(ctx, (struct lys_node_list *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2538 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2539 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2540 | case LYS_ANYDATA: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2541 | lys_anydata_free(ctx, (struct lys_node_anydata *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2542 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2543 | case LYS_USES: |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2544 | lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2545 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2546 | case LYS_CASE: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2547 | lys_when_free(ctx, ((struct lys_node_case *)node)->when, private_destructor); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2548 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2549 | case LYS_AUGMENT: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2550 | /* do nothing */ |
| 2551 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2552 | case LYS_GROUPING: |
| 2553 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2554 | case LYS_ACTION: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2555 | lys_grp_free(ctx, (struct lys_node_grp *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2556 | break; |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2557 | case LYS_NOTIF: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2558 | lys_notif_free(ctx, (struct lys_node_notif *)node, private_destructor); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2559 | break; |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2560 | case LYS_INPUT: |
| 2561 | case LYS_OUTPUT: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2562 | lys_inout_free(ctx, (struct lys_node_inout *)node, private_destructor); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2563 | break; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 2564 | case LYS_EXT: |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 2565 | case LYS_UNKNOWN: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2566 | LOGINT(ctx); |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 2567 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2568 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2569 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2570 | /* again common part */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2571 | lys_node_unlink(node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2572 | free(node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2573 | } |
| 2574 | |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 2575 | API struct lys_module * |
| 2576 | lys_implemented_module(const struct lys_module *mod) |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 2577 | { |
| 2578 | struct ly_ctx *ctx; |
| 2579 | int i; |
| 2580 | |
| 2581 | if (!mod || mod->implemented) { |
| 2582 | /* invalid argument or the module itself is implemented */ |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 2583 | return (struct lys_module *)mod; |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | ctx = mod->ctx; |
| 2587 | for (i = 0; i < ctx->models.used; i++) { |
| 2588 | if (!ctx->models.list[i]->implemented) { |
| 2589 | continue; |
| 2590 | } |
| 2591 | |
| 2592 | if (ly_strequal(mod->name, ctx->models.list[i]->name, 1)) { |
| 2593 | /* we have some revision of the module implemented */ |
| 2594 | return ctx->models.list[i]; |
| 2595 | } |
| 2596 | } |
| 2597 | |
| 2598 | /* we have no revision of the module implemented, return the module itself, |
| 2599 | * 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] | 2600 | return (struct lys_module *)mod; |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 2601 | } |
| 2602 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2603 | /* free_int_mods - flag whether to free the internal modules as well */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2604 | static void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2605 | 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] | 2606 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2607 | struct ly_ctx *ctx; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2608 | struct lys_node *next, *iter; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2609 | unsigned int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2610 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2611 | assert(module->ctx); |
| 2612 | ctx = module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2613 | |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2614 | /* just free the import array, imported modules will stay in the context */ |
Radek Krejci | 225376f | 2016-02-16 17:36:22 +0100 | [diff] [blame] | 2615 | for (i = 0; i < module->imp_size; i++) { |
Michal Vasko | a99c163 | 2016-06-06 16:23:52 +0200 | [diff] [blame] | 2616 | lydict_remove(ctx, module->imp[i].prefix); |
Michal Vasko | 8bfe381 | 2016-07-27 13:37:52 +0200 | [diff] [blame] | 2617 | lydict_remove(ctx, module->imp[i].dsc); |
| 2618 | lydict_remove(ctx, module->imp[i].ref); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2619 | 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] | 2620 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2621 | free(module->imp); |
| 2622 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2623 | /* submodules don't have data tree, the data nodes |
| 2624 | * are placed in the main module altogether */ |
| 2625 | if (!module->type) { |
| 2626 | LY_TREE_FOR_SAFE(module->data, next, iter) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2627 | lys_node_free(iter, private_destructor, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2628 | } |
Radek Krejci | 2118196 | 2015-06-30 14:11:00 +0200 | [diff] [blame] | 2629 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2630 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2631 | lydict_remove(ctx, module->dsc); |
| 2632 | lydict_remove(ctx, module->ref); |
| 2633 | lydict_remove(ctx, module->org); |
| 2634 | lydict_remove(ctx, module->contact); |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 2635 | lydict_remove(ctx, module->filepath); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2636 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2637 | /* revisions */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2638 | for (i = 0; i < module->rev_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2639 | 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] | 2640 | lydict_remove(ctx, module->rev[i].dsc); |
| 2641 | lydict_remove(ctx, module->rev[i].ref); |
| 2642 | } |
| 2643 | free(module->rev); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2644 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2645 | /* identities */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2646 | for (i = 0; i < module->ident_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2647 | lys_ident_free(ctx, &module->ident[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2648 | } |
| 2649 | module->ident_size = 0; |
| 2650 | free(module->ident); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2651 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2652 | /* typedefs */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2653 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2654 | lys_tpdf_free(ctx, &module->tpdf[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2655 | } |
| 2656 | free(module->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2657 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2658 | /* extension instances */ |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2659 | lys_extension_instances_free(ctx, module->ext, module->ext_size, private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2660 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2661 | /* augment */ |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2662 | for (i = 0; i < module->augment_size; i++) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2663 | lys_augment_free(ctx, &module->augment[i], private_destructor); |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2664 | } |
| 2665 | free(module->augment); |
| 2666 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2667 | /* features */ |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2668 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2669 | lys_feature_free(ctx, &module->features[i], private_destructor); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2670 | } |
| 2671 | free(module->features); |
| 2672 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2673 | /* deviations */ |
| 2674 | for (i = 0; i < module->deviation_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2675 | lys_deviation_free(module, &module->deviation[i], private_destructor); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2676 | } |
| 2677 | free(module->deviation); |
| 2678 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2679 | /* extensions */ |
| 2680 | for (i = 0; i < module->extensions_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2681 | lys_extension_free(ctx, &module->extensions[i], private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2682 | } |
| 2683 | free(module->extensions); |
| 2684 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2685 | lydict_remove(ctx, module->name); |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 2686 | lydict_remove(ctx, module->prefix); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2687 | } |
| 2688 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2689 | void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2690 | 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] | 2691 | { |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 2692 | int i; |
| 2693 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2694 | if (!submodule) { |
| 2695 | return; |
| 2696 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2697 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2698 | /* common part with struct ly_module */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2699 | module_free_common((struct lys_module *)submodule, private_destructor); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2700 | |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 2701 | /* include */ |
| 2702 | for (i = 0; i < submodule->inc_size; i++) { |
| 2703 | lydict_remove(submodule->ctx, submodule->inc[i].dsc); |
| 2704 | lydict_remove(submodule->ctx, submodule->inc[i].ref); |
| 2705 | lys_extension_instances_free(submodule->ctx, submodule->inc[i].ext, submodule->inc[i].ext_size, private_destructor); |
| 2706 | /* complete submodule free is done only from main module since |
| 2707 | * submodules propagate their includes to the main module */ |
| 2708 | } |
| 2709 | free(submodule->inc); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2710 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2711 | free(submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2712 | } |
| 2713 | |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 2714 | int |
| 2715 | lys_ingrouping(const struct lys_node *node) |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 2716 | { |
| 2717 | const struct lys_node *iter = node; |
| 2718 | assert(node); |
| 2719 | |
| 2720 | for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter)); |
| 2721 | if (!iter) { |
| 2722 | return 0; |
| 2723 | } else { |
| 2724 | return 1; |
| 2725 | } |
| 2726 | } |
| 2727 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2728 | /* |
| 2729 | * final: 0 - do not change config flags; 1 - inherit config flags from the parent; 2 - remove config flags |
| 2730 | */ |
| 2731 | static struct lys_node * |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 2732 | 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] | 2733 | struct unres_schema *unres, int shallow, int finalize) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2734 | { |
Radek Krejci | 7b9f566 | 2016-11-07 16:28:37 +0100 | [diff] [blame] | 2735 | struct lys_node *retval = NULL, *iter, *p; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2736 | struct ly_ctx *ctx = module->ctx; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2737 | int i, j, rc; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2738 | unsigned int size, size1, size2; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 2739 | struct unres_list_uniq *unique_info; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2740 | uint16_t flags; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2741 | |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2742 | struct lys_node_container *cont = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2743 | struct lys_node_container *cont_orig = (struct lys_node_container *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2744 | struct lys_node_choice *choice = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2745 | struct lys_node_choice *choice_orig = (struct lys_node_choice *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2746 | struct lys_node_leaf *leaf = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2747 | struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2748 | struct lys_node_leaflist *llist = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2749 | struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2750 | struct lys_node_list *list = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2751 | struct lys_node_list *list_orig = (struct lys_node_list *)node; |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2752 | struct lys_node_anydata *any = NULL; |
| 2753 | struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2754 | struct lys_node_uses *uses = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2755 | struct lys_node_uses *uses_orig = (struct lys_node_uses *)node; |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2756 | struct lys_node_rpc_action *rpc = NULL; |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2757 | struct lys_node_inout *io = NULL; |
Radek Krejci | c43151c | 2017-03-12 07:10:52 +0100 | [diff] [blame] | 2758 | struct lys_node_notif *ntf = NULL; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2759 | struct lys_node_case *cs = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2760 | struct lys_node_case *cs_orig = (struct lys_node_case *)node; |
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 | /* we cannot just duplicate memory since the strings are stored in |
| 2763 | * dictionary and we need to update dictionary counters. |
| 2764 | */ |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2765 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2766 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2767 | case LYS_CONTAINER: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2768 | cont = calloc(1, sizeof *cont); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2769 | retval = (struct lys_node *)cont; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2770 | break; |
| 2771 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2772 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2773 | choice = calloc(1, sizeof *choice); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2774 | retval = (struct lys_node *)choice; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2775 | break; |
| 2776 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2777 | case LYS_LEAF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2778 | leaf = calloc(1, sizeof *leaf); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2779 | retval = (struct lys_node *)leaf; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2780 | break; |
| 2781 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2782 | case LYS_LEAFLIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2783 | llist = calloc(1, sizeof *llist); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2784 | retval = (struct lys_node *)llist; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2785 | break; |
| 2786 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2787 | case LYS_LIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2788 | list = calloc(1, sizeof *list); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2789 | retval = (struct lys_node *)list; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2790 | break; |
| 2791 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2792 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2793 | case LYS_ANYDATA: |
| 2794 | any = calloc(1, sizeof *any); |
| 2795 | retval = (struct lys_node *)any; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2796 | break; |
| 2797 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2798 | case LYS_USES: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2799 | uses = calloc(1, sizeof *uses); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2800 | retval = (struct lys_node *)uses; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2801 | break; |
| 2802 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2803 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2804 | cs = calloc(1, sizeof *cs); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2805 | retval = (struct lys_node *)cs; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2806 | break; |
| 2807 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2808 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2809 | case LYS_ACTION: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2810 | rpc = calloc(1, sizeof *rpc); |
| 2811 | retval = (struct lys_node *)rpc; |
| 2812 | break; |
| 2813 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2814 | case LYS_INPUT: |
| 2815 | case LYS_OUTPUT: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2816 | io = calloc(1, sizeof *io); |
| 2817 | retval = (struct lys_node *)io; |
| 2818 | break; |
| 2819 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2820 | case LYS_NOTIF: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2821 | ntf = calloc(1, sizeof *ntf); |
| 2822 | retval = (struct lys_node *)ntf; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2823 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2824 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2825 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2826 | LOGINT(ctx); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2827 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2828 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2829 | LY_CHECK_ERR_RETURN(!retval, LOGMEM(ctx), NULL); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2830 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2831 | /* |
| 2832 | * duplicate generic part of the structure |
| 2833 | */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2834 | retval->name = lydict_insert(ctx, node->name, 0); |
| 2835 | retval->dsc = lydict_insert(ctx, node->dsc, 0); |
| 2836 | retval->ref = lydict_insert(ctx, node->ref, 0); |
| 2837 | retval->flags = node->flags; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2838 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2839 | retval->module = module; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2840 | retval->nodetype = node->nodetype; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2841 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2842 | retval->prev = retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2843 | |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 2844 | retval->ext_size = node->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 2845 | if (lys_ext_dup(ctx, 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] | 2846 | goto error; |
| 2847 | } |
| 2848 | |
Radek Krejci | 0621404 | 2016-11-04 16:25:58 +0100 | [diff] [blame] | 2849 | if (node->iffeature_size) { |
| 2850 | retval->iffeature_size = node->iffeature_size; |
| 2851 | retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2852 | LY_CHECK_ERR_GOTO(!retval->iffeature, LOGMEM(ctx), error); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2853 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2854 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2855 | if (!shallow) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 2856 | for (i = 0; i < node->iffeature_size; ++i) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2857 | resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2); |
| 2858 | if (size1) { |
| 2859 | /* there is something to duplicate */ |
| 2860 | |
| 2861 | /* duplicate compiled expression */ |
| 2862 | size = (size1 / 4) + (size1 % 4) ? 1 : 0; |
| 2863 | retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2864 | LY_CHECK_ERR_GOTO(!retval->iffeature[i].expr, LOGMEM(ctx), error); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2865 | memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr); |
| 2866 | |
| 2867 | /* 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] | 2868 | retval->iffeature[i].features = calloc(size2, sizeof *retval->iffeature[i].features); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2869 | LY_CHECK_ERR_GOTO(!retval->iffeature[i].features, LOGMEM(ctx); free(retval->iffeature[i].expr), error); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 2870 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2871 | for (j = 0; (unsigned int)j < size2; j++) { |
| 2872 | rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT, |
| 2873 | &retval->iffeature[i].features[j]); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 2874 | if (rc == EXIT_FAILURE) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2875 | /* feature is resolved in origin, so copy it |
| 2876 | * - duplication is used for instantiating groupings |
| 2877 | * and if-feature inside grouping is supposed to be |
| 2878 | * resolved inside the original grouping, so we want |
| 2879 | * to keep pointers to features from the grouping |
| 2880 | * context */ |
| 2881 | retval->iffeature[i].features[j] = node->iffeature[i].features[j]; |
| 2882 | } else if (rc == -1) { |
| 2883 | goto error; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 2884 | } /* else unres was duplicated */ |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2885 | } |
| 2886 | } |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 2887 | |
| 2888 | /* duplicate if-feature's extensions */ |
| 2889 | retval->iffeature[i].ext_size = node->iffeature[i].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 2890 | if (lys_ext_dup(ctx, module, node->iffeature[i].ext, node->iffeature[i].ext_size, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2891 | &retval->iffeature[i], LYEXT_PAR_IFFEATURE, &retval->iffeature[i].ext, shallow, unres)) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 2892 | goto error; |
| 2893 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2894 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2895 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2896 | /* inherit config flags */ |
Radek Krejci | 7b9f566 | 2016-11-07 16:28:37 +0100 | [diff] [blame] | 2897 | p = parent; |
| 2898 | do { |
| 2899 | for (iter = p; iter && (iter->nodetype == LYS_USES); iter = iter->parent); |
| 2900 | } while (iter && iter->nodetype == LYS_AUGMENT && (p = lys_parent(iter))); |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2901 | if (iter) { |
| 2902 | flags = iter->flags & LYS_CONFIG_MASK; |
| 2903 | } else { |
| 2904 | /* default */ |
| 2905 | flags = LYS_CONFIG_W; |
| 2906 | } |
| 2907 | |
| 2908 | switch (finalize) { |
| 2909 | case 1: |
| 2910 | /* inherit config flags */ |
| 2911 | if (retval->flags & LYS_CONFIG_SET) { |
| 2912 | /* skip nodes with an explicit config value */ |
| 2913 | if ((flags & LYS_CONFIG_R) && (retval->flags & LYS_CONFIG_W)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2914 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, "true", "config"); |
| 2915 | LOGVAL(ctx, 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] | 2916 | goto error; |
| 2917 | } |
| 2918 | break; |
| 2919 | } |
| 2920 | |
| 2921 | if (retval->nodetype != LYS_USES) { |
| 2922 | retval->flags = (retval->flags & ~LYS_CONFIG_MASK) | flags; |
| 2923 | } |
Radek Krejci | 2cc2532 | 2017-09-06 16:32:02 +0200 | [diff] [blame] | 2924 | |
| 2925 | /* inherit status */ |
Radek Krejci | e2807ea | 2017-09-07 10:52:21 +0200 | [diff] [blame] | 2926 | if ((parent->flags & LYS_STATUS_MASK) > (retval->flags & LYS_STATUS_MASK)) { |
| 2927 | /* but do it only in case the parent has a stonger status */ |
| 2928 | retval->flags &= ~LYS_STATUS_MASK; |
| 2929 | retval->flags |= (parent->flags & LYS_STATUS_MASK); |
| 2930 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2931 | break; |
| 2932 | case 2: |
| 2933 | /* erase config flags */ |
| 2934 | retval->flags &= ~LYS_CONFIG_MASK; |
| 2935 | retval->flags &= ~LYS_CONFIG_SET; |
| 2936 | break; |
| 2937 | } |
| 2938 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2939 | /* connect it to the parent */ |
| 2940 | if (lys_node_addchild(parent, retval->module, retval)) { |
| 2941 | goto error; |
| 2942 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2943 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2944 | /* go recursively */ |
| 2945 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2946 | LY_TREE_FOR(node->child, iter) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 2947 | if (iter->nodetype & LYS_GROUPING) { |
| 2948 | /* do not instantiate groupings */ |
| 2949 | continue; |
| 2950 | } |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 2951 | if (!lys_node_dup_recursion(module, retval, iter, unres, 0, finalize)) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2952 | goto error; |
| 2953 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2954 | } |
| 2955 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2956 | |
| 2957 | if (finalize == 1) { |
| 2958 | /* check that configuration lists have keys |
| 2959 | * - we really want to check keys_size in original node, because the keys are |
| 2960 | * not yet resolved here, it is done below in nodetype specific part */ |
| 2961 | if ((retval->nodetype == LYS_LIST) && (retval->flags & LYS_CONFIG_W) |
| 2962 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2963 | LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list"); |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2964 | goto error; |
| 2965 | } |
| 2966 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2967 | } else { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 2968 | memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2969 | } |
| 2970 | |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2971 | /* |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2972 | * duplicate specific part of the structure |
| 2973 | */ |
| 2974 | switch (node->nodetype) { |
| 2975 | case LYS_CONTAINER: |
| 2976 | if (cont_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2977 | cont->when = lys_when_dup(module, cont_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 2978 | LY_CHECK_GOTO(!cont->when, error); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2979 | } |
| 2980 | cont->presence = lydict_insert(ctx, cont_orig->presence, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2981 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 2982 | if (cont_orig->must) { |
| 2983 | cont->must = lys_restr_dup(module, cont_orig->must, cont_orig->must_size, shallow, unres); |
| 2984 | LY_CHECK_GOTO(!cont->must, error); |
| 2985 | cont->must_size = cont_orig->must_size; |
| 2986 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2987 | |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 2988 | /* typedefs are not needed in instantiated grouping, nor the deviation's shallow copy */ |
| 2989 | |
| 2990 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2991 | case LYS_CHOICE: |
| 2992 | if (choice_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2993 | choice->when = lys_when_dup(module, choice_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 2994 | LY_CHECK_GOTO(!choice->when, error); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2995 | } |
| 2996 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2997 | if (!shallow) { |
| 2998 | if (choice_orig->dflt) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2999 | rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0, |
| 3000 | LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST, |
| 3001 | (const struct lys_node **)&choice->dflt); |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3002 | if (rc) { |
| 3003 | if (rc == EXIT_FAILURE) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3004 | LOGINT(ctx); |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3005 | } |
| 3006 | goto error; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3007 | } |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3008 | } else { |
| 3009 | /* useless to check return value, we don't know whether |
| 3010 | * there really wasn't any default defined or it just hasn't |
| 3011 | * been resolved, we just hope for the best :) |
| 3012 | */ |
| 3013 | unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3014 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3015 | } else { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3016 | choice->dflt = choice_orig->dflt; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3017 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3018 | break; |
| 3019 | |
| 3020 | case LYS_LEAF: |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 3021 | if (lys_type_dup(module, retval, &(leaf->type), &(leaf_orig->type), lys_ingrouping(retval), shallow, unres)) { |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 3022 | goto error; |
| 3023 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3024 | leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0); |
| 3025 | |
| 3026 | if (leaf_orig->dflt) { |
| 3027 | leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3028 | } |
| 3029 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3030 | if (leaf_orig->must) { |
| 3031 | leaf->must = lys_restr_dup(module, leaf_orig->must, leaf_orig->must_size, shallow, unres); |
| 3032 | LY_CHECK_GOTO(!leaf->must, error); |
| 3033 | leaf->must_size = leaf_orig->must_size; |
| 3034 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3035 | |
| 3036 | if (leaf_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3037 | leaf->when = lys_when_dup(module, leaf_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3038 | LY_CHECK_GOTO(!leaf->when, error); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3039 | } |
| 3040 | break; |
| 3041 | |
| 3042 | case LYS_LEAFLIST: |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 3043 | if (lys_type_dup(module, retval, &(llist->type), &(llist_orig->type), lys_ingrouping(retval), shallow, unres)) { |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 3044 | goto error; |
| 3045 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3046 | llist->units = lydict_insert(module->ctx, llist_orig->units, 0); |
| 3047 | |
| 3048 | llist->min = llist_orig->min; |
| 3049 | llist->max = llist_orig->max; |
| 3050 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3051 | if (llist_orig->must) { |
| 3052 | llist->must = lys_restr_dup(module, llist_orig->must, llist_orig->must_size, shallow, unres); |
| 3053 | LY_CHECK_GOTO(!llist->must, error); |
| 3054 | llist->must_size = llist_orig->must_size; |
| 3055 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3056 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3057 | if (llist_orig->dflt) { |
| 3058 | llist->dflt = malloc(llist_orig->dflt_size * sizeof *llist->dflt); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3059 | LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), error); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3060 | llist->dflt_size = llist_orig->dflt_size; |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3061 | |
Radek Krejci | c699e42 | 2017-06-02 15:18:58 +0200 | [diff] [blame] | 3062 | for (i = 0; i < llist->dflt_size; i++) { |
| 3063 | llist->dflt[i] = lydict_insert(ctx, llist_orig->dflt[i], 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3064 | } |
| 3065 | } |
| 3066 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3067 | if (llist_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3068 | llist->when = lys_when_dup(module, llist_orig->when, shallow, unres); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3069 | } |
| 3070 | break; |
| 3071 | |
| 3072 | case LYS_LIST: |
| 3073 | list->min = list_orig->min; |
| 3074 | list->max = list_orig->max; |
| 3075 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3076 | if (list_orig->must) { |
| 3077 | list->must = lys_restr_dup(module, list_orig->must, list_orig->must_size, shallow, unres); |
| 3078 | LY_CHECK_GOTO(!list->must, error); |
| 3079 | list->must_size = list_orig->must_size; |
| 3080 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3081 | |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3082 | /* 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] | 3083 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3084 | if (list_orig->keys_size) { |
| 3085 | list->keys = calloc(list_orig->keys_size, sizeof *list->keys); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3086 | LY_CHECK_ERR_GOTO(!list->keys, LOGMEM(ctx), error); |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3087 | list->keys_str = lydict_insert(ctx, list_orig->keys_str, 0); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3088 | list->keys_size = list_orig->keys_size; |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3089 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3090 | if (!shallow) { |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3091 | /* the keys are going to be resolved only if the list is instantiated in data tree, not just |
| 3092 | * in another grouping */ |
| 3093 | for (iter = parent; iter && iter->nodetype != LYS_GROUPING; iter = iter->parent); |
| 3094 | if (!iter && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) { |
| 3095 | goto error; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3096 | } |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3097 | } else { |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3098 | memcpy(list->keys, list_orig->keys, list_orig->keys_size * sizeof *list->keys); |
Radek Krejci | a01e543 | 2015-06-16 10:35:25 +0200 | [diff] [blame] | 3099 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3100 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3101 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3102 | if (list_orig->unique) { |
| 3103 | list->unique = malloc(list_orig->unique_size * sizeof *list->unique); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3104 | LY_CHECK_ERR_GOTO(!list->unique, LOGMEM(ctx), error); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3105 | list->unique_size = list_orig->unique_size; |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3106 | |
Radek Krejci | c699e42 | 2017-06-02 15:18:58 +0200 | [diff] [blame] | 3107 | for (i = 0; i < list->unique_size; ++i) { |
| 3108 | list->unique[i].expr = malloc(list_orig->unique[i].expr_size * sizeof *list->unique[i].expr); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3109 | LY_CHECK_ERR_GOTO(!list->unique[i].expr, LOGMEM(ctx), error); |
Radek Krejci | c699e42 | 2017-06-02 15:18:58 +0200 | [diff] [blame] | 3110 | list->unique[i].expr_size = list_orig->unique[i].expr_size; |
| 3111 | for (j = 0; j < list->unique[i].expr_size; j++) { |
| 3112 | list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0); |
| 3113 | |
| 3114 | /* if it stays in unres list, duplicate it also there */ |
| 3115 | unique_info = malloc(sizeof *unique_info); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3116 | LY_CHECK_ERR_GOTO(!unique_info, LOGMEM(ctx), error); |
Radek Krejci | c699e42 | 2017-06-02 15:18:58 +0200 | [diff] [blame] | 3117 | unique_info->list = (struct lys_node *)list; |
| 3118 | unique_info->expr = list->unique[i].expr[j]; |
| 3119 | unique_info->trg_type = &list->unique[i].trg_type; |
| 3120 | unres_schema_dup(module, unres, &list_orig, UNRES_LIST_UNIQ, unique_info); |
| 3121 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3122 | } |
| 3123 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 3124 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3125 | if (list_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3126 | list->when = lys_when_dup(module, list_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3127 | LY_CHECK_GOTO(!list->when, error); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 3128 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 3129 | break; |
| 3130 | |
| 3131 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3132 | case LYS_ANYDATA: |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3133 | if (any_orig->must) { |
| 3134 | any->must = lys_restr_dup(module, any_orig->must, any_orig->must_size, shallow, unres); |
| 3135 | LY_CHECK_GOTO(!any->must, error); |
| 3136 | any->must_size = any_orig->must_size; |
| 3137 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3138 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3139 | if (any_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3140 | any->when = lys_when_dup(module, any_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3141 | LY_CHECK_GOTO(!any->when, error); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3142 | } |
| 3143 | break; |
| 3144 | |
| 3145 | case LYS_USES: |
| 3146 | uses->grp = uses_orig->grp; |
| 3147 | |
| 3148 | if (uses_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3149 | uses->when = lys_when_dup(module, uses_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3150 | LY_CHECK_GOTO(!uses->when, error); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3151 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 3152 | /* 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] | 3153 | break; |
| 3154 | |
| 3155 | case LYS_CASE: |
| 3156 | if (cs_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3157 | cs->when = lys_when_dup(module, cs_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3158 | LY_CHECK_GOTO(!cs->when, error); |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 3159 | } |
| 3160 | break; |
| 3161 | |
Radek Krejci | 9693540 | 2016-11-04 16:27:28 +0100 | [diff] [blame] | 3162 | case LYS_ACTION: |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 3163 | case LYS_RPC: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3164 | case LYS_INPUT: |
| 3165 | case LYS_OUTPUT: |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3166 | case LYS_NOTIF: |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3167 | /* 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] | 3168 | break; |
| 3169 | |
| 3170 | default: |
| 3171 | /* LY_NODE_AUGMENT */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3172 | LOGINT(ctx); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3173 | goto error; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3174 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3175 | |
| 3176 | return retval; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3177 | |
| 3178 | error: |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3179 | lys_node_free(retval, NULL, 0); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3180 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3181 | } |
| 3182 | |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 3183 | int |
| 3184 | lys_has_xpath(const struct lys_node *node) |
| 3185 | { |
| 3186 | assert(node); |
| 3187 | |
| 3188 | switch (node->nodetype) { |
| 3189 | case LYS_AUGMENT: |
| 3190 | if (((struct lys_node_augment *)node)->when) { |
| 3191 | return 1; |
| 3192 | } |
| 3193 | break; |
| 3194 | case LYS_CASE: |
| 3195 | if (((struct lys_node_case *)node)->when) { |
| 3196 | return 1; |
| 3197 | } |
| 3198 | break; |
| 3199 | case LYS_CHOICE: |
| 3200 | if (((struct lys_node_choice *)node)->when) { |
| 3201 | return 1; |
| 3202 | } |
| 3203 | break; |
| 3204 | case LYS_ANYDATA: |
| 3205 | if (((struct lys_node_anydata *)node)->when || ((struct lys_node_anydata *)node)->must_size) { |
| 3206 | return 1; |
| 3207 | } |
| 3208 | break; |
| 3209 | case LYS_LEAF: |
| 3210 | if (((struct lys_node_leaf *)node)->when || ((struct lys_node_leaf *)node)->must_size) { |
| 3211 | return 1; |
| 3212 | } |
| 3213 | break; |
| 3214 | case LYS_LEAFLIST: |
| 3215 | if (((struct lys_node_leaflist *)node)->when || ((struct lys_node_leaflist *)node)->must_size) { |
| 3216 | return 1; |
| 3217 | } |
| 3218 | break; |
| 3219 | case LYS_LIST: |
| 3220 | if (((struct lys_node_list *)node)->when || ((struct lys_node_list *)node)->must_size) { |
| 3221 | return 1; |
| 3222 | } |
| 3223 | break; |
| 3224 | case LYS_CONTAINER: |
| 3225 | if (((struct lys_node_container *)node)->when || ((struct lys_node_container *)node)->must_size) { |
| 3226 | return 1; |
| 3227 | } |
| 3228 | break; |
| 3229 | case LYS_INPUT: |
| 3230 | case LYS_OUTPUT: |
| 3231 | if (((struct lys_node_inout *)node)->must_size) { |
| 3232 | return 1; |
| 3233 | } |
| 3234 | break; |
| 3235 | case LYS_NOTIF: |
| 3236 | if (((struct lys_node_notif *)node)->must_size) { |
| 3237 | return 1; |
| 3238 | } |
| 3239 | break; |
| 3240 | case LYS_USES: |
| 3241 | if (((struct lys_node_uses *)node)->when) { |
| 3242 | return 1; |
| 3243 | } |
| 3244 | break; |
| 3245 | default: |
| 3246 | /* does not have XPath */ |
| 3247 | break; |
| 3248 | } |
| 3249 | |
| 3250 | return 0; |
| 3251 | } |
| 3252 | |
Michal Vasko | 568b195 | 2018-01-30 15:53:30 +0100 | [diff] [blame] | 3253 | int |
| 3254 | lys_type_is_local(const struct lys_type *type) |
| 3255 | { |
| 3256 | if (!type->der->module) { |
| 3257 | /* build-in type */ |
| 3258 | return 1; |
| 3259 | } |
| 3260 | /* type->parent can be either a typedef or leaf/leaf-list, but module pointers are compatible */ |
| 3261 | return (lys_main_module(type->der->module) == lys_main_module(((struct lys_tpdf *)type->parent)->module)); |
| 3262 | } |
| 3263 | |
Radek Krejci | 2cc2532 | 2017-09-06 16:32:02 +0200 | [diff] [blame] | 3264 | /* |
| 3265 | * shallow - |
| 3266 | * - do not inherit status from the parent |
| 3267 | */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3268 | struct lys_node * |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 3269 | 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] | 3270 | struct unres_schema *unres, int shallow) |
| 3271 | { |
| 3272 | struct lys_node *p = NULL; |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 3273 | int finalize = 0; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3274 | struct lys_node *result, *iter, *next; |
| 3275 | |
| 3276 | if (!shallow) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3277 | /* 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] | 3278 | for (p = parent; |
| 3279 | p && !(p->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC | LYS_ACTION | LYS_GROUPING)); |
| 3280 | p = lys_parent(p)); |
| 3281 | finalize = p ? ((p->nodetype == LYS_GROUPING) ? 0 : 2) : 1; |
| 3282 | } |
| 3283 | |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 3284 | result = lys_node_dup_recursion(module, parent, node, unres, shallow, finalize); |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3285 | if (finalize) { |
| 3286 | /* check xpath expressions in the instantiated tree */ |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3287 | for (iter = next = result; iter; iter = next) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 3288 | 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] | 3289 | /* invalid xpath */ |
| 3290 | return NULL; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3291 | } |
| 3292 | |
| 3293 | /* select next item */ |
| 3294 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA | LYS_GROUPING)) { |
| 3295 | /* child exception for leafs, leaflists and anyxml without children, ignore groupings */ |
| 3296 | next = NULL; |
| 3297 | } else { |
| 3298 | next = iter->child; |
| 3299 | } |
| 3300 | if (!next) { |
| 3301 | /* no children, try siblings */ |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3302 | if (iter == result) { |
| 3303 | /* we are done, no next element to process */ |
| 3304 | break; |
| 3305 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3306 | next = iter->next; |
| 3307 | } |
| 3308 | while (!next) { |
| 3309 | /* parent is already processed, go to its sibling */ |
| 3310 | iter = lys_parent(iter); |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3311 | if (lys_parent(iter) == lys_parent(result)) { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3312 | /* we are done, no next element to process */ |
| 3313 | break; |
| 3314 | } |
| 3315 | next = iter->next; |
| 3316 | } |
| 3317 | } |
| 3318 | } |
| 3319 | |
| 3320 | return result; |
| 3321 | } |
| 3322 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 3323 | void |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3324 | lys_node_switch(struct lys_node *dst, struct lys_node *src) |
| 3325 | { |
| 3326 | struct lys_node *child; |
| 3327 | |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 3328 | 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] | 3329 | |
| 3330 | /* sibling next */ |
Michal Vasko | 8328da8 | 2016-08-25 09:27:22 +0200 | [diff] [blame] | 3331 | if (dst->prev->next) { |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3332 | dst->prev->next = src; |
| 3333 | } |
| 3334 | |
| 3335 | /* sibling prev */ |
| 3336 | if (dst->next) { |
| 3337 | dst->next->prev = src; |
Michal Vasko | 8328da8 | 2016-08-25 09:27:22 +0200 | [diff] [blame] | 3338 | } else { |
| 3339 | for (child = dst->prev; child->prev->next; child = child->prev); |
| 3340 | child->prev = src; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3341 | } |
| 3342 | |
| 3343 | /* next */ |
| 3344 | src->next = dst->next; |
| 3345 | dst->next = NULL; |
| 3346 | |
| 3347 | /* prev */ |
| 3348 | if (dst->prev != dst) { |
| 3349 | src->prev = dst->prev; |
| 3350 | } |
| 3351 | dst->prev = dst; |
| 3352 | |
| 3353 | /* parent child */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 3354 | if (dst->parent) { |
| 3355 | if (dst->parent->child == dst) { |
| 3356 | dst->parent->child = src; |
| 3357 | } |
Radek Krejci | 115fa88 | 2017-03-01 16:15:07 +0100 | [diff] [blame] | 3358 | } else if (lys_main_module(dst->module)->data == dst) { |
| 3359 | lys_main_module(dst->module)->data = src; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3360 | } |
| 3361 | |
| 3362 | /* parent */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 3363 | src->parent = dst->parent; dst->parent = NULL; |
| 3364 | |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3365 | |
| 3366 | /* child parent */ |
| 3367 | LY_TREE_FOR(dst->child, child) { |
| 3368 | if (child->parent == dst) { |
| 3369 | child->parent = src; |
| 3370 | } |
| 3371 | } |
| 3372 | |
| 3373 | /* child */ |
| 3374 | src->child = dst->child; |
| 3375 | dst->child = NULL; |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3376 | |
| 3377 | /* node-specific data */ |
| 3378 | switch (dst->nodetype) { |
| 3379 | case LYS_CONTAINER: |
| 3380 | ((struct lys_node_container *)src)->tpdf_size = ((struct lys_node_container *)dst)->tpdf_size; |
| 3381 | ((struct lys_node_container *)src)->tpdf = ((struct lys_node_container *)dst)->tpdf; |
| 3382 | ((struct lys_node_container *)dst)->tpdf_size = 0; |
| 3383 | ((struct lys_node_container *)dst)->tpdf = NULL; |
| 3384 | break; |
| 3385 | case LYS_LIST: |
| 3386 | ((struct lys_node_list *)src)->tpdf_size = ((struct lys_node_list *)dst)->tpdf_size; |
| 3387 | ((struct lys_node_list *)src)->tpdf = ((struct lys_node_list *)dst)->tpdf; |
| 3388 | ((struct lys_node_list *)dst)->tpdf_size = 0; |
| 3389 | ((struct lys_node_list *)dst)->tpdf = NULL; |
| 3390 | break; |
| 3391 | case LYS_RPC: |
| 3392 | case LYS_ACTION: |
| 3393 | ((struct lys_node_rpc_action *)src)->tpdf_size = ((struct lys_node_rpc_action *)dst)->tpdf_size; |
| 3394 | ((struct lys_node_rpc_action *)src)->tpdf = ((struct lys_node_rpc_action *)dst)->tpdf; |
| 3395 | ((struct lys_node_rpc_action *)dst)->tpdf_size = 0; |
| 3396 | ((struct lys_node_rpc_action *)dst)->tpdf = NULL; |
| 3397 | break; |
| 3398 | case LYS_NOTIF: |
| 3399 | ((struct lys_node_notif *)src)->tpdf_size = ((struct lys_node_notif *)dst)->tpdf_size; |
| 3400 | ((struct lys_node_notif *)src)->tpdf = ((struct lys_node_notif *)dst)->tpdf; |
| 3401 | ((struct lys_node_notif *)dst)->tpdf_size = 0; |
| 3402 | ((struct lys_node_notif *)dst)->tpdf = NULL; |
| 3403 | break; |
| 3404 | case LYS_INPUT: |
| 3405 | case LYS_OUTPUT: |
| 3406 | ((struct lys_node_inout *)src)->tpdf_size = ((struct lys_node_inout *)dst)->tpdf_size; |
| 3407 | ((struct lys_node_inout *)src)->tpdf = ((struct lys_node_inout *)dst)->tpdf; |
| 3408 | ((struct lys_node_inout *)dst)->tpdf_size = 0; |
| 3409 | ((struct lys_node_inout *)dst)->tpdf = NULL; |
| 3410 | break; |
| 3411 | default: |
| 3412 | /* nothing special */ |
| 3413 | break; |
| 3414 | } |
| 3415 | |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3416 | } |
| 3417 | |
| 3418 | void |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 3419 | lys_free(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv), int free_subs, int remove_from_ctx) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3420 | { |
| 3421 | struct ly_ctx *ctx; |
| 3422 | int i; |
| 3423 | |
| 3424 | if (!module) { |
| 3425 | return; |
| 3426 | } |
| 3427 | |
| 3428 | /* remove schema from the context */ |
| 3429 | ctx = module->ctx; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 3430 | if (remove_from_ctx && ctx->models.used) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3431 | for (i = 0; i < ctx->models.used; i++) { |
| 3432 | if (ctx->models.list[i] == module) { |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 3433 | /* move all the models to not change the order in the list */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3434 | ctx->models.used--; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 3435 | 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] | 3436 | ctx->models.list[ctx->models.used] = NULL; |
| 3437 | /* we are done */ |
| 3438 | break; |
| 3439 | } |
| 3440 | } |
| 3441 | } |
| 3442 | |
| 3443 | /* common part with struct ly_submodule */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 3444 | module_free_common(module, private_destructor); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3445 | |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 3446 | /* include */ |
| 3447 | for (i = 0; i < module->inc_size; i++) { |
| 3448 | lydict_remove(ctx, module->inc[i].dsc); |
| 3449 | lydict_remove(ctx, module->inc[i].ref); |
| 3450 | lys_extension_instances_free(ctx, module->inc[i].ext, module->inc[i].ext_size, private_destructor); |
| 3451 | /* complete submodule free is done only from main module since |
| 3452 | * submodules propagate their includes to the main module */ |
| 3453 | if (free_subs) { |
| 3454 | lys_submodule_free(module->inc[i].submodule, private_destructor); |
| 3455 | } |
| 3456 | } |
| 3457 | free(module->inc); |
| 3458 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3459 | /* specific items to free */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 3460 | lydict_remove(ctx, module->ns); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3461 | |
| 3462 | free(module); |
| 3463 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3464 | |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 3465 | static void |
| 3466 | lys_features_disable_recursive(struct lys_feature *f) |
| 3467 | { |
| 3468 | unsigned int i; |
| 3469 | struct lys_feature *depf; |
| 3470 | |
| 3471 | /* disable the feature */ |
| 3472 | f->flags &= ~LYS_FENABLED; |
| 3473 | |
| 3474 | /* by disabling feature we have to disable also all features that depends on this feature */ |
| 3475 | if (f->depfeatures) { |
| 3476 | for (i = 0; i < f->depfeatures->number; i++) { |
| 3477 | depf = (struct lys_feature *)f->depfeatures->set.g[i]; |
| 3478 | if (depf->flags & LYS_FENABLED) { |
| 3479 | lys_features_disable_recursive(depf); |
| 3480 | } |
| 3481 | } |
| 3482 | } |
| 3483 | } |
| 3484 | |
| 3485 | |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3486 | /* |
| 3487 | * op: 1 - enable, 0 - disable |
| 3488 | */ |
| 3489 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3490 | 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] | 3491 | { |
| 3492 | int all = 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3493 | int i, j, k; |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3494 | int progress, faili, failj, failk; |
| 3495 | |
| 3496 | uint8_t fsize; |
| 3497 | struct lys_feature *f; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3498 | |
| 3499 | if (!module || !name || !strlen(name)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3500 | LOGARG; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3501 | return EXIT_FAILURE; |
| 3502 | } |
| 3503 | |
| 3504 | if (!strcmp(name, "*")) { |
| 3505 | /* enable all */ |
| 3506 | all = 1; |
| 3507 | } |
| 3508 | |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3509 | progress = failk = 1; |
| 3510 | while (progress && failk) { |
| 3511 | for (i = -1, failk = progress = 0; i < module->inc_size; i++) { |
| 3512 | if (i == -1) { |
| 3513 | fsize = module->features_size; |
| 3514 | f = module->features; |
| 3515 | } else { |
| 3516 | fsize = module->inc[i].submodule->features_size; |
| 3517 | f = module->inc[i].submodule->features; |
| 3518 | } |
| 3519 | |
| 3520 | for (j = 0; j < fsize; j++) { |
| 3521 | if (all || !strcmp(f[j].name, name)) { |
Michal Vasko | 34c3b55 | 2016-11-21 09:07:43 +0100 | [diff] [blame] | 3522 | if ((op && (f[j].flags & LYS_FENABLED)) || (!op && !(f[j].flags & LYS_FENABLED))) { |
| 3523 | if (all) { |
| 3524 | /* skip already set features */ |
| 3525 | continue; |
| 3526 | } else { |
| 3527 | /* feature already set correctly */ |
| 3528 | return EXIT_SUCCESS; |
| 3529 | } |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3530 | } |
| 3531 | |
| 3532 | if (op) { |
| 3533 | /* check referenced features if they are enabled */ |
| 3534 | for (k = 0; k < f[j].iffeature_size; k++) { |
| 3535 | if (!resolve_iffeature(&f[j].iffeature[k])) { |
| 3536 | if (all) { |
| 3537 | faili = i; |
| 3538 | failj = j; |
| 3539 | failk = k + 1; |
| 3540 | break; |
| 3541 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3542 | LOGERR(module->ctx, LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.", |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3543 | f[j].name, k + 1); |
| 3544 | return EXIT_FAILURE; |
| 3545 | } |
| 3546 | } |
| 3547 | } |
| 3548 | |
| 3549 | if (k == f[j].iffeature_size) { |
| 3550 | /* the last check passed, do the change */ |
| 3551 | f[j].flags |= LYS_FENABLED; |
| 3552 | progress++; |
| 3553 | } |
| 3554 | } else { |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 3555 | lys_features_disable_recursive(&f[j]); |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3556 | progress++; |
| 3557 | } |
| 3558 | if (!all) { |
| 3559 | /* stop in case changing a single feature */ |
| 3560 | return EXIT_SUCCESS; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3561 | } |
| 3562 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3563 | } |
| 3564 | } |
| 3565 | } |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3566 | if (failk) { |
| 3567 | /* print info about the last failing feature */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3568 | LOGERR(module->ctx, LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.", |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3569 | faili == -1 ? module->features[failj].name : module->inc[faili].submodule->features[failj].name, failk); |
| 3570 | return EXIT_FAILURE; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3571 | } |
| 3572 | |
| 3573 | if (all) { |
| 3574 | return EXIT_SUCCESS; |
| 3575 | } else { |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3576 | /* the specified feature not found */ |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3577 | return EXIT_FAILURE; |
| 3578 | } |
| 3579 | } |
| 3580 | |
| 3581 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3582 | lys_features_enable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3583 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3584 | return lys_features_change(module, feature, 1); |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3585 | } |
| 3586 | |
| 3587 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3588 | lys_features_disable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3589 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3590 | return lys_features_change(module, feature, 0); |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3594 | lys_features_state(const struct lys_module *module, const char *feature) |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3595 | { |
| 3596 | int i, j; |
| 3597 | |
| 3598 | if (!module || !feature) { |
| 3599 | return -1; |
| 3600 | } |
| 3601 | |
| 3602 | /* search for the specified feature */ |
| 3603 | /* module itself */ |
| 3604 | for (i = 0; i < module->features_size; i++) { |
| 3605 | if (!strcmp(feature, module->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3606 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3607 | return 1; |
| 3608 | } else { |
| 3609 | return 0; |
| 3610 | } |
| 3611 | } |
| 3612 | } |
| 3613 | |
| 3614 | /* submodules */ |
| 3615 | for (j = 0; j < module->inc_size; j++) { |
| 3616 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
| 3617 | if (!strcmp(feature, module->inc[j].submodule->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3618 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3619 | return 1; |
| 3620 | } else { |
| 3621 | return 0; |
| 3622 | } |
| 3623 | } |
| 3624 | } |
| 3625 | } |
| 3626 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3627 | /* feature definition not found */ |
| 3628 | return -1; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3629 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3630 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3631 | API const char ** |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3632 | lys_features_list(const struct lys_module *module, uint8_t **states) |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3633 | { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3634 | const char **result = NULL; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3635 | int i, j; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3636 | unsigned int count; |
| 3637 | |
| 3638 | if (!module) { |
| 3639 | return NULL; |
| 3640 | } |
| 3641 | |
| 3642 | count = module->features_size; |
| 3643 | for (i = 0; i < module->inc_size; i++) { |
| 3644 | count += module->inc[i].submodule->features_size; |
| 3645 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3646 | result = malloc((count + 1) * sizeof *result); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3647 | LY_CHECK_ERR_RETURN(!result, LOGMEM(module->ctx), NULL); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3648 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3649 | if (states) { |
| 3650 | *states = malloc((count + 1) * sizeof **states); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3651 | LY_CHECK_ERR_RETURN(!(*states), LOGMEM(module->ctx); free(result), NULL); |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3652 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3653 | count = 0; |
| 3654 | |
| 3655 | /* module itself */ |
| 3656 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3657 | result[count] = module->features[i].name; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3658 | if (states) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3659 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3660 | (*states)[count] = 1; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3661 | } else { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3662 | (*states)[count] = 0; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3663 | } |
| 3664 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3665 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3666 | } |
| 3667 | |
| 3668 | /* submodules */ |
| 3669 | for (j = 0; j < module->inc_size; j++) { |
| 3670 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3671 | result[count] = module->inc[j].submodule->features[i].name; |
Radek Krejci | 374b94e | 2015-08-13 09:44:22 +0200 | [diff] [blame] | 3672 | if (states) { |
| 3673 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
| 3674 | (*states)[count] = 1; |
| 3675 | } else { |
| 3676 | (*states)[count] = 0; |
| 3677 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3678 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3679 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3680 | } |
| 3681 | } |
| 3682 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3683 | /* terminating NULL byte */ |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3684 | result[count] = NULL; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3685 | |
| 3686 | return result; |
| 3687 | } |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3688 | |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 3689 | API struct lys_module * |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 3690 | lys_node_module(const struct lys_node *node) |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3691 | { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 3692 | if (!node) { |
| 3693 | return NULL; |
| 3694 | } |
| 3695 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3696 | return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module; |
| 3697 | } |
| 3698 | |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 3699 | API struct lys_module * |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 3700 | lys_main_module(const struct lys_module *module) |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 3701 | { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 3702 | if (!module) { |
| 3703 | return NULL; |
| 3704 | } |
| 3705 | |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 3706 | return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module); |
| 3707 | } |
| 3708 | |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3709 | API struct lys_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3710 | lys_parent(const struct lys_node *node) |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3711 | { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 3712 | struct lys_node *parent; |
| 3713 | |
| 3714 | if (!node) { |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3715 | return NULL; |
| 3716 | } |
| 3717 | |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 3718 | if (node->nodetype == LYS_EXT) { |
| 3719 | if (((struct lys_ext_instance_complex*)node)->parent_type != LYEXT_PAR_NODE) { |
| 3720 | return NULL; |
| 3721 | } |
| 3722 | parent = (struct lys_node*)((struct lys_ext_instance_complex*)node)->parent; |
| 3723 | } else if (!node->parent) { |
| 3724 | return NULL; |
| 3725 | } else { |
| 3726 | parent = node->parent; |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3727 | } |
| 3728 | |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 3729 | if (parent->nodetype == LYS_AUGMENT) { |
| 3730 | return ((struct lys_node_augment *)parent)->target; |
| 3731 | } else { |
| 3732 | return parent; |
| 3733 | } |
| 3734 | } |
| 3735 | |
| 3736 | struct lys_node ** |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 3737 | lys_child(const struct lys_node *node, LYS_NODE nodetype) |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 3738 | { |
| 3739 | void *pp; |
| 3740 | assert(node); |
| 3741 | |
| 3742 | if (node->nodetype == LYS_EXT) { |
| 3743 | pp = lys_ext_complex_get_substmt(lys_snode2stmt(nodetype), (struct lys_ext_instance_complex*)node, NULL); |
| 3744 | if (!pp) { |
| 3745 | return NULL; |
| 3746 | } |
| 3747 | return (struct lys_node **)pp; |
Michal Vasko | 54d4c20 | 2017-08-09 14:09:18 +0200 | [diff] [blame] | 3748 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Radek Krejci | f7fe2cb | 2017-08-09 10:27:12 +0200 | [diff] [blame] | 3749 | return NULL; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 3750 | } else { |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 3751 | return (struct lys_node **)&node->child; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 3752 | } |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3753 | } |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3754 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 3755 | API void * |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3756 | lys_set_private(const struct lys_node *node, void *priv) |
| 3757 | { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 3758 | void *prev; |
| 3759 | |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3760 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3761 | LOGARG; |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 3762 | return NULL; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3763 | } |
| 3764 | |
Mislav Novakovic | b5529e5 | 2016-02-29 11:42:43 +0100 | [diff] [blame] | 3765 | prev = node->priv; |
| 3766 | ((struct lys_node *)node)->priv = priv; |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 3767 | |
| 3768 | return prev; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3769 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3770 | |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3771 | int |
| 3772 | lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref) |
| 3773 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3774 | struct lys_node_leaf *iter; |
| 3775 | struct ly_ctx *ctx = leafref_target->module->ctx; |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 3776 | |
Michal Vasko | 48a573d | 2016-07-01 11:46:02 +0200 | [diff] [blame] | 3777 | if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3778 | LOGINT(ctx); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3779 | return -1; |
| 3780 | } |
| 3781 | |
Pavol Vican | 9317515 | 2016-08-30 15:34:44 +0200 | [diff] [blame] | 3782 | /* check for config flag */ |
Radek Krejci | c688ca0 | 2017-03-20 12:54:39 +0100 | [diff] [blame] | 3783 | if (((struct lys_node_leaf*)leafref)->type.info.lref.req != -1 && |
| 3784 | (leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3785 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, leafref, |
Radek Krejci | d831dd4 | 2017-03-16 12:59:30 +0100 | [diff] [blame] | 3786 | "The leafref %s is config but refers to a non-config %s.", |
Pavol Vican | 9317515 | 2016-08-30 15:34:44 +0200 | [diff] [blame] | 3787 | strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype)); |
| 3788 | return -1; |
| 3789 | } |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 3790 | /* check for cycles */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3791 | for (iter = leafref_target; iter && iter->type.base == LY_TYPE_LEAFREF; iter = iter->type.info.lref.target) { |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 3792 | if ((void *)iter == (void *)leafref) { |
| 3793 | /* cycle detected */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3794 | LOGVAL(ctx, LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref); |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 3795 | return -1; |
| 3796 | } |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 3797 | } |
| 3798 | |
| 3799 | /* create fake child - the ly_set structure to hold the list of |
Michal Vasko | 48a573d | 2016-07-01 11:46:02 +0200 | [diff] [blame] | 3800 | * leafrefs referencing the leaf(-list) */ |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 3801 | if (!leafref_target->backlinks) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3802 | leafref_target->backlinks = (void *)ly_set_new(); |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 3803 | if (!leafref_target->backlinks) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3804 | LOGMEM(ctx); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3805 | return -1; |
| 3806 | } |
| 3807 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3808 | ly_set_add(leafref_target->backlinks, leafref, 0); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3809 | |
| 3810 | return 0; |
| 3811 | } |
| 3812 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3813 | /* not needed currently */ |
| 3814 | #if 0 |
| 3815 | |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3816 | static const char * |
| 3817 | lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len) |
| 3818 | { |
| 3819 | struct lys_module *prev_mod; |
| 3820 | uint32_t str_len, mod_len, buf_idx; |
| 3821 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3822 | 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] | 3823 | LOGINT; |
| 3824 | return NULL; |
| 3825 | } |
| 3826 | |
| 3827 | buf_idx = buf_len - 1; |
| 3828 | buf[buf_idx] = '\0'; |
| 3829 | |
| 3830 | while (node) { |
| 3831 | if (lys_parent(node)) { |
| 3832 | prev_mod = lys_node_module(lys_parent(node)); |
| 3833 | } else { |
| 3834 | prev_mod = NULL; |
| 3835 | } |
| 3836 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3837 | 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] | 3838 | str_len = strlen(node->name); |
| 3839 | |
| 3840 | if (prev_mod != node->module) { |
| 3841 | mod_len = strlen(node->module->name); |
| 3842 | } else { |
| 3843 | mod_len = 0; |
| 3844 | } |
| 3845 | |
| 3846 | if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) { |
| 3847 | LOGINT; |
| 3848 | return NULL; |
| 3849 | } |
| 3850 | |
| 3851 | buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len; |
| 3852 | |
| 3853 | buf[buf_idx] = '/'; |
| 3854 | if (mod_len) { |
| 3855 | memcpy(buf + buf_idx + 1, node->module->name, mod_len); |
| 3856 | buf[buf_idx + 1 + mod_len] = ':'; |
| 3857 | } |
| 3858 | memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len); |
| 3859 | } |
| 3860 | |
| 3861 | node = lys_parent(node); |
| 3862 | } |
| 3863 | |
| 3864 | return buf + buf_idx; |
| 3865 | } |
| 3866 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3867 | #endif |
| 3868 | |
| 3869 | API struct ly_set * |
Michal Vasko | 9ff79aa | 2017-07-03 14:10:32 +0200 | [diff] [blame] | 3870 | lys_xpath_atomize(const struct lys_node *ctx_node, enum lyxp_node_type ctx_node_type, const char *expr, int options) |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3871 | { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3872 | struct lyxp_set set; |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3873 | struct ly_set *ret_set; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3874 | uint32_t i; |
| 3875 | |
Michal Vasko | 9ff79aa | 2017-07-03 14:10:32 +0200 | [diff] [blame] | 3876 | if (!ctx_node || !expr) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3877 | LOGARG; |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3878 | return NULL; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3879 | } |
| 3880 | |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 3881 | /* adjust the root */ |
Michal Vasko | 9ff79aa | 2017-07-03 14:10:32 +0200 | [diff] [blame] | 3882 | if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) { |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 3883 | do { |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 3884 | ctx_node = lys_getnext(NULL, NULL, lys_node_module(ctx_node), LYS_GETNEXT_NOSTATECHECK); |
Michal Vasko | 9ff79aa | 2017-07-03 14:10:32 +0200 | [diff] [blame] | 3885 | } while ((ctx_node_type == LYXP_NODE_ROOT_CONFIG) && (ctx_node->flags & LYS_CONFIG_R)); |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 3886 | } |
| 3887 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3888 | memset(&set, 0, sizeof set); |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3889 | |
| 3890 | if (options & LYXP_MUST) { |
| 3891 | options &= ~LYXP_MUST; |
| 3892 | options |= LYXP_SNODE_MUST; |
| 3893 | } else if (options & LYXP_WHEN) { |
| 3894 | options &= ~LYXP_WHEN; |
| 3895 | options |= LYXP_SNODE_WHEN; |
| 3896 | } else { |
| 3897 | options |= LYXP_SNODE; |
| 3898 | } |
| 3899 | |
Michal Vasko | 9ff79aa | 2017-07-03 14:10:32 +0200 | [diff] [blame] | 3900 | if (lyxp_atomize(expr, ctx_node, ctx_node_type, &set, options, NULL)) { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3901 | free(set.val.snodes); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3902 | LOGVAL(ctx_node->module->ctx, LYE_SPEC, LY_VLOG_LYS, ctx_node, "Resolving XPath expression \"%s\" failed.", expr); |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3903 | return NULL; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3904 | } |
| 3905 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3906 | ret_set = ly_set_new(); |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3907 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3908 | for (i = 0; i < set.used; ++i) { |
| 3909 | switch (set.val.snodes[i].type) { |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3910 | case LYXP_NODE_ELEM: |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3911 | 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] | 3912 | ly_set_free(ret_set); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3913 | free(set.val.snodes); |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3914 | return NULL; |
| 3915 | } |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3916 | break; |
| 3917 | default: |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3918 | /* ignore roots, text and attr should not ever appear */ |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3919 | break; |
| 3920 | } |
| 3921 | } |
| 3922 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3923 | free(set.val.snodes); |
| 3924 | return ret_set; |
| 3925 | } |
| 3926 | |
| 3927 | API struct ly_set * |
| 3928 | lys_node_xpath_atomize(const struct lys_node *node, int options) |
| 3929 | { |
| 3930 | const struct lys_node *next, *elem, *parent, *tmp; |
| 3931 | struct lyxp_set set; |
| 3932 | struct ly_set *ret_set; |
| 3933 | uint16_t i; |
| 3934 | |
| 3935 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3936 | LOGARG; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3937 | return NULL; |
| 3938 | } |
| 3939 | |
| 3940 | for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent)); |
| 3941 | if (!parent) { |
| 3942 | /* not in input, output, or notification */ |
| 3943 | return NULL; |
| 3944 | } |
| 3945 | |
| 3946 | ret_set = ly_set_new(); |
Radek Krejci | d9af8d2 | 2016-09-07 18:44:26 +0200 | [diff] [blame] | 3947 | if (!ret_set) { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3948 | return NULL; |
| 3949 | } |
| 3950 | |
| 3951 | LY_TREE_DFS_BEGIN(node, next, elem) { |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 3952 | if ((options & LYXP_NO_LOCAL) && !(elem->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP))) { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3953 | /* elem has no dependencies from other subtrees and local nodes get discarded */ |
| 3954 | goto next_iter; |
| 3955 | } |
| 3956 | |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 3957 | if (lyxp_node_atomize(elem, &set, 0)) { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3958 | ly_set_free(ret_set); |
| 3959 | free(set.val.snodes); |
| 3960 | return NULL; |
| 3961 | } |
| 3962 | |
| 3963 | for (i = 0; i < set.used; ++i) { |
| 3964 | switch (set.val.snodes[i].type) { |
| 3965 | case LYXP_NODE_ELEM: |
| 3966 | if (options & LYXP_NO_LOCAL) { |
| 3967 | for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp)); |
| 3968 | if (tmp) { |
| 3969 | /* in local subtree, discard */ |
| 3970 | break; |
| 3971 | } |
| 3972 | } |
| 3973 | if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) { |
| 3974 | ly_set_free(ret_set); |
| 3975 | free(set.val.snodes); |
| 3976 | return NULL; |
| 3977 | } |
| 3978 | break; |
| 3979 | default: |
| 3980 | /* ignore roots, text and attr should not ever appear */ |
| 3981 | break; |
| 3982 | } |
| 3983 | } |
| 3984 | |
| 3985 | free(set.val.snodes); |
| 3986 | if (!(options & LYXP_RECURSIVE)) { |
| 3987 | break; |
| 3988 | } |
| 3989 | next_iter: |
| 3990 | LY_TREE_DFS_END(node, next, elem); |
| 3991 | } |
| 3992 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3993 | return ret_set; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3994 | } |
| 3995 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 3996 | /* logs */ |
| 3997 | int |
| 3998 | apply_aug(struct lys_node_augment *augment, struct unres_schema *unres) |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 3999 | { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4000 | struct lys_node *child, *parent; |
| 4001 | int clear_config; |
| 4002 | unsigned int u; |
Michal Vasko | d02e30e | 2018-01-22 13:35:48 +0100 | [diff] [blame] | 4003 | uint8_t *v; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4004 | struct lys_ext_instance *ext; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4005 | |
| 4006 | assert(augment->target && (augment->flags & LYS_NOTAPPLIED)); |
| 4007 | |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4008 | if (!augment->child) { |
| 4009 | /* nothing to apply */ |
| 4010 | goto success; |
| 4011 | } |
| 4012 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4013 | /* check that all the modules are implemented */ |
| 4014 | for (parent = augment->target; parent; parent = lys_parent(parent)) { |
| 4015 | if (!lys_node_module(parent)->implemented) { |
| 4016 | if (lys_set_implemented(lys_node_module(parent))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4017 | LOGERR(augment->module->ctx, ly_errno, "Making the augment target module \"%s\" implemented failed.", |
| 4018 | lys_node_module(parent)->name); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4019 | return -1; |
| 4020 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4021 | } |
| 4022 | } |
| 4023 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4024 | /* reconnect augmenting data into the target - add them to the target child list */ |
| 4025 | if (augment->target->child) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4026 | child = augment->target->child->prev; |
| 4027 | child->next = augment->child; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4028 | augment->target->child->prev = augment->child->prev; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4029 | augment->child->prev = child; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4030 | } else { |
| 4031 | augment->target->child = augment->child; |
| 4032 | } |
| 4033 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4034 | /* inherit config information from actual parent */ |
| 4035 | for (parent = augment->target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = lys_parent(parent)); |
| 4036 | clear_config = (parent) ? 1 : 0; |
| 4037 | LY_TREE_FOR(augment->child, child) { |
| 4038 | if (inherit_config_flag(child, augment->target->flags & LYS_CONFIG_MASK, clear_config)) { |
| 4039 | return -1; |
| 4040 | } |
| 4041 | } |
| 4042 | |
| 4043 | /* inherit extensions if any */ |
| 4044 | for (u = 0; u < augment->target->ext_size; u++) { |
| 4045 | ext = augment->target->ext[u]; /* shortcut */ |
| 4046 | if (ext && ext->def->plugin && (ext->def->plugin->flags & LYEXT_OPT_INHERIT)) { |
Michal Vasko | d02e30e | 2018-01-22 13:35:48 +0100 | [diff] [blame] | 4047 | v = malloc(sizeof *v); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4048 | LY_CHECK_ERR_RETURN(!v, LOGMEM(augment->module->ctx), -1); |
Michal Vasko | d02e30e | 2018-01-22 13:35:48 +0100 | [diff] [blame] | 4049 | *v = u; |
| 4050 | if (unres_schema_add_node(lys_main_module(augment->module), unres, &augment->target->ext, |
| 4051 | UNRES_EXT_FINALIZE, (struct lys_node *)v) == -1) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4052 | /* something really bad happend since the extension finalization is not actually |
| 4053 | * being resolved while adding into unres, so something more serious with the unres |
| 4054 | * list itself must happened */ |
| 4055 | return -1; |
| 4056 | } |
| 4057 | } |
| 4058 | } |
| 4059 | |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4060 | success: |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4061 | /* remove the flag about not applicability */ |
| 4062 | augment->flags &= ~LYS_NOTAPPLIED; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4063 | return EXIT_SUCCESS; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4064 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4065 | |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4066 | static void |
| 4067 | remove_aug(struct lys_node_augment *augment) |
| 4068 | { |
| 4069 | struct lys_node *last, *elem; |
| 4070 | |
Michal Vasko | f1aa47d | 2017-09-21 12:09:29 +0200 | [diff] [blame] | 4071 | if ((augment->flags & LYS_NOTAPPLIED) || !augment->target) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4072 | /* skip already not applied augment */ |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4073 | return; |
| 4074 | } |
| 4075 | |
| 4076 | elem = augment->child; |
| 4077 | if (elem) { |
| 4078 | LY_TREE_FOR(elem, last) { |
| 4079 | if (!last->next || (last->next->parent != (struct lys_node *)augment)) { |
| 4080 | break; |
| 4081 | } |
| 4082 | } |
| 4083 | /* elem is first augment child, last is the last child */ |
| 4084 | |
| 4085 | /* parent child ptr */ |
| 4086 | if (augment->target->child == elem) { |
| 4087 | augment->target->child = last->next; |
| 4088 | } |
| 4089 | |
| 4090 | /* parent child next ptr */ |
| 4091 | if (elem->prev->next) { |
| 4092 | elem->prev->next = last->next; |
| 4093 | } |
| 4094 | |
| 4095 | /* parent child prev ptr */ |
| 4096 | if (last->next) { |
| 4097 | last->next->prev = elem->prev; |
| 4098 | } else if (augment->target->child) { |
| 4099 | augment->target->child->prev = elem->prev; |
| 4100 | } |
| 4101 | |
| 4102 | /* update augment children themselves */ |
| 4103 | elem->prev = last; |
| 4104 | last->next = NULL; |
| 4105 | } |
| 4106 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4107 | /* augment->target still keeps the resolved target, but for lys_augment_free() |
| 4108 | * we have to keep information that this augment is not applied to free its data */ |
| 4109 | augment->flags |= LYS_NOTAPPLIED; |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4110 | } |
| 4111 | |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4112 | /* |
| 4113 | * @param[in] module - the module where the deviation is defined |
| 4114 | */ |
| 4115 | static void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4116 | 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] | 4117 | { |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4118 | int ret, reapply = 0; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4119 | char *parent_path; |
| 4120 | struct lys_node *target = NULL, *parent; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4121 | struct ly_set *set; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4122 | |
| 4123 | if (!dev->deviate) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4124 | return; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4125 | } |
| 4126 | |
| 4127 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 4128 | if (dev->orig_node) { |
| 4129 | /* removing not-supported deviation ... */ |
| 4130 | if (strrchr(dev->target_name, '/') != dev->target_name) { |
| 4131 | /* ... from a parent */ |
| 4132 | |
| 4133 | /* reconnect to its previous position */ |
| 4134 | parent = dev->orig_node->parent; |
Michal Vasko | a1074a5 | 2018-01-03 12:18:53 +0100 | [diff] [blame] | 4135 | if (parent && (parent->nodetype == LYS_AUGMENT)) { |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4136 | dev->orig_node->parent = NULL; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4137 | /* the original node was actually from augment, we have to get know if the augment is |
| 4138 | * applied (its module is enabled and implemented). If yes, the node will be connected |
| 4139 | * to the augment and the linkage with the target will be fixed if needed, otherwise |
| 4140 | * it will be connected only to the augment */ |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4141 | if (!(parent->flags & LYS_NOTAPPLIED)) { |
| 4142 | /* start with removing augment if applied before adding nodes, we have to make sure |
| 4143 | * that everything will be connect correctly */ |
| 4144 | remove_aug((struct lys_node_augment *)parent); |
| 4145 | reapply = 1; |
| 4146 | } |
| 4147 | /* connect the deviated node back into the augment */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4148 | lys_node_addchild(parent, NULL, dev->orig_node); |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4149 | if (reapply) { |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4150 | /* augment is supposed to be applied, so fix pointers in target and the status of the original node */ |
Michal Vasko | 823fbe0 | 2017-12-14 11:01:40 +0100 | [diff] [blame] | 4151 | parent->flags |= LYS_NOTAPPLIED; /* allow apply_aug() */ |
| 4152 | apply_aug((struct lys_node_augment *)parent, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4153 | } |
Michal Vasko | a1074a5 | 2018-01-03 12:18:53 +0100 | [diff] [blame] | 4154 | } else if (parent && (parent->nodetype == LYS_USES)) { |
| 4155 | /* uses child */ |
| 4156 | lys_node_addchild(parent, NULL, dev->orig_node); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4157 | } else { |
| 4158 | /* non-augment, non-toplevel */ |
| 4159 | parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4160 | ret = resolve_schema_nodeid(parent_path, NULL, module, &set, 0, 1); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4161 | free(parent_path); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4162 | if (ret == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4163 | LOGINT(module->ctx); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4164 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4165 | return; |
| 4166 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4167 | target = set->set.s[0]; |
| 4168 | ly_set_free(set); |
| 4169 | |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4170 | lys_node_addchild(target, NULL, dev->orig_node); |
| 4171 | } |
| 4172 | } else { |
| 4173 | /* ... from top-level data */ |
| 4174 | lys_node_addchild(NULL, (struct lys_module *)dev->orig_node->module, dev->orig_node); |
| 4175 | } |
| 4176 | |
| 4177 | dev->orig_node = NULL; |
| 4178 | } else { |
| 4179 | /* adding not-supported deviation */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4180 | ret = resolve_schema_nodeid(dev->target_name, NULL, module, &set, 0, 1); |
| 4181 | if (ret == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4182 | LOGINT(module->ctx); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4183 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4184 | return; |
| 4185 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4186 | target = set->set.s[0]; |
| 4187 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4188 | |
| 4189 | /* unlink and store the original node */ |
| 4190 | parent = target->parent; |
| 4191 | lys_node_unlink(target); |
Michal Vasko | a1074a5 | 2018-01-03 12:18:53 +0100 | [diff] [blame] | 4192 | if (parent && (parent->nodetype & (LYS_AUGMENT | LYS_USES))) { |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4193 | /* hack for augment, because when the original will be sometime reconnected back, we actually need |
| 4194 | * to reconnect it to both - the augment and its target (which is deduced from the deviations target |
| 4195 | * path), so we need to remember the augment as an addition */ |
Michal Vasko | a1074a5 | 2018-01-03 12:18:53 +0100 | [diff] [blame] | 4196 | /* we also need to remember the parent uses so that we connect it back to it when switching deviation state */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4197 | target->parent = parent; |
| 4198 | } |
| 4199 | dev->orig_node = target; |
| 4200 | } |
| 4201 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4202 | ret = resolve_schema_nodeid(dev->target_name, NULL, module, &set, 0, 1); |
| 4203 | if (ret == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4204 | LOGINT(module->ctx); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4205 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4206 | return; |
| 4207 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4208 | target = set->set.s[0]; |
| 4209 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4210 | |
| 4211 | lys_node_switch(target, dev->orig_node); |
| 4212 | dev->orig_node = target; |
| 4213 | } |
| 4214 | } |
| 4215 | |
| 4216 | /* temporarily removes or applies deviations, updates module deviation flag accordingly */ |
| 4217 | void |
| 4218 | lys_switch_deviations(struct lys_module *module) |
| 4219 | { |
| 4220 | uint32_t i = 0, j; |
| 4221 | const struct lys_module *mod; |
| 4222 | const char *ptr; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4223 | struct unres_schema *unres; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4224 | |
| 4225 | if (module->deviated) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4226 | unres = calloc(1, sizeof *unres); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4227 | LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), ); |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4228 | |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4229 | while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) { |
| 4230 | if (mod == module) { |
| 4231 | continue; |
| 4232 | } |
| 4233 | |
| 4234 | for (j = 0; j < mod->deviation_size; ++j) { |
| 4235 | ptr = strstr(mod->deviation[j].target_name, module->name); |
| 4236 | if (ptr && ptr[strlen(module->name)] == ':') { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4237 | lys_switch_deviation(&mod->deviation[j], mod, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4238 | } |
| 4239 | } |
| 4240 | } |
| 4241 | |
| 4242 | if (module->deviated == 2) { |
| 4243 | module->deviated = 1; |
| 4244 | } else { |
| 4245 | module->deviated = 2; |
| 4246 | } |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4247 | for (j = 0; j < module->inc_size; j++) { |
| 4248 | if (module->inc[j].submodule->deviated) { |
| 4249 | module->inc[j].submodule->deviated = module->deviated; |
| 4250 | } |
| 4251 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4252 | |
| 4253 | if (unres->count) { |
| 4254 | resolve_unres_schema(module, unres); |
| 4255 | } |
| 4256 | unres_schema_free(module, &unres, 1); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4257 | } |
| 4258 | } |
| 4259 | |
| 4260 | static void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4261 | 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] | 4262 | { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4263 | lys_switch_deviation(dev, module, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4264 | |
| 4265 | assert(dev->orig_node); |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4266 | lys_node_module(dev->orig_node)->deviated = 1; /* main module */ |
| 4267 | dev->orig_node->module->deviated = 1; /* possible submodule */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4268 | } |
| 4269 | |
| 4270 | static void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4271 | 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] | 4272 | { |
| 4273 | uint32_t idx = 0, j; |
| 4274 | const struct lys_module *mod; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4275 | struct lys_module *target_mod, *target_submod; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4276 | const char *ptr; |
| 4277 | |
| 4278 | if (dev->orig_node) { |
| 4279 | target_mod = lys_node_module(dev->orig_node); |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4280 | target_submod = dev->orig_node->module; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4281 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4282 | LOGINT(module->ctx); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4283 | return; |
| 4284 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4285 | lys_switch_deviation(dev, module, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4286 | |
| 4287 | /* clear the deviation flag if possible */ |
| 4288 | while ((mod = ly_ctx_get_module_iter(module->ctx, &idx))) { |
| 4289 | if ((mod == module) || (mod == target_mod)) { |
| 4290 | continue; |
| 4291 | } |
| 4292 | |
| 4293 | for (j = 0; j < mod->deviation_size; ++j) { |
| 4294 | ptr = strstr(mod->deviation[j].target_name, target_mod->name); |
| 4295 | if (ptr && (ptr[strlen(target_mod->name)] == ':')) { |
| 4296 | /* some other module deviation targets the inspected module, flag remains */ |
| 4297 | break; |
| 4298 | } |
| 4299 | } |
| 4300 | |
| 4301 | if (j < mod->deviation_size) { |
| 4302 | break; |
| 4303 | } |
| 4304 | } |
| 4305 | |
| 4306 | if (!mod) { |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4307 | target_mod->deviated = 0; /* main module */ |
| 4308 | target_submod->deviated = 0; /* possible submodule */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4309 | } |
| 4310 | } |
| 4311 | |
| 4312 | void |
| 4313 | lys_sub_module_apply_devs_augs(struct lys_module *module) |
| 4314 | { |
| 4315 | uint8_t u, v; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4316 | struct unres_schema *unres; |
| 4317 | |
| 4318 | unres = calloc(1, sizeof *unres); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4319 | LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), ); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4320 | |
| 4321 | /* remove applied deviations */ |
| 4322 | for (u = 0; u < module->deviation_size; ++u) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4323 | apply_dev(&module->deviation[u], module, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4324 | } |
| 4325 | /* remove applied augments */ |
| 4326 | for (u = 0; u < module->augment_size; ++u) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4327 | apply_aug(&module->augment[u], unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4328 | } |
| 4329 | |
| 4330 | /* remove deviation and augments defined in submodules */ |
| 4331 | for (v = 0; v < module->inc_size; ++v) { |
| 4332 | for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4333 | apply_dev(&module->inc[v].submodule->deviation[u], module, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4334 | } |
| 4335 | |
| 4336 | for (u = 0; u < module->inc[v].submodule->augment_size; ++u) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4337 | apply_aug(&module->inc[v].submodule->augment[u], unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4338 | } |
| 4339 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4340 | |
| 4341 | if (unres->count) { |
| 4342 | resolve_unres_schema(module, unres); |
| 4343 | } |
| 4344 | /* nothing else left to do even if something is not resolved */ |
| 4345 | unres_schema_free(module, &unres, 1); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4346 | } |
| 4347 | |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4348 | void |
| 4349 | lys_sub_module_remove_devs_augs(struct lys_module *module) |
| 4350 | { |
| 4351 | uint8_t u, v; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4352 | struct unres_schema *unres; |
| 4353 | |
| 4354 | unres = calloc(1, sizeof *unres); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4355 | LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), ); |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4356 | |
| 4357 | /* remove applied deviations */ |
| 4358 | for (u = 0; u < module->deviation_size; ++u) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4359 | remove_dev(&module->deviation[u], module, unres); |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4360 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4361 | /* remove applied augments */ |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4362 | for (u = 0; u < module->augment_size; ++u) { |
| 4363 | remove_aug(&module->augment[u]); |
| 4364 | } |
| 4365 | |
| 4366 | /* remove deviation and augments defined in submodules */ |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 4367 | for (v = 0; v < module->inc_size && module->inc[v].submodule; ++v) { |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4368 | for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4369 | remove_dev(&module->inc[v].submodule->deviation[u], module, unres); |
Radek Krejci | dbc1526 | 2016-06-16 14:58:29 +0200 | [diff] [blame] | 4370 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4371 | |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4372 | for (u = 0; u < module->inc[v].submodule->augment_size; ++u) { |
| 4373 | remove_aug(&module->inc[v].submodule->augment[u]); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4374 | } |
| 4375 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4376 | |
| 4377 | if (unres->count) { |
| 4378 | resolve_unres_schema(module, unres); |
| 4379 | } |
| 4380 | /* nothing else left to do even if something is not resolved */ |
| 4381 | unres_schema_free(module, &unres, 1); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4382 | } |
| 4383 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4384 | static int |
| 4385 | lys_set_implemented_recursion(struct lys_module *module, struct unres_schema *unres) |
| 4386 | { |
| 4387 | struct lys_node *root, *next, *node; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 4388 | uint16_t i, j; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4389 | |
| 4390 | for (i = 0; i < module->augment_size; i++) { |
| 4391 | /* apply augment */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4392 | assert(module->augment[i].target); |
| 4393 | if (apply_aug(&module->augment[i], unres)) { |
| 4394 | return EXIT_FAILURE; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4395 | } |
| 4396 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 4397 | |
| 4398 | /* identities */ |
| 4399 | for (i = 0; i < module->ident_size; i++) { |
| 4400 | for (j = 0; j < module->ident[i].base_size; j++) { |
| 4401 | resolve_identity_backlink_update(&module->ident[i], module->ident[i].base[j]); |
| 4402 | } |
| 4403 | } |
| 4404 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4405 | LY_TREE_FOR(module->data, root) { |
| 4406 | /* handle leafrefs and recursively change the implemented flags in the leafref targets */ |
| 4407 | LY_TREE_DFS_BEGIN(root, next, node) { |
| 4408 | if (node->nodetype == LYS_GROUPING) { |
| 4409 | goto nextsibling; |
| 4410 | } |
| 4411 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4412 | if (((struct lys_node_leaf *)node)->type.base == LY_TYPE_LEAFREF) { |
| 4413 | if (unres_schema_add_node(module, unres, &((struct lys_node_leaf *)node)->type, |
| 4414 | UNRES_TYPE_LEAFREF, node) == -1) { |
| 4415 | return EXIT_FAILURE; |
| 4416 | } |
| 4417 | } |
| 4418 | } |
| 4419 | |
| 4420 | /* modified LY_TREE_DFS_END */ |
| 4421 | next = node->child; |
| 4422 | /* child exception for leafs, leaflists and anyxml without children */ |
| 4423 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 4424 | next = NULL; |
| 4425 | } |
| 4426 | if (!next) { |
| 4427 | nextsibling: |
| 4428 | /* no children */ |
| 4429 | if (node == root) { |
| 4430 | /* we are done, root has no children */ |
| 4431 | break; |
| 4432 | } |
| 4433 | /* try siblings */ |
| 4434 | next = node->next; |
| 4435 | } |
| 4436 | while (!next) { |
| 4437 | /* parent is already processed, go to its sibling */ |
| 4438 | node = lys_parent(node); |
| 4439 | /* no siblings, go back through parents */ |
| 4440 | if (lys_parent(node) == lys_parent(root)) { |
| 4441 | /* we are done, no next element to process */ |
| 4442 | break; |
| 4443 | } |
| 4444 | next = node->next; |
| 4445 | } |
| 4446 | } |
| 4447 | } |
| 4448 | |
| 4449 | return EXIT_SUCCESS; |
| 4450 | } |
| 4451 | |
| 4452 | API int |
| 4453 | lys_set_implemented(const struct lys_module *module) |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4454 | { |
| 4455 | struct ly_ctx *ctx; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4456 | struct unres_schema *unres; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 4457 | int i, j, k, disabled = 0; |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4458 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4459 | if (!module) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4460 | LOGARG; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4461 | return EXIT_FAILURE; |
| 4462 | } |
| 4463 | |
| 4464 | module = lys_main_module(module); |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4465 | |
| 4466 | if (module->disabled) { |
| 4467 | disabled = 1; |
| 4468 | lys_set_enabled(module); |
| 4469 | } |
| 4470 | |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4471 | if (module->implemented) { |
| 4472 | return EXIT_SUCCESS; |
| 4473 | } |
| 4474 | |
| 4475 | ctx = module->ctx; |
| 4476 | |
| 4477 | for (i = 0; i < ctx->models.used; ++i) { |
| 4478 | if (module == ctx->models.list[i]) { |
| 4479 | continue; |
| 4480 | } |
| 4481 | |
| 4482 | if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4483 | LOGERR(ctx, LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name); |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4484 | if (disabled) { |
| 4485 | /* set it back disabled */ |
| 4486 | lys_set_disabled(module); |
| 4487 | } |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4488 | return EXIT_FAILURE; |
| 4489 | } |
| 4490 | } |
| 4491 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4492 | unres = calloc(1, sizeof *unres); |
| 4493 | if (!unres) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4494 | LOGMEM(ctx); |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4495 | if (disabled) { |
| 4496 | /* set it back disabled */ |
| 4497 | lys_set_disabled(module); |
| 4498 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4499 | return EXIT_FAILURE; |
| 4500 | } |
| 4501 | /* recursively make the module implemented */ |
| 4502 | ((struct lys_module *)module)->implemented = 1; |
| 4503 | if (lys_set_implemented_recursion((struct lys_module *)module, unres)) { |
| 4504 | goto error; |
| 4505 | } |
| 4506 | /* process augments in submodules */ |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 4507 | for (i = 0; i < module->inc_size && module->inc[i].submodule; ++i) { |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4508 | for (j = 0; j < module->inc[i].submodule->augment_size; j++) { |
| 4509 | /* apply augment */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4510 | assert(module->inc[i].submodule->augment[j].target); |
| 4511 | if (apply_aug(&module->inc[i].submodule->augment[j], unres)) { |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4512 | goto error; |
| 4513 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4514 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 4515 | |
| 4516 | /* identities */ |
| 4517 | for (j = 0; j < module->inc[i].submodule->ident_size; j++) { |
| 4518 | for (k = 0; k < module->inc[i].submodule->ident[j].base_size; k++) { |
| 4519 | resolve_identity_backlink_update(&module->inc[i].submodule->ident[j], |
| 4520 | module->inc[i].submodule->ident[j].base[k]); |
| 4521 | } |
| 4522 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4523 | } |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4524 | /* try again resolve augments in other modules possibly augmenting this one, |
| 4525 | * since we have just enabled it |
| 4526 | */ |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4527 | /* resolve rest of unres items */ |
| 4528 | if (unres->count && resolve_unres_schema((struct lys_module *)module, unres)) { |
| 4529 | goto error; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4530 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4531 | unres_schema_free(NULL, &unres, 0); |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4532 | |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4533 | /* reflect implemented flag in submodules */ |
| 4534 | for (i = 0; i < module->inc_size; i++) { |
| 4535 | module->inc[i].submodule->implemented = 1; |
| 4536 | } |
| 4537 | |
Michal Vasko | be136f6 | 2017-09-21 12:08:39 +0200 | [diff] [blame] | 4538 | LOGVRB("Module \"%s%s%s\" now implemented.", module->name, (module->rev_size ? "@" : ""), |
| 4539 | (module->rev_size ? module->rev[0].date : "")); |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4540 | return EXIT_SUCCESS; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4541 | |
| 4542 | error: |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4543 | if (disabled) { |
| 4544 | /* set it back disabled */ |
| 4545 | lys_set_disabled(module); |
| 4546 | } |
| 4547 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4548 | ((struct lys_module *)module)->implemented = 0; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4549 | unres_schema_free((struct lys_module *)module, &unres, 1); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4550 | return EXIT_FAILURE; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4551 | } |
| 4552 | |
| 4553 | void |
| 4554 | lys_submodule_module_data_free(struct lys_submodule *submodule) |
| 4555 | { |
| 4556 | struct lys_node *next, *elem; |
| 4557 | |
| 4558 | /* remove parsed data */ |
| 4559 | LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) { |
| 4560 | if (elem->module == (struct lys_module *)submodule) { |
| 4561 | lys_node_free(elem, NULL, 0); |
| 4562 | } |
| 4563 | } |
| 4564 | } |
Radek Krejci | a1c33bf | 2016-09-07 12:38:49 +0200 | [diff] [blame] | 4565 | |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4566 | API char * |
Michal Vasko | 395b0a0 | 2018-01-22 09:36:20 +0100 | [diff] [blame] | 4567 | lys_path(const struct lys_node *node, int options) |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4568 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4569 | char *buf = NULL; |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4570 | |
| 4571 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4572 | LOGARG; |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4573 | return NULL; |
| 4574 | } |
| 4575 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4576 | if (ly_vlog_build_path(LY_VLOG_LYS, node, &buf, !options)) { |
Michal Vasko | 59631dd | 2017-10-02 11:56:11 +0200 | [diff] [blame] | 4577 | return NULL; |
| 4578 | } |
| 4579 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4580 | return buf; |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4581 | } |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 4582 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4583 | API char * |
| 4584 | lys_data_path(const struct lys_node *node) |
| 4585 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4586 | char *result = NULL, buf[1024]; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 4587 | const char *separator, *name; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4588 | int i, used; |
| 4589 | struct ly_set *set; |
| 4590 | const struct lys_module *prev_mod; |
| 4591 | |
| 4592 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4593 | LOGARG; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4594 | return NULL; |
| 4595 | } |
| 4596 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4597 | set = ly_set_new(); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4598 | LY_CHECK_ERR_GOTO(!set, LOGMEM(node->module->ctx), cleanup); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4599 | |
| 4600 | while (node) { |
| 4601 | ly_set_add(set, (void *)node, 0); |
| 4602 | do { |
| 4603 | node = lys_parent(node); |
| 4604 | } while (node && (node->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))); |
| 4605 | } |
| 4606 | |
| 4607 | prev_mod = NULL; |
| 4608 | used = 0; |
| 4609 | for (i = set->number - 1; i > -1; --i) { |
| 4610 | node = set->set.s[i]; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 4611 | if (node->nodetype == LYS_EXT) { |
| 4612 | if (strcmp(((struct lys_ext_instance *)node)->def->name, "yang-data")) { |
| 4613 | continue; |
| 4614 | } |
| 4615 | name = ((struct lys_ext_instance *)node)->arg_value; |
| 4616 | separator = ":#"; |
| 4617 | } else { |
| 4618 | name = node->name; |
| 4619 | separator = ":"; |
| 4620 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4621 | used += sprintf(buf + used, "/%s%s%s", (lys_node_module(node) == prev_mod ? "" : lys_node_module(node)->name), |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 4622 | (lys_node_module(node) == prev_mod ? "" : separator), name); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4623 | prev_mod = lys_node_module(node); |
| 4624 | } |
| 4625 | |
| 4626 | result = strdup(buf); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4627 | LY_CHECK_ERR_GOTO(!result, LOGMEM(node->module->ctx), cleanup); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4628 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4629 | cleanup: |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4630 | ly_set_free(set); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4631 | return result; |
| 4632 | } |
| 4633 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4634 | struct lys_node_augment * |
| 4635 | lys_getnext_target_aug(struct lys_node_augment *last, const struct lys_module *mod, const struct lys_node *aug_target) |
| 4636 | { |
| 4637 | int i, j, last_found; |
| 4638 | |
Michal Vasko | b690687 | 2018-03-12 11:35:09 +0100 | [diff] [blame] | 4639 | assert(mod && aug_target); |
| 4640 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4641 | if (!last) { |
| 4642 | last_found = 1; |
| 4643 | } else { |
| 4644 | last_found = 0; |
| 4645 | } |
| 4646 | |
| 4647 | /* search module augments */ |
| 4648 | for (i = 0; i < mod->augment_size; ++i) { |
| 4649 | if (!mod->augment[i].target) { |
| 4650 | /* still unresolved, skip */ |
| 4651 | continue; |
| 4652 | } |
| 4653 | |
| 4654 | if (mod->augment[i].target == aug_target) { |
| 4655 | if (last_found) { |
| 4656 | /* next match after last */ |
| 4657 | return &mod->augment[i]; |
| 4658 | } |
| 4659 | |
| 4660 | if (&mod->augment[i] == last) { |
| 4661 | last_found = 1; |
| 4662 | } |
| 4663 | } |
| 4664 | } |
| 4665 | |
| 4666 | /* search submodule augments */ |
| 4667 | for (i = 0; i < mod->inc_size; ++i) { |
| 4668 | for (j = 0; j < mod->inc[i].submodule->augment_size; ++j) { |
| 4669 | if (!mod->inc[i].submodule->augment[j].target) { |
| 4670 | continue; |
| 4671 | } |
| 4672 | |
| 4673 | if (mod->inc[i].submodule->augment[j].target == aug_target) { |
| 4674 | if (last_found) { |
| 4675 | /* next match after last */ |
| 4676 | return &mod->inc[i].submodule->augment[j]; |
| 4677 | } |
| 4678 | |
| 4679 | if (&mod->inc[i].submodule->augment[j] == last) { |
| 4680 | last_found = 1; |
| 4681 | } |
| 4682 | } |
| 4683 | } |
| 4684 | } |
| 4685 | |
| 4686 | return NULL; |
| 4687 | } |
| 4688 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4689 | API struct ly_set * |
| 4690 | lys_find_path(const struct lys_module *cur_module, const struct lys_node *cur_node, const char *path) |
| 4691 | { |
| 4692 | struct ly_set *ret; |
| 4693 | int rc; |
| 4694 | |
| 4695 | if ((!cur_module && !cur_node) || !path) { |
| 4696 | return NULL; |
| 4697 | } |
| 4698 | |
| 4699 | rc = resolve_schema_nodeid(path, cur_node, cur_module, &ret, 1, 1); |
| 4700 | if (rc == -1) { |
| 4701 | return NULL; |
| 4702 | } |
| 4703 | |
| 4704 | return ret; |
| 4705 | } |
| 4706 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4707 | static void |
| 4708 | lys_extcomplex_free_str(struct ly_ctx *ctx, struct lys_ext_instance_complex *ext, LY_STMT stmt) |
| 4709 | { |
| 4710 | struct lyext_substmt *info; |
Radek Krejci | b71243e | 2017-02-08 16:20:08 +0100 | [diff] [blame] | 4711 | const char **str, ***a; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4712 | int c; |
| 4713 | |
| 4714 | str = lys_ext_complex_get_substmt(stmt, ext, &info); |
| 4715 | if (!str || !(*str)) { |
| 4716 | return; |
| 4717 | } |
| 4718 | if (info->cardinality >= LY_STMT_CARD_SOME) { |
| 4719 | /* we have array */ |
Radek Krejci | b71243e | 2017-02-08 16:20:08 +0100 | [diff] [blame] | 4720 | a = (const char ***)str; |
Radek Krejci | 56c8041 | 2017-02-09 10:44:16 +0100 | [diff] [blame] | 4721 | for (str = (*(const char ***)str), c = 0; str[c]; c++) { |
| 4722 | lydict_remove(ctx, str[c]); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4723 | } |
Radek Krejci | 56c8041 | 2017-02-09 10:44:16 +0100 | [diff] [blame] | 4724 | free(a[0]); |
| 4725 | if (stmt == LY_STMT_BELONGSTO) { |
| 4726 | for (str = a[1], c = 0; str[c]; c++) { |
| 4727 | lydict_remove(ctx, str[c]); |
| 4728 | } |
| 4729 | free(a[1]); |
PavolVican | 99c7072 | 2017-02-18 17:25:52 +0100 | [diff] [blame] | 4730 | } else if (stmt == LY_STMT_ARGUMENT) { |
| 4731 | free(a[1]); |
Radek Krejci | 56c8041 | 2017-02-09 10:44:16 +0100 | [diff] [blame] | 4732 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4733 | } else { |
Radek Krejci | 56c8041 | 2017-02-09 10:44:16 +0100 | [diff] [blame] | 4734 | lydict_remove(ctx, str[0]); |
| 4735 | if (stmt == LY_STMT_BELONGSTO) { |
| 4736 | lydict_remove(ctx, str[1]); |
| 4737 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4738 | } |
| 4739 | } |
| 4740 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4741 | lys_extension_instances_free(struct ly_ctx *ctx, struct lys_ext_instance **e, unsigned int size, |
| 4742 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4743 | { |
Radek Krejci | f8d05c2 | 2017-02-10 15:33:35 +0100 | [diff] [blame] | 4744 | unsigned int i, j, k; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4745 | struct lyext_substmt *substmt; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4746 | void **pp, **start; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4747 | struct lys_node *siter, *snext; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4748 | |
| 4749 | #define EXTCOMPLEX_FREE_STRUCT(STMT, TYPE, FUNC, FREE, ARGS...) \ |
Radek Krejci | b84686f | 2017-02-09 16:04:55 +0100 | [diff] [blame] | 4750 | 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] | 4751 | if (!pp || !(*pp)) { break; } \ |
Radek Krejci | b84686f | 2017-02-09 16:04:55 +0100 | [diff] [blame] | 4752 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */ \ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4753 | for (start = pp = *pp; *pp; pp++) { \ |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4754 | FUNC(ctx, (TYPE *)(*pp), ##ARGS, private_destructor); \ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4755 | if (FREE) { free(*pp); } \ |
| 4756 | } \ |
| 4757 | free(start); \ |
| 4758 | } else { /* single item */ \ |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4759 | FUNC(ctx, (TYPE *)(*pp), ##ARGS, private_destructor); \ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4760 | if (FREE) { free(*pp); } \ |
| 4761 | } |
| 4762 | |
PavolVican | 9d61d40 | 2018-02-05 15:52:48 +0100 | [diff] [blame] | 4763 | if (!size || !e) { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4764 | return; |
| 4765 | } |
| 4766 | |
| 4767 | for (i = 0; i < size; i++) { |
| 4768 | if (!e[i]) { |
| 4769 | continue; |
| 4770 | } |
| 4771 | |
| 4772 | if (e[i]->flags & (LYEXT_OPT_INHERIT)) { |
| 4773 | /* no free, this is just a shadow copy of the original extension instance */ |
| 4774 | } else { |
| 4775 | if (e[i]->flags & (LYEXT_OPT_YANG)) { |
| 4776 | free(e[i]->def); /* remove name of instance extension */ |
PavolVican | 19dc615 | 2017-02-06 12:04:15 +0100 | [diff] [blame] | 4777 | e[i]->def = NULL; |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 4778 | 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] | 4779 | } |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4780 | /* remove private object */ |
| 4781 | if (e[i]->priv && private_destructor) { |
| 4782 | private_destructor((struct lys_node*)e[i], e[i]->priv); |
| 4783 | } |
| 4784 | 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] | 4785 | lydict_remove(ctx, e[i]->arg_value); |
| 4786 | } |
| 4787 | |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 4788 | if (e[i]->def && e[i]->def->plugin && e[i]->def->plugin->type == LYEXT_COMPLEX |
| 4789 | && ((e[i]->flags & LYEXT_OPT_CONTENT) == 0)) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4790 | substmt = ((struct lys_ext_instance_complex *)e[i])->substmt; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4791 | for (j = 0; substmt[j].stmt; j++) { |
| 4792 | switch(substmt[j].stmt) { |
| 4793 | case LY_STMT_DESCRIPTION: |
| 4794 | case LY_STMT_REFERENCE: |
| 4795 | case LY_STMT_UNITS: |
Radek Krejci | b71243e | 2017-02-08 16:20:08 +0100 | [diff] [blame] | 4796 | case LY_STMT_ARGUMENT: |
| 4797 | case LY_STMT_DEFAULT: |
| 4798 | case LY_STMT_ERRTAG: |
| 4799 | case LY_STMT_ERRMSG: |
| 4800 | case LY_STMT_PREFIX: |
| 4801 | case LY_STMT_NAMESPACE: |
| 4802 | case LY_STMT_PRESENCE: |
| 4803 | case LY_STMT_REVISIONDATE: |
| 4804 | case LY_STMT_KEY: |
| 4805 | case LY_STMT_BASE: |
| 4806 | case LY_STMT_BELONGSTO: |
| 4807 | case LY_STMT_CONTACT: |
| 4808 | case LY_STMT_ORGANIZATION: |
| 4809 | case LY_STMT_PATH: |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4810 | lys_extcomplex_free_str(ctx, (struct lys_ext_instance_complex *)e[i], substmt[j].stmt); |
| 4811 | break; |
| 4812 | case LY_STMT_TYPE: |
| 4813 | EXTCOMPLEX_FREE_STRUCT(LY_STMT_TYPE, struct lys_type, lys_type_free, 1); |
| 4814 | break; |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 4815 | case LY_STMT_TYPEDEF: |
| 4816 | EXTCOMPLEX_FREE_STRUCT(LY_STMT_TYPEDEF, struct lys_tpdf, lys_tpdf_free, 1); |
| 4817 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4818 | case LY_STMT_IFFEATURE: |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 4819 | EXTCOMPLEX_FREE_STRUCT(LY_STMT_IFFEATURE, struct lys_iffeature, lys_iffeature_free, 0, 1, 0); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4820 | break; |
Radek Krejci | 5496fae | 2017-02-10 13:26:48 +0100 | [diff] [blame] | 4821 | case LY_STMT_MAX: |
| 4822 | case LY_STMT_MIN: |
| 4823 | case LY_STMT_POSITION: |
PavolVican | 2ed9f4e | 2017-02-16 00:08:45 +0100 | [diff] [blame] | 4824 | case LY_STMT_VALUE: |
Radek Krejci | f8d05c2 | 2017-02-10 15:33:35 +0100 | [diff] [blame] | 4825 | 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] | 4826 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME && *pp) { |
Radek Krejci | f8d05c2 | 2017-02-10 15:33:35 +0100 | [diff] [blame] | 4827 | for(k = 0; ((uint32_t**)(*pp))[k]; k++) { |
| 4828 | free(((uint32_t**)(*pp))[k]); |
| 4829 | } |
| 4830 | } |
| 4831 | free(*pp); |
| 4832 | break; |
| 4833 | case LY_STMT_DIGITS: |
Radek Krejci | b84686f | 2017-02-09 16:04:55 +0100 | [diff] [blame] | 4834 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { |
| 4835 | /* free the array */ |
| 4836 | pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset]; |
| 4837 | free(*pp); |
| 4838 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4839 | break; |
Radek Krejci | 37f9ba3 | 2017-02-10 16:50:35 +0100 | [diff] [blame] | 4840 | case LY_STMT_MODULE: |
| 4841 | /* modules are part of the context, so they will be freed there */ |
| 4842 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { |
| 4843 | /* free the array */ |
| 4844 | pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset]; |
| 4845 | free(*pp); |
| 4846 | } |
| 4847 | break; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4848 | case LY_STMT_ACTION: |
Radek Krejci | b31762b | 2017-02-15 10:48:42 +0100 | [diff] [blame] | 4849 | case LY_STMT_ANYDATA: |
| 4850 | case LY_STMT_ANYXML: |
| 4851 | case LY_STMT_CASE: |
| 4852 | case LY_STMT_CHOICE: |
| 4853 | case LY_STMT_CONTAINER: |
| 4854 | case LY_STMT_GROUPING: |
| 4855 | case LY_STMT_INPUT: |
| 4856 | case LY_STMT_LEAF: |
| 4857 | case LY_STMT_LEAFLIST: |
| 4858 | case LY_STMT_LIST: |
| 4859 | case LY_STMT_NOTIFICATION: |
| 4860 | case LY_STMT_OUTPUT: |
| 4861 | case LY_STMT_RPC: |
| 4862 | case LY_STMT_USES: |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4863 | pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset]; |
| 4864 | LY_TREE_FOR_SAFE((struct lys_node *)(*pp), snext, siter) { |
| 4865 | lys_node_free(siter, NULL, 0); |
| 4866 | } |
Radek Krejci | b31762b | 2017-02-15 10:48:42 +0100 | [diff] [blame] | 4867 | *pp = NULL; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4868 | break; |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 4869 | case LY_STMT_UNIQUE: |
| 4870 | pp = lys_ext_complex_get_substmt(LY_STMT_UNIQUE, (struct lys_ext_instance_complex *)e[i], NULL); |
| 4871 | if (!pp || !(*pp)) { |
| 4872 | break; |
| 4873 | } |
| 4874 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */ |
| 4875 | for (start = pp = *pp; *pp; pp++) { |
| 4876 | for (k = 0; k < (*(struct lys_unique**)pp)->expr_size; k++) { |
| 4877 | lydict_remove(ctx, (*(struct lys_unique**)pp)->expr[k]); |
| 4878 | } |
| 4879 | free((*(struct lys_unique**)pp)->expr); |
| 4880 | free(*pp); |
| 4881 | } |
| 4882 | free(start); |
| 4883 | } else { /* single item */ |
| 4884 | for (k = 0; k < (*(struct lys_unique**)pp)->expr_size; k++) { |
| 4885 | lydict_remove(ctx, (*(struct lys_unique**)pp)->expr[k]); |
| 4886 | } |
| 4887 | free((*(struct lys_unique**)pp)->expr); |
| 4888 | free(*pp); |
| 4889 | } |
| 4890 | break; |
Radek Krejci | aa9c520 | 2017-02-15 16:10:14 +0100 | [diff] [blame] | 4891 | case LY_STMT_LENGTH: |
| 4892 | case LY_STMT_MUST: |
| 4893 | case LY_STMT_PATTERN: |
| 4894 | case LY_STMT_RANGE: |
| 4895 | EXTCOMPLEX_FREE_STRUCT(substmt[j].stmt, struct lys_restr, lys_restr_free, 1); |
| 4896 | break; |
Radek Krejci | c5cc530 | 2017-02-16 10:07:46 +0100 | [diff] [blame] | 4897 | case LY_STMT_WHEN: |
| 4898 | EXTCOMPLEX_FREE_STRUCT(LY_STMT_WHEN, struct lys_when, lys_when_free, 0); |
| 4899 | break; |
Radek Krejci | 7417a08 | 2017-02-16 11:07:59 +0100 | [diff] [blame] | 4900 | case LY_STMT_REVISION: |
| 4901 | pp = lys_ext_complex_get_substmt(LY_STMT_REVISION, (struct lys_ext_instance_complex *)e[i], NULL); |
| 4902 | if (!pp || !(*pp)) { |
| 4903 | break; |
| 4904 | } |
| 4905 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */ |
| 4906 | for (start = pp = *pp; *pp; pp++) { |
| 4907 | lydict_remove(ctx, (*(struct lys_revision**)pp)->dsc); |
| 4908 | lydict_remove(ctx, (*(struct lys_revision**)pp)->ref); |
| 4909 | lys_extension_instances_free(ctx, (*(struct lys_revision**)pp)->ext, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4910 | (*(struct lys_revision**)pp)->ext_size, private_destructor); |
Radek Krejci | 7417a08 | 2017-02-16 11:07:59 +0100 | [diff] [blame] | 4911 | free(*pp); |
| 4912 | } |
| 4913 | free(start); |
| 4914 | } else { /* single item */ |
| 4915 | lydict_remove(ctx, (*(struct lys_revision**)pp)->dsc); |
| 4916 | lydict_remove(ctx, (*(struct lys_revision**)pp)->ref); |
| 4917 | lys_extension_instances_free(ctx, (*(struct lys_revision**)pp)->ext, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4918 | (*(struct lys_revision**)pp)->ext_size, private_destructor); |
Radek Krejci | 7417a08 | 2017-02-16 11:07:59 +0100 | [diff] [blame] | 4919 | free(*pp); |
| 4920 | } |
| 4921 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4922 | default: |
Radek Krejci | b84686f | 2017-02-09 16:04:55 +0100 | [diff] [blame] | 4923 | /* nothing to free */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4924 | break; |
| 4925 | } |
| 4926 | } |
| 4927 | } |
| 4928 | |
| 4929 | free(e[i]); |
| 4930 | } |
| 4931 | free(e); |
| 4932 | |
| 4933 | #undef EXTCOMPLEX_FREE_STRUCT |
| 4934 | } |