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