Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_schema_helpers.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame^] | 4 | * @brief Parsing and validation helper functions for schema trees |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 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 | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 14 | #include "common.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 15 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame^] | 16 | #include <assert.h> |
| 17 | #include <bits/types/struct_tm.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 21 | #include <limits.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame^] | 22 | #include <stdint.h> |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 23 | #include <stdlib.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame^] | 24 | #include <string.h> |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 25 | #include <unistd.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 26 | #include <time.h> |
| 27 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame^] | 28 | #include "context.h" |
| 29 | #include "dict.h" |
| 30 | #include "extensions.h" |
| 31 | #include "hash_table.h" |
| 32 | #include "log.h" |
| 33 | #include "set.h" |
| 34 | #include "tree.h" |
| 35 | #include "tree_schema.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 36 | #include "tree_schema_internal.h" |
| 37 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 38 | /** |
| 39 | * @brief Parse an identifier. |
| 40 | * |
| 41 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 42 | * identifier = (ALPHA / "_") |
| 43 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 44 | * |
| 45 | * @param[in,out] id Identifier to parse. When returned, it points to the first character which is not part of the identifier. |
| 46 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid starting character. |
| 47 | */ |
| 48 | static LY_ERR |
| 49 | lys_parse_id(const char **id) |
| 50 | { |
| 51 | assert(id && *id); |
| 52 | |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 53 | if (!is_yangidentstartchar(**id)) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 54 | return LY_EINVAL; |
| 55 | } |
| 56 | ++(*id); |
| 57 | |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 58 | while (is_yangidentchar(**id)) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 59 | ++(*id); |
| 60 | } |
| 61 | return LY_SUCCESS; |
| 62 | } |
| 63 | |
| 64 | LY_ERR |
| 65 | lys_parse_nodeid(const char **id, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len) |
| 66 | { |
| 67 | assert(id && *id); |
| 68 | assert(prefix && prefix_len); |
| 69 | assert(name && name_len); |
| 70 | |
| 71 | *prefix = *id; |
| 72 | *prefix_len = 0; |
| 73 | *name = NULL; |
| 74 | *name_len = 0; |
| 75 | |
| 76 | LY_CHECK_RET(lys_parse_id(id)); |
| 77 | if (**id == ':') { |
| 78 | /* there is prefix */ |
| 79 | *prefix_len = *id - *prefix; |
| 80 | ++(*id); |
| 81 | *name = *id; |
| 82 | |
| 83 | LY_CHECK_RET(lys_parse_id(id)); |
| 84 | *name_len = *id - *name; |
| 85 | } else { |
| 86 | /* there is no prefix, so what we have as prefix now is actually the name */ |
| 87 | *name = *prefix; |
| 88 | *name_len = *id - *name; |
| 89 | *prefix = NULL; |
| 90 | } |
| 91 | |
| 92 | return LY_SUCCESS; |
| 93 | } |
| 94 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 95 | LY_ERR |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 96 | lys_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *context_node, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 97 | const struct lys_module *context_module, int nodetype, int implement, |
| 98 | const struct lysc_node **target, uint16_t *result_flag) |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 99 | { |
| 100 | LY_ERR ret = LY_EVALID; |
| 101 | const char *name, *prefix, *id; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 102 | size_t name_len, prefix_len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 103 | const struct lys_module *mod; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 104 | const char *nodeid_type; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 105 | int getnext_extra_flag = 0; |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 106 | int current_nodetype = 0; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 107 | |
| 108 | assert(nodeid); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 109 | assert(target); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 110 | assert(result_flag); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 111 | *target = NULL; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 112 | *result_flag = 0; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 113 | |
| 114 | id = nodeid; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 115 | |
| 116 | if (context_node) { |
| 117 | /* descendant-schema-nodeid */ |
| 118 | nodeid_type = "descendant"; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 119 | |
| 120 | if (*id == '/') { |
| 121 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 122 | "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.", |
| 123 | nodeid_len ? nodeid_len : strlen(nodeid), nodeid); |
| 124 | return LY_EVALID; |
| 125 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 126 | } else { |
| 127 | /* absolute-schema-nodeid */ |
| 128 | nodeid_type = "absolute"; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 129 | |
| 130 | if (*id != '/') { |
| 131 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 132 | "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".", |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 133 | nodeid_len ? nodeid_len : strlen(nodeid), nodeid); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 134 | return LY_EVALID; |
| 135 | } |
| 136 | ++id; |
| 137 | } |
| 138 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 139 | while (*id && (ret = lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) { |
| 140 | if (prefix) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 141 | mod = lys_module_find_prefix(context_module, prefix, prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 142 | if (!mod) { |
| 143 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 144 | "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
| 145 | nodeid_type, id - nodeid, nodeid, prefix_len, prefix, context_module->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 146 | return LY_ENOTFOUND; |
| 147 | } |
| 148 | } else { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 149 | mod = context_module; |
| 150 | } |
| 151 | if (implement && !mod->implemented) { |
| 152 | /* make the module implemented */ |
| 153 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 154 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 155 | if (context_node && context_node->nodetype == LYS_ACTION) { |
| 156 | /* move through input/output manually */ |
| 157 | if (!strncmp("input", name, name_len)) { |
| 158 | (*result_flag) |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 159 | } else if (!strncmp("output", name, name_len)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 160 | (*result_flag) |= LYSC_OPT_RPC_OUTPUT; |
| 161 | getnext_extra_flag = LYS_GETNEXT_OUTPUT; |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 162 | } else { |
| 163 | goto getnext; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 164 | } |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 165 | current_nodetype = LYS_INOUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 166 | } else { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 167 | getnext: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 168 | context_node = lys_child(context_node, mod, name, name_len, 0, |
| 169 | getnext_extra_flag | LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE); |
| 170 | if (!context_node) { |
| 171 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 172 | "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.", nodeid_type, id - nodeid, nodeid); |
| 173 | return LY_ENOTFOUND; |
| 174 | } |
| 175 | getnext_extra_flag = 0; |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 176 | current_nodetype = context_node->nodetype; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 177 | |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 178 | if (current_nodetype == LYS_NOTIF) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 179 | (*result_flag) |= LYSC_OPT_NOTIFICATION; |
| 180 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 181 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 182 | if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 183 | break; |
| 184 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 185 | if (*id != '/') { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 186 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 187 | "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.", |
| 188 | nodeid_type, id - nodeid + 1, nodeid); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 189 | return LY_EVALID; |
| 190 | } |
| 191 | ++id; |
| 192 | } |
| 193 | |
| 194 | if (ret == LY_SUCCESS) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 195 | *target = context_node; |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 196 | if (nodetype & LYS_INOUT) { |
| 197 | /* instead of input/output nodes, the RPC/action node is actually returned */ |
| 198 | } |
| 199 | if (nodetype && !(current_nodetype & nodetype)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 200 | return LY_EDENIED; |
| 201 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 202 | } else { |
| 203 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 204 | "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.", |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 205 | nodeid_type, nodeid_len ? nodeid_len : strlen(nodeid), nodeid); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame^] | 212 | lysp_check_prefix(struct lys_parser_ctx *ctx, struct lysp_import *imports, const char *module_prefix, const char **value) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 213 | { |
| 214 | struct lysp_import *i; |
| 215 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 216 | if (module_prefix && &module_prefix != value && !strcmp(module_prefix, *value)) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 217 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, |
| 218 | "Prefix \"%s\" already used as module prefix.", *value); |
| 219 | return LY_EEXIST; |
| 220 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 221 | LY_ARRAY_FOR(imports, struct lysp_import, i) { |
| 222 | if (i->prefix && &i->prefix != value && !strcmp(i->prefix, *value)) { |
| 223 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", |
| 224 | *value, i->name); |
| 225 | return LY_EEXIST; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | return LY_SUCCESS; |
| 229 | } |
| 230 | |
| 231 | LY_ERR |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 232 | lysc_check_status(struct lysc_ctx *ctx, |
| 233 | uint16_t flags1, void *mod1, const char *name1, |
| 234 | uint16_t flags2, void *mod2, const char *name2) |
| 235 | { |
| 236 | uint16_t flg1, flg2; |
| 237 | |
| 238 | flg1 = (flags1 & LYS_STATUS_MASK) ? (flags1 & LYS_STATUS_MASK) : LYS_STATUS_CURR; |
| 239 | flg2 = (flags2 & LYS_STATUS_MASK) ? (flags2 & LYS_STATUS_MASK) : LYS_STATUS_CURR; |
| 240 | |
| 241 | if ((flg1 < flg2) && (mod1 == mod2)) { |
| 242 | if (ctx) { |
| 243 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 244 | "A %s definition \"%s\" is not allowed to reference %s definition \"%s\".", |
| 245 | flg1 == LYS_STATUS_CURR ? "current" : "deprecated", name1, |
| 246 | flg2 == LYS_STATUS_OBSLT ? "obsolete" : "deprecated", name2); |
| 247 | } |
| 248 | return LY_EVALID; |
| 249 | } |
| 250 | |
| 251 | return LY_SUCCESS; |
| 252 | } |
| 253 | |
| 254 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame^] | 255 | lysp_check_date(struct lys_parser_ctx *ctx, const char *date, int date_len, const char *stmt) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 256 | { |
| 257 | int i; |
| 258 | struct tm tm, tm_; |
| 259 | char *r; |
| 260 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 261 | LY_CHECK_ARG_RET(ctx ? ctx->ctx : NULL, date, LY_EINVAL); |
| 262 | LY_CHECK_ERR_RET(date_len != LY_REV_SIZE - 1, LOGARG(ctx ? ctx->ctx : NULL, date_len), LY_EINVAL); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 263 | |
| 264 | /* check format */ |
| 265 | for (i = 0; i < date_len; i++) { |
| 266 | if (i == 4 || i == 7) { |
| 267 | if (date[i] != '-') { |
| 268 | goto error; |
| 269 | } |
| 270 | } else if (!isdigit(date[i])) { |
| 271 | goto error; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /* check content, e.g. 2018-02-31 */ |
| 276 | memset(&tm, 0, sizeof tm); |
| 277 | r = strptime(date, "%Y-%m-%d", &tm); |
| 278 | if (!r || r != &date[LY_REV_SIZE - 1]) { |
| 279 | goto error; |
| 280 | } |
| 281 | memcpy(&tm_, &tm, sizeof tm); |
| 282 | mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */ |
| 283 | if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */ |
| 284 | /* checking days is enough, since other errors |
| 285 | * have been checked by strptime() */ |
| 286 | goto error; |
| 287 | } |
| 288 | |
| 289 | return LY_SUCCESS; |
| 290 | |
| 291 | error: |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 292 | if (stmt) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 293 | if (ctx) { |
| 294 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LY_VCODE_INVAL, date_len, date, stmt); |
| 295 | } else { |
| 296 | LOGVAL(NULL, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, date_len, date, stmt); |
| 297 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 298 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 299 | return LY_EINVAL; |
| 300 | } |
| 301 | |
| 302 | void |
| 303 | lysp_sort_revisions(struct lysp_revision *revs) |
| 304 | { |
| 305 | uint8_t i, r; |
| 306 | struct lysp_revision rev; |
| 307 | |
| 308 | for (i = 1, r = 0; revs && i < LY_ARRAY_SIZE(revs); i++) { |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 309 | if (strcmp(revs[i].date, revs[r].date) > 0) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 310 | r = i; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if (r) { |
| 315 | /* the newest revision is not on position 0, switch them */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 316 | memcpy(&rev, &revs[0], sizeof rev); |
| 317 | memcpy(&revs[0], &revs[r], sizeof rev); |
| 318 | memcpy(&revs[r], &rev, sizeof rev); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 319 | } |
| 320 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 321 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 322 | static const struct lysp_tpdf * |
| 323 | lysp_type_match(const char *name, struct lysp_node *node) |
| 324 | { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 325 | const struct lysp_tpdf *typedefs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 326 | unsigned int u; |
| 327 | |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 328 | typedefs = lysp_node_typedefs(node); |
| 329 | LY_ARRAY_FOR(typedefs, u) { |
| 330 | if (!strcmp(name, typedefs[u].name)) { |
| 331 | /* match */ |
| 332 | return &typedefs[u]; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
| 336 | return NULL; |
| 337 | } |
| 338 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 339 | static LY_DATA_TYPE |
| 340 | lysp_type_str2builtin(const char *name, size_t len) |
| 341 | { |
| 342 | if (len >= 4) { /* otherwise it does not match any built-in type */ |
| 343 | if (name[0] == 'b') { |
| 344 | if (name[1] == 'i') { |
| 345 | if (len == 6 && !strncmp(&name[2], "nary", 4)) { |
| 346 | return LY_TYPE_BINARY; |
| 347 | } else if (len == 4 && !strncmp(&name[2], "ts", 2)) { |
| 348 | return LY_TYPE_BITS; |
| 349 | } |
| 350 | } else if (len == 7 && !strncmp(&name[1], "oolean", 6)) { |
| 351 | return LY_TYPE_BOOL; |
| 352 | } |
| 353 | } else if (name[0] == 'd') { |
| 354 | if (len == 9 && !strncmp(&name[1], "ecimal64", 8)) { |
| 355 | return LY_TYPE_DEC64; |
| 356 | } |
| 357 | } else if (name[0] == 'e') { |
| 358 | if (len == 5 && !strncmp(&name[1], "mpty", 4)) { |
| 359 | return LY_TYPE_EMPTY; |
| 360 | } else if (len == 11 && !strncmp(&name[1], "numeration", 10)) { |
| 361 | return LY_TYPE_ENUM; |
| 362 | } |
| 363 | } else if (name[0] == 'i') { |
| 364 | if (name[1] == 'n') { |
| 365 | if (len == 4 && !strncmp(&name[2], "t8", 2)) { |
| 366 | return LY_TYPE_INT8; |
| 367 | } else if (len == 5) { |
| 368 | if (!strncmp(&name[2], "t16", 3)) { |
| 369 | return LY_TYPE_INT16; |
| 370 | } else if (!strncmp(&name[2], "t32", 3)) { |
| 371 | return LY_TYPE_INT32; |
| 372 | } else if (!strncmp(&name[2], "t64", 3)) { |
| 373 | return LY_TYPE_INT64; |
| 374 | } |
| 375 | } else if (len == 19 && !strncmp(&name[2], "stance-identifier", 17)) { |
| 376 | return LY_TYPE_INST; |
| 377 | } |
| 378 | } else if (len == 11 && !strncmp(&name[1], "dentityref", 10)) { |
| 379 | return LY_TYPE_IDENT; |
| 380 | } |
| 381 | } else if (name[0] == 'l') { |
| 382 | if (len == 7 && !strncmp(&name[1], "eafref", 6)) { |
| 383 | return LY_TYPE_LEAFREF; |
| 384 | } |
| 385 | } else if (name[0] == 's') { |
| 386 | if (len == 6 && !strncmp(&name[1], "tring", 5)) { |
| 387 | return LY_TYPE_STRING; |
| 388 | } |
| 389 | } else if (name[0] == 'u') { |
| 390 | if (name[1] == 'n') { |
| 391 | if (len == 5 && !strncmp(&name[2], "ion", 3)) { |
| 392 | return LY_TYPE_UNION; |
| 393 | } |
| 394 | } else if (name[1] == 'i' && name[2] == 'n' && name[3] == 't') { |
| 395 | if (len == 5 && name[4] == '8') { |
| 396 | return LY_TYPE_UINT8; |
| 397 | } else if (len == 6) { |
| 398 | if (!strncmp(&name[4], "16", 2)) { |
| 399 | return LY_TYPE_UINT16; |
| 400 | } else if (!strncmp(&name[4], "32", 2)) { |
| 401 | return LY_TYPE_UINT32; |
| 402 | } else if (!strncmp(&name[4], "64", 2)) { |
| 403 | return LY_TYPE_UINT64; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | return LY_TYPE_UNKNOWN; |
| 411 | } |
| 412 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 413 | LY_ERR |
| 414 | lysp_type_find(const char *id, struct lysp_node *start_node, struct lysp_module *start_module, |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 415 | LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node, struct lysp_module **module) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 416 | { |
| 417 | const char *str, *name; |
| 418 | struct lysp_tpdf *typedefs; |
| 419 | unsigned int u, v; |
| 420 | |
| 421 | assert(id); |
| 422 | assert(start_module); |
| 423 | assert(tpdf); |
| 424 | assert(node); |
| 425 | assert(module); |
| 426 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 427 | *node = NULL; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 428 | str = strchr(id, ':'); |
| 429 | if (str) { |
| 430 | *module = lysp_module_find_prefix(start_module, id, str - id); |
| 431 | name = str + 1; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 432 | *type = LY_TYPE_UNKNOWN; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 433 | } else { |
| 434 | *module = start_module; |
| 435 | name = id; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 436 | |
| 437 | /* check for built-in types */ |
| 438 | *type = lysp_type_str2builtin(name, strlen(name)); |
| 439 | if (*type) { |
| 440 | *tpdf = NULL; |
| 441 | return LY_SUCCESS; |
| 442 | } |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 443 | } |
| 444 | LY_CHECK_RET(!(*module), LY_ENOTFOUND); |
| 445 | |
| 446 | if (start_node && *module == start_module) { |
| 447 | /* search typedefs in parent's nodes */ |
| 448 | *node = start_node; |
| 449 | while (*node) { |
| 450 | *tpdf = lysp_type_match(name, *node); |
| 451 | if (*tpdf) { |
| 452 | /* match */ |
| 453 | return LY_SUCCESS; |
| 454 | } |
| 455 | *node = (*node)->parent; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /* search in top-level typedefs */ |
| 460 | if ((*module)->typedefs) { |
| 461 | LY_ARRAY_FOR((*module)->typedefs, u) { |
| 462 | if (!strcmp(name, (*module)->typedefs[u].name)) { |
| 463 | /* match */ |
| 464 | *tpdf = &(*module)->typedefs[u]; |
| 465 | return LY_SUCCESS; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | /* search in submodules' typedefs */ |
| 471 | LY_ARRAY_FOR((*module)->includes, u) { |
| 472 | typedefs = (*module)->includes[u].submodule->typedefs; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 473 | LY_ARRAY_FOR(typedefs, v) { |
| 474 | if (!strcmp(name, typedefs[v].name)) { |
| 475 | /* match */ |
| 476 | *tpdf = &typedefs[v]; |
| 477 | return LY_SUCCESS; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | return LY_ENOTFOUND; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * @brief Check name of a new type to avoid name collisions. |
| 487 | * |
| 488 | * @param[in] ctx Parser context, module where the type is being defined is taken from here. |
| 489 | * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef. |
| 490 | * @param[in] tpdf Typedef definition to check. |
| 491 | * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's |
| 492 | * typedefs are checked, caller is supposed to free the table. |
| 493 | * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's |
| 494 | * typedefs are checked, caller is supposed to free the table. |
| 495 | * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise. |
| 496 | */ |
| 497 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame^] | 498 | lysp_check_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf, |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 499 | struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped) |
| 500 | { |
| 501 | struct lysp_node *parent; |
| 502 | uint32_t hash; |
| 503 | size_t name_len; |
| 504 | const char *name; |
| 505 | unsigned int u; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 506 | const struct lysp_tpdf *typedefs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 507 | |
| 508 | assert(ctx); |
| 509 | assert(tpdf); |
| 510 | |
| 511 | name = tpdf->name; |
| 512 | name_len = strlen(name); |
| 513 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 514 | if (lysp_type_str2builtin(name, name_len)) { |
| 515 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 516 | "Invalid name \"%s\" of typedef - name collision with a built-in type.", name); |
| 517 | return LY_EEXIST; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | /* check locally scoped typedefs (avoid name shadowing) */ |
| 521 | if (node) { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 522 | typedefs = lysp_node_typedefs(node); |
| 523 | LY_ARRAY_FOR(typedefs, u) { |
| 524 | if (&typedefs[u] == tpdf) { |
| 525 | break; |
| 526 | } |
| 527 | if (!strcmp(name, typedefs[u].name)) { |
| 528 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 529 | "Invalid name \"%s\" of typedef - name collision with sibling type.", name); |
| 530 | return LY_EEXIST; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | /* search typedefs in parent's nodes */ |
Radek Krejci | 87e78ca | 2019-05-02 09:51:29 +0200 | [diff] [blame] | 534 | for (parent = node->parent; parent; parent = parent->parent) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 535 | if (lysp_type_match(name, parent)) { |
| 536 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 537 | "Invalid name \"%s\" of typedef - name collision with another scoped type.", name); |
| 538 | return LY_EEXIST; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | /* check collision with the top-level typedefs */ |
| 544 | hash = dict_hash(name, name_len); |
| 545 | if (node) { |
| 546 | lyht_insert(tpdfs_scoped, &name, hash, NULL); |
| 547 | if (!lyht_find(tpdfs_global, &name, hash, NULL)) { |
| 548 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 549 | "Invalid name \"%s\" of typedef - scoped type collide with a top-level type.", name); |
| 550 | return LY_EEXIST; |
| 551 | } |
| 552 | } else { |
| 553 | if (lyht_insert(tpdfs_global, &name, hash, NULL)) { |
| 554 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 555 | "Invalid name \"%s\" of typedef - name collision with another top-level type.", name); |
| 556 | return LY_EEXIST; |
| 557 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 558 | /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the |
| 559 | * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision |
| 560 | * is detected in the first branch few lines above */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | return LY_SUCCESS; |
| 564 | } |
| 565 | |
| 566 | static int |
| 567 | lysp_id_cmp(void *val1, void *val2, int UNUSED(mod), void *UNUSED(cb_data)) |
| 568 | { |
| 569 | return !strcmp(val1, val2); |
| 570 | } |
| 571 | |
| 572 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame^] | 573 | lysp_check_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 574 | { |
| 575 | struct hash_table *ids_global; |
| 576 | struct hash_table *ids_scoped; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 577 | const struct lysp_tpdf *typedefs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 578 | unsigned int i, u; |
| 579 | LY_ERR ret = LY_EVALID; |
| 580 | |
| 581 | /* check name collisions - typedefs and groupings */ |
| 582 | ids_global = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1); |
| 583 | ids_scoped = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 584 | LY_ARRAY_FOR(mod->typedefs, i) { |
| 585 | if (lysp_check_typedef(ctx, NULL, &mod->typedefs[i], ids_global, ids_scoped)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 586 | goto cleanup; |
| 587 | } |
| 588 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 589 | LY_ARRAY_FOR(mod->includes, i) { |
| 590 | LY_ARRAY_FOR(mod->includes[i].submodule->typedefs, u) { |
| 591 | if (lysp_check_typedef(ctx, NULL, &mod->includes[i].submodule->typedefs[u], ids_global, ids_scoped)) { |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 592 | goto cleanup; |
| 593 | } |
| 594 | } |
| 595 | } |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 596 | for (u = 0; u < ctx->tpdfs_nodes.count; ++u) { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 597 | typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[u]); |
| 598 | LY_ARRAY_FOR(typedefs, i) { |
| 599 | if (lysp_check_typedef(ctx, (struct lysp_node *)ctx->tpdfs_nodes.objs[u], &typedefs[i], ids_global, ids_scoped)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 600 | goto cleanup; |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | ret = LY_SUCCESS; |
| 605 | cleanup: |
| 606 | lyht_free(ids_global); |
| 607 | lyht_free(ids_scoped); |
| 608 | ly_set_erase(&ctx->tpdfs_nodes, NULL); |
| 609 | |
| 610 | return ret; |
| 611 | } |
| 612 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 613 | struct lysp_load_module_check_data { |
| 614 | const char *name; |
| 615 | const char *revision; |
| 616 | const char *path; |
| 617 | const char* submoduleof; |
| 618 | }; |
| 619 | |
| 620 | static LY_ERR |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 621 | lysp_load_module_check(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data) |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 622 | { |
| 623 | struct lysp_load_module_check_data *info = data; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 624 | const char *filename, *dot, *rev, *name; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 625 | size_t len; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 626 | struct lysp_revision *revs; |
| 627 | |
| 628 | name = mod ? mod->mod->name : submod->name; |
| 629 | revs = mod ? mod->revs : submod->revs; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 630 | |
| 631 | if (info->name) { |
| 632 | /* check name of the parsed model */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 633 | if (strcmp(info->name, name)) { |
| 634 | LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 635 | return LY_EINVAL; |
| 636 | } |
| 637 | } |
| 638 | if (info->revision) { |
| 639 | /* check revision of the parsed model */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 640 | if (!revs || strcmp(info->revision, revs[0].date)) { |
| 641 | LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name, |
Radek Krejci | b07b5c9 | 2019-04-08 10:56:37 +0200 | [diff] [blame] | 642 | revs ? revs[0].date : "none", info->revision); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 643 | return LY_EINVAL; |
| 644 | } |
| 645 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 646 | if (submod) { |
| 647 | assert(info->submoduleof); |
| 648 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 649 | /* check that the submodule belongs-to our module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 650 | if (strcmp(info->submoduleof, submod->belongsto)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 651 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 652 | submod->name, info->submoduleof, submod->belongsto); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 653 | return LY_EVALID; |
| 654 | } |
| 655 | /* check circular dependency */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 656 | if (submod->parsing) { |
| 657 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 658 | return LY_EVALID; |
| 659 | } |
| 660 | } |
| 661 | if (info->path) { |
| 662 | /* check that name and revision match filename */ |
| 663 | filename = strrchr(info->path, '/'); |
| 664 | if (!filename) { |
| 665 | filename = info->path; |
| 666 | } else { |
| 667 | filename++; |
| 668 | } |
| 669 | /* name */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 670 | len = strlen(name); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 671 | rev = strchr(filename, '@'); |
| 672 | dot = strrchr(info->path, '.'); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 673 | if (strncmp(filename, name, len) || |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 674 | ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 675 | LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 676 | } |
| 677 | /* revision */ |
| 678 | if (rev) { |
| 679 | len = dot - ++rev; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 680 | if (!revs || len != 10 || strncmp(revs[0].date, rev, len)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 681 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 682 | revs ? revs[0].date : "none"); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | } |
| 686 | return LY_SUCCESS; |
| 687 | } |
| 688 | |
| 689 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame^] | 690 | lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, int implement, struct lys_parser_ctx *main_ctx, |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 691 | void **result) |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 692 | { |
| 693 | int fd; |
| 694 | char *filepath = NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 695 | const char **fp; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 696 | LYS_INFORMAT format; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 697 | void *mod = NULL; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 698 | LY_ERR ret = LY_SUCCESS; |
| 699 | struct lysp_load_module_check_data check_data = {0}; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 700 | char rpath[PATH_MAX]; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 701 | |
| 702 | LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, revision, |
| 703 | &filepath, &format)); |
| 704 | LY_CHECK_ERR_RET(!filepath, LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", |
| 705 | name, revision ? "@" : "", revision ? revision : ""), LY_ENOTFOUND); |
| 706 | |
| 707 | |
| 708 | LOGVRB("Loading schema from \"%s\" file.", filepath); |
| 709 | |
| 710 | /* open the file */ |
| 711 | fd = open(filepath, O_RDONLY); |
| 712 | LY_CHECK_ERR_GOTO(fd < 0, LOGERR(ctx, LY_ESYS, "Unable to open data model file \"%s\" (%s).", |
| 713 | filepath, strerror(errno)); ret = LY_ESYS, cleanup); |
| 714 | |
| 715 | check_data.name = name; |
| 716 | check_data.revision = revision; |
| 717 | check_data.path = filepath; |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 718 | mod = lys_parse_fd_(ctx, fd, format, implement, main_ctx, |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 719 | lysp_load_module_check, &check_data); |
| 720 | close(fd); |
| 721 | LY_CHECK_ERR_GOTO(!mod, ly_errcode(ctx), cleanup); |
| 722 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 723 | if (main_ctx) { |
| 724 | fp = &((struct lysp_submodule*)mod)->filepath; |
| 725 | } else { |
| 726 | fp = &((struct lys_module*)mod)->filepath; |
| 727 | } |
| 728 | if (!(*fp)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 729 | if (realpath(filepath, rpath) != NULL) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 730 | (*fp) = lydict_insert(ctx, rpath, 0); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 731 | } else { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 732 | (*fp) = lydict_insert(ctx, filepath, 0); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
| 736 | *result = mod; |
| 737 | |
| 738 | /* success */ |
| 739 | cleanup: |
| 740 | free(filepath); |
| 741 | return ret; |
| 742 | } |
| 743 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 744 | LY_ERR |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 745 | lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, int implement, int require_parsed, struct lys_module **mod) |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 746 | { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 747 | const char *module_data = NULL; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 748 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 749 | void (*module_data_free)(void *module_data, void *user_data) = NULL; |
| 750 | struct lysp_load_module_check_data check_data = {0}; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 751 | struct lys_module *m; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 752 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 753 | assert(mod); |
| 754 | |
| 755 | if (!*mod) { |
| 756 | /* try to get the module from the context */ |
| 757 | if (revision) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 758 | /* get the specific revision */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 759 | *mod = (struct lys_module*)ly_ctx_get_module(ctx, name, revision); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 760 | } else if (implement) { |
| 761 | /* prefer the implemented module instead of the latest one */ |
| 762 | *mod = (struct lys_module*)ly_ctx_get_module_implemented(ctx, name); |
| 763 | if (!*mod) { |
| 764 | /* there is no implemented module in the context, try to get the latest revision module */ |
| 765 | goto latest_in_the_context; |
| 766 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 767 | } else { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 768 | /* get the requested module of the latest revision in the context */ |
| 769 | latest_in_the_context: |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 770 | *mod = (struct lys_module*)ly_ctx_get_module_latest(ctx, name); |
| 771 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 772 | } |
| 773 | |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 774 | if (!(*mod) || (require_parsed && !(*mod)->parsed)) { |
| 775 | (*mod) = NULL; |
| 776 | |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 777 | /* check collision with other implemented revision */ |
| 778 | if (implement && ly_ctx_get_module_implemented(ctx, name)) { |
| 779 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, |
| 780 | "Module \"%s\" is already present in other implemented revision.", name); |
| 781 | return LY_EDENIED; |
| 782 | } |
| 783 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 784 | /* module not present in the context, get the input data and parse it */ |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 785 | if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 786 | search_clb: |
| 787 | if (ctx->imp_clb) { |
| 788 | if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 789 | &format, &module_data, &module_data_free) == LY_SUCCESS) { |
| 790 | check_data.name = name; |
| 791 | check_data.revision = revision; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 792 | *mod = lys_parse_mem_module(ctx, module_data, format, implement, |
| 793 | lysp_load_module_check, &check_data); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 794 | if (module_data_free) { |
| 795 | module_data_free((void*)module_data, ctx->imp_clb_data); |
| 796 | } |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 797 | if (*mod && implement && lys_compile(*mod, 0)) { |
| 798 | ly_set_rm(&ctx->list, *mod, NULL); |
| 799 | lys_module_free(*mod, NULL); |
| 800 | *mod = NULL; |
| 801 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 805 | goto search_file; |
| 806 | } |
| 807 | } else { |
| 808 | search_file: |
| 809 | if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) { |
| 810 | /* module was not received from the callback or there is no callback set */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 811 | lys_module_localfile(ctx, name, revision, implement, NULL, (void **)mod); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 812 | } |
| 813 | if (!(*mod) && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 814 | goto search_clb; |
| 815 | } |
| 816 | } |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 817 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 818 | if ((*mod) && !revision && ((*mod)->latest_revision == 1)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 819 | /* update the latest_revision flag - here we have selected the latest available schema, |
| 820 | * consider that even the callback provides correct latest revision */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 821 | (*mod)->latest_revision = 2; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 822 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 823 | } else { |
| 824 | /* we have module from the current context */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 825 | if (implement) { |
| 826 | m = ly_ctx_get_module_implemented(ctx, name); |
| 827 | if (m && m != *mod) { |
| 828 | /* check collision with other implemented revision */ |
| 829 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, |
| 830 | "Module \"%s\" is already present in other implemented revision.", name); |
| 831 | *mod = NULL; |
| 832 | return LY_EDENIED; |
| 833 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /* circular check */ |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 837 | if ((*mod)->parsed && (*mod)->parsed->parsing) { |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 838 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name); |
| 839 | *mod = NULL; |
| 840 | return LY_EVALID; |
| 841 | } |
| 842 | } |
| 843 | if (!(*mod)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 844 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "%s \"%s\" module failed.", implement ? "Loading" : "Importing", name); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 845 | return LY_EVALID; |
| 846 | } |
| 847 | |
| 848 | if (implement) { |
| 849 | /* mark the module implemented, check for collision was already done */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 850 | (*mod)->implemented = 1; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 851 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 852 | |
| 853 | return LY_SUCCESS; |
| 854 | } |
| 855 | |
| 856 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame^] | 857 | lysp_load_submodule(struct lys_parser_ctx *ctx, struct lysp_module *mod, struct lysp_include *inc) |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 858 | { |
Radek Krejci | 3eb299d | 2019-04-08 15:07:44 +0200 | [diff] [blame] | 859 | struct lysp_submodule *submod = NULL; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 860 | const char *submodule_data = NULL; |
| 861 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
| 862 | void (*submodule_data_free)(void *module_data, void *user_data) = NULL; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 863 | struct lysp_load_module_check_data check_data = {0}; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 864 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 865 | /* submodule not present in the context, get the input data and parse it */ |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 866 | if (!(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 867 | search_clb: |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 868 | if (ctx->ctx->imp_clb) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 869 | if (ctx->ctx->imp_clb(mod->mod->name, NULL, inc->name, inc->rev[0] ? inc->rev : NULL, ctx->ctx->imp_clb_data, |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 870 | &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) { |
| 871 | check_data.name = inc->name; |
| 872 | check_data.revision = inc->rev[0] ? inc->rev : NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 873 | check_data.submoduleof = mod->mod->name; |
| 874 | submod = lys_parse_mem_submodule(ctx->ctx, submodule_data, format, ctx, |
| 875 | lysp_load_module_check, &check_data); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 876 | if (submodule_data_free) { |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 877 | submodule_data_free((void*)submodule_data, ctx->ctx->imp_clb_data); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 878 | } |
| 879 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 880 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 881 | if (!submod && !(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 882 | goto search_file; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 883 | } |
Radek Krejci | 2d31ea7 | 2018-10-25 15:46:42 +0200 | [diff] [blame] | 884 | } else { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 885 | search_file: |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 886 | if (!(ctx->ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 887 | /* submodule was not received from the callback or there is no callback set */ |
| 888 | lys_module_localfile(ctx->ctx, inc->name, inc->rev[0] ? inc->rev : NULL, 0, ctx, (void**)&submod); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 889 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 890 | if (!submod && (ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 891 | goto search_clb; |
| 892 | } |
| 893 | } |
| 894 | if (submod) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 895 | if (!inc->rev[0] && (submod->latest_revision == 1)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 896 | /* update the latest_revision flag - here we have selected the latest available schema, |
| 897 | * consider that even the callback provides correct latest revision */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 898 | submod->latest_revision = 2; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 899 | } |
| 900 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 901 | inc->submodule = submod; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 902 | } |
| 903 | if (!inc->submodule) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 904 | LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", |
| 905 | inc->name, mod->mod->name); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 906 | return LY_EVALID; |
| 907 | } |
| 908 | |
| 909 | return LY_SUCCESS; |
| 910 | } |
| 911 | |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 912 | #define FIND_MODULE(TYPE, MOD) \ |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 913 | TYPE *imp; \ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 914 | if (!strncmp((MOD)->mod->prefix, prefix, len) && (MOD)->mod->prefix[len] == '\0') { \ |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 915 | /* it is the prefix of the module itself */ \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 916 | m = ly_ctx_get_module((MOD)->mod->ctx, (MOD)->mod->name, (MOD)->mod->revision); \ |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 917 | } \ |
| 918 | /* search in imports */ \ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 919 | if (!m) { \ |
| 920 | LY_ARRAY_FOR((MOD)->imports, TYPE, imp) { \ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 921 | if (!strncmp(imp->prefix, prefix, len) && imp->prefix[len] == '\0') { \ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 922 | m = imp->module; \ |
| 923 | break; \ |
| 924 | } \ |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 925 | } \ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 926 | } |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 927 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 928 | struct lysc_module * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 929 | lysc_module_find_prefix(const struct lysc_module *mod, const char *prefix, size_t len) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 930 | { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 931 | const struct lys_module *m = NULL; |
| 932 | |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 933 | FIND_MODULE(struct lysc_import, mod); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 934 | return m ? m->compiled : NULL; |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 935 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 936 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 937 | struct lysp_module * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 938 | lysp_module_find_prefix(const struct lysp_module *mod, const char *prefix, size_t len) |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 939 | { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 940 | const struct lys_module *m = NULL; |
| 941 | |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 942 | FIND_MODULE(struct lysp_import, mod); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 943 | return m ? m->parsed : NULL; |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 944 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 945 | |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 946 | struct lys_module * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 947 | lys_module_find_prefix(const struct lys_module *mod, const char *prefix, size_t len) |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 948 | { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 949 | const struct lys_module *m = NULL; |
| 950 | |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 951 | if (mod->compiled) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 952 | FIND_MODULE(struct lysc_import, mod->compiled); |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 953 | } else { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 954 | FIND_MODULE(struct lysp_import, mod->parsed); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 955 | } |
| 956 | return (struct lys_module*)m; |
| 957 | } |
| 958 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 959 | const char * |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 960 | lys_prefix_find_module(const struct lys_module *mod, const struct lys_module *import) |
| 961 | { |
| 962 | unsigned int u; |
| 963 | |
| 964 | if (import == mod) { |
| 965 | return mod->prefix; |
| 966 | } |
| 967 | |
| 968 | if (mod->parsed) { |
| 969 | LY_ARRAY_FOR(mod->parsed->imports, u) { |
| 970 | if (mod->parsed->imports[u].module == import) { |
| 971 | return mod->parsed->imports[u].prefix; |
| 972 | } |
| 973 | } |
| 974 | } else { |
| 975 | /* we don't have original information about the import's prefix, |
| 976 | * so the prefix of the import module itself is returned instead */ |
| 977 | return import->prefix; |
| 978 | } |
| 979 | |
| 980 | return NULL; |
| 981 | } |
| 982 | |
| 983 | const char * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 984 | lys_nodetype2str(uint16_t nodetype) |
| 985 | { |
| 986 | switch(nodetype) { |
| 987 | case LYS_CONTAINER: |
| 988 | return "container"; |
| 989 | case LYS_CHOICE: |
| 990 | return "choice"; |
| 991 | case LYS_LEAF: |
| 992 | return "leaf"; |
| 993 | case LYS_LEAFLIST: |
| 994 | return "leaf-list"; |
| 995 | case LYS_LIST: |
| 996 | return "list"; |
| 997 | case LYS_ANYXML: |
| 998 | return "anyxml"; |
| 999 | case LYS_ANYDATA: |
| 1000 | return "anydata"; |
Radek Krejci | f12a1f0 | 2019-02-11 16:42:08 +0100 | [diff] [blame] | 1001 | case LYS_CASE: |
| 1002 | return "case"; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1003 | case LYS_ACTION: |
| 1004 | return "RPC/action"; |
| 1005 | case LYS_NOTIF: |
| 1006 | return "Notification"; |
Radek Krejci | fc81ea8 | 2019-04-18 13:27:22 +0200 | [diff] [blame] | 1007 | case LYS_USES: |
| 1008 | return "uses"; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1009 | default: |
| 1010 | return "unknown"; |
| 1011 | } |
| 1012 | } |
| 1013 | |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1014 | const char * |
| 1015 | lys_datatype2str(LY_DATA_TYPE basetype) |
| 1016 | { |
| 1017 | switch(basetype) { |
| 1018 | case LY_TYPE_BINARY: |
| 1019 | return "binary"; |
| 1020 | case LY_TYPE_UINT8: |
| 1021 | return "uint8"; |
| 1022 | case LY_TYPE_UINT16: |
| 1023 | return "uint16"; |
| 1024 | case LY_TYPE_UINT32: |
| 1025 | return "uint32"; |
| 1026 | case LY_TYPE_UINT64: |
| 1027 | return "uint64"; |
| 1028 | case LY_TYPE_STRING: |
| 1029 | return "string"; |
| 1030 | case LY_TYPE_BITS: |
| 1031 | return "bits"; |
| 1032 | case LY_TYPE_BOOL: |
| 1033 | return "boolean"; |
| 1034 | case LY_TYPE_DEC64: |
| 1035 | return "decimal64"; |
| 1036 | case LY_TYPE_EMPTY: |
| 1037 | return "empty"; |
| 1038 | case LY_TYPE_ENUM: |
| 1039 | return "enumeration"; |
| 1040 | case LY_TYPE_IDENT: |
| 1041 | return "identityref"; |
| 1042 | case LY_TYPE_INST: |
| 1043 | return "instance-identifier"; |
| 1044 | case LY_TYPE_LEAFREF: |
| 1045 | return "leafref"; |
| 1046 | case LY_TYPE_UNION: |
| 1047 | return "union"; |
| 1048 | case LY_TYPE_INT8: |
| 1049 | return "int8"; |
| 1050 | case LY_TYPE_INT16: |
| 1051 | return "int16"; |
| 1052 | case LY_TYPE_INT32: |
| 1053 | return "int32"; |
| 1054 | case LY_TYPE_INT64: |
| 1055 | return "int64"; |
| 1056 | default: |
| 1057 | return "unknown"; |
| 1058 | } |
| 1059 | } |
| 1060 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1061 | API const struct lysp_tpdf * |
| 1062 | lysp_node_typedefs(const struct lysp_node *node) |
| 1063 | { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1064 | switch (node->nodetype) { |
| 1065 | case LYS_CONTAINER: |
| 1066 | return ((struct lysp_node_container*)node)->typedefs; |
| 1067 | case LYS_LIST: |
| 1068 | return ((struct lysp_node_list*)node)->typedefs; |
| 1069 | case LYS_GROUPING: |
| 1070 | return ((struct lysp_grp*)node)->typedefs; |
| 1071 | case LYS_ACTION: |
| 1072 | return ((struct lysp_action*)node)->typedefs; |
| 1073 | case LYS_INOUT: |
| 1074 | return ((struct lysp_action_inout*)node)->typedefs; |
| 1075 | case LYS_NOTIF: |
| 1076 | return ((struct lysp_notif*)node)->typedefs; |
| 1077 | default: |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1078 | return NULL; |
| 1079 | } |
| 1080 | } |
| 1081 | |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1082 | API const struct lysp_grp * |
| 1083 | lysp_node_groupings(const struct lysp_node *node) |
| 1084 | { |
| 1085 | switch (node->nodetype) { |
| 1086 | case LYS_CONTAINER: |
| 1087 | return ((struct lysp_node_container*)node)->groupings; |
| 1088 | case LYS_LIST: |
| 1089 | return ((struct lysp_node_list*)node)->groupings; |
| 1090 | case LYS_GROUPING: |
| 1091 | return ((struct lysp_grp*)node)->groupings; |
| 1092 | case LYS_ACTION: |
| 1093 | return ((struct lysp_action*)node)->groupings; |
| 1094 | case LYS_INOUT: |
| 1095 | return ((struct lysp_action_inout*)node)->groupings; |
| 1096 | case LYS_NOTIF: |
| 1097 | return ((struct lysp_notif*)node)->groupings; |
| 1098 | default: |
| 1099 | return NULL; |
| 1100 | } |
| 1101 | } |
| 1102 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1103 | struct lysp_action ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1104 | lysp_node_actions_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1105 | { |
| 1106 | assert(node); |
| 1107 | switch (node->nodetype) { |
| 1108 | case LYS_CONTAINER: |
| 1109 | return &((struct lysp_node_container*)node)->actions; |
| 1110 | case LYS_LIST: |
| 1111 | return &((struct lysp_node_list*)node)->actions; |
| 1112 | case LYS_GROUPING: |
| 1113 | return &((struct lysp_grp*)node)->actions; |
| 1114 | case LYS_AUGMENT: |
| 1115 | return &((struct lysp_augment*)node)->actions; |
| 1116 | default: |
| 1117 | return NULL; |
| 1118 | } |
| 1119 | } |
| 1120 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1121 | API const struct lysp_action * |
| 1122 | lysp_node_actions(const struct lysp_node *node) |
| 1123 | { |
| 1124 | struct lysp_action **actions; |
| 1125 | actions = lysp_node_actions_p((struct lysp_node*)node); |
| 1126 | if (actions) { |
| 1127 | return *actions; |
| 1128 | } else { |
| 1129 | return NULL; |
| 1130 | } |
| 1131 | } |
| 1132 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1133 | struct lysp_notif ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1134 | lysp_node_notifs_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1135 | { |
| 1136 | assert(node); |
| 1137 | switch (node->nodetype) { |
| 1138 | case LYS_CONTAINER: |
| 1139 | return &((struct lysp_node_container*)node)->notifs; |
| 1140 | case LYS_LIST: |
| 1141 | return &((struct lysp_node_list*)node)->notifs; |
| 1142 | case LYS_GROUPING: |
| 1143 | return &((struct lysp_grp*)node)->notifs; |
| 1144 | case LYS_AUGMENT: |
| 1145 | return &((struct lysp_augment*)node)->notifs; |
| 1146 | default: |
| 1147 | return NULL; |
| 1148 | } |
| 1149 | } |
| 1150 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1151 | API const struct lysp_notif * |
| 1152 | lysp_node_notifs(const struct lysp_node *node) |
| 1153 | { |
| 1154 | struct lysp_notif **notifs; |
| 1155 | notifs = lysp_node_notifs_p((struct lysp_node*)node); |
| 1156 | if (notifs) { |
| 1157 | return *notifs; |
| 1158 | } else { |
| 1159 | return NULL; |
| 1160 | } |
| 1161 | } |
| 1162 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1163 | struct lysp_node ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1164 | lysp_node_children_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1165 | { |
| 1166 | assert(node); |
| 1167 | switch (node->nodetype) { |
| 1168 | case LYS_CONTAINER: |
| 1169 | return &((struct lysp_node_container*)node)->child; |
| 1170 | case LYS_CHOICE: |
| 1171 | return &((struct lysp_node_choice*)node)->child; |
| 1172 | case LYS_LIST: |
| 1173 | return &((struct lysp_node_list*)node)->child; |
| 1174 | case LYS_CASE: |
| 1175 | return &((struct lysp_node_case*)node)->child; |
| 1176 | case LYS_GROUPING: |
| 1177 | return &((struct lysp_grp*)node)->data; |
| 1178 | case LYS_AUGMENT: |
| 1179 | return &((struct lysp_augment*)node)->child; |
| 1180 | case LYS_INOUT: |
| 1181 | return &((struct lysp_action_inout*)node)->data; |
| 1182 | case LYS_NOTIF: |
| 1183 | return &((struct lysp_notif*)node)->data; |
| 1184 | default: |
| 1185 | return NULL; |
| 1186 | } |
| 1187 | } |
| 1188 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1189 | API const struct lysp_node * |
| 1190 | lysp_node_children(const struct lysp_node *node) |
| 1191 | { |
| 1192 | struct lysp_node **children; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame^] | 1193 | |
| 1194 | if (!node) { |
| 1195 | return NULL; |
| 1196 | } |
| 1197 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1198 | children = lysp_node_children_p((struct lysp_node*)node); |
| 1199 | if (children) { |
| 1200 | return *children; |
| 1201 | } else { |
| 1202 | return NULL; |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | struct lysc_action ** |
| 1207 | lysc_node_actions_p(struct lysc_node *node) |
| 1208 | { |
| 1209 | assert(node); |
| 1210 | switch (node->nodetype) { |
| 1211 | case LYS_CONTAINER: |
| 1212 | return &((struct lysc_node_container*)node)->actions; |
| 1213 | case LYS_LIST: |
| 1214 | return &((struct lysc_node_list*)node)->actions; |
| 1215 | default: |
| 1216 | return NULL; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | API const struct lysc_action * |
| 1221 | lysc_node_actions(const struct lysc_node *node) |
| 1222 | { |
| 1223 | struct lysc_action **actions; |
| 1224 | actions = lysc_node_actions_p((struct lysc_node*)node); |
| 1225 | if (actions) { |
| 1226 | return *actions; |
| 1227 | } else { |
| 1228 | return NULL; |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | struct lysc_notif ** |
| 1233 | lysc_node_notifs_p(struct lysc_node *node) |
| 1234 | { |
| 1235 | assert(node); |
| 1236 | switch (node->nodetype) { |
| 1237 | case LYS_CONTAINER: |
| 1238 | return &((struct lysc_node_container*)node)->notifs; |
| 1239 | case LYS_LIST: |
| 1240 | return &((struct lysc_node_list*)node)->notifs; |
| 1241 | default: |
| 1242 | return NULL; |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | API const struct lysc_notif * |
| 1247 | lysc_node_notifs(const struct lysc_node *node) |
| 1248 | { |
| 1249 | struct lysc_notif **notifs; |
| 1250 | notifs = lysc_node_notifs_p((struct lysc_node*)node); |
| 1251 | if (notifs) { |
| 1252 | return *notifs; |
| 1253 | } else { |
| 1254 | return NULL; |
| 1255 | } |
| 1256 | } |
| 1257 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1258 | struct lysc_node ** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 1259 | lysc_node_children_p(const struct lysc_node *node, uint16_t flags) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1260 | { |
| 1261 | assert(node); |
| 1262 | switch (node->nodetype) { |
| 1263 | case LYS_CONTAINER: |
| 1264 | return &((struct lysc_node_container*)node)->child; |
| 1265 | case LYS_CHOICE: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1266 | if (((struct lysc_node_choice*)node)->cases) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1267 | return &((struct lysc_node_choice*)node)->cases->child; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1268 | } else { |
| 1269 | return NULL; |
| 1270 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 1271 | case LYS_CASE: |
| 1272 | return &((struct lysc_node_case*)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1273 | case LYS_LIST: |
| 1274 | return &((struct lysc_node_list*)node)->child; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 1275 | case LYS_ACTION: |
| 1276 | if (flags & LYS_CONFIG_R) { |
| 1277 | return &((struct lysc_action*)node)->output.data; |
| 1278 | } else { |
| 1279 | /* LYS_CONFIG_W, but also the default case */ |
| 1280 | return &((struct lysc_action*)node)->input.data; |
| 1281 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 1282 | case LYS_NOTIF: |
| 1283 | return &((struct lysc_notif*)node)->data; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1284 | default: |
| 1285 | return NULL; |
| 1286 | } |
| 1287 | } |
| 1288 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1289 | API const struct lysc_node * |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 1290 | lysc_node_children(const struct lysc_node *node, uint16_t flags) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1291 | { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1292 | struct lysc_node **children; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame^] | 1293 | |
| 1294 | if (!node) { |
| 1295 | return NULL; |
| 1296 | } |
| 1297 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 1298 | children = lysc_node_children_p((struct lysc_node*)node, flags); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1299 | if (children) { |
| 1300 | return *children; |
| 1301 | } else { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1302 | return NULL; |
| 1303 | } |
| 1304 | } |
| 1305 | |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1306 | struct lys_module * |
| 1307 | lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod) |
| 1308 | { |
| 1309 | unsigned int u; |
| 1310 | |
| 1311 | for (u = 0; u < ctx->list.count; ++u) { |
| 1312 | if (((struct lys_module*)ctx->list.objs[u])->parsed == mod) { |
| 1313 | return ((struct lys_module*)ctx->list.objs[u]); |
| 1314 | } |
| 1315 | } |
| 1316 | return NULL; |
| 1317 | } |
| 1318 | |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1319 | unsigned int |
| 1320 | lysp_ext_instance_iter(struct lysp_ext_instance *ext, unsigned int index, LYEXT_SUBSTMT substmt) |
| 1321 | { |
| 1322 | LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL); |
| 1323 | |
| 1324 | for (; index < LY_ARRAY_SIZE(ext); index++) { |
| 1325 | if (ext[index].insubstmt == substmt) { |
| 1326 | return index; |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | return LY_ARRAY_SIZE(ext); |
| 1331 | } |
| 1332 | |
| 1333 | |