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