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