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 */ |
Radek Krejci | 87e78ca | 2019-05-02 09:51:29 +0200 | [diff] [blame] | 526 | for (parent = node->parent; parent; parent = parent->parent) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 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) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 750 | /* get the specific revision */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 751 | *mod = (struct lys_module*)ly_ctx_get_module(ctx, name, revision); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 752 | } else if (implement) { |
| 753 | /* prefer the implemented module instead of the latest one */ |
| 754 | *mod = (struct lys_module*)ly_ctx_get_module_implemented(ctx, name); |
| 755 | if (!*mod) { |
| 756 | /* there is no implemented module in the context, try to get the latest revision module */ |
| 757 | goto latest_in_the_context; |
| 758 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 759 | } else { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 760 | /* get the requested module of the latest revision in the context */ |
| 761 | latest_in_the_context: |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 762 | *mod = (struct lys_module*)ly_ctx_get_module_latest(ctx, name); |
| 763 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 764 | } |
| 765 | |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 766 | if (!(*mod) || (require_parsed && !(*mod)->parsed)) { |
| 767 | (*mod) = NULL; |
| 768 | |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 769 | /* check collision with other implemented revision */ |
| 770 | if (implement && ly_ctx_get_module_implemented(ctx, name)) { |
| 771 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, |
| 772 | "Module \"%s\" is already present in other implemented revision.", name); |
| 773 | return LY_EDENIED; |
| 774 | } |
| 775 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 776 | /* 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] | 777 | if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 778 | search_clb: |
| 779 | if (ctx->imp_clb) { |
| 780 | if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 781 | &format, &module_data, &module_data_free) == LY_SUCCESS) { |
| 782 | check_data.name = name; |
| 783 | check_data.revision = revision; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 784 | *mod = lys_parse_mem_module(ctx, module_data, format, implement, |
| 785 | lysp_load_module_check, &check_data); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 786 | if (module_data_free) { |
| 787 | module_data_free((void*)module_data, ctx->imp_clb_data); |
| 788 | } |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 789 | if (*mod && implement && lys_compile(*mod, 0)) { |
| 790 | ly_set_rm(&ctx->list, *mod, NULL); |
| 791 | lys_module_free(*mod, NULL); |
| 792 | *mod = NULL; |
| 793 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 794 | } |
| 795 | } |
| 796 | if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 797 | goto search_file; |
| 798 | } |
| 799 | } else { |
| 800 | search_file: |
| 801 | if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) { |
| 802 | /* 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] | 803 | lys_module_localfile(ctx, name, revision, implement, NULL, (void **)mod); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 804 | } |
| 805 | if (!(*mod) && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 806 | goto search_clb; |
| 807 | } |
| 808 | } |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 809 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 810 | if ((*mod) && !revision && ((*mod)->latest_revision == 1)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 811 | /* update the latest_revision flag - here we have selected the latest available schema, |
| 812 | * consider that even the callback provides correct latest revision */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 813 | (*mod)->latest_revision = 2; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 814 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 815 | } else { |
| 816 | /* we have module from the current context */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 817 | if (implement) { |
| 818 | m = ly_ctx_get_module_implemented(ctx, name); |
| 819 | if (m && m != *mod) { |
| 820 | /* check collision with other implemented revision */ |
| 821 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, |
| 822 | "Module \"%s\" is already present in other implemented revision.", name); |
| 823 | *mod = NULL; |
| 824 | return LY_EDENIED; |
| 825 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | /* circular check */ |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 829 | if ((*mod)->parsed && (*mod)->parsed->parsing) { |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 830 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name); |
| 831 | *mod = NULL; |
| 832 | return LY_EVALID; |
| 833 | } |
| 834 | } |
| 835 | if (!(*mod)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 836 | 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] | 837 | return LY_EVALID; |
| 838 | } |
| 839 | |
| 840 | if (implement) { |
| 841 | /* mark the module implemented, check for collision was already done */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 842 | (*mod)->implemented = 1; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 843 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 844 | |
| 845 | return LY_SUCCESS; |
| 846 | } |
| 847 | |
| 848 | LY_ERR |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 849 | 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] | 850 | { |
Radek Krejci | 3eb299d | 2019-04-08 15:07:44 +0200 | [diff] [blame] | 851 | struct lysp_submodule *submod = NULL; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 852 | const char *submodule_data = NULL; |
| 853 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
| 854 | void (*submodule_data_free)(void *module_data, void *user_data) = NULL; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 855 | struct lysp_load_module_check_data check_data = {0}; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 856 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 857 | /* 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] | 858 | if (!(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 859 | search_clb: |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 860 | if (ctx->ctx->imp_clb) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 861 | 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] | 862 | &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) { |
| 863 | check_data.name = inc->name; |
| 864 | check_data.revision = inc->rev[0] ? inc->rev : NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 865 | check_data.submoduleof = mod->mod->name; |
| 866 | submod = lys_parse_mem_submodule(ctx->ctx, submodule_data, format, ctx, |
| 867 | lysp_load_module_check, &check_data); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 868 | if (submodule_data_free) { |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 869 | submodule_data_free((void*)submodule_data, ctx->ctx->imp_clb_data); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 870 | } |
| 871 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 872 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 873 | if (!submod && !(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 874 | goto search_file; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 875 | } |
Radek Krejci | 2d31ea7 | 2018-10-25 15:46:42 +0200 | [diff] [blame] | 876 | } else { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 877 | search_file: |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 878 | if (!(ctx->ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 879 | /* submodule was not received from the callback or there is no callback set */ |
| 880 | 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] | 881 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 882 | if (!submod && (ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 883 | goto search_clb; |
| 884 | } |
| 885 | } |
| 886 | if (submod) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 887 | if (!inc->rev[0] && (submod->latest_revision == 1)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 888 | /* update the latest_revision flag - here we have selected the latest available schema, |
| 889 | * consider that even the callback provides correct latest revision */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 890 | submod->latest_revision = 2; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 891 | } |
| 892 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 893 | inc->submodule = submod; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 894 | } |
| 895 | if (!inc->submodule) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 896 | LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", |
| 897 | inc->name, mod->mod->name); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 898 | return LY_EVALID; |
| 899 | } |
| 900 | |
| 901 | return LY_SUCCESS; |
| 902 | } |
| 903 | |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 904 | #define FIND_MODULE(TYPE, MOD) \ |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 905 | TYPE *imp; \ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 906 | if (!strncmp((MOD)->mod->prefix, prefix, len) && (MOD)->mod->prefix[len] == '\0') { \ |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 907 | /* it is the prefix of the module itself */ \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 908 | 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] | 909 | } \ |
| 910 | /* search in imports */ \ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 911 | if (!m) { \ |
| 912 | LY_ARRAY_FOR((MOD)->imports, TYPE, imp) { \ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 913 | if (!strncmp(imp->prefix, prefix, len) && imp->prefix[len] == '\0') { \ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 914 | m = imp->module; \ |
| 915 | break; \ |
| 916 | } \ |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 917 | } \ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 918 | } |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 919 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 920 | struct lysc_module * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 921 | 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] | 922 | { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 923 | const struct lys_module *m = NULL; |
| 924 | |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 925 | FIND_MODULE(struct lysc_import, mod); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 926 | return m ? m->compiled : NULL; |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 927 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 928 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 929 | struct lysp_module * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 930 | 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] | 931 | { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 932 | const struct lys_module *m = NULL; |
| 933 | |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 934 | FIND_MODULE(struct lysp_import, mod); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 935 | return m ? m->parsed : NULL; |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 936 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 937 | |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 938 | struct lys_module * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 939 | 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] | 940 | { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 941 | const struct lys_module *m = NULL; |
| 942 | |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 943 | if (mod->compiled) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 944 | FIND_MODULE(struct lysc_import, mod->compiled); |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 945 | } else { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 946 | FIND_MODULE(struct lysp_import, mod->parsed); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 947 | } |
| 948 | return (struct lys_module*)m; |
| 949 | } |
| 950 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 951 | const char * |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 952 | lys_prefix_find_module(const struct lys_module *mod, const struct lys_module *import) |
| 953 | { |
| 954 | unsigned int u; |
| 955 | |
| 956 | if (import == mod) { |
| 957 | return mod->prefix; |
| 958 | } |
| 959 | |
| 960 | if (mod->parsed) { |
| 961 | LY_ARRAY_FOR(mod->parsed->imports, u) { |
| 962 | if (mod->parsed->imports[u].module == import) { |
| 963 | return mod->parsed->imports[u].prefix; |
| 964 | } |
| 965 | } |
| 966 | } else { |
| 967 | /* we don't have original information about the import's prefix, |
| 968 | * so the prefix of the import module itself is returned instead */ |
| 969 | return import->prefix; |
| 970 | } |
| 971 | |
| 972 | return NULL; |
| 973 | } |
| 974 | |
| 975 | const char * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 976 | lys_nodetype2str(uint16_t nodetype) |
| 977 | { |
| 978 | switch(nodetype) { |
| 979 | case LYS_CONTAINER: |
| 980 | return "container"; |
| 981 | case LYS_CHOICE: |
| 982 | return "choice"; |
| 983 | case LYS_LEAF: |
| 984 | return "leaf"; |
| 985 | case LYS_LEAFLIST: |
| 986 | return "leaf-list"; |
| 987 | case LYS_LIST: |
| 988 | return "list"; |
| 989 | case LYS_ANYXML: |
| 990 | return "anyxml"; |
| 991 | case LYS_ANYDATA: |
| 992 | return "anydata"; |
Radek Krejci | f12a1f0 | 2019-02-11 16:42:08 +0100 | [diff] [blame] | 993 | case LYS_CASE: |
| 994 | return "case"; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 995 | case LYS_ACTION: |
| 996 | return "RPC/action"; |
| 997 | case LYS_NOTIF: |
| 998 | return "Notification"; |
Radek Krejci | fc81ea8 | 2019-04-18 13:27:22 +0200 | [diff] [blame] | 999 | case LYS_USES: |
| 1000 | return "uses"; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1001 | default: |
| 1002 | return "unknown"; |
| 1003 | } |
| 1004 | } |
| 1005 | |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1006 | const char * |
| 1007 | lys_datatype2str(LY_DATA_TYPE basetype) |
| 1008 | { |
| 1009 | switch(basetype) { |
| 1010 | case LY_TYPE_BINARY: |
| 1011 | return "binary"; |
| 1012 | case LY_TYPE_UINT8: |
| 1013 | return "uint8"; |
| 1014 | case LY_TYPE_UINT16: |
| 1015 | return "uint16"; |
| 1016 | case LY_TYPE_UINT32: |
| 1017 | return "uint32"; |
| 1018 | case LY_TYPE_UINT64: |
| 1019 | return "uint64"; |
| 1020 | case LY_TYPE_STRING: |
| 1021 | return "string"; |
| 1022 | case LY_TYPE_BITS: |
| 1023 | return "bits"; |
| 1024 | case LY_TYPE_BOOL: |
| 1025 | return "boolean"; |
| 1026 | case LY_TYPE_DEC64: |
| 1027 | return "decimal64"; |
| 1028 | case LY_TYPE_EMPTY: |
| 1029 | return "empty"; |
| 1030 | case LY_TYPE_ENUM: |
| 1031 | return "enumeration"; |
| 1032 | case LY_TYPE_IDENT: |
| 1033 | return "identityref"; |
| 1034 | case LY_TYPE_INST: |
| 1035 | return "instance-identifier"; |
| 1036 | case LY_TYPE_LEAFREF: |
| 1037 | return "leafref"; |
| 1038 | case LY_TYPE_UNION: |
| 1039 | return "union"; |
| 1040 | case LY_TYPE_INT8: |
| 1041 | return "int8"; |
| 1042 | case LY_TYPE_INT16: |
| 1043 | return "int16"; |
| 1044 | case LY_TYPE_INT32: |
| 1045 | return "int32"; |
| 1046 | case LY_TYPE_INT64: |
| 1047 | return "int64"; |
| 1048 | default: |
| 1049 | return "unknown"; |
| 1050 | } |
| 1051 | } |
| 1052 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1053 | API const struct lysp_tpdf * |
| 1054 | lysp_node_typedefs(const struct lysp_node *node) |
| 1055 | { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1056 | switch (node->nodetype) { |
| 1057 | case LYS_CONTAINER: |
| 1058 | return ((struct lysp_node_container*)node)->typedefs; |
| 1059 | case LYS_LIST: |
| 1060 | return ((struct lysp_node_list*)node)->typedefs; |
| 1061 | case LYS_GROUPING: |
| 1062 | return ((struct lysp_grp*)node)->typedefs; |
| 1063 | case LYS_ACTION: |
| 1064 | return ((struct lysp_action*)node)->typedefs; |
| 1065 | case LYS_INOUT: |
| 1066 | return ((struct lysp_action_inout*)node)->typedefs; |
| 1067 | case LYS_NOTIF: |
| 1068 | return ((struct lysp_notif*)node)->typedefs; |
| 1069 | default: |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1070 | return NULL; |
| 1071 | } |
| 1072 | } |
| 1073 | |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1074 | API const struct lysp_grp * |
| 1075 | lysp_node_groupings(const struct lysp_node *node) |
| 1076 | { |
| 1077 | switch (node->nodetype) { |
| 1078 | case LYS_CONTAINER: |
| 1079 | return ((struct lysp_node_container*)node)->groupings; |
| 1080 | case LYS_LIST: |
| 1081 | return ((struct lysp_node_list*)node)->groupings; |
| 1082 | case LYS_GROUPING: |
| 1083 | return ((struct lysp_grp*)node)->groupings; |
| 1084 | case LYS_ACTION: |
| 1085 | return ((struct lysp_action*)node)->groupings; |
| 1086 | case LYS_INOUT: |
| 1087 | return ((struct lysp_action_inout*)node)->groupings; |
| 1088 | case LYS_NOTIF: |
| 1089 | return ((struct lysp_notif*)node)->groupings; |
| 1090 | default: |
| 1091 | return NULL; |
| 1092 | } |
| 1093 | } |
| 1094 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1095 | struct lysp_action ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1096 | lysp_node_actions_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1097 | { |
| 1098 | assert(node); |
| 1099 | switch (node->nodetype) { |
| 1100 | case LYS_CONTAINER: |
| 1101 | return &((struct lysp_node_container*)node)->actions; |
| 1102 | case LYS_LIST: |
| 1103 | return &((struct lysp_node_list*)node)->actions; |
| 1104 | case LYS_GROUPING: |
| 1105 | return &((struct lysp_grp*)node)->actions; |
| 1106 | case LYS_AUGMENT: |
| 1107 | return &((struct lysp_augment*)node)->actions; |
| 1108 | default: |
| 1109 | return NULL; |
| 1110 | } |
| 1111 | } |
| 1112 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1113 | API const struct lysp_action * |
| 1114 | lysp_node_actions(const struct lysp_node *node) |
| 1115 | { |
| 1116 | struct lysp_action **actions; |
| 1117 | actions = lysp_node_actions_p((struct lysp_node*)node); |
| 1118 | if (actions) { |
| 1119 | return *actions; |
| 1120 | } else { |
| 1121 | return NULL; |
| 1122 | } |
| 1123 | } |
| 1124 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1125 | struct lysp_notif ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1126 | lysp_node_notifs_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1127 | { |
| 1128 | assert(node); |
| 1129 | switch (node->nodetype) { |
| 1130 | case LYS_CONTAINER: |
| 1131 | return &((struct lysp_node_container*)node)->notifs; |
| 1132 | case LYS_LIST: |
| 1133 | return &((struct lysp_node_list*)node)->notifs; |
| 1134 | case LYS_GROUPING: |
| 1135 | return &((struct lysp_grp*)node)->notifs; |
| 1136 | case LYS_AUGMENT: |
| 1137 | return &((struct lysp_augment*)node)->notifs; |
| 1138 | default: |
| 1139 | return NULL; |
| 1140 | } |
| 1141 | } |
| 1142 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1143 | API const struct lysp_notif * |
| 1144 | lysp_node_notifs(const struct lysp_node *node) |
| 1145 | { |
| 1146 | struct lysp_notif **notifs; |
| 1147 | notifs = lysp_node_notifs_p((struct lysp_node*)node); |
| 1148 | if (notifs) { |
| 1149 | return *notifs; |
| 1150 | } else { |
| 1151 | return NULL; |
| 1152 | } |
| 1153 | } |
| 1154 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1155 | struct lysp_node ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1156 | lysp_node_children_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1157 | { |
| 1158 | assert(node); |
| 1159 | switch (node->nodetype) { |
| 1160 | case LYS_CONTAINER: |
| 1161 | return &((struct lysp_node_container*)node)->child; |
| 1162 | case LYS_CHOICE: |
| 1163 | return &((struct lysp_node_choice*)node)->child; |
| 1164 | case LYS_LIST: |
| 1165 | return &((struct lysp_node_list*)node)->child; |
| 1166 | case LYS_CASE: |
| 1167 | return &((struct lysp_node_case*)node)->child; |
| 1168 | case LYS_GROUPING: |
| 1169 | return &((struct lysp_grp*)node)->data; |
| 1170 | case LYS_AUGMENT: |
| 1171 | return &((struct lysp_augment*)node)->child; |
| 1172 | case LYS_INOUT: |
| 1173 | return &((struct lysp_action_inout*)node)->data; |
| 1174 | case LYS_NOTIF: |
| 1175 | return &((struct lysp_notif*)node)->data; |
| 1176 | default: |
| 1177 | return NULL; |
| 1178 | } |
| 1179 | } |
| 1180 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1181 | API const struct lysp_node * |
| 1182 | lysp_node_children(const struct lysp_node *node) |
| 1183 | { |
| 1184 | struct lysp_node **children; |
| 1185 | children = lysp_node_children_p((struct lysp_node*)node); |
| 1186 | if (children) { |
| 1187 | return *children; |
| 1188 | } else { |
| 1189 | return NULL; |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | struct lysc_action ** |
| 1194 | lysc_node_actions_p(struct lysc_node *node) |
| 1195 | { |
| 1196 | assert(node); |
| 1197 | switch (node->nodetype) { |
| 1198 | case LYS_CONTAINER: |
| 1199 | return &((struct lysc_node_container*)node)->actions; |
| 1200 | case LYS_LIST: |
| 1201 | return &((struct lysc_node_list*)node)->actions; |
| 1202 | default: |
| 1203 | return NULL; |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | API const struct lysc_action * |
| 1208 | lysc_node_actions(const struct lysc_node *node) |
| 1209 | { |
| 1210 | struct lysc_action **actions; |
| 1211 | actions = lysc_node_actions_p((struct lysc_node*)node); |
| 1212 | if (actions) { |
| 1213 | return *actions; |
| 1214 | } else { |
| 1215 | return NULL; |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | struct lysc_notif ** |
| 1220 | lysc_node_notifs_p(struct lysc_node *node) |
| 1221 | { |
| 1222 | assert(node); |
| 1223 | switch (node->nodetype) { |
| 1224 | case LYS_CONTAINER: |
| 1225 | return &((struct lysc_node_container*)node)->notifs; |
| 1226 | case LYS_LIST: |
| 1227 | return &((struct lysc_node_list*)node)->notifs; |
| 1228 | default: |
| 1229 | return NULL; |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | API const struct lysc_notif * |
| 1234 | lysc_node_notifs(const struct lysc_node *node) |
| 1235 | { |
| 1236 | struct lysc_notif **notifs; |
| 1237 | notifs = lysc_node_notifs_p((struct lysc_node*)node); |
| 1238 | if (notifs) { |
| 1239 | return *notifs; |
| 1240 | } else { |
| 1241 | return NULL; |
| 1242 | } |
| 1243 | } |
| 1244 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1245 | struct lysc_node ** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 1246 | lysc_node_children_p(const struct lysc_node *node, uint16_t flags) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1247 | { |
| 1248 | assert(node); |
| 1249 | switch (node->nodetype) { |
| 1250 | case LYS_CONTAINER: |
| 1251 | return &((struct lysc_node_container*)node)->child; |
| 1252 | case LYS_CHOICE: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1253 | if (((struct lysc_node_choice*)node)->cases) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1254 | return &((struct lysc_node_choice*)node)->cases->child; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1255 | } else { |
| 1256 | return NULL; |
| 1257 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 1258 | case LYS_CASE: |
| 1259 | return &((struct lysc_node_case*)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1260 | case LYS_LIST: |
| 1261 | return &((struct lysc_node_list*)node)->child; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 1262 | case LYS_ACTION: |
| 1263 | if (flags & LYS_CONFIG_R) { |
| 1264 | return &((struct lysc_action*)node)->output.data; |
| 1265 | } else { |
| 1266 | /* LYS_CONFIG_W, but also the default case */ |
| 1267 | return &((struct lysc_action*)node)->input.data; |
| 1268 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 1269 | case LYS_NOTIF: |
| 1270 | return &((struct lysc_notif*)node)->data; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1271 | default: |
| 1272 | return NULL; |
| 1273 | } |
| 1274 | } |
| 1275 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1276 | API const struct lysc_node * |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 1277 | lysc_node_children(const struct lysc_node *node, uint16_t flags) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1278 | { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1279 | struct lysc_node **children; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 1280 | children = lysc_node_children_p((struct lysc_node*)node, flags); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1281 | if (children) { |
| 1282 | return *children; |
| 1283 | } else { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1284 | return NULL; |
| 1285 | } |
| 1286 | } |
| 1287 | |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1288 | struct lys_module * |
| 1289 | lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod) |
| 1290 | { |
| 1291 | unsigned int u; |
| 1292 | |
| 1293 | for (u = 0; u < ctx->list.count; ++u) { |
| 1294 | if (((struct lys_module*)ctx->list.objs[u])->parsed == mod) { |
| 1295 | return ((struct lys_module*)ctx->list.objs[u]); |
| 1296 | } |
| 1297 | } |
| 1298 | return NULL; |
| 1299 | } |
| 1300 | |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1301 | unsigned int |
| 1302 | lysp_ext_instance_iter(struct lysp_ext_instance *ext, unsigned int index, LYEXT_SUBSTMT substmt) |
| 1303 | { |
| 1304 | LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL); |
| 1305 | |
| 1306 | for (; index < LY_ARRAY_SIZE(ext); index++) { |
| 1307 | if (ext[index].insubstmt == substmt) { |
| 1308 | return index; |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | return LY_ARRAY_SIZE(ext); |
| 1313 | } |
| 1314 | |
| 1315 | |