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