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; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 109 | |
| 110 | if (*id == '/') { |
| 111 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 112 | "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.", |
| 113 | nodeid_len ? nodeid_len : strlen(nodeid), nodeid); |
| 114 | return LY_EVALID; |
| 115 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 116 | } else { |
| 117 | /* absolute-schema-nodeid */ |
| 118 | nodeid_type = "absolute"; |
| 119 | context_module = ctx->mod_def; |
| 120 | |
| 121 | if (*id != '/') { |
| 122 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 123 | "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".", |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 124 | nodeid_len ? nodeid_len : strlen(nodeid), nodeid); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 125 | return LY_EVALID; |
| 126 | } |
| 127 | ++id; |
| 128 | } |
| 129 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 130 | while (*id && (ret = lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) { |
| 131 | if (prefix) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 132 | mod = lys_module_find_prefix(context_module, prefix, prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 133 | if (!mod) { |
| 134 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 135 | "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
| 136 | nodeid_type, id - nodeid, nodeid, prefix_len, prefix, context_module->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 137 | return LY_ENOTFOUND; |
| 138 | } |
| 139 | } else { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 140 | mod = context_module; |
| 141 | } |
| 142 | if (implement && !mod->implemented) { |
| 143 | /* make the module implemented */ |
| 144 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 145 | } |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 146 | 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] | 147 | if (!context) { |
| 148 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 149 | "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] | 150 | return LY_ENOTFOUND; |
| 151 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 152 | if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 153 | break; |
| 154 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 155 | if (*id != '/') { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 156 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 157 | "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.", |
| 158 | nodeid_type, id - nodeid + 1, nodeid); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 159 | return LY_EVALID; |
| 160 | } |
| 161 | ++id; |
| 162 | } |
| 163 | |
| 164 | if (ret == LY_SUCCESS) { |
| 165 | *target = context; |
| 166 | if (nodetype && !(context->nodetype & nodetype)) { |
| 167 | return LY_EDENIED; |
| 168 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 169 | } else { |
| 170 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 171 | "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.", |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 172 | nodeid_type, nodeid_len ? nodeid_len : strlen(nodeid), nodeid); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 178 | LY_ERR |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 179 | 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] | 180 | { |
| 181 | struct lysp_import *i; |
| 182 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 183 | if (module_prefix && &module_prefix != value && !strcmp(module_prefix, *value)) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 184 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, |
| 185 | "Prefix \"%s\" already used as module prefix.", *value); |
| 186 | return LY_EEXIST; |
| 187 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 188 | LY_ARRAY_FOR(imports, struct lysp_import, i) { |
| 189 | if (i->prefix && &i->prefix != value && !strcmp(i->prefix, *value)) { |
| 190 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", |
| 191 | *value, i->name); |
| 192 | return LY_EEXIST; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | return LY_SUCCESS; |
| 196 | } |
| 197 | |
| 198 | LY_ERR |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 199 | lysc_check_status(struct lysc_ctx *ctx, |
| 200 | uint16_t flags1, void *mod1, const char *name1, |
| 201 | uint16_t flags2, void *mod2, const char *name2) |
| 202 | { |
| 203 | uint16_t flg1, flg2; |
| 204 | |
| 205 | flg1 = (flags1 & LYS_STATUS_MASK) ? (flags1 & LYS_STATUS_MASK) : LYS_STATUS_CURR; |
| 206 | flg2 = (flags2 & LYS_STATUS_MASK) ? (flags2 & LYS_STATUS_MASK) : LYS_STATUS_CURR; |
| 207 | |
| 208 | if ((flg1 < flg2) && (mod1 == mod2)) { |
| 209 | if (ctx) { |
| 210 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 211 | "A %s definition \"%s\" is not allowed to reference %s definition \"%s\".", |
| 212 | flg1 == LYS_STATUS_CURR ? "current" : "deprecated", name1, |
| 213 | flg2 == LYS_STATUS_OBSLT ? "obsolete" : "deprecated", name2); |
| 214 | } |
| 215 | return LY_EVALID; |
| 216 | } |
| 217 | |
| 218 | return LY_SUCCESS; |
| 219 | } |
| 220 | |
| 221 | LY_ERR |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 222 | 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] | 223 | { |
| 224 | int i; |
| 225 | struct tm tm, tm_; |
| 226 | char *r; |
| 227 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 228 | LY_CHECK_ARG_RET(ctx ? ctx->ctx : NULL, date, LY_EINVAL); |
| 229 | 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] | 230 | |
| 231 | /* check format */ |
| 232 | for (i = 0; i < date_len; i++) { |
| 233 | if (i == 4 || i == 7) { |
| 234 | if (date[i] != '-') { |
| 235 | goto error; |
| 236 | } |
| 237 | } else if (!isdigit(date[i])) { |
| 238 | goto error; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* check content, e.g. 2018-02-31 */ |
| 243 | memset(&tm, 0, sizeof tm); |
| 244 | r = strptime(date, "%Y-%m-%d", &tm); |
| 245 | if (!r || r != &date[LY_REV_SIZE - 1]) { |
| 246 | goto error; |
| 247 | } |
| 248 | memcpy(&tm_, &tm, sizeof tm); |
| 249 | mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */ |
| 250 | if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */ |
| 251 | /* checking days is enough, since other errors |
| 252 | * have been checked by strptime() */ |
| 253 | goto error; |
| 254 | } |
| 255 | |
| 256 | return LY_SUCCESS; |
| 257 | |
| 258 | error: |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 259 | if (stmt) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 260 | if (ctx) { |
| 261 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LY_VCODE_INVAL, date_len, date, stmt); |
| 262 | } else { |
| 263 | LOGVAL(NULL, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, date_len, date, stmt); |
| 264 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 265 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 266 | return LY_EINVAL; |
| 267 | } |
| 268 | |
| 269 | void |
| 270 | lysp_sort_revisions(struct lysp_revision *revs) |
| 271 | { |
| 272 | uint8_t i, r; |
| 273 | struct lysp_revision rev; |
| 274 | |
| 275 | for (i = 1, r = 0; revs && i < LY_ARRAY_SIZE(revs); i++) { |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 276 | if (strcmp(revs[i].date, revs[r].date) > 0) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 277 | r = i; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | if (r) { |
| 282 | /* the newest revision is not on position 0, switch them */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 283 | memcpy(&rev, &revs[0], sizeof rev); |
| 284 | memcpy(&revs[0], &revs[r], sizeof rev); |
| 285 | memcpy(&revs[r], &rev, sizeof rev); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 286 | } |
| 287 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 288 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 289 | static const struct lysp_tpdf * |
| 290 | lysp_type_match(const char *name, struct lysp_node *node) |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 291 | { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 292 | const struct lysp_tpdf *typedefs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 293 | unsigned int u; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 294 | |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 295 | typedefs = lysp_node_typedefs(node); |
| 296 | LY_ARRAY_FOR(typedefs, u) { |
| 297 | if (!strcmp(name, typedefs[u].name)) { |
| 298 | /* match */ |
| 299 | return &typedefs[u]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | return NULL; |
| 304 | } |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 305 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 306 | static LY_DATA_TYPE |
| 307 | lysp_type_str2builtin(const char *name, size_t len) |
| 308 | { |
| 309 | if (len >= 4) { /* otherwise it does not match any built-in type */ |
| 310 | if (name[0] == 'b') { |
| 311 | if (name[1] == 'i') { |
| 312 | if (len == 6 && !strncmp(&name[2], "nary", 4)) { |
| 313 | return LY_TYPE_BINARY; |
| 314 | } else if (len == 4 && !strncmp(&name[2], "ts", 2)) { |
| 315 | return LY_TYPE_BITS; |
| 316 | } |
| 317 | } else if (len == 7 && !strncmp(&name[1], "oolean", 6)) { |
| 318 | return LY_TYPE_BOOL; |
| 319 | } |
| 320 | } else if (name[0] == 'd') { |
| 321 | if (len == 9 && !strncmp(&name[1], "ecimal64", 8)) { |
| 322 | return LY_TYPE_DEC64; |
| 323 | } |
| 324 | } else if (name[0] == 'e') { |
| 325 | if (len == 5 && !strncmp(&name[1], "mpty", 4)) { |
| 326 | return LY_TYPE_EMPTY; |
| 327 | } else if (len == 11 && !strncmp(&name[1], "numeration", 10)) { |
| 328 | return LY_TYPE_ENUM; |
| 329 | } |
| 330 | } else if (name[0] == 'i') { |
| 331 | if (name[1] == 'n') { |
| 332 | if (len == 4 && !strncmp(&name[2], "t8", 2)) { |
| 333 | return LY_TYPE_INT8; |
| 334 | } else if (len == 5) { |
| 335 | if (!strncmp(&name[2], "t16", 3)) { |
| 336 | return LY_TYPE_INT16; |
| 337 | } else if (!strncmp(&name[2], "t32", 3)) { |
| 338 | return LY_TYPE_INT32; |
| 339 | } else if (!strncmp(&name[2], "t64", 3)) { |
| 340 | return LY_TYPE_INT64; |
| 341 | } |
| 342 | } else if (len == 19 && !strncmp(&name[2], "stance-identifier", 17)) { |
| 343 | return LY_TYPE_INST; |
| 344 | } |
| 345 | } else if (len == 11 && !strncmp(&name[1], "dentityref", 10)) { |
| 346 | return LY_TYPE_IDENT; |
| 347 | } |
| 348 | } else if (name[0] == 'l') { |
| 349 | if (len == 7 && !strncmp(&name[1], "eafref", 6)) { |
| 350 | return LY_TYPE_LEAFREF; |
| 351 | } |
| 352 | } else if (name[0] == 's') { |
| 353 | if (len == 6 && !strncmp(&name[1], "tring", 5)) { |
| 354 | return LY_TYPE_STRING; |
| 355 | } |
| 356 | } else if (name[0] == 'u') { |
| 357 | if (name[1] == 'n') { |
| 358 | if (len == 5 && !strncmp(&name[2], "ion", 3)) { |
| 359 | return LY_TYPE_UNION; |
| 360 | } |
| 361 | } else if (name[1] == 'i' && name[2] == 'n' && name[3] == 't') { |
| 362 | if (len == 5 && name[4] == '8') { |
| 363 | return LY_TYPE_UINT8; |
| 364 | } else if (len == 6) { |
| 365 | if (!strncmp(&name[4], "16", 2)) { |
| 366 | return LY_TYPE_UINT16; |
| 367 | } else if (!strncmp(&name[4], "32", 2)) { |
| 368 | return LY_TYPE_UINT32; |
| 369 | } else if (!strncmp(&name[4], "64", 2)) { |
| 370 | return LY_TYPE_UINT64; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return LY_TYPE_UNKNOWN; |
| 378 | } |
| 379 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 380 | LY_ERR |
| 381 | 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] | 382 | 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] | 383 | { |
| 384 | const char *str, *name; |
| 385 | struct lysp_tpdf *typedefs; |
| 386 | unsigned int u, v; |
| 387 | |
| 388 | assert(id); |
| 389 | assert(start_module); |
| 390 | assert(tpdf); |
| 391 | assert(node); |
| 392 | assert(module); |
| 393 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 394 | *node = NULL; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 395 | str = strchr(id, ':'); |
| 396 | if (str) { |
| 397 | *module = lysp_module_find_prefix(start_module, id, str - id); |
| 398 | name = str + 1; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 399 | *type = LY_TYPE_UNKNOWN; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 400 | } else { |
| 401 | *module = start_module; |
| 402 | name = id; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 403 | |
| 404 | /* check for built-in types */ |
| 405 | *type = lysp_type_str2builtin(name, strlen(name)); |
| 406 | if (*type) { |
| 407 | *tpdf = NULL; |
| 408 | return LY_SUCCESS; |
| 409 | } |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 410 | } |
| 411 | LY_CHECK_RET(!(*module), LY_ENOTFOUND); |
| 412 | |
| 413 | if (start_node && *module == start_module) { |
| 414 | /* search typedefs in parent's nodes */ |
| 415 | *node = start_node; |
| 416 | while (*node) { |
| 417 | *tpdf = lysp_type_match(name, *node); |
| 418 | if (*tpdf) { |
| 419 | /* match */ |
| 420 | return LY_SUCCESS; |
| 421 | } |
| 422 | *node = (*node)->parent; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /* search in top-level typedefs */ |
| 427 | if ((*module)->typedefs) { |
| 428 | LY_ARRAY_FOR((*module)->typedefs, u) { |
| 429 | if (!strcmp(name, (*module)->typedefs[u].name)) { |
| 430 | /* match */ |
| 431 | *tpdf = &(*module)->typedefs[u]; |
| 432 | return LY_SUCCESS; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /* search in submodules' typedefs */ |
| 438 | LY_ARRAY_FOR((*module)->includes, u) { |
| 439 | typedefs = (*module)->includes[u].submodule->typedefs; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 440 | LY_ARRAY_FOR(typedefs, v) { |
| 441 | if (!strcmp(name, typedefs[v].name)) { |
| 442 | /* match */ |
| 443 | *tpdf = &typedefs[v]; |
| 444 | return LY_SUCCESS; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | return LY_ENOTFOUND; |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * @brief Check name of a new type to avoid name collisions. |
| 454 | * |
| 455 | * @param[in] ctx Parser context, module where the type is being defined is taken from here. |
| 456 | * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef. |
| 457 | * @param[in] tpdf Typedef definition to check. |
| 458 | * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's |
| 459 | * typedefs are checked, caller is supposed to free the table. |
| 460 | * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's |
| 461 | * typedefs are checked, caller is supposed to free the table. |
| 462 | * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise. |
| 463 | */ |
| 464 | static LY_ERR |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 465 | 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] | 466 | struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped) |
| 467 | { |
| 468 | struct lysp_node *parent; |
| 469 | uint32_t hash; |
| 470 | size_t name_len; |
| 471 | const char *name; |
| 472 | unsigned int u; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 473 | const struct lysp_tpdf *typedefs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 474 | |
| 475 | assert(ctx); |
| 476 | assert(tpdf); |
| 477 | |
| 478 | name = tpdf->name; |
| 479 | name_len = strlen(name); |
| 480 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 481 | if (lysp_type_str2builtin(name, name_len)) { |
| 482 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 483 | "Invalid name \"%s\" of typedef - name collision with a built-in type.", name); |
| 484 | return LY_EEXIST; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /* check locally scoped typedefs (avoid name shadowing) */ |
| 488 | if (node) { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 489 | typedefs = lysp_node_typedefs(node); |
| 490 | LY_ARRAY_FOR(typedefs, u) { |
| 491 | if (&typedefs[u] == tpdf) { |
| 492 | break; |
| 493 | } |
| 494 | if (!strcmp(name, typedefs[u].name)) { |
| 495 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 496 | "Invalid name \"%s\" of typedef - name collision with sibling type.", name); |
| 497 | return LY_EEXIST; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | /* search typedefs in parent's nodes */ |
| 501 | for (parent = node->parent; parent; parent = node->parent) { |
| 502 | if (lysp_type_match(name, parent)) { |
| 503 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 504 | "Invalid name \"%s\" of typedef - name collision with another scoped type.", name); |
| 505 | return LY_EEXIST; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | /* check collision with the top-level typedefs */ |
| 511 | hash = dict_hash(name, name_len); |
| 512 | if (node) { |
| 513 | lyht_insert(tpdfs_scoped, &name, hash, NULL); |
| 514 | if (!lyht_find(tpdfs_global, &name, hash, NULL)) { |
| 515 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 516 | "Invalid name \"%s\" of typedef - scoped type collide with a top-level type.", name); |
| 517 | return LY_EEXIST; |
| 518 | } |
| 519 | } else { |
| 520 | if (lyht_insert(tpdfs_global, &name, hash, NULL)) { |
| 521 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG, |
| 522 | "Invalid name \"%s\" of typedef - name collision with another top-level type.", name); |
| 523 | return LY_EEXIST; |
| 524 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 525 | /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the |
| 526 | * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision |
| 527 | * is detected in the first branch few lines above */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | return LY_SUCCESS; |
| 531 | } |
| 532 | |
| 533 | static int |
| 534 | lysp_id_cmp(void *val1, void *val2, int UNUSED(mod), void *UNUSED(cb_data)) |
| 535 | { |
| 536 | return !strcmp(val1, val2); |
| 537 | } |
| 538 | |
| 539 | LY_ERR |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 540 | lysp_check_typedefs(struct ly_parser_ctx *ctx, struct lysp_module *mod) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 541 | { |
| 542 | struct hash_table *ids_global; |
| 543 | struct hash_table *ids_scoped; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 544 | const struct lysp_tpdf *typedefs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 545 | unsigned int i, u; |
| 546 | LY_ERR ret = LY_EVALID; |
| 547 | |
| 548 | /* check name collisions - typedefs and groupings */ |
| 549 | ids_global = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1); |
| 550 | ids_scoped = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 551 | LY_ARRAY_FOR(mod->typedefs, i) { |
| 552 | 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] | 553 | goto cleanup; |
| 554 | } |
| 555 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 556 | LY_ARRAY_FOR(mod->includes, i) { |
| 557 | LY_ARRAY_FOR(mod->includes[i].submodule->typedefs, u) { |
| 558 | 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] | 559 | goto cleanup; |
| 560 | } |
| 561 | } |
| 562 | } |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 563 | for (u = 0; u < ctx->tpdfs_nodes.count; ++u) { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 564 | typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[u]); |
| 565 | LY_ARRAY_FOR(typedefs, i) { |
| 566 | 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] | 567 | goto cleanup; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | ret = LY_SUCCESS; |
| 572 | cleanup: |
| 573 | lyht_free(ids_global); |
| 574 | lyht_free(ids_scoped); |
| 575 | ly_set_erase(&ctx->tpdfs_nodes, NULL); |
| 576 | |
| 577 | return ret; |
| 578 | } |
| 579 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 580 | struct lysp_load_module_check_data { |
| 581 | const char *name; |
| 582 | const char *revision; |
| 583 | const char *path; |
| 584 | const char* submoduleof; |
| 585 | }; |
| 586 | |
| 587 | static LY_ERR |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 588 | 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] | 589 | { |
| 590 | struct lysp_load_module_check_data *info = data; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 591 | const char *filename, *dot, *rev, *name; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 592 | size_t len; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 593 | struct lysp_revision *revs; |
| 594 | |
| 595 | name = mod ? mod->mod->name : submod->name; |
| 596 | revs = mod ? mod->revs : submod->revs; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 597 | |
| 598 | if (info->name) { |
| 599 | /* check name of the parsed model */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 600 | if (strcmp(info->name, name)) { |
| 601 | 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] | 602 | return LY_EINVAL; |
| 603 | } |
| 604 | } |
| 605 | if (info->revision) { |
| 606 | /* check revision of the parsed model */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 607 | if (!revs || strcmp(info->revision, revs[0].date)) { |
| 608 | LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name, |
| 609 | revs[0].date, info->revision); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 610 | return LY_EINVAL; |
| 611 | } |
| 612 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 613 | if (submod) { |
| 614 | assert(info->submoduleof); |
| 615 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 616 | /* check that the submodule belongs-to our module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 617 | if (strcmp(info->submoduleof, submod->belongsto)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 618 | 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] | 619 | submod->name, info->submoduleof, submod->belongsto); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 620 | return LY_EVALID; |
| 621 | } |
| 622 | /* check circular dependency */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 623 | if (submod->parsing) { |
| 624 | 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] | 625 | return LY_EVALID; |
| 626 | } |
| 627 | } |
| 628 | if (info->path) { |
| 629 | /* check that name and revision match filename */ |
| 630 | filename = strrchr(info->path, '/'); |
| 631 | if (!filename) { |
| 632 | filename = info->path; |
| 633 | } else { |
| 634 | filename++; |
| 635 | } |
| 636 | /* name */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 637 | len = strlen(name); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 638 | rev = strchr(filename, '@'); |
| 639 | dot = strrchr(info->path, '.'); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 640 | if (strncmp(filename, name, len) || |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 641 | ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 642 | 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] | 643 | } |
| 644 | /* revision */ |
| 645 | if (rev) { |
| 646 | len = dot - ++rev; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 647 | if (!revs || len != 10 || strncmp(revs[0].date, rev, len)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 648 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 649 | revs ? revs[0].date : "none"); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | } |
| 653 | return LY_SUCCESS; |
| 654 | } |
| 655 | |
| 656 | LY_ERR |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 657 | 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] | 658 | void **result) |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 659 | { |
| 660 | int fd; |
| 661 | char *filepath = NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 662 | const char **fp; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 663 | LYS_INFORMAT format; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 664 | void *mod = NULL; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 665 | LY_ERR ret = LY_SUCCESS; |
| 666 | struct lysp_load_module_check_data check_data = {0}; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 667 | char rpath[PATH_MAX]; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 668 | |
| 669 | LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, revision, |
| 670 | &filepath, &format)); |
| 671 | LY_CHECK_ERR_RET(!filepath, LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", |
| 672 | name, revision ? "@" : "", revision ? revision : ""), LY_ENOTFOUND); |
| 673 | |
| 674 | |
| 675 | LOGVRB("Loading schema from \"%s\" file.", filepath); |
| 676 | |
| 677 | /* open the file */ |
| 678 | fd = open(filepath, O_RDONLY); |
| 679 | LY_CHECK_ERR_GOTO(fd < 0, LOGERR(ctx, LY_ESYS, "Unable to open data model file \"%s\" (%s).", |
| 680 | filepath, strerror(errno)); ret = LY_ESYS, cleanup); |
| 681 | |
| 682 | check_data.name = name; |
| 683 | check_data.revision = revision; |
| 684 | check_data.path = filepath; |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 685 | mod = lys_parse_fd_(ctx, fd, format, implement, main_ctx, |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 686 | lysp_load_module_check, &check_data); |
| 687 | close(fd); |
| 688 | LY_CHECK_ERR_GOTO(!mod, ly_errcode(ctx), cleanup); |
| 689 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 690 | if (main_ctx) { |
| 691 | fp = &((struct lysp_submodule*)mod)->filepath; |
| 692 | } else { |
| 693 | fp = &((struct lys_module*)mod)->filepath; |
| 694 | } |
| 695 | if (!(*fp)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 696 | if (realpath(filepath, rpath) != NULL) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 697 | (*fp) = lydict_insert(ctx, rpath, 0); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 698 | } else { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 699 | (*fp) = lydict_insert(ctx, filepath, 0); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
| 703 | *result = mod; |
| 704 | |
| 705 | /* success */ |
| 706 | cleanup: |
| 707 | free(filepath); |
| 708 | return ret; |
| 709 | } |
| 710 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 711 | LY_ERR |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 712 | 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] | 713 | { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 714 | const char *module_data = NULL; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 715 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 716 | void (*module_data_free)(void *module_data, void *user_data) = NULL; |
| 717 | struct lysp_load_module_check_data check_data = {0}; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 718 | struct lys_module *m; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 719 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 720 | assert(mod); |
| 721 | |
| 722 | if (!*mod) { |
| 723 | /* try to get the module from the context */ |
| 724 | if (revision) { |
| 725 | *mod = (struct lys_module*)ly_ctx_get_module(ctx, name, revision); |
| 726 | } else { |
| 727 | *mod = (struct lys_module*)ly_ctx_get_module_latest(ctx, name); |
| 728 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 729 | } |
| 730 | |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 731 | if (!(*mod) || (require_parsed && !(*mod)->parsed)) { |
| 732 | (*mod) = NULL; |
| 733 | |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 734 | /* check collision with other implemented revision */ |
| 735 | if (implement && ly_ctx_get_module_implemented(ctx, name)) { |
| 736 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, |
| 737 | "Module \"%s\" is already present in other implemented revision.", name); |
| 738 | return LY_EDENIED; |
| 739 | } |
| 740 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 741 | /* 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] | 742 | if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 743 | search_clb: |
| 744 | if (ctx->imp_clb) { |
| 745 | if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 746 | &format, &module_data, &module_data_free) == LY_SUCCESS) { |
| 747 | check_data.name = name; |
| 748 | check_data.revision = revision; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 749 | *mod = lys_parse_mem_module(ctx, module_data, format, implement, |
| 750 | lysp_load_module_check, &check_data); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 751 | if (module_data_free) { |
| 752 | module_data_free((void*)module_data, ctx->imp_clb_data); |
| 753 | } |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 754 | if (*mod && implement && lys_compile(*mod, 0)) { |
| 755 | ly_set_rm(&ctx->list, *mod, NULL); |
| 756 | lys_module_free(*mod, NULL); |
| 757 | *mod = NULL; |
| 758 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 762 | goto search_file; |
| 763 | } |
| 764 | } else { |
| 765 | search_file: |
| 766 | if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) { |
| 767 | /* 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] | 768 | lys_module_localfile(ctx, name, revision, implement, NULL, (void **)mod); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 769 | } |
| 770 | if (!(*mod) && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 771 | goto search_clb; |
| 772 | } |
| 773 | } |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 774 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 775 | if ((*mod) && !revision && ((*mod)->latest_revision == 1)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 776 | /* update the latest_revision flag - here we have selected the latest available schema, |
| 777 | * consider that even the callback provides correct latest revision */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 778 | (*mod)->latest_revision = 2; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 779 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 780 | } else { |
| 781 | /* we have module from the current context */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 782 | if (implement) { |
| 783 | m = ly_ctx_get_module_implemented(ctx, name); |
| 784 | if (m && m != *mod) { |
| 785 | /* check collision with other implemented revision */ |
| 786 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, |
| 787 | "Module \"%s\" is already present in other implemented revision.", name); |
| 788 | *mod = NULL; |
| 789 | return LY_EDENIED; |
| 790 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | /* circular check */ |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 794 | if ((*mod)->parsed && (*mod)->parsed->parsing) { |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 795 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name); |
| 796 | *mod = NULL; |
| 797 | return LY_EVALID; |
| 798 | } |
| 799 | } |
| 800 | if (!(*mod)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 801 | 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] | 802 | return LY_EVALID; |
| 803 | } |
| 804 | |
| 805 | if (implement) { |
| 806 | /* mark the module implemented, check for collision was already done */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 807 | (*mod)->implemented = 1; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 808 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 809 | |
| 810 | return LY_SUCCESS; |
| 811 | } |
| 812 | |
| 813 | LY_ERR |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 814 | 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] | 815 | { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 816 | struct lysp_submodule *submod; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 817 | const char *submodule_data = NULL; |
| 818 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
| 819 | void (*submodule_data_free)(void *module_data, void *user_data) = NULL; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 820 | struct lysp_load_module_check_data check_data = {0}; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 821 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 822 | /* 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] | 823 | if (!(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 824 | search_clb: |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 825 | if (ctx->ctx->imp_clb) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 826 | 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] | 827 | &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) { |
| 828 | check_data.name = inc->name; |
| 829 | check_data.revision = inc->rev[0] ? inc->rev : NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 830 | check_data.submoduleof = mod->mod->name; |
| 831 | submod = lys_parse_mem_submodule(ctx->ctx, submodule_data, format, ctx, |
| 832 | lysp_load_module_check, &check_data); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 833 | if (submodule_data_free) { |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 834 | submodule_data_free((void*)submodule_data, ctx->ctx->imp_clb_data); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 835 | } |
| 836 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 837 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 838 | if (!submod && !(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 839 | goto search_file; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 840 | } |
Radek Krejci | 2d31ea7 | 2018-10-25 15:46:42 +0200 | [diff] [blame] | 841 | } else { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 842 | search_file: |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 843 | if (!(ctx->ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 844 | /* submodule was not received from the callback or there is no callback set */ |
| 845 | 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] | 846 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 847 | if (!submod && (ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 848 | goto search_clb; |
| 849 | } |
| 850 | } |
| 851 | if (submod) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 852 | if (!inc->rev[0] && (submod->latest_revision == 1)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 853 | /* update the latest_revision flag - here we have selected the latest available schema, |
| 854 | * consider that even the callback provides correct latest revision */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 855 | submod->latest_revision = 2; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 856 | } |
| 857 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 858 | inc->submodule = submod; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 859 | } |
| 860 | if (!inc->submodule) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 861 | LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", |
| 862 | inc->name, mod->mod->name); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 863 | return LY_EVALID; |
| 864 | } |
| 865 | |
| 866 | return LY_SUCCESS; |
| 867 | } |
| 868 | |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 869 | #define FIND_MODULE(TYPE, MOD) \ |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 870 | TYPE *imp; \ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 871 | if (!strncmp((MOD)->mod->prefix, prefix, len) && (MOD)->mod->prefix[len] == '\0') { \ |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 872 | /* it is the prefix of the module itself */ \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 873 | 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] | 874 | } \ |
| 875 | /* search in imports */ \ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 876 | if (!m) { \ |
| 877 | LY_ARRAY_FOR((MOD)->imports, TYPE, imp) { \ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 878 | if (!strncmp(imp->prefix, prefix, len) && imp->prefix[len] == '\0') { \ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 879 | m = imp->module; \ |
| 880 | break; \ |
| 881 | } \ |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 882 | } \ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 883 | } |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 884 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 885 | struct lysc_module * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 886 | lysc_module_find_prefix(const struct lysc_module *mod, const char *prefix, size_t len) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 887 | { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 888 | const struct lys_module *m = NULL; |
| 889 | |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 890 | FIND_MODULE(struct lysc_import, mod); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 891 | return m ? m->compiled : NULL; |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 892 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 893 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 894 | struct lysp_module * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 895 | 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] | 896 | { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 897 | const struct lys_module *m = NULL; |
| 898 | |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 899 | FIND_MODULE(struct lysp_import, mod); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 900 | return m ? m->parsed : NULL; |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 901 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 902 | |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 903 | struct lys_module * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 904 | 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] | 905 | { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 906 | const struct lys_module *m = NULL; |
| 907 | |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 908 | if (mod->compiled) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 909 | FIND_MODULE(struct lysc_import, mod->compiled); |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 910 | } else { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 911 | FIND_MODULE(struct lysp_import, mod->parsed); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 912 | } |
| 913 | return (struct lys_module*)m; |
| 914 | } |
| 915 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 916 | const char * |
| 917 | lys_nodetype2str(uint16_t nodetype) |
| 918 | { |
| 919 | switch(nodetype) { |
| 920 | case LYS_CONTAINER: |
| 921 | return "container"; |
| 922 | case LYS_CHOICE: |
| 923 | return "choice"; |
| 924 | case LYS_LEAF: |
| 925 | return "leaf"; |
| 926 | case LYS_LEAFLIST: |
| 927 | return "leaf-list"; |
| 928 | case LYS_LIST: |
| 929 | return "list"; |
| 930 | case LYS_ANYXML: |
| 931 | return "anyxml"; |
| 932 | case LYS_ANYDATA: |
| 933 | return "anydata"; |
Radek Krejci | f12a1f0 | 2019-02-11 16:42:08 +0100 | [diff] [blame] | 934 | case LYS_CASE: |
| 935 | return "case"; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 936 | default: |
| 937 | return "unknown"; |
| 938 | } |
| 939 | } |
| 940 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 941 | API const struct lysp_tpdf * |
| 942 | lysp_node_typedefs(const struct lysp_node *node) |
| 943 | { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 944 | switch (node->nodetype) { |
| 945 | case LYS_CONTAINER: |
| 946 | return ((struct lysp_node_container*)node)->typedefs; |
| 947 | case LYS_LIST: |
| 948 | return ((struct lysp_node_list*)node)->typedefs; |
| 949 | case LYS_GROUPING: |
| 950 | return ((struct lysp_grp*)node)->typedefs; |
| 951 | case LYS_ACTION: |
| 952 | return ((struct lysp_action*)node)->typedefs; |
| 953 | case LYS_INOUT: |
| 954 | return ((struct lysp_action_inout*)node)->typedefs; |
| 955 | case LYS_NOTIF: |
| 956 | return ((struct lysp_notif*)node)->typedefs; |
| 957 | default: |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 958 | return NULL; |
| 959 | } |
| 960 | } |
| 961 | |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 962 | API const struct lysp_grp * |
| 963 | lysp_node_groupings(const struct lysp_node *node) |
| 964 | { |
| 965 | switch (node->nodetype) { |
| 966 | case LYS_CONTAINER: |
| 967 | return ((struct lysp_node_container*)node)->groupings; |
| 968 | case LYS_LIST: |
| 969 | return ((struct lysp_node_list*)node)->groupings; |
| 970 | case LYS_GROUPING: |
| 971 | return ((struct lysp_grp*)node)->groupings; |
| 972 | case LYS_ACTION: |
| 973 | return ((struct lysp_action*)node)->groupings; |
| 974 | case LYS_INOUT: |
| 975 | return ((struct lysp_action_inout*)node)->groupings; |
| 976 | case LYS_NOTIF: |
| 977 | return ((struct lysp_notif*)node)->groupings; |
| 978 | default: |
| 979 | return NULL; |
| 980 | } |
| 981 | } |
| 982 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 983 | struct lysp_action ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 984 | lysp_node_actions_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 985 | { |
| 986 | assert(node); |
| 987 | switch (node->nodetype) { |
| 988 | case LYS_CONTAINER: |
| 989 | return &((struct lysp_node_container*)node)->actions; |
| 990 | case LYS_LIST: |
| 991 | return &((struct lysp_node_list*)node)->actions; |
| 992 | case LYS_GROUPING: |
| 993 | return &((struct lysp_grp*)node)->actions; |
| 994 | case LYS_AUGMENT: |
| 995 | return &((struct lysp_augment*)node)->actions; |
| 996 | default: |
| 997 | return NULL; |
| 998 | } |
| 999 | } |
| 1000 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1001 | API const struct lysp_action * |
| 1002 | lysp_node_actions(const struct lysp_node *node) |
| 1003 | { |
| 1004 | struct lysp_action **actions; |
| 1005 | actions = lysp_node_actions_p((struct lysp_node*)node); |
| 1006 | if (actions) { |
| 1007 | return *actions; |
| 1008 | } else { |
| 1009 | return NULL; |
| 1010 | } |
| 1011 | } |
| 1012 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1013 | struct lysp_notif ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1014 | lysp_node_notifs_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1015 | { |
| 1016 | assert(node); |
| 1017 | switch (node->nodetype) { |
| 1018 | case LYS_CONTAINER: |
| 1019 | return &((struct lysp_node_container*)node)->notifs; |
| 1020 | case LYS_LIST: |
| 1021 | return &((struct lysp_node_list*)node)->notifs; |
| 1022 | case LYS_GROUPING: |
| 1023 | return &((struct lysp_grp*)node)->notifs; |
| 1024 | case LYS_AUGMENT: |
| 1025 | return &((struct lysp_augment*)node)->notifs; |
| 1026 | default: |
| 1027 | return NULL; |
| 1028 | } |
| 1029 | } |
| 1030 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1031 | API const struct lysp_notif * |
| 1032 | lysp_node_notifs(const struct lysp_node *node) |
| 1033 | { |
| 1034 | struct lysp_notif **notifs; |
| 1035 | notifs = lysp_node_notifs_p((struct lysp_node*)node); |
| 1036 | if (notifs) { |
| 1037 | return *notifs; |
| 1038 | } else { |
| 1039 | return NULL; |
| 1040 | } |
| 1041 | } |
| 1042 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1043 | struct lysp_node ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1044 | lysp_node_children_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1045 | { |
| 1046 | assert(node); |
| 1047 | switch (node->nodetype) { |
| 1048 | case LYS_CONTAINER: |
| 1049 | return &((struct lysp_node_container*)node)->child; |
| 1050 | case LYS_CHOICE: |
| 1051 | return &((struct lysp_node_choice*)node)->child; |
| 1052 | case LYS_LIST: |
| 1053 | return &((struct lysp_node_list*)node)->child; |
| 1054 | case LYS_CASE: |
| 1055 | return &((struct lysp_node_case*)node)->child; |
| 1056 | case LYS_GROUPING: |
| 1057 | return &((struct lysp_grp*)node)->data; |
| 1058 | case LYS_AUGMENT: |
| 1059 | return &((struct lysp_augment*)node)->child; |
| 1060 | case LYS_INOUT: |
| 1061 | return &((struct lysp_action_inout*)node)->data; |
| 1062 | case LYS_NOTIF: |
| 1063 | return &((struct lysp_notif*)node)->data; |
| 1064 | default: |
| 1065 | return NULL; |
| 1066 | } |
| 1067 | } |
| 1068 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1069 | API const struct lysp_node * |
| 1070 | lysp_node_children(const struct lysp_node *node) |
| 1071 | { |
| 1072 | struct lysp_node **children; |
| 1073 | children = lysp_node_children_p((struct lysp_node*)node); |
| 1074 | if (children) { |
| 1075 | return *children; |
| 1076 | } else { |
| 1077 | return NULL; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | struct lysc_action ** |
| 1082 | lysc_node_actions_p(struct lysc_node *node) |
| 1083 | { |
| 1084 | assert(node); |
| 1085 | switch (node->nodetype) { |
| 1086 | case LYS_CONTAINER: |
| 1087 | return &((struct lysc_node_container*)node)->actions; |
| 1088 | case LYS_LIST: |
| 1089 | return &((struct lysc_node_list*)node)->actions; |
| 1090 | default: |
| 1091 | return NULL; |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | API const struct lysc_action * |
| 1096 | lysc_node_actions(const struct lysc_node *node) |
| 1097 | { |
| 1098 | struct lysc_action **actions; |
| 1099 | actions = lysc_node_actions_p((struct lysc_node*)node); |
| 1100 | if (actions) { |
| 1101 | return *actions; |
| 1102 | } else { |
| 1103 | return NULL; |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | struct lysc_notif ** |
| 1108 | lysc_node_notifs_p(struct lysc_node *node) |
| 1109 | { |
| 1110 | assert(node); |
| 1111 | switch (node->nodetype) { |
| 1112 | case LYS_CONTAINER: |
| 1113 | return &((struct lysc_node_container*)node)->notifs; |
| 1114 | case LYS_LIST: |
| 1115 | return &((struct lysc_node_list*)node)->notifs; |
| 1116 | default: |
| 1117 | return NULL; |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | API const struct lysc_notif * |
| 1122 | lysc_node_notifs(const struct lysc_node *node) |
| 1123 | { |
| 1124 | struct lysc_notif **notifs; |
| 1125 | notifs = lysc_node_notifs_p((struct lysc_node*)node); |
| 1126 | if (notifs) { |
| 1127 | return *notifs; |
| 1128 | } else { |
| 1129 | return NULL; |
| 1130 | } |
| 1131 | } |
| 1132 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1133 | struct lysc_node ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1134 | lysc_node_children_p(const struct lysc_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1135 | { |
| 1136 | assert(node); |
| 1137 | switch (node->nodetype) { |
| 1138 | case LYS_CONTAINER: |
| 1139 | return &((struct lysc_node_container*)node)->child; |
| 1140 | case LYS_CHOICE: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1141 | if (((struct lysc_node_choice*)node)->cases) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1142 | return &((struct lysc_node_choice*)node)->cases->child; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1143 | } else { |
| 1144 | return NULL; |
| 1145 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 1146 | case LYS_CASE: |
| 1147 | return &((struct lysc_node_case*)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1148 | case LYS_LIST: |
| 1149 | return &((struct lysc_node_list*)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1150 | /* TODO |
| 1151 | case LYS_INOUT: |
| 1152 | return &((struct lysc_action_inout*)node)->child; |
| 1153 | case LYS_NOTIF: |
| 1154 | return &((struct lysc_notif*)node)->child; |
| 1155 | */ |
| 1156 | default: |
| 1157 | return NULL; |
| 1158 | } |
| 1159 | } |
| 1160 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1161 | API const struct lysc_node * |
| 1162 | lysc_node_children(const struct lysc_node *node) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1163 | { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1164 | struct lysc_node **children; |
| 1165 | children = lysc_node_children_p((struct lysc_node*)node); |
| 1166 | if (children) { |
| 1167 | return *children; |
| 1168 | } else { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1169 | return NULL; |
| 1170 | } |
| 1171 | } |
| 1172 | |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1173 | struct lys_module * |
| 1174 | lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod) |
| 1175 | { |
| 1176 | unsigned int u; |
| 1177 | |
| 1178 | for (u = 0; u < ctx->list.count; ++u) { |
| 1179 | if (((struct lys_module*)ctx->list.objs[u])->parsed == mod) { |
| 1180 | return ((struct lys_module*)ctx->list.objs[u]); |
| 1181 | } |
| 1182 | } |
| 1183 | return NULL; |
| 1184 | } |
| 1185 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 1186 | enum yang_keyword |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1187 | match_keyword(const char *data) |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 1188 | { |
| 1189 | /* TODO make this function usable in get_keyword function */ |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1190 | #define MOVE_IN(DATA, COUNT) (data)+=COUNT; |
| 1191 | #define IF_KEYWORD(STR, LEN, STMT) if (!strncmp((data), STR, LEN)) {MOVE_IN(data, LEN);kw=STMT;} |
| 1192 | #define IF_KEYWORD_PREFIX(STR, LEN) if (!strncmp((data), STR, LEN)) {MOVE_IN(data, LEN); |
| 1193 | #define IF_KEYWORD_PREFIX_END } |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1194 | |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1195 | enum yang_keyword kw = YANG_NONE; |
| 1196 | /* read the keyword itself */ |
| 1197 | switch (*data) { |
| 1198 | case 'a': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1199 | MOVE_IN(data, 1); |
| 1200 | IF_KEYWORD("rgument", 7, YANG_ARGUMENT) |
| 1201 | else IF_KEYWORD("ugment", 6, YANG_AUGMENT) |
| 1202 | else IF_KEYWORD("ction", 5, YANG_ACTION) |
| 1203 | else IF_KEYWORD_PREFIX("ny", 2) |
| 1204 | IF_KEYWORD("data", 4, YANG_ANYDATA) |
| 1205 | else IF_KEYWORD("xml", 3, YANG_ANYXML) |
| 1206 | IF_KEYWORD_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1207 | break; |
| 1208 | case 'b': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1209 | MOVE_IN(data, 1); |
| 1210 | IF_KEYWORD("ase", 3, YANG_BASE) |
| 1211 | else IF_KEYWORD("elongs-to", 9, YANG_BELONGS_TO) |
| 1212 | else IF_KEYWORD("it", 2, YANG_BIT) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1213 | break; |
| 1214 | case 'c': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1215 | MOVE_IN(data, 1); |
| 1216 | IF_KEYWORD("ase", 3, YANG_CASE) |
| 1217 | else IF_KEYWORD("hoice", 5, YANG_CHOICE) |
| 1218 | else IF_KEYWORD_PREFIX("on", 2) |
| 1219 | IF_KEYWORD("fig", 3, YANG_CONFIG) |
| 1220 | else IF_KEYWORD_PREFIX("ta", 2) |
| 1221 | IF_KEYWORD("ct", 2, YANG_CONTACT) |
| 1222 | else IF_KEYWORD("iner", 4, YANG_CONTAINER) |
| 1223 | IF_KEYWORD_PREFIX_END |
| 1224 | IF_KEYWORD_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1225 | break; |
| 1226 | case 'd': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1227 | MOVE_IN(data, 1); |
| 1228 | IF_KEYWORD_PREFIX("e", 1) |
| 1229 | IF_KEYWORD("fault", 5, YANG_DEFAULT) |
| 1230 | else IF_KEYWORD("scription", 9, YANG_DESCRIPTION) |
| 1231 | else IF_KEYWORD_PREFIX("viat", 4) |
| 1232 | IF_KEYWORD("e", 1, YANG_DEVIATE) |
| 1233 | else IF_KEYWORD("ion", 3, YANG_DEVIATION) |
| 1234 | IF_KEYWORD_PREFIX_END |
| 1235 | IF_KEYWORD_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1236 | break; |
| 1237 | case 'e': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1238 | MOVE_IN(data, 1); |
| 1239 | IF_KEYWORD("num", 3, YANG_ENUM) |
| 1240 | else IF_KEYWORD_PREFIX("rror-", 5) |
| 1241 | IF_KEYWORD("app-tag", 7, YANG_ERROR_APP_TAG) |
| 1242 | else IF_KEYWORD("message", 7, YANG_ERROR_MESSAGE) |
| 1243 | IF_KEYWORD_PREFIX_END |
| 1244 | else IF_KEYWORD("xtension", 8, YANG_EXTENSION) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1245 | break; |
| 1246 | case 'f': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1247 | MOVE_IN(data, 1); |
| 1248 | IF_KEYWORD("eature", 6, YANG_FEATURE) |
| 1249 | else IF_KEYWORD("raction-digits", 14, YANG_FRACTION_DIGITS) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1250 | break; |
| 1251 | case 'g': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1252 | MOVE_IN(data, 1); |
| 1253 | IF_KEYWORD("rouping", 7, YANG_GROUPING) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1254 | break; |
| 1255 | case 'i': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1256 | MOVE_IN(data, 1); |
| 1257 | IF_KEYWORD("dentity", 7, YANG_IDENTITY) |
| 1258 | else IF_KEYWORD("f-feature", 9, YANG_IF_FEATURE) |
| 1259 | else IF_KEYWORD("mport", 5, YANG_IMPORT) |
| 1260 | else IF_KEYWORD_PREFIX("n", 1) |
| 1261 | IF_KEYWORD("clude", 5, YANG_INCLUDE) |
| 1262 | else IF_KEYWORD("put", 3, YANG_INPUT) |
| 1263 | IF_KEYWORD_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1264 | break; |
| 1265 | case 'k': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1266 | MOVE_IN(data, 1); |
| 1267 | IF_KEYWORD("ey", 2, YANG_KEY) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1268 | break; |
| 1269 | case 'l': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1270 | MOVE_IN(data, 1); |
| 1271 | IF_KEYWORD_PREFIX("e", 1) |
| 1272 | IF_KEYWORD("af-list", 7, YANG_LEAF_LIST) |
| 1273 | else IF_KEYWORD("af", 2, YANG_LEAF) |
| 1274 | else IF_KEYWORD("ngth", 4, YANG_LENGTH) |
| 1275 | IF_KEYWORD_PREFIX_END |
| 1276 | else IF_KEYWORD("ist", 3, YANG_LIST) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1277 | break; |
| 1278 | case 'm': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1279 | MOVE_IN(data, 1); |
| 1280 | IF_KEYWORD_PREFIX("a", 1) |
| 1281 | IF_KEYWORD("ndatory", 7, YANG_MANDATORY) |
| 1282 | else IF_KEYWORD("x-elements", 10, YANG_MAX_ELEMENTS) |
| 1283 | IF_KEYWORD_PREFIX_END |
| 1284 | else IF_KEYWORD("in-elements", 11, YANG_MIN_ELEMENTS) |
| 1285 | else IF_KEYWORD("ust", 3, YANG_MUST) |
| 1286 | else IF_KEYWORD_PREFIX("od", 2) |
| 1287 | IF_KEYWORD("ule", 3, YANG_MODULE) |
| 1288 | else IF_KEYWORD("ifier", 5, YANG_MODIFIER) |
| 1289 | IF_KEYWORD_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1290 | break; |
| 1291 | case 'n': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1292 | MOVE_IN(data, 1); |
| 1293 | IF_KEYWORD("amespace", 8, YANG_NAMESPACE) |
| 1294 | else IF_KEYWORD("otification", 11, YANG_NOTIFICATION) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1295 | break; |
| 1296 | case 'o': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1297 | MOVE_IN(data, 1); |
| 1298 | IF_KEYWORD_PREFIX("r", 1) |
| 1299 | IF_KEYWORD("dered-by", 8, YANG_ORDERED_BY) |
| 1300 | else IF_KEYWORD("ganization", 10, YANG_ORGANIZATION) |
| 1301 | IF_KEYWORD_PREFIX_END |
| 1302 | else IF_KEYWORD("utput", 5, YANG_OUTPUT) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1303 | break; |
| 1304 | case 'p': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1305 | MOVE_IN(data, 1); |
| 1306 | IF_KEYWORD("ath", 3, YANG_PATH) |
| 1307 | else IF_KEYWORD("attern", 6, YANG_PATTERN) |
| 1308 | else IF_KEYWORD("osition", 7, YANG_POSITION) |
| 1309 | else IF_KEYWORD_PREFIX("re", 2) |
| 1310 | IF_KEYWORD("fix", 3, YANG_PREFIX) |
| 1311 | else IF_KEYWORD("sence", 5, YANG_PRESENCE) |
| 1312 | IF_KEYWORD_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1313 | break; |
| 1314 | case 'r': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1315 | MOVE_IN(data, 1); |
| 1316 | IF_KEYWORD("ange", 4, YANG_RANGE) |
| 1317 | else IF_KEYWORD_PREFIX("e", 1) |
| 1318 | IF_KEYWORD_PREFIX("f", 1) |
| 1319 | IF_KEYWORD("erence", 6, YANG_REFERENCE) |
| 1320 | else IF_KEYWORD("ine", 3, YANG_REFINE) |
| 1321 | IF_KEYWORD_PREFIX_END |
| 1322 | else IF_KEYWORD("quire-instance", 14, YANG_REQUIRE_INSTANCE) |
| 1323 | else IF_KEYWORD("vision-date", 11, YANG_REVISION_DATE) |
| 1324 | else IF_KEYWORD("vision", 6, YANG_REVISION) |
| 1325 | IF_KEYWORD_PREFIX_END |
| 1326 | else IF_KEYWORD("pc", 2, YANG_RPC) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1327 | break; |
| 1328 | case 's': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1329 | MOVE_IN(data, 1); |
| 1330 | IF_KEYWORD("tatus", 5, YANG_STATUS) |
| 1331 | else IF_KEYWORD("ubmodule", 8, YANG_SUBMODULE) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1332 | break; |
| 1333 | case 't': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1334 | MOVE_IN(data, 1); |
| 1335 | IF_KEYWORD("ypedef", 6, YANG_TYPEDEF) |
| 1336 | else IF_KEYWORD("ype", 3, YANG_TYPE) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1337 | break; |
| 1338 | case 'u': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1339 | MOVE_IN(data, 1); |
| 1340 | IF_KEYWORD_PREFIX("ni", 2) |
| 1341 | IF_KEYWORD("que", 3, YANG_UNIQUE) |
| 1342 | else IF_KEYWORD("ts", 2, YANG_UNITS) |
| 1343 | IF_KEYWORD_PREFIX_END |
| 1344 | else IF_KEYWORD("ses", 3, YANG_USES) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1345 | break; |
| 1346 | case 'v': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1347 | MOVE_IN(data, 1); |
| 1348 | IF_KEYWORD("alue", 4, YANG_VALUE) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1349 | break; |
| 1350 | case 'w': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1351 | MOVE_IN(data, 1); |
| 1352 | IF_KEYWORD("hen", 3, YANG_WHEN) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1353 | break; |
| 1354 | case 'y': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1355 | MOVE_IN(data, 1); |
| 1356 | IF_KEYWORD("ang-version", 11, YANG_YANG_VERSION) |
| 1357 | else IF_KEYWORD("in-element", 10, YANG_YIN_ELEMENT) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1358 | break; |
| 1359 | case ';': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1360 | MOVE_IN(data, 1); |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1361 | kw = YANG_SEMICOLON; |
| 1362 | //goto success; |
| 1363 | break; |
| 1364 | case '{': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1365 | MOVE_IN(data, 1); |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1366 | kw = YANG_LEFT_BRACE; |
| 1367 | //goto success; |
| 1368 | break; |
| 1369 | case '}': |
David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1370 | MOVE_IN(data, 1); |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1371 | kw = YANG_RIGHT_BRACE; |
| 1372 | //goto success; |
| 1373 | break; |
| 1374 | default: |
| 1375 | break; |
| 1376 | } |
| 1377 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 1378 | /* TODO important fix whole keyword must be matched */ |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1379 | return kw; |
| 1380 | } |