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