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