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