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; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 50 | LY_ARRAY_SIZE_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 | } |
| 95 | if (u + 1 < LY_ARRAY_SIZE(actions)) { |
| 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 | } |
| 111 | if (u + 1 < LY_ARRAY_SIZE(notifs)) { |
| 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 | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 336 | char * |
| 337 | lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LYSC_PATH_TYPE pathtype, char *buffer, |
| 338 | size_t buflen) |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 339 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 340 | const struct lysc_node *iter; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 341 | char *path = NULL; |
| 342 | int len = 0; |
| 343 | |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 344 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 345 | if (buffer) { |
| 346 | LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL); |
| 347 | } |
| 348 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 349 | switch (pathtype) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 350 | case LYSC_PATH_LOG: |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 351 | for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 352 | char *s = buffer ? strdup(buffer) : path; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 353 | char *id; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 354 | const char *slash; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 355 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 356 | id = strdup(iter->name); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 357 | if (parent && (iter->parent == parent)) { |
| 358 | slash = ""; |
| 359 | } else { |
| 360 | slash = "/"; |
| 361 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 362 | if (!iter->parent || iter->parent->module != iter->module) { |
| 363 | /* print prefix */ |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 364 | if (buffer) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 365 | 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] | 366 | } else { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 367 | 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] | 368 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 369 | } else { |
| 370 | /* prefix is the same as in parent */ |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 371 | if (buffer) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 372 | len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : ""); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 373 | } else { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 374 | len = asprintf(&path, "%s%s%s", slash, id, s ? s : ""); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 375 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 376 | } |
| 377 | free(s); |
| 378 | free(id); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 379 | |
| 380 | if (buffer && buflen <= (size_t)len) { |
| 381 | /* not enough space in buffer */ |
| 382 | break; |
| 383 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | if (len < 0) { |
| 387 | free(path); |
| 388 | path = NULL; |
| 389 | } else if (len == 0) { |
Radek Krejci | 3bbd93e | 2019-07-24 09:57:23 +0200 | [diff] [blame] | 390 | if (buffer) { |
| 391 | strcpy(buffer, "/"); |
| 392 | } else { |
| 393 | path = strdup("/"); |
| 394 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 395 | } |
| 396 | break; |
| 397 | } |
| 398 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 399 | if (buffer) { |
| 400 | return buffer; |
| 401 | } else { |
| 402 | return path; |
| 403 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 406 | API char * |
| 407 | lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen) |
| 408 | { |
| 409 | return lysc_path_until(node, NULL, pathtype, buffer, buflen); |
| 410 | } |
| 411 | |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 412 | API LY_ERR |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 413 | lysc_feature_value(const struct lysc_feature *feature) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 414 | { |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 415 | LY_CHECK_ARG_RET(NULL, feature, LY_EINVAL); |
| 416 | return feature->flags & LYS_FENABLED ? LY_SUCCESS : LY_ENOT; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 417 | } |
| 418 | |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 419 | uint8_t |
| 420 | lysc_iff_getop(uint8_t *list, int pos) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 421 | { |
| 422 | uint8_t *item; |
| 423 | uint8_t mask = 3, result; |
| 424 | |
| 425 | assert(pos >= 0); |
| 426 | |
| 427 | item = &list[pos / 4]; |
| 428 | result = (*item) & (mask << 2 * (pos % 4)); |
| 429 | return result >> 2 * (pos % 4); |
| 430 | } |
| 431 | |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 432 | static LY_ERR |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 433 | lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f) |
| 434 | { |
| 435 | uint8_t op; |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 436 | LY_ERR a, b; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 437 | |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 438 | op = lysc_iff_getop(iff->expr, *index_e); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 439 | (*index_e)++; |
| 440 | |
| 441 | switch (op) { |
| 442 | case LYS_IFF_F: |
| 443 | /* resolve feature */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 444 | return lysc_feature_value(iff->features[(*index_f)++]); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 445 | case LYS_IFF_NOT: |
| 446 | /* invert result */ |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 447 | 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] | 448 | case LYS_IFF_AND: |
| 449 | case LYS_IFF_OR: |
| 450 | a = lysc_iffeature_value_(iff, index_e, index_f); |
| 451 | b = lysc_iffeature_value_(iff, index_e, index_f); |
| 452 | if (op == LYS_IFF_AND) { |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 453 | if ((a == LY_SUCCESS) && (b == LY_SUCCESS)) { |
| 454 | return LY_SUCCESS; |
| 455 | } else { |
| 456 | return LY_ENOT; |
| 457 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 458 | } else { /* LYS_IFF_OR */ |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 459 | if ((a == LY_SUCCESS) || (b == LY_SUCCESS)) { |
| 460 | return LY_SUCCESS; |
| 461 | } else { |
| 462 | return LY_ENOT; |
| 463 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 470 | API LY_ERR |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 471 | lysc_iffeature_value(const struct lysc_iffeature *iff) |
| 472 | { |
| 473 | int index_e = 0, index_f = 0; |
| 474 | |
| 475 | LY_CHECK_ARG_RET(NULL, iff, -1); |
| 476 | |
| 477 | if (iff->expr) { |
| 478 | return lysc_iffeature_value_(iff, &index_e, &index_f); |
| 479 | } |
| 480 | return 0; |
| 481 | } |
| 482 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 483 | /** |
| 484 | * @brief Enable/Disable the specified feature in the module. |
| 485 | * |
| 486 | * If the feature is already set to the desired value, LY_SUCCESS is returned. |
| 487 | * By changing the feature, also all the feature which depends on it via their |
| 488 | * if-feature statements are again evaluated (disabled if a if-feature statemen |
| 489 | * evaluates to false). |
| 490 | * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 491 | * @param[in] mod Module where to set (search for) the feature. |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 492 | * @param[in] name Name of the feature to set. Asterisk ('*') can be used to |
| 493 | * set all the features in the module. |
| 494 | * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable). |
| 495 | * @return LY_ERR value. |
| 496 | */ |
| 497 | static LY_ERR |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 498 | 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] | 499 | { |
| 500 | int all = 0; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 501 | LY_ARRAY_SIZE_TYPE u, disabled_count; |
| 502 | uint32_t changed_count; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 503 | struct lysc_feature *f, **df; |
| 504 | struct lysc_iffeature *iff; |
| 505 | struct ly_set *changed; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 506 | struct ly_ctx *ctx = mod->ctx; /* shortcut */ |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 507 | |
Radek Krejci | 6e67c40 | 2019-05-02 09:55:39 +0200 | [diff] [blame] | 508 | if (!strcmp(name, "*")) { |
| 509 | /* enable all */ |
| 510 | all = 1; |
| 511 | } |
| 512 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 513 | if (!mod->compiled) { |
| 514 | LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.", |
| 515 | mod->name); |
| 516 | return LY_EINVAL; |
| 517 | } |
| 518 | if (!mod->compiled->features) { |
Radek Krejci | 6e67c40 | 2019-05-02 09:55:39 +0200 | [diff] [blame] | 519 | if (all) { |
| 520 | /* no feature to enable */ |
| 521 | return LY_SUCCESS; |
| 522 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 523 | 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] | 524 | return LY_EINVAL; |
| 525 | } |
| 526 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 527 | changed = ly_set_new(); |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 528 | changed_count = 0; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 529 | |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 530 | run: |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 531 | for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->compiled->features); ++u) { |
| 532 | f = &mod->compiled->features[u]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 533 | if (all || !strcmp(f->name, name)) { |
| 534 | if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) { |
| 535 | if (all) { |
| 536 | /* skip already set features */ |
| 537 | continue; |
| 538 | } else { |
| 539 | /* feature already set correctly */ |
| 540 | ly_set_free(changed, NULL); |
| 541 | return LY_SUCCESS; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | if (value) { /* enable */ |
| 546 | /* check referenced features if they are enabled */ |
| 547 | LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) { |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 548 | if (lysc_iffeature_value(iff) == LY_ENOT) { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 549 | if (all) { |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 550 | ++disabled_count; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 551 | goto next; |
| 552 | } else { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 553 | LOGERR(ctx, LY_EDENIED, |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 554 | "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).", |
| 555 | f->name); |
| 556 | ly_set_free(changed, NULL); |
| 557 | return LY_EDENIED; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | /* enable the feature */ |
| 562 | f->flags |= LYS_FENABLED; |
| 563 | } else { /* disable */ |
| 564 | /* disable the feature */ |
| 565 | f->flags &= ~LYS_FENABLED; |
| 566 | } |
| 567 | |
| 568 | /* remember the changed feature */ |
| 569 | ly_set_add(changed, f, LY_SET_OPT_USEASLIST); |
| 570 | |
| 571 | if (!all) { |
| 572 | /* stop in case changing a single feature */ |
| 573 | break; |
| 574 | } |
| 575 | } |
| 576 | next: |
| 577 | ; |
| 578 | } |
| 579 | |
| 580 | if (!all && !changed->count) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 581 | 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] | 582 | ly_set_free(changed, NULL); |
| 583 | return LY_EINVAL; |
| 584 | } |
| 585 | |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 586 | if (value && all && disabled_count) { |
| 587 | if (changed_count == changed->count) { |
| 588 | /* no change in last run -> not able to enable all ... */ |
| 589 | /* ... print errors */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 590 | for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->compiled->features); ++u) { |
| 591 | if (!(mod->compiled->features[u].flags & LYS_FENABLED)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 592 | LOGERR(ctx, LY_EDENIED, |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 593 | "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] | 594 | mod->compiled->features[u].name); |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 595 | --disabled_count; |
| 596 | } |
| 597 | } |
| 598 | /* ... restore the original state */ |
| 599 | for (u = 0; u < changed->count; ++u) { |
| 600 | f = changed->objs[u]; |
| 601 | /* re-disable the feature */ |
| 602 | f->flags &= ~LYS_FENABLED; |
| 603 | } |
| 604 | |
| 605 | ly_set_free(changed, NULL); |
| 606 | return LY_EDENIED; |
| 607 | } else { |
| 608 | /* we did some change in last run, try it again */ |
| 609 | changed_count = changed->count; |
| 610 | goto run; |
| 611 | } |
| 612 | } |
| 613 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 614 | /* reflect change(s) in the dependent features */ |
| 615 | for (u = 0; u < changed->count; ++u) { |
| 616 | /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of |
| 617 | * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled |
| 618 | * is not done - by default, features are disabled and must be explicitely enabled. */ |
| 619 | f = changed->objs[u]; |
| 620 | LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) { |
| 621 | if (!((*df)->flags & LYS_FENABLED)) { |
| 622 | /* not enabled, nothing to do */ |
| 623 | continue; |
| 624 | } |
| 625 | /* check the feature's if-features which could change by the previous change of our feature */ |
| 626 | LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) { |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 627 | if (lysc_iffeature_value(iff) == LY_ENOT) { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 628 | /* the feature must be disabled now */ |
| 629 | (*df)->flags &= ~LYS_FENABLED; |
| 630 | /* add the feature into the list of changed features */ |
| 631 | ly_set_add(changed, *df, LY_SET_OPT_USEASLIST); |
| 632 | break; |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | ly_set_free(changed, NULL); |
| 639 | return LY_SUCCESS; |
| 640 | } |
| 641 | |
| 642 | API LY_ERR |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 643 | lys_feature_enable(const struct lys_module *module, const char *feature) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 644 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 645 | LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 646 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 647 | return lys_feature_change((struct lys_module*)module, feature, 1); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | API LY_ERR |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 651 | lys_feature_disable(const struct lys_module *module, const char *feature) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 652 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 653 | LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 654 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 655 | return lys_feature_change((struct lys_module*)module, feature, 0); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | API int |
| 659 | lys_feature_value(const struct lys_module *module, const char *feature) |
| 660 | { |
| 661 | struct lysc_feature *f; |
| 662 | struct lysc_module *mod; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 663 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 664 | |
| 665 | LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1); |
| 666 | mod = module->compiled; |
| 667 | |
| 668 | /* search for the specified feature */ |
| 669 | for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) { |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 670 | f = &mod->features[u]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 671 | if (!strcmp(f->name, feature)) { |
| 672 | if (f->flags & LYS_FENABLED) { |
| 673 | return 1; |
| 674 | } else { |
| 675 | return 0; |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | /* feature definition not found */ |
| 681 | return -1; |
| 682 | } |
| 683 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 684 | API const struct lysc_node * |
Radek Krejci | fab954b | 2019-09-11 11:25:14 +0200 | [diff] [blame] | 685 | lysc_node_is_disabled(const struct lysc_node *node, int recursive) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 686 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 687 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 688 | |
| 689 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 690 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 691 | do { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 692 | if (node->iffeatures) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 693 | /* check local if-features */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 694 | LY_ARRAY_FOR(node->iffeatures, u) { |
Michal Vasko | 28d7843 | 2020-05-26 13:10:53 +0200 | [diff] [blame] | 695 | if (lysc_iffeature_value(&node->iffeatures[u]) == LY_ENOT) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 696 | return node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | if (!recursive) { |
| 702 | return NULL; |
| 703 | } |
| 704 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 705 | /* go through schema-only parents */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 706 | node = node->parent; |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 707 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 708 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 709 | return NULL; |
| 710 | } |
| 711 | |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 712 | LY_ERR |
| 713 | lys_set_implemented_internal(struct lys_module *mod, uint8_t value) |
| 714 | { |
| 715 | struct lys_module *m; |
| 716 | |
| 717 | LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL); |
| 718 | |
| 719 | if (mod->implemented) { |
| 720 | return LY_SUCCESS; |
| 721 | } |
| 722 | |
| 723 | /* we have module from the current context */ |
| 724 | m = ly_ctx_get_module_implemented(mod->ctx, mod->name); |
| 725 | if (m) { |
| 726 | if (m != mod) { |
| 727 | /* check collision with other implemented revision */ |
| 728 | LOGERR(mod->ctx, LY_EDENIED, "Module \"%s\" is present in the context in other implemented revision (%s).", |
| 729 | mod->name, mod->revision ? mod->revision : "module without revision"); |
| 730 | return LY_EDENIED; |
| 731 | } else { |
| 732 | /* mod is already implemented */ |
| 733 | return LY_SUCCESS; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | /* mark the module implemented, check for collision was already done */ |
| 738 | mod->implemented = value; |
| 739 | |
| 740 | /* compile the schema */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 741 | LY_CHECK_RET(lys_compile(&mod, LYSC_OPT_INTERNAL)); |
Radek Krejci | 77a8bcd | 2019-09-11 11:20:02 +0200 | [diff] [blame] | 742 | |
| 743 | return LY_SUCCESS; |
| 744 | } |
| 745 | |
| 746 | API LY_ERR |
| 747 | lys_set_implemented(struct lys_module *mod) |
| 748 | { |
| 749 | return lys_set_implemented_internal(mod, 1); |
| 750 | } |
| 751 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 752 | struct lysp_submodule * |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 753 | 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] | 754 | 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] | 755 | { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 756 | LY_ERR ret = LY_EINVAL; |
| 757 | struct lysp_submodule *submod = NULL, *latest_sp; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 758 | struct lys_yang_parser_ctx *yangctx = NULL; |
| 759 | struct lys_yin_parser_ctx *yinctx = NULL; |
| 760 | struct lys_parser_ctx *pctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 761 | |
| 762 | LY_CHECK_ARG_RET(ctx, ctx, data, NULL); |
| 763 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 764 | switch (format) { |
| 765 | case LYS_IN_YIN: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 766 | ret = yin_parse_submodule(&yinctx, ctx, main_ctx, data, &submod); |
| 767 | pctx = (struct lys_parser_ctx *)yinctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 768 | break; |
| 769 | case LYS_IN_YANG: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 770 | ret = yang_parse_submodule(&yangctx, ctx, main_ctx, data, &submod); |
| 771 | pctx = (struct lys_parser_ctx *)yangctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 772 | break; |
| 773 | default: |
David Sedlák | 4f2f5ba | 2019-08-15 13:18:48 +0200 | [diff] [blame] | 774 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 775 | break; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 776 | } |
Radek Krejci | f6923e8 | 2020-07-02 16:36:53 +0200 | [diff] [blame^] | 777 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 778 | |
| 779 | /* make sure that the newest revision is at position 0 */ |
| 780 | lysp_sort_revisions(submod->revs); |
| 781 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 782 | /* decide the latest revision */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 783 | 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] | 784 | if (latest_sp) { |
| 785 | if (submod->revs) { |
| 786 | if (!latest_sp->revs) { |
| 787 | /* latest has no revision, so mod is anyway newer */ |
| 788 | submod->latest_revision = latest_sp->latest_revision; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 789 | /* the latest_sp is zeroed later when the new module is being inserted into the context */ |
| 790 | } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) { |
| 791 | submod->latest_revision = latest_sp->latest_revision; |
| 792 | /* 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] | 793 | } else { |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 794 | latest_sp = NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 795 | } |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 796 | } else { |
| 797 | latest_sp = NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 798 | } |
| 799 | } else { |
| 800 | submod->latest_revision = 1; |
| 801 | } |
| 802 | |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 803 | if (custom_check) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 804 | 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] | 805 | } |
| 806 | |
| 807 | if (latest_sp) { |
| 808 | latest_sp->latest_revision = 0; |
| 809 | } |
| 810 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 811 | /* 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] | 812 | memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes); |
| 813 | 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] | 814 | |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 815 | if (format == LYS_IN_YANG) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 816 | yang_parser_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 817 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 818 | yin_parser_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 819 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 820 | return submod; |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 821 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 822 | error: |
| 823 | lysp_submodule_free(ctx, submod); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 824 | if (format == LYS_IN_YANG) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 825 | yang_parser_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 826 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 827 | yin_parser_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 828 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 829 | return NULL; |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 830 | } |
| 831 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 832 | struct lys_module * |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 833 | 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] | 834 | 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] | 835 | void *check_data) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 836 | { |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 837 | struct lys_module *mod = NULL, *latest, *mod_dup; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 838 | struct lysp_import *imp; |
| 839 | struct lysp_include *inc; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 840 | LY_ERR ret = LY_EINVAL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 841 | LY_ARRAY_SIZE_TYPE u, v; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 842 | struct lys_yang_parser_ctx *yangctx = NULL; |
| 843 | struct lys_yin_parser_ctx *yinctx = NULL; |
Radek Krejci | f6923e8 | 2020-07-02 16:36:53 +0200 | [diff] [blame^] | 844 | struct lys_parser_ctx *pctx = NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 845 | |
| 846 | LY_CHECK_ARG_RET(ctx, ctx, data, NULL); |
| 847 | |
| 848 | mod = calloc(1, sizeof *mod); |
| 849 | LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 850 | mod->ctx = ctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 851 | |
| 852 | switch (format) { |
| 853 | case LYS_IN_YIN: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 854 | ret = yin_parse_module(&yinctx, data, mod); |
| 855 | pctx = (struct lys_parser_ctx *)yinctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 856 | break; |
| 857 | case LYS_IN_YANG: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 858 | ret = yang_parse_module(&yangctx, data, mod); |
| 859 | pctx = (struct lys_parser_ctx *)yangctx; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 860 | break; |
| 861 | default: |
| 862 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
| 863 | break; |
| 864 | } |
Radek Krejci | f6923e8 | 2020-07-02 16:36:53 +0200 | [diff] [blame^] | 865 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 866 | |
| 867 | /* make sure that the newest revision is at position 0 */ |
| 868 | lysp_sort_revisions(mod->parsed->revs); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 869 | if (mod->parsed->revs) { |
| 870 | mod->revision = lydict_insert(ctx, mod->parsed->revs[0].date, 0); |
| 871 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 872 | |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 873 | /* decide the latest revision */ |
| 874 | latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->name); |
| 875 | if (latest) { |
| 876 | if (mod->revision) { |
| 877 | if (!latest->revision) { |
| 878 | /* latest has no revision, so mod is anyway newer */ |
| 879 | mod->latest_revision = latest->latest_revision; |
| 880 | /* the latest is zeroed later when the new module is being inserted into the context */ |
| 881 | } else if (strcmp(mod->revision, latest->revision) > 0) { |
| 882 | mod->latest_revision = latest->latest_revision; |
| 883 | /* the latest is zeroed later when the new module is being inserted into the context */ |
| 884 | } else { |
| 885 | latest = NULL; |
| 886 | } |
| 887 | } else { |
| 888 | latest = NULL; |
| 889 | } |
| 890 | } else { |
| 891 | mod->latest_revision = 1; |
| 892 | } |
| 893 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 894 | if (custom_check) { |
| 895 | LY_CHECK_GOTO(custom_check(ctx, mod->parsed, NULL, check_data), error); |
| 896 | } |
| 897 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 898 | if (implement) { |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 899 | /* mark the loaded module implemented */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 900 | if (ly_ctx_get_module_implemented(ctx, mod->name)) { |
| 901 | 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] | 902 | goto error; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 903 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 904 | mod->implemented = 1; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 905 | } |
| 906 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 907 | /* check for duplicity in the context */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 908 | 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] | 909 | if (mod_dup) { |
| 910 | if (mod_dup->parsed) { |
| 911 | /* error */ |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 912 | if (mod->parsed->revs) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 913 | LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.", |
| 914 | mod->name, mod->parsed->revs[0].date); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 915 | } else { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 916 | LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.", |
| 917 | mod->name); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 918 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 919 | goto error; |
| 920 | } else { |
| 921 | /* add the parsed data to the currently compiled-only module in the context */ |
| 922 | mod_dup->parsed = mod->parsed; |
| 923 | mod_dup->parsed->mod = mod_dup; |
| 924 | mod->parsed = NULL; |
| 925 | lys_module_free(mod, NULL); |
| 926 | mod = mod_dup; |
| 927 | goto finish_parsing; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 928 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 929 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 930 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 931 | if (!mod->implemented) { |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 932 | /* pre-compile features of the module */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 933 | 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] | 934 | } |
| 935 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 936 | if (latest) { |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 937 | latest->latest_revision = 0; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 938 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 939 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 940 | /* add into context */ |
| 941 | ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 942 | |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 943 | finish_parsing: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 944 | /* resolve imports */ |
| 945 | mod->parsed->parsing = 1; |
| 946 | LY_ARRAY_FOR(mod->parsed->imports, u) { |
| 947 | imp = &mod->parsed->imports[u]; |
| 948 | if (!imp->module && lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)) { |
| 949 | goto error_ctx; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 950 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 951 | /* check for importing the same module twice */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 952 | for (v = 0; v < u; ++v) { |
| 953 | if (imp->module == mod->parsed->imports[v].module) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 954 | 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] | 955 | goto error_ctx; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 956 | } |
| 957 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 958 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 959 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
| 960 | inc = &mod->parsed->includes[u]; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 961 | if (!inc->submodule && lysp_load_submodule(pctx, mod->parsed, inc)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 962 | goto error_ctx; |
| 963 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 964 | if (!mod->implemented) { |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 965 | /* pre-compile features of the submodule */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 966 | 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] | 967 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 968 | } |
| 969 | mod->parsed->parsing = 0; |
| 970 | |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 971 | /* check name collisions - typedefs and TODO groupings */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 972 | LY_CHECK_GOTO(lysp_check_typedefs(pctx, mod->parsed), error_ctx); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 973 | |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 974 | if (format == LYS_IN_YANG) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 975 | yang_parser_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 976 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 977 | yin_parser_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 978 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 979 | return mod; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 980 | |
| 981 | error_ctx: |
| 982 | ly_set_rm(&ctx->list, mod, NULL); |
| 983 | error: |
| 984 | lys_module_free(mod, NULL); |
Radek Krejci | f6923e8 | 2020-07-02 16:36:53 +0200 | [diff] [blame^] | 985 | if (pctx) { |
| 986 | ly_set_erase(&pctx->tpdfs_nodes, NULL); |
| 987 | } |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 988 | if (format == LYS_IN_YANG) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 989 | yang_parser_ctx_free(yangctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 990 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 991 | yin_parser_ctx_free(yinctx); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 992 | } |
| 993 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 994 | return NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 995 | } |
| 996 | |
Radek Krejci | d14e969 | 2018-11-01 11:00:37 +0100 | [diff] [blame] | 997 | API struct lys_module * |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 998 | 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] | 999 | { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1000 | struct lys_module *mod; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1001 | char *filename, *rev, *dot; |
| 1002 | size_t len; |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1003 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1004 | LY_CHECK_ARG_RET(NULL, ctx, in, format > LYS_IN_UNKNOWN, NULL); |
| 1005 | |
| 1006 | mod = lys_parse_mem_module(ctx, in->current, format, 1, NULL, NULL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1007 | LY_CHECK_RET(!mod, NULL); |
| 1008 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1009 | switch (in->type) { |
| 1010 | case LY_IN_FILEPATH: |
| 1011 | /* check that name and revision match filename */ |
| 1012 | filename = strrchr(in->method.fpath.filepath, '/'); |
| 1013 | if (!filename) { |
| 1014 | filename = in->method.fpath.filepath; |
| 1015 | } else { |
| 1016 | filename++; |
| 1017 | } |
| 1018 | rev = strchr(filename, '@'); |
| 1019 | dot = strrchr(filename, '.'); |
| 1020 | |
| 1021 | /* name */ |
| 1022 | len = strlen(mod->name); |
| 1023 | if (strncmp(filename, mod->name, len) || |
| 1024 | ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) { |
| 1025 | LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name); |
| 1026 | } |
| 1027 | if (rev) { |
| 1028 | len = dot - ++rev; |
| 1029 | if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) { |
| 1030 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
| 1031 | mod->parsed->revs ? mod->parsed->revs[0].date : "none"); |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | break; |
| 1036 | case LY_IN_FD: |
| 1037 | case LY_IN_FILE: |
| 1038 | case LY_IN_MEMORY: |
| 1039 | /* nothing special to do */ |
| 1040 | break; |
| 1041 | default: |
| 1042 | LOGINT(ctx); |
| 1043 | break; |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1044 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1045 | |
| 1046 | lys_parser_fill_filepath(ctx, in, &mod->filepath); |
| 1047 | lys_compile(&mod, 0); |
| 1048 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1049 | return mod; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1050 | } |
| 1051 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1052 | API struct lys_module * |
| 1053 | 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] | 1054 | { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1055 | LY_ERR ret; |
| 1056 | struct ly_in *in = NULL; |
| 1057 | struct lys_module *result = NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1058 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1059 | LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, NULL); |
Radek Krejci | 65639b9 | 2018-11-27 10:51:37 +0100 | [diff] [blame] | 1060 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1061 | 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] | 1062 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1063 | result = lys_parse(ctx, in, format); |
| 1064 | ly_in_free(in, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1065 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1066 | return result; |
| 1067 | } |
| 1068 | |
Radek Krejci | d14e969 | 2018-11-01 11:00:37 +0100 | [diff] [blame] | 1069 | API struct lys_module * |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1070 | lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format) |
| 1071 | { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1072 | LY_ERR ret; |
| 1073 | struct ly_in *in = NULL; |
| 1074 | struct lys_module *result = NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1075 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1076 | LY_CHECK_ARG_RET(ctx, fd != -1, format != LYS_IN_UNKNOWN, NULL); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1077 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1078 | 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] | 1079 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1080 | result = lys_parse(ctx, in, format); |
| 1081 | ly_in_free(in, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1082 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1083 | return result; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1084 | } |
| 1085 | |
Radek Krejci | d14e969 | 2018-11-01 11:00:37 +0100 | [diff] [blame] | 1086 | API struct lys_module * |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1087 | lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format) |
| 1088 | { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1089 | LY_ERR ret; |
| 1090 | struct ly_in *in = NULL; |
| 1091 | struct lys_module *result = NULL; |
| 1092 | |
| 1093 | LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, NULL); |
| 1094 | |
| 1095 | 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); |
| 1096 | |
| 1097 | result = lys_parse(ctx, in, format); |
| 1098 | ly_in_free(in, 0); |
| 1099 | |
| 1100 | return result; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | API LY_ERR |
| 1104 | lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision, |
| 1105 | char **localfile, LYS_INFORMAT *format) |
| 1106 | { |
| 1107 | size_t len, flen, match_len = 0, dir_len; |
| 1108 | int i, implicit_cwd = 0, ret = EXIT_FAILURE; |
| 1109 | char *wd, *wn = NULL; |
| 1110 | DIR *dir = NULL; |
| 1111 | struct dirent *file; |
| 1112 | char *match_name = NULL; |
| 1113 | LYS_INFORMAT format_aux, match_format = 0; |
| 1114 | struct ly_set *dirs; |
| 1115 | struct stat st; |
| 1116 | |
| 1117 | LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL); |
| 1118 | |
| 1119 | /* start to fill the dir fifo with the context's search path (if set) |
| 1120 | * and the current working directory */ |
| 1121 | dirs = ly_set_new(); |
| 1122 | if (!dirs) { |
| 1123 | LOGMEM(NULL); |
| 1124 | return EXIT_FAILURE; |
| 1125 | } |
| 1126 | |
| 1127 | len = strlen(name); |
| 1128 | if (cwd) { |
| 1129 | wd = get_current_dir_name(); |
| 1130 | if (!wd) { |
| 1131 | LOGMEM(NULL); |
| 1132 | goto cleanup; |
| 1133 | } else { |
| 1134 | /* add implicit current working directory (./) to be searched, |
| 1135 | * this directory is not searched recursively */ |
| 1136 | if (ly_set_add(dirs, wd, 0) == -1) { |
| 1137 | goto cleanup; |
| 1138 | } |
| 1139 | implicit_cwd = 1; |
| 1140 | } |
| 1141 | } |
| 1142 | if (searchpaths) { |
| 1143 | for (i = 0; searchpaths[i]; i++) { |
| 1144 | /* check for duplicities with the implicit current working directory */ |
| 1145 | if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) { |
| 1146 | implicit_cwd = 0; |
| 1147 | continue; |
| 1148 | } |
| 1149 | wd = strdup(searchpaths[i]); |
| 1150 | if (!wd) { |
| 1151 | LOGMEM(NULL); |
| 1152 | goto cleanup; |
| 1153 | } else if (ly_set_add(dirs, wd, 0) == -1) { |
| 1154 | goto cleanup; |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | wd = NULL; |
| 1159 | |
| 1160 | /* start searching */ |
| 1161 | while (dirs->count) { |
| 1162 | free(wd); |
| 1163 | free(wn); wn = NULL; |
| 1164 | |
| 1165 | dirs->count--; |
| 1166 | wd = (char *)dirs->objs[dirs->count]; |
| 1167 | dirs->objs[dirs->count] = NULL; |
| 1168 | LOGVRB("Searching for \"%s\" in %s.", name, wd); |
| 1169 | |
| 1170 | if (dir) { |
| 1171 | closedir(dir); |
| 1172 | } |
| 1173 | dir = opendir(wd); |
| 1174 | dir_len = strlen(wd); |
| 1175 | if (!dir) { |
| 1176 | LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno)); |
| 1177 | } else { |
| 1178 | while ((file = readdir(dir))) { |
| 1179 | if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) { |
| 1180 | /* skip . and .. */ |
| 1181 | continue; |
| 1182 | } |
| 1183 | free(wn); |
| 1184 | if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) { |
| 1185 | LOGMEM(NULL); |
| 1186 | goto cleanup; |
| 1187 | } |
| 1188 | if (stat(wn, &st) == -1) { |
| 1189 | LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)", |
| 1190 | file->d_name, wd, strerror(errno)); |
| 1191 | continue; |
| 1192 | } |
| 1193 | if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) { |
| 1194 | /* we have another subdirectory in searchpath to explore, |
| 1195 | * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */ |
| 1196 | if (ly_set_add(dirs, wn, 0) == -1) { |
| 1197 | goto cleanup; |
| 1198 | } |
| 1199 | /* continue with the next item in current directory */ |
| 1200 | wn = NULL; |
| 1201 | continue; |
| 1202 | } else if (!S_ISREG(st.st_mode)) { |
| 1203 | /* not a regular file (note that we see the target of symlinks instead of symlinks */ |
| 1204 | continue; |
| 1205 | } |
| 1206 | |
| 1207 | /* here we know that the item is a file which can contain a module */ |
| 1208 | if (strncmp(name, file->d_name, len) || |
| 1209 | (file->d_name[len] != '.' && file->d_name[len] != '@')) { |
| 1210 | /* different filename than the module we search for */ |
| 1211 | continue; |
| 1212 | } |
| 1213 | |
| 1214 | /* get type according to filename suffix */ |
| 1215 | flen = strlen(file->d_name); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1216 | if (!strcmp(&file->d_name[flen - 5], ".yang")) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1217 | format_aux = LYS_IN_YANG; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1218 | /* TODO YIN parser |
| 1219 | } else if (!strcmp(&file->d_name[flen - 4], ".yin")) { |
| 1220 | format_aux = LYS_IN_YIN; |
| 1221 | */ |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1222 | } else { |
| 1223 | /* not supportde suffix/file format */ |
| 1224 | continue; |
| 1225 | } |
| 1226 | |
| 1227 | if (revision) { |
| 1228 | /* we look for the specific revision, try to get it from the filename */ |
| 1229 | if (file->d_name[len] == '@') { |
| 1230 | /* check revision from the filename */ |
| 1231 | if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) { |
| 1232 | /* another revision */ |
| 1233 | continue; |
| 1234 | } else { |
| 1235 | /* exact revision */ |
| 1236 | free(match_name); |
| 1237 | match_name = wn; |
| 1238 | wn = NULL; |
| 1239 | match_len = dir_len + 1 + len; |
| 1240 | match_format = format_aux; |
| 1241 | goto success; |
| 1242 | } |
| 1243 | } else { |
| 1244 | /* continue trying to find exact revision match, use this only if not found */ |
| 1245 | free(match_name); |
| 1246 | match_name = wn; |
| 1247 | wn = NULL; |
| 1248 | match_len = dir_len + 1 +len; |
| 1249 | match_format = format_aux; |
| 1250 | continue; |
| 1251 | } |
| 1252 | } else { |
| 1253 | /* remember the revision and try to find the newest one */ |
| 1254 | if (match_name) { |
| 1255 | if (file->d_name[len] != '@' || |
| 1256 | lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) { |
| 1257 | continue; |
| 1258 | } else if (match_name[match_len] == '@' && |
| 1259 | (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) { |
| 1260 | continue; |
| 1261 | } |
| 1262 | free(match_name); |
| 1263 | } |
| 1264 | |
| 1265 | match_name = wn; |
| 1266 | wn = NULL; |
| 1267 | match_len = dir_len + 1 + len; |
| 1268 | match_format = format_aux; |
| 1269 | continue; |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | success: |
| 1276 | (*localfile) = match_name; |
| 1277 | match_name = NULL; |
| 1278 | if (format) { |
| 1279 | (*format) = match_format; |
| 1280 | } |
| 1281 | ret = EXIT_SUCCESS; |
| 1282 | |
| 1283 | cleanup: |
| 1284 | free(wn); |
| 1285 | free(wd); |
| 1286 | if (dir) { |
| 1287 | closedir(dir); |
| 1288 | } |
| 1289 | free(match_name); |
| 1290 | ly_set_free(dirs, free); |
| 1291 | |
| 1292 | return ret; |
| 1293 | } |
| 1294 | |