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