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 | |
Christian Hopps | 32874e1 | 2021-05-01 09:43:54 -0400 | [diff] [blame] | 15 | #define _GNU_SOURCE /* asprintf, strdup */ |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 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 | e482bb9 | 2023-02-01 11:41:41 +0100 | [diff] [blame] | 40 | #include "plugins_exts.h" |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 41 | #include "plugins_internal.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 42 | #include "schema_compile.h" |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 43 | #include "schema_compile_amend.h" |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 44 | #include "schema_features.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 45 | #include "set.h" |
| 46 | #include "tree.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 47 | #include "tree_edit.h" |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 48 | #include "tree_schema_free.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 49 | #include "tree_schema_internal.h" |
| 50 | #include "xpath.h" |
Radek Krejci | 3f5e3db | 2018-10-11 15:57:47 +0200 | [diff] [blame] | 51 | |
Radek Krejci | eccf660 | 2021-02-05 19:42:54 +0100 | [diff] [blame] | 52 | const char * const ly_devmod_list[] = { |
| 53 | [LYS_DEV_NOT_SUPPORTED] = "not-supported", |
| 54 | [LYS_DEV_ADD] = "add", |
| 55 | [LYS_DEV_DELETE] = "delete", |
| 56 | [LYS_DEV_REPLACE] = "replace", |
| 57 | }; |
| 58 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 59 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 60 | lysc_tree_dfs_full(const struct lysc_node *root, lysc_dfs_clb dfs_clb, void *data) |
| 61 | { |
Michal Vasko | 1d972ca | 2020-11-03 17:16:56 +0100 | [diff] [blame] | 62 | struct lysc_node *elem, *elem2; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 63 | const struct lysc_node_action *action; |
| 64 | const struct lysc_node_notif *notif; |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 65 | |
| 66 | LY_CHECK_ARG_RET(NULL, root, dfs_clb, LY_EINVAL); |
| 67 | |
| 68 | LYSC_TREE_DFS_BEGIN(root, elem) { |
| 69 | /* schema node */ |
| 70 | LY_CHECK_RET(dfs_clb(elem, data, &LYSC_TREE_DFS_continue)); |
| 71 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 72 | LY_LIST_FOR(lysc_node_actions(elem), action) { |
| 73 | LYSC_TREE_DFS_BEGIN(action, elem2) { |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 74 | /* action subtree */ |
| 75 | LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue)); |
| 76 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 77 | LYSC_TREE_DFS_END(action, elem2); |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 81 | LY_LIST_FOR(lysc_node_notifs(elem), notif) { |
| 82 | LYSC_TREE_DFS_BEGIN(notif, elem2) { |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 83 | /* notification subtree */ |
| 84 | LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue)); |
| 85 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 86 | LYSC_TREE_DFS_END(notif, elem2); |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | LYSC_TREE_DFS_END(root, elem); |
| 91 | } |
| 92 | |
| 93 | return LY_SUCCESS; |
| 94 | } |
| 95 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 96 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 97 | lysc_module_dfs_full(const struct lys_module *mod, lysc_dfs_clb dfs_clb, void *data) |
| 98 | { |
Michal Vasko | 2336cf5 | 2020-11-03 17:18:15 +0100 | [diff] [blame] | 99 | const struct lysc_node *root; |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 100 | |
| 101 | LY_CHECK_ARG_RET(NULL, mod, mod->compiled, dfs_clb, LY_EINVAL); |
| 102 | |
| 103 | /* schema nodes */ |
Michal Vasko | 2336cf5 | 2020-11-03 17:18:15 +0100 | [diff] [blame] | 104 | LY_LIST_FOR(mod->compiled->data, root) { |
| 105 | LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data)); |
| 106 | } |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 107 | |
| 108 | /* RPCs */ |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 109 | LY_LIST_FOR((const struct lysc_node *)mod->compiled->rpcs, root) { |
| 110 | LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data)); |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* notifications */ |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 114 | LY_LIST_FOR((const struct lysc_node *)mod->compiled->notifs, root) { |
| 115 | LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data)); |
Michal Vasko | f1ab44f | 2020-10-22 08:58:32 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | return LY_SUCCESS; |
| 119 | } |
| 120 | |
Radek Krejci | b93bd41 | 2020-11-02 13:23:11 +0100 | [diff] [blame] | 121 | static void |
| 122 | lys_getnext_into_case(const struct lysc_node_case *first_case, const struct lysc_node **last, const struct lysc_node **next) |
| 123 | { |
Radek Krejci | c5b54a0 | 2020-11-05 17:13:18 +0100 | [diff] [blame] | 124 | 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] | 125 | if (first_case->child) { |
| 126 | /* there is something to return */ |
| 127 | (*next) = first_case->child; |
| 128 | return; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* no children in choice's cases, so go to the choice's sibling instead of into it */ |
| 133 | (*last) = (*next); |
| 134 | (*next) = (*next)->next; |
| 135 | } |
| 136 | |
Radek Krejci | 035dacf | 2021-02-12 18:25:53 +0100 | [diff] [blame] | 137 | /** |
| 138 | * @brief Generic getnext function for ::lys_getnext() and ::lys_getnext_ext(). |
| 139 | * |
| 140 | * Gets next schema tree (sibling) node element that can be instantiated in a data tree. Returned node can |
| 141 | * be from an augment. If the @p ext is provided, the function is locked inside the schema tree defined in the |
| 142 | * extension instance. |
| 143 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 144 | * ::lys_getnext_() is supposed to be called sequentially. In the first call, the @p last parameter is usually NULL |
| 145 | * and function starts returning i) the first @p parent's child or ii) the first top level element specified in the |
| 146 | * given extension (if provided) or iii) the first top level element of the @p module. |
| 147 | * Consequent calls suppose to provide the previously returned node as the @p last parameter and still the same |
| 148 | * @p parent and @p module parameters. |
Radek Krejci | 035dacf | 2021-02-12 18:25:53 +0100 | [diff] [blame] | 149 | * |
| 150 | * Without options, the function is used to traverse only the schema nodes that can be paired with corresponding |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 151 | * data nodes in a data tree. By setting some @p options the behavior can be modified to the extent that |
Radek Krejci | 035dacf | 2021-02-12 18:25:53 +0100 | [diff] [blame] | 152 | * all the schema nodes are iteratively returned. |
| 153 | * |
| 154 | * @param[in] last Previously returned schema tree node, or NULL in case of the first call. |
| 155 | * @param[in] parent Parent of the subtree where the function starts processing. |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 156 | * @param[in] module In case of iterating on top level elements, the @p parent is NULL and |
Radek Krejci | 035dacf | 2021-02-12 18:25:53 +0100 | [diff] [blame] | 157 | * module must be specified. |
| 158 | * @param[in] ext The extension instance to provide a separate schema tree. To consider the top level elements in the tree, |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 159 | * the @p parent must be NULL. Anyway, at least one of @p parent, @p module and @p ext parameters must be specified. |
Radek Krejci | 035dacf | 2021-02-12 18:25:53 +0100 | [diff] [blame] | 160 | * @param[in] options [ORed options](@ref sgetnextflags). |
| 161 | * @return Next schema tree node that can be instantiated in a data tree, NULL in case there is no such element. |
| 162 | */ |
| 163 | static const struct lysc_node * |
| 164 | lys_getnext_(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, |
| 165 | const struct lysc_ext_instance *ext, uint32_t options) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 166 | { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 167 | const struct lysc_node *next = NULL; |
Michal Vasko | e482bb9 | 2023-02-01 11:41:41 +0100 | [diff] [blame] | 168 | ly_bool action_flag = 0, notif_flag = 0, sm_flag = options & LYS_GETNEXT_WITHSCHEMAMOUNT ? 0 : 1; |
| 169 | LY_ARRAY_COUNT_TYPE u; |
| 170 | struct ly_ctx *sm_ctx = NULL; |
| 171 | const struct lys_module *mod; |
| 172 | uint32_t idx; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 173 | |
Radek Krejci | 035dacf | 2021-02-12 18:25:53 +0100 | [diff] [blame] | 174 | LY_CHECK_ARG_RET(NULL, parent || module || ext, NULL); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 175 | |
Radek Krejci | d5a2b9d | 2019-04-12 10:39:30 +0200 | [diff] [blame] | 176 | next: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 177 | if (!last) { |
| 178 | /* first call */ |
| 179 | |
Michal Vasko | e482bb9 | 2023-02-01 11:41:41 +0100 | [diff] [blame] | 180 | /* learn where to start */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 181 | if (parent) { |
| 182 | /* schema subtree */ |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 183 | next = last = lysc_node_child(parent); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 184 | } else { |
| 185 | /* top level data */ |
Radek Krejci | 035dacf | 2021-02-12 18:25:53 +0100 | [diff] [blame] | 186 | if (ext) { |
Michal Vasko | fbd037c | 2022-11-08 10:34:20 +0100 | [diff] [blame] | 187 | lyplg_ext_get_storage(ext, LY_STMT_DATA_NODE_MASK, sizeof last, (const void **)&last); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 188 | next = last; |
Radek Krejci | 035dacf | 2021-02-12 18:25:53 +0100 | [diff] [blame] | 189 | } else { |
| 190 | next = last = module->data; |
| 191 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 192 | } |
| 193 | if (!next) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 194 | /* try to get action or notification */ |
| 195 | goto repeat; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 196 | } |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 197 | /* test if the next can be returned */ |
| 198 | goto check; |
| 199 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 200 | } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 201 | action_flag = 1; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 202 | next = last->next; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 203 | } else if (last->nodetype == LYS_NOTIF) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 204 | action_flag = notif_flag = 1; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 205 | next = last->next; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 206 | } else { |
| 207 | next = last->next; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 210 | repeat: |
| 211 | if (!next) { |
Michal Vasko | e482bb9 | 2023-02-01 11:41:41 +0100 | [diff] [blame] | 212 | if (last && !sm_flag && parent && (last->module->ctx != parent->module->ctx)) { |
| 213 | sm_flag = 1; |
| 214 | |
| 215 | /* find the module of last */ |
| 216 | sm_ctx = last->module->ctx; |
| 217 | idx = 0; |
| 218 | while ((mod = ly_ctx_get_module_iter(sm_ctx, &idx))) { |
| 219 | if (mod == last->module) { |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | assert(mod); |
| 224 | |
| 225 | /* get node from the next mounted module */ |
| 226 | while (!next && (mod = ly_ctx_get_module_iter(sm_ctx, &idx))) { |
| 227 | if (!mod->implemented) { |
| 228 | continue; |
| 229 | } |
| 230 | |
| 231 | next = lys_getnext(NULL, NULL, mod->compiled, options & ~LYS_GETNEXT_WITHSCHEMAMOUNT); |
| 232 | } |
| 233 | } else if (last && (last->parent != parent)) { |
| 234 | /* go back to parent */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 235 | last = last->parent; |
Radek Krejci | d5a2b9d | 2019-04-12 10:39:30 +0200 | [diff] [blame] | 236 | goto next; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 237 | } else if (!action_flag) { |
| 238 | action_flag = 1; |
Radek Krejci | 035dacf | 2021-02-12 18:25:53 +0100 | [diff] [blame] | 239 | if (ext) { |
Michal Vasko | fbd037c | 2022-11-08 10:34:20 +0100 | [diff] [blame] | 240 | lyplg_ext_get_storage(ext, LY_STMT_OP_MASK, sizeof next, (const void **)&next); |
Radek Krejci | 035dacf | 2021-02-12 18:25:53 +0100 | [diff] [blame] | 241 | } else if (parent) { |
| 242 | next = (struct lysc_node *)lysc_node_actions(parent); |
| 243 | } else { |
| 244 | next = (struct lysc_node *)module->rpcs; |
| 245 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 246 | } else if (!notif_flag) { |
| 247 | notif_flag = 1; |
Radek Krejci | 035dacf | 2021-02-12 18:25:53 +0100 | [diff] [blame] | 248 | if (ext) { |
Michal Vasko | fbd037c | 2022-11-08 10:34:20 +0100 | [diff] [blame] | 249 | lyplg_ext_get_storage(ext, LY_STMT_NOTIFICATION, sizeof next, (const void **)&next); |
Radek Krejci | 035dacf | 2021-02-12 18:25:53 +0100 | [diff] [blame] | 250 | } else if (parent) { |
| 251 | next = (struct lysc_node *)lysc_node_notifs(parent); |
| 252 | } else { |
| 253 | next = (struct lysc_node *)module->notifs; |
| 254 | } |
Michal Vasko | e482bb9 | 2023-02-01 11:41:41 +0100 | [diff] [blame] | 255 | } else if (!sm_flag) { |
| 256 | sm_flag = 1; |
| 257 | if (parent) { |
| 258 | LY_ARRAY_FOR(parent->exts, u) { |
| 259 | if (!strcmp(parent->exts[u].def->name, "mount-point") && |
| 260 | !strcmp(parent->exts[u].def->module->name, "ietf-yang-schema-mount")) { |
| 261 | lyplg_ext_schema_mount_create_context(&parent->exts[u], &sm_ctx); |
| 262 | if (sm_ctx) { |
| 263 | /* some usable context created */ |
| 264 | break; |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | if (sm_ctx) { |
| 269 | /* get the first node from the first usable module */ |
| 270 | idx = 0; |
| 271 | while (!next && (mod = ly_ctx_get_module_iter(sm_ctx, &idx))) { |
| 272 | if (!mod->implemented) { |
| 273 | continue; |
| 274 | } |
| 275 | |
| 276 | next = lys_getnext(NULL, NULL, mod->compiled, options & ~LYS_GETNEXT_WITHSCHEMAMOUNT); |
| 277 | } |
| 278 | if (!next) { |
| 279 | /* no nodes found */ |
| 280 | ly_ctx_destroy(sm_ctx); |
| 281 | } |
| 282 | } |
| 283 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 284 | } else { |
| 285 | return NULL; |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 286 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 287 | goto repeat; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 288 | } |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 289 | check: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 290 | switch (next->nodetype) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 291 | case LYS_RPC: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 292 | case LYS_ACTION: |
| 293 | case LYS_NOTIF: |
| 294 | case LYS_LEAF: |
| 295 | case LYS_ANYXML: |
| 296 | case LYS_ANYDATA: |
| 297 | case LYS_LIST: |
| 298 | case LYS_LEAFLIST: |
| 299 | break; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 300 | case LYS_CASE: |
| 301 | if (options & LYS_GETNEXT_WITHCASE) { |
| 302 | break; |
| 303 | } else { |
| 304 | /* go into */ |
Radek Krejci | b93bd41 | 2020-11-02 13:23:11 +0100 | [diff] [blame] | 305 | lys_getnext_into_case((const struct lysc_node_case *)next, &last, &next); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 306 | } |
| 307 | goto repeat; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 308 | case LYS_CONTAINER: |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 309 | if (!(next->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) { |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 310 | if (lysc_node_child(next)) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 311 | /* go into */ |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 312 | next = lysc_node_child(next); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 313 | } else { |
Radek Krejci | b93bd41 | 2020-11-02 13:23:11 +0100 | [diff] [blame] | 314 | last = next; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 315 | next = next->next; |
| 316 | } |
| 317 | goto repeat; |
| 318 | } |
| 319 | break; |
| 320 | case LYS_CHOICE: |
| 321 | if (options & LYS_GETNEXT_WITHCHOICE) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 322 | break; |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 323 | } else if ((options & LYS_GETNEXT_NOCHOICE) || !lysc_node_child(next)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 324 | next = next->next; |
| 325 | } else { |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 326 | if (options & LYS_GETNEXT_WITHCASE) { |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 327 | next = lysc_node_child(next); |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 328 | } else { |
Radek Krejci | b93bd41 | 2020-11-02 13:23:11 +0100 | [diff] [blame] | 329 | /* go into */ |
| 330 | lys_getnext_into_case(((struct lysc_node_choice *)next)->cases, &last, &next); |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 331 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 332 | } |
| 333 | goto repeat; |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 334 | case LYS_INPUT: |
| 335 | if (options & LYS_GETNEXT_OUTPUT) { |
| 336 | /* skip */ |
| 337 | next = next->next; |
| 338 | } else { |
| 339 | /* go into */ |
| 340 | next = lysc_node_child(next); |
| 341 | } |
| 342 | goto repeat; |
| 343 | case LYS_OUTPUT: |
| 344 | if (!(options & LYS_GETNEXT_OUTPUT)) { |
| 345 | /* skip */ |
| 346 | next = next->next; |
| 347 | } else { |
| 348 | /* go into */ |
| 349 | next = lysc_node_child(next); |
| 350 | } |
| 351 | goto repeat; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 352 | default: |
| 353 | /* we should not be here */ |
Radek Krejci | 035dacf | 2021-02-12 18:25:53 +0100 | [diff] [blame] | 354 | LOGINT(module ? module->mod->ctx : parent ? parent->module->ctx : ext->module->ctx); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 355 | return NULL; |
| 356 | } |
| 357 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 358 | return next; |
| 359 | } |
| 360 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 361 | LIBYANG_API_DEF const struct lysc_node * |
Radek Krejci | 035dacf | 2021-02-12 18:25:53 +0100 | [diff] [blame] | 362 | lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, uint32_t options) |
| 363 | { |
| 364 | return lys_getnext_(last, parent, module, NULL, options); |
| 365 | } |
| 366 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 367 | LIBYANG_API_DEF const struct lysc_node * |
Radek Krejci | 035dacf | 2021-02-12 18:25:53 +0100 | [diff] [blame] | 368 | lys_getnext_ext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_ext_instance *ext, uint32_t options) |
| 369 | { |
| 370 | return lys_getnext_(last, parent, NULL, ext, options); |
| 371 | } |
| 372 | |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 373 | const struct lysc_node * |
Radek Krejci | ba05eab | 2021-03-10 13:19:29 +0100 | [diff] [blame] | 374 | lysc_ext_find_node(const struct lysc_ext_instance *ext, const struct lys_module *module, const char *name, size_t name_len, |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 375 | uint16_t nodetype, uint32_t options) |
| 376 | { |
| 377 | const struct lysc_node *node = NULL; |
| 378 | |
| 379 | LY_CHECK_ARG_RET(NULL, ext, name, NULL); |
| 380 | if (!nodetype) { |
| 381 | nodetype = LYS_NODETYPE_MASK; |
| 382 | } |
| 383 | |
| 384 | if (module && (module != ext->module)) { |
| 385 | return NULL; |
| 386 | } |
| 387 | |
| 388 | while ((node = lys_getnext_ext(node, NULL, ext, options))) { |
| 389 | if (!(node->nodetype & nodetype)) { |
| 390 | continue; |
| 391 | } |
| 392 | |
| 393 | if (name_len) { |
| 394 | if (!ly_strncmp(node->name, name, name_len)) { |
| 395 | return node; |
| 396 | } |
| 397 | } else { |
| 398 | if (!strcmp(node->name, name)) { |
| 399 | return node; |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | return NULL; |
| 404 | } |
| 405 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 406 | LIBYANG_API_DEF const struct lysc_node * |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 407 | 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] | 408 | uint16_t nodetype, uint32_t options) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 409 | { |
| 410 | const struct lysc_node *node = NULL; |
| 411 | |
| 412 | LY_CHECK_ARG_RET(NULL, module, name, NULL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 413 | LY_CHECK_CTX_EQUAL_RET(parent ? parent->module->ctx : NULL, module->ctx, NULL); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 414 | if (!nodetype) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 415 | nodetype = LYS_NODETYPE_MASK; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | while ((node = lys_getnext(node, parent, module->compiled, options))) { |
| 419 | if (!(node->nodetype & nodetype)) { |
| 420 | continue; |
| 421 | } |
| 422 | if (node->module != module) { |
| 423 | continue; |
| 424 | } |
| 425 | |
| 426 | if (name_len) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 427 | if (!ly_strncmp(node->name, name, name_len)) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 428 | return node; |
| 429 | } |
| 430 | } else { |
| 431 | if (!strcmp(node->name, name)) { |
| 432 | return node; |
| 433 | } |
| 434 | } |
| 435 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 436 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 437 | return NULL; |
| 438 | } |
| 439 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 440 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 441 | lys_find_xpath_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options, |
| 442 | struct ly_set **set) |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 443 | { |
| 444 | LY_ERR ret = LY_SUCCESS; |
| 445 | struct lyxp_set xp_set; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 446 | struct lyxp_expr *exp = NULL; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 447 | uint32_t i; |
| 448 | |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 449 | LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 450 | LY_CHECK_CTX_EQUAL_RET(ctx, ctx_node ? ctx_node->module->ctx : NULL, LY_EINVAL); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 451 | if (!(options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 452 | options |= LYXP_SCNODE; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 453 | } |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 454 | if (!ctx) { |
| 455 | ctx = ctx_node->module->ctx; |
| 456 | } |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 457 | |
| 458 | memset(&xp_set, 0, sizeof xp_set); |
| 459 | |
| 460 | /* compile expression */ |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 461 | ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 462 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 463 | |
| 464 | /* atomize expression */ |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 465 | ret = lyxp_atomize(ctx, exp, NULL, LY_VALUE_JSON, NULL, ctx_node, ctx_node, &xp_set, options); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 466 | LY_CHECK_GOTO(ret, cleanup); |
| 467 | |
| 468 | /* allocate return set */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 469 | ret = ly_set_new(set); |
| 470 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 471 | |
| 472 | /* transform into ly_set */ |
| 473 | (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs); |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 474 | LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 475 | (*set)->size = xp_set.used; |
| 476 | |
| 477 | for (i = 0; i < xp_set.used; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 478 | if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 479 | 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] | 480 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
| 484 | cleanup: |
| 485 | lyxp_set_free_content(&xp_set); |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 486 | lyxp_expr_free(ctx, exp); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 487 | return ret; |
| 488 | } |
| 489 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 490 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 491 | lys_find_expr_atoms(const struct lysc_node *ctx_node, const struct lys_module *cur_mod, const struct lyxp_expr *expr, |
| 492 | const struct lysc_prefix *prefixes, uint32_t options, struct ly_set **set) |
| 493 | { |
| 494 | LY_ERR ret = LY_SUCCESS; |
| 495 | struct lyxp_set xp_set = {0}; |
| 496 | uint32_t i; |
| 497 | |
| 498 | LY_CHECK_ARG_RET(NULL, cur_mod, expr, prefixes, set, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 499 | LY_CHECK_CTX_EQUAL_RET(ctx_node ? ctx_node->module->ctx : NULL, cur_mod->ctx, LY_EINVAL); |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 500 | if (!(options & LYXP_SCNODE_ALL)) { |
| 501 | options = LYXP_SCNODE; |
| 502 | } |
| 503 | |
| 504 | /* atomize expression */ |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 505 | ret = lyxp_atomize(cur_mod->ctx, expr, cur_mod, LY_VALUE_SCHEMA_RESOLVED, (void *)prefixes, ctx_node, ctx_node, |
| 506 | &xp_set, options); |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 507 | LY_CHECK_GOTO(ret, cleanup); |
| 508 | |
| 509 | /* allocate return set */ |
| 510 | ret = ly_set_new(set); |
| 511 | LY_CHECK_GOTO(ret, cleanup); |
| 512 | |
| 513 | /* transform into ly_set */ |
| 514 | (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs); |
| 515 | LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(cur_mod->ctx); ret = LY_EMEM, cleanup); |
| 516 | (*set)->size = xp_set.used; |
| 517 | |
| 518 | for (i = 0; i < xp_set.used; ++i) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 519 | if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx >= LYXP_SET_SCNODE_ATOM_NODE)) { |
| 520 | assert((xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) || |
| 521 | (xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL) || |
Michal Vasko | d97959c | 2020-12-10 12:18:28 +0100 | [diff] [blame] | 522 | (xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX)); |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 523 | ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL); |
| 524 | LY_CHECK_GOTO(ret, cleanup); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | cleanup: |
| 529 | lyxp_set_free_content(&xp_set); |
| 530 | if (ret) { |
| 531 | ly_set_free(*set, NULL); |
| 532 | *set = NULL; |
| 533 | } |
| 534 | return ret; |
| 535 | } |
| 536 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 537 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 538 | lys_find_xpath(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options, |
| 539 | struct ly_set **set) |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 540 | { |
| 541 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 542 | struct lyxp_set xp_set = {0}; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 543 | struct lyxp_expr *exp = NULL; |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 544 | uint32_t i; |
| 545 | |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 546 | LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 547 | LY_CHECK_CTX_EQUAL_RET(ctx, ctx_node ? ctx_node->module->ctx : NULL, LY_EINVAL); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 548 | if (!(options & LYXP_SCNODE_ALL)) { |
| 549 | options = LYXP_SCNODE; |
| 550 | } |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 551 | if (!ctx) { |
| 552 | ctx = ctx_node->module->ctx; |
| 553 | } |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 554 | |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 555 | /* compile expression */ |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 556 | ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 557 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 558 | |
| 559 | /* atomize expression */ |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 560 | ret = lyxp_atomize(ctx, exp, NULL, LY_VALUE_JSON, NULL, ctx_node, ctx_node, &xp_set, options); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 561 | LY_CHECK_GOTO(ret, cleanup); |
| 562 | |
| 563 | /* allocate return set */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 564 | ret = ly_set_new(set); |
| 565 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 566 | |
| 567 | /* transform into ly_set */ |
| 568 | (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs); |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 569 | LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 570 | (*set)->size = xp_set.used; |
| 571 | |
| 572 | for (i = 0; i < xp_set.used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 573 | if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 574 | 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] | 575 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
| 579 | cleanup: |
| 580 | lyxp_set_free_content(&xp_set); |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 581 | lyxp_expr_free(ctx, exp); |
Michal Vasko | ae15966 | 2020-10-21 11:57:24 +0200 | [diff] [blame] | 582 | if (ret) { |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 583 | ly_set_free(*set, NULL); |
| 584 | *set = NULL; |
| 585 | } |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 586 | return ret; |
| 587 | } |
| 588 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 589 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 590 | lys_find_lypath_atoms(const struct ly_path *path, struct ly_set **set) |
| 591 | { |
| 592 | LY_ERR ret = LY_SUCCESS; |
| 593 | LY_ARRAY_COUNT_TYPE u, v; |
| 594 | |
| 595 | LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL); |
| 596 | |
| 597 | /* allocate return set */ |
| 598 | LY_CHECK_RET(ly_set_new(set)); |
| 599 | |
| 600 | LY_ARRAY_FOR(path, u) { |
| 601 | /* add nodes from the path */ |
| 602 | LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].node, 0, NULL), cleanup); |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 603 | LY_ARRAY_FOR(path[u].predicates, v) { |
| 604 | if ((path[u].predicates[v].type == LY_PATH_PREDTYPE_LIST) || (path[u].predicates[v].type == LY_PATH_PREDTYPE_LIST_VAR)) { |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 605 | /* add all the keys in a predicate */ |
| 606 | LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup); |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | cleanup: |
| 612 | if (ret) { |
| 613 | ly_set_free(*set, NULL); |
| 614 | *set = NULL; |
| 615 | } |
| 616 | return ret; |
| 617 | } |
| 618 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 619 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 620 | lys_find_path_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output, |
| 621 | struct ly_set **set) |
| 622 | { |
| 623 | LY_ERR ret = LY_SUCCESS; |
| 624 | uint8_t oper; |
| 625 | struct lyxp_expr *expr = NULL; |
| 626 | struct ly_path *p = NULL; |
| 627 | |
| 628 | LY_CHECK_ARG_RET(ctx, ctx || ctx_node, path, set, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 629 | LY_CHECK_CTX_EQUAL_RET(ctx, ctx_node ? ctx_node->module->ctx : NULL, LY_EINVAL); |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 630 | |
| 631 | if (!ctx) { |
| 632 | ctx = ctx_node->module->ctx; |
| 633 | } |
| 634 | |
| 635 | /* parse */ |
Michal Vasko | 4f7c53d | 2023-04-17 08:45:53 +0200 | [diff] [blame] | 636 | ret = ly_path_parse(ctx, ctx_node, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_FIRST, |
| 637 | LY_PATH_PRED_SIMPLE, &expr); |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 638 | LY_CHECK_GOTO(ret, cleanup); |
| 639 | |
| 640 | /* compile */ |
| 641 | oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT; |
Michal Vasko | 0884d21 | 2021-10-14 09:21:46 +0200 | [diff] [blame] | 642 | ret = ly_path_compile(ctx, NULL, ctx_node, NULL, expr, oper, LY_PATH_TARGET_MANY, 0, LY_VALUE_JSON, NULL, &p); |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 643 | LY_CHECK_GOTO(ret, cleanup); |
| 644 | |
| 645 | /* resolve */ |
| 646 | ret = lys_find_lypath_atoms(p, set); |
| 647 | |
| 648 | cleanup: |
| 649 | ly_path_free(ctx, p); |
| 650 | lyxp_expr_free(ctx, expr); |
| 651 | return ret; |
| 652 | } |
| 653 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 654 | LIBYANG_API_DEF const struct lysc_node * |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 655 | lys_find_path(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output) |
| 656 | { |
| 657 | const struct lysc_node *snode = NULL; |
Michal Vasko | 4f7c53d | 2023-04-17 08:45:53 +0200 | [diff] [blame] | 658 | struct lyxp_expr *expr = NULL; |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 659 | struct ly_path *p = NULL; |
| 660 | LY_ERR ret; |
| 661 | uint8_t oper; |
| 662 | |
| 663 | LY_CHECK_ARG_RET(ctx, ctx || ctx_node, NULL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 664 | LY_CHECK_CTX_EQUAL_RET(ctx, ctx_node ? ctx_node->module->ctx : NULL, NULL); |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 665 | |
| 666 | if (!ctx) { |
| 667 | ctx = ctx_node->module->ctx; |
| 668 | } |
| 669 | |
| 670 | /* parse */ |
Michal Vasko | 4f7c53d | 2023-04-17 08:45:53 +0200 | [diff] [blame] | 671 | ret = ly_path_parse(ctx, ctx_node, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_FIRST, |
| 672 | LY_PATH_PRED_SIMPLE, &expr); |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 673 | LY_CHECK_GOTO(ret, cleanup); |
| 674 | |
| 675 | /* compile */ |
| 676 | oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT; |
Michal Vasko | 4f7c53d | 2023-04-17 08:45:53 +0200 | [diff] [blame] | 677 | ret = ly_path_compile(ctx, NULL, ctx_node, NULL, expr, oper, LY_PATH_TARGET_MANY, 0, LY_VALUE_JSON, NULL, &p); |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 678 | LY_CHECK_GOTO(ret, cleanup); |
| 679 | |
| 680 | /* get last node */ |
| 681 | snode = p[LY_ARRAY_COUNT(p) - 1].node; |
| 682 | |
| 683 | cleanup: |
| 684 | ly_path_free(ctx, p); |
Michal Vasko | 4f7c53d | 2023-04-17 08:45:53 +0200 | [diff] [blame] | 685 | lyxp_expr_free(ctx, expr); |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 686 | return snode; |
| 687 | } |
| 688 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 689 | char * |
| 690 | 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] | 691 | size_t buflen) |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 692 | { |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 693 | const struct lysc_node *iter, *par, *key; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 694 | char *path = NULL; |
| 695 | int len = 0; |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 696 | ly_bool skip_schema; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 697 | |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 698 | if (buffer) { |
| 699 | LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL); |
Michal Vasko | 770d3fc | 2021-01-26 09:14:35 +0100 | [diff] [blame] | 700 | buffer[0] = '\0'; |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 701 | } |
| 702 | |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 703 | if ((pathtype == LYSC_PATH_DATA) || (pathtype == LYSC_PATH_DATA_PATTERN)) { |
| 704 | /* skip schema-only nodes */ |
| 705 | skip_schema = 1; |
| 706 | } else { |
| 707 | skip_schema = 0; |
| 708 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 709 | |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 710 | for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) { |
| 711 | char *s; |
| 712 | const char *slash; |
Michal Vasko | 65de040 | 2020-08-03 16:34:19 +0200 | [diff] [blame] | 713 | |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 714 | if (skip_schema && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))) { |
| 715 | /* schema-only node */ |
| 716 | continue; |
| 717 | } |
Michal Vasko | 87dd134 | 2021-08-23 16:17:55 +0200 | [diff] [blame] | 718 | |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 719 | if ((pathtype == LYSC_PATH_DATA_PATTERN) && (iter->nodetype == LYS_LIST)) { |
| 720 | key = NULL; |
| 721 | while ((key = lys_getnext(key, iter, NULL, 0)) && lysc_is_key(key)) { |
| 722 | s = buffer ? strdup(buffer) : path; |
Michal Vasko | 87dd134 | 2021-08-23 16:17:55 +0200 | [diff] [blame] | 723 | |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 724 | /* print key predicate */ |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 725 | if (buffer) { |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 726 | len = snprintf(buffer, buflen, "[%s='%%s']%s", key->name, s ? s : ""); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 727 | } else { |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 728 | len = asprintf(&path, "[%s='%%s']%s", key->name, s ? s : ""); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 729 | } |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 730 | free(s); |
Michal Vasko | 28d0381 | 2022-03-24 15:26:11 +0100 | [diff] [blame] | 731 | |
| 732 | if (buffer && (buflen <= (size_t)len)) { |
| 733 | /* not enough space in buffer */ |
| 734 | break; |
| 735 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 736 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 737 | } |
| 738 | |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 739 | s = buffer ? strdup(buffer) : path; |
| 740 | if (parent && (iter->parent == parent)) { |
| 741 | slash = ""; |
| 742 | } else { |
| 743 | slash = "/"; |
| 744 | } |
| 745 | |
| 746 | if (skip_schema) { |
| 747 | par = lysc_data_parent(iter); |
| 748 | } else { |
| 749 | par = iter->parent; |
| 750 | } |
| 751 | |
| 752 | if (!par || (par->module != iter->module)) { |
| 753 | /* print prefix */ |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 754 | if (buffer) { |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 755 | len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, iter->name, s ? s : ""); |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 756 | } else { |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 757 | len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, iter->name, s ? s : ""); |
| 758 | } |
| 759 | } else { |
| 760 | /* prefix is the same as in parent */ |
| 761 | if (buffer) { |
| 762 | len = snprintf(buffer, buflen, "%s%s%s", slash, iter->name, s ? s : ""); |
| 763 | } else { |
| 764 | len = asprintf(&path, "%s%s%s", slash, iter->name, s ? s : ""); |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 765 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 766 | } |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 767 | free(s); |
| 768 | |
| 769 | if (buffer && (buflen <= (size_t)len)) { |
| 770 | /* not enough space in buffer */ |
| 771 | break; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | if (len < 0) { |
| 776 | free(path); |
| 777 | path = NULL; |
| 778 | } else if (len == 0) { |
| 779 | if (buffer) { |
| 780 | strcpy(buffer, "/"); |
| 781 | } else { |
| 782 | path = strdup("/"); |
| 783 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 784 | } |
| 785 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 786 | if (buffer) { |
| 787 | return buffer; |
| 788 | } else { |
| 789 | return path; |
| 790 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 791 | } |
| 792 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 793 | LIBYANG_API_DEF char * |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 794 | lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen) |
| 795 | { |
| 796 | return lysc_path_until(node, NULL, pathtype, buffer, buflen); |
| 797 | } |
| 798 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 799 | LY_ERR |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 800 | _lys_set_implemented(struct lys_module *mod, const char **features, struct lys_glob_unres *unres) |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 801 | { |
| 802 | LY_ERR ret = LY_SUCCESS, r; |
aPiecek | 6b3d542 | 2021-07-30 15:55:43 +0200 | [diff] [blame] | 803 | struct lys_module *mod_iter; |
Michal Vasko | c56d637 | 2021-10-19 12:29:00 +0200 | [diff] [blame] | 804 | const char **imp_f, *all_f[] = {"*", NULL}; |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 805 | uint32_t i; |
Michal Vasko | 916aefb | 2020-11-02 15:43:16 +0100 | [diff] [blame] | 806 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 807 | if (mod->implemented) { |
| 808 | /* mod is already implemented, set the features */ |
| 809 | r = lys_set_features(mod->parsed, features); |
| 810 | if (r == LY_EEXIST) { |
| 811 | /* no changes */ |
| 812 | return LY_SUCCESS; |
Michal Vasko | 7213ac5 | 2021-06-15 11:52:13 +0200 | [diff] [blame] | 813 | } else if (!r) { |
| 814 | /* mark the module as changed */ |
Michal Vasko | 01db7de | 2021-04-16 12:23:30 +0200 | [diff] [blame] | 815 | mod->to_compile = 1; |
Michal Vasko | 01db7de | 2021-04-16 12:23:30 +0200 | [diff] [blame] | 816 | } |
Michal Vasko | 7213ac5 | 2021-06-15 11:52:13 +0200 | [diff] [blame] | 817 | |
| 818 | return r; |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 819 | } |
Michal Vasko | 08c8b27 | 2020-11-24 18:11:30 +0100 | [diff] [blame] | 820 | |
Michal Vasko | 7213ac5 | 2021-06-15 11:52:13 +0200 | [diff] [blame] | 821 | /* implement, ignore recompilation because it must always take place later */ |
| 822 | r = lys_implement(mod, features, unres); |
| 823 | LY_CHECK_ERR_GOTO(r && (r != LY_ERECOMPILE), ret = r, cleanup); |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 824 | |
| 825 | if (mod->ctx->flags & LY_CTX_ALL_IMPLEMENTED) { |
| 826 | /* implement all the imports as well */ |
| 827 | for (i = 0; i < unres->creating.count; ++i) { |
| 828 | mod = unres->creating.objs[i]; |
| 829 | if (mod->implemented) { |
| 830 | continue; |
| 831 | } |
| 832 | |
Michal Vasko | c56d637 | 2021-10-19 12:29:00 +0200 | [diff] [blame] | 833 | imp_f = (mod->ctx->flags & LY_CTX_ENABLE_IMP_FEATURES) ? all_f : NULL; |
| 834 | r = lys_implement(mod, imp_f, unres); |
Michal Vasko | 7213ac5 | 2021-06-15 11:52:13 +0200 | [diff] [blame] | 835 | LY_CHECK_ERR_GOTO(r && (r != LY_ERECOMPILE), ret = r, cleanup); |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | |
aPiecek | 6b3d542 | 2021-07-30 15:55:43 +0200 | [diff] [blame] | 839 | /* Try to find module with LYS_MOD_IMPORTED_REV flag. */ |
| 840 | i = 0; |
| 841 | while ((mod_iter = ly_ctx_get_module_iter(mod->ctx, &i))) { |
Michal Vasko | e8b085b | 2021-09-02 08:20:08 +0200 | [diff] [blame] | 842 | if (!strcmp(mod_iter->name, mod->name) && (mod_iter != mod) && (mod_iter->latest_revision & LYS_MOD_IMPORTED_REV)) { |
| 843 | LOGVRB("Implemented module \"%s@%s\" was not and will not be imported if the revision-date is missing" |
| 844 | " in the import statement. Instead, the revision \"%s\" is imported.", mod->name, mod->revision, |
| 845 | mod_iter->revision); |
aPiecek | 6b3d542 | 2021-07-30 15:55:43 +0200 | [diff] [blame] | 846 | break; |
| 847 | } |
| 848 | } |
| 849 | |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 850 | cleanup: |
| 851 | return ret; |
| 852 | } |
| 853 | |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 854 | /** |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 855 | * @brief Check whether it may be needed to (re)compile a module from a particular dependency set |
| 856 | * and if so, add it into its dep set. |
| 857 | * |
| 858 | * Dependency set includes all modules that need to be (re)compiled in case any of the module(s) |
| 859 | * in the dep set are (re)compiled. |
| 860 | * |
| 861 | * The reason for recompilation is possible disabled nodes and updating |
| 862 | * leafref targets to point to the newly compiled modules. Using the import relation, the |
| 863 | * dependency is reflexive because of possible foreign augments and deviations, which are compiled |
| 864 | * during the target module compilation. |
| 865 | * |
| 866 | * - every module must belong to exactly one dep set |
| 867 | * - implement flag must be ignored because it can be changed during dep set compilation |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 868 | * |
| 869 | * @param[in] mod Module to process. |
| 870 | * @param[in,out] ctx_set Set with all not-yet-processed modules. |
| 871 | * @param[in,out] dep_set Current dependency set to update. |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 872 | * @param[in] aux_set Set of traversed non-compiled modules, should be empty on first call. |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 873 | * @return LY_ERR value. |
| 874 | */ |
| 875 | static LY_ERR |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 876 | lys_unres_dep_sets_create_mod_r(struct lys_module *mod, struct ly_set *ctx_set, struct ly_set *dep_set, |
| 877 | struct ly_set *aux_set) |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 878 | { |
| 879 | struct lys_module *mod2; |
| 880 | struct lysp_import *imports; |
| 881 | uint32_t i; |
| 882 | LY_ARRAY_COUNT_TYPE u, v; |
| 883 | ly_bool found; |
| 884 | |
Michal Vasko | 0bccbf1 | 2021-11-22 09:59:57 +0100 | [diff] [blame] | 885 | if (LYS_IS_SINGLE_DEP_SET(mod)) { |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 886 | /* is already in a separate dep set */ |
Michal Vasko | 87cfdba | 2022-02-22 14:13:45 +0100 | [diff] [blame] | 887 | if (!lys_has_dep_mods(mod)) { |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 888 | /* break the dep set here, no modules depend on this one */ |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 889 | return LY_SUCCESS; |
| 890 | } |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 891 | |
| 892 | if (ly_set_contains(aux_set, mod, NULL)) { |
| 893 | /* it was traversed */ |
| 894 | return LY_SUCCESS; |
| 895 | } |
| 896 | |
| 897 | /* add a new auxiliary module */ |
| 898 | LY_CHECK_RET(ly_set_add(aux_set, mod, 1, NULL)); |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 899 | } else { |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 900 | if (!ly_set_contains(ctx_set, mod, &i)) { |
| 901 | /* it was already processed */ |
| 902 | return LY_SUCCESS; |
| 903 | } |
| 904 | |
| 905 | /* remove it from the set, we are processing it now */ |
| 906 | ly_set_rm_index(ctx_set, i, NULL); |
| 907 | |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 908 | /* add a new dependent module into the dep set */ |
| 909 | LY_CHECK_RET(ly_set_add(dep_set, mod, 1, NULL)); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | /* process imports of the module and submodules */ |
| 913 | imports = mod->parsed->imports; |
| 914 | LY_ARRAY_FOR(imports, u) { |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 915 | mod2 = imports[u].module; |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 916 | LY_CHECK_RET(lys_unres_dep_sets_create_mod_r(mod2, ctx_set, dep_set, aux_set)); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 917 | } |
| 918 | LY_ARRAY_FOR(mod->parsed->includes, v) { |
| 919 | imports = mod->parsed->includes[v].submodule->imports; |
| 920 | LY_ARRAY_FOR(imports, u) { |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 921 | mod2 = imports[u].module; |
Michal Vasko | 87cfdba | 2022-02-22 14:13:45 +0100 | [diff] [blame] | 922 | if (LYS_IS_SINGLE_DEP_SET(mod2) && !lys_has_dep_mods(mod2)) { |
| 923 | /* break the dep set here, no modules depend on this one */ |
| 924 | continue; |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 925 | } |
| 926 | |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 927 | LY_CHECK_RET(lys_unres_dep_sets_create_mod_r(imports[u].module, ctx_set, dep_set, aux_set)); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 928 | } |
| 929 | } |
| 930 | |
| 931 | /* process modules and submodules importing this module */ |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 932 | for (i = 0; i < mod->ctx->list.count; ++i) { |
| 933 | mod2 = mod->ctx->list.objs[i]; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 934 | found = 0; |
| 935 | |
| 936 | imports = mod2->parsed->imports; |
| 937 | LY_ARRAY_FOR(imports, u) { |
| 938 | if (imports[u].module == mod) { |
| 939 | found = 1; |
| 940 | break; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | if (!found) { |
| 945 | LY_ARRAY_FOR(mod2->parsed->includes, v) { |
| 946 | imports = mod2->parsed->includes[v].submodule->imports; |
| 947 | LY_ARRAY_FOR(imports, u) { |
| 948 | if (imports[u].module == mod) { |
| 949 | found = 1; |
| 950 | break; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | if (found) { |
| 955 | break; |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | if (found) { |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 961 | LY_CHECK_RET(lys_unres_dep_sets_create_mod_r(mod2, ctx_set, dep_set, aux_set)); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | |
| 965 | return LY_SUCCESS; |
| 966 | } |
| 967 | |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 968 | /** |
| 969 | * @brief Add all simple modules (that have nothing to (re)compile) into separate dep sets. |
| 970 | * |
| 971 | * @param[in,out] ctx_set Set with all not-yet-processed modules. |
| 972 | * @param[in,out] main_set Set of dependency module sets. |
| 973 | * @return LY_ERR value. |
| 974 | */ |
| 975 | static LY_ERR |
| 976 | lys_unres_dep_sets_create_single(struct ly_set *ctx_set, struct ly_set *main_set) |
| 977 | { |
| 978 | LY_ERR ret = LY_SUCCESS; |
| 979 | struct lys_module *m; |
| 980 | uint32_t i = 0; |
| 981 | struct ly_set *dep_set = NULL; |
| 982 | |
| 983 | while (i < ctx_set->count) { |
| 984 | m = ctx_set->objs[i]; |
Michal Vasko | 0bccbf1 | 2021-11-22 09:59:57 +0100 | [diff] [blame] | 985 | if (LYS_IS_SINGLE_DEP_SET(m)) { |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 986 | /* remove it from the set, we are processing it now */ |
| 987 | ly_set_rm_index(ctx_set, i, NULL); |
| 988 | |
| 989 | /* this module can be in a separate dep set (but there still may be modules importing this one |
| 990 | * that depend on imports of this one in case it defines groupings) */ |
| 991 | LY_CHECK_GOTO(ret = ly_set_new(&dep_set), cleanup); |
| 992 | LY_CHECK_GOTO(ret = ly_set_add(dep_set, m, 1, NULL), cleanup); |
| 993 | LY_CHECK_GOTO(ret = ly_set_add(main_set, dep_set, 1, NULL), cleanup); |
| 994 | dep_set = NULL; |
| 995 | } else { |
| 996 | ++i; |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | cleanup: |
| 1001 | ly_set_free(dep_set, NULL); |
| 1002 | return ret; |
| 1003 | } |
| 1004 | |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1005 | LY_ERR |
Michal Vasko | 50bc09a | 2021-06-17 17:31:56 +0200 | [diff] [blame] | 1006 | lys_unres_dep_sets_create(struct ly_ctx *ctx, struct ly_set *main_set, struct lys_module *mod) |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1007 | { |
| 1008 | LY_ERR ret = LY_SUCCESS; |
| 1009 | struct lys_module *m; |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 1010 | struct ly_set *dep_set = NULL, *ctx_set = NULL, aux_set = {0}; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1011 | uint32_t i; |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 1012 | ly_bool found; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1013 | |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1014 | assert(!main_set->count); |
| 1015 | |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1016 | /* start with a duplicate set of modules that we will remove from */ |
| 1017 | LY_CHECK_GOTO(ret = ly_set_dup(&ctx->list, NULL, &ctx_set), cleanup); |
| 1018 | |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 1019 | /* first create all dep sets with single modules */ |
| 1020 | LY_CHECK_GOTO(ret = lys_unres_dep_sets_create_single(ctx_set, main_set), cleanup); |
| 1021 | |
| 1022 | if (mod && !ly_set_contains(ctx_set, mod, NULL)) { |
| 1023 | /* dep set for this module has already been created, nothing else to do */ |
| 1024 | goto cleanup; |
| 1025 | } |
| 1026 | |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1027 | while (ctx_set->count) { |
| 1028 | /* create new dep set */ |
| 1029 | LY_CHECK_GOTO(ret = ly_set_new(&dep_set), cleanup); |
| 1030 | |
| 1031 | if (mod) { |
| 1032 | /* use the module create a dep set with the rest of its dependent modules */ |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 1033 | LY_CHECK_GOTO(ret = lys_unres_dep_sets_create_mod_r(mod, ctx_set, dep_set, &aux_set), cleanup); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1034 | } else { |
| 1035 | /* use first ctx mod to create a dep set with the rest of its dependent modules */ |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 1036 | LY_CHECK_GOTO(ret = lys_unres_dep_sets_create_mod_r(ctx_set->objs[0], ctx_set, dep_set, &aux_set), cleanup); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1037 | } |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 1038 | ly_set_erase(&aux_set, NULL); |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 1039 | assert(dep_set->count); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1040 | |
| 1041 | /* check whether there is any module that will be (re)compiled */ |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 1042 | found = 0; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1043 | for (i = 0; i < dep_set->count; ++i) { |
| 1044 | m = dep_set->objs[i]; |
| 1045 | if (m->to_compile) { |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 1046 | found = 1; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1047 | break; |
| 1048 | } |
| 1049 | } |
| 1050 | |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 1051 | if (found) { |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1052 | /* if there is, all the implemented modules need to be recompiled */ |
| 1053 | for (i = 0; i < dep_set->count; ++i) { |
| 1054 | m = dep_set->objs[i]; |
| 1055 | if (m->implemented) { |
| 1056 | m->to_compile = 1; |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 1061 | /* add the dep set into main set */ |
| 1062 | LY_CHECK_GOTO(ret = ly_set_add(main_set, dep_set, 1, NULL), cleanup); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1063 | dep_set = NULL; |
| 1064 | |
| 1065 | if (mod) { |
| 1066 | /* we need dep set only for this module */ |
| 1067 | break; |
| 1068 | } |
| 1069 | } |
| 1070 | |
Michal Vasko | e558f79 | 2021-07-28 08:20:15 +0200 | [diff] [blame] | 1071 | #ifndef NDEBUG |
| 1072 | LOGDBG(LY_LDGDEPSETS, "dep sets created (%" PRIu32 "):", main_set->count); |
| 1073 | for (i = 0; i < main_set->count; ++i) { |
| 1074 | struct ly_set *iter_set = main_set->objs[i]; |
| 1075 | |
| 1076 | LOGDBG(LY_LDGDEPSETS, "dep set #%" PRIu32 ":", i); |
| 1077 | for (uint32_t j = 0; j < iter_set->count; ++j) { |
| 1078 | m = iter_set->objs[j]; |
| 1079 | LOGDBG(LY_LDGDEPSETS, "\t%s", m->name); |
| 1080 | } |
| 1081 | } |
| 1082 | #endif |
| 1083 | |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1084 | cleanup: |
Michal Vasko | bd65c2e | 2021-06-16 11:49:45 +0200 | [diff] [blame] | 1085 | assert(ret || main_set->objs); |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 1086 | ly_set_erase(&aux_set, NULL); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1087 | ly_set_free(dep_set, NULL); |
| 1088 | ly_set_free(ctx_set, NULL); |
| 1089 | return ret; |
| 1090 | } |
| 1091 | |
| 1092 | void |
| 1093 | lys_unres_glob_revert(struct ly_ctx *ctx, struct lys_glob_unres *unres) |
| 1094 | { |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 1095 | uint32_t i, j, idx, temp_lo = 0; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1096 | struct lysf_ctx fctx = {.ctx = ctx}; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1097 | struct ly_set *dep_set; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1098 | LY_ERR ret; |
| 1099 | |
| 1100 | for (i = 0; i < unres->implementing.count; ++i) { |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1101 | fctx.mod = unres->implementing.objs[i]; |
| 1102 | assert(fctx.mod->implemented); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1103 | |
| 1104 | /* make the module correctly non-implemented again */ |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1105 | fctx.mod->implemented = 0; |
| 1106 | lys_precompile_augments_deviations_revert(ctx, fctx.mod); |
| 1107 | lysc_module_free(&fctx, fctx.mod->compiled); |
| 1108 | fctx.mod->compiled = NULL; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1109 | |
| 1110 | /* should not be made implemented */ |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1111 | fctx.mod->to_compile = 0; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | for (i = 0; i < unres->creating.count; ++i) { |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1115 | fctx.mod = unres->creating.objs[i]; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1116 | |
Michal Vasko | d297f2a | 2021-06-16 11:51:28 +0200 | [diff] [blame] | 1117 | /* remove the module from the context */ |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1118 | ly_set_rm(&ctx->list, fctx.mod, NULL); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1119 | |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1120 | /* remove it also from dep sets */ |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 1121 | for (j = 0; j < unres->dep_sets.count; ++j) { |
| 1122 | dep_set = unres->dep_sets.objs[j]; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1123 | if (ly_set_contains(dep_set, fctx.mod, &idx)) { |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 1124 | ly_set_rm_index(dep_set, idx, NULL); |
| 1125 | break; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1126 | } |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1127 | } |
Michal Vasko | d297f2a | 2021-06-16 11:51:28 +0200 | [diff] [blame] | 1128 | |
| 1129 | /* free the module */ |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1130 | lys_module_free(&fctx, fctx.mod, 1); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1133 | /* remove the extensions as well */ |
| 1134 | lysf_ctx_erase(&fctx); |
| 1135 | |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1136 | if (unres->implementing.count) { |
| 1137 | /* recompile previous context because some implemented modules are no longer implemented, |
| 1138 | * we can reuse the current to_compile flags */ |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 1139 | ly_temp_log_options(&temp_lo); |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1140 | ret = lys_compile_depset_all(ctx, &ctx->unres); |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 1141 | ly_temp_log_options(NULL); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1142 | if (ret) { |
| 1143 | LOGINT(ctx); |
| 1144 | } |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | void |
| 1149 | lys_unres_glob_erase(struct lys_glob_unres *unres) |
| 1150 | { |
| 1151 | uint32_t i; |
| 1152 | |
| 1153 | for (i = 0; i < unres->dep_sets.count; ++i) { |
| 1154 | ly_set_free(unres->dep_sets.objs[i], NULL); |
| 1155 | } |
| 1156 | ly_set_erase(&unres->dep_sets, NULL); |
| 1157 | ly_set_erase(&unres->implementing, NULL); |
| 1158 | ly_set_erase(&unres->creating, NULL); |
| 1159 | |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 1160 | assert(!unres->ds_unres.whens.count); |
| 1161 | assert(!unres->ds_unres.musts.count); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1162 | assert(!unres->ds_unres.leafrefs.count); |
aPiecek | c6526b4 | 2021-07-12 15:21:39 +0200 | [diff] [blame] | 1163 | assert(!unres->ds_unres.disabled_leafrefs.count); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1164 | assert(!unres->ds_unres.dflts.count); |
| 1165 | assert(!unres->ds_unres.disabled.count); |
| 1166 | } |
| 1167 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1168 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 1169 | lys_set_implemented(struct lys_module *mod, const char **features) |
| 1170 | { |
| 1171 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1172 | struct lys_glob_unres *unres = &mod->ctx->unres; |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 1173 | |
| 1174 | LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL); |
| 1175 | |
| 1176 | /* implement */ |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1177 | ret = _lys_set_implemented(mod, features, unres); |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 1178 | LY_CHECK_GOTO(ret, cleanup); |
| 1179 | |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1180 | if (!(mod->ctx->flags & LY_CTX_EXPLICIT_COMPILE)) { |
| 1181 | /* create dep set for the module and mark all the modules that will be (re)compiled */ |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1182 | LY_CHECK_GOTO(ret = lys_unres_dep_sets_create(mod->ctx, &unres->dep_sets, mod), cleanup); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1183 | |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 1184 | /* (re)compile the whole dep set (other dep sets will have no modules marked for compilation) */ |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1185 | LY_CHECK_GOTO(ret = lys_compile_depset_all(mod->ctx, unres), cleanup); |
| 1186 | |
| 1187 | /* unres resolved */ |
| 1188 | lys_unres_glob_erase(unres); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1189 | } |
| 1190 | |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 1191 | cleanup: |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1192 | if (ret) { |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1193 | lys_unres_glob_revert(mod->ctx, unres); |
| 1194 | lys_unres_glob_erase(unres); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1195 | } |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 1196 | return ret; |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 1197 | } |
| 1198 | |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1199 | /** |
| 1200 | * @brief Resolve (find) all imported and included modules. |
| 1201 | * |
| 1202 | * @param[in] pctx Parser context. |
| 1203 | * @param[in] pmod Parsed module to resolve. |
| 1204 | * @param[out] new_mods Set with all the newly loaded modules. |
| 1205 | * @return LY_ERR value. |
| 1206 | */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1207 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1208 | lysp_resolve_import_include(struct lysp_ctx *pctx, struct lysp_module *pmod, struct ly_set *new_mods) |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1209 | { |
| 1210 | struct lysp_import *imp; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1211 | LY_ARRAY_COUNT_TYPE u, v; |
| 1212 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1213 | pmod->parsing = 1; |
| 1214 | LY_ARRAY_FOR(pmod->imports, u) { |
| 1215 | imp = &pmod->imports[u]; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1216 | if (!imp->module) { |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 1217 | LY_CHECK_RET(lys_parse_load(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, new_mods, &imp->module)); |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 1218 | |
| 1219 | if (!imp->rev[0]) { |
| 1220 | /* This module must be selected for the next similar |
| 1221 | * import without revision-date to avoid incorrect |
| 1222 | * derived identities in the ::lys_module.identities. |
| 1223 | */ |
| 1224 | imp->module->latest_revision |= LYS_MOD_IMPORTED_REV; |
| 1225 | } |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1226 | } |
| 1227 | /* check for importing the same module twice */ |
| 1228 | for (v = 0; v < u; ++v) { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1229 | if (imp->module == pmod->imports[v].module) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1230 | LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name); |
| 1231 | } |
| 1232 | } |
| 1233 | } |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 1234 | LY_CHECK_RET(lysp_load_submodules(pctx, pmod, new_mods)); |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1235 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1236 | pmod->parsing = 0; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1237 | |
| 1238 | return LY_SUCCESS; |
| 1239 | } |
| 1240 | |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1241 | /** |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1242 | * @brief Generate path of the given paresed node. |
| 1243 | * |
| 1244 | * @param[in] node Schema path of this node will be generated. |
| 1245 | * @param[in] parent Build relative path only until this parent is found. If NULL, the full absolute path is printed. |
| 1246 | * @return NULL in case of memory allocation error, path of the node otherwise. |
| 1247 | * In case the @p buffer is NULL, the returned string is dynamically allocated and caller is responsible to free it. |
| 1248 | */ |
| 1249 | static char * |
| 1250 | lysp_path_until(const struct lysp_node *node, const struct lysp_node *parent, const struct lysp_module *pmod) |
| 1251 | { |
| 1252 | const struct lysp_node *iter, *par; |
| 1253 | char *path = NULL, *s; |
| 1254 | const char *slash; |
| 1255 | int len = 0; |
| 1256 | |
| 1257 | for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) { |
| 1258 | if (parent && (iter->parent == parent)) { |
| 1259 | slash = ""; |
| 1260 | } else { |
| 1261 | slash = "/"; |
| 1262 | } |
| 1263 | |
| 1264 | s = path; |
| 1265 | par = iter->parent; |
| 1266 | if (!par) { |
| 1267 | /* print prefix */ |
| 1268 | len = asprintf(&path, "%s%s:%s%s", slash, pmod->mod->name, iter->name, s ? s : ""); |
| 1269 | } else { |
| 1270 | /* prefix is the same as in parent */ |
| 1271 | len = asprintf(&path, "%s%s%s", slash, iter->name, s ? s : ""); |
| 1272 | } |
| 1273 | free(s); |
| 1274 | } |
| 1275 | |
| 1276 | if (len < 0) { |
| 1277 | free(path); |
| 1278 | path = NULL; |
| 1279 | } else if (len == 0) { |
| 1280 | path = strdup("/"); |
| 1281 | } |
| 1282 | |
| 1283 | return path; |
| 1284 | } |
| 1285 | |
| 1286 | /** |
| 1287 | * @brief Build log path for a parsed extension instance. |
| 1288 | * |
| 1289 | * @param[in] pcxt Parse context. |
| 1290 | * @param[in] ext Parsed extension instance. |
| 1291 | * @param[out] path Generated path. |
| 1292 | * @return LY_ERR value. |
| 1293 | */ |
| 1294 | static LY_ERR |
| 1295 | lysp_resolve_ext_instance_log_path(const struct lysp_ctx *pctx, const struct lysp_ext_instance *ext, char **path) |
| 1296 | { |
| 1297 | char *buf = NULL; |
| 1298 | uint32_t used = 0, size = 0; |
| 1299 | |
| 1300 | if (ext->parent_stmt & LY_STMT_NODE_MASK) { |
| 1301 | /* parsed node path */ |
| 1302 | buf = lysp_path_until(ext->parent, NULL, PARSER_CUR_PMOD(pctx)); |
| 1303 | LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(pctx)), LY_EMEM); |
| 1304 | size = used = strlen(buf); |
| 1305 | |
| 1306 | /* slash */ |
| 1307 | size += 1; |
| 1308 | buf = realloc(buf, size + 1); |
| 1309 | LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(pctx)), LY_EMEM); |
| 1310 | used += sprintf(buf + used, "/"); |
| 1311 | } else { |
| 1312 | /* module */ |
| 1313 | size += 1 + strlen(PARSER_CUR_PMOD(pctx)->mod->name) + 1; |
| 1314 | buf = realloc(buf, size + 1); |
| 1315 | LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(pctx)), LY_EMEM); |
| 1316 | used += sprintf(buf + used, "/%s:", PARSER_CUR_PMOD(pctx)->mod->name); |
| 1317 | } |
| 1318 | |
| 1319 | /* extension name */ |
| 1320 | size += 12 + strlen(ext->name) + 2; |
| 1321 | buf = realloc(buf, size + 1); |
| 1322 | LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(pctx)), LY_EMEM); |
| 1323 | used += sprintf(buf + used, "{extension='%s'}", ext->name); |
| 1324 | |
| 1325 | /* extension argument */ |
| 1326 | if (ext->argument) { |
| 1327 | size += 1 + strlen(ext->argument); |
| 1328 | buf = realloc(buf, size + 1); |
| 1329 | LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(pctx)), LY_EMEM); |
| 1330 | used += sprintf(buf + used, "/%s", ext->argument); |
| 1331 | } |
| 1332 | |
| 1333 | *path = buf; |
| 1334 | return LY_SUCCESS; |
| 1335 | } |
| 1336 | |
| 1337 | /** |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1338 | * @brief Resolve (find) all extension instance records and finish their parsing. |
| 1339 | * |
| 1340 | * @param[in] pctx Parse context with all the parsed extension instances. |
| 1341 | * @return LY_ERR value. |
| 1342 | */ |
| 1343 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1344 | lysp_resolve_ext_instance_records(struct lysp_ctx *pctx) |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1345 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1346 | LY_ERR r; |
| 1347 | struct lysf_ctx fctx = {.ctx = PARSER_CTX(pctx)}; |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1348 | struct lysp_ext_instance *exts, *ext; |
| 1349 | const struct lys_module *mod; |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1350 | uint32_t i; |
| 1351 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1352 | char *path = NULL; |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1353 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1354 | /* first finish parsing all extension instances ... */ |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1355 | for (i = 0; i < pctx->ext_inst.count; ++i) { |
| 1356 | exts = pctx->ext_inst.objs[i]; |
| 1357 | LY_ARRAY_FOR(exts, u) { |
| 1358 | ext = &exts[u]; |
| 1359 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1360 | /* find the extension definition */ |
| 1361 | LY_CHECK_RET(lysp_ext_find_definition(PARSER_CTX(pctx), ext, &mod, &ext->def)); |
| 1362 | |
| 1363 | /* resolve the argument, if needed */ |
| 1364 | LY_CHECK_RET(lysp_ext_instance_resolve_argument(PARSER_CTX(pctx), ext)); |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1365 | |
| 1366 | /* find the extension record, if any */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1367 | ext->record = lyplg_ext_record_find(mod->name, mod->revision, ext->def->name); |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | /* ... then call the parse callback */ |
| 1372 | for (i = 0; i < pctx->ext_inst.count; ++i) { |
| 1373 | exts = pctx->ext_inst.objs[i]; |
| 1374 | u = 0; |
| 1375 | while (u < LY_ARRAY_COUNT(exts)) { |
| 1376 | ext = &exts[u]; |
| 1377 | if (!ext->record || !ext->record->plugin.parse) { |
| 1378 | goto next_iter; |
| 1379 | } |
| 1380 | |
| 1381 | /* set up log path */ |
| 1382 | if ((r = lysp_resolve_ext_instance_log_path(pctx, ext, &path))) { |
| 1383 | return r; |
| 1384 | } |
Michal Vasko | f8ebf13 | 2022-11-21 14:06:48 +0100 | [diff] [blame] | 1385 | LOG_LOCSET(NULL, NULL, path, NULL); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1386 | |
| 1387 | /* parse */ |
| 1388 | r = ext->record->plugin.parse(pctx, ext); |
| 1389 | |
| 1390 | LOG_LOCBACK(0, 0, 1, 0); |
| 1391 | free(path); |
| 1392 | |
| 1393 | if (r == LY_ENOT) { |
| 1394 | /* instance should be ignored, remove it */ |
| 1395 | lysp_ext_instance_free(&fctx, ext); |
| 1396 | LY_ARRAY_DECREMENT(exts); |
| 1397 | if (u < LY_ARRAY_COUNT(exts)) { |
| 1398 | /* replace by the last item */ |
| 1399 | *ext = exts[LY_ARRAY_COUNT(exts)]; |
| 1400 | } /* else if there are no more items, leave the empty array, we are not able to free it */ |
| 1401 | continue; |
| 1402 | } else if (r) { |
| 1403 | /* error */ |
| 1404 | return r; |
| 1405 | } |
| 1406 | |
| 1407 | next_iter: |
| 1408 | ++u; |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | return LY_SUCCESS; |
| 1413 | } |
| 1414 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1415 | LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1416 | lys_parse_submodule(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, struct lysp_ctx *main_ctx, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1417 | LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *), |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 1418 | void *check_data, struct ly_set *new_mods, struct lysp_submodule **submodule) |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1419 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1420 | LY_ERR ret; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1421 | struct lysp_submodule *submod = NULL, *latest_sp; |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1422 | struct lysp_yang_ctx *yangctx = NULL; |
| 1423 | struct lysp_yin_ctx *yinctx = NULL; |
| 1424 | struct lysp_ctx *pctx; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1425 | struct lysf_ctx fctx = {.ctx = ctx}; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1426 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1427 | LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1428 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1429 | switch (format) { |
| 1430 | case LYS_IN_YIN: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1431 | ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod); |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1432 | pctx = (struct lysp_ctx *)yinctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1433 | break; |
| 1434 | case LYS_IN_YANG: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1435 | ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod); |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1436 | pctx = (struct lysp_ctx *)yangctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1437 | break; |
| 1438 | default: |
David Sedlák | 4f2f5ba | 2019-08-15 13:18:48 +0200 | [diff] [blame] | 1439 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
Radek Krejci | 82fa8d4 | 2020-07-11 22:00:59 +0200 | [diff] [blame] | 1440 | ret = LY_EINVAL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1441 | break; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1442 | } |
Radek Krejci | f6923e8 | 2020-07-02 16:36:53 +0200 | [diff] [blame] | 1443 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | f027df7 | 2020-09-15 13:00:28 +0200 | [diff] [blame] | 1444 | assert(submod); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1445 | |
| 1446 | /* make sure that the newest revision is at position 0 */ |
| 1447 | lysp_sort_revisions(submod->revs); |
| 1448 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1449 | /* decide the latest revision */ |
Michal Vasko | 8dc3199 | 2021-02-22 10:30:47 +0100 | [diff] [blame] | 1450 | latest_sp = (struct lysp_submodule *)ly_ctx_get_submodule2_latest(submod->mod, submod->name); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1451 | if (latest_sp) { |
| 1452 | if (submod->revs) { |
| 1453 | if (!latest_sp->revs) { |
| 1454 | /* latest has no revision, so mod is anyway newer */ |
| 1455 | submod->latest_revision = latest_sp->latest_revision; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1456 | /* the latest_sp is zeroed later when the new module is being inserted into the context */ |
| 1457 | } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) { |
| 1458 | submod->latest_revision = latest_sp->latest_revision; |
| 1459 | /* 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] | 1460 | } else { |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1461 | latest_sp = NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1462 | } |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1463 | } else { |
| 1464 | latest_sp = NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1465 | } |
| 1466 | } else { |
| 1467 | submod->latest_revision = 1; |
| 1468 | } |
| 1469 | |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1470 | if (custom_check) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1471 | LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | if (latest_sp) { |
| 1475 | latest_sp->latest_revision = 0; |
| 1476 | } |
| 1477 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1478 | lys_parser_fill_filepath(ctx, in, &submod->filepath); |
| 1479 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1480 | /* resolve imports and includes */ |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1481 | LY_CHECK_GOTO(ret = lysp_resolve_import_include(pctx, (struct lysp_module *)submod, new_mods), error); |
| 1482 | |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1483 | if (format == LYS_IN_YANG) { |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1484 | lysp_yang_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1485 | } else { |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1486 | lysp_yin_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1487 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1488 | *submodule = submod; |
| 1489 | return LY_SUCCESS; |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1490 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1491 | error: |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1492 | if (!submod || !submod->name) { |
| 1493 | LOGERR(ctx, ret, "Parsing submodule failed."); |
| 1494 | } else { |
| 1495 | LOGERR(ctx, ret, "Parsing submodule \"%s\" failed.", submod->name); |
| 1496 | } |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1497 | lysp_module_free(&fctx, (struct lysp_module *)submod); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1498 | if (format == LYS_IN_YANG) { |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1499 | lysp_yang_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1500 | } else { |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1501 | lysp_yin_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1502 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1503 | return ret; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1504 | } |
| 1505 | |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1506 | /** |
| 1507 | * @brief Add ietf-netconf metadata to the parsed module. Operation, filter, and select are added. |
| 1508 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1509 | * @param[in] pctx Parse context. |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1510 | * @param[in] mod Parsed module to add to. |
| 1511 | * @return LY_SUCCESS on success. |
| 1512 | * @return LY_ERR on error. |
| 1513 | */ |
| 1514 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1515 | lysp_add_internal_ietf_netconf(struct lysp_ctx *pctx, struct lysp_module *mod) |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1516 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1517 | struct lysp_ext_instance *extp; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1518 | struct lysp_stmt *stmt; |
| 1519 | struct lysp_import *imp; |
| 1520 | |
| 1521 | /* |
| 1522 | * 1) edit-config's operation |
| 1523 | */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1524 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, extp, LY_EMEM); |
| 1525 | LY_CHECK_ERR_RET(!extp, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1526 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &extp->name)); |
| 1527 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "operation", 0, &extp->argument)); |
| 1528 | extp->format = LY_VALUE_SCHEMA; |
| 1529 | extp->prefix_data = mod; |
| 1530 | extp->parent = mod; |
| 1531 | extp->parent_stmt = LY_STMT_MODULE; |
| 1532 | extp->flags = LYS_INTERNAL; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1533 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1534 | extp->child = stmt = calloc(1, sizeof *extp->child); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1535 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1536 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt)); |
| 1537 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1538 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1539 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1540 | stmt->kw = LY_STMT_TYPE; |
| 1541 | |
| 1542 | stmt->child = calloc(1, sizeof *stmt->child); |
| 1543 | stmt = stmt->child; |
| 1544 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1545 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1546 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "merge", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1547 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1548 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1549 | stmt->kw = LY_STMT_ENUM; |
| 1550 | |
| 1551 | stmt->next = calloc(1, sizeof *stmt->child); |
| 1552 | stmt = stmt->next; |
| 1553 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1554 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1555 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "replace", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1556 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1557 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1558 | stmt->kw = LY_STMT_ENUM; |
| 1559 | |
| 1560 | stmt->next = calloc(1, sizeof *stmt->child); |
| 1561 | stmt = stmt->next; |
| 1562 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1563 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1564 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "create", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1565 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1566 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1567 | stmt->kw = LY_STMT_ENUM; |
| 1568 | |
| 1569 | stmt->next = calloc(1, sizeof *stmt->child); |
| 1570 | stmt = stmt->next; |
| 1571 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1572 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1573 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "delete", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1574 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1575 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1576 | stmt->kw = LY_STMT_ENUM; |
| 1577 | |
| 1578 | stmt->next = calloc(1, sizeof *stmt->child); |
| 1579 | stmt = stmt->next; |
| 1580 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1581 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1582 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "remove", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1583 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1584 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1585 | stmt->kw = LY_STMT_ENUM; |
| 1586 | |
| 1587 | /* |
| 1588 | * 2) filter's type |
| 1589 | */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1590 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, extp, LY_EMEM); |
| 1591 | LY_CHECK_ERR_RET(!extp, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1592 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &extp->name)); |
| 1593 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &extp->argument)); |
| 1594 | extp->format = LY_VALUE_SCHEMA; |
| 1595 | extp->prefix_data = mod; |
| 1596 | extp->parent = mod; |
| 1597 | extp->parent_stmt = LY_STMT_MODULE; |
| 1598 | extp->flags = LYS_INTERNAL; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1599 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1600 | extp->child = stmt = calloc(1, sizeof *extp->child); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1601 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1602 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt)); |
| 1603 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1604 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1605 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1606 | stmt->kw = LY_STMT_TYPE; |
| 1607 | |
| 1608 | stmt->child = calloc(1, sizeof *stmt->child); |
| 1609 | stmt = stmt->child; |
| 1610 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1611 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1612 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "subtree", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1613 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1614 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1615 | stmt->kw = LY_STMT_ENUM; |
| 1616 | |
| 1617 | stmt->next = calloc(1, sizeof *stmt->child); |
| 1618 | stmt = stmt->next; |
| 1619 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1620 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1621 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1622 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1623 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1624 | stmt->kw = LY_STMT_ENUM; |
| 1625 | |
| 1626 | /* if-feature for enum allowed only for YANG 1.1 modules */ |
| 1627 | if (mod->version >= LYS_VERSION_1_1) { |
| 1628 | stmt->child = calloc(1, sizeof *stmt->child); |
| 1629 | stmt = stmt->child; |
| 1630 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1631 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "if-feature", 0, &stmt->stmt)); |
| 1632 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1633 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1634 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1635 | stmt->kw = LY_STMT_IF_FEATURE; |
| 1636 | } |
| 1637 | |
| 1638 | /* |
| 1639 | * 3) filter's select |
| 1640 | */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1641 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, extp, LY_EMEM); |
| 1642 | LY_CHECK_ERR_RET(!extp, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1643 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &extp->name)); |
| 1644 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "select", 0, &extp->argument)); |
| 1645 | extp->format = LY_VALUE_SCHEMA; |
| 1646 | extp->prefix_data = mod; |
| 1647 | extp->parent = mod; |
| 1648 | extp->parent_stmt = LY_STMT_MODULE; |
| 1649 | extp->flags = LYS_INTERNAL; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1650 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1651 | extp->child = stmt = calloc(1, sizeof *extp->child); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1652 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1653 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt)); |
| 1654 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_:xpath1.0", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1655 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1656 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1657 | stmt->kw = LY_STMT_TYPE; |
| 1658 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1659 | if (LY_ARRAY_COUNT(mod->exts) == 3) { |
| 1660 | /* first extension instances */ |
Michal Vasko | c8806c8 | 2022-12-06 10:31:24 +0100 | [diff] [blame] | 1661 | assert(pctx->main_ctx == pctx); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1662 | LY_CHECK_RET(ly_set_add(&pctx->ext_inst, mod->exts, 1, NULL)); |
| 1663 | } |
| 1664 | |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1665 | /* create new imports for the used prefixes */ |
| 1666 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1667 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name)); |
| 1668 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix)); |
| 1669 | imp->flags = LYS_INTERNAL; |
| 1670 | |
| 1671 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1672 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-types", 0, &imp->name)); |
| 1673 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_", 0, &imp->prefix)); |
| 1674 | imp->flags = LYS_INTERNAL; |
| 1675 | |
| 1676 | return LY_SUCCESS; |
| 1677 | } |
| 1678 | |
| 1679 | /** |
| 1680 | * @brief Add ietf-netconf-with-defaults "default" metadata to the parsed module. |
| 1681 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1682 | * @param[in] pctx Parse context. |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1683 | * @param[in] mod Parsed module to add to. |
| 1684 | * @return LY_SUCCESS on success. |
| 1685 | * @return LY_ERR on error. |
| 1686 | */ |
| 1687 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1688 | lysp_add_internal_ietf_netconf_with_defaults(struct lysp_ctx *pctx, struct lysp_module *mod) |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1689 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1690 | struct lysp_ext_instance *extp; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1691 | struct lysp_stmt *stmt; |
| 1692 | struct lysp_import *imp; |
| 1693 | |
| 1694 | /* add new extension instance */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1695 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, extp, LY_EMEM); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1696 | |
| 1697 | /* fill in the extension instance fields */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1698 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &extp->name)); |
| 1699 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "default", 0, &extp->argument)); |
| 1700 | extp->format = LY_VALUE_SCHEMA; |
| 1701 | extp->prefix_data = mod; |
| 1702 | extp->parent = mod; |
| 1703 | extp->parent_stmt = LY_STMT_MODULE; |
| 1704 | extp->flags = LYS_INTERNAL; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1705 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1706 | extp->child = stmt = calloc(1, sizeof *extp->child); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1707 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1708 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt)); |
| 1709 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "boolean", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1710 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1711 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1712 | stmt->kw = LY_STMT_TYPE; |
| 1713 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1714 | if (LY_ARRAY_COUNT(mod->exts) == 1) { |
| 1715 | /* first extension instance */ |
Michal Vasko | c8806c8 | 2022-12-06 10:31:24 +0100 | [diff] [blame] | 1716 | assert(pctx->main_ctx == pctx); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1717 | LY_CHECK_RET(ly_set_add(&pctx->ext_inst, mod->exts, 1, NULL)); |
| 1718 | } |
| 1719 | |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1720 | /* create new import for the used prefix */ |
| 1721 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1722 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name)); |
| 1723 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix)); |
| 1724 | imp->flags = LYS_INTERNAL; |
| 1725 | |
| 1726 | return LY_SUCCESS; |
| 1727 | } |
| 1728 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1729 | LY_ERR |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 1730 | lys_parse_in(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1731 | LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data), |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 1732 | void *check_data, struct ly_set *new_mods, struct lys_module **module) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1733 | { |
Michal Vasko | a9309bb | 2021-07-09 09:31:55 +0200 | [diff] [blame] | 1734 | struct lys_module *mod = NULL, *latest, *mod_dup = NULL; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1735 | LY_ERR ret; |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1736 | struct lysp_yang_ctx *yangctx = NULL; |
| 1737 | struct lysp_yin_ctx *yinctx = NULL; |
| 1738 | struct lysp_ctx *pctx = NULL; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1739 | struct lysf_ctx fctx = {.ctx = ctx}; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1740 | char *filename, *rev, *dot; |
| 1741 | size_t len; |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1742 | ly_bool module_created = 0; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1743 | |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 1744 | assert(ctx && in && new_mods); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1745 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1746 | if (module) { |
| 1747 | *module = NULL; |
| 1748 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1749 | |
| 1750 | mod = calloc(1, sizeof *mod); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1751 | LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1752 | mod->ctx = ctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1753 | |
Michal Vasko | 8a69a1b | 2020-12-03 14:19:02 +0100 | [diff] [blame] | 1754 | /* parse */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1755 | switch (format) { |
| 1756 | case LYS_IN_YIN: |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 1757 | ret = yin_parse_module(&yinctx, in, mod); |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1758 | pctx = (struct lysp_ctx *)yinctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1759 | break; |
| 1760 | case LYS_IN_YANG: |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 1761 | ret = yang_parse_module(&yangctx, in, mod); |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1762 | pctx = (struct lysp_ctx *)yangctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1763 | break; |
| 1764 | default: |
| 1765 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1766 | ret = LY_EINVAL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1767 | break; |
| 1768 | } |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1769 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1770 | |
| 1771 | /* make sure that the newest revision is at position 0 */ |
| 1772 | lysp_sort_revisions(mod->parsed->revs); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1773 | if (mod->parsed->revs) { |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1774 | LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), cleanup); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1775 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1776 | |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1777 | /* decide the latest revision */ |
Michal Vasko | a51ef07 | 2021-07-02 10:40:30 +0200 | [diff] [blame] | 1778 | latest = ly_ctx_get_module_latest(ctx, mod->name); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1779 | if (latest) { |
| 1780 | if (mod->revision) { |
| 1781 | if (!latest->revision) { |
| 1782 | /* latest has no revision, so mod is anyway newer */ |
aPiecek | 8ca21bd | 2021-07-26 14:31:01 +0200 | [diff] [blame] | 1783 | mod->latest_revision = latest->latest_revision & (LYS_MOD_LATEST_REV | LYS_MOD_LATEST_SEARCHDIRS); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1784 | /* the latest is zeroed later when the new module is being inserted into the context */ |
| 1785 | } else if (strcmp(mod->revision, latest->revision) > 0) { |
aPiecek | 8ca21bd | 2021-07-26 14:31:01 +0200 | [diff] [blame] | 1786 | mod->latest_revision = latest->latest_revision & (LYS_MOD_LATEST_REV | LYS_MOD_LATEST_SEARCHDIRS); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1787 | /* the latest is zeroed later when the new module is being inserted into the context */ |
| 1788 | } else { |
| 1789 | latest = NULL; |
| 1790 | } |
| 1791 | } else { |
| 1792 | latest = NULL; |
| 1793 | } |
| 1794 | } else { |
aPiecek | 8ca21bd | 2021-07-26 14:31:01 +0200 | [diff] [blame] | 1795 | mod->latest_revision = LYS_MOD_LATEST_REV; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1796 | } |
| 1797 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1798 | if (custom_check) { |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1799 | LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), cleanup); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1800 | } |
| 1801 | |
Michal Vasko | 8a69a1b | 2020-12-03 14:19:02 +0100 | [diff] [blame] | 1802 | /* check whether it is not already in the context in the same revision */ |
Michal Vasko | a51ef07 | 2021-07-02 10:40:30 +0200 | [diff] [blame] | 1803 | mod_dup = ly_ctx_get_module(ctx, mod->name, mod->revision); |
Michal Vasko | 8a69a1b | 2020-12-03 14:19:02 +0100 | [diff] [blame] | 1804 | if (mod_dup) { |
Michal Vasko | 8a69a1b | 2020-12-03 14:19:02 +0100 | [diff] [blame] | 1805 | /* nothing to do */ |
| 1806 | LOGVRB("Module \"%s@%s\" is already present in the context.", mod_dup->name, |
| 1807 | mod_dup->revision ? mod_dup->revision : "<none>"); |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1808 | goto cleanup; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1809 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1810 | |
Michal Vasko | b24145d | 2022-07-13 18:34:39 +0200 | [diff] [blame] | 1811 | /* check whether there is not a namespace collision */ |
| 1812 | mod_dup = ly_ctx_get_module_latest_ns(ctx, mod->ns); |
| 1813 | if (mod_dup && (mod_dup->revision == mod->revision)) { |
| 1814 | LOGERR(ctx, LY_EINVAL, "Two different modules (\"%s\" and \"%s\") have the same namespace \"%s\".", |
| 1815 | mod_dup->name, mod->name, mod->ns); |
| 1816 | ret = LY_EINVAL; |
| 1817 | goto cleanup; |
| 1818 | } |
| 1819 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1820 | switch (in->type) { |
| 1821 | case LY_IN_FILEPATH: |
| 1822 | /* check that name and revision match filename */ |
| 1823 | filename = strrchr(in->method.fpath.filepath, '/'); |
| 1824 | if (!filename) { |
| 1825 | filename = in->method.fpath.filepath; |
| 1826 | } else { |
| 1827 | filename++; |
| 1828 | } |
| 1829 | rev = strchr(filename, '@'); |
| 1830 | dot = strrchr(filename, '.'); |
| 1831 | |
| 1832 | /* name */ |
| 1833 | len = strlen(mod->name); |
| 1834 | if (strncmp(filename, mod->name, len) || |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1835 | ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1836 | LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name); |
| 1837 | } |
| 1838 | if (rev) { |
| 1839 | len = dot - ++rev; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1840 | if (!mod->parsed->revs || (len != LY_REV_SIZE - 1) || strncmp(mod->parsed->revs[0].date, rev, len)) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1841 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1842 | mod->parsed->revs ? mod->parsed->revs[0].date : "none"); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | break; |
| 1847 | case LY_IN_FD: |
| 1848 | case LY_IN_FILE: |
| 1849 | case LY_IN_MEMORY: |
| 1850 | /* nothing special to do */ |
| 1851 | break; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1852 | case LY_IN_ERROR: |
| 1853 | LOGINT(ctx); |
| 1854 | ret = LY_EINT; |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1855 | goto cleanup; |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1856 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1857 | lys_parser_fill_filepath(ctx, in, &mod->filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1858 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1859 | if (latest) { |
aPiecek | 8ca21bd | 2021-07-26 14:31:01 +0200 | [diff] [blame] | 1860 | latest->latest_revision &= ~(LYS_MOD_LATEST_REV | LYS_MOD_LATEST_SEARCHDIRS); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1861 | } |
| 1862 | |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1863 | /* add internal data in case specific modules were parsed */ |
| 1864 | if (!strcmp(mod->name, "ietf-netconf")) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1865 | LY_CHECK_GOTO(ret = lysp_add_internal_ietf_netconf(pctx, mod->parsed), cleanup); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1866 | } else if (!strcmp(mod->name, "ietf-netconf-with-defaults")) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1867 | LY_CHECK_GOTO(ret = lysp_add_internal_ietf_netconf_with_defaults(pctx, mod->parsed), cleanup); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1868 | } |
| 1869 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1870 | /* add the module into newly created module set, will also be freed from there on any error */ |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1871 | LY_CHECK_GOTO(ret = ly_set_add(new_mods, mod, 1, NULL), cleanup); |
| 1872 | module_created = 1; |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1873 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1874 | /* add into context */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 1875 | ret = ly_set_add(&ctx->list, mod, 1, NULL); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1876 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 794ab4b | 2021-03-31 09:42:19 +0200 | [diff] [blame] | 1877 | ctx->change_count++; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1878 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1879 | /* resolve includes and all imports */ |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1880 | LY_CHECK_GOTO(ret = lysp_resolve_import_include(pctx, mod->parsed, new_mods), cleanup); |
| 1881 | |
| 1882 | /* resolve extension instance plugin records */ |
| 1883 | LY_CHECK_GOTO(ret = lysp_resolve_ext_instance_records(pctx), cleanup); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1884 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1885 | /* check name collisions */ |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1886 | LY_CHECK_GOTO(ret = lysp_check_dup_typedefs(pctx, mod->parsed), cleanup); |
aPiecek | 63e080d | 2021-06-29 13:53:28 +0200 | [diff] [blame] | 1887 | LY_CHECK_GOTO(ret = lysp_check_dup_groupings(pctx, mod->parsed), cleanup); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1888 | LY_CHECK_GOTO(ret = lysp_check_dup_features(pctx, mod->parsed), cleanup); |
| 1889 | LY_CHECK_GOTO(ret = lysp_check_dup_identities(pctx, mod->parsed), cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1890 | |
| 1891 | /* compile features */ |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1892 | LY_CHECK_GOTO(ret = lys_compile_feature_iffeatures(mod->parsed), cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1893 | |
aPiecek | 6b3d542 | 2021-07-30 15:55:43 +0200 | [diff] [blame] | 1894 | /* compile identities */ |
| 1895 | LY_CHECK_GOTO(ret = lys_compile_identities(mod), cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1896 | |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1897 | cleanup: |
Michal Vasko | be04af4 | 2021-07-16 10:48:43 +0200 | [diff] [blame] | 1898 | if (ret && (ret != LY_EEXIST)) { |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1899 | if (mod && mod->name) { |
| 1900 | /* there are cases when path is not available for parsing error, so this additional |
| 1901 | * message tries to add information about the module where the error occurred */ |
| 1902 | struct ly_err_item *e = ly_err_last(ctx); |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 1903 | |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1904 | if (e && (!e->path || !strncmp(e->path, "Line ", ly_strlen_const("Line ")))) { |
| 1905 | LOGERR(ctx, ret, "Parsing module \"%s\" failed.", mod->name); |
| 1906 | } |
| 1907 | } |
| 1908 | } |
| 1909 | if (!module_created) { |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1910 | fctx.mod = mod; |
| 1911 | lys_module_free(&fctx, mod, 0); |
| 1912 | lysf_ctx_erase(&fctx); |
| 1913 | |
Michal Vasko | 0e02e8e | 2021-02-26 15:01:55 +0100 | [diff] [blame] | 1914 | mod = mod_dup; |
| 1915 | } |
Michal Vasko | 8a69a1b | 2020-12-03 14:19:02 +0100 | [diff] [blame] | 1916 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1917 | if (format == LYS_IN_YANG) { |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1918 | lysp_yang_ctx_free(yangctx); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1919 | } else { |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1920 | lysp_yin_ctx_free(yinctx); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1921 | } |
| 1922 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1923 | if (!ret && module) { |
| 1924 | *module = mod; |
| 1925 | } |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1926 | return ret; |
| 1927 | } |
| 1928 | |
Radek Krejci | 545b487 | 2020-11-15 10:15:12 +0100 | [diff] [blame] | 1929 | static LYS_INFORMAT |
| 1930 | lys_parse_get_format(const struct ly_in *in, LYS_INFORMAT format) |
| 1931 | { |
| 1932 | if (!format && (in->type == LY_IN_FILEPATH)) { |
| 1933 | /* unknown format - try to detect it from filename's suffix */ |
| 1934 | const char *path = in->method.fpath.filepath; |
| 1935 | size_t len = strlen(path); |
| 1936 | |
| 1937 | /* ignore trailing whitespaces */ |
| 1938 | for ( ; len > 0 && isspace(path[len - 1]); len--) {} |
| 1939 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1940 | if ((len >= LY_YANG_SUFFIX_LEN + 1) && |
| 1941 | !strncmp(&path[len - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX, LY_YANG_SUFFIX_LEN)) { |
Radek Krejci | 545b487 | 2020-11-15 10:15:12 +0100 | [diff] [blame] | 1942 | format = LYS_IN_YANG; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1943 | } else if ((len >= LY_YIN_SUFFIX_LEN + 1) && |
| 1944 | !strncmp(&path[len - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX, LY_YIN_SUFFIX_LEN)) { |
Radek Krejci | 545b487 | 2020-11-15 10:15:12 +0100 | [diff] [blame] | 1945 | format = LYS_IN_YIN; |
| 1946 | } /* else still unknown */ |
| 1947 | } |
| 1948 | |
| 1949 | return format; |
| 1950 | } |
| 1951 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1952 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 1953 | lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const char **features, struct lys_module **module) |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1954 | { |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 1955 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 1956 | struct lys_module *mod; |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1957 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1958 | if (module) { |
| 1959 | *module = NULL; |
| 1960 | } |
Radek Krejci | 545b487 | 2020-11-15 10:15:12 +0100 | [diff] [blame] | 1961 | LY_CHECK_ARG_RET(NULL, ctx, in, LY_EINVAL); |
| 1962 | |
| 1963 | format = lys_parse_get_format(in, format); |
| 1964 | LY_CHECK_ARG_RET(ctx, format, LY_EINVAL); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1965 | |
| 1966 | /* remember input position */ |
| 1967 | in->func_start = in->current; |
| 1968 | |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 1969 | /* parse */ |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1970 | ret = lys_parse_in(ctx, in, format, NULL, NULL, &ctx->unres.creating, &mod); |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 1971 | LY_CHECK_GOTO(ret, cleanup); |
| 1972 | |
| 1973 | /* implement */ |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1974 | ret = _lys_set_implemented(mod, features, &ctx->unres); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1975 | LY_CHECK_GOTO(ret, cleanup); |
| 1976 | |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1977 | if (!(ctx->flags & LY_CTX_EXPLICIT_COMPILE)) { |
| 1978 | /* create dep set for the module and mark all the modules that will be (re)compiled */ |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1979 | LY_CHECK_GOTO(ret = lys_unres_dep_sets_create(ctx, &ctx->unres.dep_sets, mod), cleanup); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1980 | |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 1981 | /* (re)compile the whole dep set (other dep sets will have no modules marked for compilation) */ |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1982 | LY_CHECK_GOTO(ret = lys_compile_depset_all(ctx, &ctx->unres), cleanup); |
| 1983 | |
| 1984 | /* unres resolved */ |
| 1985 | lys_unres_glob_erase(&ctx->unres); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1986 | } |
| 1987 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1988 | cleanup: |
| 1989 | if (ret) { |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1990 | lys_unres_glob_revert(ctx, &ctx->unres); |
| 1991 | lys_unres_glob_erase(&ctx->unres); |
Michal Vasko | 87f1cf0 | 2021-06-08 14:02:47 +0200 | [diff] [blame] | 1992 | } else if (module) { |
| 1993 | *module = mod; |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1994 | } |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1995 | return ret; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1996 | } |
| 1997 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1998 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 1999 | lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, struct lys_module **module) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 2000 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2001 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 2002 | struct ly_in *in = NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 2003 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2004 | LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL); |
Radek Krejci | 65639b9 | 2018-11-27 10:51:37 +0100 | [diff] [blame] | 2005 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2006 | 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] | 2007 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 2008 | ret = lys_parse(ctx, in, format, NULL, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 2009 | ly_in_free(in, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 2010 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2011 | return ret; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2012 | } |
| 2013 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2014 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 2015 | lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, struct lys_module **module) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 2016 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2017 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 2018 | struct ly_in *in = NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 2019 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2020 | 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] | 2021 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2022 | 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] | 2023 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 2024 | ret = lys_parse(ctx, in, format, NULL, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 2025 | ly_in_free(in, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 2026 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2027 | return ret; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 2028 | } |
| 2029 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2030 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 2031 | lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, struct lys_module **module) |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2032 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2033 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 2034 | struct ly_in *in = NULL; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 2035 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2036 | LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 2037 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2038 | LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2039 | 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] | 2040 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 2041 | ret = lys_parse(ctx, in, format, NULL, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 2042 | ly_in_free(in, 0); |
| 2043 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2044 | return ret; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2045 | } |
| 2046 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2047 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2048 | 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] | 2049 | char **localfile, LYS_INFORMAT *format) |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2050 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2051 | LY_ERR ret = LY_EMEM; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2052 | size_t len, flen, match_len = 0, dir_len; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2053 | ly_bool implicit_cwd = 0; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2054 | char *wd, *wn = NULL; |
| 2055 | DIR *dir = NULL; |
| 2056 | struct dirent *file; |
| 2057 | char *match_name = NULL; |
| 2058 | LYS_INFORMAT format_aux, match_format = 0; |
| 2059 | struct ly_set *dirs; |
| 2060 | struct stat st; |
| 2061 | |
| 2062 | LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL); |
| 2063 | |
| 2064 | /* start to fill the dir fifo with the context's search path (if set) |
| 2065 | * and the current working directory */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2066 | LY_CHECK_RET(ly_set_new(&dirs)); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2067 | |
| 2068 | len = strlen(name); |
| 2069 | if (cwd) { |
| 2070 | wd = get_current_dir_name(); |
| 2071 | if (!wd) { |
| 2072 | LOGMEM(NULL); |
| 2073 | goto cleanup; |
| 2074 | } else { |
| 2075 | /* add implicit current working directory (./) to be searched, |
| 2076 | * this directory is not searched recursively */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2077 | ret = ly_set_add(dirs, wd, 0, NULL); |
| 2078 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2079 | implicit_cwd = 1; |
| 2080 | } |
| 2081 | } |
| 2082 | if (searchpaths) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2083 | for (uint64_t i = 0; searchpaths[i]; i++) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2084 | /* check for duplicities with the implicit current working directory */ |
| 2085 | if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) { |
| 2086 | implicit_cwd = 0; |
| 2087 | continue; |
| 2088 | } |
| 2089 | wd = strdup(searchpaths[i]); |
| 2090 | if (!wd) { |
| 2091 | LOGMEM(NULL); |
| 2092 | goto cleanup; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2093 | } else { |
| 2094 | ret = ly_set_add(dirs, wd, 0, NULL); |
| 2095 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2096 | } |
| 2097 | } |
| 2098 | } |
| 2099 | wd = NULL; |
| 2100 | |
| 2101 | /* start searching */ |
| 2102 | while (dirs->count) { |
| 2103 | free(wd); |
| 2104 | free(wn); wn = NULL; |
| 2105 | |
| 2106 | dirs->count--; |
| 2107 | wd = (char *)dirs->objs[dirs->count]; |
| 2108 | dirs->objs[dirs->count] = NULL; |
Radek Krejci | eeee95c | 2021-01-19 10:57:22 +0100 | [diff] [blame] | 2109 | LOGVRB("Searching for \"%s\" in \"%s\".", name, wd); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2110 | |
| 2111 | if (dir) { |
| 2112 | closedir(dir); |
| 2113 | } |
| 2114 | dir = opendir(wd); |
| 2115 | dir_len = strlen(wd); |
| 2116 | if (!dir) { |
| 2117 | LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno)); |
| 2118 | } else { |
| 2119 | while ((file = readdir(dir))) { |
| 2120 | if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) { |
| 2121 | /* skip . and .. */ |
| 2122 | continue; |
| 2123 | } |
| 2124 | free(wn); |
| 2125 | if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) { |
| 2126 | LOGMEM(NULL); |
| 2127 | goto cleanup; |
| 2128 | } |
| 2129 | if (stat(wn, &st) == -1) { |
| 2130 | 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] | 2131 | file->d_name, wd, strerror(errno)); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2132 | continue; |
| 2133 | } |
| 2134 | if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) { |
| 2135 | /* we have another subdirectory in searchpath to explore, |
| 2136 | * 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] | 2137 | ret = ly_set_add(dirs, wn, 0, NULL); |
| 2138 | LY_CHECK_GOTO(ret, cleanup); |
| 2139 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2140 | /* continue with the next item in current directory */ |
| 2141 | wn = NULL; |
| 2142 | continue; |
| 2143 | } else if (!S_ISREG(st.st_mode)) { |
| 2144 | /* not a regular file (note that we see the target of symlinks instead of symlinks */ |
| 2145 | continue; |
| 2146 | } |
| 2147 | |
| 2148 | /* here we know that the item is a file which can contain a module */ |
| 2149 | if (strncmp(name, file->d_name, len) || |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2150 | ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2151 | /* different filename than the module we search for */ |
| 2152 | continue; |
| 2153 | } |
| 2154 | |
| 2155 | /* get type according to filename suffix */ |
| 2156 | flen = strlen(file->d_name); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 2157 | if ((flen >= LY_YANG_SUFFIX_LEN + 1) && |
| 2158 | !strcmp(&file->d_name[flen - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX)) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2159 | format_aux = LYS_IN_YANG; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 2160 | } else if ((flen >= LY_YIN_SUFFIX_LEN + 1) && |
| 2161 | !strcmp(&file->d_name[flen - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX)) { |
Radek Krejci | 01a937f | 2020-11-15 10:14:12 +0100 | [diff] [blame] | 2162 | format_aux = LYS_IN_YIN; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2163 | } else { |
| 2164 | /* not supportde suffix/file format */ |
| 2165 | continue; |
| 2166 | } |
| 2167 | |
| 2168 | if (revision) { |
| 2169 | /* we look for the specific revision, try to get it from the filename */ |
| 2170 | if (file->d_name[len] == '@') { |
| 2171 | /* check revision from the filename */ |
| 2172 | if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) { |
| 2173 | /* another revision */ |
| 2174 | continue; |
| 2175 | } else { |
| 2176 | /* exact revision */ |
| 2177 | free(match_name); |
| 2178 | match_name = wn; |
| 2179 | wn = NULL; |
| 2180 | match_len = dir_len + 1 + len; |
| 2181 | match_format = format_aux; |
| 2182 | goto success; |
| 2183 | } |
| 2184 | } else { |
| 2185 | /* continue trying to find exact revision match, use this only if not found */ |
| 2186 | free(match_name); |
| 2187 | match_name = wn; |
| 2188 | wn = NULL; |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 2189 | match_len = dir_len + 1 + len; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2190 | match_format = format_aux; |
| 2191 | continue; |
| 2192 | } |
| 2193 | } else { |
| 2194 | /* remember the revision and try to find the newest one */ |
| 2195 | if (match_name) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2196 | if ((file->d_name[len] != '@') || |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 2197 | lysp_check_date(NULL, &file->d_name[len + 1], |
| 2198 | flen - ((format_aux == LYS_IN_YANG) ? LY_YANG_SUFFIX_LEN : LY_YIN_SUFFIX_LEN) - len - 1, NULL)) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2199 | continue; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2200 | } else if ((match_name[match_len] == '@') && |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2201 | (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) { |
| 2202 | continue; |
| 2203 | } |
| 2204 | free(match_name); |
| 2205 | } |
| 2206 | |
| 2207 | match_name = wn; |
| 2208 | wn = NULL; |
| 2209 | match_len = dir_len + 1 + len; |
| 2210 | match_format = format_aux; |
| 2211 | continue; |
| 2212 | } |
| 2213 | } |
| 2214 | } |
| 2215 | } |
| 2216 | |
| 2217 | success: |
| 2218 | (*localfile) = match_name; |
| 2219 | match_name = NULL; |
| 2220 | if (format) { |
| 2221 | (*format) = match_format; |
| 2222 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2223 | ret = LY_SUCCESS; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2224 | |
| 2225 | cleanup: |
| 2226 | free(wn); |
| 2227 | free(wd); |
| 2228 | if (dir) { |
| 2229 | closedir(dir); |
| 2230 | } |
| 2231 | free(match_name); |
| 2232 | ly_set_free(dirs, free); |
| 2233 | |
| 2234 | return ret; |
| 2235 | } |