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 | |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 40 | static int |
| 41 | lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old, |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 42 | int tpdftype, struct unres_schema *unres); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 43 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 44 | API const struct lys_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 45 | lys_is_disabled(const struct lys_node *node, int recursive) |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 46 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 47 | int i; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 48 | |
| 49 | check: |
| 50 | if (node->nodetype != LYS_INPUT && node->nodetype != LYS_OUTPUT) { |
| 51 | /* input/output does not have if-feature, so skip them */ |
| 52 | |
| 53 | /* check local if-features */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 54 | for (i = 0; i < node->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 55 | if (!resolve_iffeature(&node->iffeature[i])) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 56 | return node; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if (!recursive) { |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | /* go through parents */ |
| 66 | if (node->nodetype == LYS_AUGMENT) { |
| 67 | /* go to parent actually means go to the target node */ |
| 68 | node = ((struct lys_node_augment *)node)->target; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 69 | } else if (node->parent) { |
| 70 | node = node->parent; |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 71 | } else { |
| 72 | return NULL; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 75 | if (recursive == 2) { |
| 76 | /* continue only if the node cannot have a data instance */ |
| 77 | if (node->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST)) { |
| 78 | return NULL; |
| 79 | } |
| 80 | } |
| 81 | goto check; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 84 | int |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 85 | lys_get_sibling(const struct lys_node *siblings, const char *mod_name, int mod_name_len, const char *name, |
| 86 | int nam_len, LYS_NODE type, const struct lys_node **ret) |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 87 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 88 | const struct lys_node *node, *parent = NULL; |
| 89 | const struct lys_module *mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 90 | const char *node_mod_name; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 91 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 92 | assert(siblings && mod_name && name); |
Michal Vasko | 165dc4a | 2015-10-23 09:44:27 +0200 | [diff] [blame] | 93 | assert(!(type & (LYS_USES | LYS_GROUPING))); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 94 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 95 | /* fill the lengths in case the caller is so indifferent */ |
| 96 | if (!mod_name_len) { |
| 97 | mod_name_len = strlen(mod_name); |
| 98 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 99 | if (!nam_len) { |
| 100 | nam_len = strlen(name); |
| 101 | } |
| 102 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 103 | /* set mod correctly */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 104 | parent = lys_parent(siblings); |
| 105 | if (!parent) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 106 | mod = lys_node_module(siblings); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 109 | /* try to find the node */ |
| 110 | node = NULL; |
| 111 | while ((node = lys_getnext(node, parent, mod, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) { |
| 112 | if (!type || (node->nodetype & type)) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 113 | /* module name comparison */ |
| 114 | node_mod_name = lys_node_module(node)->name; |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 115 | 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] | 116 | continue; |
| 117 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 118 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 119 | /* direct name check */ |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 120 | 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] | 121 | if (ret) { |
| 122 | *ret = node; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 123 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 124 | return EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 125 | } |
| 126 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | return EXIT_FAILURE; |
| 130 | } |
| 131 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 132 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 133 | lys_get_data_sibling(const struct lys_module *mod, const struct lys_node *siblings, const char *name, LYS_NODE type, |
| 134 | const struct lys_node **ret) |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 135 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 136 | const struct lys_node *node; |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 137 | |
| 138 | assert(siblings && name); |
| 139 | assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))); |
| 140 | |
| 141 | /* find the beginning */ |
| 142 | while (siblings->prev->next) { |
| 143 | siblings = siblings->prev; |
| 144 | } |
| 145 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 146 | if (!mod) { |
| 147 | mod = siblings->module; |
| 148 | } |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 149 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 150 | /* try to find the node */ |
| 151 | node = NULL; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 152 | while ((node = lys_getnext(node, lys_parent(siblings), mod, 0))) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 153 | if (!type || (node->nodetype & type)) { |
| 154 | /* module check */ |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 155 | if (lys_node_module(node) != lys_main_module(mod)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 156 | continue; |
| 157 | } |
| 158 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 159 | /* direct name check */ |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 160 | if (ly_strequal(node->name, name, 0)) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 161 | if (ret) { |
| 162 | *ret = node; |
| 163 | } |
| 164 | return EXIT_SUCCESS; |
| 165 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 166 | } |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | return EXIT_FAILURE; |
| 170 | } |
| 171 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 172 | API const struct lys_node * |
| 173 | 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] | 174 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 175 | const struct lys_node *next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 176 | |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 177 | if (!last) { |
| 178 | /* first call */ |
| 179 | |
| 180 | /* get know where to start */ |
| 181 | if (parent) { |
| 182 | /* schema subtree */ |
| 183 | next = last = parent->child; |
| 184 | } else { |
| 185 | /* top level data */ |
| 186 | assert(module); |
| 187 | next = last = module->data; |
| 188 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 189 | } else if ((last->nodetype == LYS_USES) && (options & LYS_GETNEXT_INTOUSES) && last->child) { |
| 190 | /* continue with uses content */ |
| 191 | next = last->child; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 192 | } else { |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 193 | /* continue after the last returned value */ |
| 194 | next = last->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | repeat: |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 198 | while (next && (next->nodetype == LYS_GROUPING)) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 199 | if (options & LYS_GETNEXT_WITHGROUPING) { |
| 200 | return next; |
| 201 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 202 | next = next->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 205 | if (!next) { /* cover case when parent is augment */ |
| 206 | if (!last || last->parent == parent || lys_parent(last) == parent) { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 207 | /* no next element */ |
| 208 | return NULL; |
| 209 | } |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 210 | last = lys_parent(last); |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 211 | next = last->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 212 | goto repeat; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 213 | } else { |
| 214 | last = next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | switch (next->nodetype) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 218 | case LYS_INPUT: |
| 219 | case LYS_OUTPUT: |
| 220 | if (options & LYS_GETNEXT_WITHINOUT) { |
| 221 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 222 | } else if (next->child) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 223 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 224 | } else { |
| 225 | next = next->next; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 226 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 227 | goto repeat; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 228 | |
Michal Vasko | a5835e9 | 2015-10-20 15:07:39 +0200 | [diff] [blame] | 229 | case LYS_CASE: |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 230 | if (options & LYS_GETNEXT_WITHCASE) { |
| 231 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 232 | } else if (next->child) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 233 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 234 | } else { |
| 235 | next = next->next; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 236 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 237 | goto repeat; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 238 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 239 | case LYS_USES: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 240 | /* go into */ |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 241 | if (options & LYS_GETNEXT_WITHUSES) { |
| 242 | return next; |
| 243 | } else if (next->child) { |
| 244 | next = next->child; |
| 245 | } else { |
| 246 | next = next->next; |
| 247 | } |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 248 | goto repeat; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 249 | |
Radek Krejci | dfcae7d | 2015-10-20 17:13:01 +0200 | [diff] [blame] | 250 | case LYS_RPC: |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 251 | case LYS_ACTION: |
Radek Krejci | dfcae7d | 2015-10-20 17:13:01 +0200 | [diff] [blame] | 252 | case LYS_NOTIF: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 253 | case LYS_LEAF: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 254 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 255 | case LYS_ANYDATA: |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 256 | case LYS_LIST: |
| 257 | case LYS_LEAFLIST: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 258 | return next; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 259 | |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 260 | case LYS_CONTAINER: |
| 261 | if (!((struct lys_node_container *)next)->presence && (options & LYS_GETNEXT_INTONPCONT)) { |
| 262 | if (next->child) { |
| 263 | /* go into */ |
| 264 | next = next->child; |
| 265 | } else { |
| 266 | next = next->next; |
| 267 | } |
| 268 | goto repeat; |
| 269 | } else { |
| 270 | return next; |
| 271 | } |
| 272 | |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 273 | case LYS_CHOICE: |
| 274 | if (options & LYS_GETNEXT_WITHCHOICE) { |
| 275 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 276 | } else if (next->child) { |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 277 | /* go into */ |
| 278 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 279 | } else { |
| 280 | next = next->next; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 281 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 282 | goto repeat; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 283 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 284 | default: |
| 285 | /* we should not be here */ |
| 286 | return NULL; |
| 287 | } |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 288 | |
| 289 | |
| 290 | } |
| 291 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 292 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 293 | lys_node_unlink(struct lys_node *node) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 294 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 295 | struct lys_node *parent, *first; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 296 | struct lys_module *main_module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 297 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 298 | if (!node) { |
| 299 | return; |
| 300 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 301 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 302 | /* unlink from data model if necessary */ |
| 303 | if (node->module) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 304 | /* get main module with data tree */ |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 305 | main_module = lys_node_module(node); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 306 | if (main_module->data == node) { |
| 307 | main_module->data = node->next; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 308 | } |
| 309 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 310 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 311 | /* store pointers to important nodes */ |
| 312 | parent = node->parent; |
Michal Vasko | 3a9943b | 2015-09-23 11:33:50 +0200 | [diff] [blame] | 313 | if (parent && (parent->nodetype == LYS_AUGMENT)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 314 | /* handle augments - first, unlink it from the augment parent ... */ |
| 315 | if (parent->child == node) { |
| 316 | parent->child = node->next; |
| 317 | } |
| 318 | /* and then continue with the target parent */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 319 | parent = ((struct lys_node_augment *)parent)->target; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 320 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 321 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 322 | /* unlink from parent */ |
| 323 | if (parent) { |
| 324 | if (parent->child == node) { |
| 325 | parent->child = node->next; |
| 326 | } |
| 327 | node->parent = NULL; |
| 328 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 329 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 330 | /* unlink from siblings */ |
| 331 | if (node->prev == node) { |
| 332 | /* there are no more siblings */ |
| 333 | return; |
| 334 | } |
| 335 | if (node->next) { |
| 336 | node->next->prev = node->prev; |
| 337 | } else { |
| 338 | /* unlinking the last element */ |
| 339 | if (parent) { |
| 340 | first = parent->child; |
| 341 | } else { |
| 342 | first = node; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 343 | while (first->prev->next) { |
Michal Vasko | 276f96b | 2015-09-23 11:34:28 +0200 | [diff] [blame] | 344 | first = first->prev; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | first->prev = node->prev; |
| 348 | } |
| 349 | if (node->prev->next) { |
| 350 | node->prev->next = node->next; |
| 351 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 352 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 353 | /* clean up the unlinked element */ |
| 354 | node->next = NULL; |
| 355 | node->prev = node; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 356 | } |
| 357 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 358 | struct lys_node_grp * |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 359 | lys_find_grouping_up(const char *name, struct lys_node *start) |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 360 | { |
| 361 | struct lys_node *par_iter, *iter, *stop; |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 362 | |
| 363 | for (par_iter = start; par_iter; par_iter = par_iter->parent) { |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 364 | /* top-level augment, look into module (uses augment is handled correctly below) */ |
| 365 | if (par_iter->parent && !par_iter->parent->parent && (par_iter->parent->nodetype == LYS_AUGMENT)) { |
| 366 | par_iter = par_iter->parent->module->data; |
| 367 | if (!par_iter) { |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 368 | break; |
| 369 | } |
| 370 | } |
| 371 | |
Michal Vasko | 6f929da | 2015-10-02 16:23:25 +0200 | [diff] [blame] | 372 | 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] | 373 | continue; |
| 374 | } |
| 375 | |
| 376 | for (iter = par_iter, stop = NULL; iter; iter = iter->prev) { |
| 377 | if (!stop) { |
| 378 | stop = par_iter; |
| 379 | } else if (iter == stop) { |
| 380 | break; |
| 381 | } |
| 382 | if (iter->nodetype != LYS_GROUPING) { |
| 383 | continue; |
| 384 | } |
| 385 | |
Radek Krejci | f8426a7 | 2015-10-31 23:14:03 +0100 | [diff] [blame] | 386 | if (!strcmp(name, iter->name)) { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 387 | return (struct lys_node_grp *)iter; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 392 | return NULL; |
| 393 | } |
| 394 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 395 | /* |
| 396 | * get next grouping in the root's subtree, in the |
| 397 | * first call, tha last is NULL |
| 398 | */ |
| 399 | static struct lys_node_grp * |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 400 | 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] | 401 | { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 402 | struct lys_node *last = (struct lys_node *)lastgrp; |
| 403 | struct lys_node *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 404 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 405 | assert(root); |
| 406 | |
| 407 | if (!last) { |
| 408 | last = root; |
| 409 | } |
| 410 | |
| 411 | while (1) { |
| 412 | if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) { |
| 413 | next = last->child; |
| 414 | } else { |
| 415 | next = NULL; |
| 416 | } |
| 417 | if (!next) { |
| 418 | if (last == root) { |
| 419 | /* we are done */ |
| 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | /* no children, go to siblings */ |
| 424 | next = last->next; |
| 425 | } |
| 426 | while (!next) { |
| 427 | /* go back through parents */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 428 | if (lys_parent(last) == root) { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 429 | /* we are done */ |
| 430 | return NULL; |
| 431 | } |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 432 | next = last->next; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 433 | last = lys_parent(last); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | if (next->nodetype == LYS_GROUPING) { |
| 437 | return (struct lys_node_grp *)next; |
| 438 | } |
| 439 | |
| 440 | last = next; |
| 441 | } |
| 442 | } |
| 443 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 444 | /* logs directly */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 445 | int |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 446 | lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module) |
| 447 | { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 448 | struct lys_node *start, *stop, *iter; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 449 | struct lys_node_grp *grp; |
| 450 | int down; |
| 451 | |
| 452 | assert(node); |
| 453 | |
| 454 | if (!parent) { |
| 455 | assert(module); |
| 456 | } else { |
| 457 | module = parent->module; |
| 458 | } |
| 459 | |
| 460 | switch (node->nodetype) { |
| 461 | case LYS_GROUPING: |
| 462 | /* 6.2.1, rule 6 */ |
| 463 | if (parent) { |
| 464 | if (parent->child) { |
| 465 | down = 1; |
| 466 | start = parent->child; |
| 467 | } else { |
| 468 | down = 0; |
| 469 | start = parent; |
| 470 | } |
| 471 | } else { |
| 472 | down = 1; |
| 473 | start = module->data; |
| 474 | } |
| 475 | /* go up */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 476 | if (lys_find_grouping_up(node->name, start)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 477 | LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name); |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 478 | return EXIT_FAILURE; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 479 | } |
| 480 | /* go down, because grouping can be defined after e.g. container in which is collision */ |
| 481 | if (down) { |
| 482 | for (iter = start, stop = NULL; iter; iter = iter->prev) { |
| 483 | if (!stop) { |
| 484 | stop = start; |
| 485 | } else if (iter == stop) { |
| 486 | break; |
| 487 | } |
| 488 | if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) { |
| 489 | continue; |
| 490 | } |
| 491 | |
| 492 | grp = NULL; |
| 493 | while ((grp = lys_get_next_grouping(grp, iter))) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 494 | if (ly_strequal(node->name, grp->name, 1)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 495 | LOGVAL(LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 496 | return EXIT_FAILURE; |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | break; |
| 502 | case LYS_LEAF: |
| 503 | case LYS_LEAFLIST: |
| 504 | case LYS_LIST: |
| 505 | case LYS_CONTAINER: |
| 506 | case LYS_CHOICE: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 507 | case LYS_ANYDATA: |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 508 | /* 6.2.1, rule 7 */ |
| 509 | if (parent) { |
| 510 | iter = parent; |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 511 | while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE | LYS_AUGMENT))) { |
| 512 | if (iter->nodetype == LYS_AUGMENT) { |
| 513 | if (((struct lys_node_augment *)iter)->target) { |
| 514 | /* augment is resolved, go up */ |
| 515 | iter = ((struct lys_node_augment *)iter)->target; |
| 516 | continue; |
| 517 | } |
| 518 | /* augment is not resolved, this is the final parent */ |
| 519 | break; |
| 520 | } |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 521 | iter = iter->parent; |
| 522 | } |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 523 | |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 524 | if (!iter) { |
| 525 | stop = NULL; |
| 526 | iter = module->data; |
| 527 | } else { |
| 528 | stop = iter; |
| 529 | iter = iter->child; |
| 530 | } |
| 531 | } else { |
| 532 | stop = NULL; |
| 533 | iter = module->data; |
| 534 | } |
| 535 | while (iter) { |
| 536 | if (iter->nodetype & (LYS_USES | LYS_CASE)) { |
| 537 | iter = iter->child; |
| 538 | continue; |
| 539 | } |
| 540 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 541 | 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] | 542 | if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 543 | LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, strnodetype(node->nodetype), node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 544 | return EXIT_FAILURE; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /* special case for choice - we must check the choice's name as |
| 549 | * well as the names of nodes under the choice |
| 550 | */ |
| 551 | if (iter->nodetype == LYS_CHOICE) { |
| 552 | iter = iter->child; |
| 553 | continue; |
| 554 | } |
| 555 | |
| 556 | /* go to siblings */ |
| 557 | if (!iter->next) { |
| 558 | /* no sibling, go to parent's sibling */ |
| 559 | do { |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 560 | /* for parent LYS_AUGMENT */ |
| 561 | if (iter->parent == stop) { |
| 562 | iter = stop; |
| 563 | break; |
| 564 | } |
| 565 | iter = lys_parent(iter); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 566 | if (iter && iter->next) { |
| 567 | break; |
| 568 | } |
| 569 | } while (iter != stop); |
| 570 | |
| 571 | if (iter == stop) { |
| 572 | break; |
| 573 | } |
| 574 | } |
| 575 | iter = iter->next; |
| 576 | } |
| 577 | break; |
| 578 | case LYS_CASE: |
| 579 | /* 6.2.1, rule 8 */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 580 | if (parent) { |
| 581 | start = parent->child; |
| 582 | } else { |
| 583 | start = module->data; |
| 584 | } |
| 585 | |
| 586 | LY_TREE_FOR(start, iter) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 587 | 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] | 588 | continue; |
| 589 | } |
| 590 | |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 591 | if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 592 | LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "case", node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 593 | return EXIT_FAILURE; |
| 594 | } |
| 595 | } |
| 596 | break; |
| 597 | default: |
| 598 | /* no check needed */ |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | return EXIT_SUCCESS; |
| 603 | } |
| 604 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 605 | /* logs directly */ |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 606 | int |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 607 | lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child) |
| 608 | { |
Radek Krejci | 9272055 | 2015-10-05 15:28:27 +0200 | [diff] [blame] | 609 | struct lys_node *iter; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 610 | int type; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 611 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 612 | assert(child); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 613 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 614 | if (parent) { |
| 615 | type = parent->nodetype; |
| 616 | module = parent->module; |
| 617 | } else { |
| 618 | assert(module); |
| 619 | type = 0; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 620 | } |
| 621 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 622 | /* checks */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 623 | switch (type) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 624 | case LYS_CONTAINER: |
| 625 | case LYS_LIST: |
| 626 | case LYS_GROUPING: |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 627 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 628 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF | |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 629 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION))) { |
| 630 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
| 631 | return EXIT_FAILURE; |
| 632 | } |
| 633 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 634 | case LYS_USES: |
| 635 | case LYS_INPUT: |
| 636 | case LYS_OUTPUT: |
| 637 | case LYS_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 638 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 639 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 640 | LYS_LEAFLIST | LYS_LIST | LYS_USES))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 641 | 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] | 642 | return EXIT_FAILURE; |
| 643 | } |
| 644 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 645 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 646 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 647 | (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 648 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 649 | return EXIT_FAILURE; |
| 650 | } |
| 651 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 652 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 653 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 654 | (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] | 655 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 656 | return EXIT_FAILURE; |
| 657 | } |
| 658 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 659 | case LYS_RPC: |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 660 | case LYS_ACTION: |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 661 | if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 662 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc"); |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 663 | return EXIT_FAILURE; |
| 664 | } |
| 665 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 666 | case LYS_LEAF: |
| 667 | case LYS_LEAFLIST: |
| 668 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 669 | case LYS_ANYDATA: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 670 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
| 671 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, NULL, "The \"%s\" statement cannot have any data substatement.", |
Michal Vasko | 6ea3e36 | 2016-03-11 10:25:36 +0100 | [diff] [blame] | 672 | strnodetype(parent->nodetype)); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 673 | return EXIT_FAILURE; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 674 | case LYS_AUGMENT: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 675 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 676 | (LYS_ANYDATA | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 677 | | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 678 | 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] | 679 | return EXIT_FAILURE; |
| 680 | } |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 681 | break; |
| 682 | case LYS_UNKNOWN: |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 683 | /* top level */ |
| 684 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 685 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 686 | | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 687 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "(sub)module"); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 688 | return EXIT_FAILURE; |
| 689 | } |
| 690 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 691 | break; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /* check identifier uniqueness */ |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 695 | if (lys_check_id(child, parent, module)) { |
| 696 | return EXIT_FAILURE; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 697 | } |
Radek Krejci | b7155b5 | 2015-06-10 17:03:01 +0200 | [diff] [blame] | 698 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 699 | if (child->parent) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 700 | lys_node_unlink(child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 701 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 702 | |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 703 | /* connect the child correctly */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 704 | if (!parent) { |
Radek Krejci | 9272055 | 2015-10-05 15:28:27 +0200 | [diff] [blame] | 705 | if (module->data) { |
| 706 | module->data->prev->next = child; |
| 707 | child->prev = module->data->prev; |
| 708 | module->data->prev = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 709 | } else { |
Radek Krejci | 9272055 | 2015-10-05 15:28:27 +0200 | [diff] [blame] | 710 | module->data = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 711 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 712 | } else { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 713 | if (!parent->child) { |
| 714 | /* the only/first child of the parent */ |
| 715 | parent->child = child; |
| 716 | child->parent = parent; |
| 717 | iter = child; |
| 718 | } else { |
| 719 | /* add a new child at the end of parent's child list */ |
| 720 | iter = parent->child->prev; |
| 721 | iter->next = child; |
| 722 | child->prev = iter; |
| 723 | } |
| 724 | while (iter->next) { |
| 725 | iter = iter->next; |
| 726 | iter->parent = parent; |
| 727 | } |
| 728 | parent->child->prev = iter; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 729 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 730 | |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 731 | /* check config value */ |
| 732 | if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_AUGMENT))) { |
| 733 | for (iter = child; iter && !(iter->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); iter = iter->parent); |
| 734 | if (!iter && (parent->flags & LYS_CONFIG_R) && (child->flags & LYS_CONFIG_W)) { |
| 735 | LOGVAL(LYE_INARG, LY_VLOG_LYS, child, "true", "config"); |
| 736 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, child, "State nodes cannot have configuration nodes as children."); |
| 737 | return EXIT_FAILURE; |
| 738 | } |
| 739 | } |
| 740 | |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 741 | /* propagate information about status data presence */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 742 | 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] | 743 | (child->flags & LYS_INCL_STATUS)) { |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 744 | for(iter = parent; iter; iter = lys_parent(iter)) { |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 745 | /* store it only into container or list - the only data inner nodes */ |
| 746 | if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
| 747 | if (iter->flags & LYS_INCL_STATUS) { |
| 748 | /* done, someone else set it already from here */ |
| 749 | break; |
| 750 | } |
| 751 | /* set flag about including status data */ |
| 752 | iter->flags |= LYS_INCL_STATUS; |
| 753 | } |
| 754 | } |
| 755 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 756 | return EXIT_SUCCESS; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 757 | } |
| 758 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 759 | static const struct lys_module * |
| 760 | lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int internal) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 761 | { |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 762 | char *enlarged_data = NULL; |
Radek Krejci | 0b5805d | 2015-08-13 09:38:02 +0200 | [diff] [blame] | 763 | struct lys_module *mod = NULL; |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 764 | unsigned int len; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 765 | |
Radek Krejci | f347abc | 2016-06-22 10:18:47 +0200 | [diff] [blame] | 766 | ly_errno = LY_SUCCESS; |
| 767 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 768 | if (!ctx || !data) { |
| 769 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 770 | return NULL; |
| 771 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 772 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 773 | if (!internal && format == LYS_IN_YANG) { |
| 774 | /* enlarge data by 2 bytes for flex */ |
| 775 | len = strlen(data); |
| 776 | enlarged_data = malloc((len + 2) * sizeof *enlarged_data); |
| 777 | if (!enlarged_data) { |
| 778 | LOGMEM; |
| 779 | return NULL; |
| 780 | } |
| 781 | memcpy(enlarged_data, data, len); |
| 782 | enlarged_data[len] = enlarged_data[len + 1] = '\0'; |
| 783 | data = enlarged_data; |
| 784 | } |
| 785 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 786 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 787 | case LYS_IN_YIN: |
Radek Krejci | ff4874d | 2016-03-07 12:30:50 +0100 | [diff] [blame] | 788 | mod = yin_read_module(ctx, data, NULL, 1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 789 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 790 | case LYS_IN_YANG: |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 791 | mod = yang_read_module(ctx, data, 0, NULL, 1); |
| 792 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 793 | default: |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 794 | LOGERR(LY_EINVAL, "Invalid schema input format."); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 795 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 796 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 797 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 798 | free(enlarged_data); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 799 | return mod; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 800 | } |
| 801 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 802 | API const struct lys_module * |
| 803 | lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format) |
| 804 | { |
| 805 | return lys_parse_mem_(ctx, data, format, 0); |
| 806 | } |
| 807 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 808 | struct lys_submodule * |
| 809 | lys_submodule_parse(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] | 810 | { |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 811 | struct lys_submodule *submod = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 812 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 813 | assert(module); |
| 814 | assert(data); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 815 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 816 | /* get the main module */ |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 817 | module = lys_main_module(module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 818 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 819 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 820 | case LYS_IN_YIN: |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 821 | submod = yin_read_submodule(module, data, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 822 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 823 | case LYS_IN_YANG: |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 824 | submod = yang_read_submodule(module, data, 0, unres); |
| 825 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 826 | default: |
Radek Krejci | 90a550a | 2016-04-13 16:00:58 +0200 | [diff] [blame] | 827 | assert(0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 828 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 829 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 830 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 831 | return submod; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 832 | } |
| 833 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 834 | API const struct lys_module * |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 835 | lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format) |
| 836 | { |
| 837 | int fd; |
| 838 | const struct lys_module *ret; |
| 839 | |
| 840 | if (!ctx || !path) { |
| 841 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 842 | return NULL; |
| 843 | } |
| 844 | |
| 845 | fd = open(path, O_RDONLY); |
| 846 | if (fd == -1) { |
| 847 | LOGERR(LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)); |
| 848 | return NULL; |
| 849 | } |
| 850 | |
| 851 | ret = lys_parse_fd(ctx, fd, format); |
| 852 | close(fd); |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 853 | |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 854 | if (ret && !ret->filepath) { |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 855 | /* store URI */ |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 856 | ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0); |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 857 | } |
| 858 | |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 859 | return ret; |
| 860 | } |
| 861 | |
| 862 | API const struct lys_module * |
| 863 | lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format) |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 864 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 865 | const struct lys_module *module; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 866 | struct stat sb; |
| 867 | char *addr; |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 868 | char buf[PATH_MAX]; |
| 869 | int len; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 870 | |
| 871 | if (!ctx || fd < 0) { |
| 872 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 873 | return NULL; |
| 874 | } |
| 875 | |
Radek Krejci | 10a833c | 2015-12-16 15:28:37 +0100 | [diff] [blame] | 876 | if (fstat(fd, &sb) == -1) { |
| 877 | LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno)); |
| 878 | return NULL; |
| 879 | } |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 880 | if (!S_ISREG(sb.st_mode)) { |
| 881 | LOGERR(LY_EINVAL, "Invalid parameter, input file is not a regular file"); |
| 882 | return NULL; |
| 883 | } |
| 884 | |
Michal Vasko | 164d901 | 2016-04-01 10:16:59 +0200 | [diff] [blame] | 885 | if (!sb.st_size) { |
| 886 | LOGERR(LY_EINVAL, "File empty."); |
| 887 | return NULL; |
| 888 | } |
| 889 | |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 890 | addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 891 | if (addr == MAP_FAILED) { |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 892 | LOGERR(LY_EMEM, "Map file into memory failed (%s()).",__func__); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 893 | return NULL; |
| 894 | } |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 895 | module = lys_parse_mem_(ctx, addr, format, 1); |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 896 | munmap(addr, sb.st_size + 2); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 897 | |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 898 | if (module && !module->filepath) { |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 899 | /* get URI if there is /proc */ |
| 900 | addr = NULL; |
Radek Krejci | 15412ca | 2016-03-03 11:16:52 +0100 | [diff] [blame] | 901 | if (asprintf(&addr, "/proc/self/fd/%d", fd) != -1) { |
| 902 | if ((len = readlink(addr, buf, PATH_MAX - 1)) > 0) { |
| 903 | ((struct lys_module *)module)->filepath = lydict_insert(ctx, buf, len); |
| 904 | } |
| 905 | free(addr); |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 906 | } |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 907 | } |
| 908 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 909 | return module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 910 | } |
| 911 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 912 | struct lys_submodule * |
| 913 | lys_submodule_read(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] | 914 | { |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 915 | struct lys_submodule *submodule; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 916 | struct stat sb; |
| 917 | char *addr; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 918 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 919 | assert(module); |
| 920 | assert(fd >= 0); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 921 | |
Radek Krejci | 10a833c | 2015-12-16 15:28:37 +0100 | [diff] [blame] | 922 | if (fstat(fd, &sb) == -1) { |
| 923 | LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno)); |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 924 | return NULL; |
Radek Krejci | 10a833c | 2015-12-16 15:28:37 +0100 | [diff] [blame] | 925 | } |
Michal Vasko | 164d901 | 2016-04-01 10:16:59 +0200 | [diff] [blame] | 926 | |
| 927 | if (!sb.st_size) { |
| 928 | LOGERR(LY_EINVAL, "File empty."); |
| 929 | return NULL; |
| 930 | } |
| 931 | |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 932 | addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 933 | if (addr == MAP_FAILED) { |
| 934 | LOGERR(LY_EMEM,"Map file into memory failed (%s()).",__func__); |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 935 | return NULL; |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 936 | } |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 937 | submodule = lys_submodule_parse(module, addr, format, unres); |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 938 | munmap(addr, sb.st_size + 2); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 939 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 940 | return submodule; |
| 941 | |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 942 | } |
| 943 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 944 | static struct lys_restr * |
| 945 | lys_restr_dup(struct ly_ctx *ctx, struct lys_restr *old, int size) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 946 | { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 947 | struct lys_restr *result; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 948 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 949 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 950 | if (!size) { |
| 951 | return NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 952 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 953 | |
| 954 | result = calloc(size, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 955 | if (!result) { |
Radek Krejci | aa2a893 | 2016-02-17 15:03:14 +0100 | [diff] [blame] | 956 | LOGMEM; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 957 | return NULL; |
| 958 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 959 | for (i = 0; i < size; i++) { |
| 960 | result[i].expr = lydict_insert(ctx, old[i].expr, 0); |
| 961 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 962 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 963 | result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0); |
| 964 | result[i].emsg = lydict_insert(ctx, old[i].emsg, 0); |
| 965 | } |
| 966 | |
| 967 | return result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 968 | } |
| 969 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 970 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 971 | lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr) |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 972 | { |
| 973 | assert(ctx); |
| 974 | if (!restr) { |
| 975 | return; |
| 976 | } |
| 977 | |
| 978 | lydict_remove(ctx, restr->expr); |
| 979 | lydict_remove(ctx, restr->dsc); |
| 980 | lydict_remove(ctx, restr->ref); |
| 981 | lydict_remove(ctx, restr->eapptag); |
| 982 | lydict_remove(ctx, restr->emsg); |
| 983 | } |
| 984 | |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 985 | static void |
| 986 | lys_iffeature_free(struct lys_iffeature *iffeature, uint8_t iffeature_size) |
| 987 | { |
| 988 | uint8_t i; |
| 989 | |
| 990 | for (i = 0; i < iffeature_size; ++i) { |
| 991 | free(iffeature[i].expr); |
| 992 | free(iffeature[i].features); |
| 993 | } |
| 994 | free(iffeature); |
| 995 | } |
| 996 | |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 997 | static int |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 998 | type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old, |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 999 | LY_DATA_TYPE base, int tpdftype, struct unres_schema *unres) |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1000 | { |
| 1001 | int i; |
| 1002 | |
| 1003 | switch (base) { |
| 1004 | case LY_TYPE_BINARY: |
| 1005 | if (old->info.binary.length) { |
| 1006 | new->info.binary.length = lys_restr_dup(mod->ctx, old->info.binary.length, 1); |
| 1007 | } |
| 1008 | break; |
| 1009 | |
| 1010 | case LY_TYPE_BITS: |
| 1011 | new->info.bits.count = old->info.bits.count; |
| 1012 | if (new->info.bits.count) { |
| 1013 | new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit); |
| 1014 | if (!new->info.bits.bit) { |
| 1015 | LOGMEM; |
| 1016 | return -1; |
| 1017 | } |
| 1018 | for (i = 0; i < new->info.bits.count; i++) { |
| 1019 | new->info.bits.bit[i].name = lydict_insert(mod->ctx, old->info.bits.bit[i].name, 0); |
| 1020 | new->info.bits.bit[i].dsc = lydict_insert(mod->ctx, old->info.bits.bit[i].dsc, 0); |
| 1021 | new->info.bits.bit[i].ref = lydict_insert(mod->ctx, old->info.bits.bit[i].ref, 0); |
| 1022 | new->info.bits.bit[i].flags = old->info.bits.bit[i].flags; |
| 1023 | new->info.bits.bit[i].pos = old->info.bits.bit[i].pos; |
| 1024 | } |
| 1025 | } |
| 1026 | break; |
| 1027 | |
| 1028 | case LY_TYPE_DEC64: |
| 1029 | new->info.dec64.dig = old->info.dec64.dig; |
Radek Krejci | 8c3b4b6 | 2016-06-17 14:32:12 +0200 | [diff] [blame] | 1030 | new->info.dec64.div = old->info.dec64.div; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1031 | if (old->info.dec64.range) { |
| 1032 | new->info.dec64.range = lys_restr_dup(mod->ctx, old->info.dec64.range, 1); |
| 1033 | } |
| 1034 | break; |
| 1035 | |
| 1036 | case LY_TYPE_ENUM: |
| 1037 | new->info.enums.count = old->info.enums.count; |
| 1038 | if (new->info.enums.count) { |
| 1039 | new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm); |
| 1040 | if (!new->info.enums.enm) { |
| 1041 | LOGMEM; |
| 1042 | return -1; |
| 1043 | } |
| 1044 | for (i = 0; i < new->info.enums.count; i++) { |
| 1045 | new->info.enums.enm[i].name = lydict_insert(mod->ctx, old->info.enums.enm[i].name, 0); |
| 1046 | new->info.enums.enm[i].dsc = lydict_insert(mod->ctx, old->info.enums.enm[i].dsc, 0); |
| 1047 | new->info.enums.enm[i].ref = lydict_insert(mod->ctx, old->info.enums.enm[i].ref, 0); |
| 1048 | new->info.enums.enm[i].flags = old->info.enums.enm[i].flags; |
| 1049 | new->info.enums.enm[i].value = old->info.enums.enm[i].value; |
| 1050 | } |
| 1051 | } |
| 1052 | break; |
| 1053 | |
| 1054 | case LY_TYPE_IDENT: |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 1055 | if (old->info.ident.count) { |
| 1056 | new->info.ident.ref = malloc(old->info.ident.count * sizeof *new->info.ident.ref); |
| 1057 | if (!new->info.ident.ref) { |
| 1058 | LOGMEM; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1059 | return -1; |
| 1060 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 1061 | memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref); |
| 1062 | } else { |
| 1063 | /* there can be several unresolved base identities, duplicate them all */ |
| 1064 | i = -1; |
| 1065 | while ((i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF)) != -1) { |
| 1066 | if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i]) == -1) { |
| 1067 | return -1; |
| 1068 | } |
| 1069 | --i; |
| 1070 | } |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1071 | } |
| 1072 | break; |
| 1073 | |
| 1074 | case LY_TYPE_INST: |
| 1075 | new->info.inst.req = old->info.inst.req; |
| 1076 | break; |
| 1077 | |
| 1078 | case LY_TYPE_INT8: |
| 1079 | case LY_TYPE_INT16: |
| 1080 | case LY_TYPE_INT32: |
| 1081 | case LY_TYPE_INT64: |
| 1082 | case LY_TYPE_UINT8: |
| 1083 | case LY_TYPE_UINT16: |
| 1084 | case LY_TYPE_UINT32: |
| 1085 | case LY_TYPE_UINT64: |
| 1086 | if (old->info.num.range) { |
| 1087 | new->info.num.range = lys_restr_dup(mod->ctx, old->info.num.range, 1); |
| 1088 | } |
| 1089 | break; |
| 1090 | |
| 1091 | case LY_TYPE_LEAFREF: |
| 1092 | if (old->info.lref.path) { |
| 1093 | new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0); |
Michal Vasko | 5d63140 | 2016-07-21 13:15:15 +0200 | [diff] [blame] | 1094 | if (!tpdftype && unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent) == -1) { |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1095 | return -1; |
| 1096 | } |
| 1097 | } |
| 1098 | break; |
| 1099 | |
| 1100 | case LY_TYPE_STRING: |
| 1101 | if (old->info.str.length) { |
| 1102 | new->info.str.length = lys_restr_dup(mod->ctx, old->info.str.length, 1); |
| 1103 | } |
| 1104 | new->info.str.patterns = lys_restr_dup(mod->ctx, old->info.str.patterns, old->info.str.pat_count); |
| 1105 | new->info.str.pat_count = old->info.str.pat_count; |
| 1106 | break; |
| 1107 | |
| 1108 | case LY_TYPE_UNION: |
| 1109 | new->info.uni.count = old->info.uni.count; |
| 1110 | if (new->info.uni.count) { |
| 1111 | new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types); |
| 1112 | if (!new->info.uni.types) { |
| 1113 | LOGMEM; |
| 1114 | return -1; |
| 1115 | } |
| 1116 | for (i = 0; i < new->info.uni.count; i++) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1117 | if (lys_type_dup(mod, parent, &(new->info.uni.types[i]), &(old->info.uni.types[i]), tpdftype, unres)) { |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1118 | return -1; |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | break; |
| 1123 | |
| 1124 | default: |
| 1125 | /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */ |
| 1126 | break; |
| 1127 | } |
| 1128 | return EXIT_SUCCESS; |
| 1129 | } |
| 1130 | |
| 1131 | struct yang_type * |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1132 | lys_yang_type_dup(struct lys_module *module, struct lys_node *parent, struct yang_type *old, struct lys_type *type, |
| 1133 | int tpdftype, struct unres_schema *unres) |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1134 | { |
| 1135 | struct yang_type *new; |
| 1136 | |
| 1137 | new = calloc(1, sizeof *new); |
| 1138 | if (!new) { |
| 1139 | LOGMEM; |
| 1140 | return NULL; |
| 1141 | } |
| 1142 | new->flags = old->flags; |
| 1143 | new->base = old->base; |
Pavol Vican | 6658629 | 2016-04-07 11:38:52 +0200 | [diff] [blame] | 1144 | new->name = lydict_insert(module->ctx, old->name, 0); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1145 | new->type = type; |
| 1146 | if (!new->name) { |
| 1147 | LOGMEM; |
| 1148 | goto error; |
| 1149 | } |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1150 | if (type_dup(module, parent, type, old->type, new->base, tpdftype, unres)) { |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1151 | new->type->base = new->base; |
| 1152 | lys_type_free(module->ctx, new->type); |
| 1153 | memset(&new->type->info, 0, sizeof new->type->info); |
| 1154 | goto error; |
| 1155 | } |
| 1156 | return new; |
| 1157 | |
| 1158 | error: |
| 1159 | free(new); |
| 1160 | return NULL; |
| 1161 | } |
| 1162 | |
| 1163 | static int |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1164 | lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old, |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1165 | int tpdftype, struct unres_schema *unres) |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1166 | { |
| 1167 | int i; |
| 1168 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1169 | new->module_name = lydict_insert(mod->ctx, old->module_name, 0); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1170 | new->base = old->base; |
| 1171 | new->der = old->der; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1172 | new->parent = (struct lys_tpdf *)parent; |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1173 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 1174 | i = unres_schema_find(unres, -1, old, tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1175 | if (i != -1) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 1176 | /* HACK (serious one) for unres */ |
| 1177 | /* nothing else we can do but duplicate it immediately */ |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1178 | if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1179 | new->der = (struct lys_tpdf *)lys_yang_type_dup(mod, parent, (struct yang_type *)old->der, new, tpdftype, unres); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1180 | } else { |
| 1181 | new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1); |
| 1182 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1183 | /* all these unres additions can fail even though they did not before */ |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1184 | if (!new->der || unres_schema_add_node(mod, unres, new, |
Michal Vasko | 5d63140 | 2016-07-21 13:15:15 +0200 | [diff] [blame] | 1185 | tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER, parent) == -1) { |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1186 | return -1; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 1187 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1188 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1189 | } |
| 1190 | |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1191 | return type_dup(mod, parent, new, old, new->base, tpdftype, unres); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1195 | lys_type_free(struct ly_ctx *ctx, struct lys_type *type) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1196 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1197 | int i; |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1198 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1199 | assert(ctx); |
| 1200 | if (!type) { |
| 1201 | return; |
| 1202 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1203 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1204 | lydict_remove(ctx, type->module_name); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1205 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1206 | switch (type->base) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1207 | case LY_TYPE_BINARY: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1208 | lys_restr_free(ctx, type->info.binary.length); |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1209 | free(type->info.binary.length); |
| 1210 | break; |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 1211 | case LY_TYPE_BITS: |
| 1212 | for (i = 0; i < type->info.bits.count; i++) { |
| 1213 | lydict_remove(ctx, type->info.bits.bit[i].name); |
| 1214 | lydict_remove(ctx, type->info.bits.bit[i].dsc); |
| 1215 | lydict_remove(ctx, type->info.bits.bit[i].ref); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 1216 | lys_iffeature_free(type->info.bits.bit[i].iffeature, type->info.bits.bit[i].iffeature_size); |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 1217 | } |
| 1218 | free(type->info.bits.bit); |
| 1219 | break; |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1220 | |
| 1221 | case LY_TYPE_DEC64: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1222 | lys_restr_free(ctx, type->info.dec64.range); |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1223 | free(type->info.dec64.range); |
| 1224 | break; |
| 1225 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1226 | case LY_TYPE_ENUM: |
| 1227 | for (i = 0; i < type->info.enums.count; i++) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1228 | lydict_remove(ctx, type->info.enums.enm[i].name); |
| 1229 | lydict_remove(ctx, type->info.enums.enm[i].dsc); |
| 1230 | lydict_remove(ctx, type->info.enums.enm[i].ref); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 1231 | lys_iffeature_free(type->info.enums.enm[i].iffeature, type->info.enums.enm[i].iffeature_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1232 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1233 | free(type->info.enums.enm); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1234 | break; |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1235 | |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 1236 | case LY_TYPE_INT8: |
| 1237 | case LY_TYPE_INT16: |
| 1238 | case LY_TYPE_INT32: |
| 1239 | case LY_TYPE_INT64: |
| 1240 | case LY_TYPE_UINT8: |
| 1241 | case LY_TYPE_UINT16: |
| 1242 | case LY_TYPE_UINT32: |
| 1243 | case LY_TYPE_UINT64: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1244 | lys_restr_free(ctx, type->info.num.range); |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 1245 | free(type->info.num.range); |
| 1246 | break; |
| 1247 | |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1248 | case LY_TYPE_LEAFREF: |
| 1249 | lydict_remove(ctx, type->info.lref.path); |
| 1250 | break; |
| 1251 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1252 | case LY_TYPE_STRING: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1253 | lys_restr_free(ctx, type->info.str.length); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1254 | free(type->info.str.length); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1255 | for (i = 0; i < type->info.str.pat_count; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1256 | lys_restr_free(ctx, &type->info.str.patterns[i]); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1257 | } |
| 1258 | free(type->info.str.patterns); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1259 | break; |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1260 | |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1261 | case LY_TYPE_UNION: |
| 1262 | for (i = 0; i < type->info.uni.count; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1263 | lys_type_free(ctx, &type->info.uni.types[i]); |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1264 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1265 | free(type->info.uni.types); |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1266 | break; |
| 1267 | |
Michal Vasko | d328219 | 2016-09-05 11:27:57 +0200 | [diff] [blame] | 1268 | case LY_TYPE_IDENT: |
| 1269 | free(type->info.ident.ref); |
| 1270 | break; |
| 1271 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1272 | default: |
Michal Vasko | d328219 | 2016-09-05 11:27:57 +0200 | [diff] [blame] | 1273 | /* 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] | 1274 | break; |
| 1275 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1276 | } |
| 1277 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1278 | static void |
| 1279 | lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1280 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1281 | assert(ctx); |
| 1282 | if (!tpdf) { |
| 1283 | return; |
| 1284 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1285 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1286 | lydict_remove(ctx, tpdf->name); |
| 1287 | lydict_remove(ctx, tpdf->dsc); |
| 1288 | lydict_remove(ctx, tpdf->ref); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1289 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1290 | lys_type_free(ctx, &tpdf->type); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1291 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1292 | lydict_remove(ctx, tpdf->units); |
| 1293 | lydict_remove(ctx, tpdf->dflt); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1294 | } |
| 1295 | |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1296 | static struct lys_tpdf * |
| 1297 | lys_tpdf_dup(struct lys_module *mod, struct lys_node *parent, struct lys_tpdf *old, int size, struct unres_schema *unres) |
| 1298 | { |
| 1299 | struct lys_tpdf *result; |
| 1300 | int i, j; |
| 1301 | |
| 1302 | if (!size) { |
| 1303 | return NULL; |
| 1304 | } |
| 1305 | |
| 1306 | result = calloc(size, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1307 | if (!result) { |
| 1308 | LOGMEM; |
| 1309 | return NULL; |
| 1310 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1311 | for (i = 0; i < size; i++) { |
| 1312 | result[i].name = lydict_insert(mod->ctx, old[i].name, 0); |
| 1313 | result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0); |
| 1314 | result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0); |
| 1315 | result[i].flags = old[i].flags; |
| 1316 | result[i].module = old[i].module; |
| 1317 | |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1318 | if (lys_type_dup(mod, parent, &(result[i].type), &(old[i].type), 1, unres)) { |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1319 | for (j = 0; j <= i; ++j) { |
| 1320 | lys_tpdf_free(mod->ctx, &result[j]); |
| 1321 | } |
| 1322 | free(result); |
| 1323 | return NULL; |
| 1324 | } |
| 1325 | |
| 1326 | result[i].dflt = lydict_insert(mod->ctx, old[i].dflt, 0); |
| 1327 | result[i].units = lydict_insert(mod->ctx, old[i].units, 0); |
| 1328 | } |
| 1329 | |
| 1330 | return result; |
| 1331 | } |
| 1332 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1333 | static struct lys_when * |
| 1334 | lys_when_dup(struct ly_ctx *ctx, struct lys_when *old) |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1335 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1336 | struct lys_when *new; |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1337 | |
| 1338 | if (!old) { |
| 1339 | return NULL; |
| 1340 | } |
| 1341 | |
| 1342 | new = calloc(1, sizeof *new); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1343 | if (!new) { |
| 1344 | LOGMEM; |
| 1345 | return NULL; |
| 1346 | } |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1347 | new->cond = lydict_insert(ctx, old->cond, 0); |
| 1348 | new->dsc = lydict_insert(ctx, old->dsc, 0); |
| 1349 | new->ref = lydict_insert(ctx, old->ref, 0); |
| 1350 | |
| 1351 | return new; |
| 1352 | } |
| 1353 | |
Michal Vasko | 0308dd6 | 2015-10-07 09:14:40 +0200 | [diff] [blame] | 1354 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1355 | lys_when_free(struct ly_ctx *ctx, struct lys_when *w) |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1356 | { |
| 1357 | if (!w) { |
| 1358 | return; |
| 1359 | } |
| 1360 | |
| 1361 | lydict_remove(ctx, w->cond); |
| 1362 | lydict_remove(ctx, w->dsc); |
| 1363 | lydict_remove(ctx, w->ref); |
| 1364 | |
| 1365 | free(w); |
| 1366 | } |
| 1367 | |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1368 | static void |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1369 | lys_augment_free(struct ly_ctx *ctx, struct lys_node_augment *aug, void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1370 | { |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 1371 | struct lys_node *next, *sub; |
| 1372 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1373 | /* children from a resolved augment are freed under the target node */ |
Michal Vasko | d2fbade | 2016-05-02 17:14:00 +0200 | [diff] [blame] | 1374 | if (!aug->target) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1375 | LY_TREE_FOR_SAFE(aug->child, next, sub) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1376 | lys_node_free(sub, private_destructor, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1377 | } |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 1378 | } |
| 1379 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1380 | lydict_remove(ctx, aug->target_name); |
| 1381 | lydict_remove(ctx, aug->dsc); |
| 1382 | lydict_remove(ctx, aug->ref); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1383 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1384 | lys_iffeature_free(aug->iffeature, aug->iffeature_size); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1385 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1386 | lys_when_free(ctx, aug->when); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1387 | } |
| 1388 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1389 | static struct lys_node_augment * |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1390 | lys_augment_dup(struct lys_module *module, struct lys_node *parent, struct lys_node_augment *old, int size) |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1391 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1392 | struct lys_node_augment *new = NULL; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1393 | struct lys_node *old_child, *new_child; |
| 1394 | int i; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1395 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1396 | if (!size) { |
| 1397 | return NULL; |
| 1398 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1399 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1400 | new = calloc(size, sizeof *new); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1401 | if (!new) { |
| 1402 | LOGMEM; |
| 1403 | return NULL; |
| 1404 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1405 | for (i = 0; i < size; i++) { |
| 1406 | new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0); |
| 1407 | new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0); |
| 1408 | new[i].ref = lydict_insert(module->ctx, old[i].ref, 0); |
| 1409 | new[i].flags = old[i].flags; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1410 | new[i].module = old[i].module; |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 1411 | new[i].nodetype = old[i].nodetype; |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1412 | |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1413 | /* this must succeed, it was already resolved once */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1414 | if (resolve_augment_schema_nodeid(new[i].target_name, parent->child, NULL, |
| 1415 | (const struct lys_node **)&new[i].target)) { |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1416 | LOGINT; |
| 1417 | free(new); |
| 1418 | return NULL; |
| 1419 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1420 | new[i].parent = parent; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1421 | |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1422 | /* Correct the augment nodes. |
| 1423 | * This function can only be called from lys_node_dup() with uses |
| 1424 | * being the node duplicated, so we must have a case of grouping |
| 1425 | * with a uses with augments. The augmented nodes have already been |
| 1426 | * copied and everything is almost fine except their parent is wrong |
Michal Vasko | a5900c5 | 2015-09-24 13:17:11 +0200 | [diff] [blame] | 1427 | * (it was set to their actual data parent, not an augment), and |
| 1428 | * the new augment does not have child pointer to its augment nodes, |
| 1429 | * so we just correct it. |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1430 | */ |
| 1431 | LY_TREE_FOR(new[i].target->child, new_child) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 1432 | if (ly_strequal(new_child->name, old[i].child->name, 1)) { |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1433 | break; |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1434 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1435 | } |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1436 | assert(new_child); |
Michal Vasko | a5900c5 | 2015-09-24 13:17:11 +0200 | [diff] [blame] | 1437 | new[i].child = new_child; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1438 | LY_TREE_FOR(old[i].child, old_child) { |
| 1439 | /* all augment nodes were connected as siblings, there can be no more after this */ |
| 1440 | if (old_child->parent != (struct lys_node *)&old[i]) { |
| 1441 | break; |
| 1442 | } |
| 1443 | |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 1444 | assert(ly_strequal(old_child->name, new_child->name, 1)); |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1445 | |
| 1446 | new_child->parent = (struct lys_node *)&new[i]; |
| 1447 | new_child = new_child->next; |
| 1448 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1449 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1450 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1451 | return new; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1452 | } |
| 1453 | |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 1454 | static const char ** |
| 1455 | lys_dflt_dup(struct ly_ctx *ctx, const char **old, int size) |
| 1456 | { |
| 1457 | int i; |
| 1458 | const char **result; |
| 1459 | |
| 1460 | if (!size) { |
| 1461 | return NULL; |
| 1462 | } |
| 1463 | |
| 1464 | result = calloc(size, sizeof *result); |
| 1465 | if (!result) { |
| 1466 | LOGMEM; |
| 1467 | return NULL; |
| 1468 | } |
| 1469 | |
| 1470 | for (i = 0; i < size; i++) { |
| 1471 | result[i] = lydict_insert(ctx, old[i], 0); |
| 1472 | } |
| 1473 | return result; |
| 1474 | } |
| 1475 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1476 | static struct lys_refine * |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 1477 | lys_refine_dup(struct lys_module *mod, struct lys_refine *old, int size) |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1478 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1479 | struct lys_refine *result; |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 1480 | int i; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1481 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1482 | if (!size) { |
| 1483 | return NULL; |
| 1484 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1485 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1486 | result = calloc(size, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1487 | if (!result) { |
| 1488 | LOGMEM; |
| 1489 | return NULL; |
| 1490 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1491 | for (i = 0; i < size; i++) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1492 | result[i].target_name = lydict_insert(mod->ctx, old[i].target_name, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1493 | result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0); |
| 1494 | result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1495 | result[i].flags = old[i].flags; |
| 1496 | result[i].target_type = old[i].target_type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1497 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1498 | result[i].must_size = old[i].must_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1499 | result[i].must = lys_restr_dup(mod->ctx, old[i].must, old[i].must_size); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1500 | |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 1501 | result[i].dflt_size = old[i].dflt_size; |
| 1502 | result[i].dflt = lys_dflt_dup(mod->ctx, old[i].dflt, old[i].dflt_size); |
| 1503 | |
| 1504 | if (result[i].target_type == LYS_CONTAINER) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1505 | result[i].mod.presence = lydict_insert(mod->ctx, old[i].mod.presence, 0); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1506 | } else if (result[i].target_type & (LYS_LIST | LYS_LEAFLIST)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1507 | result[i].mod.list = old[i].mod.list; |
| 1508 | } |
| 1509 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1510 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1511 | return result; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1512 | } |
| 1513 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1514 | static void |
| 1515 | lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident) |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1516 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1517 | assert(ctx); |
| 1518 | if (!ident) { |
| 1519 | return; |
| 1520 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1521 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 1522 | free(ident->base); |
Radek Krejci | 1b61d0e | 2016-04-15 13:55:44 +0200 | [diff] [blame] | 1523 | free(ident->der); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1524 | lydict_remove(ctx, ident->name); |
| 1525 | lydict_remove(ctx, ident->dsc); |
| 1526 | lydict_remove(ctx, ident->ref); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 1527 | lys_iffeature_free(ident->iffeature, ident->iffeature_size); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1528 | |
| 1529 | } |
| 1530 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1531 | static void |
| 1532 | lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1533 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1534 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1535 | |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1536 | /* handle only specific parts for LYS_GROUPING */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1537 | for (i = 0; i < grp->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1538 | lys_tpdf_free(ctx, &grp->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1539 | } |
| 1540 | free(grp->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1541 | } |
| 1542 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1543 | static void |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 1544 | lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io) |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1545 | { |
| 1546 | int i; |
| 1547 | |
| 1548 | /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */ |
| 1549 | for (i = 0; i < io->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1550 | lys_tpdf_free(ctx, &io->tpdf[i]); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1551 | } |
| 1552 | free(io->tpdf); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 1553 | |
| 1554 | for (i = 0; i < io->must_size; i++) { |
| 1555 | lys_restr_free(ctx, &io->must[i]); |
| 1556 | } |
| 1557 | free(io->must); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1558 | } |
| 1559 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1560 | static void |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 1561 | lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif) |
| 1562 | { |
| 1563 | int i; |
| 1564 | |
| 1565 | for (i = 0; i < notif->must_size; i++) { |
| 1566 | lys_restr_free(ctx, ¬if->must[i]); |
| 1567 | } |
| 1568 | free(notif->must); |
| 1569 | |
| 1570 | for (i = 0; i < notif->tpdf_size; i++) { |
| 1571 | lys_tpdf_free(ctx, ¬if->tpdf[i]); |
| 1572 | } |
| 1573 | free(notif->tpdf); |
| 1574 | } |
| 1575 | static void |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1576 | lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml) |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1577 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1578 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1579 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1580 | for (i = 0; i < anyxml->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1581 | lys_restr_free(ctx, &anyxml->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1582 | } |
| 1583 | free(anyxml->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1584 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1585 | lys_when_free(ctx, anyxml->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1586 | } |
| 1587 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1588 | static void |
| 1589 | lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1590 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1591 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1592 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1593 | if (leaf->child) { |
| 1594 | /* leafref backlinks */ |
| 1595 | ly_set_free((struct ly_set *)leaf->child); |
| 1596 | } |
| 1597 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1598 | for (i = 0; i < leaf->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1599 | lys_restr_free(ctx, &leaf->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1600 | } |
| 1601 | free(leaf->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1602 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1603 | lys_when_free(ctx, leaf->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1604 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1605 | lys_type_free(ctx, &leaf->type); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1606 | lydict_remove(ctx, leaf->units); |
| 1607 | lydict_remove(ctx, leaf->dflt); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1608 | } |
| 1609 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1610 | static void |
| 1611 | lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1612 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1613 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1614 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1615 | if (llist->child) { |
| 1616 | /* leafref backlinks */ |
| 1617 | ly_set_free((struct ly_set *)llist->child); |
| 1618 | } |
| 1619 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1620 | for (i = 0; i < llist->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1621 | lys_restr_free(ctx, &llist->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1622 | } |
| 1623 | free(llist->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1624 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1625 | for (i = 0; i < llist->dflt_size; i++) { |
| 1626 | lydict_remove(ctx, llist->dflt[i]); |
| 1627 | } |
| 1628 | free(llist->dflt); |
| 1629 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1630 | lys_when_free(ctx, llist->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1631 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1632 | lys_type_free(ctx, &llist->type); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1633 | lydict_remove(ctx, llist->units); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1634 | } |
| 1635 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1636 | static void |
| 1637 | lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1638 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1639 | int i, j; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1640 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1641 | /* handle only specific parts for LY_NODE_LIST */ |
| 1642 | for (i = 0; i < list->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1643 | lys_tpdf_free(ctx, &list->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1644 | } |
| 1645 | free(list->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1646 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1647 | for (i = 0; i < list->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1648 | lys_restr_free(ctx, &list->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1649 | } |
| 1650 | free(list->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1651 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1652 | lys_when_free(ctx, list->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1653 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1654 | for (i = 0; i < list->unique_size; i++) { |
Michal Vasko | f5e940a | 2016-06-07 09:39:26 +0200 | [diff] [blame] | 1655 | for (j = 0; j < list->unique[i].expr_size; j++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1656 | lydict_remove(ctx, list->unique[i].expr[j]); |
| 1657 | } |
| 1658 | free(list->unique[i].expr); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1659 | } |
| 1660 | free(list->unique); |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1661 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1662 | free(list->keys); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1663 | } |
| 1664 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1665 | static void |
| 1666 | lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1667 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1668 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1669 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1670 | /* handle only specific parts for LY_NODE_CONTAINER */ |
| 1671 | lydict_remove(ctx, cont->presence); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1672 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1673 | for (i = 0; i < cont->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1674 | lys_tpdf_free(ctx, &cont->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1675 | } |
| 1676 | free(cont->tpdf); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1677 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1678 | for (i = 0; i < cont->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1679 | lys_restr_free(ctx, &cont->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1680 | } |
| 1681 | free(cont->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1682 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1683 | lys_when_free(ctx, cont->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1684 | } |
| 1685 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1686 | static void |
| 1687 | lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f) |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1688 | { |
| 1689 | lydict_remove(ctx, f->name); |
| 1690 | lydict_remove(ctx, f->dsc); |
| 1691 | lydict_remove(ctx, f->ref); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1692 | lys_iffeature_free(f->iffeature, f->iffeature_size); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1693 | } |
| 1694 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1695 | static void |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 1696 | lys_deviation_free(struct lys_module *module, struct lys_deviation *dev) |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1697 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1698 | int i, j, k; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 1699 | struct ly_ctx *ctx; |
| 1700 | struct lys_node *next, *elem; |
| 1701 | |
| 1702 | ctx = module->ctx; |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1703 | |
| 1704 | lydict_remove(ctx, dev->target_name); |
| 1705 | lydict_remove(ctx, dev->dsc); |
| 1706 | lydict_remove(ctx, dev->ref); |
| 1707 | |
Pavol Vican | 64d0b76 | 2016-08-25 10:44:59 +0200 | [diff] [blame] | 1708 | if (!dev->deviate) { |
| 1709 | return ; |
| 1710 | } |
| 1711 | |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 1712 | /* the module was freed, but we only need the context from orig_node, use ours */ |
| 1713 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 1714 | /* it's actually a node subtree, we need to update modules on all the nodes :-/ */ |
| 1715 | LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) { |
| 1716 | elem->module = module; |
| 1717 | |
| 1718 | LY_TREE_DFS_END(dev->orig_node, next, elem); |
| 1719 | } |
| 1720 | lys_node_free(dev->orig_node, NULL, 0); |
| 1721 | } else { |
| 1722 | /* it's just a shallow copy, freeing one node */ |
| 1723 | dev->orig_node->module = module; |
| 1724 | lys_node_free(dev->orig_node, NULL, 1); |
| 1725 | } |
| 1726 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1727 | for (i = 0; i < dev->deviate_size; i++) { |
Radek Krejci | d5a5c28 | 2016-08-15 15:38:08 +0200 | [diff] [blame] | 1728 | for (j = 0; j < dev->deviate[i].dflt_size; j++) { |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1729 | lydict_remove(ctx, dev->deviate[i].dflt[j]); |
Radek Krejci | d5a5c28 | 2016-08-15 15:38:08 +0200 | [diff] [blame] | 1730 | } |
| 1731 | free(dev->deviate[i].dflt); |
| 1732 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1733 | lydict_remove(ctx, dev->deviate[i].units); |
| 1734 | |
| 1735 | if (dev->deviate[i].mod == LY_DEVIATE_DEL) { |
| 1736 | for (j = 0; j < dev->deviate[i].must_size; j++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1737 | lys_restr_free(ctx, &dev->deviate[i].must[j]); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1738 | } |
| 1739 | free(dev->deviate[i].must); |
| 1740 | |
| 1741 | for (j = 0; j < dev->deviate[i].unique_size; j++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1742 | for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) { |
| 1743 | lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]); |
| 1744 | } |
Michal Vasko | 0238ccf | 2016-03-07 15:02:18 +0100 | [diff] [blame] | 1745 | free(dev->deviate[i].unique[j].expr); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1746 | } |
| 1747 | free(dev->deviate[i].unique); |
| 1748 | } |
| 1749 | } |
| 1750 | free(dev->deviate); |
| 1751 | } |
| 1752 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1753 | static void |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1754 | lys_uses_free(struct ly_ctx *ctx, struct lys_node_uses *uses, void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1755 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1756 | int i, j; |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1757 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1758 | for (i = 0; i < uses->refine_size; i++) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1759 | lydict_remove(ctx, uses->refine[i].target_name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1760 | lydict_remove(ctx, uses->refine[i].dsc); |
| 1761 | lydict_remove(ctx, uses->refine[i].ref); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1762 | |
Michal Vasko | a275c0a | 2015-09-24 09:55:42 +0200 | [diff] [blame] | 1763 | for (j = 0; j < uses->refine[i].must_size; j++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1764 | lys_restr_free(ctx, &uses->refine[i].must[j]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1765 | } |
| 1766 | free(uses->refine[i].must); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1767 | |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 1768 | for (j = 0; j < uses->refine[i].dflt_size; j++) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 1769 | lydict_remove(ctx, uses->refine[i].dflt[j]); |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 1770 | } |
| 1771 | free(uses->refine[i].dflt); |
| 1772 | |
| 1773 | if (uses->refine[i].target_type & LYS_CONTAINER) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1774 | lydict_remove(ctx, uses->refine[i].mod.presence); |
| 1775 | } |
| 1776 | } |
| 1777 | free(uses->refine); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1778 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1779 | for (i = 0; i < uses->augment_size; i++) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1780 | lys_augment_free(ctx, &uses->augment[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1781 | } |
| 1782 | free(uses->augment); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1783 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1784 | lys_when_free(ctx, uses->when); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1785 | } |
| 1786 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1787 | void |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1788 | 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] | 1789 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1790 | struct ly_ctx *ctx; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1791 | struct lys_node *sub, *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1792 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1793 | if (!node) { |
| 1794 | return; |
| 1795 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1796 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1797 | assert(node->module); |
| 1798 | assert(node->module->ctx); |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1799 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1800 | ctx = node->module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1801 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1802 | /* remove private object */ |
Mislav Novakovic | b5529e5 | 2016-02-29 11:42:43 +0100 | [diff] [blame] | 1803 | if (node->priv && private_destructor) { |
| 1804 | private_destructor(node, node->priv); |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1805 | } |
| 1806 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1807 | /* common part */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1808 | lydict_remove(ctx, node->name); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1809 | if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1810 | lys_iffeature_free(node->iffeature, node->iffeature_size); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1811 | lydict_remove(ctx, node->dsc); |
| 1812 | lydict_remove(ctx, node->ref); |
| 1813 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1814 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1815 | if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1816 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1817 | lys_node_free(sub, private_destructor, 0); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1818 | } |
| 1819 | } |
| 1820 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1821 | /* specific part */ |
| 1822 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1823 | case LYS_CONTAINER: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1824 | lys_container_free(ctx, (struct lys_node_container *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1825 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1826 | case LYS_CHOICE: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1827 | lys_when_free(ctx, ((struct lys_node_choice *)node)->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1828 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1829 | case LYS_LEAF: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1830 | lys_leaf_free(ctx, (struct lys_node_leaf *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1831 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1832 | case LYS_LEAFLIST: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1833 | lys_leaflist_free(ctx, (struct lys_node_leaflist *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1834 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1835 | case LYS_LIST: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1836 | lys_list_free(ctx, (struct lys_node_list *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1837 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1838 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1839 | case LYS_ANYDATA: |
| 1840 | lys_anydata_free(ctx, (struct lys_node_anydata *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1841 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1842 | case LYS_USES: |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1843 | lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1844 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1845 | case LYS_CASE: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1846 | lys_when_free(ctx, ((struct lys_node_case *)node)->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1847 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1848 | case LYS_AUGMENT: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1849 | /* do nothing */ |
| 1850 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1851 | case LYS_GROUPING: |
| 1852 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 1853 | case LYS_ACTION: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1854 | lys_grp_free(ctx, (struct lys_node_grp *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1855 | break; |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 1856 | case LYS_NOTIF: |
| 1857 | lys_notif_free(ctx, (struct lys_node_notif *)node); |
| 1858 | break; |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1859 | case LYS_INPUT: |
| 1860 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 1861 | lys_inout_free(ctx, (struct lys_node_inout *)node); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1862 | break; |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 1863 | case LYS_UNKNOWN: |
| 1864 | LOGINT; |
| 1865 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1866 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1867 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1868 | /* again common part */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1869 | lys_node_unlink(node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1870 | free(node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1871 | } |
| 1872 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1873 | const struct lys_module * |
| 1874 | 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] | 1875 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1876 | const struct lys_module *main_module; |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 1877 | char *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1878 | int i; |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 1879 | |
Michal Vasko | a7789a8 | 2016-02-11 15:42:55 +0100 | [diff] [blame] | 1880 | assert(!prefix || !name); |
| 1881 | |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 1882 | if (prefix && !pref_len) { |
| 1883 | pref_len = strlen(prefix); |
| 1884 | } |
| 1885 | if (name && !name_len) { |
| 1886 | name_len = strlen(name); |
| 1887 | } |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 1888 | |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 1889 | main_module = lys_main_module(module); |
Michal Vasko | a7789a8 | 2016-02-11 15:42:55 +0100 | [diff] [blame] | 1890 | |
| 1891 | /* module own prefix, submodule own prefix, (sub)module own name */ |
| 1892 | if ((!prefix || (!module->type && !strncmp(main_module->prefix, prefix, pref_len) && !main_module->prefix[pref_len]) |
| 1893 | || (module->type && !strncmp(module->prefix, prefix, pref_len) && !module->prefix[pref_len])) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1894 | && (!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] | 1895 | return main_module; |
Michal Vasko | d8da86b | 2015-10-21 13:35:37 +0200 | [diff] [blame] | 1896 | } |
| 1897 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 1898 | /* standard import */ |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 1899 | for (i = 0; i < module->imp_size; ++i) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1900 | if ((!prefix || (!strncmp(module->imp[i].prefix, prefix, pref_len) && !module->imp[i].prefix[pref_len])) |
| 1901 | && (!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] | 1902 | return module->imp[i].module; |
| 1903 | } |
| 1904 | } |
| 1905 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 1906 | /* module required by a foreign grouping, deviation, or submodule */ |
| 1907 | if (name) { |
| 1908 | str = strndup(name, name_len); |
| 1909 | if (!str) { |
| 1910 | LOGMEM; |
| 1911 | return NULL; |
| 1912 | } |
| 1913 | main_module = ly_ctx_get_module(module->ctx, str, NULL); |
| 1914 | free(str); |
| 1915 | return main_module; |
| 1916 | } |
| 1917 | |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 1918 | return NULL; |
| 1919 | } |
| 1920 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 1921 | /* free_int_mods - flag whether to free the internal modules as well */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1922 | static void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 1923 | 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] | 1924 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1925 | struct ly_ctx *ctx; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1926 | struct lys_node *next, *iter; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1927 | unsigned int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1928 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1929 | assert(module->ctx); |
| 1930 | ctx = module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1931 | |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 1932 | /* just free the import array, imported modules will stay in the context */ |
Radek Krejci | 225376f | 2016-02-16 17:36:22 +0100 | [diff] [blame] | 1933 | for (i = 0; i < module->imp_size; i++) { |
Michal Vasko | a99c163 | 2016-06-06 16:23:52 +0200 | [diff] [blame] | 1934 | lydict_remove(ctx, module->imp[i].prefix); |
Michal Vasko | 8bfe381 | 2016-07-27 13:37:52 +0200 | [diff] [blame] | 1935 | lydict_remove(ctx, module->imp[i].dsc); |
| 1936 | lydict_remove(ctx, module->imp[i].ref); |
Radek Krejci | 225376f | 2016-02-16 17:36:22 +0100 | [diff] [blame] | 1937 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 1938 | free(module->imp); |
| 1939 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1940 | /* submodules don't have data tree, the data nodes |
| 1941 | * are placed in the main module altogether */ |
| 1942 | if (!module->type) { |
| 1943 | LY_TREE_FOR_SAFE(module->data, next, iter) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1944 | lys_node_free(iter, private_destructor, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1945 | } |
Radek Krejci | 2118196 | 2015-06-30 14:11:00 +0200 | [diff] [blame] | 1946 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1947 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1948 | lydict_remove(ctx, module->dsc); |
| 1949 | lydict_remove(ctx, module->ref); |
| 1950 | lydict_remove(ctx, module->org); |
| 1951 | lydict_remove(ctx, module->contact); |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1952 | lydict_remove(ctx, module->filepath); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1953 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1954 | /* revisions */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1955 | for (i = 0; i < module->rev_size; i++) { |
| 1956 | lydict_remove(ctx, module->rev[i].dsc); |
| 1957 | lydict_remove(ctx, module->rev[i].ref); |
| 1958 | } |
| 1959 | free(module->rev); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1960 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1961 | /* identities */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1962 | for (i = 0; i < module->ident_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1963 | lys_ident_free(ctx, &module->ident[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1964 | } |
| 1965 | module->ident_size = 0; |
| 1966 | free(module->ident); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1967 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1968 | /* typedefs */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1969 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1970 | lys_tpdf_free(ctx, &module->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1971 | } |
| 1972 | free(module->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1973 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1974 | /* include */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1975 | for (i = 0; i < module->inc_size; i++) { |
Michal Vasko | 8bfe381 | 2016-07-27 13:37:52 +0200 | [diff] [blame] | 1976 | lydict_remove(ctx, module->inc[i].dsc); |
| 1977 | lydict_remove(ctx, module->inc[i].ref); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1978 | /* complete submodule free is done only from main module since |
| 1979 | * submodules propagate their includes to the main module */ |
| 1980 | if (!module->type) { |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 1981 | lys_submodule_free(module->inc[i].submodule, private_destructor); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1982 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1983 | } |
| 1984 | free(module->inc); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1985 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1986 | /* augment */ |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 1987 | for (i = 0; i < module->augment_size; i++) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1988 | lys_augment_free(ctx, &module->augment[i], private_destructor); |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 1989 | } |
| 1990 | free(module->augment); |
| 1991 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1992 | /* features */ |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1993 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1994 | lys_feature_free(ctx, &module->features[i]); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1995 | } |
| 1996 | free(module->features); |
| 1997 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1998 | /* deviations */ |
| 1999 | for (i = 0; i < module->deviation_size; i++) { |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2000 | lys_deviation_free(module, &module->deviation[i]); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2001 | } |
| 2002 | free(module->deviation); |
| 2003 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2004 | lydict_remove(ctx, module->name); |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 2005 | lydict_remove(ctx, module->prefix); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2006 | } |
| 2007 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2008 | void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2009 | 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] | 2010 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2011 | if (!submodule) { |
| 2012 | return; |
| 2013 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2014 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2015 | /* common part with struct ly_module */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2016 | module_free_common((struct lys_module *)submodule, private_destructor); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2017 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2018 | /* no specific items to free */ |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2019 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2020 | free(submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2021 | } |
| 2022 | |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 2023 | static int |
| 2024 | ingrouping(const struct lys_node *node) |
| 2025 | { |
| 2026 | const struct lys_node *iter = node; |
| 2027 | assert(node); |
| 2028 | |
| 2029 | for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter)); |
| 2030 | if (!iter) { |
| 2031 | return 0; |
| 2032 | } else { |
| 2033 | return 1; |
| 2034 | } |
| 2035 | } |
| 2036 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2037 | struct lys_node * |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2038 | lys_node_dup(struct lys_module *module, struct lys_node *parent, const struct lys_node *node, uint8_t flags, |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2039 | uint8_t nacm, struct unres_schema *unres, int shallow) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2040 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2041 | struct lys_node *retval = NULL, *child; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2042 | struct ly_ctx *ctx = module->ctx; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2043 | int i, j, rc; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2044 | unsigned int size, size1, size2; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 2045 | struct unres_list_uniq *unique_info; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2046 | |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2047 | struct lys_node_container *cont = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2048 | struct lys_node_container *cont_orig = (struct lys_node_container *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2049 | struct lys_node_choice *choice = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2050 | struct lys_node_choice *choice_orig = (struct lys_node_choice *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2051 | struct lys_node_leaf *leaf = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2052 | struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2053 | struct lys_node_leaflist *llist = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2054 | struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2055 | struct lys_node_list *list = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2056 | struct lys_node_list *list_orig = (struct lys_node_list *)node; |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2057 | struct lys_node_anydata *any = NULL; |
| 2058 | struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2059 | struct lys_node_uses *uses = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2060 | struct lys_node_uses *uses_orig = (struct lys_node_uses *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2061 | struct lys_node_grp *grp = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2062 | struct lys_node_grp *grp_orig = (struct lys_node_grp *)node; |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2063 | struct lys_node_rpc_action *rpc = NULL; |
| 2064 | struct lys_node_rpc_action *rpc_orig = (struct lys_node_rpc_action *)node; |
| 2065 | struct lys_node_inout *io = NULL; |
| 2066 | struct lys_node_inout *io_orig = (struct lys_node_inout *)node; |
| 2067 | struct lys_node_rpc_action *ntf = NULL; |
| 2068 | struct lys_node_rpc_action *ntf_orig = (struct lys_node_rpc_action *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2069 | struct lys_node_case *cs = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2070 | struct lys_node_case *cs_orig = (struct lys_node_case *)node; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2071 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2072 | /* we cannot just duplicate memory since the strings are stored in |
| 2073 | * dictionary and we need to update dictionary counters. |
| 2074 | */ |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2075 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2076 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2077 | case LYS_CONTAINER: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2078 | cont = calloc(1, sizeof *cont); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2079 | retval = (struct lys_node *)cont; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2080 | break; |
| 2081 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2082 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2083 | choice = calloc(1, sizeof *choice); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2084 | retval = (struct lys_node *)choice; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2085 | break; |
| 2086 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2087 | case LYS_LEAF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2088 | leaf = calloc(1, sizeof *leaf); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2089 | retval = (struct lys_node *)leaf; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2090 | break; |
| 2091 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2092 | case LYS_LEAFLIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2093 | llist = calloc(1, sizeof *llist); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2094 | retval = (struct lys_node *)llist; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2095 | break; |
| 2096 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2097 | case LYS_LIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2098 | list = calloc(1, sizeof *list); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2099 | retval = (struct lys_node *)list; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2100 | break; |
| 2101 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2102 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2103 | case LYS_ANYDATA: |
| 2104 | any = calloc(1, sizeof *any); |
| 2105 | retval = (struct lys_node *)any; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2106 | break; |
| 2107 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2108 | case LYS_USES: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2109 | uses = calloc(1, sizeof *uses); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2110 | retval = (struct lys_node *)uses; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2111 | break; |
| 2112 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2113 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2114 | cs = calloc(1, sizeof *cs); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2115 | retval = (struct lys_node *)cs; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2116 | break; |
| 2117 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2118 | case LYS_GROUPING: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2119 | grp = calloc(1, sizeof *grp); |
| 2120 | retval = (struct lys_node *)grp; |
| 2121 | break; |
| 2122 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2123 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2124 | case LYS_ACTION: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2125 | rpc = calloc(1, sizeof *rpc); |
| 2126 | retval = (struct lys_node *)rpc; |
| 2127 | break; |
| 2128 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2129 | case LYS_INPUT: |
| 2130 | case LYS_OUTPUT: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2131 | io = calloc(1, sizeof *io); |
| 2132 | retval = (struct lys_node *)io; |
| 2133 | break; |
| 2134 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2135 | case LYS_NOTIF: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2136 | ntf = calloc(1, sizeof *ntf); |
| 2137 | retval = (struct lys_node *)ntf; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2138 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2139 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2140 | default: |
Michal Vasko | d23ce59 | 2015-08-06 09:55:37 +0200 | [diff] [blame] | 2141 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2142 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2143 | } |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame] | 2144 | |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2145 | if (!retval) { |
| 2146 | LOGMEM; |
| 2147 | return NULL; |
| 2148 | } |
| 2149 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2150 | /* |
| 2151 | * duplicate generic part of the structure |
| 2152 | */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2153 | retval->name = lydict_insert(ctx, node->name, 0); |
| 2154 | retval->dsc = lydict_insert(ctx, node->dsc, 0); |
| 2155 | retval->ref = lydict_insert(ctx, node->ref, 0); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 2156 | retval->nacm = nacm; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2157 | retval->flags = node->flags; |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2158 | if (!(retval->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2159 | /* set parent's config flag */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2160 | retval->flags |= flags & LYS_CONFIG_MASK; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2161 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2162 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2163 | retval->module = module; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2164 | retval->nodetype = node->nodetype; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2165 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2166 | retval->prev = retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2167 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 2168 | retval->iffeature_size = node->iffeature_size; |
| 2169 | retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature); |
| 2170 | if (!retval->iffeature) { |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2171 | LOGMEM; |
| 2172 | goto error; |
| 2173 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2174 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2175 | if (!shallow) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 2176 | for (i = 0; i < node->iffeature_size; ++i) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2177 | resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2); |
| 2178 | if (size1) { |
| 2179 | /* there is something to duplicate */ |
| 2180 | |
| 2181 | /* duplicate compiled expression */ |
| 2182 | size = (size1 / 4) + (size1 % 4) ? 1 : 0; |
| 2183 | retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr); |
| 2184 | memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr); |
| 2185 | |
| 2186 | /* list of feature pointer must be updated to point to the resulting tree */ |
| 2187 | retval->iffeature[i].features = malloc(size2 * sizeof *retval->iffeature[i].features); |
| 2188 | for (j = 0; (unsigned int)j < size2; j++) { |
| 2189 | rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT, |
| 2190 | &retval->iffeature[i].features[j]); |
| 2191 | if (rc == EXIT_SUCCESS) { |
| 2192 | /* feature is not resolved, duplicate the expression string */ |
| 2193 | retval->iffeature[i].features[j] = (void *)strdup((char *)node->iffeature[i].features[j]); |
| 2194 | } else if (rc == EXIT_FAILURE) { |
| 2195 | /* feature is resolved in origin, so copy it |
| 2196 | * - duplication is used for instantiating groupings |
| 2197 | * and if-feature inside grouping is supposed to be |
| 2198 | * resolved inside the original grouping, so we want |
| 2199 | * to keep pointers to features from the grouping |
| 2200 | * context */ |
| 2201 | retval->iffeature[i].features[j] = node->iffeature[i].features[j]; |
| 2202 | } else if (rc == -1) { |
| 2203 | goto error; |
| 2204 | } |
| 2205 | } |
| 2206 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2207 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2208 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2209 | /* connect it to the parent */ |
| 2210 | if (lys_node_addchild(parent, retval->module, retval)) { |
| 2211 | goto error; |
| 2212 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2213 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2214 | /* go recursively */ |
| 2215 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2216 | LY_TREE_FOR(node->child, child) { |
| 2217 | if (!lys_node_dup(module, retval, child, retval->flags, retval->nacm, unres, 0)) { |
| 2218 | goto error; |
| 2219 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2220 | } |
| 2221 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2222 | } else { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 2223 | memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2224 | } |
| 2225 | |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2226 | /* |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2227 | * duplicate specific part of the structure |
| 2228 | */ |
| 2229 | switch (node->nodetype) { |
| 2230 | case LYS_CONTAINER: |
| 2231 | if (cont_orig->when) { |
| 2232 | cont->when = lys_when_dup(ctx, cont_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2233 | } |
| 2234 | cont->presence = lydict_insert(ctx, cont_orig->presence, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2235 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2236 | cont->must_size = cont_orig->must_size; |
| 2237 | cont->tpdf_size = cont_orig->tpdf_size; |
| 2238 | |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2239 | cont->must = lys_restr_dup(ctx, cont_orig->must, cont->must_size); |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2240 | cont->tpdf = lys_tpdf_dup(module, lys_parent(node), cont_orig->tpdf, cont->tpdf_size, unres); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2241 | break; |
| 2242 | |
| 2243 | case LYS_CHOICE: |
| 2244 | if (choice_orig->when) { |
| 2245 | choice->when = lys_when_dup(ctx, choice_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2246 | } |
| 2247 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2248 | if (!shallow) { |
| 2249 | if (choice_orig->dflt) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2250 | rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0, |
| 2251 | LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST, |
| 2252 | (const struct lys_node **)&choice->dflt); |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2253 | if (rc) { |
| 2254 | if (rc == EXIT_FAILURE) { |
| 2255 | LOGINT; |
| 2256 | } |
| 2257 | goto error; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2258 | } |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2259 | } else { |
| 2260 | /* useless to check return value, we don't know whether |
| 2261 | * there really wasn't any default defined or it just hasn't |
| 2262 | * been resolved, we just hope for the best :) |
| 2263 | */ |
| 2264 | unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2265 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2266 | } else { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2267 | choice->dflt = choice_orig->dflt; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2268 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2269 | break; |
| 2270 | |
| 2271 | case LYS_LEAF: |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 2272 | if (lys_type_dup(module, retval, &(leaf->type), &(leaf_orig->type), ingrouping(retval), unres)) { |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 2273 | goto error; |
| 2274 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2275 | leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0); |
| 2276 | |
| 2277 | if (leaf_orig->dflt) { |
| 2278 | leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0); |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2279 | if (unres_schema_add_str(module, unres, &leaf->type, UNRES_TYPE_DFLT, leaf->dflt) == -1) { |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2280 | goto error; |
| 2281 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | leaf->must_size = leaf_orig->must_size; |
| 2285 | leaf->must = lys_restr_dup(ctx, leaf_orig->must, leaf->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2286 | |
| 2287 | if (leaf_orig->when) { |
| 2288 | leaf->when = lys_when_dup(ctx, leaf_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2289 | } |
| 2290 | break; |
| 2291 | |
| 2292 | case LYS_LEAFLIST: |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 2293 | if (lys_type_dup(module, retval, &(llist->type), &(llist_orig->type), ingrouping(retval), unres)) { |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 2294 | goto error; |
| 2295 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2296 | llist->units = lydict_insert(module->ctx, llist_orig->units, 0); |
| 2297 | |
| 2298 | llist->min = llist_orig->min; |
| 2299 | llist->max = llist_orig->max; |
| 2300 | |
| 2301 | llist->must_size = llist_orig->must_size; |
| 2302 | llist->must = lys_restr_dup(ctx, llist_orig->must, llist->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2303 | |
| 2304 | if (llist_orig->when) { |
| 2305 | llist->when = lys_when_dup(ctx, llist_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2306 | } |
| 2307 | break; |
| 2308 | |
| 2309 | case LYS_LIST: |
| 2310 | list->min = list_orig->min; |
| 2311 | list->max = list_orig->max; |
| 2312 | |
| 2313 | list->must_size = list_orig->must_size; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2314 | list->must = lys_restr_dup(ctx, list_orig->must, list->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2315 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2316 | list->tpdf_size = list_orig->tpdf_size; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2317 | list->tpdf = lys_tpdf_dup(module, lys_parent(node), list_orig->tpdf, list->tpdf_size, unres); |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2318 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2319 | list->keys_size = list_orig->keys_size; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2320 | if (list->keys_size) { |
| 2321 | list->keys = calloc(list->keys_size, sizeof *list->keys); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2322 | if (!list->keys) { |
| 2323 | LOGMEM; |
| 2324 | goto error; |
| 2325 | } |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 2326 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2327 | if (!shallow) { |
| 2328 | /* we managed to resolve it before, resolve it again manually */ |
| 2329 | if (list_orig->keys[0]) { |
| 2330 | for (i = 0; i < list->keys_size; ++i) { |
| 2331 | rc = lys_get_sibling(list->child, lys_node_module(retval)->name, 0, list_orig->keys[i]->name, 0, LYS_LEAF, |
| 2332 | (const struct lys_node **)&list->keys[i]); |
| 2333 | if (rc) { |
| 2334 | if (rc == EXIT_FAILURE) { |
| 2335 | LOGINT; |
| 2336 | } |
| 2337 | goto error; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2338 | } |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2339 | } |
| 2340 | /* it was not resolved yet, add unres copy */ |
| 2341 | } else { |
| 2342 | if (unres_schema_dup(module, unres, list_orig, UNRES_LIST_KEYS, list)) { |
| 2343 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2344 | goto error; |
| 2345 | } |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2346 | } |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 2347 | } else { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2348 | memcpy(list->keys, list_orig->keys, list->keys_size * sizeof *list->keys); |
Radek Krejci | a01e543 | 2015-06-16 10:35:25 +0200 | [diff] [blame] | 2349 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2350 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2351 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2352 | list->unique_size = list_orig->unique_size; |
| 2353 | list->unique = malloc(list->unique_size * sizeof *list->unique); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2354 | if (!list->unique) { |
| 2355 | LOGMEM; |
| 2356 | goto error; |
| 2357 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2358 | for (i = 0; i < list->unique_size; ++i) { |
| 2359 | list->unique[i].expr_size = list_orig->unique[i].expr_size; |
| 2360 | list->unique[i].expr = malloc(list->unique[i].expr_size * sizeof *list->unique[i].expr); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2361 | if (!list->unique[i].expr) { |
| 2362 | LOGMEM; |
| 2363 | goto error; |
| 2364 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2365 | for (j = 0; j < list->unique[i].expr_size; j++) { |
| 2366 | list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0); |
| 2367 | |
| 2368 | /* if it stays in unres list, duplicate it also there */ |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 2369 | unique_info = malloc(sizeof *unique_info); |
| 2370 | unique_info->list = (struct lys_node *)list; |
| 2371 | unique_info->expr = list->unique[i].expr[j]; |
| 2372 | unique_info->trg_type = &list->unique[i].trg_type; |
| 2373 | unres_schema_dup(module, unres, &list_orig, UNRES_LIST_UNIQ, unique_info); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2374 | } |
| 2375 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2376 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2377 | if (list_orig->when) { |
| 2378 | list->when = lys_when_dup(ctx, list_orig->when); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2379 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2380 | break; |
| 2381 | |
| 2382 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2383 | case LYS_ANYDATA: |
| 2384 | any->must_size = any_orig->must_size; |
| 2385 | any->must = lys_restr_dup(ctx, any_orig->must, any->must_size); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2386 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2387 | if (any_orig->when) { |
| 2388 | any->when = lys_when_dup(ctx, any_orig->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2389 | } |
| 2390 | break; |
| 2391 | |
| 2392 | case LYS_USES: |
| 2393 | uses->grp = uses_orig->grp; |
| 2394 | |
| 2395 | if (uses_orig->when) { |
| 2396 | uses->when = lys_when_dup(ctx, uses_orig->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2397 | } |
| 2398 | |
| 2399 | uses->refine_size = uses_orig->refine_size; |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 2400 | uses->refine = lys_refine_dup(module, uses_orig->refine, uses_orig->refine_size); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2401 | uses->augment_size = uses_orig->augment_size; |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2402 | if (!shallow) { |
| 2403 | uses->augment = lys_augment_dup(module, (struct lys_node *)uses, uses_orig->augment, uses_orig->augment_size); |
Michal Vasko | cda5171 | 2016-05-19 15:22:11 +0200 | [diff] [blame] | 2404 | if (!uses->grp || uses->grp->nacm) { |
| 2405 | assert(!uses->child); |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2406 | if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL) == -1) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2407 | goto error; |
| 2408 | } |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2409 | } |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2410 | } else { |
| 2411 | memcpy(uses->augment, uses_orig->augment, uses->augment_size * sizeof *uses->augment); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2412 | } |
| 2413 | break; |
| 2414 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2415 | case LYS_CASE: |
| 2416 | if (cs_orig->when) { |
| 2417 | cs->when = lys_when_dup(ctx, cs_orig->when); |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2418 | } |
| 2419 | break; |
| 2420 | |
| 2421 | case LYS_GROUPING: |
| 2422 | grp->tpdf_size = grp_orig->tpdf_size; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2423 | grp->tpdf = lys_tpdf_dup(module, lys_parent(node), grp_orig->tpdf, grp->tpdf_size, unres); |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2424 | break; |
| 2425 | |
| 2426 | case LYS_RPC: |
| 2427 | rpc->tpdf_size = rpc_orig->tpdf_size; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2428 | rpc->tpdf = lys_tpdf_dup(module, lys_parent(node), rpc_orig->tpdf, rpc->tpdf_size, unres); |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2429 | break; |
| 2430 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2431 | case LYS_INPUT: |
| 2432 | case LYS_OUTPUT: |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2433 | io->tpdf_size = io_orig->tpdf_size; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2434 | io->tpdf = lys_tpdf_dup(module, lys_parent(node), io_orig->tpdf, io->tpdf_size, unres); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2435 | break; |
| 2436 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2437 | case LYS_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2438 | ntf->tpdf_size = ntf_orig->tpdf_size; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2439 | ntf->tpdf = lys_tpdf_dup(module, lys_parent(node), ntf_orig->tpdf, ntf->tpdf_size, unres); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2440 | break; |
| 2441 | |
| 2442 | default: |
| 2443 | /* LY_NODE_AUGMENT */ |
| 2444 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2445 | goto error; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2446 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2447 | |
| 2448 | return retval; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2449 | |
| 2450 | error: |
| 2451 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2452 | lys_node_free(retval, NULL, 0); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2453 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2454 | } |
| 2455 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2456 | void |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2457 | lys_node_switch(struct lys_node *dst, struct lys_node *src) |
| 2458 | { |
| 2459 | struct lys_node *child; |
| 2460 | |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 2461 | 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] | 2462 | |
| 2463 | /* sibling next */ |
Michal Vasko | 8328da8 | 2016-08-25 09:27:22 +0200 | [diff] [blame] | 2464 | if (dst->prev->next) { |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2465 | dst->prev->next = src; |
| 2466 | } |
| 2467 | |
| 2468 | /* sibling prev */ |
| 2469 | if (dst->next) { |
| 2470 | dst->next->prev = src; |
Michal Vasko | 8328da8 | 2016-08-25 09:27:22 +0200 | [diff] [blame] | 2471 | } else { |
| 2472 | for (child = dst->prev; child->prev->next; child = child->prev); |
| 2473 | child->prev = src; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | /* next */ |
| 2477 | src->next = dst->next; |
| 2478 | dst->next = NULL; |
| 2479 | |
| 2480 | /* prev */ |
| 2481 | if (dst->prev != dst) { |
| 2482 | src->prev = dst->prev; |
| 2483 | } |
| 2484 | dst->prev = dst; |
| 2485 | |
| 2486 | /* parent child */ |
| 2487 | if (dst->parent && (dst->parent->child == dst)) { |
| 2488 | dst->parent->child = src; |
| 2489 | } |
| 2490 | |
| 2491 | /* parent */ |
| 2492 | src->parent = dst->parent; |
| 2493 | dst->parent = NULL; |
| 2494 | |
| 2495 | /* child parent */ |
| 2496 | LY_TREE_FOR(dst->child, child) { |
| 2497 | if (child->parent == dst) { |
| 2498 | child->parent = src; |
| 2499 | } |
| 2500 | } |
| 2501 | |
| 2502 | /* child */ |
| 2503 | src->child = dst->child; |
| 2504 | dst->child = NULL; |
| 2505 | } |
| 2506 | |
| 2507 | void |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2508 | 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] | 2509 | { |
| 2510 | struct ly_ctx *ctx; |
| 2511 | int i; |
| 2512 | |
| 2513 | if (!module) { |
| 2514 | return; |
| 2515 | } |
| 2516 | |
| 2517 | /* remove schema from the context */ |
| 2518 | ctx = module->ctx; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2519 | if (remove_from_ctx && ctx->models.used) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2520 | for (i = 0; i < ctx->models.used; i++) { |
| 2521 | if (ctx->models.list[i] == module) { |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2522 | /* move all the models to not change the order in the list */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2523 | ctx->models.used--; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2524 | 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] | 2525 | ctx->models.list[ctx->models.used] = NULL; |
| 2526 | /* we are done */ |
| 2527 | break; |
| 2528 | } |
| 2529 | } |
| 2530 | } |
| 2531 | |
| 2532 | /* common part with struct ly_submodule */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2533 | module_free_common(module, private_destructor); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2534 | |
| 2535 | /* specific items to free */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2536 | lydict_remove(ctx, module->ns); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2537 | |
| 2538 | free(module); |
| 2539 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2540 | |
| 2541 | /* |
| 2542 | * op: 1 - enable, 0 - disable |
| 2543 | */ |
| 2544 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2545 | 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] | 2546 | { |
| 2547 | int all = 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2548 | int i, j, k; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2549 | |
| 2550 | if (!module || !name || !strlen(name)) { |
| 2551 | return EXIT_FAILURE; |
| 2552 | } |
| 2553 | |
| 2554 | if (!strcmp(name, "*")) { |
| 2555 | /* enable all */ |
| 2556 | all = 1; |
| 2557 | } |
| 2558 | |
| 2559 | /* module itself */ |
| 2560 | for (i = 0; i < module->features_size; i++) { |
| 2561 | if (all || !strcmp(module->features[i].name, name)) { |
| 2562 | if (op) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2563 | /* check referenced features if they are enabled */ |
| 2564 | for (j = 0; j < module->features[i].iffeature_size; j++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 2565 | if (!resolve_iffeature(&module->features[i].iffeature[j])) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2566 | LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.", |
| 2567 | module->features[i].name, j + 1); |
| 2568 | return EXIT_FAILURE; |
| 2569 | } |
| 2570 | } |
| 2571 | |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2572 | module->features[i].flags |= LYS_FENABLED; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2573 | } else { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2574 | module->features[i].flags &= ~LYS_FENABLED; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2575 | } |
| 2576 | if (!all) { |
| 2577 | return EXIT_SUCCESS; |
| 2578 | } |
| 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | /* submodules */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 2583 | for (i = 0; i < module->inc_size; i++) { |
| 2584 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
| 2585 | if (all || !strcmp(module->inc[i].submodule->features[j].name, name)) { |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2586 | if (op) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2587 | /* check referenced features if they are enabled */ |
| 2588 | for (k = 0; k < module->inc[i].submodule->features[j].iffeature_size; k++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 2589 | if (!resolve_iffeature(&module->inc[i].submodule->features[j].iffeature[k])) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2590 | LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.", |
| 2591 | module->inc[i].submodule->features[j].name, k + 1); |
| 2592 | return EXIT_FAILURE; |
| 2593 | } |
| 2594 | } |
| 2595 | |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 2596 | module->inc[i].submodule->features[j].flags |= LYS_FENABLED; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2597 | } else { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 2598 | module->inc[i].submodule->features[j].flags &= ~LYS_FENABLED; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2599 | } |
| 2600 | if (!all) { |
| 2601 | return EXIT_SUCCESS; |
| 2602 | } |
| 2603 | } |
| 2604 | } |
| 2605 | } |
| 2606 | |
| 2607 | if (all) { |
| 2608 | return EXIT_SUCCESS; |
| 2609 | } else { |
| 2610 | return EXIT_FAILURE; |
| 2611 | } |
| 2612 | } |
| 2613 | |
| 2614 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2615 | lys_features_enable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2616 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2617 | return lys_features_change(module, feature, 1); |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2621 | lys_features_disable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2622 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2623 | return lys_features_change(module, feature, 0); |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2624 | } |
| 2625 | |
| 2626 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2627 | lys_features_state(const struct lys_module *module, const char *feature) |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2628 | { |
| 2629 | int i, j; |
| 2630 | |
| 2631 | if (!module || !feature) { |
| 2632 | return -1; |
| 2633 | } |
| 2634 | |
| 2635 | /* search for the specified feature */ |
| 2636 | /* module itself */ |
| 2637 | for (i = 0; i < module->features_size; i++) { |
| 2638 | if (!strcmp(feature, module->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2639 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2640 | return 1; |
| 2641 | } else { |
| 2642 | return 0; |
| 2643 | } |
| 2644 | } |
| 2645 | } |
| 2646 | |
| 2647 | /* submodules */ |
| 2648 | for (j = 0; j < module->inc_size; j++) { |
| 2649 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
| 2650 | if (!strcmp(feature, module->inc[j].submodule->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2651 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2652 | return 1; |
| 2653 | } else { |
| 2654 | return 0; |
| 2655 | } |
| 2656 | } |
| 2657 | } |
| 2658 | } |
| 2659 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2660 | /* feature definition not found */ |
| 2661 | return -1; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2662 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2663 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2664 | API const char ** |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2665 | lys_features_list(const struct lys_module *module, uint8_t **states) |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2666 | { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2667 | const char **result = NULL; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2668 | int i, j; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2669 | unsigned int count; |
| 2670 | |
| 2671 | if (!module) { |
| 2672 | return NULL; |
| 2673 | } |
| 2674 | |
| 2675 | count = module->features_size; |
| 2676 | for (i = 0; i < module->inc_size; i++) { |
| 2677 | count += module->inc[i].submodule->features_size; |
| 2678 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2679 | result = malloc((count + 1) * sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2680 | if (!result) { |
| 2681 | LOGMEM; |
| 2682 | return NULL; |
| 2683 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2684 | if (states) { |
| 2685 | *states = malloc((count + 1) * sizeof **states); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2686 | if (!(*states)) { |
| 2687 | LOGMEM; |
| 2688 | free(result); |
| 2689 | return NULL; |
| 2690 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2691 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2692 | count = 0; |
| 2693 | |
| 2694 | /* module itself */ |
| 2695 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2696 | result[count] = module->features[i].name; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2697 | if (states) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2698 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2699 | (*states)[count] = 1; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2700 | } else { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2701 | (*states)[count] = 0; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2702 | } |
| 2703 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2704 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2705 | } |
| 2706 | |
| 2707 | /* submodules */ |
| 2708 | for (j = 0; j < module->inc_size; j++) { |
| 2709 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2710 | result[count] = module->inc[j].submodule->features[i].name; |
Radek Krejci | 374b94e | 2015-08-13 09:44:22 +0200 | [diff] [blame] | 2711 | if (states) { |
| 2712 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
| 2713 | (*states)[count] = 1; |
| 2714 | } else { |
| 2715 | (*states)[count] = 0; |
| 2716 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2717 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2718 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2719 | } |
| 2720 | } |
| 2721 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2722 | /* terminating NULL byte */ |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2723 | result[count] = NULL; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2724 | |
| 2725 | return result; |
| 2726 | } |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 2727 | |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 2728 | API struct lys_module * |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 2729 | lys_node_module(const struct lys_node *node) |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2730 | { |
| 2731 | return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module; |
| 2732 | } |
| 2733 | |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 2734 | API struct lys_module * |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 2735 | lys_main_module(const struct lys_module *module) |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 2736 | { |
| 2737 | return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module); |
| 2738 | } |
| 2739 | |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 2740 | API struct lys_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2741 | lys_parent(const struct lys_node *node) |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 2742 | { |
| 2743 | if (!node || !node->parent) { |
| 2744 | return NULL; |
| 2745 | } |
| 2746 | |
| 2747 | if (node->parent->nodetype == LYS_AUGMENT) { |
| 2748 | return ((struct lys_node_augment *)node->parent)->target; |
| 2749 | } |
| 2750 | |
| 2751 | return node->parent; |
| 2752 | } |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2753 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2754 | API void * |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2755 | lys_set_private(const struct lys_node *node, void *priv) |
| 2756 | { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2757 | void *prev; |
| 2758 | |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2759 | if (!node) { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2760 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 2761 | return NULL; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2762 | } |
| 2763 | |
Mislav Novakovic | b5529e5 | 2016-02-29 11:42:43 +0100 | [diff] [blame] | 2764 | prev = node->priv; |
| 2765 | ((struct lys_node *)node)->priv = priv; |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2766 | |
| 2767 | return prev; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2768 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2769 | |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2770 | int |
| 2771 | lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref) |
| 2772 | { |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 2773 | struct lys_node_leaf *iter = leafref_target; |
| 2774 | |
Michal Vasko | 48a573d | 2016-07-01 11:46:02 +0200 | [diff] [blame] | 2775 | if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2776 | LOGINT; |
| 2777 | return -1; |
| 2778 | } |
| 2779 | |
Pavol Vican | 9317515 | 2016-08-30 15:34:44 +0200 | [diff] [blame] | 2780 | /* check for config flag */ |
| 2781 | if ((leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) { |
| 2782 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, leafref, |
| 2783 | "The %s is config but refers to a non-config %s.", |
| 2784 | strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype)); |
| 2785 | return -1; |
| 2786 | } |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 2787 | /* check for cycles */ |
| 2788 | while (iter && iter->type.base == LY_TYPE_LEAFREF) { |
| 2789 | if ((void *)iter == (void *)leafref) { |
| 2790 | /* cycle detected */ |
| 2791 | LOGVAL(LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref); |
| 2792 | return -1; |
| 2793 | } |
| 2794 | iter = iter->type.info.lref.target; |
| 2795 | } |
| 2796 | |
| 2797 | /* create fake child - the ly_set structure to hold the list of |
Michal Vasko | 48a573d | 2016-07-01 11:46:02 +0200 | [diff] [blame] | 2798 | * leafrefs referencing the leaf(-list) */ |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2799 | if (!leafref_target->child) { |
| 2800 | leafref_target->child = (void*)ly_set_new(); |
| 2801 | if (!leafref_target->child) { |
| 2802 | LOGMEM; |
| 2803 | return -1; |
| 2804 | } |
| 2805 | } |
Radek Krejci | 09891a2 | 2016-06-10 10:59:22 +0200 | [diff] [blame] | 2806 | ly_set_add((struct ly_set *)leafref_target->child, leafref, 0); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 2807 | |
| 2808 | return 0; |
| 2809 | } |
| 2810 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 2811 | /* not needed currently */ |
| 2812 | #if 0 |
| 2813 | |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 2814 | static const char * |
| 2815 | lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len) |
| 2816 | { |
| 2817 | struct lys_module *prev_mod; |
| 2818 | uint32_t str_len, mod_len, buf_idx; |
| 2819 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2820 | 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] | 2821 | LOGINT; |
| 2822 | return NULL; |
| 2823 | } |
| 2824 | |
| 2825 | buf_idx = buf_len - 1; |
| 2826 | buf[buf_idx] = '\0'; |
| 2827 | |
| 2828 | while (node) { |
| 2829 | if (lys_parent(node)) { |
| 2830 | prev_mod = lys_node_module(lys_parent(node)); |
| 2831 | } else { |
| 2832 | prev_mod = NULL; |
| 2833 | } |
| 2834 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2835 | 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] | 2836 | str_len = strlen(node->name); |
| 2837 | |
| 2838 | if (prev_mod != node->module) { |
| 2839 | mod_len = strlen(node->module->name); |
| 2840 | } else { |
| 2841 | mod_len = 0; |
| 2842 | } |
| 2843 | |
| 2844 | if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) { |
| 2845 | LOGINT; |
| 2846 | return NULL; |
| 2847 | } |
| 2848 | |
| 2849 | buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len; |
| 2850 | |
| 2851 | buf[buf_idx] = '/'; |
| 2852 | if (mod_len) { |
| 2853 | memcpy(buf + buf_idx + 1, node->module->name, mod_len); |
| 2854 | buf[buf_idx + 1 + mod_len] = ':'; |
| 2855 | } |
| 2856 | memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len); |
| 2857 | } |
| 2858 | |
| 2859 | node = lys_parent(node); |
| 2860 | } |
| 2861 | |
| 2862 | return buf + buf_idx; |
| 2863 | } |
| 2864 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 2865 | #endif |
| 2866 | |
| 2867 | API struct ly_set * |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 2868 | 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] | 2869 | { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 2870 | struct lyxp_set set; |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 2871 | struct ly_set *ret_set; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 2872 | uint32_t i; |
| 2873 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 2874 | if (!cur_snode || cur_snode->prev->next || !expr) { |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 2875 | return NULL; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 2876 | } |
| 2877 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 2878 | memset(&set, 0, sizeof set); |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 2879 | |
| 2880 | if (options & LYXP_MUST) { |
| 2881 | options &= ~LYXP_MUST; |
| 2882 | options |= LYXP_SNODE_MUST; |
| 2883 | } else if (options & LYXP_WHEN) { |
| 2884 | options &= ~LYXP_WHEN; |
| 2885 | options |= LYXP_SNODE_WHEN; |
| 2886 | } else { |
| 2887 | options |= LYXP_SNODE; |
| 2888 | } |
| 2889 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 2890 | if (lyxp_atomize(expr, cur_snode, cur_snode_type, &set, options)) { |
| 2891 | free(set.val.snodes); |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 2892 | return NULL; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 2893 | } |
| 2894 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 2895 | ret_set = ly_set_new(); |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 2896 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 2897 | for (i = 0; i < set.used; ++i) { |
| 2898 | switch (set.val.snodes[i].type) { |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 2899 | case LYXP_NODE_ELEM: |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 2900 | 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] | 2901 | ly_set_free(ret_set); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 2902 | free(set.val.snodes); |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 2903 | return NULL; |
| 2904 | } |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 2905 | break; |
| 2906 | default: |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 2907 | /* ignore roots, text and attr should not ever appear */ |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 2908 | break; |
| 2909 | } |
| 2910 | } |
| 2911 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 2912 | free(set.val.snodes); |
| 2913 | return ret_set; |
| 2914 | } |
| 2915 | |
| 2916 | API struct ly_set * |
| 2917 | lys_node_xpath_atomize(const struct lys_node *node, int options) |
| 2918 | { |
| 2919 | const struct lys_node *next, *elem, *parent, *tmp; |
| 2920 | struct lyxp_set set; |
| 2921 | struct ly_set *ret_set; |
| 2922 | uint16_t i; |
| 2923 | |
| 2924 | if (!node) { |
| 2925 | return NULL; |
| 2926 | } |
| 2927 | |
| 2928 | for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent)); |
| 2929 | if (!parent) { |
| 2930 | /* not in input, output, or notification */ |
| 2931 | return NULL; |
| 2932 | } |
| 2933 | |
| 2934 | ret_set = ly_set_new(); |
Radek Krejci | d9af8d2 | 2016-09-07 18:44:26 +0200 | [diff] [blame^] | 2935 | if (!ret_set) { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 2936 | return NULL; |
| 2937 | } |
| 2938 | |
| 2939 | LY_TREE_DFS_BEGIN(node, next, elem) { |
| 2940 | if ((options & LYXP_NO_LOCAL) && !(elem->flags & LYS_XPATH_DEP)) { |
| 2941 | /* elem has no dependencies from other subtrees and local nodes get discarded */ |
| 2942 | goto next_iter; |
| 2943 | } |
| 2944 | |
| 2945 | if (lyxp_node_atomize(elem, &set)) { |
| 2946 | ly_set_free(ret_set); |
| 2947 | free(set.val.snodes); |
| 2948 | return NULL; |
| 2949 | } |
| 2950 | |
| 2951 | for (i = 0; i < set.used; ++i) { |
| 2952 | switch (set.val.snodes[i].type) { |
| 2953 | case LYXP_NODE_ELEM: |
| 2954 | if (options & LYXP_NO_LOCAL) { |
| 2955 | for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp)); |
| 2956 | if (tmp) { |
| 2957 | /* in local subtree, discard */ |
| 2958 | break; |
| 2959 | } |
| 2960 | } |
| 2961 | if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) { |
| 2962 | ly_set_free(ret_set); |
| 2963 | free(set.val.snodes); |
| 2964 | return NULL; |
| 2965 | } |
| 2966 | break; |
| 2967 | default: |
| 2968 | /* ignore roots, text and attr should not ever appear */ |
| 2969 | break; |
| 2970 | } |
| 2971 | } |
| 2972 | |
| 2973 | free(set.val.snodes); |
| 2974 | if (!(options & LYXP_RECURSIVE)) { |
| 2975 | break; |
| 2976 | } |
| 2977 | next_iter: |
| 2978 | LY_TREE_DFS_END(node, next, elem); |
| 2979 | } |
| 2980 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 2981 | return ret_set; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 2982 | } |
| 2983 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2984 | static void |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 2985 | lys_switch_deviation(struct lys_deviation *dev, const struct lys_module *target_module) |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2986 | { |
| 2987 | int ret; |
| 2988 | char *parent_path; |
| 2989 | struct lys_node *target; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2990 | |
Pavol Vican | 64d0b76 | 2016-08-25 10:44:59 +0200 | [diff] [blame] | 2991 | if (!dev->deviate) { |
| 2992 | return ; |
| 2993 | } |
| 2994 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2995 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 2996 | if (dev->orig_node) { |
| 2997 | /* removing not-supported deviation ... */ |
| 2998 | if (strrchr(dev->target_name, '/') != dev->target_name) { |
| 2999 | /* ... from a parent */ |
| 3000 | parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name); |
| 3001 | |
| 3002 | target = NULL; |
| 3003 | ret = resolve_augment_schema_nodeid(parent_path, NULL, target_module, (const struct lys_node **)&target); |
| 3004 | free(parent_path); |
| 3005 | if (ret || !target) { |
| 3006 | LOGINT; |
| 3007 | return; |
| 3008 | } |
| 3009 | |
| 3010 | lys_node_addchild(target, NULL, dev->orig_node); |
| 3011 | } else { |
| 3012 | /* ... from top-level data */ |
| 3013 | lys_node_addchild(NULL, (struct lys_module *)target_module, dev->orig_node); |
| 3014 | } |
| 3015 | |
| 3016 | dev->orig_node = NULL; |
| 3017 | } else { |
| 3018 | /* adding not-supported deviation */ |
| 3019 | target = NULL; |
| 3020 | ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, (const struct lys_node **)&target); |
| 3021 | if (ret || !target) { |
| 3022 | LOGINT; |
| 3023 | return; |
| 3024 | } |
| 3025 | |
| 3026 | lys_node_unlink(target); |
| 3027 | dev->orig_node = target; |
| 3028 | } |
| 3029 | } else { |
| 3030 | target = NULL; |
| 3031 | ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, (const struct lys_node **)&target); |
| 3032 | if (ret || !target) { |
| 3033 | LOGINT; |
| 3034 | return; |
| 3035 | } |
| 3036 | |
| 3037 | lys_node_switch(target, dev->orig_node); |
| 3038 | dev->orig_node = target; |
| 3039 | } |
| 3040 | } |
| 3041 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3042 | /* temporarily removes or applies deviations, updates module deviation flag accordingly */ |
| 3043 | void |
| 3044 | lys_switch_deviations(struct lys_module *module) |
| 3045 | { |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3046 | uint32_t i = 0, j; |
| 3047 | const struct lys_module *mod; |
| 3048 | const char *ptr; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3049 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3050 | if (module->deviated) { |
| 3051 | while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) { |
| 3052 | if (mod == module) { |
| 3053 | continue; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3054 | } |
| 3055 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3056 | for (j = 0; j < mod->deviation_size; ++j) { |
| 3057 | ptr = strstr(mod->deviation[j].target_name, module->name); |
| 3058 | if (ptr && ptr[strlen(module->name)] == ':') { |
| 3059 | lys_switch_deviation(&mod->deviation[j], module); |
| 3060 | } |
| 3061 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3062 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3063 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3064 | if (module->deviated == 2) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3065 | module->deviated = 1; |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3066 | } else { |
| 3067 | module->deviated = 2; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3068 | } |
| 3069 | } |
| 3070 | } |
| 3071 | |
| 3072 | /* not needed currently, but tested and working */ |
| 3073 | #if 0 |
| 3074 | |
| 3075 | void |
| 3076 | lys_sub_module_apply_devs_augs(struct lys_module *module) |
| 3077 | { |
| 3078 | int i; |
| 3079 | struct lys_node_augment *aug; |
| 3080 | struct lys_node *last; |
| 3081 | |
| 3082 | /* re-apply deviations */ |
| 3083 | for (i = 0; i < module->deviation_size; ++i) { |
| 3084 | lys_switch_deviation(&module->deviation[i], module); |
| 3085 | assert(module->deviation[i].orig_node); |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3086 | lys_node_module(module->deviation[i].orig_node)->deviated = 1; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | /* re-apply augments */ |
| 3090 | for (i = 0; i < module->augment_size; ++i) { |
| 3091 | aug = &module->augment[i]; |
| 3092 | assert(aug->target); |
| 3093 | |
| 3094 | /* reconnect augmenting data into the target - add them to the target child list */ |
| 3095 | if (aug->target->child) { |
| 3096 | last = aug->target->child->prev; |
| 3097 | last->next = aug->child; |
| 3098 | aug->target->child->prev = aug->child->prev; |
| 3099 | aug->child->prev = last; |
| 3100 | } else { |
| 3101 | aug->target->child = aug->child; |
| 3102 | } |
| 3103 | } |
| 3104 | } |
| 3105 | |
| 3106 | #endif |
| 3107 | |
| 3108 | void |
| 3109 | lys_sub_module_remove_devs_augs(struct lys_module *module) |
| 3110 | { |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3111 | uint32_t i = 0, j; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3112 | struct lys_node *last, *elem; |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3113 | const struct lys_module *mod; |
| 3114 | struct lys_module *target_mod; |
| 3115 | const char *ptr; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3116 | |
| 3117 | /* remove applied deviations */ |
| 3118 | for (i = 0; i < module->deviation_size; ++i) { |
Pavol Vican | 64d0b76 | 2016-08-25 10:44:59 +0200 | [diff] [blame] | 3119 | if (module->deviation[i].orig_node) { |
| 3120 | target_mod = lys_node_module(module->deviation[i].orig_node); |
| 3121 | } else { |
| 3122 | target_mod = (struct lys_module *)lys_get_import_module(module, NULL, 0, module->deviation[i].target_name + 1, |
| 3123 | strcspn(module->deviation[i].target_name, ":") - 1); |
| 3124 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3125 | lys_switch_deviation(&module->deviation[i], module); |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3126 | assert(target_mod->deviated == 1); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3127 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3128 | /* clear the deviation flag if possible */ |
| 3129 | while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) { |
| 3130 | if ((mod == module) || (mod == target_mod)) { |
| 3131 | continue; |
| 3132 | } |
| 3133 | |
| 3134 | for (j = 0; j < mod->deviation_size; ++j) { |
| 3135 | ptr = strstr(mod->deviation[j].target_name, target_mod->name); |
| 3136 | if (ptr && (ptr[strlen(target_mod->name)] == ':')) { |
| 3137 | /* some other module deviation targets the inspected module, flag remains */ |
| 3138 | break; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3139 | } |
| 3140 | } |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3141 | |
| 3142 | if (j < mod->deviation_size) { |
| 3143 | break; |
| 3144 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3145 | } |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3146 | |
| 3147 | if (!mod) { |
| 3148 | target_mod->deviated = 0; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3149 | } |
| 3150 | } |
| 3151 | |
| 3152 | /* remove applied augments */ |
| 3153 | for (i = 0; i < module->augment_size; ++i) { |
Radek Krejci | dbc1526 | 2016-06-16 14:58:29 +0200 | [diff] [blame] | 3154 | if (!module->augment[i].target) { |
| 3155 | /* skip not resolved augments */ |
| 3156 | continue; |
| 3157 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3158 | |
| 3159 | elem = module->augment[i].child; |
| 3160 | if (elem) { |
| 3161 | LY_TREE_FOR(elem, last) { |
| 3162 | if (!last->next || (last->next->parent != (struct lys_node *)&module->augment[i])) { |
| 3163 | break; |
| 3164 | } |
| 3165 | } |
| 3166 | /* elem is first augment child, last is the last child */ |
| 3167 | |
| 3168 | /* parent child ptr */ |
| 3169 | if (module->augment[i].target->child == elem) { |
| 3170 | module->augment[i].target->child = last->next; |
| 3171 | } |
| 3172 | |
| 3173 | /* parent child next ptr */ |
| 3174 | if (elem->prev->next) { |
| 3175 | elem->prev->next = last->next; |
| 3176 | } |
| 3177 | |
| 3178 | /* parent child prev ptr */ |
| 3179 | if (last->next) { |
| 3180 | last->next->prev = elem->prev; |
| 3181 | } else if (module->augment[i].target->child) { |
| 3182 | module->augment[i].target->child->prev = elem->prev; |
| 3183 | } |
| 3184 | |
| 3185 | /* update augment children themselves */ |
| 3186 | elem->prev = last; |
| 3187 | last->next = NULL; |
| 3188 | } |
Michal Vasko | b8f7132 | 2016-05-03 11:39:56 +0200 | [diff] [blame] | 3189 | |
| 3190 | /* needs to be NULL for lys_augment_free() to free the children */ |
| 3191 | module->augment[i].target = NULL; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3192 | } |
| 3193 | } |
| 3194 | |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 3195 | int |
| 3196 | lys_module_set_implement(struct lys_module *module) |
| 3197 | { |
| 3198 | struct ly_ctx *ctx; |
| 3199 | int i; |
| 3200 | |
| 3201 | if (module->implemented) { |
| 3202 | return EXIT_SUCCESS; |
| 3203 | } |
| 3204 | |
| 3205 | ctx = module->ctx; |
| 3206 | |
| 3207 | for (i = 0; i < ctx->models.used; ++i) { |
| 3208 | if (module == ctx->models.list[i]) { |
| 3209 | continue; |
| 3210 | } |
| 3211 | |
| 3212 | if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) { |
| 3213 | LOGERR(LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name); |
| 3214 | return EXIT_FAILURE; |
| 3215 | } |
| 3216 | } |
| 3217 | |
| 3218 | module->implemented = 1; |
| 3219 | return EXIT_SUCCESS; |
| 3220 | } |
| 3221 | |
| 3222 | int |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3223 | lys_sub_module_set_dev_aug_target_implement(struct lys_module *module) |
| 3224 | { |
| 3225 | int i; |
| 3226 | struct lys_module *trg_mod; |
| 3227 | |
| 3228 | for (i = 0; i < module->deviation_size; ++i) { |
| 3229 | assert(module->deviation[i].orig_node); |
| 3230 | trg_mod = lys_node_module(module->deviation[i].orig_node); |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 3231 | if (lys_module_set_implement(trg_mod)) { |
| 3232 | return EXIT_FAILURE; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3233 | } |
| 3234 | } |
| 3235 | |
| 3236 | for (i = 0; i < module->augment_size; ++i) { |
| 3237 | assert(module->augment[i].target); |
| 3238 | trg_mod = lys_node_module(module->augment[i].target); |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 3239 | if (lys_module_set_implement(trg_mod)) { |
| 3240 | return EXIT_FAILURE; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3241 | } |
| 3242 | } |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 3243 | |
| 3244 | return EXIT_SUCCESS; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | void |
| 3248 | lys_submodule_module_data_free(struct lys_submodule *submodule) |
| 3249 | { |
| 3250 | struct lys_node *next, *elem; |
| 3251 | |
| 3252 | /* remove parsed data */ |
| 3253 | LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) { |
| 3254 | if (elem->module == (struct lys_module *)submodule) { |
| 3255 | lys_node_free(elem, NULL, 0); |
| 3256 | } |
| 3257 | } |
| 3258 | } |
Radek Krejci | a1c33bf | 2016-09-07 12:38:49 +0200 | [diff] [blame] | 3259 | |
| 3260 | int |
| 3261 | lys_is_key(struct lys_node_list *list, struct lys_node_leaf *leaf) |
| 3262 | { |
| 3263 | uint8_t i; |
| 3264 | |
| 3265 | for (i = 0; i < list->keys_size; i++) { |
| 3266 | if (list->keys[i] == leaf) { |
| 3267 | return i + 1; |
| 3268 | } |
| 3269 | } |
| 3270 | |
| 3271 | return 0; |
| 3272 | } |