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