Radek Krejci | 3f5e3db | 2018-10-11 15:57:47 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_schema.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Schema tree implementation |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 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 |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 14 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 17 | #include "tree_schema.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 18 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include <assert.h> |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 20 | #include <dirent.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 21 | #include <errno.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdint.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 23 | #include <stdio.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 26 | #include <sys/stat.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 27 | #include <unistd.h> |
Radek Krejci | 3f5e3db | 2018-10-11 15:57:47 +0200 | [diff] [blame] | 28 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 29 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 30 | #include "compat.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 31 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 32 | #include "dict.h" |
| 33 | #include "log.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 34 | #include "in_internal.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 35 | #include "parser_internal.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 36 | #include "parser_schema.h" |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 37 | #include "path.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 38 | #include "schema_compile.h" |
| 39 | #include "schema_compile_amend.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 40 | #include "set.h" |
| 41 | #include "tree.h" |
| 42 | #include "tree_schema_internal.h" |
| 43 | #include "xpath.h" |
Radek Krejci | 3f5e3db | 2018-10-11 15:57:47 +0200 | [diff] [blame] | 44 | |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 45 | API LY_ERR |
| 46 | lysc_tree_dfs_full(const struct lysc_node *root, lysc_dfs_clb dfs_clb, void *data) |
| 47 | { |
| 48 | struct lysc_node *elem, *ops, *elem2; |
| 49 | LY_ARRAY_COUNT_TYPE u; |
| 50 | |
| 51 | LY_CHECK_ARG_RET(NULL, root, dfs_clb, LY_EINVAL); |
| 52 | |
| 53 | LYSC_TREE_DFS_BEGIN(root, elem) { |
| 54 | /* schema node */ |
| 55 | LY_CHECK_RET(dfs_clb(elem, data, &LYSC_TREE_DFS_continue)); |
| 56 | |
| 57 | ops = (struct lysc_node *)lysc_node_actions(elem); |
| 58 | LY_ARRAY_FOR(ops, u) { |
| 59 | LYSC_TREE_DFS_BEGIN(&ops[u], elem2) { |
| 60 | /* action subtree */ |
| 61 | LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue)); |
| 62 | |
| 63 | LYSC_TREE_DFS_END(&ops[u], elem2); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | ops = (struct lysc_node *)lysc_node_notifs(elem); |
| 68 | LY_ARRAY_FOR(ops, u) { |
| 69 | LYSC_TREE_DFS_BEGIN(&ops[u], elem2) { |
| 70 | /* notification subtree */ |
| 71 | LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue)); |
| 72 | |
| 73 | LYSC_TREE_DFS_END(&ops[u], elem2); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | LYSC_TREE_DFS_END(root, elem); |
| 78 | } |
| 79 | |
| 80 | return LY_SUCCESS; |
| 81 | } |
| 82 | |
| 83 | API LY_ERR |
| 84 | lysc_module_dfs_full(const struct lys_module *mod, lysc_dfs_clb dfs_clb, void *data) |
| 85 | { |
| 86 | LY_ARRAY_COUNT_TYPE u; |
| 87 | |
| 88 | LY_CHECK_ARG_RET(NULL, mod, mod->compiled, dfs_clb, LY_EINVAL); |
| 89 | |
| 90 | /* schema nodes */ |
| 91 | LY_CHECK_RET(lysc_tree_dfs_full(mod->compiled->data, dfs_clb, data)); |
| 92 | |
| 93 | /* RPCs */ |
| 94 | LY_ARRAY_FOR(mod->compiled->rpcs, u) { |
| 95 | LY_CHECK_RET(lysc_tree_dfs_full((struct lysc_node *)&mod->compiled->rpcs[u], dfs_clb, data)); |
| 96 | } |
| 97 | |
| 98 | /* notifications */ |
| 99 | LY_ARRAY_FOR(mod->compiled->notifs, u) { |
| 100 | LY_CHECK_RET(lysc_tree_dfs_full((struct lysc_node *)&mod->compiled->notifs[u], dfs_clb, data)); |
| 101 | } |
| 102 | |
| 103 | return LY_SUCCESS; |
| 104 | } |
| 105 | |
Radek Krejci | b93bd41 | 2020-11-02 13:23:11 +0100 | [diff] [blame^] | 106 | static void |
| 107 | lys_getnext_into_case(const struct lysc_node_case *first_case, const struct lysc_node **last, const struct lysc_node **next) |
| 108 | { |
| 109 | for (; first_case; first_case = (const struct lysc_node_case*)first_case->next) { |
| 110 | if (first_case->child) { |
| 111 | /* there is something to return */ |
| 112 | (*next) = first_case->child; |
| 113 | return; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /* no children in choice's cases, so go to the choice's sibling instead of into it */ |
| 118 | (*last) = (*next); |
| 119 | (*next) = (*next)->next; |
| 120 | } |
| 121 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 122 | API const struct lysc_node * |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 123 | lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, uint32_t options) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 124 | { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 125 | const struct lysc_node *next = NULL; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 126 | struct lysc_node **snode; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 127 | ly_bool action_flag = 0, notif_flag = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 128 | const struct lysc_action *actions; |
| 129 | const struct lysc_notif *notifs; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 130 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 131 | |
| 132 | LY_CHECK_ARG_RET(NULL, parent || module, NULL); |
| 133 | |
Radek Krejci | d5a2b9d | 2019-04-12 10:39:30 +0200 | [diff] [blame] | 134 | next: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 135 | if (!last) { |
| 136 | /* first call */ |
| 137 | |
| 138 | /* get know where to start */ |
| 139 | if (parent) { |
| 140 | /* schema subtree */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 141 | if ((parent->nodetype == LYS_CHOICE) && (options & LYS_GETNEXT_WITHCASE)) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 142 | if (((struct lysc_node_choice *)parent)->cases) { |
Radek Krejci | b93bd41 | 2020-11-02 13:23:11 +0100 | [diff] [blame^] | 143 | next = last = (const struct lysc_node *)((struct lysc_node_choice *)parent)->cases; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 144 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 145 | } else { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 146 | snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 147 | /* do not return anything if the node does not have any children */ |
Radek Krejci | d5a2b9d | 2019-04-12 10:39:30 +0200 | [diff] [blame] | 148 | if (snode && *snode) { |
| 149 | next = last = *snode; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 150 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 151 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 152 | } else { |
| 153 | /* top level data */ |
| 154 | next = last = module->data; |
| 155 | } |
| 156 | if (!next) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 157 | /* try to get action or notification */ |
| 158 | goto repeat; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 159 | } |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 160 | /* test if the next can be returned */ |
| 161 | goto check; |
| 162 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 163 | } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 164 | action_flag = 1; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 165 | if (last->parent) { |
| 166 | actions = lysc_node_actions(last->parent); |
| 167 | } else { |
| 168 | actions = module->rpcs; |
| 169 | } |
| 170 | LY_ARRAY_FOR(actions, u) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 171 | if (&actions[u] == (struct lysc_action *)last) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 172 | break; |
| 173 | } |
| 174 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 175 | if (u + 1 < LY_ARRAY_COUNT(actions)) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 176 | next = (struct lysc_node *)(&actions[u + 1]); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 177 | } |
| 178 | goto repeat; |
| 179 | } else if (last->nodetype == LYS_NOTIF) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 180 | action_flag = notif_flag = 1; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 181 | if (last->parent) { |
| 182 | notifs = lysc_node_notifs(last->parent); |
| 183 | } else { |
| 184 | notifs = module->notifs; |
| 185 | } |
| 186 | LY_ARRAY_FOR(notifs, u) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 187 | if (¬ifs[u] == (struct lysc_notif *)last) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 188 | break; |
| 189 | } |
| 190 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 191 | if (u + 1 < LY_ARRAY_COUNT(notifs)) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 192 | next = (struct lysc_node *)(¬ifs[u + 1]); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 193 | } |
| 194 | goto repeat; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 195 | } else { |
| 196 | next = last->next; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 199 | repeat: |
| 200 | if (!next) { |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 201 | /* possibly go back to parent */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 202 | if (last && (last->parent != parent)) { |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 203 | last = last->parent; |
Radek Krejci | d5a2b9d | 2019-04-12 10:39:30 +0200 | [diff] [blame] | 204 | goto next; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 205 | } else if (!action_flag) { |
| 206 | action_flag = 1; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 207 | next = parent ? (struct lysc_node *)lysc_node_actions(parent) : (struct lysc_node *)module->rpcs; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 208 | } else if (!notif_flag) { |
| 209 | notif_flag = 1; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 210 | next = parent ? (struct lysc_node *)lysc_node_notifs(parent) : (struct lysc_node *)module->notifs; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 211 | } else { |
| 212 | return NULL; |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 213 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 214 | goto repeat; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 215 | } |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 216 | check: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 217 | switch (next->nodetype) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 218 | case LYS_RPC: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 219 | case LYS_ACTION: |
| 220 | case LYS_NOTIF: |
| 221 | case LYS_LEAF: |
| 222 | case LYS_ANYXML: |
| 223 | case LYS_ANYDATA: |
| 224 | case LYS_LIST: |
| 225 | case LYS_LEAFLIST: |
| 226 | break; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 227 | case LYS_CASE: |
| 228 | if (options & LYS_GETNEXT_WITHCASE) { |
| 229 | break; |
| 230 | } else { |
| 231 | /* go into */ |
Radek Krejci | b93bd41 | 2020-11-02 13:23:11 +0100 | [diff] [blame^] | 232 | lys_getnext_into_case((const struct lysc_node_case *)next, &last, &next); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 233 | } |
| 234 | goto repeat; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 235 | case LYS_CONTAINER: |
| 236 | if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) { |
| 237 | if (((struct lysc_node_container *)next)->child) { |
| 238 | /* go into */ |
| 239 | next = ((struct lysc_node_container *)next)->child; |
| 240 | } else { |
Radek Krejci | b93bd41 | 2020-11-02 13:23:11 +0100 | [diff] [blame^] | 241 | last = next; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 242 | next = next->next; |
| 243 | } |
| 244 | goto repeat; |
| 245 | } |
| 246 | break; |
| 247 | case LYS_CHOICE: |
| 248 | if (options & LYS_GETNEXT_WITHCHOICE) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 249 | break; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 250 | } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) { |
| 251 | next = next->next; |
| 252 | } else { |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 253 | if (options & LYS_GETNEXT_WITHCASE) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 254 | next = (struct lysc_node *)((struct lysc_node_choice *)next)->cases; |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 255 | } else { |
Radek Krejci | b93bd41 | 2020-11-02 13:23:11 +0100 | [diff] [blame^] | 256 | /* go into */ |
| 257 | lys_getnext_into_case(((struct lysc_node_choice *)next)->cases, &last, &next); |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 258 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 259 | } |
| 260 | goto repeat; |
| 261 | default: |
| 262 | /* we should not be here */ |
Radek Krejci | b07b5c9 | 2019-04-08 10:56:37 +0200 | [diff] [blame] | 263 | LOGINT(module ? module->mod->ctx : parent->module->ctx); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 264 | return NULL; |
| 265 | } |
| 266 | |
| 267 | if (!(options & LYS_GETNEXT_NOSTATECHECK)) { |
| 268 | /* check if the node is disabled by if-feature */ |
Radek Krejci | fab954b | 2019-09-11 11:25:14 +0200 | [diff] [blame] | 269 | if (lysc_node_is_disabled(next, 0)) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 270 | next = next->next; |
| 271 | goto repeat; |
| 272 | } |
| 273 | } |
| 274 | return next; |
| 275 | } |
| 276 | |
| 277 | API const struct lysc_node * |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 278 | lys_find_child(const struct lysc_node *parent, const struct lys_module *module, const char *name, size_t name_len, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 279 | uint16_t nodetype, uint32_t options) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 280 | { |
| 281 | const struct lysc_node *node = NULL; |
| 282 | |
| 283 | LY_CHECK_ARG_RET(NULL, module, name, NULL); |
| 284 | if (!nodetype) { |
| 285 | nodetype = 0xffff; |
| 286 | } |
| 287 | |
| 288 | while ((node = lys_getnext(node, parent, module->compiled, options))) { |
| 289 | if (!(node->nodetype & nodetype)) { |
| 290 | continue; |
| 291 | } |
| 292 | if (node->module != module) { |
| 293 | continue; |
| 294 | } |
| 295 | |
| 296 | if (name_len) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 297 | if (!ly_strncmp(node->name, name, name_len)) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 298 | return node; |
| 299 | } |
| 300 | } else { |
| 301 | if (!strcmp(node->name, name)) { |
| 302 | return node; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | return NULL; |
| 307 | } |
| 308 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 309 | API LY_ERR |
Radek Krejci | bed1394 | 2020-10-19 16:06:28 +0200 | [diff] [blame] | 310 | lys_find_xpath_atoms(const struct lysc_node *ctx_node, const char *xpath, uint32_t options, struct ly_set **set) |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 311 | { |
| 312 | LY_ERR ret = LY_SUCCESS; |
| 313 | struct lyxp_set xp_set; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 314 | struct lyxp_expr *exp = NULL; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 315 | uint32_t i; |
| 316 | |
| 317 | LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL); |
| 318 | if (!(options & LYXP_SCNODE_ALL)) { |
| 319 | options = LYXP_SCNODE; |
| 320 | } |
| 321 | |
| 322 | memset(&xp_set, 0, sizeof xp_set); |
| 323 | |
| 324 | /* compile expression */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 325 | ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp); |
| 326 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 327 | |
| 328 | /* atomize expression */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 329 | ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 330 | LY_CHECK_GOTO(ret, cleanup); |
| 331 | |
| 332 | /* allocate return set */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 333 | ret = ly_set_new(set); |
| 334 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 335 | |
| 336 | /* transform into ly_set */ |
| 337 | (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs); |
| 338 | LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup); |
| 339 | (*set)->size = xp_set.used; |
| 340 | |
| 341 | for (i = 0; i < xp_set.used; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 342 | if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 343 | ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 344 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
| 348 | cleanup: |
| 349 | lyxp_set_free_content(&xp_set); |
| 350 | lyxp_expr_free(ctx_node->module->ctx, exp); |
| 351 | return ret; |
| 352 | } |
| 353 | |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 354 | API LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 355 | lys_find_path_atoms(const struct ly_path *path, struct ly_set **set) |
| 356 | { |
| 357 | LY_ERR ret = LY_SUCCESS; |
| 358 | LY_ARRAY_COUNT_TYPE u, v; |
| 359 | |
| 360 | LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL); |
| 361 | |
| 362 | /* allocate return set */ |
| 363 | LY_CHECK_RET(ly_set_new(set)); |
| 364 | |
| 365 | LY_ARRAY_FOR(path, u) { |
| 366 | /* add nodes from the path */ |
| 367 | LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].node, 0, NULL), cleanup); |
| 368 | if (path[u].pred_type == LY_PATH_PREDTYPE_LIST) { |
| 369 | LY_ARRAY_FOR(path[u].predicates, v) { |
| 370 | /* add all the keys in a predicate */ |
| 371 | LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | cleanup: |
| 377 | if (ret) { |
| 378 | ly_set_free(*set, NULL); |
| 379 | *set = NULL; |
| 380 | } |
| 381 | return ret; |
| 382 | } |
| 383 | |
| 384 | API LY_ERR |
| 385 | lys_find_expr_atoms(const struct lysc_node *ctx_node, const struct lys_module *cur_mod, const struct lyxp_expr *expr, |
| 386 | const struct lysc_prefix *prefixes, uint32_t options, struct ly_set **set) |
| 387 | { |
| 388 | LY_ERR ret = LY_SUCCESS; |
| 389 | struct lyxp_set xp_set = {0}; |
| 390 | uint32_t i; |
| 391 | |
| 392 | LY_CHECK_ARG_RET(NULL, cur_mod, expr, prefixes, set, LY_EINVAL); |
| 393 | if (!(options & LYXP_SCNODE_ALL)) { |
| 394 | options = LYXP_SCNODE; |
| 395 | } |
| 396 | |
| 397 | /* atomize expression */ |
| 398 | ret = lyxp_atomize(expr, cur_mod, LY_PREF_SCHEMA_RESOLVED, (void *)prefixes, ctx_node, &xp_set, options); |
| 399 | LY_CHECK_GOTO(ret, cleanup); |
| 400 | |
| 401 | /* allocate return set */ |
| 402 | ret = ly_set_new(set); |
| 403 | LY_CHECK_GOTO(ret, cleanup); |
| 404 | |
| 405 | /* transform into ly_set */ |
| 406 | (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs); |
| 407 | LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(cur_mod->ctx); ret = LY_EMEM, cleanup); |
| 408 | (*set)->size = xp_set.used; |
| 409 | |
| 410 | for (i = 0; i < xp_set.used; ++i) { |
| 411 | if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == 1)) { |
| 412 | ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL); |
| 413 | LY_CHECK_GOTO(ret, cleanup); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | cleanup: |
| 418 | lyxp_set_free_content(&xp_set); |
| 419 | if (ret) { |
| 420 | ly_set_free(*set, NULL); |
| 421 | *set = NULL; |
| 422 | } |
| 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 427 | lys_find_xpath(const struct lysc_node *ctx_node, const char *xpath, uint32_t options, struct ly_set **set) |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 428 | { |
| 429 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 430 | struct lyxp_set xp_set = {0}; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 431 | struct lyxp_expr *exp = NULL; |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 432 | uint32_t i; |
| 433 | |
| 434 | LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL); |
| 435 | if (!(options & LYXP_SCNODE_ALL)) { |
| 436 | options = LYXP_SCNODE; |
| 437 | } |
| 438 | |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 439 | /* compile expression */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 440 | ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp); |
| 441 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 442 | |
| 443 | /* atomize expression */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 444 | ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 445 | LY_CHECK_GOTO(ret, cleanup); |
| 446 | |
| 447 | /* allocate return set */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 448 | ret = ly_set_new(set); |
| 449 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 450 | |
| 451 | /* transform into ly_set */ |
| 452 | (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs); |
| 453 | LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup); |
| 454 | (*set)->size = xp_set.used; |
| 455 | |
| 456 | for (i = 0; i < xp_set.used; ++i) { |
| 457 | if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == 1)) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 458 | ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 459 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | cleanup: |
| 464 | lyxp_set_free_content(&xp_set); |
| 465 | lyxp_expr_free(ctx_node->module->ctx, exp); |
Michal Vasko | ae15966 | 2020-10-21 11:57:24 +0200 | [diff] [blame] | 466 | if (ret) { |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 467 | ly_set_free(*set, NULL); |
| 468 | *set = NULL; |
| 469 | } |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 470 | return ret; |
| 471 | } |
| 472 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 473 | char * |
| 474 | lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LYSC_PATH_TYPE pathtype, char *buffer, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 475 | size_t buflen) |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 476 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 477 | const struct lysc_node *iter; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 478 | char *path = NULL; |
| 479 | int len = 0; |
| 480 | |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 481 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 482 | if (buffer) { |
| 483 | LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL); |
| 484 | } |
| 485 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 486 | switch (pathtype) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 487 | case LYSC_PATH_LOG: |
Michal Vasko | 65de040 | 2020-08-03 16:34:19 +0200 | [diff] [blame] | 488 | case LYSC_PATH_DATA: |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 489 | for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) { |
Michal Vasko | 11deea1 | 2020-08-05 13:54:50 +0200 | [diff] [blame] | 490 | char *s, *id; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 491 | const char *slash; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 492 | |
Michal Vasko | 65de040 | 2020-08-03 16:34:19 +0200 | [diff] [blame] | 493 | if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 494 | /* schema-only node */ |
| 495 | continue; |
| 496 | } |
| 497 | |
Michal Vasko | 11deea1 | 2020-08-05 13:54:50 +0200 | [diff] [blame] | 498 | s = buffer ? strdup(buffer) : path; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 499 | id = strdup(iter->name); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 500 | if (parent && (iter->parent == parent)) { |
| 501 | slash = ""; |
| 502 | } else { |
| 503 | slash = "/"; |
| 504 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 505 | if (!iter->parent || (iter->parent->module != iter->module)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 506 | /* print prefix */ |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 507 | if (buffer) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 508 | len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : ""); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 509 | } else { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 510 | len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : ""); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 511 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 512 | } else { |
| 513 | /* prefix is the same as in parent */ |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 514 | if (buffer) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 515 | len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : ""); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 516 | } else { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 517 | len = asprintf(&path, "%s%s%s", slash, id, s ? s : ""); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 518 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 519 | } |
| 520 | free(s); |
| 521 | free(id); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 522 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 523 | if (buffer && (buflen <= (size_t)len)) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 524 | /* not enough space in buffer */ |
| 525 | break; |
| 526 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | if (len < 0) { |
| 530 | free(path); |
| 531 | path = NULL; |
| 532 | } else if (len == 0) { |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 533 | if (buffer) { |
| 534 | strcpy(buffer, "/"); |
| 535 | } else { |
| 536 | path = strdup("/"); |
| 537 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 538 | } |
| 539 | break; |
| 540 | } |
| 541 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 542 | if (buffer) { |
| 543 | return buffer; |
| 544 | } else { |
| 545 | return path; |
| 546 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 547 | } |
| 548 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 549 | API char * |
| 550 | lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen) |
| 551 | { |
| 552 | return lysc_path_until(node, NULL, pathtype, buffer, buflen); |
| 553 | } |
| 554 | |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 555 | API LY_ERR |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 556 | lysc_feature_value(const struct lysc_feature *feature) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 557 | { |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 558 | LY_CHECK_ARG_RET(NULL, feature, LY_EINVAL); |
| 559 | return feature->flags & LYS_FENABLED ? LY_SUCCESS : LY_ENOT; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 560 | } |
| 561 | |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 562 | uint8_t |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 563 | lysc_iff_getop(uint8_t *list, size_t pos) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 564 | { |
| 565 | uint8_t *item; |
| 566 | uint8_t mask = 3, result; |
| 567 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 568 | item = &list[pos / 4]; |
| 569 | result = (*item) & (mask << 2 * (pos % 4)); |
| 570 | return result >> 2 * (pos % 4); |
| 571 | } |
| 572 | |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 573 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 574 | lysc_iffeature_value_(const struct lysc_iffeature *iff, size_t *index_e, size_t *index_f) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 575 | { |
| 576 | uint8_t op; |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 577 | LY_ERR a, b; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 578 | |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 579 | op = lysc_iff_getop(iff->expr, *index_e); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 580 | (*index_e)++; |
| 581 | |
| 582 | switch (op) { |
| 583 | case LYS_IFF_F: |
| 584 | /* resolve feature */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 585 | return lysc_feature_value(iff->features[(*index_f)++]); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 586 | case LYS_IFF_NOT: |
| 587 | /* invert result */ |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 588 | return lysc_iffeature_value_(iff, index_e, index_f) == LY_SUCCESS ? LY_ENOT : LY_SUCCESS; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 589 | case LYS_IFF_AND: |
| 590 | case LYS_IFF_OR: |
| 591 | a = lysc_iffeature_value_(iff, index_e, index_f); |
| 592 | b = lysc_iffeature_value_(iff, index_e, index_f); |
| 593 | if (op == LYS_IFF_AND) { |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 594 | if ((a == LY_SUCCESS) && (b == LY_SUCCESS)) { |
| 595 | return LY_SUCCESS; |
| 596 | } else { |
| 597 | return LY_ENOT; |
| 598 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 599 | } else { /* LYS_IFF_OR */ |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 600 | if ((a == LY_SUCCESS) || (b == LY_SUCCESS)) { |
| 601 | return LY_SUCCESS; |
| 602 | } else { |
| 603 | return LY_ENOT; |
| 604 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 611 | API LY_ERR |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 612 | lysc_iffeature_value(const struct lysc_iffeature *iff) |
| 613 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 614 | size_t index_e = 0, index_f = 0; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 615 | |
| 616 | LY_CHECK_ARG_RET(NULL, iff, -1); |
| 617 | |
| 618 | if (iff->expr) { |
| 619 | return lysc_iffeature_value_(iff, &index_e, &index_f); |
| 620 | } |
| 621 | return 0; |
| 622 | } |
| 623 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 624 | /** |
| 625 | * @brief Enable/Disable the specified feature in the module. |
| 626 | * |
| 627 | * If the feature is already set to the desired value, LY_SUCCESS is returned. |
| 628 | * By changing the feature, also all the feature which depends on it via their |
Radek Krejci | a8c71e6 | 2020-10-26 08:30:50 +0100 | [diff] [blame] | 629 | * if-feature statements are again evaluated (disabled if a if-feature statement |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 630 | * evaluates to false). |
| 631 | * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 632 | * @param[in] mod Module where to set (search for) the feature. |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 633 | * @param[in] name Name of the feature to set. Asterisk ('*') can be used to |
| 634 | * set all the features in the module. |
| 635 | * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable). |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 636 | * @param[in] skip_checks Flag to skip checking of if-features and just set @p value of the feature. |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 637 | * @return LY_ERR value. |
| 638 | */ |
| 639 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 640 | lys_feature_change(const struct lys_module *mod, const char *name, ly_bool value, ly_bool skip_checks) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 641 | { |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 642 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 643 | ly_bool all = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 644 | LY_ARRAY_COUNT_TYPE u, disabled_count; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 645 | uint32_t changed_count; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 646 | struct lysc_feature *f, **df; |
| 647 | struct lysc_iffeature *iff; |
| 648 | struct ly_set *changed; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 649 | struct ly_ctx *ctx = mod->ctx; /* shortcut */ |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 650 | |
Radek Krejci | 6e67c40 | 2019-05-02 09:55:39 +0200 | [diff] [blame] | 651 | if (!strcmp(name, "*")) { |
| 652 | /* enable all */ |
| 653 | all = 1; |
| 654 | } |
| 655 | |
Michal Vasko | 997f982 | 2020-10-14 13:09:05 +0200 | [diff] [blame] | 656 | if (!mod->implemented) { |
| 657 | LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled.", mod->name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 658 | return LY_EINVAL; |
| 659 | } |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 660 | if (!mod->features) { |
Radek Krejci | 6e67c40 | 2019-05-02 09:55:39 +0200 | [diff] [blame] | 661 | if (all) { |
| 662 | /* no feature to enable */ |
| 663 | return LY_SUCCESS; |
| 664 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 665 | LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name); |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 666 | return LY_ENOTFOUND; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 667 | } |
| 668 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 669 | LY_CHECK_RET(ly_set_new(&changed)); |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 670 | changed_count = 0; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 671 | |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 672 | run: |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 673 | for (disabled_count = u = 0; u < LY_ARRAY_COUNT(mod->features); ++u) { |
| 674 | f = &mod->features[u]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 675 | if (all || !strcmp(f->name, name)) { |
| 676 | if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) { |
| 677 | if (all) { |
| 678 | /* skip already set features */ |
| 679 | continue; |
| 680 | } else { |
| 681 | /* feature already set correctly */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 682 | goto cleanup; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
| 686 | if (value) { /* enable */ |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 687 | if (!skip_checks) { |
| 688 | /* check referenced features if they are enabled */ |
| 689 | LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) { |
| 690 | if (lysc_iffeature_value(iff) == LY_ENOT) { |
| 691 | if (all) { |
| 692 | ++disabled_count; |
| 693 | goto next; |
| 694 | } else { |
Michal Vasko | 997f982 | 2020-10-14 13:09:05 +0200 | [diff] [blame] | 695 | LOGERR(ctx, LY_EDENIED, "Feature \"%s\" cannot be enabled since it is disabled by " |
| 696 | "its if-feature condition(s).", f->name); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 697 | ret = LY_EDENIED; |
| 698 | goto cleanup; |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 699 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | } |
| 703 | /* enable the feature */ |
| 704 | f->flags |= LYS_FENABLED; |
| 705 | } else { /* disable */ |
| 706 | /* disable the feature */ |
| 707 | f->flags &= ~LYS_FENABLED; |
| 708 | } |
| 709 | |
| 710 | /* remember the changed feature */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 711 | ret = ly_set_add(changed, f, 1, NULL); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 712 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 713 | |
| 714 | if (!all) { |
| 715 | /* stop in case changing a single feature */ |
| 716 | break; |
| 717 | } |
| 718 | } |
| 719 | next: |
| 720 | ; |
| 721 | } |
| 722 | |
| 723 | if (!all && !changed->count) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 724 | LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 725 | ret = LY_ENOTFOUND; |
| 726 | goto cleanup; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 727 | } |
| 728 | |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 729 | if (value && all && disabled_count) { |
| 730 | if (changed_count == changed->count) { |
| 731 | /* no change in last run -> not able to enable all ... */ |
| 732 | /* ... print errors */ |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 733 | for (u = 0; disabled_count && u < LY_ARRAY_COUNT(mod->features); ++u) { |
| 734 | if (!(mod->features[u].flags & LYS_FENABLED)) { |
Michal Vasko | 997f982 | 2020-10-14 13:09:05 +0200 | [diff] [blame] | 735 | LOGERR(ctx, LY_EDENIED, "Feature \"%s\" cannot be enabled since it is disabled by its if-feature " |
| 736 | "condition(s).", mod->features[u].name); |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 737 | --disabled_count; |
| 738 | } |
| 739 | } |
| 740 | /* ... restore the original state */ |
| 741 | for (u = 0; u < changed->count; ++u) { |
| 742 | f = changed->objs[u]; |
| 743 | /* re-disable the feature */ |
| 744 | f->flags &= ~LYS_FENABLED; |
| 745 | } |
| 746 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 747 | ret = LY_EDENIED; |
| 748 | goto cleanup; |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 749 | } else { |
| 750 | /* we did some change in last run, try it again */ |
| 751 | changed_count = changed->count; |
| 752 | goto run; |
| 753 | } |
| 754 | } |
| 755 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 756 | /* reflect change(s) in the dependent features */ |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 757 | for (u = 0; !skip_checks && (u < changed->count); ++u) { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 758 | /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of |
| 759 | * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled |
| 760 | * is not done - by default, features are disabled and must be explicitely enabled. */ |
| 761 | f = changed->objs[u]; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 762 | LY_ARRAY_FOR(f->depfeatures, struct lysc_feature *, df) { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 763 | if (!((*df)->flags & LYS_FENABLED)) { |
| 764 | /* not enabled, nothing to do */ |
| 765 | continue; |
| 766 | } |
| 767 | /* check the feature's if-features which could change by the previous change of our feature */ |
| 768 | LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) { |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 769 | if (lysc_iffeature_value(iff) == LY_ENOT) { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 770 | /* the feature must be disabled now */ |
| 771 | (*df)->flags &= ~LYS_FENABLED; |
| 772 | /* add the feature into the list of changed features */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 773 | ret = ly_set_add(changed, *df, 1, NULL); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 774 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 775 | break; |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | |
Michal Vasko | 87a97f4 | 2020-10-06 10:30:28 +0200 | [diff] [blame] | 781 | /* success */ |
| 782 | ++mod->ctx->module_set_id; |
| 783 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 784 | cleanup: |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 785 | ly_set_free(changed, NULL); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 786 | return ret; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | API LY_ERR |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 790 | lys_feature_enable(const struct lys_module *module, const char *feature) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 791 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 792 | LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 793 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 794 | return lys_feature_change((struct lys_module *)module, feature, 1, 0); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | API LY_ERR |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 798 | lys_feature_disable(const struct lys_module *module, const char *feature) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 799 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 800 | LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 801 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 802 | return lys_feature_change((struct lys_module *)module, feature, 0, 0); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 803 | } |
| 804 | |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 805 | API LY_ERR |
| 806 | lys_feature_enable_force(const struct lys_module *module, const char *feature) |
| 807 | { |
| 808 | LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL); |
| 809 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 810 | return lys_feature_change((struct lys_module *)module, feature, 1, 1); |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | API LY_ERR |
| 814 | lys_feature_disable_force(const struct lys_module *module, const char *feature) |
| 815 | { |
| 816 | LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL); |
| 817 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 818 | return lys_feature_change((struct lys_module *)module, feature, 0, 1); |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | API LY_ERR |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 822 | lys_feature_value(const struct lys_module *module, const char *feature) |
| 823 | { |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 824 | struct lysc_feature *f = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 825 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 826 | |
| 827 | LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 828 | |
| 829 | /* search for the specified feature */ |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 830 | LY_ARRAY_FOR(module->features, u) { |
| 831 | f = &module->features[u]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 832 | if (!strcmp(f->name, feature)) { |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 833 | break; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
| 837 | /* feature definition not found */ |
Michal Vasko | 82c31e6 | 2020-07-17 15:30:40 +0200 | [diff] [blame] | 838 | if (!f) { |
| 839 | return LY_ENOTFOUND; |
| 840 | } |
| 841 | |
| 842 | /* feature disabled */ |
| 843 | if (!(f->flags & LYS_FENABLED)) { |
| 844 | return LY_ENOT; |
| 845 | } |
| 846 | |
| 847 | /* check referenced features if they are enabled */ |
| 848 | LY_ARRAY_FOR(f->iffeatures, u) { |
| 849 | if (lysc_iffeature_value(&f->iffeatures[u]) == LY_ENOT) { |
| 850 | /* if-feature disabled */ |
| 851 | return LY_ENOT; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | /* feature enabled */ |
| 856 | return LY_SUCCESS; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 857 | } |
| 858 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 859 | API const struct lysc_node * |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 860 | lysc_node_is_disabled(const struct lysc_node *node, ly_bool recursive) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 861 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 862 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 863 | |
| 864 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 865 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 866 | do { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 867 | if (node->iffeatures) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 868 | /* check local if-features */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 869 | LY_ARRAY_FOR(node->iffeatures, u) { |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 870 | if (lysc_iffeature_value(&node->iffeatures[u]) == LY_ENOT) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 871 | return node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | if (!recursive) { |
| 877 | return NULL; |
| 878 | } |
| 879 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 880 | /* go through schema-only parents */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 881 | node = node->parent; |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 882 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 883 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 884 | return NULL; |
| 885 | } |
| 886 | |
Radek Krejci | 19cf805 | 2020-08-18 15:02:38 +0200 | [diff] [blame] | 887 | API LY_ERR |
Radek Krejci | af9cd80 | 2020-10-06 21:59:47 +0200 | [diff] [blame] | 888 | lysc_set_private(const struct lysc_node *node, void *priv, void **prev_priv_p) |
Radek Krejci | 19cf805 | 2020-08-18 15:02:38 +0200 | [diff] [blame] | 889 | { |
Radek Krejci | af9cd80 | 2020-10-06 21:59:47 +0200 | [diff] [blame] | 890 | struct lysc_action *act; |
| 891 | struct lysc_notif *notif; |
| 892 | |
Radek Krejci | 19cf805 | 2020-08-18 15:02:38 +0200 | [diff] [blame] | 893 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
| 894 | |
Radek Krejci | af9cd80 | 2020-10-06 21:59:47 +0200 | [diff] [blame] | 895 | switch (node->nodetype) { |
| 896 | case LYS_CONTAINER: |
| 897 | case LYS_CHOICE: |
| 898 | case LYS_CASE: |
| 899 | case LYS_LEAF: |
| 900 | case LYS_LEAFLIST: |
| 901 | case LYS_LIST: |
| 902 | case LYS_ANYXML: |
| 903 | case LYS_ANYDATA: |
| 904 | if (prev_priv_p) { |
| 905 | *prev_priv_p = node->priv; |
| 906 | } |
| 907 | ((struct lysc_node *)node)->priv = priv; |
| 908 | break; |
| 909 | case LYS_RPC: |
| 910 | case LYS_ACTION: |
| 911 | act = (struct lysc_action *)node; |
| 912 | if (prev_priv_p) { |
| 913 | *prev_priv_p = act->priv; |
| 914 | } |
| 915 | act->priv = priv; |
| 916 | break; |
| 917 | case LYS_NOTIF: |
| 918 | notif = (struct lysc_notif *)node; |
| 919 | if (prev_priv_p) { |
| 920 | *prev_priv_p = notif->priv; |
| 921 | } |
| 922 | notif->priv = priv; |
| 923 | break; |
| 924 | default: |
| 925 | return LY_EINVAL; |
Radek Krejci | 19cf805 | 2020-08-18 15:02:38 +0200 | [diff] [blame] | 926 | } |
Radek Krejci | 19cf805 | 2020-08-18 15:02:38 +0200 | [diff] [blame] | 927 | |
| 928 | return LY_SUCCESS; |
| 929 | } |
| 930 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 931 | API LY_ERR |
| 932 | lys_set_implemented(struct lys_module *mod) |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 933 | { |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 934 | LY_ERR ret = LY_SUCCESS, r; |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 935 | struct lys_module *m; |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 936 | uint32_t i, idx; |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 937 | |
| 938 | LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL); |
| 939 | |
| 940 | if (mod->implemented) { |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 941 | /* mod is already implemented */ |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 942 | return LY_SUCCESS; |
| 943 | } |
| 944 | |
| 945 | /* we have module from the current context */ |
| 946 | m = ly_ctx_get_module_implemented(mod->ctx, mod->name); |
| 947 | if (m) { |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 948 | assert(m != mod); |
| 949 | |
| 950 | /* check collision with other implemented revision */ |
| 951 | LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).", |
| 952 | mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none"); |
| 953 | return LY_EDENIED; |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 954 | } |
| 955 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 956 | /* add the module into newly implemented module set */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 957 | LY_CHECK_RET(ly_set_add(&mod->ctx->implementing, mod, 1, NULL)); |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 958 | |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 959 | /* mark the module implemented, check for collision was already done */ |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 960 | mod->implemented = 1; |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 961 | |
| 962 | /* compile the schema */ |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 963 | ret = lys_compile(mod, 0); |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 964 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 965 | if (mod == mod->ctx->implementing.objs[0]) { |
| 966 | /* the first module being implemented, consolidate the set */ |
| 967 | if (ret) { |
| 968 | /* failure, full compile revert */ |
| 969 | for (i = 0; i < mod->ctx->list.count; ++i) { |
| 970 | m = mod->ctx->list.objs[i]; |
| 971 | if (ly_set_contains(&mod->ctx->implementing, m, &idx)) { |
| 972 | assert(m->implemented); |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 973 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 974 | /* make the module correctly non-implemented again */ |
| 975 | m->implemented = 0; |
| 976 | ly_set_rm_index(&mod->ctx->implementing, idx, NULL); |
| 977 | lys_precompile_augments_deviations_revert(mod->ctx, m); |
| 978 | } |
| 979 | |
| 980 | /* free the compiled version of the module, if any */ |
| 981 | lysc_module_free(m->compiled, NULL); |
| 982 | m->compiled = NULL; |
| 983 | |
| 984 | if (m->implemented) { |
| 985 | /* recompile, must succeed because it was already compiled; hide messages because any |
| 986 | * warnings were already printed, are not really relevant, and would hide the real error */ |
| 987 | uint32_t prev_lo = ly_log_options(0); |
| 988 | r = lys_compile(m, 0); |
| 989 | ly_log_options(prev_lo); |
| 990 | if (r) { |
| 991 | LOGERR(mod->ctx, r, "Recompilation of module \"%s\" failed.", m->name); |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | ly_set_erase(&mod->ctx->implementing, NULL); |
| 998 | } |
| 999 | return ret; |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 1000 | } |
| 1001 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1002 | static LY_ERR |
| 1003 | lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *modp) |
| 1004 | { |
| 1005 | struct lysp_import *imp; |
| 1006 | struct lysp_include *inc; |
| 1007 | LY_ARRAY_COUNT_TYPE u, v; |
| 1008 | |
| 1009 | modp->parsing = 1; |
| 1010 | LY_ARRAY_FOR(modp->imports, u) { |
| 1011 | imp = &modp->imports[u]; |
| 1012 | if (!imp->module) { |
| 1013 | LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)); |
| 1014 | } |
| 1015 | /* check for importing the same module twice */ |
| 1016 | for (v = 0; v < u; ++v) { |
| 1017 | if (imp->module == modp->imports[v].module) { |
| 1018 | LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name); |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | LY_ARRAY_FOR(modp->includes, u) { |
| 1023 | inc = &modp->includes[u]; |
| 1024 | if (!inc->submodule) { |
| 1025 | LY_CHECK_RET(lysp_load_submodule(pctx, inc)); |
| 1026 | } |
| 1027 | } |
| 1028 | modp->parsing = 0; |
| 1029 | |
| 1030 | return LY_SUCCESS; |
| 1031 | } |
| 1032 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1033 | LY_ERR |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1034 | lys_parse_submodule(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, struct lys_parser_ctx *main_ctx, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1035 | LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *), |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1036 | void *check_data, struct lysp_submodule **submodule) |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1037 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1038 | LY_ERR ret; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1039 | struct lysp_submodule *submod = NULL, *latest_sp; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1040 | struct lys_yang_parser_ctx *yangctx = NULL; |
| 1041 | struct lys_yin_parser_ctx *yinctx = NULL; |
| 1042 | struct lys_parser_ctx *pctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1043 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1044 | LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1045 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1046 | switch (format) { |
| 1047 | case LYS_IN_YIN: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1048 | ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1049 | pctx = (struct lys_parser_ctx *)yinctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1050 | break; |
| 1051 | case LYS_IN_YANG: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1052 | ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1053 | pctx = (struct lys_parser_ctx *)yangctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1054 | break; |
| 1055 | default: |
David Sedlák | 4f2f5ba | 2019-08-15 13:18:48 +0200 | [diff] [blame] | 1056 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
Radek Krejci | 82fa8d4 | 2020-07-11 22:00:59 +0200 | [diff] [blame] | 1057 | ret = LY_EINVAL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1058 | break; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1059 | } |
Radek Krejci | f6923e8 | 2020-07-02 16:36:53 +0200 | [diff] [blame] | 1060 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | f027df7 | 2020-09-15 13:00:28 +0200 | [diff] [blame] | 1061 | assert(submod); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1062 | |
| 1063 | /* make sure that the newest revision is at position 0 */ |
| 1064 | lysp_sort_revisions(submod->revs); |
| 1065 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1066 | /* decide the latest revision */ |
Michal Vasko | c3781c3 | 2020-10-06 14:04:08 +0200 | [diff] [blame] | 1067 | latest_sp = ly_ctx_get_submodule(NULL, submod->mod, submod->name, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1068 | if (latest_sp) { |
| 1069 | if (submod->revs) { |
| 1070 | if (!latest_sp->revs) { |
| 1071 | /* latest has no revision, so mod is anyway newer */ |
| 1072 | submod->latest_revision = latest_sp->latest_revision; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1073 | /* the latest_sp is zeroed later when the new module is being inserted into the context */ |
| 1074 | } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) { |
| 1075 | submod->latest_revision = latest_sp->latest_revision; |
| 1076 | /* the latest_sp is zeroed later when the new module is being inserted into the context */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1077 | } else { |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1078 | latest_sp = NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1079 | } |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1080 | } else { |
| 1081 | latest_sp = NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1082 | } |
| 1083 | } else { |
| 1084 | submod->latest_revision = 1; |
| 1085 | } |
| 1086 | |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1087 | if (custom_check) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1088 | LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | if (latest_sp) { |
| 1092 | latest_sp->latest_revision = 0; |
| 1093 | } |
| 1094 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1095 | lys_parser_fill_filepath(ctx, in, &submod->filepath); |
| 1096 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1097 | /* resolve imports and includes */ |
| 1098 | LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error); |
| 1099 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1100 | /* remap possibly changed and reallocated typedefs and groupings list back to the main context */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1101 | memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes); |
| 1102 | memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1103 | |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1104 | if (format == LYS_IN_YANG) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1105 | yang_parser_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1106 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1107 | yin_parser_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1108 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1109 | *submodule = submod; |
| 1110 | return LY_SUCCESS; |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1111 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1112 | error: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1113 | lysp_module_free((struct lysp_module *)submod); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1114 | if (format == LYS_IN_YANG) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1115 | yang_parser_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1116 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1117 | yin_parser_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1118 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1119 | return ret; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1120 | } |
| 1121 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1122 | LY_ERR |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1123 | lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool implement, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1124 | LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data), |
| 1125 | void *check_data, struct lys_module **module) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1126 | { |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 1127 | struct lys_module *mod = NULL, *latest, *mod_dup; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1128 | struct lysp_submodule *submod; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1129 | LY_ERR ret; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1130 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1131 | struct lys_yang_parser_ctx *yangctx = NULL; |
| 1132 | struct lys_yin_parser_ctx *yinctx = NULL; |
Radek Krejci | f6923e8 | 2020-07-02 16:36:53 +0200 | [diff] [blame] | 1133 | struct lys_parser_ctx *pctx = NULL; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1134 | char *filename, *rev, *dot; |
| 1135 | size_t len; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1136 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1137 | LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1138 | if (module) { |
| 1139 | *module = NULL; |
| 1140 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1141 | |
| 1142 | mod = calloc(1, sizeof *mod); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1143 | LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1144 | mod->ctx = ctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1145 | |
| 1146 | switch (format) { |
| 1147 | case LYS_IN_YIN: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1148 | ret = yin_parse_module(&yinctx, in, mod); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1149 | pctx = (struct lys_parser_ctx *)yinctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1150 | break; |
| 1151 | case LYS_IN_YANG: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1152 | ret = yang_parse_module(&yangctx, in, mod); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1153 | pctx = (struct lys_parser_ctx *)yangctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1154 | break; |
| 1155 | default: |
| 1156 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1157 | ret = LY_EINVAL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1158 | break; |
| 1159 | } |
Radek Krejci | f6923e8 | 2020-07-02 16:36:53 +0200 | [diff] [blame] | 1160 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1161 | |
| 1162 | /* make sure that the newest revision is at position 0 */ |
| 1163 | lysp_sort_revisions(mod->parsed->revs); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1164 | if (mod->parsed->revs) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1165 | LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), error); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1166 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1167 | |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1168 | /* decide the latest revision */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1169 | latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1170 | if (latest) { |
| 1171 | if (mod->revision) { |
| 1172 | if (!latest->revision) { |
| 1173 | /* latest has no revision, so mod is anyway newer */ |
| 1174 | mod->latest_revision = latest->latest_revision; |
| 1175 | /* the latest is zeroed later when the new module is being inserted into the context */ |
| 1176 | } else if (strcmp(mod->revision, latest->revision) > 0) { |
| 1177 | mod->latest_revision = latest->latest_revision; |
| 1178 | /* the latest is zeroed later when the new module is being inserted into the context */ |
| 1179 | } else { |
| 1180 | latest = NULL; |
| 1181 | } |
| 1182 | } else { |
| 1183 | latest = NULL; |
| 1184 | } |
| 1185 | } else { |
| 1186 | mod->latest_revision = 1; |
| 1187 | } |
| 1188 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1189 | if (custom_check) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1190 | LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), error); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1191 | } |
| 1192 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1193 | if (implement) { |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1194 | /* mark the loaded module implemented */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1195 | if (ly_ctx_get_module_implemented(ctx, mod->name)) { |
| 1196 | LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1197 | ret = LY_EDENIED; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1198 | goto error; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 1199 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1200 | } |
| 1201 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1202 | /* check for duplicity in the context */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1203 | mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1204 | if (mod_dup) { |
| 1205 | if (mod_dup->parsed) { |
| 1206 | /* error */ |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1207 | if (mod->parsed->revs) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1208 | LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1209 | mod->name, mod->parsed->revs[0].date); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1210 | } else { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1211 | LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1212 | mod->name); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1213 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1214 | ret = LY_EEXIST; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1215 | goto error; |
| 1216 | } else { |
| 1217 | /* add the parsed data to the currently compiled-only module in the context */ |
| 1218 | mod_dup->parsed = mod->parsed; |
| 1219 | mod_dup->parsed->mod = mod_dup; |
| 1220 | mod->parsed = NULL; |
| 1221 | lys_module_free(mod, NULL); |
| 1222 | mod = mod_dup; |
| 1223 | goto finish_parsing; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1224 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1225 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1226 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1227 | switch (in->type) { |
| 1228 | case LY_IN_FILEPATH: |
| 1229 | /* check that name and revision match filename */ |
| 1230 | filename = strrchr(in->method.fpath.filepath, '/'); |
| 1231 | if (!filename) { |
| 1232 | filename = in->method.fpath.filepath; |
| 1233 | } else { |
| 1234 | filename++; |
| 1235 | } |
| 1236 | rev = strchr(filename, '@'); |
| 1237 | dot = strrchr(filename, '.'); |
| 1238 | |
| 1239 | /* name */ |
| 1240 | len = strlen(mod->name); |
| 1241 | if (strncmp(filename, mod->name, len) || |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1242 | ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1243 | LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name); |
| 1244 | } |
| 1245 | if (rev) { |
| 1246 | len = dot - ++rev; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1247 | if (!mod->parsed->revs || (len != 10) || strncmp(mod->parsed->revs[0].date, rev, len)) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1248 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1249 | mod->parsed->revs ? mod->parsed->revs[0].date : "none"); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | break; |
| 1254 | case LY_IN_FD: |
| 1255 | case LY_IN_FILE: |
| 1256 | case LY_IN_MEMORY: |
| 1257 | /* nothing special to do */ |
| 1258 | break; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1259 | case LY_IN_ERROR: |
| 1260 | LOGINT(ctx); |
| 1261 | ret = LY_EINT; |
| 1262 | goto error; |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1263 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1264 | |
| 1265 | lys_parser_fill_filepath(ctx, in, &mod->filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1266 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 1267 | if (!implement) { |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1268 | /* pre-compile features and identities of the module */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1269 | LY_CHECK_GOTO(ret = lys_feature_precompile(NULL, ctx, mod->parsed, mod->parsed->features, &mod->features), error); |
| 1270 | LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), error); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | if (latest) { |
| 1274 | latest->latest_revision = 0; |
| 1275 | } |
| 1276 | |
| 1277 | /* add into context */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 1278 | ret = ly_set_add(&ctx->list, mod, 1, NULL); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1279 | LY_CHECK_GOTO(ret, error); |
| 1280 | ctx->module_set_id++; |
| 1281 | |
| 1282 | finish_parsing: |
| 1283 | /* resolve imports and includes */ |
| 1284 | LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), error_ctx); |
| 1285 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 1286 | if (!implement) { |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1287 | /* pre-compile features and identities of any submodules */ |
| 1288 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1289 | submod = mod->parsed->includes[u].submodule; |
| 1290 | ret = lys_feature_precompile(NULL, ctx, (struct lysp_module *)submod, submod->features, &mod->features); |
| 1291 | LY_CHECK_GOTO(ret, error); |
| 1292 | ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities); |
| 1293 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | /* check name collisions - typedefs and TODO groupings */ |
| 1298 | LY_CHECK_GOTO(ret = lysp_check_typedefs(pctx, mod->parsed), error_ctx); |
| 1299 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 1300 | if (implement) { |
| 1301 | /* implement (compile) */ |
| 1302 | LY_CHECK_GOTO(ret = lys_set_implemented(mod), error_ctx); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | if (format == LYS_IN_YANG) { |
| 1306 | yang_parser_ctx_free(yangctx); |
| 1307 | } else { |
| 1308 | yin_parser_ctx_free(yinctx); |
| 1309 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1310 | if (module) { |
| 1311 | *module = mod; |
| 1312 | } |
| 1313 | return LY_SUCCESS; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1314 | |
| 1315 | error_ctx: |
| 1316 | ly_set_rm(&ctx->list, mod, NULL); |
| 1317 | error: |
| 1318 | lys_module_free(mod, NULL); |
| 1319 | if (pctx) { |
| 1320 | ly_set_erase(&pctx->tpdfs_nodes, NULL); |
| 1321 | } |
| 1322 | if (format == LYS_IN_YANG) { |
| 1323 | yang_parser_ctx_free(yangctx); |
| 1324 | } else { |
| 1325 | yin_parser_ctx_free(yinctx); |
| 1326 | } |
| 1327 | |
| 1328 | return ret; |
| 1329 | } |
| 1330 | |
| 1331 | API LY_ERR |
| 1332 | lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const struct lys_module **module) |
| 1333 | { |
| 1334 | if (module) { |
| 1335 | *module = NULL; |
| 1336 | } |
| 1337 | LY_CHECK_ARG_RET(NULL, ctx, in, format > LYS_IN_UNKNOWN, LY_EINVAL); |
| 1338 | |
| 1339 | /* remember input position */ |
| 1340 | in->func_start = in->current; |
| 1341 | |
| 1342 | return lys_create_module(ctx, in, format, 1, NULL, NULL, (struct lys_module **)module); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1343 | } |
| 1344 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1345 | API LY_ERR |
| 1346 | lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1347 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1348 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1349 | struct ly_in *in = NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1350 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1351 | LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL); |
Radek Krejci | 65639b9 | 2018-11-27 10:51:37 +0100 | [diff] [blame] | 1352 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1353 | LY_CHECK_ERR_RET(ret = ly_in_new_memory(data, &in), LOGERR(ctx, ret, "Unable to create input handler."), ret); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1354 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1355 | ret = lys_parse(ctx, in, format, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1356 | ly_in_free(in, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1357 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1358 | return ret; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1359 | } |
| 1360 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1361 | API LY_ERR |
| 1362 | lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1363 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1364 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1365 | struct ly_in *in = NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1366 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1367 | LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1368 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1369 | LY_CHECK_ERR_RET(ret = ly_in_new_fd(fd, &in), LOGERR(ctx, ret, "Unable to create input handler."), ret); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1370 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1371 | ret = lys_parse(ctx, in, format, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1372 | ly_in_free(in, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1373 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1374 | return ret; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1375 | } |
| 1376 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1377 | API LY_ERR |
| 1378 | lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module) |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1379 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1380 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1381 | struct ly_in *in = NULL; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1382 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1383 | LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1384 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1385 | LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1386 | LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1387 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1388 | ret = lys_parse(ctx, in, format, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1389 | ly_in_free(in, 0); |
| 1390 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1391 | return ret; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | API LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1395 | lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1396 | char **localfile, LYS_INFORMAT *format) |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1397 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1398 | LY_ERR ret = LY_EMEM; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1399 | size_t len, flen, match_len = 0, dir_len; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1400 | ly_bool implicit_cwd = 0; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1401 | char *wd, *wn = NULL; |
| 1402 | DIR *dir = NULL; |
| 1403 | struct dirent *file; |
| 1404 | char *match_name = NULL; |
| 1405 | LYS_INFORMAT format_aux, match_format = 0; |
| 1406 | struct ly_set *dirs; |
| 1407 | struct stat st; |
| 1408 | |
| 1409 | LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL); |
| 1410 | |
| 1411 | /* start to fill the dir fifo with the context's search path (if set) |
| 1412 | * and the current working directory */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1413 | LY_CHECK_RET(ly_set_new(&dirs)); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1414 | |
| 1415 | len = strlen(name); |
| 1416 | if (cwd) { |
| 1417 | wd = get_current_dir_name(); |
| 1418 | if (!wd) { |
| 1419 | LOGMEM(NULL); |
| 1420 | goto cleanup; |
| 1421 | } else { |
| 1422 | /* add implicit current working directory (./) to be searched, |
| 1423 | * this directory is not searched recursively */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1424 | ret = ly_set_add(dirs, wd, 0, NULL); |
| 1425 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1426 | implicit_cwd = 1; |
| 1427 | } |
| 1428 | } |
| 1429 | if (searchpaths) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1430 | for (uint64_t i = 0; searchpaths[i]; i++) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1431 | /* check for duplicities with the implicit current working directory */ |
| 1432 | if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) { |
| 1433 | implicit_cwd = 0; |
| 1434 | continue; |
| 1435 | } |
| 1436 | wd = strdup(searchpaths[i]); |
| 1437 | if (!wd) { |
| 1438 | LOGMEM(NULL); |
| 1439 | goto cleanup; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1440 | } else { |
| 1441 | ret = ly_set_add(dirs, wd, 0, NULL); |
| 1442 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1443 | } |
| 1444 | } |
| 1445 | } |
| 1446 | wd = NULL; |
| 1447 | |
| 1448 | /* start searching */ |
| 1449 | while (dirs->count) { |
| 1450 | free(wd); |
| 1451 | free(wn); wn = NULL; |
| 1452 | |
| 1453 | dirs->count--; |
| 1454 | wd = (char *)dirs->objs[dirs->count]; |
| 1455 | dirs->objs[dirs->count] = NULL; |
| 1456 | LOGVRB("Searching for \"%s\" in %s.", name, wd); |
| 1457 | |
| 1458 | if (dir) { |
| 1459 | closedir(dir); |
| 1460 | } |
| 1461 | dir = opendir(wd); |
| 1462 | dir_len = strlen(wd); |
| 1463 | if (!dir) { |
| 1464 | LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno)); |
| 1465 | } else { |
| 1466 | while ((file = readdir(dir))) { |
| 1467 | if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) { |
| 1468 | /* skip . and .. */ |
| 1469 | continue; |
| 1470 | } |
| 1471 | free(wn); |
| 1472 | if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) { |
| 1473 | LOGMEM(NULL); |
| 1474 | goto cleanup; |
| 1475 | } |
| 1476 | if (stat(wn, &st) == -1) { |
| 1477 | LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1478 | file->d_name, wd, strerror(errno)); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1479 | continue; |
| 1480 | } |
| 1481 | if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) { |
| 1482 | /* we have another subdirectory in searchpath to explore, |
| 1483 | * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1484 | ret = ly_set_add(dirs, wn, 0, NULL); |
| 1485 | LY_CHECK_GOTO(ret, cleanup); |
| 1486 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1487 | /* continue with the next item in current directory */ |
| 1488 | wn = NULL; |
| 1489 | continue; |
| 1490 | } else if (!S_ISREG(st.st_mode)) { |
| 1491 | /* not a regular file (note that we see the target of symlinks instead of symlinks */ |
| 1492 | continue; |
| 1493 | } |
| 1494 | |
| 1495 | /* here we know that the item is a file which can contain a module */ |
| 1496 | if (strncmp(name, file->d_name, len) || |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1497 | ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1498 | /* different filename than the module we search for */ |
| 1499 | continue; |
| 1500 | } |
| 1501 | |
| 1502 | /* get type according to filename suffix */ |
| 1503 | flen = strlen(file->d_name); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1504 | if (!strcmp(&file->d_name[flen - 5], ".yang")) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1505 | format_aux = LYS_IN_YANG; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1506 | /* TODO YIN parser |
| 1507 | } else if (!strcmp(&file->d_name[flen - 4], ".yin")) { |
| 1508 | format_aux = LYS_IN_YIN; |
| 1509 | */ |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1510 | } else { |
| 1511 | /* not supportde suffix/file format */ |
| 1512 | continue; |
| 1513 | } |
| 1514 | |
| 1515 | if (revision) { |
| 1516 | /* we look for the specific revision, try to get it from the filename */ |
| 1517 | if (file->d_name[len] == '@') { |
| 1518 | /* check revision from the filename */ |
| 1519 | if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) { |
| 1520 | /* another revision */ |
| 1521 | continue; |
| 1522 | } else { |
| 1523 | /* exact revision */ |
| 1524 | free(match_name); |
| 1525 | match_name = wn; |
| 1526 | wn = NULL; |
| 1527 | match_len = dir_len + 1 + len; |
| 1528 | match_format = format_aux; |
| 1529 | goto success; |
| 1530 | } |
| 1531 | } else { |
| 1532 | /* continue trying to find exact revision match, use this only if not found */ |
| 1533 | free(match_name); |
| 1534 | match_name = wn; |
| 1535 | wn = NULL; |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 1536 | match_len = dir_len + 1 + len; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1537 | match_format = format_aux; |
| 1538 | continue; |
| 1539 | } |
| 1540 | } else { |
| 1541 | /* remember the revision and try to find the newest one */ |
| 1542 | if (match_name) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1543 | if ((file->d_name[len] != '@') || |
| 1544 | lysp_check_date(NULL, &file->d_name[len + 1], flen - ((format_aux == LYS_IN_YANG) ? 5 : 4) - len - 1, NULL)) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1545 | continue; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1546 | } else if ((match_name[match_len] == '@') && |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1547 | (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) { |
| 1548 | continue; |
| 1549 | } |
| 1550 | free(match_name); |
| 1551 | } |
| 1552 | |
| 1553 | match_name = wn; |
| 1554 | wn = NULL; |
| 1555 | match_len = dir_len + 1 + len; |
| 1556 | match_format = format_aux; |
| 1557 | continue; |
| 1558 | } |
| 1559 | } |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | success: |
| 1564 | (*localfile) = match_name; |
| 1565 | match_name = NULL; |
| 1566 | if (format) { |
| 1567 | (*format) = match_format; |
| 1568 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1569 | ret = LY_SUCCESS; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1570 | |
| 1571 | cleanup: |
| 1572 | free(wn); |
| 1573 | free(wd); |
| 1574 | if (dir) { |
| 1575 | closedir(dir); |
| 1576 | } |
| 1577 | free(match_name); |
| 1578 | ly_set_free(dirs, free); |
| 1579 | |
| 1580 | return ret; |
| 1581 | } |