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; |
| 408 | if (typedefs) { |
| 409 | LY_ARRAY_FOR(typedefs, v) { |
| 410 | if (!strcmp(name, typedefs[v].name)) { |
| 411 | /* match */ |
| 412 | *tpdf = &typedefs[v]; |
| 413 | return LY_SUCCESS; |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | return LY_ENOTFOUND; |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * @brief Check name of a new type to avoid name collisions. |
| 424 | * |
| 425 | * @param[in] ctx Parser context, module where the type is being defined is taken from here. |
| 426 | * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef. |
| 427 | * @param[in] tpdf Typedef definition to check. |
| 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 | * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's |
| 431 | * typedefs are checked, caller is supposed to free the table. |
| 432 | * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise. |
| 433 | */ |
| 434 | static LY_ERR |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 435 | 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] | 436 | struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped) |
| 437 | { |
| 438 | struct lysp_node *parent; |
| 439 | uint32_t hash; |
| 440 | size_t name_len; |
| 441 | const char *name; |
| 442 | unsigned int u; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 443 | const struct lysp_tpdf *typedefs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 444 | |
| 445 | assert(ctx); |
| 446 | assert(tpdf); |
| 447 | |
| 448 | name = tpdf->name; |
| 449 | name_len = strlen(name); |
| 450 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 451 | if (lysp_type_str2builtin(name, name_len)) { |
| 452 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 453 | "Invalid name \"%s\" of typedef - name collision with a built-in type.", name); |
| 454 | return LY_EEXIST; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /* check locally scoped typedefs (avoid name shadowing) */ |
| 458 | if (node) { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 459 | typedefs = lysp_node_typedefs(node); |
| 460 | LY_ARRAY_FOR(typedefs, u) { |
| 461 | if (&typedefs[u] == tpdf) { |
| 462 | break; |
| 463 | } |
| 464 | if (!strcmp(name, typedefs[u].name)) { |
| 465 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 466 | "Invalid name \"%s\" of typedef - name collision with sibling type.", name); |
| 467 | return LY_EEXIST; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | /* search typedefs in parent's nodes */ |
| 471 | for (parent = node->parent; parent; parent = node->parent) { |
| 472 | if (lysp_type_match(name, parent)) { |
| 473 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 474 | "Invalid name \"%s\" of typedef - name collision with another scoped type.", name); |
| 475 | return LY_EEXIST; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | /* check collision with the top-level typedefs */ |
| 481 | hash = dict_hash(name, name_len); |
| 482 | if (node) { |
| 483 | lyht_insert(tpdfs_scoped, &name, hash, NULL); |
| 484 | if (!lyht_find(tpdfs_global, &name, hash, NULL)) { |
| 485 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 486 | "Invalid name \"%s\" of typedef - scoped type collide with a top-level type.", name); |
| 487 | return LY_EEXIST; |
| 488 | } |
| 489 | } else { |
| 490 | if (lyht_insert(tpdfs_global, &name, hash, NULL)) { |
| 491 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 492 | "Invalid name \"%s\" of typedef - name collision with another top-level type.", name); |
| 493 | return LY_EEXIST; |
| 494 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 495 | /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the |
| 496 | * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision |
| 497 | * is detected in the first branch few lines above */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | return LY_SUCCESS; |
| 501 | } |
| 502 | |
| 503 | static int |
| 504 | lysp_id_cmp(void *val1, void *val2, int UNUSED(mod), void *UNUSED(cb_data)) |
| 505 | { |
| 506 | return !strcmp(val1, val2); |
| 507 | } |
| 508 | |
| 509 | LY_ERR |
| 510 | lysp_check_typedefs(struct ly_parser_ctx *ctx) |
| 511 | { |
| 512 | struct hash_table *ids_global; |
| 513 | struct hash_table *ids_scoped; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 514 | const struct lysp_tpdf *typedefs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 515 | unsigned int i, u; |
| 516 | LY_ERR ret = LY_EVALID; |
| 517 | |
| 518 | /* check name collisions - typedefs and groupings */ |
| 519 | ids_global = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1); |
| 520 | ids_scoped = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1); |
| 521 | LY_ARRAY_FOR(ctx->mod->typedefs, i) { |
| 522 | if (lysp_check_typedef(ctx, NULL, &ctx->mod->typedefs[i], ids_global, ids_scoped)) { |
| 523 | goto cleanup; |
| 524 | } |
| 525 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 526 | LY_ARRAY_FOR(ctx->mod->includes, i) { |
| 527 | LY_ARRAY_FOR(ctx->mod->includes[i].submodule->typedefs, u) { |
| 528 | if (lysp_check_typedef(ctx, NULL, &ctx->mod->includes[i].submodule->typedefs[u], ids_global, ids_scoped)) { |
| 529 | goto cleanup; |
| 530 | } |
| 531 | } |
| 532 | } |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 533 | for (u = 0; u < ctx->tpdfs_nodes.count; ++u) { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 534 | typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[u]); |
| 535 | LY_ARRAY_FOR(typedefs, i) { |
| 536 | 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] | 537 | goto cleanup; |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | ret = LY_SUCCESS; |
| 542 | cleanup: |
| 543 | lyht_free(ids_global); |
| 544 | lyht_free(ids_scoped); |
| 545 | ly_set_erase(&ctx->tpdfs_nodes, NULL); |
| 546 | |
| 547 | return ret; |
| 548 | } |
| 549 | |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 550 | void |
| 551 | lys_module_implement(struct lys_module *mod) |
| 552 | { |
| 553 | assert(mod); |
| 554 | if (mod->parsed) { |
| 555 | mod->parsed->implemented = 1; |
| 556 | } |
| 557 | if (mod->compiled) { |
| 558 | mod->compiled->implemented = 1; |
| 559 | } |
| 560 | } |
| 561 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 562 | struct lysp_load_module_check_data { |
| 563 | const char *name; |
| 564 | const char *revision; |
| 565 | const char *path; |
| 566 | const char* submoduleof; |
| 567 | }; |
| 568 | |
| 569 | static LY_ERR |
| 570 | lysp_load_module_check(struct ly_ctx *ctx, struct lysp_module *mod, void *data) |
| 571 | { |
| 572 | struct lysp_load_module_check_data *info = data; |
| 573 | const char *filename, *dot, *rev; |
| 574 | size_t len; |
| 575 | |
| 576 | if (info->name) { |
| 577 | /* check name of the parsed model */ |
| 578 | if (strcmp(info->name, mod->name)) { |
| 579 | LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", mod->name, info->name); |
| 580 | return LY_EINVAL; |
| 581 | } |
| 582 | } |
| 583 | if (info->revision) { |
| 584 | /* check revision of the parsed model */ |
| 585 | if (!mod->revs || strcmp(info->revision, mod->revs[0].date)) { |
| 586 | LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", mod->name, |
| 587 | mod->revs[0].date, info->revision); |
| 588 | return LY_EINVAL; |
| 589 | } |
| 590 | } |
| 591 | if (info->submoduleof) { |
| 592 | /* check that we have really a submodule */ |
| 593 | if (!mod->submodule) { |
| 594 | /* submodule is not a submodule */ |
| 595 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Included \"%s\" schema from \"%s\" is actually not a submodule.", |
| 596 | mod->name, info->submoduleof); |
| 597 | return LY_EVALID; |
| 598 | } |
| 599 | /* check that the submodule belongs-to our module */ |
| 600 | if (strcmp(info->submoduleof, mod->belongsto)) { |
| 601 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".", |
| 602 | mod->name, info->submoduleof, mod->belongsto); |
| 603 | return LY_EVALID; |
| 604 | } |
| 605 | /* check circular dependency */ |
| 606 | if (mod->parsing) { |
| 607 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", mod->name); |
| 608 | return LY_EVALID; |
| 609 | } |
| 610 | } |
| 611 | if (info->path) { |
| 612 | /* check that name and revision match filename */ |
| 613 | filename = strrchr(info->path, '/'); |
| 614 | if (!filename) { |
| 615 | filename = info->path; |
| 616 | } else { |
| 617 | filename++; |
| 618 | } |
| 619 | /* name */ |
| 620 | len = strlen(mod->name); |
| 621 | rev = strchr(filename, '@'); |
| 622 | dot = strrchr(info->path, '.'); |
| 623 | if (strncmp(filename, mod->name, len) || |
| 624 | ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) { |
| 625 | LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name); |
| 626 | } |
| 627 | /* revision */ |
| 628 | if (rev) { |
| 629 | len = dot - ++rev; |
| 630 | if (!mod->revs || len != 10 || strncmp(mod->revs[0].date, rev, len)) { |
| 631 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
| 632 | mod->revs ? mod->revs[0].date : "none"); |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | return LY_SUCCESS; |
| 637 | } |
| 638 | |
| 639 | LY_ERR |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 640 | 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] | 641 | struct lys_module **result) |
| 642 | { |
| 643 | int fd; |
| 644 | char *filepath = NULL; |
| 645 | LYS_INFORMAT format; |
| 646 | struct lys_module *mod = NULL; |
| 647 | LY_ERR ret = LY_SUCCESS; |
| 648 | struct lysp_load_module_check_data check_data = {0}; |
| 649 | |
| 650 | LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, revision, |
| 651 | &filepath, &format)); |
| 652 | LY_CHECK_ERR_RET(!filepath, LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", |
| 653 | name, revision ? "@" : "", revision ? revision : ""), LY_ENOTFOUND); |
| 654 | |
| 655 | |
| 656 | LOGVRB("Loading schema from \"%s\" file.", filepath); |
| 657 | |
| 658 | /* open the file */ |
| 659 | fd = open(filepath, O_RDONLY); |
| 660 | LY_CHECK_ERR_GOTO(fd < 0, LOGERR(ctx, LY_ESYS, "Unable to open data model file \"%s\" (%s).", |
| 661 | filepath, strerror(errno)); ret = LY_ESYS, cleanup); |
| 662 | |
| 663 | check_data.name = name; |
| 664 | check_data.revision = revision; |
| 665 | check_data.path = filepath; |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 666 | mod = lys_parse_fd_(ctx, fd, format, implement, main_ctx, |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 667 | lysp_load_module_check, &check_data); |
| 668 | close(fd); |
| 669 | LY_CHECK_ERR_GOTO(!mod, ly_errcode(ctx), cleanup); |
| 670 | |
| 671 | if (!mod->parsed->filepath) { |
| 672 | char rpath[PATH_MAX]; |
| 673 | if (realpath(filepath, rpath) != NULL) { |
| 674 | mod->parsed->filepath = lydict_insert(ctx, rpath, 0); |
| 675 | } else { |
| 676 | mod->parsed->filepath = lydict_insert(ctx, filepath, 0); |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | *result = mod; |
| 681 | |
| 682 | /* success */ |
| 683 | cleanup: |
| 684 | free(filepath); |
| 685 | return ret; |
| 686 | } |
| 687 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 688 | LY_ERR |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 689 | 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] | 690 | { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 691 | const char *module_data = NULL; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 692 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 693 | void (*module_data_free)(void *module_data, void *user_data) = NULL; |
| 694 | struct lysp_load_module_check_data check_data = {0}; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 695 | |
| 696 | /* try to get the module from the context */ |
| 697 | if (revision) { |
| 698 | *mod = (struct lys_module*)ly_ctx_get_module(ctx, name, revision); |
| 699 | } else { |
| 700 | *mod = (struct lys_module*)ly_ctx_get_module_latest(ctx, name); |
| 701 | } |
| 702 | |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 703 | if (!(*mod) || (require_parsed && !(*mod)->parsed)) { |
| 704 | (*mod) = NULL; |
| 705 | |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 706 | /* check collision with other implemented revision */ |
| 707 | if (implement && ly_ctx_get_module_implemented(ctx, name)) { |
| 708 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, |
| 709 | "Module \"%s\" is already present in other implemented revision.", name); |
| 710 | return LY_EDENIED; |
| 711 | } |
| 712 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 713 | /* 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] | 714 | if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 715 | search_clb: |
| 716 | if (ctx->imp_clb) { |
| 717 | if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 718 | &format, &module_data, &module_data_free) == LY_SUCCESS) { |
| 719 | check_data.name = name; |
| 720 | check_data.revision = revision; |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 721 | *mod = lys_parse_mem_(ctx, module_data, format, implement, NULL, |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 722 | lysp_load_module_check, &check_data); |
| 723 | if (module_data_free) { |
| 724 | module_data_free((void*)module_data, ctx->imp_clb_data); |
| 725 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 729 | goto search_file; |
| 730 | } |
| 731 | } else { |
| 732 | search_file: |
| 733 | if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) { |
| 734 | /* 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] | 735 | lys_module_localfile(ctx, name, revision, implement, NULL, mod); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 736 | } |
| 737 | if (!(*mod) && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 738 | goto search_clb; |
| 739 | } |
| 740 | } |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 741 | |
Radek Krejci | d14e969 | 2018-11-01 11:00:37 +0100 | [diff] [blame] | 742 | if ((*mod) && !revision && ((*mod)->parsed->latest_revision == 1)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 743 | /* update the latest_revision flag - here we have selected the latest available schema, |
| 744 | * consider that even the callback provides correct latest revision */ |
| 745 | (*mod)->parsed->latest_revision = 2; |
| 746 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 747 | } else { |
| 748 | /* we have module from the current context */ |
| 749 | if (implement && (ly_ctx_get_module_implemented(ctx, name) != *mod)) { |
| 750 | /* check collision with other implemented revision */ |
| 751 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, |
| 752 | "Module \"%s\" is already present in other implemented revision.", name); |
| 753 | *mod = NULL; |
| 754 | return LY_EDENIED; |
| 755 | } |
| 756 | |
| 757 | /* circular check */ |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 758 | if ((*mod)->parsed && (*mod)->parsed->parsing) { |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 759 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name); |
| 760 | *mod = NULL; |
| 761 | return LY_EVALID; |
| 762 | } |
| 763 | } |
| 764 | if (!(*mod)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 765 | 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] | 766 | return LY_EVALID; |
| 767 | } |
| 768 | |
| 769 | if (implement) { |
| 770 | /* mark the module implemented, check for collision was already done */ |
| 771 | lys_module_implement(*mod); |
| 772 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 773 | |
| 774 | return LY_SUCCESS; |
| 775 | } |
| 776 | |
| 777 | LY_ERR |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 778 | 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] | 779 | { |
| 780 | struct lys_module *submod; |
| 781 | const char *submodule_data = NULL; |
| 782 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
| 783 | void (*submodule_data_free)(void *module_data, void *user_data) = NULL; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 784 | struct lysp_load_module_check_data check_data = {0}; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 785 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 786 | /* 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] | 787 | if (!(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 788 | search_clb: |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 789 | if (ctx->ctx->imp_clb) { |
| 790 | 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] | 791 | &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) { |
| 792 | check_data.name = inc->name; |
| 793 | check_data.revision = inc->rev[0] ? inc->rev : NULL; |
| 794 | check_data.submoduleof = mod->name; |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 795 | submod = lys_parse_mem_(ctx->ctx, submodule_data, format, mod->implemented, ctx, |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 796 | lysp_load_module_check, &check_data); |
| 797 | if (submodule_data_free) { |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 798 | submodule_data_free((void*)submodule_data, ctx->ctx->imp_clb_data); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 799 | } |
| 800 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 801 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 802 | if (!submod && !(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 803 | goto search_file; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 804 | } |
Radek Krejci | 2d31ea7 | 2018-10-25 15:46:42 +0200 | [diff] [blame] | 805 | } else { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 806 | search_file: |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 807 | if (!(ctx->ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 808 | /* 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] | 809 | 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] | 810 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 811 | if (!submod && (ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 812 | goto search_clb; |
| 813 | } |
| 814 | } |
| 815 | if (submod) { |
| 816 | if (!inc->rev[0] && (submod->parsed->latest_revision == 1)) { |
| 817 | /* update the latest_revision flag - here we have selected the latest available schema, |
| 818 | * consider that even the callback provides correct latest revision */ |
| 819 | submod->parsed->latest_revision = 2; |
| 820 | } |
| 821 | |
| 822 | inc->submodule = submod->parsed; |
| 823 | free(submod); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 824 | } |
| 825 | if (!inc->submodule) { |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 826 | 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] | 827 | return LY_EVALID; |
| 828 | } |
| 829 | |
| 830 | return LY_SUCCESS; |
| 831 | } |
| 832 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 833 | #define FIND_MODULE(TYPE, MOD, ID) \ |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 834 | TYPE *imp; \ |
| 835 | if (!strncmp((MOD)->prefix, prefix, len) && (MOD)->prefix[len] == '\0') { \ |
| 836 | /* it is the prefix of the module itself */ \ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 837 | 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] | 838 | } \ |
| 839 | /* search in imports */ \ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 840 | if (!m) { \ |
| 841 | LY_ARRAY_FOR((MOD)->imports, TYPE, imp) { \ |
| 842 | if (!strncmp(imp->prefix, prefix, len) && (MOD)->prefix[len] == '\0') { \ |
| 843 | m = imp->module; \ |
| 844 | break; \ |
| 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 | } |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 848 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 849 | struct lysc_module * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 850 | 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] | 851 | { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 852 | const struct lys_module *m = NULL; |
| 853 | |
| 854 | FIND_MODULE(struct lysc_import, mod, 1); |
| 855 | return m ? m->compiled : NULL; |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 856 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 857 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 858 | struct lysp_module * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 859 | 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] | 860 | { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 861 | const struct lys_module *m = NULL; |
| 862 | |
| 863 | FIND_MODULE(struct lysp_import, mod, 1); |
| 864 | return m ? m->parsed : NULL; |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 865 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 866 | |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 867 | struct lys_module * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 868 | 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] | 869 | { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 870 | const struct lys_module *m = NULL; |
| 871 | |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 872 | if (mod->compiled) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 873 | FIND_MODULE(struct lysc_import, mod->compiled, 1); |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 874 | } else { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 875 | FIND_MODULE(struct lysp_import, mod->parsed, 2); |
| 876 | } |
| 877 | return (struct lys_module*)m; |
| 878 | } |
| 879 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 880 | const char * |
| 881 | lys_nodetype2str(uint16_t nodetype) |
| 882 | { |
| 883 | switch(nodetype) { |
| 884 | case LYS_CONTAINER: |
| 885 | return "container"; |
| 886 | case LYS_CHOICE: |
| 887 | return "choice"; |
| 888 | case LYS_LEAF: |
| 889 | return "leaf"; |
| 890 | case LYS_LEAFLIST: |
| 891 | return "leaf-list"; |
| 892 | case LYS_LIST: |
| 893 | return "list"; |
| 894 | case LYS_ANYXML: |
| 895 | return "anyxml"; |
| 896 | case LYS_ANYDATA: |
| 897 | return "anydata"; |
| 898 | default: |
| 899 | return "unknown"; |
| 900 | } |
| 901 | } |
| 902 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 903 | API const struct lysp_tpdf * |
| 904 | lysp_node_typedefs(const struct lysp_node *node) |
| 905 | { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 906 | switch (node->nodetype) { |
| 907 | case LYS_CONTAINER: |
| 908 | return ((struct lysp_node_container*)node)->typedefs; |
| 909 | case LYS_LIST: |
| 910 | return ((struct lysp_node_list*)node)->typedefs; |
| 911 | case LYS_GROUPING: |
| 912 | return ((struct lysp_grp*)node)->typedefs; |
| 913 | case LYS_ACTION: |
| 914 | return ((struct lysp_action*)node)->typedefs; |
| 915 | case LYS_INOUT: |
| 916 | return ((struct lysp_action_inout*)node)->typedefs; |
| 917 | case LYS_NOTIF: |
| 918 | return ((struct lysp_notif*)node)->typedefs; |
| 919 | default: |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 920 | return NULL; |
| 921 | } |
| 922 | } |
| 923 | |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame^] | 924 | API const struct lysp_grp * |
| 925 | lysp_node_groupings(const struct lysp_node *node) |
| 926 | { |
| 927 | switch (node->nodetype) { |
| 928 | case LYS_CONTAINER: |
| 929 | return ((struct lysp_node_container*)node)->groupings; |
| 930 | case LYS_LIST: |
| 931 | return ((struct lysp_node_list*)node)->groupings; |
| 932 | case LYS_GROUPING: |
| 933 | return ((struct lysp_grp*)node)->groupings; |
| 934 | case LYS_ACTION: |
| 935 | return ((struct lysp_action*)node)->groupings; |
| 936 | case LYS_INOUT: |
| 937 | return ((struct lysp_action_inout*)node)->groupings; |
| 938 | case LYS_NOTIF: |
| 939 | return ((struct lysp_notif*)node)->groupings; |
| 940 | default: |
| 941 | return NULL; |
| 942 | } |
| 943 | } |
| 944 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 945 | struct lysp_action ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 946 | lysp_node_actions_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 947 | { |
| 948 | assert(node); |
| 949 | switch (node->nodetype) { |
| 950 | case LYS_CONTAINER: |
| 951 | return &((struct lysp_node_container*)node)->actions; |
| 952 | case LYS_LIST: |
| 953 | return &((struct lysp_node_list*)node)->actions; |
| 954 | case LYS_GROUPING: |
| 955 | return &((struct lysp_grp*)node)->actions; |
| 956 | case LYS_AUGMENT: |
| 957 | return &((struct lysp_augment*)node)->actions; |
| 958 | default: |
| 959 | return NULL; |
| 960 | } |
| 961 | } |
| 962 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 963 | API const struct lysp_action * |
| 964 | lysp_node_actions(const struct lysp_node *node) |
| 965 | { |
| 966 | struct lysp_action **actions; |
| 967 | actions = lysp_node_actions_p((struct lysp_node*)node); |
| 968 | if (actions) { |
| 969 | return *actions; |
| 970 | } else { |
| 971 | return NULL; |
| 972 | } |
| 973 | } |
| 974 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 975 | struct lysp_notif ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 976 | lysp_node_notifs_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 977 | { |
| 978 | assert(node); |
| 979 | switch (node->nodetype) { |
| 980 | case LYS_CONTAINER: |
| 981 | return &((struct lysp_node_container*)node)->notifs; |
| 982 | case LYS_LIST: |
| 983 | return &((struct lysp_node_list*)node)->notifs; |
| 984 | case LYS_GROUPING: |
| 985 | return &((struct lysp_grp*)node)->notifs; |
| 986 | case LYS_AUGMENT: |
| 987 | return &((struct lysp_augment*)node)->notifs; |
| 988 | default: |
| 989 | return NULL; |
| 990 | } |
| 991 | } |
| 992 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 993 | API const struct lysp_notif * |
| 994 | lysp_node_notifs(const struct lysp_node *node) |
| 995 | { |
| 996 | struct lysp_notif **notifs; |
| 997 | notifs = lysp_node_notifs_p((struct lysp_node*)node); |
| 998 | if (notifs) { |
| 999 | return *notifs; |
| 1000 | } else { |
| 1001 | return NULL; |
| 1002 | } |
| 1003 | } |
| 1004 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1005 | struct lysp_node ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1006 | lysp_node_children_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1007 | { |
| 1008 | assert(node); |
| 1009 | switch (node->nodetype) { |
| 1010 | case LYS_CONTAINER: |
| 1011 | return &((struct lysp_node_container*)node)->child; |
| 1012 | case LYS_CHOICE: |
| 1013 | return &((struct lysp_node_choice*)node)->child; |
| 1014 | case LYS_LIST: |
| 1015 | return &((struct lysp_node_list*)node)->child; |
| 1016 | case LYS_CASE: |
| 1017 | return &((struct lysp_node_case*)node)->child; |
| 1018 | case LYS_GROUPING: |
| 1019 | return &((struct lysp_grp*)node)->data; |
| 1020 | case LYS_AUGMENT: |
| 1021 | return &((struct lysp_augment*)node)->child; |
| 1022 | case LYS_INOUT: |
| 1023 | return &((struct lysp_action_inout*)node)->data; |
| 1024 | case LYS_NOTIF: |
| 1025 | return &((struct lysp_notif*)node)->data; |
| 1026 | default: |
| 1027 | return NULL; |
| 1028 | } |
| 1029 | } |
| 1030 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1031 | API const struct lysp_node * |
| 1032 | lysp_node_children(const struct lysp_node *node) |
| 1033 | { |
| 1034 | struct lysp_node **children; |
| 1035 | children = lysp_node_children_p((struct lysp_node*)node); |
| 1036 | if (children) { |
| 1037 | return *children; |
| 1038 | } else { |
| 1039 | return NULL; |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | struct lysc_action ** |
| 1044 | lysc_node_actions_p(struct lysc_node *node) |
| 1045 | { |
| 1046 | assert(node); |
| 1047 | switch (node->nodetype) { |
| 1048 | case LYS_CONTAINER: |
| 1049 | return &((struct lysc_node_container*)node)->actions; |
| 1050 | case LYS_LIST: |
| 1051 | return &((struct lysc_node_list*)node)->actions; |
| 1052 | default: |
| 1053 | return NULL; |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | API const struct lysc_action * |
| 1058 | lysc_node_actions(const struct lysc_node *node) |
| 1059 | { |
| 1060 | struct lysc_action **actions; |
| 1061 | actions = lysc_node_actions_p((struct lysc_node*)node); |
| 1062 | if (actions) { |
| 1063 | return *actions; |
| 1064 | } else { |
| 1065 | return NULL; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | struct lysc_notif ** |
| 1070 | lysc_node_notifs_p(struct lysc_node *node) |
| 1071 | { |
| 1072 | assert(node); |
| 1073 | switch (node->nodetype) { |
| 1074 | case LYS_CONTAINER: |
| 1075 | return &((struct lysc_node_container*)node)->notifs; |
| 1076 | case LYS_LIST: |
| 1077 | return &((struct lysc_node_list*)node)->notifs; |
| 1078 | default: |
| 1079 | return NULL; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | API const struct lysc_notif * |
| 1084 | lysc_node_notifs(const struct lysc_node *node) |
| 1085 | { |
| 1086 | struct lysc_notif **notifs; |
| 1087 | notifs = lysc_node_notifs_p((struct lysc_node*)node); |
| 1088 | if (notifs) { |
| 1089 | return *notifs; |
| 1090 | } else { |
| 1091 | return NULL; |
| 1092 | } |
| 1093 | } |
| 1094 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1095 | struct lysc_node ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1096 | lysc_node_children_p(const struct lysc_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 lysc_node_container*)node)->child; |
| 1102 | case LYS_CHOICE: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1103 | if (((struct lysc_node_choice*)node)->cases) { |
| 1104 | return &((struct lysc_node_choice*)node)->cases[0].child; |
| 1105 | } else { |
| 1106 | return NULL; |
| 1107 | } |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1108 | case LYS_LIST: |
| 1109 | return &((struct lysc_node_list*)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1110 | /* TODO |
| 1111 | case LYS_INOUT: |
| 1112 | return &((struct lysc_action_inout*)node)->child; |
| 1113 | case LYS_NOTIF: |
| 1114 | return &((struct lysc_notif*)node)->child; |
| 1115 | */ |
| 1116 | default: |
| 1117 | return NULL; |
| 1118 | } |
| 1119 | } |
| 1120 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1121 | API const struct lysc_node * |
| 1122 | lysc_node_children(const struct lysc_node *node) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1123 | { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1124 | struct lysc_node **children; |
| 1125 | children = lysc_node_children_p((struct lysc_node*)node); |
| 1126 | if (children) { |
| 1127 | return *children; |
| 1128 | } else { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1129 | return NULL; |
| 1130 | } |
| 1131 | } |
| 1132 | |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1133 | struct lys_module * |
| 1134 | lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod) |
| 1135 | { |
| 1136 | unsigned int u; |
| 1137 | |
| 1138 | for (u = 0; u < ctx->list.count; ++u) { |
| 1139 | if (((struct lys_module*)ctx->list.objs[u])->parsed == mod) { |
| 1140 | return ((struct lys_module*)ctx->list.objs[u]); |
| 1141 | } |
| 1142 | } |
| 1143 | return NULL; |
| 1144 | } |
| 1145 | |