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 | { |
| 1038 | uint32_t i, j, idx, prev_lo; |
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 */ |
| 1082 | prev_lo = ly_log_options(0); |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1083 | ret = lys_compile_depset_all(ctx, &ctx->unres); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1084 | ly_log_options(prev_lo); |
| 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 | |
| 1426 | /* resolve extension instance plugin records */ |
| 1427 | LY_CHECK_GOTO(ret = lysp_resolve_ext_instance_records(pctx), error); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 1428 | |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1429 | if (format == LYS_IN_YANG) { |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1430 | lysp_yang_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1431 | } else { |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1432 | lysp_yin_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1433 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1434 | *submodule = submod; |
| 1435 | return LY_SUCCESS; |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1436 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1437 | error: |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1438 | if (!submod || !submod->name) { |
| 1439 | LOGERR(ctx, ret, "Parsing submodule failed."); |
| 1440 | } else { |
| 1441 | LOGERR(ctx, ret, "Parsing submodule \"%s\" failed.", submod->name); |
| 1442 | } |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1443 | lysp_module_free(&fctx, (struct lysp_module *)submod); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1444 | if (format == LYS_IN_YANG) { |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1445 | lysp_yang_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1446 | } else { |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1447 | lysp_yin_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 1448 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1449 | return ret; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1450 | } |
| 1451 | |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1452 | /** |
| 1453 | * @brief Add ietf-netconf metadata to the parsed module. Operation, filter, and select are added. |
| 1454 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1455 | * @param[in] pctx Parse context. |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1456 | * @param[in] mod Parsed module to add to. |
| 1457 | * @return LY_SUCCESS on success. |
| 1458 | * @return LY_ERR on error. |
| 1459 | */ |
| 1460 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1461 | 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] | 1462 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1463 | struct lysp_ext_instance *extp; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1464 | struct lysp_stmt *stmt; |
| 1465 | struct lysp_import *imp; |
| 1466 | |
| 1467 | /* |
| 1468 | * 1) edit-config's operation |
| 1469 | */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1470 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, extp, LY_EMEM); |
| 1471 | LY_CHECK_ERR_RET(!extp, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1472 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &extp->name)); |
| 1473 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "operation", 0, &extp->argument)); |
| 1474 | extp->format = LY_VALUE_SCHEMA; |
| 1475 | extp->prefix_data = mod; |
| 1476 | extp->parent = mod; |
| 1477 | extp->parent_stmt = LY_STMT_MODULE; |
| 1478 | extp->flags = LYS_INTERNAL; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1479 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1480 | extp->child = stmt = calloc(1, sizeof *extp->child); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1481 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1482 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt)); |
| 1483 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1484 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1485 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1486 | stmt->kw = LY_STMT_TYPE; |
| 1487 | |
| 1488 | stmt->child = calloc(1, sizeof *stmt->child); |
| 1489 | stmt = stmt->child; |
| 1490 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1491 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1492 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "merge", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1493 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1494 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1495 | stmt->kw = LY_STMT_ENUM; |
| 1496 | |
| 1497 | stmt->next = calloc(1, sizeof *stmt->child); |
| 1498 | stmt = stmt->next; |
| 1499 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1500 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1501 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "replace", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1502 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1503 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1504 | stmt->kw = LY_STMT_ENUM; |
| 1505 | |
| 1506 | stmt->next = calloc(1, sizeof *stmt->child); |
| 1507 | stmt = stmt->next; |
| 1508 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1509 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1510 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "create", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1511 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1512 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1513 | stmt->kw = LY_STMT_ENUM; |
| 1514 | |
| 1515 | stmt->next = calloc(1, sizeof *stmt->child); |
| 1516 | stmt = stmt->next; |
| 1517 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1518 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1519 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "delete", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1520 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1521 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1522 | stmt->kw = LY_STMT_ENUM; |
| 1523 | |
| 1524 | stmt->next = calloc(1, sizeof *stmt->child); |
| 1525 | stmt = stmt->next; |
| 1526 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1527 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1528 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "remove", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1529 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1530 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1531 | stmt->kw = LY_STMT_ENUM; |
| 1532 | |
| 1533 | /* |
| 1534 | * 2) filter's type |
| 1535 | */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1536 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, extp, LY_EMEM); |
| 1537 | LY_CHECK_ERR_RET(!extp, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1538 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &extp->name)); |
| 1539 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &extp->argument)); |
| 1540 | extp->format = LY_VALUE_SCHEMA; |
| 1541 | extp->prefix_data = mod; |
| 1542 | extp->parent = mod; |
| 1543 | extp->parent_stmt = LY_STMT_MODULE; |
| 1544 | extp->flags = LYS_INTERNAL; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1545 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1546 | extp->child = stmt = calloc(1, sizeof *extp->child); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1547 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1548 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt)); |
| 1549 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1550 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1551 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1552 | stmt->kw = LY_STMT_TYPE; |
| 1553 | |
| 1554 | stmt->child = calloc(1, sizeof *stmt->child); |
| 1555 | stmt = stmt->child; |
| 1556 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1557 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1558 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "subtree", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1559 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1560 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1561 | stmt->kw = LY_STMT_ENUM; |
| 1562 | |
| 1563 | stmt->next = calloc(1, sizeof *stmt->child); |
| 1564 | stmt = stmt->next; |
| 1565 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1566 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt)); |
| 1567 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1568 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1569 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1570 | stmt->kw = LY_STMT_ENUM; |
| 1571 | |
| 1572 | /* if-feature for enum allowed only for YANG 1.1 modules */ |
| 1573 | if (mod->version >= LYS_VERSION_1_1) { |
| 1574 | stmt->child = calloc(1, sizeof *stmt->child); |
| 1575 | stmt = stmt->child; |
| 1576 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1577 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "if-feature", 0, &stmt->stmt)); |
| 1578 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1579 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1580 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1581 | stmt->kw = LY_STMT_IF_FEATURE; |
| 1582 | } |
| 1583 | |
| 1584 | /* |
| 1585 | * 3) filter's select |
| 1586 | */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1587 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, extp, LY_EMEM); |
| 1588 | LY_CHECK_ERR_RET(!extp, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1589 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &extp->name)); |
| 1590 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "select", 0, &extp->argument)); |
| 1591 | extp->format = LY_VALUE_SCHEMA; |
| 1592 | extp->prefix_data = mod; |
| 1593 | extp->parent = mod; |
| 1594 | extp->parent_stmt = LY_STMT_MODULE; |
| 1595 | extp->flags = LYS_INTERNAL; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1596 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1597 | extp->child = stmt = calloc(1, sizeof *extp->child); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1598 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1599 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt)); |
| 1600 | 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] | 1601 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1602 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1603 | stmt->kw = LY_STMT_TYPE; |
| 1604 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1605 | if (LY_ARRAY_COUNT(mod->exts) == 3) { |
| 1606 | /* first extension instances */ |
| 1607 | LY_CHECK_RET(ly_set_add(&pctx->ext_inst, mod->exts, 1, NULL)); |
| 1608 | } |
| 1609 | |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1610 | /* create new imports for the used prefixes */ |
| 1611 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1612 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name)); |
| 1613 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix)); |
| 1614 | imp->flags = LYS_INTERNAL; |
| 1615 | |
| 1616 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1617 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-types", 0, &imp->name)); |
| 1618 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_", 0, &imp->prefix)); |
| 1619 | imp->flags = LYS_INTERNAL; |
| 1620 | |
| 1621 | return LY_SUCCESS; |
| 1622 | } |
| 1623 | |
| 1624 | /** |
| 1625 | * @brief Add ietf-netconf-with-defaults "default" metadata to the parsed module. |
| 1626 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1627 | * @param[in] pctx Parse context. |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1628 | * @param[in] mod Parsed module to add to. |
| 1629 | * @return LY_SUCCESS on success. |
| 1630 | * @return LY_ERR on error. |
| 1631 | */ |
| 1632 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1633 | 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] | 1634 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1635 | struct lysp_ext_instance *extp; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1636 | struct lysp_stmt *stmt; |
| 1637 | struct lysp_import *imp; |
| 1638 | |
| 1639 | /* add new extension instance */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1640 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, extp, LY_EMEM); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1641 | |
| 1642 | /* fill in the extension instance fields */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1643 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &extp->name)); |
| 1644 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "default", 0, &extp->argument)); |
| 1645 | extp->format = LY_VALUE_SCHEMA; |
| 1646 | extp->prefix_data = mod; |
| 1647 | extp->parent = mod; |
| 1648 | extp->parent_stmt = LY_STMT_MODULE; |
| 1649 | extp->flags = LYS_INTERNAL; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1650 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1651 | extp->child = stmt = calloc(1, sizeof *extp->child); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1652 | LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM); |
| 1653 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt)); |
| 1654 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "boolean", 0, &stmt->arg)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1655 | stmt->format = LY_VALUE_SCHEMA; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1656 | stmt->prefix_data = mod; |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1657 | stmt->kw = LY_STMT_TYPE; |
| 1658 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1659 | if (LY_ARRAY_COUNT(mod->exts) == 1) { |
| 1660 | /* first extension instance */ |
| 1661 | LY_CHECK_RET(ly_set_add(&pctx->ext_inst, mod->exts, 1, NULL)); |
| 1662 | } |
| 1663 | |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1664 | /* create new import for the used prefix */ |
| 1665 | LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM); |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1666 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name)); |
| 1667 | LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix)); |
| 1668 | imp->flags = LYS_INTERNAL; |
| 1669 | |
| 1670 | return LY_SUCCESS; |
| 1671 | } |
| 1672 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1673 | LY_ERR |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 1674 | 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] | 1675 | 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] | 1676 | void *check_data, struct ly_set *new_mods, struct lys_module **module) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1677 | { |
Michal Vasko | a9309bb | 2021-07-09 09:31:55 +0200 | [diff] [blame] | 1678 | struct lys_module *mod = NULL, *latest, *mod_dup = NULL; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1679 | LY_ERR ret; |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1680 | struct lysp_yang_ctx *yangctx = NULL; |
| 1681 | struct lysp_yin_ctx *yinctx = NULL; |
| 1682 | struct lysp_ctx *pctx = NULL; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1683 | struct lysf_ctx fctx = {.ctx = ctx}; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1684 | char *filename, *rev, *dot; |
| 1685 | size_t len; |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1686 | ly_bool module_created = 0; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1687 | |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 1688 | assert(ctx && in && new_mods); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1689 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1690 | if (module) { |
| 1691 | *module = NULL; |
| 1692 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1693 | |
| 1694 | mod = calloc(1, sizeof *mod); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1695 | LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1696 | mod->ctx = ctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1697 | |
Michal Vasko | 8a69a1b | 2020-12-03 14:19:02 +0100 | [diff] [blame] | 1698 | /* parse */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1699 | switch (format) { |
| 1700 | case LYS_IN_YIN: |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 1701 | ret = yin_parse_module(&yinctx, in, mod); |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1702 | pctx = (struct lysp_ctx *)yinctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1703 | break; |
| 1704 | case LYS_IN_YANG: |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 1705 | ret = yang_parse_module(&yangctx, in, mod); |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1706 | pctx = (struct lysp_ctx *)yangctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1707 | break; |
| 1708 | default: |
| 1709 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1710 | ret = LY_EINVAL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1711 | break; |
| 1712 | } |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1713 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 1714 | |
| 1715 | /* make sure that the newest revision is at position 0 */ |
| 1716 | lysp_sort_revisions(mod->parsed->revs); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1717 | if (mod->parsed->revs) { |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1718 | 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] | 1719 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1720 | |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1721 | /* decide the latest revision */ |
Michal Vasko | a51ef07 | 2021-07-02 10:40:30 +0200 | [diff] [blame] | 1722 | latest = ly_ctx_get_module_latest(ctx, mod->name); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1723 | if (latest) { |
| 1724 | if (mod->revision) { |
| 1725 | if (!latest->revision) { |
| 1726 | /* latest has no revision, so mod is anyway newer */ |
aPiecek | 8ca21bd | 2021-07-26 14:31:01 +0200 | [diff] [blame] | 1727 | 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] | 1728 | /* the latest is zeroed later when the new module is being inserted into the context */ |
| 1729 | } else if (strcmp(mod->revision, latest->revision) > 0) { |
aPiecek | 8ca21bd | 2021-07-26 14:31:01 +0200 | [diff] [blame] | 1730 | 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] | 1731 | /* the latest is zeroed later when the new module is being inserted into the context */ |
| 1732 | } else { |
| 1733 | latest = NULL; |
| 1734 | } |
| 1735 | } else { |
| 1736 | latest = NULL; |
| 1737 | } |
| 1738 | } else { |
aPiecek | 8ca21bd | 2021-07-26 14:31:01 +0200 | [diff] [blame] | 1739 | mod->latest_revision = LYS_MOD_LATEST_REV; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 1740 | } |
| 1741 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1742 | if (custom_check) { |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1743 | 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] | 1744 | } |
| 1745 | |
Michal Vasko | 8a69a1b | 2020-12-03 14:19:02 +0100 | [diff] [blame] | 1746 | /* 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] | 1747 | mod_dup = ly_ctx_get_module(ctx, mod->name, mod->revision); |
Michal Vasko | 8a69a1b | 2020-12-03 14:19:02 +0100 | [diff] [blame] | 1748 | if (mod_dup) { |
Michal Vasko | 8a69a1b | 2020-12-03 14:19:02 +0100 | [diff] [blame] | 1749 | /* nothing to do */ |
| 1750 | LOGVRB("Module \"%s@%s\" is already present in the context.", mod_dup->name, |
| 1751 | mod_dup->revision ? mod_dup->revision : "<none>"); |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1752 | goto cleanup; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1753 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1754 | |
Michal Vasko | b24145d | 2022-07-13 18:34:39 +0200 | [diff] [blame] | 1755 | /* check whether there is not a namespace collision */ |
| 1756 | mod_dup = ly_ctx_get_module_latest_ns(ctx, mod->ns); |
| 1757 | if (mod_dup && (mod_dup->revision == mod->revision)) { |
| 1758 | LOGERR(ctx, LY_EINVAL, "Two different modules (\"%s\" and \"%s\") have the same namespace \"%s\".", |
| 1759 | mod_dup->name, mod->name, mod->ns); |
| 1760 | ret = LY_EINVAL; |
| 1761 | goto cleanup; |
| 1762 | } |
| 1763 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1764 | switch (in->type) { |
| 1765 | case LY_IN_FILEPATH: |
| 1766 | /* check that name and revision match filename */ |
| 1767 | filename = strrchr(in->method.fpath.filepath, '/'); |
| 1768 | if (!filename) { |
| 1769 | filename = in->method.fpath.filepath; |
| 1770 | } else { |
| 1771 | filename++; |
| 1772 | } |
| 1773 | rev = strchr(filename, '@'); |
| 1774 | dot = strrchr(filename, '.'); |
| 1775 | |
| 1776 | /* name */ |
| 1777 | len = strlen(mod->name); |
| 1778 | if (strncmp(filename, mod->name, len) || |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1779 | ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1780 | LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name); |
| 1781 | } |
| 1782 | if (rev) { |
| 1783 | len = dot - ++rev; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1784 | 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] | 1785 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1786 | mod->parsed->revs ? mod->parsed->revs[0].date : "none"); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | break; |
| 1791 | case LY_IN_FD: |
| 1792 | case LY_IN_FILE: |
| 1793 | case LY_IN_MEMORY: |
| 1794 | /* nothing special to do */ |
| 1795 | break; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1796 | case LY_IN_ERROR: |
| 1797 | LOGINT(ctx); |
| 1798 | ret = LY_EINT; |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1799 | goto cleanup; |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1800 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1801 | lys_parser_fill_filepath(ctx, in, &mod->filepath); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1802 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1803 | if (latest) { |
aPiecek | 8ca21bd | 2021-07-26 14:31:01 +0200 | [diff] [blame] | 1804 | latest->latest_revision &= ~(LYS_MOD_LATEST_REV | LYS_MOD_LATEST_SEARCHDIRS); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1805 | } |
| 1806 | |
Michal Vasko | 45b521c | 2020-11-04 17:14:39 +0100 | [diff] [blame] | 1807 | /* add internal data in case specific modules were parsed */ |
| 1808 | if (!strcmp(mod->name, "ietf-netconf")) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1809 | 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] | 1810 | } else if (!strcmp(mod->name, "ietf-netconf-with-defaults")) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1811 | 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] | 1812 | } |
| 1813 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1814 | /* 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] | 1815 | LY_CHECK_GOTO(ret = ly_set_add(new_mods, mod, 1, NULL), cleanup); |
| 1816 | module_created = 1; |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1817 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1818 | /* add into context */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 1819 | ret = ly_set_add(&ctx->list, mod, 1, NULL); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1820 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 794ab4b | 2021-03-31 09:42:19 +0200 | [diff] [blame] | 1821 | ctx->change_count++; |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1822 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1823 | /* resolve includes and all imports */ |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame] | 1824 | LY_CHECK_GOTO(ret = lysp_resolve_import_include(pctx, mod->parsed, new_mods), cleanup); |
| 1825 | |
| 1826 | /* resolve extension instance plugin records */ |
| 1827 | LY_CHECK_GOTO(ret = lysp_resolve_ext_instance_records(pctx), cleanup); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1828 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1829 | /* check name collisions */ |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1830 | LY_CHECK_GOTO(ret = lysp_check_dup_typedefs(pctx, mod->parsed), cleanup); |
aPiecek | 63e080d | 2021-06-29 13:53:28 +0200 | [diff] [blame] | 1831 | LY_CHECK_GOTO(ret = lysp_check_dup_groupings(pctx, mod->parsed), cleanup); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1832 | LY_CHECK_GOTO(ret = lysp_check_dup_features(pctx, mod->parsed), cleanup); |
| 1833 | LY_CHECK_GOTO(ret = lysp_check_dup_identities(pctx, mod->parsed), cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1834 | |
| 1835 | /* compile features */ |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1836 | LY_CHECK_GOTO(ret = lys_compile_feature_iffeatures(mod->parsed), cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1837 | |
aPiecek | 6b3d542 | 2021-07-30 15:55:43 +0200 | [diff] [blame] | 1838 | /* compile identities */ |
| 1839 | LY_CHECK_GOTO(ret = lys_compile_identities(mod), cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1840 | |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1841 | cleanup: |
Michal Vasko | be04af4 | 2021-07-16 10:48:43 +0200 | [diff] [blame] | 1842 | if (ret && (ret != LY_EEXIST)) { |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1843 | if (mod && mod->name) { |
| 1844 | /* there are cases when path is not available for parsing error, so this additional |
| 1845 | * message tries to add information about the module where the error occurred */ |
| 1846 | struct ly_err_item *e = ly_err_last(ctx); |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 1847 | |
Radek Krejci | c64661b | 2020-08-15 15:42:26 +0200 | [diff] [blame] | 1848 | if (e && (!e->path || !strncmp(e->path, "Line ", ly_strlen_const("Line ")))) { |
| 1849 | LOGERR(ctx, ret, "Parsing module \"%s\" failed.", mod->name); |
| 1850 | } |
| 1851 | } |
| 1852 | } |
| 1853 | if (!module_created) { |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1854 | fctx.mod = mod; |
| 1855 | lys_module_free(&fctx, mod, 0); |
| 1856 | lysf_ctx_erase(&fctx); |
| 1857 | |
Michal Vasko | 0e02e8e | 2021-02-26 15:01:55 +0100 | [diff] [blame] | 1858 | mod = mod_dup; |
| 1859 | } |
Michal Vasko | 8a69a1b | 2020-12-03 14:19:02 +0100 | [diff] [blame] | 1860 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1861 | if (format == LYS_IN_YANG) { |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1862 | lysp_yang_ctx_free(yangctx); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1863 | } else { |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1864 | lysp_yin_ctx_free(yinctx); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1865 | } |
| 1866 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1867 | if (!ret && module) { |
| 1868 | *module = mod; |
| 1869 | } |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1870 | return ret; |
| 1871 | } |
| 1872 | |
Radek Krejci | 545b487 | 2020-11-15 10:15:12 +0100 | [diff] [blame] | 1873 | static LYS_INFORMAT |
| 1874 | lys_parse_get_format(const struct ly_in *in, LYS_INFORMAT format) |
| 1875 | { |
| 1876 | if (!format && (in->type == LY_IN_FILEPATH)) { |
| 1877 | /* unknown format - try to detect it from filename's suffix */ |
| 1878 | const char *path = in->method.fpath.filepath; |
| 1879 | size_t len = strlen(path); |
| 1880 | |
| 1881 | /* ignore trailing whitespaces */ |
| 1882 | for ( ; len > 0 && isspace(path[len - 1]); len--) {} |
| 1883 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1884 | if ((len >= LY_YANG_SUFFIX_LEN + 1) && |
| 1885 | !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] | 1886 | format = LYS_IN_YANG; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1887 | } else if ((len >= LY_YIN_SUFFIX_LEN + 1) && |
| 1888 | !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] | 1889 | format = LYS_IN_YIN; |
| 1890 | } /* else still unknown */ |
| 1891 | } |
| 1892 | |
| 1893 | return format; |
| 1894 | } |
| 1895 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1896 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 1897 | 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] | 1898 | { |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 1899 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 1900 | struct lys_module *mod; |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1901 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1902 | if (module) { |
| 1903 | *module = NULL; |
| 1904 | } |
Radek Krejci | 545b487 | 2020-11-15 10:15:12 +0100 | [diff] [blame] | 1905 | LY_CHECK_ARG_RET(NULL, ctx, in, LY_EINVAL); |
| 1906 | |
| 1907 | format = lys_parse_get_format(in, format); |
| 1908 | LY_CHECK_ARG_RET(ctx, format, LY_EINVAL); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 1909 | |
| 1910 | /* remember input position */ |
| 1911 | in->func_start = in->current; |
| 1912 | |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 1913 | /* parse */ |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1914 | 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] | 1915 | LY_CHECK_GOTO(ret, cleanup); |
| 1916 | |
| 1917 | /* implement */ |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1918 | ret = _lys_set_implemented(mod, features, &ctx->unres); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1919 | LY_CHECK_GOTO(ret, cleanup); |
| 1920 | |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1921 | if (!(ctx->flags & LY_CTX_EXPLICIT_COMPILE)) { |
| 1922 | /* 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] | 1923 | 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] | 1924 | |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 1925 | /* (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] | 1926 | LY_CHECK_GOTO(ret = lys_compile_depset_all(ctx, &ctx->unres), cleanup); |
| 1927 | |
| 1928 | /* unres resolved */ |
| 1929 | lys_unres_glob_erase(&ctx->unres); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 1930 | } |
| 1931 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1932 | cleanup: |
| 1933 | if (ret) { |
Michal Vasko | 22b2622 | 2021-07-30 11:16:47 +0200 | [diff] [blame] | 1934 | lys_unres_glob_revert(ctx, &ctx->unres); |
| 1935 | lys_unres_glob_erase(&ctx->unres); |
Michal Vasko | 87f1cf0 | 2021-06-08 14:02:47 +0200 | [diff] [blame] | 1936 | } else if (module) { |
| 1937 | *module = mod; |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1938 | } |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1939 | return ret; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1940 | } |
| 1941 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1942 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 1943 | 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] | 1944 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1945 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1946 | struct ly_in *in = NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1947 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1948 | LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL); |
Radek Krejci | 65639b9 | 2018-11-27 10:51:37 +0100 | [diff] [blame] | 1949 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1950 | 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] | 1951 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1952 | ret = lys_parse(ctx, in, format, NULL, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1953 | ly_in_free(in, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1954 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1955 | return ret; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1956 | } |
| 1957 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1958 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 1959 | 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] | 1960 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1961 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1962 | struct ly_in *in = NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1963 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1964 | 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] | 1965 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1966 | 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] | 1967 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1968 | ret = lys_parse(ctx, in, format, NULL, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1969 | ly_in_free(in, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1970 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1971 | return ret; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1972 | } |
| 1973 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1974 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 1975 | 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] | 1976 | { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1977 | LY_ERR ret; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1978 | struct ly_in *in = NULL; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1979 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1980 | LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1981 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1982 | LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1983 | 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] | 1984 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1985 | ret = lys_parse(ctx, in, format, NULL, module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1986 | ly_in_free(in, 0); |
| 1987 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1988 | return ret; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1989 | } |
| 1990 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1991 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1992 | 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] | 1993 | char **localfile, LYS_INFORMAT *format) |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1994 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1995 | LY_ERR ret = LY_EMEM; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1996 | size_t len, flen, match_len = 0, dir_len; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1997 | ly_bool implicit_cwd = 0; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1998 | char *wd, *wn = NULL; |
| 1999 | DIR *dir = NULL; |
| 2000 | struct dirent *file; |
| 2001 | char *match_name = NULL; |
| 2002 | LYS_INFORMAT format_aux, match_format = 0; |
| 2003 | struct ly_set *dirs; |
| 2004 | struct stat st; |
| 2005 | |
| 2006 | LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL); |
| 2007 | |
| 2008 | /* start to fill the dir fifo with the context's search path (if set) |
| 2009 | * and the current working directory */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2010 | LY_CHECK_RET(ly_set_new(&dirs)); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2011 | |
| 2012 | len = strlen(name); |
| 2013 | if (cwd) { |
| 2014 | wd = get_current_dir_name(); |
| 2015 | if (!wd) { |
| 2016 | LOGMEM(NULL); |
| 2017 | goto cleanup; |
| 2018 | } else { |
| 2019 | /* add implicit current working directory (./) to be searched, |
| 2020 | * this directory is not searched recursively */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2021 | ret = ly_set_add(dirs, wd, 0, NULL); |
| 2022 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2023 | implicit_cwd = 1; |
| 2024 | } |
| 2025 | } |
| 2026 | if (searchpaths) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2027 | for (uint64_t i = 0; searchpaths[i]; i++) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2028 | /* check for duplicities with the implicit current working directory */ |
| 2029 | if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) { |
| 2030 | implicit_cwd = 0; |
| 2031 | continue; |
| 2032 | } |
| 2033 | wd = strdup(searchpaths[i]); |
| 2034 | if (!wd) { |
| 2035 | LOGMEM(NULL); |
| 2036 | goto cleanup; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2037 | } else { |
| 2038 | ret = ly_set_add(dirs, wd, 0, NULL); |
| 2039 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2040 | } |
| 2041 | } |
| 2042 | } |
| 2043 | wd = NULL; |
| 2044 | |
| 2045 | /* start searching */ |
| 2046 | while (dirs->count) { |
| 2047 | free(wd); |
| 2048 | free(wn); wn = NULL; |
| 2049 | |
| 2050 | dirs->count--; |
| 2051 | wd = (char *)dirs->objs[dirs->count]; |
| 2052 | dirs->objs[dirs->count] = NULL; |
Radek Krejci | eeee95c | 2021-01-19 10:57:22 +0100 | [diff] [blame] | 2053 | LOGVRB("Searching for \"%s\" in \"%s\".", name, wd); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2054 | |
| 2055 | if (dir) { |
| 2056 | closedir(dir); |
| 2057 | } |
| 2058 | dir = opendir(wd); |
| 2059 | dir_len = strlen(wd); |
| 2060 | if (!dir) { |
| 2061 | LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno)); |
| 2062 | } else { |
| 2063 | while ((file = readdir(dir))) { |
| 2064 | if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) { |
| 2065 | /* skip . and .. */ |
| 2066 | continue; |
| 2067 | } |
| 2068 | free(wn); |
| 2069 | if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) { |
| 2070 | LOGMEM(NULL); |
| 2071 | goto cleanup; |
| 2072 | } |
| 2073 | if (stat(wn, &st) == -1) { |
| 2074 | 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] | 2075 | file->d_name, wd, strerror(errno)); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2076 | continue; |
| 2077 | } |
| 2078 | if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) { |
| 2079 | /* we have another subdirectory in searchpath to explore, |
| 2080 | * 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] | 2081 | ret = ly_set_add(dirs, wn, 0, NULL); |
| 2082 | LY_CHECK_GOTO(ret, cleanup); |
| 2083 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2084 | /* continue with the next item in current directory */ |
| 2085 | wn = NULL; |
| 2086 | continue; |
| 2087 | } else if (!S_ISREG(st.st_mode)) { |
| 2088 | /* not a regular file (note that we see the target of symlinks instead of symlinks */ |
| 2089 | continue; |
| 2090 | } |
| 2091 | |
| 2092 | /* here we know that the item is a file which can contain a module */ |
| 2093 | if (strncmp(name, file->d_name, len) || |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2094 | ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2095 | /* different filename than the module we search for */ |
| 2096 | continue; |
| 2097 | } |
| 2098 | |
| 2099 | /* get type according to filename suffix */ |
| 2100 | flen = strlen(file->d_name); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 2101 | if ((flen >= LY_YANG_SUFFIX_LEN + 1) && |
| 2102 | !strcmp(&file->d_name[flen - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX)) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2103 | format_aux = LYS_IN_YANG; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 2104 | } else if ((flen >= LY_YIN_SUFFIX_LEN + 1) && |
| 2105 | !strcmp(&file->d_name[flen - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX)) { |
Radek Krejci | 01a937f | 2020-11-15 10:14:12 +0100 | [diff] [blame] | 2106 | format_aux = LYS_IN_YIN; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2107 | } else { |
| 2108 | /* not supportde suffix/file format */ |
| 2109 | continue; |
| 2110 | } |
| 2111 | |
| 2112 | if (revision) { |
| 2113 | /* we look for the specific revision, try to get it from the filename */ |
| 2114 | if (file->d_name[len] == '@') { |
| 2115 | /* check revision from the filename */ |
| 2116 | if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) { |
| 2117 | /* another revision */ |
| 2118 | continue; |
| 2119 | } else { |
| 2120 | /* exact revision */ |
| 2121 | free(match_name); |
| 2122 | match_name = wn; |
| 2123 | wn = NULL; |
| 2124 | match_len = dir_len + 1 + len; |
| 2125 | match_format = format_aux; |
| 2126 | goto success; |
| 2127 | } |
| 2128 | } else { |
| 2129 | /* continue trying to find exact revision match, use this only if not found */ |
| 2130 | free(match_name); |
| 2131 | match_name = wn; |
| 2132 | wn = NULL; |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 2133 | match_len = dir_len + 1 + len; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2134 | match_format = format_aux; |
| 2135 | continue; |
| 2136 | } |
| 2137 | } else { |
| 2138 | /* remember the revision and try to find the newest one */ |
| 2139 | if (match_name) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2140 | if ((file->d_name[len] != '@') || |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 2141 | lysp_check_date(NULL, &file->d_name[len + 1], |
| 2142 | 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] | 2143 | continue; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2144 | } else if ((match_name[match_len] == '@') && |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2145 | (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) { |
| 2146 | continue; |
| 2147 | } |
| 2148 | free(match_name); |
| 2149 | } |
| 2150 | |
| 2151 | match_name = wn; |
| 2152 | wn = NULL; |
| 2153 | match_len = dir_len + 1 + len; |
| 2154 | match_format = format_aux; |
| 2155 | continue; |
| 2156 | } |
| 2157 | } |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | success: |
| 2162 | (*localfile) = match_name; |
| 2163 | match_name = NULL; |
| 2164 | if (format) { |
| 2165 | (*format) = match_format; |
| 2166 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2167 | ret = LY_SUCCESS; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 2168 | |
| 2169 | cleanup: |
| 2170 | free(wn); |
| 2171 | free(wd); |
| 2172 | if (dir) { |
| 2173 | closedir(dir); |
| 2174 | } |
| 2175 | free(match_name); |
| 2176 | ly_set_free(dirs, free); |
| 2177 | |
| 2178 | return ret; |
| 2179 | } |