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 | 72cdfac | 2018-08-15 14:47:33 +0200 | [diff] [blame] | 31 | #include <dirent.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 32 | |
| 33 | #include "common.h" |
| 34 | #include "context.h" |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 35 | #include "parser.h" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 36 | #include "resolve.h" |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 37 | #include "xml.h" |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 38 | #include "xpath.h" |
Michal Vasko | fc5744d | 2015-10-22 12:09:34 +0200 | [diff] [blame] | 39 | #include "xml_internal.h" |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 40 | #include "tree_internal.h" |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 41 | #include "validation.h" |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 42 | #include "parser_yang.h" |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 43 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 44 | 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] | 45 | int in_grp, int shallow, struct unres_schema *unres); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 46 | |
Radek Krejci | 65aca41 | 2018-01-24 11:23:06 +0100 | [diff] [blame] | 47 | API const struct lys_node_list * |
| 48 | lys_is_key(const struct lys_node_leaf *node, uint8_t *index) |
| 49 | { |
| 50 | struct lys_node *parent = (struct lys_node *)node; |
| 51 | struct lys_node_list *list; |
| 52 | uint8_t i; |
| 53 | |
| 54 | if (!node || node->nodetype != LYS_LEAF) { |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | do { |
| 59 | parent = lys_parent(parent); |
| 60 | } while (parent && parent->nodetype == LYS_USES); |
| 61 | |
| 62 | if (!parent || parent->nodetype != LYS_LIST) { |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | list = (struct lys_node_list*)parent; |
| 67 | for (i = 0; i < list->keys_size; i++) { |
| 68 | if (list->keys[i] == node) { |
| 69 | if (index) { |
| 70 | (*index) = i; |
| 71 | } |
| 72 | return list; |
| 73 | } |
| 74 | } |
| 75 | return NULL; |
| 76 | } |
| 77 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 78 | API const struct lys_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 79 | lys_is_disabled(const struct lys_node *node, int recursive) |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 80 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 81 | int i; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 82 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 83 | if (!node) { |
| 84 | return NULL; |
| 85 | } |
| 86 | |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 87 | check: |
| 88 | if (node->nodetype != LYS_INPUT && node->nodetype != LYS_OUTPUT) { |
| 89 | /* input/output does not have if-feature, so skip them */ |
| 90 | |
| 91 | /* check local if-features */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 92 | for (i = 0; i < node->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 93 | if (!resolve_iffeature(&node->iffeature[i])) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 94 | return node; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if (!recursive) { |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | /* go through parents */ |
| 104 | if (node->nodetype == LYS_AUGMENT) { |
| 105 | /* go to parent actually means go to the target node */ |
| 106 | node = ((struct lys_node_augment *)node)->target; |
Michal Vasko | 1754919 | 2017-03-13 10:19:33 +0100 | [diff] [blame] | 107 | if (!node) { |
| 108 | /* unresolved augment, let's say it's enabled */ |
| 109 | return NULL; |
| 110 | } |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 111 | } else if (node->parent) { |
| 112 | node = node->parent; |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 113 | } else { |
| 114 | return NULL; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 117 | if (recursive == 2) { |
| 118 | /* continue only if the node cannot have a data instance */ |
| 119 | if (node->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST)) { |
| 120 | return NULL; |
| 121 | } |
| 122 | } |
| 123 | goto check; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Michal Vasko | b69d243 | 2018-09-21 13:26:22 +0200 | [diff] [blame] | 126 | API const struct lys_type * |
| 127 | lys_getnext_union_type(const struct lys_type *last, const struct lys_type *type) |
| 128 | { |
| 129 | int found = 0; |
| 130 | |
| 131 | if (!type || (type->base != LY_TYPE_UNION)) { |
Michal Vasko | 79d5d27 | 2018-09-21 13:28:22 +0200 | [diff] [blame] | 132 | return NULL; |
Michal Vasko | b69d243 | 2018-09-21 13:26:22 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Michal Vasko | 79d5d27 | 2018-09-21 13:28:22 +0200 | [diff] [blame] | 135 | return lyp_get_next_union_type((struct lys_type *)type, (struct lys_type *)last, &found); |
Michal Vasko | b69d243 | 2018-09-21 13:26:22 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 138 | int |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 139 | lys_get_sibling(const struct lys_node *siblings, const char *mod_name, int mod_name_len, const char *name, |
| 140 | int nam_len, LYS_NODE type, const struct lys_node **ret) |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 141 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 142 | const struct lys_node *node, *parent = NULL; |
| 143 | const struct lys_module *mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 144 | const char *node_mod_name; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 145 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 146 | assert(siblings && mod_name && name); |
Michal Vasko | 165dc4a | 2015-10-23 09:44:27 +0200 | [diff] [blame] | 147 | assert(!(type & (LYS_USES | LYS_GROUPING))); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 148 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 149 | /* fill the lengths in case the caller is so indifferent */ |
| 150 | if (!mod_name_len) { |
| 151 | mod_name_len = strlen(mod_name); |
| 152 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 153 | if (!nam_len) { |
| 154 | nam_len = strlen(name); |
| 155 | } |
| 156 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 157 | while (siblings && (siblings->nodetype == LYS_USES)) { |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 158 | siblings = siblings->child; |
| 159 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 160 | if (!siblings) { |
| 161 | /* unresolved uses */ |
| 162 | return EXIT_FAILURE; |
| 163 | } |
| 164 | |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 165 | if (siblings->nodetype == LYS_GROUPING) { |
| 166 | for (node = siblings; (node->nodetype == LYS_GROUPING) && (node->prev != siblings); node = node->prev); |
| 167 | if (node->nodetype == LYS_GROUPING) { |
| 168 | /* we went through all the siblings, only groupings there - no valid sibling */ |
| 169 | return EXIT_FAILURE; |
| 170 | } |
| 171 | /* update siblings to be valid */ |
| 172 | siblings = node; |
| 173 | } |
| 174 | |
Michal Vasko | 4cf7dba | 2016-10-14 09:42:01 +0200 | [diff] [blame] | 175 | /* set parent correctly */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 176 | parent = lys_parent(siblings); |
Michal Vasko | 4cf7dba | 2016-10-14 09:42:01 +0200 | [diff] [blame] | 177 | |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 178 | /* go up all uses */ |
| 179 | while (parent && (parent->nodetype == LYS_USES)) { |
| 180 | parent = lys_parent(parent); |
Michal Vasko | 4cf7dba | 2016-10-14 09:42:01 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 183 | if (!parent) { |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 184 | /* handle situation when there is a top-level uses referencing a foreign grouping */ |
| 185 | for (node = siblings; lys_parent(node) && (node->nodetype == LYS_USES); node = lys_parent(node)); |
| 186 | mod = lys_node_module(node); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 189 | /* try to find the node */ |
| 190 | node = NULL; |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 191 | 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] | 192 | if (!type || (node->nodetype & type)) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 193 | /* module name comparison */ |
| 194 | node_mod_name = lys_node_module(node)->name; |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 195 | 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] | 196 | continue; |
| 197 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 198 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 199 | /* direct name check */ |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 200 | 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] | 201 | if (ret) { |
| 202 | *ret = node; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 203 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 204 | return EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 205 | } |
| 206 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | return EXIT_FAILURE; |
| 210 | } |
| 211 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 212 | int |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 213 | lys_getnext_data(const struct lys_module *mod, const struct lys_node *parent, const char *name, int nam_len, |
| 214 | LYS_NODE type, const struct lys_node **ret) |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 215 | { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 216 | const struct lys_node *node; |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 217 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 218 | assert((mod || parent) && name); |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 219 | assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))); |
| 220 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 221 | if (!mod) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 222 | mod = lys_node_module(parent); |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 223 | } |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 224 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 225 | /* try to find the node */ |
| 226 | node = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 227 | while ((node = lys_getnext(node, parent, mod, 0))) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 228 | if (!type || (node->nodetype & type)) { |
| 229 | /* module check */ |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 230 | if (lys_node_module(node) != lys_main_module(mod)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 231 | continue; |
| 232 | } |
| 233 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 234 | /* direct name check */ |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 235 | if (!strncmp(node->name, name, nam_len) && !node->name[nam_len]) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 236 | if (ret) { |
| 237 | *ret = node; |
| 238 | } |
| 239 | return EXIT_SUCCESS; |
| 240 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 241 | } |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | return EXIT_FAILURE; |
| 245 | } |
| 246 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 247 | API const struct lys_node * |
| 248 | 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] | 249 | { |
Michal Vasko | 5a9c24b | 2017-03-13 09:25:58 +0100 | [diff] [blame] | 250 | const struct lys_node *next, *aug_parent; |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 251 | struct lys_node **snode; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 252 | |
Michal Vasko | 75702cd | 2018-02-12 11:27:09 +0100 | [diff] [blame] | 253 | 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] | 254 | LOGARG; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 255 | return NULL; |
| 256 | } |
| 257 | |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 258 | if (!last) { |
| 259 | /* first call */ |
| 260 | |
| 261 | /* get know where to start */ |
| 262 | if (parent) { |
| 263 | /* schema subtree */ |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 264 | snode = lys_child(parent, LYS_UNKNOWN); |
Michal Vasko | 5a9c24b | 2017-03-13 09:25:58 +0100 | [diff] [blame] | 265 | /* do not return anything if the augment does not have any children */ |
Radek Krejci | bb08db3 | 2017-07-03 11:29:17 +0200 | [diff] [blame] | 266 | if (!snode || !(*snode) || ((parent->nodetype == LYS_AUGMENT) && ((*snode)->parent != parent))) { |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 267 | return NULL; |
| 268 | } |
| 269 | next = last = *snode; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 270 | } else { |
| 271 | /* top level data */ |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 272 | if (!(options & LYS_GETNEXT_NOSTATECHECK) && (module->disabled || !module->implemented)) { |
| 273 | /* nothing to return from a disabled/imported module */ |
| 274 | return NULL; |
| 275 | } |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 276 | next = last = module->data; |
| 277 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 278 | } else if ((last->nodetype == LYS_USES) && (options & LYS_GETNEXT_INTOUSES) && last->child) { |
| 279 | /* continue with uses content */ |
| 280 | next = last->child; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 281 | } else { |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 282 | /* continue after the last returned value */ |
| 283 | next = last->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | repeat: |
Michal Vasko | 5a9c24b | 2017-03-13 09:25:58 +0100 | [diff] [blame] | 287 | if (parent && (parent->nodetype == LYS_AUGMENT) && next) { |
| 288 | /* do not return anything outside the parent augment */ |
| 289 | aug_parent = next->parent; |
| 290 | do { |
| 291 | while (aug_parent && (aug_parent->nodetype != LYS_AUGMENT)) { |
| 292 | aug_parent = aug_parent->parent; |
| 293 | } |
| 294 | if (aug_parent) { |
| 295 | if (aug_parent == parent) { |
| 296 | break; |
| 297 | } |
| 298 | aug_parent = ((struct lys_node_augment *)aug_parent)->target; |
| 299 | } |
| 300 | |
| 301 | } while (aug_parent); |
| 302 | if (!aug_parent) { |
| 303 | return NULL; |
| 304 | } |
| 305 | } |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 306 | while (next && (next->nodetype == LYS_GROUPING)) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 307 | if (options & LYS_GETNEXT_WITHGROUPING) { |
| 308 | return next; |
| 309 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 310 | next = next->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 311 | } |
| 312 | |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 313 | if (!next) { /* cover case when parent is augment */ |
| 314 | if (!last || last->parent == parent || lys_parent(last) == parent) { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 315 | /* no next element */ |
| 316 | return NULL; |
| 317 | } |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 318 | last = lys_parent(last); |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 319 | next = last->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 320 | goto repeat; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 321 | } else { |
| 322 | last = next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 325 | if (!(options & LYS_GETNEXT_NOSTATECHECK) && lys_is_disabled(next, 0)) { |
| 326 | next = next->next; |
| 327 | goto repeat; |
| 328 | } |
| 329 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 330 | switch (next->nodetype) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 331 | case LYS_INPUT: |
| 332 | case LYS_OUTPUT: |
| 333 | if (options & LYS_GETNEXT_WITHINOUT) { |
| 334 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 335 | } else if (next->child) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 336 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 337 | } else { |
| 338 | next = next->next; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 339 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 340 | goto repeat; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 341 | |
Michal Vasko | a5835e9 | 2015-10-20 15:07:39 +0200 | [diff] [blame] | 342 | case LYS_CASE: |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 343 | if (options & LYS_GETNEXT_WITHCASE) { |
| 344 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 345 | } else if (next->child) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 346 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 347 | } else { |
| 348 | next = next->next; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 349 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 350 | goto repeat; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 351 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 352 | case LYS_USES: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 353 | /* go into */ |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 354 | if (options & LYS_GETNEXT_WITHUSES) { |
| 355 | return next; |
| 356 | } else if (next->child) { |
| 357 | next = next->child; |
| 358 | } else { |
| 359 | next = next->next; |
| 360 | } |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 361 | goto repeat; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 362 | |
Radek Krejci | dfcae7d | 2015-10-20 17:13:01 +0200 | [diff] [blame] | 363 | case LYS_RPC: |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 364 | case LYS_ACTION: |
Radek Krejci | dfcae7d | 2015-10-20 17:13:01 +0200 | [diff] [blame] | 365 | case LYS_NOTIF: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 366 | case LYS_LEAF: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 367 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 368 | case LYS_ANYDATA: |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 369 | case LYS_LIST: |
| 370 | case LYS_LEAFLIST: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 371 | return next; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 372 | |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 373 | case LYS_CONTAINER: |
| 374 | if (!((struct lys_node_container *)next)->presence && (options & LYS_GETNEXT_INTONPCONT)) { |
| 375 | if (next->child) { |
| 376 | /* go into */ |
| 377 | next = next->child; |
| 378 | } else { |
| 379 | next = next->next; |
| 380 | } |
| 381 | goto repeat; |
| 382 | } else { |
| 383 | return next; |
| 384 | } |
| 385 | |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 386 | case LYS_CHOICE: |
| 387 | if (options & LYS_GETNEXT_WITHCHOICE) { |
| 388 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 389 | } else if (next->child) { |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 390 | /* go into */ |
| 391 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 392 | } else { |
| 393 | next = next->next; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 394 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 395 | goto repeat; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 396 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 397 | default: |
| 398 | /* we should not be here */ |
| 399 | return NULL; |
| 400 | } |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 401 | } |
| 402 | |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 403 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 404 | lys_node_unlink(struct lys_node *node) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 405 | { |
Michal Vasko | f75b277 | 2018-03-14 09:55:33 +0100 | [diff] [blame] | 406 | struct lys_node *parent, *first, **pp = NULL; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 407 | struct lys_module *main_module; |
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 | if (!node) { |
| 410 | return; |
| 411 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 412 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 413 | /* unlink from data model if necessary */ |
| 414 | if (node->module) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 415 | /* get main module with data tree */ |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 416 | main_module = lys_node_module(node); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 417 | if (main_module->data == node) { |
| 418 | main_module->data = node->next; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 419 | } |
| 420 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 421 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 422 | /* store pointers to important nodes */ |
| 423 | parent = node->parent; |
Michal Vasko | 3a9943b | 2015-09-23 11:33:50 +0200 | [diff] [blame] | 424 | if (parent && (parent->nodetype == LYS_AUGMENT)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 425 | /* handle augments - first, unlink it from the augment parent ... */ |
| 426 | if (parent->child == node) { |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 427 | parent->child = (node->next && node->next->parent == parent) ? node->next : NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 428 | } |
Radek Krejci | fec2e14 | 2017-01-05 15:19:03 +0100 | [diff] [blame] | 429 | |
| 430 | if (parent->flags & LYS_NOTAPPLIED) { |
| 431 | /* data are not connected in the target, so we cannot continue with the target as a parent */ |
| 432 | parent = NULL; |
| 433 | } else { |
| 434 | /* data are connected in target, so we will continue with the target as a parent */ |
| 435 | parent = ((struct lys_node_augment *)parent)->target; |
| 436 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 437 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 438 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 439 | /* unlink from parent */ |
| 440 | if (parent) { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 441 | if (parent->nodetype == LYS_EXT) { |
| 442 | pp = (struct lys_node **)lys_ext_complex_get_substmt(lys_snode2stmt(node->nodetype), |
| 443 | (struct lys_ext_instance_complex*)parent, NULL); |
| 444 | if (*pp == node) { |
| 445 | *pp = node->next; |
| 446 | } |
| 447 | } else if (parent->child == node) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 448 | parent->child = node->next; |
| 449 | } |
| 450 | node->parent = NULL; |
| 451 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 452 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 453 | /* unlink from siblings */ |
| 454 | if (node->prev == node) { |
| 455 | /* there are no more siblings */ |
| 456 | return; |
| 457 | } |
| 458 | if (node->next) { |
| 459 | node->next->prev = node->prev; |
| 460 | } else { |
| 461 | /* unlinking the last element */ |
| 462 | if (parent) { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 463 | if (parent->nodetype == LYS_EXT) { |
| 464 | first = *(struct lys_node **)pp; |
| 465 | } else { |
| 466 | first = parent->child; |
| 467 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 468 | } else { |
| 469 | first = node; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 470 | while (first->prev->next) { |
Michal Vasko | 276f96b | 2015-09-23 11:34:28 +0200 | [diff] [blame] | 471 | first = first->prev; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | first->prev = node->prev; |
| 475 | } |
| 476 | if (node->prev->next) { |
| 477 | node->prev->next = node->next; |
| 478 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 479 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 480 | /* clean up the unlinked element */ |
| 481 | node->next = NULL; |
| 482 | node->prev = node; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 483 | } |
| 484 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 485 | struct lys_node_grp * |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 486 | lys_find_grouping_up(const char *name, struct lys_node *start) |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 487 | { |
| 488 | struct lys_node *par_iter, *iter, *stop; |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 489 | |
| 490 | for (par_iter = start; par_iter; par_iter = par_iter->parent) { |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 491 | /* top-level augment, look into module (uses augment is handled correctly below) */ |
| 492 | 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] | 493 | par_iter = lys_main_module(par_iter->parent->module)->data; |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 494 | if (!par_iter) { |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 495 | break; |
| 496 | } |
| 497 | } |
| 498 | |
Michal Vasko | 38a0041 | 2018-03-14 10:01:50 +0100 | [diff] [blame] | 499 | if (par_iter->nodetype == LYS_EXT) { |
| 500 | /* we are in a top-level extension, search grouping in top-level groupings */ |
| 501 | par_iter = lys_main_module(par_iter->module)->data; |
| 502 | if (!par_iter) { |
| 503 | /* not connected yet, wait */ |
| 504 | return NULL; |
| 505 | } |
| 506 | } 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] | 507 | continue; |
| 508 | } |
| 509 | |
| 510 | for (iter = par_iter, stop = NULL; iter; iter = iter->prev) { |
| 511 | if (!stop) { |
| 512 | stop = par_iter; |
| 513 | } else if (iter == stop) { |
| 514 | break; |
| 515 | } |
| 516 | if (iter->nodetype != LYS_GROUPING) { |
| 517 | continue; |
| 518 | } |
| 519 | |
Radek Krejci | f8426a7 | 2015-10-31 23:14:03 +0100 | [diff] [blame] | 520 | if (!strcmp(name, iter->name)) { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 521 | return (struct lys_node_grp *)iter; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 526 | return NULL; |
| 527 | } |
| 528 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 529 | /* |
| 530 | * get next grouping in the root's subtree, in the |
| 531 | * first call, tha last is NULL |
| 532 | */ |
| 533 | static struct lys_node_grp * |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 534 | 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] | 535 | { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 536 | struct lys_node *last = (struct lys_node *)lastgrp; |
| 537 | struct lys_node *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 538 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 539 | assert(root); |
| 540 | |
| 541 | if (!last) { |
| 542 | last = root; |
| 543 | } |
| 544 | |
| 545 | while (1) { |
| 546 | if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) { |
| 547 | next = last->child; |
| 548 | } else { |
| 549 | next = NULL; |
| 550 | } |
| 551 | if (!next) { |
| 552 | if (last == root) { |
| 553 | /* we are done */ |
| 554 | return NULL; |
| 555 | } |
| 556 | |
| 557 | /* no children, go to siblings */ |
| 558 | next = last->next; |
| 559 | } |
| 560 | while (!next) { |
| 561 | /* go back through parents */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 562 | if (lys_parent(last) == root) { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 563 | /* we are done */ |
| 564 | return NULL; |
| 565 | } |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 566 | next = last->next; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 567 | last = lys_parent(last); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | if (next->nodetype == LYS_GROUPING) { |
| 571 | return (struct lys_node_grp *)next; |
| 572 | } |
| 573 | |
| 574 | last = next; |
| 575 | } |
| 576 | } |
| 577 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 578 | /* logs directly */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 579 | int |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 580 | lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module) |
| 581 | { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 582 | struct lys_node *start, *stop, *iter; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 583 | struct lys_node_grp *grp; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 584 | int down, up; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 585 | |
| 586 | assert(node); |
| 587 | |
| 588 | if (!parent) { |
| 589 | assert(module); |
| 590 | } else { |
| 591 | module = parent->module; |
| 592 | } |
Radek Krejci | 115fa88 | 2017-03-01 16:15:07 +0100 | [diff] [blame] | 593 | module = lys_main_module(module); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 594 | |
| 595 | switch (node->nodetype) { |
| 596 | case LYS_GROUPING: |
| 597 | /* 6.2.1, rule 6 */ |
| 598 | if (parent) { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 599 | start = *lys_child(parent, LYS_GROUPING); |
| 600 | if (!start) { |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 601 | down = 0; |
| 602 | start = parent; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 603 | } else { |
| 604 | down = 1; |
| 605 | } |
| 606 | if (parent->nodetype == LYS_EXT) { |
| 607 | up = 0; |
| 608 | } else { |
| 609 | up = 1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 610 | } |
| 611 | } else { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 612 | down = up = 1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 613 | start = module->data; |
| 614 | } |
| 615 | /* go up */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 616 | if (up && lys_find_grouping_up(node->name, start)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 617 | LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name); |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 618 | return EXIT_FAILURE; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 619 | } |
| 620 | /* go down, because grouping can be defined after e.g. container in which is collision */ |
| 621 | if (down) { |
| 622 | for (iter = start, stop = NULL; iter; iter = iter->prev) { |
| 623 | if (!stop) { |
| 624 | stop = start; |
| 625 | } else if (iter == stop) { |
| 626 | break; |
| 627 | } |
| 628 | if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) { |
| 629 | continue; |
| 630 | } |
| 631 | |
| 632 | grp = NULL; |
| 633 | while ((grp = lys_get_next_grouping(grp, iter))) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 634 | if (ly_strequal(node->name, grp->name, 1)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 635 | LOGVAL(module->ctx, LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 636 | return EXIT_FAILURE; |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | break; |
| 642 | case LYS_LEAF: |
| 643 | case LYS_LEAFLIST: |
| 644 | case LYS_LIST: |
| 645 | case LYS_CONTAINER: |
| 646 | case LYS_CHOICE: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 647 | case LYS_ANYDATA: |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 648 | /* 6.2.1, rule 7 */ |
| 649 | if (parent) { |
| 650 | iter = parent; |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 651 | while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE | LYS_AUGMENT))) { |
| 652 | if (iter->nodetype == LYS_AUGMENT) { |
| 653 | if (((struct lys_node_augment *)iter)->target) { |
| 654 | /* augment is resolved, go up */ |
| 655 | iter = ((struct lys_node_augment *)iter)->target; |
| 656 | continue; |
| 657 | } |
| 658 | /* augment is not resolved, this is the final parent */ |
| 659 | break; |
| 660 | } |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 661 | iter = iter->parent; |
| 662 | } |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 663 | |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 664 | if (!iter) { |
| 665 | stop = NULL; |
| 666 | iter = module->data; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 667 | } else if (iter->nodetype == LYS_EXT) { |
| 668 | stop = iter; |
PavolVican | 9d61d40 | 2018-02-05 15:52:48 +0100 | [diff] [blame] | 669 | iter = (struct lys_node *)lys_child(iter, node->nodetype); |
| 670 | if (iter) { |
| 671 | iter = *(struct lys_node **)iter; |
| 672 | } |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 673 | } else { |
| 674 | stop = iter; |
| 675 | iter = iter->child; |
| 676 | } |
| 677 | } else { |
| 678 | stop = NULL; |
| 679 | iter = module->data; |
| 680 | } |
| 681 | while (iter) { |
| 682 | if (iter->nodetype & (LYS_USES | LYS_CASE)) { |
| 683 | iter = iter->child; |
| 684 | continue; |
| 685 | } |
| 686 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 687 | 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] | 688 | if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 689 | 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] | 690 | return EXIT_FAILURE; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | /* special case for choice - we must check the choice's name as |
| 695 | * well as the names of nodes under the choice |
| 696 | */ |
| 697 | if (iter->nodetype == LYS_CHOICE) { |
| 698 | iter = iter->child; |
| 699 | continue; |
| 700 | } |
| 701 | |
| 702 | /* go to siblings */ |
| 703 | if (!iter->next) { |
| 704 | /* no sibling, go to parent's sibling */ |
| 705 | do { |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 706 | /* for parent LYS_AUGMENT */ |
| 707 | if (iter->parent == stop) { |
| 708 | iter = stop; |
| 709 | break; |
| 710 | } |
| 711 | iter = lys_parent(iter); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 712 | if (iter && iter->next) { |
| 713 | break; |
| 714 | } |
| 715 | } while (iter != stop); |
| 716 | |
| 717 | if (iter == stop) { |
| 718 | break; |
| 719 | } |
| 720 | } |
| 721 | iter = iter->next; |
| 722 | } |
| 723 | break; |
| 724 | case LYS_CASE: |
| 725 | /* 6.2.1, rule 8 */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 726 | if (parent) { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 727 | start = *lys_child(parent, LYS_CASE); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 728 | } else { |
| 729 | start = module->data; |
| 730 | } |
| 731 | |
| 732 | LY_TREE_FOR(start, iter) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 733 | 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] | 734 | continue; |
| 735 | } |
| 736 | |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 737 | if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 738 | LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, "case", node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 739 | return EXIT_FAILURE; |
| 740 | } |
| 741 | } |
| 742 | break; |
| 743 | default: |
| 744 | /* no check needed */ |
| 745 | break; |
| 746 | } |
| 747 | |
| 748 | return EXIT_SUCCESS; |
| 749 | } |
| 750 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 751 | /* logs directly */ |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 752 | int |
Michal Vasko | 8d30dd8 | 2018-09-10 10:06:12 +0200 | [diff] [blame] | 753 | lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child, int options) |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 754 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 755 | struct ly_ctx *ctx = child->module->ctx; |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 756 | struct lys_node *iter, **pchild; |
Michal Vasko | 56601c9 | 2018-08-06 10:03:06 +0200 | [diff] [blame] | 757 | struct lys_node_inout *in, *out; |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 758 | struct lys_node_case *c; |
| 759 | int type, shortcase = 0; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 760 | void *p; |
| 761 | struct lyext_substmt *info = NULL; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 762 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 763 | assert(child); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 764 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 765 | if (parent) { |
| 766 | type = parent->nodetype; |
| 767 | module = parent->module; |
| 768 | } else { |
| 769 | assert(module); |
Radek Krejci | fe3a138 | 2016-11-04 10:30:11 +0100 | [diff] [blame] | 770 | assert(!(child->nodetype & (LYS_INPUT | LYS_OUTPUT))); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 771 | type = 0; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 772 | } |
| 773 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 774 | /* checks */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 775 | switch (type) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 776 | case LYS_CONTAINER: |
| 777 | case LYS_LIST: |
| 778 | case LYS_GROUPING: |
Radek Krejci | 9693540 | 2016-11-04 16:27:28 +0100 | [diff] [blame] | 779 | case LYS_USES: |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 780 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 781 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF | |
Michal Vasko | b15cae2 | 2016-09-15 09:40:56 +0200 | [diff] [blame] | 782 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 783 | 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] | 784 | return EXIT_FAILURE; |
| 785 | } |
| 786 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 787 | case LYS_INPUT: |
| 788 | case LYS_OUTPUT: |
| 789 | case LYS_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 790 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 791 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 792 | LYS_LEAFLIST | LYS_LIST | LYS_USES))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 793 | 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] | 794 | return EXIT_FAILURE; |
| 795 | } |
| 796 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 797 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 798 | if (!(child->nodetype & |
Pavol Vican | 47a1b0a | 2016-09-20 10:18:24 +0200 | [diff] [blame] | 799 | (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] | 800 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 801 | return EXIT_FAILURE; |
| 802 | } |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 803 | if (child->nodetype != LYS_CASE) { |
| 804 | shortcase = 1; |
| 805 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 806 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 807 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 808 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 809 | (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] | 810 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 811 | return EXIT_FAILURE; |
| 812 | } |
| 813 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 814 | case LYS_RPC: |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 815 | case LYS_ACTION: |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 816 | if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 817 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc"); |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 818 | return EXIT_FAILURE; |
| 819 | } |
| 820 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 821 | case LYS_LEAF: |
| 822 | case LYS_LEAFLIST: |
| 823 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 824 | case LYS_ANYDATA: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 825 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
| 826 | 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] | 827 | strnodetype(parent->nodetype)); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 828 | return EXIT_FAILURE; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 829 | case LYS_AUGMENT: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 830 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 831 | (LYS_ANYDATA | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 832 | | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 833 | 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] | 834 | return EXIT_FAILURE; |
| 835 | } |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 836 | break; |
| 837 | case LYS_UNKNOWN: |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 838 | /* top level */ |
| 839 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 840 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 841 | | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 842 | 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] | 843 | return EXIT_FAILURE; |
| 844 | } |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 845 | break; |
| 846 | case LYS_EXT: |
| 847 | /* plugin-defined */ |
| 848 | p = lys_ext_complex_get_substmt(lys_snode2stmt(child->nodetype), (struct lys_ext_instance_complex*)parent, &info); |
| 849 | if (!p) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 850 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 851 | ((struct lys_ext_instance_complex*)parent)->def->name); |
| 852 | return EXIT_FAILURE; |
| 853 | } |
| 854 | /* TODO check cardinality */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 855 | break; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | /* check identifier uniqueness */ |
Michal Vasko | 15a4337 | 2017-09-25 14:12:42 +0200 | [diff] [blame] | 859 | 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] | 860 | return EXIT_FAILURE; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 861 | } |
Radek Krejci | b7155b5 | 2015-06-10 17:03:01 +0200 | [diff] [blame] | 862 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 863 | if (child->parent) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 864 | lys_node_unlink(child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 865 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 866 | |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 867 | if ((child->nodetype & (LYS_INPUT | LYS_OUTPUT)) && parent->nodetype != LYS_EXT) { |
Michal Vasko | 56601c9 | 2018-08-06 10:03:06 +0200 | [diff] [blame] | 868 | /* find the implicit input/output node */ |
| 869 | LY_TREE_FOR(parent->child, iter) { |
| 870 | if (iter->nodetype == child->nodetype) { |
| 871 | break; |
| 872 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 873 | } |
Michal Vasko | 56601c9 | 2018-08-06 10:03:06 +0200 | [diff] [blame] | 874 | assert(iter); |
| 875 | |
| 876 | /* switch the old implicit node (iter) with the new one (child) */ |
| 877 | if (parent->child == iter) { |
| 878 | /* first child */ |
| 879 | parent->child = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 880 | } else { |
Michal Vasko | 56601c9 | 2018-08-06 10:03:06 +0200 | [diff] [blame] | 881 | iter->prev->next = child; |
| 882 | } |
| 883 | child->prev = iter->prev; |
| 884 | child->next = iter->next; |
| 885 | if (iter->next) { |
| 886 | iter->next->prev = child; |
| 887 | } else { |
| 888 | /* last child */ |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 889 | parent->child->prev = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 890 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 891 | child->parent = parent; |
Michal Vasko | 56601c9 | 2018-08-06 10:03:06 +0200 | [diff] [blame] | 892 | |
| 893 | /* isolate the node and free it */ |
| 894 | iter->next = NULL; |
| 895 | iter->prev = iter; |
| 896 | iter->parent = NULL; |
| 897 | lys_node_free(iter, NULL, 0); |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 898 | } else { |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 899 | if (shortcase) { |
| 900 | /* create the implicit case to allow it to serve as a target of the augments, |
| 901 | * it won't be printed, but it will be present in the tree */ |
| 902 | c = calloc(1, sizeof *c); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 903 | LY_CHECK_ERR_RETURN(!c, LOGMEM(ctx), EXIT_FAILURE); |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 904 | c->name = lydict_insert(module->ctx, child->name, 0); |
| 905 | c->flags = LYS_IMPLICIT; |
Michal Vasko | 8d30dd8 | 2018-09-10 10:06:12 +0200 | [diff] [blame] | 906 | if (!(options & (LYS_PARSE_OPT_CFG_IGNORE | LYS_PARSE_OPT_CFG_NOINHERIT))) { |
| 907 | /* get config flag from parent */ |
| 908 | c->flags |= parent->flags & LYS_CONFIG_MASK; |
| 909 | } |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 910 | c->module = module; |
| 911 | c->nodetype = LYS_CASE; |
| 912 | c->prev = (struct lys_node*)c; |
Michal Vasko | 8d30dd8 | 2018-09-10 10:06:12 +0200 | [diff] [blame] | 913 | lys_node_addchild(parent, module, (struct lys_node*)c, options); |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 914 | parent = (struct lys_node*)c; |
| 915 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 916 | /* connect the child correctly */ |
| 917 | if (!parent) { |
| 918 | if (module->data) { |
| 919 | module->data->prev->next = child; |
| 920 | child->prev = module->data->prev; |
| 921 | module->data->prev = child; |
| 922 | } else { |
| 923 | module->data = child; |
| 924 | } |
| 925 | } else { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 926 | pchild = lys_child(parent, child->nodetype); |
| 927 | assert(pchild); |
| 928 | |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 929 | child->parent = parent; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 930 | if (!(*pchild)) { |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 931 | /* the only/first child of the parent */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 932 | *pchild = child; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 933 | iter = child; |
| 934 | } else { |
| 935 | /* add a new child at the end of parent's child list */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 936 | iter = (*pchild)->prev; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 937 | iter->next = child; |
| 938 | child->prev = iter; |
| 939 | } |
| 940 | while (iter->next) { |
| 941 | iter = iter->next; |
| 942 | iter->parent = parent; |
| 943 | } |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 944 | (*pchild)->prev = iter; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 945 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 946 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 947 | |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 948 | /* check config value (but ignore them in groupings and augments) */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 949 | 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] | 950 | if (parent && !iter) { |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 951 | for (iter = child; iter && !(iter->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); iter = iter->parent); |
| 952 | if (!iter && (parent->flags & LYS_CONFIG_R) && (child->flags & LYS_CONFIG_W)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 953 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, child, "true", "config"); |
| 954 | 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] | 955 | return EXIT_FAILURE; |
| 956 | } |
| 957 | } |
| 958 | |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 959 | /* propagate information about status data presence */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 960 | 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] | 961 | (child->flags & LYS_INCL_STATUS)) { |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 962 | for(iter = parent; iter; iter = lys_parent(iter)) { |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 963 | /* store it only into container or list - the only data inner nodes */ |
| 964 | if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
| 965 | if (iter->flags & LYS_INCL_STATUS) { |
| 966 | /* done, someone else set it already from here */ |
| 967 | break; |
| 968 | } |
| 969 | /* set flag about including status data */ |
| 970 | iter->flags |= LYS_INCL_STATUS; |
| 971 | } |
| 972 | } |
| 973 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 974 | |
| 975 | /* 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] | 976 | if ((child->nodetype & (LYS_RPC | LYS_ACTION)) && !child->child) { |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 977 | in = calloc(1, sizeof *in); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 978 | out = calloc(1, sizeof *out); |
| 979 | if (!in || !out) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 980 | LOGMEM(ctx); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 981 | free(in); |
| 982 | free(out); |
| 983 | return EXIT_FAILURE; |
| 984 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 985 | in->nodetype = LYS_INPUT; |
| 986 | in->name = lydict_insert(child->module->ctx, "input", 5); |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 987 | out->nodetype = LYS_OUTPUT; |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 988 | out->name = lydict_insert(child->module->ctx, "output", 6); |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 989 | in->module = out->module = child->module; |
| 990 | in->parent = out->parent = child; |
| 991 | in->flags = out->flags = LYS_IMPLICIT; |
| 992 | in->next = (struct lys_node *)out; |
| 993 | in->prev = (struct lys_node *)out; |
| 994 | out->prev = (struct lys_node *)in; |
| 995 | child->child = (struct lys_node *)in; |
| 996 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 997 | return EXIT_SUCCESS; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 998 | } |
| 999 | |
Michal Vasko | 2924566 | 2017-04-18 15:56:31 +0200 | [diff] [blame] | 1000 | const struct lys_module * |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1001 | 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] | 1002 | { |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1003 | char *enlarged_data = NULL; |
Radek Krejci | 0b5805d | 2015-08-13 09:38:02 +0200 | [diff] [blame] | 1004 | struct lys_module *mod = NULL; |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1005 | unsigned int len; |
Radek Krejci | f347abc | 2016-06-22 10:18:47 +0200 | [diff] [blame] | 1006 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1007 | if (!ctx || !data) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1008 | LOGARG; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1009 | return NULL; |
| 1010 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1011 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1012 | if (!internal && format == LYS_IN_YANG) { |
| 1013 | /* enlarge data by 2 bytes for flex */ |
| 1014 | len = strlen(data); |
| 1015 | enlarged_data = malloc((len + 2) * sizeof *enlarged_data); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1016 | LY_CHECK_ERR_RETURN(!enlarged_data, LOGMEM(ctx), NULL); |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1017 | memcpy(enlarged_data, data, len); |
| 1018 | enlarged_data[len] = enlarged_data[len + 1] = '\0'; |
| 1019 | data = enlarged_data; |
| 1020 | } |
| 1021 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1022 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1023 | case LYS_IN_YIN: |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1024 | mod = yin_read_module(ctx, data, revision, implement); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1025 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1026 | case LYS_IN_YANG: |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1027 | mod = yang_read_module(ctx, data, 0, revision, implement); |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1028 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1029 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1030 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1031 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1032 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1033 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1034 | free(enlarged_data); |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 1035 | |
| 1036 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 1037 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 1038 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 1039 | * the anotation definitions available in the internal schema structure. There is another hack in schema |
| 1040 | * printers to do not print this internally added annotation. */ |
| 1041 | if (mod && ly_strequal(mod->name, "ietf-netconf", 0)) { |
Radek Krejci | 5b19066 | 2017-04-13 08:56:14 +0200 | [diff] [blame] | 1042 | if (lyp_add_ietf_netconf_annotations(mod)) { |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 1043 | lys_free(mod, NULL, 1, 1); |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 1044 | return NULL; |
| 1045 | } |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 1046 | } |
| 1047 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1048 | return mod; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1049 | } |
| 1050 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1051 | API const struct lys_module * |
| 1052 | lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format) |
| 1053 | { |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1054 | return lys_parse_mem_(ctx, data, format, NULL, 0, 1); |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1055 | } |
| 1056 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1057 | struct lys_submodule * |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1058 | 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] | 1059 | { |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1060 | char *enlarged_data = NULL; |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1061 | struct lys_submodule *submod = NULL; |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1062 | unsigned int len; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1063 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1064 | assert(module); |
| 1065 | assert(data); |
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 | if (format == LYS_IN_YANG) { |
| 1068 | /* enlarge data by 2 bytes for flex */ |
| 1069 | len = strlen(data); |
| 1070 | enlarged_data = malloc((len + 2) * sizeof *enlarged_data); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1071 | LY_CHECK_ERR_RETURN(!enlarged_data, LOGMEM(module->ctx), NULL); |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1072 | memcpy(enlarged_data, data, len); |
| 1073 | enlarged_data[len] = enlarged_data[len + 1] = '\0'; |
| 1074 | data = enlarged_data; |
| 1075 | } |
| 1076 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1077 | /* get the main module */ |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 1078 | module = lys_main_module(module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1079 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1080 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1081 | case LYS_IN_YIN: |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1082 | submod = yin_read_submodule(module, data, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1083 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1084 | case LYS_IN_YANG: |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1085 | submod = yang_read_submodule(module, data, 0, unres); |
| 1086 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1087 | default: |
Radek Krejci | 90a550a | 2016-04-13 16:00:58 +0200 | [diff] [blame] | 1088 | assert(0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1089 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1090 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1091 | |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1092 | free(enlarged_data); |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1093 | return submod; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1094 | } |
| 1095 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1096 | API const struct lys_module * |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1097 | lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format) |
| 1098 | { |
| 1099 | int fd; |
| 1100 | const struct lys_module *ret; |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 1101 | const char *rev, *dot, *filename; |
| 1102 | size_t len; |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1103 | |
| 1104 | if (!ctx || !path) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1105 | LOGARG; |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1106 | return NULL; |
| 1107 | } |
| 1108 | |
| 1109 | fd = open(path, O_RDONLY); |
| 1110 | if (fd == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1111 | LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)); |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1112 | return NULL; |
| 1113 | } |
| 1114 | |
| 1115 | ret = lys_parse_fd(ctx, fd, format); |
| 1116 | close(fd); |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 1117 | |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 1118 | if (!ret) { |
| 1119 | /* error */ |
| 1120 | return NULL; |
| 1121 | } |
| 1122 | |
| 1123 | /* check that name and revision match filename */ |
| 1124 | filename = strrchr(path, '/'); |
| 1125 | if (!filename) { |
| 1126 | filename = path; |
| 1127 | } else { |
| 1128 | filename++; |
| 1129 | } |
| 1130 | rev = strchr(filename, '@'); |
| 1131 | dot = strrchr(filename, '.'); |
| 1132 | |
| 1133 | /* name */ |
| 1134 | len = strlen(ret->name); |
| 1135 | if (strncmp(filename, ret->name, len) || |
| 1136 | ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1137 | 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] | 1138 | } |
| 1139 | if (rev) { |
| 1140 | len = dot - ++rev; |
| 1141 | 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] | 1142 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 1143 | ret->rev_size ? ret->rev[0].date : "none"); |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | if (!ret->filepath) { |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 1148 | /* store URI */ |
Alexandre Snarskii | 12b2218 | 2018-09-04 17:12:28 +0300 | [diff] [blame] | 1149 | char rpath[PATH_MAX]; |
| 1150 | if (realpath(path, rpath) != NULL) { |
| 1151 | ((struct lys_module *)ret)->filepath = lydict_insert(ctx, rpath, 0); |
| 1152 | } else { |
| 1153 | ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0); |
| 1154 | } |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 1155 | } |
| 1156 | |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1157 | return ret; |
| 1158 | } |
| 1159 | |
| 1160 | API const struct lys_module * |
| 1161 | lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format) |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1162 | { |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1163 | return lys_parse_fd_(ctx, fd, format, NULL, 1); |
| 1164 | } |
| 1165 | |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1166 | static void |
| 1167 | lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd) |
| 1168 | { |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1169 | #ifdef __APPLE__ |
| 1170 | char path[MAXPATHLEN]; |
| 1171 | #else |
Michal Vasko | 25601f3 | 2018-02-16 09:41:46 +0100 | [diff] [blame] | 1172 | int len; |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1173 | char path[PATH_MAX], proc_path[32]; |
| 1174 | #endif |
| 1175 | |
| 1176 | #ifdef __APPLE__ |
| 1177 | if (fcntl(fd, F_GETPATH, path) != -1) { |
| 1178 | *filename = lydict_insert(ctx, path, 0); |
| 1179 | } |
| 1180 | #else |
| 1181 | /* get URI if there is /proc */ |
| 1182 | sprintf(proc_path, "/proc/self/fd/%d", fd); |
| 1183 | if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) { |
| 1184 | *filename = lydict_insert(ctx, path, len); |
| 1185 | } |
| 1186 | #endif |
| 1187 | } |
| 1188 | |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1189 | const struct lys_module * |
| 1190 | lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const char *revision, int implement) |
| 1191 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1192 | const struct lys_module *module; |
Radek Krejci | 0fb1150 | 2017-01-31 16:45:42 +0100 | [diff] [blame] | 1193 | size_t length; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1194 | char *addr; |
| 1195 | |
| 1196 | if (!ctx || fd < 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1197 | LOGARG; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1198 | return NULL; |
| 1199 | } |
| 1200 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1201 | if (lyp_mmap(ctx, fd, format == LYS_IN_YANG ? 1 : 0, &length, (void **)&addr)) { |
| 1202 | LOGERR(ctx, LY_ESYS, "Mapping file descriptor into memory failed (%s()).", __func__); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1203 | return NULL; |
Radek Krejci | 10c216a | 2017-02-01 10:36:00 +0100 | [diff] [blame] | 1204 | } else if (!addr) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1205 | LOGERR(ctx, LY_EINVAL, "Empty schema file."); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1206 | return NULL; |
| 1207 | } |
Radek Krejci | 0fb1150 | 2017-01-31 16:45:42 +0100 | [diff] [blame] | 1208 | |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1209 | module = lys_parse_mem_(ctx, addr, format, revision, 1, implement); |
Radek Krejci | 0fb1150 | 2017-01-31 16:45:42 +0100 | [diff] [blame] | 1210 | lyp_munmap(addr, length); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1211 | |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1212 | if (module && !module->filepath) { |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1213 | lys_parse_set_filename(ctx, (const char **)&module->filepath, fd); |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1214 | } |
| 1215 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1216 | return module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1217 | } |
| 1218 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1219 | struct lys_submodule * |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1220 | 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] | 1221 | { |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1222 | struct lys_submodule *submodule; |
Radek Krejci | 0fb1150 | 2017-01-31 16:45:42 +0100 | [diff] [blame] | 1223 | size_t length; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1224 | char *addr; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1225 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1226 | assert(module); |
| 1227 | assert(fd >= 0); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1228 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1229 | if (lyp_mmap(module->ctx, fd, format == LYS_IN_YANG ? 1 : 0, &length, (void **)&addr)) { |
| 1230 | 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] | 1231 | return NULL; |
Radek Krejci | 10c216a | 2017-02-01 10:36:00 +0100 | [diff] [blame] | 1232 | } else if (!addr) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1233 | LOGERR(module->ctx, LY_EINVAL, "Empty submodule schema file."); |
Michal Vasko | 2e7241e | 2016-02-15 16:06:34 +0100 | [diff] [blame] | 1234 | return NULL; |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1235 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1236 | |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1237 | /* get the main module */ |
| 1238 | module = lys_main_module(module); |
| 1239 | |
| 1240 | switch (format) { |
| 1241 | case LYS_IN_YIN: |
| 1242 | submodule = yin_read_submodule(module, addr, unres); |
| 1243 | break; |
| 1244 | case LYS_IN_YANG: |
| 1245 | submodule = yang_read_submodule(module, addr, 0, unres); |
| 1246 | break; |
| 1247 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1248 | LOGINT(module->ctx); |
Michal Vasko | 85d4152 | 2017-02-24 09:49:16 +0100 | [diff] [blame] | 1249 | return NULL; |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1250 | } |
| 1251 | |
Radek Krejci | c645a3a | 2017-01-31 16:59:00 +0100 | [diff] [blame] | 1252 | lyp_munmap(addr, length); |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1253 | |
| 1254 | if (submodule && !submodule->filepath) { |
| 1255 | lys_parse_set_filename(module->ctx, (const char **)&submodule->filepath, fd); |
| 1256 | } |
| 1257 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1258 | return submodule; |
| 1259 | |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1260 | } |
| 1261 | |
Radek Krejci | 72cdfac | 2018-08-15 14:47:33 +0200 | [diff] [blame] | 1262 | API int |
Radek Krejci | cf3b7ca | 2018-08-17 09:55:06 +0200 | [diff] [blame] | 1263 | lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision, char **localfile, LYS_INFORMAT *format) |
Radek Krejci | 72cdfac | 2018-08-15 14:47:33 +0200 | [diff] [blame] | 1264 | { |
| 1265 | size_t len, flen, match_len = 0, dir_len; |
| 1266 | int i, implicit_cwd = 0, ret = EXIT_FAILURE; |
| 1267 | char *wd, *wn = NULL; |
| 1268 | DIR *dir = NULL; |
| 1269 | struct dirent *file; |
| 1270 | char *match_name = NULL; |
| 1271 | LYS_INFORMAT format_aux, match_format = 0; |
| 1272 | unsigned int u; |
| 1273 | struct ly_set *dirs; |
| 1274 | struct stat st; |
| 1275 | |
| 1276 | if (!localfile) { |
| 1277 | LOGARG; |
| 1278 | return EXIT_FAILURE; |
| 1279 | } |
| 1280 | |
| 1281 | /* start to fill the dir fifo with the context's search path (if set) |
| 1282 | * and the current working directory */ |
| 1283 | dirs = ly_set_new(); |
| 1284 | if (!dirs) { |
| 1285 | LOGMEM(NULL); |
| 1286 | return EXIT_FAILURE; |
| 1287 | } |
| 1288 | |
| 1289 | len = strlen(name); |
Radek Krejci | cf3b7ca | 2018-08-17 09:55:06 +0200 | [diff] [blame] | 1290 | if (cwd) { |
| 1291 | wd = get_current_dir_name(); |
| 1292 | if (!wd) { |
| 1293 | LOGMEM(NULL); |
Radek Krejci | 72cdfac | 2018-08-15 14:47:33 +0200 | [diff] [blame] | 1294 | goto cleanup; |
Radek Krejci | cf3b7ca | 2018-08-17 09:55:06 +0200 | [diff] [blame] | 1295 | } else { |
| 1296 | /* add implicit current working directory (./) to be searched, |
| 1297 | * this directory is not searched recursively */ |
| 1298 | if (ly_set_add(dirs, wd, 0) == -1) { |
| 1299 | goto cleanup; |
| 1300 | } |
| 1301 | implicit_cwd = 1; |
Radek Krejci | 72cdfac | 2018-08-15 14:47:33 +0200 | [diff] [blame] | 1302 | } |
Radek Krejci | 72cdfac | 2018-08-15 14:47:33 +0200 | [diff] [blame] | 1303 | } |
| 1304 | if (searchpaths) { |
| 1305 | for (i = 0; searchpaths[i]; i++) { |
| 1306 | /* check for duplicities with the implicit current working directory */ |
| 1307 | if (implicit_cwd && !strcmp(dirs->set.g[0], searchpaths[i])) { |
| 1308 | implicit_cwd = 0; |
| 1309 | continue; |
| 1310 | } |
| 1311 | wd = strdup(searchpaths[i]); |
| 1312 | if (!wd) { |
| 1313 | LOGMEM(NULL); |
| 1314 | goto cleanup; |
| 1315 | } else if (ly_set_add(dirs, wd, 0) == -1) { |
| 1316 | goto cleanup; |
| 1317 | } |
| 1318 | } |
| 1319 | } |
| 1320 | wd = NULL; |
| 1321 | |
| 1322 | /* start searching */ |
| 1323 | while (dirs->number) { |
| 1324 | free(wd); |
| 1325 | free(wn); wn = NULL; |
| 1326 | |
| 1327 | dirs->number--; |
| 1328 | wd = (char *)dirs->set.g[dirs->number]; |
| 1329 | dirs->set.g[dirs->number] = NULL; |
| 1330 | LOGVRB("Searching for \"%s\" in %s.", name, wd); |
| 1331 | |
| 1332 | if (dir) { |
| 1333 | closedir(dir); |
| 1334 | } |
| 1335 | dir = opendir(wd); |
| 1336 | dir_len = strlen(wd); |
| 1337 | if (!dir) { |
| 1338 | LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno)); |
| 1339 | } else { |
| 1340 | while ((file = readdir(dir))) { |
| 1341 | if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) { |
| 1342 | /* skip . and .. */ |
| 1343 | continue; |
| 1344 | } |
| 1345 | free(wn); |
| 1346 | if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) { |
| 1347 | LOGMEM(NULL); |
| 1348 | goto cleanup; |
| 1349 | } |
| 1350 | if (stat(wn, &st) == -1) { |
| 1351 | LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)", |
| 1352 | file->d_name, wd, strerror(errno)); |
| 1353 | continue; |
| 1354 | } |
| 1355 | if (S_ISDIR(st.st_mode) && (dirs->number || !implicit_cwd)) { |
| 1356 | /* we have another subdirectory in searchpath to explore, |
| 1357 | * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */ |
| 1358 | if (ly_set_add(dirs, wn, 0) == -1) { |
| 1359 | goto cleanup; |
| 1360 | } |
| 1361 | /* continue with the next item in current directory */ |
| 1362 | wn = NULL; |
| 1363 | continue; |
| 1364 | } else if (!S_ISREG(st.st_mode)) { |
| 1365 | /* not a regular file (note that we see the target of symlinks instead of symlinks */ |
| 1366 | continue; |
| 1367 | } |
| 1368 | |
| 1369 | /* here we know that the item is a file which can contain a module */ |
| 1370 | if (strncmp(name, file->d_name, len) || |
| 1371 | (file->d_name[len] != '.' && file->d_name[len] != '@')) { |
| 1372 | /* different filename than the module we search for */ |
| 1373 | continue; |
| 1374 | } |
| 1375 | |
| 1376 | /* get type according to filename suffix */ |
| 1377 | flen = strlen(file->d_name); |
| 1378 | if (!strcmp(&file->d_name[flen - 4], ".yin")) { |
| 1379 | format_aux = LYS_IN_YIN; |
| 1380 | } else if (!strcmp(&file->d_name[flen - 5], ".yang")) { |
| 1381 | format_aux = LYS_IN_YANG; |
| 1382 | } else { |
| 1383 | /* not supportde suffix/file format */ |
| 1384 | continue; |
| 1385 | } |
| 1386 | |
| 1387 | if (revision) { |
| 1388 | /* we look for the specific revision, try to get it from the filename */ |
| 1389 | if (file->d_name[len] == '@') { |
| 1390 | /* check revision from the filename */ |
| 1391 | if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) { |
| 1392 | /* another revision */ |
| 1393 | continue; |
| 1394 | } else { |
| 1395 | /* exact revision */ |
| 1396 | free(match_name); |
| 1397 | match_name = wn; |
| 1398 | wn = NULL; |
| 1399 | match_len = dir_len + 1 + len; |
| 1400 | match_format = format_aux; |
| 1401 | goto success; |
| 1402 | } |
| 1403 | } else { |
| 1404 | /* continue trying to find exact revision match, use this only if not found */ |
| 1405 | free(match_name); |
| 1406 | match_name = wn; |
| 1407 | wn = NULL; |
| 1408 | match_len = dir_len + 1 +len; |
| 1409 | match_format = format_aux; |
| 1410 | continue; |
| 1411 | } |
| 1412 | } else { |
| 1413 | /* remember the revision and try to find the newest one */ |
| 1414 | if (match_name) { |
| 1415 | if (file->d_name[len] != '@' || lyp_check_date(NULL, &file->d_name[len + 1])) { |
| 1416 | continue; |
| 1417 | } else if (match_name[match_len] == '@' && |
| 1418 | (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) { |
| 1419 | continue; |
| 1420 | } |
| 1421 | free(match_name); |
| 1422 | } |
| 1423 | |
| 1424 | match_name = wn; |
| 1425 | wn = NULL; |
| 1426 | match_len = dir_len + 1 + len; |
| 1427 | match_format = format_aux; |
| 1428 | continue; |
| 1429 | } |
| 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | success: |
| 1435 | (*localfile) = match_name; |
| 1436 | match_name = NULL; |
| 1437 | if (format) { |
| 1438 | (*format) = match_format; |
| 1439 | } |
| 1440 | ret = EXIT_SUCCESS; |
| 1441 | |
| 1442 | cleanup: |
| 1443 | free(wn); |
| 1444 | free(wd); |
| 1445 | if (dir) { |
| 1446 | closedir(dir); |
| 1447 | } |
| 1448 | free(match_name); |
| 1449 | for (u = 0; u < dirs->number; u++) { |
| 1450 | free(dirs->set.g[u]); |
| 1451 | } |
| 1452 | ly_set_free(dirs); |
| 1453 | |
| 1454 | return ret; |
| 1455 | } |
| 1456 | |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 1457 | int |
| 1458 | lys_ext_iter(struct lys_ext_instance **ext, uint8_t ext_size, uint8_t start, LYEXT_SUBSTMT substmt) |
| 1459 | { |
| 1460 | unsigned int u; |
| 1461 | |
| 1462 | for (u = start; u < ext_size; u++) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1463 | if (ext[u]->insubstmt == substmt) { |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 1464 | return u; |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | return -1; |
| 1469 | } |
| 1470 | |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1471 | /* |
| 1472 | * duplicate extension instance |
| 1473 | */ |
| 1474 | int |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1475 | lys_ext_dup(struct ly_ctx *ctx, struct lys_module *mod, struct lys_ext_instance **orig, uint8_t size, void *parent, |
| 1476 | 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] | 1477 | { |
| 1478 | int i; |
| 1479 | uint8_t u = 0; |
| 1480 | struct lys_ext_instance **result; |
| 1481 | struct unres_ext *info, *info_orig; |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 1482 | size_t len; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1483 | |
| 1484 | assert(new); |
| 1485 | |
| 1486 | if (!size) { |
| 1487 | if (orig) { |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1488 | LOGINT(ctx); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1489 | return EXIT_FAILURE; |
| 1490 | } |
| 1491 | (*new) = NULL; |
| 1492 | return EXIT_SUCCESS; |
| 1493 | } |
| 1494 | |
| 1495 | (*new) = result = calloc(size, sizeof *result); |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1496 | LY_CHECK_ERR_RETURN(!result, LOGMEM(ctx), EXIT_FAILURE); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1497 | for (u = 0; u < size; u++) { |
| 1498 | if (orig[u]) { |
| 1499 | /* resolved extension instance, just duplicate it */ |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 1500 | switch(orig[u]->ext_type) { |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1501 | case LYEXT_FLAG: |
| 1502 | result[u] = malloc(sizeof(struct lys_ext_instance)); |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1503 | LY_CHECK_ERR_GOTO(!result[u], LOGMEM(ctx), error); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1504 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 1505 | case LYEXT_COMPLEX: |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 1506 | len = ((struct lyext_plugin_complex*)orig[u]->def->plugin)->instance_size; |
| 1507 | result[u] = calloc(1, len); |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1508 | LY_CHECK_ERR_GOTO(!result[u], LOGMEM(ctx), error); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1509 | |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1510 | ((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] | 1511 | /* TODO duplicate data in extension instance content */ |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 1512 | 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] | 1513 | break; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1514 | } |
| 1515 | /* generic part */ |
| 1516 | result[u]->def = orig[u]->def; |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 1517 | result[u]->flags = LYEXT_OPT_CONTENT; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1518 | result[u]->arg_value = lydict_insert(ctx, orig[u]->arg_value, 0); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1519 | result[u]->parent = parent; |
| 1520 | result[u]->parent_type = parent_type; |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1521 | result[u]->insubstmt = orig[u]->insubstmt; |
| 1522 | result[u]->insubstmt_index = orig[u]->insubstmt_index; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 1523 | result[u]->ext_type = orig[u]->ext_type; |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 1524 | result[u]->priv = NULL; |
| 1525 | result[u]->nodetype = LYS_EXT; |
| 1526 | result[u]->module = mod; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1527 | |
| 1528 | /* extensions */ |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1529 | result[u]->ext_size = orig[u]->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1530 | 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] | 1531 | LYEXT_PAR_EXTINST, &result[u]->ext, shallow, unres)) { |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1532 | goto error; |
| 1533 | } |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1534 | |
| 1535 | /* in case of shallow copy (duplication for deviation), duplicate only the link to private data |
| 1536 | * in a new copy, otherwise (grouping instantiation) do not duplicate the private data */ |
| 1537 | if (shallow) { |
| 1538 | result[u]->priv = orig[u]->priv; |
| 1539 | } |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1540 | } else { |
| 1541 | /* original extension is not yet resolved, so duplicate it in unres */ |
| 1542 | i = unres_schema_find(unres, -1, &orig, UNRES_EXT); |
| 1543 | if (i == -1) { |
| 1544 | /* extension not found in unres */ |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1545 | LOGINT(ctx); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1546 | goto error; |
| 1547 | } |
| 1548 | info_orig = unres->str_snode[i]; |
| 1549 | info = malloc(sizeof *info); |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1550 | LY_CHECK_ERR_GOTO(!info, LOGMEM(ctx), error); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1551 | info->datatype = info_orig->datatype; |
| 1552 | if (info->datatype == LYS_IN_YIN) { |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1553 | 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] | 1554 | } /* else TODO YANG */ |
| 1555 | info->parent = parent; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 1556 | info->mod = mod; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1557 | info->parent_type = parent_type; |
| 1558 | info->ext_index = u; |
| 1559 | if (unres_schema_add_node(info->mod, unres, new, UNRES_EXT, (struct lys_node *)info) == -1) { |
| 1560 | goto error; |
| 1561 | } |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | return EXIT_SUCCESS; |
| 1566 | |
| 1567 | error: |
| 1568 | (*new) = NULL; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1569 | lys_extension_instances_free(ctx, result, u, NULL); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1570 | return EXIT_FAILURE; |
| 1571 | } |
| 1572 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1573 | static struct lys_restr * |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1574 | 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] | 1575 | { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1576 | struct lys_restr *result; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1577 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1578 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1579 | if (!size) { |
| 1580 | return NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1581 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1582 | |
| 1583 | result = calloc(size, sizeof *result); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1584 | LY_CHECK_ERR_RETURN(!result, LOGMEM(mod->ctx), NULL); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1585 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1586 | for (i = 0; i < size; i++) { |
Radek Krejci | 77f22b2 | 2017-01-17 15:23:03 +0100 | [diff] [blame] | 1587 | result[i].ext_size = old[i].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1588 | 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] | 1589 | result[i].expr = lydict_insert(mod->ctx, old[i].expr, 0); |
| 1590 | result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0); |
| 1591 | result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0); |
| 1592 | result[i].eapptag = lydict_insert(mod->ctx, old[i].eapptag, 0); |
| 1593 | result[i].emsg = lydict_insert(mod->ctx, old[i].emsg, 0); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | return result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1597 | } |
| 1598 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1599 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1600 | lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr, |
| 1601 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1602 | { |
| 1603 | assert(ctx); |
| 1604 | if (!restr) { |
| 1605 | return; |
| 1606 | } |
| 1607 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1608 | lys_extension_instances_free(ctx, restr->ext, restr->ext_size, private_destructor); |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1609 | lydict_remove(ctx, restr->expr); |
| 1610 | lydict_remove(ctx, restr->dsc); |
| 1611 | lydict_remove(ctx, restr->ref); |
| 1612 | lydict_remove(ctx, restr->eapptag); |
| 1613 | lydict_remove(ctx, restr->emsg); |
| 1614 | } |
| 1615 | |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 1616 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1617 | 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] | 1618 | int shallow, void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 1619 | { |
| 1620 | uint8_t i; |
| 1621 | |
| 1622 | for (i = 0; i < iffeature_size; ++i) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1623 | 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] | 1624 | if (!shallow) { |
Frank Rimpler | 2a503f5 | 2017-09-12 15:21:18 +0000 | [diff] [blame] | 1625 | free(iffeature[i].expr); |
| 1626 | free(iffeature[i].features); |
| 1627 | } |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 1628 | } |
| 1629 | free(iffeature); |
| 1630 | } |
| 1631 | |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1632 | static int |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1633 | 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] | 1634 | 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] | 1635 | { |
| 1636 | int i; |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1637 | unsigned int u; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1638 | |
| 1639 | switch (base) { |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1640 | case LY_TYPE_BINARY: |
| 1641 | if (old->info.binary.length) { |
| 1642 | new->info.binary.length = lys_restr_dup(mod, old->info.binary.length, 1, shallow, unres); |
| 1643 | } |
| 1644 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1645 | |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1646 | case LY_TYPE_BITS: |
| 1647 | new->info.bits.count = old->info.bits.count; |
| 1648 | if (new->info.bits.count) { |
| 1649 | 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] | 1650 | LY_CHECK_ERR_RETURN(!new->info.bits.bit, LOGMEM(mod->ctx), -1); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1651 | |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1652 | for (u = 0; u < new->info.bits.count; u++) { |
| 1653 | new->info.bits.bit[u].name = lydict_insert(mod->ctx, old->info.bits.bit[u].name, 0); |
| 1654 | new->info.bits.bit[u].dsc = lydict_insert(mod->ctx, old->info.bits.bit[u].dsc, 0); |
| 1655 | new->info.bits.bit[u].ref = lydict_insert(mod->ctx, old->info.bits.bit[u].ref, 0); |
| 1656 | new->info.bits.bit[u].flags = old->info.bits.bit[u].flags; |
| 1657 | new->info.bits.bit[u].pos = old->info.bits.bit[u].pos; |
| 1658 | 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] | 1659 | 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] | 1660 | &new->info.bits.bit[u], LYEXT_PAR_TYPE_BIT, |
| 1661 | &new->info.bits.bit[u].ext, shallow, unres)) { |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1662 | return -1; |
| 1663 | } |
| 1664 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1665 | } |
| 1666 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1667 | |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1668 | case LY_TYPE_DEC64: |
| 1669 | new->info.dec64.dig = old->info.dec64.dig; |
| 1670 | new->info.dec64.div = old->info.dec64.div; |
| 1671 | if (old->info.dec64.range) { |
| 1672 | new->info.dec64.range = lys_restr_dup(mod, old->info.dec64.range, 1, shallow, unres); |
| 1673 | } |
| 1674 | break; |
| 1675 | |
| 1676 | case LY_TYPE_ENUM: |
| 1677 | new->info.enums.count = old->info.enums.count; |
| 1678 | if (new->info.enums.count) { |
| 1679 | 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] | 1680 | LY_CHECK_ERR_RETURN(!new->info.enums.enm, LOGMEM(mod->ctx), -1); |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1681 | |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1682 | for (u = 0; u < new->info.enums.count; u++) { |
| 1683 | new->info.enums.enm[u].name = lydict_insert(mod->ctx, old->info.enums.enm[u].name, 0); |
| 1684 | new->info.enums.enm[u].dsc = lydict_insert(mod->ctx, old->info.enums.enm[u].dsc, 0); |
| 1685 | new->info.enums.enm[u].ref = lydict_insert(mod->ctx, old->info.enums.enm[u].ref, 0); |
| 1686 | new->info.enums.enm[u].flags = old->info.enums.enm[u].flags; |
| 1687 | new->info.enums.enm[u].value = old->info.enums.enm[u].value; |
| 1688 | 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] | 1689 | 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] | 1690 | &new->info.enums.enm[u], LYEXT_PAR_TYPE_ENUM, |
| 1691 | &new->info.enums.enm[u].ext, shallow, unres)) { |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1692 | return -1; |
| 1693 | } |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1694 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1695 | } |
| 1696 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1697 | |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1698 | case LY_TYPE_IDENT: |
| 1699 | new->info.ident.count = old->info.ident.count; |
| 1700 | if (old->info.ident.count) { |
| 1701 | 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] | 1702 | LY_CHECK_ERR_RETURN(!new->info.ident.ref, LOGMEM(mod->ctx), -1); |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1703 | memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref); |
| 1704 | } else { |
| 1705 | /* there can be several unresolved base identities, duplicate them all */ |
| 1706 | i = -1; |
| 1707 | do { |
| 1708 | i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF); |
| 1709 | if (i != -1) { |
| 1710 | 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] | 1711 | return -1; |
| 1712 | } |
| 1713 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1714 | --i; |
| 1715 | } while (i > -1); |
| 1716 | } |
| 1717 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1718 | |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1719 | case LY_TYPE_INST: |
| 1720 | new->info.inst.req = old->info.inst.req; |
| 1721 | break; |
| 1722 | |
| 1723 | case LY_TYPE_INT8: |
| 1724 | case LY_TYPE_INT16: |
| 1725 | case LY_TYPE_INT32: |
| 1726 | case LY_TYPE_INT64: |
| 1727 | case LY_TYPE_UINT8: |
| 1728 | case LY_TYPE_UINT16: |
| 1729 | case LY_TYPE_UINT32: |
| 1730 | case LY_TYPE_UINT64: |
| 1731 | if (old->info.num.range) { |
| 1732 | new->info.num.range = lys_restr_dup(mod, old->info.num.range, 1, shallow, unres); |
| 1733 | } |
| 1734 | break; |
| 1735 | |
| 1736 | case LY_TYPE_LEAFREF: |
| 1737 | if (old->info.lref.path) { |
| 1738 | new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0); |
Michal Vasko | 48c73df | 2018-10-31 09:09:13 +0100 | [diff] [blame^] | 1739 | new->info.lref.req = old->info.lref.req; |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1740 | if (!in_grp && unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent) == -1) { |
| 1741 | return -1; |
| 1742 | } |
| 1743 | } |
| 1744 | break; |
| 1745 | |
| 1746 | case LY_TYPE_STRING: |
| 1747 | if (old->info.str.length) { |
| 1748 | new->info.str.length = lys_restr_dup(mod, old->info.str.length, 1, shallow, unres); |
| 1749 | } |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 1750 | if (old->info.str.pat_count) { |
| 1751 | new->info.str.patterns = lys_restr_dup(mod, old->info.str.patterns, old->info.str.pat_count, shallow, unres); |
| 1752 | new->info.str.pat_count = old->info.str.pat_count; |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 1753 | #ifdef LY_ENABLED_CACHE |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 1754 | if (!in_grp) { |
| 1755 | 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] | 1756 | 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] | 1757 | for (u = 0; u < new->info.str.pat_count; u++) { |
Michal Vasko | a26db30 | 2018-02-14 15:22:10 +0100 | [diff] [blame] | 1758 | 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] | 1759 | (pcre**)&new->info.str.patterns_pcre[2 * u], |
| 1760 | (pcre_extra**)&new->info.str.patterns_pcre[2 * u + 1])) { |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 1761 | free(new->info.str.patterns_pcre); |
| 1762 | new->info.str.patterns_pcre = NULL; |
| 1763 | return -1; |
| 1764 | } |
| 1765 | } |
| 1766 | } |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 1767 | #endif |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 1768 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1769 | break; |
| 1770 | |
| 1771 | case LY_TYPE_UNION: |
| 1772 | new->info.uni.has_ptr_type = old->info.uni.has_ptr_type; |
| 1773 | new->info.uni.count = old->info.uni.count; |
| 1774 | if (new->info.uni.count) { |
| 1775 | 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] | 1776 | LY_CHECK_ERR_RETURN(!new->info.uni.types, LOGMEM(mod->ctx), -1); |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1777 | |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1778 | for (u = 0; u < new->info.uni.count; u++) { |
| 1779 | 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] | 1780 | shallow, unres)) { |
| 1781 | return -1; |
| 1782 | } |
| 1783 | } |
| 1784 | } |
| 1785 | break; |
| 1786 | |
| 1787 | default: |
| 1788 | /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */ |
| 1789 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1790 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1791 | |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1792 | return EXIT_SUCCESS; |
| 1793 | } |
| 1794 | |
| 1795 | struct yang_type * |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1796 | 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] | 1797 | int in_grp, int shallow, struct unres_schema *unres) |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1798 | { |
| 1799 | struct yang_type *new; |
| 1800 | |
| 1801 | new = calloc(1, sizeof *new); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1802 | LY_CHECK_ERR_RETURN(!new, LOGMEM(module->ctx), NULL); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1803 | new->flags = old->flags; |
| 1804 | new->base = old->base; |
Pavol Vican | 6658629 | 2016-04-07 11:38:52 +0200 | [diff] [blame] | 1805 | new->name = lydict_insert(module->ctx, old->name, 0); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1806 | new->type = type; |
| 1807 | if (!new->name) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1808 | LOGMEM(module->ctx); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1809 | goto error; |
| 1810 | } |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1811 | 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] | 1812 | new->type->base = new->base; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1813 | lys_type_free(module->ctx, new->type, NULL); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1814 | memset(&new->type->info, 0, sizeof new->type->info); |
| 1815 | goto error; |
| 1816 | } |
| 1817 | return new; |
| 1818 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1819 | error: |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1820 | free(new); |
| 1821 | return NULL; |
| 1822 | } |
| 1823 | |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1824 | int |
| 1825 | lys_copy_union_leafrefs(struct lys_module *mod, struct lys_node *parent, struct lys_type *type, struct lys_type *prev_new, |
| 1826 | struct unres_schema *unres) |
| 1827 | { |
| 1828 | struct lys_type new; |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1829 | unsigned int i, top_type; |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1830 | struct lys_ext_instance **ext; |
| 1831 | uint8_t ext_size; |
| 1832 | void *reloc; |
| 1833 | |
| 1834 | if (!prev_new) { |
| 1835 | /* this is the "top-level" type, meaning it is a real type and no typedef directly above */ |
| 1836 | top_type = 1; |
| 1837 | |
| 1838 | memset(&new, 0, sizeof new); |
| 1839 | |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1840 | new.base = type->base; |
| 1841 | new.parent = (struct lys_tpdf *)parent; |
| 1842 | |
| 1843 | prev_new = &new; |
| 1844 | } else { |
| 1845 | /* this is not top-level type, just a type of a typedef */ |
| 1846 | top_type = 0; |
| 1847 | } |
| 1848 | |
Radek Krejci | 9c5cb6d | 2017-08-09 11:15:23 +0200 | [diff] [blame] | 1849 | assert(type->der); |
| 1850 | if (type->der->module) { |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1851 | /* typedef, skip it, but keep the extensions */ |
| 1852 | ext_size = type->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1853 | 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] | 1854 | return -1; |
| 1855 | } |
| 1856 | if (prev_new->ext) { |
| 1857 | 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] | 1858 | LY_CHECK_ERR_RETURN(!reloc, LOGMEM(mod->ctx), -1); |
Radek Krejci | 70379e2 | 2017-08-09 11:21:07 +0200 | [diff] [blame] | 1859 | prev_new->ext = reloc; |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1860 | |
| 1861 | memcpy(prev_new->ext + prev_new->ext_size, ext, ext_size * sizeof *ext); |
| 1862 | free(ext); |
| 1863 | |
| 1864 | prev_new->ext_size += ext_size; |
| 1865 | } else { |
| 1866 | prev_new->ext = ext; |
| 1867 | prev_new->ext_size = ext_size; |
| 1868 | } |
| 1869 | |
| 1870 | if (lys_copy_union_leafrefs(mod, parent, &type->der->type, prev_new, unres)) { |
| 1871 | return -1; |
| 1872 | } |
| 1873 | } else { |
| 1874 | /* type, just make a deep copy */ |
| 1875 | switch (type->base) { |
| 1876 | case LY_TYPE_UNION: |
| 1877 | prev_new->info.uni.has_ptr_type = type->info.uni.has_ptr_type; |
| 1878 | prev_new->info.uni.count = type->info.uni.count; |
| 1879 | /* this cannot be a typedef anymore */ |
| 1880 | assert(prev_new->info.uni.count); |
| 1881 | |
| 1882 | 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] | 1883 | 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] | 1884 | |
| 1885 | for (i = 0; i < prev_new->info.uni.count; i++) { |
| 1886 | if (lys_copy_union_leafrefs(mod, parent, &(type->info.uni.types[i]), &(prev_new->info.uni.types[i]), unres)) { |
| 1887 | return -1; |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | prev_new->der = type->der; |
| 1892 | break; |
| 1893 | default: |
| 1894 | if (lys_type_dup(mod, parent, prev_new, type, 0, 0, unres)) { |
| 1895 | return -1; |
| 1896 | } |
| 1897 | break; |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | if (top_type) { |
| 1902 | memcpy(type, prev_new, sizeof *type); |
| 1903 | } |
| 1904 | return EXIT_SUCCESS; |
| 1905 | } |
| 1906 | |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1907 | API const void * |
| 1908 | lys_ext_instance_substmt(const struct lys_ext_instance *ext) |
| 1909 | { |
| 1910 | if (!ext) { |
| 1911 | return NULL; |
| 1912 | } |
| 1913 | |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1914 | switch (ext->insubstmt) { |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1915 | case LYEXT_SUBSTMT_SELF: |
| 1916 | case LYEXT_SUBSTMT_MODIFIER: |
| 1917 | case LYEXT_SUBSTMT_VERSION: |
| 1918 | return NULL; |
| 1919 | case LYEXT_SUBSTMT_ARGUMENT: |
| 1920 | if (ext->parent_type == LYEXT_PAR_EXT) { |
| 1921 | return ((struct lys_ext_instance*)ext->parent)->arg_value; |
| 1922 | } |
| 1923 | break; |
| 1924 | case LYEXT_SUBSTMT_BASE: |
| 1925 | if (ext->parent_type == LYEXT_PAR_TYPE) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1926 | return ((struct lys_type*)ext->parent)->info.ident.ref[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1927 | } else if (ext->parent_type == LYEXT_PAR_IDENT) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1928 | return ((struct lys_ident*)ext->parent)->base[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1929 | } |
| 1930 | break; |
| 1931 | case LYEXT_SUBSTMT_BELONGSTO: |
| 1932 | if (ext->parent_type == LYEXT_PAR_MODULE && ((struct lys_module*)ext->parent)->type) { |
| 1933 | return ((struct lys_submodule*)ext->parent)->belongsto; |
| 1934 | } |
| 1935 | break; |
| 1936 | case LYEXT_SUBSTMT_CONFIG: |
| 1937 | case LYEXT_SUBSTMT_MANDATORY: |
| 1938 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 1939 | return &((struct lys_node*)ext->parent)->flags; |
| 1940 | } else if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
| 1941 | return &((struct lys_deviate*)ext->parent)->flags; |
| 1942 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 1943 | return &((struct lys_refine*)ext->parent)->flags; |
| 1944 | } |
| 1945 | break; |
| 1946 | case LYEXT_SUBSTMT_CONTACT: |
| 1947 | if (ext->parent_type == LYEXT_PAR_MODULE) { |
| 1948 | return ((struct lys_module*)ext->parent)->contact; |
| 1949 | } |
| 1950 | break; |
| 1951 | case LYEXT_SUBSTMT_DEFAULT: |
| 1952 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 1953 | switch (((struct lys_node*)ext->parent)->nodetype) { |
| 1954 | case LYS_LEAF: |
| 1955 | case LYS_LEAFLIST: |
| 1956 | /* in case of leaf, the index is supposed to be 0, so it will return the |
| 1957 | * correct pointer despite the leaf structure does not have dflt as array */ |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1958 | return ((struct lys_node_leaflist*)ext->parent)->dflt[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1959 | case LYS_CHOICE: |
| 1960 | return ((struct lys_node_choice*)ext->parent)->dflt; |
| 1961 | default: |
| 1962 | /* internal error */ |
| 1963 | break; |
| 1964 | } |
| 1965 | } else if (ext->parent_type == LYEXT_PAR_TPDF) { |
| 1966 | return ((struct lys_tpdf*)ext->parent)->dflt; |
| 1967 | } else if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1968 | return ((struct lys_deviate*)ext->parent)->dflt[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1969 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1970 | return &((struct lys_refine*)ext->parent)->dflt[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1971 | } |
| 1972 | break; |
| 1973 | case LYEXT_SUBSTMT_DESCRIPTION: |
| 1974 | switch (ext->parent_type) { |
| 1975 | case LYEXT_PAR_NODE: |
| 1976 | return ((struct lys_node*)ext->parent)->dsc; |
| 1977 | case LYEXT_PAR_MODULE: |
| 1978 | return ((struct lys_module*)ext->parent)->dsc; |
| 1979 | case LYEXT_PAR_IMPORT: |
| 1980 | return ((struct lys_import*)ext->parent)->dsc; |
| 1981 | case LYEXT_PAR_INCLUDE: |
| 1982 | return ((struct lys_include*)ext->parent)->dsc; |
| 1983 | case LYEXT_PAR_EXT: |
| 1984 | return ((struct lys_ext*)ext->parent)->dsc; |
| 1985 | case LYEXT_PAR_FEATURE: |
| 1986 | return ((struct lys_feature*)ext->parent)->dsc; |
| 1987 | case LYEXT_PAR_TPDF: |
| 1988 | return ((struct lys_tpdf*)ext->parent)->dsc; |
| 1989 | case LYEXT_PAR_TYPE_BIT: |
| 1990 | return ((struct lys_type_bit*)ext->parent)->dsc; |
| 1991 | case LYEXT_PAR_TYPE_ENUM: |
| 1992 | return ((struct lys_type_enum*)ext->parent)->dsc; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1993 | case LYEXT_PAR_RESTR: |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1994 | return ((struct lys_restr*)ext->parent)->dsc; |
| 1995 | case LYEXT_PAR_WHEN: |
| 1996 | return ((struct lys_when*)ext->parent)->dsc; |
| 1997 | case LYEXT_PAR_IDENT: |
| 1998 | return ((struct lys_ident*)ext->parent)->dsc; |
| 1999 | case LYEXT_PAR_DEVIATION: |
| 2000 | return ((struct lys_deviation*)ext->parent)->dsc; |
| 2001 | case LYEXT_PAR_REVISION: |
| 2002 | return ((struct lys_revision*)ext->parent)->dsc; |
| 2003 | case LYEXT_PAR_REFINE: |
| 2004 | return ((struct lys_refine*)ext->parent)->dsc; |
| 2005 | default: |
| 2006 | break; |
| 2007 | } |
| 2008 | break; |
| 2009 | case LYEXT_SUBSTMT_ERRTAG: |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 2010 | if (ext->parent_type == LYEXT_PAR_RESTR) { |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 2011 | return ((struct lys_restr*)ext->parent)->eapptag; |
| 2012 | } |
| 2013 | break; |
| 2014 | case LYEXT_SUBSTMT_ERRMSG: |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 2015 | if (ext->parent_type == LYEXT_PAR_RESTR) { |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 2016 | return ((struct lys_restr*)ext->parent)->emsg; |
| 2017 | } |
| 2018 | break; |
| 2019 | case LYEXT_SUBSTMT_DIGITS: |
| 2020 | if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_DEC64) { |
| 2021 | return &((struct lys_type*)ext->parent)->info.dec64.dig; |
| 2022 | } |
| 2023 | break; |
| 2024 | case LYEXT_SUBSTMT_KEY: |
| 2025 | if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) { |
| 2026 | return ((struct lys_node_list*)ext->parent)->keys; |
| 2027 | } |
| 2028 | break; |
| 2029 | case LYEXT_SUBSTMT_MAX: |
| 2030 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 2031 | if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) { |
| 2032 | return &((struct lys_node_list*)ext->parent)->max; |
| 2033 | } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) { |
| 2034 | return &((struct lys_node_leaflist*)ext->parent)->max; |
| 2035 | } |
| 2036 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 2037 | return &((struct lys_refine*)ext->parent)->mod.list.max; |
| 2038 | } |
| 2039 | break; |
| 2040 | case LYEXT_SUBSTMT_MIN: |
| 2041 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 2042 | if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) { |
| 2043 | return &((struct lys_node_list*)ext->parent)->min; |
| 2044 | } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) { |
| 2045 | return &((struct lys_node_leaflist*)ext->parent)->min; |
| 2046 | } |
| 2047 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 2048 | return &((struct lys_refine*)ext->parent)->mod.list.min; |
| 2049 | } |
| 2050 | break; |
| 2051 | case LYEXT_SUBSTMT_NAMESPACE: |
| 2052 | if (ext->parent_type == LYEXT_PAR_MODULE && !((struct lys_module*)ext->parent)->type) { |
| 2053 | return ((struct lys_module*)ext->parent)->ns; |
| 2054 | } |
| 2055 | break; |
| 2056 | case LYEXT_SUBSTMT_ORDEREDBY: |
| 2057 | if (ext->parent_type == LYEXT_PAR_NODE && |
| 2058 | (((struct lys_node*)ext->parent)->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 2059 | return &((struct lys_node_list*)ext->parent)->flags; |
| 2060 | } |
| 2061 | break; |
| 2062 | case LYEXT_SUBSTMT_ORGANIZATION: |
| 2063 | if (ext->parent_type == LYEXT_PAR_MODULE) { |
| 2064 | return ((struct lys_module*)ext->parent)->org; |
| 2065 | } |
| 2066 | break; |
| 2067 | case LYEXT_SUBSTMT_PATH: |
| 2068 | if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) { |
| 2069 | return ((struct lys_type*)ext->parent)->info.lref.path; |
| 2070 | } |
| 2071 | break; |
| 2072 | case LYEXT_SUBSTMT_POSITION: |
| 2073 | if (ext->parent_type == LYEXT_PAR_TYPE_BIT) { |
| 2074 | return &((struct lys_type_bit*)ext->parent)->pos; |
| 2075 | } |
| 2076 | break; |
| 2077 | case LYEXT_SUBSTMT_PREFIX: |
| 2078 | if (ext->parent_type == LYEXT_PAR_MODULE) { |
| 2079 | /* covers also lys_submodule */ |
| 2080 | return ((struct lys_module*)ext->parent)->prefix; |
| 2081 | } else if (ext->parent_type == LYEXT_PAR_IMPORT) { |
| 2082 | return ((struct lys_import*)ext->parent)->prefix; |
| 2083 | } |
| 2084 | break; |
| 2085 | case LYEXT_SUBSTMT_PRESENCE: |
| 2086 | if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_CONTAINER) { |
| 2087 | return ((struct lys_node_container*)ext->parent)->presence; |
| 2088 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 2089 | return ((struct lys_refine*)ext->parent)->mod.presence; |
| 2090 | } |
| 2091 | break; |
| 2092 | case LYEXT_SUBSTMT_REFERENCE: |
| 2093 | switch (ext->parent_type) { |
| 2094 | case LYEXT_PAR_NODE: |
| 2095 | return ((struct lys_node*)ext->parent)->ref; |
| 2096 | case LYEXT_PAR_MODULE: |
| 2097 | return ((struct lys_module*)ext->parent)->ref; |
| 2098 | case LYEXT_PAR_IMPORT: |
| 2099 | return ((struct lys_import*)ext->parent)->ref; |
| 2100 | case LYEXT_PAR_INCLUDE: |
| 2101 | return ((struct lys_include*)ext->parent)->ref; |
| 2102 | case LYEXT_PAR_EXT: |
| 2103 | return ((struct lys_ext*)ext->parent)->ref; |
| 2104 | case LYEXT_PAR_FEATURE: |
| 2105 | return ((struct lys_feature*)ext->parent)->ref; |
| 2106 | case LYEXT_PAR_TPDF: |
| 2107 | return ((struct lys_tpdf*)ext->parent)->ref; |
| 2108 | case LYEXT_PAR_TYPE_BIT: |
| 2109 | return ((struct lys_type_bit*)ext->parent)->ref; |
| 2110 | case LYEXT_PAR_TYPE_ENUM: |
| 2111 | return ((struct lys_type_enum*)ext->parent)->ref; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 2112 | case LYEXT_PAR_RESTR: |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 2113 | return ((struct lys_restr*)ext->parent)->ref; |
| 2114 | case LYEXT_PAR_WHEN: |
| 2115 | return ((struct lys_when*)ext->parent)->ref; |
| 2116 | case LYEXT_PAR_IDENT: |
| 2117 | return ((struct lys_ident*)ext->parent)->ref; |
| 2118 | case LYEXT_PAR_DEVIATION: |
| 2119 | return ((struct lys_deviation*)ext->parent)->ref; |
| 2120 | case LYEXT_PAR_REVISION: |
| 2121 | return ((struct lys_revision*)ext->parent)->ref; |
| 2122 | case LYEXT_PAR_REFINE: |
| 2123 | return ((struct lys_refine*)ext->parent)->ref; |
| 2124 | default: |
| 2125 | break; |
| 2126 | } |
| 2127 | break; |
Radek Krejci | be33639 | 2017-02-07 10:54:24 +0100 | [diff] [blame] | 2128 | case LYEXT_SUBSTMT_REQINSTANCE: |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 2129 | if (ext->parent_type == LYEXT_PAR_TYPE) { |
| 2130 | if (((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) { |
| 2131 | return &((struct lys_type*)ext->parent)->info.lref.req; |
| 2132 | } else if (((struct lys_type*)ext->parent)->base == LY_TYPE_INST) { |
| 2133 | return &((struct lys_type*)ext->parent)->info.inst.req; |
| 2134 | } |
| 2135 | } |
| 2136 | break; |
| 2137 | case LYEXT_SUBSTMT_REVISIONDATE: |
| 2138 | if (ext->parent_type == LYEXT_PAR_IMPORT) { |
| 2139 | return ((struct lys_import*)ext->parent)->rev; |
| 2140 | } else if (ext->parent_type == LYEXT_PAR_INCLUDE) { |
| 2141 | return ((struct lys_include*)ext->parent)->rev; |
| 2142 | } |
| 2143 | break; |
| 2144 | case LYEXT_SUBSTMT_STATUS: |
| 2145 | switch (ext->parent_type) { |
| 2146 | case LYEXT_PAR_NODE: |
| 2147 | case LYEXT_PAR_IDENT: |
| 2148 | case LYEXT_PAR_TPDF: |
| 2149 | case LYEXT_PAR_EXT: |
| 2150 | case LYEXT_PAR_FEATURE: |
| 2151 | case LYEXT_PAR_TYPE_ENUM: |
| 2152 | case LYEXT_PAR_TYPE_BIT: |
| 2153 | /* in all structures the flags member is at the same offset */ |
| 2154 | return &((struct lys_node*)ext->parent)->flags; |
| 2155 | default: |
| 2156 | break; |
| 2157 | } |
| 2158 | break; |
| 2159 | case LYEXT_SUBSTMT_UNIQUE: |
| 2160 | if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 2161 | return &((struct lys_deviate*)ext->parent)->unique[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 2162 | } 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] | 2163 | return &((struct lys_node_list*)ext->parent)->unique[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 2164 | } |
| 2165 | break; |
| 2166 | case LYEXT_SUBSTMT_UNITS: |
| 2167 | if (ext->parent_type == LYEXT_PAR_NODE && |
| 2168 | (((struct lys_node*)ext->parent)->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2169 | /* units is at the same offset in both lys_node_leaf and lys_node_leaflist */ |
| 2170 | return ((struct lys_node_leaf*)ext->parent)->units; |
| 2171 | } else if (ext->parent_type == LYEXT_PAR_TPDF) { |
| 2172 | return ((struct lys_tpdf*)ext->parent)->units; |
| 2173 | } else if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
| 2174 | return ((struct lys_deviate*)ext->parent)->units; |
| 2175 | } |
| 2176 | break; |
| 2177 | case LYEXT_SUBSTMT_VALUE: |
| 2178 | if (ext->parent_type == LYEXT_PAR_TYPE_ENUM) { |
| 2179 | return &((struct lys_type_enum*)ext->parent)->value; |
| 2180 | } |
| 2181 | break; |
| 2182 | case LYEXT_SUBSTMT_YINELEM: |
| 2183 | if (ext->parent_type == LYEXT_PAR_EXT) { |
| 2184 | return &((struct lys_ext*)ext->parent)->flags; |
| 2185 | } |
| 2186 | break; |
| 2187 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2188 | LOGINT(ext->module->ctx); |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 2189 | return NULL; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2190 | } |
| 2191 | |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 2192 | static int |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2193 | 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] | 2194 | int in_grp, int shallow, struct unres_schema *unres) |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 2195 | { |
| 2196 | int i; |
| 2197 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 2198 | new->base = old->base; |
| 2199 | new->der = old->der; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 2200 | new->parent = (struct lys_tpdf *)parent; |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 2201 | new->ext_size = old->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 2202 | 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] | 2203 | return -1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2204 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 2205 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 2206 | i = unres_schema_find(unres, -1, old, UNRES_TYPE_DER); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2207 | if (i != -1) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 2208 | /* HACK (serious one) for unres */ |
| 2209 | /* nothing else we can do but duplicate it immediately */ |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 2210 | if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2211 | new->der = (struct lys_tpdf *)lys_yang_type_dup(mod, parent, (struct yang_type *)old->der, new, in_grp, |
| 2212 | shallow, unres); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 2213 | } else { |
| 2214 | new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1); |
| 2215 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 2216 | /* all these unres additions can fail even though they did not before */ |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 2217 | 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] | 2218 | return -1; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2219 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 2220 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2221 | } |
| 2222 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2223 | 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] | 2224 | } |
| 2225 | |
| 2226 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2227 | lys_type_free(struct ly_ctx *ctx, struct lys_type *type, |
| 2228 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2229 | { |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 2230 | unsigned int i; |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2231 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2232 | assert(ctx); |
| 2233 | if (!type) { |
| 2234 | return; |
| 2235 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2236 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2237 | lys_extension_instances_free(ctx, type->ext, type->ext_size, private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2238 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2239 | switch (type->base) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 2240 | case LY_TYPE_BINARY: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2241 | lys_restr_free(ctx, type->info.binary.length, private_destructor); |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 2242 | free(type->info.binary.length); |
| 2243 | break; |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 2244 | case LY_TYPE_BITS: |
| 2245 | for (i = 0; i < type->info.bits.count; i++) { |
| 2246 | lydict_remove(ctx, type->info.bits.bit[i].name); |
| 2247 | lydict_remove(ctx, type->info.bits.bit[i].dsc); |
| 2248 | lydict_remove(ctx, type->info.bits.bit[i].ref); |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2249 | 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] | 2250 | private_destructor); |
| 2251 | lys_extension_instances_free(ctx, type->info.bits.bit[i].ext, type->info.bits.bit[i].ext_size, |
| 2252 | private_destructor); |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 2253 | } |
| 2254 | free(type->info.bits.bit); |
| 2255 | break; |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 2256 | |
| 2257 | case LY_TYPE_DEC64: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2258 | lys_restr_free(ctx, type->info.dec64.range, private_destructor); |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 2259 | free(type->info.dec64.range); |
| 2260 | break; |
| 2261 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2262 | case LY_TYPE_ENUM: |
| 2263 | for (i = 0; i < type->info.enums.count; i++) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2264 | lydict_remove(ctx, type->info.enums.enm[i].name); |
| 2265 | lydict_remove(ctx, type->info.enums.enm[i].dsc); |
| 2266 | lydict_remove(ctx, type->info.enums.enm[i].ref); |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2267 | 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] | 2268 | private_destructor); |
| 2269 | lys_extension_instances_free(ctx, type->info.enums.enm[i].ext, type->info.enums.enm[i].ext_size, |
| 2270 | private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2271 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2272 | free(type->info.enums.enm); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2273 | break; |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 2274 | |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 2275 | case LY_TYPE_INT8: |
| 2276 | case LY_TYPE_INT16: |
| 2277 | case LY_TYPE_INT32: |
| 2278 | case LY_TYPE_INT64: |
| 2279 | case LY_TYPE_UINT8: |
| 2280 | case LY_TYPE_UINT16: |
| 2281 | case LY_TYPE_UINT32: |
| 2282 | case LY_TYPE_UINT64: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2283 | lys_restr_free(ctx, type->info.num.range, private_destructor); |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 2284 | free(type->info.num.range); |
| 2285 | break; |
| 2286 | |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 2287 | case LY_TYPE_LEAFREF: |
| 2288 | lydict_remove(ctx, type->info.lref.path); |
| 2289 | break; |
| 2290 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 2291 | case LY_TYPE_STRING: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2292 | lys_restr_free(ctx, type->info.str.length, private_destructor); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 2293 | free(type->info.str.length); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 2294 | for (i = 0; i < type->info.str.pat_count; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2295 | lys_restr_free(ctx, &type->info.str.patterns[i], private_destructor); |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 2296 | #ifdef LY_ENABLED_CACHE |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 2297 | if (type->info.str.patterns_pcre) { |
| 2298 | pcre_free((pcre*)type->info.str.patterns_pcre[2 * i]); |
| 2299 | pcre_free_study((pcre_extra*)type->info.str.patterns_pcre[2 * i + 1]); |
| 2300 | } |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 2301 | #endif |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 2302 | } |
| 2303 | free(type->info.str.patterns); |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 2304 | #ifdef LY_ENABLED_CACHE |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 2305 | free(type->info.str.patterns_pcre); |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 2306 | #endif |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 2307 | break; |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 2308 | |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 2309 | case LY_TYPE_UNION: |
| 2310 | for (i = 0; i < type->info.uni.count; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2311 | lys_type_free(ctx, &type->info.uni.types[i], private_destructor); |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 2312 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2313 | free(type->info.uni.types); |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 2314 | break; |
| 2315 | |
Michal Vasko | d328219 | 2016-09-05 11:27:57 +0200 | [diff] [blame] | 2316 | case LY_TYPE_IDENT: |
| 2317 | free(type->info.ident.ref); |
| 2318 | break; |
| 2319 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2320 | default: |
Michal Vasko | d328219 | 2016-09-05 11:27:57 +0200 | [diff] [blame] | 2321 | /* 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] | 2322 | break; |
| 2323 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2324 | } |
| 2325 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2326 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2327 | lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf, |
| 2328 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2329 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2330 | assert(ctx); |
| 2331 | if (!tpdf) { |
| 2332 | return; |
| 2333 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2334 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2335 | lydict_remove(ctx, tpdf->name); |
| 2336 | lydict_remove(ctx, tpdf->dsc); |
| 2337 | lydict_remove(ctx, tpdf->ref); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2338 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2339 | lys_type_free(ctx, &tpdf->type, private_destructor); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2340 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2341 | lydict_remove(ctx, tpdf->units); |
| 2342 | lydict_remove(ctx, tpdf->dflt); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2343 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2344 | lys_extension_instances_free(ctx, tpdf->ext, tpdf->ext_size, private_destructor); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2345 | } |
| 2346 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2347 | static struct lys_when * |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2348 | 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] | 2349 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2350 | struct lys_when *new; |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2351 | |
| 2352 | if (!old) { |
| 2353 | return NULL; |
| 2354 | } |
| 2355 | |
| 2356 | new = calloc(1, sizeof *new); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2357 | LY_CHECK_ERR_RETURN(!new, LOGMEM(mod->ctx), NULL); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 2358 | new->cond = lydict_insert(mod->ctx, old->cond, 0); |
| 2359 | new->dsc = lydict_insert(mod->ctx, old->dsc, 0); |
| 2360 | new->ref = lydict_insert(mod->ctx, old->ref, 0); |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 2361 | new->ext_size = old->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 2362 | 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] | 2363 | |
| 2364 | return new; |
| 2365 | } |
| 2366 | |
Michal Vasko | 0308dd6 | 2015-10-07 09:14:40 +0200 | [diff] [blame] | 2367 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2368 | lys_when_free(struct ly_ctx *ctx, struct lys_when *w, |
| 2369 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2370 | { |
| 2371 | if (!w) { |
| 2372 | return; |
| 2373 | } |
| 2374 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2375 | lys_extension_instances_free(ctx, w->ext, w->ext_size, private_destructor); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2376 | lydict_remove(ctx, w->cond); |
| 2377 | lydict_remove(ctx, w->dsc); |
| 2378 | lydict_remove(ctx, w->ref); |
| 2379 | |
| 2380 | free(w); |
| 2381 | } |
| 2382 | |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2383 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2384 | lys_augment_free(struct ly_ctx *ctx, struct lys_node_augment *aug, |
| 2385 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2386 | { |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 2387 | struct lys_node *next, *sub; |
| 2388 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2389 | /* children from a resolved augment are freed under the target node */ |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 2390 | if (!aug->target || (aug->flags & LYS_NOTAPPLIED)) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2391 | LY_TREE_FOR_SAFE(aug->child, next, sub) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2392 | lys_node_free(sub, private_destructor, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2393 | } |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 2394 | } |
| 2395 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2396 | lydict_remove(ctx, aug->target_name); |
| 2397 | lydict_remove(ctx, aug->dsc); |
| 2398 | lydict_remove(ctx, aug->ref); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2399 | |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2400 | lys_iffeature_free(ctx, aug->iffeature, aug->iffeature_size, 0, private_destructor); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2401 | lys_extension_instances_free(ctx, aug->ext, aug->ext_size, private_destructor); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2402 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2403 | lys_when_free(ctx, aug->when, private_destructor); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2404 | } |
| 2405 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2406 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2407 | lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident, |
| 2408 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2409 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2410 | assert(ctx); |
| 2411 | if (!ident) { |
| 2412 | return; |
| 2413 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2414 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 2415 | free(ident->base); |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 2416 | ly_set_free(ident->der); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2417 | lydict_remove(ctx, ident->name); |
| 2418 | lydict_remove(ctx, ident->dsc); |
| 2419 | lydict_remove(ctx, ident->ref); |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2420 | lys_iffeature_free(ctx, ident->iffeature, ident->iffeature_size, 0, private_destructor); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2421 | lys_extension_instances_free(ctx, ident->ext, ident->ext_size, private_destructor); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2422 | |
| 2423 | } |
| 2424 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2425 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2426 | lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp, |
| 2427 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2428 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2429 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2430 | |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2431 | /* handle only specific parts for LYS_GROUPING */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2432 | for (i = 0; i < grp->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2433 | lys_tpdf_free(ctx, &grp->tpdf[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2434 | } |
| 2435 | free(grp->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2436 | } |
| 2437 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2438 | static void |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 2439 | lys_rpc_action_free(struct ly_ctx *ctx, struct lys_node_rpc_action *rpc_act, |
| 2440 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
| 2441 | { |
| 2442 | int i; |
| 2443 | |
| 2444 | /* handle only specific parts for LYS_GROUPING */ |
| 2445 | for (i = 0; i < rpc_act->tpdf_size; i++) { |
| 2446 | lys_tpdf_free(ctx, &rpc_act->tpdf[i], private_destructor); |
| 2447 | } |
| 2448 | free(rpc_act->tpdf); |
| 2449 | } |
| 2450 | |
| 2451 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2452 | lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io, |
| 2453 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2454 | { |
| 2455 | int i; |
| 2456 | |
| 2457 | /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */ |
| 2458 | for (i = 0; i < io->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2459 | lys_tpdf_free(ctx, &io->tpdf[i], private_destructor); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2460 | } |
| 2461 | free(io->tpdf); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2462 | |
| 2463 | for (i = 0; i < io->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2464 | lys_restr_free(ctx, &io->must[i], private_destructor); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2465 | } |
| 2466 | free(io->must); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2467 | } |
| 2468 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2469 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2470 | lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif, |
| 2471 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2472 | { |
| 2473 | int i; |
| 2474 | |
| 2475 | for (i = 0; i < notif->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2476 | lys_restr_free(ctx, ¬if->must[i], private_destructor); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2477 | } |
| 2478 | free(notif->must); |
| 2479 | |
| 2480 | for (i = 0; i < notif->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2481 | lys_tpdf_free(ctx, ¬if->tpdf[i], private_destructor); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2482 | } |
| 2483 | free(notif->tpdf); |
| 2484 | } |
| 2485 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2486 | lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml, |
| 2487 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2488 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2489 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2490 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2491 | for (i = 0; i < anyxml->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2492 | lys_restr_free(ctx, &anyxml->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2493 | } |
| 2494 | free(anyxml->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2495 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2496 | lys_when_free(ctx, anyxml->when, private_destructor); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2497 | } |
| 2498 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2499 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2500 | lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf, |
| 2501 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2502 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2503 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2504 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 2505 | /* leafref backlinks */ |
| 2506 | ly_set_free((struct ly_set *)leaf->backlinks); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2507 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2508 | for (i = 0; i < leaf->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2509 | lys_restr_free(ctx, &leaf->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2510 | } |
| 2511 | free(leaf->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2512 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2513 | lys_when_free(ctx, leaf->when, private_destructor); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2514 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2515 | lys_type_free(ctx, &leaf->type, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2516 | lydict_remove(ctx, leaf->units); |
| 2517 | lydict_remove(ctx, leaf->dflt); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2518 | } |
| 2519 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2520 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2521 | lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist, |
| 2522 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2523 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2524 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2525 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2526 | if (llist->backlinks) { |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2527 | /* leafref backlinks */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2528 | ly_set_free(llist->backlinks); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2529 | } |
| 2530 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2531 | for (i = 0; i < llist->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2532 | lys_restr_free(ctx, &llist->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2533 | } |
| 2534 | free(llist->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2535 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2536 | for (i = 0; i < llist->dflt_size; i++) { |
| 2537 | lydict_remove(ctx, llist->dflt[i]); |
| 2538 | } |
| 2539 | free(llist->dflt); |
| 2540 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2541 | lys_when_free(ctx, llist->when, private_destructor); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2542 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2543 | lys_type_free(ctx, &llist->type, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2544 | lydict_remove(ctx, llist->units); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2545 | } |
| 2546 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2547 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2548 | lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list, |
| 2549 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2550 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2551 | int i, j; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2552 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2553 | /* handle only specific parts for LY_NODE_LIST */ |
Michal Vasko | aaeab1d | 2018-02-16 09:36:46 +0100 | [diff] [blame] | 2554 | lys_when_free(ctx, list->when, private_destructor); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2555 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2556 | for (i = 0; i < list->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2557 | lys_restr_free(ctx, &list->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2558 | } |
| 2559 | free(list->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2560 | |
Michal Vasko | aaeab1d | 2018-02-16 09:36:46 +0100 | [diff] [blame] | 2561 | for (i = 0; i < list->tpdf_size; i++) { |
| 2562 | lys_tpdf_free(ctx, &list->tpdf[i], private_destructor); |
| 2563 | } |
| 2564 | free(list->tpdf); |
| 2565 | |
| 2566 | free(list->keys); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2567 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2568 | for (i = 0; i < list->unique_size; i++) { |
Michal Vasko | f5e940a | 2016-06-07 09:39:26 +0200 | [diff] [blame] | 2569 | for (j = 0; j < list->unique[i].expr_size; j++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2570 | lydict_remove(ctx, list->unique[i].expr[j]); |
| 2571 | } |
| 2572 | free(list->unique[i].expr); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2573 | } |
| 2574 | free(list->unique); |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 2575 | |
Michal Vasko | aaeab1d | 2018-02-16 09:36:46 +0100 | [diff] [blame] | 2576 | lydict_remove(ctx, list->keys_str); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2577 | } |
| 2578 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2579 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2580 | lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont, |
| 2581 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2582 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2583 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2584 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2585 | /* handle only specific parts for LY_NODE_CONTAINER */ |
| 2586 | lydict_remove(ctx, cont->presence); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2587 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2588 | for (i = 0; i < cont->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2589 | lys_tpdf_free(ctx, &cont->tpdf[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2590 | } |
| 2591 | free(cont->tpdf); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2592 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2593 | for (i = 0; i < cont->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2594 | lys_restr_free(ctx, &cont->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2595 | } |
| 2596 | free(cont->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2597 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2598 | lys_when_free(ctx, cont->when, private_destructor); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2599 | } |
| 2600 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2601 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2602 | lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f, |
| 2603 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2604 | { |
| 2605 | lydict_remove(ctx, f->name); |
| 2606 | lydict_remove(ctx, f->dsc); |
| 2607 | lydict_remove(ctx, f->ref); |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2608 | lys_iffeature_free(ctx, f->iffeature, f->iffeature_size, 0, private_destructor); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 2609 | ly_set_free(f->depfeatures); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2610 | lys_extension_instances_free(ctx, f->ext, f->ext_size, private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2614 | lys_extension_free(struct ly_ctx *ctx, struct lys_ext *e, |
| 2615 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2616 | { |
| 2617 | lydict_remove(ctx, e->name); |
| 2618 | lydict_remove(ctx, e->dsc); |
| 2619 | lydict_remove(ctx, e->ref); |
| 2620 | lydict_remove(ctx, e->argument); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2621 | lys_extension_instances_free(ctx, e->ext, e->ext_size, private_destructor); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2622 | } |
| 2623 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2624 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2625 | lys_deviation_free(struct lys_module *module, struct lys_deviation *dev, |
| 2626 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2627 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2628 | int i, j, k; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2629 | struct ly_ctx *ctx; |
| 2630 | struct lys_node *next, *elem; |
| 2631 | |
| 2632 | ctx = module->ctx; |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2633 | |
| 2634 | lydict_remove(ctx, dev->target_name); |
| 2635 | lydict_remove(ctx, dev->dsc); |
| 2636 | lydict_remove(ctx, dev->ref); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2637 | lys_extension_instances_free(ctx, dev->ext, dev->ext_size, private_destructor); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2638 | |
Pavol Vican | 64d0b76 | 2016-08-25 10:44:59 +0200 | [diff] [blame] | 2639 | if (!dev->deviate) { |
Michal Vasko | 7427b26 | 2018-05-14 15:23:55 +0200 | [diff] [blame] | 2640 | return; |
Pavol Vican | 64d0b76 | 2016-08-25 10:44:59 +0200 | [diff] [blame] | 2641 | } |
| 2642 | |
Michal Vasko | 7427b26 | 2018-05-14 15:23:55 +0200 | [diff] [blame] | 2643 | /* it could not be applied because it failed to be applied */ |
| 2644 | if (dev->orig_node) { |
| 2645 | /* the module was freed, but we only need the context from orig_node, use ours */ |
| 2646 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 2647 | /* it's actually a node subtree, we need to update modules on all the nodes :-/ */ |
| 2648 | LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) { |
| 2649 | elem->module = module; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2650 | |
Michal Vasko | 7427b26 | 2018-05-14 15:23:55 +0200 | [diff] [blame] | 2651 | LY_TREE_DFS_END(dev->orig_node, next, elem); |
| 2652 | } |
| 2653 | lys_node_free(dev->orig_node, NULL, 0); |
| 2654 | } else { |
| 2655 | /* it's just a shallow copy, freeing one node */ |
| 2656 | dev->orig_node->module = module; |
| 2657 | lys_node_free(dev->orig_node, NULL, 1); |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2658 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2659 | } |
| 2660 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2661 | for (i = 0; i < dev->deviate_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2662 | 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] | 2663 | |
Radek Krejci | d5a5c28 | 2016-08-15 15:38:08 +0200 | [diff] [blame] | 2664 | for (j = 0; j < dev->deviate[i].dflt_size; j++) { |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2665 | lydict_remove(ctx, dev->deviate[i].dflt[j]); |
Radek Krejci | d5a5c28 | 2016-08-15 15:38:08 +0200 | [diff] [blame] | 2666 | } |
| 2667 | free(dev->deviate[i].dflt); |
| 2668 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2669 | lydict_remove(ctx, dev->deviate[i].units); |
| 2670 | |
| 2671 | if (dev->deviate[i].mod == LY_DEVIATE_DEL) { |
| 2672 | for (j = 0; j < dev->deviate[i].must_size; j++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2673 | lys_restr_free(ctx, &dev->deviate[i].must[j], private_destructor); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2674 | } |
| 2675 | free(dev->deviate[i].must); |
| 2676 | |
| 2677 | for (j = 0; j < dev->deviate[i].unique_size; j++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2678 | for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) { |
| 2679 | lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]); |
| 2680 | } |
Michal Vasko | 0238ccf | 2016-03-07 15:02:18 +0100 | [diff] [blame] | 2681 | free(dev->deviate[i].unique[j].expr); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2682 | } |
| 2683 | free(dev->deviate[i].unique); |
| 2684 | } |
| 2685 | } |
| 2686 | free(dev->deviate); |
| 2687 | } |
| 2688 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2689 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2690 | lys_uses_free(struct ly_ctx *ctx, struct lys_node_uses *uses, |
| 2691 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2692 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2693 | int i, j; |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2694 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2695 | for (i = 0; i < uses->refine_size; i++) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2696 | lydict_remove(ctx, uses->refine[i].target_name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2697 | lydict_remove(ctx, uses->refine[i].dsc); |
| 2698 | lydict_remove(ctx, uses->refine[i].ref); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2699 | |
Radek Krejci | fde04bd | 2017-09-13 16:38:38 +0200 | [diff] [blame] | 2700 | lys_iffeature_free(ctx, uses->refine[i].iffeature, uses->refine[i].iffeature_size, 0, private_destructor); |
| 2701 | |
Michal Vasko | a275c0a | 2015-09-24 09:55:42 +0200 | [diff] [blame] | 2702 | for (j = 0; j < uses->refine[i].must_size; j++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2703 | lys_restr_free(ctx, &uses->refine[i].must[j], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2704 | } |
| 2705 | free(uses->refine[i].must); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2706 | |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 2707 | for (j = 0; j < uses->refine[i].dflt_size; j++) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 2708 | lydict_remove(ctx, uses->refine[i].dflt[j]); |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 2709 | } |
| 2710 | free(uses->refine[i].dflt); |
| 2711 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2712 | 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] | 2713 | |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 2714 | if (uses->refine[i].target_type & LYS_CONTAINER) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2715 | lydict_remove(ctx, uses->refine[i].mod.presence); |
| 2716 | } |
| 2717 | } |
| 2718 | free(uses->refine); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2719 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2720 | for (i = 0; i < uses->augment_size; i++) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2721 | lys_augment_free(ctx, &uses->augment[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2722 | } |
| 2723 | free(uses->augment); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2724 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2725 | lys_when_free(ctx, uses->when, private_destructor); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2726 | } |
| 2727 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2728 | void |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2729 | 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] | 2730 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2731 | struct ly_ctx *ctx; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2732 | struct lys_node *sub, *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2733 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2734 | if (!node) { |
| 2735 | return; |
| 2736 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2737 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2738 | assert(node->module); |
| 2739 | assert(node->module->ctx); |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2740 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2741 | ctx = node->module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2742 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2743 | /* remove private object */ |
Mislav Novakovic | b5529e5 | 2016-02-29 11:42:43 +0100 | [diff] [blame] | 2744 | if (node->priv && private_destructor) { |
| 2745 | private_destructor(node, node->priv); |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2746 | } |
| 2747 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2748 | /* common part */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2749 | lydict_remove(ctx, node->name); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2750 | if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2751 | lys_iffeature_free(ctx, node->iffeature, node->iffeature_size, shallow, private_destructor); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2752 | lydict_remove(ctx, node->dsc); |
| 2753 | lydict_remove(ctx, node->ref); |
| 2754 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2755 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2756 | if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2757 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2758 | lys_node_free(sub, private_destructor, 0); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2759 | } |
| 2760 | } |
| 2761 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2762 | lys_extension_instances_free(ctx, node->ext, node->ext_size, private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2763 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2764 | /* specific part */ |
| 2765 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2766 | case LYS_CONTAINER: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2767 | lys_container_free(ctx, (struct lys_node_container *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2768 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2769 | case LYS_CHOICE: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2770 | lys_when_free(ctx, ((struct lys_node_choice *)node)->when, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2771 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2772 | case LYS_LEAF: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2773 | lys_leaf_free(ctx, (struct lys_node_leaf *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2774 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2775 | case LYS_LEAFLIST: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2776 | lys_leaflist_free(ctx, (struct lys_node_leaflist *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2777 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2778 | case LYS_LIST: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2779 | lys_list_free(ctx, (struct lys_node_list *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2780 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2781 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2782 | case LYS_ANYDATA: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2783 | lys_anydata_free(ctx, (struct lys_node_anydata *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2784 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2785 | case LYS_USES: |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2786 | lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2787 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2788 | case LYS_CASE: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2789 | lys_when_free(ctx, ((struct lys_node_case *)node)->when, private_destructor); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2790 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2791 | case LYS_AUGMENT: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2792 | /* do nothing */ |
| 2793 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2794 | case LYS_GROUPING: |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 2795 | lys_grp_free(ctx, (struct lys_node_grp *)node, private_destructor); |
| 2796 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2797 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2798 | case LYS_ACTION: |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 2799 | lys_rpc_action_free(ctx, (struct lys_node_rpc_action *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2800 | break; |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2801 | case LYS_NOTIF: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2802 | lys_notif_free(ctx, (struct lys_node_notif *)node, private_destructor); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2803 | break; |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2804 | case LYS_INPUT: |
| 2805 | case LYS_OUTPUT: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2806 | lys_inout_free(ctx, (struct lys_node_inout *)node, private_destructor); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2807 | break; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 2808 | case LYS_EXT: |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 2809 | case LYS_UNKNOWN: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2810 | LOGINT(ctx); |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 2811 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2812 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2813 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2814 | /* again common part */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2815 | lys_node_unlink(node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2816 | free(node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2817 | } |
| 2818 | |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 2819 | API struct lys_module * |
| 2820 | lys_implemented_module(const struct lys_module *mod) |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 2821 | { |
| 2822 | struct ly_ctx *ctx; |
| 2823 | int i; |
| 2824 | |
| 2825 | if (!mod || mod->implemented) { |
| 2826 | /* invalid argument or the module itself is implemented */ |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 2827 | return (struct lys_module *)mod; |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 2828 | } |
| 2829 | |
| 2830 | ctx = mod->ctx; |
| 2831 | for (i = 0; i < ctx->models.used; i++) { |
| 2832 | if (!ctx->models.list[i]->implemented) { |
| 2833 | continue; |
| 2834 | } |
| 2835 | |
| 2836 | if (ly_strequal(mod->name, ctx->models.list[i]->name, 1)) { |
| 2837 | /* we have some revision of the module implemented */ |
| 2838 | return ctx->models.list[i]; |
| 2839 | } |
| 2840 | } |
| 2841 | |
| 2842 | /* we have no revision of the module implemented, return the module itself, |
| 2843 | * 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] | 2844 | return (struct lys_module *)mod; |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 2845 | } |
| 2846 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2847 | /* free_int_mods - flag whether to free the internal modules as well */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2848 | static void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2849 | 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] | 2850 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2851 | struct ly_ctx *ctx; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2852 | struct lys_node *next, *iter; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2853 | unsigned int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2854 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2855 | assert(module->ctx); |
| 2856 | ctx = module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2857 | |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2858 | /* just free the import array, imported modules will stay in the context */ |
Radek Krejci | 225376f | 2016-02-16 17:36:22 +0100 | [diff] [blame] | 2859 | for (i = 0; i < module->imp_size; i++) { |
Michal Vasko | a99c163 | 2016-06-06 16:23:52 +0200 | [diff] [blame] | 2860 | lydict_remove(ctx, module->imp[i].prefix); |
Michal Vasko | 8bfe381 | 2016-07-27 13:37:52 +0200 | [diff] [blame] | 2861 | lydict_remove(ctx, module->imp[i].dsc); |
| 2862 | lydict_remove(ctx, module->imp[i].ref); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2863 | 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] | 2864 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2865 | free(module->imp); |
| 2866 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2867 | /* submodules don't have data tree, the data nodes |
| 2868 | * are placed in the main module altogether */ |
| 2869 | if (!module->type) { |
| 2870 | LY_TREE_FOR_SAFE(module->data, next, iter) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2871 | lys_node_free(iter, private_destructor, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2872 | } |
Radek Krejci | 2118196 | 2015-06-30 14:11:00 +0200 | [diff] [blame] | 2873 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2874 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2875 | lydict_remove(ctx, module->dsc); |
| 2876 | lydict_remove(ctx, module->ref); |
| 2877 | lydict_remove(ctx, module->org); |
| 2878 | lydict_remove(ctx, module->contact); |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 2879 | lydict_remove(ctx, module->filepath); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2880 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2881 | /* revisions */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2882 | for (i = 0; i < module->rev_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2883 | 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] | 2884 | lydict_remove(ctx, module->rev[i].dsc); |
| 2885 | lydict_remove(ctx, module->rev[i].ref); |
| 2886 | } |
| 2887 | free(module->rev); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2888 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2889 | /* identities */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2890 | for (i = 0; i < module->ident_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2891 | lys_ident_free(ctx, &module->ident[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2892 | } |
| 2893 | module->ident_size = 0; |
| 2894 | free(module->ident); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2895 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2896 | /* typedefs */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2897 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2898 | lys_tpdf_free(ctx, &module->tpdf[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2899 | } |
| 2900 | free(module->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2901 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2902 | /* extension instances */ |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2903 | lys_extension_instances_free(ctx, module->ext, module->ext_size, private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2904 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2905 | /* augment */ |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2906 | for (i = 0; i < module->augment_size; i++) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2907 | lys_augment_free(ctx, &module->augment[i], private_destructor); |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2908 | } |
| 2909 | free(module->augment); |
| 2910 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2911 | /* features */ |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2912 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2913 | lys_feature_free(ctx, &module->features[i], private_destructor); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2914 | } |
| 2915 | free(module->features); |
| 2916 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2917 | /* deviations */ |
| 2918 | for (i = 0; i < module->deviation_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2919 | lys_deviation_free(module, &module->deviation[i], private_destructor); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2920 | } |
| 2921 | free(module->deviation); |
| 2922 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2923 | /* extensions */ |
| 2924 | for (i = 0; i < module->extensions_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2925 | lys_extension_free(ctx, &module->extensions[i], private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2926 | } |
| 2927 | free(module->extensions); |
| 2928 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2929 | lydict_remove(ctx, module->name); |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 2930 | lydict_remove(ctx, module->prefix); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2931 | } |
| 2932 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2933 | void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2934 | 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] | 2935 | { |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 2936 | int i; |
| 2937 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2938 | if (!submodule) { |
| 2939 | return; |
| 2940 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2941 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2942 | /* common part with struct ly_module */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2943 | module_free_common((struct lys_module *)submodule, private_destructor); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2944 | |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 2945 | /* include */ |
| 2946 | for (i = 0; i < submodule->inc_size; i++) { |
| 2947 | lydict_remove(submodule->ctx, submodule->inc[i].dsc); |
| 2948 | lydict_remove(submodule->ctx, submodule->inc[i].ref); |
| 2949 | lys_extension_instances_free(submodule->ctx, submodule->inc[i].ext, submodule->inc[i].ext_size, private_destructor); |
| 2950 | /* complete submodule free is done only from main module since |
| 2951 | * submodules propagate their includes to the main module */ |
| 2952 | } |
| 2953 | free(submodule->inc); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2954 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2955 | free(submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2956 | } |
| 2957 | |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 2958 | int |
| 2959 | lys_ingrouping(const struct lys_node *node) |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 2960 | { |
| 2961 | const struct lys_node *iter = node; |
| 2962 | assert(node); |
| 2963 | |
| 2964 | for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter)); |
| 2965 | if (!iter) { |
| 2966 | return 0; |
| 2967 | } else { |
| 2968 | return 1; |
| 2969 | } |
| 2970 | } |
| 2971 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2972 | /* |
| 2973 | * final: 0 - do not change config flags; 1 - inherit config flags from the parent; 2 - remove config flags |
| 2974 | */ |
| 2975 | static struct lys_node * |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 2976 | 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] | 2977 | struct unres_schema *unres, int shallow, int finalize) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2978 | { |
Radek Krejci | 7b9f566 | 2016-11-07 16:28:37 +0100 | [diff] [blame] | 2979 | struct lys_node *retval = NULL, *iter, *p; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2980 | struct ly_ctx *ctx = module->ctx; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2981 | int i, j, rc; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2982 | unsigned int size, size1, size2; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 2983 | struct unres_list_uniq *unique_info; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2984 | uint16_t flags; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2985 | |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2986 | struct lys_node_container *cont = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2987 | struct lys_node_container *cont_orig = (struct lys_node_container *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2988 | struct lys_node_choice *choice = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2989 | struct lys_node_choice *choice_orig = (struct lys_node_choice *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2990 | struct lys_node_leaf *leaf = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2991 | struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2992 | struct lys_node_leaflist *llist = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2993 | struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2994 | struct lys_node_list *list = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2995 | struct lys_node_list *list_orig = (struct lys_node_list *)node; |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2996 | struct lys_node_anydata *any = NULL; |
| 2997 | struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2998 | struct lys_node_uses *uses = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2999 | struct lys_node_uses *uses_orig = (struct lys_node_uses *)node; |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 3000 | struct lys_node_rpc_action *rpc = NULL; |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 3001 | struct lys_node_inout *io = NULL; |
Radek Krejci | c43151c | 2017-03-12 07:10:52 +0100 | [diff] [blame] | 3002 | struct lys_node_notif *ntf = NULL; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 3003 | struct lys_node_case *cs = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3004 | struct lys_node_case *cs_orig = (struct lys_node_case *)node; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3005 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3006 | /* we cannot just duplicate memory since the strings are stored in |
| 3007 | * dictionary and we need to update dictionary counters. |
| 3008 | */ |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3009 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3010 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3011 | case LYS_CONTAINER: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3012 | cont = calloc(1, sizeof *cont); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3013 | retval = (struct lys_node *)cont; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3014 | break; |
| 3015 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3016 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3017 | choice = calloc(1, sizeof *choice); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3018 | retval = (struct lys_node *)choice; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3019 | break; |
| 3020 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3021 | case LYS_LEAF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3022 | leaf = calloc(1, sizeof *leaf); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3023 | retval = (struct lys_node *)leaf; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3024 | break; |
| 3025 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3026 | case LYS_LEAFLIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3027 | llist = calloc(1, sizeof *llist); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3028 | retval = (struct lys_node *)llist; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3029 | break; |
| 3030 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3031 | case LYS_LIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3032 | list = calloc(1, sizeof *list); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3033 | retval = (struct lys_node *)list; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3034 | break; |
| 3035 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3036 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3037 | case LYS_ANYDATA: |
| 3038 | any = calloc(1, sizeof *any); |
| 3039 | retval = (struct lys_node *)any; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3040 | break; |
| 3041 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3042 | case LYS_USES: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3043 | uses = calloc(1, sizeof *uses); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3044 | retval = (struct lys_node *)uses; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3045 | break; |
| 3046 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3047 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3048 | cs = calloc(1, sizeof *cs); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3049 | retval = (struct lys_node *)cs; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3050 | break; |
| 3051 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3052 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 3053 | case LYS_ACTION: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 3054 | rpc = calloc(1, sizeof *rpc); |
| 3055 | retval = (struct lys_node *)rpc; |
| 3056 | break; |
| 3057 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3058 | case LYS_INPUT: |
| 3059 | case LYS_OUTPUT: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 3060 | io = calloc(1, sizeof *io); |
| 3061 | retval = (struct lys_node *)io; |
| 3062 | break; |
| 3063 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3064 | case LYS_NOTIF: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 3065 | ntf = calloc(1, sizeof *ntf); |
| 3066 | retval = (struct lys_node *)ntf; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3067 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3068 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3069 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3070 | LOGINT(ctx); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3071 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3072 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3073 | LY_CHECK_ERR_RETURN(!retval, LOGMEM(ctx), NULL); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3074 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3075 | /* |
| 3076 | * duplicate generic part of the structure |
| 3077 | */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3078 | retval->name = lydict_insert(ctx, node->name, 0); |
| 3079 | retval->dsc = lydict_insert(ctx, node->dsc, 0); |
| 3080 | retval->ref = lydict_insert(ctx, node->ref, 0); |
| 3081 | retval->flags = node->flags; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3082 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3083 | retval->module = module; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3084 | retval->nodetype = node->nodetype; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3085 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3086 | retval->prev = retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3087 | |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3088 | retval->ext_size = node->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 3089 | 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] | 3090 | goto error; |
| 3091 | } |
| 3092 | |
Radek Krejci | 0621404 | 2016-11-04 16:25:58 +0100 | [diff] [blame] | 3093 | if (node->iffeature_size) { |
| 3094 | retval->iffeature_size = node->iffeature_size; |
| 3095 | retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3096 | LY_CHECK_ERR_GOTO(!retval->iffeature, LOGMEM(ctx), error); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3097 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3098 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3099 | if (!shallow) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 3100 | for (i = 0; i < node->iffeature_size; ++i) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3101 | resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2); |
| 3102 | if (size1) { |
| 3103 | /* there is something to duplicate */ |
| 3104 | |
| 3105 | /* duplicate compiled expression */ |
| 3106 | size = (size1 / 4) + (size1 % 4) ? 1 : 0; |
| 3107 | retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3108 | LY_CHECK_ERR_GOTO(!retval->iffeature[i].expr, LOGMEM(ctx), error); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3109 | memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr); |
| 3110 | |
| 3111 | /* 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] | 3112 | retval->iffeature[i].features = calloc(size2, sizeof *retval->iffeature[i].features); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3113 | 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] | 3114 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3115 | for (j = 0; (unsigned int)j < size2; j++) { |
| 3116 | rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT, |
| 3117 | &retval->iffeature[i].features[j]); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 3118 | if (rc == EXIT_FAILURE) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3119 | /* feature is resolved in origin, so copy it |
| 3120 | * - duplication is used for instantiating groupings |
| 3121 | * and if-feature inside grouping is supposed to be |
| 3122 | * resolved inside the original grouping, so we want |
| 3123 | * to keep pointers to features from the grouping |
| 3124 | * context */ |
| 3125 | retval->iffeature[i].features[j] = node->iffeature[i].features[j]; |
| 3126 | } else if (rc == -1) { |
| 3127 | goto error; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 3128 | } /* else unres was duplicated */ |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3129 | } |
| 3130 | } |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3131 | |
| 3132 | /* duplicate if-feature's extensions */ |
| 3133 | retval->iffeature[i].ext_size = node->iffeature[i].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 3134 | 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] | 3135 | &retval->iffeature[i], LYEXT_PAR_IFFEATURE, &retval->iffeature[i].ext, shallow, unres)) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3136 | goto error; |
| 3137 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3138 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3139 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3140 | /* inherit config flags */ |
Radek Krejci | 7b9f566 | 2016-11-07 16:28:37 +0100 | [diff] [blame] | 3141 | p = parent; |
| 3142 | do { |
| 3143 | for (iter = p; iter && (iter->nodetype == LYS_USES); iter = iter->parent); |
| 3144 | } while (iter && iter->nodetype == LYS_AUGMENT && (p = lys_parent(iter))); |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3145 | if (iter) { |
| 3146 | flags = iter->flags & LYS_CONFIG_MASK; |
| 3147 | } else { |
| 3148 | /* default */ |
| 3149 | flags = LYS_CONFIG_W; |
| 3150 | } |
| 3151 | |
| 3152 | switch (finalize) { |
| 3153 | case 1: |
| 3154 | /* inherit config flags */ |
| 3155 | if (retval->flags & LYS_CONFIG_SET) { |
| 3156 | /* skip nodes with an explicit config value */ |
| 3157 | if ((flags & LYS_CONFIG_R) && (retval->flags & LYS_CONFIG_W)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3158 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, "true", "config"); |
| 3159 | 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] | 3160 | goto error; |
| 3161 | } |
| 3162 | break; |
| 3163 | } |
| 3164 | |
| 3165 | if (retval->nodetype != LYS_USES) { |
| 3166 | retval->flags = (retval->flags & ~LYS_CONFIG_MASK) | flags; |
| 3167 | } |
Radek Krejci | 2cc2532 | 2017-09-06 16:32:02 +0200 | [diff] [blame] | 3168 | |
| 3169 | /* inherit status */ |
Radek Krejci | e2807ea | 2017-09-07 10:52:21 +0200 | [diff] [blame] | 3170 | if ((parent->flags & LYS_STATUS_MASK) > (retval->flags & LYS_STATUS_MASK)) { |
| 3171 | /* but do it only in case the parent has a stonger status */ |
| 3172 | retval->flags &= ~LYS_STATUS_MASK; |
| 3173 | retval->flags |= (parent->flags & LYS_STATUS_MASK); |
| 3174 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3175 | break; |
| 3176 | case 2: |
| 3177 | /* erase config flags */ |
| 3178 | retval->flags &= ~LYS_CONFIG_MASK; |
| 3179 | retval->flags &= ~LYS_CONFIG_SET; |
| 3180 | break; |
| 3181 | } |
| 3182 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3183 | /* connect it to the parent */ |
Michal Vasko | 8d30dd8 | 2018-09-10 10:06:12 +0200 | [diff] [blame] | 3184 | if (lys_node_addchild(parent, retval->module, retval, 0)) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3185 | goto error; |
| 3186 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 3187 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3188 | /* go recursively */ |
| 3189 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3190 | LY_TREE_FOR(node->child, iter) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3191 | if (iter->nodetype & LYS_GROUPING) { |
| 3192 | /* do not instantiate groupings */ |
| 3193 | continue; |
| 3194 | } |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 3195 | if (!lys_node_dup_recursion(module, retval, iter, unres, 0, finalize)) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3196 | goto error; |
| 3197 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3198 | } |
| 3199 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3200 | |
| 3201 | if (finalize == 1) { |
| 3202 | /* check that configuration lists have keys |
| 3203 | * - we really want to check keys_size in original node, because the keys are |
| 3204 | * not yet resolved here, it is done below in nodetype specific part */ |
| 3205 | if ((retval->nodetype == LYS_LIST) && (retval->flags & LYS_CONFIG_W) |
| 3206 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3207 | LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list"); |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3208 | goto error; |
| 3209 | } |
| 3210 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3211 | } else { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 3212 | memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3213 | } |
| 3214 | |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3215 | /* |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3216 | * duplicate specific part of the structure |
| 3217 | */ |
| 3218 | switch (node->nodetype) { |
| 3219 | case LYS_CONTAINER: |
| 3220 | if (cont_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3221 | cont->when = lys_when_dup(module, cont_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3222 | LY_CHECK_GOTO(!cont->when, error); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3223 | } |
| 3224 | cont->presence = lydict_insert(ctx, cont_orig->presence, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3225 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3226 | if (cont_orig->must) { |
| 3227 | cont->must = lys_restr_dup(module, cont_orig->must, cont_orig->must_size, shallow, unres); |
| 3228 | LY_CHECK_GOTO(!cont->must, error); |
| 3229 | cont->must_size = cont_orig->must_size; |
| 3230 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3231 | |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3232 | /* typedefs are not needed in instantiated grouping, nor the deviation's shallow copy */ |
| 3233 | |
| 3234 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3235 | case LYS_CHOICE: |
| 3236 | if (choice_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3237 | choice->when = lys_when_dup(module, choice_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3238 | LY_CHECK_GOTO(!choice->when, error); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3239 | } |
| 3240 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3241 | if (!shallow) { |
| 3242 | if (choice_orig->dflt) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3243 | rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0, |
| 3244 | LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST, |
| 3245 | (const struct lys_node **)&choice->dflt); |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3246 | if (rc) { |
| 3247 | if (rc == EXIT_FAILURE) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3248 | LOGINT(ctx); |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3249 | } |
| 3250 | goto error; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3251 | } |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3252 | } else { |
| 3253 | /* useless to check return value, we don't know whether |
| 3254 | * there really wasn't any default defined or it just hasn't |
| 3255 | * been resolved, we just hope for the best :) |
| 3256 | */ |
| 3257 | unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3258 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3259 | } else { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3260 | choice->dflt = choice_orig->dflt; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3261 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3262 | break; |
| 3263 | |
| 3264 | case LYS_LEAF: |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 3265 | 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] | 3266 | goto error; |
| 3267 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3268 | leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0); |
| 3269 | |
| 3270 | if (leaf_orig->dflt) { |
| 3271 | leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3272 | } |
| 3273 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3274 | if (leaf_orig->must) { |
| 3275 | leaf->must = lys_restr_dup(module, leaf_orig->must, leaf_orig->must_size, shallow, unres); |
| 3276 | LY_CHECK_GOTO(!leaf->must, error); |
| 3277 | leaf->must_size = leaf_orig->must_size; |
| 3278 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3279 | |
| 3280 | if (leaf_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3281 | leaf->when = lys_when_dup(module, leaf_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3282 | LY_CHECK_GOTO(!leaf->when, error); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3283 | } |
| 3284 | break; |
| 3285 | |
| 3286 | case LYS_LEAFLIST: |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 3287 | 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] | 3288 | goto error; |
| 3289 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3290 | llist->units = lydict_insert(module->ctx, llist_orig->units, 0); |
| 3291 | |
| 3292 | llist->min = llist_orig->min; |
| 3293 | llist->max = llist_orig->max; |
| 3294 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3295 | if (llist_orig->must) { |
| 3296 | llist->must = lys_restr_dup(module, llist_orig->must, llist_orig->must_size, shallow, unres); |
| 3297 | LY_CHECK_GOTO(!llist->must, error); |
| 3298 | llist->must_size = llist_orig->must_size; |
| 3299 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3300 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3301 | if (llist_orig->dflt) { |
| 3302 | llist->dflt = malloc(llist_orig->dflt_size * sizeof *llist->dflt); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3303 | LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), error); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3304 | llist->dflt_size = llist_orig->dflt_size; |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3305 | |
Radek Krejci | c699e42 | 2017-06-02 15:18:58 +0200 | [diff] [blame] | 3306 | for (i = 0; i < llist->dflt_size; i++) { |
| 3307 | llist->dflt[i] = lydict_insert(ctx, llist_orig->dflt[i], 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3308 | } |
| 3309 | } |
| 3310 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3311 | if (llist_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3312 | llist->when = lys_when_dup(module, llist_orig->when, shallow, unres); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3313 | } |
| 3314 | break; |
| 3315 | |
| 3316 | case LYS_LIST: |
| 3317 | list->min = list_orig->min; |
| 3318 | list->max = list_orig->max; |
| 3319 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3320 | if (list_orig->must) { |
| 3321 | list->must = lys_restr_dup(module, list_orig->must, list_orig->must_size, shallow, unres); |
| 3322 | LY_CHECK_GOTO(!list->must, error); |
| 3323 | list->must_size = list_orig->must_size; |
| 3324 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3325 | |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3326 | /* 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] | 3327 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3328 | if (list_orig->keys_size) { |
| 3329 | list->keys = calloc(list_orig->keys_size, sizeof *list->keys); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3330 | LY_CHECK_ERR_GOTO(!list->keys, LOGMEM(ctx), error); |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3331 | list->keys_str = lydict_insert(ctx, list_orig->keys_str, 0); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3332 | list->keys_size = list_orig->keys_size; |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3333 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3334 | if (!shallow) { |
Radek Krejci | 7a1e607 | 2018-08-13 14:59:16 +0200 | [diff] [blame] | 3335 | if (unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) { |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3336 | goto error; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3337 | } |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3338 | } else { |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3339 | 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] | 3340 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3341 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3342 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3343 | if (list_orig->unique) { |
| 3344 | list->unique = malloc(list_orig->unique_size * sizeof *list->unique); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3345 | LY_CHECK_ERR_GOTO(!list->unique, LOGMEM(ctx), error); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3346 | list->unique_size = list_orig->unique_size; |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3347 | |
Radek Krejci | c699e42 | 2017-06-02 15:18:58 +0200 | [diff] [blame] | 3348 | for (i = 0; i < list->unique_size; ++i) { |
| 3349 | 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] | 3350 | LY_CHECK_ERR_GOTO(!list->unique[i].expr, LOGMEM(ctx), error); |
Radek Krejci | c699e42 | 2017-06-02 15:18:58 +0200 | [diff] [blame] | 3351 | list->unique[i].expr_size = list_orig->unique[i].expr_size; |
| 3352 | for (j = 0; j < list->unique[i].expr_size; j++) { |
| 3353 | list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0); |
| 3354 | |
| 3355 | /* if it stays in unres list, duplicate it also there */ |
| 3356 | unique_info = malloc(sizeof *unique_info); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3357 | LY_CHECK_ERR_GOTO(!unique_info, LOGMEM(ctx), error); |
Radek Krejci | c699e42 | 2017-06-02 15:18:58 +0200 | [diff] [blame] | 3358 | unique_info->list = (struct lys_node *)list; |
| 3359 | unique_info->expr = list->unique[i].expr[j]; |
| 3360 | unique_info->trg_type = &list->unique[i].trg_type; |
| 3361 | unres_schema_dup(module, unres, &list_orig, UNRES_LIST_UNIQ, unique_info); |
| 3362 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3363 | } |
| 3364 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 3365 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3366 | if (list_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3367 | list->when = lys_when_dup(module, list_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3368 | LY_CHECK_GOTO(!list->when, error); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 3369 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 3370 | break; |
| 3371 | |
| 3372 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3373 | case LYS_ANYDATA: |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3374 | if (any_orig->must) { |
| 3375 | any->must = lys_restr_dup(module, any_orig->must, any_orig->must_size, shallow, unres); |
| 3376 | LY_CHECK_GOTO(!any->must, error); |
| 3377 | any->must_size = any_orig->must_size; |
| 3378 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3379 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3380 | if (any_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3381 | any->when = lys_when_dup(module, any_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3382 | LY_CHECK_GOTO(!any->when, error); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3383 | } |
| 3384 | break; |
| 3385 | |
| 3386 | case LYS_USES: |
| 3387 | uses->grp = uses_orig->grp; |
| 3388 | |
| 3389 | if (uses_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3390 | uses->when = lys_when_dup(module, uses_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3391 | LY_CHECK_GOTO(!uses->when, error); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3392 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 3393 | /* 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] | 3394 | break; |
| 3395 | |
| 3396 | case LYS_CASE: |
| 3397 | if (cs_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3398 | cs->when = lys_when_dup(module, cs_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3399 | LY_CHECK_GOTO(!cs->when, error); |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 3400 | } |
| 3401 | break; |
| 3402 | |
Radek Krejci | 9693540 | 2016-11-04 16:27:28 +0100 | [diff] [blame] | 3403 | case LYS_ACTION: |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 3404 | case LYS_RPC: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3405 | case LYS_INPUT: |
| 3406 | case LYS_OUTPUT: |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3407 | case LYS_NOTIF: |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3408 | /* 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] | 3409 | break; |
| 3410 | |
| 3411 | default: |
| 3412 | /* LY_NODE_AUGMENT */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3413 | LOGINT(ctx); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3414 | goto error; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3415 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3416 | |
| 3417 | return retval; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3418 | |
| 3419 | error: |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3420 | lys_node_free(retval, NULL, 0); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3421 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3422 | } |
| 3423 | |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 3424 | int |
| 3425 | lys_has_xpath(const struct lys_node *node) |
| 3426 | { |
| 3427 | assert(node); |
| 3428 | |
| 3429 | switch (node->nodetype) { |
| 3430 | case LYS_AUGMENT: |
| 3431 | if (((struct lys_node_augment *)node)->when) { |
| 3432 | return 1; |
| 3433 | } |
| 3434 | break; |
| 3435 | case LYS_CASE: |
| 3436 | if (((struct lys_node_case *)node)->when) { |
| 3437 | return 1; |
| 3438 | } |
| 3439 | break; |
| 3440 | case LYS_CHOICE: |
| 3441 | if (((struct lys_node_choice *)node)->when) { |
| 3442 | return 1; |
| 3443 | } |
| 3444 | break; |
| 3445 | case LYS_ANYDATA: |
| 3446 | if (((struct lys_node_anydata *)node)->when || ((struct lys_node_anydata *)node)->must_size) { |
| 3447 | return 1; |
| 3448 | } |
| 3449 | break; |
| 3450 | case LYS_LEAF: |
| 3451 | if (((struct lys_node_leaf *)node)->when || ((struct lys_node_leaf *)node)->must_size) { |
| 3452 | return 1; |
| 3453 | } |
| 3454 | break; |
| 3455 | case LYS_LEAFLIST: |
| 3456 | if (((struct lys_node_leaflist *)node)->when || ((struct lys_node_leaflist *)node)->must_size) { |
| 3457 | return 1; |
| 3458 | } |
| 3459 | break; |
| 3460 | case LYS_LIST: |
| 3461 | if (((struct lys_node_list *)node)->when || ((struct lys_node_list *)node)->must_size) { |
| 3462 | return 1; |
| 3463 | } |
| 3464 | break; |
| 3465 | case LYS_CONTAINER: |
| 3466 | if (((struct lys_node_container *)node)->when || ((struct lys_node_container *)node)->must_size) { |
| 3467 | return 1; |
| 3468 | } |
| 3469 | break; |
| 3470 | case LYS_INPUT: |
| 3471 | case LYS_OUTPUT: |
| 3472 | if (((struct lys_node_inout *)node)->must_size) { |
| 3473 | return 1; |
| 3474 | } |
| 3475 | break; |
| 3476 | case LYS_NOTIF: |
| 3477 | if (((struct lys_node_notif *)node)->must_size) { |
| 3478 | return 1; |
| 3479 | } |
| 3480 | break; |
| 3481 | case LYS_USES: |
| 3482 | if (((struct lys_node_uses *)node)->when) { |
| 3483 | return 1; |
| 3484 | } |
| 3485 | break; |
| 3486 | default: |
| 3487 | /* does not have XPath */ |
| 3488 | break; |
| 3489 | } |
| 3490 | |
| 3491 | return 0; |
| 3492 | } |
| 3493 | |
Michal Vasko | 568b195 | 2018-01-30 15:53:30 +0100 | [diff] [blame] | 3494 | int |
| 3495 | lys_type_is_local(const struct lys_type *type) |
| 3496 | { |
| 3497 | if (!type->der->module) { |
| 3498 | /* build-in type */ |
| 3499 | return 1; |
| 3500 | } |
| 3501 | /* type->parent can be either a typedef or leaf/leaf-list, but module pointers are compatible */ |
| 3502 | return (lys_main_module(type->der->module) == lys_main_module(((struct lys_tpdf *)type->parent)->module)); |
| 3503 | } |
| 3504 | |
Radek Krejci | 2cc2532 | 2017-09-06 16:32:02 +0200 | [diff] [blame] | 3505 | /* |
| 3506 | * shallow - |
| 3507 | * - do not inherit status from the parent |
| 3508 | */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3509 | struct lys_node * |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 3510 | 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] | 3511 | struct unres_schema *unres, int shallow) |
| 3512 | { |
| 3513 | struct lys_node *p = NULL; |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 3514 | int finalize = 0; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3515 | struct lys_node *result, *iter, *next; |
| 3516 | |
| 3517 | if (!shallow) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3518 | /* 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] | 3519 | for (p = parent; |
| 3520 | p && !(p->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC | LYS_ACTION | LYS_GROUPING)); |
| 3521 | p = lys_parent(p)); |
| 3522 | finalize = p ? ((p->nodetype == LYS_GROUPING) ? 0 : 2) : 1; |
| 3523 | } |
| 3524 | |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 3525 | result = lys_node_dup_recursion(module, parent, node, unres, shallow, finalize); |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3526 | if (finalize) { |
| 3527 | /* check xpath expressions in the instantiated tree */ |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3528 | for (iter = next = result; iter; iter = next) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 3529 | 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] | 3530 | /* invalid xpath */ |
| 3531 | return NULL; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3532 | } |
| 3533 | |
| 3534 | /* select next item */ |
| 3535 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA | LYS_GROUPING)) { |
| 3536 | /* child exception for leafs, leaflists and anyxml without children, ignore groupings */ |
| 3537 | next = NULL; |
| 3538 | } else { |
| 3539 | next = iter->child; |
| 3540 | } |
| 3541 | if (!next) { |
| 3542 | /* no children, try siblings */ |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3543 | if (iter == result) { |
| 3544 | /* we are done, no next element to process */ |
| 3545 | break; |
| 3546 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3547 | next = iter->next; |
| 3548 | } |
| 3549 | while (!next) { |
| 3550 | /* parent is already processed, go to its sibling */ |
| 3551 | iter = lys_parent(iter); |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3552 | if (lys_parent(iter) == lys_parent(result)) { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3553 | /* we are done, no next element to process */ |
| 3554 | break; |
| 3555 | } |
| 3556 | next = iter->next; |
| 3557 | } |
| 3558 | } |
| 3559 | } |
| 3560 | |
| 3561 | return result; |
| 3562 | } |
| 3563 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3564 | /** |
| 3565 | * @brief Switch contents of two same schema nodes. One of the nodes |
| 3566 | * is expected to be ashallow copy of the other. |
| 3567 | * |
| 3568 | * @param[in] node1 Node whose contents will be switched with \p node2. |
| 3569 | * @param[in] node2 Node whose contents will be switched with \p node1. |
| 3570 | */ |
| 3571 | static void |
| 3572 | lys_node_switch(struct lys_node *node1, struct lys_node *node2) |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3573 | { |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3574 | const size_t mem_size = 104; |
| 3575 | uint8_t mem[mem_size]; |
| 3576 | size_t offset, size; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3577 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3578 | assert((node1->module == node2->module) && ly_strequal(node1->name, node2->name, 1) && (node1->nodetype == node2->nodetype)); |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3579 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3580 | /* |
| 3581 | * Initially, the nodes were really switched in the tree which |
| 3582 | * caused problems for some other nodes with pointers (augments, leafrefs, ...) |
| 3583 | * because their pointers were not being updated. Code kept in case there is |
| 3584 | * a use of it in future (it took some debugging to cover all the cases). |
| 3585 | |
| 3586 | * sibling next * |
| 3587 | if (node1->prev->next) { |
| 3588 | node1->prev->next = node2; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3589 | } |
| 3590 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3591 | * sibling prev * |
| 3592 | if (node1->next) { |
| 3593 | node1->next->prev = node2; |
Michal Vasko | 8328da8 | 2016-08-25 09:27:22 +0200 | [diff] [blame] | 3594 | } else { |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3595 | for (child = node1->prev; child->prev->next; child = child->prev); |
| 3596 | child->prev = node2; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3597 | } |
| 3598 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3599 | * next * |
| 3600 | node2->next = node1->next; |
| 3601 | node1->next = NULL; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3602 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3603 | * prev * |
| 3604 | if (node1->prev != node1) { |
| 3605 | node2->prev = node1->prev; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3606 | } |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3607 | node1->prev = node1; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3608 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3609 | * parent child * |
| 3610 | if (node1->parent) { |
| 3611 | if (node1->parent->child == node1) { |
| 3612 | node1->parent->child = node2; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 3613 | } |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3614 | } else if (lys_main_module(node1->module)->data == node1) { |
| 3615 | lys_main_module(node1->module)->data = node2; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3616 | } |
| 3617 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3618 | * parent * |
| 3619 | node2->parent = node1->parent; |
| 3620 | node1->parent = NULL; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 3621 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3622 | * child parent * |
| 3623 | LY_TREE_FOR(node1->child, child) { |
| 3624 | if (child->parent == node1) { |
| 3625 | child->parent = node2; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3626 | } |
| 3627 | } |
| 3628 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3629 | * child * |
| 3630 | node2->child = node1->child; |
| 3631 | node1->child = NULL; |
| 3632 | */ |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3633 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3634 | /* switch common node part */ |
| 3635 | offset = 3 * sizeof(char *); |
| 3636 | size = sizeof(uint16_t) + 6 * sizeof(uint8_t) + sizeof(struct lys_ext_instance **) + sizeof(struct lys_iffeature *); |
| 3637 | memcpy(mem, ((uint8_t *)node1) + offset, size); |
| 3638 | memcpy(((uint8_t *)node1) + offset, ((uint8_t *)node2) + offset, size); |
| 3639 | memcpy(((uint8_t *)node2) + offset, mem, size); |
| 3640 | |
| 3641 | /* switch node-specific data */ |
| 3642 | offset = sizeof(struct lys_node); |
| 3643 | switch (node1->nodetype) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3644 | case LYS_CONTAINER: |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3645 | size = sizeof(struct lys_node_container) - offset; |
| 3646 | break; |
| 3647 | case LYS_CHOICE: |
| 3648 | size = sizeof(struct lys_node_choice) - offset; |
| 3649 | break; |
| 3650 | case LYS_LEAF: |
| 3651 | size = sizeof(struct lys_node_leaf) - offset; |
| 3652 | break; |
| 3653 | case LYS_LEAFLIST: |
| 3654 | size = sizeof(struct lys_node_leaflist) - offset; |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3655 | break; |
| 3656 | case LYS_LIST: |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3657 | size = sizeof(struct lys_node_list) - offset; |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3658 | break; |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3659 | case LYS_ANYDATA: |
| 3660 | case LYS_ANYXML: |
| 3661 | size = sizeof(struct lys_node_anydata) - offset; |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3662 | break; |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3663 | case LYS_CASE: |
| 3664 | size = sizeof(struct lys_node_case) - offset; |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3665 | break; |
| 3666 | case LYS_INPUT: |
| 3667 | case LYS_OUTPUT: |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3668 | size = sizeof(struct lys_node_inout) - offset; |
| 3669 | break; |
| 3670 | case LYS_NOTIF: |
| 3671 | size = sizeof(struct lys_node_notif) - offset; |
| 3672 | break; |
| 3673 | case LYS_RPC: |
| 3674 | case LYS_ACTION: |
| 3675 | size = sizeof(struct lys_node_rpc_action) - offset; |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3676 | break; |
| 3677 | default: |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3678 | assert(0); |
| 3679 | LOGINT(node1->module->ctx); |
| 3680 | return; |
| 3681 | } |
| 3682 | assert(size <= mem_size); |
| 3683 | memcpy(mem, ((uint8_t *)node1) + offset, size); |
| 3684 | memcpy(((uint8_t *)node1) + offset, ((uint8_t *)node2) + offset, size); |
| 3685 | memcpy(((uint8_t *)node2) + offset, mem, size); |
| 3686 | |
Michal Vasko | 114ffd4 | 2018-08-03 14:30:36 +0200 | [diff] [blame] | 3687 | /* typedefs were not copied to the backup node, so always reuse them, |
| 3688 | * in leaves/leaf-lists we must correct the type parent pointer */ |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3689 | switch (node1->nodetype) { |
| 3690 | case LYS_CONTAINER: |
| 3691 | ((struct lys_node_container *)node1)->tpdf_size = ((struct lys_node_container *)node2)->tpdf_size; |
| 3692 | ((struct lys_node_container *)node1)->tpdf = ((struct lys_node_container *)node2)->tpdf; |
| 3693 | ((struct lys_node_container *)node2)->tpdf_size = 0; |
| 3694 | ((struct lys_node_container *)node2)->tpdf = NULL; |
| 3695 | break; |
| 3696 | case LYS_LIST: |
| 3697 | ((struct lys_node_list *)node1)->tpdf_size = ((struct lys_node_list *)node2)->tpdf_size; |
| 3698 | ((struct lys_node_list *)node1)->tpdf = ((struct lys_node_list *)node2)->tpdf; |
| 3699 | ((struct lys_node_list *)node2)->tpdf_size = 0; |
| 3700 | ((struct lys_node_list *)node2)->tpdf = NULL; |
| 3701 | break; |
| 3702 | case LYS_RPC: |
| 3703 | case LYS_ACTION: |
| 3704 | ((struct lys_node_rpc_action *)node1)->tpdf_size = ((struct lys_node_rpc_action *)node2)->tpdf_size; |
| 3705 | ((struct lys_node_rpc_action *)node1)->tpdf = ((struct lys_node_rpc_action *)node2)->tpdf; |
| 3706 | ((struct lys_node_rpc_action *)node2)->tpdf_size = 0; |
| 3707 | ((struct lys_node_rpc_action *)node2)->tpdf = NULL; |
| 3708 | break; |
| 3709 | case LYS_NOTIF: |
| 3710 | ((struct lys_node_notif *)node1)->tpdf_size = ((struct lys_node_notif *)node2)->tpdf_size; |
| 3711 | ((struct lys_node_notif *)node1)->tpdf = ((struct lys_node_notif *)node2)->tpdf; |
| 3712 | ((struct lys_node_notif *)node2)->tpdf_size = 0; |
| 3713 | ((struct lys_node_notif *)node2)->tpdf = NULL; |
| 3714 | break; |
| 3715 | case LYS_INPUT: |
| 3716 | case LYS_OUTPUT: |
| 3717 | ((struct lys_node_inout *)node1)->tpdf_size = ((struct lys_node_inout *)node2)->tpdf_size; |
| 3718 | ((struct lys_node_inout *)node1)->tpdf = ((struct lys_node_inout *)node2)->tpdf; |
| 3719 | ((struct lys_node_inout *)node2)->tpdf_size = 0; |
| 3720 | ((struct lys_node_inout *)node2)->tpdf = NULL; |
| 3721 | break; |
Michal Vasko | 114ffd4 | 2018-08-03 14:30:36 +0200 | [diff] [blame] | 3722 | case LYS_LEAF: |
| 3723 | case LYS_LEAFLIST: |
| 3724 | ((struct lys_node_leaf *)node1)->type.parent = (struct lys_tpdf *)node1; |
| 3725 | ((struct lys_node_leaf *)node2)->type.parent = (struct lys_tpdf *)node2; |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3726 | default: |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3727 | break; |
| 3728 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | void |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 3732 | 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] | 3733 | { |
| 3734 | struct ly_ctx *ctx; |
| 3735 | int i; |
| 3736 | |
| 3737 | if (!module) { |
| 3738 | return; |
| 3739 | } |
| 3740 | |
| 3741 | /* remove schema from the context */ |
| 3742 | ctx = module->ctx; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 3743 | if (remove_from_ctx && ctx->models.used) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3744 | for (i = 0; i < ctx->models.used; i++) { |
| 3745 | if (ctx->models.list[i] == module) { |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 3746 | /* move all the models to not change the order in the list */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3747 | ctx->models.used--; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 3748 | 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] | 3749 | ctx->models.list[ctx->models.used] = NULL; |
| 3750 | /* we are done */ |
| 3751 | break; |
| 3752 | } |
| 3753 | } |
| 3754 | } |
| 3755 | |
| 3756 | /* common part with struct ly_submodule */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 3757 | module_free_common(module, private_destructor); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3758 | |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 3759 | /* include */ |
| 3760 | for (i = 0; i < module->inc_size; i++) { |
| 3761 | lydict_remove(ctx, module->inc[i].dsc); |
| 3762 | lydict_remove(ctx, module->inc[i].ref); |
| 3763 | lys_extension_instances_free(ctx, module->inc[i].ext, module->inc[i].ext_size, private_destructor); |
| 3764 | /* complete submodule free is done only from main module since |
| 3765 | * submodules propagate their includes to the main module */ |
| 3766 | if (free_subs) { |
| 3767 | lys_submodule_free(module->inc[i].submodule, private_destructor); |
| 3768 | } |
| 3769 | } |
| 3770 | free(module->inc); |
| 3771 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3772 | /* specific items to free */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 3773 | lydict_remove(ctx, module->ns); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3774 | |
| 3775 | free(module); |
| 3776 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3777 | |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 3778 | static void |
| 3779 | lys_features_disable_recursive(struct lys_feature *f) |
| 3780 | { |
| 3781 | unsigned int i; |
| 3782 | struct lys_feature *depf; |
| 3783 | |
| 3784 | /* disable the feature */ |
| 3785 | f->flags &= ~LYS_FENABLED; |
| 3786 | |
| 3787 | /* by disabling feature we have to disable also all features that depends on this feature */ |
| 3788 | if (f->depfeatures) { |
| 3789 | for (i = 0; i < f->depfeatures->number; i++) { |
| 3790 | depf = (struct lys_feature *)f->depfeatures->set.g[i]; |
| 3791 | if (depf->flags & LYS_FENABLED) { |
| 3792 | lys_features_disable_recursive(depf); |
| 3793 | } |
| 3794 | } |
| 3795 | } |
| 3796 | } |
| 3797 | |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3798 | /* |
| 3799 | * op: 1 - enable, 0 - disable |
| 3800 | */ |
| 3801 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3802 | 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] | 3803 | { |
| 3804 | int all = 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3805 | int i, j, k; |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3806 | int progress, faili, failj, failk; |
| 3807 | |
| 3808 | uint8_t fsize; |
| 3809 | struct lys_feature *f; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3810 | |
| 3811 | if (!module || !name || !strlen(name)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3812 | LOGARG; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3813 | return EXIT_FAILURE; |
| 3814 | } |
| 3815 | |
| 3816 | if (!strcmp(name, "*")) { |
| 3817 | /* enable all */ |
| 3818 | all = 1; |
| 3819 | } |
| 3820 | |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3821 | progress = failk = 1; |
| 3822 | while (progress && failk) { |
| 3823 | for (i = -1, failk = progress = 0; i < module->inc_size; i++) { |
| 3824 | if (i == -1) { |
| 3825 | fsize = module->features_size; |
| 3826 | f = module->features; |
| 3827 | } else { |
| 3828 | fsize = module->inc[i].submodule->features_size; |
| 3829 | f = module->inc[i].submodule->features; |
| 3830 | } |
| 3831 | |
| 3832 | for (j = 0; j < fsize; j++) { |
| 3833 | if (all || !strcmp(f[j].name, name)) { |
Michal Vasko | 34c3b55 | 2016-11-21 09:07:43 +0100 | [diff] [blame] | 3834 | if ((op && (f[j].flags & LYS_FENABLED)) || (!op && !(f[j].flags & LYS_FENABLED))) { |
| 3835 | if (all) { |
| 3836 | /* skip already set features */ |
| 3837 | continue; |
| 3838 | } else { |
| 3839 | /* feature already set correctly */ |
| 3840 | return EXIT_SUCCESS; |
| 3841 | } |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3842 | } |
| 3843 | |
| 3844 | if (op) { |
| 3845 | /* check referenced features if they are enabled */ |
| 3846 | for (k = 0; k < f[j].iffeature_size; k++) { |
| 3847 | if (!resolve_iffeature(&f[j].iffeature[k])) { |
| 3848 | if (all) { |
| 3849 | faili = i; |
| 3850 | failj = j; |
| 3851 | failk = k + 1; |
| 3852 | break; |
| 3853 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3854 | 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] | 3855 | f[j].name, k + 1); |
| 3856 | return EXIT_FAILURE; |
| 3857 | } |
| 3858 | } |
| 3859 | } |
| 3860 | |
| 3861 | if (k == f[j].iffeature_size) { |
| 3862 | /* the last check passed, do the change */ |
| 3863 | f[j].flags |= LYS_FENABLED; |
| 3864 | progress++; |
| 3865 | } |
| 3866 | } else { |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 3867 | lys_features_disable_recursive(&f[j]); |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3868 | progress++; |
| 3869 | } |
| 3870 | if (!all) { |
| 3871 | /* stop in case changing a single feature */ |
| 3872 | return EXIT_SUCCESS; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3873 | } |
| 3874 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3875 | } |
| 3876 | } |
| 3877 | } |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3878 | if (failk) { |
| 3879 | /* print info about the last failing feature */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3880 | 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] | 3881 | faili == -1 ? module->features[failj].name : module->inc[faili].submodule->features[failj].name, failk); |
| 3882 | return EXIT_FAILURE; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3883 | } |
| 3884 | |
| 3885 | if (all) { |
| 3886 | return EXIT_SUCCESS; |
| 3887 | } else { |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3888 | /* the specified feature not found */ |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3889 | return EXIT_FAILURE; |
| 3890 | } |
| 3891 | } |
| 3892 | |
| 3893 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3894 | lys_features_enable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3895 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3896 | return lys_features_change(module, feature, 1); |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3897 | } |
| 3898 | |
| 3899 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3900 | lys_features_disable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3901 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3902 | return lys_features_change(module, feature, 0); |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3906 | lys_features_state(const struct lys_module *module, const char *feature) |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3907 | { |
| 3908 | int i, j; |
| 3909 | |
| 3910 | if (!module || !feature) { |
| 3911 | return -1; |
| 3912 | } |
| 3913 | |
| 3914 | /* search for the specified feature */ |
| 3915 | /* module itself */ |
| 3916 | for (i = 0; i < module->features_size; i++) { |
| 3917 | if (!strcmp(feature, module->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3918 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3919 | return 1; |
| 3920 | } else { |
| 3921 | return 0; |
| 3922 | } |
| 3923 | } |
| 3924 | } |
| 3925 | |
| 3926 | /* submodules */ |
| 3927 | for (j = 0; j < module->inc_size; j++) { |
| 3928 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
| 3929 | if (!strcmp(feature, module->inc[j].submodule->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3930 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3931 | return 1; |
| 3932 | } else { |
| 3933 | return 0; |
| 3934 | } |
| 3935 | } |
| 3936 | } |
| 3937 | } |
| 3938 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3939 | /* feature definition not found */ |
| 3940 | return -1; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3941 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3942 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3943 | API const char ** |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3944 | lys_features_list(const struct lys_module *module, uint8_t **states) |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3945 | { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3946 | const char **result = NULL; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3947 | int i, j; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3948 | unsigned int count; |
| 3949 | |
| 3950 | if (!module) { |
| 3951 | return NULL; |
| 3952 | } |
| 3953 | |
| 3954 | count = module->features_size; |
| 3955 | for (i = 0; i < module->inc_size; i++) { |
| 3956 | count += module->inc[i].submodule->features_size; |
| 3957 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3958 | result = malloc((count + 1) * sizeof *result); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3959 | LY_CHECK_ERR_RETURN(!result, LOGMEM(module->ctx), NULL); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3960 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3961 | if (states) { |
| 3962 | *states = malloc((count + 1) * sizeof **states); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3963 | LY_CHECK_ERR_RETURN(!(*states), LOGMEM(module->ctx); free(result), NULL); |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3964 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3965 | count = 0; |
| 3966 | |
| 3967 | /* module itself */ |
| 3968 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3969 | result[count] = module->features[i].name; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3970 | if (states) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3971 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3972 | (*states)[count] = 1; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3973 | } else { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3974 | (*states)[count] = 0; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3975 | } |
| 3976 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3977 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3978 | } |
| 3979 | |
| 3980 | /* submodules */ |
| 3981 | for (j = 0; j < module->inc_size; j++) { |
| 3982 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3983 | result[count] = module->inc[j].submodule->features[i].name; |
Radek Krejci | 374b94e | 2015-08-13 09:44:22 +0200 | [diff] [blame] | 3984 | if (states) { |
| 3985 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
| 3986 | (*states)[count] = 1; |
| 3987 | } else { |
| 3988 | (*states)[count] = 0; |
| 3989 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3990 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3991 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3992 | } |
| 3993 | } |
| 3994 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3995 | /* terminating NULL byte */ |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3996 | result[count] = NULL; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3997 | |
| 3998 | return result; |
| 3999 | } |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 4000 | |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 4001 | API struct lys_module * |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 4002 | lys_node_module(const struct lys_node *node) |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4003 | { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 4004 | if (!node) { |
| 4005 | return NULL; |
| 4006 | } |
| 4007 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4008 | return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module; |
| 4009 | } |
| 4010 | |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 4011 | API struct lys_module * |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 4012 | lys_main_module(const struct lys_module *module) |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 4013 | { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 4014 | if (!module) { |
| 4015 | return NULL; |
| 4016 | } |
| 4017 | |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 4018 | return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module); |
| 4019 | } |
| 4020 | |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 4021 | API struct lys_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4022 | lys_parent(const struct lys_node *node) |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 4023 | { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4024 | struct lys_node *parent; |
| 4025 | |
| 4026 | if (!node) { |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 4027 | return NULL; |
| 4028 | } |
| 4029 | |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4030 | if (node->nodetype == LYS_EXT) { |
| 4031 | if (((struct lys_ext_instance_complex*)node)->parent_type != LYEXT_PAR_NODE) { |
| 4032 | return NULL; |
| 4033 | } |
| 4034 | parent = (struct lys_node*)((struct lys_ext_instance_complex*)node)->parent; |
| 4035 | } else if (!node->parent) { |
| 4036 | return NULL; |
| 4037 | } else { |
| 4038 | parent = node->parent; |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 4039 | } |
| 4040 | |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4041 | if (parent->nodetype == LYS_AUGMENT) { |
| 4042 | return ((struct lys_node_augment *)parent)->target; |
| 4043 | } else { |
| 4044 | return parent; |
| 4045 | } |
| 4046 | } |
| 4047 | |
| 4048 | struct lys_node ** |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 4049 | lys_child(const struct lys_node *node, LYS_NODE nodetype) |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4050 | { |
| 4051 | void *pp; |
| 4052 | assert(node); |
| 4053 | |
| 4054 | if (node->nodetype == LYS_EXT) { |
| 4055 | pp = lys_ext_complex_get_substmt(lys_snode2stmt(nodetype), (struct lys_ext_instance_complex*)node, NULL); |
| 4056 | if (!pp) { |
| 4057 | return NULL; |
| 4058 | } |
| 4059 | return (struct lys_node **)pp; |
Michal Vasko | 54d4c20 | 2017-08-09 14:09:18 +0200 | [diff] [blame] | 4060 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Radek Krejci | f7fe2cb | 2017-08-09 10:27:12 +0200 | [diff] [blame] | 4061 | return NULL; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4062 | } else { |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 4063 | return (struct lys_node **)&node->child; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4064 | } |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 4065 | } |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 4066 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 4067 | API void * |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 4068 | lys_set_private(const struct lys_node *node, void *priv) |
| 4069 | { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 4070 | void *prev; |
| 4071 | |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 4072 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4073 | LOGARG; |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 4074 | return NULL; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 4075 | } |
| 4076 | |
Mislav Novakovic | b5529e5 | 2016-02-29 11:42:43 +0100 | [diff] [blame] | 4077 | prev = node->priv; |
| 4078 | ((struct lys_node *)node)->priv = priv; |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 4079 | |
| 4080 | return prev; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 4081 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4082 | |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 4083 | int |
| 4084 | lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref) |
| 4085 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4086 | struct lys_node_leaf *iter; |
| 4087 | struct ly_ctx *ctx = leafref_target->module->ctx; |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 4088 | |
Michal Vasko | 48a573d | 2016-07-01 11:46:02 +0200 | [diff] [blame] | 4089 | if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4090 | LOGINT(ctx); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 4091 | return -1; |
| 4092 | } |
| 4093 | |
Pavol Vican | 9317515 | 2016-08-30 15:34:44 +0200 | [diff] [blame] | 4094 | /* check for config flag */ |
Radek Krejci | c688ca0 | 2017-03-20 12:54:39 +0100 | [diff] [blame] | 4095 | if (((struct lys_node_leaf*)leafref)->type.info.lref.req != -1 && |
| 4096 | (leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4097 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, leafref, |
Radek Krejci | d831dd4 | 2017-03-16 12:59:30 +0100 | [diff] [blame] | 4098 | "The leafref %s is config but refers to a non-config %s.", |
Pavol Vican | 9317515 | 2016-08-30 15:34:44 +0200 | [diff] [blame] | 4099 | strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype)); |
| 4100 | return -1; |
| 4101 | } |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 4102 | /* check for cycles */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4103 | 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] | 4104 | if ((void *)iter == (void *)leafref) { |
| 4105 | /* cycle detected */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4106 | LOGVAL(ctx, LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref); |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 4107 | return -1; |
| 4108 | } |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 4109 | } |
| 4110 | |
| 4111 | /* create fake child - the ly_set structure to hold the list of |
Michal Vasko | 48a573d | 2016-07-01 11:46:02 +0200 | [diff] [blame] | 4112 | * leafrefs referencing the leaf(-list) */ |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 4113 | if (!leafref_target->backlinks) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4114 | leafref_target->backlinks = (void *)ly_set_new(); |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 4115 | if (!leafref_target->backlinks) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4116 | LOGMEM(ctx); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 4117 | return -1; |
| 4118 | } |
| 4119 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 4120 | ly_set_add(leafref_target->backlinks, leafref, 0); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 4121 | |
| 4122 | return 0; |
| 4123 | } |
| 4124 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 4125 | /* not needed currently */ |
| 4126 | #if 0 |
| 4127 | |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4128 | static const char * |
| 4129 | lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len) |
| 4130 | { |
| 4131 | struct lys_module *prev_mod; |
| 4132 | uint32_t str_len, mod_len, buf_idx; |
| 4133 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4134 | 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] | 4135 | LOGINT; |
| 4136 | return NULL; |
| 4137 | } |
| 4138 | |
| 4139 | buf_idx = buf_len - 1; |
| 4140 | buf[buf_idx] = '\0'; |
| 4141 | |
| 4142 | while (node) { |
| 4143 | if (lys_parent(node)) { |
| 4144 | prev_mod = lys_node_module(lys_parent(node)); |
| 4145 | } else { |
| 4146 | prev_mod = NULL; |
| 4147 | } |
| 4148 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4149 | 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] | 4150 | str_len = strlen(node->name); |
| 4151 | |
| 4152 | if (prev_mod != node->module) { |
| 4153 | mod_len = strlen(node->module->name); |
| 4154 | } else { |
| 4155 | mod_len = 0; |
| 4156 | } |
| 4157 | |
| 4158 | if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) { |
| 4159 | LOGINT; |
| 4160 | return NULL; |
| 4161 | } |
| 4162 | |
| 4163 | buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len; |
| 4164 | |
| 4165 | buf[buf_idx] = '/'; |
| 4166 | if (mod_len) { |
| 4167 | memcpy(buf + buf_idx + 1, node->module->name, mod_len); |
| 4168 | buf[buf_idx + 1 + mod_len] = ':'; |
| 4169 | } |
| 4170 | memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len); |
| 4171 | } |
| 4172 | |
| 4173 | node = lys_parent(node); |
| 4174 | } |
| 4175 | |
| 4176 | return buf + buf_idx; |
| 4177 | } |
| 4178 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 4179 | #endif |
| 4180 | |
| 4181 | API struct ly_set * |
Michal Vasko | 9ff79aa | 2017-07-03 14:10:32 +0200 | [diff] [blame] | 4182 | 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] | 4183 | { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4184 | struct lyxp_set set; |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 4185 | struct ly_set *ret_set; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4186 | uint32_t i; |
| 4187 | |
Michal Vasko | 9ff79aa | 2017-07-03 14:10:32 +0200 | [diff] [blame] | 4188 | if (!ctx_node || !expr) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4189 | LOGARG; |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 4190 | return NULL; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4191 | } |
| 4192 | |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 4193 | /* adjust the root */ |
Michal Vasko | 9ff79aa | 2017-07-03 14:10:32 +0200 | [diff] [blame] | 4194 | 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] | 4195 | do { |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 4196 | 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] | 4197 | } 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] | 4198 | } |
| 4199 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4200 | memset(&set, 0, sizeof set); |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4201 | |
| 4202 | if (options & LYXP_MUST) { |
| 4203 | options &= ~LYXP_MUST; |
| 4204 | options |= LYXP_SNODE_MUST; |
| 4205 | } else if (options & LYXP_WHEN) { |
| 4206 | options &= ~LYXP_WHEN; |
| 4207 | options |= LYXP_SNODE_WHEN; |
| 4208 | } else { |
| 4209 | options |= LYXP_SNODE; |
| 4210 | } |
| 4211 | |
Michal Vasko | 9ff79aa | 2017-07-03 14:10:32 +0200 | [diff] [blame] | 4212 | if (lyxp_atomize(expr, ctx_node, ctx_node_type, &set, options, NULL)) { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4213 | free(set.val.snodes); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4214 | 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] | 4215 | return NULL; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4216 | } |
| 4217 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 4218 | ret_set = ly_set_new(); |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4219 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4220 | for (i = 0; i < set.used; ++i) { |
| 4221 | switch (set.val.snodes[i].type) { |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4222 | case LYXP_NODE_ELEM: |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4223 | 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] | 4224 | ly_set_free(ret_set); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4225 | free(set.val.snodes); |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 4226 | return NULL; |
| 4227 | } |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4228 | break; |
| 4229 | default: |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4230 | /* ignore roots, text and attr should not ever appear */ |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4231 | break; |
| 4232 | } |
| 4233 | } |
| 4234 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4235 | free(set.val.snodes); |
| 4236 | return ret_set; |
| 4237 | } |
| 4238 | |
| 4239 | API struct ly_set * |
| 4240 | lys_node_xpath_atomize(const struct lys_node *node, int options) |
| 4241 | { |
| 4242 | const struct lys_node *next, *elem, *parent, *tmp; |
| 4243 | struct lyxp_set set; |
| 4244 | struct ly_set *ret_set; |
| 4245 | uint16_t i; |
| 4246 | |
| 4247 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4248 | LOGARG; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4249 | return NULL; |
| 4250 | } |
| 4251 | |
| 4252 | for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent)); |
| 4253 | if (!parent) { |
| 4254 | /* not in input, output, or notification */ |
| 4255 | return NULL; |
| 4256 | } |
| 4257 | |
| 4258 | ret_set = ly_set_new(); |
Radek Krejci | d9af8d2 | 2016-09-07 18:44:26 +0200 | [diff] [blame] | 4259 | if (!ret_set) { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4260 | return NULL; |
| 4261 | } |
| 4262 | |
| 4263 | LY_TREE_DFS_BEGIN(node, next, elem) { |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 4264 | 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] | 4265 | /* elem has no dependencies from other subtrees and local nodes get discarded */ |
| 4266 | goto next_iter; |
| 4267 | } |
| 4268 | |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 4269 | if (lyxp_node_atomize(elem, &set, 0)) { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4270 | ly_set_free(ret_set); |
| 4271 | free(set.val.snodes); |
| 4272 | return NULL; |
| 4273 | } |
| 4274 | |
| 4275 | for (i = 0; i < set.used; ++i) { |
| 4276 | switch (set.val.snodes[i].type) { |
| 4277 | case LYXP_NODE_ELEM: |
| 4278 | if (options & LYXP_NO_LOCAL) { |
| 4279 | for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp)); |
| 4280 | if (tmp) { |
| 4281 | /* in local subtree, discard */ |
| 4282 | break; |
| 4283 | } |
| 4284 | } |
| 4285 | if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) { |
| 4286 | ly_set_free(ret_set); |
| 4287 | free(set.val.snodes); |
| 4288 | return NULL; |
| 4289 | } |
| 4290 | break; |
| 4291 | default: |
| 4292 | /* ignore roots, text and attr should not ever appear */ |
| 4293 | break; |
| 4294 | } |
| 4295 | } |
| 4296 | |
| 4297 | free(set.val.snodes); |
| 4298 | if (!(options & LYXP_RECURSIVE)) { |
| 4299 | break; |
| 4300 | } |
| 4301 | next_iter: |
| 4302 | LY_TREE_DFS_END(node, next, elem); |
| 4303 | } |
| 4304 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 4305 | return ret_set; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4306 | } |
| 4307 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4308 | /* logs */ |
| 4309 | int |
| 4310 | apply_aug(struct lys_node_augment *augment, struct unres_schema *unres) |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4311 | { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4312 | struct lys_node *child, *parent; |
| 4313 | int clear_config; |
| 4314 | unsigned int u; |
Michal Vasko | d02e30e | 2018-01-22 13:35:48 +0100 | [diff] [blame] | 4315 | uint8_t *v; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4316 | struct lys_ext_instance *ext; |
Adam Weast | 4ca7edd | 2018-06-14 08:59:22 -0500 | [diff] [blame] | 4317 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4318 | assert(augment->target && (augment->flags & LYS_NOTAPPLIED)); |
| 4319 | |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4320 | if (!augment->child) { |
| 4321 | /* nothing to apply */ |
| 4322 | goto success; |
| 4323 | } |
| 4324 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4325 | /* reconnect augmenting data into the target - add them to the target child list */ |
| 4326 | if (augment->target->child) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4327 | child = augment->target->child->prev; |
| 4328 | child->next = augment->child; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4329 | augment->target->child->prev = augment->child->prev; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4330 | augment->child->prev = child; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4331 | } else { |
| 4332 | augment->target->child = augment->child; |
| 4333 | } |
| 4334 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4335 | /* inherit config information from actual parent */ |
| 4336 | for (parent = augment->target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = lys_parent(parent)); |
| 4337 | clear_config = (parent) ? 1 : 0; |
| 4338 | LY_TREE_FOR(augment->child, child) { |
| 4339 | if (inherit_config_flag(child, augment->target->flags & LYS_CONFIG_MASK, clear_config)) { |
| 4340 | return -1; |
| 4341 | } |
| 4342 | } |
| 4343 | |
| 4344 | /* inherit extensions if any */ |
| 4345 | for (u = 0; u < augment->target->ext_size; u++) { |
| 4346 | ext = augment->target->ext[u]; /* shortcut */ |
| 4347 | if (ext && ext->def->plugin && (ext->def->plugin->flags & LYEXT_OPT_INHERIT)) { |
Michal Vasko | d02e30e | 2018-01-22 13:35:48 +0100 | [diff] [blame] | 4348 | v = malloc(sizeof *v); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4349 | LY_CHECK_ERR_RETURN(!v, LOGMEM(augment->module->ctx), -1); |
Michal Vasko | d02e30e | 2018-01-22 13:35:48 +0100 | [diff] [blame] | 4350 | *v = u; |
| 4351 | if (unres_schema_add_node(lys_main_module(augment->module), unres, &augment->target->ext, |
| 4352 | UNRES_EXT_FINALIZE, (struct lys_node *)v) == -1) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4353 | /* something really bad happend since the extension finalization is not actually |
| 4354 | * being resolved while adding into unres, so something more serious with the unres |
| 4355 | * list itself must happened */ |
| 4356 | return -1; |
| 4357 | } |
| 4358 | } |
| 4359 | } |
| 4360 | |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4361 | success: |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4362 | /* remove the flag about not applicability */ |
| 4363 | augment->flags &= ~LYS_NOTAPPLIED; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4364 | return EXIT_SUCCESS; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4365 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4366 | |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4367 | static void |
| 4368 | remove_aug(struct lys_node_augment *augment) |
| 4369 | { |
| 4370 | struct lys_node *last, *elem; |
| 4371 | |
Michal Vasko | f1aa47d | 2017-09-21 12:09:29 +0200 | [diff] [blame] | 4372 | if ((augment->flags & LYS_NOTAPPLIED) || !augment->target) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4373 | /* skip already not applied augment */ |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4374 | return; |
| 4375 | } |
| 4376 | |
| 4377 | elem = augment->child; |
| 4378 | if (elem) { |
| 4379 | LY_TREE_FOR(elem, last) { |
| 4380 | if (!last->next || (last->next->parent != (struct lys_node *)augment)) { |
| 4381 | break; |
| 4382 | } |
| 4383 | } |
| 4384 | /* elem is first augment child, last is the last child */ |
| 4385 | |
| 4386 | /* parent child ptr */ |
| 4387 | if (augment->target->child == elem) { |
| 4388 | augment->target->child = last->next; |
| 4389 | } |
| 4390 | |
| 4391 | /* parent child next ptr */ |
| 4392 | if (elem->prev->next) { |
| 4393 | elem->prev->next = last->next; |
| 4394 | } |
| 4395 | |
| 4396 | /* parent child prev ptr */ |
| 4397 | if (last->next) { |
| 4398 | last->next->prev = elem->prev; |
| 4399 | } else if (augment->target->child) { |
| 4400 | augment->target->child->prev = elem->prev; |
| 4401 | } |
| 4402 | |
| 4403 | /* update augment children themselves */ |
| 4404 | elem->prev = last; |
| 4405 | last->next = NULL; |
| 4406 | } |
| 4407 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4408 | /* augment->target still keeps the resolved target, but for lys_augment_free() |
| 4409 | * we have to keep information that this augment is not applied to free its data */ |
| 4410 | augment->flags |= LYS_NOTAPPLIED; |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4411 | } |
| 4412 | |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4413 | /* |
| 4414 | * @param[in] module - the module where the deviation is defined |
| 4415 | */ |
| 4416 | static void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4417 | 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] | 4418 | { |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4419 | int ret, reapply = 0; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4420 | char *parent_path; |
| 4421 | struct lys_node *target = NULL, *parent; |
Michal Vasko | 5df038e | 2018-08-02 09:41:26 +0200 | [diff] [blame] | 4422 | struct lys_node_inout *inout; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4423 | struct ly_set *set; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4424 | |
| 4425 | if (!dev->deviate) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4426 | return; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4427 | } |
| 4428 | |
| 4429 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 4430 | if (dev->orig_node) { |
| 4431 | /* removing not-supported deviation ... */ |
| 4432 | if (strrchr(dev->target_name, '/') != dev->target_name) { |
| 4433 | /* ... from a parent */ |
| 4434 | |
| 4435 | /* reconnect to its previous position */ |
| 4436 | parent = dev->orig_node->parent; |
Michal Vasko | a1074a5 | 2018-01-03 12:18:53 +0100 | [diff] [blame] | 4437 | if (parent && (parent->nodetype == LYS_AUGMENT)) { |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4438 | dev->orig_node->parent = NULL; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4439 | /* the original node was actually from augment, we have to get know if the augment is |
| 4440 | * applied (its module is enabled and implemented). If yes, the node will be connected |
| 4441 | * to the augment and the linkage with the target will be fixed if needed, otherwise |
| 4442 | * it will be connected only to the augment */ |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4443 | if (!(parent->flags & LYS_NOTAPPLIED)) { |
| 4444 | /* start with removing augment if applied before adding nodes, we have to make sure |
| 4445 | * that everything will be connect correctly */ |
| 4446 | remove_aug((struct lys_node_augment *)parent); |
| 4447 | reapply = 1; |
| 4448 | } |
| 4449 | /* connect the deviated node back into the augment */ |
Michal Vasko | 8d30dd8 | 2018-09-10 10:06:12 +0200 | [diff] [blame] | 4450 | lys_node_addchild(parent, NULL, dev->orig_node, 0); |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4451 | if (reapply) { |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4452 | /* augment is supposed to be applied, so fix pointers in target and the status of the original node */ |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4453 | assert(lys_node_module(parent)->implemented); |
Michal Vasko | 823fbe0 | 2017-12-14 11:01:40 +0100 | [diff] [blame] | 4454 | parent->flags |= LYS_NOTAPPLIED; /* allow apply_aug() */ |
| 4455 | apply_aug((struct lys_node_augment *)parent, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4456 | } |
Michal Vasko | a1074a5 | 2018-01-03 12:18:53 +0100 | [diff] [blame] | 4457 | } else if (parent && (parent->nodetype == LYS_USES)) { |
| 4458 | /* uses child */ |
Michal Vasko | 8d30dd8 | 2018-09-10 10:06:12 +0200 | [diff] [blame] | 4459 | lys_node_addchild(parent, NULL, dev->orig_node, 0); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4460 | } else { |
| 4461 | /* non-augment, non-toplevel */ |
| 4462 | 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] | 4463 | ret = resolve_schema_nodeid(parent_path, NULL, module, &set, 0, 1); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4464 | free(parent_path); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4465 | if (ret == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4466 | LOGINT(module->ctx); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4467 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4468 | return; |
| 4469 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4470 | target = set->set.s[0]; |
| 4471 | ly_set_free(set); |
| 4472 | |
Michal Vasko | 8d30dd8 | 2018-09-10 10:06:12 +0200 | [diff] [blame] | 4473 | lys_node_addchild(target, NULL, dev->orig_node, 0); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4474 | } |
| 4475 | } else { |
| 4476 | /* ... from top-level data */ |
Michal Vasko | 8d30dd8 | 2018-09-10 10:06:12 +0200 | [diff] [blame] | 4477 | lys_node_addchild(NULL, (struct lys_module *)dev->orig_node->module, dev->orig_node, 0); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4478 | } |
| 4479 | |
| 4480 | dev->orig_node = NULL; |
| 4481 | } else { |
| 4482 | /* adding not-supported deviation */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4483 | ret = resolve_schema_nodeid(dev->target_name, NULL, module, &set, 0, 1); |
| 4484 | if (ret == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4485 | LOGINT(module->ctx); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4486 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4487 | return; |
| 4488 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4489 | target = set->set.s[0]; |
| 4490 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4491 | |
| 4492 | /* unlink and store the original node */ |
| 4493 | parent = target->parent; |
| 4494 | lys_node_unlink(target); |
Michal Vasko | 5df038e | 2018-08-02 09:41:26 +0200 | [diff] [blame] | 4495 | if (parent) { |
| 4496 | if (parent->nodetype & (LYS_AUGMENT | LYS_USES)) { |
| 4497 | /* hack for augment, because when the original will be sometime reconnected back, we actually need |
| 4498 | * to reconnect it to both - the augment and its target (which is deduced from the deviations target |
| 4499 | * path), so we need to remember the augment as an addition */ |
| 4500 | /* we also need to remember the parent uses so that we connect it back to it when switching deviation state */ |
| 4501 | target->parent = parent; |
| 4502 | } else if (parent->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 4503 | /* re-create implicit node */ |
| 4504 | inout = calloc(1, sizeof *inout); |
| 4505 | LY_CHECK_ERR_RETURN(!inout, LOGMEM(module->ctx), ); |
| 4506 | |
| 4507 | inout->nodetype = target->nodetype; |
| 4508 | inout->name = lydict_insert(module->ctx, (inout->nodetype == LYS_INPUT) ? "input" : "output", 0); |
| 4509 | inout->module = target->module; |
| 4510 | inout->flags = LYS_IMPLICIT; |
| 4511 | |
| 4512 | /* insert it manually */ |
| 4513 | assert(parent->child && !parent->child->next |
| 4514 | && (parent->child->nodetype == (inout->nodetype == LYS_INPUT ? LYS_OUTPUT : LYS_INPUT))); |
| 4515 | parent->child->next = (struct lys_node *)inout; |
| 4516 | inout->prev = parent->child; |
| 4517 | parent->child->prev = (struct lys_node *)inout; |
| 4518 | inout->parent = parent; |
| 4519 | } |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4520 | } |
| 4521 | dev->orig_node = target; |
| 4522 | } |
| 4523 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4524 | ret = resolve_schema_nodeid(dev->target_name, NULL, module, &set, 0, 1); |
| 4525 | if (ret == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4526 | LOGINT(module->ctx); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4527 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4528 | return; |
| 4529 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4530 | target = set->set.s[0]; |
| 4531 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4532 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 4533 | /* contents are switched */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4534 | lys_node_switch(target, dev->orig_node); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4535 | } |
| 4536 | } |
| 4537 | |
| 4538 | /* temporarily removes or applies deviations, updates module deviation flag accordingly */ |
| 4539 | void |
Michal Vasko | 5a65f21 | 2018-03-19 15:08:08 +0100 | [diff] [blame] | 4540 | lys_enable_deviations(struct lys_module *module) |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4541 | { |
| 4542 | uint32_t i = 0, j; |
| 4543 | const struct lys_module *mod; |
| 4544 | const char *ptr; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4545 | struct unres_schema *unres; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4546 | |
| 4547 | if (module->deviated) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4548 | unres = calloc(1, sizeof *unres); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4549 | LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), ); |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4550 | |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4551 | while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) { |
| 4552 | if (mod == module) { |
| 4553 | continue; |
| 4554 | } |
| 4555 | |
| 4556 | for (j = 0; j < mod->deviation_size; ++j) { |
| 4557 | ptr = strstr(mod->deviation[j].target_name, module->name); |
| 4558 | if (ptr && ptr[strlen(module->name)] == ':') { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4559 | lys_switch_deviation(&mod->deviation[j], mod, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4560 | } |
| 4561 | } |
| 4562 | } |
| 4563 | |
Michal Vasko | 5a65f21 | 2018-03-19 15:08:08 +0100 | [diff] [blame] | 4564 | assert(module->deviated == 2); |
| 4565 | module->deviated = 1; |
| 4566 | |
| 4567 | for (j = 0; j < module->inc_size; j++) { |
| 4568 | if (module->inc[j].submodule->deviated) { |
| 4569 | module->inc[j].submodule->deviated = module->deviated; |
| 4570 | } |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4571 | } |
Michal Vasko | 5a65f21 | 2018-03-19 15:08:08 +0100 | [diff] [blame] | 4572 | |
| 4573 | if (unres->count) { |
| 4574 | resolve_unres_schema(module, unres); |
| 4575 | } |
| 4576 | unres_schema_free(module, &unres, 1); |
| 4577 | } |
| 4578 | } |
| 4579 | |
| 4580 | void |
| 4581 | lys_disable_deviations(struct lys_module *module) |
| 4582 | { |
| 4583 | uint32_t i, j; |
| 4584 | const struct lys_module *mod; |
| 4585 | const char *ptr; |
| 4586 | struct unres_schema *unres; |
| 4587 | |
| 4588 | if (module->deviated) { |
| 4589 | unres = calloc(1, sizeof *unres); |
| 4590 | LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), ); |
| 4591 | |
| 4592 | i = module->ctx->models.used; |
| 4593 | while (i--) { |
| 4594 | mod = module->ctx->models.list[i]; |
| 4595 | |
| 4596 | if (mod == module) { |
| 4597 | continue; |
| 4598 | } |
| 4599 | |
| 4600 | j = mod->deviation_size; |
| 4601 | while (j--) { |
| 4602 | ptr = strstr(mod->deviation[j].target_name, module->name); |
| 4603 | if (ptr && ptr[strlen(module->name)] == ':') { |
| 4604 | lys_switch_deviation(&mod->deviation[j], mod, unres); |
| 4605 | } |
| 4606 | } |
| 4607 | } |
| 4608 | |
| 4609 | assert(module->deviated == 1); |
| 4610 | module->deviated = 2; |
| 4611 | |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4612 | for (j = 0; j < module->inc_size; j++) { |
| 4613 | if (module->inc[j].submodule->deviated) { |
| 4614 | module->inc[j].submodule->deviated = module->deviated; |
| 4615 | } |
| 4616 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4617 | |
| 4618 | if (unres->count) { |
| 4619 | resolve_unres_schema(module, unres); |
| 4620 | } |
| 4621 | unres_schema_free(module, &unres, 1); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4622 | } |
| 4623 | } |
| 4624 | |
| 4625 | static void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4626 | 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] | 4627 | { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4628 | lys_switch_deviation(dev, module, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4629 | |
| 4630 | assert(dev->orig_node); |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4631 | lys_node_module(dev->orig_node)->deviated = 1; /* main module */ |
| 4632 | dev->orig_node->module->deviated = 1; /* possible submodule */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4633 | } |
| 4634 | |
| 4635 | static void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4636 | 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] | 4637 | { |
| 4638 | uint32_t idx = 0, j; |
| 4639 | const struct lys_module *mod; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4640 | struct lys_module *target_mod, *target_submod; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4641 | const char *ptr; |
| 4642 | |
| 4643 | if (dev->orig_node) { |
| 4644 | target_mod = lys_node_module(dev->orig_node); |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4645 | target_submod = dev->orig_node->module; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4646 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4647 | LOGINT(module->ctx); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4648 | return; |
| 4649 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4650 | lys_switch_deviation(dev, module, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4651 | |
| 4652 | /* clear the deviation flag if possible */ |
| 4653 | while ((mod = ly_ctx_get_module_iter(module->ctx, &idx))) { |
| 4654 | if ((mod == module) || (mod == target_mod)) { |
| 4655 | continue; |
| 4656 | } |
| 4657 | |
| 4658 | for (j = 0; j < mod->deviation_size; ++j) { |
| 4659 | ptr = strstr(mod->deviation[j].target_name, target_mod->name); |
| 4660 | if (ptr && (ptr[strlen(target_mod->name)] == ':')) { |
| 4661 | /* some other module deviation targets the inspected module, flag remains */ |
| 4662 | break; |
| 4663 | } |
| 4664 | } |
| 4665 | |
| 4666 | if (j < mod->deviation_size) { |
| 4667 | break; |
| 4668 | } |
| 4669 | } |
| 4670 | |
| 4671 | if (!mod) { |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4672 | target_mod->deviated = 0; /* main module */ |
| 4673 | target_submod->deviated = 0; /* possible submodule */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4674 | } |
| 4675 | } |
| 4676 | |
| 4677 | void |
| 4678 | lys_sub_module_apply_devs_augs(struct lys_module *module) |
| 4679 | { |
| 4680 | uint8_t u, v; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4681 | struct unres_schema *unres; |
| 4682 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4683 | assert(module->implemented); |
| 4684 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4685 | unres = calloc(1, sizeof *unres); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4686 | LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), ); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4687 | |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 4688 | /* apply deviations */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4689 | for (u = 0; u < module->deviation_size; ++u) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4690 | apply_dev(&module->deviation[u], module, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4691 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 4692 | /* apply augments */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4693 | for (u = 0; u < module->augment_size; ++u) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4694 | apply_aug(&module->augment[u], unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4695 | } |
| 4696 | |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 4697 | /* apply deviations and augments defined in submodules */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4698 | for (v = 0; v < module->inc_size; ++v) { |
| 4699 | for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4700 | apply_dev(&module->inc[v].submodule->deviation[u], module, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4701 | } |
| 4702 | |
| 4703 | for (u = 0; u < module->inc[v].submodule->augment_size; ++u) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4704 | apply_aug(&module->inc[v].submodule->augment[u], unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4705 | } |
| 4706 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4707 | |
| 4708 | if (unres->count) { |
| 4709 | resolve_unres_schema(module, unres); |
| 4710 | } |
| 4711 | /* nothing else left to do even if something is not resolved */ |
| 4712 | unres_schema_free(module, &unres, 1); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4713 | } |
| 4714 | |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4715 | void |
| 4716 | lys_sub_module_remove_devs_augs(struct lys_module *module) |
| 4717 | { |
Andrew Langefeld | f976324 | 2018-09-16 22:23:50 -0500 | [diff] [blame] | 4718 | uint8_t u, v, w; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4719 | struct unres_schema *unres; |
| 4720 | |
| 4721 | unres = calloc(1, sizeof *unres); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4722 | LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), ); |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4723 | |
| 4724 | /* remove applied deviations */ |
| 4725 | for (u = 0; u < module->deviation_size; ++u) { |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 4726 | /* the deviation could not be applied because it failed to be applied in the first place*/ |
Michal Vasko | 7427b26 | 2018-05-14 15:23:55 +0200 | [diff] [blame] | 4727 | if (module->deviation[u].orig_node) { |
| 4728 | remove_dev(&module->deviation[u], module, unres); |
| 4729 | } |
Andrew Langefeld | f976324 | 2018-09-16 22:23:50 -0500 | [diff] [blame] | 4730 | |
| 4731 | /* Free the deviation's must array(s). These are shallow copies of the arrays |
| 4732 | on the target node(s), so a deep free is not needed. */ |
| 4733 | for (v = 0; v < module->deviation[u].deviate_size; ++v) { |
| 4734 | if (module->deviation[u].deviate[v].mod == LY_DEVIATE_ADD) { |
| 4735 | free(module->deviation[u].deviate[v].must); |
| 4736 | } |
| 4737 | } |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4738 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4739 | /* remove applied augments */ |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4740 | for (u = 0; u < module->augment_size; ++u) { |
| 4741 | remove_aug(&module->augment[u]); |
| 4742 | } |
| 4743 | |
| 4744 | /* remove deviation and augments defined in submodules */ |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 4745 | for (v = 0; v < module->inc_size && module->inc[v].submodule; ++v) { |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4746 | for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) { |
Michal Vasko | 7427b26 | 2018-05-14 15:23:55 +0200 | [diff] [blame] | 4747 | if (module->inc[v].submodule->deviation[u].orig_node) { |
| 4748 | remove_dev(&module->inc[v].submodule->deviation[u], module, unres); |
| 4749 | } |
Andrew Langefeld | f976324 | 2018-09-16 22:23:50 -0500 | [diff] [blame] | 4750 | |
| 4751 | /* Free the deviation's must array(s). These are shallow copies of the arrays |
| 4752 | on the target node(s), so a deep free is not needed. */ |
| 4753 | for (w = 0; w < module->inc[v].submodule->deviation[u].deviate_size; ++w) { |
| 4754 | if (module->inc[v].submodule->deviation[u].deviate[w].mod == LY_DEVIATE_ADD) { |
| 4755 | free(module->inc[v].submodule->deviation[u].deviate[w].must); |
| 4756 | } |
| 4757 | } |
Radek Krejci | dbc1526 | 2016-06-16 14:58:29 +0200 | [diff] [blame] | 4758 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4759 | |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4760 | for (u = 0; u < module->inc[v].submodule->augment_size; ++u) { |
| 4761 | remove_aug(&module->inc[v].submodule->augment[u]); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4762 | } |
| 4763 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4764 | |
| 4765 | if (unres->count) { |
| 4766 | resolve_unres_schema(module, unres); |
| 4767 | } |
| 4768 | /* nothing else left to do even if something is not resolved */ |
| 4769 | unres_schema_free(module, &unres, 1); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4770 | } |
| 4771 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 4772 | int |
| 4773 | lys_make_implemented_r(struct lys_module *module, struct unres_schema *unres) |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4774 | { |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 4775 | struct ly_ctx *ctx; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4776 | struct lys_node *root, *next, *node; |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4777 | struct lys_module *target_module; |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 4778 | uint16_t i, j, k; |
| 4779 | |
| 4780 | assert(module->implemented); |
| 4781 | ctx = module->ctx; |
| 4782 | |
| 4783 | for (i = 0; i < ctx->models.used; ++i) { |
| 4784 | if (module == ctx->models.list[i]) { |
| 4785 | continue; |
| 4786 | } |
| 4787 | |
| 4788 | if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) { |
| 4789 | LOGERR(ctx, LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name); |
| 4790 | return EXIT_FAILURE; |
| 4791 | } |
| 4792 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4793 | |
| 4794 | for (i = 0; i < module->augment_size; i++) { |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4795 | |
| 4796 | /* make target module implemented if was not */ |
| 4797 | assert(module->augment[i].target); |
| 4798 | target_module = lys_node_module(module->augment[i].target); |
| 4799 | if (!target_module->implemented) { |
| 4800 | target_module->implemented = 1; |
| 4801 | if (unres_schema_add_node(target_module, unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) { |
| 4802 | return -1; |
| 4803 | } |
| 4804 | } |
| 4805 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4806 | /* apply augment */ |
Adam Weast | 368fd58 | 2018-06-14 13:32:35 -0500 | [diff] [blame] | 4807 | if ((module->augment[i].flags & LYS_NOTAPPLIED) && apply_aug(&module->augment[i], unres)) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4808 | return EXIT_FAILURE; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4809 | } |
| 4810 | } |
Adam Weast | 4ca7edd | 2018-06-14 08:59:22 -0500 | [diff] [blame] | 4811 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 4812 | /* identities */ |
| 4813 | for (i = 0; i < module->ident_size; i++) { |
| 4814 | for (j = 0; j < module->ident[i].base_size; j++) { |
| 4815 | resolve_identity_backlink_update(&module->ident[i], module->ident[i].base[j]); |
| 4816 | } |
| 4817 | } |
| 4818 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 4819 | /* process augments in submodules */ |
| 4820 | for (i = 0; i < module->inc_size && module->inc[i].submodule; ++i) { |
| 4821 | module->inc[i].submodule->implemented = 1; |
| 4822 | |
| 4823 | for (j = 0; j < module->inc[i].submodule->augment_size; j++) { |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4824 | |
| 4825 | /* make target module implemented if it was not */ |
Michal Vasko | da267cb | 2018-06-15 11:18:00 +0200 | [diff] [blame] | 4826 | assert(module->inc[i].submodule->augment[j].target); |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4827 | target_module = lys_node_module(module->inc[i].submodule->augment[j].target); |
| 4828 | if (!target_module->implemented) { |
| 4829 | target_module->implemented = 1; |
| 4830 | if (unres_schema_add_node(target_module, unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) { |
| 4831 | return -1; |
| 4832 | } |
| 4833 | } |
| 4834 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 4835 | /* apply augment */ |
Adam Weast | 368fd58 | 2018-06-14 13:32:35 -0500 | [diff] [blame] | 4836 | if ((module->inc[i].submodule->augment[j].flags & LYS_NOTAPPLIED) && apply_aug(&module->inc[i].submodule->augment[j], unres)) { |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 4837 | return EXIT_FAILURE; |
| 4838 | } |
| 4839 | } |
| 4840 | |
| 4841 | /* identities */ |
| 4842 | for (j = 0; j < module->inc[i].submodule->ident_size; j++) { |
| 4843 | for (k = 0; k < module->inc[i].submodule->ident[j].base_size; k++) { |
| 4844 | resolve_identity_backlink_update(&module->inc[i].submodule->ident[j], |
| 4845 | module->inc[i].submodule->ident[j].base[k]); |
| 4846 | } |
| 4847 | } |
| 4848 | } |
| 4849 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4850 | LY_TREE_FOR(module->data, root) { |
| 4851 | /* handle leafrefs and recursively change the implemented flags in the leafref targets */ |
| 4852 | LY_TREE_DFS_BEGIN(root, next, node) { |
| 4853 | if (node->nodetype == LYS_GROUPING) { |
| 4854 | goto nextsibling; |
| 4855 | } |
| 4856 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4857 | if (((struct lys_node_leaf *)node)->type.base == LY_TYPE_LEAFREF) { |
| 4858 | if (unres_schema_add_node(module, unres, &((struct lys_node_leaf *)node)->type, |
| 4859 | UNRES_TYPE_LEAFREF, node) == -1) { |
| 4860 | return EXIT_FAILURE; |
| 4861 | } |
| 4862 | } |
| 4863 | } |
| 4864 | |
| 4865 | /* modified LY_TREE_DFS_END */ |
| 4866 | next = node->child; |
| 4867 | /* child exception for leafs, leaflists and anyxml without children */ |
| 4868 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 4869 | next = NULL; |
| 4870 | } |
| 4871 | if (!next) { |
| 4872 | nextsibling: |
| 4873 | /* no children */ |
| 4874 | if (node == root) { |
| 4875 | /* we are done, root has no children */ |
| 4876 | break; |
| 4877 | } |
| 4878 | /* try siblings */ |
| 4879 | next = node->next; |
| 4880 | } |
| 4881 | while (!next) { |
| 4882 | /* parent is already processed, go to its sibling */ |
| 4883 | node = lys_parent(node); |
| 4884 | /* no siblings, go back through parents */ |
| 4885 | if (lys_parent(node) == lys_parent(root)) { |
| 4886 | /* we are done, no next element to process */ |
| 4887 | break; |
| 4888 | } |
| 4889 | next = node->next; |
| 4890 | } |
| 4891 | } |
| 4892 | } |
| 4893 | |
| 4894 | return EXIT_SUCCESS; |
| 4895 | } |
| 4896 | |
| 4897 | API int |
| 4898 | lys_set_implemented(const struct lys_module *module) |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4899 | { |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4900 | struct unres_schema *unres; |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 4901 | int disabled = 0; |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4902 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4903 | if (!module) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4904 | LOGARG; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4905 | return EXIT_FAILURE; |
| 4906 | } |
| 4907 | |
| 4908 | module = lys_main_module(module); |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4909 | |
| 4910 | if (module->disabled) { |
| 4911 | disabled = 1; |
| 4912 | lys_set_enabled(module); |
| 4913 | } |
| 4914 | |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4915 | if (module->implemented) { |
| 4916 | return EXIT_SUCCESS; |
| 4917 | } |
| 4918 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4919 | unres = calloc(1, sizeof *unres); |
| 4920 | if (!unres) { |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 4921 | LOGMEM(module->ctx); |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4922 | if (disabled) { |
| 4923 | /* set it back disabled */ |
| 4924 | lys_set_disabled(module); |
| 4925 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4926 | return EXIT_FAILURE; |
| 4927 | } |
| 4928 | /* recursively make the module implemented */ |
| 4929 | ((struct lys_module *)module)->implemented = 1; |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 4930 | if (lys_make_implemented_r((struct lys_module *)module, unres)) { |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4931 | goto error; |
| 4932 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 4933 | |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4934 | /* try again resolve augments in other modules possibly augmenting this one, |
| 4935 | * since we have just enabled it |
| 4936 | */ |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4937 | /* resolve rest of unres items */ |
| 4938 | if (unres->count && resolve_unres_schema((struct lys_module *)module, unres)) { |
| 4939 | goto error; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4940 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4941 | unres_schema_free(NULL, &unres, 0); |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4942 | |
Michal Vasko | be136f6 | 2017-09-21 12:08:39 +0200 | [diff] [blame] | 4943 | LOGVRB("Module \"%s%s%s\" now implemented.", module->name, (module->rev_size ? "@" : ""), |
| 4944 | (module->rev_size ? module->rev[0].date : "")); |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4945 | return EXIT_SUCCESS; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4946 | |
| 4947 | error: |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4948 | if (disabled) { |
| 4949 | /* set it back disabled */ |
| 4950 | lys_set_disabled(module); |
| 4951 | } |
| 4952 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4953 | ((struct lys_module *)module)->implemented = 0; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4954 | unres_schema_free((struct lys_module *)module, &unres, 1); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4955 | return EXIT_FAILURE; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4956 | } |
| 4957 | |
| 4958 | void |
| 4959 | lys_submodule_module_data_free(struct lys_submodule *submodule) |
| 4960 | { |
| 4961 | struct lys_node *next, *elem; |
| 4962 | |
| 4963 | /* remove parsed data */ |
| 4964 | LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) { |
| 4965 | if (elem->module == (struct lys_module *)submodule) { |
| 4966 | lys_node_free(elem, NULL, 0); |
| 4967 | } |
| 4968 | } |
| 4969 | } |
Radek Krejci | a1c33bf | 2016-09-07 12:38:49 +0200 | [diff] [blame] | 4970 | |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4971 | API char * |
Michal Vasko | 395b0a0 | 2018-01-22 09:36:20 +0100 | [diff] [blame] | 4972 | lys_path(const struct lys_node *node, int options) |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4973 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4974 | char *buf = NULL; |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4975 | |
| 4976 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4977 | LOGARG; |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4978 | return NULL; |
| 4979 | } |
| 4980 | |
Michal Vasko | 185b527 | 2018-09-13 14:26:12 +0200 | [diff] [blame] | 4981 | if (ly_vlog_build_path(LY_VLOG_LYS, node, &buf, !options, 0)) { |
Michal Vasko | 59631dd | 2017-10-02 11:56:11 +0200 | [diff] [blame] | 4982 | return NULL; |
| 4983 | } |
| 4984 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4985 | return buf; |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4986 | } |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 4987 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4988 | API char * |
| 4989 | lys_data_path(const struct lys_node *node) |
| 4990 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4991 | char *result = NULL, buf[1024]; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 4992 | const char *separator, *name; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4993 | int i, used; |
| 4994 | struct ly_set *set; |
| 4995 | const struct lys_module *prev_mod; |
| 4996 | |
| 4997 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4998 | LOGARG; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4999 | return NULL; |
| 5000 | } |
| 5001 | |
Michal Vasko | d60a1a3 | 2018-05-23 16:31:22 +0200 | [diff] [blame] | 5002 | buf[0] = '\0'; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5003 | set = ly_set_new(); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5004 | LY_CHECK_ERR_GOTO(!set, LOGMEM(node->module->ctx), cleanup); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5005 | |
| 5006 | while (node) { |
| 5007 | ly_set_add(set, (void *)node, 0); |
| 5008 | do { |
| 5009 | node = lys_parent(node); |
| 5010 | } while (node && (node->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))); |
| 5011 | } |
| 5012 | |
| 5013 | prev_mod = NULL; |
| 5014 | used = 0; |
| 5015 | for (i = set->number - 1; i > -1; --i) { |
| 5016 | node = set->set.s[i]; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 5017 | if (node->nodetype == LYS_EXT) { |
| 5018 | if (strcmp(((struct lys_ext_instance *)node)->def->name, "yang-data")) { |
| 5019 | continue; |
| 5020 | } |
| 5021 | name = ((struct lys_ext_instance *)node)->arg_value; |
| 5022 | separator = ":#"; |
| 5023 | } else { |
| 5024 | name = node->name; |
| 5025 | separator = ":"; |
| 5026 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5027 | 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] | 5028 | (lys_node_module(node) == prev_mod ? "" : separator), name); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5029 | prev_mod = lys_node_module(node); |
| 5030 | } |
| 5031 | |
| 5032 | result = strdup(buf); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5033 | LY_CHECK_ERR_GOTO(!result, LOGMEM(node->module->ctx), cleanup); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5034 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5035 | cleanup: |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5036 | ly_set_free(set); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5037 | return result; |
| 5038 | } |
| 5039 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 5040 | struct lys_node_augment * |
| 5041 | lys_getnext_target_aug(struct lys_node_augment *last, const struct lys_module *mod, const struct lys_node *aug_target) |
| 5042 | { |
| 5043 | int i, j, last_found; |
| 5044 | |
Michal Vasko | b690687 | 2018-03-12 11:35:09 +0100 | [diff] [blame] | 5045 | assert(mod && aug_target); |
| 5046 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 5047 | if (!last) { |
| 5048 | last_found = 1; |
| 5049 | } else { |
| 5050 | last_found = 0; |
| 5051 | } |
| 5052 | |
| 5053 | /* search module augments */ |
| 5054 | for (i = 0; i < mod->augment_size; ++i) { |
| 5055 | if (!mod->augment[i].target) { |
| 5056 | /* still unresolved, skip */ |
| 5057 | continue; |
| 5058 | } |
| 5059 | |
| 5060 | if (mod->augment[i].target == aug_target) { |
| 5061 | if (last_found) { |
| 5062 | /* next match after last */ |
| 5063 | return &mod->augment[i]; |
| 5064 | } |
| 5065 | |
| 5066 | if (&mod->augment[i] == last) { |
| 5067 | last_found = 1; |
| 5068 | } |
| 5069 | } |
| 5070 | } |
| 5071 | |
| 5072 | /* search submodule augments */ |
| 5073 | for (i = 0; i < mod->inc_size; ++i) { |
| 5074 | for (j = 0; j < mod->inc[i].submodule->augment_size; ++j) { |
| 5075 | if (!mod->inc[i].submodule->augment[j].target) { |
| 5076 | continue; |
| 5077 | } |
| 5078 | |
| 5079 | if (mod->inc[i].submodule->augment[j].target == aug_target) { |
| 5080 | if (last_found) { |
| 5081 | /* next match after last */ |
| 5082 | return &mod->inc[i].submodule->augment[j]; |
| 5083 | } |
| 5084 | |
| 5085 | if (&mod->inc[i].submodule->augment[j] == last) { |
| 5086 | last_found = 1; |
| 5087 | } |
| 5088 | } |
| 5089 | } |
| 5090 | } |
| 5091 | |
| 5092 | return NULL; |
| 5093 | } |
| 5094 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5095 | API struct ly_set * |
| 5096 | lys_find_path(const struct lys_module *cur_module, const struct lys_node *cur_node, const char *path) |
| 5097 | { |
| 5098 | struct ly_set *ret; |
| 5099 | int rc; |
| 5100 | |
| 5101 | if ((!cur_module && !cur_node) || !path) { |
| 5102 | return NULL; |
| 5103 | } |
| 5104 | |
| 5105 | rc = resolve_schema_nodeid(path, cur_node, cur_module, &ret, 1, 1); |
| 5106 | if (rc == -1) { |
| 5107 | return NULL; |
| 5108 | } |
| 5109 | |
| 5110 | return ret; |
| 5111 | } |
| 5112 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5113 | static void |
| 5114 | lys_extcomplex_free_str(struct ly_ctx *ctx, struct lys_ext_instance_complex *ext, LY_STMT stmt) |
| 5115 | { |
| 5116 | struct lyext_substmt *info; |
Radek Krejci | b71243e | 2017-02-08 16:20:08 +0100 | [diff] [blame] | 5117 | const char **str, ***a; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5118 | int c; |
| 5119 | |
| 5120 | str = lys_ext_complex_get_substmt(stmt, ext, &info); |
| 5121 | if (!str || !(*str)) { |
| 5122 | return; |
| 5123 | } |
| 5124 | if (info->cardinality >= LY_STMT_CARD_SOME) { |
| 5125 | /* we have array */ |
Radek Krejci | b71243e | 2017-02-08 16:20:08 +0100 | [diff] [blame] | 5126 | a = (const char ***)str; |
Radek Krejci | 56c8041 | 2017-02-09 10:44:16 +0100 | [diff] [blame] | 5127 | for (str = (*(const char ***)str), c = 0; str[c]; c++) { |
| 5128 | lydict_remove(ctx, str[c]); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5129 | } |
Radek Krejci | 56c8041 | 2017-02-09 10:44:16 +0100 | [diff] [blame] | 5130 | free(a[0]); |
| 5131 | if (stmt == LY_STMT_BELONGSTO) { |
| 5132 | for (str = a[1], c = 0; str[c]; c++) { |
| 5133 | lydict_remove(ctx, str[c]); |
| 5134 | } |
| 5135 | free(a[1]); |
PavolVican | 99c7072 | 2017-02-18 17:25:52 +0100 | [diff] [blame] | 5136 | } else if (stmt == LY_STMT_ARGUMENT) { |
| 5137 | free(a[1]); |
Radek Krejci | 56c8041 | 2017-02-09 10:44:16 +0100 | [diff] [blame] | 5138 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5139 | } else { |
Radek Krejci | 56c8041 | 2017-02-09 10:44:16 +0100 | [diff] [blame] | 5140 | lydict_remove(ctx, str[0]); |
| 5141 | if (stmt == LY_STMT_BELONGSTO) { |
| 5142 | lydict_remove(ctx, str[1]); |
| 5143 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5144 | } |
| 5145 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 5146 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5147 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 5148 | lys_extension_instances_free(struct ly_ctx *ctx, struct lys_ext_instance **e, unsigned int size, |
| 5149 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5150 | { |
Radek Krejci | f8d05c2 | 2017-02-10 15:33:35 +0100 | [diff] [blame] | 5151 | unsigned int i, j, k; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5152 | struct lyext_substmt *substmt; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5153 | void **pp, **start; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 5154 | struct lys_node *siter, *snext; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5155 | |
| 5156 | #define EXTCOMPLEX_FREE_STRUCT(STMT, TYPE, FUNC, FREE, ARGS...) \ |
Radek Krejci | b84686f | 2017-02-09 16:04:55 +0100 | [diff] [blame] | 5157 | 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] | 5158 | if (!pp || !(*pp)) { break; } \ |
Radek Krejci | b84686f | 2017-02-09 16:04:55 +0100 | [diff] [blame] | 5159 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */ \ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5160 | for (start = pp = *pp; *pp; pp++) { \ |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 5161 | FUNC(ctx, (TYPE *)(*pp), ##ARGS, private_destructor); \ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5162 | if (FREE) { free(*pp); } \ |
| 5163 | } \ |
| 5164 | free(start); \ |
| 5165 | } else { /* single item */ \ |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 5166 | FUNC(ctx, (TYPE *)(*pp), ##ARGS, private_destructor); \ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5167 | if (FREE) { free(*pp); } \ |
| 5168 | } |
| 5169 | |
PavolVican | 9d61d40 | 2018-02-05 15:52:48 +0100 | [diff] [blame] | 5170 | if (!size || !e) { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5171 | return; |
| 5172 | } |
| 5173 | |
| 5174 | for (i = 0; i < size; i++) { |
| 5175 | if (!e[i]) { |
| 5176 | continue; |
| 5177 | } |
| 5178 | |
| 5179 | if (e[i]->flags & (LYEXT_OPT_INHERIT)) { |
| 5180 | /* no free, this is just a shadow copy of the original extension instance */ |
| 5181 | } else { |
| 5182 | if (e[i]->flags & (LYEXT_OPT_YANG)) { |
| 5183 | free(e[i]->def); /* remove name of instance extension */ |
PavolVican | 19dc615 | 2017-02-06 12:04:15 +0100 | [diff] [blame] | 5184 | e[i]->def = NULL; |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 5185 | 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] | 5186 | } |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 5187 | /* remove private object */ |
| 5188 | if (e[i]->priv && private_destructor) { |
| 5189 | private_destructor((struct lys_node*)e[i], e[i]->priv); |
| 5190 | } |
| 5191 | 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] | 5192 | lydict_remove(ctx, e[i]->arg_value); |
| 5193 | } |
| 5194 | |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 5195 | if (e[i]->def && e[i]->def->plugin && e[i]->def->plugin->type == LYEXT_COMPLEX |
| 5196 | && ((e[i]->flags & LYEXT_OPT_CONTENT) == 0)) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 5197 | substmt = ((struct lys_ext_instance_complex *)e[i])->substmt; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5198 | for (j = 0; substmt[j].stmt; j++) { |
| 5199 | switch(substmt[j].stmt) { |
| 5200 | case LY_STMT_DESCRIPTION: |
| 5201 | case LY_STMT_REFERENCE: |
| 5202 | case LY_STMT_UNITS: |
Radek Krejci | b71243e | 2017-02-08 16:20:08 +0100 | [diff] [blame] | 5203 | case LY_STMT_ARGUMENT: |
| 5204 | case LY_STMT_DEFAULT: |
| 5205 | case LY_STMT_ERRTAG: |
| 5206 | case LY_STMT_ERRMSG: |
| 5207 | case LY_STMT_PREFIX: |
| 5208 | case LY_STMT_NAMESPACE: |
| 5209 | case LY_STMT_PRESENCE: |
| 5210 | case LY_STMT_REVISIONDATE: |
| 5211 | case LY_STMT_KEY: |
| 5212 | case LY_STMT_BASE: |
| 5213 | case LY_STMT_BELONGSTO: |
| 5214 | case LY_STMT_CONTACT: |
| 5215 | case LY_STMT_ORGANIZATION: |
| 5216 | case LY_STMT_PATH: |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5217 | lys_extcomplex_free_str(ctx, (struct lys_ext_instance_complex *)e[i], substmt[j].stmt); |
| 5218 | break; |
| 5219 | case LY_STMT_TYPE: |
| 5220 | EXTCOMPLEX_FREE_STRUCT(LY_STMT_TYPE, struct lys_type, lys_type_free, 1); |
| 5221 | break; |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 5222 | case LY_STMT_TYPEDEF: |
| 5223 | EXTCOMPLEX_FREE_STRUCT(LY_STMT_TYPEDEF, struct lys_tpdf, lys_tpdf_free, 1); |
| 5224 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5225 | case LY_STMT_IFFEATURE: |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 5226 | 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] | 5227 | break; |
Radek Krejci | 5496fae | 2017-02-10 13:26:48 +0100 | [diff] [blame] | 5228 | case LY_STMT_MAX: |
| 5229 | case LY_STMT_MIN: |
| 5230 | case LY_STMT_POSITION: |
PavolVican | 2ed9f4e | 2017-02-16 00:08:45 +0100 | [diff] [blame] | 5231 | case LY_STMT_VALUE: |
Radek Krejci | f8d05c2 | 2017-02-10 15:33:35 +0100 | [diff] [blame] | 5232 | 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] | 5233 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME && *pp) { |
Radek Krejci | f8d05c2 | 2017-02-10 15:33:35 +0100 | [diff] [blame] | 5234 | for(k = 0; ((uint32_t**)(*pp))[k]; k++) { |
| 5235 | free(((uint32_t**)(*pp))[k]); |
| 5236 | } |
| 5237 | } |
| 5238 | free(*pp); |
| 5239 | break; |
| 5240 | case LY_STMT_DIGITS: |
Radek Krejci | b84686f | 2017-02-09 16:04:55 +0100 | [diff] [blame] | 5241 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { |
| 5242 | /* free the array */ |
| 5243 | pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset]; |
| 5244 | free(*pp); |
| 5245 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5246 | break; |
Radek Krejci | 37f9ba3 | 2017-02-10 16:50:35 +0100 | [diff] [blame] | 5247 | case LY_STMT_MODULE: |
| 5248 | /* modules are part of the context, so they will be freed there */ |
| 5249 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { |
| 5250 | /* free the array */ |
| 5251 | pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset]; |
| 5252 | free(*pp); |
| 5253 | } |
| 5254 | break; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 5255 | case LY_STMT_ACTION: |
Radek Krejci | b31762b | 2017-02-15 10:48:42 +0100 | [diff] [blame] | 5256 | case LY_STMT_ANYDATA: |
| 5257 | case LY_STMT_ANYXML: |
| 5258 | case LY_STMT_CASE: |
| 5259 | case LY_STMT_CHOICE: |
| 5260 | case LY_STMT_CONTAINER: |
| 5261 | case LY_STMT_GROUPING: |
| 5262 | case LY_STMT_INPUT: |
| 5263 | case LY_STMT_LEAF: |
| 5264 | case LY_STMT_LEAFLIST: |
| 5265 | case LY_STMT_LIST: |
| 5266 | case LY_STMT_NOTIFICATION: |
| 5267 | case LY_STMT_OUTPUT: |
| 5268 | case LY_STMT_RPC: |
| 5269 | case LY_STMT_USES: |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 5270 | pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset]; |
| 5271 | LY_TREE_FOR_SAFE((struct lys_node *)(*pp), snext, siter) { |
| 5272 | lys_node_free(siter, NULL, 0); |
| 5273 | } |
Radek Krejci | b31762b | 2017-02-15 10:48:42 +0100 | [diff] [blame] | 5274 | *pp = NULL; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 5275 | break; |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 5276 | case LY_STMT_UNIQUE: |
| 5277 | pp = lys_ext_complex_get_substmt(LY_STMT_UNIQUE, (struct lys_ext_instance_complex *)e[i], NULL); |
| 5278 | if (!pp || !(*pp)) { |
| 5279 | break; |
| 5280 | } |
| 5281 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */ |
| 5282 | for (start = pp = *pp; *pp; pp++) { |
| 5283 | for (k = 0; k < (*(struct lys_unique**)pp)->expr_size; k++) { |
| 5284 | lydict_remove(ctx, (*(struct lys_unique**)pp)->expr[k]); |
| 5285 | } |
| 5286 | free((*(struct lys_unique**)pp)->expr); |
| 5287 | free(*pp); |
| 5288 | } |
| 5289 | free(start); |
| 5290 | } else { /* single item */ |
| 5291 | for (k = 0; k < (*(struct lys_unique**)pp)->expr_size; k++) { |
| 5292 | lydict_remove(ctx, (*(struct lys_unique**)pp)->expr[k]); |
| 5293 | } |
| 5294 | free((*(struct lys_unique**)pp)->expr); |
| 5295 | free(*pp); |
| 5296 | } |
| 5297 | break; |
Radek Krejci | aa9c520 | 2017-02-15 16:10:14 +0100 | [diff] [blame] | 5298 | case LY_STMT_LENGTH: |
| 5299 | case LY_STMT_MUST: |
| 5300 | case LY_STMT_PATTERN: |
| 5301 | case LY_STMT_RANGE: |
| 5302 | EXTCOMPLEX_FREE_STRUCT(substmt[j].stmt, struct lys_restr, lys_restr_free, 1); |
| 5303 | break; |
Radek Krejci | c5cc530 | 2017-02-16 10:07:46 +0100 | [diff] [blame] | 5304 | case LY_STMT_WHEN: |
| 5305 | EXTCOMPLEX_FREE_STRUCT(LY_STMT_WHEN, struct lys_when, lys_when_free, 0); |
| 5306 | break; |
Radek Krejci | 7417a08 | 2017-02-16 11:07:59 +0100 | [diff] [blame] | 5307 | case LY_STMT_REVISION: |
| 5308 | pp = lys_ext_complex_get_substmt(LY_STMT_REVISION, (struct lys_ext_instance_complex *)e[i], NULL); |
| 5309 | if (!pp || !(*pp)) { |
| 5310 | break; |
| 5311 | } |
| 5312 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */ |
| 5313 | for (start = pp = *pp; *pp; pp++) { |
| 5314 | lydict_remove(ctx, (*(struct lys_revision**)pp)->dsc); |
| 5315 | lydict_remove(ctx, (*(struct lys_revision**)pp)->ref); |
| 5316 | lys_extension_instances_free(ctx, (*(struct lys_revision**)pp)->ext, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 5317 | (*(struct lys_revision**)pp)->ext_size, private_destructor); |
Radek Krejci | 7417a08 | 2017-02-16 11:07:59 +0100 | [diff] [blame] | 5318 | free(*pp); |
| 5319 | } |
| 5320 | free(start); |
| 5321 | } else { /* single item */ |
| 5322 | lydict_remove(ctx, (*(struct lys_revision**)pp)->dsc); |
| 5323 | lydict_remove(ctx, (*(struct lys_revision**)pp)->ref); |
| 5324 | lys_extension_instances_free(ctx, (*(struct lys_revision**)pp)->ext, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 5325 | (*(struct lys_revision**)pp)->ext_size, private_destructor); |
Radek Krejci | 7417a08 | 2017-02-16 11:07:59 +0100 | [diff] [blame] | 5326 | free(*pp); |
| 5327 | } |
| 5328 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5329 | default: |
Radek Krejci | b84686f | 2017-02-09 16:04:55 +0100 | [diff] [blame] | 5330 | /* nothing to free */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5331 | break; |
| 5332 | } |
| 5333 | } |
| 5334 | } |
| 5335 | |
| 5336 | free(e[i]); |
| 5337 | } |
| 5338 | free(e); |
| 5339 | |
| 5340 | #undef EXTCOMPLEX_FREE_STRUCT |
| 5341 | } |