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