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