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