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