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