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