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 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 30 | #include "compat.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 31 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 32 | #include "dict.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 33 | #include "in.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 34 | #include "in_internal.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 35 | #include "log.h" |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame] | 36 | #include "ly_common.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; |
Michal Vasko | 0b1d076 | 2023-07-10 10:11:22 +0200 | [diff] [blame] | 445 | struct lyxp_set xp_set = {0}; |
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 | |
Michal Vasko | 0b1d076 | 2023-07-10 10:11:22 +0200 | [diff] [blame] | 458 | /* allocate return set */ |
| 459 | ret = ly_set_new(set); |
| 460 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 461 | |
| 462 | /* compile expression */ |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 463 | ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 464 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 465 | |
| 466 | /* atomize expression */ |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 467 | 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] | 468 | LY_CHECK_GOTO(ret, cleanup); |
| 469 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 470 | /* transform into ly_set */ |
| 471 | (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs); |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 472 | LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 473 | (*set)->size = xp_set.used; |
| 474 | |
| 475 | for (i = 0; i < xp_set.used; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 476 | if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 477 | 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] | 478 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
| 482 | cleanup: |
| 483 | lyxp_set_free_content(&xp_set); |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 484 | lyxp_expr_free(ctx, exp); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 485 | return ret; |
| 486 | } |
| 487 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 488 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 489 | lys_find_expr_atoms(const struct lysc_node *ctx_node, const struct lys_module *cur_mod, const struct lyxp_expr *expr, |
| 490 | const struct lysc_prefix *prefixes, uint32_t options, struct ly_set **set) |
| 491 | { |
| 492 | LY_ERR ret = LY_SUCCESS; |
| 493 | struct lyxp_set xp_set = {0}; |
| 494 | uint32_t i; |
| 495 | |
| 496 | LY_CHECK_ARG_RET(NULL, cur_mod, expr, prefixes, set, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 497 | 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] | 498 | if (!(options & LYXP_SCNODE_ALL)) { |
| 499 | options = LYXP_SCNODE; |
| 500 | } |
| 501 | |
Michal Vasko | 0b1d076 | 2023-07-10 10:11:22 +0200 | [diff] [blame] | 502 | /* allocate return set */ |
| 503 | ret = ly_set_new(set); |
| 504 | LY_CHECK_GOTO(ret, cleanup); |
| 505 | |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 506 | /* atomize expression */ |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 507 | ret = lyxp_atomize(cur_mod->ctx, expr, cur_mod, LY_VALUE_SCHEMA_RESOLVED, (void *)prefixes, ctx_node, ctx_node, |
| 508 | &xp_set, options); |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 509 | LY_CHECK_GOTO(ret, cleanup); |
| 510 | |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 511 | /* transform into ly_set */ |
| 512 | (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs); |
| 513 | LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(cur_mod->ctx); ret = LY_EMEM, cleanup); |
| 514 | (*set)->size = xp_set.used; |
| 515 | |
| 516 | for (i = 0; i < xp_set.used; ++i) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 517 | if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx >= LYXP_SET_SCNODE_ATOM_NODE)) { |
| 518 | assert((xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) || |
| 519 | (xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL) || |
Michal Vasko | d97959c | 2020-12-10 12:18:28 +0100 | [diff] [blame] | 520 | (xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX)); |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 521 | ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL); |
| 522 | LY_CHECK_GOTO(ret, cleanup); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | cleanup: |
| 527 | lyxp_set_free_content(&xp_set); |
| 528 | if (ret) { |
| 529 | ly_set_free(*set, NULL); |
| 530 | *set = NULL; |
| 531 | } |
| 532 | return ret; |
| 533 | } |
| 534 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 535 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 536 | lys_find_xpath(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options, |
| 537 | struct ly_set **set) |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 538 | { |
| 539 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 540 | struct lyxp_set xp_set = {0}; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 541 | struct lyxp_expr *exp = NULL; |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 542 | uint32_t i; |
| 543 | |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 544 | LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 545 | 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] | 546 | if (!(options & LYXP_SCNODE_ALL)) { |
| 547 | options = LYXP_SCNODE; |
| 548 | } |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 549 | if (!ctx) { |
| 550 | ctx = ctx_node->module->ctx; |
| 551 | } |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 552 | |
Michal Vasko | 0b1d076 | 2023-07-10 10:11:22 +0200 | [diff] [blame] | 553 | /* allocate return set */ |
| 554 | ret = ly_set_new(set); |
| 555 | LY_CHECK_GOTO(ret, cleanup); |
| 556 | |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 557 | /* compile expression */ |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 558 | ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 559 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 560 | |
| 561 | /* atomize expression */ |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 562 | 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] | 563 | LY_CHECK_GOTO(ret, cleanup); |
| 564 | |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 565 | /* transform into ly_set */ |
| 566 | (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs); |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 567 | LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 568 | (*set)->size = xp_set.used; |
| 569 | |
| 570 | for (i = 0; i < xp_set.used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 571 | 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] | 572 | 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] | 573 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
| 577 | cleanup: |
| 578 | lyxp_set_free_content(&xp_set); |
Michal Vasko | 2651268 | 2021-01-11 11:35:40 +0100 | [diff] [blame] | 579 | lyxp_expr_free(ctx, exp); |
Michal Vasko | ae15966 | 2020-10-21 11:57:24 +0200 | [diff] [blame] | 580 | if (ret) { |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 581 | ly_set_free(*set, NULL); |
| 582 | *set = NULL; |
| 583 | } |
Michal Vasko | 072de48 | 2020-08-05 13:27:21 +0200 | [diff] [blame] | 584 | return ret; |
| 585 | } |
| 586 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 587 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 588 | lys_find_lypath_atoms(const struct ly_path *path, struct ly_set **set) |
| 589 | { |
| 590 | LY_ERR ret = LY_SUCCESS; |
| 591 | LY_ARRAY_COUNT_TYPE u, v; |
| 592 | |
| 593 | LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL); |
| 594 | |
| 595 | /* allocate return set */ |
| 596 | LY_CHECK_RET(ly_set_new(set)); |
| 597 | |
| 598 | LY_ARRAY_FOR(path, u) { |
| 599 | /* add nodes from the path */ |
| 600 | 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] | 601 | LY_ARRAY_FOR(path[u].predicates, v) { |
| 602 | 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] | 603 | /* add all the keys in a predicate */ |
| 604 | LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup); |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | cleanup: |
| 610 | if (ret) { |
| 611 | ly_set_free(*set, NULL); |
| 612 | *set = NULL; |
| 613 | } |
| 614 | return ret; |
| 615 | } |
| 616 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 617 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 618 | lys_find_path_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output, |
| 619 | struct ly_set **set) |
| 620 | { |
| 621 | LY_ERR ret = LY_SUCCESS; |
| 622 | uint8_t oper; |
| 623 | struct lyxp_expr *expr = NULL; |
| 624 | struct ly_path *p = NULL; |
| 625 | |
| 626 | LY_CHECK_ARG_RET(ctx, ctx || ctx_node, path, set, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 627 | 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] | 628 | |
| 629 | if (!ctx) { |
| 630 | ctx = ctx_node->module->ctx; |
| 631 | } |
| 632 | |
| 633 | /* parse */ |
Michal Vasko | 4f7c53d | 2023-04-17 08:45:53 +0200 | [diff] [blame] | 634 | ret = ly_path_parse(ctx, ctx_node, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_FIRST, |
| 635 | LY_PATH_PRED_SIMPLE, &expr); |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 636 | LY_CHECK_GOTO(ret, cleanup); |
| 637 | |
| 638 | /* compile */ |
| 639 | oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT; |
Michal Vasko | 0884d21 | 2021-10-14 09:21:46 +0200 | [diff] [blame] | 640 | 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] | 641 | LY_CHECK_GOTO(ret, cleanup); |
| 642 | |
| 643 | /* resolve */ |
| 644 | ret = lys_find_lypath_atoms(p, set); |
| 645 | |
| 646 | cleanup: |
| 647 | ly_path_free(ctx, p); |
| 648 | lyxp_expr_free(ctx, expr); |
| 649 | return ret; |
| 650 | } |
| 651 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 652 | LIBYANG_API_DEF const struct lysc_node * |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 653 | lys_find_path(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output) |
| 654 | { |
| 655 | const struct lysc_node *snode = NULL; |
Michal Vasko | 4f7c53d | 2023-04-17 08:45:53 +0200 | [diff] [blame] | 656 | struct lyxp_expr *expr = NULL; |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 657 | struct ly_path *p = NULL; |
| 658 | LY_ERR ret; |
| 659 | uint8_t oper; |
| 660 | |
| 661 | LY_CHECK_ARG_RET(ctx, ctx || ctx_node, NULL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 662 | 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] | 663 | |
| 664 | if (!ctx) { |
| 665 | ctx = ctx_node->module->ctx; |
| 666 | } |
| 667 | |
| 668 | /* parse */ |
Michal Vasko | 4f7c53d | 2023-04-17 08:45:53 +0200 | [diff] [blame] | 669 | ret = ly_path_parse(ctx, ctx_node, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_FIRST, |
| 670 | LY_PATH_PRED_SIMPLE, &expr); |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 671 | LY_CHECK_GOTO(ret, cleanup); |
| 672 | |
| 673 | /* compile */ |
| 674 | oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT; |
Michal Vasko | 4f7c53d | 2023-04-17 08:45:53 +0200 | [diff] [blame] | 675 | 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] | 676 | LY_CHECK_GOTO(ret, cleanup); |
| 677 | |
| 678 | /* get last node */ |
| 679 | snode = p[LY_ARRAY_COUNT(p) - 1].node; |
| 680 | |
| 681 | cleanup: |
| 682 | ly_path_free(ctx, p); |
Michal Vasko | 4f7c53d | 2023-04-17 08:45:53 +0200 | [diff] [blame] | 683 | lyxp_expr_free(ctx, expr); |
Radek Krejci | bc5644c | 2020-10-27 14:53:17 +0100 | [diff] [blame] | 684 | return snode; |
| 685 | } |
| 686 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 687 | char * |
| 688 | 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] | 689 | size_t buflen) |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 690 | { |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 691 | const struct lysc_node *iter, *par, *key; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 692 | char *path = NULL; |
| 693 | int len = 0; |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 694 | ly_bool skip_schema; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 695 | |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 696 | if (buffer) { |
| 697 | LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL); |
Michal Vasko | 770d3fc | 2021-01-26 09:14:35 +0100 | [diff] [blame] | 698 | buffer[0] = '\0'; |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 699 | } |
| 700 | |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 701 | if ((pathtype == LYSC_PATH_DATA) || (pathtype == LYSC_PATH_DATA_PATTERN)) { |
| 702 | /* skip schema-only nodes */ |
| 703 | skip_schema = 1; |
| 704 | } else { |
| 705 | skip_schema = 0; |
| 706 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 707 | |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 708 | for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) { |
| 709 | char *s; |
| 710 | const char *slash; |
Michal Vasko | 65de040 | 2020-08-03 16:34:19 +0200 | [diff] [blame] | 711 | |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 712 | if (skip_schema && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))) { |
| 713 | /* schema-only node */ |
| 714 | continue; |
| 715 | } |
Michal Vasko | 87dd134 | 2021-08-23 16:17:55 +0200 | [diff] [blame] | 716 | |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 717 | if ((pathtype == LYSC_PATH_DATA_PATTERN) && (iter->nodetype == LYS_LIST)) { |
| 718 | key = NULL; |
| 719 | while ((key = lys_getnext(key, iter, NULL, 0)) && lysc_is_key(key)) { |
| 720 | s = buffer ? strdup(buffer) : path; |
Michal Vasko | 87dd134 | 2021-08-23 16:17:55 +0200 | [diff] [blame] | 721 | |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 722 | /* print key predicate */ |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 723 | if (buffer) { |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 724 | len = snprintf(buffer, buflen, "[%s='%%s']%s", key->name, s ? s : ""); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 725 | } else { |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 726 | len = asprintf(&path, "[%s='%%s']%s", key->name, s ? s : ""); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 727 | } |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 728 | free(s); |
Michal Vasko | 28d0381 | 2022-03-24 15:26:11 +0100 | [diff] [blame] | 729 | |
| 730 | if (buffer && (buflen <= (size_t)len)) { |
| 731 | /* not enough space in buffer */ |
| 732 | break; |
| 733 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 734 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 735 | } |
| 736 | |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 737 | s = buffer ? strdup(buffer) : path; |
| 738 | if (parent && (iter->parent == parent)) { |
| 739 | slash = ""; |
| 740 | } else { |
| 741 | slash = "/"; |
| 742 | } |
| 743 | |
| 744 | if (skip_schema) { |
| 745 | par = lysc_data_parent(iter); |
| 746 | } else { |
| 747 | par = iter->parent; |
| 748 | } |
| 749 | |
| 750 | if (!par || (par->module != iter->module)) { |
| 751 | /* print prefix */ |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 752 | if (buffer) { |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 753 | 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] | 754 | } else { |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 755 | len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, iter->name, s ? s : ""); |
| 756 | } |
| 757 | } else { |
| 758 | /* prefix is the same as in parent */ |
| 759 | if (buffer) { |
| 760 | len = snprintf(buffer, buflen, "%s%s%s", slash, iter->name, s ? s : ""); |
| 761 | } else { |
| 762 | len = asprintf(&path, "%s%s%s", slash, iter->name, s ? s : ""); |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 763 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 764 | } |
Michal Vasko | bde22b6 | 2022-03-22 15:32:56 +0100 | [diff] [blame] | 765 | free(s); |
| 766 | |
| 767 | if (buffer && (buflen <= (size_t)len)) { |
| 768 | /* not enough space in buffer */ |
| 769 | break; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | if (len < 0) { |
| 774 | free(path); |
| 775 | path = NULL; |
| 776 | } else if (len == 0) { |
| 777 | if (buffer) { |
| 778 | strcpy(buffer, "/"); |
| 779 | } else { |
| 780 | path = strdup("/"); |
| 781 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 782 | } |
| 783 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 784 | if (buffer) { |
| 785 | return buffer; |
| 786 | } else { |
| 787 | return path; |
| 788 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 789 | } |
| 790 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 791 | LIBYANG_API_DEF char * |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 792 | lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen) |
| 793 | { |
| 794 | return lysc_path_until(node, NULL, pathtype, buffer, buflen); |
| 795 | } |
| 796 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 797 | LY_ERR |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 798 | _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] | 799 | { |
| 800 | LY_ERR ret = LY_SUCCESS, r; |
aPiecek | 6b3d542 | 2021-07-30 15:55:43 +0200 | [diff] [blame] | 801 | struct lys_module *mod_iter; |
Michal Vasko | c56d637 | 2021-10-19 12:29:00 +0200 | [diff] [blame] | 802 | const char **imp_f, *all_f[] = {"*", NULL}; |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 803 | uint32_t i; |
Michal Vasko | 916aefb | 2020-11-02 15:43:16 +0100 | [diff] [blame] | 804 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 805 | if (mod->implemented) { |
| 806 | /* mod is already implemented, set the features */ |
| 807 | r = lys_set_features(mod->parsed, features); |
| 808 | if (r == LY_EEXIST) { |
| 809 | /* no changes */ |
| 810 | return LY_SUCCESS; |
Michal Vasko | 7213ac5 | 2021-06-15 11:52:13 +0200 | [diff] [blame] | 811 | } else if (!r) { |
| 812 | /* mark the module as changed */ |
Michal Vasko | 01db7de | 2021-04-16 12:23:30 +0200 | [diff] [blame] | 813 | mod->to_compile = 1; |
Michal Vasko | 01db7de | 2021-04-16 12:23:30 +0200 | [diff] [blame] | 814 | } |
Michal Vasko | 7213ac5 | 2021-06-15 11:52:13 +0200 | [diff] [blame] | 815 | |
| 816 | return r; |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 817 | } |
Michal Vasko | 08c8b27 | 2020-11-24 18:11:30 +0100 | [diff] [blame] | 818 | |
Michal Vasko | 7213ac5 | 2021-06-15 11:52:13 +0200 | [diff] [blame] | 819 | /* implement, ignore recompilation because it must always take place later */ |
| 820 | r = lys_implement(mod, features, unres); |
| 821 | LY_CHECK_ERR_GOTO(r && (r != LY_ERECOMPILE), ret = r, cleanup); |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 822 | |
| 823 | if (mod->ctx->flags & LY_CTX_ALL_IMPLEMENTED) { |
| 824 | /* implement all the imports as well */ |
| 825 | for (i = 0; i < unres->creating.count; ++i) { |
| 826 | mod = unres->creating.objs[i]; |
| 827 | if (mod->implemented) { |
| 828 | continue; |
| 829 | } |
| 830 | |
Michal Vasko | c56d637 | 2021-10-19 12:29:00 +0200 | [diff] [blame] | 831 | imp_f = (mod->ctx->flags & LY_CTX_ENABLE_IMP_FEATURES) ? all_f : NULL; |
| 832 | r = lys_implement(mod, imp_f, unres); |
Michal Vasko | 7213ac5 | 2021-06-15 11:52:13 +0200 | [diff] [blame] | 833 | LY_CHECK_ERR_GOTO(r && (r != LY_ERECOMPILE), ret = r, cleanup); |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
aPiecek | 6b3d542 | 2021-07-30 15:55:43 +0200 | [diff] [blame] | 837 | /* Try to find module with LYS_MOD_IMPORTED_REV flag. */ |
| 838 | i = 0; |
| 839 | while ((mod_iter = ly_ctx_get_module_iter(mod->ctx, &i))) { |
Michal Vasko | e8b085b | 2021-09-02 08:20:08 +0200 | [diff] [blame] | 840 | if (!strcmp(mod_iter->name, mod->name) && (mod_iter != mod) && (mod_iter->latest_revision & LYS_MOD_IMPORTED_REV)) { |
| 841 | LOGVRB("Implemented module \"%s@%s\" was not and will not be imported if the revision-date is missing" |
| 842 | " in the import statement. Instead, the revision \"%s\" is imported.", mod->name, mod->revision, |
| 843 | mod_iter->revision); |
aPiecek | 6b3d542 | 2021-07-30 15:55:43 +0200 | [diff] [blame] | 844 | break; |
| 845 | } |
| 846 | } |
| 847 | |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 848 | cleanup: |
| 849 | return ret; |
| 850 | } |
| 851 | |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 852 | /** |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 853 | * @brief Check whether it may be needed to (re)compile a module from a particular dependency set |
| 854 | * and if so, add it into its dep set. |
| 855 | * |
| 856 | * Dependency set includes all modules that need to be (re)compiled in case any of the module(s) |
| 857 | * in the dep set are (re)compiled. |
| 858 | * |
| 859 | * The reason for recompilation is possible disabled nodes and updating |
| 860 | * leafref targets to point to the newly compiled modules. Using the import relation, the |
| 861 | * dependency is reflexive because of possible foreign augments and deviations, which are compiled |
| 862 | * during the target module compilation. |
| 863 | * |
| 864 | * - every module must belong to exactly one dep set |
| 865 | * - 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] | 866 | * |
| 867 | * @param[in] mod Module to process. |
| 868 | * @param[in,out] ctx_set Set with all not-yet-processed modules. |
| 869 | * @param[in,out] dep_set Current dependency set to update. |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 870 | * @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] | 871 | * @return LY_ERR value. |
| 872 | */ |
| 873 | static LY_ERR |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 874 | lys_unres_dep_sets_create_mod_r(struct lys_module *mod, struct ly_set *ctx_set, struct ly_set *dep_set, |
| 875 | struct ly_set *aux_set) |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 876 | { |
| 877 | struct lys_module *mod2; |
| 878 | struct lysp_import *imports; |
| 879 | uint32_t i; |
| 880 | LY_ARRAY_COUNT_TYPE u, v; |
| 881 | ly_bool found; |
| 882 | |
Michal Vasko | 0bccbf1 | 2021-11-22 09:59:57 +0100 | [diff] [blame] | 883 | if (LYS_IS_SINGLE_DEP_SET(mod)) { |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 884 | /* is already in a separate dep set */ |
Michal Vasko | 87cfdba | 2022-02-22 14:13:45 +0100 | [diff] [blame] | 885 | if (!lys_has_dep_mods(mod)) { |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 886 | /* break the dep set here, no modules depend on this one */ |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 887 | return LY_SUCCESS; |
| 888 | } |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 889 | |
| 890 | if (ly_set_contains(aux_set, mod, NULL)) { |
| 891 | /* it was traversed */ |
| 892 | return LY_SUCCESS; |
| 893 | } |
| 894 | |
| 895 | /* add a new auxiliary module */ |
| 896 | LY_CHECK_RET(ly_set_add(aux_set, mod, 1, NULL)); |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 897 | } else { |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 898 | if (!ly_set_contains(ctx_set, mod, &i)) { |
| 899 | /* it was already processed */ |
| 900 | return LY_SUCCESS; |
| 901 | } |
| 902 | |
| 903 | /* remove it from the set, we are processing it now */ |
| 904 | ly_set_rm_index(ctx_set, i, NULL); |
| 905 | |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 906 | /* add a new dependent module into the dep set */ |
| 907 | LY_CHECK_RET(ly_set_add(dep_set, mod, 1, NULL)); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | /* process imports of the module and submodules */ |
| 911 | imports = mod->parsed->imports; |
| 912 | LY_ARRAY_FOR(imports, u) { |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 913 | mod2 = imports[u].module; |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 914 | 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] | 915 | } |
| 916 | LY_ARRAY_FOR(mod->parsed->includes, v) { |
| 917 | imports = mod->parsed->includes[v].submodule->imports; |
| 918 | LY_ARRAY_FOR(imports, u) { |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 919 | mod2 = imports[u].module; |
Michal Vasko | 87cfdba | 2022-02-22 14:13:45 +0100 | [diff] [blame] | 920 | if (LYS_IS_SINGLE_DEP_SET(mod2) && !lys_has_dep_mods(mod2)) { |
| 921 | /* break the dep set here, no modules depend on this one */ |
| 922 | continue; |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 923 | } |
| 924 | |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 925 | 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] | 926 | } |
| 927 | } |
| 928 | |
| 929 | /* process modules and submodules importing this module */ |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 930 | for (i = 0; i < mod->ctx->list.count; ++i) { |
| 931 | mod2 = mod->ctx->list.objs[i]; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 932 | found = 0; |
| 933 | |
| 934 | imports = mod2->parsed->imports; |
| 935 | LY_ARRAY_FOR(imports, u) { |
| 936 | if (imports[u].module == mod) { |
| 937 | found = 1; |
| 938 | break; |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | if (!found) { |
| 943 | LY_ARRAY_FOR(mod2->parsed->includes, v) { |
| 944 | imports = mod2->parsed->includes[v].submodule->imports; |
| 945 | LY_ARRAY_FOR(imports, u) { |
| 946 | if (imports[u].module == mod) { |
| 947 | found = 1; |
| 948 | break; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | if (found) { |
| 953 | break; |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | if (found) { |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 959 | 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] | 960 | } |
| 961 | } |
| 962 | |
| 963 | return LY_SUCCESS; |
| 964 | } |
| 965 | |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 966 | /** |
| 967 | * @brief Add all simple modules (that have nothing to (re)compile) into separate dep sets. |
| 968 | * |
| 969 | * @param[in,out] ctx_set Set with all not-yet-processed modules. |
| 970 | * @param[in,out] main_set Set of dependency module sets. |
| 971 | * @return LY_ERR value. |
| 972 | */ |
| 973 | static LY_ERR |
| 974 | lys_unres_dep_sets_create_single(struct ly_set *ctx_set, struct ly_set *main_set) |
| 975 | { |
| 976 | LY_ERR ret = LY_SUCCESS; |
| 977 | struct lys_module *m; |
| 978 | uint32_t i = 0; |
| 979 | struct ly_set *dep_set = NULL; |
| 980 | |
| 981 | while (i < ctx_set->count) { |
| 982 | m = ctx_set->objs[i]; |
Michal Vasko | 0bccbf1 | 2021-11-22 09:59:57 +0100 | [diff] [blame] | 983 | if (LYS_IS_SINGLE_DEP_SET(m)) { |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 984 | /* remove it from the set, we are processing it now */ |
| 985 | ly_set_rm_index(ctx_set, i, NULL); |
| 986 | |
| 987 | /* this module can be in a separate dep set (but there still may be modules importing this one |
| 988 | * that depend on imports of this one in case it defines groupings) */ |
| 989 | LY_CHECK_GOTO(ret = ly_set_new(&dep_set), cleanup); |
| 990 | LY_CHECK_GOTO(ret = ly_set_add(dep_set, m, 1, NULL), cleanup); |
| 991 | LY_CHECK_GOTO(ret = ly_set_add(main_set, dep_set, 1, NULL), cleanup); |
| 992 | dep_set = NULL; |
| 993 | } else { |
| 994 | ++i; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | cleanup: |
| 999 | ly_set_free(dep_set, NULL); |
| 1000 | return ret; |
| 1001 | } |
| 1002 | |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1003 | LY_ERR |
Michal Vasko | 50bc09a | 2021-06-17 17:31:56 +0200 | [diff] [blame] | 1004 | 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] | 1005 | { |
| 1006 | LY_ERR ret = LY_SUCCESS; |
| 1007 | struct lys_module *m; |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 1008 | struct ly_set *dep_set = NULL, *ctx_set = NULL, aux_set = {0}; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1009 | uint32_t i; |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 1010 | ly_bool found; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1011 | |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1012 | assert(!main_set->count); |
| 1013 | |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1014 | /* start with a duplicate set of modules that we will remove from */ |
| 1015 | LY_CHECK_GOTO(ret = ly_set_dup(&ctx->list, NULL, &ctx_set), cleanup); |
| 1016 | |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 1017 | /* first create all dep sets with single modules */ |
| 1018 | LY_CHECK_GOTO(ret = lys_unres_dep_sets_create_single(ctx_set, main_set), cleanup); |
| 1019 | |
| 1020 | if (mod && !ly_set_contains(ctx_set, mod, NULL)) { |
| 1021 | /* dep set for this module has already been created, nothing else to do */ |
| 1022 | goto cleanup; |
| 1023 | } |
| 1024 | |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1025 | while (ctx_set->count) { |
| 1026 | /* create new dep set */ |
| 1027 | LY_CHECK_GOTO(ret = ly_set_new(&dep_set), cleanup); |
| 1028 | |
| 1029 | if (mod) { |
| 1030 | /* 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] | 1031 | 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] | 1032 | } else { |
| 1033 | /* 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] | 1034 | 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] | 1035 | } |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 1036 | ly_set_erase(&aux_set, NULL); |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 1037 | assert(dep_set->count); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1038 | |
| 1039 | /* check whether there is any module that will be (re)compiled */ |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 1040 | found = 0; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1041 | for (i = 0; i < dep_set->count; ++i) { |
| 1042 | m = dep_set->objs[i]; |
| 1043 | if (m->to_compile) { |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 1044 | found = 1; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1045 | break; |
| 1046 | } |
| 1047 | } |
| 1048 | |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 1049 | if (found) { |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1050 | /* if there is, all the implemented modules need to be recompiled */ |
| 1051 | for (i = 0; i < dep_set->count; ++i) { |
| 1052 | m = dep_set->objs[i]; |
| 1053 | if (m->implemented) { |
| 1054 | m->to_compile = 1; |
| 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 1059 | /* add the dep set into main set */ |
| 1060 | 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] | 1061 | dep_set = NULL; |
| 1062 | |
| 1063 | if (mod) { |
| 1064 | /* we need dep set only for this module */ |
| 1065 | break; |
| 1066 | } |
| 1067 | } |
| 1068 | |
Michal Vasko | e558f79 | 2021-07-28 08:20:15 +0200 | [diff] [blame] | 1069 | #ifndef NDEBUG |
| 1070 | LOGDBG(LY_LDGDEPSETS, "dep sets created (%" PRIu32 "):", main_set->count); |
| 1071 | for (i = 0; i < main_set->count; ++i) { |
| 1072 | struct ly_set *iter_set = main_set->objs[i]; |
| 1073 | |
| 1074 | LOGDBG(LY_LDGDEPSETS, "dep set #%" PRIu32 ":", i); |
| 1075 | for (uint32_t j = 0; j < iter_set->count; ++j) { |
| 1076 | m = iter_set->objs[j]; |
| 1077 | LOGDBG(LY_LDGDEPSETS, "\t%s", m->name); |
| 1078 | } |
| 1079 | } |
| 1080 | #endif |
| 1081 | |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1082 | cleanup: |
Michal Vasko | bd65c2e | 2021-06-16 11:49:45 +0200 | [diff] [blame] | 1083 | assert(ret || main_set->objs); |
Michal Vasko | 775fbd0 | 2021-07-28 08:25:29 +0200 | [diff] [blame] | 1084 | ly_set_erase(&aux_set, NULL); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1085 | ly_set_free(dep_set, NULL); |
| 1086 | ly_set_free(ctx_set, NULL); |
| 1087 | return ret; |
| 1088 | } |
| 1089 | |
| 1090 | void |
| 1091 | lys_unres_glob_revert(struct ly_ctx *ctx, struct lys_glob_unres *unres) |
| 1092 | { |
Michal Vasko | 59004e3 | 2024-01-30 16:09:54 +0100 | [diff] [blame] | 1093 | uint32_t i, j, idx, *prev_lo, temp_lo = 0; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1094 | struct lysf_ctx fctx = {.ctx = ctx}; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1095 | struct ly_set *dep_set; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1096 | LY_ERR ret; |
| 1097 | |
| 1098 | for (i = 0; i < unres->implementing.count; ++i) { |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1099 | fctx.mod = unres->implementing.objs[i]; |
| 1100 | assert(fctx.mod->implemented); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1101 | |
| 1102 | /* make the module correctly non-implemented again */ |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1103 | fctx.mod->implemented = 0; |
| 1104 | lys_precompile_augments_deviations_revert(ctx, fctx.mod); |
| 1105 | lysc_module_free(&fctx, fctx.mod->compiled); |
| 1106 | fctx.mod->compiled = NULL; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1107 | |
| 1108 | /* should not be made implemented */ |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1109 | fctx.mod->to_compile = 0; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | for (i = 0; i < unres->creating.count; ++i) { |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1113 | fctx.mod = unres->creating.objs[i]; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1114 | |
Michal Vasko | d297f2a | 2021-06-16 11:51:28 +0200 | [diff] [blame] | 1115 | /* remove the module from the context */ |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1116 | ly_set_rm(&ctx->list, fctx.mod, NULL); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1117 | |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1118 | /* remove it also from dep sets */ |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 1119 | for (j = 0; j < unres->dep_sets.count; ++j) { |
| 1120 | dep_set = unres->dep_sets.objs[j]; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1121 | if (ly_set_contains(dep_set, fctx.mod, &idx)) { |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 1122 | ly_set_rm_index(dep_set, idx, NULL); |
| 1123 | break; |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1124 | } |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1125 | } |
Michal Vasko | d297f2a | 2021-06-16 11:51:28 +0200 | [diff] [blame] | 1126 | |
| 1127 | /* free the module */ |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1128 | lys_module_free(&fctx, fctx.mod, 1); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1129 | } |
| 1130 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1131 | /* remove the extensions as well */ |
| 1132 | lysf_ctx_erase(&fctx); |
| 1133 | |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1134 | if (unres->implementing.count) { |
| 1135 | /* recompile previous context because some implemented modules are no longer implemented, |
| 1136 | * we can reuse the current to_compile flags */ |
Michal Vasko | 59004e3 | 2024-01-30 16:09:54 +0100 | [diff] [blame] | 1137 | prev_lo = ly_temp_log_options(&temp_lo); |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1138 | ret = lys_compile_depset_all(ctx, &ctx->unres); |
Michal Vasko | 59004e3 | 2024-01-30 16:09:54 +0100 | [diff] [blame] | 1139 | ly_temp_log_options(prev_lo); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1140 | if (ret) { |
| 1141 | LOGINT(ctx); |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | void |
| 1147 | lys_unres_glob_erase(struct lys_glob_unres *unres) |
| 1148 | { |
| 1149 | uint32_t i; |
| 1150 | |
| 1151 | for (i = 0; i < unres->dep_sets.count; ++i) { |
| 1152 | ly_set_free(unres->dep_sets.objs[i], NULL); |
| 1153 | } |
| 1154 | ly_set_erase(&unres->dep_sets, NULL); |
| 1155 | ly_set_erase(&unres->implementing, NULL); |
| 1156 | ly_set_erase(&unres->creating, NULL); |
| 1157 | |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 1158 | assert(!unres->ds_unres.whens.count); |
| 1159 | assert(!unres->ds_unres.musts.count); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1160 | assert(!unres->ds_unres.leafrefs.count); |
aPiecek | c6526b4 | 2021-07-12 15:21:39 +0200 | [diff] [blame] | 1161 | assert(!unres->ds_unres.disabled_leafrefs.count); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1162 | assert(!unres->ds_unres.dflts.count); |
| 1163 | assert(!unres->ds_unres.disabled.count); |
| 1164 | } |
| 1165 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1166 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 1167 | lys_set_implemented(struct lys_module *mod, const char **features) |
| 1168 | { |
| 1169 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1170 | struct lys_glob_unres *unres = &mod->ctx->unres; |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 1171 | |
| 1172 | LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL); |
| 1173 | |
| 1174 | /* implement */ |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1175 | ret = _lys_set_implemented(mod, features, unres); |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 1176 | LY_CHECK_GOTO(ret, cleanup); |
| 1177 | |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1178 | if (!(mod->ctx->flags & LY_CTX_EXPLICIT_COMPILE)) { |
| 1179 | /* 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] | 1180 | 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] | 1181 | |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 1182 | /* (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] | 1183 | LY_CHECK_GOTO(ret = lys_compile_depset_all(mod->ctx, unres), cleanup); |
| 1184 | |
| 1185 | /* unres resolved */ |
| 1186 | lys_unres_glob_erase(unres); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1187 | } |
| 1188 | |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 1189 | cleanup: |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1190 | if (ret) { |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1191 | lys_unres_glob_revert(mod->ctx, unres); |
| 1192 | lys_unres_glob_erase(unres); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1193 | } |
Michal Vasko | 89b5c07 | 2020-10-06 13:52:44 +0200 | [diff] [blame] | 1194 | return ret; |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 1195 | } |
| 1196 | |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1197 | /** |
| 1198 | * @brief Resolve (find) all imported and included modules. |
| 1199 | * |
| 1200 | * @param[in] pctx Parser context. |
| 1201 | * @param[in] pmod Parsed module to resolve. |
| 1202 | * @param[out] new_mods Set with all the newly loaded modules. |
| 1203 | * @return LY_ERR value. |
| 1204 | */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1205 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1206 | 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] | 1207 | { |
| 1208 | struct lysp_import *imp; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1209 | LY_ARRAY_COUNT_TYPE u, v; |
| 1210 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1211 | pmod->parsing = 1; |
| 1212 | LY_ARRAY_FOR(pmod->imports, u) { |
| 1213 | imp = &pmod->imports[u]; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1214 | if (!imp->module) { |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 1215 | 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] | 1216 | |
| 1217 | if (!imp->rev[0]) { |
| 1218 | /* This module must be selected for the next similar |
| 1219 | * import without revision-date to avoid incorrect |
| 1220 | * derived identities in the ::lys_module.identities. |
| 1221 | */ |
| 1222 | imp->module->latest_revision |= LYS_MOD_IMPORTED_REV; |
| 1223 | } |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1224 | } |
| 1225 | /* check for importing the same module twice */ |
| 1226 | for (v = 0; v < u; ++v) { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1227 | if (imp->module == pmod->imports[v].module) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1228 | LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name); |
| 1229 | } |
| 1230 | } |
| 1231 | } |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 1232 | LY_CHECK_RET(lysp_load_submodules(pctx, pmod, new_mods)); |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1233 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1234 | pmod->parsing = 0; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1235 | |
| 1236 | return LY_SUCCESS; |
| 1237 | } |
| 1238 | |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1239 | /** |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1240 | * @brief Generate path of the given paresed node. |
| 1241 | * |
| 1242 | * @param[in] node Schema path of this node will be generated. |
| 1243 | * @param[in] parent Build relative path only until this parent is found. If NULL, the full absolute path is printed. |
| 1244 | * @return NULL in case of memory allocation error, path of the node otherwise. |
| 1245 | * In case the @p buffer is NULL, the returned string is dynamically allocated and caller is responsible to free it. |
| 1246 | */ |
| 1247 | static char * |
| 1248 | lysp_path_until(const struct lysp_node *node, const struct lysp_node *parent, const struct lysp_module *pmod) |
| 1249 | { |
| 1250 | const struct lysp_node *iter, *par; |
| 1251 | char *path = NULL, *s; |
| 1252 | const char *slash; |
| 1253 | int len = 0; |
| 1254 | |
| 1255 | for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) { |
| 1256 | if (parent && (iter->parent == parent)) { |
| 1257 | slash = ""; |
| 1258 | } else { |
| 1259 | slash = "/"; |
| 1260 | } |
| 1261 | |
| 1262 | s = path; |
| 1263 | par = iter->parent; |
| 1264 | if (!par) { |
| 1265 | /* print prefix */ |
| 1266 | len = asprintf(&path, "%s%s:%s%s", slash, pmod->mod->name, iter->name, s ? s : ""); |
| 1267 | } else { |
| 1268 | /* prefix is the same as in parent */ |
| 1269 | len = asprintf(&path, "%s%s%s", slash, iter->name, s ? s : ""); |
| 1270 | } |
| 1271 | free(s); |
| 1272 | } |
| 1273 | |
| 1274 | if (len < 0) { |
| 1275 | free(path); |
| 1276 | path = NULL; |
| 1277 | } else if (len == 0) { |
| 1278 | path = strdup("/"); |
| 1279 | } |
| 1280 | |
| 1281 | return path; |
| 1282 | } |
| 1283 | |
| 1284 | /** |
| 1285 | * @brief Build log path for a parsed extension instance. |
| 1286 | * |
| 1287 | * @param[in] pcxt Parse context. |
| 1288 | * @param[in] ext Parsed extension instance. |
| 1289 | * @param[out] path Generated path. |
| 1290 | * @return LY_ERR value. |
| 1291 | */ |
| 1292 | static LY_ERR |
| 1293 | lysp_resolve_ext_instance_log_path(const struct lysp_ctx *pctx, const struct lysp_ext_instance *ext, char **path) |
| 1294 | { |
| 1295 | char *buf = NULL; |
| 1296 | uint32_t used = 0, size = 0; |
| 1297 | |
| 1298 | if (ext->parent_stmt & LY_STMT_NODE_MASK) { |
| 1299 | /* parsed node path */ |
| 1300 | buf = lysp_path_until(ext->parent, NULL, PARSER_CUR_PMOD(pctx)); |
| 1301 | LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(pctx)), LY_EMEM); |
| 1302 | size = used = strlen(buf); |
| 1303 | |
| 1304 | /* slash */ |
| 1305 | size += 1; |
| 1306 | buf = realloc(buf, size + 1); |
| 1307 | LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(pctx)), LY_EMEM); |
| 1308 | used += sprintf(buf + used, "/"); |
| 1309 | } else { |
| 1310 | /* module */ |
| 1311 | size += 1 + strlen(PARSER_CUR_PMOD(pctx)->mod->name) + 1; |
| 1312 | buf = realloc(buf, size + 1); |
| 1313 | LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(pctx)), LY_EMEM); |
| 1314 | used += sprintf(buf + used, "/%s:", PARSER_CUR_PMOD(pctx)->mod->name); |
| 1315 | } |
| 1316 | |
| 1317 | /* extension name */ |
| 1318 | size += 12 + strlen(ext->name) + 2; |
| 1319 | buf = realloc(buf, size + 1); |
| 1320 | LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(pctx)), LY_EMEM); |
| 1321 | used += sprintf(buf + used, "{extension='%s'}", ext->name); |
| 1322 | |
| 1323 | /* extension argument */ |
| 1324 | if (ext->argument) { |
| 1325 | size += 1 + strlen(ext->argument); |
| 1326 | buf = realloc(buf, size + 1); |
| 1327 | LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(pctx)), LY_EMEM); |
| 1328 | used += sprintf(buf + used, "/%s", ext->argument); |
| 1329 | } |
| 1330 | |
| 1331 | *path = buf; |
| 1332 | return LY_SUCCESS; |
| 1333 | } |
| 1334 | |
| 1335 | /** |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1336 | * @brief Resolve (find) all extension instance records and finish their parsing. |
| 1337 | * |
| 1338 | * @param[in] pctx Parse context with all the parsed extension instances. |
| 1339 | * @return LY_ERR value. |
| 1340 | */ |
| 1341 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1342 | lysp_resolve_ext_instance_records(struct lysp_ctx *pctx) |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1343 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1344 | LY_ERR r; |
| 1345 | struct lysf_ctx fctx = {.ctx = PARSER_CTX(pctx)}; |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1346 | struct lysp_ext_instance *exts, *ext; |
| 1347 | const struct lys_module *mod; |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1348 | uint32_t i; |
| 1349 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1350 | char *path = NULL; |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1351 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1352 | /* first finish parsing all extension instances ... */ |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1353 | for (i = 0; i < pctx->ext_inst.count; ++i) { |
| 1354 | exts = pctx->ext_inst.objs[i]; |
| 1355 | LY_ARRAY_FOR(exts, u) { |
| 1356 | ext = &exts[u]; |
| 1357 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1358 | /* find the extension definition */ |
| 1359 | LY_CHECK_RET(lysp_ext_find_definition(PARSER_CTX(pctx), ext, &mod, &ext->def)); |
| 1360 | |
| 1361 | /* resolve the argument, if needed */ |
| 1362 | LY_CHECK_RET(lysp_ext_instance_resolve_argument(PARSER_CTX(pctx), ext)); |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1363 | |
| 1364 | /* find the extension record, if any */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1365 | ext->record = lyplg_ext_record_find(mod->name, mod->revision, ext->def->name); |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | /* ... then call the parse callback */ |
| 1370 | for (i = 0; i < pctx->ext_inst.count; ++i) { |
| 1371 | exts = pctx->ext_inst.objs[i]; |
| 1372 | u = 0; |
| 1373 | while (u < LY_ARRAY_COUNT(exts)) { |
| 1374 | ext = &exts[u]; |
| 1375 | if (!ext->record || !ext->record->plugin.parse) { |
| 1376 | goto next_iter; |
| 1377 | } |
| 1378 | |
| 1379 | /* set up log path */ |
| 1380 | if ((r = lysp_resolve_ext_instance_log_path(pctx, ext, &path))) { |
| 1381 | return r; |
| 1382 | } |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1383 | ly_log_location(NULL, NULL, path, NULL); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1384 | |
| 1385 | /* parse */ |
| 1386 | r = ext->record->plugin.parse(pctx, ext); |
| 1387 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1388 | ly_log_location_revert(0, 0, 1, 0); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1389 | free(path); |
| 1390 | |
| 1391 | if (r == LY_ENOT) { |
| 1392 | /* instance should be ignored, remove it */ |
| 1393 | lysp_ext_instance_free(&fctx, ext); |
| 1394 | LY_ARRAY_DECREMENT(exts); |
| 1395 | if (u < LY_ARRAY_COUNT(exts)) { |
| 1396 | /* replace by the last item */ |
| 1397 | *ext = exts[LY_ARRAY_COUNT(exts)]; |
| 1398 | } /* else if there are no more items, leave the empty array, we are not able to free it */ |
| 1399 | continue; |
| 1400 | } else if (r) { |
| 1401 | /* error */ |
| 1402 | return r; |
| 1403 | } |
| 1404 | |
| 1405 | next_iter: |
| 1406 | ++u; |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | return LY_SUCCESS; |
| 1411 | } |
| 1412 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1413 | LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1414 | 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] | 1415 | 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] | 1416 | void *check_data, struct ly_set *new_mods, struct lysp_submodule **submodule) |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1417 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1418 | LY_ERR ret; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1419 | struct lysp_submodule *submod = NULL, *latest_sp; |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1420 | struct lysp_yang_ctx *yangctx = NULL; |
| 1421 | struct lysp_yin_ctx *yinctx = NULL; |
| 1422 | struct lysp_ctx *pctx; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1423 | struct lysf_ctx fctx = {.ctx = ctx}; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1424 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1425 | LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1426 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1427 | switch (format) { |
| 1428 | case LYS_IN_YIN: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1429 | ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod); |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1430 | pctx = (struct lysp_ctx *)yinctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1431 | break; |
| 1432 | case LYS_IN_YANG: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1433 | ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod); |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1434 | pctx = (struct lysp_ctx *)yangctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1435 | break; |
| 1436 | default: |
David Sedlák | 4f2f5ba | 2019-08-15 13:18:48 +0200 | [diff] [blame] | 1437 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
Radek Krejci | 82fa8d4 | 2020-07-11 22:00:59 +0200 | [diff] [blame] | 1438 | ret = LY_EINVAL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1439 | break; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1440 | } |
Radek Krejci | f6923e8 | 2020-07-02 16:36:53 +0200 | [diff] [blame] | 1441 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | f027df7 | 2020-09-15 13:00:28 +0200 | [diff] [blame] | 1442 | assert(submod); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1443 | |
| 1444 | /* make sure that the newest revision is at position 0 */ |
| 1445 | lysp_sort_revisions(submod->revs); |
| 1446 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1447 | /* decide the latest revision */ |
Michal Vasko | 8dc3199 | 2021-02-22 10:30:47 +0100 | [diff] [blame] | 1448 | 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] | 1449 | if (latest_sp) { |
| 1450 | if (submod->revs) { |
| 1451 | if (!latest_sp->revs) { |
| 1452 | /* latest has no revision, so mod is anyway newer */ |
| 1453 | submod->latest_revision = latest_sp->latest_revision; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1454 | /* the latest_sp is zeroed later when the new module is being inserted into the context */ |
| 1455 | } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) { |
| 1456 | submod->latest_revision = latest_sp->latest_revision; |
| 1457 | /* 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] | 1458 | } else { |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1459 | latest_sp = NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1460 | } |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1461 | } else { |
| 1462 | latest_sp = NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1463 | } |
| 1464 | } else { |
| 1465 | submod->latest_revision = 1; |
| 1466 | } |
| 1467 | |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1468 | if (custom_check) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1469 | LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | if (latest_sp) { |
| 1473 | latest_sp->latest_revision = 0; |
| 1474 | } |
| 1475 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1476 | lys_parser_fill_filepath(ctx, in, &submod->filepath); |
| 1477 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1478 | /* resolve imports and includes */ |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1479 | LY_CHECK_GOTO(ret = lysp_resolve_import_include(pctx, (struct lysp_module *)submod, new_mods), error); |
| 1480 | |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1481 | if (format == LYS_IN_YANG) { |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1482 | lysp_yang_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1483 | } else { |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1484 | lysp_yin_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1485 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1486 | *submodule = submod; |
| 1487 | return LY_SUCCESS; |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1488 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1489 | error: |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1490 | if (!submod || !submod->name) { |
| 1491 | LOGERR(ctx, ret, "Parsing submodule failed."); |
| 1492 | } else { |
| 1493 | LOGERR(ctx, ret, "Parsing submodule \"%s\" failed.", submod->name); |
| 1494 | } |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1495 | lysp_module_free(&fctx, (struct lysp_module *)submod); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1496 | if (format == LYS_IN_YANG) { |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1497 | lysp_yang_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1498 | } else { |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1499 | lysp_yin_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1500 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1501 | return ret; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1502 | } |
| 1503 | |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1504 | /** |
| 1505 | * @brief Add ietf-netconf metadata to the parsed module. Operation, filter, and select are added. |
| 1506 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1507 | * @param[in] pctx Parse context. |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1508 | * @param[in] mod Parsed module to add to. |
| 1509 | * @return LY_SUCCESS on success. |
| 1510 | * @return LY_ERR on error. |
| 1511 | */ |
| 1512 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1513 | 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] | 1514 | { |
Michal Vasko | 125b07c | 2023-04-28 09:07:14 +0200 | [diff] [blame] | 1515 | struct lysp_ext_instance *extp, *prev_exts = mod->exts; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1516 | struct lysp_stmt *stmt; |
| 1517 | struct lysp_import *imp; |
Michal Vasko | 125b07c | 2023-04-28 09:07:14 +0200 | [diff] [blame] | 1518 | uint32_t idx; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1519 | |
| 1520 | /* |
| 1521 | * 1) edit-config's operation |
| 1522 | */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1523 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, extp, LY_EMEM); |
| 1524 | LY_CHECK_ERR_RET(!extp, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1525 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &extp->name)); |
| 1526 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "operation", 0, &extp->argument)); |
| 1527 | extp->format = LY_VALUE_SCHEMA; |
| 1528 | extp->prefix_data = mod; |
| 1529 | extp->parent = mod; |
| 1530 | extp->parent_stmt = LY_STMT_MODULE; |
| 1531 | extp->flags = LYS_INTERNAL; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1532 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1533 | extp->child = stmt = calloc(1, sizeof *extp->child); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1534 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1535 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt)); |
| 1536 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1537 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1538 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1539 | stmt->kw = LY_STMT_TYPE; |
| 1540 | |
| 1541 | stmt->child = calloc(1, sizeof *stmt->child); |
| 1542 | stmt = stmt->child; |
| 1543 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1544 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1545 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "merge", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1546 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1547 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1548 | stmt->kw = LY_STMT_ENUM; |
| 1549 | |
| 1550 | stmt->next = calloc(1, sizeof *stmt->child); |
| 1551 | stmt = stmt->next; |
| 1552 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1553 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1554 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "replace", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1555 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1556 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1557 | stmt->kw = LY_STMT_ENUM; |
| 1558 | |
| 1559 | stmt->next = calloc(1, sizeof *stmt->child); |
| 1560 | stmt = stmt->next; |
| 1561 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1562 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1563 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "create", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1564 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1565 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1566 | stmt->kw = LY_STMT_ENUM; |
| 1567 | |
| 1568 | stmt->next = calloc(1, sizeof *stmt->child); |
| 1569 | stmt = stmt->next; |
| 1570 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1571 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1572 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "delete", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1573 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1574 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1575 | stmt->kw = LY_STMT_ENUM; |
| 1576 | |
| 1577 | stmt->next = calloc(1, sizeof *stmt->child); |
| 1578 | stmt = stmt->next; |
| 1579 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1580 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1581 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "remove", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1582 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1583 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1584 | stmt->kw = LY_STMT_ENUM; |
| 1585 | |
| 1586 | /* |
| 1587 | * 2) filter's type |
| 1588 | */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1589 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, extp, LY_EMEM); |
| 1590 | LY_CHECK_ERR_RET(!extp, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1591 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &extp->name)); |
| 1592 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &extp->argument)); |
| 1593 | extp->format = LY_VALUE_SCHEMA; |
| 1594 | extp->prefix_data = mod; |
| 1595 | extp->parent = mod; |
| 1596 | extp->parent_stmt = LY_STMT_MODULE; |
| 1597 | extp->flags = LYS_INTERNAL; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1598 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1599 | extp->child = stmt = calloc(1, sizeof *extp->child); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1600 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1601 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt)); |
| 1602 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1603 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1604 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1605 | stmt->kw = LY_STMT_TYPE; |
| 1606 | |
| 1607 | stmt->child = calloc(1, sizeof *stmt->child); |
| 1608 | stmt = stmt->child; |
| 1609 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1610 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1611 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "subtree", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1612 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1613 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1614 | stmt->kw = LY_STMT_ENUM; |
| 1615 | |
| 1616 | stmt->next = calloc(1, sizeof *stmt->child); |
| 1617 | stmt = stmt->next; |
| 1618 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1619 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1620 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1621 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1622 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1623 | stmt->kw = LY_STMT_ENUM; |
| 1624 | |
| 1625 | /* if-feature for enum allowed only for YANG 1.1 modules */ |
| 1626 | if (mod->version >= LYS_VERSION_1_1) { |
| 1627 | stmt->child = calloc(1, sizeof *stmt->child); |
| 1628 | stmt = stmt->child; |
| 1629 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1630 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "if-feature", 0, &stmt->stmt)); |
| 1631 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1632 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1633 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1634 | stmt->kw = LY_STMT_IF_FEATURE; |
| 1635 | } |
| 1636 | |
| 1637 | /* |
| 1638 | * 3) filter's select |
| 1639 | */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1640 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, extp, LY_EMEM); |
| 1641 | LY_CHECK_ERR_RET(!extp, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1642 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &extp->name)); |
| 1643 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "select", 0, &extp->argument)); |
| 1644 | extp->format = LY_VALUE_SCHEMA; |
| 1645 | extp->prefix_data = mod; |
| 1646 | extp->parent = mod; |
| 1647 | extp->parent_stmt = LY_STMT_MODULE; |
| 1648 | extp->flags = LYS_INTERNAL; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1649 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1650 | extp->child = stmt = calloc(1, sizeof *extp->child); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1651 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1652 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt)); |
| 1653 | 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] | 1654 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1655 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1656 | stmt->kw = LY_STMT_TYPE; |
| 1657 | |
Michal Vasko | 125b07c | 2023-04-28 09:07:14 +0200 | [diff] [blame] | 1658 | if (!prev_exts) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1659 | /* first extension instances */ |
Michal Vasko | c8806c8 | 2022-12-06 10:31:24 +0100 | [diff] [blame] | 1660 | assert(pctx->main_ctx == pctx); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1661 | LY_CHECK_RET(ly_set_add(&pctx->ext_inst, mod->exts, 1, NULL)); |
Michal Vasko | 125b07c | 2023-04-28 09:07:14 +0200 | [diff] [blame] | 1662 | } else { |
| 1663 | /* replace previously stored extension instances */ |
| 1664 | if (!ly_set_contains(&pctx->ext_inst, prev_exts, &idx)) { |
| 1665 | LOGINT_RET(mod->mod->ctx); |
| 1666 | } |
| 1667 | pctx->ext_inst.objs[idx] = mod->exts; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1668 | } |
| 1669 | |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1670 | /* create new imports for the used prefixes */ |
| 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-metadata", 0, &imp->name)); |
| 1673 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix)); |
| 1674 | imp->flags = LYS_INTERNAL; |
| 1675 | |
| 1676 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1677 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-types", 0, &imp->name)); |
| 1678 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_", 0, &imp->prefix)); |
| 1679 | imp->flags = LYS_INTERNAL; |
| 1680 | |
| 1681 | return LY_SUCCESS; |
| 1682 | } |
| 1683 | |
| 1684 | /** |
| 1685 | * @brief Add ietf-netconf-with-defaults "default" metadata to the parsed module. |
| 1686 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1687 | * @param[in] pctx Parse context. |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1688 | * @param[in] mod Parsed module to add to. |
| 1689 | * @return LY_SUCCESS on success. |
| 1690 | * @return LY_ERR on error. |
| 1691 | */ |
| 1692 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1693 | 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] | 1694 | { |
Michal Vasko | 125b07c | 2023-04-28 09:07:14 +0200 | [diff] [blame] | 1695 | struct lysp_ext_instance *extp, *prev_exts = mod->exts; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1696 | struct lysp_stmt *stmt; |
| 1697 | struct lysp_import *imp; |
Michal Vasko | 125b07c | 2023-04-28 09:07:14 +0200 | [diff] [blame] | 1698 | uint32_t idx; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1699 | |
| 1700 | /* add new extension instance */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1701 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, extp, LY_EMEM); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1702 | |
| 1703 | /* fill in the extension instance fields */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1704 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &extp->name)); |
| 1705 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "default", 0, &extp->argument)); |
| 1706 | extp->format = LY_VALUE_SCHEMA; |
| 1707 | extp->prefix_data = mod; |
| 1708 | extp->parent = mod; |
| 1709 | extp->parent_stmt = LY_STMT_MODULE; |
| 1710 | extp->flags = LYS_INTERNAL; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1711 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1712 | extp->child = stmt = calloc(1, sizeof *extp->child); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1713 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1714 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt)); |
| 1715 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "boolean", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1716 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1717 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1718 | stmt->kw = LY_STMT_TYPE; |
| 1719 | |
Michal Vasko | 125b07c | 2023-04-28 09:07:14 +0200 | [diff] [blame] | 1720 | if (!prev_exts) { |
| 1721 | /* first extension instances */ |
Michal Vasko | c8806c8 | 2022-12-06 10:31:24 +0100 | [diff] [blame] | 1722 | assert(pctx->main_ctx == pctx); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1723 | LY_CHECK_RET(ly_set_add(&pctx->ext_inst, mod->exts, 1, NULL)); |
Michal Vasko | 125b07c | 2023-04-28 09:07:14 +0200 | [diff] [blame] | 1724 | } else { |
| 1725 | /* replace previously stored extension instances */ |
| 1726 | if (!ly_set_contains(&pctx->ext_inst, prev_exts, &idx)) { |
| 1727 | LOGINT_RET(mod->mod->ctx); |
| 1728 | } |
| 1729 | pctx->ext_inst.objs[idx] = mod->exts; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1730 | } |
| 1731 | |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1732 | /* create new import for the used prefix */ |
| 1733 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1734 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name)); |
| 1735 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix)); |
| 1736 | imp->flags = LYS_INTERNAL; |
| 1737 | |
| 1738 | return LY_SUCCESS; |
| 1739 | } |
| 1740 | |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame^] | 1741 | /** |
| 1742 | * @brief Define a new internal 'lyds_tree' value for metadata. |
| 1743 | * |
| 1744 | * The 'lyds_tree' is a data type containing a reference to a binary search tree |
| 1745 | * by which the data nodes are ordered. |
| 1746 | * |
| 1747 | * @param[in] pctx Parse context. |
| 1748 | * @param[in] mod Parsed module to add to. |
| 1749 | * @return LY_SUCCESS on success. |
| 1750 | * @return LY_ERR on error. |
| 1751 | */ |
| 1752 | static LY_ERR |
| 1753 | lysp_add_internal_yang(struct lysp_ctx *pctx, struct lysp_module *mod) |
| 1754 | { |
| 1755 | struct lysp_ext_instance *extp, *prev_exts = mod->exts; |
| 1756 | struct lysp_stmt *stmt; |
| 1757 | struct lysp_tpdf *tpdf; |
| 1758 | uint32_t idx; |
| 1759 | |
| 1760 | /* add new typedef */ |
| 1761 | LY_ARRAY_NEW_RET(PARSER_CTX(pctx), mod->typedefs, tpdf, LY_EMEM); |
| 1762 | LY_CHECK_RET(lydict_insert(PARSER_CTX(pctx), "lyds_tree", 0, &tpdf->name)); |
| 1763 | LY_CHECK_RET(lydict_insert(PARSER_CTX(pctx), "uint64", 0, &tpdf->type.name)); |
| 1764 | tpdf->type.pmod = mod; |
| 1765 | |
| 1766 | /* add new extension instance */ |
| 1767 | LY_ARRAY_NEW_RET(PARSER_CTX(pctx), mod->exts, extp, LY_EMEM); |
| 1768 | |
| 1769 | /* fill in the extension instance fields */ |
| 1770 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md:annotation", 0, &extp->name)); |
| 1771 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "lyds_tree", 0, &extp->argument)); |
| 1772 | extp->format = LY_VALUE_SCHEMA; |
| 1773 | extp->prefix_data = mod; |
| 1774 | extp->parent = mod; |
| 1775 | extp->parent_stmt = LY_STMT_MODULE; |
| 1776 | extp->flags = LYS_INTERNAL; |
| 1777 | |
| 1778 | /* prepare for metadata plugin */ |
| 1779 | extp->child = stmt = calloc(1, sizeof *extp->child); |
| 1780 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1781 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt)); |
| 1782 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "lyds_tree", 0, &stmt->arg)); |
| 1783 | stmt->format = LY_VALUE_SCHEMA; |
| 1784 | stmt->prefix_data = mod; |
| 1785 | stmt->kw = LY_STMT_TYPE; |
| 1786 | |
| 1787 | if (!prev_exts) { |
| 1788 | /* first extension instances */ |
| 1789 | assert(pctx->main_ctx == pctx); |
| 1790 | LY_CHECK_RET(ly_set_add(&pctx->ext_inst, mod->exts, 1, NULL)); |
| 1791 | } else { |
| 1792 | /* replace previously stored extension instances */ |
| 1793 | if (!ly_set_contains(&pctx->ext_inst, prev_exts, &idx)) { |
| 1794 | LOGINT_RET(mod->mod->ctx); |
| 1795 | } |
| 1796 | pctx->ext_inst.objs[idx] = mod->exts; |
| 1797 | } |
| 1798 | |
| 1799 | return LY_SUCCESS; |
| 1800 | } |
| 1801 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1802 | LY_ERR |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 1803 | 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] | 1804 | 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] | 1805 | void *check_data, struct ly_set *new_mods, struct lys_module **module) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1806 | { |
Michal Vasko | a9309bb | 2021-07-09 09:31:55 +0200 | [diff] [blame] | 1807 | struct lys_module *mod = NULL, *latest, *mod_dup = NULL; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1808 | LY_ERR ret; |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1809 | struct lysp_yang_ctx *yangctx = NULL; |
| 1810 | struct lysp_yin_ctx *yinctx = NULL; |
| 1811 | struct lysp_ctx *pctx = NULL; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1812 | struct lysf_ctx fctx = {.ctx = ctx}; |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1813 | ly_bool module_created = 0; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1814 | |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 1815 | assert(ctx && in && new_mods); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1816 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1817 | if (module) { |
| 1818 | *module = NULL; |
| 1819 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1820 | |
| 1821 | mod = calloc(1, sizeof *mod); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1822 | LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1823 | mod->ctx = ctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1824 | |
Michal Vasko | 8a69a1b | 2020-12-03 14:19:02 +0100 | [diff] [blame] | 1825 | /* parse */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1826 | switch (format) { |
| 1827 | case LYS_IN_YIN: |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 1828 | ret = yin_parse_module(&yinctx, in, mod); |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1829 | pctx = (struct lysp_ctx *)yinctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1830 | break; |
| 1831 | case LYS_IN_YANG: |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 1832 | ret = yang_parse_module(&yangctx, in, mod); |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1833 | pctx = (struct lysp_ctx *)yangctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1834 | break; |
| 1835 | default: |
| 1836 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1837 | ret = LY_EINVAL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1838 | break; |
| 1839 | } |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1840 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1841 | |
| 1842 | /* make sure that the newest revision is at position 0 */ |
| 1843 | lysp_sort_revisions(mod->parsed->revs); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1844 | if (mod->parsed->revs) { |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1845 | 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] | 1846 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1847 | |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1848 | /* decide the latest revision */ |
Michal Vasko | a51ef07 | 2021-07-02 10:40:30 +0200 | [diff] [blame] | 1849 | latest = ly_ctx_get_module_latest(ctx, mod->name); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1850 | if (latest) { |
| 1851 | if (mod->revision) { |
| 1852 | if (!latest->revision) { |
| 1853 | /* latest has no revision, so mod is anyway newer */ |
aPiecek | 8ca21bd | 2021-07-26 14:31:01 +0200 | [diff] [blame] | 1854 | 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] | 1855 | /* the latest is zeroed later when the new module is being inserted into the context */ |
| 1856 | } else if (strcmp(mod->revision, latest->revision) > 0) { |
aPiecek | 8ca21bd | 2021-07-26 14:31:01 +0200 | [diff] [blame] | 1857 | 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] | 1858 | /* the latest is zeroed later when the new module is being inserted into the context */ |
| 1859 | } else { |
| 1860 | latest = NULL; |
| 1861 | } |
| 1862 | } else { |
| 1863 | latest = NULL; |
| 1864 | } |
| 1865 | } else { |
aPiecek | 8ca21bd | 2021-07-26 14:31:01 +0200 | [diff] [blame] | 1866 | mod->latest_revision = LYS_MOD_LATEST_REV; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1867 | } |
| 1868 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1869 | if (custom_check) { |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1870 | 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] | 1871 | } |
| 1872 | |
Michal Vasko | 8a69a1b | 2020-12-03 14:19:02 +0100 | [diff] [blame] | 1873 | /* 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] | 1874 | mod_dup = ly_ctx_get_module(ctx, mod->name, mod->revision); |
Michal Vasko | 8a69a1b | 2020-12-03 14:19:02 +0100 | [diff] [blame] | 1875 | if (mod_dup) { |
Michal Vasko | 8a69a1b | 2020-12-03 14:19:02 +0100 | [diff] [blame] | 1876 | /* nothing to do */ |
| 1877 | LOGVRB("Module \"%s@%s\" is already present in the context.", mod_dup->name, |
| 1878 | mod_dup->revision ? mod_dup->revision : "<none>"); |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1879 | goto cleanup; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1880 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1881 | |
Michal Vasko | b24145d | 2022-07-13 18:34:39 +0200 | [diff] [blame] | 1882 | /* check whether there is not a namespace collision */ |
| 1883 | mod_dup = ly_ctx_get_module_latest_ns(ctx, mod->ns); |
| 1884 | if (mod_dup && (mod_dup->revision == mod->revision)) { |
| 1885 | LOGERR(ctx, LY_EINVAL, "Two different modules (\"%s\" and \"%s\") have the same namespace \"%s\".", |
| 1886 | mod_dup->name, mod->name, mod->ns); |
| 1887 | ret = LY_EINVAL; |
| 1888 | goto cleanup; |
| 1889 | } |
| 1890 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1891 | switch (in->type) { |
| 1892 | case LY_IN_FILEPATH: |
Jan Kundrát | 026737b | 2023-09-05 13:03:30 +0200 | [diff] [blame] | 1893 | ly_check_module_filename(ctx, mod->name, mod->parsed->revs ? mod->parsed->revs[0].date : NULL, in->method.fpath.filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1894 | break; |
| 1895 | case LY_IN_FD: |
| 1896 | case LY_IN_FILE: |
| 1897 | case LY_IN_MEMORY: |
| 1898 | /* nothing special to do */ |
| 1899 | break; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1900 | case LY_IN_ERROR: |
| 1901 | LOGINT(ctx); |
| 1902 | ret = LY_EINT; |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1903 | goto cleanup; |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1904 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1905 | lys_parser_fill_filepath(ctx, in, &mod->filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1906 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1907 | if (latest) { |
aPiecek | 8ca21bd | 2021-07-26 14:31:01 +0200 | [diff] [blame] | 1908 | latest->latest_revision &= ~(LYS_MOD_LATEST_REV | LYS_MOD_LATEST_SEARCHDIRS); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1909 | } |
| 1910 | |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1911 | /* add internal data in case specific modules were parsed */ |
| 1912 | if (!strcmp(mod->name, "ietf-netconf")) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1913 | 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] | 1914 | } else if (!strcmp(mod->name, "ietf-netconf-with-defaults")) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1915 | LY_CHECK_GOTO(ret = lysp_add_internal_ietf_netconf_with_defaults(pctx, mod->parsed), cleanup); |
aPiecek | 6cf1d16 | 2023-11-08 16:07:00 +0100 | [diff] [blame^] | 1916 | } else if (!strcmp(mod->name, "yang")) { |
| 1917 | LY_CHECK_GOTO(ret = lysp_add_internal_yang(pctx, mod->parsed), cleanup); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1918 | } |
| 1919 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1920 | /* 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] | 1921 | LY_CHECK_GOTO(ret = ly_set_add(new_mods, mod, 1, NULL), cleanup); |
| 1922 | module_created = 1; |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1923 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1924 | /* add into context */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 1925 | ret = ly_set_add(&ctx->list, mod, 1, NULL); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1926 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 794ab4b | 2021-03-31 09:42:19 +0200 | [diff] [blame] | 1927 | ctx->change_count++; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1928 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1929 | /* resolve includes and all imports */ |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1930 | LY_CHECK_GOTO(ret = lysp_resolve_import_include(pctx, mod->parsed, new_mods), cleanup); |
| 1931 | |
| 1932 | /* resolve extension instance plugin records */ |
| 1933 | LY_CHECK_GOTO(ret = lysp_resolve_ext_instance_records(pctx), cleanup); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1934 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1935 | /* check name collisions */ |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1936 | LY_CHECK_GOTO(ret = lysp_check_dup_typedefs(pctx, mod->parsed), cleanup); |
aPiecek | 63e080d | 2021-06-29 13:53:28 +0200 | [diff] [blame] | 1937 | LY_CHECK_GOTO(ret = lysp_check_dup_groupings(pctx, mod->parsed), cleanup); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1938 | LY_CHECK_GOTO(ret = lysp_check_dup_features(pctx, mod->parsed), cleanup); |
| 1939 | LY_CHECK_GOTO(ret = lysp_check_dup_identities(pctx, mod->parsed), cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1940 | |
| 1941 | /* compile features */ |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1942 | LY_CHECK_GOTO(ret = lys_compile_feature_iffeatures(mod->parsed), cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1943 | |
aPiecek | 6b3d542 | 2021-07-30 15:55:43 +0200 | [diff] [blame] | 1944 | /* compile identities */ |
| 1945 | LY_CHECK_GOTO(ret = lys_compile_identities(mod), cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1946 | |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1947 | cleanup: |
Michal Vasko | be04af4 | 2021-07-16 10:48:43 +0200 | [diff] [blame] | 1948 | if (ret && (ret != LY_EEXIST)) { |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1949 | if (mod && mod->name) { |
| 1950 | /* there are cases when path is not available for parsing error, so this additional |
| 1951 | * message tries to add information about the module where the error occurred */ |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1952 | const struct ly_err_item *e = ly_err_last(ctx); |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 1953 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1954 | if (e && (!e->schema_path || e->line)) { |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1955 | LOGERR(ctx, ret, "Parsing module \"%s\" failed.", mod->name); |
| 1956 | } |
| 1957 | } |
| 1958 | } |
| 1959 | if (!module_created) { |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1960 | fctx.mod = mod; |
| 1961 | lys_module_free(&fctx, mod, 0); |
| 1962 | lysf_ctx_erase(&fctx); |
| 1963 | |
Michal Vasko | 0e02e8e | 2021-02-26 15:01:55 +0100 | [diff] [blame] | 1964 | mod = mod_dup; |
| 1965 | } |
Michal Vasko | 8a69a1b | 2020-12-03 14:19:02 +0100 | [diff] [blame] | 1966 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1967 | if (format == LYS_IN_YANG) { |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1968 | lysp_yang_ctx_free(yangctx); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1969 | } else { |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1970 | lysp_yin_ctx_free(yinctx); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1971 | } |
| 1972 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1973 | if (!ret && module) { |
| 1974 | *module = mod; |
| 1975 | } |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1976 | return ret; |
| 1977 | } |
| 1978 | |
Radek Krejci | 545b487 | 2020-11-15 10:15:12 +0100 | [diff] [blame] | 1979 | static LYS_INFORMAT |
| 1980 | lys_parse_get_format(const struct ly_in *in, LYS_INFORMAT format) |
| 1981 | { |
| 1982 | if (!format && (in->type == LY_IN_FILEPATH)) { |
| 1983 | /* unknown format - try to detect it from filename's suffix */ |
| 1984 | const char *path = in->method.fpath.filepath; |
| 1985 | size_t len = strlen(path); |
| 1986 | |
| 1987 | /* ignore trailing whitespaces */ |
| 1988 | for ( ; len > 0 && isspace(path[len - 1]); len--) {} |
| 1989 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1990 | if ((len >= LY_YANG_SUFFIX_LEN + 1) && |
| 1991 | !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] | 1992 | format = LYS_IN_YANG; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1993 | } else if ((len >= LY_YIN_SUFFIX_LEN + 1) && |
| 1994 | !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] | 1995 | format = LYS_IN_YIN; |
| 1996 | } /* else still unknown */ |
| 1997 | } |
| 1998 | |
| 1999 | return format; |
| 2000 | } |
| 2001 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2002 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 2003 | 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] | 2004 | { |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2005 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 2006 | struct lys_module *mod; |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 2007 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 2008 | if (module) { |
| 2009 | *module = NULL; |
| 2010 | } |
Radek Krejci | 545b487 | 2020-11-15 10:15:12 +0100 | [diff] [blame] | 2011 | LY_CHECK_ARG_RET(NULL, ctx, in, LY_EINVAL); |
| 2012 | |
| 2013 | format = lys_parse_get_format(in, format); |
| 2014 | LY_CHECK_ARG_RET(ctx, format, LY_EINVAL); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 2015 | |
| 2016 | /* remember input position */ |
| 2017 | in->func_start = in->current; |
| 2018 | |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 2019 | /* parse */ |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 2020 | 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] | 2021 | LY_CHECK_GOTO(ret, cleanup); |
| 2022 | |
| 2023 | /* implement */ |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 2024 | ret = _lys_set_implemented(mod, features, &ctx->unres); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 2025 | LY_CHECK_GOTO(ret, cleanup); |
| 2026 | |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 2027 | if (!(ctx->flags & LY_CTX_EXPLICIT_COMPILE)) { |
| 2028 | /* 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] | 2029 | 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] | 2030 | |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 2031 | /* (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] | 2032 | LY_CHECK_GOTO(ret = lys_compile_depset_all(ctx, &ctx->unres), cleanup); |
| 2033 | |
| 2034 | /* unres resolved */ |
| 2035 | lys_unres_glob_erase(&ctx->unres); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 2036 | } |
| 2037 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 2038 | cleanup: |
| 2039 | if (ret) { |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 2040 | lys_unres_glob_revert(ctx, &ctx->unres); |
| 2041 | lys_unres_glob_erase(&ctx->unres); |
Michal Vasko | 87f1cf0 | 2021-06-08 14:02:47 +0200 | [diff] [blame] | 2042 | } else if (module) { |
| 2043 | *module = mod; |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 2044 | } |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 2045 | return ret; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 2046 | } |
| 2047 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2048 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 2049 | 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] | 2050 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2051 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 2052 | struct ly_in *in = NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 2053 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2054 | LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL); |
Radek Krejci | 65639b9 | 2018-11-27 10:51:37 +0100 | [diff] [blame] | 2055 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2056 | 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] | 2057 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 2058 | ret = lys_parse(ctx, in, format, NULL, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 2059 | ly_in_free(in, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 2060 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2061 | return ret; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2062 | } |
| 2063 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2064 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 2065 | 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] | 2066 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2067 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 2068 | struct ly_in *in = NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 2069 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2070 | 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] | 2071 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2072 | 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] | 2073 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 2074 | ret = lys_parse(ctx, in, format, NULL, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 2075 | ly_in_free(in, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 2076 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2077 | return ret; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 2078 | } |
| 2079 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2080 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 2081 | 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] | 2082 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2083 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 2084 | struct ly_in *in = NULL; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 2085 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2086 | LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 2087 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2088 | LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2089 | 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] | 2090 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 2091 | ret = lys_parse(ctx, in, format, NULL, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 2092 | ly_in_free(in, 0); |
| 2093 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2094 | return ret; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2095 | } |
| 2096 | |
Michal Vasko | 9c49d88 | 2024-01-23 11:34:56 +0100 | [diff] [blame] | 2097 | /** |
| 2098 | * @brief Check file type of a file. |
| 2099 | * |
| 2100 | * @param[in] file Dirent file to check. |
| 2101 | * @param[in] wd Working directory. |
| 2102 | * @param[in,out] dirs Set with searched directories to add to. |
| 2103 | * @param[in] implicit_cwd Whether implicit CWD is used. |
| 2104 | * @param[out] skip Whether to skip this file. |
| 2105 | * @return LY_ERR value. |
| 2106 | */ |
| 2107 | static LY_ERR |
| 2108 | lys_search_localfile_file_type(const struct dirent *file, const char *wd, struct ly_set *dirs, ly_bool implicit_cwd, |
| 2109 | ly_bool *skip) |
| 2110 | { |
| 2111 | LY_ERR rc = LY_SUCCESS; |
| 2112 | char *str = NULL; |
| 2113 | ly_bool is_dir = 0, is_reg = 0; |
| 2114 | struct stat st; |
| 2115 | |
| 2116 | *skip = 0; |
| 2117 | |
| 2118 | if (file->d_type == DT_UNKNOWN) { |
| 2119 | /* FS does not support this field, need to call stat */ |
| 2120 | if (asprintf(&str, "%s/%s", wd, file->d_name) == -1) { |
| 2121 | LOGMEM(NULL); |
| 2122 | rc = LY_EMEM; |
| 2123 | goto cleanup; |
| 2124 | } |
| 2125 | if (stat(str, &st)) { |
| 2126 | LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)", |
| 2127 | file->d_name, wd, strerror(errno)); |
| 2128 | } else if (S_ISDIR(st.st_mode)) { |
| 2129 | /* stat - dir */ |
| 2130 | is_dir = 1; |
| 2131 | } else if (S_ISREG(st.st_mode)) { |
| 2132 | /* stat - file */ |
| 2133 | is_reg = 1; |
| 2134 | } |
| 2135 | } else if (file->d_type == DT_DIR) { |
| 2136 | /* dirent - dir */ |
| 2137 | is_dir = 1; |
| 2138 | } else if (file->d_type == DT_REG) { |
| 2139 | /* dirent - file */ |
| 2140 | is_reg = 1; |
| 2141 | } |
| 2142 | |
| 2143 | if (is_dir && (dirs->count || !implicit_cwd)) { |
| 2144 | /* we have another subdirectory in searchpath to explore, |
| 2145 | * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */ |
| 2146 | if (!str && (asprintf(&str, "%s/%s", wd, file->d_name) == -1)) { |
| 2147 | LOGMEM(NULL); |
| 2148 | rc = LY_EMEM; |
| 2149 | goto cleanup; |
| 2150 | } |
| 2151 | if ((rc = ly_set_add(dirs, str, 0, NULL))) { |
| 2152 | goto cleanup; |
| 2153 | } |
| 2154 | str = NULL; |
| 2155 | |
| 2156 | /* continue with the next item in current directory */ |
| 2157 | *skip = 1; |
| 2158 | } else if (!is_reg) { |
| 2159 | /* not a regular file (note that we see the target of symlinks instead of symlinks */ |
| 2160 | *skip = 1; |
| 2161 | } |
| 2162 | |
| 2163 | cleanup: |
| 2164 | free(str); |
| 2165 | return rc; |
| 2166 | } |
| 2167 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2168 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2169 | 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] | 2170 | char **localfile, LYS_INFORMAT *format) |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2171 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2172 | LY_ERR ret = LY_EMEM; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2173 | size_t len, flen, match_len = 0, dir_len; |
Michal Vasko | 9c49d88 | 2024-01-23 11:34:56 +0100 | [diff] [blame] | 2174 | ly_bool implicit_cwd = 0, skip; |
| 2175 | char *wd; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2176 | DIR *dir = NULL; |
| 2177 | struct dirent *file; |
| 2178 | char *match_name = NULL; |
| 2179 | LYS_INFORMAT format_aux, match_format = 0; |
| 2180 | struct ly_set *dirs; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2181 | |
| 2182 | LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL); |
| 2183 | |
| 2184 | /* start to fill the dir fifo with the context's search path (if set) |
| 2185 | * and the current working directory */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2186 | LY_CHECK_RET(ly_set_new(&dirs)); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2187 | |
| 2188 | len = strlen(name); |
| 2189 | if (cwd) { |
| 2190 | wd = get_current_dir_name(); |
| 2191 | if (!wd) { |
| 2192 | LOGMEM(NULL); |
| 2193 | goto cleanup; |
| 2194 | } else { |
| 2195 | /* add implicit current working directory (./) to be searched, |
| 2196 | * this directory is not searched recursively */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2197 | ret = ly_set_add(dirs, wd, 0, NULL); |
| 2198 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2199 | implicit_cwd = 1; |
| 2200 | } |
| 2201 | } |
| 2202 | if (searchpaths) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2203 | for (uint64_t i = 0; searchpaths[i]; i++) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2204 | /* check for duplicities with the implicit current working directory */ |
| 2205 | if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) { |
| 2206 | implicit_cwd = 0; |
| 2207 | continue; |
| 2208 | } |
| 2209 | wd = strdup(searchpaths[i]); |
| 2210 | if (!wd) { |
| 2211 | LOGMEM(NULL); |
| 2212 | goto cleanup; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2213 | } else { |
| 2214 | ret = ly_set_add(dirs, wd, 0, NULL); |
| 2215 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2216 | } |
| 2217 | } |
| 2218 | } |
| 2219 | wd = NULL; |
| 2220 | |
| 2221 | /* start searching */ |
| 2222 | while (dirs->count) { |
| 2223 | free(wd); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2224 | |
| 2225 | dirs->count--; |
| 2226 | wd = (char *)dirs->objs[dirs->count]; |
| 2227 | dirs->objs[dirs->count] = NULL; |
Radek Krejci | eeee95c | 2021-01-19 10:57:22 +0100 | [diff] [blame] | 2228 | LOGVRB("Searching for \"%s\" in \"%s\".", name, wd); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2229 | |
| 2230 | if (dir) { |
| 2231 | closedir(dir); |
| 2232 | } |
| 2233 | dir = opendir(wd); |
| 2234 | dir_len = strlen(wd); |
| 2235 | if (!dir) { |
| 2236 | LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno)); |
Michal Vasko | b89abc0 | 2024-01-19 09:36:13 +0100 | [diff] [blame] | 2237 | continue; |
| 2238 | } |
| 2239 | |
| 2240 | /* search the directory */ |
| 2241 | while ((file = readdir(dir))) { |
| 2242 | if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) { |
| 2243 | /* skip . and .. */ |
| 2244 | continue; |
| 2245 | } |
| 2246 | |
Michal Vasko | 9c49d88 | 2024-01-23 11:34:56 +0100 | [diff] [blame] | 2247 | /* check whether file type is */ |
| 2248 | if ((ret = lys_search_localfile_file_type(file, wd, dirs, implicit_cwd, &skip))) { |
| 2249 | goto cleanup; |
Michal Vasko | b89abc0 | 2024-01-19 09:36:13 +0100 | [diff] [blame] | 2250 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2251 | |
Michal Vasko | b89abc0 | 2024-01-19 09:36:13 +0100 | [diff] [blame] | 2252 | /* here we know that the item is a file which can contain a module */ |
| 2253 | if (strncmp(name, file->d_name, len) || ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) { |
| 2254 | /* different filename than the module we search for */ |
| 2255 | continue; |
| 2256 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2257 | |
Michal Vasko | b89abc0 | 2024-01-19 09:36:13 +0100 | [diff] [blame] | 2258 | /* get type according to filename suffix */ |
| 2259 | flen = strlen(file->d_name); |
| 2260 | if ((flen >= LY_YANG_SUFFIX_LEN + 1) && !strcmp(&file->d_name[flen - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX)) { |
| 2261 | format_aux = LYS_IN_YANG; |
| 2262 | } else if ((flen >= LY_YIN_SUFFIX_LEN + 1) && !strcmp(&file->d_name[flen - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX)) { |
| 2263 | format_aux = LYS_IN_YIN; |
| 2264 | } else { |
| 2265 | /* not supported suffix/file format */ |
| 2266 | continue; |
| 2267 | } |
| 2268 | |
| 2269 | if (revision) { |
| 2270 | /* we look for the specific revision, try to get it from the filename */ |
| 2271 | if (file->d_name[len] == '@') { |
| 2272 | /* check revision from the filename */ |
| 2273 | if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) { |
| 2274 | /* another revision */ |
| 2275 | continue; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2276 | } else { |
Michal Vasko | b89abc0 | 2024-01-19 09:36:13 +0100 | [diff] [blame] | 2277 | /* exact revision */ |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2278 | free(match_name); |
Michal Vasko | b89abc0 | 2024-01-19 09:36:13 +0100 | [diff] [blame] | 2279 | if (asprintf(&match_name, "%s/%s", wd, file->d_name) == -1) { |
| 2280 | LOGMEM(NULL); |
| 2281 | goto cleanup; |
| 2282 | } |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 2283 | match_len = dir_len + 1 + len; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2284 | match_format = format_aux; |
Michal Vasko | b89abc0 | 2024-01-19 09:36:13 +0100 | [diff] [blame] | 2285 | goto success; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2286 | } |
| 2287 | } else { |
Michal Vasko | b89abc0 | 2024-01-19 09:36:13 +0100 | [diff] [blame] | 2288 | /* continue trying to find exact revision match, use this only if not found */ |
| 2289 | free(match_name); |
| 2290 | if (asprintf(&match_name, "%s/%s", wd, file->d_name) == -1) { |
| 2291 | LOGMEM(NULL); |
| 2292 | goto cleanup; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2293 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2294 | match_len = dir_len + 1 + len; |
| 2295 | match_format = format_aux; |
| 2296 | continue; |
| 2297 | } |
Michal Vasko | b89abc0 | 2024-01-19 09:36:13 +0100 | [diff] [blame] | 2298 | } else { |
| 2299 | /* remember the revision and try to find the newest one */ |
| 2300 | if (match_name) { |
| 2301 | if ((file->d_name[len] != '@') || |
| 2302 | lysp_check_date(NULL, &file->d_name[len + 1], |
| 2303 | flen - ((format_aux == LYS_IN_YANG) ? LY_YANG_SUFFIX_LEN : LY_YIN_SUFFIX_LEN) - len - 1, NULL)) { |
| 2304 | continue; |
| 2305 | } else if ((match_name[match_len] == '@') && |
| 2306 | (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) { |
| 2307 | continue; |
| 2308 | } |
| 2309 | free(match_name); |
| 2310 | } |
| 2311 | |
| 2312 | if (asprintf(&match_name, "%s/%s", wd, file->d_name) == -1) { |
| 2313 | LOGMEM(NULL); |
| 2314 | goto cleanup; |
| 2315 | } |
| 2316 | match_len = dir_len + 1 + len; |
| 2317 | match_format = format_aux; |
| 2318 | continue; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2319 | } |
| 2320 | } |
| 2321 | } |
| 2322 | |
| 2323 | success: |
| 2324 | (*localfile) = match_name; |
| 2325 | match_name = NULL; |
| 2326 | if (format) { |
| 2327 | (*format) = match_format; |
| 2328 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2329 | ret = LY_SUCCESS; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2330 | |
| 2331 | cleanup: |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2332 | free(wd); |
| 2333 | if (dir) { |
| 2334 | closedir(dir); |
| 2335 | } |
| 2336 | free(match_name); |
| 2337 | ly_set_free(dirs, free); |
| 2338 | |
| 2339 | return ret; |
| 2340 | } |