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) { |
| 288 | nodetype = 0xffff; |
| 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) { |
| 384 | if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == 1)) { |
| 385 | ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL); |
| 386 | LY_CHECK_GOTO(ret, cleanup); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | cleanup: |
| 391 | lyxp_set_free_content(&xp_set); |
| 392 | if (ret) { |
| 393 | ly_set_free(*set, NULL); |
| 394 | *set = NULL; |
| 395 | } |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 400 | 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] | 401 | { |
| 402 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 403 | struct lyxp_set xp_set = {0}; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 404 | struct lyxp_expr *exp = NULL; |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 405 | uint32_t i; |
| 406 | |
| 407 | LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL); |
| 408 | if (!(options & LYXP_SCNODE_ALL)) { |
| 409 | options = LYXP_SCNODE; |
| 410 | } |
| 411 | |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 412 | /* compile expression */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 413 | ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp); |
| 414 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 415 | |
| 416 | /* atomize expression */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 417 | 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] | 418 | LY_CHECK_GOTO(ret, cleanup); |
| 419 | |
| 420 | /* allocate return set */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 421 | ret = ly_set_new(set); |
| 422 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 423 | |
| 424 | /* transform into ly_set */ |
| 425 | (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs); |
| 426 | LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup); |
| 427 | (*set)->size = xp_set.used; |
| 428 | |
| 429 | for (i = 0; i < xp_set.used; ++i) { |
| 430 | if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == 1)) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 431 | 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] | 432 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
| 436 | cleanup: |
| 437 | lyxp_set_free_content(&xp_set); |
| 438 | lyxp_expr_free(ctx_node->module->ctx, exp); |
Michal Vasko | ae15966 | 2020-10-21 11:57:24 +0200 | [diff] [blame] | 439 | if (ret) { |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 440 | ly_set_free(*set, NULL); |
| 441 | *set = NULL; |
| 442 | } |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 443 | return ret; |
| 444 | } |
| 445 | |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 446 | API LY_ERR |
| 447 | lys_find_lypath_atoms(const struct ly_path *path, struct ly_set **set) |
| 448 | { |
| 449 | LY_ERR ret = LY_SUCCESS; |
| 450 | LY_ARRAY_COUNT_TYPE u, v; |
| 451 | |
| 452 | LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL); |
| 453 | |
| 454 | /* allocate return set */ |
| 455 | LY_CHECK_RET(ly_set_new(set)); |
| 456 | |
| 457 | LY_ARRAY_FOR(path, u) { |
| 458 | /* add nodes from the path */ |
| 459 | LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].node, 0, NULL), cleanup); |
| 460 | if (path[u].pred_type == LY_PATH_PREDTYPE_LIST) { |
| 461 | LY_ARRAY_FOR(path[u].predicates, v) { |
| 462 | /* add all the keys in a predicate */ |
| 463 | LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup); |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | cleanup: |
| 469 | if (ret) { |
| 470 | ly_set_free(*set, NULL); |
| 471 | *set = NULL; |
| 472 | } |
| 473 | return ret; |
| 474 | } |
| 475 | |
| 476 | API LY_ERR |
| 477 | lys_find_path_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output, |
| 478 | struct ly_set **set) |
| 479 | { |
| 480 | LY_ERR ret = LY_SUCCESS; |
| 481 | uint8_t oper; |
| 482 | struct lyxp_expr *expr = NULL; |
| 483 | struct ly_path *p = NULL; |
| 484 | |
| 485 | LY_CHECK_ARG_RET(ctx, ctx || ctx_node, path, set, LY_EINVAL); |
| 486 | |
| 487 | if (!ctx) { |
| 488 | ctx = ctx_node->module->ctx; |
| 489 | } |
| 490 | |
| 491 | /* parse */ |
| 492 | ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &expr); |
| 493 | LY_CHECK_GOTO(ret, cleanup); |
| 494 | |
| 495 | /* compile */ |
| 496 | oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT; |
| 497 | 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^] | 498 | LY_PREF_JSON, NULL, NULL, &p); |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 499 | LY_CHECK_GOTO(ret, cleanup); |
| 500 | |
| 501 | /* resolve */ |
| 502 | ret = lys_find_lypath_atoms(p, set); |
| 503 | |
| 504 | cleanup: |
| 505 | ly_path_free(ctx, p); |
| 506 | lyxp_expr_free(ctx, expr); |
| 507 | return ret; |
| 508 | } |
| 509 | |
| 510 | API const struct lysc_node * |
| 511 | lys_find_path(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output) |
| 512 | { |
| 513 | const struct lysc_node *snode = NULL; |
| 514 | struct lyxp_expr *exp = NULL; |
| 515 | struct ly_path *p = NULL; |
| 516 | LY_ERR ret; |
| 517 | uint8_t oper; |
| 518 | |
| 519 | LY_CHECK_ARG_RET(ctx, ctx || ctx_node, NULL); |
| 520 | |
| 521 | if (!ctx) { |
| 522 | ctx = ctx_node->module->ctx; |
| 523 | } |
| 524 | |
| 525 | /* parse */ |
| 526 | ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &exp); |
| 527 | LY_CHECK_GOTO(ret, cleanup); |
| 528 | |
| 529 | /* compile */ |
| 530 | oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT; |
| 531 | 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^] | 532 | LY_PREF_JSON, NULL, NULL, &p); |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 533 | LY_CHECK_GOTO(ret, cleanup); |
| 534 | |
| 535 | /* get last node */ |
| 536 | snode = p[LY_ARRAY_COUNT(p) - 1].node; |
| 537 | |
| 538 | cleanup: |
| 539 | ly_path_free(ctx, p); |
| 540 | lyxp_expr_free(ctx, exp); |
| 541 | return snode; |
| 542 | } |
| 543 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 544 | char * |
| 545 | 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] | 546 | size_t buflen) |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 547 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 548 | const struct lysc_node *iter; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 549 | char *path = NULL; |
| 550 | int len = 0; |
| 551 | |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 552 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 553 | if (buffer) { |
| 554 | LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL); |
| 555 | } |
| 556 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 557 | switch (pathtype) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 558 | case LYSC_PATH_LOG: |
Michal Vasko | 65de040 | 2020-08-03 16:34:19 +0200 | [diff] [blame] | 559 | case LYSC_PATH_DATA: |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 560 | for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) { |
Michal Vasko | 11deea1 | 2020-08-05 13:54:50 +0200 | [diff] [blame] | 561 | char *s, *id; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 562 | const char *slash; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 563 | |
Michal Vasko | 65de040 | 2020-08-03 16:34:19 +0200 | [diff] [blame] | 564 | if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 565 | /* schema-only node */ |
| 566 | continue; |
| 567 | } |
| 568 | |
Michal Vasko | 11deea1 | 2020-08-05 13:54:50 +0200 | [diff] [blame] | 569 | s = buffer ? strdup(buffer) : path; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 570 | id = strdup(iter->name); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 571 | if (parent && (iter->parent == parent)) { |
| 572 | slash = ""; |
| 573 | } else { |
| 574 | slash = "/"; |
| 575 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 576 | if (!iter->parent || (iter->parent->module != iter->module)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 577 | /* print prefix */ |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 578 | if (buffer) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 579 | 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] | 580 | } else { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 581 | 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] | 582 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 583 | } else { |
| 584 | /* prefix is the same as in parent */ |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 585 | if (buffer) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 586 | len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : ""); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 587 | } else { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 588 | len = asprintf(&path, "%s%s%s", slash, id, s ? s : ""); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 589 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 590 | } |
| 591 | free(s); |
| 592 | free(id); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 593 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 594 | if (buffer && (buflen <= (size_t)len)) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 595 | /* not enough space in buffer */ |
| 596 | break; |
| 597 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | if (len < 0) { |
| 601 | free(path); |
| 602 | path = NULL; |
| 603 | } else if (len == 0) { |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 604 | if (buffer) { |
| 605 | strcpy(buffer, "/"); |
| 606 | } else { |
| 607 | path = strdup("/"); |
| 608 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 609 | } |
| 610 | break; |
| 611 | } |
| 612 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 613 | if (buffer) { |
| 614 | return buffer; |
| 615 | } else { |
| 616 | return path; |
| 617 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 620 | API char * |
| 621 | lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen) |
| 622 | { |
| 623 | return lysc_path_until(node, NULL, pathtype, buffer, buflen); |
| 624 | } |
| 625 | |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 626 | API LY_ERR |
Radek Krejci | af9cd80 | 2020-10-06 21:59:47 +0200 | [diff] [blame] | 627 | 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] | 628 | { |
Radek Krejci | af9cd80 | 2020-10-06 21:59:47 +0200 | [diff] [blame] | 629 | struct lysc_action *act; |
| 630 | struct lysc_notif *notif; |
| 631 | |
Radek Krejci | 19cf805 | 2020-08-18 15:02:38 +0200 | [diff] [blame] | 632 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
| 633 | |
Radek Krejci | af9cd80 | 2020-10-06 21:59:47 +0200 | [diff] [blame] | 634 | switch (node->nodetype) { |
| 635 | case LYS_CONTAINER: |
| 636 | case LYS_CHOICE: |
| 637 | case LYS_CASE: |
| 638 | case LYS_LEAF: |
| 639 | case LYS_LEAFLIST: |
| 640 | case LYS_LIST: |
| 641 | case LYS_ANYXML: |
| 642 | case LYS_ANYDATA: |
| 643 | if (prev_priv_p) { |
| 644 | *prev_priv_p = node->priv; |
| 645 | } |
| 646 | ((struct lysc_node *)node)->priv = priv; |
| 647 | break; |
| 648 | case LYS_RPC: |
| 649 | case LYS_ACTION: |
| 650 | act = (struct lysc_action *)node; |
| 651 | if (prev_priv_p) { |
| 652 | *prev_priv_p = act->priv; |
| 653 | } |
| 654 | act->priv = priv; |
| 655 | break; |
| 656 | case LYS_NOTIF: |
| 657 | notif = (struct lysc_notif *)node; |
| 658 | if (prev_priv_p) { |
| 659 | *prev_priv_p = notif->priv; |
| 660 | } |
| 661 | notif->priv = priv; |
| 662 | break; |
| 663 | default: |
| 664 | return LY_EINVAL; |
Radek Krejci | 19cf805 | 2020-08-18 15:02:38 +0200 | [diff] [blame] | 665 | } |
Radek Krejci | 19cf805 | 2020-08-18 15:02:38 +0200 | [diff] [blame] | 666 | |
| 667 | return LY_SUCCESS; |
| 668 | } |
| 669 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 670 | LY_ERR |
| 671 | 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] | 672 | { |
| 673 | struct lys_module *m; |
| 674 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 675 | assert(!mod->implemented); |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 676 | |
| 677 | /* we have module from the current context */ |
| 678 | m = ly_ctx_get_module_implemented(mod->ctx, mod->name); |
| 679 | if (m) { |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 680 | assert(m != mod); |
| 681 | |
| 682 | /* check collision with other implemented revision */ |
| 683 | LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).", |
| 684 | mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none"); |
| 685 | return LY_EDENIED; |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 686 | } |
| 687 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 688 | /* enable features */ |
| 689 | LY_CHECK_RET(lys_enable_features(mod->parsed, features)); |
| 690 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 691 | /* add the module into newly implemented module set */ |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 692 | LY_CHECK_RET(ly_set_add(&unres->implementing, mod, 1, NULL)); |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 693 | |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 694 | /* mark the module implemented, check for collision was already done */ |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 695 | mod->implemented = 1; |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 696 | |
| 697 | /* compile the schema */ |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 698 | return lys_compile(mod, 0, unres); |
| 699 | } |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 700 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 701 | API LY_ERR |
| 702 | lys_set_implemented(struct lys_module *mod, const char **features) |
| 703 | { |
| 704 | LY_ERR ret = LY_SUCCESS, r; |
| 705 | struct lys_glob_unres unres = {0}; |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 706 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 707 | LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL); |
Michal Vasko | 916aefb | 2020-11-02 15:43:16 +0100 | [diff] [blame] | 708 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 709 | if (mod->implemented) { |
| 710 | /* mod is already implemented, set the features */ |
| 711 | r = lys_set_features(mod->parsed, features); |
| 712 | if (r == LY_EEXIST) { |
| 713 | /* no changes */ |
| 714 | return LY_SUCCESS; |
| 715 | } else if (r) { |
| 716 | /* error */ |
| 717 | return r; |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 718 | } |
| 719 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 720 | /* full recompilation */ |
| 721 | return lys_recompile(mod->ctx, 1); |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 722 | } |
Michal Vasko | 08c8b27 | 2020-11-24 18:11:30 +0100 | [diff] [blame] | 723 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 724 | /* implement this module and any other required modules, recursively */ |
| 725 | ret = lys_set_implemented_r(mod, features, &unres); |
| 726 | assert(mod == unres.implementing.objs[0]); |
| 727 | |
| 728 | /* the first module being implemented is finished, resolve global unres, consolidate the set */ |
| 729 | if (!ret) { |
| 730 | ret = lys_compile_unres_glob(mod->ctx, &unres); |
| 731 | } |
| 732 | if (ret) { |
| 733 | /* failure, full compile revert */ |
| 734 | lys_compile_unres_glob_revert(mod->ctx, &unres); |
| 735 | } |
| 736 | |
| 737 | lys_compile_unres_glob_erase(mod->ctx, &unres); |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 738 | return ret; |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 739 | } |
| 740 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 741 | static LY_ERR |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 742 | 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] | 743 | { |
| 744 | struct lysp_import *imp; |
| 745 | struct lysp_include *inc; |
| 746 | LY_ARRAY_COUNT_TYPE u, v; |
| 747 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 748 | pmod->parsing = 1; |
| 749 | LY_ARRAY_FOR(pmod->imports, u) { |
| 750 | imp = &pmod->imports[u]; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 751 | if (!imp->module) { |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 752 | LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, NULL, |
| 753 | pctx->unres, &imp->module)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 754 | } |
| 755 | /* check for importing the same module twice */ |
| 756 | for (v = 0; v < u; ++v) { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 757 | if (imp->module == pmod->imports[v].module) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 758 | LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name); |
| 759 | } |
| 760 | } |
| 761 | } |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 762 | LY_ARRAY_FOR(pmod->includes, u) { |
| 763 | inc = &pmod->includes[u]; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 764 | if (!inc->submodule) { |
| 765 | LY_CHECK_RET(lysp_load_submodule(pctx, inc)); |
| 766 | } |
| 767 | } |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 768 | pmod->parsing = 0; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 769 | |
| 770 | return LY_SUCCESS; |
| 771 | } |
| 772 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 773 | LY_ERR |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 774 | 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] | 775 | 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] | 776 | void *check_data, struct lysp_submodule **submodule) |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 777 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 778 | LY_ERR ret; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 779 | struct lysp_submodule *submod = NULL, *latest_sp; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 780 | struct lys_yang_parser_ctx *yangctx = NULL; |
| 781 | struct lys_yin_parser_ctx *yinctx = NULL; |
| 782 | struct lys_parser_ctx *pctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 783 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 784 | LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 785 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 786 | switch (format) { |
| 787 | case LYS_IN_YIN: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 788 | ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 789 | pctx = (struct lys_parser_ctx *)yinctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 790 | break; |
| 791 | case LYS_IN_YANG: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 792 | ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 793 | pctx = (struct lys_parser_ctx *)yangctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 794 | break; |
| 795 | default: |
David Sedlák | 4f2f5ba | 2019-08-15 13:18:48 +0200 | [diff] [blame] | 796 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
Radek Krejci | 82fa8d4 | 2020-07-11 22:00:59 +0200 | [diff] [blame] | 797 | ret = LY_EINVAL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 798 | break; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 799 | } |
Radek Krejci | f6923e8 | 2020-07-02 16:36:53 +0200 | [diff] [blame] | 800 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | f027df7 | 2020-09-15 13:00:28 +0200 | [diff] [blame] | 801 | assert(submod); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 802 | |
| 803 | /* make sure that the newest revision is at position 0 */ |
| 804 | lysp_sort_revisions(submod->revs); |
| 805 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 806 | /* decide the latest revision */ |
Michal Vasko | c3781c3 | 2020-10-06 14:04:08 +0200 | [diff] [blame] | 807 | latest_sp = ly_ctx_get_submodule(NULL, submod->mod, submod->name, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 808 | if (latest_sp) { |
| 809 | if (submod->revs) { |
| 810 | if (!latest_sp->revs) { |
| 811 | /* latest has no revision, so mod is anyway newer */ |
| 812 | submod->latest_revision = latest_sp->latest_revision; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 813 | /* the latest_sp is zeroed later when the new module is being inserted into the context */ |
| 814 | } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) { |
| 815 | submod->latest_revision = latest_sp->latest_revision; |
| 816 | /* 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] | 817 | } else { |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 818 | latest_sp = NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 819 | } |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 820 | } else { |
| 821 | latest_sp = NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 822 | } |
| 823 | } else { |
| 824 | submod->latest_revision = 1; |
| 825 | } |
| 826 | |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 827 | if (custom_check) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 828 | LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | if (latest_sp) { |
| 832 | latest_sp->latest_revision = 0; |
| 833 | } |
| 834 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 835 | lys_parser_fill_filepath(ctx, in, &submod->filepath); |
| 836 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 837 | /* resolve imports and includes */ |
| 838 | LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error); |
| 839 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 840 | /* 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] | 841 | memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes); |
| 842 | 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] | 843 | |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 844 | if (format == LYS_IN_YANG) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 845 | yang_parser_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 846 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 847 | yin_parser_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 848 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 849 | *submodule = submod; |
| 850 | return LY_SUCCESS; |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 851 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 852 | error: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 853 | lysp_module_free((struct lysp_module *)submod); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 854 | if (format == LYS_IN_YANG) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 855 | yang_parser_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 856 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 857 | yin_parser_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 858 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 859 | return ret; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 860 | } |
| 861 | |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 862 | /** |
| 863 | * @brief Add ietf-netconf metadata to the parsed module. Operation, filter, and select are added. |
| 864 | * |
| 865 | * @param[in] mod Parsed module to add to. |
| 866 | * @return LY_SUCCESS on success. |
| 867 | * @return LY_ERR on error. |
| 868 | */ |
| 869 | static LY_ERR |
| 870 | lys_parsed_add_internal_ietf_netconf(struct lysp_module *mod) |
| 871 | { |
| 872 | struct lysp_ext_instance *ext_p; |
| 873 | struct lysp_stmt *stmt; |
| 874 | struct lysp_import *imp; |
| 875 | |
| 876 | /* |
| 877 | * 1) edit-config's operation |
| 878 | */ |
| 879 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM); |
| 880 | LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 881 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name)); |
| 882 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "operation", 0, &ext_p->argument)); |
| 883 | ext_p->flags = LYS_INTERNAL; |
| 884 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 885 | ext_p->insubstmt_index = 0; |
| 886 | |
| 887 | ext_p->child = stmt = calloc(1, sizeof *ext_p->child); |
| 888 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 889 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt)); |
| 890 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg)); |
| 891 | stmt->kw = LY_STMT_TYPE; |
| 892 | |
| 893 | stmt->child = calloc(1, sizeof *stmt->child); |
| 894 | stmt = stmt->child; |
| 895 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 896 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 897 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "merge", 0, &stmt->arg)); |
| 898 | stmt->kw = LY_STMT_ENUM; |
| 899 | |
| 900 | stmt->next = calloc(1, sizeof *stmt->child); |
| 901 | stmt = stmt->next; |
| 902 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 903 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 904 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "replace", 0, &stmt->arg)); |
| 905 | stmt->kw = LY_STMT_ENUM; |
| 906 | |
| 907 | stmt->next = calloc(1, sizeof *stmt->child); |
| 908 | stmt = stmt->next; |
| 909 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 910 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 911 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "create", 0, &stmt->arg)); |
| 912 | stmt->kw = LY_STMT_ENUM; |
| 913 | |
| 914 | stmt->next = calloc(1, sizeof *stmt->child); |
| 915 | stmt = stmt->next; |
| 916 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 917 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 918 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "delete", 0, &stmt->arg)); |
| 919 | stmt->kw = LY_STMT_ENUM; |
| 920 | |
| 921 | stmt->next = calloc(1, sizeof *stmt->child); |
| 922 | stmt = stmt->next; |
| 923 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 924 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 925 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "remove", 0, &stmt->arg)); |
| 926 | stmt->kw = LY_STMT_ENUM; |
| 927 | |
| 928 | /* |
| 929 | * 2) filter's type |
| 930 | */ |
| 931 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM); |
| 932 | LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 933 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name)); |
| 934 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &ext_p->argument)); |
| 935 | ext_p->flags = LYS_INTERNAL; |
| 936 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 937 | ext_p->insubstmt_index = 0; |
| 938 | |
| 939 | ext_p->child = stmt = calloc(1, sizeof *ext_p->child); |
| 940 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 941 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt)); |
| 942 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg)); |
| 943 | stmt->kw = LY_STMT_TYPE; |
| 944 | |
| 945 | stmt->child = calloc(1, sizeof *stmt->child); |
| 946 | stmt = stmt->child; |
| 947 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 948 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 949 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "subtree", 0, &stmt->arg)); |
| 950 | stmt->kw = LY_STMT_ENUM; |
| 951 | |
| 952 | stmt->next = calloc(1, sizeof *stmt->child); |
| 953 | stmt = stmt->next; |
| 954 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 955 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 956 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg)); |
| 957 | stmt->kw = LY_STMT_ENUM; |
| 958 | |
| 959 | /* if-feature for enum allowed only for YANG 1.1 modules */ |
| 960 | if (mod->version >= LYS_VERSION_1_1) { |
| 961 | stmt->child = calloc(1, sizeof *stmt->child); |
| 962 | stmt = stmt->child; |
| 963 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 964 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "if-feature", 0, &stmt->stmt)); |
| 965 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg)); |
| 966 | stmt->kw = LY_STMT_IF_FEATURE; |
| 967 | } |
| 968 | |
| 969 | /* |
| 970 | * 3) filter's select |
| 971 | */ |
| 972 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM); |
| 973 | LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 974 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name)); |
| 975 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "select", 0, &ext_p->argument)); |
| 976 | ext_p->flags = LYS_INTERNAL; |
| 977 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 978 | ext_p->insubstmt_index = 0; |
| 979 | |
| 980 | ext_p->child = stmt = calloc(1, sizeof *ext_p->child); |
| 981 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 982 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt)); |
| 983 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_:xpath1.0", 0, &stmt->arg)); |
| 984 | stmt->kw = LY_STMT_TYPE; |
| 985 | |
| 986 | /* create new imports for the used prefixes */ |
| 987 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM); |
| 988 | |
| 989 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name)); |
| 990 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix)); |
| 991 | imp->flags = LYS_INTERNAL; |
| 992 | |
| 993 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM); |
| 994 | |
| 995 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-types", 0, &imp->name)); |
| 996 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_", 0, &imp->prefix)); |
| 997 | imp->flags = LYS_INTERNAL; |
| 998 | |
| 999 | return LY_SUCCESS; |
| 1000 | } |
| 1001 | |
| 1002 | /** |
| 1003 | * @brief Add ietf-netconf-with-defaults "default" metadata to the parsed module. |
| 1004 | * |
| 1005 | * @param[in] mod Parsed module to add to. |
| 1006 | * @return LY_SUCCESS on success. |
| 1007 | * @return LY_ERR on error. |
| 1008 | */ |
| 1009 | static LY_ERR |
| 1010 | lys_parsed_add_internal_ietf_netconf_with_defaults(struct lysp_module *mod) |
| 1011 | { |
| 1012 | struct lysp_ext_instance *ext_p; |
| 1013 | struct lysp_stmt *stmt; |
| 1014 | struct lysp_import *imp; |
| 1015 | |
| 1016 | /* add new extension instance */ |
| 1017 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM); |
| 1018 | |
| 1019 | /* fill in the extension instance fields */ |
| 1020 | LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1021 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name)); |
| 1022 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "default", 0, &ext_p->argument)); |
| 1023 | ext_p->flags = LYS_INTERNAL; |
| 1024 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 1025 | ext_p->insubstmt_index = 0; |
| 1026 | |
| 1027 | ext_p->child = stmt = calloc(1, sizeof *ext_p->child); |
| 1028 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1029 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt)); |
| 1030 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "boolean", 0, &stmt->arg)); |
| 1031 | stmt->kw = LY_STMT_TYPE; |
| 1032 | |
| 1033 | /* create new import for the used prefix */ |
| 1034 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM); |
| 1035 | |
| 1036 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name)); |
| 1037 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix)); |
| 1038 | imp->flags = LYS_INTERNAL; |
| 1039 | |
| 1040 | return LY_SUCCESS; |
| 1041 | } |
| 1042 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1043 | LY_ERR |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1044 | 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] | 1045 | 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^] | 1046 | 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] | 1047 | { |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 1048 | struct lys_module *mod = NULL, *latest, *mod_dup; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1049 | struct lysp_submodule *submod; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1050 | LY_ERR ret; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1051 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1052 | struct lys_yang_parser_ctx *yangctx = NULL; |
| 1053 | struct lys_yin_parser_ctx *yinctx = NULL; |
Radek Krejci | f6923e8 | 2020-07-02 16:36:53 +0200 | [diff] [blame] | 1054 | struct lys_parser_ctx *pctx = NULL; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1055 | char *filename, *rev, *dot; |
| 1056 | size_t len; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1057 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 1058 | assert(ctx && in && (!features || implement) && unres); |
| 1059 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1060 | if (module) { |
| 1061 | *module = NULL; |
| 1062 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1063 | |
| 1064 | mod = calloc(1, sizeof *mod); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1065 | LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1066 | mod->ctx = ctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1067 | |
| 1068 | switch (format) { |
| 1069 | case LYS_IN_YIN: |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 1070 | ret = yin_parse_module(&yinctx, in, mod, unres); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1071 | pctx = (struct lys_parser_ctx *)yinctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1072 | break; |
| 1073 | case LYS_IN_YANG: |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 1074 | ret = yang_parse_module(&yangctx, in, mod, unres); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1075 | pctx = (struct lys_parser_ctx *)yangctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1076 | break; |
| 1077 | default: |
| 1078 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1079 | ret = LY_EINVAL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1080 | break; |
| 1081 | } |
Radek Krejci | f6923e8 | 2020-07-02 16:36:53 +0200 | [diff] [blame] | 1082 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1083 | |
| 1084 | /* make sure that the newest revision is at position 0 */ |
| 1085 | lysp_sort_revisions(mod->parsed->revs); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1086 | if (mod->parsed->revs) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1087 | LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), error); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1088 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1089 | |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1090 | /* decide the latest revision */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1091 | latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1092 | if (latest) { |
| 1093 | if (mod->revision) { |
| 1094 | if (!latest->revision) { |
| 1095 | /* latest has no revision, so mod is anyway newer */ |
| 1096 | mod->latest_revision = latest->latest_revision; |
| 1097 | /* the latest is zeroed later when the new module is being inserted into the context */ |
| 1098 | } else if (strcmp(mod->revision, latest->revision) > 0) { |
| 1099 | mod->latest_revision = latest->latest_revision; |
| 1100 | /* the latest is zeroed later when the new module is being inserted into the context */ |
| 1101 | } else { |
| 1102 | latest = NULL; |
| 1103 | } |
| 1104 | } else { |
| 1105 | latest = NULL; |
| 1106 | } |
| 1107 | } else { |
| 1108 | mod->latest_revision = 1; |
| 1109 | } |
| 1110 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1111 | if (custom_check) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1112 | LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), error); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1113 | } |
| 1114 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1115 | /* check for duplicity in the context */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1116 | if (implement && ly_ctx_get_module_implemented(ctx, mod->name)) { |
| 1117 | LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name); |
| 1118 | ret = LY_EDENIED; |
| 1119 | goto error; |
| 1120 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1121 | mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1122 | if (mod_dup) { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1123 | if (mod->parsed->revs) { |
| 1124 | LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.", |
| 1125 | mod->name, mod->parsed->revs[0].date); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1126 | } else { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1127 | LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.", |
| 1128 | mod->name); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1129 | } |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1130 | ret = LY_EEXIST; |
| 1131 | goto error; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1132 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1133 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1134 | switch (in->type) { |
| 1135 | case LY_IN_FILEPATH: |
| 1136 | /* check that name and revision match filename */ |
| 1137 | filename = strrchr(in->method.fpath.filepath, '/'); |
| 1138 | if (!filename) { |
| 1139 | filename = in->method.fpath.filepath; |
| 1140 | } else { |
| 1141 | filename++; |
| 1142 | } |
| 1143 | rev = strchr(filename, '@'); |
| 1144 | dot = strrchr(filename, '.'); |
| 1145 | |
| 1146 | /* name */ |
| 1147 | len = strlen(mod->name); |
| 1148 | if (strncmp(filename, mod->name, len) || |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1149 | ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1150 | LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name); |
| 1151 | } |
| 1152 | if (rev) { |
| 1153 | len = dot - ++rev; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1154 | if (!mod->parsed->revs || (len != 10) || strncmp(mod->parsed->revs[0].date, rev, len)) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1155 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1156 | mod->parsed->revs ? mod->parsed->revs[0].date : "none"); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | break; |
| 1161 | case LY_IN_FD: |
| 1162 | case LY_IN_FILE: |
| 1163 | case LY_IN_MEMORY: |
| 1164 | /* nothing special to do */ |
| 1165 | break; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1166 | case LY_IN_ERROR: |
| 1167 | LOGINT(ctx); |
| 1168 | ret = LY_EINT; |
| 1169 | goto error; |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1170 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1171 | lys_parser_fill_filepath(ctx, in, &mod->filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1172 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1173 | if (latest) { |
| 1174 | latest->latest_revision = 0; |
| 1175 | } |
| 1176 | |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1177 | /* add internal data in case specific modules were parsed */ |
| 1178 | if (!strcmp(mod->name, "ietf-netconf")) { |
| 1179 | LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf(mod->parsed), error); |
| 1180 | } else if (!strcmp(mod->name, "ietf-netconf-with-defaults")) { |
| 1181 | LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf_with_defaults(mod->parsed), error); |
| 1182 | } |
| 1183 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 1184 | /* add the module into newly created module set, will also be freed from there on any error */ |
| 1185 | LY_CHECK_GOTO(ret = ly_set_add(&unres->creating, mod, 1, NULL), error); |
| 1186 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1187 | /* add into context */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 1188 | ret = ly_set_add(&ctx->list, mod, 1, NULL); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 1189 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1190 | ctx->module_set_id++; |
| 1191 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1192 | /* resolve includes and all imports */ |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 1193 | LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), cleanup); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1194 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1195 | /* check name collisions */ |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 1196 | LY_CHECK_GOTO(ret = lysp_check_dup_typedefs(pctx, mod->parsed), cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1197 | /* TODO groupings */ |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 1198 | LY_CHECK_GOTO(ret = lysp_check_dup_features(pctx, mod->parsed), cleanup); |
| 1199 | LY_CHECK_GOTO(ret = lysp_check_dup_identities(pctx, mod->parsed), cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1200 | |
| 1201 | /* compile features */ |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 1202 | LY_CHECK_GOTO(ret = lys_compile_feature_iffeatures(mod->parsed), cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1203 | |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 1204 | if (!implement) { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1205 | /* pre-compile identities of the module */ |
| 1206 | LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), error); |
| 1207 | |
| 1208 | /* pre-compile identities of any submodules */ |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1209 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1210 | submod = mod->parsed->includes[u].submodule; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1211 | ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities); |
| 1212 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1213 | } |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1214 | } else { |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 1215 | /* implement (compile) */ |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 1216 | LY_CHECK_GOTO(ret = lys_set_implemented_r(mod, features, unres), cleanup); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1217 | } |
| 1218 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 1219 | /* success */ |
| 1220 | goto cleanup; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1221 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1222 | error: |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 1223 | assert(ret); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1224 | lys_module_free(mod, NULL); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 1225 | cleanup: |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1226 | if (pctx) { |
| 1227 | ly_set_erase(&pctx->tpdfs_nodes, NULL); |
| 1228 | } |
| 1229 | if (format == LYS_IN_YANG) { |
| 1230 | yang_parser_ctx_free(yangctx); |
| 1231 | } else { |
| 1232 | yin_parser_ctx_free(yinctx); |
| 1233 | } |
| 1234 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 1235 | if (!ret && module) { |
| 1236 | *module = mod; |
| 1237 | } |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1238 | return ret; |
| 1239 | } |
| 1240 | |
Radek Krejci | 545b487 | 2020-11-15 10:15:12 +0100 | [diff] [blame] | 1241 | static LYS_INFORMAT |
| 1242 | lys_parse_get_format(const struct ly_in *in, LYS_INFORMAT format) |
| 1243 | { |
| 1244 | if (!format && (in->type == LY_IN_FILEPATH)) { |
| 1245 | /* unknown format - try to detect it from filename's suffix */ |
| 1246 | const char *path = in->method.fpath.filepath; |
| 1247 | size_t len = strlen(path); |
| 1248 | |
| 1249 | /* ignore trailing whitespaces */ |
| 1250 | for ( ; len > 0 && isspace(path[len - 1]); len--) {} |
| 1251 | |
| 1252 | if ((len >= 5) && !strncmp(&path[len - 5], ".yang", 5)) { |
| 1253 | format = LYS_IN_YANG; |
| 1254 | } else if ((len >= 6) && !strncmp(&path[len - 4], ".yin", 4)) { |
| 1255 | format = LYS_IN_YIN; |
| 1256 | } /* else still unknown */ |
| 1257 | } |
| 1258 | |
| 1259 | return format; |
| 1260 | } |
| 1261 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1262 | API LY_ERR |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1263 | 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] | 1264 | { |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 1265 | LY_ERR ret; |
| 1266 | struct lys_glob_unres unres = {0}; |
| 1267 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1268 | if (module) { |
| 1269 | *module = NULL; |
| 1270 | } |
Radek Krejci | 545b487 | 2020-11-15 10:15:12 +0100 | [diff] [blame] | 1271 | LY_CHECK_ARG_RET(NULL, ctx, in, LY_EINVAL); |
| 1272 | |
| 1273 | format = lys_parse_get_format(in, format); |
| 1274 | LY_CHECK_ARG_RET(ctx, format, LY_EINVAL); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1275 | |
| 1276 | /* remember input position */ |
| 1277 | in->func_start = in->current; |
| 1278 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame^] | 1279 | ret = lys_create_module(ctx, in, format, 1, NULL, NULL, features, &unres, (struct lys_module **)module); |
| 1280 | LY_CHECK_GOTO(ret, cleanup); |
| 1281 | |
| 1282 | /* resolve global unres */ |
| 1283 | ret = lys_compile_unres_glob(ctx, &unres); |
| 1284 | LY_CHECK_GOTO(ret, cleanup); |
| 1285 | |
| 1286 | cleanup: |
| 1287 | if (ret) { |
| 1288 | lys_compile_unres_glob_revert(ctx, &unres); |
| 1289 | } |
| 1290 | lys_compile_unres_glob_erase(ctx, &unres); |
| 1291 | if (ret && module) { |
| 1292 | *module = NULL; |
| 1293 | } |
| 1294 | return ret; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1295 | } |
| 1296 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1297 | API LY_ERR |
| 1298 | 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] | 1299 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1300 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1301 | struct ly_in *in = NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1302 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1303 | LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL); |
Radek Krejci | 65639b9 | 2018-11-27 10:51:37 +0100 | [diff] [blame] | 1304 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1305 | 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] | 1306 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1307 | ret = lys_parse(ctx, in, format, NULL, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1308 | ly_in_free(in, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1309 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1310 | return ret; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1311 | } |
| 1312 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1313 | API LY_ERR |
| 1314 | 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] | 1315 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1316 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1317 | struct ly_in *in = NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1318 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1319 | 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] | 1320 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1321 | 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] | 1322 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1323 | ret = lys_parse(ctx, in, format, NULL, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1324 | ly_in_free(in, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1325 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1326 | return ret; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1327 | } |
| 1328 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1329 | API LY_ERR |
| 1330 | 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] | 1331 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1332 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1333 | struct ly_in *in = NULL; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1334 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1335 | LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1336 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1337 | LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1338 | 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] | 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); |
| 1342 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1343 | return ret; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | API LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1347 | 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] | 1348 | char **localfile, LYS_INFORMAT *format) |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1349 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1350 | LY_ERR ret = LY_EMEM; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1351 | size_t len, flen, match_len = 0, dir_len; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1352 | ly_bool implicit_cwd = 0; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1353 | char *wd, *wn = NULL; |
| 1354 | DIR *dir = NULL; |
| 1355 | struct dirent *file; |
| 1356 | char *match_name = NULL; |
| 1357 | LYS_INFORMAT format_aux, match_format = 0; |
| 1358 | struct ly_set *dirs; |
| 1359 | struct stat st; |
| 1360 | |
| 1361 | LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL); |
| 1362 | |
| 1363 | /* start to fill the dir fifo with the context's search path (if set) |
| 1364 | * and the current working directory */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1365 | LY_CHECK_RET(ly_set_new(&dirs)); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1366 | |
| 1367 | len = strlen(name); |
| 1368 | if (cwd) { |
| 1369 | wd = get_current_dir_name(); |
| 1370 | if (!wd) { |
| 1371 | LOGMEM(NULL); |
| 1372 | goto cleanup; |
| 1373 | } else { |
| 1374 | /* add implicit current working directory (./) to be searched, |
| 1375 | * this directory is not searched recursively */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1376 | ret = ly_set_add(dirs, wd, 0, NULL); |
| 1377 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1378 | implicit_cwd = 1; |
| 1379 | } |
| 1380 | } |
| 1381 | if (searchpaths) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1382 | for (uint64_t i = 0; searchpaths[i]; i++) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1383 | /* check for duplicities with the implicit current working directory */ |
| 1384 | if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) { |
| 1385 | implicit_cwd = 0; |
| 1386 | continue; |
| 1387 | } |
| 1388 | wd = strdup(searchpaths[i]); |
| 1389 | if (!wd) { |
| 1390 | LOGMEM(NULL); |
| 1391 | goto cleanup; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1392 | } else { |
| 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 | } |
| 1396 | } |
| 1397 | } |
| 1398 | wd = NULL; |
| 1399 | |
| 1400 | /* start searching */ |
| 1401 | while (dirs->count) { |
| 1402 | free(wd); |
| 1403 | free(wn); wn = NULL; |
| 1404 | |
| 1405 | dirs->count--; |
| 1406 | wd = (char *)dirs->objs[dirs->count]; |
| 1407 | dirs->objs[dirs->count] = NULL; |
| 1408 | LOGVRB("Searching for \"%s\" in %s.", name, wd); |
| 1409 | |
| 1410 | if (dir) { |
| 1411 | closedir(dir); |
| 1412 | } |
| 1413 | dir = opendir(wd); |
| 1414 | dir_len = strlen(wd); |
| 1415 | if (!dir) { |
| 1416 | LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno)); |
| 1417 | } else { |
| 1418 | while ((file = readdir(dir))) { |
| 1419 | if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) { |
| 1420 | /* skip . and .. */ |
| 1421 | continue; |
| 1422 | } |
| 1423 | free(wn); |
| 1424 | if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) { |
| 1425 | LOGMEM(NULL); |
| 1426 | goto cleanup; |
| 1427 | } |
| 1428 | if (stat(wn, &st) == -1) { |
| 1429 | 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] | 1430 | file->d_name, wd, strerror(errno)); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1431 | continue; |
| 1432 | } |
| 1433 | if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) { |
| 1434 | /* we have another subdirectory in searchpath to explore, |
| 1435 | * 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] | 1436 | ret = ly_set_add(dirs, wn, 0, NULL); |
| 1437 | LY_CHECK_GOTO(ret, cleanup); |
| 1438 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1439 | /* continue with the next item in current directory */ |
| 1440 | wn = NULL; |
| 1441 | continue; |
| 1442 | } else if (!S_ISREG(st.st_mode)) { |
| 1443 | /* not a regular file (note that we see the target of symlinks instead of symlinks */ |
| 1444 | continue; |
| 1445 | } |
| 1446 | |
| 1447 | /* here we know that the item is a file which can contain a module */ |
| 1448 | if (strncmp(name, file->d_name, len) || |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1449 | ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1450 | /* different filename than the module we search for */ |
| 1451 | continue; |
| 1452 | } |
| 1453 | |
| 1454 | /* get type according to filename suffix */ |
| 1455 | flen = strlen(file->d_name); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1456 | if (!strcmp(&file->d_name[flen - 5], ".yang")) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1457 | format_aux = LYS_IN_YANG; |
Radek Krejci | 01a937f | 2020-11-15 10:14:12 +0100 | [diff] [blame] | 1458 | } else if (!strcmp(&file->d_name[flen - 4], ".yin")) { |
| 1459 | format_aux = LYS_IN_YIN; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1460 | } else { |
| 1461 | /* not supportde suffix/file format */ |
| 1462 | continue; |
| 1463 | } |
| 1464 | |
| 1465 | if (revision) { |
| 1466 | /* we look for the specific revision, try to get it from the filename */ |
| 1467 | if (file->d_name[len] == '@') { |
| 1468 | /* check revision from the filename */ |
| 1469 | if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) { |
| 1470 | /* another revision */ |
| 1471 | continue; |
| 1472 | } else { |
| 1473 | /* exact revision */ |
| 1474 | free(match_name); |
| 1475 | match_name = wn; |
| 1476 | wn = NULL; |
| 1477 | match_len = dir_len + 1 + len; |
| 1478 | match_format = format_aux; |
| 1479 | goto success; |
| 1480 | } |
| 1481 | } else { |
| 1482 | /* continue trying to find exact revision match, use this only if not found */ |
| 1483 | free(match_name); |
| 1484 | match_name = wn; |
| 1485 | wn = NULL; |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 1486 | match_len = dir_len + 1 + len; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1487 | match_format = format_aux; |
| 1488 | continue; |
| 1489 | } |
| 1490 | } else { |
| 1491 | /* remember the revision and try to find the newest one */ |
| 1492 | if (match_name) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1493 | if ((file->d_name[len] != '@') || |
| 1494 | lysp_check_date(NULL, &file->d_name[len + 1], flen - ((format_aux == LYS_IN_YANG) ? 5 : 4) - len - 1, NULL)) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1495 | continue; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1496 | } else if ((match_name[match_len] == '@') && |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1497 | (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) { |
| 1498 | continue; |
| 1499 | } |
| 1500 | free(match_name); |
| 1501 | } |
| 1502 | |
| 1503 | match_name = wn; |
| 1504 | wn = NULL; |
| 1505 | match_len = dir_len + 1 + len; |
| 1506 | match_format = format_aux; |
| 1507 | continue; |
| 1508 | } |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | success: |
| 1514 | (*localfile) = match_name; |
| 1515 | match_name = NULL; |
| 1516 | if (format) { |
| 1517 | (*format) = match_format; |
| 1518 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1519 | ret = LY_SUCCESS; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1520 | |
| 1521 | cleanup: |
| 1522 | free(wn); |
| 1523 | free(wd); |
| 1524 | if (dir) { |
| 1525 | closedir(dir); |
| 1526 | } |
| 1527 | free(match_name); |
| 1528 | ly_set_free(dirs, free); |
| 1529 | |
| 1530 | return ret; |
| 1531 | } |