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