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