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