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> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 4 | * @brief Parsing and validation helper functions for schema trees |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 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 | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 14 | |
| 15 | #define _GNU_SOURCE |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 16 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 19 | #include <stddef.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | #include <stdint.h> |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 21 | #include <stdlib.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <string.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 23 | #include <time.h> |
| 24 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include "common.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 26 | #include "compat.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 27 | #include "context.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 28 | #include "dict.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 29 | #include "hash_table.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 30 | #include "in.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 31 | #include "in_internal.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 32 | #include "log.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 33 | #include "parser_schema.h" |
Michal Vasko | 962b6cd | 2020-12-08 10:07:49 +0100 | [diff] [blame] | 34 | #include "schema_compile.h" |
Michal Vasko | 79135ae | 2020-12-16 10:08:35 +0100 | [diff] [blame] | 35 | #include "schema_features.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 36 | #include "set.h" |
| 37 | #include "tree.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 38 | #include "tree_edit.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 39 | #include "tree_schema.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 40 | #include "tree_schema_internal.h" |
| 41 | |
Radek Krejci | 8574795 | 2019-06-07 16:43:43 +0200 | [diff] [blame] | 42 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 43 | lysp_check_prefix(struct lys_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] | 44 | { |
| 45 | struct lysp_import *i; |
| 46 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 47 | if (module_prefix && (&module_prefix != value) && !strcmp(module_prefix, *value)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 48 | LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 49 | return LY_EEXIST; |
| 50 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 51 | LY_ARRAY_FOR(imports, struct lysp_import, i) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 52 | if (i->prefix && (&i->prefix != value) && !strcmp(i->prefix, *value)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 53 | LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 54 | return LY_EEXIST; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | return LY_SUCCESS; |
| 58 | } |
| 59 | |
| 60 | LY_ERR |
Juraj Vijtiuk | 74dad9e | 2021-05-26 12:42:14 +0200 | [diff] [blame] | 61 | lysp_check_date(struct lys_parser_ctx *ctx, const char *date, size_t date_len, const char *stmt) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 62 | { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 63 | struct tm tm, tm_; |
| 64 | char *r; |
| 65 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 66 | LY_CHECK_ARG_RET(ctx ? PARSER_CTX(ctx) : NULL, date, LY_EINVAL); |
| 67 | LY_CHECK_ERR_RET(date_len != LY_REV_SIZE - 1, LOGARG(ctx ? PARSER_CTX(ctx) : NULL, date_len), LY_EINVAL); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 68 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 69 | /* check format: YYYY-MM-DD */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 70 | for (uint8_t i = 0; i < date_len; i++) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 71 | if ((i == 4) || (i == 7)) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 72 | if (date[i] != '-') { |
| 73 | goto error; |
| 74 | } |
| 75 | } else if (!isdigit(date[i])) { |
| 76 | goto error; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /* check content, e.g. 2018-02-31 */ |
| 81 | memset(&tm, 0, sizeof tm); |
| 82 | r = strptime(date, "%Y-%m-%d", &tm); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 83 | if (!r || (r != &date[LY_REV_SIZE - 1])) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 84 | goto error; |
| 85 | } |
| 86 | memcpy(&tm_, &tm, sizeof tm); |
| 87 | mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */ |
| 88 | if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */ |
| 89 | /* checking days is enough, since other errors |
| 90 | * have been checked by strptime() */ |
| 91 | goto error; |
| 92 | } |
| 93 | |
| 94 | return LY_SUCCESS; |
| 95 | |
| 96 | error: |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 97 | if (stmt) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 98 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 99 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 100 | return LY_EINVAL; |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | lysp_sort_revisions(struct lysp_revision *revs) |
| 105 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 106 | LY_ARRAY_COUNT_TYPE i, r; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 107 | struct lysp_revision rev; |
| 108 | |
Radek Krejci | c7d13e3 | 2020-12-09 12:32:24 +0100 | [diff] [blame] | 109 | for (i = 1, r = 0; i < LY_ARRAY_COUNT(revs); i++) { |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 110 | if (strcmp(revs[i].date, revs[r].date) > 0) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 111 | r = i; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (r) { |
| 116 | /* the newest revision is not on position 0, switch them */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 117 | memcpy(&rev, &revs[0], sizeof rev); |
| 118 | memcpy(&revs[0], &revs[r], sizeof rev); |
| 119 | memcpy(&revs[r], &rev, sizeof rev); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 120 | } |
| 121 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 122 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 123 | static const struct lysp_tpdf * |
| 124 | lysp_type_match(const char *name, struct lysp_node *node) |
| 125 | { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 126 | const struct lysp_tpdf *typedefs; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 127 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 128 | |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 129 | typedefs = lysp_node_typedefs(node); |
| 130 | LY_ARRAY_FOR(typedefs, u) { |
| 131 | if (!strcmp(name, typedefs[u].name)) { |
| 132 | /* match */ |
| 133 | return &typedefs[u]; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | return NULL; |
| 138 | } |
| 139 | |
aPiecek | 63e080d | 2021-06-29 13:53:28 +0200 | [diff] [blame] | 140 | static const struct lysp_node_grp * |
| 141 | lysp_grouping_match(const char *name, struct lysp_node *node) |
| 142 | { |
| 143 | const struct lysp_node_grp *groupings, *grp_iter; |
| 144 | |
| 145 | groupings = lysp_node_groupings(node); |
| 146 | LY_LIST_FOR(groupings, grp_iter) { |
| 147 | if (!strcmp(name, grp_iter->name)) { |
| 148 | /* match */ |
| 149 | return grp_iter; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return NULL; |
| 154 | } |
| 155 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 156 | static LY_DATA_TYPE |
| 157 | lysp_type_str2builtin(const char *name, size_t len) |
| 158 | { |
| 159 | if (len >= 4) { /* otherwise it does not match any built-in type */ |
| 160 | if (name[0] == 'b') { |
| 161 | if (name[1] == 'i') { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 162 | if ((len == 6) && !strncmp(&name[2], "nary", 4)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 163 | return LY_TYPE_BINARY; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 164 | } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 165 | return LY_TYPE_BITS; |
| 166 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 167 | } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 168 | return LY_TYPE_BOOL; |
| 169 | } |
| 170 | } else if (name[0] == 'd') { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 171 | if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 172 | return LY_TYPE_DEC64; |
| 173 | } |
| 174 | } else if (name[0] == 'e') { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 175 | if ((len == 5) && !strncmp(&name[1], "mpty", 4)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 176 | return LY_TYPE_EMPTY; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 177 | } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 178 | return LY_TYPE_ENUM; |
| 179 | } |
| 180 | } else if (name[0] == 'i') { |
| 181 | if (name[1] == 'n') { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 182 | if ((len == 4) && !strncmp(&name[2], "t8", 2)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 183 | return LY_TYPE_INT8; |
| 184 | } else if (len == 5) { |
| 185 | if (!strncmp(&name[2], "t16", 3)) { |
| 186 | return LY_TYPE_INT16; |
| 187 | } else if (!strncmp(&name[2], "t32", 3)) { |
| 188 | return LY_TYPE_INT32; |
| 189 | } else if (!strncmp(&name[2], "t64", 3)) { |
| 190 | return LY_TYPE_INT64; |
| 191 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 192 | } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 193 | return LY_TYPE_INST; |
| 194 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 195 | } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 196 | return LY_TYPE_IDENT; |
| 197 | } |
| 198 | } else if (name[0] == 'l') { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 199 | if ((len == 7) && !strncmp(&name[1], "eafref", 6)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 200 | return LY_TYPE_LEAFREF; |
| 201 | } |
| 202 | } else if (name[0] == 's') { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 203 | if ((len == 6) && !strncmp(&name[1], "tring", 5)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 204 | return LY_TYPE_STRING; |
| 205 | } |
| 206 | } else if (name[0] == 'u') { |
| 207 | if (name[1] == 'n') { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 208 | if ((len == 5) && !strncmp(&name[2], "ion", 3)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 209 | return LY_TYPE_UNION; |
| 210 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 211 | } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) { |
| 212 | if ((len == 5) && (name[4] == '8')) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 213 | return LY_TYPE_UINT8; |
| 214 | } else if (len == 6) { |
| 215 | if (!strncmp(&name[4], "16", 2)) { |
| 216 | return LY_TYPE_UINT16; |
| 217 | } else if (!strncmp(&name[4], "32", 2)) { |
| 218 | return LY_TYPE_UINT32; |
| 219 | } else if (!strncmp(&name[4], "64", 2)) { |
| 220 | return LY_TYPE_UINT64; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return LY_TYPE_UNKNOWN; |
| 228 | } |
| 229 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 230 | LY_ERR |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 231 | lysp_type_find(const char *id, struct lysp_node *start_node, const struct lysp_module *start_module, |
| 232 | LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 233 | { |
| 234 | const char *str, *name; |
| 235 | struct lysp_tpdf *typedefs; |
Michal Vasko | b2d55bf | 2020-11-02 15:42:43 +0100 | [diff] [blame] | 236 | const struct lys_module *mod; |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 237 | const struct lysp_module *local_module; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 238 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 239 | |
| 240 | assert(id); |
| 241 | assert(start_module); |
| 242 | assert(tpdf); |
| 243 | assert(node); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 244 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 245 | *node = NULL; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 246 | str = strchr(id, ':'); |
| 247 | if (str) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 248 | mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_VALUE_SCHEMA, (void *)start_module); |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 249 | local_module = mod ? mod->parsed : NULL; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 250 | name = str + 1; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 251 | *type = LY_TYPE_UNKNOWN; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 252 | } else { |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 253 | local_module = start_module; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 254 | name = id; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 255 | |
| 256 | /* check for built-in types */ |
| 257 | *type = lysp_type_str2builtin(name, strlen(name)); |
| 258 | if (*type) { |
| 259 | *tpdf = NULL; |
| 260 | return LY_SUCCESS; |
| 261 | } |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 262 | } |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 263 | LY_CHECK_RET(!local_module, LY_ENOTFOUND); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 264 | |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 265 | if (start_node && (local_module == start_module)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 266 | /* search typedefs in parent's nodes */ |
| 267 | *node = start_node; |
| 268 | while (*node) { |
| 269 | *tpdf = lysp_type_match(name, *node); |
| 270 | if (*tpdf) { |
| 271 | /* match */ |
| 272 | return LY_SUCCESS; |
| 273 | } |
| 274 | *node = (*node)->parent; |
| 275 | } |
| 276 | } |
| 277 | |
Michal Vasko | 915e544 | 2021-06-08 14:59:21 +0200 | [diff] [blame] | 278 | /* go to main module if in submodule */ |
| 279 | local_module = local_module->mod->parsed; |
| 280 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 281 | /* search in top-level typedefs */ |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 282 | if (local_module->typedefs) { |
| 283 | LY_ARRAY_FOR(local_module->typedefs, u) { |
| 284 | if (!strcmp(name, local_module->typedefs[u].name)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 285 | /* match */ |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 286 | *tpdf = &local_module->typedefs[u]; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 287 | return LY_SUCCESS; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
Michal Vasko | 915e544 | 2021-06-08 14:59:21 +0200 | [diff] [blame] | 292 | /* search in all submodules' typedefs */ |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 293 | LY_ARRAY_FOR(local_module->includes, u) { |
| 294 | typedefs = local_module->includes[u].submodule->typedefs; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 295 | LY_ARRAY_FOR(typedefs, v) { |
| 296 | if (!strcmp(name, typedefs[v].name)) { |
| 297 | /* match */ |
| 298 | *tpdf = &typedefs[v]; |
| 299 | return LY_SUCCESS; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | return LY_ENOTFOUND; |
| 305 | } |
| 306 | |
David Sedlák | 6544c18 | 2019-07-12 13:17:33 +0200 | [diff] [blame] | 307 | LY_ERR |
David Sedlák | 07869a5 | 2019-07-12 14:28:19 +0200 | [diff] [blame] | 308 | lysp_check_enum_name(struct lys_parser_ctx *ctx, const char *name, size_t name_len) |
David Sedlák | 6544c18 | 2019-07-12 13:17:33 +0200 | [diff] [blame] | 309 | { |
| 310 | if (!name_len) { |
| 311 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length."); |
| 312 | return LY_EVALID; |
| 313 | } else if (isspace(name[0]) || isspace(name[name_len - 1])) { |
| 314 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 315 | (int)name_len, name); |
David Sedlák | 6544c18 | 2019-07-12 13:17:33 +0200 | [diff] [blame] | 316 | return LY_EVALID; |
| 317 | } else { |
| 318 | for (size_t u = 0; u < name_len; ++u) { |
| 319 | if (iscntrl(name[u])) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 320 | LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 321 | (int)name_len, name, u + 1); |
David Sedlák | 6544c18 | 2019-07-12 13:17:33 +0200 | [diff] [blame] | 322 | break; |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | return LY_SUCCESS; |
| 328 | } |
| 329 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 330 | /** |
aPiecek | dc12b9f | 2021-06-25 10:55:47 +0200 | [diff] [blame] | 331 | * @brief Insert @p name to hash table and if @p name has already |
| 332 | * been added, then log an error. |
| 333 | * |
| 334 | * This function is used to detect duplicate names. |
| 335 | * |
| 336 | * @param[in,out] ctx Context to log the error. |
| 337 | * @param[in,out] ht Hash table with top-level names. |
| 338 | * @param[in] name Inserted top-level identifier. |
| 339 | * @param[in] statement The name of the statement type from which |
| 340 | * @p name originated (eg typedef, feature, ...). |
| 341 | * @param[in] err_detail Optional error specification. |
| 342 | * @return LY_ERR, but LY_EEXIST is mapped to LY_EVALID. |
| 343 | */ |
| 344 | static LY_ERR |
| 345 | lysp_check_dup_ht_insert(struct lys_parser_ctx *ctx, struct hash_table *ht, |
| 346 | const char *name, const char *statement, const char *err_detail) |
| 347 | { |
| 348 | LY_ERR ret; |
| 349 | uint32_t hash; |
| 350 | |
| 351 | hash = dict_hash(name, strlen(name)); |
| 352 | ret = lyht_insert(ht, &name, hash, NULL); |
| 353 | if (ret == LY_EEXIST) { |
| 354 | if (err_detail) { |
| 355 | LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT2, name, statement, err_detail); |
| 356 | } else { |
| 357 | LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, name, statement); |
| 358 | } |
| 359 | ret = LY_EVALID; |
| 360 | } |
| 361 | |
| 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | /** |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 366 | * @brief Check name of a new type to avoid name collisions. |
| 367 | * |
| 368 | * @param[in] ctx Parser context, module where the type is being defined is taken from here. |
| 369 | * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef. |
| 370 | * @param[in] tpdf Typedef definition to check. |
| 371 | * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's |
aPiecek | c7594b7 | 2021-06-29 09:00:03 +0200 | [diff] [blame] | 372 | * typedefs are checked, caller is supposed to free the table. |
| 373 | * @return LY_EVALID in case of collision, LY_SUCCESS otherwise. |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 374 | */ |
| 375 | static LY_ERR |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 376 | lysp_check_dup_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf, |
aPiecek | e1fbd95 | 2021-06-29 08:12:55 +0200 | [diff] [blame] | 377 | struct hash_table *tpdfs_global) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 378 | { |
| 379 | struct lysp_node *parent; |
| 380 | uint32_t hash; |
| 381 | size_t name_len; |
| 382 | const char *name; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 383 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 384 | const struct lysp_tpdf *typedefs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 385 | |
| 386 | assert(ctx); |
| 387 | assert(tpdf); |
| 388 | |
| 389 | name = tpdf->name; |
| 390 | name_len = strlen(name); |
| 391 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 392 | if (lysp_type_str2builtin(name, name_len)) { |
aPiecek | c7594b7 | 2021-06-29 09:00:03 +0200 | [diff] [blame] | 393 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, |
| 394 | "Duplicate identifier \"%s\" of typedef statement - name collision with a built-in type.", name); |
| 395 | return LY_EVALID; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /* check locally scoped typedefs (avoid name shadowing) */ |
| 399 | if (node) { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 400 | typedefs = lysp_node_typedefs(node); |
| 401 | LY_ARRAY_FOR(typedefs, u) { |
| 402 | if (&typedefs[u] == tpdf) { |
| 403 | break; |
| 404 | } |
| 405 | if (!strcmp(name, typedefs[u].name)) { |
aPiecek | c7594b7 | 2021-06-29 09:00:03 +0200 | [diff] [blame] | 406 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, |
| 407 | "Duplicate identifier \"%s\" of typedef statement - name collision with sibling type.", name); |
| 408 | return LY_EVALID; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | /* search typedefs in parent's nodes */ |
Radek Krejci | 87e78ca | 2019-05-02 09:51:29 +0200 | [diff] [blame] | 412 | for (parent = node->parent; parent; parent = parent->parent) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 413 | if (lysp_type_match(name, parent)) { |
aPiecek | c7594b7 | 2021-06-29 09:00:03 +0200 | [diff] [blame] | 414 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, |
| 415 | "Duplicate identifier \"%s\" of typedef statement - name collision with another scoped type.", name); |
| 416 | return LY_EVALID; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /* check collision with the top-level typedefs */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 422 | if (node) { |
aPiecek | c7594b7 | 2021-06-29 09:00:03 +0200 | [diff] [blame] | 423 | hash = dict_hash(name, name_len); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 424 | if (!lyht_find(tpdfs_global, &name, hash, NULL)) { |
aPiecek | c7594b7 | 2021-06-29 09:00:03 +0200 | [diff] [blame] | 425 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, |
| 426 | "Duplicate identifier \"%s\" of typedef statement - scoped type collide with a top-level type.", name); |
| 427 | return LY_EVALID; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 428 | } |
| 429 | } else { |
aPiecek | c7594b7 | 2021-06-29 09:00:03 +0200 | [diff] [blame] | 430 | LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, tpdfs_global, name, "typedef", |
| 431 | "name collision with another top-level type")); |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 432 | /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the |
| 433 | * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision |
| 434 | * is detected in the first branch few lines above */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | return LY_SUCCESS; |
| 438 | } |
| 439 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 440 | /** |
| 441 | * @brief Compare identifiers. |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 442 | * Implementation of ::lyht_value_equal_cb. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 443 | */ |
| 444 | static ly_bool |
| 445 | lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data)) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 446 | { |
Michal Vasko | 11ac39a | 2021-07-23 12:46:56 +0200 | [diff] [blame] | 447 | char *id1, *id2; |
| 448 | |
| 449 | id1 = *(char **)val1; |
| 450 | id2 = *(char **)val2; |
| 451 | |
| 452 | return strcmp(id1, id2) == 0 ? 1 : 0; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | LY_ERR |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 456 | lysp_check_dup_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 457 | { |
| 458 | struct hash_table *ids_global; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 459 | const struct lysp_tpdf *typedefs; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 460 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 461 | uint32_t i; |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 462 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 463 | |
| 464 | /* check name collisions - typedefs and groupings */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 465 | ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1); |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 466 | LY_ARRAY_FOR(mod->typedefs, v) { |
aPiecek | e1fbd95 | 2021-06-29 08:12:55 +0200 | [diff] [blame] | 467 | ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 468 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 469 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 470 | LY_ARRAY_FOR(mod->includes, v) { |
| 471 | LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) { |
aPiecek | e1fbd95 | 2021-06-29 08:12:55 +0200 | [diff] [blame] | 472 | ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 473 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 474 | } |
| 475 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 476 | for (i = 0; i < ctx->tpdfs_nodes.count; ++i) { |
| 477 | typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]); |
| 478 | LY_ARRAY_FOR(typedefs, u) { |
aPiecek | e1fbd95 | 2021-06-29 08:12:55 +0200 | [diff] [blame] | 479 | ret = lysp_check_dup_typedef(ctx, (struct lysp_node *)ctx->tpdfs_nodes.objs[i], &typedefs[u], ids_global); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 480 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 481 | } |
| 482 | } |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 483 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 484 | cleanup: |
| 485 | lyht_free(ids_global); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 486 | return ret; |
| 487 | } |
| 488 | |
aPiecek | 63e080d | 2021-06-29 13:53:28 +0200 | [diff] [blame] | 489 | /** |
| 490 | * @brief Check name of a new grouping to avoid name collisions. |
| 491 | * |
| 492 | * @param[in] ctx Parser context, module where the grouping is being defined is taken from here. |
| 493 | * @param[in] node Schema node where the grouping is being defined, NULL in case of a top-level grouping. |
| 494 | * @param[in] grp Grouping definition to check. |
| 495 | * @param[in,out] grps_global Initialized hash table to store temporary data between calls. When the module's |
| 496 | * groupings are checked, caller is supposed to free the table. |
| 497 | * @return LY_EVALID in case of collision, LY_SUCCESS otherwise. |
| 498 | */ |
| 499 | static LY_ERR |
| 500 | lysp_check_dup_grouping(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_node_grp *grp, |
| 501 | struct hash_table *grps_global) |
| 502 | { |
| 503 | struct lysp_node *parent; |
| 504 | uint32_t hash; |
| 505 | size_t name_len; |
| 506 | const char *name; |
| 507 | const struct lysp_node_grp *groupings, *grp_iter; |
| 508 | |
| 509 | assert(ctx); |
| 510 | assert(grp); |
| 511 | |
| 512 | name = grp->name; |
| 513 | name_len = strlen(name); |
| 514 | |
| 515 | /* check locally scoped groupings (avoid name shadowing) */ |
| 516 | if (node) { |
| 517 | groupings = lysp_node_groupings(node); |
| 518 | LY_LIST_FOR(groupings, grp_iter) { |
| 519 | if (grp_iter == grp) { |
| 520 | break; |
| 521 | } |
| 522 | if (!strcmp(name, grp_iter->name)) { |
| 523 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, |
| 524 | "Duplicate identifier \"%s\" of grouping statement - name collision with sibling grouping.", name); |
| 525 | return LY_EVALID; |
| 526 | } |
| 527 | } |
| 528 | /* search grouping in parent's nodes */ |
| 529 | for (parent = node->parent; parent; parent = parent->parent) { |
| 530 | if (lysp_grouping_match(name, parent)) { |
| 531 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, |
| 532 | "Duplicate identifier \"%s\" of grouping statement - name collision with another scoped grouping.", name); |
| 533 | return LY_EVALID; |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | /* check collision with the top-level groupings */ |
| 539 | if (node) { |
| 540 | hash = dict_hash(name, name_len); |
| 541 | if (!lyht_find(grps_global, &name, hash, NULL)) { |
| 542 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, |
| 543 | "Duplicate identifier \"%s\" of grouping statement - scoped grouping collide with a top-level grouping.", name); |
| 544 | return LY_EVALID; |
| 545 | } |
| 546 | } else { |
| 547 | LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, grps_global, name, "grouping", |
| 548 | "name collision with another top-level grouping")); |
| 549 | } |
| 550 | |
| 551 | return LY_SUCCESS; |
| 552 | } |
| 553 | |
| 554 | LY_ERR |
| 555 | lysp_check_dup_groupings(struct lys_parser_ctx *ctx, struct lysp_module *mod) |
| 556 | { |
| 557 | struct hash_table *ids_global; |
| 558 | const struct lysp_node_grp *groupings, *grp_iter; |
| 559 | LY_ARRAY_COUNT_TYPE u; |
| 560 | uint32_t i; |
| 561 | LY_ERR ret = LY_SUCCESS; |
| 562 | |
| 563 | ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1); |
| 564 | LY_LIST_FOR(mod->groupings, grp_iter) { |
| 565 | ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global); |
| 566 | LY_CHECK_GOTO(ret, cleanup); |
| 567 | } |
| 568 | LY_ARRAY_FOR(mod->includes, u) { |
| 569 | LY_LIST_FOR(mod->includes[u].submodule->groupings, grp_iter) { |
| 570 | ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global); |
| 571 | LY_CHECK_GOTO(ret, cleanup); |
| 572 | } |
| 573 | } |
| 574 | for (i = 0; i < ctx->grps_nodes.count; ++i) { |
| 575 | groupings = lysp_node_groupings((struct lysp_node *)ctx->grps_nodes.objs[i]); |
| 576 | LY_LIST_FOR(groupings, grp_iter) { |
| 577 | ret = lysp_check_dup_grouping(ctx, (struct lysp_node *)ctx->grps_nodes.objs[i], grp_iter, ids_global); |
| 578 | LY_CHECK_GOTO(ret, cleanup); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | cleanup: |
| 583 | lyht_free(ids_global); |
| 584 | return ret; |
| 585 | } |
| 586 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 587 | static ly_bool |
| 588 | ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data)) |
| 589 | { |
| 590 | void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p); |
| 591 | |
| 592 | return ptr1 == ptr2 ? 1 : 0; |
| 593 | } |
| 594 | |
| 595 | LY_ERR |
| 596 | lysp_check_dup_features(struct lys_parser_ctx *ctx, struct lysp_module *mod) |
| 597 | { |
| 598 | LY_ARRAY_COUNT_TYPE u; |
| 599 | struct hash_table *ht; |
| 600 | struct lysp_feature *f; |
aPiecek | dc12b9f | 2021-06-25 10:55:47 +0200 | [diff] [blame] | 601 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 602 | |
aPiecek | f620307 | 2021-06-25 10:58:26 +0200 | [diff] [blame] | 603 | ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 604 | LY_CHECK_RET(!ht, LY_EMEM); |
| 605 | |
| 606 | /* add all module features into a hash table */ |
| 607 | LY_ARRAY_FOR(mod->features, struct lysp_feature, f) { |
aPiecek | ea147b3 | 2021-06-29 07:52:47 +0200 | [diff] [blame] | 608 | ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature", |
| 609 | "name collision with another top-level feature"); |
aPiecek | dc12b9f | 2021-06-25 10:55:47 +0200 | [diff] [blame] | 610 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /* add all submodule features into a hash table */ |
| 614 | LY_ARRAY_FOR(mod->includes, u) { |
| 615 | LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) { |
aPiecek | ea147b3 | 2021-06-29 07:52:47 +0200 | [diff] [blame] | 616 | ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature", |
| 617 | "name collision with another top-level feature"); |
aPiecek | dc12b9f | 2021-06-25 10:55:47 +0200 | [diff] [blame] | 618 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
| 622 | cleanup: |
| 623 | lyht_free(ht); |
| 624 | return ret; |
| 625 | } |
| 626 | |
| 627 | LY_ERR |
| 628 | lysp_check_dup_identities(struct lys_parser_ctx *ctx, struct lysp_module *mod) |
| 629 | { |
| 630 | LY_ARRAY_COUNT_TYPE u; |
| 631 | struct hash_table *ht; |
| 632 | struct lysp_ident *i; |
aPiecek | dc12b9f | 2021-06-25 10:55:47 +0200 | [diff] [blame] | 633 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 634 | |
aPiecek | f620307 | 2021-06-25 10:58:26 +0200 | [diff] [blame] | 635 | ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 636 | LY_CHECK_RET(!ht, LY_EMEM); |
| 637 | |
| 638 | /* add all module identities into a hash table */ |
| 639 | LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) { |
aPiecek | ea147b3 | 2021-06-29 07:52:47 +0200 | [diff] [blame] | 640 | ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity", |
| 641 | "name collision with another top-level identity"); |
aPiecek | dc12b9f | 2021-06-25 10:55:47 +0200 | [diff] [blame] | 642 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | /* add all submodule identities into a hash table */ |
| 646 | LY_ARRAY_FOR(mod->includes, u) { |
| 647 | LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) { |
aPiecek | ea147b3 | 2021-06-29 07:52:47 +0200 | [diff] [blame] | 648 | ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity", |
| 649 | "name collision with another top-level identity"); |
aPiecek | dc12b9f | 2021-06-25 10:55:47 +0200 | [diff] [blame] | 650 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
| 654 | cleanup: |
| 655 | lyht_free(ht); |
| 656 | return ret; |
| 657 | } |
| 658 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 659 | struct lysp_load_module_check_data { |
| 660 | const char *name; |
| 661 | const char *revision; |
| 662 | const char *path; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 663 | const char *submoduleof; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 664 | }; |
| 665 | |
| 666 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 667 | lysp_load_module_check(const 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] | 668 | { |
| 669 | struct lysp_load_module_check_data *info = data; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 670 | const char *filename, *dot, *rev, *name; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 671 | uint8_t latest_revision; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 672 | size_t len; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 673 | struct lysp_revision *revs; |
| 674 | |
| 675 | name = mod ? mod->mod->name : submod->name; |
| 676 | revs = mod ? mod->revs : submod->revs; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 677 | latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 678 | |
| 679 | if (info->name) { |
| 680 | /* check name of the parsed model */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 681 | if (strcmp(info->name, name)) { |
| 682 | 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] | 683 | return LY_EINVAL; |
| 684 | } |
| 685 | } |
| 686 | if (info->revision) { |
| 687 | /* check revision of the parsed model */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 688 | if (!revs || strcmp(info->revision, revs[0].date)) { |
| 689 | LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 690 | revs ? revs[0].date : "none", info->revision); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 691 | return LY_EINVAL; |
| 692 | } |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 693 | } else if (!latest_revision) { |
| 694 | /* do not log, we just need to drop the schema and use the latest revision from the context */ |
| 695 | return LY_EEXIST; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 696 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 697 | if (submod) { |
| 698 | assert(info->submoduleof); |
| 699 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 700 | /* check that the submodule belongs-to our module */ |
Michal Vasko | c3781c3 | 2020-10-06 14:04:08 +0200 | [diff] [blame] | 701 | if (strcmp(info->submoduleof, submod->mod->name)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 702 | LOGVAL(ctx, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 703 | submod->name, info->submoduleof, submod->mod->name); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 704 | return LY_EVALID; |
| 705 | } |
| 706 | /* check circular dependency */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 707 | if (submod->parsing) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 708 | LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 709 | return LY_EVALID; |
| 710 | } |
| 711 | } |
| 712 | if (info->path) { |
| 713 | /* check that name and revision match filename */ |
| 714 | filename = strrchr(info->path, '/'); |
| 715 | if (!filename) { |
| 716 | filename = info->path; |
| 717 | } else { |
| 718 | filename++; |
| 719 | } |
| 720 | /* name */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 721 | len = strlen(name); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 722 | rev = strchr(filename, '@'); |
| 723 | dot = strrchr(info->path, '.'); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 724 | if (strncmp(filename, name, len) || |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 725 | ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 726 | 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] | 727 | } |
| 728 | /* revision */ |
| 729 | if (rev) { |
| 730 | len = dot - ++rev; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 731 | if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 732 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 733 | revs ? revs[0].date : "none"); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | } |
| 737 | return LY_SUCCESS; |
| 738 | } |
| 739 | |
Michal Vasko | e7a1daf | 2021-04-19 18:04:27 +0200 | [diff] [blame] | 740 | /** |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 741 | * @brief Parse a (sub)module from a local file and add into the context. |
Michal Vasko | e7a1daf | 2021-04-19 18:04:27 +0200 | [diff] [blame] | 742 | * |
| 743 | * This function does not check the presence of the (sub)module in context, it should be done before calling this function. |
| 744 | * |
Michal Vasko | e7a1daf | 2021-04-19 18:04:27 +0200 | [diff] [blame] | 745 | * @param[in] ctx libyang context where to work. |
| 746 | * @param[in] name Name of the (sub)module to load. |
| 747 | * @param[in] revision Optional revision of the (sub)module to load, if NULL the newest revision is being loaded. |
Michal Vasko | e7a1daf | 2021-04-19 18:04:27 +0200 | [diff] [blame] | 748 | * @param[in] main_ctx Parser context of the main module in case of loading submodule. |
| 749 | * @param[in] main_name Main module name in case of loading submodule. |
| 750 | * @param[in] required Module is required so error (even if the input file not found) are important. If 0, there is some |
| 751 | * backup and it is actually ok if the input data are not found. However, parser reports errors even in this case. |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 752 | * @param[in,out] new_mods Set of all the new mods added to the context. Includes this module and all of its imports. |
Michal Vasko | e7a1daf | 2021-04-19 18:04:27 +0200 | [diff] [blame] | 753 | * @param[out] result Parsed YANG schema tree of the requested module (struct lys_module*) or submodule (struct lysp_submodule*). |
| 754 | * If it is a module, it is already in the context! |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 755 | * @return LY_SUCCESS on success. |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 756 | * @return LY_ERR on error. |
Michal Vasko | e7a1daf | 2021-04-19 18:04:27 +0200 | [diff] [blame] | 757 | */ |
| 758 | static LY_ERR |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 759 | lys_parse_localfile(struct ly_ctx *ctx, const char *name, const char *revision, struct lys_parser_ctx *main_ctx, |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 760 | const char *main_name, ly_bool required, struct ly_set *new_mods, void **result) |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 761 | { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 762 | struct ly_in *in; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 763 | char *filepath = NULL; |
| 764 | LYS_INFORMAT format; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 765 | void *mod = NULL; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 766 | LY_ERR ret = LY_SUCCESS; |
| 767 | struct lysp_load_module_check_data check_data = {0}; |
| 768 | |
Michal Vasko | 87f1cf0 | 2021-06-08 14:02:47 +0200 | [diff] [blame] | 769 | LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, |
| 770 | revision, &filepath, &format)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 771 | if (!filepath) { |
| 772 | if (required) { |
| 773 | LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 774 | revision ? revision : ""); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 775 | } |
| 776 | return LY_ENOTFOUND; |
| 777 | } |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 778 | |
| 779 | LOGVRB("Loading schema from \"%s\" file.", filepath); |
| 780 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 781 | /* get the (sub)module */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 782 | LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 783 | LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 784 | check_data.name = name; |
| 785 | check_data.revision = revision; |
| 786 | check_data.path = filepath; |
fredgan | cd485b8 | 2019-10-18 15:00:17 +0800 | [diff] [blame] | 787 | check_data.submoduleof = main_name; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 788 | if (main_ctx) { |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 789 | ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data, new_mods, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 790 | (struct lysp_submodule **)&mod); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 791 | } else { |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 792 | ret = lys_parse_in(ctx, in, format, lysp_load_module_check, &check_data, new_mods, (struct lys_module **)&mod); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 793 | |
| 794 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 795 | ly_in_free(in, 1); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 796 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 797 | |
| 798 | *result = mod; |
| 799 | |
| 800 | /* success */ |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 801 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 802 | cleanup: |
| 803 | free(filepath); |
| 804 | return ret; |
| 805 | } |
| 806 | |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 807 | /** |
| 808 | * @brief Load module from searchdirs or from callback. |
| 809 | * |
| 810 | * @param[in] ctx libyang context where to work. |
| 811 | * @param[in] name Name of module to load. |
| 812 | * @param[in] revision Revision of module to load. |
| 813 | * @param[in] mod_latest Module with the latest revision found in |
| 814 | * context and the searchdirs should be searched, otherwise set to NULL. |
| 815 | * @param[in,out] new_mods Set of all the new mods added to the context. |
| 816 | * Includes this module and all of its imports. |
| 817 | * @param[out] mod Loaded module. |
| 818 | * @return LY_SUCCESS on success. |
| 819 | * @return LY_ERR on error. |
| 820 | */ |
| 821 | static LY_ERR |
| 822 | lys_parse_load_from_clb_or_file(struct ly_ctx *ctx, const char *name, const char *revision, |
| 823 | struct lys_module *mod_latest, struct ly_set *new_mods, struct lys_module **mod) |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 824 | { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 825 | const char *module_data = NULL; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 826 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 827 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 828 | void (*module_data_free)(void *module_data, void *user_data) = NULL; |
| 829 | struct lysp_load_module_check_data check_data = {0}; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 830 | struct ly_in *in; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 831 | |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 832 | /* Module not present in the context, get the input data and parse it. */ |
| 833 | if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 834 | search_clb: |
| 835 | if (ctx->imp_clb && !ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, &format, &module_data, |
| 836 | &module_data_free)) { |
| 837 | LY_CHECK_RET(ly_in_new_memory(module_data, &in)); |
| 838 | check_data.name = name; |
| 839 | check_data.revision = revision; |
| 840 | lys_parse_in(ctx, in, format, lysp_load_module_check, &check_data, new_mods, mod); |
| 841 | ly_in_free(in, 0); |
| 842 | if (module_data_free) { |
| 843 | module_data_free((void *)module_data, ctx->imp_clb_data); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 844 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 845 | } |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 846 | if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 847 | goto search_file; |
| 848 | } |
| 849 | } else { |
| 850 | search_file: |
| 851 | if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) { |
| 852 | /* Module was not received from the callback or there is no callback set. */ |
| 853 | lys_parse_localfile(ctx, name, revision, NULL, NULL, mod_latest ? 0 : 1, new_mods, (void **)mod); |
| 854 | } |
| 855 | if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 856 | goto search_clb; |
| 857 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 858 | } |
| 859 | |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 860 | return LY_SUCCESS; |
| 861 | } |
| 862 | |
| 863 | /** |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 864 | * @brief Get module without revision according to priorities. |
| 865 | * |
| 866 | * 1. Search for the module with LYS_MOD_IMPORTED_REV. |
| 867 | * 2. Search for the implemented module. |
| 868 | * 3. Search for the latest module in the context. |
| 869 | * |
| 870 | * @param[in] ctx libyang context where module is searched. |
| 871 | * @param[in] name Name of the searched module. |
| 872 | * @param[out] keep_search Flag set to 1 if searchpaths or module |
| 873 | * callback must be searched to obtain latest available revision. |
| 874 | * @return Found module from context or NULL. |
| 875 | */ |
| 876 | static struct lys_module * |
| 877 | lys_get_module_without_revision(struct ly_ctx *ctx, const char *name, ly_bool *keep_search) |
| 878 | { |
| 879 | struct lys_module *mod, *mod_impl; |
| 880 | uint32_t index; |
| 881 | |
| 882 | *keep_search = 0; |
| 883 | |
| 884 | /* Try to find module with LYS_MOD_IMPORTED_REV flag. */ |
| 885 | index = 0; |
| 886 | while ((mod = ly_ctx_get_module_iter(ctx, &index))) { |
| 887 | if (!strcmp(mod->name, name) && (mod->latest_revision & LYS_MOD_IMPORTED_REV)) { |
| 888 | break; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | /* Try to find the implemented module. */ |
| 893 | mod_impl = ly_ctx_get_module_implemented(ctx, name); |
| 894 | if (mod && mod_impl) { |
| 895 | LOGVRB("Implemented module \"%s@%s\" is not used for import, " |
| 896 | "revision \"%s\" is imported instead.", |
| 897 | mod_impl->name, mod_impl->revision, mod->revision); |
| 898 | return mod; |
| 899 | } else if (mod_impl) { |
| 900 | return mod_impl; |
| 901 | } |
| 902 | |
| 903 | /* Try to find the latest module in the current context. */ |
| 904 | mod = ly_ctx_get_module_latest(ctx, name); |
| 905 | *keep_search = 1; |
| 906 | |
| 907 | return mod; |
| 908 | } |
| 909 | |
| 910 | /** |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 911 | * @brief Check if a circular dependency exists between modules. |
| 912 | * |
| 913 | * @param[in] ctx libyang context for log an error. |
| 914 | * @param[in,out] mod Examined module which is set to NULL |
| 915 | * if the circular dependency is detected. |
| 916 | * @return LY_SUCCESS if no circular dependecy is detected, |
| 917 | * otherwise LY_EVALID. |
| 918 | */ |
| 919 | static LY_ERR |
| 920 | lys_check_circular_dependency(struct ly_ctx *ctx, struct lys_module **mod) |
| 921 | { |
| 922 | if ((*mod) && (*mod)->parsed->parsing) { |
| 923 | LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", (*mod)->name); |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 924 | *mod = NULL; |
| 925 | return LY_EVALID; |
Michal Vasko | 0550b76 | 2020-11-24 18:04:08 +0100 | [diff] [blame] | 926 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 927 | |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 928 | return LY_SUCCESS; |
| 929 | } |
| 930 | |
| 931 | LY_ERR |
| 932 | lys_parse_load(struct ly_ctx *ctx, const char *name, const char *revision, struct ly_set *new_mods, |
| 933 | struct lys_module **mod) |
| 934 | { |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 935 | ly_bool keep_search; |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 936 | struct lys_module *mod_latest = NULL; |
| 937 | |
| 938 | assert(mod && new_mods); |
| 939 | |
Michal Vasko | 0550b76 | 2020-11-24 18:04:08 +0100 | [diff] [blame] | 940 | /* |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 941 | * Try to get the module from the context. |
Michal Vasko | 0550b76 | 2020-11-24 18:04:08 +0100 | [diff] [blame] | 942 | */ |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 943 | if (revision) { |
| 944 | /* Get the specific revision. */ |
| 945 | *mod = ly_ctx_get_module(ctx, name, revision); |
| 946 | } else { |
| 947 | /* Get the requested module of the latest revision in the context. */ |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 948 | *mod = lys_get_module_without_revision(ctx, name, &keep_search); |
| 949 | if (keep_search) { |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 950 | /* Let us now search with callback and searchpaths to check |
| 951 | * if there is newer revision outside the context. |
| 952 | */ |
| 953 | mod_latest = *mod; |
| 954 | *mod = NULL; |
| 955 | } |
| 956 | } |
| 957 | |
Michal Vasko | 0550b76 | 2020-11-24 18:04:08 +0100 | [diff] [blame] | 958 | if (!*mod) { |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 959 | /* No suitable module in the context, try to load it. */ |
| 960 | LY_CHECK_RET(lys_parse_load_from_clb_or_file(ctx, name, revision, |
| 961 | mod_latest, new_mods, mod)); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 962 | |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 963 | if (!*mod && !mod_latest) { |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 964 | LOGVAL(ctx, LYVE_REFERENCE, "Loading \"%s\" module failed.", name); |
Michal Vasko | 0550b76 | 2020-11-24 18:04:08 +0100 | [diff] [blame] | 965 | return LY_EVALID; |
| 966 | } |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 967 | |
| 968 | /* Update the latest_revision flag - here we have selected the latest available schema, |
| 969 | * consider that even the callback provides correct latest revision. |
| 970 | */ |
| 971 | if (!*mod) { |
| 972 | LOGVRB("Newer revision than \"%s@%s\" not found, using this as the latest revision.", |
| 973 | mod_latest->name, mod_latest->revision); |
| 974 | assert(mod_latest->latest_revision & LYS_MOD_LATEST_REV); |
| 975 | mod_latest->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS; |
| 976 | *mod = mod_latest; |
| 977 | } else if (*mod && !revision && ((*mod)->latest_revision & LYS_MOD_LATEST_REV)) { |
| 978 | (*mod)->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS; |
| 979 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 980 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 981 | |
aPiecek | 9f8c7e7 | 2021-07-28 12:01:56 +0200 | [diff] [blame] | 982 | /* Checking the circular dependence of imported modules. */ |
| 983 | LY_CHECK_RET(lys_check_circular_dependency(ctx, mod)); |
| 984 | |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 985 | return LY_SUCCESS; |
| 986 | } |
| 987 | |
| 988 | LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 989 | lysp_check_stringchar(struct lys_parser_ctx *ctx, uint32_t c) |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 990 | { |
| 991 | if (!is_yangutf8char(c)) { |
| 992 | LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c); |
| 993 | return LY_EVALID; |
| 994 | } |
| 995 | return LY_SUCCESS; |
| 996 | } |
| 997 | |
| 998 | LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 999 | lysp_check_identifierchar(struct lys_parser_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix) |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 1000 | { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1001 | if (first || (prefix && ((*prefix) == 1))) { |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 1002 | if (!is_yangidentstartchar(c)) { |
aPiecek | c89b224 | 2021-05-14 14:19:11 +0200 | [diff] [blame] | 1003 | if ((c < UCHAR_MAX) && isprint(c)) { |
Michal Vasko | 7c76904 | 2021-03-25 12:20:49 +0100 | [diff] [blame] | 1004 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c); |
| 1005 | } else { |
| 1006 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character 0x%04x.", c); |
| 1007 | } |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 1008 | return LY_EVALID; |
| 1009 | } |
| 1010 | if (prefix) { |
| 1011 | if (first) { |
| 1012 | (*prefix) = 0; |
| 1013 | } else { |
| 1014 | (*prefix) = 2; |
| 1015 | } |
| 1016 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1017 | } else if ((c == ':') && prefix && ((*prefix) == 0)) { |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 1018 | (*prefix) = 1; |
| 1019 | } else if (!is_yangidentchar(c)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1020 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c); |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 1021 | return LY_EVALID; |
| 1022 | } |
| 1023 | |
| 1024 | return LY_SUCCESS; |
| 1025 | } |
| 1026 | |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1027 | /** |
| 1028 | * @brief Try to find the parsed submodule in main module for the given include record. |
| 1029 | * |
| 1030 | * @param[in] pctx main parser context |
| 1031 | * @param[in] inc The include record with missing parsed submodule. According to include info try to find |
| 1032 | * the corresponding parsed submodule in main module's includes. |
| 1033 | * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record |
| 1034 | * @return LY_ENOT - the parsed module was not found. |
| 1035 | * @return LY_EVALID - YANG rule violation |
| 1036 | */ |
| 1037 | static LY_ERR |
Michal Vasko | a3afbfe | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1038 | lysp_main_pmod_get_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc) |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1039 | { |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1040 | LY_ARRAY_COUNT_TYPE i; |
Michal Vasko | a3afbfe | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1041 | struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1042 | |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1043 | LY_ARRAY_FOR(main_pmod->includes, i) { |
| 1044 | if (strcmp(main_pmod->includes[i].name, inc->name)) { |
| 1045 | continue; |
| 1046 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1047 | |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1048 | if (inc->rev[0] && strncmp(inc->rev, main_pmod->includes[i].rev, LY_REV_SIZE)) { |
| 1049 | LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE, |
| 1050 | "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.", |
Michal Vasko | a3afbfe | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1051 | ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev, |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1052 | main_pmod->includes[i].name, main_pmod->includes[i].rev, main_pmod->mod->name); |
| 1053 | return LY_EVALID; |
| 1054 | } |
| 1055 | |
| 1056 | inc->submodule = main_pmod->includes[i].submodule; |
| 1057 | return inc->submodule ? LY_SUCCESS : LY_ENOT; |
| 1058 | } |
| 1059 | |
| 1060 | if (main_pmod->version == LYS_VERSION_1_1) { |
| 1061 | LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE, |
| 1062 | "YANG 1.1 requires all submodules to be included from main module. " |
| 1063 | "But submodule \"%s\" includes submodule \"%s\" which is not included by main module \"%s\".", |
Michal Vasko | a3afbfe | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1064 | ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->name, main_pmod->mod->name); |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1065 | return LY_EVALID; |
| 1066 | } else { |
| 1067 | return LY_ENOT; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | /** |
Michal Vasko | a3afbfe | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1072 | * @brief Try to find the parsed submodule in currenlty parsed modules for the given include record. |
| 1073 | * |
| 1074 | * @param[in] pctx main parser context |
| 1075 | * @param[in] inc The include record with missing parsed submodule. |
| 1076 | * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record |
| 1077 | * @return LY_ENOT - the parsed module was not found. |
| 1078 | * @return LY_EVALID - YANG rule violation |
| 1079 | */ |
| 1080 | static LY_ERR |
| 1081 | lysp_parsed_mods_get_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc) |
| 1082 | { |
| 1083 | uint32_t i; |
| 1084 | struct lysp_submodule *submod; |
| 1085 | |
| 1086 | for (i = 0; i < pctx->parsed_mods->count - 1; ++i) { |
| 1087 | submod = pctx->parsed_mods->objs[i]; |
| 1088 | if (!submod->is_submod) { |
| 1089 | continue; |
| 1090 | } |
| 1091 | |
| 1092 | if (strcmp(submod->name, inc->name)) { |
| 1093 | continue; |
| 1094 | } |
| 1095 | |
| 1096 | if (inc->rev[0] && submod->revs && strncmp(inc->rev, submod->revs[0].date, LY_REV_SIZE)) { |
| 1097 | LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE, |
| 1098 | "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.", |
| 1099 | ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev, |
| 1100 | submod->name, submod->revs[0].date, PARSER_CUR_PMOD(pctx)->mod->name); |
| 1101 | return LY_EVALID; |
| 1102 | } |
| 1103 | |
| 1104 | inc->submodule = submod; |
| 1105 | return LY_SUCCESS; |
| 1106 | } |
| 1107 | |
| 1108 | return LY_ENOT; |
| 1109 | } |
| 1110 | |
| 1111 | /** |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1112 | * @brief Make the copy of the given include record into the main module. |
| 1113 | * |
| 1114 | * YANG 1.0 does not require the main module to include all the submodules. Therefore, parsing submodules can cause |
| 1115 | * reallocating and extending the includes array in the main module by the submodules included only in submodules. |
| 1116 | * |
| 1117 | * @param[in] pctx main parser context |
| 1118 | * @param[in] inc Include record to copy into main module taken from @p pctx. |
| 1119 | * @return LY_ERR value. |
| 1120 | */ |
| 1121 | static LY_ERR |
| 1122 | lysp_inject_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc) |
| 1123 | { |
| 1124 | LY_ARRAY_COUNT_TYPE i; |
| 1125 | struct lysp_include *inc_new, *inc_tofill = NULL; |
Michal Vasko | a3afbfe | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1126 | struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed; |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1127 | |
| 1128 | /* first, try to find the corresponding record with missing parsed submodule */ |
| 1129 | LY_ARRAY_FOR(main_pmod->includes, i) { |
| 1130 | if (strcmp(main_pmod->includes[i].name, inc->name)) { |
| 1131 | continue; |
| 1132 | } |
| 1133 | inc_tofill = &main_pmod->includes[i]; |
| 1134 | break; |
| 1135 | } |
| 1136 | |
| 1137 | if (inc_tofill) { |
| 1138 | inc_tofill->submodule = inc->submodule; |
| 1139 | } else { |
| 1140 | LY_ARRAY_NEW_RET(PARSER_CTX(pctx), main_pmod->includes, inc_new, LY_EMEM); |
| 1141 | |
| 1142 | inc_new->submodule = inc->submodule; |
| 1143 | DUP_STRING_RET(PARSER_CTX(pctx), inc->name, inc_new->name); |
| 1144 | DUP_STRING_RET(PARSER_CTX(pctx), inc->dsc, inc_new->dsc); |
| 1145 | DUP_STRING_RET(PARSER_CTX(pctx), inc->ref, inc_new->ref); |
| 1146 | /* TODO duplicate extensions */ |
| 1147 | memcpy(inc_new->rev, inc->rev, LY_REV_SIZE); |
| 1148 | inc_new->injected = 1; |
| 1149 | } |
| 1150 | return LY_SUCCESS; |
| 1151 | } |
| 1152 | |
| 1153 | LY_ERR |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 1154 | lysp_load_submodules(struct lys_parser_ctx *pctx, struct lysp_module *pmod, struct ly_set *new_mods) |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1155 | { |
| 1156 | LY_ARRAY_COUNT_TYPE u; |
| 1157 | struct ly_ctx *ctx = PARSER_CTX(pctx); |
| 1158 | |
| 1159 | LY_ARRAY_FOR(pmod->includes, u) { |
Michal Vasko | a3afbfe | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1160 | LY_ERR ret = LY_SUCCESS, r; |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1161 | struct lysp_submodule *submod = NULL; |
| 1162 | struct lysp_include *inc = &pmod->includes[u]; |
| 1163 | |
| 1164 | if (inc->submodule) { |
| 1165 | continue; |
| 1166 | } |
| 1167 | |
| 1168 | if (pmod->is_submod) { |
| 1169 | /* try to find the submodule in the main module or its submodules */ |
Michal Vasko | a3afbfe | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1170 | ret = lysp_main_pmod_get_submodule(pctx, inc); |
| 1171 | LY_CHECK_RET(ret != LY_ENOT, ret); |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1172 | } |
| 1173 | |
Michal Vasko | a3afbfe | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1174 | /* try to use currently parsed submodule */ |
| 1175 | r = lysp_parsed_mods_get_submodule(pctx, inc); |
| 1176 | LY_CHECK_RET(r != LY_ENOT, r); |
| 1177 | |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1178 | /* submodule not present in the main module, get the input data and parse it */ |
| 1179 | if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 1180 | search_clb: |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1181 | if (ctx->imp_clb) { |
| 1182 | const char *submodule_data = NULL; |
| 1183 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
| 1184 | void (*submodule_data_free)(void *module_data, void *user_data) = NULL; |
| 1185 | struct lysp_load_module_check_data check_data = {0}; |
| 1186 | struct ly_in *in; |
| 1187 | |
Michal Vasko | a3afbfe | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1188 | if (ctx->imp_clb(PARSER_CUR_PMOD(pctx)->mod->name, NULL, inc->name, |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1189 | inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data, |
| 1190 | &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) { |
| 1191 | LY_CHECK_RET(ly_in_new_memory(submodule_data, &in)); |
| 1192 | check_data.name = inc->name; |
| 1193 | check_data.revision = inc->rev[0] ? inc->rev : NULL; |
Michal Vasko | a3afbfe | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1194 | check_data.submoduleof = PARSER_CUR_PMOD(pctx)->mod->name; |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 1195 | lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, new_mods, &submod); |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1196 | |
| 1197 | /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting |
| 1198 | * submodule's include into main module, where it is missing */ |
| 1199 | inc = &pmod->includes[u]; |
| 1200 | |
| 1201 | ly_in_free(in, 0); |
| 1202 | if (submodule_data_free) { |
| 1203 | submodule_data_free((void *)submodule_data, ctx->imp_clb_data); |
| 1204 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1205 | } |
| 1206 | } |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1207 | if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 1208 | goto search_file; |
| 1209 | } |
| 1210 | } else { |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 1211 | search_file: |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1212 | if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) { |
| 1213 | /* submodule was not received from the callback or there is no callback set */ |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 1214 | lys_parse_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, pctx, |
Michal Vasko | a3afbfe | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1215 | PARSER_CUR_PMOD(pctx)->mod->name, 1, new_mods, (void **)&submod); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1216 | |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1217 | /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting |
| 1218 | * submodule's include into main module, where it is missing */ |
| 1219 | inc = &pmod->includes[u]; |
| 1220 | } |
| 1221 | if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 1222 | goto search_clb; |
| 1223 | } |
| 1224 | } |
| 1225 | if (submod) { |
| 1226 | if (!inc->rev[0] && (submod->latest_revision == 1)) { |
| 1227 | /* update the latest_revision flag - here we have selected the latest available schema, |
| 1228 | * consider that even the callback provides correct latest revision */ |
| 1229 | submod->latest_revision = 2; |
| 1230 | } |
| 1231 | |
| 1232 | inc->submodule = submod; |
| 1233 | if (ret == LY_ENOT) { |
| 1234 | /* the submodule include is not present in YANG 1.0 main module - add it there */ |
| 1235 | LY_CHECK_RET(lysp_inject_submodule(pctx, &pmod->includes[u])); |
| 1236 | } |
| 1237 | } |
| 1238 | if (!inc->submodule) { |
| 1239 | LOGVAL(ctx, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name, |
Michal Vasko | a3afbfe | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1240 | PARSER_CUR_PMOD(pctx)->is_submod ? ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name : |
| 1241 | PARSER_CUR_PMOD(pctx)->mod->name); |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1242 | return LY_EVALID; |
| 1243 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | return LY_SUCCESS; |
| 1247 | } |
| 1248 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame^] | 1249 | LIBYANG_API_DEF const struct lysc_when * |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 1250 | lysc_has_when(const struct lysc_node *node) |
| 1251 | { |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1252 | struct lysc_when **when; |
| 1253 | |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 1254 | if (!node) { |
| 1255 | return NULL; |
| 1256 | } |
| 1257 | |
| 1258 | do { |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1259 | when = lysc_node_when(node); |
| 1260 | if (when) { |
| 1261 | return when[0]; |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 1262 | } |
| 1263 | node = node->parent; |
| 1264 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 1265 | |
| 1266 | return NULL; |
| 1267 | } |
| 1268 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame^] | 1269 | LIBYANG_API_DEF const struct lys_module * |
Michal Vasko | ef53c81 | 2021-10-13 10:21:03 +0200 | [diff] [blame] | 1270 | lysc_owner_module(const struct lysc_node *node) |
| 1271 | { |
| 1272 | if (!node) { |
| 1273 | return NULL; |
| 1274 | } |
| 1275 | |
| 1276 | for ( ; node->parent; node = node->parent) {} |
| 1277 | return node->module; |
| 1278 | } |
| 1279 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame^] | 1280 | LIBYANG_API_DEF const char * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1281 | lys_nodetype2str(uint16_t nodetype) |
| 1282 | { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1283 | switch (nodetype) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1284 | case LYS_CONTAINER: |
| 1285 | return "container"; |
| 1286 | case LYS_CHOICE: |
| 1287 | return "choice"; |
| 1288 | case LYS_LEAF: |
| 1289 | return "leaf"; |
| 1290 | case LYS_LEAFLIST: |
| 1291 | return "leaf-list"; |
| 1292 | case LYS_LIST: |
| 1293 | return "list"; |
| 1294 | case LYS_ANYXML: |
| 1295 | return "anyxml"; |
| 1296 | case LYS_ANYDATA: |
| 1297 | return "anydata"; |
Radek Krejci | f12a1f0 | 2019-02-11 16:42:08 +0100 | [diff] [blame] | 1298 | case LYS_CASE: |
| 1299 | return "case"; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1300 | case LYS_RPC: |
| 1301 | return "RPC"; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1302 | case LYS_ACTION: |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1303 | return "action"; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1304 | case LYS_NOTIF: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 1305 | return "notification"; |
Radek Krejci | fc81ea8 | 2019-04-18 13:27:22 +0200 | [diff] [blame] | 1306 | case LYS_USES: |
| 1307 | return "uses"; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1308 | default: |
| 1309 | return "unknown"; |
| 1310 | } |
| 1311 | } |
| 1312 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame^] | 1313 | LIBYANG_API_DEF enum ly_stmt |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 1314 | lys_nodetype2stmt(uint16_t nodetype) |
| 1315 | { |
| 1316 | switch (nodetype) { |
| 1317 | case LYS_CONTAINER: |
| 1318 | return LY_STMT_CONTAINER; |
| 1319 | case LYS_CHOICE: |
| 1320 | return LY_STMT_CHOICE; |
| 1321 | case LYS_LEAF: |
| 1322 | return LY_STMT_LEAF; |
| 1323 | case LYS_LEAFLIST: |
| 1324 | return LY_STMT_LEAF_LIST; |
| 1325 | case LYS_LIST: |
| 1326 | return LY_STMT_LIST; |
| 1327 | case LYS_ANYXML: |
| 1328 | return LY_STMT_ANYXML; |
| 1329 | case LYS_ANYDATA: |
| 1330 | return LY_STMT_ANYDATA; |
| 1331 | case LYS_CASE: |
| 1332 | return LY_STMT_CASE; |
| 1333 | case LYS_RPC: |
| 1334 | return LY_STMT_RPC; |
| 1335 | case LYS_ACTION: |
| 1336 | return LY_STMT_ACTION; |
| 1337 | case LYS_NOTIF: |
| 1338 | return LY_STMT_NOTIFICATION; |
| 1339 | case LYS_USES: |
| 1340 | return LY_STMT_USES; |
| 1341 | case LYS_INPUT: |
| 1342 | return LY_STMT_INPUT; |
| 1343 | case LYS_OUTPUT: |
| 1344 | return LY_STMT_OUTPUT; |
| 1345 | default: |
| 1346 | return LY_STMT_NONE; |
| 1347 | } |
| 1348 | } |
| 1349 | |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1350 | const char * |
| 1351 | lys_datatype2str(LY_DATA_TYPE basetype) |
| 1352 | { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1353 | switch (basetype) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1354 | case LY_TYPE_BINARY: |
| 1355 | return "binary"; |
| 1356 | case LY_TYPE_UINT8: |
| 1357 | return "uint8"; |
| 1358 | case LY_TYPE_UINT16: |
| 1359 | return "uint16"; |
| 1360 | case LY_TYPE_UINT32: |
| 1361 | return "uint32"; |
| 1362 | case LY_TYPE_UINT64: |
| 1363 | return "uint64"; |
| 1364 | case LY_TYPE_STRING: |
| 1365 | return "string"; |
| 1366 | case LY_TYPE_BITS: |
| 1367 | return "bits"; |
| 1368 | case LY_TYPE_BOOL: |
| 1369 | return "boolean"; |
| 1370 | case LY_TYPE_DEC64: |
| 1371 | return "decimal64"; |
| 1372 | case LY_TYPE_EMPTY: |
| 1373 | return "empty"; |
| 1374 | case LY_TYPE_ENUM: |
| 1375 | return "enumeration"; |
| 1376 | case LY_TYPE_IDENT: |
| 1377 | return "identityref"; |
| 1378 | case LY_TYPE_INST: |
| 1379 | return "instance-identifier"; |
| 1380 | case LY_TYPE_LEAFREF: |
| 1381 | return "leafref"; |
| 1382 | case LY_TYPE_UNION: |
| 1383 | return "union"; |
| 1384 | case LY_TYPE_INT8: |
| 1385 | return "int8"; |
| 1386 | case LY_TYPE_INT16: |
| 1387 | return "int16"; |
| 1388 | case LY_TYPE_INT32: |
| 1389 | return "int32"; |
| 1390 | case LY_TYPE_INT64: |
| 1391 | return "int64"; |
| 1392 | default: |
| 1393 | return "unknown"; |
| 1394 | } |
| 1395 | } |
| 1396 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame^] | 1397 | LIBYANG_API_DEF const struct lysp_tpdf * |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1398 | lysp_node_typedefs(const struct lysp_node *node) |
| 1399 | { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1400 | switch (node->nodetype) { |
| 1401 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1402 | return ((struct lysp_node_container *)node)->typedefs; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1403 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1404 | return ((struct lysp_node_list *)node)->typedefs; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1405 | case LYS_GROUPING: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1406 | return ((struct lysp_node_grp *)node)->typedefs; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1407 | case LYS_RPC: |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1408 | case LYS_ACTION: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1409 | return ((struct lysp_node_action *)node)->typedefs; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1410 | case LYS_INPUT: |
| 1411 | case LYS_OUTPUT: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1412 | return ((struct lysp_node_action_inout *)node)->typedefs; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1413 | case LYS_NOTIF: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1414 | return ((struct lysp_node_notif *)node)->typedefs; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1415 | default: |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1416 | return NULL; |
| 1417 | } |
| 1418 | } |
| 1419 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame^] | 1420 | LIBYANG_API_DEF const struct lysp_node_grp * |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1421 | lysp_node_groupings(const struct lysp_node *node) |
| 1422 | { |
| 1423 | switch (node->nodetype) { |
| 1424 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1425 | return ((struct lysp_node_container *)node)->groupings; |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1426 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1427 | return ((struct lysp_node_list *)node)->groupings; |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1428 | case LYS_GROUPING: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1429 | return ((struct lysp_node_grp *)node)->groupings; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1430 | case LYS_RPC: |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1431 | case LYS_ACTION: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1432 | return ((struct lysp_node_action *)node)->groupings; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1433 | case LYS_INPUT: |
| 1434 | case LYS_OUTPUT: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1435 | return ((struct lysp_node_action_inout *)node)->groupings; |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1436 | case LYS_NOTIF: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1437 | return ((struct lysp_node_notif *)node)->groupings; |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1438 | default: |
| 1439 | return NULL; |
| 1440 | } |
| 1441 | } |
| 1442 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1443 | struct lysp_node_action ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1444 | lysp_node_actions_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1445 | { |
| 1446 | assert(node); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1447 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1448 | switch (node->nodetype) { |
| 1449 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1450 | return &((struct lysp_node_container *)node)->actions; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1451 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1452 | return &((struct lysp_node_list *)node)->actions; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1453 | case LYS_GROUPING: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1454 | return &((struct lysp_node_grp *)node)->actions; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1455 | case LYS_AUGMENT: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1456 | return &((struct lysp_node_augment *)node)->actions; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1457 | default: |
| 1458 | return NULL; |
| 1459 | } |
| 1460 | } |
| 1461 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame^] | 1462 | LIBYANG_API_DEF const struct lysp_node_action * |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1463 | lysp_node_actions(const struct lysp_node *node) |
| 1464 | { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1465 | struct lysp_node_action **actions; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1466 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1467 | actions = lysp_node_actions_p((struct lysp_node *)node); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1468 | if (actions) { |
| 1469 | return *actions; |
| 1470 | } else { |
| 1471 | return NULL; |
| 1472 | } |
| 1473 | } |
| 1474 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1475 | struct lysp_node_notif ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1476 | lysp_node_notifs_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1477 | { |
| 1478 | assert(node); |
| 1479 | switch (node->nodetype) { |
| 1480 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1481 | return &((struct lysp_node_container *)node)->notifs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1482 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1483 | return &((struct lysp_node_list *)node)->notifs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1484 | case LYS_GROUPING: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1485 | return &((struct lysp_node_grp *)node)->notifs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1486 | case LYS_AUGMENT: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1487 | return &((struct lysp_node_augment *)node)->notifs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1488 | default: |
| 1489 | return NULL; |
| 1490 | } |
| 1491 | } |
| 1492 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame^] | 1493 | LIBYANG_API_DEF const struct lysp_node_notif * |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1494 | lysp_node_notifs(const struct lysp_node *node) |
| 1495 | { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1496 | struct lysp_node_notif **notifs; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1497 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1498 | notifs = lysp_node_notifs_p((struct lysp_node *)node); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1499 | if (notifs) { |
| 1500 | return *notifs; |
| 1501 | } else { |
| 1502 | return NULL; |
| 1503 | } |
| 1504 | } |
| 1505 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1506 | struct lysp_node ** |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1507 | lysp_node_child_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1508 | { |
| 1509 | assert(node); |
| 1510 | switch (node->nodetype) { |
| 1511 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1512 | return &((struct lysp_node_container *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1513 | case LYS_CHOICE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1514 | return &((struct lysp_node_choice *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1515 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1516 | return &((struct lysp_node_list *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1517 | case LYS_CASE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1518 | return &((struct lysp_node_case *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1519 | case LYS_GROUPING: |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1520 | return &((struct lysp_node_grp *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1521 | case LYS_AUGMENT: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1522 | return &((struct lysp_node_augment *)node)->child; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1523 | case LYS_INPUT: |
| 1524 | case LYS_OUTPUT: |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1525 | return &((struct lysp_node_action_inout *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1526 | case LYS_NOTIF: |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1527 | return &((struct lysp_node_notif *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1528 | default: |
| 1529 | return NULL; |
| 1530 | } |
| 1531 | } |
| 1532 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame^] | 1533 | LIBYANG_API_DEF const struct lysp_node * |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1534 | lysp_node_child(const struct lysp_node *node) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1535 | { |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1536 | struct lysp_node **child; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1537 | |
| 1538 | if (!node) { |
| 1539 | return NULL; |
| 1540 | } |
| 1541 | |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1542 | child = lysp_node_child_p((struct lysp_node *)node); |
| 1543 | if (child) { |
| 1544 | return *child; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1545 | } else { |
| 1546 | return NULL; |
| 1547 | } |
| 1548 | } |
| 1549 | |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1550 | struct lysp_restr ** |
| 1551 | lysp_node_musts_p(const struct lysp_node *node) |
| 1552 | { |
| 1553 | if (!node) { |
| 1554 | return NULL; |
| 1555 | } |
| 1556 | |
Radek Krejci | 2d5f6df | 2021-01-28 14:00:13 +0100 | [diff] [blame] | 1557 | switch (node->nodetype) { |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1558 | case LYS_CONTAINER: |
| 1559 | return &((struct lysp_node_container *)node)->musts; |
| 1560 | case LYS_LEAF: |
| 1561 | return &((struct lysp_node_leaf *)node)->musts; |
| 1562 | case LYS_LEAFLIST: |
| 1563 | return &((struct lysp_node_leaflist *)node)->musts; |
| 1564 | case LYS_LIST: |
| 1565 | return &((struct lysp_node_list *)node)->musts; |
| 1566 | case LYS_ANYXML: |
| 1567 | case LYS_ANYDATA: |
| 1568 | return &((struct lysp_node_anydata *)node)->musts; |
| 1569 | case LYS_NOTIF: |
| 1570 | return &((struct lysp_node_notif *)node)->musts; |
| 1571 | case LYS_INPUT: |
| 1572 | case LYS_OUTPUT: |
| 1573 | return &((struct lysp_node_action_inout *)node)->musts; |
| 1574 | default: |
| 1575 | return NULL; |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | struct lysp_restr * |
| 1580 | lysp_node_musts(const struct lysp_node *node) |
| 1581 | { |
| 1582 | struct lysp_restr **musts; |
| 1583 | |
| 1584 | musts = lysp_node_musts_p(node); |
| 1585 | if (musts) { |
| 1586 | return *musts; |
| 1587 | } else { |
| 1588 | return NULL; |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | struct lysp_when ** |
| 1593 | lysp_node_when_p(const struct lysp_node *node) |
| 1594 | { |
| 1595 | if (!node) { |
| 1596 | return NULL; |
| 1597 | } |
| 1598 | |
Radek Krejci | 2d5f6df | 2021-01-28 14:00:13 +0100 | [diff] [blame] | 1599 | switch (node->nodetype) { |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1600 | case LYS_CONTAINER: |
| 1601 | return &((struct lysp_node_container *)node)->when; |
| 1602 | case LYS_CHOICE: |
| 1603 | return &((struct lysp_node_choice *)node)->when; |
| 1604 | case LYS_LEAF: |
| 1605 | return &((struct lysp_node_leaf *)node)->when; |
| 1606 | case LYS_LEAFLIST: |
| 1607 | return &((struct lysp_node_leaflist *)node)->when; |
| 1608 | case LYS_LIST: |
| 1609 | return &((struct lysp_node_list *)node)->when; |
| 1610 | case LYS_ANYXML: |
| 1611 | case LYS_ANYDATA: |
| 1612 | return &((struct lysp_node_anydata *)node)->when; |
| 1613 | case LYS_CASE: |
| 1614 | return &((struct lysp_node_case *)node)->when; |
| 1615 | case LYS_USES: |
| 1616 | return &((struct lysp_node_uses *)node)->when; |
| 1617 | case LYS_AUGMENT: |
| 1618 | return &((struct lysp_node_augment *)node)->when; |
| 1619 | default: |
| 1620 | return NULL; |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | struct lysp_when * |
| 1625 | lysp_node_when(const struct lysp_node *node) |
| 1626 | { |
| 1627 | struct lysp_when **when; |
| 1628 | |
| 1629 | when = lysp_node_when_p(node); |
| 1630 | if (when) { |
| 1631 | return *when; |
| 1632 | } else { |
| 1633 | return NULL; |
| 1634 | } |
| 1635 | } |
| 1636 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1637 | struct lysc_node_action ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1638 | lysc_node_actions_p(struct lysc_node *node) |
| 1639 | { |
| 1640 | assert(node); |
| 1641 | switch (node->nodetype) { |
| 1642 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1643 | return &((struct lysc_node_container *)node)->actions; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1644 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1645 | return &((struct lysc_node_list *)node)->actions; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1646 | default: |
| 1647 | return NULL; |
| 1648 | } |
| 1649 | } |
| 1650 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame^] | 1651 | LIBYANG_API_DEF const struct lysc_node_action * |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1652 | lysc_node_actions(const struct lysc_node *node) |
| 1653 | { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1654 | struct lysc_node_action **actions; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1655 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1656 | actions = lysc_node_actions_p((struct lysc_node *)node); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1657 | if (actions) { |
| 1658 | return *actions; |
| 1659 | } else { |
| 1660 | return NULL; |
| 1661 | } |
| 1662 | } |
| 1663 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1664 | struct lysc_node_notif ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1665 | lysc_node_notifs_p(struct lysc_node *node) |
| 1666 | { |
| 1667 | assert(node); |
| 1668 | switch (node->nodetype) { |
| 1669 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1670 | return &((struct lysc_node_container *)node)->notifs; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1671 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1672 | return &((struct lysc_node_list *)node)->notifs; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1673 | default: |
| 1674 | return NULL; |
| 1675 | } |
| 1676 | } |
| 1677 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame^] | 1678 | LIBYANG_API_DEF const struct lysc_node_notif * |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1679 | lysc_node_notifs(const struct lysc_node *node) |
| 1680 | { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1681 | struct lysc_node_notif **notifs; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1682 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1683 | notifs = lysc_node_notifs_p((struct lysc_node *)node); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1684 | if (notifs) { |
| 1685 | return *notifs; |
| 1686 | } else { |
| 1687 | return NULL; |
| 1688 | } |
| 1689 | } |
| 1690 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1691 | struct lysc_node ** |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1692 | lysc_node_child_p(const struct lysc_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1693 | { |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1694 | assert(node && !(node->nodetype & (LYS_RPC | LYS_ACTION))); |
| 1695 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1696 | switch (node->nodetype) { |
| 1697 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1698 | return &((struct lysc_node_container *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1699 | case LYS_CHOICE: |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 1700 | return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 1701 | case LYS_CASE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1702 | return &((struct lysc_node_case *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1703 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1704 | return &((struct lysc_node_list *)node)->child; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1705 | case LYS_INPUT: |
| 1706 | case LYS_OUTPUT: |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1707 | return &((struct lysc_node_action_inout *)node)->child; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 1708 | case LYS_NOTIF: |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1709 | return &((struct lysc_node_notif *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1710 | default: |
| 1711 | return NULL; |
| 1712 | } |
| 1713 | } |
| 1714 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame^] | 1715 | LIBYANG_API_DEF const struct lysc_node * |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1716 | lysc_node_child(const struct lysc_node *node) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1717 | { |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1718 | struct lysc_node **child; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1719 | |
| 1720 | if (!node) { |
| 1721 | return NULL; |
| 1722 | } |
| 1723 | |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1724 | if (node->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 1725 | return &((struct lysc_node_action *)node)->input.node; |
Radek Krejci | be15444 | 2021-01-21 11:06:36 +0100 | [diff] [blame] | 1726 | } else { |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1727 | child = lysc_node_child_p(node); |
| 1728 | if (child) { |
| 1729 | return *child; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1730 | } |
Michal Vasko | 2a66871 | 2020-10-21 11:48:09 +0200 | [diff] [blame] | 1731 | } |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1732 | |
| 1733 | return NULL; |
Michal Vasko | 2a66871 | 2020-10-21 11:48:09 +0200 | [diff] [blame] | 1734 | } |
| 1735 | |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1736 | struct lysc_must ** |
| 1737 | lysc_node_musts_p(const struct lysc_node *node) |
| 1738 | { |
| 1739 | if (!node) { |
| 1740 | return NULL; |
| 1741 | } |
| 1742 | |
Radek Krejci | 2d5f6df | 2021-01-28 14:00:13 +0100 | [diff] [blame] | 1743 | switch (node->nodetype) { |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1744 | case LYS_CONTAINER: |
| 1745 | return &((struct lysc_node_container *)node)->musts; |
| 1746 | case LYS_LEAF: |
| 1747 | return &((struct lysc_node_leaf *)node)->musts; |
| 1748 | case LYS_LEAFLIST: |
| 1749 | return &((struct lysc_node_leaflist *)node)->musts; |
| 1750 | case LYS_LIST: |
| 1751 | return &((struct lysc_node_list *)node)->musts; |
| 1752 | case LYS_ANYXML: |
| 1753 | case LYS_ANYDATA: |
| 1754 | return &((struct lysc_node_anydata *)node)->musts; |
| 1755 | case LYS_NOTIF: |
| 1756 | return &((struct lysc_node_notif *)node)->musts; |
| 1757 | case LYS_INPUT: |
| 1758 | case LYS_OUTPUT: |
| 1759 | return &((struct lysc_node_action_inout *)node)->musts; |
| 1760 | default: |
| 1761 | return NULL; |
| 1762 | } |
| 1763 | } |
| 1764 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame^] | 1765 | LIBYANG_API_DEF struct lysc_must * |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1766 | lysc_node_musts(const struct lysc_node *node) |
| 1767 | { |
| 1768 | struct lysc_must **must_p; |
| 1769 | |
| 1770 | must_p = lysc_node_musts_p(node); |
| 1771 | if (must_p) { |
| 1772 | return *must_p; |
| 1773 | } else { |
| 1774 | return NULL; |
| 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | struct lysc_when *** |
| 1779 | lysc_node_when_p(const struct lysc_node *node) |
| 1780 | { |
| 1781 | if (!node) { |
| 1782 | return NULL; |
| 1783 | } |
| 1784 | |
Radek Krejci | 2d5f6df | 2021-01-28 14:00:13 +0100 | [diff] [blame] | 1785 | switch (node->nodetype) { |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1786 | case LYS_CONTAINER: |
| 1787 | return &((struct lysc_node_container *)node)->when; |
| 1788 | case LYS_CHOICE: |
| 1789 | return &((struct lysc_node_choice *)node)->when; |
| 1790 | case LYS_LEAF: |
| 1791 | return &((struct lysc_node_leaf *)node)->when; |
| 1792 | case LYS_LEAFLIST: |
| 1793 | return &((struct lysc_node_leaflist *)node)->when; |
| 1794 | case LYS_LIST: |
| 1795 | return &((struct lysc_node_list *)node)->when; |
| 1796 | case LYS_ANYXML: |
| 1797 | case LYS_ANYDATA: |
| 1798 | return &((struct lysc_node_anydata *)node)->when; |
| 1799 | case LYS_CASE: |
| 1800 | return &((struct lysc_node_case *)node)->when; |
| 1801 | case LYS_NOTIF: |
| 1802 | return &((struct lysc_node_notif *)node)->when; |
| 1803 | case LYS_RPC: |
| 1804 | case LYS_ACTION: |
| 1805 | return &((struct lysc_node_action *)node)->when; |
| 1806 | default: |
| 1807 | return NULL; |
| 1808 | } |
| 1809 | } |
| 1810 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame^] | 1811 | LIBYANG_API_DEF struct lysc_when ** |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1812 | lysc_node_when(const struct lysc_node *node) |
| 1813 | { |
| 1814 | struct lysc_when ***when_p; |
| 1815 | |
| 1816 | when_p = lysc_node_when_p(node); |
| 1817 | if (when_p) { |
| 1818 | return *when_p; |
| 1819 | } else { |
| 1820 | return NULL; |
| 1821 | } |
| 1822 | } |
| 1823 | |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1824 | struct lys_module * |
| 1825 | lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod) |
| 1826 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1827 | for (uint32_t u = 0; u < ctx->list.count; ++u) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1828 | if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1829 | return (struct lys_module *)ctx->list.objs[u]; |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1830 | } |
| 1831 | } |
| 1832 | return NULL; |
| 1833 | } |
| 1834 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1835 | enum ly_stmt |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1836 | lysp_match_kw(struct ly_in *in, uint64_t *indent) |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 1837 | { |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1838 | /** |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1839 | * @brief Move the input by COUNT items. Also updates the indent value in yang parser context |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1840 | * @param[in] COUNT number of items for which the DATA pointer is supposed to move on. |
Michal Vasko | 64246d8 | 2020-08-19 12:35:00 +0200 | [diff] [blame] | 1841 | * |
| 1842 | * *INDENT-OFF* |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1843 | */ |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1844 | #define MOVE_IN(COUNT) \ |
| 1845 | ly_in_skip(in, COUNT); \ |
| 1846 | if (indent) { \ |
| 1847 | (*indent)+=COUNT; \ |
| 1848 | } |
| 1849 | #define IF_KW(STR, LEN, STMT) \ |
| 1850 | if (!strncmp(in->current, STR, LEN)) { \ |
| 1851 | MOVE_IN(LEN); \ |
| 1852 | (*kw)=STMT; \ |
| 1853 | } |
| 1854 | #define IF_KW_PREFIX(STR, LEN) \ |
| 1855 | if (!strncmp(in->current, STR, LEN)) { \ |
| 1856 | MOVE_IN(LEN); |
| 1857 | #define IF_KW_PREFIX_END \ |
| 1858 | } |
David Sedlák | 572e7ab | 2019-06-04 16:01:58 +0200 | [diff] [blame] | 1859 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1860 | const char *start = in->current; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1861 | enum ly_stmt result = LY_STMT_NONE; |
| 1862 | enum ly_stmt *kw = &result; |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1863 | /* read the keyword itself */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1864 | switch (in->current[0]) { |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1865 | case 'a': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1866 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1867 | IF_KW("rgument", 7, LY_STMT_ARGUMENT) |
| 1868 | else IF_KW("ugment", 6, LY_STMT_AUGMENT) |
| 1869 | else IF_KW("ction", 5, LY_STMT_ACTION) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1870 | else IF_KW_PREFIX("ny", 2) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1871 | IF_KW("data", 4, LY_STMT_ANYDATA) |
| 1872 | else IF_KW("xml", 3, LY_STMT_ANYXML) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1873 | IF_KW_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1874 | break; |
| 1875 | case 'b': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1876 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1877 | IF_KW("ase", 3, LY_STMT_BASE) |
| 1878 | else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO) |
| 1879 | else IF_KW("it", 2, LY_STMT_BIT) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1880 | break; |
| 1881 | case 'c': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1882 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1883 | IF_KW("ase", 3, LY_STMT_CASE) |
| 1884 | else IF_KW("hoice", 5, LY_STMT_CHOICE) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1885 | else IF_KW_PREFIX("on", 2) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1886 | IF_KW("fig", 3, LY_STMT_CONFIG) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1887 | else IF_KW_PREFIX("ta", 2) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1888 | IF_KW("ct", 2, LY_STMT_CONTACT) |
| 1889 | else IF_KW("iner", 4, LY_STMT_CONTAINER) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1890 | IF_KW_PREFIX_END |
| 1891 | IF_KW_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1892 | break; |
| 1893 | case 'd': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1894 | MOVE_IN(1); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1895 | IF_KW_PREFIX("e", 1) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1896 | IF_KW("fault", 5, LY_STMT_DEFAULT) |
| 1897 | else IF_KW("scription", 9, LY_STMT_DESCRIPTION) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1898 | else IF_KW_PREFIX("viat", 4) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1899 | IF_KW("e", 1, LY_STMT_DEVIATE) |
| 1900 | else IF_KW("ion", 3, LY_STMT_DEVIATION) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1901 | IF_KW_PREFIX_END |
| 1902 | IF_KW_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1903 | break; |
| 1904 | case 'e': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1905 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1906 | IF_KW("num", 3, LY_STMT_ENUM) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1907 | else IF_KW_PREFIX("rror-", 5) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1908 | IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG) |
| 1909 | else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1910 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1911 | else IF_KW("xtension", 8, LY_STMT_EXTENSION) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1912 | break; |
| 1913 | case 'f': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1914 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1915 | IF_KW("eature", 6, LY_STMT_FEATURE) |
| 1916 | else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1917 | break; |
| 1918 | case 'g': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1919 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1920 | IF_KW("rouping", 7, LY_STMT_GROUPING) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1921 | break; |
| 1922 | case 'i': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1923 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1924 | IF_KW("dentity", 7, LY_STMT_IDENTITY) |
| 1925 | else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE) |
| 1926 | else IF_KW("mport", 5, LY_STMT_IMPORT) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1927 | else IF_KW_PREFIX("n", 1) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1928 | IF_KW("clude", 5, LY_STMT_INCLUDE) |
| 1929 | else IF_KW("put", 3, LY_STMT_INPUT) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1930 | IF_KW_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1931 | break; |
| 1932 | case 'k': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1933 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1934 | IF_KW("ey", 2, LY_STMT_KEY) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1935 | break; |
| 1936 | case 'l': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1937 | MOVE_IN(1); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1938 | IF_KW_PREFIX("e", 1) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1939 | IF_KW("af-list", 7, LY_STMT_LEAF_LIST) |
| 1940 | else IF_KW("af", 2, LY_STMT_LEAF) |
| 1941 | else IF_KW("ngth", 4, LY_STMT_LENGTH) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1942 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1943 | else IF_KW("ist", 3, LY_STMT_LIST) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1944 | break; |
| 1945 | case 'm': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1946 | MOVE_IN(1); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1947 | IF_KW_PREFIX("a", 1) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1948 | IF_KW("ndatory", 7, LY_STMT_MANDATORY) |
| 1949 | else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1950 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1951 | else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS) |
| 1952 | else IF_KW("ust", 3, LY_STMT_MUST) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1953 | else IF_KW_PREFIX("od", 2) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1954 | IF_KW("ule", 3, LY_STMT_MODULE) |
| 1955 | else IF_KW("ifier", 5, LY_STMT_MODIFIER) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1956 | IF_KW_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1957 | break; |
| 1958 | case 'n': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1959 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1960 | IF_KW("amespace", 8, LY_STMT_NAMESPACE) |
| 1961 | else IF_KW("otification", 11, LY_STMT_NOTIFICATION) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1962 | break; |
| 1963 | case 'o': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1964 | MOVE_IN(1); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1965 | IF_KW_PREFIX("r", 1) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1966 | IF_KW("dered-by", 8, LY_STMT_ORDERED_BY) |
| 1967 | else IF_KW("ganization", 10, LY_STMT_ORGANIZATION) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1968 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1969 | else IF_KW("utput", 5, LY_STMT_OUTPUT) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1970 | break; |
| 1971 | case 'p': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1972 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1973 | IF_KW("ath", 3, LY_STMT_PATH) |
| 1974 | else IF_KW("attern", 6, LY_STMT_PATTERN) |
| 1975 | else IF_KW("osition", 7, LY_STMT_POSITION) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1976 | else IF_KW_PREFIX("re", 2) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1977 | IF_KW("fix", 3, LY_STMT_PREFIX) |
| 1978 | else IF_KW("sence", 5, LY_STMT_PRESENCE) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1979 | IF_KW_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1980 | break; |
| 1981 | case 'r': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1982 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1983 | IF_KW("ange", 4, LY_STMT_RANGE) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1984 | else IF_KW_PREFIX("e", 1) |
| 1985 | IF_KW_PREFIX("f", 1) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1986 | IF_KW("erence", 6, LY_STMT_REFERENCE) |
| 1987 | else IF_KW("ine", 3, LY_STMT_REFINE) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1988 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1989 | else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE) |
| 1990 | else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE) |
| 1991 | else IF_KW("vision", 6, LY_STMT_REVISION) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1992 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1993 | else IF_KW("pc", 2, LY_STMT_RPC) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1994 | break; |
| 1995 | case 's': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1996 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1997 | IF_KW("tatus", 5, LY_STMT_STATUS) |
| 1998 | else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1999 | break; |
| 2000 | case 't': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2001 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2002 | IF_KW("ypedef", 6, LY_STMT_TYPEDEF) |
| 2003 | else IF_KW("ype", 3, LY_STMT_TYPE) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 2004 | break; |
| 2005 | case 'u': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2006 | MOVE_IN(1); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 2007 | IF_KW_PREFIX("ni", 2) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2008 | IF_KW("que", 3, LY_STMT_UNIQUE) |
| 2009 | else IF_KW("ts", 2, LY_STMT_UNITS) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 2010 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2011 | else IF_KW("ses", 3, LY_STMT_USES) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 2012 | break; |
| 2013 | case 'v': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2014 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2015 | IF_KW("alue", 4, LY_STMT_VALUE) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 2016 | break; |
| 2017 | case 'w': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2018 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2019 | IF_KW("hen", 3, LY_STMT_WHEN) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 2020 | break; |
| 2021 | case 'y': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2022 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2023 | IF_KW("ang-version", 11, LY_STMT_YANG_VERSION) |
| 2024 | else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 2025 | break; |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 2026 | default: |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2027 | /* if indent is not NULL we are matching keyword from YANG data */ |
| 2028 | if (indent) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 2029 | if (in->current[0] == ';') { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2030 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2031 | *kw = LY_STMT_SYNTAX_SEMICOLON; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 2032 | } else if (in->current[0] == '{') { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2033 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2034 | *kw = LY_STMT_SYNTAX_LEFT_BRACE; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 2035 | } else if (in->current[0] == '}') { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2036 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2037 | *kw = LY_STMT_SYNTAX_RIGHT_BRACE; |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 2038 | } |
| 2039 | } |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 2040 | break; |
| 2041 | } |
| 2042 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 2043 | if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) { |
Radek Krejci | 6e546bf | 2020-05-19 16:16:19 +0200 | [diff] [blame] | 2044 | /* the keyword is not terminated */ |
| 2045 | *kw = LY_STMT_NONE; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 2046 | in->current = start; |
Radek Krejci | 6e546bf | 2020-05-19 16:16:19 +0200 | [diff] [blame] | 2047 | } |
| 2048 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 2049 | #undef IF_KW |
| 2050 | #undef IF_KW_PREFIX |
| 2051 | #undef IF_KW_PREFIX_END |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 2052 | #undef MOVE_IN |
Michal Vasko | 64246d8 | 2020-08-19 12:35:00 +0200 | [diff] [blame] | 2053 | /* *INDENT-ON* */ |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 2054 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 2055 | return result; |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 2056 | } |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 2057 | |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2058 | LY_ERR |
| 2059 | lysp_ext_find_definition(const struct ly_ctx *ctx, const struct lysp_ext_instance *ext, const struct lys_module **ext_mod, |
| 2060 | struct lysp_ext **ext_def) |
| 2061 | { |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2062 | const char *tmp, *name, *prefix; |
| 2063 | size_t pref_len, name_len; |
| 2064 | LY_ARRAY_COUNT_TYPE v; |
| 2065 | const struct lys_module *mod = NULL; |
| 2066 | |
Radek Krejci | cbb6242 | 2021-03-15 09:29:21 +0100 | [diff] [blame] | 2067 | assert(ext_def); |
| 2068 | |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2069 | *ext_def = NULL; |
| 2070 | if (ext_mod) { |
| 2071 | *ext_mod = NULL; |
| 2072 | } |
| 2073 | |
Radek Krejci | 677abea | 2021-03-05 14:24:53 +0100 | [diff] [blame] | 2074 | /* parse the prefix, the nodeid was previously already parsed and checked */ |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2075 | tmp = ext->name; |
Radek Krejci | 677abea | 2021-03-05 14:24:53 +0100 | [diff] [blame] | 2076 | ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len); |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2077 | |
| 2078 | /* get module where the extension definition should be placed */ |
| 2079 | mod = ly_resolve_prefix(ctx, prefix, pref_len, ext->format, ext->prefix_data); |
| 2080 | if (!mod) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 2081 | LOGVAL(ctx, LYVE_REFERENCE, "Invalid prefix \"%.*s\" used for extension instance identifier.", (int)pref_len, prefix); |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2082 | return LY_EVALID; |
| 2083 | } else if (!mod->parsed->extensions) { |
| 2084 | LOGVAL(ctx, LYVE_REFERENCE, "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 2085 | ext->name, mod->name); |
| 2086 | return LY_EVALID; |
| 2087 | } |
| 2088 | |
| 2089 | /* find the parsed extension definition there */ |
| 2090 | LY_ARRAY_FOR(mod->parsed->extensions, v) { |
| 2091 | if (!strcmp(name, mod->parsed->extensions[v].name)) { |
| 2092 | *ext_def = &mod->parsed->extensions[v]; |
| 2093 | break; |
| 2094 | } |
| 2095 | } |
| 2096 | |
Radek Krejci | cbb6242 | 2021-03-15 09:29:21 +0100 | [diff] [blame] | 2097 | if (!(*ext_def)) { |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2098 | LOGVAL(ctx, LYVE_REFERENCE, "Extension definition of extension instance \"%s\" not found.", ext->name); |
| 2099 | return LY_EVALID; |
| 2100 | } |
| 2101 | |
| 2102 | if (ext_mod) { |
| 2103 | *ext_mod = mod; |
| 2104 | } |
| 2105 | return LY_SUCCESS; |
| 2106 | } |
| 2107 | |
| 2108 | LY_ERR |
| 2109 | lysp_ext_instance_resolve_argument(struct ly_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysp_ext *ext_def) |
| 2110 | { |
Radek Krejci | 9f87b0c | 2021-03-05 14:45:26 +0100 | [diff] [blame] | 2111 | if (!ext_def->argname || ext_p->argument) { |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2112 | /* nothing to do */ |
| 2113 | return LY_SUCCESS; |
| 2114 | } |
| 2115 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 2116 | if (ext_p->format == LY_VALUE_XML) { |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2117 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 2118 | struct lysp_stmt *stmt = NULL; |
| 2119 | |
| 2120 | if (ext_def->flags & LYS_YINELEM_TRUE) { |
| 2121 | /* ... argument was the first XML child element */ |
| 2122 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {} |
| 2123 | if (stmt) { |
| 2124 | const char *arg, *ext, *name_arg, *name_ext, *prefix_arg, *prefix_ext; |
| 2125 | size_t name_arg_len, name_ext_len, prefix_arg_len, prefix_ext_len; |
| 2126 | |
| 2127 | stmt = ext_p->child; |
| 2128 | |
| 2129 | arg = stmt->stmt; |
| 2130 | ly_parse_nodeid(&arg, &prefix_arg, &prefix_arg_len, &name_arg, &name_arg_len); |
Radek Krejci | 9f87b0c | 2021-03-05 14:45:26 +0100 | [diff] [blame] | 2131 | if (ly_strncmp(ext_def->argname, name_arg, name_arg_len)) { |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2132 | LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" expects argument element \"%s\" as its first XML child, " |
Radek Krejci | 9f87b0c | 2021-03-05 14:45:26 +0100 | [diff] [blame] | 2133 | "but \"%.*s\" element found.", ext_p->name, ext_def->argname, (int)name_arg_len, name_arg); |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2134 | return LY_EVALID; |
| 2135 | } |
| 2136 | |
| 2137 | /* check namespace - all the extension instances must be qualified and argument element is expected in the same |
| 2138 | * namespace. Do not check just prefixes, there can be different prefixes pointing to the same namespace */ |
| 2139 | ext = ext_p->name; /* include prefix */ |
| 2140 | ly_parse_nodeid(&ext, &prefix_ext, &prefix_ext_len, &name_ext, &name_ext_len); |
| 2141 | |
| 2142 | if (ly_resolve_prefix(ctx, prefix_ext, prefix_ext_len, ext_p->format, ext_p->prefix_data) != |
| 2143 | ly_resolve_prefix(ctx, prefix_arg, prefix_arg_len, stmt->format, stmt->prefix_data)) { |
| 2144 | LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" element and its argument element \"%s\" are " |
Radek Krejci | 9f87b0c | 2021-03-05 14:45:26 +0100 | [diff] [blame] | 2145 | "expected in the same namespace, but they differ.", ext_p->name, ext_def->argname); |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2146 | return LY_EVALID; |
| 2147 | } |
| 2148 | } |
| 2149 | } else { |
| 2150 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 2151 | * with LYS_YIN_ATTR flag */ |
| 2152 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
Radek Krejci | 9f87b0c | 2021-03-05 14:45:26 +0100 | [diff] [blame] | 2153 | if (!strcmp(stmt->stmt, ext_def->argname)) { |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2154 | /* this is the extension's argument */ |
| 2155 | break; |
| 2156 | } |
| 2157 | } |
| 2158 | } |
| 2159 | |
| 2160 | if (stmt) { |
| 2161 | LY_CHECK_RET(lydict_insert(ctx, stmt->arg, 0, &ext_p->argument)); |
| 2162 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 2163 | } |
| 2164 | } |
| 2165 | |
| 2166 | if (!ext_p->argument) { |
| 2167 | /* missing extension's argument */ |
| 2168 | LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" misses argument %s\"%s\".", |
Radek Krejci | 9f87b0c | 2021-03-05 14:45:26 +0100 | [diff] [blame] | 2169 | ext_p->name, (ext_def->flags & LYS_YINELEM_TRUE) ? "element " : "", ext_def->argname); |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2170 | return LY_EVALID; |
| 2171 | } |
| 2172 | |
| 2173 | return LY_SUCCESS; |
| 2174 | } |
| 2175 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2176 | LY_ARRAY_COUNT_TYPE |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 2177 | lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, enum ly_stmt substmt) |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2178 | { |
| 2179 | LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL); |
| 2180 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2181 | for ( ; index < LY_ARRAY_COUNT(ext); index++) { |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 2182 | if (ext[index].parent_stmt == substmt) { |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2183 | return index; |
| 2184 | } |
| 2185 | } |
| 2186 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2187 | return LY_ARRAY_COUNT(ext); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2188 | } |
| 2189 | |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 2190 | const struct lysc_node * |
Michal Vasko | 7224488 | 2021-01-12 15:21:05 +0100 | [diff] [blame] | 2191 | lysc_data_node(const struct lysc_node *schema) |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 2192 | { |
| 2193 | const struct lysc_node *parent; |
| 2194 | |
Michal Vasko | 7224488 | 2021-01-12 15:21:05 +0100 | [diff] [blame] | 2195 | parent = schema; |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 2196 | while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | |
| 2197 | LYS_ACTION | LYS_NOTIF))) { |
Radek Krejci | 7d95fbb | 2021-01-26 17:33:13 +0100 | [diff] [blame] | 2198 | parent = parent->parent; |
Michal Vasko | 7224488 | 2021-01-12 15:21:05 +0100 | [diff] [blame] | 2199 | } |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 2200 | |
| 2201 | return parent; |
| 2202 | } |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 2203 | |
| 2204 | ly_bool |
| 2205 | lys_has_recompiled(const struct lys_module *mod) |
| 2206 | { |
| 2207 | LY_ARRAY_COUNT_TYPE u; |
| 2208 | |
| 2209 | if (LYSP_HAS_RECOMPILED(mod->parsed)) { |
| 2210 | return 1; |
| 2211 | } |
| 2212 | |
| 2213 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
| 2214 | if (LYSP_HAS_RECOMPILED(mod->parsed->includes[u].submodule)) { |
| 2215 | return 1; |
| 2216 | } |
| 2217 | } |
| 2218 | |
| 2219 | return 0; |
| 2220 | } |
| 2221 | |
| 2222 | ly_bool |
| 2223 | lys_has_compiled(const struct lys_module *mod) |
| 2224 | { |
| 2225 | LY_ARRAY_COUNT_TYPE u; |
| 2226 | |
| 2227 | if (LYSP_HAS_COMPILED(mod->parsed)) { |
| 2228 | return 1; |
| 2229 | } |
| 2230 | |
| 2231 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
| 2232 | if (LYSP_HAS_COMPILED(mod->parsed->includes[u].submodule)) { |
| 2233 | return 1; |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | return 0; |
| 2238 | } |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 2239 | |
| 2240 | ly_bool |
| 2241 | lys_has_groupings(const struct lys_module *mod) |
| 2242 | { |
| 2243 | LY_ARRAY_COUNT_TYPE u; |
| 2244 | |
| 2245 | if (mod->parsed->groupings) { |
| 2246 | return 1; |
| 2247 | } |
| 2248 | |
| 2249 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
| 2250 | if (mod->parsed->includes[u].submodule->groupings) { |
| 2251 | return 1; |
| 2252 | } |
| 2253 | } |
| 2254 | |
| 2255 | return 0; |
| 2256 | } |