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 | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 126 | int |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 127 | lys_get_sibling(const struct lys_node *siblings, const char *mod_name, int mod_name_len, const char *name, |
| 128 | int nam_len, LYS_NODE type, const struct lys_node **ret) |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 129 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 130 | const struct lys_node *node, *parent = NULL; |
| 131 | const struct lys_module *mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 132 | const char *node_mod_name; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 133 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 134 | assert(siblings && mod_name && name); |
Michal Vasko | 165dc4a | 2015-10-23 09:44:27 +0200 | [diff] [blame] | 135 | assert(!(type & (LYS_USES | LYS_GROUPING))); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 136 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 137 | /* fill the lengths in case the caller is so indifferent */ |
| 138 | if (!mod_name_len) { |
| 139 | mod_name_len = strlen(mod_name); |
| 140 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 141 | if (!nam_len) { |
| 142 | nam_len = strlen(name); |
| 143 | } |
| 144 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 145 | while (siblings && (siblings->nodetype == LYS_USES)) { |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 146 | siblings = siblings->child; |
| 147 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 148 | if (!siblings) { |
| 149 | /* unresolved uses */ |
| 150 | return EXIT_FAILURE; |
| 151 | } |
| 152 | |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 153 | if (siblings->nodetype == LYS_GROUPING) { |
| 154 | for (node = siblings; (node->nodetype == LYS_GROUPING) && (node->prev != siblings); node = node->prev); |
| 155 | if (node->nodetype == LYS_GROUPING) { |
| 156 | /* we went through all the siblings, only groupings there - no valid sibling */ |
| 157 | return EXIT_FAILURE; |
| 158 | } |
| 159 | /* update siblings to be valid */ |
| 160 | siblings = node; |
| 161 | } |
| 162 | |
Michal Vasko | 4cf7dba | 2016-10-14 09:42:01 +0200 | [diff] [blame] | 163 | /* set parent correctly */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 164 | parent = lys_parent(siblings); |
Michal Vasko | 4cf7dba | 2016-10-14 09:42:01 +0200 | [diff] [blame] | 165 | |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 166 | /* go up all uses */ |
| 167 | while (parent && (parent->nodetype == LYS_USES)) { |
| 168 | parent = lys_parent(parent); |
Michal Vasko | 4cf7dba | 2016-10-14 09:42:01 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 171 | if (!parent) { |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 172 | /* handle situation when there is a top-level uses referencing a foreign grouping */ |
| 173 | for (node = siblings; lys_parent(node) && (node->nodetype == LYS_USES); node = lys_parent(node)); |
| 174 | mod = lys_node_module(node); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 177 | /* try to find the node */ |
| 178 | node = NULL; |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 179 | 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] | 180 | if (!type || (node->nodetype & type)) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 181 | /* module name comparison */ |
| 182 | node_mod_name = lys_node_module(node)->name; |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 183 | 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] | 184 | continue; |
| 185 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 186 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 187 | /* direct name check */ |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 188 | 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] | 189 | if (ret) { |
| 190 | *ret = node; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 191 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 192 | return EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 193 | } |
| 194 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | return EXIT_FAILURE; |
| 198 | } |
| 199 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 200 | int |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 201 | lys_getnext_data(const struct lys_module *mod, const struct lys_node *parent, const char *name, int nam_len, |
| 202 | LYS_NODE type, const struct lys_node **ret) |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 203 | { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 204 | const struct lys_node *node; |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 205 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 206 | assert((mod || parent) && name); |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 207 | assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))); |
| 208 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 209 | if (!mod) { |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 210 | mod = lys_node_module(parent); |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 211 | } |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 212 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 213 | /* try to find the node */ |
| 214 | node = NULL; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 215 | while ((node = lys_getnext(node, parent, mod, 0))) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 216 | if (!type || (node->nodetype & type)) { |
| 217 | /* module check */ |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 218 | if (lys_node_module(node) != lys_main_module(mod)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 219 | continue; |
| 220 | } |
| 221 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 222 | /* direct name check */ |
Michal Vasko | 0f99d3e | 2017-01-10 10:50:40 +0100 | [diff] [blame] | 223 | if (!strncmp(node->name, name, nam_len) && !node->name[nam_len]) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 224 | if (ret) { |
| 225 | *ret = node; |
| 226 | } |
| 227 | return EXIT_SUCCESS; |
| 228 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 229 | } |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | return EXIT_FAILURE; |
| 233 | } |
| 234 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 235 | API const struct lys_node * |
| 236 | 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] | 237 | { |
Michal Vasko | 5a9c24b | 2017-03-13 09:25:58 +0100 | [diff] [blame] | 238 | const struct lys_node *next, *aug_parent; |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 239 | struct lys_node **snode; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 240 | |
Michal Vasko | 75702cd | 2018-02-12 11:27:09 +0100 | [diff] [blame] | 241 | 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] | 242 | LOGARG; |
Michal Vasko | 24476fa | 2017-03-08 12:33:48 +0100 | [diff] [blame] | 243 | return NULL; |
| 244 | } |
| 245 | |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 246 | if (!last) { |
| 247 | /* first call */ |
| 248 | |
| 249 | /* get know where to start */ |
| 250 | if (parent) { |
| 251 | /* schema subtree */ |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 252 | snode = lys_child(parent, LYS_UNKNOWN); |
Michal Vasko | 5a9c24b | 2017-03-13 09:25:58 +0100 | [diff] [blame] | 253 | /* do not return anything if the augment does not have any children */ |
Radek Krejci | bb08db3 | 2017-07-03 11:29:17 +0200 | [diff] [blame] | 254 | if (!snode || !(*snode) || ((parent->nodetype == LYS_AUGMENT) && ((*snode)->parent != parent))) { |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 255 | return NULL; |
| 256 | } |
| 257 | next = last = *snode; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 258 | } else { |
| 259 | /* top level data */ |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 260 | if (!(options & LYS_GETNEXT_NOSTATECHECK) && (module->disabled || !module->implemented)) { |
| 261 | /* nothing to return from a disabled/imported module */ |
| 262 | return NULL; |
| 263 | } |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 264 | next = last = module->data; |
| 265 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 266 | } else if ((last->nodetype == LYS_USES) && (options & LYS_GETNEXT_INTOUSES) && last->child) { |
| 267 | /* continue with uses content */ |
| 268 | next = last->child; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 269 | } else { |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 270 | /* continue after the last returned value */ |
| 271 | next = last->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | repeat: |
Michal Vasko | 5a9c24b | 2017-03-13 09:25:58 +0100 | [diff] [blame] | 275 | if (parent && (parent->nodetype == LYS_AUGMENT) && next) { |
| 276 | /* do not return anything outside the parent augment */ |
| 277 | aug_parent = next->parent; |
| 278 | do { |
| 279 | while (aug_parent && (aug_parent->nodetype != LYS_AUGMENT)) { |
| 280 | aug_parent = aug_parent->parent; |
| 281 | } |
| 282 | if (aug_parent) { |
| 283 | if (aug_parent == parent) { |
| 284 | break; |
| 285 | } |
| 286 | aug_parent = ((struct lys_node_augment *)aug_parent)->target; |
| 287 | } |
| 288 | |
| 289 | } while (aug_parent); |
| 290 | if (!aug_parent) { |
| 291 | return NULL; |
| 292 | } |
| 293 | } |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 294 | while (next && (next->nodetype == LYS_GROUPING)) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 295 | if (options & LYS_GETNEXT_WITHGROUPING) { |
| 296 | return next; |
| 297 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 298 | next = next->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 301 | if (!next) { /* cover case when parent is augment */ |
| 302 | if (!last || last->parent == parent || lys_parent(last) == parent) { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 303 | /* no next element */ |
| 304 | return NULL; |
| 305 | } |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 306 | last = lys_parent(last); |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 307 | next = last->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 308 | goto repeat; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 309 | } else { |
| 310 | last = next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 311 | } |
| 312 | |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 313 | if (!(options & LYS_GETNEXT_NOSTATECHECK) && lys_is_disabled(next, 0)) { |
| 314 | next = next->next; |
| 315 | goto repeat; |
| 316 | } |
| 317 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 318 | switch (next->nodetype) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 319 | case LYS_INPUT: |
| 320 | case LYS_OUTPUT: |
| 321 | if (options & LYS_GETNEXT_WITHINOUT) { |
| 322 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 323 | } else if (next->child) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 324 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 325 | } else { |
| 326 | next = next->next; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 327 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 328 | goto repeat; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 329 | |
Michal Vasko | a5835e9 | 2015-10-20 15:07:39 +0200 | [diff] [blame] | 330 | case LYS_CASE: |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 331 | if (options & LYS_GETNEXT_WITHCASE) { |
| 332 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 333 | } else if (next->child) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 334 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 335 | } else { |
| 336 | next = next->next; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 337 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 338 | goto repeat; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 339 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 340 | case LYS_USES: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 341 | /* go into */ |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 342 | if (options & LYS_GETNEXT_WITHUSES) { |
| 343 | return next; |
| 344 | } else if (next->child) { |
| 345 | next = next->child; |
| 346 | } else { |
| 347 | next = next->next; |
| 348 | } |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 349 | goto repeat; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 350 | |
Radek Krejci | dfcae7d | 2015-10-20 17:13:01 +0200 | [diff] [blame] | 351 | case LYS_RPC: |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 352 | case LYS_ACTION: |
Radek Krejci | dfcae7d | 2015-10-20 17:13:01 +0200 | [diff] [blame] | 353 | case LYS_NOTIF: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 354 | case LYS_LEAF: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 355 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 356 | case LYS_ANYDATA: |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 357 | case LYS_LIST: |
| 358 | case LYS_LEAFLIST: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 359 | return next; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 360 | |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 361 | case LYS_CONTAINER: |
| 362 | if (!((struct lys_node_container *)next)->presence && (options & LYS_GETNEXT_INTONPCONT)) { |
| 363 | if (next->child) { |
| 364 | /* go into */ |
| 365 | next = next->child; |
| 366 | } else { |
| 367 | next = next->next; |
| 368 | } |
| 369 | goto repeat; |
| 370 | } else { |
| 371 | return next; |
| 372 | } |
| 373 | |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 374 | case LYS_CHOICE: |
| 375 | if (options & LYS_GETNEXT_WITHCHOICE) { |
| 376 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 377 | } else if (next->child) { |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 378 | /* go into */ |
| 379 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 380 | } else { |
| 381 | next = next->next; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 382 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 383 | goto repeat; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 384 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 385 | default: |
| 386 | /* we should not be here */ |
| 387 | return NULL; |
| 388 | } |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 389 | } |
| 390 | |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 391 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 392 | lys_node_unlink(struct lys_node *node) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 393 | { |
Michal Vasko | f75b277 | 2018-03-14 09:55:33 +0100 | [diff] [blame] | 394 | struct lys_node *parent, *first, **pp = NULL; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 395 | struct lys_module *main_module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 396 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 397 | if (!node) { |
| 398 | return; |
| 399 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 400 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 401 | /* unlink from data model if necessary */ |
| 402 | if (node->module) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 403 | /* get main module with data tree */ |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 404 | main_module = lys_node_module(node); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 405 | if (main_module->data == node) { |
| 406 | main_module->data = node->next; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 407 | } |
| 408 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 409 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 410 | /* store pointers to important nodes */ |
| 411 | parent = node->parent; |
Michal Vasko | 3a9943b | 2015-09-23 11:33:50 +0200 | [diff] [blame] | 412 | if (parent && (parent->nodetype == LYS_AUGMENT)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 413 | /* handle augments - first, unlink it from the augment parent ... */ |
| 414 | if (parent->child == node) { |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 415 | parent->child = (node->next && node->next->parent == parent) ? node->next : NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 416 | } |
Radek Krejci | fec2e14 | 2017-01-05 15:19:03 +0100 | [diff] [blame] | 417 | |
| 418 | if (parent->flags & LYS_NOTAPPLIED) { |
| 419 | /* data are not connected in the target, so we cannot continue with the target as a parent */ |
| 420 | parent = NULL; |
| 421 | } else { |
| 422 | /* data are connected in target, so we will continue with the target as a parent */ |
| 423 | parent = ((struct lys_node_augment *)parent)->target; |
| 424 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 425 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 426 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 427 | /* unlink from parent */ |
| 428 | if (parent) { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 429 | if (parent->nodetype == LYS_EXT) { |
| 430 | pp = (struct lys_node **)lys_ext_complex_get_substmt(lys_snode2stmt(node->nodetype), |
| 431 | (struct lys_ext_instance_complex*)parent, NULL); |
| 432 | if (*pp == node) { |
| 433 | *pp = node->next; |
| 434 | } |
| 435 | } else if (parent->child == node) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 436 | parent->child = node->next; |
| 437 | } |
| 438 | node->parent = NULL; |
| 439 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 440 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 441 | /* unlink from siblings */ |
| 442 | if (node->prev == node) { |
| 443 | /* there are no more siblings */ |
| 444 | return; |
| 445 | } |
| 446 | if (node->next) { |
| 447 | node->next->prev = node->prev; |
| 448 | } else { |
| 449 | /* unlinking the last element */ |
| 450 | if (parent) { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 451 | if (parent->nodetype == LYS_EXT) { |
| 452 | first = *(struct lys_node **)pp; |
| 453 | } else { |
| 454 | first = parent->child; |
| 455 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 456 | } else { |
| 457 | first = node; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 458 | while (first->prev->next) { |
Michal Vasko | 276f96b | 2015-09-23 11:34:28 +0200 | [diff] [blame] | 459 | first = first->prev; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | first->prev = node->prev; |
| 463 | } |
| 464 | if (node->prev->next) { |
| 465 | node->prev->next = node->next; |
| 466 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 467 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 468 | /* clean up the unlinked element */ |
| 469 | node->next = NULL; |
| 470 | node->prev = node; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 471 | } |
| 472 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 473 | struct lys_node_grp * |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 474 | lys_find_grouping_up(const char *name, struct lys_node *start) |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 475 | { |
| 476 | struct lys_node *par_iter, *iter, *stop; |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 477 | |
| 478 | for (par_iter = start; par_iter; par_iter = par_iter->parent) { |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 479 | /* top-level augment, look into module (uses augment is handled correctly below) */ |
| 480 | 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] | 481 | par_iter = lys_main_module(par_iter->parent->module)->data; |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 482 | if (!par_iter) { |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 483 | break; |
| 484 | } |
| 485 | } |
| 486 | |
Michal Vasko | 38a0041 | 2018-03-14 10:01:50 +0100 | [diff] [blame] | 487 | if (par_iter->nodetype == LYS_EXT) { |
| 488 | /* we are in a top-level extension, search grouping in top-level groupings */ |
| 489 | par_iter = lys_main_module(par_iter->module)->data; |
| 490 | if (!par_iter) { |
| 491 | /* not connected yet, wait */ |
| 492 | return NULL; |
| 493 | } |
| 494 | } 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] | 495 | continue; |
| 496 | } |
| 497 | |
| 498 | for (iter = par_iter, stop = NULL; iter; iter = iter->prev) { |
| 499 | if (!stop) { |
| 500 | stop = par_iter; |
| 501 | } else if (iter == stop) { |
| 502 | break; |
| 503 | } |
| 504 | if (iter->nodetype != LYS_GROUPING) { |
| 505 | continue; |
| 506 | } |
| 507 | |
Radek Krejci | f8426a7 | 2015-10-31 23:14:03 +0100 | [diff] [blame] | 508 | if (!strcmp(name, iter->name)) { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 509 | return (struct lys_node_grp *)iter; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 514 | return NULL; |
| 515 | } |
| 516 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 517 | /* |
| 518 | * get next grouping in the root's subtree, in the |
| 519 | * first call, tha last is NULL |
| 520 | */ |
| 521 | static struct lys_node_grp * |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 522 | 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] | 523 | { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 524 | struct lys_node *last = (struct lys_node *)lastgrp; |
| 525 | struct lys_node *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 526 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 527 | assert(root); |
| 528 | |
| 529 | if (!last) { |
| 530 | last = root; |
| 531 | } |
| 532 | |
| 533 | while (1) { |
| 534 | if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) { |
| 535 | next = last->child; |
| 536 | } else { |
| 537 | next = NULL; |
| 538 | } |
| 539 | if (!next) { |
| 540 | if (last == root) { |
| 541 | /* we are done */ |
| 542 | return NULL; |
| 543 | } |
| 544 | |
| 545 | /* no children, go to siblings */ |
| 546 | next = last->next; |
| 547 | } |
| 548 | while (!next) { |
| 549 | /* go back through parents */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 550 | if (lys_parent(last) == root) { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 551 | /* we are done */ |
| 552 | return NULL; |
| 553 | } |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 554 | next = last->next; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 555 | last = lys_parent(last); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | if (next->nodetype == LYS_GROUPING) { |
| 559 | return (struct lys_node_grp *)next; |
| 560 | } |
| 561 | |
| 562 | last = next; |
| 563 | } |
| 564 | } |
| 565 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 566 | /* logs directly */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 567 | int |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 568 | lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module) |
| 569 | { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 570 | struct lys_node *start, *stop, *iter; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 571 | struct lys_node_grp *grp; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 572 | int down, up; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 573 | |
| 574 | assert(node); |
| 575 | |
| 576 | if (!parent) { |
| 577 | assert(module); |
| 578 | } else { |
| 579 | module = parent->module; |
| 580 | } |
Radek Krejci | 115fa88 | 2017-03-01 16:15:07 +0100 | [diff] [blame] | 581 | module = lys_main_module(module); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 582 | |
| 583 | switch (node->nodetype) { |
| 584 | case LYS_GROUPING: |
| 585 | /* 6.2.1, rule 6 */ |
| 586 | if (parent) { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 587 | start = *lys_child(parent, LYS_GROUPING); |
| 588 | if (!start) { |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 589 | down = 0; |
| 590 | start = parent; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 591 | } else { |
| 592 | down = 1; |
| 593 | } |
| 594 | if (parent->nodetype == LYS_EXT) { |
| 595 | up = 0; |
| 596 | } else { |
| 597 | up = 1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 598 | } |
| 599 | } else { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 600 | down = up = 1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 601 | start = module->data; |
| 602 | } |
| 603 | /* go up */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 604 | if (up && lys_find_grouping_up(node->name, start)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 605 | LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name); |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 606 | return EXIT_FAILURE; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 607 | } |
| 608 | /* go down, because grouping can be defined after e.g. container in which is collision */ |
| 609 | if (down) { |
| 610 | for (iter = start, stop = NULL; iter; iter = iter->prev) { |
| 611 | if (!stop) { |
| 612 | stop = start; |
| 613 | } else if (iter == stop) { |
| 614 | break; |
| 615 | } |
| 616 | if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) { |
| 617 | continue; |
| 618 | } |
| 619 | |
| 620 | grp = NULL; |
| 621 | while ((grp = lys_get_next_grouping(grp, iter))) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 622 | if (ly_strequal(node->name, grp->name, 1)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 623 | LOGVAL(module->ctx, LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 624 | return EXIT_FAILURE; |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | break; |
| 630 | case LYS_LEAF: |
| 631 | case LYS_LEAFLIST: |
| 632 | case LYS_LIST: |
| 633 | case LYS_CONTAINER: |
| 634 | case LYS_CHOICE: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 635 | case LYS_ANYDATA: |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 636 | /* 6.2.1, rule 7 */ |
| 637 | if (parent) { |
| 638 | iter = parent; |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 639 | while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE | LYS_AUGMENT))) { |
| 640 | if (iter->nodetype == LYS_AUGMENT) { |
| 641 | if (((struct lys_node_augment *)iter)->target) { |
| 642 | /* augment is resolved, go up */ |
| 643 | iter = ((struct lys_node_augment *)iter)->target; |
| 644 | continue; |
| 645 | } |
| 646 | /* augment is not resolved, this is the final parent */ |
| 647 | break; |
| 648 | } |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 649 | iter = iter->parent; |
| 650 | } |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 651 | |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 652 | if (!iter) { |
| 653 | stop = NULL; |
| 654 | iter = module->data; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 655 | } else if (iter->nodetype == LYS_EXT) { |
| 656 | stop = iter; |
PavolVican | 9d61d40 | 2018-02-05 15:52:48 +0100 | [diff] [blame] | 657 | iter = (struct lys_node *)lys_child(iter, node->nodetype); |
| 658 | if (iter) { |
| 659 | iter = *(struct lys_node **)iter; |
| 660 | } |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 661 | } else { |
| 662 | stop = iter; |
| 663 | iter = iter->child; |
| 664 | } |
| 665 | } else { |
| 666 | stop = NULL; |
| 667 | iter = module->data; |
| 668 | } |
| 669 | while (iter) { |
| 670 | if (iter->nodetype & (LYS_USES | LYS_CASE)) { |
| 671 | iter = iter->child; |
| 672 | continue; |
| 673 | } |
| 674 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 675 | 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] | 676 | if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 677 | 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] | 678 | return EXIT_FAILURE; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | /* special case for choice - we must check the choice's name as |
| 683 | * well as the names of nodes under the choice |
| 684 | */ |
| 685 | if (iter->nodetype == LYS_CHOICE) { |
| 686 | iter = iter->child; |
| 687 | continue; |
| 688 | } |
| 689 | |
| 690 | /* go to siblings */ |
| 691 | if (!iter->next) { |
| 692 | /* no sibling, go to parent's sibling */ |
| 693 | do { |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 694 | /* for parent LYS_AUGMENT */ |
| 695 | if (iter->parent == stop) { |
| 696 | iter = stop; |
| 697 | break; |
| 698 | } |
| 699 | iter = lys_parent(iter); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 700 | if (iter && iter->next) { |
| 701 | break; |
| 702 | } |
| 703 | } while (iter != stop); |
| 704 | |
| 705 | if (iter == stop) { |
| 706 | break; |
| 707 | } |
| 708 | } |
| 709 | iter = iter->next; |
| 710 | } |
| 711 | break; |
| 712 | case LYS_CASE: |
| 713 | /* 6.2.1, rule 8 */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 714 | if (parent) { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 715 | start = *lys_child(parent, LYS_CASE); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 716 | } else { |
| 717 | start = module->data; |
| 718 | } |
| 719 | |
| 720 | LY_TREE_FOR(start, iter) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 721 | 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] | 722 | continue; |
| 723 | } |
| 724 | |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 725 | if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 726 | LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, "case", node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 727 | return EXIT_FAILURE; |
| 728 | } |
| 729 | } |
| 730 | break; |
| 731 | default: |
| 732 | /* no check needed */ |
| 733 | break; |
| 734 | } |
| 735 | |
| 736 | return EXIT_SUCCESS; |
| 737 | } |
| 738 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 739 | /* logs directly */ |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 740 | int |
Michal Vasko | 8d30dd8 | 2018-09-10 10:06:12 +0200 | [diff] [blame] | 741 | 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] | 742 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 743 | struct ly_ctx *ctx = child->module->ctx; |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 744 | struct lys_node *iter, **pchild; |
Michal Vasko | 56601c9 | 2018-08-06 10:03:06 +0200 | [diff] [blame] | 745 | struct lys_node_inout *in, *out; |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 746 | struct lys_node_case *c; |
| 747 | int type, shortcase = 0; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 748 | void *p; |
| 749 | struct lyext_substmt *info = NULL; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 750 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 751 | assert(child); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 752 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 753 | if (parent) { |
| 754 | type = parent->nodetype; |
| 755 | module = parent->module; |
| 756 | } else { |
| 757 | assert(module); |
Radek Krejci | fe3a138 | 2016-11-04 10:30:11 +0100 | [diff] [blame] | 758 | assert(!(child->nodetype & (LYS_INPUT | LYS_OUTPUT))); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 759 | type = 0; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 760 | } |
| 761 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 762 | /* checks */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 763 | switch (type) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 764 | case LYS_CONTAINER: |
| 765 | case LYS_LIST: |
| 766 | case LYS_GROUPING: |
Radek Krejci | 9693540 | 2016-11-04 16:27:28 +0100 | [diff] [blame] | 767 | case LYS_USES: |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 768 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 769 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF | |
Michal Vasko | b15cae2 | 2016-09-15 09:40:56 +0200 | [diff] [blame] | 770 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 771 | 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] | 772 | return EXIT_FAILURE; |
| 773 | } |
| 774 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 775 | case LYS_INPUT: |
| 776 | case LYS_OUTPUT: |
| 777 | case LYS_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 778 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 779 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 780 | LYS_LEAFLIST | LYS_LIST | LYS_USES))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 781 | 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] | 782 | return EXIT_FAILURE; |
| 783 | } |
| 784 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 785 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 786 | if (!(child->nodetype & |
Pavol Vican | 47a1b0a | 2016-09-20 10:18:24 +0200 | [diff] [blame] | 787 | (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] | 788 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 789 | return EXIT_FAILURE; |
| 790 | } |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 791 | if (child->nodetype != LYS_CASE) { |
| 792 | shortcase = 1; |
| 793 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 794 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 795 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 796 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 797 | (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] | 798 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 799 | return EXIT_FAILURE; |
| 800 | } |
| 801 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 802 | case LYS_RPC: |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 803 | case LYS_ACTION: |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 804 | if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 805 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc"); |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 806 | return EXIT_FAILURE; |
| 807 | } |
| 808 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 809 | case LYS_LEAF: |
| 810 | case LYS_LEAFLIST: |
| 811 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 812 | case LYS_ANYDATA: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 813 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
| 814 | 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] | 815 | strnodetype(parent->nodetype)); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 816 | return EXIT_FAILURE; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 817 | case LYS_AUGMENT: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 818 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 819 | (LYS_ANYDATA | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF |
Michal Vasko | db01726 | 2017-01-24 13:10:04 +0100 | [diff] [blame] | 820 | | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 821 | 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] | 822 | return EXIT_FAILURE; |
| 823 | } |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 824 | break; |
| 825 | case LYS_UNKNOWN: |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 826 | /* top level */ |
| 827 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 828 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 829 | | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 830 | 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] | 831 | return EXIT_FAILURE; |
| 832 | } |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 833 | break; |
| 834 | case LYS_EXT: |
| 835 | /* plugin-defined */ |
| 836 | p = lys_ext_complex_get_substmt(lys_snode2stmt(child->nodetype), (struct lys_ext_instance_complex*)parent, &info); |
| 837 | if (!p) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 838 | LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 839 | ((struct lys_ext_instance_complex*)parent)->def->name); |
| 840 | return EXIT_FAILURE; |
| 841 | } |
| 842 | /* TODO check cardinality */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 843 | break; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | /* check identifier uniqueness */ |
Michal Vasko | 15a4337 | 2017-09-25 14:12:42 +0200 | [diff] [blame] | 847 | 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] | 848 | return EXIT_FAILURE; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 849 | } |
Radek Krejci | b7155b5 | 2015-06-10 17:03:01 +0200 | [diff] [blame] | 850 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 851 | if (child->parent) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 852 | lys_node_unlink(child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 853 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 854 | |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 855 | if ((child->nodetype & (LYS_INPUT | LYS_OUTPUT)) && parent->nodetype != LYS_EXT) { |
Michal Vasko | 56601c9 | 2018-08-06 10:03:06 +0200 | [diff] [blame] | 856 | /* find the implicit input/output node */ |
| 857 | LY_TREE_FOR(parent->child, iter) { |
| 858 | if (iter->nodetype == child->nodetype) { |
| 859 | break; |
| 860 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 861 | } |
Michal Vasko | 56601c9 | 2018-08-06 10:03:06 +0200 | [diff] [blame] | 862 | assert(iter); |
| 863 | |
| 864 | /* switch the old implicit node (iter) with the new one (child) */ |
| 865 | if (parent->child == iter) { |
| 866 | /* first child */ |
| 867 | parent->child = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 868 | } else { |
Michal Vasko | 56601c9 | 2018-08-06 10:03:06 +0200 | [diff] [blame] | 869 | iter->prev->next = child; |
| 870 | } |
| 871 | child->prev = iter->prev; |
| 872 | child->next = iter->next; |
| 873 | if (iter->next) { |
| 874 | iter->next->prev = child; |
| 875 | } else { |
| 876 | /* last child */ |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 877 | parent->child->prev = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 878 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 879 | child->parent = parent; |
Michal Vasko | 56601c9 | 2018-08-06 10:03:06 +0200 | [diff] [blame] | 880 | |
| 881 | /* isolate the node and free it */ |
| 882 | iter->next = NULL; |
| 883 | iter->prev = iter; |
| 884 | iter->parent = NULL; |
| 885 | lys_node_free(iter, NULL, 0); |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 886 | } else { |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 887 | if (shortcase) { |
| 888 | /* create the implicit case to allow it to serve as a target of the augments, |
| 889 | * it won't be printed, but it will be present in the tree */ |
| 890 | c = calloc(1, sizeof *c); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 891 | LY_CHECK_ERR_RETURN(!c, LOGMEM(ctx), EXIT_FAILURE); |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 892 | c->name = lydict_insert(module->ctx, child->name, 0); |
| 893 | c->flags = LYS_IMPLICIT; |
Michal Vasko | 8d30dd8 | 2018-09-10 10:06:12 +0200 | [diff] [blame] | 894 | if (!(options & (LYS_PARSE_OPT_CFG_IGNORE | LYS_PARSE_OPT_CFG_NOINHERIT))) { |
| 895 | /* get config flag from parent */ |
| 896 | c->flags |= parent->flags & LYS_CONFIG_MASK; |
| 897 | } |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 898 | c->module = module; |
| 899 | c->nodetype = LYS_CASE; |
| 900 | c->prev = (struct lys_node*)c; |
Michal Vasko | 8d30dd8 | 2018-09-10 10:06:12 +0200 | [diff] [blame] | 901 | lys_node_addchild(parent, module, (struct lys_node*)c, options); |
Radek Krejci | 744c2d4 | 2017-03-26 13:30:00 -0500 | [diff] [blame] | 902 | parent = (struct lys_node*)c; |
| 903 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 904 | /* connect the child correctly */ |
| 905 | if (!parent) { |
| 906 | if (module->data) { |
| 907 | module->data->prev->next = child; |
| 908 | child->prev = module->data->prev; |
| 909 | module->data->prev = child; |
| 910 | } else { |
| 911 | module->data = child; |
| 912 | } |
| 913 | } else { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 914 | pchild = lys_child(parent, child->nodetype); |
| 915 | assert(pchild); |
| 916 | |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 917 | child->parent = parent; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 918 | if (!(*pchild)) { |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 919 | /* the only/first child of the parent */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 920 | *pchild = child; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 921 | iter = child; |
| 922 | } else { |
| 923 | /* add a new child at the end of parent's child list */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 924 | iter = (*pchild)->prev; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 925 | iter->next = child; |
| 926 | child->prev = iter; |
| 927 | } |
| 928 | while (iter->next) { |
| 929 | iter = iter->next; |
| 930 | iter->parent = parent; |
| 931 | } |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 932 | (*pchild)->prev = iter; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 933 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 934 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 935 | |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 936 | /* check config value (but ignore them in groupings and augments) */ |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 937 | 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] | 938 | if (parent && !iter) { |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 939 | for (iter = child; iter && !(iter->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); iter = iter->parent); |
| 940 | if (!iter && (parent->flags & LYS_CONFIG_R) && (child->flags & LYS_CONFIG_W)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 941 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, child, "true", "config"); |
| 942 | 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] | 943 | return EXIT_FAILURE; |
| 944 | } |
| 945 | } |
| 946 | |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 947 | /* propagate information about status data presence */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 948 | 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] | 949 | (child->flags & LYS_INCL_STATUS)) { |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 950 | for(iter = parent; iter; iter = lys_parent(iter)) { |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 951 | /* store it only into container or list - the only data inner nodes */ |
| 952 | if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
| 953 | if (iter->flags & LYS_INCL_STATUS) { |
| 954 | /* done, someone else set it already from here */ |
| 955 | break; |
| 956 | } |
| 957 | /* set flag about including status data */ |
| 958 | iter->flags |= LYS_INCL_STATUS; |
| 959 | } |
| 960 | } |
| 961 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 962 | |
| 963 | /* 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] | 964 | if ((child->nodetype & (LYS_RPC | LYS_ACTION)) && !child->child) { |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 965 | in = calloc(1, sizeof *in); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 966 | out = calloc(1, sizeof *out); |
| 967 | if (!in || !out) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 968 | LOGMEM(ctx); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 969 | free(in); |
| 970 | free(out); |
| 971 | return EXIT_FAILURE; |
| 972 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 973 | in->nodetype = LYS_INPUT; |
| 974 | in->name = lydict_insert(child->module->ctx, "input", 5); |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 975 | out->nodetype = LYS_OUTPUT; |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 976 | out->name = lydict_insert(child->module->ctx, "output", 6); |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 977 | in->module = out->module = child->module; |
| 978 | in->parent = out->parent = child; |
| 979 | in->flags = out->flags = LYS_IMPLICIT; |
| 980 | in->next = (struct lys_node *)out; |
| 981 | in->prev = (struct lys_node *)out; |
| 982 | out->prev = (struct lys_node *)in; |
| 983 | child->child = (struct lys_node *)in; |
| 984 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 985 | return EXIT_SUCCESS; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 986 | } |
| 987 | |
Michal Vasko | 2924566 | 2017-04-18 15:56:31 +0200 | [diff] [blame] | 988 | const struct lys_module * |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 989 | 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] | 990 | { |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 991 | char *enlarged_data = NULL; |
Radek Krejci | 0b5805d | 2015-08-13 09:38:02 +0200 | [diff] [blame] | 992 | struct lys_module *mod = NULL; |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 993 | unsigned int len; |
Radek Krejci | f347abc | 2016-06-22 10:18:47 +0200 | [diff] [blame] | 994 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 995 | if (!ctx || !data) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 996 | LOGARG; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 997 | return NULL; |
| 998 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 999 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1000 | if (!internal && format == LYS_IN_YANG) { |
| 1001 | /* enlarge data by 2 bytes for flex */ |
| 1002 | len = strlen(data); |
| 1003 | enlarged_data = malloc((len + 2) * sizeof *enlarged_data); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1004 | LY_CHECK_ERR_RETURN(!enlarged_data, LOGMEM(ctx), NULL); |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1005 | memcpy(enlarged_data, data, len); |
| 1006 | enlarged_data[len] = enlarged_data[len + 1] = '\0'; |
| 1007 | data = enlarged_data; |
| 1008 | } |
| 1009 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1010 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1011 | case LYS_IN_YIN: |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1012 | mod = yin_read_module(ctx, data, revision, implement); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1013 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1014 | case LYS_IN_YANG: |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1015 | mod = yang_read_module(ctx, data, 0, revision, implement); |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1016 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1017 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1018 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1019 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1020 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1021 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1022 | free(enlarged_data); |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 1023 | |
| 1024 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 1025 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 1026 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 1027 | * the anotation definitions available in the internal schema structure. There is another hack in schema |
| 1028 | * printers to do not print this internally added annotation. */ |
| 1029 | if (mod && ly_strequal(mod->name, "ietf-netconf", 0)) { |
Radek Krejci | 5b19066 | 2017-04-13 08:56:14 +0200 | [diff] [blame] | 1030 | if (lyp_add_ietf_netconf_annotations(mod)) { |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 1031 | lys_free(mod, NULL, 1, 1); |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 1032 | return NULL; |
| 1033 | } |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 1034 | } |
| 1035 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1036 | return mod; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1037 | } |
| 1038 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1039 | API const struct lys_module * |
| 1040 | lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format) |
| 1041 | { |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1042 | return lys_parse_mem_(ctx, data, format, NULL, 0, 1); |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1043 | } |
| 1044 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1045 | struct lys_submodule * |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1046 | 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] | 1047 | { |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1048 | char *enlarged_data = NULL; |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1049 | struct lys_submodule *submod = NULL; |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1050 | unsigned int len; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1051 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1052 | assert(module); |
| 1053 | assert(data); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1054 | |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1055 | if (format == LYS_IN_YANG) { |
| 1056 | /* enlarge data by 2 bytes for flex */ |
| 1057 | len = strlen(data); |
| 1058 | enlarged_data = malloc((len + 2) * sizeof *enlarged_data); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1059 | LY_CHECK_ERR_RETURN(!enlarged_data, LOGMEM(module->ctx), NULL); |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1060 | memcpy(enlarged_data, data, len); |
| 1061 | enlarged_data[len] = enlarged_data[len + 1] = '\0'; |
| 1062 | data = enlarged_data; |
| 1063 | } |
| 1064 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1065 | /* get the main module */ |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 1066 | module = lys_main_module(module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1067 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1068 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1069 | case LYS_IN_YIN: |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1070 | submod = yin_read_submodule(module, data, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1071 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1072 | case LYS_IN_YANG: |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1073 | submod = yang_read_submodule(module, data, 0, unres); |
| 1074 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1075 | default: |
Radek Krejci | 90a550a | 2016-04-13 16:00:58 +0200 | [diff] [blame] | 1076 | assert(0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1077 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1078 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1079 | |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1080 | free(enlarged_data); |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1081 | return submod; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1082 | } |
| 1083 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1084 | API const struct lys_module * |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1085 | lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format) |
| 1086 | { |
| 1087 | int fd; |
| 1088 | const struct lys_module *ret; |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 1089 | const char *rev, *dot, *filename; |
| 1090 | size_t len; |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1091 | |
| 1092 | if (!ctx || !path) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1093 | LOGARG; |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1094 | return NULL; |
| 1095 | } |
| 1096 | |
| 1097 | fd = open(path, O_RDONLY); |
| 1098 | if (fd == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1099 | LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)); |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1100 | return NULL; |
| 1101 | } |
| 1102 | |
| 1103 | ret = lys_parse_fd(ctx, fd, format); |
| 1104 | close(fd); |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 1105 | |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 1106 | if (!ret) { |
| 1107 | /* error */ |
| 1108 | return NULL; |
| 1109 | } |
| 1110 | |
| 1111 | /* check that name and revision match filename */ |
| 1112 | filename = strrchr(path, '/'); |
| 1113 | if (!filename) { |
| 1114 | filename = path; |
| 1115 | } else { |
| 1116 | filename++; |
| 1117 | } |
| 1118 | rev = strchr(filename, '@'); |
| 1119 | dot = strrchr(filename, '.'); |
| 1120 | |
| 1121 | /* name */ |
| 1122 | len = strlen(ret->name); |
| 1123 | if (strncmp(filename, ret->name, len) || |
| 1124 | ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1125 | 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] | 1126 | } |
| 1127 | if (rev) { |
| 1128 | len = dot - ++rev; |
| 1129 | 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] | 1130 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 1131 | ret->rev_size ? ret->rev[0].date : "none"); |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | if (!ret->filepath) { |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 1136 | /* store URI */ |
Alexandre Snarskii | 12b2218 | 2018-09-04 17:12:28 +0300 | [diff] [blame] | 1137 | char rpath[PATH_MAX]; |
| 1138 | if (realpath(path, rpath) != NULL) { |
| 1139 | ((struct lys_module *)ret)->filepath = lydict_insert(ctx, rpath, 0); |
| 1140 | } else { |
| 1141 | ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0); |
| 1142 | } |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 1143 | } |
| 1144 | |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1145 | return ret; |
| 1146 | } |
| 1147 | |
| 1148 | API const struct lys_module * |
| 1149 | lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format) |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1150 | { |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1151 | return lys_parse_fd_(ctx, fd, format, NULL, 1); |
| 1152 | } |
| 1153 | |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1154 | static void |
| 1155 | lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd) |
| 1156 | { |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1157 | #ifdef __APPLE__ |
| 1158 | char path[MAXPATHLEN]; |
| 1159 | #else |
Michal Vasko | 25601f3 | 2018-02-16 09:41:46 +0100 | [diff] [blame] | 1160 | int len; |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1161 | char path[PATH_MAX], proc_path[32]; |
| 1162 | #endif |
| 1163 | |
| 1164 | #ifdef __APPLE__ |
| 1165 | if (fcntl(fd, F_GETPATH, path) != -1) { |
| 1166 | *filename = lydict_insert(ctx, path, 0); |
| 1167 | } |
| 1168 | #else |
| 1169 | /* get URI if there is /proc */ |
| 1170 | sprintf(proc_path, "/proc/self/fd/%d", fd); |
| 1171 | if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) { |
| 1172 | *filename = lydict_insert(ctx, path, len); |
| 1173 | } |
| 1174 | #endif |
| 1175 | } |
| 1176 | |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1177 | const struct lys_module * |
| 1178 | lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const char *revision, int implement) |
| 1179 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1180 | const struct lys_module *module; |
Radek Krejci | 0fb1150 | 2017-01-31 16:45:42 +0100 | [diff] [blame] | 1181 | size_t length; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1182 | char *addr; |
| 1183 | |
| 1184 | if (!ctx || fd < 0) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1185 | LOGARG; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1186 | return NULL; |
| 1187 | } |
| 1188 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1189 | if (lyp_mmap(ctx, fd, format == LYS_IN_YANG ? 1 : 0, &length, (void **)&addr)) { |
| 1190 | LOGERR(ctx, LY_ESYS, "Mapping file descriptor into memory failed (%s()).", __func__); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1191 | return NULL; |
Radek Krejci | 10c216a | 2017-02-01 10:36:00 +0100 | [diff] [blame] | 1192 | } else if (!addr) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1193 | LOGERR(ctx, LY_EINVAL, "Empty schema file."); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1194 | return NULL; |
| 1195 | } |
Radek Krejci | 0fb1150 | 2017-01-31 16:45:42 +0100 | [diff] [blame] | 1196 | |
Michal Vasko | fb98dc4 | 2018-01-11 13:38:28 +0100 | [diff] [blame] | 1197 | module = lys_parse_mem_(ctx, addr, format, revision, 1, implement); |
Radek Krejci | 0fb1150 | 2017-01-31 16:45:42 +0100 | [diff] [blame] | 1198 | lyp_munmap(addr, length); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1199 | |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1200 | if (module && !module->filepath) { |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1201 | lys_parse_set_filename(ctx, (const char **)&module->filepath, fd); |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1202 | } |
| 1203 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1204 | return module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1205 | } |
| 1206 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1207 | struct lys_submodule * |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1208 | 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] | 1209 | { |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1210 | struct lys_submodule *submodule; |
Radek Krejci | 0fb1150 | 2017-01-31 16:45:42 +0100 | [diff] [blame] | 1211 | size_t length; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1212 | char *addr; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1213 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1214 | assert(module); |
| 1215 | assert(fd >= 0); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1216 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1217 | if (lyp_mmap(module->ctx, fd, format == LYS_IN_YANG ? 1 : 0, &length, (void **)&addr)) { |
| 1218 | 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] | 1219 | return NULL; |
Radek Krejci | 10c216a | 2017-02-01 10:36:00 +0100 | [diff] [blame] | 1220 | } else if (!addr) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1221 | LOGERR(module->ctx, LY_EINVAL, "Empty submodule schema file."); |
Michal Vasko | 2e7241e | 2016-02-15 16:06:34 +0100 | [diff] [blame] | 1222 | return NULL; |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1223 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1224 | |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1225 | /* get the main module */ |
| 1226 | module = lys_main_module(module); |
| 1227 | |
| 1228 | switch (format) { |
| 1229 | case LYS_IN_YIN: |
| 1230 | submodule = yin_read_submodule(module, addr, unres); |
| 1231 | break; |
| 1232 | case LYS_IN_YANG: |
| 1233 | submodule = yang_read_submodule(module, addr, 0, unres); |
| 1234 | break; |
| 1235 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1236 | LOGINT(module->ctx); |
Michal Vasko | 85d4152 | 2017-02-24 09:49:16 +0100 | [diff] [blame] | 1237 | return NULL; |
Michal Vasko | 5b99871 | 2017-01-26 10:34:06 +0100 | [diff] [blame] | 1238 | } |
| 1239 | |
Radek Krejci | c645a3a | 2017-01-31 16:59:00 +0100 | [diff] [blame] | 1240 | lyp_munmap(addr, length); |
Michal Vasko | b0bbf5f | 2018-02-16 09:35:59 +0100 | [diff] [blame] | 1241 | |
| 1242 | if (submodule && !submodule->filepath) { |
| 1243 | lys_parse_set_filename(module->ctx, (const char **)&submodule->filepath, fd); |
| 1244 | } |
| 1245 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1246 | return submodule; |
| 1247 | |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1248 | } |
| 1249 | |
Radek Krejci | 72cdfac | 2018-08-15 14:47:33 +0200 | [diff] [blame] | 1250 | API int |
Radek Krejci | cf3b7ca | 2018-08-17 09:55:06 +0200 | [diff] [blame] | 1251 | 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] | 1252 | { |
| 1253 | size_t len, flen, match_len = 0, dir_len; |
| 1254 | int i, implicit_cwd = 0, ret = EXIT_FAILURE; |
| 1255 | char *wd, *wn = NULL; |
| 1256 | DIR *dir = NULL; |
| 1257 | struct dirent *file; |
| 1258 | char *match_name = NULL; |
| 1259 | LYS_INFORMAT format_aux, match_format = 0; |
| 1260 | unsigned int u; |
| 1261 | struct ly_set *dirs; |
| 1262 | struct stat st; |
| 1263 | |
| 1264 | if (!localfile) { |
| 1265 | LOGARG; |
| 1266 | return EXIT_FAILURE; |
| 1267 | } |
| 1268 | |
| 1269 | /* start to fill the dir fifo with the context's search path (if set) |
| 1270 | * and the current working directory */ |
| 1271 | dirs = ly_set_new(); |
| 1272 | if (!dirs) { |
| 1273 | LOGMEM(NULL); |
| 1274 | return EXIT_FAILURE; |
| 1275 | } |
| 1276 | |
| 1277 | len = strlen(name); |
Radek Krejci | cf3b7ca | 2018-08-17 09:55:06 +0200 | [diff] [blame] | 1278 | if (cwd) { |
| 1279 | wd = get_current_dir_name(); |
| 1280 | if (!wd) { |
| 1281 | LOGMEM(NULL); |
Radek Krejci | 72cdfac | 2018-08-15 14:47:33 +0200 | [diff] [blame] | 1282 | goto cleanup; |
Radek Krejci | cf3b7ca | 2018-08-17 09:55:06 +0200 | [diff] [blame] | 1283 | } else { |
| 1284 | /* add implicit current working directory (./) to be searched, |
| 1285 | * this directory is not searched recursively */ |
| 1286 | if (ly_set_add(dirs, wd, 0) == -1) { |
| 1287 | goto cleanup; |
| 1288 | } |
| 1289 | implicit_cwd = 1; |
Radek Krejci | 72cdfac | 2018-08-15 14:47:33 +0200 | [diff] [blame] | 1290 | } |
Radek Krejci | 72cdfac | 2018-08-15 14:47:33 +0200 | [diff] [blame] | 1291 | } |
| 1292 | if (searchpaths) { |
| 1293 | for (i = 0; searchpaths[i]; i++) { |
| 1294 | /* check for duplicities with the implicit current working directory */ |
| 1295 | if (implicit_cwd && !strcmp(dirs->set.g[0], searchpaths[i])) { |
| 1296 | implicit_cwd = 0; |
| 1297 | continue; |
| 1298 | } |
| 1299 | wd = strdup(searchpaths[i]); |
| 1300 | if (!wd) { |
| 1301 | LOGMEM(NULL); |
| 1302 | goto cleanup; |
| 1303 | } else if (ly_set_add(dirs, wd, 0) == -1) { |
| 1304 | goto cleanup; |
| 1305 | } |
| 1306 | } |
| 1307 | } |
| 1308 | wd = NULL; |
| 1309 | |
| 1310 | /* start searching */ |
| 1311 | while (dirs->number) { |
| 1312 | free(wd); |
| 1313 | free(wn); wn = NULL; |
| 1314 | |
| 1315 | dirs->number--; |
| 1316 | wd = (char *)dirs->set.g[dirs->number]; |
| 1317 | dirs->set.g[dirs->number] = NULL; |
| 1318 | LOGVRB("Searching for \"%s\" in %s.", name, wd); |
| 1319 | |
| 1320 | if (dir) { |
| 1321 | closedir(dir); |
| 1322 | } |
| 1323 | dir = opendir(wd); |
| 1324 | dir_len = strlen(wd); |
| 1325 | if (!dir) { |
| 1326 | LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno)); |
| 1327 | } else { |
| 1328 | while ((file = readdir(dir))) { |
| 1329 | if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) { |
| 1330 | /* skip . and .. */ |
| 1331 | continue; |
| 1332 | } |
| 1333 | free(wn); |
| 1334 | if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) { |
| 1335 | LOGMEM(NULL); |
| 1336 | goto cleanup; |
| 1337 | } |
| 1338 | if (stat(wn, &st) == -1) { |
| 1339 | LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)", |
| 1340 | file->d_name, wd, strerror(errno)); |
| 1341 | continue; |
| 1342 | } |
| 1343 | if (S_ISDIR(st.st_mode) && (dirs->number || !implicit_cwd)) { |
| 1344 | /* we have another subdirectory in searchpath to explore, |
| 1345 | * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */ |
| 1346 | if (ly_set_add(dirs, wn, 0) == -1) { |
| 1347 | goto cleanup; |
| 1348 | } |
| 1349 | /* continue with the next item in current directory */ |
| 1350 | wn = NULL; |
| 1351 | continue; |
| 1352 | } else if (!S_ISREG(st.st_mode)) { |
| 1353 | /* not a regular file (note that we see the target of symlinks instead of symlinks */ |
| 1354 | continue; |
| 1355 | } |
| 1356 | |
| 1357 | /* here we know that the item is a file which can contain a module */ |
| 1358 | if (strncmp(name, file->d_name, len) || |
| 1359 | (file->d_name[len] != '.' && file->d_name[len] != '@')) { |
| 1360 | /* different filename than the module we search for */ |
| 1361 | continue; |
| 1362 | } |
| 1363 | |
| 1364 | /* get type according to filename suffix */ |
| 1365 | flen = strlen(file->d_name); |
| 1366 | if (!strcmp(&file->d_name[flen - 4], ".yin")) { |
| 1367 | format_aux = LYS_IN_YIN; |
| 1368 | } else if (!strcmp(&file->d_name[flen - 5], ".yang")) { |
| 1369 | format_aux = LYS_IN_YANG; |
| 1370 | } else { |
| 1371 | /* not supportde suffix/file format */ |
| 1372 | continue; |
| 1373 | } |
| 1374 | |
| 1375 | if (revision) { |
| 1376 | /* we look for the specific revision, try to get it from the filename */ |
| 1377 | if (file->d_name[len] == '@') { |
| 1378 | /* check revision from the filename */ |
| 1379 | if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) { |
| 1380 | /* another revision */ |
| 1381 | continue; |
| 1382 | } else { |
| 1383 | /* exact revision */ |
| 1384 | free(match_name); |
| 1385 | match_name = wn; |
| 1386 | wn = NULL; |
| 1387 | match_len = dir_len + 1 + len; |
| 1388 | match_format = format_aux; |
| 1389 | goto success; |
| 1390 | } |
| 1391 | } else { |
| 1392 | /* continue trying to find exact revision match, use this only if not found */ |
| 1393 | free(match_name); |
| 1394 | match_name = wn; |
| 1395 | wn = NULL; |
| 1396 | match_len = dir_len + 1 +len; |
| 1397 | match_format = format_aux; |
| 1398 | continue; |
| 1399 | } |
| 1400 | } else { |
| 1401 | /* remember the revision and try to find the newest one */ |
| 1402 | if (match_name) { |
| 1403 | if (file->d_name[len] != '@' || lyp_check_date(NULL, &file->d_name[len + 1])) { |
| 1404 | continue; |
| 1405 | } else if (match_name[match_len] == '@' && |
| 1406 | (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) { |
| 1407 | continue; |
| 1408 | } |
| 1409 | free(match_name); |
| 1410 | } |
| 1411 | |
| 1412 | match_name = wn; |
| 1413 | wn = NULL; |
| 1414 | match_len = dir_len + 1 + len; |
| 1415 | match_format = format_aux; |
| 1416 | continue; |
| 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | success: |
| 1423 | (*localfile) = match_name; |
| 1424 | match_name = NULL; |
| 1425 | if (format) { |
| 1426 | (*format) = match_format; |
| 1427 | } |
| 1428 | ret = EXIT_SUCCESS; |
| 1429 | |
| 1430 | cleanup: |
| 1431 | free(wn); |
| 1432 | free(wd); |
| 1433 | if (dir) { |
| 1434 | closedir(dir); |
| 1435 | } |
| 1436 | free(match_name); |
| 1437 | for (u = 0; u < dirs->number; u++) { |
| 1438 | free(dirs->set.g[u]); |
| 1439 | } |
| 1440 | ly_set_free(dirs); |
| 1441 | |
| 1442 | return ret; |
| 1443 | } |
| 1444 | |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 1445 | int |
| 1446 | lys_ext_iter(struct lys_ext_instance **ext, uint8_t ext_size, uint8_t start, LYEXT_SUBSTMT substmt) |
| 1447 | { |
| 1448 | unsigned int u; |
| 1449 | |
| 1450 | for (u = start; u < ext_size; u++) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1451 | if (ext[u]->insubstmt == substmt) { |
Radek Krejci | bf28583 | 2017-01-26 16:05:41 +0100 | [diff] [blame] | 1452 | return u; |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | return -1; |
| 1457 | } |
| 1458 | |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1459 | /* |
| 1460 | * duplicate extension instance |
| 1461 | */ |
| 1462 | int |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1463 | lys_ext_dup(struct ly_ctx *ctx, struct lys_module *mod, struct lys_ext_instance **orig, uint8_t size, void *parent, |
| 1464 | 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] | 1465 | { |
| 1466 | int i; |
| 1467 | uint8_t u = 0; |
| 1468 | struct lys_ext_instance **result; |
| 1469 | struct unres_ext *info, *info_orig; |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 1470 | size_t len; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1471 | |
| 1472 | assert(new); |
| 1473 | |
| 1474 | if (!size) { |
| 1475 | if (orig) { |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1476 | LOGINT(ctx); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1477 | return EXIT_FAILURE; |
| 1478 | } |
| 1479 | (*new) = NULL; |
| 1480 | return EXIT_SUCCESS; |
| 1481 | } |
| 1482 | |
| 1483 | (*new) = result = calloc(size, sizeof *result); |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1484 | LY_CHECK_ERR_RETURN(!result, LOGMEM(ctx), EXIT_FAILURE); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1485 | for (u = 0; u < size; u++) { |
| 1486 | if (orig[u]) { |
| 1487 | /* resolved extension instance, just duplicate it */ |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 1488 | switch(orig[u]->ext_type) { |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1489 | case LYEXT_FLAG: |
| 1490 | result[u] = malloc(sizeof(struct lys_ext_instance)); |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1491 | LY_CHECK_ERR_GOTO(!result[u], LOGMEM(ctx), error); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1492 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 1493 | case LYEXT_COMPLEX: |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 1494 | len = ((struct lyext_plugin_complex*)orig[u]->def->plugin)->instance_size; |
| 1495 | result[u] = calloc(1, len); |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1496 | LY_CHECK_ERR_GOTO(!result[u], LOGMEM(ctx), error); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1497 | |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1498 | ((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] | 1499 | /* TODO duplicate data in extension instance content */ |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 1500 | 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] | 1501 | break; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1502 | } |
| 1503 | /* generic part */ |
| 1504 | result[u]->def = orig[u]->def; |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 1505 | result[u]->flags = LYEXT_OPT_CONTENT; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1506 | result[u]->arg_value = lydict_insert(ctx, orig[u]->arg_value, 0); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1507 | result[u]->parent = parent; |
| 1508 | result[u]->parent_type = parent_type; |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1509 | result[u]->insubstmt = orig[u]->insubstmt; |
| 1510 | result[u]->insubstmt_index = orig[u]->insubstmt_index; |
Radek Krejci | 8de8f61 | 2017-02-16 15:03:32 +0100 | [diff] [blame] | 1511 | result[u]->ext_type = orig[u]->ext_type; |
Radek Krejci | 7f1d47e | 2017-04-12 15:29:02 +0200 | [diff] [blame] | 1512 | result[u]->priv = NULL; |
| 1513 | result[u]->nodetype = LYS_EXT; |
| 1514 | result[u]->module = mod; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1515 | |
| 1516 | /* extensions */ |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1517 | result[u]->ext_size = orig[u]->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1518 | 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] | 1519 | LYEXT_PAR_EXTINST, &result[u]->ext, shallow, unres)) { |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1520 | goto error; |
| 1521 | } |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1522 | |
| 1523 | /* in case of shallow copy (duplication for deviation), duplicate only the link to private data |
| 1524 | * in a new copy, otherwise (grouping instantiation) do not duplicate the private data */ |
| 1525 | if (shallow) { |
| 1526 | result[u]->priv = orig[u]->priv; |
| 1527 | } |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1528 | } else { |
| 1529 | /* original extension is not yet resolved, so duplicate it in unres */ |
| 1530 | i = unres_schema_find(unres, -1, &orig, UNRES_EXT); |
| 1531 | if (i == -1) { |
| 1532 | /* extension not found in unres */ |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1533 | LOGINT(ctx); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1534 | goto error; |
| 1535 | } |
| 1536 | info_orig = unres->str_snode[i]; |
| 1537 | info = malloc(sizeof *info); |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1538 | LY_CHECK_ERR_GOTO(!info, LOGMEM(ctx), error); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1539 | info->datatype = info_orig->datatype; |
| 1540 | if (info->datatype == LYS_IN_YIN) { |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1541 | 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] | 1542 | } /* else TODO YANG */ |
| 1543 | info->parent = parent; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 1544 | info->mod = mod; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1545 | info->parent_type = parent_type; |
| 1546 | info->ext_index = u; |
| 1547 | if (unres_schema_add_node(info->mod, unres, new, UNRES_EXT, (struct lys_node *)info) == -1) { |
| 1548 | goto error; |
| 1549 | } |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | return EXIT_SUCCESS; |
| 1554 | |
| 1555 | error: |
| 1556 | (*new) = NULL; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1557 | lys_extension_instances_free(ctx, result, u, NULL); |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1558 | return EXIT_FAILURE; |
| 1559 | } |
| 1560 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1561 | static struct lys_restr * |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1562 | 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] | 1563 | { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1564 | struct lys_restr *result; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1565 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1566 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1567 | if (!size) { |
| 1568 | return NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1569 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1570 | |
| 1571 | result = calloc(size, sizeof *result); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1572 | LY_CHECK_ERR_RETURN(!result, LOGMEM(mod->ctx), NULL); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1573 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1574 | for (i = 0; i < size; i++) { |
Radek Krejci | 77f22b2 | 2017-01-17 15:23:03 +0100 | [diff] [blame] | 1575 | result[i].ext_size = old[i].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1576 | 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] | 1577 | result[i].expr = lydict_insert(mod->ctx, old[i].expr, 0); |
| 1578 | result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0); |
| 1579 | result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0); |
| 1580 | result[i].eapptag = lydict_insert(mod->ctx, old[i].eapptag, 0); |
| 1581 | result[i].emsg = lydict_insert(mod->ctx, old[i].emsg, 0); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | return result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1585 | } |
| 1586 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1587 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1588 | lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr, |
| 1589 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1590 | { |
| 1591 | assert(ctx); |
| 1592 | if (!restr) { |
| 1593 | return; |
| 1594 | } |
| 1595 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1596 | lys_extension_instances_free(ctx, restr->ext, restr->ext_size, private_destructor); |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1597 | lydict_remove(ctx, restr->expr); |
| 1598 | lydict_remove(ctx, restr->dsc); |
| 1599 | lydict_remove(ctx, restr->ref); |
| 1600 | lydict_remove(ctx, restr->eapptag); |
| 1601 | lydict_remove(ctx, restr->emsg); |
| 1602 | } |
| 1603 | |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 1604 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1605 | 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] | 1606 | int shallow, void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 1607 | { |
| 1608 | uint8_t i; |
| 1609 | |
| 1610 | for (i = 0; i < iffeature_size; ++i) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1611 | 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] | 1612 | if (!shallow) { |
Frank Rimpler | 2a503f5 | 2017-09-12 15:21:18 +0000 | [diff] [blame] | 1613 | free(iffeature[i].expr); |
| 1614 | free(iffeature[i].features); |
| 1615 | } |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 1616 | } |
| 1617 | free(iffeature); |
| 1618 | } |
| 1619 | |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1620 | static int |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1621 | 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] | 1622 | 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] | 1623 | { |
| 1624 | int i; |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1625 | unsigned int u; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1626 | |
| 1627 | switch (base) { |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1628 | case LY_TYPE_BINARY: |
| 1629 | if (old->info.binary.length) { |
| 1630 | new->info.binary.length = lys_restr_dup(mod, old->info.binary.length, 1, shallow, unres); |
| 1631 | } |
| 1632 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1633 | |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1634 | case LY_TYPE_BITS: |
| 1635 | new->info.bits.count = old->info.bits.count; |
| 1636 | if (new->info.bits.count) { |
| 1637 | 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] | 1638 | LY_CHECK_ERR_RETURN(!new->info.bits.bit, LOGMEM(mod->ctx), -1); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1639 | |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1640 | for (u = 0; u < new->info.bits.count; u++) { |
| 1641 | new->info.bits.bit[u].name = lydict_insert(mod->ctx, old->info.bits.bit[u].name, 0); |
| 1642 | new->info.bits.bit[u].dsc = lydict_insert(mod->ctx, old->info.bits.bit[u].dsc, 0); |
| 1643 | new->info.bits.bit[u].ref = lydict_insert(mod->ctx, old->info.bits.bit[u].ref, 0); |
| 1644 | new->info.bits.bit[u].flags = old->info.bits.bit[u].flags; |
| 1645 | new->info.bits.bit[u].pos = old->info.bits.bit[u].pos; |
| 1646 | 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] | 1647 | 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] | 1648 | &new->info.bits.bit[u], LYEXT_PAR_TYPE_BIT, |
| 1649 | &new->info.bits.bit[u].ext, shallow, unres)) { |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1650 | return -1; |
| 1651 | } |
| 1652 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1653 | } |
| 1654 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1655 | |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1656 | case LY_TYPE_DEC64: |
| 1657 | new->info.dec64.dig = old->info.dec64.dig; |
| 1658 | new->info.dec64.div = old->info.dec64.div; |
| 1659 | if (old->info.dec64.range) { |
| 1660 | new->info.dec64.range = lys_restr_dup(mod, old->info.dec64.range, 1, shallow, unres); |
| 1661 | } |
| 1662 | break; |
| 1663 | |
| 1664 | case LY_TYPE_ENUM: |
| 1665 | new->info.enums.count = old->info.enums.count; |
| 1666 | if (new->info.enums.count) { |
| 1667 | 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] | 1668 | LY_CHECK_ERR_RETURN(!new->info.enums.enm, LOGMEM(mod->ctx), -1); |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1669 | |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1670 | for (u = 0; u < new->info.enums.count; u++) { |
| 1671 | new->info.enums.enm[u].name = lydict_insert(mod->ctx, old->info.enums.enm[u].name, 0); |
| 1672 | new->info.enums.enm[u].dsc = lydict_insert(mod->ctx, old->info.enums.enm[u].dsc, 0); |
| 1673 | new->info.enums.enm[u].ref = lydict_insert(mod->ctx, old->info.enums.enm[u].ref, 0); |
| 1674 | new->info.enums.enm[u].flags = old->info.enums.enm[u].flags; |
| 1675 | new->info.enums.enm[u].value = old->info.enums.enm[u].value; |
| 1676 | 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] | 1677 | 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] | 1678 | &new->info.enums.enm[u], LYEXT_PAR_TYPE_ENUM, |
| 1679 | &new->info.enums.enm[u].ext, shallow, unres)) { |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1680 | return -1; |
| 1681 | } |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1682 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1683 | } |
| 1684 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1685 | |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1686 | case LY_TYPE_IDENT: |
| 1687 | new->info.ident.count = old->info.ident.count; |
| 1688 | if (old->info.ident.count) { |
| 1689 | 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] | 1690 | LY_CHECK_ERR_RETURN(!new->info.ident.ref, LOGMEM(mod->ctx), -1); |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1691 | memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref); |
| 1692 | } else { |
| 1693 | /* there can be several unresolved base identities, duplicate them all */ |
| 1694 | i = -1; |
| 1695 | do { |
| 1696 | i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF); |
| 1697 | if (i != -1) { |
| 1698 | 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] | 1699 | return -1; |
| 1700 | } |
| 1701 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1702 | --i; |
| 1703 | } while (i > -1); |
| 1704 | } |
| 1705 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1706 | |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1707 | case LY_TYPE_INST: |
| 1708 | new->info.inst.req = old->info.inst.req; |
| 1709 | break; |
| 1710 | |
| 1711 | case LY_TYPE_INT8: |
| 1712 | case LY_TYPE_INT16: |
| 1713 | case LY_TYPE_INT32: |
| 1714 | case LY_TYPE_INT64: |
| 1715 | case LY_TYPE_UINT8: |
| 1716 | case LY_TYPE_UINT16: |
| 1717 | case LY_TYPE_UINT32: |
| 1718 | case LY_TYPE_UINT64: |
| 1719 | if (old->info.num.range) { |
| 1720 | new->info.num.range = lys_restr_dup(mod, old->info.num.range, 1, shallow, unres); |
| 1721 | } |
| 1722 | break; |
| 1723 | |
| 1724 | case LY_TYPE_LEAFREF: |
| 1725 | if (old->info.lref.path) { |
| 1726 | new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0); |
| 1727 | if (!in_grp && unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent) == -1) { |
| 1728 | return -1; |
| 1729 | } |
| 1730 | } |
| 1731 | break; |
| 1732 | |
| 1733 | case LY_TYPE_STRING: |
| 1734 | if (old->info.str.length) { |
| 1735 | new->info.str.length = lys_restr_dup(mod, old->info.str.length, 1, shallow, unres); |
| 1736 | } |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 1737 | if (old->info.str.pat_count) { |
| 1738 | new->info.str.patterns = lys_restr_dup(mod, old->info.str.patterns, old->info.str.pat_count, shallow, unres); |
| 1739 | new->info.str.pat_count = old->info.str.pat_count; |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 1740 | #ifdef LY_ENABLED_CACHE |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 1741 | if (!in_grp) { |
| 1742 | 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] | 1743 | 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] | 1744 | for (u = 0; u < new->info.str.pat_count; u++) { |
Michal Vasko | a26db30 | 2018-02-14 15:22:10 +0100 | [diff] [blame] | 1745 | 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] | 1746 | (pcre**)&new->info.str.patterns_pcre[2 * u], |
| 1747 | (pcre_extra**)&new->info.str.patterns_pcre[2 * u + 1])) { |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 1748 | free(new->info.str.patterns_pcre); |
| 1749 | new->info.str.patterns_pcre = NULL; |
| 1750 | return -1; |
| 1751 | } |
| 1752 | } |
| 1753 | } |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 1754 | #endif |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 1755 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1756 | break; |
| 1757 | |
| 1758 | case LY_TYPE_UNION: |
| 1759 | new->info.uni.has_ptr_type = old->info.uni.has_ptr_type; |
| 1760 | new->info.uni.count = old->info.uni.count; |
| 1761 | if (new->info.uni.count) { |
| 1762 | 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] | 1763 | LY_CHECK_ERR_RETURN(!new->info.uni.types, LOGMEM(mod->ctx), -1); |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1764 | |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1765 | for (u = 0; u < new->info.uni.count; u++) { |
| 1766 | 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] | 1767 | shallow, unres)) { |
| 1768 | return -1; |
| 1769 | } |
| 1770 | } |
| 1771 | } |
| 1772 | break; |
| 1773 | |
| 1774 | default: |
| 1775 | /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */ |
| 1776 | break; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1777 | } |
Michal Vasko | b4ab1cb | 2017-06-30 13:14:54 +0200 | [diff] [blame] | 1778 | |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1779 | return EXIT_SUCCESS; |
| 1780 | } |
| 1781 | |
| 1782 | struct yang_type * |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1783 | 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] | 1784 | int in_grp, int shallow, struct unres_schema *unres) |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1785 | { |
| 1786 | struct yang_type *new; |
| 1787 | |
| 1788 | new = calloc(1, sizeof *new); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1789 | LY_CHECK_ERR_RETURN(!new, LOGMEM(module->ctx), NULL); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1790 | new->flags = old->flags; |
| 1791 | new->base = old->base; |
Pavol Vican | 6658629 | 2016-04-07 11:38:52 +0200 | [diff] [blame] | 1792 | new->name = lydict_insert(module->ctx, old->name, 0); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1793 | new->type = type; |
| 1794 | if (!new->name) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1795 | LOGMEM(module->ctx); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1796 | goto error; |
| 1797 | } |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1798 | 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] | 1799 | new->type->base = new->base; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1800 | lys_type_free(module->ctx, new->type, NULL); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1801 | memset(&new->type->info, 0, sizeof new->type->info); |
| 1802 | goto error; |
| 1803 | } |
| 1804 | return new; |
| 1805 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1806 | error: |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1807 | free(new); |
| 1808 | return NULL; |
| 1809 | } |
| 1810 | |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1811 | int |
| 1812 | lys_copy_union_leafrefs(struct lys_module *mod, struct lys_node *parent, struct lys_type *type, struct lys_type *prev_new, |
| 1813 | struct unres_schema *unres) |
| 1814 | { |
| 1815 | struct lys_type new; |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1816 | unsigned int i, top_type; |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1817 | struct lys_ext_instance **ext; |
| 1818 | uint8_t ext_size; |
| 1819 | void *reloc; |
| 1820 | |
| 1821 | if (!prev_new) { |
| 1822 | /* this is the "top-level" type, meaning it is a real type and no typedef directly above */ |
| 1823 | top_type = 1; |
| 1824 | |
| 1825 | memset(&new, 0, sizeof new); |
| 1826 | |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1827 | new.base = type->base; |
| 1828 | new.parent = (struct lys_tpdf *)parent; |
| 1829 | |
| 1830 | prev_new = &new; |
| 1831 | } else { |
| 1832 | /* this is not top-level type, just a type of a typedef */ |
| 1833 | top_type = 0; |
| 1834 | } |
| 1835 | |
Radek Krejci | 9c5cb6d | 2017-08-09 11:15:23 +0200 | [diff] [blame] | 1836 | assert(type->der); |
| 1837 | if (type->der->module) { |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1838 | /* typedef, skip it, but keep the extensions */ |
| 1839 | ext_size = type->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 1840 | 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] | 1841 | return -1; |
| 1842 | } |
| 1843 | if (prev_new->ext) { |
| 1844 | 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] | 1845 | LY_CHECK_ERR_RETURN(!reloc, LOGMEM(mod->ctx), -1); |
Radek Krejci | 70379e2 | 2017-08-09 11:21:07 +0200 | [diff] [blame] | 1846 | prev_new->ext = reloc; |
Michal Vasko | c5c55ca | 2017-06-30 13:15:18 +0200 | [diff] [blame] | 1847 | |
| 1848 | memcpy(prev_new->ext + prev_new->ext_size, ext, ext_size * sizeof *ext); |
| 1849 | free(ext); |
| 1850 | |
| 1851 | prev_new->ext_size += ext_size; |
| 1852 | } else { |
| 1853 | prev_new->ext = ext; |
| 1854 | prev_new->ext_size = ext_size; |
| 1855 | } |
| 1856 | |
| 1857 | if (lys_copy_union_leafrefs(mod, parent, &type->der->type, prev_new, unres)) { |
| 1858 | return -1; |
| 1859 | } |
| 1860 | } else { |
| 1861 | /* type, just make a deep copy */ |
| 1862 | switch (type->base) { |
| 1863 | case LY_TYPE_UNION: |
| 1864 | prev_new->info.uni.has_ptr_type = type->info.uni.has_ptr_type; |
| 1865 | prev_new->info.uni.count = type->info.uni.count; |
| 1866 | /* this cannot be a typedef anymore */ |
| 1867 | assert(prev_new->info.uni.count); |
| 1868 | |
| 1869 | 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] | 1870 | 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] | 1871 | |
| 1872 | for (i = 0; i < prev_new->info.uni.count; i++) { |
| 1873 | if (lys_copy_union_leafrefs(mod, parent, &(type->info.uni.types[i]), &(prev_new->info.uni.types[i]), unres)) { |
| 1874 | return -1; |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | prev_new->der = type->der; |
| 1879 | break; |
| 1880 | default: |
| 1881 | if (lys_type_dup(mod, parent, prev_new, type, 0, 0, unres)) { |
| 1882 | return -1; |
| 1883 | } |
| 1884 | break; |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | if (top_type) { |
| 1889 | memcpy(type, prev_new, sizeof *type); |
| 1890 | } |
| 1891 | return EXIT_SUCCESS; |
| 1892 | } |
| 1893 | |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1894 | API const void * |
| 1895 | lys_ext_instance_substmt(const struct lys_ext_instance *ext) |
| 1896 | { |
| 1897 | if (!ext) { |
| 1898 | return NULL; |
| 1899 | } |
| 1900 | |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1901 | switch (ext->insubstmt) { |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1902 | case LYEXT_SUBSTMT_SELF: |
| 1903 | case LYEXT_SUBSTMT_MODIFIER: |
| 1904 | case LYEXT_SUBSTMT_VERSION: |
| 1905 | return NULL; |
| 1906 | case LYEXT_SUBSTMT_ARGUMENT: |
| 1907 | if (ext->parent_type == LYEXT_PAR_EXT) { |
| 1908 | return ((struct lys_ext_instance*)ext->parent)->arg_value; |
| 1909 | } |
| 1910 | break; |
| 1911 | case LYEXT_SUBSTMT_BASE: |
| 1912 | if (ext->parent_type == LYEXT_PAR_TYPE) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1913 | return ((struct lys_type*)ext->parent)->info.ident.ref[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1914 | } else if (ext->parent_type == LYEXT_PAR_IDENT) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1915 | return ((struct lys_ident*)ext->parent)->base[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1916 | } |
| 1917 | break; |
| 1918 | case LYEXT_SUBSTMT_BELONGSTO: |
| 1919 | if (ext->parent_type == LYEXT_PAR_MODULE && ((struct lys_module*)ext->parent)->type) { |
| 1920 | return ((struct lys_submodule*)ext->parent)->belongsto; |
| 1921 | } |
| 1922 | break; |
| 1923 | case LYEXT_SUBSTMT_CONFIG: |
| 1924 | case LYEXT_SUBSTMT_MANDATORY: |
| 1925 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 1926 | return &((struct lys_node*)ext->parent)->flags; |
| 1927 | } else if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
| 1928 | return &((struct lys_deviate*)ext->parent)->flags; |
| 1929 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 1930 | return &((struct lys_refine*)ext->parent)->flags; |
| 1931 | } |
| 1932 | break; |
| 1933 | case LYEXT_SUBSTMT_CONTACT: |
| 1934 | if (ext->parent_type == LYEXT_PAR_MODULE) { |
| 1935 | return ((struct lys_module*)ext->parent)->contact; |
| 1936 | } |
| 1937 | break; |
| 1938 | case LYEXT_SUBSTMT_DEFAULT: |
| 1939 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 1940 | switch (((struct lys_node*)ext->parent)->nodetype) { |
| 1941 | case LYS_LEAF: |
| 1942 | case LYS_LEAFLIST: |
| 1943 | /* in case of leaf, the index is supposed to be 0, so it will return the |
| 1944 | * correct pointer despite the leaf structure does not have dflt as array */ |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1945 | return ((struct lys_node_leaflist*)ext->parent)->dflt[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1946 | case LYS_CHOICE: |
| 1947 | return ((struct lys_node_choice*)ext->parent)->dflt; |
| 1948 | default: |
| 1949 | /* internal error */ |
| 1950 | break; |
| 1951 | } |
| 1952 | } else if (ext->parent_type == LYEXT_PAR_TPDF) { |
| 1953 | return ((struct lys_tpdf*)ext->parent)->dflt; |
| 1954 | } else if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1955 | return ((struct lys_deviate*)ext->parent)->dflt[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1956 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 1957 | return &((struct lys_refine*)ext->parent)->dflt[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1958 | } |
| 1959 | break; |
| 1960 | case LYEXT_SUBSTMT_DESCRIPTION: |
| 1961 | switch (ext->parent_type) { |
| 1962 | case LYEXT_PAR_NODE: |
| 1963 | return ((struct lys_node*)ext->parent)->dsc; |
| 1964 | case LYEXT_PAR_MODULE: |
| 1965 | return ((struct lys_module*)ext->parent)->dsc; |
| 1966 | case LYEXT_PAR_IMPORT: |
| 1967 | return ((struct lys_import*)ext->parent)->dsc; |
| 1968 | case LYEXT_PAR_INCLUDE: |
| 1969 | return ((struct lys_include*)ext->parent)->dsc; |
| 1970 | case LYEXT_PAR_EXT: |
| 1971 | return ((struct lys_ext*)ext->parent)->dsc; |
| 1972 | case LYEXT_PAR_FEATURE: |
| 1973 | return ((struct lys_feature*)ext->parent)->dsc; |
| 1974 | case LYEXT_PAR_TPDF: |
| 1975 | return ((struct lys_tpdf*)ext->parent)->dsc; |
| 1976 | case LYEXT_PAR_TYPE_BIT: |
| 1977 | return ((struct lys_type_bit*)ext->parent)->dsc; |
| 1978 | case LYEXT_PAR_TYPE_ENUM: |
| 1979 | return ((struct lys_type_enum*)ext->parent)->dsc; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1980 | case LYEXT_PAR_RESTR: |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1981 | return ((struct lys_restr*)ext->parent)->dsc; |
| 1982 | case LYEXT_PAR_WHEN: |
| 1983 | return ((struct lys_when*)ext->parent)->dsc; |
| 1984 | case LYEXT_PAR_IDENT: |
| 1985 | return ((struct lys_ident*)ext->parent)->dsc; |
| 1986 | case LYEXT_PAR_DEVIATION: |
| 1987 | return ((struct lys_deviation*)ext->parent)->dsc; |
| 1988 | case LYEXT_PAR_REVISION: |
| 1989 | return ((struct lys_revision*)ext->parent)->dsc; |
| 1990 | case LYEXT_PAR_REFINE: |
| 1991 | return ((struct lys_refine*)ext->parent)->dsc; |
| 1992 | default: |
| 1993 | break; |
| 1994 | } |
| 1995 | break; |
| 1996 | case LYEXT_SUBSTMT_ERRTAG: |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 1997 | if (ext->parent_type == LYEXT_PAR_RESTR) { |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 1998 | return ((struct lys_restr*)ext->parent)->eapptag; |
| 1999 | } |
| 2000 | break; |
| 2001 | case LYEXT_SUBSTMT_ERRMSG: |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 2002 | if (ext->parent_type == LYEXT_PAR_RESTR) { |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 2003 | return ((struct lys_restr*)ext->parent)->emsg; |
| 2004 | } |
| 2005 | break; |
| 2006 | case LYEXT_SUBSTMT_DIGITS: |
| 2007 | if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_DEC64) { |
| 2008 | return &((struct lys_type*)ext->parent)->info.dec64.dig; |
| 2009 | } |
| 2010 | break; |
| 2011 | case LYEXT_SUBSTMT_KEY: |
| 2012 | if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) { |
| 2013 | return ((struct lys_node_list*)ext->parent)->keys; |
| 2014 | } |
| 2015 | break; |
| 2016 | case LYEXT_SUBSTMT_MAX: |
| 2017 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 2018 | if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) { |
| 2019 | return &((struct lys_node_list*)ext->parent)->max; |
| 2020 | } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) { |
| 2021 | return &((struct lys_node_leaflist*)ext->parent)->max; |
| 2022 | } |
| 2023 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 2024 | return &((struct lys_refine*)ext->parent)->mod.list.max; |
| 2025 | } |
| 2026 | break; |
| 2027 | case LYEXT_SUBSTMT_MIN: |
| 2028 | if (ext->parent_type == LYEXT_PAR_NODE) { |
| 2029 | if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) { |
| 2030 | return &((struct lys_node_list*)ext->parent)->min; |
| 2031 | } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) { |
| 2032 | return &((struct lys_node_leaflist*)ext->parent)->min; |
| 2033 | } |
| 2034 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 2035 | return &((struct lys_refine*)ext->parent)->mod.list.min; |
| 2036 | } |
| 2037 | break; |
| 2038 | case LYEXT_SUBSTMT_NAMESPACE: |
| 2039 | if (ext->parent_type == LYEXT_PAR_MODULE && !((struct lys_module*)ext->parent)->type) { |
| 2040 | return ((struct lys_module*)ext->parent)->ns; |
| 2041 | } |
| 2042 | break; |
| 2043 | case LYEXT_SUBSTMT_ORDEREDBY: |
| 2044 | if (ext->parent_type == LYEXT_PAR_NODE && |
| 2045 | (((struct lys_node*)ext->parent)->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 2046 | return &((struct lys_node_list*)ext->parent)->flags; |
| 2047 | } |
| 2048 | break; |
| 2049 | case LYEXT_SUBSTMT_ORGANIZATION: |
| 2050 | if (ext->parent_type == LYEXT_PAR_MODULE) { |
| 2051 | return ((struct lys_module*)ext->parent)->org; |
| 2052 | } |
| 2053 | break; |
| 2054 | case LYEXT_SUBSTMT_PATH: |
| 2055 | if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) { |
| 2056 | return ((struct lys_type*)ext->parent)->info.lref.path; |
| 2057 | } |
| 2058 | break; |
| 2059 | case LYEXT_SUBSTMT_POSITION: |
| 2060 | if (ext->parent_type == LYEXT_PAR_TYPE_BIT) { |
| 2061 | return &((struct lys_type_bit*)ext->parent)->pos; |
| 2062 | } |
| 2063 | break; |
| 2064 | case LYEXT_SUBSTMT_PREFIX: |
| 2065 | if (ext->parent_type == LYEXT_PAR_MODULE) { |
| 2066 | /* covers also lys_submodule */ |
| 2067 | return ((struct lys_module*)ext->parent)->prefix; |
| 2068 | } else if (ext->parent_type == LYEXT_PAR_IMPORT) { |
| 2069 | return ((struct lys_import*)ext->parent)->prefix; |
| 2070 | } |
| 2071 | break; |
| 2072 | case LYEXT_SUBSTMT_PRESENCE: |
| 2073 | if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_CONTAINER) { |
| 2074 | return ((struct lys_node_container*)ext->parent)->presence; |
| 2075 | } else if (ext->parent_type == LYEXT_PAR_REFINE) { |
| 2076 | return ((struct lys_refine*)ext->parent)->mod.presence; |
| 2077 | } |
| 2078 | break; |
| 2079 | case LYEXT_SUBSTMT_REFERENCE: |
| 2080 | switch (ext->parent_type) { |
| 2081 | case LYEXT_PAR_NODE: |
| 2082 | return ((struct lys_node*)ext->parent)->ref; |
| 2083 | case LYEXT_PAR_MODULE: |
| 2084 | return ((struct lys_module*)ext->parent)->ref; |
| 2085 | case LYEXT_PAR_IMPORT: |
| 2086 | return ((struct lys_import*)ext->parent)->ref; |
| 2087 | case LYEXT_PAR_INCLUDE: |
| 2088 | return ((struct lys_include*)ext->parent)->ref; |
| 2089 | case LYEXT_PAR_EXT: |
| 2090 | return ((struct lys_ext*)ext->parent)->ref; |
| 2091 | case LYEXT_PAR_FEATURE: |
| 2092 | return ((struct lys_feature*)ext->parent)->ref; |
| 2093 | case LYEXT_PAR_TPDF: |
| 2094 | return ((struct lys_tpdf*)ext->parent)->ref; |
| 2095 | case LYEXT_PAR_TYPE_BIT: |
| 2096 | return ((struct lys_type_bit*)ext->parent)->ref; |
| 2097 | case LYEXT_PAR_TYPE_ENUM: |
| 2098 | return ((struct lys_type_enum*)ext->parent)->ref; |
Radek Krejci | fdc0d70 | 2017-01-23 15:58:38 +0100 | [diff] [blame] | 2099 | case LYEXT_PAR_RESTR: |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 2100 | return ((struct lys_restr*)ext->parent)->ref; |
| 2101 | case LYEXT_PAR_WHEN: |
| 2102 | return ((struct lys_when*)ext->parent)->ref; |
| 2103 | case LYEXT_PAR_IDENT: |
| 2104 | return ((struct lys_ident*)ext->parent)->ref; |
| 2105 | case LYEXT_PAR_DEVIATION: |
| 2106 | return ((struct lys_deviation*)ext->parent)->ref; |
| 2107 | case LYEXT_PAR_REVISION: |
| 2108 | return ((struct lys_revision*)ext->parent)->ref; |
| 2109 | case LYEXT_PAR_REFINE: |
| 2110 | return ((struct lys_refine*)ext->parent)->ref; |
| 2111 | default: |
| 2112 | break; |
| 2113 | } |
| 2114 | break; |
Radek Krejci | be33639 | 2017-02-07 10:54:24 +0100 | [diff] [blame] | 2115 | case LYEXT_SUBSTMT_REQINSTANCE: |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 2116 | if (ext->parent_type == LYEXT_PAR_TYPE) { |
| 2117 | if (((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) { |
| 2118 | return &((struct lys_type*)ext->parent)->info.lref.req; |
| 2119 | } else if (((struct lys_type*)ext->parent)->base == LY_TYPE_INST) { |
| 2120 | return &((struct lys_type*)ext->parent)->info.inst.req; |
| 2121 | } |
| 2122 | } |
| 2123 | break; |
| 2124 | case LYEXT_SUBSTMT_REVISIONDATE: |
| 2125 | if (ext->parent_type == LYEXT_PAR_IMPORT) { |
| 2126 | return ((struct lys_import*)ext->parent)->rev; |
| 2127 | } else if (ext->parent_type == LYEXT_PAR_INCLUDE) { |
| 2128 | return ((struct lys_include*)ext->parent)->rev; |
| 2129 | } |
| 2130 | break; |
| 2131 | case LYEXT_SUBSTMT_STATUS: |
| 2132 | switch (ext->parent_type) { |
| 2133 | case LYEXT_PAR_NODE: |
| 2134 | case LYEXT_PAR_IDENT: |
| 2135 | case LYEXT_PAR_TPDF: |
| 2136 | case LYEXT_PAR_EXT: |
| 2137 | case LYEXT_PAR_FEATURE: |
| 2138 | case LYEXT_PAR_TYPE_ENUM: |
| 2139 | case LYEXT_PAR_TYPE_BIT: |
| 2140 | /* in all structures the flags member is at the same offset */ |
| 2141 | return &((struct lys_node*)ext->parent)->flags; |
| 2142 | default: |
| 2143 | break; |
| 2144 | } |
| 2145 | break; |
| 2146 | case LYEXT_SUBSTMT_UNIQUE: |
| 2147 | if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 2148 | return &((struct lys_deviate*)ext->parent)->unique[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 2149 | } 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] | 2150 | return &((struct lys_node_list*)ext->parent)->unique[ext->insubstmt_index]; |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 2151 | } |
| 2152 | break; |
| 2153 | case LYEXT_SUBSTMT_UNITS: |
| 2154 | if (ext->parent_type == LYEXT_PAR_NODE && |
| 2155 | (((struct lys_node*)ext->parent)->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2156 | /* units is at the same offset in both lys_node_leaf and lys_node_leaflist */ |
| 2157 | return ((struct lys_node_leaf*)ext->parent)->units; |
| 2158 | } else if (ext->parent_type == LYEXT_PAR_TPDF) { |
| 2159 | return ((struct lys_tpdf*)ext->parent)->units; |
| 2160 | } else if (ext->parent_type == LYEXT_PAR_DEVIATE) { |
| 2161 | return ((struct lys_deviate*)ext->parent)->units; |
| 2162 | } |
| 2163 | break; |
| 2164 | case LYEXT_SUBSTMT_VALUE: |
| 2165 | if (ext->parent_type == LYEXT_PAR_TYPE_ENUM) { |
| 2166 | return &((struct lys_type_enum*)ext->parent)->value; |
| 2167 | } |
| 2168 | break; |
| 2169 | case LYEXT_SUBSTMT_YINELEM: |
| 2170 | if (ext->parent_type == LYEXT_PAR_EXT) { |
| 2171 | return &((struct lys_ext*)ext->parent)->flags; |
| 2172 | } |
| 2173 | break; |
| 2174 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2175 | LOGINT(ext->module->ctx); |
Radek Krejci | 43ce4b7 | 2017-01-04 11:02:38 +0100 | [diff] [blame] | 2176 | return NULL; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2177 | } |
| 2178 | |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 2179 | static int |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2180 | 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] | 2181 | int in_grp, int shallow, struct unres_schema *unres) |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 2182 | { |
| 2183 | int i; |
| 2184 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 2185 | new->base = old->base; |
| 2186 | new->der = old->der; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 2187 | new->parent = (struct lys_tpdf *)parent; |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 2188 | new->ext_size = old->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 2189 | 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] | 2190 | return -1; |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2191 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 2192 | |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 2193 | i = unres_schema_find(unres, -1, old, UNRES_TYPE_DER); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2194 | if (i != -1) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 2195 | /* HACK (serious one) for unres */ |
| 2196 | /* nothing else we can do but duplicate it immediately */ |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 2197 | if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2198 | new->der = (struct lys_tpdf *)lys_yang_type_dup(mod, parent, (struct yang_type *)old->der, new, in_grp, |
| 2199 | shallow, unres); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 2200 | } else { |
| 2201 | new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1); |
| 2202 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 2203 | /* all these unres additions can fail even though they did not before */ |
Michal Vasko | 1c00717 | 2017-03-10 10:20:44 +0100 | [diff] [blame] | 2204 | 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] | 2205 | return -1; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2206 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 2207 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2208 | } |
| 2209 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2210 | 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] | 2211 | } |
| 2212 | |
| 2213 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2214 | lys_type_free(struct ly_ctx *ctx, struct lys_type *type, |
| 2215 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2216 | { |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 2217 | unsigned int i; |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2218 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2219 | assert(ctx); |
| 2220 | if (!type) { |
| 2221 | return; |
| 2222 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2223 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2224 | lys_extension_instances_free(ctx, type->ext, type->ext_size, private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2225 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2226 | switch (type->base) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 2227 | case LY_TYPE_BINARY: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2228 | lys_restr_free(ctx, type->info.binary.length, private_destructor); |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 2229 | free(type->info.binary.length); |
| 2230 | break; |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 2231 | case LY_TYPE_BITS: |
| 2232 | for (i = 0; i < type->info.bits.count; i++) { |
| 2233 | lydict_remove(ctx, type->info.bits.bit[i].name); |
| 2234 | lydict_remove(ctx, type->info.bits.bit[i].dsc); |
| 2235 | lydict_remove(ctx, type->info.bits.bit[i].ref); |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2236 | 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] | 2237 | private_destructor); |
| 2238 | lys_extension_instances_free(ctx, type->info.bits.bit[i].ext, type->info.bits.bit[i].ext_size, |
| 2239 | private_destructor); |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 2240 | } |
| 2241 | free(type->info.bits.bit); |
| 2242 | break; |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 2243 | |
| 2244 | case LY_TYPE_DEC64: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2245 | lys_restr_free(ctx, type->info.dec64.range, private_destructor); |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 2246 | free(type->info.dec64.range); |
| 2247 | break; |
| 2248 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2249 | case LY_TYPE_ENUM: |
| 2250 | for (i = 0; i < type->info.enums.count; i++) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2251 | lydict_remove(ctx, type->info.enums.enm[i].name); |
| 2252 | lydict_remove(ctx, type->info.enums.enm[i].dsc); |
| 2253 | lydict_remove(ctx, type->info.enums.enm[i].ref); |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2254 | 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] | 2255 | private_destructor); |
| 2256 | lys_extension_instances_free(ctx, type->info.enums.enm[i].ext, type->info.enums.enm[i].ext_size, |
| 2257 | private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2258 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2259 | free(type->info.enums.enm); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2260 | break; |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 2261 | |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 2262 | case LY_TYPE_INT8: |
| 2263 | case LY_TYPE_INT16: |
| 2264 | case LY_TYPE_INT32: |
| 2265 | case LY_TYPE_INT64: |
| 2266 | case LY_TYPE_UINT8: |
| 2267 | case LY_TYPE_UINT16: |
| 2268 | case LY_TYPE_UINT32: |
| 2269 | case LY_TYPE_UINT64: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2270 | lys_restr_free(ctx, type->info.num.range, private_destructor); |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 2271 | free(type->info.num.range); |
| 2272 | break; |
| 2273 | |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 2274 | case LY_TYPE_LEAFREF: |
| 2275 | lydict_remove(ctx, type->info.lref.path); |
| 2276 | break; |
| 2277 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 2278 | case LY_TYPE_STRING: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2279 | lys_restr_free(ctx, type->info.str.length, private_destructor); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 2280 | free(type->info.str.length); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 2281 | for (i = 0; i < type->info.str.pat_count; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2282 | lys_restr_free(ctx, &type->info.str.patterns[i], private_destructor); |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 2283 | #ifdef LY_ENABLED_CACHE |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 2284 | if (type->info.str.patterns_pcre) { |
| 2285 | pcre_free((pcre*)type->info.str.patterns_pcre[2 * i]); |
| 2286 | pcre_free_study((pcre_extra*)type->info.str.patterns_pcre[2 * i + 1]); |
| 2287 | } |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 2288 | #endif |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 2289 | } |
| 2290 | free(type->info.str.patterns); |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 2291 | #ifdef LY_ENABLED_CACHE |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 2292 | free(type->info.str.patterns_pcre); |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 2293 | #endif |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 2294 | break; |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 2295 | |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 2296 | case LY_TYPE_UNION: |
| 2297 | for (i = 0; i < type->info.uni.count; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2298 | lys_type_free(ctx, &type->info.uni.types[i], private_destructor); |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 2299 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2300 | free(type->info.uni.types); |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 2301 | break; |
| 2302 | |
Michal Vasko | d328219 | 2016-09-05 11:27:57 +0200 | [diff] [blame] | 2303 | case LY_TYPE_IDENT: |
| 2304 | free(type->info.ident.ref); |
| 2305 | break; |
| 2306 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2307 | default: |
Michal Vasko | d328219 | 2016-09-05 11:27:57 +0200 | [diff] [blame] | 2308 | /* 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] | 2309 | break; |
| 2310 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2311 | } |
| 2312 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2313 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2314 | lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf, |
| 2315 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2316 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2317 | assert(ctx); |
| 2318 | if (!tpdf) { |
| 2319 | return; |
| 2320 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2321 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2322 | lydict_remove(ctx, tpdf->name); |
| 2323 | lydict_remove(ctx, tpdf->dsc); |
| 2324 | lydict_remove(ctx, tpdf->ref); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2325 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2326 | lys_type_free(ctx, &tpdf->type, private_destructor); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2327 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2328 | lydict_remove(ctx, tpdf->units); |
| 2329 | lydict_remove(ctx, tpdf->dflt); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2330 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2331 | lys_extension_instances_free(ctx, tpdf->ext, tpdf->ext_size, private_destructor); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2332 | } |
| 2333 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2334 | static struct lys_when * |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2335 | 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] | 2336 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2337 | struct lys_when *new; |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2338 | |
| 2339 | if (!old) { |
| 2340 | return NULL; |
| 2341 | } |
| 2342 | |
| 2343 | new = calloc(1, sizeof *new); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2344 | LY_CHECK_ERR_RETURN(!new, LOGMEM(mod->ctx), NULL); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 2345 | new->cond = lydict_insert(mod->ctx, old->cond, 0); |
| 2346 | new->dsc = lydict_insert(mod->ctx, old->dsc, 0); |
| 2347 | new->ref = lydict_insert(mod->ctx, old->ref, 0); |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 2348 | new->ext_size = old->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 2349 | 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] | 2350 | |
| 2351 | return new; |
| 2352 | } |
| 2353 | |
Michal Vasko | 0308dd6 | 2015-10-07 09:14:40 +0200 | [diff] [blame] | 2354 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2355 | lys_when_free(struct ly_ctx *ctx, struct lys_when *w, |
| 2356 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2357 | { |
| 2358 | if (!w) { |
| 2359 | return; |
| 2360 | } |
| 2361 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2362 | lys_extension_instances_free(ctx, w->ext, w->ext_size, private_destructor); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2363 | lydict_remove(ctx, w->cond); |
| 2364 | lydict_remove(ctx, w->dsc); |
| 2365 | lydict_remove(ctx, w->ref); |
| 2366 | |
| 2367 | free(w); |
| 2368 | } |
| 2369 | |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2370 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2371 | lys_augment_free(struct ly_ctx *ctx, struct lys_node_augment *aug, |
| 2372 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2373 | { |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 2374 | struct lys_node *next, *sub; |
| 2375 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2376 | /* children from a resolved augment are freed under the target node */ |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 2377 | if (!aug->target || (aug->flags & LYS_NOTAPPLIED)) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2378 | LY_TREE_FOR_SAFE(aug->child, next, sub) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2379 | lys_node_free(sub, private_destructor, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2380 | } |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 2381 | } |
| 2382 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2383 | lydict_remove(ctx, aug->target_name); |
| 2384 | lydict_remove(ctx, aug->dsc); |
| 2385 | lydict_remove(ctx, aug->ref); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2386 | |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2387 | lys_iffeature_free(ctx, aug->iffeature, aug->iffeature_size, 0, private_destructor); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2388 | lys_extension_instances_free(ctx, aug->ext, aug->ext_size, private_destructor); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2389 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2390 | lys_when_free(ctx, aug->when, private_destructor); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 2391 | } |
| 2392 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2393 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2394 | lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident, |
| 2395 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2396 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2397 | assert(ctx); |
| 2398 | if (!ident) { |
| 2399 | return; |
| 2400 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2401 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 2402 | free(ident->base); |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 2403 | ly_set_free(ident->der); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2404 | lydict_remove(ctx, ident->name); |
| 2405 | lydict_remove(ctx, ident->dsc); |
| 2406 | lydict_remove(ctx, ident->ref); |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2407 | lys_iffeature_free(ctx, ident->iffeature, ident->iffeature_size, 0, private_destructor); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2408 | lys_extension_instances_free(ctx, ident->ext, ident->ext_size, private_destructor); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2409 | |
| 2410 | } |
| 2411 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2412 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2413 | lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp, |
| 2414 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2415 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2416 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2417 | |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2418 | /* handle only specific parts for LYS_GROUPING */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2419 | for (i = 0; i < grp->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2420 | lys_tpdf_free(ctx, &grp->tpdf[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2421 | } |
| 2422 | free(grp->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2423 | } |
| 2424 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2425 | static void |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 2426 | lys_rpc_action_free(struct ly_ctx *ctx, struct lys_node_rpc_action *rpc_act, |
| 2427 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
| 2428 | { |
| 2429 | int i; |
| 2430 | |
| 2431 | /* handle only specific parts for LYS_GROUPING */ |
| 2432 | for (i = 0; i < rpc_act->tpdf_size; i++) { |
| 2433 | lys_tpdf_free(ctx, &rpc_act->tpdf[i], private_destructor); |
| 2434 | } |
| 2435 | free(rpc_act->tpdf); |
| 2436 | } |
| 2437 | |
| 2438 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2439 | lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io, |
| 2440 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2441 | { |
| 2442 | int i; |
| 2443 | |
| 2444 | /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */ |
| 2445 | for (i = 0; i < io->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2446 | lys_tpdf_free(ctx, &io->tpdf[i], private_destructor); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2447 | } |
| 2448 | free(io->tpdf); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2449 | |
| 2450 | for (i = 0; i < io->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2451 | lys_restr_free(ctx, &io->must[i], private_destructor); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2452 | } |
| 2453 | free(io->must); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2454 | } |
| 2455 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2456 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2457 | lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif, |
| 2458 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2459 | { |
| 2460 | int i; |
| 2461 | |
| 2462 | for (i = 0; i < notif->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2463 | lys_restr_free(ctx, ¬if->must[i], private_destructor); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2464 | } |
| 2465 | free(notif->must); |
| 2466 | |
| 2467 | for (i = 0; i < notif->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2468 | lys_tpdf_free(ctx, ¬if->tpdf[i], private_destructor); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2469 | } |
| 2470 | free(notif->tpdf); |
| 2471 | } |
| 2472 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2473 | lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml, |
| 2474 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2475 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2476 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2477 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2478 | for (i = 0; i < anyxml->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2479 | lys_restr_free(ctx, &anyxml->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2480 | } |
| 2481 | free(anyxml->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2482 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2483 | lys_when_free(ctx, anyxml->when, private_destructor); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2484 | } |
| 2485 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2486 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2487 | lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf, |
| 2488 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2489 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2490 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2491 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 2492 | /* leafref backlinks */ |
| 2493 | ly_set_free((struct ly_set *)leaf->backlinks); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2494 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2495 | for (i = 0; i < leaf->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2496 | lys_restr_free(ctx, &leaf->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2497 | } |
| 2498 | free(leaf->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2499 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2500 | lys_when_free(ctx, leaf->when, private_destructor); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2501 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2502 | lys_type_free(ctx, &leaf->type, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2503 | lydict_remove(ctx, leaf->units); |
| 2504 | lydict_remove(ctx, leaf->dflt); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2505 | } |
| 2506 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2507 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2508 | lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist, |
| 2509 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2510 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2511 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2512 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2513 | if (llist->backlinks) { |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2514 | /* leafref backlinks */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 2515 | ly_set_free(llist->backlinks); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2516 | } |
| 2517 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2518 | for (i = 0; i < llist->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2519 | lys_restr_free(ctx, &llist->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2520 | } |
| 2521 | free(llist->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2522 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2523 | for (i = 0; i < llist->dflt_size; i++) { |
| 2524 | lydict_remove(ctx, llist->dflt[i]); |
| 2525 | } |
| 2526 | free(llist->dflt); |
| 2527 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2528 | lys_when_free(ctx, llist->when, private_destructor); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2529 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2530 | lys_type_free(ctx, &llist->type, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2531 | lydict_remove(ctx, llist->units); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2532 | } |
| 2533 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2534 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2535 | lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list, |
| 2536 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2537 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2538 | int i, j; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2539 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2540 | /* handle only specific parts for LY_NODE_LIST */ |
Michal Vasko | aaeab1d | 2018-02-16 09:36:46 +0100 | [diff] [blame] | 2541 | lys_when_free(ctx, list->when, private_destructor); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2542 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2543 | for (i = 0; i < list->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2544 | lys_restr_free(ctx, &list->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2545 | } |
| 2546 | free(list->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 2547 | |
Michal Vasko | aaeab1d | 2018-02-16 09:36:46 +0100 | [diff] [blame] | 2548 | for (i = 0; i < list->tpdf_size; i++) { |
| 2549 | lys_tpdf_free(ctx, &list->tpdf[i], private_destructor); |
| 2550 | } |
| 2551 | free(list->tpdf); |
| 2552 | |
| 2553 | free(list->keys); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2554 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2555 | for (i = 0; i < list->unique_size; i++) { |
Michal Vasko | f5e940a | 2016-06-07 09:39:26 +0200 | [diff] [blame] | 2556 | for (j = 0; j < list->unique[i].expr_size; j++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2557 | lydict_remove(ctx, list->unique[i].expr[j]); |
| 2558 | } |
| 2559 | free(list->unique[i].expr); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2560 | } |
| 2561 | free(list->unique); |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 2562 | |
Michal Vasko | aaeab1d | 2018-02-16 09:36:46 +0100 | [diff] [blame] | 2563 | lydict_remove(ctx, list->keys_str); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2564 | } |
| 2565 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2566 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2567 | lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont, |
| 2568 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2569 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2570 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2571 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2572 | /* handle only specific parts for LY_NODE_CONTAINER */ |
| 2573 | lydict_remove(ctx, cont->presence); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2574 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2575 | for (i = 0; i < cont->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2576 | lys_tpdf_free(ctx, &cont->tpdf[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2577 | } |
| 2578 | free(cont->tpdf); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2579 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2580 | for (i = 0; i < cont->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2581 | lys_restr_free(ctx, &cont->must[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2582 | } |
| 2583 | free(cont->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2584 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2585 | lys_when_free(ctx, cont->when, private_destructor); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2586 | } |
| 2587 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2588 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2589 | lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f, |
| 2590 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2591 | { |
| 2592 | lydict_remove(ctx, f->name); |
| 2593 | lydict_remove(ctx, f->dsc); |
| 2594 | lydict_remove(ctx, f->ref); |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2595 | lys_iffeature_free(ctx, f->iffeature, f->iffeature_size, 0, private_destructor); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 2596 | ly_set_free(f->depfeatures); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2597 | lys_extension_instances_free(ctx, f->ext, f->ext_size, private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2601 | lys_extension_free(struct ly_ctx *ctx, struct lys_ext *e, |
| 2602 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2603 | { |
| 2604 | lydict_remove(ctx, e->name); |
| 2605 | lydict_remove(ctx, e->dsc); |
| 2606 | lydict_remove(ctx, e->ref); |
| 2607 | lydict_remove(ctx, e->argument); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2608 | lys_extension_instances_free(ctx, e->ext, e->ext_size, private_destructor); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2609 | } |
| 2610 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2611 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2612 | lys_deviation_free(struct lys_module *module, struct lys_deviation *dev, |
| 2613 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2614 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2615 | int i, j, k; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2616 | struct ly_ctx *ctx; |
| 2617 | struct lys_node *next, *elem; |
| 2618 | |
| 2619 | ctx = module->ctx; |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2620 | |
| 2621 | lydict_remove(ctx, dev->target_name); |
| 2622 | lydict_remove(ctx, dev->dsc); |
| 2623 | lydict_remove(ctx, dev->ref); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2624 | lys_extension_instances_free(ctx, dev->ext, dev->ext_size, private_destructor); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2625 | |
Pavol Vican | 64d0b76 | 2016-08-25 10:44:59 +0200 | [diff] [blame] | 2626 | if (!dev->deviate) { |
Michal Vasko | 7427b26 | 2018-05-14 15:23:55 +0200 | [diff] [blame] | 2627 | return; |
Pavol Vican | 64d0b76 | 2016-08-25 10:44:59 +0200 | [diff] [blame] | 2628 | } |
| 2629 | |
Michal Vasko | 7427b26 | 2018-05-14 15:23:55 +0200 | [diff] [blame] | 2630 | /* it could not be applied because it failed to be applied */ |
| 2631 | if (dev->orig_node) { |
| 2632 | /* the module was freed, but we only need the context from orig_node, use ours */ |
| 2633 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 2634 | /* it's actually a node subtree, we need to update modules on all the nodes :-/ */ |
| 2635 | LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) { |
| 2636 | elem->module = module; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2637 | |
Michal Vasko | 7427b26 | 2018-05-14 15:23:55 +0200 | [diff] [blame] | 2638 | LY_TREE_DFS_END(dev->orig_node, next, elem); |
| 2639 | } |
| 2640 | lys_node_free(dev->orig_node, NULL, 0); |
| 2641 | } else { |
| 2642 | /* it's just a shallow copy, freeing one node */ |
| 2643 | dev->orig_node->module = module; |
| 2644 | lys_node_free(dev->orig_node, NULL, 1); |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2645 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2646 | } |
| 2647 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2648 | for (i = 0; i < dev->deviate_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2649 | 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] | 2650 | |
Radek Krejci | d5a5c28 | 2016-08-15 15:38:08 +0200 | [diff] [blame] | 2651 | for (j = 0; j < dev->deviate[i].dflt_size; j++) { |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2652 | lydict_remove(ctx, dev->deviate[i].dflt[j]); |
Radek Krejci | d5a5c28 | 2016-08-15 15:38:08 +0200 | [diff] [blame] | 2653 | } |
| 2654 | free(dev->deviate[i].dflt); |
| 2655 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2656 | lydict_remove(ctx, dev->deviate[i].units); |
| 2657 | |
| 2658 | if (dev->deviate[i].mod == LY_DEVIATE_DEL) { |
| 2659 | for (j = 0; j < dev->deviate[i].must_size; j++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2660 | lys_restr_free(ctx, &dev->deviate[i].must[j], private_destructor); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2661 | } |
| 2662 | free(dev->deviate[i].must); |
| 2663 | |
| 2664 | for (j = 0; j < dev->deviate[i].unique_size; j++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2665 | for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) { |
| 2666 | lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]); |
| 2667 | } |
Michal Vasko | 0238ccf | 2016-03-07 15:02:18 +0100 | [diff] [blame] | 2668 | free(dev->deviate[i].unique[j].expr); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2669 | } |
| 2670 | free(dev->deviate[i].unique); |
| 2671 | } |
| 2672 | } |
| 2673 | free(dev->deviate); |
| 2674 | } |
| 2675 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2676 | static void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2677 | lys_uses_free(struct ly_ctx *ctx, struct lys_node_uses *uses, |
| 2678 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2679 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2680 | int i, j; |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2681 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2682 | for (i = 0; i < uses->refine_size; i++) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2683 | lydict_remove(ctx, uses->refine[i].target_name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2684 | lydict_remove(ctx, uses->refine[i].dsc); |
| 2685 | lydict_remove(ctx, uses->refine[i].ref); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2686 | |
Radek Krejci | fde04bd | 2017-09-13 16:38:38 +0200 | [diff] [blame] | 2687 | lys_iffeature_free(ctx, uses->refine[i].iffeature, uses->refine[i].iffeature_size, 0, private_destructor); |
| 2688 | |
Michal Vasko | a275c0a | 2015-09-24 09:55:42 +0200 | [diff] [blame] | 2689 | for (j = 0; j < uses->refine[i].must_size; j++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2690 | lys_restr_free(ctx, &uses->refine[i].must[j], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2691 | } |
| 2692 | free(uses->refine[i].must); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2693 | |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 2694 | for (j = 0; j < uses->refine[i].dflt_size; j++) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 2695 | lydict_remove(ctx, uses->refine[i].dflt[j]); |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 2696 | } |
| 2697 | free(uses->refine[i].dflt); |
| 2698 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2699 | 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] | 2700 | |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 2701 | if (uses->refine[i].target_type & LYS_CONTAINER) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2702 | lydict_remove(ctx, uses->refine[i].mod.presence); |
| 2703 | } |
| 2704 | } |
| 2705 | free(uses->refine); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2706 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2707 | for (i = 0; i < uses->augment_size; i++) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2708 | lys_augment_free(ctx, &uses->augment[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2709 | } |
| 2710 | free(uses->augment); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2711 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2712 | lys_when_free(ctx, uses->when, private_destructor); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 2713 | } |
| 2714 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2715 | void |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2716 | 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] | 2717 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2718 | struct ly_ctx *ctx; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2719 | struct lys_node *sub, *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2720 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2721 | if (!node) { |
| 2722 | return; |
| 2723 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2724 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2725 | assert(node->module); |
| 2726 | assert(node->module->ctx); |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2727 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2728 | ctx = node->module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2729 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2730 | /* remove private object */ |
Mislav Novakovic | b5529e5 | 2016-02-29 11:42:43 +0100 | [diff] [blame] | 2731 | if (node->priv && private_destructor) { |
| 2732 | private_destructor(node, node->priv); |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2733 | } |
| 2734 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2735 | /* common part */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 2736 | lydict_remove(ctx, node->name); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2737 | if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 2738 | lys_iffeature_free(ctx, node->iffeature, node->iffeature_size, shallow, private_destructor); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2739 | lydict_remove(ctx, node->dsc); |
| 2740 | lydict_remove(ctx, node->ref); |
| 2741 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2742 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2743 | if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2744 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2745 | lys_node_free(sub, private_destructor, 0); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 2746 | } |
| 2747 | } |
| 2748 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2749 | lys_extension_instances_free(ctx, node->ext, node->ext_size, private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2750 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2751 | /* specific part */ |
| 2752 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2753 | case LYS_CONTAINER: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2754 | lys_container_free(ctx, (struct lys_node_container *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2755 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2756 | case LYS_CHOICE: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2757 | lys_when_free(ctx, ((struct lys_node_choice *)node)->when, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2758 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2759 | case LYS_LEAF: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2760 | lys_leaf_free(ctx, (struct lys_node_leaf *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2761 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2762 | case LYS_LEAFLIST: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2763 | lys_leaflist_free(ctx, (struct lys_node_leaflist *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2764 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2765 | case LYS_LIST: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2766 | lys_list_free(ctx, (struct lys_node_list *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2767 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2768 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2769 | case LYS_ANYDATA: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2770 | lys_anydata_free(ctx, (struct lys_node_anydata *)node, 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_USES: |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2773 | lys_uses_free(ctx, (struct lys_node_uses *)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_CASE: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2776 | lys_when_free(ctx, ((struct lys_node_case *)node)->when, private_destructor); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2777 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2778 | case LYS_AUGMENT: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2779 | /* do nothing */ |
| 2780 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2781 | case LYS_GROUPING: |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 2782 | lys_grp_free(ctx, (struct lys_node_grp *)node, private_destructor); |
| 2783 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2784 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2785 | case LYS_ACTION: |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 2786 | 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] | 2787 | break; |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2788 | case LYS_NOTIF: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2789 | lys_notif_free(ctx, (struct lys_node_notif *)node, private_destructor); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 2790 | break; |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2791 | case LYS_INPUT: |
| 2792 | case LYS_OUTPUT: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2793 | lys_inout_free(ctx, (struct lys_node_inout *)node, private_destructor); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2794 | break; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 2795 | case LYS_EXT: |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 2796 | case LYS_UNKNOWN: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 2797 | LOGINT(ctx); |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 2798 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2799 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2800 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2801 | /* again common part */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2802 | lys_node_unlink(node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2803 | free(node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2804 | } |
| 2805 | |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 2806 | API struct lys_module * |
| 2807 | lys_implemented_module(const struct lys_module *mod) |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 2808 | { |
| 2809 | struct ly_ctx *ctx; |
| 2810 | int i; |
| 2811 | |
| 2812 | if (!mod || mod->implemented) { |
| 2813 | /* invalid argument or the module itself is implemented */ |
Radek Krejci | 2eee5c0 | 2016-12-06 19:18:05 +0100 | [diff] [blame] | 2814 | return (struct lys_module *)mod; |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 2815 | } |
| 2816 | |
| 2817 | ctx = mod->ctx; |
| 2818 | for (i = 0; i < ctx->models.used; i++) { |
| 2819 | if (!ctx->models.list[i]->implemented) { |
| 2820 | continue; |
| 2821 | } |
| 2822 | |
| 2823 | if (ly_strequal(mod->name, ctx->models.list[i]->name, 1)) { |
| 2824 | /* we have some revision of the module implemented */ |
| 2825 | return ctx->models.list[i]; |
| 2826 | } |
| 2827 | } |
| 2828 | |
| 2829 | /* we have no revision of the module implemented, return the module itself, |
| 2830 | * 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] | 2831 | return (struct lys_module *)mod; |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 2832 | } |
| 2833 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2834 | /* free_int_mods - flag whether to free the internal modules as well */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2835 | static void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2836 | 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] | 2837 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2838 | struct ly_ctx *ctx; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2839 | struct lys_node *next, *iter; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2840 | unsigned int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2841 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2842 | assert(module->ctx); |
| 2843 | ctx = module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2844 | |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2845 | /* just free the import array, imported modules will stay in the context */ |
Radek Krejci | 225376f | 2016-02-16 17:36:22 +0100 | [diff] [blame] | 2846 | for (i = 0; i < module->imp_size; i++) { |
Michal Vasko | a99c163 | 2016-06-06 16:23:52 +0200 | [diff] [blame] | 2847 | lydict_remove(ctx, module->imp[i].prefix); |
Michal Vasko | 8bfe381 | 2016-07-27 13:37:52 +0200 | [diff] [blame] | 2848 | lydict_remove(ctx, module->imp[i].dsc); |
| 2849 | lydict_remove(ctx, module->imp[i].ref); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2850 | 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] | 2851 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2852 | free(module->imp); |
| 2853 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2854 | /* submodules don't have data tree, the data nodes |
| 2855 | * are placed in the main module altogether */ |
| 2856 | if (!module->type) { |
| 2857 | LY_TREE_FOR_SAFE(module->data, next, iter) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2858 | lys_node_free(iter, private_destructor, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2859 | } |
Radek Krejci | 2118196 | 2015-06-30 14:11:00 +0200 | [diff] [blame] | 2860 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2861 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2862 | lydict_remove(ctx, module->dsc); |
| 2863 | lydict_remove(ctx, module->ref); |
| 2864 | lydict_remove(ctx, module->org); |
| 2865 | lydict_remove(ctx, module->contact); |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 2866 | lydict_remove(ctx, module->filepath); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2867 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2868 | /* revisions */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2869 | for (i = 0; i < module->rev_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2870 | 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] | 2871 | lydict_remove(ctx, module->rev[i].dsc); |
| 2872 | lydict_remove(ctx, module->rev[i].ref); |
| 2873 | } |
| 2874 | free(module->rev); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2875 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2876 | /* identities */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2877 | for (i = 0; i < module->ident_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2878 | lys_ident_free(ctx, &module->ident[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2879 | } |
| 2880 | module->ident_size = 0; |
| 2881 | free(module->ident); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2882 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2883 | /* typedefs */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2884 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2885 | lys_tpdf_free(ctx, &module->tpdf[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2886 | } |
| 2887 | free(module->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2888 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2889 | /* extension instances */ |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2890 | lys_extension_instances_free(ctx, module->ext, module->ext_size, private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2891 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2892 | /* augment */ |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2893 | for (i = 0; i < module->augment_size; i++) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2894 | lys_augment_free(ctx, &module->augment[i], private_destructor); |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2895 | } |
| 2896 | free(module->augment); |
| 2897 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2898 | /* features */ |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2899 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2900 | lys_feature_free(ctx, &module->features[i], private_destructor); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2901 | } |
| 2902 | free(module->features); |
| 2903 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2904 | /* deviations */ |
| 2905 | for (i = 0; i < module->deviation_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2906 | lys_deviation_free(module, &module->deviation[i], private_destructor); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2907 | } |
| 2908 | free(module->deviation); |
| 2909 | |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2910 | /* extensions */ |
| 2911 | for (i = 0; i < module->extensions_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2912 | lys_extension_free(ctx, &module->extensions[i], private_destructor); |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 2913 | } |
| 2914 | free(module->extensions); |
| 2915 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2916 | lydict_remove(ctx, module->name); |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 2917 | lydict_remove(ctx, module->prefix); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2918 | } |
| 2919 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2920 | void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2921 | 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] | 2922 | { |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 2923 | int i; |
| 2924 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2925 | if (!submodule) { |
| 2926 | return; |
| 2927 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2928 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2929 | /* common part with struct ly_module */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2930 | module_free_common((struct lys_module *)submodule, private_destructor); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2931 | |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 2932 | /* include */ |
| 2933 | for (i = 0; i < submodule->inc_size; i++) { |
| 2934 | lydict_remove(submodule->ctx, submodule->inc[i].dsc); |
| 2935 | lydict_remove(submodule->ctx, submodule->inc[i].ref); |
| 2936 | lys_extension_instances_free(submodule->ctx, submodule->inc[i].ext, submodule->inc[i].ext_size, private_destructor); |
| 2937 | /* complete submodule free is done only from main module since |
| 2938 | * submodules propagate their includes to the main module */ |
| 2939 | } |
| 2940 | free(submodule->inc); |
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 | free(submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2943 | } |
| 2944 | |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 2945 | int |
| 2946 | lys_ingrouping(const struct lys_node *node) |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 2947 | { |
| 2948 | const struct lys_node *iter = node; |
| 2949 | assert(node); |
| 2950 | |
| 2951 | for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter)); |
| 2952 | if (!iter) { |
| 2953 | return 0; |
| 2954 | } else { |
| 2955 | return 1; |
| 2956 | } |
| 2957 | } |
| 2958 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2959 | /* |
| 2960 | * final: 0 - do not change config flags; 1 - inherit config flags from the parent; 2 - remove config flags |
| 2961 | */ |
| 2962 | static struct lys_node * |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 2963 | 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] | 2964 | struct unres_schema *unres, int shallow, int finalize) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2965 | { |
Radek Krejci | 7b9f566 | 2016-11-07 16:28:37 +0100 | [diff] [blame] | 2966 | struct lys_node *retval = NULL, *iter, *p; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2967 | struct ly_ctx *ctx = module->ctx; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2968 | int i, j, rc; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2969 | unsigned int size, size1, size2; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 2970 | struct unres_list_uniq *unique_info; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2971 | uint16_t flags; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2972 | |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2973 | struct lys_node_container *cont = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2974 | struct lys_node_container *cont_orig = (struct lys_node_container *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2975 | struct lys_node_choice *choice = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2976 | struct lys_node_choice *choice_orig = (struct lys_node_choice *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2977 | struct lys_node_leaf *leaf = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2978 | struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2979 | struct lys_node_leaflist *llist = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2980 | struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2981 | struct lys_node_list *list = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2982 | struct lys_node_list *list_orig = (struct lys_node_list *)node; |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2983 | struct lys_node_anydata *any = NULL; |
| 2984 | struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2985 | struct lys_node_uses *uses = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2986 | struct lys_node_uses *uses_orig = (struct lys_node_uses *)node; |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2987 | struct lys_node_rpc_action *rpc = NULL; |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2988 | struct lys_node_inout *io = NULL; |
Radek Krejci | c43151c | 2017-03-12 07:10:52 +0100 | [diff] [blame] | 2989 | struct lys_node_notif *ntf = NULL; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2990 | struct lys_node_case *cs = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2991 | struct lys_node_case *cs_orig = (struct lys_node_case *)node; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2992 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2993 | /* we cannot just duplicate memory since the strings are stored in |
| 2994 | * dictionary and we need to update dictionary counters. |
| 2995 | */ |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2996 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2997 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2998 | case LYS_CONTAINER: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2999 | cont = calloc(1, sizeof *cont); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3000 | retval = (struct lys_node *)cont; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3001 | break; |
| 3002 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3003 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3004 | choice = calloc(1, sizeof *choice); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3005 | retval = (struct lys_node *)choice; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3006 | break; |
| 3007 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3008 | case LYS_LEAF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3009 | leaf = calloc(1, sizeof *leaf); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3010 | retval = (struct lys_node *)leaf; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3011 | break; |
| 3012 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3013 | case LYS_LEAFLIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3014 | llist = calloc(1, sizeof *llist); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3015 | retval = (struct lys_node *)llist; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3016 | break; |
| 3017 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3018 | case LYS_LIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3019 | list = calloc(1, sizeof *list); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3020 | retval = (struct lys_node *)list; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3021 | break; |
| 3022 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3023 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3024 | case LYS_ANYDATA: |
| 3025 | any = calloc(1, sizeof *any); |
| 3026 | retval = (struct lys_node *)any; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3027 | break; |
| 3028 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3029 | case LYS_USES: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3030 | uses = calloc(1, sizeof *uses); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3031 | retval = (struct lys_node *)uses; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3032 | break; |
| 3033 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3034 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3035 | cs = calloc(1, sizeof *cs); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3036 | retval = (struct lys_node *)cs; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3037 | break; |
| 3038 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3039 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 3040 | case LYS_ACTION: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 3041 | rpc = calloc(1, sizeof *rpc); |
| 3042 | retval = (struct lys_node *)rpc; |
| 3043 | break; |
| 3044 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3045 | case LYS_INPUT: |
| 3046 | case LYS_OUTPUT: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 3047 | io = calloc(1, sizeof *io); |
| 3048 | retval = (struct lys_node *)io; |
| 3049 | break; |
| 3050 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3051 | case LYS_NOTIF: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 3052 | ntf = calloc(1, sizeof *ntf); |
| 3053 | retval = (struct lys_node *)ntf; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3054 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3055 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3056 | default: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3057 | LOGINT(ctx); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3058 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3059 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3060 | LY_CHECK_ERR_RETURN(!retval, LOGMEM(ctx), NULL); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3061 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3062 | /* |
| 3063 | * duplicate generic part of the structure |
| 3064 | */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3065 | retval->name = lydict_insert(ctx, node->name, 0); |
| 3066 | retval->dsc = lydict_insert(ctx, node->dsc, 0); |
| 3067 | retval->ref = lydict_insert(ctx, node->ref, 0); |
| 3068 | retval->flags = node->flags; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3069 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3070 | retval->module = module; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3071 | retval->nodetype = node->nodetype; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3072 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3073 | retval->prev = retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3074 | |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3075 | retval->ext_size = node->ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 3076 | 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] | 3077 | goto error; |
| 3078 | } |
| 3079 | |
Radek Krejci | 0621404 | 2016-11-04 16:25:58 +0100 | [diff] [blame] | 3080 | if (node->iffeature_size) { |
| 3081 | retval->iffeature_size = node->iffeature_size; |
| 3082 | retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3083 | LY_CHECK_ERR_GOTO(!retval->iffeature, LOGMEM(ctx), error); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3084 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3085 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3086 | if (!shallow) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 3087 | for (i = 0; i < node->iffeature_size; ++i) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3088 | resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2); |
| 3089 | if (size1) { |
| 3090 | /* there is something to duplicate */ |
| 3091 | |
| 3092 | /* duplicate compiled expression */ |
| 3093 | size = (size1 / 4) + (size1 % 4) ? 1 : 0; |
| 3094 | retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3095 | LY_CHECK_ERR_GOTO(!retval->iffeature[i].expr, LOGMEM(ctx), error); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3096 | memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr); |
| 3097 | |
| 3098 | /* 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] | 3099 | retval->iffeature[i].features = calloc(size2, sizeof *retval->iffeature[i].features); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3100 | 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] | 3101 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3102 | for (j = 0; (unsigned int)j < size2; j++) { |
| 3103 | rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT, |
| 3104 | &retval->iffeature[i].features[j]); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 3105 | if (rc == EXIT_FAILURE) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3106 | /* feature is resolved in origin, so copy it |
| 3107 | * - duplication is used for instantiating groupings |
| 3108 | * and if-feature inside grouping is supposed to be |
| 3109 | * resolved inside the original grouping, so we want |
| 3110 | * to keep pointers to features from the grouping |
| 3111 | * context */ |
| 3112 | retval->iffeature[i].features[j] = node->iffeature[i].features[j]; |
| 3113 | } else if (rc == -1) { |
| 3114 | goto error; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 3115 | } /* else unres was duplicated */ |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3116 | } |
| 3117 | } |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3118 | |
| 3119 | /* duplicate if-feature's extensions */ |
| 3120 | retval->iffeature[i].ext_size = node->iffeature[i].ext_size; |
Michal Vasko | 17e8ba3 | 2018-02-15 10:58:56 +0100 | [diff] [blame] | 3121 | 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] | 3122 | &retval->iffeature[i], LYEXT_PAR_IFFEATURE, &retval->iffeature[i].ext, shallow, unres)) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3123 | goto error; |
| 3124 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3125 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3126 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3127 | /* inherit config flags */ |
Radek Krejci | 7b9f566 | 2016-11-07 16:28:37 +0100 | [diff] [blame] | 3128 | p = parent; |
| 3129 | do { |
| 3130 | for (iter = p; iter && (iter->nodetype == LYS_USES); iter = iter->parent); |
| 3131 | } while (iter && iter->nodetype == LYS_AUGMENT && (p = lys_parent(iter))); |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3132 | if (iter) { |
| 3133 | flags = iter->flags & LYS_CONFIG_MASK; |
| 3134 | } else { |
| 3135 | /* default */ |
| 3136 | flags = LYS_CONFIG_W; |
| 3137 | } |
| 3138 | |
| 3139 | switch (finalize) { |
| 3140 | case 1: |
| 3141 | /* inherit config flags */ |
| 3142 | if (retval->flags & LYS_CONFIG_SET) { |
| 3143 | /* skip nodes with an explicit config value */ |
| 3144 | if ((flags & LYS_CONFIG_R) && (retval->flags & LYS_CONFIG_W)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3145 | LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, "true", "config"); |
| 3146 | 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] | 3147 | goto error; |
| 3148 | } |
| 3149 | break; |
| 3150 | } |
| 3151 | |
| 3152 | if (retval->nodetype != LYS_USES) { |
| 3153 | retval->flags = (retval->flags & ~LYS_CONFIG_MASK) | flags; |
| 3154 | } |
Radek Krejci | 2cc2532 | 2017-09-06 16:32:02 +0200 | [diff] [blame] | 3155 | |
| 3156 | /* inherit status */ |
Radek Krejci | e2807ea | 2017-09-07 10:52:21 +0200 | [diff] [blame] | 3157 | if ((parent->flags & LYS_STATUS_MASK) > (retval->flags & LYS_STATUS_MASK)) { |
| 3158 | /* but do it only in case the parent has a stonger status */ |
| 3159 | retval->flags &= ~LYS_STATUS_MASK; |
| 3160 | retval->flags |= (parent->flags & LYS_STATUS_MASK); |
| 3161 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3162 | break; |
| 3163 | case 2: |
| 3164 | /* erase config flags */ |
| 3165 | retval->flags &= ~LYS_CONFIG_MASK; |
| 3166 | retval->flags &= ~LYS_CONFIG_SET; |
| 3167 | break; |
| 3168 | } |
| 3169 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3170 | /* connect it to the parent */ |
Michal Vasko | 8d30dd8 | 2018-09-10 10:06:12 +0200 | [diff] [blame] | 3171 | if (lys_node_addchild(parent, retval->module, retval, 0)) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3172 | goto error; |
| 3173 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 3174 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3175 | /* go recursively */ |
| 3176 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3177 | LY_TREE_FOR(node->child, iter) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3178 | if (iter->nodetype & LYS_GROUPING) { |
| 3179 | /* do not instantiate groupings */ |
| 3180 | continue; |
| 3181 | } |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 3182 | if (!lys_node_dup_recursion(module, retval, iter, unres, 0, finalize)) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3183 | goto error; |
| 3184 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3185 | } |
| 3186 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3187 | |
| 3188 | if (finalize == 1) { |
| 3189 | /* check that configuration lists have keys |
| 3190 | * - we really want to check keys_size in original node, because the keys are |
| 3191 | * not yet resolved here, it is done below in nodetype specific part */ |
| 3192 | if ((retval->nodetype == LYS_LIST) && (retval->flags & LYS_CONFIG_W) |
| 3193 | && !((struct lys_node_list *)node)->keys_size) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3194 | LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list"); |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3195 | goto error; |
| 3196 | } |
| 3197 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3198 | } else { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 3199 | memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3200 | } |
| 3201 | |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3202 | /* |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3203 | * duplicate specific part of the structure |
| 3204 | */ |
| 3205 | switch (node->nodetype) { |
| 3206 | case LYS_CONTAINER: |
| 3207 | if (cont_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3208 | cont->when = lys_when_dup(module, cont_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3209 | LY_CHECK_GOTO(!cont->when, error); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3210 | } |
| 3211 | cont->presence = lydict_insert(ctx, cont_orig->presence, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3212 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3213 | if (cont_orig->must) { |
| 3214 | cont->must = lys_restr_dup(module, cont_orig->must, cont_orig->must_size, shallow, unres); |
| 3215 | LY_CHECK_GOTO(!cont->must, error); |
| 3216 | cont->must_size = cont_orig->must_size; |
| 3217 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3218 | |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3219 | /* typedefs are not needed in instantiated grouping, nor the deviation's shallow copy */ |
| 3220 | |
| 3221 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3222 | case LYS_CHOICE: |
| 3223 | if (choice_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3224 | choice->when = lys_when_dup(module, choice_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3225 | LY_CHECK_GOTO(!choice->when, error); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3226 | } |
| 3227 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3228 | if (!shallow) { |
| 3229 | if (choice_orig->dflt) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3230 | rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0, |
| 3231 | LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST, |
| 3232 | (const struct lys_node **)&choice->dflt); |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3233 | if (rc) { |
| 3234 | if (rc == EXIT_FAILURE) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3235 | LOGINT(ctx); |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3236 | } |
| 3237 | goto error; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3238 | } |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3239 | } else { |
| 3240 | /* useless to check return value, we don't know whether |
| 3241 | * there really wasn't any default defined or it just hasn't |
| 3242 | * been resolved, we just hope for the best :) |
| 3243 | */ |
| 3244 | unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3245 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3246 | } else { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3247 | choice->dflt = choice_orig->dflt; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3248 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3249 | break; |
| 3250 | |
| 3251 | case LYS_LEAF: |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 3252 | 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] | 3253 | goto error; |
| 3254 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3255 | leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0); |
| 3256 | |
| 3257 | if (leaf_orig->dflt) { |
| 3258 | leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3259 | } |
| 3260 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3261 | if (leaf_orig->must) { |
| 3262 | leaf->must = lys_restr_dup(module, leaf_orig->must, leaf_orig->must_size, shallow, unres); |
| 3263 | LY_CHECK_GOTO(!leaf->must, error); |
| 3264 | leaf->must_size = leaf_orig->must_size; |
| 3265 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3266 | |
| 3267 | if (leaf_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3268 | leaf->when = lys_when_dup(module, leaf_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3269 | LY_CHECK_GOTO(!leaf->when, error); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3270 | } |
| 3271 | break; |
| 3272 | |
| 3273 | case LYS_LEAFLIST: |
Radek Krejci | b53154b | 2017-07-19 09:14:13 +0200 | [diff] [blame] | 3274 | 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] | 3275 | goto error; |
| 3276 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3277 | llist->units = lydict_insert(module->ctx, llist_orig->units, 0); |
| 3278 | |
| 3279 | llist->min = llist_orig->min; |
| 3280 | llist->max = llist_orig->max; |
| 3281 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3282 | if (llist_orig->must) { |
| 3283 | llist->must = lys_restr_dup(module, llist_orig->must, llist_orig->must_size, shallow, unres); |
| 3284 | LY_CHECK_GOTO(!llist->must, error); |
| 3285 | llist->must_size = llist_orig->must_size; |
| 3286 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3287 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3288 | if (llist_orig->dflt) { |
| 3289 | llist->dflt = malloc(llist_orig->dflt_size * sizeof *llist->dflt); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3290 | LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), error); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3291 | llist->dflt_size = llist_orig->dflt_size; |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3292 | |
Radek Krejci | c699e42 | 2017-06-02 15:18:58 +0200 | [diff] [blame] | 3293 | for (i = 0; i < llist->dflt_size; i++) { |
| 3294 | llist->dflt[i] = lydict_insert(ctx, llist_orig->dflt[i], 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 3295 | } |
| 3296 | } |
| 3297 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3298 | if (llist_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3299 | llist->when = lys_when_dup(module, llist_orig->when, shallow, unres); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3300 | } |
| 3301 | break; |
| 3302 | |
| 3303 | case LYS_LIST: |
| 3304 | list->min = list_orig->min; |
| 3305 | list->max = list_orig->max; |
| 3306 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3307 | if (list_orig->must) { |
| 3308 | list->must = lys_restr_dup(module, list_orig->must, list_orig->must_size, shallow, unres); |
| 3309 | LY_CHECK_GOTO(!list->must, error); |
| 3310 | list->must_size = list_orig->must_size; |
| 3311 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3312 | |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3313 | /* 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] | 3314 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3315 | if (list_orig->keys_size) { |
| 3316 | list->keys = calloc(list_orig->keys_size, sizeof *list->keys); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3317 | LY_CHECK_ERR_GOTO(!list->keys, LOGMEM(ctx), error); |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 3318 | list->keys_str = lydict_insert(ctx, list_orig->keys_str, 0); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3319 | list->keys_size = list_orig->keys_size; |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3320 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3321 | if (!shallow) { |
Radek Krejci | 7a1e607 | 2018-08-13 14:59:16 +0200 | [diff] [blame] | 3322 | 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] | 3323 | goto error; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3324 | } |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3325 | } else { |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3326 | 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] | 3327 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 3328 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3329 | |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3330 | if (list_orig->unique) { |
| 3331 | list->unique = malloc(list_orig->unique_size * sizeof *list->unique); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3332 | LY_CHECK_ERR_GOTO(!list->unique, LOGMEM(ctx), error); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3333 | list->unique_size = list_orig->unique_size; |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 3334 | |
Radek Krejci | c699e42 | 2017-06-02 15:18:58 +0200 | [diff] [blame] | 3335 | for (i = 0; i < list->unique_size; ++i) { |
| 3336 | 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] | 3337 | LY_CHECK_ERR_GOTO(!list->unique[i].expr, LOGMEM(ctx), error); |
Radek Krejci | c699e42 | 2017-06-02 15:18:58 +0200 | [diff] [blame] | 3338 | list->unique[i].expr_size = list_orig->unique[i].expr_size; |
| 3339 | for (j = 0; j < list->unique[i].expr_size; j++) { |
| 3340 | list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0); |
| 3341 | |
| 3342 | /* if it stays in unres list, duplicate it also there */ |
| 3343 | unique_info = malloc(sizeof *unique_info); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3344 | LY_CHECK_ERR_GOTO(!unique_info, LOGMEM(ctx), error); |
Radek Krejci | c699e42 | 2017-06-02 15:18:58 +0200 | [diff] [blame] | 3345 | unique_info->list = (struct lys_node *)list; |
| 3346 | unique_info->expr = list->unique[i].expr[j]; |
| 3347 | unique_info->trg_type = &list->unique[i].trg_type; |
| 3348 | unres_schema_dup(module, unres, &list_orig, UNRES_LIST_UNIQ, unique_info); |
| 3349 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3350 | } |
| 3351 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 3352 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3353 | if (list_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3354 | list->when = lys_when_dup(module, list_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3355 | LY_CHECK_GOTO(!list->when, error); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 3356 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 3357 | break; |
| 3358 | |
| 3359 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3360 | case LYS_ANYDATA: |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3361 | if (any_orig->must) { |
| 3362 | any->must = lys_restr_dup(module, any_orig->must, any_orig->must_size, shallow, unres); |
| 3363 | LY_CHECK_GOTO(!any->must, error); |
| 3364 | any->must_size = any_orig->must_size; |
| 3365 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3366 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3367 | if (any_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3368 | any->when = lys_when_dup(module, any_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3369 | LY_CHECK_GOTO(!any->when, error); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3370 | } |
| 3371 | break; |
| 3372 | |
| 3373 | case LYS_USES: |
| 3374 | uses->grp = uses_orig->grp; |
| 3375 | |
| 3376 | if (uses_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3377 | uses->when = lys_when_dup(module, uses_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3378 | LY_CHECK_GOTO(!uses->when, error); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3379 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 3380 | /* 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] | 3381 | break; |
| 3382 | |
| 3383 | case LYS_CASE: |
| 3384 | if (cs_orig->when) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3385 | cs->when = lys_when_dup(module, cs_orig->when, shallow, unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3386 | LY_CHECK_GOTO(!cs->when, error); |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 3387 | } |
| 3388 | break; |
| 3389 | |
Radek Krejci | 9693540 | 2016-11-04 16:27:28 +0100 | [diff] [blame] | 3390 | case LYS_ACTION: |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 3391 | case LYS_RPC: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3392 | case LYS_INPUT: |
| 3393 | case LYS_OUTPUT: |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3394 | case LYS_NOTIF: |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3395 | /* 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] | 3396 | break; |
| 3397 | |
| 3398 | default: |
| 3399 | /* LY_NODE_AUGMENT */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3400 | LOGINT(ctx); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3401 | goto error; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3402 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3403 | |
| 3404 | return retval; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3405 | |
| 3406 | error: |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 3407 | lys_node_free(retval, NULL, 0); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 3408 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3409 | } |
| 3410 | |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 3411 | int |
| 3412 | lys_has_xpath(const struct lys_node *node) |
| 3413 | { |
| 3414 | assert(node); |
| 3415 | |
| 3416 | switch (node->nodetype) { |
| 3417 | case LYS_AUGMENT: |
| 3418 | if (((struct lys_node_augment *)node)->when) { |
| 3419 | return 1; |
| 3420 | } |
| 3421 | break; |
| 3422 | case LYS_CASE: |
| 3423 | if (((struct lys_node_case *)node)->when) { |
| 3424 | return 1; |
| 3425 | } |
| 3426 | break; |
| 3427 | case LYS_CHOICE: |
| 3428 | if (((struct lys_node_choice *)node)->when) { |
| 3429 | return 1; |
| 3430 | } |
| 3431 | break; |
| 3432 | case LYS_ANYDATA: |
| 3433 | if (((struct lys_node_anydata *)node)->when || ((struct lys_node_anydata *)node)->must_size) { |
| 3434 | return 1; |
| 3435 | } |
| 3436 | break; |
| 3437 | case LYS_LEAF: |
| 3438 | if (((struct lys_node_leaf *)node)->when || ((struct lys_node_leaf *)node)->must_size) { |
| 3439 | return 1; |
| 3440 | } |
| 3441 | break; |
| 3442 | case LYS_LEAFLIST: |
| 3443 | if (((struct lys_node_leaflist *)node)->when || ((struct lys_node_leaflist *)node)->must_size) { |
| 3444 | return 1; |
| 3445 | } |
| 3446 | break; |
| 3447 | case LYS_LIST: |
| 3448 | if (((struct lys_node_list *)node)->when || ((struct lys_node_list *)node)->must_size) { |
| 3449 | return 1; |
| 3450 | } |
| 3451 | break; |
| 3452 | case LYS_CONTAINER: |
| 3453 | if (((struct lys_node_container *)node)->when || ((struct lys_node_container *)node)->must_size) { |
| 3454 | return 1; |
| 3455 | } |
| 3456 | break; |
| 3457 | case LYS_INPUT: |
| 3458 | case LYS_OUTPUT: |
| 3459 | if (((struct lys_node_inout *)node)->must_size) { |
| 3460 | return 1; |
| 3461 | } |
| 3462 | break; |
| 3463 | case LYS_NOTIF: |
| 3464 | if (((struct lys_node_notif *)node)->must_size) { |
| 3465 | return 1; |
| 3466 | } |
| 3467 | break; |
| 3468 | case LYS_USES: |
| 3469 | if (((struct lys_node_uses *)node)->when) { |
| 3470 | return 1; |
| 3471 | } |
| 3472 | break; |
| 3473 | default: |
| 3474 | /* does not have XPath */ |
| 3475 | break; |
| 3476 | } |
| 3477 | |
| 3478 | return 0; |
| 3479 | } |
| 3480 | |
Michal Vasko | 568b195 | 2018-01-30 15:53:30 +0100 | [diff] [blame] | 3481 | int |
| 3482 | lys_type_is_local(const struct lys_type *type) |
| 3483 | { |
| 3484 | if (!type->der->module) { |
| 3485 | /* build-in type */ |
| 3486 | return 1; |
| 3487 | } |
| 3488 | /* type->parent can be either a typedef or leaf/leaf-list, but module pointers are compatible */ |
| 3489 | return (lys_main_module(type->der->module) == lys_main_module(((struct lys_tpdf *)type->parent)->module)); |
| 3490 | } |
| 3491 | |
Radek Krejci | 2cc2532 | 2017-09-06 16:32:02 +0200 | [diff] [blame] | 3492 | /* |
| 3493 | * shallow - |
| 3494 | * - do not inherit status from the parent |
| 3495 | */ |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3496 | struct lys_node * |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 3497 | 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] | 3498 | struct unres_schema *unres, int shallow) |
| 3499 | { |
| 3500 | struct lys_node *p = NULL; |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 3501 | int finalize = 0; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3502 | struct lys_node *result, *iter, *next; |
| 3503 | |
| 3504 | if (!shallow) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3505 | /* 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] | 3506 | for (p = parent; |
| 3507 | p && !(p->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC | LYS_ACTION | LYS_GROUPING)); |
| 3508 | p = lys_parent(p)); |
| 3509 | finalize = p ? ((p->nodetype == LYS_GROUPING) ? 0 : 2) : 1; |
| 3510 | } |
| 3511 | |
Radek Krejci | 6ff885d | 2017-01-03 14:06:22 +0100 | [diff] [blame] | 3512 | result = lys_node_dup_recursion(module, parent, node, unres, shallow, finalize); |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3513 | if (finalize) { |
| 3514 | /* check xpath expressions in the instantiated tree */ |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3515 | for (iter = next = result; iter; iter = next) { |
Radek Krejci | b314231 | 2016-11-09 11:04:12 +0100 | [diff] [blame] | 3516 | 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] | 3517 | /* invalid xpath */ |
| 3518 | return NULL; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3519 | } |
| 3520 | |
| 3521 | /* select next item */ |
| 3522 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA | LYS_GROUPING)) { |
| 3523 | /* child exception for leafs, leaflists and anyxml without children, ignore groupings */ |
| 3524 | next = NULL; |
| 3525 | } else { |
| 3526 | next = iter->child; |
| 3527 | } |
| 3528 | if (!next) { |
| 3529 | /* no children, try siblings */ |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3530 | if (iter == result) { |
| 3531 | /* we are done, no next element to process */ |
| 3532 | break; |
| 3533 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3534 | next = iter->next; |
| 3535 | } |
| 3536 | while (!next) { |
| 3537 | /* parent is already processed, go to its sibling */ |
| 3538 | iter = lys_parent(iter); |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3539 | if (lys_parent(iter) == lys_parent(result)) { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 3540 | /* we are done, no next element to process */ |
| 3541 | break; |
| 3542 | } |
| 3543 | next = iter->next; |
| 3544 | } |
| 3545 | } |
| 3546 | } |
| 3547 | |
| 3548 | return result; |
| 3549 | } |
| 3550 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3551 | /** |
| 3552 | * @brief Switch contents of two same schema nodes. One of the nodes |
| 3553 | * is expected to be ashallow copy of the other. |
| 3554 | * |
| 3555 | * @param[in] node1 Node whose contents will be switched with \p node2. |
| 3556 | * @param[in] node2 Node whose contents will be switched with \p node1. |
| 3557 | */ |
| 3558 | static void |
| 3559 | lys_node_switch(struct lys_node *node1, struct lys_node *node2) |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3560 | { |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3561 | const size_t mem_size = 104; |
| 3562 | uint8_t mem[mem_size]; |
| 3563 | size_t offset, size; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3564 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3565 | 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] | 3566 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3567 | /* |
| 3568 | * Initially, the nodes were really switched in the tree which |
| 3569 | * caused problems for some other nodes with pointers (augments, leafrefs, ...) |
| 3570 | * because their pointers were not being updated. Code kept in case there is |
| 3571 | * a use of it in future (it took some debugging to cover all the cases). |
| 3572 | |
| 3573 | * sibling next * |
| 3574 | if (node1->prev->next) { |
| 3575 | node1->prev->next = node2; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3576 | } |
| 3577 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3578 | * sibling prev * |
| 3579 | if (node1->next) { |
| 3580 | node1->next->prev = node2; |
Michal Vasko | 8328da8 | 2016-08-25 09:27:22 +0200 | [diff] [blame] | 3581 | } else { |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3582 | for (child = node1->prev; child->prev->next; child = child->prev); |
| 3583 | child->prev = node2; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3584 | } |
| 3585 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3586 | * next * |
| 3587 | node2->next = node1->next; |
| 3588 | node1->next = NULL; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3589 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3590 | * prev * |
| 3591 | if (node1->prev != node1) { |
| 3592 | node2->prev = node1->prev; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3593 | } |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3594 | node1->prev = node1; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3595 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3596 | * parent child * |
| 3597 | if (node1->parent) { |
| 3598 | if (node1->parent->child == node1) { |
| 3599 | node1->parent->child = node2; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 3600 | } |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3601 | } else if (lys_main_module(node1->module)->data == node1) { |
| 3602 | lys_main_module(node1->module)->data = node2; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3603 | } |
| 3604 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3605 | * parent * |
| 3606 | node2->parent = node1->parent; |
| 3607 | node1->parent = NULL; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 3608 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3609 | * child parent * |
| 3610 | LY_TREE_FOR(node1->child, child) { |
| 3611 | if (child->parent == node1) { |
| 3612 | child->parent = node2; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3613 | } |
| 3614 | } |
| 3615 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3616 | * child * |
| 3617 | node2->child = node1->child; |
| 3618 | node1->child = NULL; |
| 3619 | */ |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3620 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3621 | /* switch common node part */ |
| 3622 | offset = 3 * sizeof(char *); |
| 3623 | size = sizeof(uint16_t) + 6 * sizeof(uint8_t) + sizeof(struct lys_ext_instance **) + sizeof(struct lys_iffeature *); |
| 3624 | memcpy(mem, ((uint8_t *)node1) + offset, size); |
| 3625 | memcpy(((uint8_t *)node1) + offset, ((uint8_t *)node2) + offset, size); |
| 3626 | memcpy(((uint8_t *)node2) + offset, mem, size); |
| 3627 | |
| 3628 | /* switch node-specific data */ |
| 3629 | offset = sizeof(struct lys_node); |
| 3630 | switch (node1->nodetype) { |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3631 | case LYS_CONTAINER: |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3632 | size = sizeof(struct lys_node_container) - offset; |
| 3633 | break; |
| 3634 | case LYS_CHOICE: |
| 3635 | size = sizeof(struct lys_node_choice) - offset; |
| 3636 | break; |
| 3637 | case LYS_LEAF: |
| 3638 | size = sizeof(struct lys_node_leaf) - offset; |
| 3639 | break; |
| 3640 | case LYS_LEAFLIST: |
| 3641 | size = sizeof(struct lys_node_leaflist) - offset; |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3642 | break; |
| 3643 | case LYS_LIST: |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3644 | size = sizeof(struct lys_node_list) - offset; |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3645 | break; |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3646 | case LYS_ANYDATA: |
| 3647 | case LYS_ANYXML: |
| 3648 | size = sizeof(struct lys_node_anydata) - offset; |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3649 | break; |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3650 | case LYS_CASE: |
| 3651 | size = sizeof(struct lys_node_case) - offset; |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3652 | break; |
| 3653 | case LYS_INPUT: |
| 3654 | case LYS_OUTPUT: |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3655 | size = sizeof(struct lys_node_inout) - offset; |
| 3656 | break; |
| 3657 | case LYS_NOTIF: |
| 3658 | size = sizeof(struct lys_node_notif) - offset; |
| 3659 | break; |
| 3660 | case LYS_RPC: |
| 3661 | case LYS_ACTION: |
| 3662 | size = sizeof(struct lys_node_rpc_action) - offset; |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3663 | break; |
| 3664 | default: |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3665 | assert(0); |
| 3666 | LOGINT(node1->module->ctx); |
| 3667 | return; |
| 3668 | } |
| 3669 | assert(size <= mem_size); |
| 3670 | memcpy(mem, ((uint8_t *)node1) + offset, size); |
| 3671 | memcpy(((uint8_t *)node1) + offset, ((uint8_t *)node2) + offset, size); |
| 3672 | memcpy(((uint8_t *)node2) + offset, mem, size); |
| 3673 | |
Michal Vasko | 114ffd4 | 2018-08-03 14:30:36 +0200 | [diff] [blame] | 3674 | /* typedefs were not copied to the backup node, so always reuse them, |
| 3675 | * in leaves/leaf-lists we must correct the type parent pointer */ |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3676 | switch (node1->nodetype) { |
| 3677 | case LYS_CONTAINER: |
| 3678 | ((struct lys_node_container *)node1)->tpdf_size = ((struct lys_node_container *)node2)->tpdf_size; |
| 3679 | ((struct lys_node_container *)node1)->tpdf = ((struct lys_node_container *)node2)->tpdf; |
| 3680 | ((struct lys_node_container *)node2)->tpdf_size = 0; |
| 3681 | ((struct lys_node_container *)node2)->tpdf = NULL; |
| 3682 | break; |
| 3683 | case LYS_LIST: |
| 3684 | ((struct lys_node_list *)node1)->tpdf_size = ((struct lys_node_list *)node2)->tpdf_size; |
| 3685 | ((struct lys_node_list *)node1)->tpdf = ((struct lys_node_list *)node2)->tpdf; |
| 3686 | ((struct lys_node_list *)node2)->tpdf_size = 0; |
| 3687 | ((struct lys_node_list *)node2)->tpdf = NULL; |
| 3688 | break; |
| 3689 | case LYS_RPC: |
| 3690 | case LYS_ACTION: |
| 3691 | ((struct lys_node_rpc_action *)node1)->tpdf_size = ((struct lys_node_rpc_action *)node2)->tpdf_size; |
| 3692 | ((struct lys_node_rpc_action *)node1)->tpdf = ((struct lys_node_rpc_action *)node2)->tpdf; |
| 3693 | ((struct lys_node_rpc_action *)node2)->tpdf_size = 0; |
| 3694 | ((struct lys_node_rpc_action *)node2)->tpdf = NULL; |
| 3695 | break; |
| 3696 | case LYS_NOTIF: |
| 3697 | ((struct lys_node_notif *)node1)->tpdf_size = ((struct lys_node_notif *)node2)->tpdf_size; |
| 3698 | ((struct lys_node_notif *)node1)->tpdf = ((struct lys_node_notif *)node2)->tpdf; |
| 3699 | ((struct lys_node_notif *)node2)->tpdf_size = 0; |
| 3700 | ((struct lys_node_notif *)node2)->tpdf = NULL; |
| 3701 | break; |
| 3702 | case LYS_INPUT: |
| 3703 | case LYS_OUTPUT: |
| 3704 | ((struct lys_node_inout *)node1)->tpdf_size = ((struct lys_node_inout *)node2)->tpdf_size; |
| 3705 | ((struct lys_node_inout *)node1)->tpdf = ((struct lys_node_inout *)node2)->tpdf; |
| 3706 | ((struct lys_node_inout *)node2)->tpdf_size = 0; |
| 3707 | ((struct lys_node_inout *)node2)->tpdf = NULL; |
| 3708 | break; |
Michal Vasko | 114ffd4 | 2018-08-03 14:30:36 +0200 | [diff] [blame] | 3709 | case LYS_LEAF: |
| 3710 | case LYS_LEAFLIST: |
| 3711 | ((struct lys_node_leaf *)node1)->type.parent = (struct lys_tpdf *)node1; |
| 3712 | ((struct lys_node_leaf *)node2)->type.parent = (struct lys_tpdf *)node2; |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 3713 | default: |
Radek Krejci | f0bb360 | 2017-01-25 17:05:08 +0100 | [diff] [blame] | 3714 | break; |
| 3715 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 3716 | } |
| 3717 | |
| 3718 | void |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 3719 | 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] | 3720 | { |
| 3721 | struct ly_ctx *ctx; |
| 3722 | int i; |
| 3723 | |
| 3724 | if (!module) { |
| 3725 | return; |
| 3726 | } |
| 3727 | |
| 3728 | /* remove schema from the context */ |
| 3729 | ctx = module->ctx; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 3730 | if (remove_from_ctx && ctx->models.used) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3731 | for (i = 0; i < ctx->models.used; i++) { |
| 3732 | if (ctx->models.list[i] == module) { |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 3733 | /* move all the models to not change the order in the list */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3734 | ctx->models.used--; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 3735 | 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] | 3736 | ctx->models.list[ctx->models.used] = NULL; |
| 3737 | /* we are done */ |
| 3738 | break; |
| 3739 | } |
| 3740 | } |
| 3741 | } |
| 3742 | |
| 3743 | /* common part with struct ly_submodule */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 3744 | module_free_common(module, private_destructor); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3745 | |
Michal Vasko | 10681e8 | 2018-01-16 14:54:16 +0100 | [diff] [blame] | 3746 | /* include */ |
| 3747 | for (i = 0; i < module->inc_size; i++) { |
| 3748 | lydict_remove(ctx, module->inc[i].dsc); |
| 3749 | lydict_remove(ctx, module->inc[i].ref); |
| 3750 | lys_extension_instances_free(ctx, module->inc[i].ext, module->inc[i].ext_size, private_destructor); |
| 3751 | /* complete submodule free is done only from main module since |
| 3752 | * submodules propagate their includes to the main module */ |
| 3753 | if (free_subs) { |
| 3754 | lys_submodule_free(module->inc[i].submodule, private_destructor); |
| 3755 | } |
| 3756 | } |
| 3757 | free(module->inc); |
| 3758 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3759 | /* specific items to free */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 3760 | lydict_remove(ctx, module->ns); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3761 | |
| 3762 | free(module); |
| 3763 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3764 | |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 3765 | static void |
| 3766 | lys_features_disable_recursive(struct lys_feature *f) |
| 3767 | { |
| 3768 | unsigned int i; |
| 3769 | struct lys_feature *depf; |
| 3770 | |
| 3771 | /* disable the feature */ |
| 3772 | f->flags &= ~LYS_FENABLED; |
| 3773 | |
| 3774 | /* by disabling feature we have to disable also all features that depends on this feature */ |
| 3775 | if (f->depfeatures) { |
| 3776 | for (i = 0; i < f->depfeatures->number; i++) { |
| 3777 | depf = (struct lys_feature *)f->depfeatures->set.g[i]; |
| 3778 | if (depf->flags & LYS_FENABLED) { |
| 3779 | lys_features_disable_recursive(depf); |
| 3780 | } |
| 3781 | } |
| 3782 | } |
| 3783 | } |
| 3784 | |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3785 | /* |
| 3786 | * op: 1 - enable, 0 - disable |
| 3787 | */ |
| 3788 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3789 | 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] | 3790 | { |
| 3791 | int all = 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3792 | int i, j, k; |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3793 | int progress, faili, failj, failk; |
| 3794 | |
| 3795 | uint8_t fsize; |
| 3796 | struct lys_feature *f; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3797 | |
| 3798 | if (!module || !name || !strlen(name)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3799 | LOGARG; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3800 | return EXIT_FAILURE; |
| 3801 | } |
| 3802 | |
| 3803 | if (!strcmp(name, "*")) { |
| 3804 | /* enable all */ |
| 3805 | all = 1; |
| 3806 | } |
| 3807 | |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3808 | progress = failk = 1; |
| 3809 | while (progress && failk) { |
| 3810 | for (i = -1, failk = progress = 0; i < module->inc_size; i++) { |
| 3811 | if (i == -1) { |
| 3812 | fsize = module->features_size; |
| 3813 | f = module->features; |
| 3814 | } else { |
| 3815 | fsize = module->inc[i].submodule->features_size; |
| 3816 | f = module->inc[i].submodule->features; |
| 3817 | } |
| 3818 | |
| 3819 | for (j = 0; j < fsize; j++) { |
| 3820 | if (all || !strcmp(f[j].name, name)) { |
Michal Vasko | 34c3b55 | 2016-11-21 09:07:43 +0100 | [diff] [blame] | 3821 | if ((op && (f[j].flags & LYS_FENABLED)) || (!op && !(f[j].flags & LYS_FENABLED))) { |
| 3822 | if (all) { |
| 3823 | /* skip already set features */ |
| 3824 | continue; |
| 3825 | } else { |
| 3826 | /* feature already set correctly */ |
| 3827 | return EXIT_SUCCESS; |
| 3828 | } |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3829 | } |
| 3830 | |
| 3831 | if (op) { |
| 3832 | /* check referenced features if they are enabled */ |
| 3833 | for (k = 0; k < f[j].iffeature_size; k++) { |
| 3834 | if (!resolve_iffeature(&f[j].iffeature[k])) { |
| 3835 | if (all) { |
| 3836 | faili = i; |
| 3837 | failj = j; |
| 3838 | failk = k + 1; |
| 3839 | break; |
| 3840 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3841 | 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] | 3842 | f[j].name, k + 1); |
| 3843 | return EXIT_FAILURE; |
| 3844 | } |
| 3845 | } |
| 3846 | } |
| 3847 | |
| 3848 | if (k == f[j].iffeature_size) { |
| 3849 | /* the last check passed, do the change */ |
| 3850 | f[j].flags |= LYS_FENABLED; |
| 3851 | progress++; |
| 3852 | } |
| 3853 | } else { |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 3854 | lys_features_disable_recursive(&f[j]); |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3855 | progress++; |
| 3856 | } |
| 3857 | if (!all) { |
| 3858 | /* stop in case changing a single feature */ |
| 3859 | return EXIT_SUCCESS; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 3860 | } |
| 3861 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3862 | } |
| 3863 | } |
| 3864 | } |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3865 | if (failk) { |
| 3866 | /* print info about the last failing feature */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3867 | 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] | 3868 | faili == -1 ? module->features[failj].name : module->inc[faili].submodule->features[failj].name, failk); |
| 3869 | return EXIT_FAILURE; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3870 | } |
| 3871 | |
| 3872 | if (all) { |
| 3873 | return EXIT_SUCCESS; |
| 3874 | } else { |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 3875 | /* the specified feature not found */ |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3876 | return EXIT_FAILURE; |
| 3877 | } |
| 3878 | } |
| 3879 | |
| 3880 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3881 | lys_features_enable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3882 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3883 | return lys_features_change(module, feature, 1); |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3887 | lys_features_disable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3888 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3889 | return lys_features_change(module, feature, 0); |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3890 | } |
| 3891 | |
| 3892 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3893 | lys_features_state(const struct lys_module *module, const char *feature) |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3894 | { |
| 3895 | int i, j; |
| 3896 | |
| 3897 | if (!module || !feature) { |
| 3898 | return -1; |
| 3899 | } |
| 3900 | |
| 3901 | /* search for the specified feature */ |
| 3902 | /* module itself */ |
| 3903 | for (i = 0; i < module->features_size; i++) { |
| 3904 | if (!strcmp(feature, module->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3905 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3906 | return 1; |
| 3907 | } else { |
| 3908 | return 0; |
| 3909 | } |
| 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | /* submodules */ |
| 3914 | for (j = 0; j < module->inc_size; j++) { |
| 3915 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
| 3916 | if (!strcmp(feature, module->inc[j].submodule->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3917 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3918 | return 1; |
| 3919 | } else { |
| 3920 | return 0; |
| 3921 | } |
| 3922 | } |
| 3923 | } |
| 3924 | } |
| 3925 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3926 | /* feature definition not found */ |
| 3927 | return -1; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 3928 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3929 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3930 | API const char ** |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3931 | lys_features_list(const struct lys_module *module, uint8_t **states) |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3932 | { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3933 | const char **result = NULL; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3934 | int i, j; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3935 | unsigned int count; |
| 3936 | |
| 3937 | if (!module) { |
| 3938 | return NULL; |
| 3939 | } |
| 3940 | |
| 3941 | count = module->features_size; |
| 3942 | for (i = 0; i < module->inc_size; i++) { |
| 3943 | count += module->inc[i].submodule->features_size; |
| 3944 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3945 | result = malloc((count + 1) * sizeof *result); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3946 | LY_CHECK_ERR_RETURN(!result, LOGMEM(module->ctx), NULL); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3947 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3948 | if (states) { |
| 3949 | *states = malloc((count + 1) * sizeof **states); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 3950 | LY_CHECK_ERR_RETURN(!(*states), LOGMEM(module->ctx); free(result), NULL); |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3951 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3952 | count = 0; |
| 3953 | |
| 3954 | /* module itself */ |
| 3955 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3956 | result[count] = module->features[i].name; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3957 | if (states) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3958 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3959 | (*states)[count] = 1; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3960 | } else { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3961 | (*states)[count] = 0; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3962 | } |
| 3963 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3964 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3965 | } |
| 3966 | |
| 3967 | /* submodules */ |
| 3968 | for (j = 0; j < module->inc_size; j++) { |
| 3969 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3970 | result[count] = module->inc[j].submodule->features[i].name; |
Radek Krejci | 374b94e | 2015-08-13 09:44:22 +0200 | [diff] [blame] | 3971 | if (states) { |
| 3972 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
| 3973 | (*states)[count] = 1; |
| 3974 | } else { |
| 3975 | (*states)[count] = 0; |
| 3976 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3977 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3978 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3979 | } |
| 3980 | } |
| 3981 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3982 | /* terminating NULL byte */ |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3983 | result[count] = NULL; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3984 | |
| 3985 | return result; |
| 3986 | } |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3987 | |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 3988 | API struct lys_module * |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 3989 | lys_node_module(const struct lys_node *node) |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3990 | { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 3991 | if (!node) { |
| 3992 | return NULL; |
| 3993 | } |
| 3994 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3995 | return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module; |
| 3996 | } |
| 3997 | |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 3998 | API struct lys_module * |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 3999 | lys_main_module(const struct lys_module *module) |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 4000 | { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 4001 | if (!module) { |
| 4002 | return NULL; |
| 4003 | } |
| 4004 | |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 4005 | return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module); |
| 4006 | } |
| 4007 | |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 4008 | API struct lys_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4009 | lys_parent(const struct lys_node *node) |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 4010 | { |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4011 | struct lys_node *parent; |
| 4012 | |
| 4013 | if (!node) { |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 4014 | return NULL; |
| 4015 | } |
| 4016 | |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4017 | if (node->nodetype == LYS_EXT) { |
| 4018 | if (((struct lys_ext_instance_complex*)node)->parent_type != LYEXT_PAR_NODE) { |
| 4019 | return NULL; |
| 4020 | } |
| 4021 | parent = (struct lys_node*)((struct lys_ext_instance_complex*)node)->parent; |
| 4022 | } else if (!node->parent) { |
| 4023 | return NULL; |
| 4024 | } else { |
| 4025 | parent = node->parent; |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 4026 | } |
| 4027 | |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4028 | if (parent->nodetype == LYS_AUGMENT) { |
| 4029 | return ((struct lys_node_augment *)parent)->target; |
| 4030 | } else { |
| 4031 | return parent; |
| 4032 | } |
| 4033 | } |
| 4034 | |
| 4035 | struct lys_node ** |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 4036 | lys_child(const struct lys_node *node, LYS_NODE nodetype) |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4037 | { |
| 4038 | void *pp; |
| 4039 | assert(node); |
| 4040 | |
| 4041 | if (node->nodetype == LYS_EXT) { |
| 4042 | pp = lys_ext_complex_get_substmt(lys_snode2stmt(nodetype), (struct lys_ext_instance_complex*)node, NULL); |
| 4043 | if (!pp) { |
| 4044 | return NULL; |
| 4045 | } |
| 4046 | return (struct lys_node **)pp; |
Michal Vasko | 54d4c20 | 2017-08-09 14:09:18 +0200 | [diff] [blame] | 4047 | } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Radek Krejci | f7fe2cb | 2017-08-09 10:27:12 +0200 | [diff] [blame] | 4048 | return NULL; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4049 | } else { |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 4050 | return (struct lys_node **)&node->child; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 4051 | } |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 4052 | } |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 4053 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 4054 | API void * |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 4055 | lys_set_private(const struct lys_node *node, void *priv) |
| 4056 | { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 4057 | void *prev; |
| 4058 | |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 4059 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4060 | LOGARG; |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 4061 | return NULL; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 4062 | } |
| 4063 | |
Mislav Novakovic | b5529e5 | 2016-02-29 11:42:43 +0100 | [diff] [blame] | 4064 | prev = node->priv; |
| 4065 | ((struct lys_node *)node)->priv = priv; |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 4066 | |
| 4067 | return prev; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 4068 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4069 | |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 4070 | int |
| 4071 | lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref) |
| 4072 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4073 | struct lys_node_leaf *iter; |
| 4074 | struct ly_ctx *ctx = leafref_target->module->ctx; |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 4075 | |
Michal Vasko | 48a573d | 2016-07-01 11:46:02 +0200 | [diff] [blame] | 4076 | if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4077 | LOGINT(ctx); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 4078 | return -1; |
| 4079 | } |
| 4080 | |
Pavol Vican | 9317515 | 2016-08-30 15:34:44 +0200 | [diff] [blame] | 4081 | /* check for config flag */ |
Radek Krejci | c688ca0 | 2017-03-20 12:54:39 +0100 | [diff] [blame] | 4082 | if (((struct lys_node_leaf*)leafref)->type.info.lref.req != -1 && |
| 4083 | (leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4084 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, leafref, |
Radek Krejci | d831dd4 | 2017-03-16 12:59:30 +0100 | [diff] [blame] | 4085 | "The leafref %s is config but refers to a non-config %s.", |
Pavol Vican | 9317515 | 2016-08-30 15:34:44 +0200 | [diff] [blame] | 4086 | strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype)); |
| 4087 | return -1; |
| 4088 | } |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 4089 | /* check for cycles */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4090 | 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] | 4091 | if ((void *)iter == (void *)leafref) { |
| 4092 | /* cycle detected */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4093 | LOGVAL(ctx, LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref); |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 4094 | return -1; |
| 4095 | } |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 4096 | } |
| 4097 | |
| 4098 | /* create fake child - the ly_set structure to hold the list of |
Michal Vasko | 48a573d | 2016-07-01 11:46:02 +0200 | [diff] [blame] | 4099 | * leafrefs referencing the leaf(-list) */ |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 4100 | if (!leafref_target->backlinks) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4101 | leafref_target->backlinks = (void *)ly_set_new(); |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 4102 | if (!leafref_target->backlinks) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4103 | LOGMEM(ctx); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 4104 | return -1; |
| 4105 | } |
| 4106 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 4107 | ly_set_add(leafref_target->backlinks, leafref, 0); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 4108 | |
| 4109 | return 0; |
| 4110 | } |
| 4111 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 4112 | /* not needed currently */ |
| 4113 | #if 0 |
| 4114 | |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4115 | static const char * |
| 4116 | lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len) |
| 4117 | { |
| 4118 | struct lys_module *prev_mod; |
| 4119 | uint32_t str_len, mod_len, buf_idx; |
| 4120 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4121 | 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] | 4122 | LOGINT; |
| 4123 | return NULL; |
| 4124 | } |
| 4125 | |
| 4126 | buf_idx = buf_len - 1; |
| 4127 | buf[buf_idx] = '\0'; |
| 4128 | |
| 4129 | while (node) { |
| 4130 | if (lys_parent(node)) { |
| 4131 | prev_mod = lys_node_module(lys_parent(node)); |
| 4132 | } else { |
| 4133 | prev_mod = NULL; |
| 4134 | } |
| 4135 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 4136 | 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] | 4137 | str_len = strlen(node->name); |
| 4138 | |
| 4139 | if (prev_mod != node->module) { |
| 4140 | mod_len = strlen(node->module->name); |
| 4141 | } else { |
| 4142 | mod_len = 0; |
| 4143 | } |
| 4144 | |
| 4145 | if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) { |
| 4146 | LOGINT; |
| 4147 | return NULL; |
| 4148 | } |
| 4149 | |
| 4150 | buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len; |
| 4151 | |
| 4152 | buf[buf_idx] = '/'; |
| 4153 | if (mod_len) { |
| 4154 | memcpy(buf + buf_idx + 1, node->module->name, mod_len); |
| 4155 | buf[buf_idx + 1 + mod_len] = ':'; |
| 4156 | } |
| 4157 | memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len); |
| 4158 | } |
| 4159 | |
| 4160 | node = lys_parent(node); |
| 4161 | } |
| 4162 | |
| 4163 | return buf + buf_idx; |
| 4164 | } |
| 4165 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 4166 | #endif |
| 4167 | |
| 4168 | API struct ly_set * |
Michal Vasko | 9ff79aa | 2017-07-03 14:10:32 +0200 | [diff] [blame] | 4169 | 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] | 4170 | { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4171 | struct lyxp_set set; |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 4172 | struct ly_set *ret_set; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4173 | uint32_t i; |
| 4174 | |
Michal Vasko | 9ff79aa | 2017-07-03 14:10:32 +0200 | [diff] [blame] | 4175 | if (!ctx_node || !expr) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4176 | LOGARG; |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 4177 | return NULL; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4178 | } |
| 4179 | |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 4180 | /* adjust the root */ |
Michal Vasko | 9ff79aa | 2017-07-03 14:10:32 +0200 | [diff] [blame] | 4181 | 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] | 4182 | do { |
Michal Vasko | cb45f47 | 2018-02-12 10:47:42 +0100 | [diff] [blame] | 4183 | 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] | 4184 | } 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] | 4185 | } |
| 4186 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4187 | memset(&set, 0, sizeof set); |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4188 | |
| 4189 | if (options & LYXP_MUST) { |
| 4190 | options &= ~LYXP_MUST; |
| 4191 | options |= LYXP_SNODE_MUST; |
| 4192 | } else if (options & LYXP_WHEN) { |
| 4193 | options &= ~LYXP_WHEN; |
| 4194 | options |= LYXP_SNODE_WHEN; |
| 4195 | } else { |
| 4196 | options |= LYXP_SNODE; |
| 4197 | } |
| 4198 | |
Michal Vasko | 9ff79aa | 2017-07-03 14:10:32 +0200 | [diff] [blame] | 4199 | if (lyxp_atomize(expr, ctx_node, ctx_node_type, &set, options, NULL)) { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4200 | free(set.val.snodes); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4201 | 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] | 4202 | return NULL; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4203 | } |
| 4204 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 4205 | ret_set = ly_set_new(); |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4206 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4207 | for (i = 0; i < set.used; ++i) { |
| 4208 | switch (set.val.snodes[i].type) { |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4209 | case LYXP_NODE_ELEM: |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4210 | 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] | 4211 | ly_set_free(ret_set); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4212 | free(set.val.snodes); |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 4213 | return NULL; |
| 4214 | } |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4215 | break; |
| 4216 | default: |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4217 | /* ignore roots, text and attr should not ever appear */ |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4218 | break; |
| 4219 | } |
| 4220 | } |
| 4221 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4222 | free(set.val.snodes); |
| 4223 | return ret_set; |
| 4224 | } |
| 4225 | |
| 4226 | API struct ly_set * |
| 4227 | lys_node_xpath_atomize(const struct lys_node *node, int options) |
| 4228 | { |
| 4229 | const struct lys_node *next, *elem, *parent, *tmp; |
| 4230 | struct lyxp_set set; |
| 4231 | struct ly_set *ret_set; |
| 4232 | uint16_t i; |
| 4233 | |
| 4234 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4235 | LOGARG; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4236 | return NULL; |
| 4237 | } |
| 4238 | |
| 4239 | for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent)); |
| 4240 | if (!parent) { |
| 4241 | /* not in input, output, or notification */ |
| 4242 | return NULL; |
| 4243 | } |
| 4244 | |
| 4245 | ret_set = ly_set_new(); |
Radek Krejci | d9af8d2 | 2016-09-07 18:44:26 +0200 | [diff] [blame] | 4246 | if (!ret_set) { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4247 | return NULL; |
| 4248 | } |
| 4249 | |
| 4250 | LY_TREE_DFS_BEGIN(node, next, elem) { |
Michal Vasko | c04173b | 2018-03-09 10:43:22 +0100 | [diff] [blame] | 4251 | 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] | 4252 | /* elem has no dependencies from other subtrees and local nodes get discarded */ |
| 4253 | goto next_iter; |
| 4254 | } |
| 4255 | |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 4256 | if (lyxp_node_atomize(elem, &set, 0)) { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 4257 | ly_set_free(ret_set); |
| 4258 | free(set.val.snodes); |
| 4259 | return NULL; |
| 4260 | } |
| 4261 | |
| 4262 | for (i = 0; i < set.used; ++i) { |
| 4263 | switch (set.val.snodes[i].type) { |
| 4264 | case LYXP_NODE_ELEM: |
| 4265 | if (options & LYXP_NO_LOCAL) { |
| 4266 | for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp)); |
| 4267 | if (tmp) { |
| 4268 | /* in local subtree, discard */ |
| 4269 | break; |
| 4270 | } |
| 4271 | } |
| 4272 | if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) { |
| 4273 | ly_set_free(ret_set); |
| 4274 | free(set.val.snodes); |
| 4275 | return NULL; |
| 4276 | } |
| 4277 | break; |
| 4278 | default: |
| 4279 | /* ignore roots, text and attr should not ever appear */ |
| 4280 | break; |
| 4281 | } |
| 4282 | } |
| 4283 | |
| 4284 | free(set.val.snodes); |
| 4285 | if (!(options & LYXP_RECURSIVE)) { |
| 4286 | break; |
| 4287 | } |
| 4288 | next_iter: |
| 4289 | LY_TREE_DFS_END(node, next, elem); |
| 4290 | } |
| 4291 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 4292 | return ret_set; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 4293 | } |
| 4294 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4295 | /* logs */ |
| 4296 | int |
| 4297 | apply_aug(struct lys_node_augment *augment, struct unres_schema *unres) |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4298 | { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4299 | struct lys_node *child, *parent; |
| 4300 | int clear_config; |
| 4301 | unsigned int u; |
Michal Vasko | d02e30e | 2018-01-22 13:35:48 +0100 | [diff] [blame] | 4302 | uint8_t *v; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4303 | struct lys_ext_instance *ext; |
Adam Weast | 4ca7edd | 2018-06-14 08:59:22 -0500 | [diff] [blame] | 4304 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4305 | assert(augment->target && (augment->flags & LYS_NOTAPPLIED)); |
| 4306 | |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4307 | if (!augment->child) { |
| 4308 | /* nothing to apply */ |
| 4309 | goto success; |
| 4310 | } |
| 4311 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4312 | /* reconnect augmenting data into the target - add them to the target child list */ |
| 4313 | if (augment->target->child) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4314 | child = augment->target->child->prev; |
| 4315 | child->next = augment->child; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4316 | augment->target->child->prev = augment->child->prev; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4317 | augment->child->prev = child; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4318 | } else { |
| 4319 | augment->target->child = augment->child; |
| 4320 | } |
| 4321 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4322 | /* inherit config information from actual parent */ |
| 4323 | for (parent = augment->target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = lys_parent(parent)); |
| 4324 | clear_config = (parent) ? 1 : 0; |
| 4325 | LY_TREE_FOR(augment->child, child) { |
| 4326 | if (inherit_config_flag(child, augment->target->flags & LYS_CONFIG_MASK, clear_config)) { |
| 4327 | return -1; |
| 4328 | } |
| 4329 | } |
| 4330 | |
| 4331 | /* inherit extensions if any */ |
| 4332 | for (u = 0; u < augment->target->ext_size; u++) { |
| 4333 | ext = augment->target->ext[u]; /* shortcut */ |
| 4334 | if (ext && ext->def->plugin && (ext->def->plugin->flags & LYEXT_OPT_INHERIT)) { |
Michal Vasko | d02e30e | 2018-01-22 13:35:48 +0100 | [diff] [blame] | 4335 | v = malloc(sizeof *v); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4336 | LY_CHECK_ERR_RETURN(!v, LOGMEM(augment->module->ctx), -1); |
Michal Vasko | d02e30e | 2018-01-22 13:35:48 +0100 | [diff] [blame] | 4337 | *v = u; |
| 4338 | if (unres_schema_add_node(lys_main_module(augment->module), unres, &augment->target->ext, |
| 4339 | UNRES_EXT_FINALIZE, (struct lys_node *)v) == -1) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4340 | /* something really bad happend since the extension finalization is not actually |
| 4341 | * being resolved while adding into unres, so something more serious with the unres |
| 4342 | * list itself must happened */ |
| 4343 | return -1; |
| 4344 | } |
| 4345 | } |
| 4346 | } |
| 4347 | |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4348 | success: |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4349 | /* remove the flag about not applicability */ |
| 4350 | augment->flags &= ~LYS_NOTAPPLIED; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4351 | return EXIT_SUCCESS; |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4352 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4353 | |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4354 | static void |
| 4355 | remove_aug(struct lys_node_augment *augment) |
| 4356 | { |
| 4357 | struct lys_node *last, *elem; |
| 4358 | |
Michal Vasko | f1aa47d | 2017-09-21 12:09:29 +0200 | [diff] [blame] | 4359 | if ((augment->flags & LYS_NOTAPPLIED) || !augment->target) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4360 | /* skip already not applied augment */ |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4361 | return; |
| 4362 | } |
| 4363 | |
| 4364 | elem = augment->child; |
| 4365 | if (elem) { |
| 4366 | LY_TREE_FOR(elem, last) { |
| 4367 | if (!last->next || (last->next->parent != (struct lys_node *)augment)) { |
| 4368 | break; |
| 4369 | } |
| 4370 | } |
| 4371 | /* elem is first augment child, last is the last child */ |
| 4372 | |
| 4373 | /* parent child ptr */ |
| 4374 | if (augment->target->child == elem) { |
| 4375 | augment->target->child = last->next; |
| 4376 | } |
| 4377 | |
| 4378 | /* parent child next ptr */ |
| 4379 | if (elem->prev->next) { |
| 4380 | elem->prev->next = last->next; |
| 4381 | } |
| 4382 | |
| 4383 | /* parent child prev ptr */ |
| 4384 | if (last->next) { |
| 4385 | last->next->prev = elem->prev; |
| 4386 | } else if (augment->target->child) { |
| 4387 | augment->target->child->prev = elem->prev; |
| 4388 | } |
| 4389 | |
| 4390 | /* update augment children themselves */ |
| 4391 | elem->prev = last; |
| 4392 | last->next = NULL; |
| 4393 | } |
| 4394 | |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4395 | /* augment->target still keeps the resolved target, but for lys_augment_free() |
| 4396 | * we have to keep information that this augment is not applied to free its data */ |
| 4397 | augment->flags |= LYS_NOTAPPLIED; |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4398 | } |
| 4399 | |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4400 | /* |
| 4401 | * @param[in] module - the module where the deviation is defined |
| 4402 | */ |
| 4403 | static void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4404 | 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] | 4405 | { |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4406 | int ret, reapply = 0; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4407 | char *parent_path; |
| 4408 | struct lys_node *target = NULL, *parent; |
Michal Vasko | 5df038e | 2018-08-02 09:41:26 +0200 | [diff] [blame] | 4409 | struct lys_node_inout *inout; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4410 | struct ly_set *set; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4411 | |
| 4412 | if (!dev->deviate) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4413 | return; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4414 | } |
| 4415 | |
| 4416 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 4417 | if (dev->orig_node) { |
| 4418 | /* removing not-supported deviation ... */ |
| 4419 | if (strrchr(dev->target_name, '/') != dev->target_name) { |
| 4420 | /* ... from a parent */ |
| 4421 | |
| 4422 | /* reconnect to its previous position */ |
| 4423 | parent = dev->orig_node->parent; |
Michal Vasko | a1074a5 | 2018-01-03 12:18:53 +0100 | [diff] [blame] | 4424 | if (parent && (parent->nodetype == LYS_AUGMENT)) { |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4425 | dev->orig_node->parent = NULL; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4426 | /* the original node was actually from augment, we have to get know if the augment is |
| 4427 | * applied (its module is enabled and implemented). If yes, the node will be connected |
| 4428 | * to the augment and the linkage with the target will be fixed if needed, otherwise |
| 4429 | * it will be connected only to the augment */ |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4430 | if (!(parent->flags & LYS_NOTAPPLIED)) { |
| 4431 | /* start with removing augment if applied before adding nodes, we have to make sure |
| 4432 | * that everything will be connect correctly */ |
| 4433 | remove_aug((struct lys_node_augment *)parent); |
| 4434 | reapply = 1; |
| 4435 | } |
| 4436 | /* connect the deviated node back into the augment */ |
Michal Vasko | 8d30dd8 | 2018-09-10 10:06:12 +0200 | [diff] [blame] | 4437 | lys_node_addchild(parent, NULL, dev->orig_node, 0); |
Radek Krejci | c9d7869 | 2017-08-24 17:17:18 +0200 | [diff] [blame] | 4438 | if (reapply) { |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4439 | /* 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] | 4440 | assert(lys_node_module(parent)->implemented); |
Michal Vasko | 823fbe0 | 2017-12-14 11:01:40 +0100 | [diff] [blame] | 4441 | parent->flags |= LYS_NOTAPPLIED; /* allow apply_aug() */ |
| 4442 | apply_aug((struct lys_node_augment *)parent, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4443 | } |
Michal Vasko | a1074a5 | 2018-01-03 12:18:53 +0100 | [diff] [blame] | 4444 | } else if (parent && (parent->nodetype == LYS_USES)) { |
| 4445 | /* uses child */ |
Michal Vasko | 8d30dd8 | 2018-09-10 10:06:12 +0200 | [diff] [blame] | 4446 | lys_node_addchild(parent, NULL, dev->orig_node, 0); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4447 | } else { |
| 4448 | /* non-augment, non-toplevel */ |
| 4449 | 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] | 4450 | ret = resolve_schema_nodeid(parent_path, NULL, module, &set, 0, 1); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4451 | free(parent_path); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4452 | if (ret == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4453 | LOGINT(module->ctx); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4454 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4455 | return; |
| 4456 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4457 | target = set->set.s[0]; |
| 4458 | ly_set_free(set); |
| 4459 | |
Michal Vasko | 8d30dd8 | 2018-09-10 10:06:12 +0200 | [diff] [blame] | 4460 | lys_node_addchild(target, NULL, dev->orig_node, 0); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4461 | } |
| 4462 | } else { |
| 4463 | /* ... from top-level data */ |
Michal Vasko | 8d30dd8 | 2018-09-10 10:06:12 +0200 | [diff] [blame] | 4464 | 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] | 4465 | } |
| 4466 | |
| 4467 | dev->orig_node = NULL; |
| 4468 | } else { |
| 4469 | /* adding not-supported deviation */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4470 | ret = resolve_schema_nodeid(dev->target_name, NULL, module, &set, 0, 1); |
| 4471 | if (ret == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4472 | LOGINT(module->ctx); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4473 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4474 | return; |
| 4475 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4476 | target = set->set.s[0]; |
| 4477 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4478 | |
| 4479 | /* unlink and store the original node */ |
| 4480 | parent = target->parent; |
| 4481 | lys_node_unlink(target); |
Michal Vasko | 5df038e | 2018-08-02 09:41:26 +0200 | [diff] [blame] | 4482 | if (parent) { |
| 4483 | if (parent->nodetype & (LYS_AUGMENT | LYS_USES)) { |
| 4484 | /* hack for augment, because when the original will be sometime reconnected back, we actually need |
| 4485 | * to reconnect it to both - the augment and its target (which is deduced from the deviations target |
| 4486 | * path), so we need to remember the augment as an addition */ |
| 4487 | /* we also need to remember the parent uses so that we connect it back to it when switching deviation state */ |
| 4488 | target->parent = parent; |
| 4489 | } else if (parent->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 4490 | /* re-create implicit node */ |
| 4491 | inout = calloc(1, sizeof *inout); |
| 4492 | LY_CHECK_ERR_RETURN(!inout, LOGMEM(module->ctx), ); |
| 4493 | |
| 4494 | inout->nodetype = target->nodetype; |
| 4495 | inout->name = lydict_insert(module->ctx, (inout->nodetype == LYS_INPUT) ? "input" : "output", 0); |
| 4496 | inout->module = target->module; |
| 4497 | inout->flags = LYS_IMPLICIT; |
| 4498 | |
| 4499 | /* insert it manually */ |
| 4500 | assert(parent->child && !parent->child->next |
| 4501 | && (parent->child->nodetype == (inout->nodetype == LYS_INPUT ? LYS_OUTPUT : LYS_INPUT))); |
| 4502 | parent->child->next = (struct lys_node *)inout; |
| 4503 | inout->prev = parent->child; |
| 4504 | parent->child->prev = (struct lys_node *)inout; |
| 4505 | inout->parent = parent; |
| 4506 | } |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4507 | } |
| 4508 | dev->orig_node = target; |
| 4509 | } |
| 4510 | } else { |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4511 | ret = resolve_schema_nodeid(dev->target_name, NULL, module, &set, 0, 1); |
| 4512 | if (ret == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4513 | LOGINT(module->ctx); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4514 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4515 | return; |
| 4516 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4517 | target = set->set.s[0]; |
| 4518 | ly_set_free(set); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4519 | |
Michal Vasko | 81c7144 | 2018-08-03 12:56:53 +0200 | [diff] [blame] | 4520 | /* contents are switched */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4521 | lys_node_switch(target, dev->orig_node); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4522 | } |
| 4523 | } |
| 4524 | |
| 4525 | /* temporarily removes or applies deviations, updates module deviation flag accordingly */ |
| 4526 | void |
Michal Vasko | 5a65f21 | 2018-03-19 15:08:08 +0100 | [diff] [blame] | 4527 | lys_enable_deviations(struct lys_module *module) |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4528 | { |
| 4529 | uint32_t i = 0, j; |
| 4530 | const struct lys_module *mod; |
| 4531 | const char *ptr; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4532 | struct unres_schema *unres; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4533 | |
| 4534 | if (module->deviated) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4535 | unres = calloc(1, sizeof *unres); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4536 | LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), ); |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4537 | |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4538 | while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) { |
| 4539 | if (mod == module) { |
| 4540 | continue; |
| 4541 | } |
| 4542 | |
| 4543 | for (j = 0; j < mod->deviation_size; ++j) { |
| 4544 | ptr = strstr(mod->deviation[j].target_name, module->name); |
| 4545 | if (ptr && ptr[strlen(module->name)] == ':') { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4546 | lys_switch_deviation(&mod->deviation[j], mod, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4547 | } |
| 4548 | } |
| 4549 | } |
| 4550 | |
Michal Vasko | 5a65f21 | 2018-03-19 15:08:08 +0100 | [diff] [blame] | 4551 | assert(module->deviated == 2); |
| 4552 | module->deviated = 1; |
| 4553 | |
| 4554 | for (j = 0; j < module->inc_size; j++) { |
| 4555 | if (module->inc[j].submodule->deviated) { |
| 4556 | module->inc[j].submodule->deviated = module->deviated; |
| 4557 | } |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4558 | } |
Michal Vasko | 5a65f21 | 2018-03-19 15:08:08 +0100 | [diff] [blame] | 4559 | |
| 4560 | if (unres->count) { |
| 4561 | resolve_unres_schema(module, unres); |
| 4562 | } |
| 4563 | unres_schema_free(module, &unres, 1); |
| 4564 | } |
| 4565 | } |
| 4566 | |
| 4567 | void |
| 4568 | lys_disable_deviations(struct lys_module *module) |
| 4569 | { |
| 4570 | uint32_t i, j; |
| 4571 | const struct lys_module *mod; |
| 4572 | const char *ptr; |
| 4573 | struct unres_schema *unres; |
| 4574 | |
| 4575 | if (module->deviated) { |
| 4576 | unres = calloc(1, sizeof *unres); |
| 4577 | LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), ); |
| 4578 | |
| 4579 | i = module->ctx->models.used; |
| 4580 | while (i--) { |
| 4581 | mod = module->ctx->models.list[i]; |
| 4582 | |
| 4583 | if (mod == module) { |
| 4584 | continue; |
| 4585 | } |
| 4586 | |
| 4587 | j = mod->deviation_size; |
| 4588 | while (j--) { |
| 4589 | ptr = strstr(mod->deviation[j].target_name, module->name); |
| 4590 | if (ptr && ptr[strlen(module->name)] == ':') { |
| 4591 | lys_switch_deviation(&mod->deviation[j], mod, unres); |
| 4592 | } |
| 4593 | } |
| 4594 | } |
| 4595 | |
| 4596 | assert(module->deviated == 1); |
| 4597 | module->deviated = 2; |
| 4598 | |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4599 | for (j = 0; j < module->inc_size; j++) { |
| 4600 | if (module->inc[j].submodule->deviated) { |
| 4601 | module->inc[j].submodule->deviated = module->deviated; |
| 4602 | } |
| 4603 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4604 | |
| 4605 | if (unres->count) { |
| 4606 | resolve_unres_schema(module, unres); |
| 4607 | } |
| 4608 | unres_schema_free(module, &unres, 1); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4609 | } |
| 4610 | } |
| 4611 | |
| 4612 | static void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4613 | 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] | 4614 | { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4615 | lys_switch_deviation(dev, module, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4616 | |
| 4617 | assert(dev->orig_node); |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4618 | lys_node_module(dev->orig_node)->deviated = 1; /* main module */ |
| 4619 | dev->orig_node->module->deviated = 1; /* possible submodule */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4620 | } |
| 4621 | |
| 4622 | static void |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4623 | 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] | 4624 | { |
| 4625 | uint32_t idx = 0, j; |
| 4626 | const struct lys_module *mod; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4627 | struct lys_module *target_mod, *target_submod; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4628 | const char *ptr; |
| 4629 | |
| 4630 | if (dev->orig_node) { |
| 4631 | target_mod = lys_node_module(dev->orig_node); |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4632 | target_submod = dev->orig_node->module; |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4633 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4634 | LOGINT(module->ctx); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4635 | return; |
| 4636 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4637 | lys_switch_deviation(dev, module, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4638 | |
| 4639 | /* clear the deviation flag if possible */ |
| 4640 | while ((mod = ly_ctx_get_module_iter(module->ctx, &idx))) { |
| 4641 | if ((mod == module) || (mod == target_mod)) { |
| 4642 | continue; |
| 4643 | } |
| 4644 | |
| 4645 | for (j = 0; j < mod->deviation_size; ++j) { |
| 4646 | ptr = strstr(mod->deviation[j].target_name, target_mod->name); |
| 4647 | if (ptr && (ptr[strlen(target_mod->name)] == ':')) { |
| 4648 | /* some other module deviation targets the inspected module, flag remains */ |
| 4649 | break; |
| 4650 | } |
| 4651 | } |
| 4652 | |
| 4653 | if (j < mod->deviation_size) { |
| 4654 | break; |
| 4655 | } |
| 4656 | } |
| 4657 | |
| 4658 | if (!mod) { |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4659 | target_mod->deviated = 0; /* main module */ |
| 4660 | target_submod->deviated = 0; /* possible submodule */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4661 | } |
| 4662 | } |
| 4663 | |
| 4664 | void |
| 4665 | lys_sub_module_apply_devs_augs(struct lys_module *module) |
| 4666 | { |
| 4667 | uint8_t u, v; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4668 | struct unres_schema *unres; |
| 4669 | |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4670 | assert(module->implemented); |
| 4671 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4672 | unres = calloc(1, sizeof *unres); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4673 | LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), ); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4674 | |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 4675 | /* apply deviations */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4676 | for (u = 0; u < module->deviation_size; ++u) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4677 | apply_dev(&module->deviation[u], module, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4678 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 4679 | /* apply augments */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4680 | for (u = 0; u < module->augment_size; ++u) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4681 | apply_aug(&module->augment[u], unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4682 | } |
| 4683 | |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 4684 | /* apply deviations and augments defined in submodules */ |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4685 | for (v = 0; v < module->inc_size; ++v) { |
| 4686 | for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4687 | apply_dev(&module->inc[v].submodule->deviation[u], module, unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4688 | } |
| 4689 | |
| 4690 | for (u = 0; u < module->inc[v].submodule->augment_size; ++u) { |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4691 | apply_aug(&module->inc[v].submodule->augment[u], unres); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4692 | } |
| 4693 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4694 | |
| 4695 | if (unres->count) { |
| 4696 | resolve_unres_schema(module, unres); |
| 4697 | } |
| 4698 | /* nothing else left to do even if something is not resolved */ |
| 4699 | unres_schema_free(module, &unres, 1); |
Radek Krejci | 30bfcd2 | 2017-01-27 16:54:48 +0100 | [diff] [blame] | 4700 | } |
| 4701 | |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4702 | void |
| 4703 | lys_sub_module_remove_devs_augs(struct lys_module *module) |
| 4704 | { |
| 4705 | uint8_t u, v; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4706 | struct unres_schema *unres; |
| 4707 | |
| 4708 | unres = calloc(1, sizeof *unres); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4709 | LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), ); |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4710 | |
| 4711 | /* remove applied deviations */ |
| 4712 | for (u = 0; u < module->deviation_size; ++u) { |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 4713 | /* 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] | 4714 | if (module->deviation[u].orig_node) { |
| 4715 | remove_dev(&module->deviation[u], module, unres); |
| 4716 | } |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4717 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4718 | /* remove applied augments */ |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4719 | for (u = 0; u < module->augment_size; ++u) { |
| 4720 | remove_aug(&module->augment[u]); |
| 4721 | } |
| 4722 | |
| 4723 | /* remove deviation and augments defined in submodules */ |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 4724 | for (v = 0; v < module->inc_size && module->inc[v].submodule; ++v) { |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4725 | for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) { |
Michal Vasko | 7427b26 | 2018-05-14 15:23:55 +0200 | [diff] [blame] | 4726 | if (module->inc[v].submodule->deviation[u].orig_node) { |
| 4727 | remove_dev(&module->inc[v].submodule->deviation[u], module, unres); |
| 4728 | } |
Radek Krejci | dbc1526 | 2016-06-16 14:58:29 +0200 | [diff] [blame] | 4729 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4730 | |
Radek Krejci | b2541a3 | 2016-12-12 16:45:57 +0100 | [diff] [blame] | 4731 | for (u = 0; u < module->inc[v].submodule->augment_size; ++u) { |
| 4732 | remove_aug(&module->inc[v].submodule->augment[u]); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4733 | } |
| 4734 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4735 | |
| 4736 | if (unres->count) { |
| 4737 | resolve_unres_schema(module, unres); |
| 4738 | } |
| 4739 | /* nothing else left to do even if something is not resolved */ |
| 4740 | unres_schema_free(module, &unres, 1); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4741 | } |
| 4742 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 4743 | int |
| 4744 | lys_make_implemented_r(struct lys_module *module, struct unres_schema *unres) |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4745 | { |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 4746 | struct ly_ctx *ctx; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4747 | struct lys_node *root, *next, *node; |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4748 | struct lys_module *target_module; |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 4749 | uint16_t i, j, k; |
| 4750 | |
| 4751 | assert(module->implemented); |
| 4752 | ctx = module->ctx; |
| 4753 | |
| 4754 | for (i = 0; i < ctx->models.used; ++i) { |
| 4755 | if (module == ctx->models.list[i]) { |
| 4756 | continue; |
| 4757 | } |
| 4758 | |
| 4759 | if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) { |
| 4760 | LOGERR(ctx, LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name); |
| 4761 | return EXIT_FAILURE; |
| 4762 | } |
| 4763 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4764 | |
| 4765 | for (i = 0; i < module->augment_size; i++) { |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4766 | |
| 4767 | /* make target module implemented if was not */ |
| 4768 | assert(module->augment[i].target); |
| 4769 | target_module = lys_node_module(module->augment[i].target); |
| 4770 | if (!target_module->implemented) { |
| 4771 | target_module->implemented = 1; |
| 4772 | if (unres_schema_add_node(target_module, unres, NULL, UNRES_MOD_IMPLEMENT, NULL) == -1) { |
| 4773 | return -1; |
| 4774 | } |
| 4775 | } |
| 4776 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4777 | /* apply augment */ |
Adam Weast | 368fd58 | 2018-06-14 13:32:35 -0500 | [diff] [blame] | 4778 | 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] | 4779 | return EXIT_FAILURE; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4780 | } |
| 4781 | } |
Adam Weast | 4ca7edd | 2018-06-14 08:59:22 -0500 | [diff] [blame] | 4782 | |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 4783 | /* identities */ |
| 4784 | for (i = 0; i < module->ident_size; i++) { |
| 4785 | for (j = 0; j < module->ident[i].base_size; j++) { |
| 4786 | resolve_identity_backlink_update(&module->ident[i], module->ident[i].base[j]); |
| 4787 | } |
| 4788 | } |
| 4789 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 4790 | /* process augments in submodules */ |
| 4791 | for (i = 0; i < module->inc_size && module->inc[i].submodule; ++i) { |
| 4792 | module->inc[i].submodule->implemented = 1; |
| 4793 | |
| 4794 | for (j = 0; j < module->inc[i].submodule->augment_size; j++) { |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4795 | |
| 4796 | /* make target module implemented if it was not */ |
Michal Vasko | da267cb | 2018-06-15 11:18:00 +0200 | [diff] [blame] | 4797 | assert(module->inc[i].submodule->augment[j].target); |
Michal Vasko | 74083ec | 2018-06-15 10:00:12 +0200 | [diff] [blame] | 4798 | target_module = lys_node_module(module->inc[i].submodule->augment[j].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 | |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 4806 | /* apply augment */ |
Adam Weast | 368fd58 | 2018-06-14 13:32:35 -0500 | [diff] [blame] | 4807 | 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] | 4808 | return EXIT_FAILURE; |
| 4809 | } |
| 4810 | } |
| 4811 | |
| 4812 | /* identities */ |
| 4813 | for (j = 0; j < module->inc[i].submodule->ident_size; j++) { |
| 4814 | for (k = 0; k < module->inc[i].submodule->ident[j].base_size; k++) { |
| 4815 | resolve_identity_backlink_update(&module->inc[i].submodule->ident[j], |
| 4816 | module->inc[i].submodule->ident[j].base[k]); |
| 4817 | } |
| 4818 | } |
| 4819 | } |
| 4820 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4821 | LY_TREE_FOR(module->data, root) { |
| 4822 | /* handle leafrefs and recursively change the implemented flags in the leafref targets */ |
| 4823 | LY_TREE_DFS_BEGIN(root, next, node) { |
| 4824 | if (node->nodetype == LYS_GROUPING) { |
| 4825 | goto nextsibling; |
| 4826 | } |
| 4827 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4828 | if (((struct lys_node_leaf *)node)->type.base == LY_TYPE_LEAFREF) { |
| 4829 | if (unres_schema_add_node(module, unres, &((struct lys_node_leaf *)node)->type, |
| 4830 | UNRES_TYPE_LEAFREF, node) == -1) { |
| 4831 | return EXIT_FAILURE; |
| 4832 | } |
| 4833 | } |
| 4834 | } |
| 4835 | |
| 4836 | /* modified LY_TREE_DFS_END */ |
| 4837 | next = node->child; |
| 4838 | /* child exception for leafs, leaflists and anyxml without children */ |
| 4839 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 4840 | next = NULL; |
| 4841 | } |
| 4842 | if (!next) { |
| 4843 | nextsibling: |
| 4844 | /* no children */ |
| 4845 | if (node == root) { |
| 4846 | /* we are done, root has no children */ |
| 4847 | break; |
| 4848 | } |
| 4849 | /* try siblings */ |
| 4850 | next = node->next; |
| 4851 | } |
| 4852 | while (!next) { |
| 4853 | /* parent is already processed, go to its sibling */ |
| 4854 | node = lys_parent(node); |
| 4855 | /* no siblings, go back through parents */ |
| 4856 | if (lys_parent(node) == lys_parent(root)) { |
| 4857 | /* we are done, no next element to process */ |
| 4858 | break; |
| 4859 | } |
| 4860 | next = node->next; |
| 4861 | } |
| 4862 | } |
| 4863 | } |
| 4864 | |
| 4865 | return EXIT_SUCCESS; |
| 4866 | } |
| 4867 | |
| 4868 | API int |
| 4869 | lys_set_implemented(const struct lys_module *module) |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4870 | { |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4871 | struct unres_schema *unres; |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 4872 | int disabled = 0; |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4873 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4874 | if (!module) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4875 | LOGARG; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4876 | return EXIT_FAILURE; |
| 4877 | } |
| 4878 | |
| 4879 | module = lys_main_module(module); |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4880 | |
| 4881 | if (module->disabled) { |
| 4882 | disabled = 1; |
| 4883 | lys_set_enabled(module); |
| 4884 | } |
| 4885 | |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4886 | if (module->implemented) { |
| 4887 | return EXIT_SUCCESS; |
| 4888 | } |
| 4889 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4890 | unres = calloc(1, sizeof *unres); |
| 4891 | if (!unres) { |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 4892 | LOGMEM(module->ctx); |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4893 | if (disabled) { |
| 4894 | /* set it back disabled */ |
| 4895 | lys_set_disabled(module); |
| 4896 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4897 | return EXIT_FAILURE; |
| 4898 | } |
| 4899 | /* recursively make the module implemented */ |
| 4900 | ((struct lys_module *)module)->implemented = 1; |
Michal Vasko | 0f43706 | 2018-06-08 15:52:39 +0200 | [diff] [blame] | 4901 | if (lys_make_implemented_r((struct lys_module *)module, unres)) { |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4902 | goto error; |
| 4903 | } |
Radek Krejci | 9e6af73 | 2017-04-27 14:40:25 +0200 | [diff] [blame] | 4904 | |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 4905 | /* try again resolve augments in other modules possibly augmenting this one, |
| 4906 | * since we have just enabled it |
| 4907 | */ |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4908 | /* resolve rest of unres items */ |
| 4909 | if (unres->count && resolve_unres_schema((struct lys_module *)module, unres)) { |
| 4910 | goto error; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4911 | } |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4912 | unres_schema_free(NULL, &unres, 0); |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4913 | |
Michal Vasko | be136f6 | 2017-09-21 12:08:39 +0200 | [diff] [blame] | 4914 | LOGVRB("Module \"%s%s%s\" now implemented.", module->name, (module->rev_size ? "@" : ""), |
| 4915 | (module->rev_size ? module->rev[0].date : "")); |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 4916 | return EXIT_SUCCESS; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4917 | |
| 4918 | error: |
Radek Krejci | 0ec51da | 2016-12-14 16:42:03 +0100 | [diff] [blame] | 4919 | if (disabled) { |
| 4920 | /* set it back disabled */ |
| 4921 | lys_set_disabled(module); |
| 4922 | } |
| 4923 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4924 | ((struct lys_module *)module)->implemented = 0; |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 4925 | unres_schema_free((struct lys_module *)module, &unres, 1); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 4926 | return EXIT_FAILURE; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 4927 | } |
| 4928 | |
| 4929 | void |
| 4930 | lys_submodule_module_data_free(struct lys_submodule *submodule) |
| 4931 | { |
| 4932 | struct lys_node *next, *elem; |
| 4933 | |
| 4934 | /* remove parsed data */ |
| 4935 | LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) { |
| 4936 | if (elem->module == (struct lys_module *)submodule) { |
| 4937 | lys_node_free(elem, NULL, 0); |
| 4938 | } |
| 4939 | } |
| 4940 | } |
Radek Krejci | a1c33bf | 2016-09-07 12:38:49 +0200 | [diff] [blame] | 4941 | |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4942 | API char * |
Michal Vasko | 395b0a0 | 2018-01-22 09:36:20 +0100 | [diff] [blame] | 4943 | lys_path(const struct lys_node *node, int options) |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4944 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4945 | char *buf = NULL; |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4946 | |
| 4947 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4948 | LOGARG; |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4949 | return NULL; |
| 4950 | } |
| 4951 | |
Michal Vasko | 185b527 | 2018-09-13 14:26:12 +0200 | [diff] [blame^] | 4952 | if (ly_vlog_build_path(LY_VLOG_LYS, node, &buf, !options, 0)) { |
Michal Vasko | 59631dd | 2017-10-02 11:56:11 +0200 | [diff] [blame] | 4953 | return NULL; |
| 4954 | } |
| 4955 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4956 | return buf; |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 4957 | } |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 4958 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4959 | API char * |
| 4960 | lys_data_path(const struct lys_node *node) |
| 4961 | { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4962 | char *result = NULL, buf[1024]; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 4963 | const char *separator, *name; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4964 | int i, used; |
| 4965 | struct ly_set *set; |
| 4966 | const struct lys_module *prev_mod; |
| 4967 | |
| 4968 | if (!node) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4969 | LOGARG; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4970 | return NULL; |
| 4971 | } |
| 4972 | |
Michal Vasko | d60a1a3 | 2018-05-23 16:31:22 +0200 | [diff] [blame] | 4973 | buf[0] = '\0'; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4974 | set = ly_set_new(); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 4975 | LY_CHECK_ERR_GOTO(!set, LOGMEM(node->module->ctx), cleanup); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4976 | |
| 4977 | while (node) { |
| 4978 | ly_set_add(set, (void *)node, 0); |
| 4979 | do { |
| 4980 | node = lys_parent(node); |
| 4981 | } while (node && (node->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))); |
| 4982 | } |
| 4983 | |
| 4984 | prev_mod = NULL; |
| 4985 | used = 0; |
| 4986 | for (i = set->number - 1; i > -1; --i) { |
| 4987 | node = set->set.s[i]; |
PavolVican | b28bbff | 2018-02-21 00:44:02 +0100 | [diff] [blame] | 4988 | if (node->nodetype == LYS_EXT) { |
| 4989 | if (strcmp(((struct lys_ext_instance *)node)->def->name, "yang-data")) { |
| 4990 | continue; |
| 4991 | } |
| 4992 | name = ((struct lys_ext_instance *)node)->arg_value; |
| 4993 | separator = ":#"; |
| 4994 | } else { |
| 4995 | name = node->name; |
| 4996 | separator = ":"; |
| 4997 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4998 | 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] | 4999 | (lys_node_module(node) == prev_mod ? "" : separator), name); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5000 | prev_mod = lys_node_module(node); |
| 5001 | } |
| 5002 | |
| 5003 | result = strdup(buf); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5004 | LY_CHECK_ERR_GOTO(!result, LOGMEM(node->module->ctx), cleanup); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5005 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 5006 | cleanup: |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5007 | ly_set_free(set); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5008 | return result; |
| 5009 | } |
| 5010 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 5011 | struct lys_node_augment * |
| 5012 | lys_getnext_target_aug(struct lys_node_augment *last, const struct lys_module *mod, const struct lys_node *aug_target) |
| 5013 | { |
| 5014 | int i, j, last_found; |
| 5015 | |
Michal Vasko | b690687 | 2018-03-12 11:35:09 +0100 | [diff] [blame] | 5016 | assert(mod && aug_target); |
| 5017 | |
Michal Vasko | bb52044 | 2017-05-23 10:55:18 +0200 | [diff] [blame] | 5018 | if (!last) { |
| 5019 | last_found = 1; |
| 5020 | } else { |
| 5021 | last_found = 0; |
| 5022 | } |
| 5023 | |
| 5024 | /* search module augments */ |
| 5025 | for (i = 0; i < mod->augment_size; ++i) { |
| 5026 | if (!mod->augment[i].target) { |
| 5027 | /* still unresolved, skip */ |
| 5028 | continue; |
| 5029 | } |
| 5030 | |
| 5031 | if (mod->augment[i].target == aug_target) { |
| 5032 | if (last_found) { |
| 5033 | /* next match after last */ |
| 5034 | return &mod->augment[i]; |
| 5035 | } |
| 5036 | |
| 5037 | if (&mod->augment[i] == last) { |
| 5038 | last_found = 1; |
| 5039 | } |
| 5040 | } |
| 5041 | } |
| 5042 | |
| 5043 | /* search submodule augments */ |
| 5044 | for (i = 0; i < mod->inc_size; ++i) { |
| 5045 | for (j = 0; j < mod->inc[i].submodule->augment_size; ++j) { |
| 5046 | if (!mod->inc[i].submodule->augment[j].target) { |
| 5047 | continue; |
| 5048 | } |
| 5049 | |
| 5050 | if (mod->inc[i].submodule->augment[j].target == aug_target) { |
| 5051 | if (last_found) { |
| 5052 | /* next match after last */ |
| 5053 | return &mod->inc[i].submodule->augment[j]; |
| 5054 | } |
| 5055 | |
| 5056 | if (&mod->inc[i].submodule->augment[j] == last) { |
| 5057 | last_found = 1; |
| 5058 | } |
| 5059 | } |
| 5060 | } |
| 5061 | } |
| 5062 | |
| 5063 | return NULL; |
| 5064 | } |
| 5065 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 5066 | API struct ly_set * |
| 5067 | lys_find_path(const struct lys_module *cur_module, const struct lys_node *cur_node, const char *path) |
| 5068 | { |
| 5069 | struct ly_set *ret; |
| 5070 | int rc; |
| 5071 | |
| 5072 | if ((!cur_module && !cur_node) || !path) { |
| 5073 | return NULL; |
| 5074 | } |
| 5075 | |
| 5076 | rc = resolve_schema_nodeid(path, cur_node, cur_module, &ret, 1, 1); |
| 5077 | if (rc == -1) { |
| 5078 | return NULL; |
| 5079 | } |
| 5080 | |
| 5081 | return ret; |
| 5082 | } |
| 5083 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5084 | static void |
| 5085 | lys_extcomplex_free_str(struct ly_ctx *ctx, struct lys_ext_instance_complex *ext, LY_STMT stmt) |
| 5086 | { |
| 5087 | struct lyext_substmt *info; |
Radek Krejci | b71243e | 2017-02-08 16:20:08 +0100 | [diff] [blame] | 5088 | const char **str, ***a; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5089 | int c; |
| 5090 | |
| 5091 | str = lys_ext_complex_get_substmt(stmt, ext, &info); |
| 5092 | if (!str || !(*str)) { |
| 5093 | return; |
| 5094 | } |
| 5095 | if (info->cardinality >= LY_STMT_CARD_SOME) { |
| 5096 | /* we have array */ |
Radek Krejci | b71243e | 2017-02-08 16:20:08 +0100 | [diff] [blame] | 5097 | a = (const char ***)str; |
Radek Krejci | 56c8041 | 2017-02-09 10:44:16 +0100 | [diff] [blame] | 5098 | for (str = (*(const char ***)str), c = 0; str[c]; c++) { |
| 5099 | lydict_remove(ctx, str[c]); |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5100 | } |
Radek Krejci | 56c8041 | 2017-02-09 10:44:16 +0100 | [diff] [blame] | 5101 | free(a[0]); |
| 5102 | if (stmt == LY_STMT_BELONGSTO) { |
| 5103 | for (str = a[1], c = 0; str[c]; c++) { |
| 5104 | lydict_remove(ctx, str[c]); |
| 5105 | } |
| 5106 | free(a[1]); |
PavolVican | 99c7072 | 2017-02-18 17:25:52 +0100 | [diff] [blame] | 5107 | } else if (stmt == LY_STMT_ARGUMENT) { |
| 5108 | free(a[1]); |
Radek Krejci | 56c8041 | 2017-02-09 10:44:16 +0100 | [diff] [blame] | 5109 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5110 | } else { |
Radek Krejci | 56c8041 | 2017-02-09 10:44:16 +0100 | [diff] [blame] | 5111 | lydict_remove(ctx, str[0]); |
| 5112 | if (stmt == LY_STMT_BELONGSTO) { |
| 5113 | lydict_remove(ctx, str[1]); |
| 5114 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5115 | } |
| 5116 | } |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 5117 | |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5118 | void |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 5119 | lys_extension_instances_free(struct ly_ctx *ctx, struct lys_ext_instance **e, unsigned int size, |
| 5120 | void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5121 | { |
Radek Krejci | f8d05c2 | 2017-02-10 15:33:35 +0100 | [diff] [blame] | 5122 | unsigned int i, j, k; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5123 | struct lyext_substmt *substmt; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5124 | void **pp, **start; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 5125 | struct lys_node *siter, *snext; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5126 | |
| 5127 | #define EXTCOMPLEX_FREE_STRUCT(STMT, TYPE, FUNC, FREE, ARGS...) \ |
Radek Krejci | b84686f | 2017-02-09 16:04:55 +0100 | [diff] [blame] | 5128 | 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] | 5129 | if (!pp || !(*pp)) { break; } \ |
Radek Krejci | b84686f | 2017-02-09 16:04:55 +0100 | [diff] [blame] | 5130 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */ \ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5131 | for (start = pp = *pp; *pp; pp++) { \ |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 5132 | FUNC(ctx, (TYPE *)(*pp), ##ARGS, private_destructor); \ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5133 | if (FREE) { free(*pp); } \ |
| 5134 | } \ |
| 5135 | free(start); \ |
| 5136 | } else { /* single item */ \ |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 5137 | FUNC(ctx, (TYPE *)(*pp), ##ARGS, private_destructor); \ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5138 | if (FREE) { free(*pp); } \ |
| 5139 | } |
| 5140 | |
PavolVican | 9d61d40 | 2018-02-05 15:52:48 +0100 | [diff] [blame] | 5141 | if (!size || !e) { |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5142 | return; |
| 5143 | } |
| 5144 | |
| 5145 | for (i = 0; i < size; i++) { |
| 5146 | if (!e[i]) { |
| 5147 | continue; |
| 5148 | } |
| 5149 | |
| 5150 | if (e[i]->flags & (LYEXT_OPT_INHERIT)) { |
| 5151 | /* no free, this is just a shadow copy of the original extension instance */ |
| 5152 | } else { |
| 5153 | if (e[i]->flags & (LYEXT_OPT_YANG)) { |
| 5154 | free(e[i]->def); /* remove name of instance extension */ |
PavolVican | 19dc615 | 2017-02-06 12:04:15 +0100 | [diff] [blame] | 5155 | e[i]->def = NULL; |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 5156 | 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] | 5157 | } |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 5158 | /* remove private object */ |
| 5159 | if (e[i]->priv && private_destructor) { |
| 5160 | private_destructor((struct lys_node*)e[i], e[i]->priv); |
| 5161 | } |
| 5162 | 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] | 5163 | lydict_remove(ctx, e[i]->arg_value); |
| 5164 | } |
| 5165 | |
fanchanghu | 8d86f6b | 2017-06-10 12:49:54 +0800 | [diff] [blame] | 5166 | if (e[i]->def && e[i]->def->plugin && e[i]->def->plugin->type == LYEXT_COMPLEX |
| 5167 | && ((e[i]->flags & LYEXT_OPT_CONTENT) == 0)) { |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 5168 | substmt = ((struct lys_ext_instance_complex *)e[i])->substmt; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5169 | for (j = 0; substmt[j].stmt; j++) { |
| 5170 | switch(substmt[j].stmt) { |
| 5171 | case LY_STMT_DESCRIPTION: |
| 5172 | case LY_STMT_REFERENCE: |
| 5173 | case LY_STMT_UNITS: |
Radek Krejci | b71243e | 2017-02-08 16:20:08 +0100 | [diff] [blame] | 5174 | case LY_STMT_ARGUMENT: |
| 5175 | case LY_STMT_DEFAULT: |
| 5176 | case LY_STMT_ERRTAG: |
| 5177 | case LY_STMT_ERRMSG: |
| 5178 | case LY_STMT_PREFIX: |
| 5179 | case LY_STMT_NAMESPACE: |
| 5180 | case LY_STMT_PRESENCE: |
| 5181 | case LY_STMT_REVISIONDATE: |
| 5182 | case LY_STMT_KEY: |
| 5183 | case LY_STMT_BASE: |
| 5184 | case LY_STMT_BELONGSTO: |
| 5185 | case LY_STMT_CONTACT: |
| 5186 | case LY_STMT_ORGANIZATION: |
| 5187 | case LY_STMT_PATH: |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5188 | lys_extcomplex_free_str(ctx, (struct lys_ext_instance_complex *)e[i], substmt[j].stmt); |
| 5189 | break; |
| 5190 | case LY_STMT_TYPE: |
| 5191 | EXTCOMPLEX_FREE_STRUCT(LY_STMT_TYPE, struct lys_type, lys_type_free, 1); |
| 5192 | break; |
Radek Krejci | 63fc096 | 2017-02-15 13:20:18 +0100 | [diff] [blame] | 5193 | case LY_STMT_TYPEDEF: |
| 5194 | EXTCOMPLEX_FREE_STRUCT(LY_STMT_TYPEDEF, struct lys_tpdf, lys_tpdf_free, 1); |
| 5195 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5196 | case LY_STMT_IFFEATURE: |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 5197 | 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] | 5198 | break; |
Radek Krejci | 5496fae | 2017-02-10 13:26:48 +0100 | [diff] [blame] | 5199 | case LY_STMT_MAX: |
| 5200 | case LY_STMT_MIN: |
| 5201 | case LY_STMT_POSITION: |
PavolVican | 2ed9f4e | 2017-02-16 00:08:45 +0100 | [diff] [blame] | 5202 | case LY_STMT_VALUE: |
Radek Krejci | f8d05c2 | 2017-02-10 15:33:35 +0100 | [diff] [blame] | 5203 | 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] | 5204 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME && *pp) { |
Radek Krejci | f8d05c2 | 2017-02-10 15:33:35 +0100 | [diff] [blame] | 5205 | for(k = 0; ((uint32_t**)(*pp))[k]; k++) { |
| 5206 | free(((uint32_t**)(*pp))[k]); |
| 5207 | } |
| 5208 | } |
| 5209 | free(*pp); |
| 5210 | break; |
| 5211 | case LY_STMT_DIGITS: |
Radek Krejci | b84686f | 2017-02-09 16:04:55 +0100 | [diff] [blame] | 5212 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { |
| 5213 | /* free the array */ |
| 5214 | pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset]; |
| 5215 | free(*pp); |
| 5216 | } |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5217 | break; |
Radek Krejci | 37f9ba3 | 2017-02-10 16:50:35 +0100 | [diff] [blame] | 5218 | case LY_STMT_MODULE: |
| 5219 | /* modules are part of the context, so they will be freed there */ |
| 5220 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { |
| 5221 | /* free the array */ |
| 5222 | pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset]; |
| 5223 | free(*pp); |
| 5224 | } |
| 5225 | break; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 5226 | case LY_STMT_ACTION: |
Radek Krejci | b31762b | 2017-02-15 10:48:42 +0100 | [diff] [blame] | 5227 | case LY_STMT_ANYDATA: |
| 5228 | case LY_STMT_ANYXML: |
| 5229 | case LY_STMT_CASE: |
| 5230 | case LY_STMT_CHOICE: |
| 5231 | case LY_STMT_CONTAINER: |
| 5232 | case LY_STMT_GROUPING: |
| 5233 | case LY_STMT_INPUT: |
| 5234 | case LY_STMT_LEAF: |
| 5235 | case LY_STMT_LEAFLIST: |
| 5236 | case LY_STMT_LIST: |
| 5237 | case LY_STMT_NOTIFICATION: |
| 5238 | case LY_STMT_OUTPUT: |
| 5239 | case LY_STMT_RPC: |
| 5240 | case LY_STMT_USES: |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 5241 | pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset]; |
| 5242 | LY_TREE_FOR_SAFE((struct lys_node *)(*pp), snext, siter) { |
| 5243 | lys_node_free(siter, NULL, 0); |
| 5244 | } |
Radek Krejci | b31762b | 2017-02-15 10:48:42 +0100 | [diff] [blame] | 5245 | *pp = NULL; |
Radek Krejci | f95b629 | 2017-02-13 15:57:37 +0100 | [diff] [blame] | 5246 | break; |
Radek Krejci | c3f1b6f | 2017-02-15 10:51:10 +0100 | [diff] [blame] | 5247 | case LY_STMT_UNIQUE: |
| 5248 | pp = lys_ext_complex_get_substmt(LY_STMT_UNIQUE, (struct lys_ext_instance_complex *)e[i], NULL); |
| 5249 | if (!pp || !(*pp)) { |
| 5250 | break; |
| 5251 | } |
| 5252 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */ |
| 5253 | for (start = pp = *pp; *pp; pp++) { |
| 5254 | for (k = 0; k < (*(struct lys_unique**)pp)->expr_size; k++) { |
| 5255 | lydict_remove(ctx, (*(struct lys_unique**)pp)->expr[k]); |
| 5256 | } |
| 5257 | free((*(struct lys_unique**)pp)->expr); |
| 5258 | free(*pp); |
| 5259 | } |
| 5260 | free(start); |
| 5261 | } else { /* single item */ |
| 5262 | for (k = 0; k < (*(struct lys_unique**)pp)->expr_size; k++) { |
| 5263 | lydict_remove(ctx, (*(struct lys_unique**)pp)->expr[k]); |
| 5264 | } |
| 5265 | free((*(struct lys_unique**)pp)->expr); |
| 5266 | free(*pp); |
| 5267 | } |
| 5268 | break; |
Radek Krejci | aa9c520 | 2017-02-15 16:10:14 +0100 | [diff] [blame] | 5269 | case LY_STMT_LENGTH: |
| 5270 | case LY_STMT_MUST: |
| 5271 | case LY_STMT_PATTERN: |
| 5272 | case LY_STMT_RANGE: |
| 5273 | EXTCOMPLEX_FREE_STRUCT(substmt[j].stmt, struct lys_restr, lys_restr_free, 1); |
| 5274 | break; |
Radek Krejci | c5cc530 | 2017-02-16 10:07:46 +0100 | [diff] [blame] | 5275 | case LY_STMT_WHEN: |
| 5276 | EXTCOMPLEX_FREE_STRUCT(LY_STMT_WHEN, struct lys_when, lys_when_free, 0); |
| 5277 | break; |
Radek Krejci | 7417a08 | 2017-02-16 11:07:59 +0100 | [diff] [blame] | 5278 | case LY_STMT_REVISION: |
| 5279 | pp = lys_ext_complex_get_substmt(LY_STMT_REVISION, (struct lys_ext_instance_complex *)e[i], NULL); |
| 5280 | if (!pp || !(*pp)) { |
| 5281 | break; |
| 5282 | } |
| 5283 | if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */ |
| 5284 | for (start = pp = *pp; *pp; pp++) { |
| 5285 | lydict_remove(ctx, (*(struct lys_revision**)pp)->dsc); |
| 5286 | lydict_remove(ctx, (*(struct lys_revision**)pp)->ref); |
| 5287 | lys_extension_instances_free(ctx, (*(struct lys_revision**)pp)->ext, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 5288 | (*(struct lys_revision**)pp)->ext_size, private_destructor); |
Radek Krejci | 7417a08 | 2017-02-16 11:07:59 +0100 | [diff] [blame] | 5289 | free(*pp); |
| 5290 | } |
| 5291 | free(start); |
| 5292 | } else { /* single item */ |
| 5293 | lydict_remove(ctx, (*(struct lys_revision**)pp)->dsc); |
| 5294 | lydict_remove(ctx, (*(struct lys_revision**)pp)->ref); |
| 5295 | lys_extension_instances_free(ctx, (*(struct lys_revision**)pp)->ext, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 5296 | (*(struct lys_revision**)pp)->ext_size, private_destructor); |
Radek Krejci | 7417a08 | 2017-02-16 11:07:59 +0100 | [diff] [blame] | 5297 | free(*pp); |
| 5298 | } |
| 5299 | break; |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5300 | default: |
Radek Krejci | b84686f | 2017-02-09 16:04:55 +0100 | [diff] [blame] | 5301 | /* nothing to free */ |
Radek Krejci | 8d6b742 | 2017-02-03 14:42:13 +0100 | [diff] [blame] | 5302 | break; |
| 5303 | } |
| 5304 | } |
| 5305 | } |
| 5306 | |
| 5307 | free(e[i]); |
| 5308 | } |
| 5309 | free(e); |
| 5310 | |
| 5311 | #undef EXTCOMPLEX_FREE_STRUCT |
| 5312 | } |