Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 2 | * @file tree_schema.c |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 4 | * @brief Manipulation with libyang schema data structures |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 5 | * |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 7 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 13 | */ |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 14 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 16 | |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 17 | #ifdef __APPLE__ |
| 18 | # include <sys/param.h> |
| 19 | #endif |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 20 | #include <assert.h> |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 21 | #include <ctype.h> |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 22 | #include <limits.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <sys/mman.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 25 | #include <sys/types.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 26 | #include <sys/stat.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 27 | #include <fcntl.h> |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 28 | #include <string.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | #include <errno.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 31 | |
| 32 | #include "common.h" |
| 33 | #include "context.h" |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 34 | #include "parser.h" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 35 | #include "resolve.h" |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 36 | #include "xml.h" |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 37 | #include "xpath.h" |
Michal Vasko | fc5744d | 2015-10-22 12:09:34 +0200 | [diff] [blame] | 38 | #include "xml_internal.h" |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 39 | #include "tree_internal.h" |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 40 | #include "validation.h" |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 41 | #include "parser_yang.h" |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 42 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 43 | static int lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 44 | int in_grp, int shallow, struct unres_schema *unres); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 45 | |
Radek Krejci | 65aca41 | 2018-01-24 11:23:06 +0100 | [diff] [blame] | 46 | API const struct lys_node_list * |
| 47 | lys_is_key(const struct lys_node_leaf *node, uint8_t *index) |
| 48 | { |
| 49 | struct lys_node *parent = (struct lys_node *)node; |
| 50 | struct lys_node_list *list; |
| 51 | uint8_t i; |
| 52 | |
| 53 | if (!node || node->nodetype != LYS_LEAF) { |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | do { |
| 58 | parent = lys_parent(parent); |
| 59 | } while (parent && parent->nodetype == LYS_USES); |
| 60 | |
| 61 | if (!parent || parent->nodetype != LYS_LIST) { |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | list = (struct lys_node_list*)parent; |
| 66 | for (i = 0; i < list->keys_size; i++) { |
| 67 | if (list->keys[i] == node) { |
| 68 | if (index) { |
| 69 | (*index) = i; |
| 70 | } |
| 71 | return list; |
| 72 | } |
| 73 | } |
| 74 | return NULL; |
| 75 | } |
| 76 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 77 | API const struct lys_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 78 | lys_is_disabled(const struct lys_node *node, int recursive) |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 79 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 80 | int i; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 81 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 82 | if (!node) { |
| 83 | return NULL; |
| 84 | } |
| 85 | |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 86 | check: |
| 87 | if (node->nodetype != LYS_INPUT && node->nodetype != LYS_OUTPUT) { |
| 88 | /* input/output does not have if-feature, so skip them */ |
| 89 | |
| 90 | /* check local if-features */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 91 | for (i = 0; i < node->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 92 | if (!resolve_iffeature(&node->iffeature[i])) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 93 | return node; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if (!recursive) { |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | /* go through parents */ |
| 103 | if (node->nodetype == LYS_AUGMENT) { |
| 104 | /* go to parent actually means go to the target node */ |
| 105 | node = ((struct lys_node_augment *)node)->target; |
Michal Vasko | 1754919 | 2017-03-13 10:19:33 +0100 | [diff] [blame] | 106 | if (!node) { |
| 107 | /* unresolved augment, let's say it's enabled */ |
| 108 | return NULL; |
| 109 | } |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 110 | } else if (node->parent) { |
| 111 | node = node->parent; |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 112 | } else { |
| 113 | return NULL; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 116 | if (recursive == 2) { |
| 117 | /* continue only if the node cannot have a data instance */ |
| 118 | if (node->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST)) { |
| 119 | return NULL; |
| 120 | } |
| 121 | } |
| 122 | goto check; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 125 | int |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 126 | lys_get_sibling(const struct lys_node *siblings, const char *mod_name, int mod_name_len, const char *name, |
| 127 | int nam_len, LYS_NODE type, const struct lys_node **ret) |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 128 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 129 | const struct lys_node *node, *parent = NULL; |
| 130 | const struct lys_module *mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 131 | const char *node_mod_name; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 132 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 133 | assert(siblings && mod_name && name); |
Michal Vasko | 165dc4a | 2015-10-23 09:44:27 +0200 | [diff] [blame] | 134 | assert(!(type & (LYS_USES | LYS_GROUPING))); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 135 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 136 | /* fill the lengths in case the caller is so indifferent */ |
| 137 | if (!mod_name_len) { |
| 138 | mod_name_len = strlen(mod_name); |
| 139 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 140 | if (!nam_len) { |
| 141 | nam_len = strlen(name); |
| 142 | } |
| 143 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 144 | while (siblings && (siblings->nodetype == LYS_USES)) { |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 145 | siblings = siblings->child; |
| 146 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 147 | if (!siblings) { |
| 148 | /* unresolved uses */ |
| 149 | return EXIT_FAILURE; |
| 150 | } |
| 151 | |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 152 | if (siblings->nodetype == LYS_GROUPING) { |
| 153 | for (node = siblings; (node->nodetype == LYS_GROUPING) && (node->prev != siblings); node = node->prev); |
| 154 | if (node->nodetype == LYS_GROUPING) { |
| 155 | /* we went through all the siblings, only groupings there - no valid sibling */ |
| 156 | return EXIT_FAILURE; |
| 157 | } |
| 158 | /* update siblings to be valid */ |
| 159 | siblings = node; |
| 160 | } |
| 161 | |
Michal Vasko | 4cf7dba | 2016-10-14 09:42:01 +0200 | [diff] [blame] | 162 | /* set parent correctly */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 163 | parent = lys_parent(siblings); |
Michal Vasko | 4cf7dba | 2016-10-14 09:42:01 +0200 | [diff] [blame] | 164 | |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 165 | /* go up all uses */ |
| 166 | while (parent && (parent->nodetype == LYS_USES)) { |
| 167 | parent = lys_parent(parent); |
Michal Vasko | 4cf7dba | 2016-10-14 09:42:01 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 170 | if (!parent) { |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 171 | /* handle situation when there is a top-level uses referencing a foreign grouping */ |
| 172 | for (node = siblings; lys_parent(node) && (node->nodetype == LYS_USES); node = lys_parent(node)); |
| 173 | mod = lys_node_module(node); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 176 | /* try to find the node */ |
| 177 | node = NULL; |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 178 | while ((node = lys_getnext(node, parent, mod, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 179 | if (!type || (node->nodetype & type)) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 180 | /* module name comparison */ |
| 181 | node_mod_name = lys_node_module(node)->name; |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 182 | if (!ly_strequal(node_mod_name, mod_name, 1) && (strncmp(node_mod_name, mod_name, mod_name_len) || node_mod_name[mod_name_len])) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 183 | continue; |
| 184 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 185 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 186 | /* direct name check */ |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 187 | if (ly_strequal(node->name, name, 1) || (!strncmp(node->name, name, nam_len) && !node->name[nam_len])) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 188 | if (ret) { |
| 189 | *ret = node; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 190 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 191 | return EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 192 | } |
| 193 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | return EXIT_FAILURE; |
| 197 | } |
| 198 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 199 | int |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 200 | lys_getnext_data(const struct lys_module *mod, const struct lys_node *parent, const char *name, int nam_len, |
| 201 | LYS_NODE type, const struct lys_node **ret) |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 202 | { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 203 | const struct lys_node *node; |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 204 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 205 | assert((mod || parent) && name); |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 206 | assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))); |
| 207 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 208 | if (!mod) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 209 | mod = lys_node_module(parent); |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 210 | } |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 211 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 212 | /* try to find the node */ |
| 213 | node = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 214 | while ((node = lys_getnext(node, parent, mod, 0))) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 215 | if (!type || (node->nodetype & type)) { |
| 216 | /* module check */ |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 217 | if (lys_node_module(node) != lys_main_module(mod)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 218 | continue; |
| 219 | } |
| 220 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 221 | /* direct name check */ |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 222 | if (!strncmp(node->name, name, nam_len) && !node->name[nam_len]) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 223 | if (ret) { |
| 224 | *ret = node; |
| 225 | } |
| 226 | return EXIT_SUCCESS; |
| 227 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 228 | } |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return EXIT_FAILURE; |
| 232 | } |
| 233 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 234 | API const struct lys_node * |
| 235 | lys_getnext(const struct lys_node *last, const struct lys_node *parent, const struct lys_module *module, int options) |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 236 | { |
Michal Vasko | 5a9c24b | 2017-03-13 09:25:58 +0100 | [diff] [blame] | 237 | const struct lys_node *next, *aug_parent; |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 238 | struct lys_node **snode; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 239 | |
Michal Vasko | 75702cd | 2018-02-12 11:27:09 +0100 | [diff] [blame] | 240 | if ((!parent && !module) || (module && module->type) || (parent && (parent->nodetype == LYS_USES) && !(options & LYS_GETNEXT_PARENTUSES))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 241 | LOGARG; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 242 | return NULL; |
| 243 | } |
| 244 | |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 245 | if (!last) { |
| 246 | /* first call */ |
| 247 | |
| 248 | /* get know where to start */ |
| 249 | if (parent) { |
| 250 | /* schema subtree */ |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 251 | snode = lys_child(parent, LYS_UNKNOWN); |
Michal Vasko | 5a9c24b | 2017-03-13 09:25:58 +0100 | [diff] [blame] | 252 | /* do not return anything if the augment does not have any children */ |
Radek Krejci | bb08db3 | 2017-07-03 11:29:17 +0200 | [diff] [blame] | 253 | if (!snode || !(*snode) || ((parent->nodetype == LYS_AUGMENT) && ((*snode)->parent != parent))) { |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 254 | return NULL; |
| 255 | } |
| 256 | next = last = *snode; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 257 | } else { |
| 258 | /* top level data */ |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 259 | if (!(options & LYS_GETNEXT_NOSTATECHECK) && (module->disabled || !module->implemented)) { |
| 260 | /* nothing to return from a disabled/imported module */ |
| 261 | return NULL; |
| 262 | } |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 263 | next = last = module->data; |
| 264 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 265 | } else if ((last->nodetype == LYS_USES) && (options & LYS_GETNEXT_INTOUSES) && last->child) { |
| 266 | /* continue with uses content */ |
| 267 | next = last->child; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 268 | } else { |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 269 | /* continue after the last returned value */ |
| 270 | next = last->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | repeat: |
Michal Vasko | 5a9c24b | 2017-03-13 09:25:58 +0100 | [diff] [blame] | 274 | if (parent && (parent->nodetype == LYS_AUGMENT) && next) { |
| 275 | /* do not return anything outside the parent augment */ |
| 276 | aug_parent = next->parent; |
| 277 | do { |
| 278 | while (aug_parent && (aug_parent->nodetype != LYS_AUGMENT)) { |
| 279 | aug_parent = aug_parent->parent; |
| 280 | } |
| 281 | if (aug_parent) { |
| 282 | if (aug_parent == parent) { |
| 283 | break; |
| 284 | } |
| 285 | aug_parent = ((struct lys_node_augment *)aug_parent)->target; |
| 286 | } |
| 287 | |
| 288 | } while (aug_parent); |
| 289 | if (!aug_parent) { |
| 290 | return NULL; |
| 291 | } |
| 292 | } |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 293 | while (next && (next->nodetype == LYS_GROUPING)) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 294 | if (options & LYS_GETNEXT_WITHGROUPING) { |
| 295 | return next; |
| 296 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 297 | next = next->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 300 | if (!next) { /* cover case when parent is augment */ |
| 301 | if (!last || last->parent == parent || lys_parent(last) == parent) { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 302 | /* no next element */ |
| 303 | return NULL; |
| 304 | } |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 305 | last = lys_parent(last); |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 306 | next = last->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 307 | goto repeat; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 308 | } else { |
| 309 | last = next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 310 | } |
| 311 | |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 312 | if (!(options & LYS_GETNEXT_NOSTATECHECK) && lys_is_disabled(next, 0)) { |
| 313 | next = next->next; |
| 314 | goto repeat; |
| 315 | } |
| 316 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 317 | switch (next->nodetype) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 318 | case LYS_INPUT: |
| 319 | case LYS_OUTPUT: |
| 320 | if (options & LYS_GETNEXT_WITHINOUT) { |
| 321 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 322 | } else if (next->child) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 323 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 324 | } else { |
| 325 | next = next->next; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 326 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 327 | goto repeat; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 328 | |
Michal Vasko | a5835e9 | 2015-10-20 15:07:39 +0200 | [diff] [blame] | 329 | case LYS_CASE: |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 330 | if (options & LYS_GETNEXT_WITHCASE) { |
| 331 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 332 | } else if (next->child) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 333 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 334 | } else { |
| 335 | next = next->next; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 336 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 337 | goto repeat; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 338 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 339 | case LYS_USES: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 340 | /* go into */ |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 341 | if (options & LYS_GETNEXT_WITHUSES) { |
| 342 | return next; |
| 343 | } else if (next->child) { |
| 344 | next = next->child; |
| 345 | } else { |
| 346 | next = next->next; |
| 347 | } |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 348 | goto repeat; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 349 | |
Radek Krejci | dfcae7d | 2015-10-20 17:13:01 +0200 | [diff] [blame] | 350 | case LYS_RPC: |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 351 | case LYS_ACTION: |
Radek Krejci | dfcae7d | 2015-10-20 17:13:01 +0200 | [diff] [blame] | 352 | case LYS_NOTIF: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 353 | case LYS_LEAF: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 354 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 355 | case LYS_ANYDATA: |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 356 | case LYS_LIST: |
| 357 | case LYS_LEAFLIST: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 358 | return next; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 359 | |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 360 | case LYS_CONTAINER: |
| 361 | if (!((struct lys_node_container *)next)->presence && (options & LYS_GETNEXT_INTONPCONT)) { |
| 362 | if (next->child) { |
| 363 | /* go into */ |
| 364 | next = next->child; |
| 365 | } else { |
| 366 | next = next->next; |
| 367 | } |
| 368 | goto repeat; |
| 369 | } else { |
| 370 | return next; |
| 371 | } |
| 372 | |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 373 | case LYS_CHOICE: |
| 374 | if (options & LYS_GETNEXT_WITHCHOICE) { |
| 375 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 376 | } else if (next->child) { |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 377 | /* go into */ |
| 378 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 379 | } else { |
| 380 | next = next->next; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 381 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 382 | goto repeat; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 383 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 384 | default: |
| 385 | /* we should not be here */ |
| 386 | return NULL; |
| 387 | } |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 388 | } |
| 389 | |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 390 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 391 | lys_node_unlink(struct lys_node *node) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 392 | { |
Michal Vasko | f75b277 | 2018-03-14 09:55:33 +0100 | [diff] [blame] | 393 | struct lys_node *parent, *first, **pp = NULL; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 394 | struct lys_module *main_module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 395 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 396 | if (!node) { |
| 397 | return; |
| 398 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 399 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 400 | /* unlink from data model if necessary */ |
| 401 | if (node->module) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 402 | /* get main module with data tree */ |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 403 | main_module = lys_node_module(node); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 404 | if (main_module->data == node) { |
| 405 | main_module->data = node->next; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 406 | } |
| 407 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 408 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 409 | /* store pointers to important nodes */ |
| 410 | parent = node->parent; |
Michal Vasko | 3a9943b | 2015-09-23 11:33:50 +0200 | [diff] [blame] | 411 | if (parent && (parent->nodetype == LYS_AUGMENT)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 412 | /* handle augments - first, unlink it from the augment parent ... */ |
| 413 | if (parent->child == node) { |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 414 | parent->child = (node->next && node->next->parent == parent) ? node->next : NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 415 | } |
Radek Krejci | fec2e14 | 2017-01-05 15:19:03 +0100 | [diff] [blame] | 416 | |
| 417 | if (parent->flags & LYS_NOTAPPLIED) { |
| 418 | /* data are not connected in the target, so we cannot continue with the target as a parent */ |
| 419 | parent = NULL; |
| 420 | } else { |
| 421 | /* data are connected in target, so we will continue with the target as a parent */ |
| 422 | parent = ((struct lys_node_augment *)parent)->target; |
| 423 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 424 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 425 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 426 | /* unlink from parent */ |
| 427 | if (parent) { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 428 | if (parent->nodetype == LYS_EXT) { |
| 429 | pp = (struct lys_node **)lys_ext_complex_get_substmt(lys_snode2stmt(node->nodetype), |
| 430 | (struct lys_ext_instance_complex*)parent, NULL); |
| 431 | if (*pp == node) { |
| 432 | *pp = node->next; |
| 433 | } |
| 434 | } else if (parent->child == node) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 435 | parent->child = node->next; |
| 436 | } |
| 437 | node->parent = NULL; |
| 438 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 439 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 440 | /* unlink from siblings */ |
| 441 | if (node->prev == node) { |
| 442 | /* there are no more siblings */ |
| 443 | return; |
| 444 | } |
| 445 | if (node->next) { |
| 446 | node->next->prev = node->prev; |
| 447 | } else { |
| 448 | /* unlinking the last element */ |
| 449 | if (parent) { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 450 | if (parent->nodetype == LYS_EXT) { |
| 451 | first = *(struct lys_node **)pp; |
| 452 | } else { |
| 453 | first = parent->child; |
| 454 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 455 | } else { |
| 456 | first = node; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 457 | while (first->prev->next) { |
Michal Vasko | 276f96b | 2015-09-23 11:34:28 +0200 | [diff] [blame] | 458 | first = first->prev; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | first->prev = node->prev; |
| 462 | } |
| 463 | if (node->prev->next) { |
| 464 | node->prev->next = node->next; |
| 465 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 466 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 467 | /* clean up the unlinked element */ |
| 468 | node->next = NULL; |
| 469 | node->prev = node; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 472 | struct lys_node_grp * |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 473 | lys_find_grouping_up(const char *name, struct lys_node *start) |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 474 | { |
| 475 | struct lys_node *par_iter, *iter, *stop; |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 476 | |
| 477 | for (par_iter = start; par_iter; par_iter = par_iter->parent) { |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 478 | /* top-level augment, look into module (uses augment is handled correctly below) */ |
| 479 | if (par_iter->parent && !par_iter->parent->parent && (par_iter->parent->nodetype == LYS_AUGMENT)) { |
Radek Krejci | 115fa88 | 2017-03-01 16:15:07 +0100 | [diff] [blame] | 480 | par_iter = lys_main_module(par_iter->parent->module)->data; |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 481 | if (!par_iter) { |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 482 | break; |
| 483 | } |
| 484 | } |
| 485 | |
Michal Vasko | 38a0041 | 2018-03-14 10:01:50 +0100 | [diff] [blame] | 486 | if (par_iter->nodetype == LYS_EXT) { |
| 487 | /* we are in a top-level extension, search grouping in top-level groupings */ |
| 488 | par_iter = lys_main_module(par_iter->module)->data; |
| 489 | if (!par_iter) { |
| 490 | /* not connected yet, wait */ |
| 491 | return NULL; |
| 492 | } |
| 493 | } else if (par_iter->parent && (par_iter->parent->nodetype & (LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_USES))) { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 494 | continue; |
| 495 | } |
| 496 | |
| 497 | for (iter = par_iter, stop = NULL; iter; iter = iter->prev) { |
| 498 | if (!stop) { |
| 499 | stop = par_iter; |
| 500 | } else if (iter == stop) { |
| 501 | break; |
| 502 | } |
| 503 | if (iter->nodetype != LYS_GROUPING) { |
| 504 | continue; |
| 505 | } |
| 506 | |
Radek Krejci | f8426a7 | 2015-10-31 23:14:03 +0100 | [diff] [blame] | 507 | if (!strcmp(name, iter->name)) { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 508 | return (struct lys_node_grp *)iter; |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 513 | return NULL; |
| 514 | } |
| 515 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 516 | /* |
| 517 | * get next grouping in the root's subtree, in the |
| 518 | * first call, tha last is NULL |
| 519 | */ |
| 520 | static struct lys_node_grp * |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 521 | lys_get_next_grouping(struct lys_node_grp *lastgrp, struct lys_node *root) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 522 | { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 523 | struct lys_node *last = (struct lys_node *)lastgrp; |
| 524 | struct lys_node *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 525 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 526 | assert(root); |
| 527 | |
| 528 | if (!last) { |
| 529 | last = root; |
| 530 | } |
| 531 | |
| 532 | while (1) { |
| 533 | if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) { |
| 534 | next = last->child; |
| 535 | } else { |
| 536 | next = NULL; |
| 537 | } |
| 538 | if (!next) { |
| 539 | if (last == root) { |
| 540 | /* we are done */ |
| 541 | return NULL; |
| 542 | } |
| 543 | |
| 544 | /* no children, go to siblings */ |
| 545 | next = last->next; |
| 546 | } |
| 547 | while (!next) { |
| 548 | /* go back through parents */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 549 | if (lys_parent(last) == root) { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 550 | /* we are done */ |
| 551 | return NULL; |
| 552 | } |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 553 | next = last->next; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 554 | last = lys_parent(last); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | if (next->nodetype == LYS_GROUPING) { |
| 558 | return (struct lys_node_grp *)next; |
| 559 | } |
| 560 | |
| 561 | last = next; |
| 562 | } |
| 563 | } |
| 564 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 565 | /* logs directly */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 566 | int |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 567 | lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module) |
| 568 | { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 569 | struct lys_node *start, *stop, *iter; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 570 | struct lys_node_grp *grp; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 571 | int down, up; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 572 | |
| 573 | assert(node); |
| 574 | |
| 575 | if (!parent) { |
| 576 | assert(module); |
| 577 | } else { |
| 578 | module = parent->module; |
| 579 | } |
Radek Krejci | 115fa88 | 2017-03-01 16:15:07 +0100 | [diff] [blame] | 580 | module = lys_main_module(module); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 581 | |
| 582 | switch (node->nodetype) { |
| 583 | case LYS_GROUPING: |
| 584 | /* 6.2.1, rule 6 */ |
| 585 | if (parent) { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 586 | start = *lys_child(parent, LYS_GROUPING); |
| 587 | if (!start) { |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 588 | down = 0; |
| 589 | start = parent; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 590 | } else { |
| 591 | down = 1; |
| 592 | } |
| 593 | if (parent->nodetype == LYS_EXT) { |
| 594 | up = 0; |
| 595 | } else { |
| 596 | up = 1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 597 | } |
| 598 | } else { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 599 | down = up = 1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 600 | start = module->data; |
| 601 | } |
| 602 | /* go up */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 603 | if (up && lys_find_grouping_up(node->name, start)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 604 | LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name); |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 605 | return EXIT_FAILURE; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 606 | } |
| 607 | /* go down, because grouping can be defined after e.g. container in which is collision */ |
| 608 | if (down) { |
| 609 | for (iter = start, stop = NULL; iter; iter = iter->prev) { |
| 610 | if (!stop) { |
| 611 | stop = start; |
| 612 | } else if (iter == stop) { |
| 613 | break; |
| 614 | } |
| 615 | if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) { |
| 616 | continue; |
| 617 | } |
| 618 | |
| 619 | grp = NULL; |
| 620 | while ((grp = lys_get_next_grouping(grp, iter))) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 621 | if (ly_strequal(node->name, grp->name, 1)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 622 | LOGVAL(module->ctx, LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 623 | return EXIT_FAILURE; |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | break; |
| 629 | case LYS_LEAF: |
| 630 | case LYS_LEAFLIST: |
| 631 | case LYS_LIST: |
| 632 | case LYS_CONTAINER: |
| 633 | case LYS_CHOICE: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 634 | case LYS_ANYDATA: |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 635 | /* 6.2.1, rule 7 */ |
| 636 | if (parent) { |
| 637 | iter = parent; |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 638 | while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE | LYS_AUGMENT))) { |
| 639 | if (iter->nodetype == LYS_AUGMENT) { |
| 640 | if (((struct lys_node_augment *)iter)->target) { |
| 641 | /* augment is resolved, go up */ |
| 642 | iter = ((struct lys_node_augment *)iter)->target; |
| 643 | continue; |
| 644 | } |
| 645 | /* augment is not resolved, this is the final parent */ |
| 646 | break; |
| 647 | } |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 648 | iter = iter->parent; |
| 649 | } |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 650 | |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 651 | if (!iter) { |
| 652 | stop = NULL; |
| 653 | iter = module->data; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 654 | } else if (iter->nodetype == LYS_EXT) { |
| 655 | stop = iter; |
PavolVican | 9d61d40 | 2018-02-05 15:52:48 +0100 | [diff] [blame] | 656 | iter = (struct lys_node *)lys_child(iter, node->nodetype); |
| 657 | if (iter) { |
| 658 | iter = *(struct lys_node **)iter; |
| 659 | } |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 660 | } else { |
| 661 | stop = iter; |
| 662 | iter = iter->child; |
| 663 | } |
| 664 | } else { |
| 665 | stop = NULL; |
| 666 | iter = module->data; |
| 667 | } |
| 668 | while (iter) { |
| 669 | if (iter->nodetype & (LYS_USES | LYS_CASE)) { |
| 670 | iter = iter->child; |
| 671 | continue; |
| 672 | } |
| 673 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 674 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CONTAINER | LYS_CHOICE | LYS_ANYDATA)) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 675 | if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 676 | LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, strnodetype(node->nodetype), node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 677 | return EXIT_FAILURE; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | /* special case for choice - we must check the choice's name as |
| 682 | * well as the names of nodes under the choice |
| 683 | */ |
| 684 | if (iter->nodetype == LYS_CHOICE) { |
| 685 | iter = iter->child; |
| 686 | continue; |
| 687 | } |
| 688 | |
| 689 | /* go to siblings */ |
| 690 | if (!iter->next) { |
| 691 | /* no sibling, go to parent's sibling */ |
| 692 | do { |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 693 | /* for parent LYS_AUGMENT */ |
| 694 | if (iter->parent == stop) { |
| 695 | iter = stop; |
| 696 | break; |
| 697 | } |
| 698 | iter = lys_parent(iter); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 699 | if (iter && iter->next) { |
| 700 | break; |
| 701 | } |
| 702 | } while (iter != stop); |
| 703 | |
| 704 | if (iter == stop) { |
| 705 | break; |
| 706 | } |
| 707 | } |
| 708 | iter = iter->next; |
| 709 | } |
| 710 | break; |
| 711 | case LYS_CASE: |
| 712 | /* 6.2.1, rule 8 */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 713 | if (parent) { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 714 | start = *lys_child(parent, LYS_CASE); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 715 | } else { |
| 716 | start = module->data; |
| 717 | } |
| 718 | |
| 719 | LY_TREE_FOR(start, iter) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 720 | if (!(iter->nodetype & (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) { |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 721 | continue; |
| 722 | } |
| 723 | |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 724 | if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 725 | LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, "case", node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 726 | return EXIT_FAILURE; |
| 727 | } |
| 728 | } |
| 729 | break; |
| 730 | default: |
| 731 | /* no check needed */ |
| 732 | break; |
| 733 | } |
| 734 | |
| 735 | return EXIT_SUCCESS; |
| 736 | } |
| 737 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 738 | /* logs directly */ |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 739 | int |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 740 | lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child) |
| 741 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 742 | struct ly_ctx *ctx = child->module->ctx; |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 743 | struct lys_node *iter, **pchild; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 744 | struct lys_node_inout *in, *out, *inout; |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 745 | struct lys_node_case *c; |
| 746 | int type, shortcase = 0; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 747 | void *p; |
| 748 | struct lyext_substmt *info = NULL; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 749 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 750 | assert(child); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 751 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 752 | if (parent) { |
| 753 | type = parent->nodetype; |
| 754 | module = parent->module; |
| 755 | } else { |
| 756 | assert(module); |
Radek Krejci | fe3a138 | 2016-11-04 10:30:11 +0100 | [diff] [blame] | 757 | assert(!(child->nodetype & (LYS_INPUT | LYS_OUTPUT))); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 758 | type = 0; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 759 | } |
| 760 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 761 | /* checks */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 762 | switch (type) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 763 | case LYS_CONTAINER: |
| 764 | case LYS_LIST: |
| 765 | case LYS_GROUPING: |
Radek Krejci | 9693540 | 2016-11-04 16:27:28 +0100 | [diff] [blame] | 766 | case LYS_USES: |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 767 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 768 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF | |
Michal Vasko | b15cae2 | 2016-09-15 09:40:56 +0200 | [diff] [blame] | 769 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 770 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 771 | return EXIT_FAILURE; |
| 772 | } |
| 773 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 774 | case LYS_INPUT: |
| 775 | case LYS_OUTPUT: |
| 776 | case LYS_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 777 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 778 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 779 | LYS_LEAFLIST | LYS_LIST | LYS_USES))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 780 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 781 | return EXIT_FAILURE; |
| 782 | } |
| 783 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 784 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 785 | if (!(child->nodetype & |
Pavol Vican | 47a1b0a | 2016-09-20 10:18:24 +0200 | [diff] [blame] | 786 | (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 787 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 788 | return EXIT_FAILURE; |
| 789 | } |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 790 | if (child->nodetype != LYS_CASE) { |
| 791 | shortcase = 1; |
| 792 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 793 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 794 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 795 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 796 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_USES))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 797 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 798 | return EXIT_FAILURE; |
| 799 | } |
| 800 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 801 | case LYS_RPC: |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 802 | case LYS_ACTION: |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 803 | if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 804 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc"); |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 805 | return EXIT_FAILURE; |
| 806 | } |
| 807 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 808 | case LYS_LEAF: |
| 809 | case LYS_LEAFLIST: |
| 810 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 811 | case LYS_ANYDATA: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 812 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
| 813 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "The \"%s\" statement cannot have any data substatement.", |
Michal Vasko | 6ea3e36 | 2016-03-11 10:25:36 +0100 | [diff] [blame] | 814 | strnodetype(parent->nodetype)); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 815 | return EXIT_FAILURE; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 816 | case LYS_AUGMENT: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 817 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 818 | (LYS_ANYDATA | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 819 | | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 820 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 821 | return EXIT_FAILURE; |
| 822 | } |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 823 | break; |
| 824 | case LYS_UNKNOWN: |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 825 | /* top level */ |
| 826 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 827 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 828 | | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 829 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "(sub)module"); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 830 | return EXIT_FAILURE; |
| 831 | } |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 832 | break; |
| 833 | case LYS_EXT: |
| 834 | /* plugin-defined */ |
| 835 | p = lys_ext_complex_get_substmt(lys_snode2stmt(child->nodetype), (struct lys_ext_instance_complex*)parent, &info); |
| 836 | if (!p) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 837 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 838 | ((struct lys_ext_instance_complex*)parent)->def->name); |
| 839 | return EXIT_FAILURE; |
| 840 | } |
| 841 | /* TODO check cardinality */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 842 | break; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /* check identifier uniqueness */ |
Michal Vasko | 15a4337 | 2017-09-25 14:12:42 +0200 | [diff] [blame] | 846 | if (!(module->ctx->models.flags & LY_CTX_TRUSTED) && lys_check_id(child, parent, module)) { |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 847 | return EXIT_FAILURE; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 848 | } |
Radek Krejci | b7155b5 | 2015-06-10 17:03:01 +0200 | [diff] [blame] | 849 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 850 | if (child->parent) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 851 | lys_node_unlink(child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 852 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 853 | |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 854 | if ((child->nodetype & (LYS_INPUT | LYS_OUTPUT)) && parent->nodetype != LYS_EXT) { |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 855 | /* replace the implicit input/output node */ |
| 856 | if (child->nodetype == LYS_OUTPUT) { |
| 857 | inout = (struct lys_node_inout *)parent->child->next; |
| 858 | } else { /* LYS_INPUT */ |
| 859 | inout = (struct lys_node_inout *)parent->child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 860 | parent->child = child; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 861 | } |
| 862 | if (inout->next) { |
| 863 | child->next = inout->next; |
| 864 | inout->next->prev = child; |
| 865 | inout->next = NULL; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 866 | } else { |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 867 | parent->child->prev = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 868 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 869 | child->prev = inout->prev; |
| 870 | if (inout->prev->next) { |
| 871 | inout->prev->next = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 872 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 873 | inout->prev = (struct lys_node *)inout; |
| 874 | child->parent = parent; |
| 875 | inout->parent = NULL; |
| 876 | lys_node_free((struct lys_node *)inout, NULL, 0); |
| 877 | } else { |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 878 | if (shortcase) { |
| 879 | /* create the implicit case to allow it to serve as a target of the augments, |
| 880 | * it won't be printed, but it will be present in the tree */ |
| 881 | c = calloc(1, sizeof *c); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 882 | LY_CHECK_ERR_RETURN(!c, LOGMEM(ctx), EXIT_FAILURE); |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 883 | c->name = lydict_insert(module->ctx, child->name, 0); |
| 884 | c->flags = LYS_IMPLICIT; |
| 885 | c->module = module; |
| 886 | c->nodetype = LYS_CASE; |
| 887 | c->prev = (struct lys_node*)c; |
| 888 | lys_node_addchild(parent, module, (struct lys_node*)c); |
| 889 | parent = (struct lys_node*)c; |
| 890 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 891 | /* connect the child correctly */ |
| 892 | if (!parent) { |
| 893 | if (module->data) { |
| 894 | module->data->prev->next = child; |
| 895 | child->prev = module->data->prev; |
| 896 | module->data->prev = child; |
| 897 | } else { |
| 898 | module->data = child; |
| 899 | } |
| 900 | } else { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 901 | pchild = lys_child(parent, child->nodetype); |
| 902 | assert(pchild); |
| 903 | |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 904 | child->parent = parent; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 905 | if (!(*pchild)) { |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 906 | /* the only/first child of the parent */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 907 | *pchild = child; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 908 | iter = child; |
| 909 | } else { |
| 910 | /* add a new child at the end of parent's child list */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 911 | iter = (*pchild)->prev; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 912 | iter->next = child; |
| 913 | child->prev = iter; |
| 914 | } |
| 915 | while (iter->next) { |
| 916 | iter = iter->next; |
| 917 | iter->parent = parent; |
| 918 | } |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 919 | (*pchild)->prev = iter; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 920 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 921 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 922 | |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 923 | /* check config value (but ignore them in groupings and augments) */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 924 | for (iter = parent; iter && !(iter->nodetype & (LYS_GROUPING | LYS_AUGMENT | LYS_EXT)); iter = iter->parent); |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 925 | if (parent && !iter) { |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 926 | for (iter = child; iter && !(iter->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); iter = iter->parent); |
| 927 | if (!iter && (parent->flags & LYS_CONFIG_R) && (child->flags & LYS_CONFIG_W)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 928 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, child, "true", "config"); |
| 929 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children."); |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 930 | return EXIT_FAILURE; |
| 931 | } |
| 932 | } |
| 933 | |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 934 | /* propagate information about status data presence */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 935 | if ((child->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA)) && |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 936 | (child->flags & LYS_INCL_STATUS)) { |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 937 | for(iter = parent; iter; iter = lys_parent(iter)) { |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 938 | /* store it only into container or list - the only data inner nodes */ |
| 939 | if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
| 940 | if (iter->flags & LYS_INCL_STATUS) { |
| 941 | /* done, someone else set it already from here */ |
| 942 | break; |
| 943 | } |
| 944 | /* set flag about including status data */ |
| 945 | iter->flags |= LYS_INCL_STATUS; |
| 946 | } |
| 947 | } |
| 948 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 949 | |
| 950 | /* create implicit input/output nodes to have available them as possible target for augment */ |
Radek Krejci | 6025123 | 2017-08-24 17:13:08 +0200 | [diff] [blame] | 951 | if ((child->nodetype & (LYS_RPC | LYS_ACTION)) && !child->child) { |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 952 | in = calloc(1, sizeof *in); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 953 | out = calloc(1, sizeof *out); |
| 954 | if (!in || !out) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 955 | LOGMEM(ctx); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 956 | free(in); |
| 957 | free(out); |
| 958 | return EXIT_FAILURE; |
| 959 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 960 | in->nodetype = LYS_INPUT; |
| 961 | in->name = lydict_insert(child->module->ctx, "input", 5); |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 962 | out->nodetype = LYS_OUTPUT; |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 963 | out->name = lydict_insert(child->module->ctx, "output", 6); |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 964 | in->module = out->module = child->module; |
| 965 | in->parent = out->parent = child; |
| 966 | in->flags = out->flags = LYS_IMPLICIT; |
| 967 | in->next = (struct lys_node *)out; |
| 968 | in->prev = (struct lys_node *)out; |
| 969 | out->prev = (struct lys_node *)in; |
| 970 | child->child = (struct lys_node *)in; |
| 971 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 972 | return EXIT_SUCCESS; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 973 | } |
| 974 | |
Michal Vasko | 2924566 | 2017-04-18 15:56:31 +0200 | [diff] [blame] | 975 | const struct lys_module * |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 976 | lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const char *revision, int internal, int implement) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 977 | { |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 978 | char *enlarged_data = NULL; |
Radek Krejci | 0b5805d | 2015-08-13 09:38:02 +0200 | [diff] [blame] | 979 | struct lys_module *mod = NULL; |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 980 | unsigned int len; |
Radek Krejci | f347abc | 2016-06-22 10:18:47 +0200 | [diff] [blame] | 981 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 982 | if (!ctx || !data) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 983 | LOGARG; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 984 | return NULL; |
| 985 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 986 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 987 | if (!internal && format == LYS_IN_YANG) { |
| 988 | /* enlarge data by 2 bytes for flex */ |
| 989 | len = strlen(data); |
| 990 | enlarged_data = malloc((len + 2) * sizeof *enlarged_data); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 991 | LY_CHECK_ERR_RETURN(!enlarged_data, LOGMEM(ctx), NULL); |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 992 | memcpy(enlarged_data, data, len); |
| 993 | enlarged_data[len] = enlarged_data[len + 1] = '\0'; |
| 994 | data = enlarged_data; |
| 995 | } |
| 996 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 997 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 998 | case LYS_IN_YIN: |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 999 | mod = yin_read_module(ctx, data, revision, implement); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1000 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1001 | case LYS_IN_YANG: |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1002 | mod = yang_read_module(ctx, data, 0, revision, implement); |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1003 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1004 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1005 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1006 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1007 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1008 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1009 | free(enlarged_data); |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 1010 | |
| 1011 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 1012 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 1013 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 1014 | * the anotation definitions available in the internal schema structure. There is another hack in schema |
| 1015 | * printers to do not print this internally added annotation. */ |
| 1016 | if (mod && ly_strequal(mod->name, "ietf-netconf", 0)) { |
Radek Krejci | 5b19066 | 2017-04-13 08:56:14 +0200 | [diff] [blame] | 1017 | if (lyp_add_ietf_netconf_annotations(mod)) { |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 1018 | lys_free(mod, NULL, 1, 1); |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 1019 | return NULL; |
| 1020 | } |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 1021 | } |
| 1022 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1023 | return mod; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1026 | API const struct lys_module * |
| 1027 | lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format) |
| 1028 | { |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1029 | return lys_parse_mem_(ctx, data, format, NULL, 0, 1); |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1030 | } |
| 1031 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1032 | struct lys_submodule * |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1033 | lys_sub_parse_mem(struct lys_module *module, const char *data, LYS_INFORMAT format, struct unres_schema *unres) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1034 | { |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1035 | char *enlarged_data = NULL; |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1036 | struct lys_submodule *submod = NULL; |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1037 | unsigned int len; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1038 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1039 | assert(module); |
| 1040 | assert(data); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1041 | |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1042 | if (format == LYS_IN_YANG) { |
| 1043 | /* enlarge data by 2 bytes for flex */ |
| 1044 | len = strlen(data); |
| 1045 | enlarged_data = malloc((len + 2) * sizeof *enlarged_data); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1046 | LY_CHECK_ERR_RETURN(!enlarged_data, LOGMEM(module->ctx), NULL); |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1047 | memcpy(enlarged_data, data, len); |
| 1048 | enlarged_data[len] = enlarged_data[len + 1] = '\0'; |
| 1049 | data = enlarged_data; |
| 1050 | } |
| 1051 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1052 | /* get the main module */ |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 1053 | module = lys_main_module(module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1054 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1055 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1056 | case LYS_IN_YIN: |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1057 | submod = yin_read_submodule(module, data, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1058 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1059 | case LYS_IN_YANG: |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1060 | submod = yang_read_submodule(module, data, 0, unres); |
| 1061 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1062 | default: |
Radek Krejci | 90a550a | 2016-04-13 16:00:58 +0200 | [diff] [blame] | 1063 | assert(0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1064 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1065 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1066 | |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1067 | free(enlarged_data); |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1068 | return submod; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1069 | } |
| 1070 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1071 | API const struct lys_module * |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1072 | lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format) |
| 1073 | { |
| 1074 | int fd; |
| 1075 | const struct lys_module *ret; |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 1076 | const char *rev, *dot, *filename; |
| 1077 | size_t len; |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1078 | |
| 1079 | if (!ctx || !path) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1080 | LOGARG; |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1081 | return NULL; |
| 1082 | } |
| 1083 | |
| 1084 | fd = open(path, O_RDONLY); |
| 1085 | if (fd == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1086 | LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)); |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1087 | return NULL; |
| 1088 | } |
| 1089 | |
| 1090 | ret = lys_parse_fd(ctx, fd, format); |
| 1091 | close(fd); |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 1092 | |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 1093 | if (!ret) { |
| 1094 | /* error */ |
| 1095 | return NULL; |
| 1096 | } |
| 1097 | |
| 1098 | /* check that name and revision match filename */ |
| 1099 | filename = strrchr(path, '/'); |
| 1100 | if (!filename) { |
| 1101 | filename = path; |
| 1102 | } else { |
| 1103 | filename++; |
| 1104 | } |
| 1105 | rev = strchr(filename, '@'); |
| 1106 | dot = strrchr(filename, '.'); |
| 1107 | |
| 1108 | /* name */ |
| 1109 | len = strlen(ret->name); |
| 1110 | if (strncmp(filename, ret->name, len) || |
| 1111 | ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1112 | LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, ret->name); |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 1113 | } |
| 1114 | if (rev) { |
| 1115 | len = dot - ++rev; |
| 1116 | if (!ret->rev_size || len != 10 || strncmp(ret->rev[0].date, rev, len)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1117 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 1118 | ret->rev_size ? ret->rev[0].date : "none"); |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | if (!ret->filepath) { |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 1123 | /* store URI */ |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1124 | ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0); |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 1125 | } |
| 1126 | |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1127 | return ret; |
| 1128 | } |
| 1129 | |
| 1130 | API const struct lys_module * |
| 1131 | lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format) |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1132 | { |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1133 | return lys_parse_fd_(ctx, fd, format, NULL, 1); |
| 1134 | } |
| 1135 | |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1136 | static void |
| 1137 | lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd) |
| 1138 | { |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1139 | #ifdef __APPLE__ |
| 1140 | char path[MAXPATHLEN]; |
| 1141 | #else |
Michal Vasko | 25601f3 | 2018-02-16 09:41:46 +0100 | [diff] [blame] | 1142 | int len; |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1143 | char path[PATH_MAX], proc_path[32]; |
| 1144 | #endif |
| 1145 | |
| 1146 | #ifdef __APPLE__ |
| 1147 | if (fcntl(fd, F_GETPATH, path) != -1) { |
| 1148 | *filename = lydict_insert(ctx, path, 0); |
| 1149 | } |
| 1150 | #else |
| 1151 | /* get URI if there is /proc */ |
| 1152 | sprintf(proc_path, "/proc/self/fd/%d", fd); |
| 1153 | if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) { |
| 1154 | *filename = lydict_insert(ctx, path, len); |
| 1155 | } |
| 1156 | #endif |
| 1157 | } |
| 1158 | |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1159 | const struct lys_module * |
| 1160 | lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const char *revision, int implement) |
| 1161 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1162 | const struct lys_module *module; |
Radek Krejci | 0fb1150 | 2017-01-31 16:45:42 +0100 | [diff] [blame] | 1163 | size_t length; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1164 | char *addr; |
| 1165 | |
| 1166 | if (!ctx || fd < 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1167 | LOGARG; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1168 | return NULL; |
| 1169 | } |
| 1170 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1171 | if (lyp_mmap(ctx, fd, format == LYS_IN_YANG ? 1 : 0, &length, (void **)&addr)) { |
| 1172 | LOGERR(ctx, LY_ESYS, "Mapping file descriptor into memory failed (%s()).", __func__); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1173 | return NULL; |
Radek Krejci | 10c216a | 2017-02-01 10:36:00 +0100 | [diff] [blame] | 1174 | } else if (!addr) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1175 | LOGERR(ctx, LY_EINVAL, "Empty schema file."); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1176 | return NULL; |
| 1177 | } |
Radek Krejci | 0fb1150 | 2017-01-31 16:45:42 +0100 | [diff] [blame] | 1178 | |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1179 | module = lys_parse_mem_(ctx, addr, format, revision, 1, implement); |
Radek Krejci | 0fb1150 | 2017-01-31 16:45:42 +0100 | [diff] [blame] | 1180 | lyp_munmap(addr, length); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1181 | |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1182 | if (module && !module->filepath) { |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1183 | lys_parse_set_filename(ctx, (const char **)&module->filepath, fd); |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1184 | } |
| 1185 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1186 | return module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1187 | } |
| 1188 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1189 | struct lys_submodule * |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1190 | lys_sub_parse_fd(struct lys_module *module, int fd, LYS_INFORMAT format, struct unres_schema *unres) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1191 | { |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1192 | struct lys_submodule *submodule; |
Radek Krejci | 0fb1150 | 2017-01-31 16:45:42 +0100 | [diff] [blame] | 1193 | size_t length; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1194 | char *addr; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1195 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1196 | assert(module); |
| 1197 | assert(fd >= 0); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1198 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1199 | if (lyp_mmap(module->ctx, fd, format == LYS_IN_YANG ? 1 : 0, &length, (void **)&addr)) { |
| 1200 | LOGERR(module->ctx, LY_ESYS, "Mapping file descriptor into memory failed (%s()).", __func__); |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1201 | return NULL; |
Radek Krejci | 10c216a | 2017-02-01 10:36:00 +0100 | [diff] [blame] | 1202 | } else if (!addr) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1203 | LOGERR(module->ctx, LY_EINVAL, "Empty submodule schema file."); |
Michal Vasko | 2e7241e | 2016-02-15 16:06:34 +0100 | [diff] [blame] | 1204 | return NULL; |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1205 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1206 | |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1207 | /* get the main module */ |
| 1208 | module = lys_main_module(module); |
| 1209 | |
| 1210 | switch (format) { |
| 1211 | case LYS_IN_YIN: |
| 1212 | submodule = yin_read_submodule(module, addr, unres); |
| 1213 | break; |
| 1214 | case LYS_IN_YANG: |
| 1215 | submodule = yang_read_submodule(module, addr, 0, unres); |
| 1216 | break; |
| 1217 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1218 | LOGINT(module->ctx); |
Michal Vasko | 85d4152 | 2017-02-24 09:49:16 +0100 | [diff] [blame] | 1219 | return NULL; |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1220 | } |
| 1221 | |
Radek Krejci | c645a3a | 2017-01-31 16:59:00 +0100 | [diff] [blame] | 1222 | lyp_munmap(addr, length); |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1223 | |
| 1224 | if (submodule && !submodule->filepath) { |
| 1225 | lys_parse_set_filename(module->ctx, (const char **)&submodule->filepath, fd); |
| 1226 | } |
| 1227 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1228 | return submodule; |
| 1229 | |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1230 | } |
| 1231 | |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 1232 | int |
| 1233 | lys_ext_iter(struct lys_ext_instance **ext, uint8_t ext_size, uint8_t start, LYEXT_SUBSTMT substmt) |
| 1234 | { |
| 1235 | unsigned int u; |
| 1236 | |
| 1237 | for (u = start; u < ext_size; u++) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1238 | if (ext[u]->insubstmt == substmt) { |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 1239 | return u; |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | return -1; |
| 1244 | } |
| 1245 | |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1246 | /* |
| 1247 | * duplicate extension instance |
| 1248 | */ |
| 1249 | int |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1250 | lys_ext_dup(struct ly_ctx *ctx, struct lys_module *mod, struct lys_ext_instance **orig, uint8_t size, void *parent, |
| 1251 | LYEXT_PAR parent_type, struct lys_ext_instance ***new, int shallow, struct unres_schema *unres) |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1252 | { |
| 1253 | int i; |
| 1254 | uint8_t u = 0; |
| 1255 | struct lys_ext_instance **result; |
| 1256 | struct unres_ext *info, *info_orig; |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 1257 | size_t len; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1258 | |
| 1259 | assert(new); |
| 1260 | |
| 1261 | if (!size) { |
| 1262 | if (orig) { |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1263 | LOGINT(ctx); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1264 | return EXIT_FAILURE; |
| 1265 | } |
| 1266 | (*new) = NULL; |
| 1267 | return EXIT_SUCCESS; |
| 1268 | } |
| 1269 | |
| 1270 | (*new) = result = calloc(size, sizeof *result); |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1271 | LY_CHECK_ERR_RETURN(!result, LOGMEM(ctx), EXIT_FAILURE); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1272 | for (u = 0; u < size; u++) { |
| 1273 | if (orig[u]) { |
| 1274 | /* resolved extension instance, just duplicate it */ |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 1275 | switch(orig[u]->ext_type) { |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1276 | case LYEXT_FLAG: |
| 1277 | result[u] = malloc(sizeof(struct lys_ext_instance)); |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1278 | LY_CHECK_ERR_GOTO(!result[u], LOGMEM(ctx), error); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1279 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 1280 | case LYEXT_COMPLEX: |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 1281 | len = ((struct lyext_plugin_complex*)orig[u]->def->plugin)->instance_size; |
| 1282 | result[u] = calloc(1, len); |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1283 | LY_CHECK_ERR_GOTO(!result[u], LOGMEM(ctx), error); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1284 | |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1285 | ((struct lys_ext_instance_complex*)result[u])->substmt = ((struct lyext_plugin_complex*)orig[u]->def->plugin)->substmt; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 1286 | /* TODO duplicate data in extension instance content */ |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 1287 | memcpy((void*)result[u] + sizeof(**orig), (void*)orig[u] + sizeof(**orig), len - sizeof(**orig)); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 1288 | break; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1289 | } |
| 1290 | /* generic part */ |
| 1291 | result[u]->def = orig[u]->def; |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 1292 | result[u]->flags = LYEXT_OPT_CONTENT; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1293 | result[u]->arg_value = lydict_insert(ctx, orig[u]->arg_value, 0); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1294 | result[u]->parent = parent; |
| 1295 | result[u]->parent_type = parent_type; |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1296 | result[u]->insubstmt = orig[u]->insubstmt; |
| 1297 | result[u]->insubstmt_index = orig[u]->insubstmt_index; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 1298 | result[u]->ext_type = orig[u]->ext_type; |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 1299 | result[u]->priv = NULL; |
| 1300 | result[u]->nodetype = LYS_EXT; |
| 1301 | result[u]->module = mod; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1302 | |
| 1303 | /* extensions */ |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1304 | result[u]->ext_size = orig[u]->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1305 | if (lys_ext_dup(ctx, mod, orig[u]->ext, orig[u]->ext_size, result[u], |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1306 | LYEXT_PAR_EXTINST, &result[u]->ext, shallow, unres)) { |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1307 | goto error; |
| 1308 | } |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1309 | |
| 1310 | /* in case of shallow copy (duplication for deviation), duplicate only the link to private data |
| 1311 | * in a new copy, otherwise (grouping instantiation) do not duplicate the private data */ |
| 1312 | if (shallow) { |
| 1313 | result[u]->priv = orig[u]->priv; |
| 1314 | } |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1315 | } else { |
| 1316 | /* original extension is not yet resolved, so duplicate it in unres */ |
| 1317 | i = unres_schema_find(unres, -1, &orig, UNRES_EXT); |
| 1318 | if (i == -1) { |
| 1319 | /* extension not found in unres */ |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1320 | LOGINT(ctx); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1321 | goto error; |
| 1322 | } |
| 1323 | info_orig = unres->str_snode[i]; |
| 1324 | info = malloc(sizeof *info); |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1325 | LY_CHECK_ERR_GOTO(!info, LOGMEM(ctx), error); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1326 | info->datatype = info_orig->datatype; |
| 1327 | if (info->datatype == LYS_IN_YIN) { |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1328 | info->data.yin = lyxml_dup_elem(ctx, info_orig->data.yin, NULL, 1); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1329 | } /* else TODO YANG */ |
| 1330 | info->parent = parent; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 1331 | info->mod = mod; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1332 | info->parent_type = parent_type; |
| 1333 | info->ext_index = u; |
| 1334 | if (unres_schema_add_node(info->mod, unres, new, UNRES_EXT, (struct lys_node *)info) == -1) { |
| 1335 | goto error; |
| 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | return EXIT_SUCCESS; |
| 1341 | |
| 1342 | error: |
| 1343 | (*new) = NULL; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1344 | lys_extension_instances_free(ctx, result, u, NULL); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1345 | return EXIT_FAILURE; |
| 1346 | } |
| 1347 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1348 | static struct lys_restr * |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1349 | lys_restr_dup(struct lys_module *mod, struct lys_restr *old, int size, int shallow, struct unres_schema *unres) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1350 | { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1351 | struct lys_restr *result; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1352 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1353 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1354 | if (!size) { |
| 1355 | return NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1356 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1357 | |
| 1358 | result = calloc(size, sizeof *result); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1359 | LY_CHECK_ERR_RETURN(!result, LOGMEM(mod->ctx), NULL); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1360 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1361 | for (i = 0; i < size; i++) { |
Radek Krejci | 77f22b2 | 2017-01-17 15:23:03 +0100 | [diff] [blame] | 1362 | result[i].ext_size = old[i].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1363 | lys_ext_dup(mod->ctx, mod, old[i].ext, old[i].ext_size, &result[i], LYEXT_PAR_RESTR, &result[i].ext, shallow, unres); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 1364 | result[i].expr = lydict_insert(mod->ctx, old[i].expr, 0); |
| 1365 | result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0); |
| 1366 | result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0); |
| 1367 | result[i].eapptag = lydict_insert(mod->ctx, old[i].eapptag, 0); |
| 1368 | result[i].emsg = lydict_insert(mod->ctx, old[i].emsg, 0); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | return result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1372 | } |
| 1373 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1374 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1375 | lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr, |
| 1376 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1377 | { |
| 1378 | assert(ctx); |
| 1379 | if (!restr) { |
| 1380 | return; |
| 1381 | } |
| 1382 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1383 | lys_extension_instances_free(ctx, restr->ext, restr->ext_size, private_destructor); |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1384 | lydict_remove(ctx, restr->expr); |
| 1385 | lydict_remove(ctx, restr->dsc); |
| 1386 | lydict_remove(ctx, restr->ref); |
| 1387 | lydict_remove(ctx, restr->eapptag); |
| 1388 | lydict_remove(ctx, restr->emsg); |
| 1389 | } |
| 1390 | |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 1391 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1392 | lys_iffeature_free(struct ly_ctx *ctx, struct lys_iffeature *iffeature, uint8_t iffeature_size, |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 1393 | int shallow, void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 1394 | { |
| 1395 | uint8_t i; |
| 1396 | |
| 1397 | for (i = 0; i < iffeature_size; ++i) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1398 | lys_extension_instances_free(ctx, iffeature[i].ext, iffeature[i].ext_size, private_destructor); |
Michal Vasko | 15a4337 | 2017-09-25 14:12:42 +0200 | [diff] [blame] | 1399 | if (!shallow) { |
Frank Rimpler | 2a503f5 | 2017-09-12 15:21:18 +0000 | [diff] [blame] | 1400 | free(iffeature[i].expr); |
| 1401 | free(iffeature[i].features); |
| 1402 | } |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 1403 | } |
| 1404 | free(iffeature); |
| 1405 | } |
| 1406 | |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1407 | static int |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1408 | type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old, |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1409 | LY_DATA_TYPE base, int in_grp, int shallow, struct unres_schema *unres) |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1410 | { |
| 1411 | int i; |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1412 | unsigned int u; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1413 | |
| 1414 | switch (base) { |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1415 | case LY_TYPE_BINARY: |
| 1416 | if (old->info.binary.length) { |
| 1417 | new->info.binary.length = lys_restr_dup(mod, old->info.binary.length, 1, shallow, unres); |
| 1418 | } |
| 1419 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1420 | |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1421 | case LY_TYPE_BITS: |
| 1422 | new->info.bits.count = old->info.bits.count; |
| 1423 | if (new->info.bits.count) { |
| 1424 | new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1425 | LY_CHECK_ERR_RETURN(!new->info.bits.bit, LOGMEM(mod->ctx), -1); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1426 | |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1427 | for (u = 0; u < new->info.bits.count; u++) { |
| 1428 | new->info.bits.bit[u].name = lydict_insert(mod->ctx, old->info.bits.bit[u].name, 0); |
| 1429 | new->info.bits.bit[u].dsc = lydict_insert(mod->ctx, old->info.bits.bit[u].dsc, 0); |
| 1430 | new->info.bits.bit[u].ref = lydict_insert(mod->ctx, old->info.bits.bit[u].ref, 0); |
| 1431 | new->info.bits.bit[u].flags = old->info.bits.bit[u].flags; |
| 1432 | new->info.bits.bit[u].pos = old->info.bits.bit[u].pos; |
| 1433 | new->info.bits.bit[u].ext_size = old->info.bits.bit[u].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1434 | if (lys_ext_dup(mod->ctx, mod, old->info.bits.bit[u].ext, old->info.bits.bit[u].ext_size, |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1435 | &new->info.bits.bit[u], LYEXT_PAR_TYPE_BIT, |
| 1436 | &new->info.bits.bit[u].ext, shallow, unres)) { |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1437 | return -1; |
| 1438 | } |
| 1439 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1440 | } |
| 1441 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1442 | |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1443 | case LY_TYPE_DEC64: |
| 1444 | new->info.dec64.dig = old->info.dec64.dig; |
| 1445 | new->info.dec64.div = old->info.dec64.div; |
| 1446 | if (old->info.dec64.range) { |
| 1447 | new->info.dec64.range = lys_restr_dup(mod, old->info.dec64.range, 1, shallow, unres); |
| 1448 | } |
| 1449 | break; |
| 1450 | |
| 1451 | case LY_TYPE_ENUM: |
| 1452 | new->info.enums.count = old->info.enums.count; |
| 1453 | if (new->info.enums.count) { |
| 1454 | new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1455 | LY_CHECK_ERR_RETURN(!new->info.enums.enm, LOGMEM(mod->ctx), -1); |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1456 | |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1457 | for (u = 0; u < new->info.enums.count; u++) { |
| 1458 | new->info.enums.enm[u].name = lydict_insert(mod->ctx, old->info.enums.enm[u].name, 0); |
| 1459 | new->info.enums.enm[u].dsc = lydict_insert(mod->ctx, old->info.enums.enm[u].dsc, 0); |
| 1460 | new->info.enums.enm[u].ref = lydict_insert(mod->ctx, old->info.enums.enm[u].ref, 0); |
| 1461 | new->info.enums.enm[u].flags = old->info.enums.enm[u].flags; |
| 1462 | new->info.enums.enm[u].value = old->info.enums.enm[u].value; |
| 1463 | new->info.enums.enm[u].ext_size = old->info.enums.enm[u].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1464 | if (lys_ext_dup(mod->ctx, mod, old->info.enums.enm[u].ext, old->info.enums.enm[u].ext_size, |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1465 | &new->info.enums.enm[u], LYEXT_PAR_TYPE_ENUM, |
| 1466 | &new->info.enums.enm[u].ext, shallow, unres)) { |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1467 | return -1; |
| 1468 | } |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1469 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1470 | } |
| 1471 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1472 | |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1473 | case LY_TYPE_IDENT: |
| 1474 | new->info.ident.count = old->info.ident.count; |
| 1475 | if (old->info.ident.count) { |
| 1476 | new->info.ident.ref = malloc(old->info.ident.count * sizeof *new->info.ident.ref); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1477 | LY_CHECK_ERR_RETURN(!new->info.ident.ref, LOGMEM(mod->ctx), -1); |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1478 | memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref); |
| 1479 | } else { |
| 1480 | /* there can be several unresolved base identities, duplicate them all */ |
| 1481 | i = -1; |
| 1482 | do { |
| 1483 | i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF); |
| 1484 | if (i != -1) { |
| 1485 | if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i]) == -1) { |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1486 | return -1; |
| 1487 | } |
| 1488 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1489 | --i; |
| 1490 | } while (i > -1); |
| 1491 | } |
| 1492 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1493 | |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1494 | case LY_TYPE_INST: |
| 1495 | new->info.inst.req = old->info.inst.req; |
| 1496 | break; |
| 1497 | |
| 1498 | case LY_TYPE_INT8: |
| 1499 | case LY_TYPE_INT16: |
| 1500 | case LY_TYPE_INT32: |
| 1501 | case LY_TYPE_INT64: |
| 1502 | case LY_TYPE_UINT8: |
| 1503 | case LY_TYPE_UINT16: |
| 1504 | case LY_TYPE_UINT32: |
| 1505 | case LY_TYPE_UINT64: |
| 1506 | if (old->info.num.range) { |
| 1507 | new->info.num.range = lys_restr_dup(mod, old->info.num.range, 1, shallow, unres); |
| 1508 | } |
| 1509 | break; |
| 1510 | |
| 1511 | case LY_TYPE_LEAFREF: |
| 1512 | if (old->info.lref.path) { |
| 1513 | new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0); |
| 1514 | if (!in_grp && unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent) == -1) { |
| 1515 | return -1; |
| 1516 | } |
| 1517 | } |
| 1518 | break; |
| 1519 | |
| 1520 | case LY_TYPE_STRING: |
| 1521 | if (old->info.str.length) { |
| 1522 | new->info.str.length = lys_restr_dup(mod, old->info.str.length, 1, shallow, unres); |
| 1523 | } |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 1524 | if (old->info.str.pat_count) { |
| 1525 | new->info.str.patterns = lys_restr_dup(mod, old->info.str.patterns, old->info.str.pat_count, shallow, unres); |
| 1526 | new->info.str.pat_count = old->info.str.pat_count; |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 1527 | #ifdef LY_ENABLED_CACHE |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 1528 | if (!in_grp) { |
| 1529 | new->info.str.patterns_pcre = malloc(new->info.str.pat_count * 2 * sizeof *new->info.str.patterns_pcre); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1530 | LY_CHECK_ERR_RETURN(!new->info.str.patterns_pcre, LOGMEM(mod->ctx), -1); |
Radek Krejci | a4c107d | 2017-10-27 14:19:26 +0200 | [diff] [blame] | 1531 | for (u = 0; u < new->info.str.pat_count; u++) { |
Michal Vasko | a26db30 | 2018-02-14 15:22:10 +0100 | [diff] [blame] | 1532 | if (lyp_precompile_pattern(mod->ctx, &new->info.str.patterns[u].expr[1], |
Radek Krejci | a4c107d | 2017-10-27 14:19:26 +0200 | [diff] [blame] | 1533 | (pcre**)&new->info.str.patterns_pcre[2 * u], |
| 1534 | (pcre_extra**)&new->info.str.patterns_pcre[2 * u + 1])) { |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 1535 | free(new->info.str.patterns_pcre); |
| 1536 | new->info.str.patterns_pcre = NULL; |
| 1537 | return -1; |
| 1538 | } |
| 1539 | } |
| 1540 | } |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 1541 | #endif |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 1542 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1543 | break; |
| 1544 | |
| 1545 | case LY_TYPE_UNION: |
| 1546 | new->info.uni.has_ptr_type = old->info.uni.has_ptr_type; |
| 1547 | new->info.uni.count = old->info.uni.count; |
| 1548 | if (new->info.uni.count) { |
| 1549 | new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1550 | LY_CHECK_ERR_RETURN(!new->info.uni.types, LOGMEM(mod->ctx), -1); |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1551 | |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1552 | for (u = 0; u < new->info.uni.count; u++) { |
| 1553 | if (lys_type_dup(mod, parent, &(new->info.uni.types[u]), &(old->info.uni.types[u]), in_grp, |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1554 | shallow, unres)) { |
| 1555 | return -1; |
| 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | break; |
| 1560 | |
| 1561 | default: |
| 1562 | /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */ |
| 1563 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1564 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1565 | |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1566 | return EXIT_SUCCESS; |
| 1567 | } |
| 1568 | |
| 1569 | struct yang_type * |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1570 | lys_yang_type_dup(struct lys_module *module, struct lys_node *parent, struct yang_type *old, struct lys_type *type, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1571 | int in_grp, int shallow, struct unres_schema *unres) |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1572 | { |
| 1573 | struct yang_type *new; |
| 1574 | |
| 1575 | new = calloc(1, sizeof *new); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1576 | LY_CHECK_ERR_RETURN(!new, LOGMEM(module->ctx), NULL); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1577 | new->flags = old->flags; |
| 1578 | new->base = old->base; |
Pavol Vican | 6658629 | 2016-04-07 11:38:52 +0200 | [diff] [blame] | 1579 | new->name = lydict_insert(module->ctx, old->name, 0); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1580 | new->type = type; |
| 1581 | if (!new->name) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1582 | LOGMEM(module->ctx); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1583 | goto error; |
| 1584 | } |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1585 | if (type_dup(module, parent, type, old->type, new->base, in_grp, shallow, unres)) { |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1586 | new->type->base = new->base; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1587 | lys_type_free(module->ctx, new->type, NULL); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1588 | memset(&new->type->info, 0, sizeof new->type->info); |
| 1589 | goto error; |
| 1590 | } |
| 1591 | return new; |
| 1592 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1593 | error: |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1594 | free(new); |
| 1595 | return NULL; |
| 1596 | } |
| 1597 | |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1598 | int |
| 1599 | lys_copy_union_leafrefs(struct lys_module *mod, struct lys_node *parent, struct lys_type *type, struct lys_type *prev_new, |
| 1600 | struct unres_schema *unres) |
| 1601 | { |
| 1602 | struct lys_type new; |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1603 | unsigned int i, top_type; |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1604 | struct lys_ext_instance **ext; |
| 1605 | uint8_t ext_size; |
| 1606 | void *reloc; |
| 1607 | |
| 1608 | if (!prev_new) { |
| 1609 | /* this is the "top-level" type, meaning it is a real type and no typedef directly above */ |
| 1610 | top_type = 1; |
| 1611 | |
| 1612 | memset(&new, 0, sizeof new); |
| 1613 | |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1614 | new.base = type->base; |
| 1615 | new.parent = (struct lys_tpdf *)parent; |
| 1616 | |
| 1617 | prev_new = &new; |
| 1618 | } else { |
| 1619 | /* this is not top-level type, just a type of a typedef */ |
| 1620 | top_type = 0; |
| 1621 | } |
| 1622 | |
Radek Krejci | 9c5cb6d | 2017-08-09 11:15:23 +0200 | [diff] [blame] | 1623 | assert(type->der); |
| 1624 | if (type->der->module) { |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1625 | /* typedef, skip it, but keep the extensions */ |
| 1626 | ext_size = type->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1627 | if (lys_ext_dup(mod->ctx, mod, type->ext, type->ext_size, prev_new, LYEXT_PAR_TYPE, &ext, 0, unres)) { |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1628 | return -1; |
| 1629 | } |
| 1630 | if (prev_new->ext) { |
| 1631 | reloc = realloc(prev_new->ext, (prev_new->ext_size + ext_size) * sizeof *prev_new->ext); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1632 | LY_CHECK_ERR_RETURN(!reloc, LOGMEM(mod->ctx), -1); |
Radek Krejci | 70379e2 | 2017-08-09 11:21:07 +0200 | [diff] [blame] | 1633 | prev_new->ext = reloc; |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1634 | |
| 1635 | memcpy(prev_new->ext + prev_new->ext_size, ext, ext_size * sizeof *ext); |
| 1636 | free(ext); |
| 1637 | |
| 1638 | prev_new->ext_size += ext_size; |
| 1639 | } else { |
| 1640 | prev_new->ext = ext; |
| 1641 | prev_new->ext_size = ext_size; |
| 1642 | } |
| 1643 | |
| 1644 | if (lys_copy_union_leafrefs(mod, parent, &type->der->type, prev_new, unres)) { |
| 1645 | return -1; |
| 1646 | } |
| 1647 | } else { |
| 1648 | /* type, just make a deep copy */ |
| 1649 | switch (type->base) { |
| 1650 | case LY_TYPE_UNION: |
| 1651 | prev_new->info.uni.has_ptr_type = type->info.uni.has_ptr_type; |
| 1652 | prev_new->info.uni.count = type->info.uni.count; |
| 1653 | /* this cannot be a typedef anymore */ |
| 1654 | assert(prev_new->info.uni.count); |
| 1655 | |
| 1656 | prev_new->info.uni.types = calloc(prev_new->info.uni.count, sizeof *prev_new->info.uni.types); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1657 | LY_CHECK_ERR_RETURN(!prev_new->info.uni.types, LOGMEM(mod->ctx), -1); |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1658 | |
| 1659 | for (i = 0; i < prev_new->info.uni.count; i++) { |
| 1660 | if (lys_copy_union_leafrefs(mod, parent, &(type->info.uni.types[i]), &(prev_new->info.uni.types[i]), unres)) { |
| 1661 | return -1; |
| 1662 | } |
| 1663 | } |
| 1664 | |
| 1665 | prev_new->der = type->der; |
| 1666 | break; |
| 1667 | default: |
| 1668 | if (lys_type_dup(mod, parent, prev_new, type, 0, 0, unres)) { |
| 1669 | return -1; |
| 1670 | } |
| 1671 | break; |
| 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | if (top_type) { |
| 1676 | memcpy(type, prev_new, sizeof *type); |
| 1677 | } |
| 1678 | return EXIT_SUCCESS; |
| 1679 | } |
| 1680 | |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1681 | API const void * |
| 1682 | lys_ext_instance_substmt(const struct lys_ext_instance *ext) |
| 1683 | { |
| 1684 | if (!ext) { |
| 1685 | return NULL; |
| 1686 | } |
| 1687 | |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1688 | switch (ext->insubstmt) { |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1689 | case LYEXT_SUBSTMT_SELF: |
| 1690 | case LYEXT_SUBSTMT_MODIFIER: |
| 1691 | case LYEXT_SUBSTMT_VERSION: |
| 1692 | return NULL; |
| 1693 | case LYEXT_SUBSTMT_ARGUMENT: |
| 1694 | if (ext->parent_type == LYEXT_PAR_EXT) { |
| 1695 | return ((struct lys_ext_instance*)ext->parent)->arg_value; |
| 1696 | } |
| 1697 | break; |
| 1698 | case LYEXT_SUBSTMT_BASE: |
| 1699 | if (ext->parent_type == LYEXT_PAR_TYPE) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1700 | return ((struct lys_type*)ext->parent)->info.ident.ref[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1701 | } else if (ext->parent_type == LYEXT_PAR_IDENT) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1702 | return ((struct lys_ident*)ext->parent)->base[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1703 | } |
| 1704 | break; |
| 1705 | case LYEXT_SUBSTMT_BELONGSTO: |
| 1706 | if (ext->parent_type == LYEXT_PAR_MODULE && ((struct lys_module*)ext->parent)->type) { |
| 1707 | return ((struct lys_submodule*)ext->parent)->belongsto; |
| 1708 | } |
| 1709 | break; |
| 1710 | case LYEXT_SUBSTMT_CONFIG: |
| 1711 | case LYEXT_SUBSTMT_MANDATORY: |
| 1712 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 1713 | return &((struct lys_node*)ext->parent)->flags; |
| 1714 | } else if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
| 1715 | return &((struct lys_deviate*)ext->parent)->flags; |
| 1716 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 1717 | return &((struct lys_refine*)ext->parent)->flags; |
| 1718 | } |
| 1719 | break; |
| 1720 | case LYEXT_SUBSTMT_CONTACT: |
| 1721 | if (ext->parent_type == LYEXT_PAR_MODULE) { |
| 1722 | return ((struct lys_module*)ext->parent)->contact; |
| 1723 | } |
| 1724 | break; |
| 1725 | case LYEXT_SUBSTMT_DEFAULT: |
| 1726 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 1727 | switch (((struct lys_node*)ext->parent)->nodetype) { |
| 1728 | case LYS_LEAF: |
| 1729 | case LYS_LEAFLIST: |
| 1730 | /* in case of leaf, the index is supposed to be 0, so it will return the |
| 1731 | * correct pointer despite the leaf structure does not have dflt as array */ |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1732 | return ((struct lys_node_leaflist*)ext->parent)->dflt[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1733 | case LYS_CHOICE: |
| 1734 | return ((struct lys_node_choice*)ext->parent)->dflt; |
| 1735 | default: |
| 1736 | /* internal error */ |
| 1737 | break; |
| 1738 | } |
| 1739 | } else if (ext->parent_type == LYEXT_PAR_TPDF) { |
| 1740 | return ((struct lys_tpdf*)ext->parent)->dflt; |
| 1741 | } else if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1742 | return ((struct lys_deviate*)ext->parent)->dflt[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1743 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1744 | return &((struct lys_refine*)ext->parent)->dflt[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1745 | } |
| 1746 | break; |
| 1747 | case LYEXT_SUBSTMT_DESCRIPTION: |
| 1748 | switch (ext->parent_type) { |
| 1749 | case LYEXT_PAR_NODE: |
| 1750 | return ((struct lys_node*)ext->parent)->dsc; |
| 1751 | case LYEXT_PAR_MODULE: |
| 1752 | return ((struct lys_module*)ext->parent)->dsc; |
| 1753 | case LYEXT_PAR_IMPORT: |
| 1754 | return ((struct lys_import*)ext->parent)->dsc; |
| 1755 | case LYEXT_PAR_INCLUDE: |
| 1756 | return ((struct lys_include*)ext->parent)->dsc; |
| 1757 | case LYEXT_PAR_EXT: |
| 1758 | return ((struct lys_ext*)ext->parent)->dsc; |
| 1759 | case LYEXT_PAR_FEATURE: |
| 1760 | return ((struct lys_feature*)ext->parent)->dsc; |
| 1761 | case LYEXT_PAR_TPDF: |
| 1762 | return ((struct lys_tpdf*)ext->parent)->dsc; |
| 1763 | case LYEXT_PAR_TYPE_BIT: |
| 1764 | return ((struct lys_type_bit*)ext->parent)->dsc; |
| 1765 | case LYEXT_PAR_TYPE_ENUM: |
| 1766 | return ((struct lys_type_enum*)ext->parent)->dsc; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1767 | case LYEXT_PAR_RESTR: |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1768 | return ((struct lys_restr*)ext->parent)->dsc; |
| 1769 | case LYEXT_PAR_WHEN: |
| 1770 | return ((struct lys_when*)ext->parent)->dsc; |
| 1771 | case LYEXT_PAR_IDENT: |
| 1772 | return ((struct lys_ident*)ext->parent)->dsc; |
| 1773 | case LYEXT_PAR_DEVIATION: |
| 1774 | return ((struct lys_deviation*)ext->parent)->dsc; |
| 1775 | case LYEXT_PAR_REVISION: |
| 1776 | return ((struct lys_revision*)ext->parent)->dsc; |
| 1777 | case LYEXT_PAR_REFINE: |
| 1778 | return ((struct lys_refine*)ext->parent)->dsc; |
| 1779 | default: |
| 1780 | break; |
| 1781 | } |
| 1782 | break; |
| 1783 | case LYEXT_SUBSTMT_ERRTAG: |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1784 | if (ext->parent_type == LYEXT_PAR_RESTR) { |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1785 | return ((struct lys_restr*)ext->parent)->eapptag; |
| 1786 | } |
| 1787 | break; |
| 1788 | case LYEXT_SUBSTMT_ERRMSG: |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1789 | if (ext->parent_type == LYEXT_PAR_RESTR) { |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1790 | return ((struct lys_restr*)ext->parent)->emsg; |
| 1791 | } |
| 1792 | break; |
| 1793 | case LYEXT_SUBSTMT_DIGITS: |
| 1794 | if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_DEC64) { |
| 1795 | return &((struct lys_type*)ext->parent)->info.dec64.dig; |
| 1796 | } |
| 1797 | break; |
| 1798 | case LYEXT_SUBSTMT_KEY: |
| 1799 | if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) { |
| 1800 | return ((struct lys_node_list*)ext->parent)->keys; |
| 1801 | } |
| 1802 | break; |
| 1803 | case LYEXT_SUBSTMT_MAX: |
| 1804 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 1805 | if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) { |
| 1806 | return &((struct lys_node_list*)ext->parent)->max; |
| 1807 | } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) { |
| 1808 | return &((struct lys_node_leaflist*)ext->parent)->max; |
| 1809 | } |
| 1810 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 1811 | return &((struct lys_refine*)ext->parent)->mod.list.max; |
| 1812 | } |
| 1813 | break; |
| 1814 | case LYEXT_SUBSTMT_MIN: |
| 1815 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 1816 | if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) { |
| 1817 | return &((struct lys_node_list*)ext->parent)->min; |
| 1818 | } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) { |
| 1819 | return &((struct lys_node_leaflist*)ext->parent)->min; |
| 1820 | } |
| 1821 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 1822 | return &((struct lys_refine*)ext->parent)->mod.list.min; |
| 1823 | } |
| 1824 | break; |
| 1825 | case LYEXT_SUBSTMT_NAMESPACE: |
| 1826 | if (ext->parent_type == LYEXT_PAR_MODULE && !((struct lys_module*)ext->parent)->type) { |
| 1827 | return ((struct lys_module*)ext->parent)->ns; |
| 1828 | } |
| 1829 | break; |
| 1830 | case LYEXT_SUBSTMT_ORDEREDBY: |
| 1831 | if (ext->parent_type == LYEXT_PAR_NODE && |
| 1832 | (((struct lys_node*)ext->parent)->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 1833 | return &((struct lys_node_list*)ext->parent)->flags; |
| 1834 | } |
| 1835 | break; |
| 1836 | case LYEXT_SUBSTMT_ORGANIZATION: |
| 1837 | if (ext->parent_type == LYEXT_PAR_MODULE) { |
| 1838 | return ((struct lys_module*)ext->parent)->org; |
| 1839 | } |
| 1840 | break; |
| 1841 | case LYEXT_SUBSTMT_PATH: |
| 1842 | if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) { |
| 1843 | return ((struct lys_type*)ext->parent)->info.lref.path; |
| 1844 | } |
| 1845 | break; |
| 1846 | case LYEXT_SUBSTMT_POSITION: |
| 1847 | if (ext->parent_type == LYEXT_PAR_TYPE_BIT) { |
| 1848 | return &((struct lys_type_bit*)ext->parent)->pos; |
| 1849 | } |
| 1850 | break; |
| 1851 | case LYEXT_SUBSTMT_PREFIX: |
| 1852 | if (ext->parent_type == LYEXT_PAR_MODULE) { |
| 1853 | /* covers also lys_submodule */ |
| 1854 | return ((struct lys_module*)ext->parent)->prefix; |
| 1855 | } else if (ext->parent_type == LYEXT_PAR_IMPORT) { |
| 1856 | return ((struct lys_import*)ext->parent)->prefix; |
| 1857 | } |
| 1858 | break; |
| 1859 | case LYEXT_SUBSTMT_PRESENCE: |
| 1860 | if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_CONTAINER) { |
| 1861 | return ((struct lys_node_container*)ext->parent)->presence; |
| 1862 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 1863 | return ((struct lys_refine*)ext->parent)->mod.presence; |
| 1864 | } |
| 1865 | break; |
| 1866 | case LYEXT_SUBSTMT_REFERENCE: |
| 1867 | switch (ext->parent_type) { |
| 1868 | case LYEXT_PAR_NODE: |
| 1869 | return ((struct lys_node*)ext->parent)->ref; |
| 1870 | case LYEXT_PAR_MODULE: |
| 1871 | return ((struct lys_module*)ext->parent)->ref; |
| 1872 | case LYEXT_PAR_IMPORT: |
| 1873 | return ((struct lys_import*)ext->parent)->ref; |
| 1874 | case LYEXT_PAR_INCLUDE: |
| 1875 | return ((struct lys_include*)ext->parent)->ref; |
| 1876 | case LYEXT_PAR_EXT: |
| 1877 | return ((struct lys_ext*)ext->parent)->ref; |
| 1878 | case LYEXT_PAR_FEATURE: |
| 1879 | return ((struct lys_feature*)ext->parent)->ref; |
| 1880 | case LYEXT_PAR_TPDF: |
| 1881 | return ((struct lys_tpdf*)ext->parent)->ref; |
| 1882 | case LYEXT_PAR_TYPE_BIT: |
| 1883 | return ((struct lys_type_bit*)ext->parent)->ref; |
| 1884 | case LYEXT_PAR_TYPE_ENUM: |
| 1885 | return ((struct lys_type_enum*)ext->parent)->ref; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1886 | case LYEXT_PAR_RESTR: |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1887 | return ((struct lys_restr*)ext->parent)->ref; |
| 1888 | case LYEXT_PAR_WHEN: |
| 1889 | return ((struct lys_when*)ext->parent)->ref; |
| 1890 | case LYEXT_PAR_IDENT: |
| 1891 | return ((struct lys_ident*)ext->parent)->ref; |
| 1892 | case LYEXT_PAR_DEVIATION: |
| 1893 | return ((struct lys_deviation*)ext->parent)->ref; |
| 1894 | case LYEXT_PAR_REVISION: |
| 1895 | return ((struct lys_revision*)ext->parent)->ref; |
| 1896 | case LYEXT_PAR_REFINE: |
| 1897 | return ((struct lys_refine*)ext->parent)->ref; |
| 1898 | default: |
| 1899 | break; |
| 1900 | } |
| 1901 | break; |
Radek Krejci | be33639 | 2017-02-07 10:54:24 +0100 | [diff] [blame] | 1902 | case LYEXT_SUBSTMT_REQINSTANCE: |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1903 | if (ext->parent_type == LYEXT_PAR_TYPE) { |
| 1904 | if (((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) { |
| 1905 | return &((struct lys_type*)ext->parent)->info.lref.req; |
| 1906 | } else if (((struct lys_type*)ext->parent)->base == LY_TYPE_INST) { |
| 1907 | return &((struct lys_type*)ext->parent)->info.inst.req; |
| 1908 | } |
| 1909 | } |
| 1910 | break; |
| 1911 | case LYEXT_SUBSTMT_REVISIONDATE: |
| 1912 | if (ext->parent_type == LYEXT_PAR_IMPORT) { |
| 1913 | return ((struct lys_import*)ext->parent)->rev; |
| 1914 | } else if (ext->parent_type == LYEXT_PAR_INCLUDE) { |
| 1915 | return ((struct lys_include*)ext->parent)->rev; |
| 1916 | } |
| 1917 | break; |
| 1918 | case LYEXT_SUBSTMT_STATUS: |
| 1919 | switch (ext->parent_type) { |
| 1920 | case LYEXT_PAR_NODE: |
| 1921 | case LYEXT_PAR_IDENT: |
| 1922 | case LYEXT_PAR_TPDF: |
| 1923 | case LYEXT_PAR_EXT: |
| 1924 | case LYEXT_PAR_FEATURE: |
| 1925 | case LYEXT_PAR_TYPE_ENUM: |
| 1926 | case LYEXT_PAR_TYPE_BIT: |
| 1927 | /* in all structures the flags member is at the same offset */ |
| 1928 | return &((struct lys_node*)ext->parent)->flags; |
| 1929 | default: |
| 1930 | break; |
| 1931 | } |
| 1932 | break; |
| 1933 | case LYEXT_SUBSTMT_UNIQUE: |
| 1934 | if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1935 | return &((struct lys_deviate*)ext->parent)->unique[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1936 | } else if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1937 | return &((struct lys_node_list*)ext->parent)->unique[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1938 | } |
| 1939 | break; |
| 1940 | case LYEXT_SUBSTMT_UNITS: |
| 1941 | if (ext->parent_type == LYEXT_PAR_NODE && |
| 1942 | (((struct lys_node*)ext->parent)->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 1943 | /* units is at the same offset in both lys_node_leaf and lys_node_leaflist */ |
| 1944 | return ((struct lys_node_leaf*)ext->parent)->units; |
| 1945 | } else if (ext->parent_type == LYEXT_PAR_TPDF) { |
| 1946 | return ((struct lys_tpdf*)ext->parent)->units; |
| 1947 | } else if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
| 1948 | return ((struct lys_deviate*)ext->parent)->units; |
| 1949 | } |
| 1950 | break; |
| 1951 | case LYEXT_SUBSTMT_VALUE: |
| 1952 | if (ext->parent_type == LYEXT_PAR_TYPE_ENUM) { |
| 1953 | return &((struct lys_type_enum*)ext->parent)->value; |
| 1954 | } |
| 1955 | break; |
| 1956 | case LYEXT_SUBSTMT_YINELEM: |
| 1957 | if (ext->parent_type == LYEXT_PAR_EXT) { |
| 1958 | return &((struct lys_ext*)ext->parent)->flags; |
| 1959 | } |
| 1960 | break; |
| 1961 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1962 | LOGINT(ext->module->ctx); |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1963 | return NULL; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1964 | } |
| 1965 | |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1966 | static int |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1967 | lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1968 | int in_grp, int shallow, struct unres_schema *unres) |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1969 | { |
| 1970 | int i; |
| 1971 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1972 | new->base = old->base; |
| 1973 | new->der = old->der; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1974 | new->parent = (struct lys_tpdf *)parent; |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 1975 | new->ext_size = old->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1976 | if (lys_ext_dup(mod->ctx, mod, old->ext, old->ext_size, new, LYEXT_PAR_TYPE, &new->ext, shallow, unres)) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 1977 | return -1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 1978 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1979 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 1980 | i = unres_schema_find(unres, -1, old, UNRES_TYPE_DER); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1981 | if (i != -1) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 1982 | /* HACK (serious one) for unres */ |
| 1983 | /* nothing else we can do but duplicate it immediately */ |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1984 | if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1985 | new->der = (struct lys_tpdf *)lys_yang_type_dup(mod, parent, (struct yang_type *)old->der, new, in_grp, |
| 1986 | shallow, unres); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1987 | } else { |
| 1988 | new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1); |
| 1989 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1990 | /* all these unres additions can fail even though they did not before */ |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 1991 | if (!new->der || (unres_schema_add_node(mod, unres, new, UNRES_TYPE_DER, parent) == -1)) { |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1992 | return -1; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 1993 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1994 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1995 | } |
| 1996 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1997 | return type_dup(mod, parent, new, old, new->base, in_grp, shallow, unres); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2001 | lys_type_free(struct ly_ctx *ctx, struct lys_type *type, |
| 2002 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2003 | { |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 2004 | unsigned int i; |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2005 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2006 | assert(ctx); |
| 2007 | if (!type) { |
| 2008 | return; |
| 2009 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2010 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2011 | lys_extension_instances_free(ctx, type->ext, type->ext_size, private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2012 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2013 | switch (type->base) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 2014 | case LY_TYPE_BINARY: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2015 | lys_restr_free(ctx, type->info.binary.length, private_destructor); |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 2016 | free(type->info.binary.length); |
| 2017 | break; |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 2018 | case LY_TYPE_BITS: |
| 2019 | for (i = 0; i < type->info.bits.count; i++) { |
| 2020 | lydict_remove(ctx, type->info.bits.bit[i].name); |
| 2021 | lydict_remove(ctx, type->info.bits.bit[i].dsc); |
| 2022 | lydict_remove(ctx, type->info.bits.bit[i].ref); |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2023 | lys_iffeature_free(ctx, type->info.bits.bit[i].iffeature, type->info.bits.bit[i].iffeature_size, 0, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2024 | private_destructor); |
| 2025 | lys_extension_instances_free(ctx, type->info.bits.bit[i].ext, type->info.bits.bit[i].ext_size, |
| 2026 | private_destructor); |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 2027 | } |
| 2028 | free(type->info.bits.bit); |
| 2029 | break; |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 2030 | |
| 2031 | case LY_TYPE_DEC64: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2032 | lys_restr_free(ctx, type->info.dec64.range, private_destructor); |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 2033 | free(type->info.dec64.range); |
| 2034 | break; |
| 2035 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2036 | case LY_TYPE_ENUM: |
| 2037 | for (i = 0; i < type->info.enums.count; i++) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2038 | lydict_remove(ctx, type->info.enums.enm[i].name); |
| 2039 | lydict_remove(ctx, type->info.enums.enm[i].dsc); |
| 2040 | lydict_remove(ctx, type->info.enums.enm[i].ref); |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2041 | lys_iffeature_free(ctx, type->info.enums.enm[i].iffeature, type->info.enums.enm[i].iffeature_size, 0, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2042 | private_destructor); |
| 2043 | lys_extension_instances_free(ctx, type->info.enums.enm[i].ext, type->info.enums.enm[i].ext_size, |
| 2044 | private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2045 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2046 | free(type->info.enums.enm); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2047 | break; |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 2048 | |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 2049 | case LY_TYPE_INT8: |
| 2050 | case LY_TYPE_INT16: |
| 2051 | case LY_TYPE_INT32: |
| 2052 | case LY_TYPE_INT64: |
| 2053 | case LY_TYPE_UINT8: |
| 2054 | case LY_TYPE_UINT16: |
| 2055 | case LY_TYPE_UINT32: |
| 2056 | case LY_TYPE_UINT64: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2057 | lys_restr_free(ctx, type->info.num.range, private_destructor); |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 2058 | free(type->info.num.range); |
| 2059 | break; |
| 2060 | |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 2061 | case LY_TYPE_LEAFREF: |
| 2062 | lydict_remove(ctx, type->info.lref.path); |
| 2063 | break; |
| 2064 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 2065 | case LY_TYPE_STRING: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2066 | lys_restr_free(ctx, type->info.str.length, private_destructor); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 2067 | free(type->info.str.length); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 2068 | for (i = 0; i < type->info.str.pat_count; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2069 | lys_restr_free(ctx, &type->info.str.patterns[i], private_destructor); |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 2070 | #ifdef LY_ENABLED_CACHE |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 2071 | if (type->info.str.patterns_pcre) { |
| 2072 | pcre_free((pcre*)type->info.str.patterns_pcre[2 * i]); |
| 2073 | pcre_free_study((pcre_extra*)type->info.str.patterns_pcre[2 * i + 1]); |
| 2074 | } |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 2075 | #endif |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 2076 | } |
| 2077 | free(type->info.str.patterns); |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 2078 | #ifdef LY_ENABLED_CACHE |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 2079 | free(type->info.str.patterns_pcre); |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 2080 | #endif |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 2081 | break; |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 2082 | |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 2083 | case LY_TYPE_UNION: |
| 2084 | for (i = 0; i < type->info.uni.count; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2085 | lys_type_free(ctx, &type->info.uni.types[i], private_destructor); |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 2086 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2087 | free(type->info.uni.types); |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 2088 | break; |
| 2089 | |
Michal Vasko | d328219 | 2016-09-05 11:27:57 +0200 | [diff] [blame] | 2090 | case LY_TYPE_IDENT: |
| 2091 | free(type->info.ident.ref); |
| 2092 | break; |
| 2093 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2094 | default: |
Michal Vasko | d328219 | 2016-09-05 11:27:57 +0200 | [diff] [blame] | 2095 | /* nothing to do for LY_TYPE_INST, LY_TYPE_BOOL, LY_TYPE_EMPTY */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2096 | break; |
| 2097 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2098 | } |
| 2099 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2100 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2101 | lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf, |
| 2102 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2103 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2104 | assert(ctx); |
| 2105 | if (!tpdf) { |
| 2106 | return; |
| 2107 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2108 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2109 | lydict_remove(ctx, tpdf->name); |
| 2110 | lydict_remove(ctx, tpdf->dsc); |
| 2111 | lydict_remove(ctx, tpdf->ref); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2112 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2113 | lys_type_free(ctx, &tpdf->type, private_destructor); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2114 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2115 | lydict_remove(ctx, tpdf->units); |
| 2116 | lydict_remove(ctx, tpdf->dflt); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2117 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2118 | lys_extension_instances_free(ctx, tpdf->ext, tpdf->ext_size, private_destructor); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2119 | } |
| 2120 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2121 | static struct lys_when * |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2122 | lys_when_dup(struct lys_module *mod, struct lys_when *old, int shallow, struct unres_schema *unres) |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2123 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2124 | struct lys_when *new; |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2125 | |
| 2126 | if (!old) { |
| 2127 | return NULL; |
| 2128 | } |
| 2129 | |
| 2130 | new = calloc(1, sizeof *new); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2131 | LY_CHECK_ERR_RETURN(!new, LOGMEM(mod->ctx), NULL); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 2132 | new->cond = lydict_insert(mod->ctx, old->cond, 0); |
| 2133 | new->dsc = lydict_insert(mod->ctx, old->dsc, 0); |
| 2134 | new->ref = lydict_insert(mod->ctx, old->ref, 0); |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 2135 | new->ext_size = old->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 2136 | lys_ext_dup(mod->ctx, mod, old->ext, old->ext_size, new, LYEXT_PAR_WHEN, &new->ext, shallow, unres); |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2137 | |
| 2138 | return new; |
| 2139 | } |
| 2140 | |
Michal Vasko | 0308dd6 | 2015-10-07 09:14:40 +0200 | [diff] [blame] | 2141 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2142 | lys_when_free(struct ly_ctx *ctx, struct lys_when *w, |
| 2143 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2144 | { |
| 2145 | if (!w) { |
| 2146 | return; |
| 2147 | } |
| 2148 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2149 | lys_extension_instances_free(ctx, w->ext, w->ext_size, private_destructor); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2150 | lydict_remove(ctx, w->cond); |
| 2151 | lydict_remove(ctx, w->dsc); |
| 2152 | lydict_remove(ctx, w->ref); |
| 2153 | |
| 2154 | free(w); |
| 2155 | } |
| 2156 | |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2157 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2158 | lys_augment_free(struct ly_ctx *ctx, struct lys_node_augment *aug, |
| 2159 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2160 | { |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 2161 | struct lys_node *next, *sub; |
| 2162 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2163 | /* children from a resolved augment are freed under the target node */ |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 2164 | if (!aug->target || (aug->flags & LYS_NOTAPPLIED)) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2165 | LY_TREE_FOR_SAFE(aug->child, next, sub) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2166 | lys_node_free(sub, private_destructor, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2167 | } |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 2168 | } |
| 2169 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2170 | lydict_remove(ctx, aug->target_name); |
| 2171 | lydict_remove(ctx, aug->dsc); |
| 2172 | lydict_remove(ctx, aug->ref); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2173 | |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2174 | lys_iffeature_free(ctx, aug->iffeature, aug->iffeature_size, 0, private_destructor); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2175 | lys_extension_instances_free(ctx, aug->ext, aug->ext_size, private_destructor); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2176 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2177 | lys_when_free(ctx, aug->when, private_destructor); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2178 | } |
| 2179 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2180 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2181 | lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident, |
| 2182 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2183 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2184 | assert(ctx); |
| 2185 | if (!ident) { |
| 2186 | return; |
| 2187 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2188 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 2189 | free(ident->base); |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 2190 | ly_set_free(ident->der); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2191 | lydict_remove(ctx, ident->name); |
| 2192 | lydict_remove(ctx, ident->dsc); |
| 2193 | lydict_remove(ctx, ident->ref); |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2194 | lys_iffeature_free(ctx, ident->iffeature, ident->iffeature_size, 0, private_destructor); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2195 | lys_extension_instances_free(ctx, ident->ext, ident->ext_size, private_destructor); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2196 | |
| 2197 | } |
| 2198 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2199 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2200 | lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp, |
| 2201 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2202 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2203 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2204 | |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2205 | /* handle only specific parts for LYS_GROUPING */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2206 | for (i = 0; i < grp->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2207 | lys_tpdf_free(ctx, &grp->tpdf[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2208 | } |
| 2209 | free(grp->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2210 | } |
| 2211 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2212 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2213 | lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io, |
| 2214 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2215 | { |
| 2216 | int i; |
| 2217 | |
| 2218 | /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */ |
| 2219 | for (i = 0; i < io->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2220 | lys_tpdf_free(ctx, &io->tpdf[i], private_destructor); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2221 | } |
| 2222 | free(io->tpdf); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2223 | |
| 2224 | for (i = 0; i < io->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2225 | lys_restr_free(ctx, &io->must[i], private_destructor); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2226 | } |
| 2227 | free(io->must); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2228 | } |
| 2229 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2230 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2231 | lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif, |
| 2232 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2233 | { |
| 2234 | int i; |
| 2235 | |
| 2236 | for (i = 0; i < notif->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2237 | lys_restr_free(ctx, ¬if->must[i], private_destructor); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2238 | } |
| 2239 | free(notif->must); |
| 2240 | |
| 2241 | for (i = 0; i < notif->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2242 | lys_tpdf_free(ctx, ¬if->tpdf[i], private_destructor); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2243 | } |
| 2244 | free(notif->tpdf); |
| 2245 | } |
| 2246 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2247 | lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml, |
| 2248 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2249 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2250 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2251 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2252 | for (i = 0; i < anyxml->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2253 | lys_restr_free(ctx, &anyxml->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2254 | } |
| 2255 | free(anyxml->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2256 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2257 | lys_when_free(ctx, anyxml->when, private_destructor); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2258 | } |
| 2259 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2260 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2261 | lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf, |
| 2262 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2263 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2264 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2265 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 2266 | /* leafref backlinks */ |
| 2267 | ly_set_free((struct ly_set *)leaf->backlinks); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2268 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2269 | for (i = 0; i < leaf->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2270 | lys_restr_free(ctx, &leaf->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2271 | } |
| 2272 | free(leaf->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2273 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2274 | lys_when_free(ctx, leaf->when, private_destructor); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2275 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2276 | lys_type_free(ctx, &leaf->type, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2277 | lydict_remove(ctx, leaf->units); |
| 2278 | lydict_remove(ctx, leaf->dflt); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2279 | } |
| 2280 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2281 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2282 | lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist, |
| 2283 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2284 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2285 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2286 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2287 | if (llist->backlinks) { |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2288 | /* leafref backlinks */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2289 | ly_set_free(llist->backlinks); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2290 | } |
| 2291 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2292 | for (i = 0; i < llist->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2293 | lys_restr_free(ctx, &llist->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2294 | } |
| 2295 | free(llist->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2296 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2297 | for (i = 0; i < llist->dflt_size; i++) { |
| 2298 | lydict_remove(ctx, llist->dflt[i]); |
| 2299 | } |
| 2300 | free(llist->dflt); |
| 2301 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2302 | lys_when_free(ctx, llist->when, private_destructor); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2303 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2304 | lys_type_free(ctx, &llist->type, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2305 | lydict_remove(ctx, llist->units); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2306 | } |
| 2307 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2308 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2309 | lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list, |
| 2310 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2311 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2312 | int i, j; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2313 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2314 | /* handle only specific parts for LY_NODE_LIST */ |
Michal Vasko | aaeab1d | 2018-02-16 09:36:46 +0100 | [diff] [blame] | 2315 | lys_when_free(ctx, list->when, private_destructor); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2316 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2317 | for (i = 0; i < list->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2318 | lys_restr_free(ctx, &list->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2319 | } |
| 2320 | free(list->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2321 | |
Michal Vasko | aaeab1d | 2018-02-16 09:36:46 +0100 | [diff] [blame] | 2322 | for (i = 0; i < list->tpdf_size; i++) { |
| 2323 | lys_tpdf_free(ctx, &list->tpdf[i], private_destructor); |
| 2324 | } |
| 2325 | free(list->tpdf); |
| 2326 | |
| 2327 | free(list->keys); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2328 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2329 | for (i = 0; i < list->unique_size; i++) { |
Michal Vasko | f5e940a | 2016-06-07 09:39:26 +0200 | [diff] [blame] | 2330 | for (j = 0; j < list->unique[i].expr_size; j++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2331 | lydict_remove(ctx, list->unique[i].expr[j]); |
| 2332 | } |
| 2333 | free(list->unique[i].expr); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2334 | } |
| 2335 | free(list->unique); |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 2336 | |
Michal Vasko | aaeab1d | 2018-02-16 09:36:46 +0100 | [diff] [blame] | 2337 | lydict_remove(ctx, list->keys_str); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2338 | } |
| 2339 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2340 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2341 | lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont, |
| 2342 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2343 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2344 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2345 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2346 | /* handle only specific parts for LY_NODE_CONTAINER */ |
| 2347 | lydict_remove(ctx, cont->presence); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2348 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2349 | for (i = 0; i < cont->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2350 | lys_tpdf_free(ctx, &cont->tpdf[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2351 | } |
| 2352 | free(cont->tpdf); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2353 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2354 | for (i = 0; i < cont->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2355 | lys_restr_free(ctx, &cont->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2356 | } |
| 2357 | free(cont->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2358 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2359 | lys_when_free(ctx, cont->when, private_destructor); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2360 | } |
| 2361 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2362 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2363 | lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f, |
| 2364 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2365 | { |
| 2366 | lydict_remove(ctx, f->name); |
| 2367 | lydict_remove(ctx, f->dsc); |
| 2368 | lydict_remove(ctx, f->ref); |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2369 | lys_iffeature_free(ctx, f->iffeature, f->iffeature_size, 0, private_destructor); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 2370 | ly_set_free(f->depfeatures); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2371 | lys_extension_instances_free(ctx, f->ext, f->ext_size, private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2375 | lys_extension_free(struct ly_ctx *ctx, struct lys_ext *e, |
| 2376 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2377 | { |
| 2378 | lydict_remove(ctx, e->name); |
| 2379 | lydict_remove(ctx, e->dsc); |
| 2380 | lydict_remove(ctx, e->ref); |
| 2381 | lydict_remove(ctx, e->argument); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2382 | lys_extension_instances_free(ctx, e->ext, e->ext_size, private_destructor); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2383 | } |
| 2384 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2385 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2386 | lys_deviation_free(struct lys_module *module, struct lys_deviation *dev, |
| 2387 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2388 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2389 | int i, j, k; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2390 | struct ly_ctx *ctx; |
| 2391 | struct lys_node *next, *elem; |
| 2392 | |
| 2393 | ctx = module->ctx; |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2394 | |
| 2395 | lydict_remove(ctx, dev->target_name); |
| 2396 | lydict_remove(ctx, dev->dsc); |
| 2397 | lydict_remove(ctx, dev->ref); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2398 | lys_extension_instances_free(ctx, dev->ext, dev->ext_size, private_destructor); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2399 | |
Pavol Vican | 64d0b76 | 2016-08-25 10:44:59 +0200 | [diff] [blame] | 2400 | if (!dev->deviate) { |
Michal Vasko | 7427b26 | 2018-05-14 15:23:55 +0200 | [diff] [blame] | 2401 | return; |
Pavol Vican | 64d0b76 | 2016-08-25 10:44:59 +0200 | [diff] [blame] | 2402 | } |
| 2403 | |
Michal Vasko | 7427b26 | 2018-05-14 15:23:55 +0200 | [diff] [blame] | 2404 | /* it could not be applied because it failed to be applied */ |
| 2405 | if (dev->orig_node) { |
| 2406 | /* the module was freed, but we only need the context from orig_node, use ours */ |
| 2407 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 2408 | /* it's actually a node subtree, we need to update modules on all the nodes :-/ */ |
| 2409 | LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) { |
| 2410 | elem->module = module; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2411 | |
Michal Vasko | 7427b26 | 2018-05-14 15:23:55 +0200 | [diff] [blame] | 2412 | LY_TREE_DFS_END(dev->orig_node, next, elem); |
| 2413 | } |
| 2414 | lys_node_free(dev->orig_node, NULL, 0); |
| 2415 | } else { |
| 2416 | /* it's just a shallow copy, freeing one node */ |
| 2417 | dev->orig_node->module = module; |
| 2418 | lys_node_free(dev->orig_node, NULL, 1); |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2419 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2420 | } |
| 2421 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2422 | for (i = 0; i < dev->deviate_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2423 | 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] | 2424 | |
Radek Krejci | d5a5c28 | 2016-08-15 15:38:08 +0200 | [diff] [blame] | 2425 | for (j = 0; j < dev->deviate[i].dflt_size; j++) { |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2426 | lydict_remove(ctx, dev->deviate[i].dflt[j]); |
Radek Krejci | d5a5c28 | 2016-08-15 15:38:08 +0200 | [diff] [blame] | 2427 | } |
| 2428 | free(dev->deviate[i].dflt); |
| 2429 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2430 | lydict_remove(ctx, dev->deviate[i].units); |
| 2431 | |
| 2432 | if (dev->deviate[i].mod == LY_DEVIATE_DEL) { |
| 2433 | for (j = 0; j < dev->deviate[i].must_size; j++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2434 | lys_restr_free(ctx, &dev->deviate[i].must[j], private_destructor); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2435 | } |
| 2436 | free(dev->deviate[i].must); |
| 2437 | |
| 2438 | for (j = 0; j < dev->deviate[i].unique_size; j++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2439 | for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) { |
| 2440 | lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]); |
| 2441 | } |
Michal Vasko | 0238ccf | 2016-03-07 15:02:18 +0100 | [diff] [blame] | 2442 | free(dev->deviate[i].unique[j].expr); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2443 | } |
| 2444 | free(dev->deviate[i].unique); |
| 2445 | } |
| 2446 | } |
| 2447 | free(dev->deviate); |
| 2448 | } |
| 2449 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2450 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2451 | lys_uses_free(struct ly_ctx *ctx, struct lys_node_uses *uses, |
| 2452 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2453 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2454 | int i, j; |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2455 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2456 | for (i = 0; i < uses->refine_size; i++) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2457 | lydict_remove(ctx, uses->refine[i].target_name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2458 | lydict_remove(ctx, uses->refine[i].dsc); |
| 2459 | lydict_remove(ctx, uses->refine[i].ref); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2460 | |
Radek Krejci | fde04bd | 2017-09-13 16:38:38 +0200 | [diff] [blame] | 2461 | lys_iffeature_free(ctx, uses->refine[i].iffeature, uses->refine[i].iffeature_size, 0, private_destructor); |
| 2462 | |
Michal Vasko | a275c0a | 2015-09-24 09:55:42 +0200 | [diff] [blame] | 2463 | for (j = 0; j < uses->refine[i].must_size; j++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2464 | lys_restr_free(ctx, &uses->refine[i].must[j], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2465 | } |
| 2466 | free(uses->refine[i].must); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2467 | |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 2468 | for (j = 0; j < uses->refine[i].dflt_size; j++) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 2469 | lydict_remove(ctx, uses->refine[i].dflt[j]); |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 2470 | } |
| 2471 | free(uses->refine[i].dflt); |
| 2472 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2473 | 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] | 2474 | |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 2475 | if (uses->refine[i].target_type & LYS_CONTAINER) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2476 | lydict_remove(ctx, uses->refine[i].mod.presence); |
| 2477 | } |
| 2478 | } |
| 2479 | free(uses->refine); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2480 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2481 | for (i = 0; i < uses->augment_size; i++) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2482 | lys_augment_free(ctx, &uses->augment[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2483 | } |
| 2484 | free(uses->augment); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2485 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2486 | lys_when_free(ctx, uses->when, private_destructor); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2487 | } |
| 2488 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2489 | void |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2490 | 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] | 2491 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2492 | struct ly_ctx *ctx; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2493 | struct lys_node *sub, *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2494 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2495 | if (!node) { |
| 2496 | return; |
| 2497 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2498 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2499 | assert(node->module); |
| 2500 | assert(node->module->ctx); |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2501 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2502 | ctx = node->module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2503 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2504 | /* remove private object */ |
Mislav Novakovic | b5529e5 | 2016-02-29 11:42:43 +0100 | [diff] [blame] | 2505 | if (node->priv && private_destructor) { |
| 2506 | private_destructor(node, node->priv); |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2507 | } |
| 2508 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2509 | /* common part */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2510 | lydict_remove(ctx, node->name); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2511 | if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2512 | lys_iffeature_free(ctx, node->iffeature, node->iffeature_size, shallow, private_destructor); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2513 | lydict_remove(ctx, node->dsc); |
| 2514 | lydict_remove(ctx, node->ref); |
| 2515 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2516 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2517 | if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2518 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2519 | lys_node_free(sub, private_destructor, 0); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2520 | } |
| 2521 | } |
| 2522 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2523 | lys_extension_instances_free(ctx, node->ext, node->ext_size, private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2524 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2525 | /* specific part */ |
| 2526 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2527 | case LYS_CONTAINER: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2528 | lys_container_free(ctx, (struct lys_node_container *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2529 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2530 | case LYS_CHOICE: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2531 | lys_when_free(ctx, ((struct lys_node_choice *)node)->when, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2532 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2533 | case LYS_LEAF: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2534 | lys_leaf_free(ctx, (struct lys_node_leaf *)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_LEAFLIST: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2537 | lys_leaflist_free(ctx, (struct lys_node_leaflist *)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_LIST: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2540 | lys_list_free(ctx, (struct lys_node_list *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2541 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2542 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2543 | case LYS_ANYDATA: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2544 | lys_anydata_free(ctx, (struct lys_node_anydata *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2545 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2546 | case LYS_USES: |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2547 | lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2548 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2549 | case LYS_CASE: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2550 | lys_when_free(ctx, ((struct lys_node_case *)node)->when, private_destructor); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2551 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2552 | case LYS_AUGMENT: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2553 | /* do nothing */ |
| 2554 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2555 | case LYS_GROUPING: |
| 2556 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2557 | case LYS_ACTION: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2558 | lys_grp_free(ctx, (struct lys_node_grp *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2559 | break; |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2560 | case LYS_NOTIF: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2561 | lys_notif_free(ctx, (struct lys_node_notif *)node, private_destructor); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2562 | break; |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2563 | case LYS_INPUT: |
| 2564 | case LYS_OUTPUT: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2565 | lys_inout_free(ctx, (struct lys_node_inout *)node, private_destructor); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2566 | break; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 2567 | case LYS_EXT: |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 2568 | case LYS_UNKNOWN: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2569 | LOGINT(ctx); |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 2570 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2571 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2572 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2573 | /* again common part */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2574 | lys_node_unlink(node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2575 | free(node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2576 | } |
| 2577 | |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 2578 | API struct lys_module * |
| 2579 | lys_implemented_module(const struct lys_module *mod) |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 2580 | { |
| 2581 | struct ly_ctx *ctx; |
| 2582 | int i; |
| 2583 | |
| 2584 | if (!mod || mod->implemented) { |
| 2585 | /* invalid argument or the module itself is implemented */ |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 2586 | return (struct lys_module *)mod; |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | ctx = mod->ctx; |
| 2590 | for (i = 0; i < ctx->models.used; i++) { |
| 2591 | if (!ctx->models.list[i]->implemented) { |
| 2592 | continue; |
| 2593 | } |
| 2594 | |
| 2595 | if (ly_strequal(mod->name, ctx->models.list[i]->name, 1)) { |
| 2596 | /* we have some revision of the module implemented */ |
| 2597 | return ctx->models.list[i]; |
| 2598 | } |
| 2599 | } |
| 2600 | |
| 2601 | /* we have no revision of the module implemented, return the module itself, |
| 2602 | * 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] | 2603 | return (struct lys_module *)mod; |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 2604 | } |
| 2605 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2606 | /* free_int_mods - flag whether to free the internal modules as well */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2607 | static void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2608 | 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] | 2609 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2610 | struct ly_ctx *ctx; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2611 | struct lys_node *next, *iter; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2612 | unsigned int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2613 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2614 | assert(module->ctx); |
| 2615 | ctx = module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2616 | |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2617 | /* just free the import array, imported modules will stay in the context */ |
Radek Krejci | 225376f | 2016-02-16 17:36:22 +0100 | [diff] [blame] | 2618 | for (i = 0; i < module->imp_size; i++) { |
Michal Vasko | a99c163 | 2016-06-06 16:23:52 +0200 | [diff] [blame] | 2619 | lydict_remove(ctx, module->imp[i].prefix); |
Michal Vasko | 8bfe381 | 2016-07-27 13:37:52 +0200 | [diff] [blame] | 2620 | lydict_remove(ctx, module->imp[i].dsc); |
| 2621 | lydict_remove(ctx, module->imp[i].ref); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2622 | 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] | 2623 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2624 | free(module->imp); |
| 2625 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2626 | /* submodules don't have data tree, the data nodes |
| 2627 | * are placed in the main module altogether */ |
| 2628 | if (!module->type) { |
| 2629 | LY_TREE_FOR_SAFE(module->data, next, iter) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2630 | lys_node_free(iter, private_destructor, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2631 | } |
Radek Krejci | 2118196 | 2015-06-30 14:11:00 +0200 | [diff] [blame] | 2632 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2633 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2634 | lydict_remove(ctx, module->dsc); |
| 2635 | lydict_remove(ctx, module->ref); |
| 2636 | lydict_remove(ctx, module->org); |
| 2637 | lydict_remove(ctx, module->contact); |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 2638 | lydict_remove(ctx, module->filepath); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2639 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2640 | /* revisions */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2641 | for (i = 0; i < module->rev_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2642 | 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] | 2643 | lydict_remove(ctx, module->rev[i].dsc); |
| 2644 | lydict_remove(ctx, module->rev[i].ref); |
| 2645 | } |
| 2646 | free(module->rev); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2647 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2648 | /* identities */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2649 | for (i = 0; i < module->ident_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2650 | lys_ident_free(ctx, &module->ident[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2651 | } |
| 2652 | module->ident_size = 0; |
| 2653 | free(module->ident); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2654 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2655 | /* typedefs */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2656 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2657 | lys_tpdf_free(ctx, &module->tpdf[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2658 | } |
| 2659 | free(module->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2660 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2661 | /* extension instances */ |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2662 | lys_extension_instances_free(ctx, module->ext, module->ext_size, private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2663 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2664 | /* augment */ |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2665 | for (i = 0; i < module->augment_size; i++) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2666 | lys_augment_free(ctx, &module->augment[i], private_destructor); |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2667 | } |
| 2668 | free(module->augment); |
| 2669 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2670 | /* features */ |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2671 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2672 | lys_feature_free(ctx, &module->features[i], private_destructor); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2673 | } |
| 2674 | free(module->features); |
| 2675 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2676 | /* deviations */ |
| 2677 | for (i = 0; i < module->deviation_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2678 | lys_deviation_free(module, &module->deviation[i], private_destructor); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2679 | } |
| 2680 | free(module->deviation); |
| 2681 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2682 | /* extensions */ |
| 2683 | for (i = 0; i < module->extensions_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2684 | lys_extension_free(ctx, &module->extensions[i], private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2685 | } |
| 2686 | free(module->extensions); |
| 2687 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2688 | lydict_remove(ctx, module->name); |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 2689 | lydict_remove(ctx, module->prefix); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2690 | } |
| 2691 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2692 | void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2693 | 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] | 2694 | { |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 2695 | int i; |
| 2696 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2697 | if (!submodule) { |
| 2698 | return; |
| 2699 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2700 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2701 | /* common part with struct ly_module */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2702 | module_free_common((struct lys_module *)submodule, private_destructor); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2703 | |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 2704 | /* include */ |
| 2705 | for (i = 0; i < submodule->inc_size; i++) { |
| 2706 | lydict_remove(submodule->ctx, submodule->inc[i].dsc); |
| 2707 | lydict_remove(submodule->ctx, submodule->inc[i].ref); |
| 2708 | lys_extension_instances_free(submodule->ctx, submodule->inc[i].ext, submodule->inc[i].ext_size, private_destructor); |
| 2709 | /* complete submodule free is done only from main module since |
| 2710 | * submodules propagate their includes to the main module */ |
| 2711 | } |
| 2712 | free(submodule->inc); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2713 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2714 | free(submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2715 | } |
| 2716 | |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 2717 | int |
| 2718 | lys_ingrouping(const struct lys_node *node) |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 2719 | { |
| 2720 | const struct lys_node *iter = node; |
| 2721 | assert(node); |
| 2722 | |
| 2723 | for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter)); |
| 2724 | if (!iter) { |
| 2725 | return 0; |
| 2726 | } else { |
| 2727 | return 1; |
| 2728 | } |
| 2729 | } |
| 2730 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2731 | /* |
| 2732 | * final: 0 - do not change config flags; 1 - inherit config flags from the parent; 2 - remove config flags |
| 2733 | */ |
| 2734 | static struct lys_node * |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 2735 | 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] | 2736 | struct unres_schema *unres, int shallow, int finalize) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2737 | { |
Radek Krejci | 7b9f566 | 2016-11-07 16:28:37 +0100 | [diff] [blame] | 2738 | struct lys_node *retval = NULL, *iter, *p; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2739 | struct ly_ctx *ctx = module->ctx; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2740 | int i, j, rc; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2741 | unsigned int size, size1, size2; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 2742 | struct unres_list_uniq *unique_info; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2743 | uint16_t flags; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2744 | |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2745 | struct lys_node_container *cont = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2746 | struct lys_node_container *cont_orig = (struct lys_node_container *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2747 | struct lys_node_choice *choice = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2748 | struct lys_node_choice *choice_orig = (struct lys_node_choice *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2749 | struct lys_node_leaf *leaf = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2750 | struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2751 | struct lys_node_leaflist *llist = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2752 | struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2753 | struct lys_node_list *list = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2754 | struct lys_node_list *list_orig = (struct lys_node_list *)node; |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2755 | struct lys_node_anydata *any = NULL; |
| 2756 | struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2757 | struct lys_node_uses *uses = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2758 | struct lys_node_uses *uses_orig = (struct lys_node_uses *)node; |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2759 | struct lys_node_rpc_action *rpc = NULL; |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2760 | struct lys_node_inout *io = NULL; |
Radek Krejci | c43151c | 2017-03-12 07:10:52 +0100 | [diff] [blame] | 2761 | struct lys_node_notif *ntf = NULL; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2762 | struct lys_node_case *cs = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2763 | struct lys_node_case *cs_orig = (struct lys_node_case *)node; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2764 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2765 | /* we cannot just duplicate memory since the strings are stored in |
| 2766 | * dictionary and we need to update dictionary counters. |
| 2767 | */ |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2768 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2769 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2770 | case LYS_CONTAINER: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2771 | cont = calloc(1, sizeof *cont); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2772 | retval = (struct lys_node *)cont; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2773 | break; |
| 2774 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2775 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2776 | choice = calloc(1, sizeof *choice); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2777 | retval = (struct lys_node *)choice; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2778 | break; |
| 2779 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2780 | case LYS_LEAF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2781 | leaf = calloc(1, sizeof *leaf); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2782 | retval = (struct lys_node *)leaf; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2783 | break; |
| 2784 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2785 | case LYS_LEAFLIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2786 | llist = calloc(1, sizeof *llist); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2787 | retval = (struct lys_node *)llist; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2788 | break; |
| 2789 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2790 | case LYS_LIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2791 | list = calloc(1, sizeof *list); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2792 | retval = (struct lys_node *)list; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2793 | break; |
| 2794 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2795 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2796 | case LYS_ANYDATA: |
| 2797 | any = calloc(1, sizeof *any); |
| 2798 | retval = (struct lys_node *)any; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2799 | break; |
| 2800 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2801 | case LYS_USES: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2802 | uses = calloc(1, sizeof *uses); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2803 | retval = (struct lys_node *)uses; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2804 | break; |
| 2805 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2806 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2807 | cs = calloc(1, sizeof *cs); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2808 | retval = (struct lys_node *)cs; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2809 | break; |
| 2810 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2811 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2812 | case LYS_ACTION: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2813 | rpc = calloc(1, sizeof *rpc); |
| 2814 | retval = (struct lys_node *)rpc; |
| 2815 | break; |
| 2816 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2817 | case LYS_INPUT: |
| 2818 | case LYS_OUTPUT: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2819 | io = calloc(1, sizeof *io); |
| 2820 | retval = (struct lys_node *)io; |
| 2821 | break; |
| 2822 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2823 | case LYS_NOTIF: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2824 | ntf = calloc(1, sizeof *ntf); |
| 2825 | retval = (struct lys_node *)ntf; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2826 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2827 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2828 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2829 | LOGINT(ctx); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2830 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2831 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2832 | LY_CHECK_ERR_RETURN(!retval, LOGMEM(ctx), NULL); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2833 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2834 | /* |
| 2835 | * duplicate generic part of the structure |
| 2836 | */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2837 | retval->name = lydict_insert(ctx, node->name, 0); |
| 2838 | retval->dsc = lydict_insert(ctx, node->dsc, 0); |
| 2839 | retval->ref = lydict_insert(ctx, node->ref, 0); |
| 2840 | retval->flags = node->flags; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2841 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2842 | retval->module = module; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2843 | retval->nodetype = node->nodetype; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2844 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2845 | retval->prev = retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2846 | |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 2847 | retval->ext_size = node->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 2848 | 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] | 2849 | goto error; |
| 2850 | } |
| 2851 | |
Radek Krejci | 0621404 | 2016-11-04 16:25:58 +0100 | [diff] [blame] | 2852 | if (node->iffeature_size) { |
| 2853 | retval->iffeature_size = node->iffeature_size; |
| 2854 | retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2855 | LY_CHECK_ERR_GOTO(!retval->iffeature, LOGMEM(ctx), error); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2856 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2857 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2858 | if (!shallow) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 2859 | for (i = 0; i < node->iffeature_size; ++i) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2860 | resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2); |
| 2861 | if (size1) { |
| 2862 | /* there is something to duplicate */ |
| 2863 | |
| 2864 | /* duplicate compiled expression */ |
| 2865 | size = (size1 / 4) + (size1 % 4) ? 1 : 0; |
| 2866 | retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2867 | LY_CHECK_ERR_GOTO(!retval->iffeature[i].expr, LOGMEM(ctx), error); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2868 | memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr); |
| 2869 | |
| 2870 | /* 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] | 2871 | retval->iffeature[i].features = calloc(size2, sizeof *retval->iffeature[i].features); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2872 | 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] | 2873 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2874 | for (j = 0; (unsigned int)j < size2; j++) { |
| 2875 | rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT, |
| 2876 | &retval->iffeature[i].features[j]); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 2877 | if (rc == EXIT_FAILURE) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2878 | /* feature is resolved in origin, so copy it |
| 2879 | * - duplication is used for instantiating groupings |
| 2880 | * and if-feature inside grouping is supposed to be |
| 2881 | * resolved inside the original grouping, so we want |
| 2882 | * to keep pointers to features from the grouping |
| 2883 | * context */ |
| 2884 | retval->iffeature[i].features[j] = node->iffeature[i].features[j]; |
| 2885 | } else if (rc == -1) { |
| 2886 | goto error; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 2887 | } /* else unres was duplicated */ |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2888 | } |
| 2889 | } |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 2890 | |
| 2891 | /* duplicate if-feature's extensions */ |
| 2892 | retval->iffeature[i].ext_size = node->iffeature[i].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 2893 | 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] | 2894 | &retval->iffeature[i], LYEXT_PAR_IFFEATURE, &retval->iffeature[i].ext, shallow, unres)) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 2895 | goto error; |
| 2896 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2897 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2898 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2899 | /* inherit config flags */ |
Radek Krejci | 7b9f566 | 2016-11-07 16:28:37 +0100 | [diff] [blame] | 2900 | p = parent; |
| 2901 | do { |
| 2902 | for (iter = p; iter && (iter->nodetype == LYS_USES); iter = iter->parent); |
| 2903 | } while (iter && iter->nodetype == LYS_AUGMENT && (p = lys_parent(iter))); |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2904 | if (iter) { |
| 2905 | flags = iter->flags & LYS_CONFIG_MASK; |
| 2906 | } else { |
| 2907 | /* default */ |
| 2908 | flags = LYS_CONFIG_W; |
| 2909 | } |
| 2910 | |
| 2911 | switch (finalize) { |
| 2912 | case 1: |
| 2913 | /* inherit config flags */ |
| 2914 | if (retval->flags & LYS_CONFIG_SET) { |
| 2915 | /* skip nodes with an explicit config value */ |
| 2916 | if ((flags & LYS_CONFIG_R) && (retval->flags & LYS_CONFIG_W)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2917 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, "true", "config"); |
| 2918 | 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] | 2919 | goto error; |
| 2920 | } |
| 2921 | break; |
| 2922 | } |
| 2923 | |
| 2924 | if (retval->nodetype != LYS_USES) { |
| 2925 | retval->flags = (retval->flags & ~LYS_CONFIG_MASK) | flags; |
| 2926 | } |
Radek Krejci | 2cc2532 | 2017-09-06 16:32:02 +0200 | [diff] [blame] | 2927 | |
| 2928 | /* inherit status */ |
Radek Krejci | e2807ea | 2017-09-07 10:52:21 +0200 | [diff] [blame] | 2929 | if ((parent->flags & LYS_STATUS_MASK) > (retval->flags & LYS_STATUS_MASK)) { |
| 2930 | /* but do it only in case the parent has a stonger status */ |
| 2931 | retval->flags &= ~LYS_STATUS_MASK; |
| 2932 | retval->flags |= (parent->flags & LYS_STATUS_MASK); |
| 2933 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2934 | break; |
| 2935 | case 2: |
| 2936 | /* erase config flags */ |
| 2937 | retval->flags &= ~LYS_CONFIG_MASK; |
| 2938 | retval->flags &= ~LYS_CONFIG_SET; |
| 2939 | break; |
| 2940 | } |
| 2941 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2942 | /* connect it to the parent */ |
| 2943 | if (lys_node_addchild(parent, retval->module, retval)) { |
| 2944 | goto error; |
| 2945 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2946 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2947 | /* go recursively */ |
| 2948 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2949 | LY_TREE_FOR(node->child, iter) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 2950 | if (iter->nodetype & LYS_GROUPING) { |
| 2951 | /* do not instantiate groupings */ |
| 2952 | continue; |
| 2953 | } |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 2954 | if (!lys_node_dup_recursion(module, retval, iter, unres, 0, finalize)) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2955 | goto error; |
| 2956 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2957 | } |
| 2958 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2959 | |
| 2960 | if (finalize == 1) { |
| 2961 | /* check that configuration lists have keys |
| 2962 | * - we really want to check keys_size in original node, because the keys are |
| 2963 | * not yet resolved here, it is done below in nodetype specific part */ |
| 2964 | if ((retval->nodetype == LYS_LIST) && (retval->flags & LYS_CONFIG_W) |
| 2965 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2966 | LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list"); |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2967 | goto error; |
| 2968 | } |
| 2969 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2970 | } else { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 2971 | memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2972 | } |
| 2973 | |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2974 | /* |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2975 | * duplicate specific part of the structure |
| 2976 | */ |
| 2977 | switch (node->nodetype) { |
| 2978 | case LYS_CONTAINER: |
| 2979 | if (cont_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2980 | cont->when = lys_when_dup(module, cont_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 2981 | LY_CHECK_GOTO(!cont->when, error); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2982 | } |
| 2983 | cont->presence = lydict_insert(ctx, cont_orig->presence, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2984 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 2985 | if (cont_orig->must) { |
| 2986 | cont->must = lys_restr_dup(module, cont_orig->must, cont_orig->must_size, shallow, unres); |
| 2987 | LY_CHECK_GOTO(!cont->must, error); |
| 2988 | cont->must_size = cont_orig->must_size; |
| 2989 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2990 | |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 2991 | /* typedefs are not needed in instantiated grouping, nor the deviation's shallow copy */ |
| 2992 | |
| 2993 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2994 | case LYS_CHOICE: |
| 2995 | if (choice_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2996 | choice->when = lys_when_dup(module, choice_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 2997 | LY_CHECK_GOTO(!choice->when, error); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2998 | } |
| 2999 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3000 | if (!shallow) { |
| 3001 | if (choice_orig->dflt) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3002 | rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0, |
| 3003 | LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST, |
| 3004 | (const struct lys_node **)&choice->dflt); |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3005 | if (rc) { |
| 3006 | if (rc == EXIT_FAILURE) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3007 | LOGINT(ctx); |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3008 | } |
| 3009 | goto error; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3010 | } |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3011 | } else { |
| 3012 | /* useless to check return value, we don't know whether |
| 3013 | * there really wasn't any default defined or it just hasn't |
| 3014 | * been resolved, we just hope for the best :) |
| 3015 | */ |
| 3016 | unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3017 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3018 | } else { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3019 | choice->dflt = choice_orig->dflt; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3020 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3021 | break; |
| 3022 | |
| 3023 | case LYS_LEAF: |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 3024 | 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] | 3025 | goto error; |
| 3026 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3027 | leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0); |
| 3028 | |
| 3029 | if (leaf_orig->dflt) { |
| 3030 | leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3031 | } |
| 3032 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3033 | if (leaf_orig->must) { |
| 3034 | leaf->must = lys_restr_dup(module, leaf_orig->must, leaf_orig->must_size, shallow, unres); |
| 3035 | LY_CHECK_GOTO(!leaf->must, error); |
| 3036 | leaf->must_size = leaf_orig->must_size; |
| 3037 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3038 | |
| 3039 | if (leaf_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3040 | leaf->when = lys_when_dup(module, leaf_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3041 | LY_CHECK_GOTO(!leaf->when, error); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3042 | } |
| 3043 | break; |
| 3044 | |
| 3045 | case LYS_LEAFLIST: |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 3046 | 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] | 3047 | goto error; |
| 3048 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3049 | llist->units = lydict_insert(module->ctx, llist_orig->units, 0); |
| 3050 | |
| 3051 | llist->min = llist_orig->min; |
| 3052 | llist->max = llist_orig->max; |
| 3053 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3054 | if (llist_orig->must) { |
| 3055 | llist->must = lys_restr_dup(module, llist_orig->must, llist_orig->must_size, shallow, unres); |
| 3056 | LY_CHECK_GOTO(!llist->must, error); |
| 3057 | llist->must_size = llist_orig->must_size; |
| 3058 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3059 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3060 | if (llist_orig->dflt) { |
| 3061 | llist->dflt = malloc(llist_orig->dflt_size * sizeof *llist->dflt); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3062 | LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), error); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3063 | llist->dflt_size = llist_orig->dflt_size; |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3064 | |
Radek Krejci | c699e42 | 2017-06-02 15:18:58 +0200 | [diff] [blame] | 3065 | for (i = 0; i < llist->dflt_size; i++) { |
| 3066 | llist->dflt[i] = lydict_insert(ctx, llist_orig->dflt[i], 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3067 | } |
| 3068 | } |
| 3069 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3070 | if (llist_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3071 | llist->when = lys_when_dup(module, llist_orig->when, shallow, unres); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3072 | } |
| 3073 | break; |
| 3074 | |
| 3075 | case LYS_LIST: |
| 3076 | list->min = list_orig->min; |
| 3077 | list->max = list_orig->max; |
| 3078 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3079 | if (list_orig->must) { |
| 3080 | list->must = lys_restr_dup(module, list_orig->must, list_orig->must_size, shallow, unres); |
| 3081 | LY_CHECK_GOTO(!list->must, error); |
| 3082 | list->must_size = list_orig->must_size; |
| 3083 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3084 | |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3085 | /* 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] | 3086 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3087 | if (list_orig->keys_size) { |
| 3088 | list->keys = calloc(list_orig->keys_size, sizeof *list->keys); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3089 | LY_CHECK_ERR_GOTO(!list->keys, LOGMEM(ctx), error); |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3090 | list->keys_str = lydict_insert(ctx, list_orig->keys_str, 0); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3091 | list->keys_size = list_orig->keys_size; |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3092 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3093 | if (!shallow) { |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3094 | /* the keys are going to be resolved only if the list is instantiated in data tree, not just |
| 3095 | * in another grouping */ |
| 3096 | for (iter = parent; iter && iter->nodetype != LYS_GROUPING; iter = iter->parent); |
| 3097 | if (!iter && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) { |
| 3098 | goto error; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3099 | } |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3100 | } else { |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3101 | 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] | 3102 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3103 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3104 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3105 | if (list_orig->unique) { |
| 3106 | list->unique = malloc(list_orig->unique_size * sizeof *list->unique); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3107 | LY_CHECK_ERR_GOTO(!list->unique, LOGMEM(ctx), error); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3108 | list->unique_size = list_orig->unique_size; |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3109 | |
Radek Krejci | c699e42 | 2017-06-02 15:18:58 +0200 | [diff] [blame] | 3110 | for (i = 0; i < list->unique_size; ++i) { |
| 3111 | 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] | 3112 | LY_CHECK_ERR_GOTO(!list->unique[i].expr, LOGMEM(ctx), error); |
Radek Krejci | c699e42 | 2017-06-02 15:18:58 +0200 | [diff] [blame] | 3113 | list->unique[i].expr_size = list_orig->unique[i].expr_size; |
| 3114 | for (j = 0; j < list->unique[i].expr_size; j++) { |
| 3115 | list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0); |
| 3116 | |
| 3117 | /* if it stays in unres list, duplicate it also there */ |
| 3118 | unique_info = malloc(sizeof *unique_info); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3119 | LY_CHECK_ERR_GOTO(!unique_info, LOGMEM(ctx), error); |
Radek Krejci | c699e42 | 2017-06-02 15:18:58 +0200 | [diff] [blame] | 3120 | unique_info->list = (struct lys_node *)list; |
| 3121 | unique_info->expr = list->unique[i].expr[j]; |
| 3122 | unique_info->trg_type = &list->unique[i].trg_type; |
| 3123 | unres_schema_dup(module, unres, &list_orig, UNRES_LIST_UNIQ, unique_info); |
| 3124 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3125 | } |
| 3126 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 3127 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3128 | if (list_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3129 | list->when = lys_when_dup(module, list_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3130 | LY_CHECK_GOTO(!list->when, error); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 3131 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 3132 | break; |
| 3133 | |
| 3134 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3135 | case LYS_ANYDATA: |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3136 | if (any_orig->must) { |
| 3137 | any->must = lys_restr_dup(module, any_orig->must, any_orig->must_size, shallow, unres); |
| 3138 | LY_CHECK_GOTO(!any->must, error); |
| 3139 | any->must_size = any_orig->must_size; |
| 3140 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3141 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3142 | if (any_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3143 | any->when = lys_when_dup(module, any_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3144 | LY_CHECK_GOTO(!any->when, error); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3145 | } |
| 3146 | break; |
| 3147 | |
| 3148 | case LYS_USES: |
| 3149 | uses->grp = uses_orig->grp; |
| 3150 | |
| 3151 | if (uses_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3152 | uses->when = lys_when_dup(module, uses_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3153 | LY_CHECK_GOTO(!uses->when, error); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3154 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 3155 | /* 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] | 3156 | break; |
| 3157 | |
| 3158 | case LYS_CASE: |
| 3159 | if (cs_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3160 | cs->when = lys_when_dup(module, cs_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3161 | LY_CHECK_GOTO(!cs->when, error); |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 3162 | } |
| 3163 | break; |
| 3164 | |
Radek Krejci | 9693540 | 2016-11-04 16:27:28 +0100 | [diff] [blame] | 3165 | case LYS_ACTION: |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 3166 | case LYS_RPC: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3167 | case LYS_INPUT: |
| 3168 | case LYS_OUTPUT: |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3169 | case LYS_NOTIF: |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3170 | /* 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] | 3171 | break; |
| 3172 | |
| 3173 | default: |
| 3174 | /* LY_NODE_AUGMENT */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3175 | LOGINT(ctx); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3176 | goto error; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3177 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3178 | |
| 3179 | return retval; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3180 | |
| 3181 | error: |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3182 | lys_node_free(retval, NULL, 0); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3183 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3184 | } |
| 3185 | |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 3186 | int |
| 3187 | lys_has_xpath(const struct lys_node *node) |
| 3188 | { |
| 3189 | assert(node); |
| 3190 | |
| 3191 | switch (node->nodetype) { |
| 3192 | case LYS_AUGMENT: |
| 3193 | if (((struct lys_node_augment *)node)->when) { |
| 3194 | return 1; |
| 3195 | } |
| 3196 | break; |
| 3197 | case LYS_CASE: |
| 3198 | if (((struct lys_node_case *)node)->when) { |
| 3199 | return 1; |
| 3200 | } |
| 3201 | break; |
| 3202 | case LYS_CHOICE: |
| 3203 | if (((struct lys_node_choice *)node)->when) { |
| 3204 | return 1; |
| 3205 | } |
| 3206 | break; |
| 3207 | case LYS_ANYDATA: |
| 3208 | if (((struct lys_node_anydata *)node)->when || ((struct lys_node_anydata *)node)->must_size) { |
| 3209 | return 1; |
| 3210 | } |
| 3211 | break; |
| 3212 | case LYS_LEAF: |
| 3213 | if (((struct lys_node_leaf *)node)->when || ((struct lys_node_leaf *)node)->must_size) { |
| 3214 | return 1; |
| 3215 | } |
| 3216 | break; |
| 3217 | case LYS_LEAFLIST: |
| 3218 | if (((struct lys_node_leaflist *)node)->when || ((struct lys_node_leaflist *)node)->must_size) { |
| 3219 | return 1; |
| 3220 | } |
| 3221 | break; |
| 3222 | case LYS_LIST: |
| 3223 | if (((struct lys_node_list *)node)->when || ((struct lys_node_list *)node)->must_size) { |
| 3224 | return 1; |
| 3225 | } |
| 3226 | break; |
| 3227 | case LYS_CONTAINER: |
| 3228 | if (((struct lys_node_container *)node)->when || ((struct lys_node_container *)node)->must_size) { |
| 3229 | return 1; |
| 3230 | } |
| 3231 | break; |
| 3232 | case LYS_INPUT: |
| 3233 | case LYS_OUTPUT: |
| 3234 | if (((struct lys_node_inout *)node)->must_size) { |
| 3235 | return 1; |
| 3236 | } |
| 3237 | break; |
| 3238 | case LYS_NOTIF: |
| 3239 | if (((struct lys_node_notif *)node)->must_size) { |
| 3240 | return 1; |
| 3241 | } |
| 3242 | break; |
| 3243 | case LYS_USES: |
| 3244 | if (((struct lys_node_uses *)node)->when) { |
| 3245 | return 1; |
| 3246 | } |
| 3247 | break; |
| 3248 | default: |
| 3249 | /* does not have XPath */ |
| 3250 | break; |
| 3251 | } |
| 3252 | |
| 3253 | return 0; |
| 3254 | } |
| 3255 | |
Michal Vasko | 568b195 | 2018-01-30 15:53:30 +0100 | [diff] [blame] | 3256 | int |
| 3257 | lys_type_is_local(const struct lys_type *type) |
| 3258 | { |
| 3259 | if (!type->der->module) { |
| 3260 | /* build-in type */ |
| 3261 | return 1; |
| 3262 | } |
| 3263 | /* type->parent can be either a typedef or leaf/leaf-list, but module pointers are compatible */ |
| 3264 | return (lys_main_module(type->der->module) == lys_main_module(((struct lys_tpdf *)type->parent)->module)); |
| 3265 | } |
| 3266 | |
Radek Krejci | 2cc2532 | 2017-09-06 16:32:02 +0200 | [diff] [blame] | 3267 | /* |
| 3268 | * shallow - |
| 3269 | * - do not inherit status from the parent |
| 3270 | */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3271 | struct lys_node * |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 3272 | 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] | 3273 | struct unres_schema *unres, int shallow) |
| 3274 | { |
| 3275 | struct lys_node *p = NULL; |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 3276 | int finalize = 0; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3277 | struct lys_node *result, *iter, *next; |
| 3278 | |
| 3279 | if (!shallow) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3280 | /* 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] | 3281 | for (p = parent; |
| 3282 | p && !(p->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC | LYS_ACTION | LYS_GROUPING)); |
| 3283 | p = lys_parent(p)); |
| 3284 | finalize = p ? ((p->nodetype == LYS_GROUPING) ? 0 : 2) : 1; |
| 3285 | } |
| 3286 | |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 3287 | result = lys_node_dup_recursion(module, parent, node, unres, shallow, finalize); |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3288 | if (finalize) { |
| 3289 | /* check xpath expressions in the instantiated tree */ |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3290 | for (iter = next = result; iter; iter = next) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 3291 | 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] | 3292 | /* invalid xpath */ |
| 3293 | return NULL; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | /* select next item */ |
| 3297 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA | LYS_GROUPING)) { |
| 3298 | /* child exception for leafs, leaflists and anyxml without children, ignore groupings */ |
| 3299 | next = NULL; |
| 3300 | } else { |
| 3301 | next = iter->child; |
| 3302 | } |
| 3303 | if (!next) { |
| 3304 | /* no children, try siblings */ |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3305 | if (iter == result) { |
| 3306 | /* we are done, no next element to process */ |
| 3307 | break; |
| 3308 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3309 | next = iter->next; |
| 3310 | } |
| 3311 | while (!next) { |
| 3312 | /* parent is already processed, go to its sibling */ |
| 3313 | iter = lys_parent(iter); |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3314 | if (lys_parent(iter) == lys_parent(result)) { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3315 | /* we are done, no next element to process */ |
| 3316 | break; |
| 3317 | } |
| 3318 | next = iter->next; |
| 3319 | } |
| 3320 | } |
| 3321 | } |
| 3322 | |
| 3323 | return result; |
| 3324 | } |
| 3325 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 3326 | void |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3327 | lys_node_switch(struct lys_node *dst, struct lys_node *src) |
| 3328 | { |
| 3329 | struct lys_node *child; |
| 3330 | |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 3331 | 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] | 3332 | |
| 3333 | /* sibling next */ |
Michal Vasko | 8328da8 | 2016-08-25 09:27:22 +0200 | [diff] [blame] | 3334 | if (dst->prev->next) { |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3335 | dst->prev->next = src; |
| 3336 | } |
| 3337 | |
| 3338 | /* sibling prev */ |
| 3339 | if (dst->next) { |
| 3340 | dst->next->prev = src; |
Michal Vasko | 8328da8 | 2016-08-25 09:27:22 +0200 | [diff] [blame] | 3341 | } else { |
| 3342 | for (child = dst->prev; child->prev->next; child = child->prev); |
| 3343 | child->prev = src; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3344 | } |
| 3345 | |
| 3346 | /* next */ |
| 3347 | src->next = dst->next; |
| 3348 | dst->next = NULL; |
| 3349 | |
| 3350 | /* prev */ |
| 3351 | if (dst->prev != dst) { |
| 3352 | src->prev = dst->prev; |
| 3353 | } |
| 3354 | dst->prev = dst; |
| 3355 | |
| 3356 | /* parent child */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 3357 | if (dst->parent) { |
| 3358 | if (dst->parent->child == dst) { |
| 3359 | dst->parent->child = src; |
| 3360 | } |
Radek Krejci | 115fa88 | 2017-03-01 16:15:07 +0100 | [diff] [blame] | 3361 | } else if (lys_main_module(dst->module)->data == dst) { |
| 3362 | lys_main_module(dst->module)->data = src; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3363 | } |
| 3364 | |
| 3365 | /* parent */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 3366 | src->parent = dst->parent; dst->parent = NULL; |
| 3367 | |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3368 | |
| 3369 | /* child parent */ |
| 3370 | LY_TREE_FOR(dst->child, child) { |
| 3371 | if (child->parent == dst) { |
| 3372 | child->parent = src; |
| 3373 | } |
| 3374 | } |
| 3375 | |
| 3376 | /* child */ |
| 3377 | src->child = dst->child; |
| 3378 | dst->child = NULL; |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3379 | |
| 3380 | /* node-specific data */ |
| 3381 | switch (dst->nodetype) { |
| 3382 | case LYS_CONTAINER: |
| 3383 | ((struct lys_node_container *)src)->tpdf_size = ((struct lys_node_container *)dst)->tpdf_size; |
| 3384 | ((struct lys_node_container *)src)->tpdf = ((struct lys_node_container *)dst)->tpdf; |
| 3385 | ((struct lys_node_container *)dst)->tpdf_size = 0; |
| 3386 | ((struct lys_node_container *)dst)->tpdf = NULL; |
| 3387 | break; |
| 3388 | case LYS_LIST: |
| 3389 | ((struct lys_node_list *)src)->tpdf_size = ((struct lys_node_list *)dst)->tpdf_size; |
| 3390 | ((struct lys_node_list *)src)->tpdf = ((struct lys_node_list *)dst)->tpdf; |
| 3391 | ((struct lys_node_list *)dst)->tpdf_size = 0; |
| 3392 | ((struct lys_node_list *)dst)->tpdf = NULL; |
| 3393 | break; |
| 3394 | case LYS_RPC: |
| 3395 | case LYS_ACTION: |
| 3396 | ((struct lys_node_rpc_action *)src)->tpdf_size = ((struct lys_node_rpc_action *)dst)->tpdf_size; |
| 3397 | ((struct lys_node_rpc_action *)src)->tpdf = ((struct lys_node_rpc_action *)dst)->tpdf; |
| 3398 | ((struct lys_node_rpc_action *)dst)->tpdf_size = 0; |
| 3399 | ((struct lys_node_rpc_action *)dst)->tpdf = NULL; |
| 3400 | break; |
| 3401 | case LYS_NOTIF: |
| 3402 | ((struct lys_node_notif *)src)->tpdf_size = ((struct lys_node_notif *)dst)->tpdf_size; |
| 3403 | ((struct lys_node_notif *)src)->tpdf = ((struct lys_node_notif *)dst)->tpdf; |
| 3404 | ((struct lys_node_notif *)dst)->tpdf_size = 0; |
| 3405 | ((struct lys_node_notif *)dst)->tpdf = NULL; |
| 3406 | break; |
| 3407 | case LYS_INPUT: |
| 3408 | case LYS_OUTPUT: |
| 3409 | ((struct lys_node_inout *)src)->tpdf_size = ((struct lys_node_inout *)dst)->tpdf_size; |
| 3410 | ((struct lys_node_inout *)src)->tpdf = ((struct lys_node_inout *)dst)->tpdf; |
| 3411 | ((struct lys_node_inout *)dst)->tpdf_size = 0; |
| 3412 | ((struct lys_node_inout *)dst)->tpdf = NULL; |
| 3413 | break; |
| 3414 | default: |
| 3415 | /* nothing special */ |
| 3416 | break; |
| 3417 | } |
| 3418 | |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3419 | } |
| 3420 | |
| 3421 | void |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 3422 | 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] | 3423 | { |
| 3424 | struct ly_ctx *ctx; |
| 3425 | int i; |
| 3426 | |
| 3427 | if (!module) { |
| 3428 | return; |
| 3429 | } |
| 3430 | |
| 3431 | /* remove schema from the context */ |
| 3432 | ctx = module->ctx; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 3433 | if (remove_from_ctx && ctx->models.used) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3434 | for (i = 0; i < ctx->models.used; i++) { |
| 3435 | if (ctx->models.list[i] == module) { |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 3436 | /* move all the models to not change the order in the list */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3437 | ctx->models.used--; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 3438 | 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] | 3439 | ctx->models.list[ctx->models.used] = NULL; |
| 3440 | /* we are done */ |
| 3441 | break; |
| 3442 | } |
| 3443 | } |
| 3444 | } |
| 3445 | |
| 3446 | /* common part with struct ly_submodule */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 3447 | module_free_common(module, private_destructor); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3448 | |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 3449 | /* include */ |
| 3450 | for (i = 0; i < module->inc_size; i++) { |
| 3451 | lydict_remove(ctx, module->inc[i].dsc); |
| 3452 | lydict_remove(ctx, module->inc[i].ref); |
| 3453 | lys_extension_instances_free(ctx, module->inc[i].ext, module->inc[i].ext_size, private_destructor); |
| 3454 | /* complete submodule free is done only from main module since |
| 3455 | * submodules propagate their includes to the main module */ |
| 3456 | if (free_subs) { |
| 3457 | lys_submodule_free(module->inc[i].submodule, private_destructor); |
| 3458 | } |
| 3459 | } |
| 3460 | free(module->inc); |
| 3461 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3462 | /* specific items to free */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 3463 | lydict_remove(ctx, module->ns); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3464 | |
| 3465 | free(module); |
| 3466 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3467 | |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 3468 | static void |
| 3469 | lys_features_disable_recursive(struct lys_feature *f) |
| 3470 | { |
| 3471 | unsigned int i; |
| 3472 | struct lys_feature *depf; |
| 3473 | |
| 3474 | /* disable the feature */ |
| 3475 | f->flags &= ~LYS_FENABLED; |
| 3476 | |
| 3477 | /* by disabling feature we have to disable also all features that depends on this feature */ |
| 3478 | if (f->depfeatures) { |
| 3479 | for (i = 0; i < f->depfeatures->number; i++) { |
| 3480 | depf = (struct lys_feature *)f->depfeatures->set.g[i]; |
| 3481 | if (depf->flags & LYS_FENABLED) { |
| 3482 | lys_features_disable_recursive(depf); |
| 3483 | } |
| 3484 | } |
| 3485 | } |
| 3486 | } |
| 3487 | |
| 3488 | |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3489 | /* |
| 3490 | * op: 1 - enable, 0 - disable |
| 3491 | */ |
| 3492 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3493 | 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] | 3494 | { |
| 3495 | int all = 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3496 | int i, j, k; |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3497 | int progress, faili, failj, failk; |
| 3498 | |
| 3499 | uint8_t fsize; |
| 3500 | struct lys_feature *f; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3501 | |
| 3502 | if (!module || !name || !strlen(name)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3503 | LOGARG; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3504 | return EXIT_FAILURE; |
| 3505 | } |
| 3506 | |
| 3507 | if (!strcmp(name, "*")) { |
| 3508 | /* enable all */ |
| 3509 | all = 1; |
| 3510 | } |
| 3511 | |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3512 | progress = failk = 1; |
| 3513 | while (progress && failk) { |
| 3514 | for (i = -1, failk = progress = 0; i < module->inc_size; i++) { |
| 3515 | if (i == -1) { |
| 3516 | fsize = module->features_size; |
| 3517 | f = module->features; |
| 3518 | } else { |
| 3519 | fsize = module->inc[i].submodule->features_size; |
| 3520 | f = module->inc[i].submodule->features; |
| 3521 | } |
| 3522 | |
| 3523 | for (j = 0; j < fsize; j++) { |
| 3524 | if (all || !strcmp(f[j].name, name)) { |
Michal Vasko | 34c3b55 | 2016-11-21 09:07:43 +0100 | [diff] [blame] | 3525 | if ((op && (f[j].flags & LYS_FENABLED)) || (!op && !(f[j].flags & LYS_FENABLED))) { |
| 3526 | if (all) { |
| 3527 | /* skip already set features */ |
| 3528 | continue; |
| 3529 | } else { |
| 3530 | /* feature already set correctly */ |
| 3531 | return EXIT_SUCCESS; |
| 3532 | } |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | if (op) { |
| 3536 | /* check referenced features if they are enabled */ |
| 3537 | for (k = 0; k < f[j].iffeature_size; k++) { |
| 3538 | if (!resolve_iffeature(&f[j].iffeature[k])) { |
| 3539 | if (all) { |
| 3540 | faili = i; |
| 3541 | failj = j; |
| 3542 | failk = k + 1; |
| 3543 | break; |
| 3544 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3545 | 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] | 3546 | f[j].name, k + 1); |
| 3547 | return EXIT_FAILURE; |
| 3548 | } |
| 3549 | } |
| 3550 | } |
| 3551 | |
| 3552 | if (k == f[j].iffeature_size) { |
| 3553 | /* the last check passed, do the change */ |
| 3554 | f[j].flags |= LYS_FENABLED; |
| 3555 | progress++; |
| 3556 | } |
| 3557 | } else { |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 3558 | lys_features_disable_recursive(&f[j]); |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3559 | progress++; |
| 3560 | } |
| 3561 | if (!all) { |
| 3562 | /* stop in case changing a single feature */ |
| 3563 | return EXIT_SUCCESS; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3564 | } |
| 3565 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3566 | } |
| 3567 | } |
| 3568 | } |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3569 | if (failk) { |
| 3570 | /* print info about the last failing feature */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3571 | 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] | 3572 | faili == -1 ? module->features[failj].name : module->inc[faili].submodule->features[failj].name, failk); |
| 3573 | return EXIT_FAILURE; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3574 | } |
| 3575 | |
| 3576 | if (all) { |
| 3577 | return EXIT_SUCCESS; |
| 3578 | } else { |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3579 | /* the specified feature not found */ |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3580 | return EXIT_FAILURE; |
| 3581 | } |
| 3582 | } |
| 3583 | |
| 3584 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3585 | lys_features_enable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3586 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3587 | return lys_features_change(module, feature, 1); |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3588 | } |
| 3589 | |
| 3590 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3591 | lys_features_disable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3592 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3593 | return lys_features_change(module, feature, 0); |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3597 | lys_features_state(const struct lys_module *module, const char *feature) |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3598 | { |
| 3599 | int i, j; |
| 3600 | |
| 3601 | if (!module || !feature) { |
| 3602 | return -1; |
| 3603 | } |
| 3604 | |
| 3605 | /* search for the specified feature */ |
| 3606 | /* module itself */ |
| 3607 | for (i = 0; i < module->features_size; i++) { |
| 3608 | if (!strcmp(feature, module->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3609 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3610 | return 1; |
| 3611 | } else { |
| 3612 | return 0; |
| 3613 | } |
| 3614 | } |
| 3615 | } |
| 3616 | |
| 3617 | /* submodules */ |
| 3618 | for (j = 0; j < module->inc_size; j++) { |
| 3619 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
| 3620 | if (!strcmp(feature, module->inc[j].submodule->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3621 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3622 | return 1; |
| 3623 | } else { |
| 3624 | return 0; |
| 3625 | } |
| 3626 | } |
| 3627 | } |
| 3628 | } |
| 3629 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3630 | /* feature definition not found */ |
| 3631 | return -1; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3632 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3633 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3634 | API const char ** |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3635 | lys_features_list(const struct lys_module *module, uint8_t **states) |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3636 | { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3637 | const char **result = NULL; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3638 | int i, j; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3639 | unsigned int count; |
| 3640 | |
| 3641 | if (!module) { |
| 3642 | return NULL; |
| 3643 | } |
| 3644 | |
| 3645 | count = module->features_size; |
| 3646 | for (i = 0; i < module->inc_size; i++) { |
| 3647 | count += module->inc[i].submodule->features_size; |
| 3648 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3649 | result = malloc((count + 1) * sizeof *result); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3650 | LY_CHECK_ERR_RETURN(!result, LOGMEM(module->ctx), NULL); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3651 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3652 | if (states) { |
| 3653 | *states = malloc((count + 1) * sizeof **states); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3654 | LY_CHECK_ERR_RETURN(!(*states), LOGMEM(module->ctx); free(result), NULL); |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3655 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3656 | count = 0; |
| 3657 | |
| 3658 | /* module itself */ |
| 3659 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3660 | result[count] = module->features[i].name; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3661 | if (states) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3662 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3663 | (*states)[count] = 1; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3664 | } else { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3665 | (*states)[count] = 0; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3666 | } |
| 3667 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3668 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3669 | } |
| 3670 | |
| 3671 | /* submodules */ |
| 3672 | for (j = 0; j < module->inc_size; j++) { |
| 3673 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3674 | result[count] = module->inc[j].submodule->features[i].name; |
Radek Krejci | 374b94e | 2015-08-13 09:44:22 +0200 | [diff] [blame] | 3675 | if (states) { |
| 3676 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
| 3677 | (*states)[count] = 1; |
| 3678 | } else { |
| 3679 | (*states)[count] = 0; |
| 3680 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3681 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3682 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3683 | } |
| 3684 | } |
| 3685 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3686 | /* terminating NULL byte */ |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3687 | result[count] = NULL; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3688 | |
| 3689 | return result; |
| 3690 | } |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3691 | |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 3692 | API struct lys_module * |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 3693 | lys_node_module(const struct lys_node *node) |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3694 | { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 3695 | if (!node) { |
| 3696 | return NULL; |
| 3697 | } |
| 3698 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3699 | return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module; |
| 3700 | } |
| 3701 | |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 3702 | API struct lys_module * |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 3703 | lys_main_module(const struct lys_module *module) |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 3704 | { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 3705 | if (!module) { |
| 3706 | return NULL; |
| 3707 | } |
| 3708 | |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 3709 | return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module); |
| 3710 | } |
| 3711 | |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3712 | API struct lys_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3713 | lys_parent(const struct lys_node *node) |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3714 | { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 3715 | struct lys_node *parent; |
| 3716 | |
| 3717 | if (!node) { |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3718 | return NULL; |
| 3719 | } |
| 3720 | |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 3721 | if (node->nodetype == LYS_EXT) { |
| 3722 | if (((struct lys_ext_instance_complex*)node)->parent_type != LYEXT_PAR_NODE) { |
| 3723 | return NULL; |
| 3724 | } |
| 3725 | parent = (struct lys_node*)((struct lys_ext_instance_complex*)node)->parent; |
| 3726 | } else if (!node->parent) { |
| 3727 | return NULL; |
| 3728 | } else { |
| 3729 | parent = node->parent; |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3730 | } |
| 3731 | |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 3732 | if (parent->nodetype == LYS_AUGMENT) { |
| 3733 | return ((struct lys_node_augment *)parent)->target; |
| 3734 | } else { |
| 3735 | return parent; |
| 3736 | } |
| 3737 | } |
| 3738 | |
| 3739 | struct lys_node ** |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 3740 | lys_child(const struct lys_node *node, LYS_NODE nodetype) |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 3741 | { |
| 3742 | void *pp; |
| 3743 | assert(node); |
| 3744 | |
| 3745 | if (node->nodetype == LYS_EXT) { |
| 3746 | pp = lys_ext_complex_get_substmt(lys_snode2stmt(nodetype), (struct lys_ext_instance_complex*)node, NULL); |
| 3747 | if (!pp) { |
| 3748 | return NULL; |
| 3749 | } |
| 3750 | return (struct lys_node **)pp; |
Michal Vasko | 54d4c20 | 2017-08-09 14:09:18 +0200 | [diff] [blame] | 3751 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Radek Krejci | f7fe2cb | 2017-08-09 10:27:12 +0200 | [diff] [blame] | 3752 | return NULL; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 3753 | } else { |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 3754 | return (struct lys_node **)&node->child; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 3755 | } |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3756 | } |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3757 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 3758 | API void * |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3759 | lys_set_private(const struct lys_node *node, void *priv) |
| 3760 | { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 3761 | void *prev; |
| 3762 | |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3763 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3764 | LOGARG; |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 3765 | return NULL; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3766 | } |
| 3767 | |
Mislav Novakovic | b5529e5 | 2016-02-29 11:42:43 +0100 | [diff] [blame] | 3768 | prev = node->priv; |
| 3769 | ((struct lys_node *)node)->priv = priv; |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 3770 | |
| 3771 | return prev; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3772 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3773 | |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3774 | int |
| 3775 | lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref) |
| 3776 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3777 | struct lys_node_leaf *iter; |
| 3778 | struct ly_ctx *ctx = leafref_target->module->ctx; |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 3779 | |
Michal Vasko | 48a573d | 2016-07-01 11:46:02 +0200 | [diff] [blame] | 3780 | if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3781 | LOGINT(ctx); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3782 | return -1; |
| 3783 | } |
| 3784 | |
Pavol Vican | 9317515 | 2016-08-30 15:34:44 +0200 | [diff] [blame] | 3785 | /* check for config flag */ |
Radek Krejci | c688ca0 | 2017-03-20 12:54:39 +0100 | [diff] [blame] | 3786 | if (((struct lys_node_leaf*)leafref)->type.info.lref.req != -1 && |
| 3787 | (leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3788 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, leafref, |
Radek Krejci | d831dd4 | 2017-03-16 12:59:30 +0100 | [diff] [blame] | 3789 | "The leafref %s is config but refers to a non-config %s.", |
Pavol Vican | 9317515 | 2016-08-30 15:34:44 +0200 | [diff] [blame] | 3790 | strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype)); |
| 3791 | return -1; |
| 3792 | } |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 3793 | /* check for cycles */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3794 | 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] | 3795 | if ((void *)iter == (void *)leafref) { |
| 3796 | /* cycle detected */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3797 | LOGVAL(ctx, LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref); |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 3798 | return -1; |
| 3799 | } |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 3800 | } |
| 3801 | |
| 3802 | /* create fake child - the ly_set structure to hold the list of |
Michal Vasko | 48a573d | 2016-07-01 11:46:02 +0200 | [diff] [blame] | 3803 | * leafrefs referencing the leaf(-list) */ |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 3804 | if (!leafref_target->backlinks) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3805 | leafref_target->backlinks = (void *)ly_set_new(); |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 3806 | if (!leafref_target->backlinks) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3807 | LOGMEM(ctx); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3808 | return -1; |
| 3809 | } |
| 3810 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 3811 | ly_set_add(leafref_target->backlinks, leafref, 0); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3812 | |
| 3813 | return 0; |
| 3814 | } |
| 3815 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3816 | /* not needed currently */ |
| 3817 | #if 0 |
| 3818 | |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3819 | static const char * |
| 3820 | lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len) |
| 3821 | { |
| 3822 | struct lys_module *prev_mod; |
| 3823 | uint32_t str_len, mod_len, buf_idx; |
| 3824 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3825 | 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] | 3826 | LOGINT; |
| 3827 | return NULL; |
| 3828 | } |
| 3829 | |
| 3830 | buf_idx = buf_len - 1; |
| 3831 | buf[buf_idx] = '\0'; |
| 3832 | |
| 3833 | while (node) { |
| 3834 | if (lys_parent(node)) { |
| 3835 | prev_mod = lys_node_module(lys_parent(node)); |
| 3836 | } else { |
| 3837 | prev_mod = NULL; |
| 3838 | } |
| 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 | str_len = strlen(node->name); |
| 3842 | |
| 3843 | if (prev_mod != node->module) { |
| 3844 | mod_len = strlen(node->module->name); |
| 3845 | } else { |
| 3846 | mod_len = 0; |
| 3847 | } |
| 3848 | |
| 3849 | if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) { |
| 3850 | LOGINT; |
| 3851 | return NULL; |
| 3852 | } |
| 3853 | |
| 3854 | buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len; |
| 3855 | |
| 3856 | buf[buf_idx] = '/'; |
| 3857 | if (mod_len) { |
| 3858 | memcpy(buf + buf_idx + 1, node->module->name, mod_len); |
| 3859 | buf[buf_idx + 1 + mod_len] = ':'; |
| 3860 | } |
| 3861 | memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len); |
| 3862 | } |
| 3863 | |
| 3864 | node = lys_parent(node); |
| 3865 | } |
| 3866 | |
| 3867 | return buf + buf_idx; |
| 3868 | } |
| 3869 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3870 | #endif |
| 3871 | |
| 3872 | API struct ly_set * |
Michal Vasko | 9ff79aa | 2017-07-03 14:10:32 +0200 | [diff] [blame] | 3873 | 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] | 3874 | { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3875 | struct lyxp_set set; |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3876 | struct ly_set *ret_set; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3877 | uint32_t i; |
| 3878 | |
Michal Vasko | 9ff79aa | 2017-07-03 14:10:32 +0200 | [diff] [blame] | 3879 | if (!ctx_node || !expr) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3880 | LOGARG; |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3881 | return NULL; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3882 | } |
| 3883 | |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 3884 | /* adjust the root */ |
Michal Vasko | 9ff79aa | 2017-07-03 14:10:32 +0200 | [diff] [blame] | 3885 | 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] | 3886 | do { |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 3887 | 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] | 3888 | } 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] | 3889 | } |
| 3890 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3891 | memset(&set, 0, sizeof set); |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3892 | |
| 3893 | if (options & LYXP_MUST) { |
| 3894 | options &= ~LYXP_MUST; |
| 3895 | options |= LYXP_SNODE_MUST; |
| 3896 | } else if (options & LYXP_WHEN) { |
| 3897 | options &= ~LYXP_WHEN; |
| 3898 | options |= LYXP_SNODE_WHEN; |
| 3899 | } else { |
| 3900 | options |= LYXP_SNODE; |
| 3901 | } |
| 3902 | |
Michal Vasko | 9ff79aa | 2017-07-03 14:10:32 +0200 | [diff] [blame] | 3903 | if (lyxp_atomize(expr, ctx_node, ctx_node_type, &set, options, NULL)) { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3904 | free(set.val.snodes); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3905 | 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] | 3906 | return NULL; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3907 | } |
| 3908 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3909 | ret_set = ly_set_new(); |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3910 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3911 | for (i = 0; i < set.used; ++i) { |
| 3912 | switch (set.val.snodes[i].type) { |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3913 | case LYXP_NODE_ELEM: |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3914 | 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] | 3915 | ly_set_free(ret_set); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3916 | free(set.val.snodes); |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3917 | return NULL; |
| 3918 | } |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3919 | break; |
| 3920 | default: |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3921 | /* ignore roots, text and attr should not ever appear */ |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3922 | break; |
| 3923 | } |
| 3924 | } |
| 3925 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3926 | free(set.val.snodes); |
| 3927 | return ret_set; |
| 3928 | } |
| 3929 | |
| 3930 | API struct ly_set * |
| 3931 | lys_node_xpath_atomize(const struct lys_node *node, int options) |
| 3932 | { |
| 3933 | const struct lys_node *next, *elem, *parent, *tmp; |
| 3934 | struct lyxp_set set; |
| 3935 | struct ly_set *ret_set; |
| 3936 | uint16_t i; |
| 3937 | |
| 3938 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3939 | LOGARG; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3940 | return NULL; |
| 3941 | } |
| 3942 | |
| 3943 | for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent)); |
| 3944 | if (!parent) { |
| 3945 | /* not in input, output, or notification */ |
| 3946 | return NULL; |
| 3947 | } |
| 3948 | |
| 3949 | ret_set = ly_set_new(); |
Radek Krejci | d9af8d2 | 2016-09-07 18:44:26 +0200 | [diff] [blame] | 3950 | if (!ret_set) { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3951 | return NULL; |
| 3952 | } |
| 3953 | |
| 3954 | LY_TREE_DFS_BEGIN(node, next, elem) { |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 3955 | 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] | 3956 | /* elem has no dependencies from other subtrees and local nodes get discarded */ |
| 3957 | goto next_iter; |
| 3958 | } |
| 3959 | |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 3960 | if (lyxp_node_atomize(elem, &set, 0)) { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3961 | ly_set_free(ret_set); |
| 3962 | free(set.val.snodes); |
| 3963 | return NULL; |
| 3964 | } |
| 3965 | |
| 3966 | for (i = 0; i < set.used; ++i) { |
| 3967 | switch (set.val.snodes[i].type) { |
| 3968 | case LYXP_NODE_ELEM: |
| 3969 | if (options & LYXP_NO_LOCAL) { |
| 3970 | for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp)); |
| 3971 | if (tmp) { |
| 3972 | /* in local subtree, discard */ |
| 3973 | break; |
| 3974 | } |
| 3975 | } |
| 3976 | if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) { |
| 3977 | ly_set_free(ret_set); |
| 3978 | free(set.val.snodes); |
| 3979 | return NULL; |
| 3980 | } |
| 3981 | break; |
| 3982 | default: |
| 3983 | /* ignore roots, text and attr should not ever appear */ |
| 3984 | break; |
| 3985 | } |
| 3986 | } |
| 3987 | |
| 3988 | free(set.val.snodes); |
| 3989 | if (!(options & LYXP_RECURSIVE)) { |
| 3990 | break; |
| 3991 | } |
| 3992 | next_iter: |
| 3993 | LY_TREE_DFS_END(node, next, elem); |
| 3994 | } |
| 3995 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3996 | return ret_set; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3997 | } |
| 3998 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 3999 | /* logs */ |
| 4000 | int |
| 4001 | apply_aug(struct lys_node_augment *augment, struct unres_schema *unres) |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4002 | { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4003 | struct lys_node *child, *parent; |
| 4004 | int clear_config; |
| 4005 | unsigned int u; |
Michal Vasko | d02e30e | 2018-01-22 13:35:48 +0100 | [diff] [blame] | 4006 | uint8_t *v; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4007 | struct lys_ext_instance *ext; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4008 | |
| 4009 | assert(augment->target && (augment->flags & LYS_NOTAPPLIED)); |
| 4010 | |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4011 | if (!augment->child) { |
| 4012 | /* nothing to apply */ |
| 4013 | goto success; |
| 4014 | } |
| 4015 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4016 | /* check that all the modules are implemented */ |
| 4017 | for (parent = augment->target; parent; parent = lys_parent(parent)) { |
| 4018 | if (!lys_node_module(parent)->implemented) { |
| 4019 | if (lys_set_implemented(lys_node_module(parent))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4020 | LOGERR(augment->module->ctx, ly_errno, "Making the augment target module \"%s\" implemented failed.", |
| 4021 | lys_node_module(parent)->name); |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4022 | return -1; |
| 4023 | } |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4024 | } |
| 4025 | } |
| 4026 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4027 | /* reconnect augmenting data into the target - add them to the target child list */ |
| 4028 | if (augment->target->child) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4029 | child = augment->target->child->prev; |
| 4030 | child->next = augment->child; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4031 | augment->target->child->prev = augment->child->prev; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4032 | augment->child->prev = child; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4033 | } else { |
| 4034 | augment->target->child = augment->child; |
| 4035 | } |
| 4036 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4037 | /* inherit config information from actual parent */ |
| 4038 | for (parent = augment->target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = lys_parent(parent)); |
| 4039 | clear_config = (parent) ? 1 : 0; |
| 4040 | LY_TREE_FOR(augment->child, child) { |
| 4041 | if (inherit_config_flag(child, augment->target->flags & LYS_CONFIG_MASK, clear_config)) { |
| 4042 | return -1; |
| 4043 | } |
| 4044 | } |
| 4045 | |
| 4046 | /* inherit extensions if any */ |
| 4047 | for (u = 0; u < augment->target->ext_size; u++) { |
| 4048 | ext = augment->target->ext[u]; /* shortcut */ |
| 4049 | if (ext && ext->def->plugin && (ext->def->plugin->flags & LYEXT_OPT_INHERIT)) { |
Michal Vasko | d02e30e | 2018-01-22 13:35:48 +0100 | [diff] [blame] | 4050 | v = malloc(sizeof *v); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4051 | LY_CHECK_ERR_RETURN(!v, LOGMEM(augment->module->ctx), -1); |
Michal Vasko | d02e30e | 2018-01-22 13:35:48 +0100 | [diff] [blame] | 4052 | *v = u; |
| 4053 | if (unres_schema_add_node(lys_main_module(augment->module), unres, &augment->target->ext, |
| 4054 | UNRES_EXT_FINALIZE, (struct lys_node *)v) == -1) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4055 | /* something really bad happend since the extension finalization is not actually |
| 4056 | * being resolved while adding into unres, so something more serious with the unres |
| 4057 | * list itself must happened */ |
| 4058 | return -1; |
| 4059 | } |
| 4060 | } |
| 4061 | } |
| 4062 | |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4063 | success: |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4064 | /* remove the flag about not applicability */ |
| 4065 | augment->flags &= ~LYS_NOTAPPLIED; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4066 | return EXIT_SUCCESS; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4067 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4068 | |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4069 | static void |
| 4070 | remove_aug(struct lys_node_augment *augment) |
| 4071 | { |
| 4072 | struct lys_node *last, *elem; |
| 4073 | |
Michal Vasko | f1aa47d | 2017-09-21 12:09:29 +0200 | [diff] [blame] | 4074 | if ((augment->flags & LYS_NOTAPPLIED) || !augment->target) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4075 | /* skip already not applied augment */ |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4076 | return; |
| 4077 | } |
| 4078 | |
| 4079 | elem = augment->child; |
| 4080 | if (elem) { |
| 4081 | LY_TREE_FOR(elem, last) { |
| 4082 | if (!last->next || (last->next->parent != (struct lys_node *)augment)) { |
| 4083 | break; |
| 4084 | } |
| 4085 | } |
| 4086 | /* elem is first augment child, last is the last child */ |
| 4087 | |
| 4088 | /* parent child ptr */ |
| 4089 | if (augment->target->child == elem) { |
| 4090 | augment->target->child = last->next; |
| 4091 | } |
| 4092 | |
| 4093 | /* parent child next ptr */ |
| 4094 | if (elem->prev->next) { |
| 4095 | elem->prev->next = last->next; |
| 4096 | } |
| 4097 | |
| 4098 | /* parent child prev ptr */ |
| 4099 | if (last->next) { |
| 4100 | last->next->prev = elem->prev; |
| 4101 | } else if (augment->target->child) { |
| 4102 | augment->target->child->prev = elem->prev; |
| 4103 | } |
| 4104 | |
| 4105 | /* update augment children themselves */ |
| 4106 | elem->prev = last; |
| 4107 | last->next = NULL; |
| 4108 | } |
| 4109 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4110 | /* augment->target still keeps the resolved target, but for lys_augment_free() |
| 4111 | * we have to keep information that this augment is not applied to free its data */ |
| 4112 | augment->flags |= LYS_NOTAPPLIED; |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4113 | } |
| 4114 | |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4115 | /* |
| 4116 | * @param[in] module - the module where the deviation is defined |
| 4117 | */ |
| 4118 | static void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4119 | 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] | 4120 | { |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4121 | int ret, reapply = 0; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4122 | char *parent_path; |
| 4123 | struct lys_node *target = NULL, *parent; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4124 | struct ly_set *set; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4125 | |
| 4126 | if (!dev->deviate) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4127 | return; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4128 | } |
| 4129 | |
| 4130 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 4131 | if (dev->orig_node) { |
| 4132 | /* removing not-supported deviation ... */ |
| 4133 | if (strrchr(dev->target_name, '/') != dev->target_name) { |
| 4134 | /* ... from a parent */ |
| 4135 | |
| 4136 | /* reconnect to its previous position */ |
| 4137 | parent = dev->orig_node->parent; |
Michal Vasko | a1074a5 | 2018-01-03 12:18:53 +0100 | [diff] [blame] | 4138 | if (parent && (parent->nodetype == LYS_AUGMENT)) { |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4139 | dev->orig_node->parent = NULL; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4140 | /* the original node was actually from augment, we have to get know if the augment is |
| 4141 | * applied (its module is enabled and implemented). If yes, the node will be connected |
| 4142 | * to the augment and the linkage with the target will be fixed if needed, otherwise |
| 4143 | * it will be connected only to the augment */ |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4144 | if (!(parent->flags & LYS_NOTAPPLIED)) { |
| 4145 | /* start with removing augment if applied before adding nodes, we have to make sure |
| 4146 | * that everything will be connect correctly */ |
| 4147 | remove_aug((struct lys_node_augment *)parent); |
| 4148 | reapply = 1; |
| 4149 | } |
| 4150 | /* connect the deviated node back into the augment */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4151 | lys_node_addchild(parent, NULL, dev->orig_node); |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4152 | if (reapply) { |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4153 | /* 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] | 4154 | parent->flags |= LYS_NOTAPPLIED; /* allow apply_aug() */ |
| 4155 | apply_aug((struct lys_node_augment *)parent, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4156 | } |
Michal Vasko | a1074a5 | 2018-01-03 12:18:53 +0100 | [diff] [blame] | 4157 | } else if (parent && (parent->nodetype == LYS_USES)) { |
| 4158 | /* uses child */ |
| 4159 | lys_node_addchild(parent, NULL, dev->orig_node); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4160 | } else { |
| 4161 | /* non-augment, non-toplevel */ |
| 4162 | 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] | 4163 | ret = resolve_schema_nodeid(parent_path, NULL, module, &set, 0, 1); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4164 | free(parent_path); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4165 | if (ret == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4166 | LOGINT(module->ctx); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4167 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4168 | return; |
| 4169 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4170 | target = set->set.s[0]; |
| 4171 | ly_set_free(set); |
| 4172 | |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4173 | lys_node_addchild(target, NULL, dev->orig_node); |
| 4174 | } |
| 4175 | } else { |
| 4176 | /* ... from top-level data */ |
| 4177 | lys_node_addchild(NULL, (struct lys_module *)dev->orig_node->module, dev->orig_node); |
| 4178 | } |
| 4179 | |
| 4180 | dev->orig_node = NULL; |
| 4181 | } else { |
| 4182 | /* adding not-supported deviation */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4183 | ret = resolve_schema_nodeid(dev->target_name, NULL, module, &set, 0, 1); |
| 4184 | if (ret == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4185 | LOGINT(module->ctx); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4186 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4187 | return; |
| 4188 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4189 | target = set->set.s[0]; |
| 4190 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4191 | |
| 4192 | /* unlink and store the original node */ |
| 4193 | parent = target->parent; |
| 4194 | lys_node_unlink(target); |
Michal Vasko | a1074a5 | 2018-01-03 12:18:53 +0100 | [diff] [blame] | 4195 | if (parent && (parent->nodetype & (LYS_AUGMENT | LYS_USES))) { |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4196 | /* hack for augment, because when the original will be sometime reconnected back, we actually need |
| 4197 | * to reconnect it to both - the augment and its target (which is deduced from the deviations target |
| 4198 | * path), so we need to remember the augment as an addition */ |
Michal Vasko | a1074a5 | 2018-01-03 12:18:53 +0100 | [diff] [blame] | 4199 | /* 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] | 4200 | target->parent = parent; |
| 4201 | } |
| 4202 | dev->orig_node = target; |
| 4203 | } |
| 4204 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4205 | ret = resolve_schema_nodeid(dev->target_name, NULL, module, &set, 0, 1); |
| 4206 | if (ret == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4207 | LOGINT(module->ctx); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4208 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4209 | return; |
| 4210 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4211 | target = set->set.s[0]; |
| 4212 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4213 | |
| 4214 | lys_node_switch(target, dev->orig_node); |
| 4215 | dev->orig_node = target; |
| 4216 | } |
| 4217 | } |
| 4218 | |
| 4219 | /* temporarily removes or applies deviations, updates module deviation flag accordingly */ |
| 4220 | void |
Michal Vasko | 5a65f21 | 2018-03-19 15:08:08 +0100 | [diff] [blame] | 4221 | lys_enable_deviations(struct lys_module *module) |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4222 | { |
| 4223 | uint32_t i = 0, j; |
| 4224 | const struct lys_module *mod; |
| 4225 | const char *ptr; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4226 | struct unres_schema *unres; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4227 | |
| 4228 | if (module->deviated) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4229 | unres = calloc(1, sizeof *unres); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4230 | LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), ); |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4231 | |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4232 | while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) { |
| 4233 | if (mod == module) { |
| 4234 | continue; |
| 4235 | } |
| 4236 | |
| 4237 | for (j = 0; j < mod->deviation_size; ++j) { |
| 4238 | ptr = strstr(mod->deviation[j].target_name, module->name); |
| 4239 | if (ptr && ptr[strlen(module->name)] == ':') { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4240 | lys_switch_deviation(&mod->deviation[j], mod, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4241 | } |
| 4242 | } |
| 4243 | } |
| 4244 | |
Michal Vasko | 5a65f21 | 2018-03-19 15:08:08 +0100 | [diff] [blame] | 4245 | assert(module->deviated == 2); |
| 4246 | module->deviated = 1; |
| 4247 | |
| 4248 | for (j = 0; j < module->inc_size; j++) { |
| 4249 | if (module->inc[j].submodule->deviated) { |
| 4250 | module->inc[j].submodule->deviated = module->deviated; |
| 4251 | } |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4252 | } |
Michal Vasko | 5a65f21 | 2018-03-19 15:08:08 +0100 | [diff] [blame] | 4253 | |
| 4254 | if (unres->count) { |
| 4255 | resolve_unres_schema(module, unres); |
| 4256 | } |
| 4257 | unres_schema_free(module, &unres, 1); |
| 4258 | } |
| 4259 | } |
| 4260 | |
| 4261 | void |
| 4262 | lys_disable_deviations(struct lys_module *module) |
| 4263 | { |
| 4264 | uint32_t i, j; |
| 4265 | const struct lys_module *mod; |
| 4266 | const char *ptr; |
| 4267 | struct unres_schema *unres; |
| 4268 | |
| 4269 | if (module->deviated) { |
| 4270 | unres = calloc(1, sizeof *unres); |
| 4271 | LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), ); |
| 4272 | |
| 4273 | i = module->ctx->models.used; |
| 4274 | while (i--) { |
| 4275 | mod = module->ctx->models.list[i]; |
| 4276 | |
| 4277 | if (mod == module) { |
| 4278 | continue; |
| 4279 | } |
| 4280 | |
| 4281 | j = mod->deviation_size; |
| 4282 | while (j--) { |
| 4283 | ptr = strstr(mod->deviation[j].target_name, module->name); |
| 4284 | if (ptr && ptr[strlen(module->name)] == ':') { |
| 4285 | lys_switch_deviation(&mod->deviation[j], mod, unres); |
| 4286 | } |
| 4287 | } |
| 4288 | } |
| 4289 | |
| 4290 | assert(module->deviated == 1); |
| 4291 | module->deviated = 2; |
| 4292 | |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4293 | for (j = 0; j < module->inc_size; j++) { |
| 4294 | if (module->inc[j].submodule->deviated) { |
| 4295 | module->inc[j].submodule->deviated = module->deviated; |
| 4296 | } |
| 4297 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4298 | |
| 4299 | if (unres->count) { |
| 4300 | resolve_unres_schema(module, unres); |
| 4301 | } |
| 4302 | unres_schema_free(module, &unres, 1); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4303 | } |
| 4304 | } |
| 4305 | |
| 4306 | static void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4307 | 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] | 4308 | { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4309 | lys_switch_deviation(dev, module, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4310 | |
| 4311 | assert(dev->orig_node); |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4312 | lys_node_module(dev->orig_node)->deviated = 1; /* main module */ |
| 4313 | dev->orig_node->module->deviated = 1; /* possible submodule */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4314 | } |
| 4315 | |
| 4316 | static void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4317 | 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] | 4318 | { |
| 4319 | uint32_t idx = 0, j; |
| 4320 | const struct lys_module *mod; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4321 | struct lys_module *target_mod, *target_submod; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4322 | const char *ptr; |
| 4323 | |
| 4324 | if (dev->orig_node) { |
| 4325 | target_mod = lys_node_module(dev->orig_node); |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4326 | target_submod = dev->orig_node->module; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4327 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4328 | LOGINT(module->ctx); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4329 | return; |
| 4330 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4331 | lys_switch_deviation(dev, module, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4332 | |
| 4333 | /* clear the deviation flag if possible */ |
| 4334 | while ((mod = ly_ctx_get_module_iter(module->ctx, &idx))) { |
| 4335 | if ((mod == module) || (mod == target_mod)) { |
| 4336 | continue; |
| 4337 | } |
| 4338 | |
| 4339 | for (j = 0; j < mod->deviation_size; ++j) { |
| 4340 | ptr = strstr(mod->deviation[j].target_name, target_mod->name); |
| 4341 | if (ptr && (ptr[strlen(target_mod->name)] == ':')) { |
| 4342 | /* some other module deviation targets the inspected module, flag remains */ |
| 4343 | break; |
| 4344 | } |
| 4345 | } |
| 4346 | |
| 4347 | if (j < mod->deviation_size) { |
| 4348 | break; |
| 4349 | } |
| 4350 | } |
| 4351 | |
| 4352 | if (!mod) { |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4353 | target_mod->deviated = 0; /* main module */ |
| 4354 | target_submod->deviated = 0; /* possible submodule */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4355 | } |
| 4356 | } |
| 4357 | |
| 4358 | void |
| 4359 | lys_sub_module_apply_devs_augs(struct lys_module *module) |
| 4360 | { |
| 4361 | uint8_t u, v; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4362 | struct unres_schema *unres; |
| 4363 | |
| 4364 | unres = calloc(1, sizeof *unres); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4365 | LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), ); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4366 | |
| 4367 | /* remove applied deviations */ |
| 4368 | for (u = 0; u < module->deviation_size; ++u) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4369 | apply_dev(&module->deviation[u], module, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4370 | } |
| 4371 | /* remove applied augments */ |
| 4372 | for (u = 0; u < module->augment_size; ++u) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4373 | apply_aug(&module->augment[u], unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4374 | } |
| 4375 | |
| 4376 | /* remove deviation and augments defined in submodules */ |
| 4377 | for (v = 0; v < module->inc_size; ++v) { |
| 4378 | for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4379 | apply_dev(&module->inc[v].submodule->deviation[u], module, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | for (u = 0; u < module->inc[v].submodule->augment_size; ++u) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4383 | apply_aug(&module->inc[v].submodule->augment[u], unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4384 | } |
| 4385 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4386 | |
| 4387 | if (unres->count) { |
| 4388 | resolve_unres_schema(module, unres); |
| 4389 | } |
| 4390 | /* nothing else left to do even if something is not resolved */ |
| 4391 | unres_schema_free(module, &unres, 1); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4392 | } |
| 4393 | |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4394 | void |
| 4395 | lys_sub_module_remove_devs_augs(struct lys_module *module) |
| 4396 | { |
| 4397 | uint8_t u, v; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4398 | struct unres_schema *unres; |
| 4399 | |
| 4400 | unres = calloc(1, sizeof *unres); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4401 | LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), ); |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4402 | |
| 4403 | /* remove applied deviations */ |
| 4404 | for (u = 0; u < module->deviation_size; ++u) { |
Michal Vasko | 7427b26 | 2018-05-14 15:23:55 +0200 | [diff] [blame] | 4405 | /* the deviation could be not applied because it failed to be applied */ |
| 4406 | if (module->deviation[u].orig_node) { |
| 4407 | remove_dev(&module->deviation[u], module, unres); |
| 4408 | } |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4409 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4410 | /* remove applied augments */ |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4411 | for (u = 0; u < module->augment_size; ++u) { |
| 4412 | remove_aug(&module->augment[u]); |
| 4413 | } |
| 4414 | |
| 4415 | /* remove deviation and augments defined in submodules */ |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 4416 | for (v = 0; v < module->inc_size && module->inc[v].submodule; ++v) { |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4417 | for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) { |
Michal Vasko | 7427b26 | 2018-05-14 15:23:55 +0200 | [diff] [blame] | 4418 | if (module->inc[v].submodule->deviation[u].orig_node) { |
| 4419 | remove_dev(&module->inc[v].submodule->deviation[u], module, unres); |
| 4420 | } |
Radek Krejci | dbc1526 | 2016-06-16 14:58:29 +0200 | [diff] [blame] | 4421 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4422 | |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4423 | for (u = 0; u < module->inc[v].submodule->augment_size; ++u) { |
| 4424 | remove_aug(&module->inc[v].submodule->augment[u]); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4425 | } |
| 4426 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4427 | |
| 4428 | if (unres->count) { |
| 4429 | resolve_unres_schema(module, unres); |
| 4430 | } |
| 4431 | /* nothing else left to do even if something is not resolved */ |
| 4432 | unres_schema_free(module, &unres, 1); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4433 | } |
| 4434 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4435 | static int |
| 4436 | lys_set_implemented_recursion(struct lys_module *module, struct unres_schema *unres) |
| 4437 | { |
| 4438 | struct lys_node *root, *next, *node; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 4439 | uint16_t i, j; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4440 | |
| 4441 | for (i = 0; i < module->augment_size; i++) { |
| 4442 | /* apply augment */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4443 | assert(module->augment[i].target); |
| 4444 | if (apply_aug(&module->augment[i], unres)) { |
| 4445 | return EXIT_FAILURE; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4446 | } |
| 4447 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 4448 | |
| 4449 | /* identities */ |
| 4450 | for (i = 0; i < module->ident_size; i++) { |
| 4451 | for (j = 0; j < module->ident[i].base_size; j++) { |
| 4452 | resolve_identity_backlink_update(&module->ident[i], module->ident[i].base[j]); |
| 4453 | } |
| 4454 | } |
| 4455 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4456 | LY_TREE_FOR(module->data, root) { |
| 4457 | /* handle leafrefs and recursively change the implemented flags in the leafref targets */ |
| 4458 | LY_TREE_DFS_BEGIN(root, next, node) { |
| 4459 | if (node->nodetype == LYS_GROUPING) { |
| 4460 | goto nextsibling; |
| 4461 | } |
| 4462 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4463 | if (((struct lys_node_leaf *)node)->type.base == LY_TYPE_LEAFREF) { |
| 4464 | if (unres_schema_add_node(module, unres, &((struct lys_node_leaf *)node)->type, |
| 4465 | UNRES_TYPE_LEAFREF, node) == -1) { |
| 4466 | return EXIT_FAILURE; |
| 4467 | } |
| 4468 | } |
| 4469 | } |
| 4470 | |
| 4471 | /* modified LY_TREE_DFS_END */ |
| 4472 | next = node->child; |
| 4473 | /* child exception for leafs, leaflists and anyxml without children */ |
| 4474 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 4475 | next = NULL; |
| 4476 | } |
| 4477 | if (!next) { |
| 4478 | nextsibling: |
| 4479 | /* no children */ |
| 4480 | if (node == root) { |
| 4481 | /* we are done, root has no children */ |
| 4482 | break; |
| 4483 | } |
| 4484 | /* try siblings */ |
| 4485 | next = node->next; |
| 4486 | } |
| 4487 | while (!next) { |
| 4488 | /* parent is already processed, go to its sibling */ |
| 4489 | node = lys_parent(node); |
| 4490 | /* no siblings, go back through parents */ |
| 4491 | if (lys_parent(node) == lys_parent(root)) { |
| 4492 | /* we are done, no next element to process */ |
| 4493 | break; |
| 4494 | } |
| 4495 | next = node->next; |
| 4496 | } |
| 4497 | } |
| 4498 | } |
| 4499 | |
| 4500 | return EXIT_SUCCESS; |
| 4501 | } |
| 4502 | |
| 4503 | API int |
| 4504 | lys_set_implemented(const struct lys_module *module) |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4505 | { |
| 4506 | struct ly_ctx *ctx; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4507 | struct unres_schema *unres; |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 4508 | int i, j, k, disabled = 0; |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4509 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4510 | if (!module) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4511 | LOGARG; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4512 | return EXIT_FAILURE; |
| 4513 | } |
| 4514 | |
| 4515 | module = lys_main_module(module); |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4516 | |
| 4517 | if (module->disabled) { |
| 4518 | disabled = 1; |
| 4519 | lys_set_enabled(module); |
| 4520 | } |
| 4521 | |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4522 | if (module->implemented) { |
| 4523 | return EXIT_SUCCESS; |
| 4524 | } |
| 4525 | |
| 4526 | ctx = module->ctx; |
| 4527 | |
| 4528 | for (i = 0; i < ctx->models.used; ++i) { |
| 4529 | if (module == ctx->models.list[i]) { |
| 4530 | continue; |
| 4531 | } |
| 4532 | |
| 4533 | 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] | 4534 | 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] | 4535 | if (disabled) { |
| 4536 | /* set it back disabled */ |
| 4537 | lys_set_disabled(module); |
| 4538 | } |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4539 | return EXIT_FAILURE; |
| 4540 | } |
| 4541 | } |
| 4542 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4543 | unres = calloc(1, sizeof *unres); |
| 4544 | if (!unres) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4545 | LOGMEM(ctx); |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4546 | if (disabled) { |
| 4547 | /* set it back disabled */ |
| 4548 | lys_set_disabled(module); |
| 4549 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4550 | return EXIT_FAILURE; |
| 4551 | } |
| 4552 | /* recursively make the module implemented */ |
| 4553 | ((struct lys_module *)module)->implemented = 1; |
| 4554 | if (lys_set_implemented_recursion((struct lys_module *)module, unres)) { |
| 4555 | goto error; |
| 4556 | } |
| 4557 | /* process augments in submodules */ |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 4558 | for (i = 0; i < module->inc_size && module->inc[i].submodule; ++i) { |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4559 | for (j = 0; j < module->inc[i].submodule->augment_size; j++) { |
| 4560 | /* apply augment */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4561 | assert(module->inc[i].submodule->augment[j].target); |
| 4562 | if (apply_aug(&module->inc[i].submodule->augment[j], unres)) { |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4563 | goto error; |
| 4564 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4565 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 4566 | |
| 4567 | /* identities */ |
| 4568 | for (j = 0; j < module->inc[i].submodule->ident_size; j++) { |
| 4569 | for (k = 0; k < module->inc[i].submodule->ident[j].base_size; k++) { |
| 4570 | resolve_identity_backlink_update(&module->inc[i].submodule->ident[j], |
| 4571 | module->inc[i].submodule->ident[j].base[k]); |
| 4572 | } |
| 4573 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4574 | } |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4575 | /* try again resolve augments in other modules possibly augmenting this one, |
| 4576 | * since we have just enabled it |
| 4577 | */ |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4578 | /* resolve rest of unres items */ |
| 4579 | if (unres->count && resolve_unres_schema((struct lys_module *)module, unres)) { |
| 4580 | goto error; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4581 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4582 | unres_schema_free(NULL, &unres, 0); |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4583 | |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4584 | /* reflect implemented flag in submodules */ |
| 4585 | for (i = 0; i < module->inc_size; i++) { |
| 4586 | module->inc[i].submodule->implemented = 1; |
| 4587 | } |
| 4588 | |
Michal Vasko | be136f6 | 2017-09-21 12:08:39 +0200 | [diff] [blame] | 4589 | LOGVRB("Module \"%s%s%s\" now implemented.", module->name, (module->rev_size ? "@" : ""), |
| 4590 | (module->rev_size ? module->rev[0].date : "")); |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4591 | return EXIT_SUCCESS; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4592 | |
| 4593 | error: |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4594 | if (disabled) { |
| 4595 | /* set it back disabled */ |
| 4596 | lys_set_disabled(module); |
| 4597 | } |
| 4598 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4599 | ((struct lys_module *)module)->implemented = 0; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4600 | unres_schema_free((struct lys_module *)module, &unres, 1); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4601 | return EXIT_FAILURE; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4602 | } |
| 4603 | |
| 4604 | void |
| 4605 | lys_submodule_module_data_free(struct lys_submodule *submodule) |
| 4606 | { |
| 4607 | struct lys_node *next, *elem; |
| 4608 | |
| 4609 | /* remove parsed data */ |
| 4610 | LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) { |
| 4611 | if (elem->module == (struct lys_module *)submodule) { |
| 4612 | lys_node_free(elem, NULL, 0); |
| 4613 | } |
| 4614 | } |
| 4615 | } |
Radek Krejci | a1c33bf | 2016-09-07 12:38:49 +0200 | [diff] [blame] | 4616 | |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4617 | API char * |
Michal Vasko | 395b0a0 | 2018-01-22 09:36:20 +0100 | [diff] [blame] | 4618 | lys_path(const struct lys_node *node, int options) |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4619 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4620 | char *buf = NULL; |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4621 | |
| 4622 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4623 | LOGARG; |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4624 | return NULL; |
| 4625 | } |
| 4626 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4627 | if (ly_vlog_build_path(LY_VLOG_LYS, node, &buf, !options)) { |
Michal Vasko | 59631dd | 2017-10-02 11:56:11 +0200 | [diff] [blame] | 4628 | return NULL; |
| 4629 | } |
| 4630 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4631 | return buf; |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4632 | } |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 4633 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4634 | API char * |
| 4635 | lys_data_path(const struct lys_node *node) |
| 4636 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4637 | char *result = NULL, buf[1024]; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 4638 | const char *separator, *name; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4639 | int i, used; |
| 4640 | struct ly_set *set; |
| 4641 | const struct lys_module *prev_mod; |
| 4642 | |
| 4643 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4644 | LOGARG; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4645 | return NULL; |
| 4646 | } |
| 4647 | |
Michal Vasko | d60a1a3 | 2018-05-23 16:31:22 +0200 | [diff] [blame^] | 4648 | buf[0] = '\0'; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4649 | set = ly_set_new(); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4650 | LY_CHECK_ERR_GOTO(!set, LOGMEM(node->module->ctx), cleanup); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4651 | |
| 4652 | while (node) { |
| 4653 | ly_set_add(set, (void *)node, 0); |
| 4654 | do { |
| 4655 | node = lys_parent(node); |
| 4656 | } while (node && (node->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))); |
| 4657 | } |
| 4658 | |
| 4659 | prev_mod = NULL; |
| 4660 | used = 0; |
| 4661 | for (i = set->number - 1; i > -1; --i) { |
| 4662 | node = set->set.s[i]; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 4663 | if (node->nodetype == LYS_EXT) { |
| 4664 | if (strcmp(((struct lys_ext_instance *)node)->def->name, "yang-data")) { |
| 4665 | continue; |
| 4666 | } |
| 4667 | name = ((struct lys_ext_instance *)node)->arg_value; |
| 4668 | separator = ":#"; |
| 4669 | } else { |
| 4670 | name = node->name; |
| 4671 | separator = ":"; |
| 4672 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4673 | 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] | 4674 | (lys_node_module(node) == prev_mod ? "" : separator), name); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4675 | prev_mod = lys_node_module(node); |
| 4676 | } |
| 4677 | |
| 4678 | result = strdup(buf); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4679 | LY_CHECK_ERR_GOTO(!result, LOGMEM(node->module->ctx), cleanup); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4680 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4681 | cleanup: |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4682 | ly_set_free(set); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4683 | return result; |
| 4684 | } |
| 4685 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4686 | struct lys_node_augment * |
| 4687 | lys_getnext_target_aug(struct lys_node_augment *last, const struct lys_module *mod, const struct lys_node *aug_target) |
| 4688 | { |
| 4689 | int i, j, last_found; |
| 4690 | |
Michal Vasko | b690687 | 2018-03-12 11:35:09 +0100 | [diff] [blame] | 4691 | assert(mod && aug_target); |
| 4692 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 4693 | if (!last) { |
| 4694 | last_found = 1; |
| 4695 | } else { |
| 4696 | last_found = 0; |
| 4697 | } |
| 4698 | |
| 4699 | /* search module augments */ |
| 4700 | for (i = 0; i < mod->augment_size; ++i) { |
| 4701 | if (!mod->augment[i].target) { |
| 4702 | /* still unresolved, skip */ |
| 4703 | continue; |
| 4704 | } |
| 4705 | |
| 4706 | if (mod->augment[i].target == aug_target) { |
| 4707 | if (last_found) { |
| 4708 | /* next match after last */ |
| 4709 | return &mod->augment[i]; |
| 4710 | } |
| 4711 | |
| 4712 | if (&mod->augment[i] == last) { |
| 4713 | last_found = 1; |
| 4714 | } |
| 4715 | } |
| 4716 | } |
| 4717 | |
| 4718 | /* search submodule augments */ |
| 4719 | for (i = 0; i < mod->inc_size; ++i) { |
| 4720 | for (j = 0; j < mod->inc[i].submodule->augment_size; ++j) { |
| 4721 | if (!mod->inc[i].submodule->augment[j].target) { |
| 4722 | continue; |
| 4723 | } |
| 4724 | |
| 4725 | if (mod->inc[i].submodule->augment[j].target == aug_target) { |
| 4726 | if (last_found) { |
| 4727 | /* next match after last */ |
| 4728 | return &mod->inc[i].submodule->augment[j]; |
| 4729 | } |
| 4730 | |
| 4731 | if (&mod->inc[i].submodule->augment[j] == last) { |
| 4732 | last_found = 1; |
| 4733 | } |
| 4734 | } |
| 4735 | } |
| 4736 | } |
| 4737 | |
| 4738 | return NULL; |
| 4739 | } |
| 4740 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4741 | API struct ly_set * |
| 4742 | lys_find_path(const struct lys_module *cur_module, const struct lys_node *cur_node, const char *path) |
| 4743 | { |
| 4744 | struct ly_set *ret; |
| 4745 | int rc; |
| 4746 | |
| 4747 | if ((!cur_module && !cur_node) || !path) { |
| 4748 | return NULL; |
| 4749 | } |
| 4750 | |
| 4751 | rc = resolve_schema_nodeid(path, cur_node, cur_module, &ret, 1, 1); |
| 4752 | if (rc == -1) { |
| 4753 | return NULL; |
| 4754 | } |
| 4755 | |
| 4756 | return ret; |
| 4757 | } |
| 4758 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4759 | static void |
| 4760 | lys_extcomplex_free_str(struct ly_ctx *ctx, struct lys_ext_instance_complex *ext, LY_STMT stmt) |
| 4761 | { |
| 4762 | struct lyext_substmt *info; |
Radek Krejci | b71243e | 2017-02-08 16:20:08 +0100 | [diff] [blame] | 4763 | const char **str, ***a; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4764 | int c; |
| 4765 | |
| 4766 | str = lys_ext_complex_get_substmt(stmt, ext, &info); |
| 4767 | if (!str || !(*str)) { |
| 4768 | return; |
| 4769 | } |
| 4770 | if (info->cardinality >= LY_STMT_CARD_SOME) { |
| 4771 | /* we have array */ |
Radek Krejci | b71243e | 2017-02-08 16:20:08 +0100 | [diff] [blame] | 4772 | a = (const char ***)str; |
Radek Krejci | 56c8041 | 2017-02-09 10:44:16 +0100 | [diff] [blame] | 4773 | for (str = (*(const char ***)str), c = 0; str[c]; c++) { |
| 4774 | lydict_remove(ctx, str[c]); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4775 | } |
Radek Krejci | 56c8041 | 2017-02-09 10:44:16 +0100 | [diff] [blame] | 4776 | free(a[0]); |
| 4777 | if (stmt == LY_STMT_BELONGSTO) { |
| 4778 | for (str = a[1], c = 0; str[c]; c++) { |
| 4779 | lydict_remove(ctx, str[c]); |
| 4780 | } |
| 4781 | free(a[1]); |
PavolVican | 99c7072 | 2017-02-18 17:25:52 +0100 | [diff] [blame] | 4782 | } else if (stmt == LY_STMT_ARGUMENT) { |
| 4783 | free(a[1]); |
Radek Krejci | 56c8041 | 2017-02-09 10:44:16 +0100 | [diff] [blame] | 4784 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4785 | } else { |
Radek Krejci | 56c8041 | 2017-02-09 10:44:16 +0100 | [diff] [blame] | 4786 | lydict_remove(ctx, str[0]); |
| 4787 | if (stmt == LY_STMT_BELONGSTO) { |
| 4788 | lydict_remove(ctx, str[1]); |
| 4789 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4790 | } |
| 4791 | } |
| 4792 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4793 | lys_extension_instances_free(struct ly_ctx *ctx, struct lys_ext_instance **e, unsigned int size, |
| 4794 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4795 | { |
Radek Krejci | f8d05c2 | 2017-02-10 15:33:35 +0100 | [diff] [blame] | 4796 | unsigned int i, j, k; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4797 | struct lyext_substmt *substmt; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4798 | void **pp, **start; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4799 | struct lys_node *siter, *snext; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4800 | |
| 4801 | #define EXTCOMPLEX_FREE_STRUCT(STMT, TYPE, FUNC, FREE, ARGS...) \ |
Radek Krejci | b84686f | 2017-02-09 16:04:55 +0100 | [diff] [blame] | 4802 | 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] | 4803 | if (!pp || !(*pp)) { break; } \ |
Radek Krejci | b84686f | 2017-02-09 16:04:55 +0100 | [diff] [blame] | 4804 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */ \ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4805 | for (start = pp = *pp; *pp; pp++) { \ |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4806 | FUNC(ctx, (TYPE *)(*pp), ##ARGS, private_destructor); \ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4807 | if (FREE) { free(*pp); } \ |
| 4808 | } \ |
| 4809 | free(start); \ |
| 4810 | } else { /* single item */ \ |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4811 | FUNC(ctx, (TYPE *)(*pp), ##ARGS, private_destructor); \ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4812 | if (FREE) { free(*pp); } \ |
| 4813 | } |
| 4814 | |
PavolVican | 9d61d40 | 2018-02-05 15:52:48 +0100 | [diff] [blame] | 4815 | if (!size || !e) { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4816 | return; |
| 4817 | } |
| 4818 | |
| 4819 | for (i = 0; i < size; i++) { |
| 4820 | if (!e[i]) { |
| 4821 | continue; |
| 4822 | } |
| 4823 | |
| 4824 | if (e[i]->flags & (LYEXT_OPT_INHERIT)) { |
| 4825 | /* no free, this is just a shadow copy of the original extension instance */ |
| 4826 | } else { |
| 4827 | if (e[i]->flags & (LYEXT_OPT_YANG)) { |
| 4828 | free(e[i]->def); /* remove name of instance extension */ |
PavolVican | 19dc615 | 2017-02-06 12:04:15 +0100 | [diff] [blame] | 4829 | e[i]->def = NULL; |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 4830 | 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] | 4831 | } |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4832 | /* remove private object */ |
| 4833 | if (e[i]->priv && private_destructor) { |
| 4834 | private_destructor((struct lys_node*)e[i], e[i]->priv); |
| 4835 | } |
| 4836 | 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] | 4837 | lydict_remove(ctx, e[i]->arg_value); |
| 4838 | } |
| 4839 | |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 4840 | if (e[i]->def && e[i]->def->plugin && e[i]->def->plugin->type == LYEXT_COMPLEX |
| 4841 | && ((e[i]->flags & LYEXT_OPT_CONTENT) == 0)) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 4842 | substmt = ((struct lys_ext_instance_complex *)e[i])->substmt; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4843 | for (j = 0; substmt[j].stmt; j++) { |
| 4844 | switch(substmt[j].stmt) { |
| 4845 | case LY_STMT_DESCRIPTION: |
| 4846 | case LY_STMT_REFERENCE: |
| 4847 | case LY_STMT_UNITS: |
Radek Krejci | b71243e | 2017-02-08 16:20:08 +0100 | [diff] [blame] | 4848 | case LY_STMT_ARGUMENT: |
| 4849 | case LY_STMT_DEFAULT: |
| 4850 | case LY_STMT_ERRTAG: |
| 4851 | case LY_STMT_ERRMSG: |
| 4852 | case LY_STMT_PREFIX: |
| 4853 | case LY_STMT_NAMESPACE: |
| 4854 | case LY_STMT_PRESENCE: |
| 4855 | case LY_STMT_REVISIONDATE: |
| 4856 | case LY_STMT_KEY: |
| 4857 | case LY_STMT_BASE: |
| 4858 | case LY_STMT_BELONGSTO: |
| 4859 | case LY_STMT_CONTACT: |
| 4860 | case LY_STMT_ORGANIZATION: |
| 4861 | case LY_STMT_PATH: |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4862 | lys_extcomplex_free_str(ctx, (struct lys_ext_instance_complex *)e[i], substmt[j].stmt); |
| 4863 | break; |
| 4864 | case LY_STMT_TYPE: |
| 4865 | EXTCOMPLEX_FREE_STRUCT(LY_STMT_TYPE, struct lys_type, lys_type_free, 1); |
| 4866 | break; |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 4867 | case LY_STMT_TYPEDEF: |
| 4868 | EXTCOMPLEX_FREE_STRUCT(LY_STMT_TYPEDEF, struct lys_tpdf, lys_tpdf_free, 1); |
| 4869 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4870 | case LY_STMT_IFFEATURE: |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 4871 | 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] | 4872 | break; |
Radek Krejci | 5496fae | 2017-02-10 13:26:48 +0100 | [diff] [blame] | 4873 | case LY_STMT_MAX: |
| 4874 | case LY_STMT_MIN: |
| 4875 | case LY_STMT_POSITION: |
PavolVican | 2ed9f4e | 2017-02-16 00:08:45 +0100 | [diff] [blame] | 4876 | case LY_STMT_VALUE: |
Radek Krejci | f8d05c2 | 2017-02-10 15:33:35 +0100 | [diff] [blame] | 4877 | 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] | 4878 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME && *pp) { |
Radek Krejci | f8d05c2 | 2017-02-10 15:33:35 +0100 | [diff] [blame] | 4879 | for(k = 0; ((uint32_t**)(*pp))[k]; k++) { |
| 4880 | free(((uint32_t**)(*pp))[k]); |
| 4881 | } |
| 4882 | } |
| 4883 | free(*pp); |
| 4884 | break; |
| 4885 | case LY_STMT_DIGITS: |
Radek Krejci | b84686f | 2017-02-09 16:04:55 +0100 | [diff] [blame] | 4886 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { |
| 4887 | /* free the array */ |
| 4888 | pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset]; |
| 4889 | free(*pp); |
| 4890 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4891 | break; |
Radek Krejci | 37f9ba3 | 2017-02-10 16:50:35 +0100 | [diff] [blame] | 4892 | case LY_STMT_MODULE: |
| 4893 | /* modules are part of the context, so they will be freed there */ |
| 4894 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { |
| 4895 | /* free the array */ |
| 4896 | pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset]; |
| 4897 | free(*pp); |
| 4898 | } |
| 4899 | break; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4900 | case LY_STMT_ACTION: |
Radek Krejci | b31762b | 2017-02-15 10:48:42 +0100 | [diff] [blame] | 4901 | case LY_STMT_ANYDATA: |
| 4902 | case LY_STMT_ANYXML: |
| 4903 | case LY_STMT_CASE: |
| 4904 | case LY_STMT_CHOICE: |
| 4905 | case LY_STMT_CONTAINER: |
| 4906 | case LY_STMT_GROUPING: |
| 4907 | case LY_STMT_INPUT: |
| 4908 | case LY_STMT_LEAF: |
| 4909 | case LY_STMT_LEAFLIST: |
| 4910 | case LY_STMT_LIST: |
| 4911 | case LY_STMT_NOTIFICATION: |
| 4912 | case LY_STMT_OUTPUT: |
| 4913 | case LY_STMT_RPC: |
| 4914 | case LY_STMT_USES: |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4915 | pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset]; |
| 4916 | LY_TREE_FOR_SAFE((struct lys_node *)(*pp), snext, siter) { |
| 4917 | lys_node_free(siter, NULL, 0); |
| 4918 | } |
Radek Krejci | b31762b | 2017-02-15 10:48:42 +0100 | [diff] [blame] | 4919 | *pp = NULL; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4920 | break; |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 4921 | case LY_STMT_UNIQUE: |
| 4922 | pp = lys_ext_complex_get_substmt(LY_STMT_UNIQUE, (struct lys_ext_instance_complex *)e[i], NULL); |
| 4923 | if (!pp || !(*pp)) { |
| 4924 | break; |
| 4925 | } |
| 4926 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */ |
| 4927 | for (start = pp = *pp; *pp; pp++) { |
| 4928 | for (k = 0; k < (*(struct lys_unique**)pp)->expr_size; k++) { |
| 4929 | lydict_remove(ctx, (*(struct lys_unique**)pp)->expr[k]); |
| 4930 | } |
| 4931 | free((*(struct lys_unique**)pp)->expr); |
| 4932 | free(*pp); |
| 4933 | } |
| 4934 | free(start); |
| 4935 | } else { /* single item */ |
| 4936 | for (k = 0; k < (*(struct lys_unique**)pp)->expr_size; k++) { |
| 4937 | lydict_remove(ctx, (*(struct lys_unique**)pp)->expr[k]); |
| 4938 | } |
| 4939 | free((*(struct lys_unique**)pp)->expr); |
| 4940 | free(*pp); |
| 4941 | } |
| 4942 | break; |
Radek Krejci | aa9c520 | 2017-02-15 16:10:14 +0100 | [diff] [blame] | 4943 | case LY_STMT_LENGTH: |
| 4944 | case LY_STMT_MUST: |
| 4945 | case LY_STMT_PATTERN: |
| 4946 | case LY_STMT_RANGE: |
| 4947 | EXTCOMPLEX_FREE_STRUCT(substmt[j].stmt, struct lys_restr, lys_restr_free, 1); |
| 4948 | break; |
Radek Krejci | c5cc530 | 2017-02-16 10:07:46 +0100 | [diff] [blame] | 4949 | case LY_STMT_WHEN: |
| 4950 | EXTCOMPLEX_FREE_STRUCT(LY_STMT_WHEN, struct lys_when, lys_when_free, 0); |
| 4951 | break; |
Radek Krejci | 7417a08 | 2017-02-16 11:07:59 +0100 | [diff] [blame] | 4952 | case LY_STMT_REVISION: |
| 4953 | pp = lys_ext_complex_get_substmt(LY_STMT_REVISION, (struct lys_ext_instance_complex *)e[i], NULL); |
| 4954 | if (!pp || !(*pp)) { |
| 4955 | break; |
| 4956 | } |
| 4957 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */ |
| 4958 | for (start = pp = *pp; *pp; pp++) { |
| 4959 | lydict_remove(ctx, (*(struct lys_revision**)pp)->dsc); |
| 4960 | lydict_remove(ctx, (*(struct lys_revision**)pp)->ref); |
| 4961 | lys_extension_instances_free(ctx, (*(struct lys_revision**)pp)->ext, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4962 | (*(struct lys_revision**)pp)->ext_size, private_destructor); |
Radek Krejci | 7417a08 | 2017-02-16 11:07:59 +0100 | [diff] [blame] | 4963 | free(*pp); |
| 4964 | } |
| 4965 | free(start); |
| 4966 | } else { /* single item */ |
| 4967 | lydict_remove(ctx, (*(struct lys_revision**)pp)->dsc); |
| 4968 | lydict_remove(ctx, (*(struct lys_revision**)pp)->ref); |
| 4969 | lys_extension_instances_free(ctx, (*(struct lys_revision**)pp)->ext, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4970 | (*(struct lys_revision**)pp)->ext_size, private_destructor); |
Radek Krejci | 7417a08 | 2017-02-16 11:07:59 +0100 | [diff] [blame] | 4971 | free(*pp); |
| 4972 | } |
| 4973 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4974 | default: |
Radek Krejci | b84686f | 2017-02-09 16:04:55 +0100 | [diff] [blame] | 4975 | /* nothing to free */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 4976 | break; |
| 4977 | } |
| 4978 | } |
| 4979 | } |
| 4980 | |
| 4981 | free(e[i]); |
| 4982 | } |
| 4983 | free(e); |
| 4984 | |
| 4985 | #undef EXTCOMPLEX_FREE_STRUCT |
| 4986 | } |