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