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