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