blob: e82f6b9000c14363d1578291bc481af10838ac36 [file] [log] [blame]
Radek Krejci86d106e2018-10-18 09:53:19 +02001/**
Michal Vasko59892dd2022-05-13 11:02:30 +02002 * @file tree_schema_common.c
Radek Krejci86d106e2018-10-18 09:53:19 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko59892dd2022-05-13 11:02:30 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
5 * @brief Parsing and validation common functions for schema trees
Radek Krejci86d106e2018-10-18 09:53:19 +02006 *
Michal Vasko59892dd2022-05-13 11:02:30 +02007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejci86d106e2018-10-18 09:53:19 +02008 *
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 Krejci535ea9f2020-05-29 16:01:05 +020015
16#define _GNU_SOURCE
Radek Krejci86d106e2018-10-18 09:53:19 +020017
Radek Krejcie7b95092019-05-15 11:03:07 +020018#include <assert.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020019#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010020#include <stddef.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <stdint.h>
Radek Krejci9ed7a192018-10-31 16:23:51 +010022#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020024#include <time.h>
25
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020027#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "context.h"
Radek Krejci77114102021-03-10 15:21:57 +010029#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010031#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020032#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010033#include "log.h"
Michal Vasko69730152020-10-09 16:30:07 +020034#include "parser_schema.h"
Michal Vasko962b6cd2020-12-08 10:07:49 +010035#include "schema_compile.h"
Michal Vasko79135ae2020-12-16 10:08:35 +010036#include "schema_features.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "set.h"
38#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010039#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020040#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020041#include "tree_schema_internal.h"
42
Michal Vasko193dacd2022-10-13 08:43:05 +020043/**
44 * @brief information about YANG statements
45 */
46struct stmt_info_s stmt_attr_info[] = {
47 [LY_STMT_NONE] = {NULL, NULL, 0},
48 [LY_STMT_ACTION] = {"action", "name", STMT_FLAG_ID},
49 [LY_STMT_ANYDATA] = {"anydata", "name", STMT_FLAG_ID},
50 [LY_STMT_ANYXML] = {"anyxml", "name", STMT_FLAG_ID},
51 [LY_STMT_ARGUMENT] = {"argument", "name", STMT_FLAG_ID},
52 [LY_STMT_ARG_TEXT] = {"text", NULL, 0},
53 [LY_STMT_ARG_VALUE] = {"value", NULL, 0},
54 [LY_STMT_AUGMENT] = {"augment", "target-node", STMT_FLAG_ID},
55 [LY_STMT_BASE] = {"base", "name", STMT_FLAG_ID},
56 [LY_STMT_BELONGS_TO] = {"belongs-to", "module", STMT_FLAG_ID},
57 [LY_STMT_BIT] = {"bit", "name", STMT_FLAG_ID},
58 [LY_STMT_CASE] = {"case", "name", STMT_FLAG_ID},
59 [LY_STMT_CHOICE] = {"choice", "name", STMT_FLAG_ID},
60 [LY_STMT_CONFIG] = {"config", "value", STMT_FLAG_ID},
61 [LY_STMT_CONTACT] = {"contact", "text", STMT_FLAG_YIN},
62 [LY_STMT_CONTAINER] = {"container", "name", STMT_FLAG_ID},
63 [LY_STMT_DEFAULT] = {"default", "value", 0},
64 [LY_STMT_DESCRIPTION] = {"description", "text", STMT_FLAG_YIN},
65 [LY_STMT_DEVIATE] = {"deviate", "value", STMT_FLAG_ID},
66 [LY_STMT_DEVIATION] = {"deviation", "target-node", STMT_FLAG_ID},
67 [LY_STMT_ENUM] = {"enum", "name", STMT_FLAG_ID},
68 [LY_STMT_ERROR_APP_TAG] = {"error-app-tag", "value", 0},
69 [LY_STMT_ERROR_MESSAGE] = {"error-message", "value", STMT_FLAG_YIN},
70 [LY_STMT_EXTENSION] = {"extension", "name", STMT_FLAG_ID},
71 [LY_STMT_EXTENSION_INSTANCE] = {NULL, NULL, 0},
72 [LY_STMT_FEATURE] = {"feature", "name", STMT_FLAG_ID},
73 [LY_STMT_FRACTION_DIGITS] = {"fraction-digits", "value", STMT_FLAG_ID},
74 [LY_STMT_GROUPING] = {"grouping", "name", STMT_FLAG_ID},
75 [LY_STMT_IDENTITY] = {"identity", "name", STMT_FLAG_ID},
76 [LY_STMT_IF_FEATURE] = {"if-feature", "name", 0},
77 [LY_STMT_IMPORT] = {"import", "module", STMT_FLAG_ID},
78 [LY_STMT_INCLUDE] = {"include", "module", STMT_FLAG_ID},
79 [LY_STMT_INPUT] = {"input", NULL, 0},
80 [LY_STMT_KEY] = {"key", "value", 0},
81 [LY_STMT_LEAF] = {"leaf", "name", STMT_FLAG_ID},
82 [LY_STMT_LEAF_LIST] = {"leaf-list", "name", STMT_FLAG_ID},
83 [LY_STMT_LENGTH] = {"length", "value", 0},
84 [LY_STMT_LIST] = {"list", "name", STMT_FLAG_ID},
85 [LY_STMT_MANDATORY] = {"mandatory", "value", STMT_FLAG_ID},
86 [LY_STMT_MAX_ELEMENTS] = {"max-elements", "value", STMT_FLAG_ID},
87 [LY_STMT_MIN_ELEMENTS] = {"min-elements", "value", STMT_FLAG_ID},
88 [LY_STMT_MODIFIER] = {"modifier", "value", STMT_FLAG_ID},
89 [LY_STMT_MODULE] = {"module", "name", STMT_FLAG_ID},
90 [LY_STMT_MUST] = {"must", "condition", 0},
91 [LY_STMT_NAMESPACE] = {"namespace", "uri", 0},
92 [LY_STMT_NOTIFICATION] = {"notification", "name", STMT_FLAG_ID},
93 [LY_STMT_ORDERED_BY] = {"ordered-by", "value", STMT_FLAG_ID},
94 [LY_STMT_ORGANIZATION] = {"organization", "text", STMT_FLAG_YIN},
95 [LY_STMT_OUTPUT] = {"output", NULL, 0},
96 [LY_STMT_PATH] = {"path", "value", 0},
97 [LY_STMT_PATTERN] = {"pattern", "value", 0},
98 [LY_STMT_POSITION] = {"position", "value", STMT_FLAG_ID},
99 [LY_STMT_PREFIX] = {"prefix", "value", STMT_FLAG_ID},
100 [LY_STMT_PRESENCE] = {"presence", "value", 0},
101 [LY_STMT_RANGE] = {"range", "value", 0},
102 [LY_STMT_REFERENCE] = {"reference", "text", STMT_FLAG_YIN},
103 [LY_STMT_REFINE] = {"refine", "target-node", STMT_FLAG_ID},
104 [LY_STMT_REQUIRE_INSTANCE] = {"require-instance", "value", STMT_FLAG_ID},
105 [LY_STMT_REVISION] = {"revision", "date", STMT_FLAG_ID},
106 [LY_STMT_REVISION_DATE] = {"revision-date", "date", STMT_FLAG_ID},
107 [LY_STMT_RPC] = {"rpc", "name", STMT_FLAG_ID},
108 [LY_STMT_STATUS] = {"status", "value", STMT_FLAG_ID},
109 [LY_STMT_SUBMODULE] = {"submodule", "name", STMT_FLAG_ID},
110 [LY_STMT_SYNTAX_LEFT_BRACE] = {"{", NULL, 0},
111 [LY_STMT_SYNTAX_RIGHT_BRACE] = {"}", NULL, 0},
112 [LY_STMT_SYNTAX_SEMICOLON] = {";", NULL, 0},
113 [LY_STMT_TYPE] = {"type", "name", STMT_FLAG_ID},
114 [LY_STMT_TYPEDEF] = {"typedef", "name", STMT_FLAG_ID},
115 [LY_STMT_UNIQUE] = {"unique", "tag", 0},
116 [LY_STMT_UNITS] = {"units", "name", 0},
117 [LY_STMT_USES] = {"uses", "name", STMT_FLAG_ID},
118 [LY_STMT_VALUE] = {"value", "value", STMT_FLAG_ID},
119 [LY_STMT_WHEN] = {"when", "condition", 0},
120 [LY_STMT_YANG_VERSION] = {"yang-version", "value", STMT_FLAG_ID},
121 [LY_STMT_YIN_ELEMENT] = {"yin-element", "value", STMT_FLAG_ID},
122};
123
Radek Krejci85747952019-06-07 16:43:43 +0200124LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200125lysp_check_prefix(struct lysp_ctx *ctx, struct lysp_import *imports, const char *module_prefix, const char **value)
Radek Krejci86d106e2018-10-18 09:53:19 +0200126{
127 struct lysp_import *i;
128
Michal Vasko69730152020-10-09 16:30:07 +0200129 if (module_prefix && (&module_prefix != value) && !strcmp(module_prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100130 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value);
Radek Krejci86d106e2018-10-18 09:53:19 +0200131 return LY_EEXIST;
132 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100133 LY_ARRAY_FOR(imports, struct lysp_import, i) {
Michal Vasko69730152020-10-09 16:30:07 +0200134 if (i->prefix && (&i->prefix != value) && !strcmp(i->prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100135 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100136 return LY_EEXIST;
Radek Krejci86d106e2018-10-18 09:53:19 +0200137 }
138 }
139 return LY_SUCCESS;
140}
141
142LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200143lysp_check_date(struct lysp_ctx *ctx, const char *date, size_t date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +0200144{
Radek Krejci86d106e2018-10-18 09:53:19 +0200145 struct tm tm, tm_;
146 char *r;
147
Michal Vaskob36053d2020-03-26 15:49:30 +0100148 LY_CHECK_ARG_RET(ctx ? PARSER_CTX(ctx) : NULL, date, LY_EINVAL);
149 LY_CHECK_ERR_RET(date_len != LY_REV_SIZE - 1, LOGARG(ctx ? PARSER_CTX(ctx) : NULL, date_len), LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200150
Radek Krejcif13b87b2020-12-01 22:02:17 +0100151 /* check format: YYYY-MM-DD */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200152 for (uint8_t i = 0; i < date_len; i++) {
Michal Vasko69730152020-10-09 16:30:07 +0200153 if ((i == 4) || (i == 7)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200154 if (date[i] != '-') {
155 goto error;
156 }
157 } else if (!isdigit(date[i])) {
158 goto error;
159 }
160 }
161
162 /* check content, e.g. 2018-02-31 */
163 memset(&tm, 0, sizeof tm);
164 r = strptime(date, "%Y-%m-%d", &tm);
Michal Vasko69730152020-10-09 16:30:07 +0200165 if (!r || (r != &date[LY_REV_SIZE - 1])) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200166 goto error;
167 }
168 memcpy(&tm_, &tm, sizeof tm);
Michal Vasko82f32822022-08-17 10:54:35 +0200169
170 /* DST may move the hour back resulting in a different day */
171 tm_.tm_hour = 1;
172
Radek Krejci86d106e2018-10-18 09:53:19 +0200173 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
174 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
175 /* checking days is enough, since other errors
176 * have been checked by strptime() */
177 goto error;
178 }
179
180 return LY_SUCCESS;
181
182error:
Radek Krejcid33273d2018-10-25 14:55:52 +0200183 if (stmt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100184 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt);
Radek Krejcid33273d2018-10-25 14:55:52 +0200185 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200186 return LY_EINVAL;
187}
188
189void
190lysp_sort_revisions(struct lysp_revision *revs)
191{
Radek Krejci857189e2020-09-01 13:26:36 +0200192 LY_ARRAY_COUNT_TYPE i, r;
Radek Krejci86d106e2018-10-18 09:53:19 +0200193 struct lysp_revision rev;
194
Radek Krejcic7d13e32020-12-09 12:32:24 +0100195 for (i = 1, r = 0; i < LY_ARRAY_COUNT(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200196 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200197 r = i;
198 }
199 }
200
201 if (r) {
202 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200203 memcpy(&rev, &revs[0], sizeof rev);
204 memcpy(&revs[0], &revs[r], sizeof rev);
205 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200206 }
207}
Radek Krejci151a5b72018-10-19 14:21:44 +0200208
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200209LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200210lysp_check_enum_name(struct lysp_ctx *ctx, const char *name, size_t name_len)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100211{
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200212 if (!name_len) {
213 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
214 return LY_EVALID;
215 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
216 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
217 (int)name_len, name);
218 return LY_EVALID;
219 } else {
220 for (size_t u = 0; u < name_len; ++u) {
221 if (iscntrl(name[u])) {
222 LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
223 (int)name_len, name, u + 1);
224 break;
225 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100226 }
227 }
228
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200229 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100230}
231
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200232/**
233 * @brief Learn built-in type from its name.
234 *
235 * @param[in] name Type name.
236 * @param[in] len Length of @p name.
237 * @return Built-in data type, ::LY_TYPE_UNKNOWN if none matches.
238 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100239static LY_DATA_TYPE
240lysp_type_str2builtin(const char *name, size_t len)
241{
242 if (len >= 4) { /* otherwise it does not match any built-in type */
243 if (name[0] == 'b') {
244 if (name[1] == 'i') {
Michal Vasko69730152020-10-09 16:30:07 +0200245 if ((len == 6) && !strncmp(&name[2], "nary", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100246 return LY_TYPE_BINARY;
Michal Vasko69730152020-10-09 16:30:07 +0200247 } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100248 return LY_TYPE_BITS;
249 }
Michal Vasko69730152020-10-09 16:30:07 +0200250 } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100251 return LY_TYPE_BOOL;
252 }
253 } else if (name[0] == 'd') {
Michal Vasko69730152020-10-09 16:30:07 +0200254 if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100255 return LY_TYPE_DEC64;
256 }
257 } else if (name[0] == 'e') {
Michal Vasko69730152020-10-09 16:30:07 +0200258 if ((len == 5) && !strncmp(&name[1], "mpty", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100259 return LY_TYPE_EMPTY;
Michal Vasko69730152020-10-09 16:30:07 +0200260 } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100261 return LY_TYPE_ENUM;
262 }
263 } else if (name[0] == 'i') {
264 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200265 if ((len == 4) && !strncmp(&name[2], "t8", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100266 return LY_TYPE_INT8;
267 } else if (len == 5) {
268 if (!strncmp(&name[2], "t16", 3)) {
269 return LY_TYPE_INT16;
270 } else if (!strncmp(&name[2], "t32", 3)) {
271 return LY_TYPE_INT32;
272 } else if (!strncmp(&name[2], "t64", 3)) {
273 return LY_TYPE_INT64;
274 }
Michal Vasko69730152020-10-09 16:30:07 +0200275 } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100276 return LY_TYPE_INST;
277 }
Michal Vasko69730152020-10-09 16:30:07 +0200278 } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100279 return LY_TYPE_IDENT;
280 }
281 } else if (name[0] == 'l') {
Michal Vasko69730152020-10-09 16:30:07 +0200282 if ((len == 7) && !strncmp(&name[1], "eafref", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100283 return LY_TYPE_LEAFREF;
284 }
285 } else if (name[0] == 's') {
Michal Vasko69730152020-10-09 16:30:07 +0200286 if ((len == 6) && !strncmp(&name[1], "tring", 5)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100287 return LY_TYPE_STRING;
288 }
289 } else if (name[0] == 'u') {
290 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200291 if ((len == 5) && !strncmp(&name[2], "ion", 3)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100292 return LY_TYPE_UNION;
293 }
Michal Vasko69730152020-10-09 16:30:07 +0200294 } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) {
295 if ((len == 5) && (name[4] == '8')) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100296 return LY_TYPE_UINT8;
297 } else if (len == 6) {
298 if (!strncmp(&name[4], "16", 2)) {
299 return LY_TYPE_UINT16;
300 } else if (!strncmp(&name[4], "32", 2)) {
301 return LY_TYPE_UINT32;
302 } else if (!strncmp(&name[4], "64", 2)) {
303 return LY_TYPE_UINT64;
304 }
305 }
306 }
307 }
308 }
309
310 return LY_TYPE_UNKNOWN;
311}
312
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200313/**
314 * @brief Find a typedef in a sized array.
315 *
316 * @param[in] name Typedef name.
317 * @param[in] typedefs Sized array of typedefs.
318 * @return Found typedef, NULL if none.
319 */
320static const struct lysp_tpdf *
321lysp_typedef_match(const char *name, const struct lysp_tpdf *typedefs)
322{
323 LY_ARRAY_COUNT_TYPE u;
324
325 LY_ARRAY_FOR(typedefs, u) {
326 if (!strcmp(name, typedefs[u].name)) {
327 /* match */
328 return &typedefs[u];
329 }
330 }
331 return NULL;
332}
333
Radek Krejcibbe09a92018-11-08 09:36:54 +0100334LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +0100335lysp_type_find(const char *id, struct lysp_node *start_node, const struct lysp_module *start_module,
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200336 const struct lysc_ext_instance *ext, LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100337{
338 const char *str, *name;
339 struct lysp_tpdf *typedefs;
Michal Vasko193dacd2022-10-13 08:43:05 +0200340 const struct lysp_tpdf *ext_typedefs;
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100341 const struct lys_module *mod;
Michal Vaskoa99b3572021-02-01 11:54:58 +0100342 const struct lysp_module *local_module;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200343 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100344
345 assert(id);
346 assert(start_module);
347 assert(tpdf);
348 assert(node);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100349
Radek Krejci4f28eda2018-11-12 11:46:16 +0100350 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100351 str = strchr(id, ':');
352 if (str) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200353 mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_VALUE_SCHEMA, (void *)start_module);
Michal Vaskoa99b3572021-02-01 11:54:58 +0100354 local_module = mod ? mod->parsed : NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100355 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100356 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100357 } else {
Michal Vaskoa99b3572021-02-01 11:54:58 +0100358 local_module = start_module;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100359 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100360
361 /* check for built-in types */
362 *type = lysp_type_str2builtin(name, strlen(name));
363 if (*type) {
364 *tpdf = NULL;
365 return LY_SUCCESS;
366 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100367 }
Michal Vaskoa99b3572021-02-01 11:54:58 +0100368 LY_CHECK_RET(!local_module, LY_ENOTFOUND);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100369
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200370 if (local_module == start_module) {
371 if (start_node) {
372 /* search typedefs in parent's nodes */
373 for (*node = start_node; *node; *node = (*node)->parent) {
374 *tpdf = lysp_typedef_match(name, lysp_node_typedefs(*node));
375 if (*tpdf) {
376 /* match */
377 return LY_SUCCESS;
378 }
379 }
380 }
381
382 if (ext) {
383 /* search typedefs directly in the extension */
Michal Vasko193dacd2022-10-13 08:43:05 +0200384 lyplg_ext_parsed_get_storage(ext, LY_STMT_TYPEDEF, (const void **)&ext_typedefs);
385 if ((*tpdf = lysp_typedef_match(name, ext_typedefs))) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100386 /* match */
387 return LY_SUCCESS;
388 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100389 }
390 }
391
Michal Vasko915e5442021-06-08 14:59:21 +0200392 /* go to main module if in submodule */
393 local_module = local_module->mod->parsed;
394
Radek Krejcibbe09a92018-11-08 09:36:54 +0100395 /* search in top-level typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100396 if (local_module->typedefs) {
397 LY_ARRAY_FOR(local_module->typedefs, u) {
398 if (!strcmp(name, local_module->typedefs[u].name)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100399 /* match */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100400 *tpdf = &local_module->typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100401 return LY_SUCCESS;
402 }
403 }
404 }
405
Michal Vasko915e5442021-06-08 14:59:21 +0200406 /* search in all submodules' typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100407 LY_ARRAY_FOR(local_module->includes, u) {
408 typedefs = local_module->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100409 LY_ARRAY_FOR(typedefs, v) {
410 if (!strcmp(name, typedefs[v].name)) {
411 /* match */
412 *tpdf = &typedefs[v];
413 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100414 }
415 }
416 }
417
418 return LY_ENOTFOUND;
419}
420
Michal Vaskob36053d2020-03-26 15:49:30 +0100421/**
aPiecekdc12b9f2021-06-25 10:55:47 +0200422 * @brief Insert @p name to hash table and if @p name has already
423 * been added, then log an error.
424 *
425 * This function is used to detect duplicate names.
426 *
427 * @param[in,out] ctx Context to log the error.
428 * @param[in,out] ht Hash table with top-level names.
429 * @param[in] name Inserted top-level identifier.
430 * @param[in] statement The name of the statement type from which
431 * @p name originated (eg typedef, feature, ...).
432 * @param[in] err_detail Optional error specification.
433 * @return LY_ERR, but LY_EEXIST is mapped to LY_EVALID.
434 */
435static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200436lysp_check_dup_ht_insert(struct lysp_ctx *ctx, struct hash_table *ht,
aPiecekdc12b9f2021-06-25 10:55:47 +0200437 const char *name, const char *statement, const char *err_detail)
438{
439 LY_ERR ret;
440 uint32_t hash;
441
442 hash = dict_hash(name, strlen(name));
443 ret = lyht_insert(ht, &name, hash, NULL);
444 if (ret == LY_EEXIST) {
445 if (err_detail) {
446 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT2, name, statement, err_detail);
447 } else {
448 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, name, statement);
449 }
450 ret = LY_EVALID;
451 }
452
453 return ret;
454}
455
456/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100457 * @brief Check name of a new type to avoid name collisions.
458 *
459 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
460 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
461 * @param[in] tpdf Typedef definition to check.
462 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
aPiecekc7594b72021-06-29 09:00:03 +0200463 * typedefs are checked, caller is supposed to free the table.
464 * @return LY_EVALID in case of collision, LY_SUCCESS otherwise.
Radek Krejcibbe09a92018-11-08 09:36:54 +0100465 */
466static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200467lysp_check_dup_typedef(struct lysp_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
aPieceke1fbd952021-06-29 08:12:55 +0200468 struct hash_table *tpdfs_global)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100469{
470 struct lysp_node *parent;
471 uint32_t hash;
472 size_t name_len;
473 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200474 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100475 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100476
477 assert(ctx);
478 assert(tpdf);
479
480 name = tpdf->name;
481 name_len = strlen(name);
482
Radek Krejci4f28eda2018-11-12 11:46:16 +0100483 if (lysp_type_str2builtin(name, name_len)) {
aPiecekc7594b72021-06-29 09:00:03 +0200484 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
485 "Duplicate identifier \"%s\" of typedef statement - name collision with a built-in type.", name);
486 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100487 }
488
489 /* check locally scoped typedefs (avoid name shadowing) */
490 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100491 typedefs = lysp_node_typedefs(node);
492 LY_ARRAY_FOR(typedefs, u) {
493 if (&typedefs[u] == tpdf) {
494 break;
495 }
496 if (!strcmp(name, typedefs[u].name)) {
aPiecekc7594b72021-06-29 09:00:03 +0200497 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
498 "Duplicate identifier \"%s\" of typedef statement - name collision with sibling type.", name);
499 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100500 }
501 }
502 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200503 for (parent = node->parent; parent; parent = parent->parent) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200504 if (lysp_typedef_match(name, lysp_node_typedefs(parent))) {
aPiecekc7594b72021-06-29 09:00:03 +0200505 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
506 "Duplicate identifier \"%s\" of typedef statement - name collision with another scoped type.", name);
507 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100508 }
509 }
510 }
511
512 /* check collision with the top-level typedefs */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100513 if (node) {
aPiecekc7594b72021-06-29 09:00:03 +0200514 hash = dict_hash(name, name_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100515 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
aPiecekc7594b72021-06-29 09:00:03 +0200516 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
517 "Duplicate identifier \"%s\" of typedef statement - scoped type collide with a top-level type.", name);
518 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100519 }
520 } else {
aPiecekc7594b72021-06-29 09:00:03 +0200521 LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, tpdfs_global, name, "typedef",
522 "name collision with another top-level type"));
Radek Krejci3b1f9292018-11-08 10:58:35 +0100523 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
524 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
525 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100526 }
527
528 return LY_SUCCESS;
529}
530
Radek Krejci857189e2020-09-01 13:26:36 +0200531/**
532 * @brief Compare identifiers.
Michal Vasko62524a92021-02-26 10:08:50 +0100533 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200534 */
535static ly_bool
536lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Radek Krejcibbe09a92018-11-08 09:36:54 +0100537{
Michal Vasko11ac39a2021-07-23 12:46:56 +0200538 char *id1, *id2;
539
540 id1 = *(char **)val1;
541 id2 = *(char **)val2;
542
543 return strcmp(id1, id2) == 0 ? 1 : 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100544}
545
546LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200547lysp_check_dup_typedefs(struct lysp_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100548{
549 struct hash_table *ids_global;
Radek Krejci0fb28562018-12-13 15:17:37 +0100550 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200551 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200552 uint32_t i;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100553 LY_ERR ret = LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100554
555 /* check name collisions - typedefs and groupings */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100556 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200557 LY_ARRAY_FOR(mod->typedefs, v) {
aPieceke1fbd952021-06-29 08:12:55 +0200558 ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100559 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100560 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200561 LY_ARRAY_FOR(mod->includes, v) {
562 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200563 ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100564 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci3b1f9292018-11-08 10:58:35 +0100565 }
566 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200567 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
568 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
569 LY_ARRAY_FOR(typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200570 ret = lysp_check_dup_typedef(ctx, (struct lysp_node *)ctx->tpdfs_nodes.objs[i], &typedefs[u], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100571 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100572 }
573 }
Michal Vasko405cc9e2020-12-01 12:01:27 +0100574
Radek Krejcibbe09a92018-11-08 09:36:54 +0100575cleanup:
576 lyht_free(ids_global);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100577 return ret;
578}
579
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200580static const struct lysp_node_grp *
581lysp_grouping_match(const char *name, struct lysp_node *node)
582{
583 const struct lysp_node_grp *groupings, *grp_iter;
584
585 groupings = lysp_node_groupings(node);
586 LY_LIST_FOR(groupings, grp_iter) {
587 if (!strcmp(name, grp_iter->name)) {
588 /* match */
589 return grp_iter;
590 }
591 }
592
593 return NULL;
594}
595
aPiecek63e080d2021-06-29 13:53:28 +0200596/**
597 * @brief Check name of a new grouping to avoid name collisions.
598 *
599 * @param[in] ctx Parser context, module where the grouping is being defined is taken from here.
600 * @param[in] node Schema node where the grouping is being defined, NULL in case of a top-level grouping.
601 * @param[in] grp Grouping definition to check.
602 * @param[in,out] grps_global Initialized hash table to store temporary data between calls. When the module's
603 * groupings are checked, caller is supposed to free the table.
604 * @return LY_EVALID in case of collision, LY_SUCCESS otherwise.
605 */
606static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200607lysp_check_dup_grouping(struct lysp_ctx *ctx, struct lysp_node *node, const struct lysp_node_grp *grp,
aPiecek63e080d2021-06-29 13:53:28 +0200608 struct hash_table *grps_global)
609{
610 struct lysp_node *parent;
611 uint32_t hash;
612 size_t name_len;
613 const char *name;
614 const struct lysp_node_grp *groupings, *grp_iter;
615
616 assert(ctx);
617 assert(grp);
618
619 name = grp->name;
620 name_len = strlen(name);
621
622 /* check locally scoped groupings (avoid name shadowing) */
623 if (node) {
624 groupings = lysp_node_groupings(node);
625 LY_LIST_FOR(groupings, grp_iter) {
626 if (grp_iter == grp) {
627 break;
628 }
629 if (!strcmp(name, grp_iter->name)) {
630 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
631 "Duplicate identifier \"%s\" of grouping statement - name collision with sibling grouping.", name);
632 return LY_EVALID;
633 }
634 }
635 /* search grouping in parent's nodes */
636 for (parent = node->parent; parent; parent = parent->parent) {
637 if (lysp_grouping_match(name, parent)) {
638 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
639 "Duplicate identifier \"%s\" of grouping statement - name collision with another scoped grouping.", name);
640 return LY_EVALID;
641 }
642 }
643 }
644
645 /* check collision with the top-level groupings */
646 if (node) {
647 hash = dict_hash(name, name_len);
648 if (!lyht_find(grps_global, &name, hash, NULL)) {
649 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
650 "Duplicate identifier \"%s\" of grouping statement - scoped grouping collide with a top-level grouping.", name);
651 return LY_EVALID;
652 }
653 } else {
654 LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, grps_global, name, "grouping",
655 "name collision with another top-level grouping"));
656 }
657
658 return LY_SUCCESS;
659}
660
661LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200662lysp_check_dup_groupings(struct lysp_ctx *ctx, struct lysp_module *mod)
aPiecek63e080d2021-06-29 13:53:28 +0200663{
664 struct hash_table *ids_global;
665 const struct lysp_node_grp *groupings, *grp_iter;
666 LY_ARRAY_COUNT_TYPE u;
667 uint32_t i;
668 LY_ERR ret = LY_SUCCESS;
669
670 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
671 LY_LIST_FOR(mod->groupings, grp_iter) {
672 ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global);
673 LY_CHECK_GOTO(ret, cleanup);
674 }
675 LY_ARRAY_FOR(mod->includes, u) {
676 LY_LIST_FOR(mod->includes[u].submodule->groupings, grp_iter) {
677 ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global);
678 LY_CHECK_GOTO(ret, cleanup);
679 }
680 }
681 for (i = 0; i < ctx->grps_nodes.count; ++i) {
682 groupings = lysp_node_groupings((struct lysp_node *)ctx->grps_nodes.objs[i]);
683 LY_LIST_FOR(groupings, grp_iter) {
684 ret = lysp_check_dup_grouping(ctx, (struct lysp_node *)ctx->grps_nodes.objs[i], grp_iter, ids_global);
685 LY_CHECK_GOTO(ret, cleanup);
686 }
687 }
688
689cleanup:
690 lyht_free(ids_global);
691 return ret;
692}
693
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100694static ly_bool
695ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
696{
697 void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
698
699 return ptr1 == ptr2 ? 1 : 0;
700}
701
702LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200703lysp_check_dup_features(struct lysp_ctx *ctx, struct lysp_module *mod)
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100704{
705 LY_ARRAY_COUNT_TYPE u;
706 struct hash_table *ht;
707 struct lysp_feature *f;
aPiecekdc12b9f2021-06-25 10:55:47 +0200708 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100709
aPiecekf6203072021-06-25 10:58:26 +0200710 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100711 LY_CHECK_RET(!ht, LY_EMEM);
712
713 /* add all module features into a hash table */
714 LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200715 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
716 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200717 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100718 }
719
720 /* add all submodule features into a hash table */
721 LY_ARRAY_FOR(mod->includes, u) {
722 LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200723 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
724 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200725 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100726 }
727 }
728
729cleanup:
730 lyht_free(ht);
731 return ret;
732}
733
734LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200735lysp_check_dup_identities(struct lysp_ctx *ctx, struct lysp_module *mod)
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100736{
737 LY_ARRAY_COUNT_TYPE u;
738 struct hash_table *ht;
739 struct lysp_ident *i;
aPiecekdc12b9f2021-06-25 10:55:47 +0200740 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100741
aPiecekf6203072021-06-25 10:58:26 +0200742 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100743 LY_CHECK_RET(!ht, LY_EMEM);
744
745 /* add all module identities into a hash table */
746 LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200747 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
748 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200749 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100750 }
751
752 /* add all submodule identities into a hash table */
753 LY_ARRAY_FOR(mod->includes, u) {
754 LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200755 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
756 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200757 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100758 }
759 }
760
761cleanup:
762 lyht_free(ht);
763 return ret;
764}
765
Radek Krejci9ed7a192018-10-31 16:23:51 +0100766struct lysp_load_module_check_data {
767 const char *name;
768 const char *revision;
769 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200770 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100771};
772
773static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100774lysp_load_module_check(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100775{
776 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100777 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200778 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100779 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100780 struct lysp_revision *revs;
781
782 name = mod ? mod->mod->name : submod->name;
783 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200784 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100785
786 if (info->name) {
787 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100788 if (strcmp(info->name, name)) {
789 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100790 return LY_EINVAL;
791 }
792 }
793 if (info->revision) {
794 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100795 if (!revs || strcmp(info->revision, revs[0].date)) {
796 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200797 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100798 return LY_EINVAL;
799 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200800 } else if (!latest_revision) {
801 /* do not log, we just need to drop the schema and use the latest revision from the context */
802 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100803 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100804 if (submod) {
805 assert(info->submoduleof);
806
Radek Krejci9ed7a192018-10-31 16:23:51 +0100807 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200808 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100809 LOGVAL(ctx, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200810 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100811 return LY_EVALID;
812 }
813 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100814 if (submod->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100815 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100816 return LY_EVALID;
817 }
818 }
819 if (info->path) {
820 /* check that name and revision match filename */
821 filename = strrchr(info->path, '/');
822 if (!filename) {
823 filename = info->path;
824 } else {
825 filename++;
826 }
827 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100828 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100829 rev = strchr(filename, '@');
830 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100831 if (strncmp(filename, name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +0200832 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100833 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100834 }
835 /* revision */
836 if (rev) {
837 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100838 if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100839 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +0200840 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100841 }
842 }
843 }
844 return LY_SUCCESS;
845}
846
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200847/**
Michal Vasko4e205e82021-06-08 14:01:47 +0200848 * @brief Parse a (sub)module from a local file and add into the context.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200849 *
850 * This function does not check the presence of the (sub)module in context, it should be done before calling this function.
851 *
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200852 * @param[in] ctx libyang context where to work.
853 * @param[in] name Name of the (sub)module to load.
854 * @param[in] revision Optional revision of the (sub)module to load, if NULL the newest revision is being loaded.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200855 * @param[in] main_ctx Parser context of the main module in case of loading submodule.
856 * @param[in] main_name Main module name in case of loading submodule.
857 * @param[in] required Module is required so error (even if the input file not found) are important. If 0, there is some
858 * backup and it is actually ok if the input data are not found. However, parser reports errors even in this case.
Michal Vaskodd992582021-06-10 14:34:57 +0200859 * @param[in,out] new_mods Set of all the new mods added to the context. Includes this module and all of its imports.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200860 * @param[out] result Parsed YANG schema tree of the requested module (struct lys_module*) or submodule (struct lysp_submodule*).
861 * If it is a module, it is already in the context!
Michal Vasko4e205e82021-06-08 14:01:47 +0200862 * @return LY_SUCCESS on success.
Michal Vasko4e205e82021-06-08 14:01:47 +0200863 * @return LY_ERR on error.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200864 */
865static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200866lys_parse_localfile(struct ly_ctx *ctx, const char *name, const char *revision, struct lysp_ctx *main_ctx,
Michal Vaskodd992582021-06-10 14:34:57 +0200867 const char *main_name, ly_bool required, struct ly_set *new_mods, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100868{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200869 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100870 char *filepath = NULL;
871 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100872 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100873 LY_ERR ret = LY_SUCCESS;
874 struct lysp_load_module_check_data check_data = {0};
875
Michal Vasko87f1cf02021-06-08 14:02:47 +0200876 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name,
877 revision, &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200878 if (!filepath) {
879 if (required) {
880 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200881 revision ? revision : "");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200882 }
883 return LY_ENOTFOUND;
884 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100885
886 LOGVRB("Loading schema from \"%s\" file.", filepath);
887
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200888 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200889 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +0200890 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100891 check_data.name = name;
892 check_data.revision = revision;
893 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800894 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200895 if (main_ctx) {
aPiecekc3e26142021-06-22 14:25:49 +0200896 ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data, new_mods,
Michal Vasko69730152020-10-09 16:30:07 +0200897 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200898 } else {
Michal Vaskodd992582021-06-10 14:34:57 +0200899 ret = lys_parse_in(ctx, in, format, lysp_load_module_check, &check_data, new_mods, (struct lys_module **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200900
901 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200902 ly_in_free(in, 1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200903 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100904
905 *result = mod;
906
907 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200908
Radek Krejci9ed7a192018-10-31 16:23:51 +0100909cleanup:
910 free(filepath);
911 return ret;
912}
913
aPiecek4725aea2021-07-28 10:18:33 +0200914/**
915 * @brief Load module from searchdirs or from callback.
916 *
917 * @param[in] ctx libyang context where to work.
918 * @param[in] name Name of module to load.
919 * @param[in] revision Revision of module to load.
Michal Vaskobdac23f2022-01-12 14:02:35 +0100920 * @param[in] mod_latest Module with the latest revision found in context, otherwise set to NULL.
921 * @param[in,out] new_mods Set of all the new mods added to the context. Includes this module and all of its imports.
aPiecek4725aea2021-07-28 10:18:33 +0200922 * @param[out] mod Loaded module.
923 * @return LY_SUCCESS on success.
924 * @return LY_ERR on error.
925 */
926static LY_ERR
927lys_parse_load_from_clb_or_file(struct ly_ctx *ctx, const char *name, const char *revision,
928 struct lys_module *mod_latest, struct ly_set *new_mods, struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200929{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100930 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200931 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200932
Radek Krejci9ed7a192018-10-31 16:23:51 +0100933 void (*module_data_free)(void *module_data, void *user_data) = NULL;
934 struct lysp_load_module_check_data check_data = {0};
Michal Vasko63f3d842020-07-08 10:10:14 +0200935 struct ly_in *in;
Radek Krejci086c7132018-10-26 15:29:04 +0200936
Michal Vaskobdac23f2022-01-12 14:02:35 +0100937 *mod = NULL;
938
939 if (mod_latest && (!ctx->imp_clb || (mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB)) &&
940 ((ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) || (mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) {
941 /* we are not able to find a newer revision */
942 return LY_SUCCESS;
943 }
944
aPiecek4725aea2021-07-28 10:18:33 +0200945 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
946search_clb:
Michal Vaskobdac23f2022-01-12 14:02:35 +0100947 /* check there is a callback and should be called */
948 if (ctx->imp_clb && (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB))) {
949 if (!ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, &format, &module_data, &module_data_free)) {
950 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
951 check_data.name = name;
952 check_data.revision = revision;
953 lys_parse_in(ctx, in, format, lysp_load_module_check, &check_data, new_mods, mod);
954 ly_in_free(in, 0);
955 if (module_data_free) {
956 module_data_free((void *)module_data, ctx->imp_clb_data);
957 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200958 }
Radek Krejci0af46292019-01-11 16:02:31 +0100959 }
Michal Vaskobdac23f2022-01-12 14:02:35 +0100960 if (*mod && !revision) {
961 /* we got the latest revision module from the callback */
962 (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB;
963 } else if (!*mod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
aPiecek4725aea2021-07-28 10:18:33 +0200964 goto search_file;
965 }
966 } else {
967search_file:
Michal Vaskobdac23f2022-01-12 14:02:35 +0100968 /* check we can use searchdirs and that we should */
969 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) &&
970 (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) {
aPiecek4725aea2021-07-28 10:18:33 +0200971 lys_parse_localfile(ctx, name, revision, NULL, NULL, mod_latest ? 0 : 1, new_mods, (void **)mod);
972 }
Michal Vaskobdac23f2022-01-12 14:02:35 +0100973 if (*mod && !revision) {
974 /* we got the latest revision module in the searchdirs */
975 (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB;
976 } else if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
aPiecek4725aea2021-07-28 10:18:33 +0200977 goto search_clb;
978 }
Radek Krejci086c7132018-10-26 15:29:04 +0200979 }
980
aPiecek4725aea2021-07-28 10:18:33 +0200981 return LY_SUCCESS;
982}
983
984/**
aPiecekd4911ee2021-07-30 07:40:24 +0200985 * @brief Get module without revision according to priorities.
986 *
987 * 1. Search for the module with LYS_MOD_IMPORTED_REV.
988 * 2. Search for the implemented module.
989 * 3. Search for the latest module in the context.
990 *
991 * @param[in] ctx libyang context where module is searched.
992 * @param[in] name Name of the searched module.
aPiecekd4911ee2021-07-30 07:40:24 +0200993 * @return Found module from context or NULL.
994 */
995static struct lys_module *
Michal Vaskobdac23f2022-01-12 14:02:35 +0100996lys_get_module_without_revision(struct ly_ctx *ctx, const char *name)
aPiecekd4911ee2021-07-30 07:40:24 +0200997{
998 struct lys_module *mod, *mod_impl;
999 uint32_t index;
1000
aPiecekd4911ee2021-07-30 07:40:24 +02001001 /* Try to find module with LYS_MOD_IMPORTED_REV flag. */
1002 index = 0;
1003 while ((mod = ly_ctx_get_module_iter(ctx, &index))) {
1004 if (!strcmp(mod->name, name) && (mod->latest_revision & LYS_MOD_IMPORTED_REV)) {
1005 break;
1006 }
1007 }
1008
1009 /* Try to find the implemented module. */
1010 mod_impl = ly_ctx_get_module_implemented(ctx, name);
1011 if (mod && mod_impl) {
Michal Vaskobdac23f2022-01-12 14:02:35 +01001012 LOGVRB("Implemented module \"%s@%s\" is not used for import, revision \"%s\" is imported instead.",
aPiecekd4911ee2021-07-30 07:40:24 +02001013 mod_impl->name, mod_impl->revision, mod->revision);
1014 return mod;
1015 } else if (mod_impl) {
1016 return mod_impl;
1017 }
1018
1019 /* Try to find the latest module in the current context. */
1020 mod = ly_ctx_get_module_latest(ctx, name);
aPiecekd4911ee2021-07-30 07:40:24 +02001021
1022 return mod;
1023}
1024
1025/**
aPiecek4725aea2021-07-28 10:18:33 +02001026 * @brief Check if a circular dependency exists between modules.
1027 *
1028 * @param[in] ctx libyang context for log an error.
1029 * @param[in,out] mod Examined module which is set to NULL
1030 * if the circular dependency is detected.
1031 * @return LY_SUCCESS if no circular dependecy is detected,
1032 * otherwise LY_EVALID.
1033 */
1034static LY_ERR
1035lys_check_circular_dependency(struct ly_ctx *ctx, struct lys_module **mod)
1036{
1037 if ((*mod) && (*mod)->parsed->parsing) {
1038 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", (*mod)->name);
Michal Vasko4e205e82021-06-08 14:01:47 +02001039 *mod = NULL;
1040 return LY_EVALID;
Michal Vasko0550b762020-11-24 18:04:08 +01001041 }
Radek Krejci086c7132018-10-26 15:29:04 +02001042
aPiecek4725aea2021-07-28 10:18:33 +02001043 return LY_SUCCESS;
1044}
1045
1046LY_ERR
1047lys_parse_load(struct ly_ctx *ctx, const char *name, const char *revision, struct ly_set *new_mods,
1048 struct lys_module **mod)
1049{
1050 struct lys_module *mod_latest = NULL;
1051
1052 assert(mod && new_mods);
1053
Michal Vasko0550b762020-11-24 18:04:08 +01001054 /*
aPiecek4725aea2021-07-28 10:18:33 +02001055 * Try to get the module from the context.
Michal Vasko0550b762020-11-24 18:04:08 +01001056 */
aPiecek4725aea2021-07-28 10:18:33 +02001057 if (revision) {
1058 /* Get the specific revision. */
1059 *mod = ly_ctx_get_module(ctx, name, revision);
1060 } else {
Michal Vaskobdac23f2022-01-12 14:02:35 +01001061 /* Get the requested module in a suitable revision in the context. */
1062 *mod = lys_get_module_without_revision(ctx, name);
1063 if (*mod && !(*mod)->implemented && !((*mod)->latest_revision & LYS_MOD_IMPORTED_REV)) {
aPiecek4725aea2021-07-28 10:18:33 +02001064 /* Let us now search with callback and searchpaths to check
1065 * if there is newer revision outside the context.
1066 */
1067 mod_latest = *mod;
1068 *mod = NULL;
1069 }
1070 }
1071
Michal Vasko0550b762020-11-24 18:04:08 +01001072 if (!*mod) {
aPiecek4725aea2021-07-28 10:18:33 +02001073 /* No suitable module in the context, try to load it. */
Michal Vaskobdac23f2022-01-12 14:02:35 +01001074 LY_CHECK_RET(lys_parse_load_from_clb_or_file(ctx, name, revision, mod_latest, new_mods, mod));
aPiecek4725aea2021-07-28 10:18:33 +02001075 if (!*mod && !mod_latest) {
Michal Vasko4e205e82021-06-08 14:01:47 +02001076 LOGVAL(ctx, LYVE_REFERENCE, "Loading \"%s\" module failed.", name);
Michal Vasko0550b762020-11-24 18:04:08 +01001077 return LY_EVALID;
1078 }
aPiecek4725aea2021-07-28 10:18:33 +02001079
1080 /* Update the latest_revision flag - here we have selected the latest available schema,
1081 * consider that even the callback provides correct latest revision.
1082 */
1083 if (!*mod) {
1084 LOGVRB("Newer revision than \"%s@%s\" not found, using this as the latest revision.",
1085 mod_latest->name, mod_latest->revision);
1086 assert(mod_latest->latest_revision & LYS_MOD_LATEST_REV);
1087 mod_latest->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS;
1088 *mod = mod_latest;
1089 } else if (*mod && !revision && ((*mod)->latest_revision & LYS_MOD_LATEST_REV)) {
1090 (*mod)->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS;
1091 }
Radek Krejci086c7132018-10-26 15:29:04 +02001092 }
Radek Krejci086c7132018-10-26 15:29:04 +02001093
aPiecek9f8c7e72021-07-28 12:01:56 +02001094 /* Checking the circular dependence of imported modules. */
1095 LY_CHECK_RET(lys_check_circular_dependency(ctx, mod));
1096
Radek Krejci086c7132018-10-26 15:29:04 +02001097 return LY_SUCCESS;
1098}
1099
1100LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001101lysp_check_stringchar(struct lysp_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +02001102{
1103 if (!is_yangutf8char(c)) {
1104 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
1105 return LY_EVALID;
1106 }
1107 return LY_SUCCESS;
1108}
1109
1110LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001111lysp_check_identifierchar(struct lysp_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +02001112{
Michal Vasko69730152020-10-09 16:30:07 +02001113 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +02001114 if (!is_yangidentstartchar(c)) {
aPiecekc89b2242021-05-14 14:19:11 +02001115 if ((c < UCHAR_MAX) && isprint(c)) {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001116 if (ctx) {
1117 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
1118 }
Michal Vasko7c769042021-03-25 12:20:49 +01001119 } else {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001120 if (ctx) {
1121 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character 0x%04x.", c);
1122 }
Michal Vasko7c769042021-03-25 12:20:49 +01001123 }
David Sedlák4a650532019-07-10 11:55:18 +02001124 return LY_EVALID;
1125 }
1126 if (prefix) {
1127 if (first) {
1128 (*prefix) = 0;
1129 } else {
1130 (*prefix) = 2;
1131 }
1132 }
Michal Vasko69730152020-10-09 16:30:07 +02001133 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +02001134 (*prefix) = 1;
1135 } else if (!is_yangidentchar(c)) {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001136 if (ctx) {
1137 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
1138 }
David Sedlák4a650532019-07-10 11:55:18 +02001139 return LY_EVALID;
1140 }
1141
1142 return LY_SUCCESS;
1143}
1144
Radek Krejci771928a2021-01-19 13:42:36 +01001145/**
1146 * @brief Try to find the parsed submodule in main module for the given include record.
1147 *
1148 * @param[in] pctx main parser context
1149 * @param[in] inc The include record with missing parsed submodule. According to include info try to find
1150 * the corresponding parsed submodule in main module's includes.
1151 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
1152 * @return LY_ENOT - the parsed module was not found.
1153 * @return LY_EVALID - YANG rule violation
1154 */
1155static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001156lysp_main_pmod_get_submodule(struct lysp_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +02001157{
Radek Krejci771928a2021-01-19 13:42:36 +01001158 LY_ARRAY_COUNT_TYPE i;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001159 struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed;
Michal Vasko69730152020-10-09 16:30:07 +02001160
Radek Krejci771928a2021-01-19 13:42:36 +01001161 LY_ARRAY_FOR(main_pmod->includes, i) {
1162 if (strcmp(main_pmod->includes[i].name, inc->name)) {
1163 continue;
1164 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001165
Radek Krejci771928a2021-01-19 13:42:36 +01001166 if (inc->rev[0] && strncmp(inc->rev, main_pmod->includes[i].rev, LY_REV_SIZE)) {
1167 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1168 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001169 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev,
Radek Krejci771928a2021-01-19 13:42:36 +01001170 main_pmod->includes[i].name, main_pmod->includes[i].rev, main_pmod->mod->name);
1171 return LY_EVALID;
1172 }
1173
1174 inc->submodule = main_pmod->includes[i].submodule;
1175 return inc->submodule ? LY_SUCCESS : LY_ENOT;
1176 }
1177
1178 if (main_pmod->version == LYS_VERSION_1_1) {
1179 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1180 "YANG 1.1 requires all submodules to be included from main module. "
1181 "But submodule \"%s\" includes submodule \"%s\" which is not included by main module \"%s\".",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001182 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->name, main_pmod->mod->name);
Radek Krejci771928a2021-01-19 13:42:36 +01001183 return LY_EVALID;
1184 } else {
1185 return LY_ENOT;
1186 }
1187}
1188
1189/**
Michal Vasko8a67eff2021-12-07 14:04:47 +01001190 * @brief Try to find the parsed submodule in currenlty parsed modules for the given include record.
1191 *
1192 * @param[in] pctx main parser context
1193 * @param[in] inc The include record with missing parsed submodule.
1194 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
1195 * @return LY_ENOT - the parsed module was not found.
1196 * @return LY_EVALID - YANG rule violation
1197 */
1198static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001199lysp_parsed_mods_get_submodule(struct lysp_ctx *pctx, struct lysp_include *inc)
Michal Vasko8a67eff2021-12-07 14:04:47 +01001200{
1201 uint32_t i;
1202 struct lysp_submodule *submod;
1203
1204 for (i = 0; i < pctx->parsed_mods->count - 1; ++i) {
1205 submod = pctx->parsed_mods->objs[i];
1206 if (!submod->is_submod) {
1207 continue;
1208 }
1209
1210 if (strcmp(submod->name, inc->name)) {
1211 continue;
1212 }
1213
1214 if (inc->rev[0] && submod->revs && strncmp(inc->rev, submod->revs[0].date, LY_REV_SIZE)) {
1215 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1216 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
1217 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev,
1218 submod->name, submod->revs[0].date, PARSER_CUR_PMOD(pctx)->mod->name);
1219 return LY_EVALID;
1220 }
1221
1222 inc->submodule = submod;
1223 return LY_SUCCESS;
1224 }
1225
1226 return LY_ENOT;
1227}
1228
1229/**
Radek Krejci771928a2021-01-19 13:42:36 +01001230 * @brief Make the copy of the given include record into the main module.
1231 *
1232 * YANG 1.0 does not require the main module to include all the submodules. Therefore, parsing submodules can cause
1233 * reallocating and extending the includes array in the main module by the submodules included only in submodules.
1234 *
1235 * @param[in] pctx main parser context
1236 * @param[in] inc Include record to copy into main module taken from @p pctx.
1237 * @return LY_ERR value.
1238 */
1239static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001240lysp_inject_submodule(struct lysp_ctx *pctx, struct lysp_include *inc)
Radek Krejci771928a2021-01-19 13:42:36 +01001241{
1242 LY_ARRAY_COUNT_TYPE i;
1243 struct lysp_include *inc_new, *inc_tofill = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001244 struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed;
Radek Krejci771928a2021-01-19 13:42:36 +01001245
1246 /* first, try to find the corresponding record with missing parsed submodule */
1247 LY_ARRAY_FOR(main_pmod->includes, i) {
1248 if (strcmp(main_pmod->includes[i].name, inc->name)) {
1249 continue;
1250 }
1251 inc_tofill = &main_pmod->includes[i];
1252 break;
1253 }
1254
1255 if (inc_tofill) {
1256 inc_tofill->submodule = inc->submodule;
1257 } else {
1258 LY_ARRAY_NEW_RET(PARSER_CTX(pctx), main_pmod->includes, inc_new, LY_EMEM);
1259
1260 inc_new->submodule = inc->submodule;
1261 DUP_STRING_RET(PARSER_CTX(pctx), inc->name, inc_new->name);
1262 DUP_STRING_RET(PARSER_CTX(pctx), inc->dsc, inc_new->dsc);
1263 DUP_STRING_RET(PARSER_CTX(pctx), inc->ref, inc_new->ref);
1264 /* TODO duplicate extensions */
1265 memcpy(inc_new->rev, inc->rev, LY_REV_SIZE);
1266 inc_new->injected = 1;
1267 }
1268 return LY_SUCCESS;
1269}
1270
1271LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001272lysp_load_submodules(struct lysp_ctx *pctx, struct lysp_module *pmod, struct ly_set *new_mods)
Radek Krejci771928a2021-01-19 13:42:36 +01001273{
1274 LY_ARRAY_COUNT_TYPE u;
1275 struct ly_ctx *ctx = PARSER_CTX(pctx);
1276
1277 LY_ARRAY_FOR(pmod->includes, u) {
Michal Vasko8a67eff2021-12-07 14:04:47 +01001278 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci771928a2021-01-19 13:42:36 +01001279 struct lysp_submodule *submod = NULL;
1280 struct lysp_include *inc = &pmod->includes[u];
1281
1282 if (inc->submodule) {
1283 continue;
1284 }
1285
1286 if (pmod->is_submod) {
1287 /* try to find the submodule in the main module or its submodules */
Michal Vasko8a67eff2021-12-07 14:04:47 +01001288 ret = lysp_main_pmod_get_submodule(pctx, inc);
1289 LY_CHECK_RET(ret != LY_ENOT, ret);
Radek Krejci771928a2021-01-19 13:42:36 +01001290 }
1291
Michal Vasko8a67eff2021-12-07 14:04:47 +01001292 /* try to use currently parsed submodule */
1293 r = lysp_parsed_mods_get_submodule(pctx, inc);
1294 LY_CHECK_RET(r != LY_ENOT, r);
1295
Radek Krejci771928a2021-01-19 13:42:36 +01001296 /* submodule not present in the main module, get the input data and parse it */
1297 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcidf549132021-01-21 10:32:32 +01001298search_clb:
Radek Krejci771928a2021-01-19 13:42:36 +01001299 if (ctx->imp_clb) {
1300 const char *submodule_data = NULL;
1301 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko26bbb272022-08-02 14:54:33 +02001302
Radek Krejci771928a2021-01-19 13:42:36 +01001303 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
1304 struct lysp_load_module_check_data check_data = {0};
1305 struct ly_in *in;
1306
Michal Vasko8a67eff2021-12-07 14:04:47 +01001307 if (ctx->imp_clb(PARSER_CUR_PMOD(pctx)->mod->name, NULL, inc->name,
Radek Krejci771928a2021-01-19 13:42:36 +01001308 inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
1309 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
1310 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
1311 check_data.name = inc->name;
1312 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001313 check_data.submoduleof = PARSER_CUR_PMOD(pctx)->mod->name;
aPiecekc3e26142021-06-22 14:25:49 +02001314 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, new_mods, &submod);
Radek Krejci771928a2021-01-19 13:42:36 +01001315
1316 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1317 * submodule's include into main module, where it is missing */
1318 inc = &pmod->includes[u];
1319
1320 ly_in_free(in, 0);
1321 if (submodule_data_free) {
1322 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
1323 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001324 }
1325 }
Radek Krejci771928a2021-01-19 13:42:36 +01001326 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1327 goto search_file;
1328 }
1329 } else {
Radek Krejcidf549132021-01-21 10:32:32 +01001330search_file:
Radek Krejci771928a2021-01-19 13:42:36 +01001331 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
1332 /* submodule was not received from the callback or there is no callback set */
Michal Vaskodd6d5a32022-07-14 13:54:58 +02001333 lys_parse_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, pctx->main_ctx,
1334 PARSER_CUR_PMOD(pctx->main_ctx)->mod->name, 1, new_mods, (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001335
Radek Krejci771928a2021-01-19 13:42:36 +01001336 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1337 * submodule's include into main module, where it is missing */
1338 inc = &pmod->includes[u];
1339 }
1340 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1341 goto search_clb;
1342 }
1343 }
1344 if (submod) {
1345 if (!inc->rev[0] && (submod->latest_revision == 1)) {
1346 /* update the latest_revision flag - here we have selected the latest available schema,
1347 * consider that even the callback provides correct latest revision */
1348 submod->latest_revision = 2;
1349 }
1350
1351 inc->submodule = submod;
1352 if (ret == LY_ENOT) {
1353 /* the submodule include is not present in YANG 1.0 main module - add it there */
1354 LY_CHECK_RET(lysp_inject_submodule(pctx, &pmod->includes[u]));
1355 }
1356 }
1357 if (!inc->submodule) {
1358 LOGVAL(ctx, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name,
Michal Vasko8a67eff2021-12-07 14:04:47 +01001359 PARSER_CUR_PMOD(pctx)->is_submod ? ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name :
1360 PARSER_CUR_PMOD(pctx)->mod->name);
Radek Krejci771928a2021-01-19 13:42:36 +01001361 return LY_EVALID;
1362 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001363 }
1364
1365 return LY_SUCCESS;
1366}
1367
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001368LIBYANG_API_DEF const struct lysc_when *
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001369lysc_has_when(const struct lysc_node *node)
1370{
Radek Krejci9a3823e2021-01-27 20:26:46 +01001371 struct lysc_when **when;
1372
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001373 if (!node) {
1374 return NULL;
1375 }
1376
1377 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001378 when = lysc_node_when(node);
1379 if (when) {
1380 return when[0];
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001381 }
1382 node = node->parent;
1383 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1384
1385 return NULL;
1386}
1387
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001388LIBYANG_API_DEF const struct lys_module *
Michal Vaskoef53c812021-10-13 10:21:03 +02001389lysc_owner_module(const struct lysc_node *node)
1390{
1391 if (!node) {
1392 return NULL;
1393 }
1394
1395 for ( ; node->parent; node = node->parent) {}
1396 return node->module;
1397}
1398
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001399LIBYANG_API_DEF const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001400lys_nodetype2str(uint16_t nodetype)
1401{
Michal Vaskod989ba02020-08-24 10:59:24 +02001402 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001403 case LYS_CONTAINER:
1404 return "container";
1405 case LYS_CHOICE:
1406 return "choice";
1407 case LYS_LEAF:
1408 return "leaf";
1409 case LYS_LEAFLIST:
1410 return "leaf-list";
1411 case LYS_LIST:
1412 return "list";
1413 case LYS_ANYXML:
1414 return "anyxml";
1415 case LYS_ANYDATA:
1416 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001417 case LYS_CASE:
1418 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001419 case LYS_RPC:
1420 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001421 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001422 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001423 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001424 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001425 case LYS_USES:
1426 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001427 default:
1428 return "unknown";
1429 }
1430}
1431
Radek Krejci693262f2019-04-29 15:23:20 +02001432const char *
1433lys_datatype2str(LY_DATA_TYPE basetype)
1434{
Michal Vaskod989ba02020-08-24 10:59:24 +02001435 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001436 case LY_TYPE_BINARY:
1437 return "binary";
1438 case LY_TYPE_UINT8:
1439 return "uint8";
1440 case LY_TYPE_UINT16:
1441 return "uint16";
1442 case LY_TYPE_UINT32:
1443 return "uint32";
1444 case LY_TYPE_UINT64:
1445 return "uint64";
1446 case LY_TYPE_STRING:
1447 return "string";
1448 case LY_TYPE_BITS:
1449 return "bits";
1450 case LY_TYPE_BOOL:
1451 return "boolean";
1452 case LY_TYPE_DEC64:
1453 return "decimal64";
1454 case LY_TYPE_EMPTY:
1455 return "empty";
1456 case LY_TYPE_ENUM:
1457 return "enumeration";
1458 case LY_TYPE_IDENT:
1459 return "identityref";
1460 case LY_TYPE_INST:
1461 return "instance-identifier";
1462 case LY_TYPE_LEAFREF:
1463 return "leafref";
1464 case LY_TYPE_UNION:
1465 return "union";
1466 case LY_TYPE_INT8:
1467 return "int8";
1468 case LY_TYPE_INT16:
1469 return "int16";
1470 case LY_TYPE_INT32:
1471 return "int32";
1472 case LY_TYPE_INT64:
1473 return "int64";
1474 default:
1475 return "unknown";
1476 }
1477}
1478
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001479LIBYANG_API_DEF const struct lysp_tpdf *
Radek Krejci056d0a82018-12-06 16:57:25 +01001480lysp_node_typedefs(const struct lysp_node *node)
1481{
Radek Krejci0fb28562018-12-13 15:17:37 +01001482 switch (node->nodetype) {
1483 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001484 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001485 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001486 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001487 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001488 return ((struct lysp_node_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001489 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001490 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001491 return ((struct lysp_node_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001492 case LYS_INPUT:
1493 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001494 return ((struct lysp_node_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001495 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001496 return ((struct lysp_node_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001497 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001498 return NULL;
1499 }
1500}
1501
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001502LIBYANG_API_DEF const struct lysp_node_grp *
Radek Krejci53ea6152018-12-13 15:21:15 +01001503lysp_node_groupings(const struct lysp_node *node)
1504{
1505 switch (node->nodetype) {
1506 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001507 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001508 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001509 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001510 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001511 return ((struct lysp_node_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001512 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001513 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001514 return ((struct lysp_node_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001515 case LYS_INPUT:
1516 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001517 return ((struct lysp_node_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001518 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001519 return ((struct lysp_node_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001520 default:
1521 return NULL;
1522 }
1523}
1524
Radek Krejci2a9fc652021-01-22 17:44:34 +01001525struct lysp_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001526lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001527{
1528 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001529
Radek Krejcibbe09a92018-11-08 09:36:54 +01001530 switch (node->nodetype) {
1531 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001532 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001533 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001534 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001535 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001536 return &((struct lysp_node_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001537 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001538 return &((struct lysp_node_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001539 default:
1540 return NULL;
1541 }
1542}
1543
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001544LIBYANG_API_DEF const struct lysp_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001545lysp_node_actions(const struct lysp_node *node)
1546{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001547 struct lysp_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001548
Michal Vasko22df3f02020-08-24 13:29:22 +02001549 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001550 if (actions) {
1551 return *actions;
1552 } else {
1553 return NULL;
1554 }
1555}
1556
Radek Krejci2a9fc652021-01-22 17:44:34 +01001557struct lysp_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001558lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001559{
1560 assert(node);
1561 switch (node->nodetype) {
1562 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001563 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001564 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001565 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001566 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001567 return &((struct lysp_node_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001568 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001569 return &((struct lysp_node_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001570 default:
1571 return NULL;
1572 }
1573}
1574
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001575LIBYANG_API_DEF const struct lysp_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001576lysp_node_notifs(const struct lysp_node *node)
1577{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001578 struct lysp_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001579
Michal Vasko22df3f02020-08-24 13:29:22 +02001580 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001581 if (notifs) {
1582 return *notifs;
1583 } else {
1584 return NULL;
1585 }
1586}
1587
Radek Krejcibbe09a92018-11-08 09:36:54 +01001588struct lysp_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001589lysp_node_child_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001590{
1591 assert(node);
1592 switch (node->nodetype) {
1593 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001594 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001595 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001596 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001597 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001598 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001599 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001600 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001601 case LYS_GROUPING:
Radek Krejci01180ac2021-01-27 08:48:22 +01001602 return &((struct lysp_node_grp *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001603 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001604 return &((struct lysp_node_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001605 case LYS_INPUT:
1606 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001607 return &((struct lysp_node_action_inout *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001608 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001609 return &((struct lysp_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001610 default:
1611 return NULL;
1612 }
1613}
1614
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001615LIBYANG_API_DEF const struct lysp_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001616lysp_node_child(const struct lysp_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01001617{
Michal Vasko544e58a2021-01-28 14:33:41 +01001618 struct lysp_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001619
1620 if (!node) {
1621 return NULL;
1622 }
1623
Michal Vasko544e58a2021-01-28 14:33:41 +01001624 child = lysp_node_child_p((struct lysp_node *)node);
1625 if (child) {
1626 return *child;
Radek Krejci056d0a82018-12-06 16:57:25 +01001627 } else {
1628 return NULL;
1629 }
1630}
1631
Radek Krejci9a3823e2021-01-27 20:26:46 +01001632struct lysp_restr **
1633lysp_node_musts_p(const struct lysp_node *node)
1634{
1635 if (!node) {
1636 return NULL;
1637 }
1638
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001639 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001640 case LYS_CONTAINER:
1641 return &((struct lysp_node_container *)node)->musts;
1642 case LYS_LEAF:
1643 return &((struct lysp_node_leaf *)node)->musts;
1644 case LYS_LEAFLIST:
1645 return &((struct lysp_node_leaflist *)node)->musts;
1646 case LYS_LIST:
1647 return &((struct lysp_node_list *)node)->musts;
1648 case LYS_ANYXML:
1649 case LYS_ANYDATA:
1650 return &((struct lysp_node_anydata *)node)->musts;
1651 case LYS_NOTIF:
1652 return &((struct lysp_node_notif *)node)->musts;
1653 case LYS_INPUT:
1654 case LYS_OUTPUT:
1655 return &((struct lysp_node_action_inout *)node)->musts;
1656 default:
1657 return NULL;
1658 }
1659}
1660
1661struct lysp_restr *
1662lysp_node_musts(const struct lysp_node *node)
1663{
1664 struct lysp_restr **musts;
1665
1666 musts = lysp_node_musts_p(node);
1667 if (musts) {
1668 return *musts;
1669 } else {
1670 return NULL;
1671 }
1672}
1673
1674struct lysp_when **
1675lysp_node_when_p(const struct lysp_node *node)
1676{
1677 if (!node) {
1678 return NULL;
1679 }
1680
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001681 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001682 case LYS_CONTAINER:
1683 return &((struct lysp_node_container *)node)->when;
1684 case LYS_CHOICE:
1685 return &((struct lysp_node_choice *)node)->when;
1686 case LYS_LEAF:
1687 return &((struct lysp_node_leaf *)node)->when;
1688 case LYS_LEAFLIST:
1689 return &((struct lysp_node_leaflist *)node)->when;
1690 case LYS_LIST:
1691 return &((struct lysp_node_list *)node)->when;
1692 case LYS_ANYXML:
1693 case LYS_ANYDATA:
1694 return &((struct lysp_node_anydata *)node)->when;
1695 case LYS_CASE:
1696 return &((struct lysp_node_case *)node)->when;
1697 case LYS_USES:
1698 return &((struct lysp_node_uses *)node)->when;
1699 case LYS_AUGMENT:
1700 return &((struct lysp_node_augment *)node)->when;
1701 default:
1702 return NULL;
1703 }
1704}
1705
1706struct lysp_when *
1707lysp_node_when(const struct lysp_node *node)
1708{
1709 struct lysp_when **when;
1710
1711 when = lysp_node_when_p(node);
1712 if (when) {
1713 return *when;
1714 } else {
1715 return NULL;
1716 }
1717}
1718
Radek Krejci2a9fc652021-01-22 17:44:34 +01001719struct lysc_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001720lysc_node_actions_p(struct lysc_node *node)
1721{
1722 assert(node);
1723 switch (node->nodetype) {
1724 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001725 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001726 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001727 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001728 default:
1729 return NULL;
1730 }
1731}
1732
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001733LIBYANG_API_DEF const struct lysc_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001734lysc_node_actions(const struct lysc_node *node)
1735{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001736 struct lysc_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001737
Michal Vasko22df3f02020-08-24 13:29:22 +02001738 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001739 if (actions) {
1740 return *actions;
1741 } else {
1742 return NULL;
1743 }
1744}
1745
Radek Krejci2a9fc652021-01-22 17:44:34 +01001746struct lysc_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001747lysc_node_notifs_p(struct lysc_node *node)
1748{
1749 assert(node);
1750 switch (node->nodetype) {
1751 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001752 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001753 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001754 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001755 default:
1756 return NULL;
1757 }
1758}
1759
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001760LIBYANG_API_DEF const struct lysc_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001761lysc_node_notifs(const struct lysc_node *node)
1762{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001763 struct lysc_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001764
Michal Vasko22df3f02020-08-24 13:29:22 +02001765 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001766 if (notifs) {
1767 return *notifs;
1768 } else {
1769 return NULL;
1770 }
1771}
1772
Radek Krejcibbe09a92018-11-08 09:36:54 +01001773struct lysc_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001774lysc_node_child_p(const struct lysc_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001775{
Michal Vasko544e58a2021-01-28 14:33:41 +01001776 assert(node && !(node->nodetype & (LYS_RPC | LYS_ACTION)));
1777
Radek Krejcibbe09a92018-11-08 09:36:54 +01001778 switch (node->nodetype) {
1779 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001780 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001781 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001782 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001783 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001784 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001785 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001786 return &((struct lysc_node_list *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001787 case LYS_INPUT:
1788 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001789 return &((struct lysc_node_action_inout *)node)->child;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001790 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001791 return &((struct lysc_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001792 default:
1793 return NULL;
1794 }
1795}
1796
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001797LIBYANG_API_DEF const struct lysc_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001798lysc_node_child(const struct lysc_node *node)
Radek Krejcia3045382018-11-22 14:30:31 +01001799{
Michal Vasko544e58a2021-01-28 14:33:41 +01001800 struct lysc_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001801
1802 if (!node) {
1803 return NULL;
1804 }
1805
Michal Vasko544e58a2021-01-28 14:33:41 +01001806 if (node->nodetype & (LYS_RPC | LYS_ACTION)) {
1807 return &((struct lysc_node_action *)node)->input.node;
Radek Krejcibe154442021-01-21 11:06:36 +01001808 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01001809 child = lysc_node_child_p(node);
1810 if (child) {
1811 return *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001812 }
Michal Vasko2a668712020-10-21 11:48:09 +02001813 }
Michal Vasko544e58a2021-01-28 14:33:41 +01001814
1815 return NULL;
Michal Vasko2a668712020-10-21 11:48:09 +02001816}
1817
Radek Krejci9a3823e2021-01-27 20:26:46 +01001818struct lysc_must **
1819lysc_node_musts_p(const struct lysc_node *node)
1820{
1821 if (!node) {
1822 return NULL;
1823 }
1824
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001825 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001826 case LYS_CONTAINER:
1827 return &((struct lysc_node_container *)node)->musts;
1828 case LYS_LEAF:
1829 return &((struct lysc_node_leaf *)node)->musts;
1830 case LYS_LEAFLIST:
1831 return &((struct lysc_node_leaflist *)node)->musts;
1832 case LYS_LIST:
1833 return &((struct lysc_node_list *)node)->musts;
1834 case LYS_ANYXML:
1835 case LYS_ANYDATA:
1836 return &((struct lysc_node_anydata *)node)->musts;
1837 case LYS_NOTIF:
1838 return &((struct lysc_node_notif *)node)->musts;
1839 case LYS_INPUT:
1840 case LYS_OUTPUT:
1841 return &((struct lysc_node_action_inout *)node)->musts;
1842 default:
1843 return NULL;
1844 }
1845}
1846
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001847LIBYANG_API_DEF struct lysc_must *
Radek Krejci9a3823e2021-01-27 20:26:46 +01001848lysc_node_musts(const struct lysc_node *node)
1849{
1850 struct lysc_must **must_p;
1851
1852 must_p = lysc_node_musts_p(node);
1853 if (must_p) {
1854 return *must_p;
1855 } else {
1856 return NULL;
1857 }
1858}
1859
1860struct lysc_when ***
1861lysc_node_when_p(const struct lysc_node *node)
1862{
1863 if (!node) {
1864 return NULL;
1865 }
1866
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001867 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001868 case LYS_CONTAINER:
1869 return &((struct lysc_node_container *)node)->when;
1870 case LYS_CHOICE:
1871 return &((struct lysc_node_choice *)node)->when;
1872 case LYS_LEAF:
1873 return &((struct lysc_node_leaf *)node)->when;
1874 case LYS_LEAFLIST:
1875 return &((struct lysc_node_leaflist *)node)->when;
1876 case LYS_LIST:
1877 return &((struct lysc_node_list *)node)->when;
1878 case LYS_ANYXML:
1879 case LYS_ANYDATA:
1880 return &((struct lysc_node_anydata *)node)->when;
1881 case LYS_CASE:
1882 return &((struct lysc_node_case *)node)->when;
1883 case LYS_NOTIF:
1884 return &((struct lysc_node_notif *)node)->when;
1885 case LYS_RPC:
1886 case LYS_ACTION:
1887 return &((struct lysc_node_action *)node)->when;
1888 default:
1889 return NULL;
1890 }
1891}
1892
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001893LIBYANG_API_DEF struct lysc_when **
Radek Krejci9a3823e2021-01-27 20:26:46 +01001894lysc_node_when(const struct lysc_node *node)
1895{
1896 struct lysc_when ***when_p;
1897
1898 when_p = lysc_node_when_p(node);
1899 if (when_p) {
1900 return *when_p;
1901 } else {
1902 return NULL;
1903 }
1904}
1905
Radek Krejcid6b76452019-09-03 17:03:03 +02001906enum ly_stmt
Radek Krejcid54412f2020-12-17 20:25:35 +01001907lysp_match_kw(struct ly_in *in, uint64_t *indent)
David Sedlákc10e7902018-12-17 02:17:59 +01001908{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001909/**
Radek Krejcid54412f2020-12-17 20:25:35 +01001910 * @brief Move the input by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001911 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001912 *
1913 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001914 */
Radek Krejcid54412f2020-12-17 20:25:35 +01001915#define MOVE_IN(COUNT) \
1916 ly_in_skip(in, COUNT); \
1917 if (indent) { \
1918 (*indent)+=COUNT; \
1919 }
1920#define IF_KW(STR, LEN, STMT) \
1921 if (!strncmp(in->current, STR, LEN)) { \
1922 MOVE_IN(LEN); \
1923 (*kw)=STMT; \
1924 }
1925#define IF_KW_PREFIX(STR, LEN) \
1926 if (!strncmp(in->current, STR, LEN)) { \
1927 MOVE_IN(LEN);
1928#define IF_KW_PREFIX_END \
1929 }
David Sedlák572e7ab2019-06-04 16:01:58 +02001930
Michal Vasko63f3d842020-07-08 10:10:14 +02001931 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001932 enum ly_stmt result = LY_STMT_NONE;
1933 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001934 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001935 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001936 case 'a':
Radek Krejcid54412f2020-12-17 20:25:35 +01001937 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001938 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1939 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1940 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001941 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001942 IF_KW("data", 4, LY_STMT_ANYDATA)
1943 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001944 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001945 break;
1946 case 'b':
Radek Krejcid54412f2020-12-17 20:25:35 +01001947 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001948 IF_KW("ase", 3, LY_STMT_BASE)
1949 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1950 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001951 break;
1952 case 'c':
Radek Krejcid54412f2020-12-17 20:25:35 +01001953 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001954 IF_KW("ase", 3, LY_STMT_CASE)
1955 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001956 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001957 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001958 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001959 IF_KW("ct", 2, LY_STMT_CONTACT)
1960 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001961 IF_KW_PREFIX_END
1962 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001963 break;
1964 case 'd':
Radek Krejcid54412f2020-12-17 20:25:35 +01001965 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001966 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001967 IF_KW("fault", 5, LY_STMT_DEFAULT)
1968 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001969 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001970 IF_KW("e", 1, LY_STMT_DEVIATE)
1971 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001972 IF_KW_PREFIX_END
1973 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001974 break;
1975 case 'e':
Radek Krejcid54412f2020-12-17 20:25:35 +01001976 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001977 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001978 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001979 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1980 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001981 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001982 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001983 break;
1984 case 'f':
Radek Krejcid54412f2020-12-17 20:25:35 +01001985 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001986 IF_KW("eature", 6, LY_STMT_FEATURE)
1987 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001988 break;
1989 case 'g':
Radek Krejcid54412f2020-12-17 20:25:35 +01001990 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001991 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001992 break;
1993 case 'i':
Radek Krejcid54412f2020-12-17 20:25:35 +01001994 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001995 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1996 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1997 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001998 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001999 IF_KW("clude", 5, LY_STMT_INCLUDE)
2000 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002001 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02002002 break;
2003 case 'k':
Radek Krejcid54412f2020-12-17 20:25:35 +01002004 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002005 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02002006 break;
2007 case 'l':
Radek Krejcid54412f2020-12-17 20:25:35 +01002008 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02002009 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02002010 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
2011 else IF_KW("af", 2, LY_STMT_LEAF)
2012 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002013 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002014 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02002015 break;
2016 case 'm':
Radek Krejcid54412f2020-12-17 20:25:35 +01002017 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02002018 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02002019 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
2020 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002021 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002022 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
2023 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002024 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02002025 IF_KW("ule", 3, LY_STMT_MODULE)
2026 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002027 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02002028 break;
2029 case 'n':
Radek Krejcid54412f2020-12-17 20:25:35 +01002030 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002031 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
2032 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02002033 break;
2034 case 'o':
Radek Krejcid54412f2020-12-17 20:25:35 +01002035 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02002036 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02002037 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
2038 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002039 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002040 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02002041 break;
2042 case 'p':
Radek Krejcid54412f2020-12-17 20:25:35 +01002043 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002044 IF_KW("ath", 3, LY_STMT_PATH)
2045 else IF_KW("attern", 6, LY_STMT_PATTERN)
2046 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002047 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02002048 IF_KW("fix", 3, LY_STMT_PREFIX)
2049 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002050 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02002051 break;
2052 case 'r':
Radek Krejcid54412f2020-12-17 20:25:35 +01002053 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002054 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002055 else IF_KW_PREFIX("e", 1)
2056 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02002057 IF_KW("erence", 6, LY_STMT_REFERENCE)
2058 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002059 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002060 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
2061 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
2062 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002063 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002064 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02002065 break;
2066 case 's':
Radek Krejcid54412f2020-12-17 20:25:35 +01002067 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002068 IF_KW("tatus", 5, LY_STMT_STATUS)
2069 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02002070 break;
2071 case 't':
Radek Krejcid54412f2020-12-17 20:25:35 +01002072 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002073 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
2074 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02002075 break;
2076 case 'u':
Radek Krejcid54412f2020-12-17 20:25:35 +01002077 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02002078 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02002079 IF_KW("que", 3, LY_STMT_UNIQUE)
2080 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002081 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002082 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02002083 break;
2084 case 'v':
Radek Krejcid54412f2020-12-17 20:25:35 +01002085 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002086 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02002087 break;
2088 case 'w':
Radek Krejcid54412f2020-12-17 20:25:35 +01002089 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002090 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02002091 break;
2092 case 'y':
Radek Krejcid54412f2020-12-17 20:25:35 +01002093 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002094 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
2095 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02002096 break;
David Sedlák23a59a62018-10-26 13:08:02 +02002097 default:
Radek Krejcid54412f2020-12-17 20:25:35 +01002098 /* if indent is not NULL we are matching keyword from YANG data */
2099 if (indent) {
Michal Vasko63f3d842020-07-08 10:10:14 +02002100 if (in->current[0] == ';') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002101 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002102 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02002103 } else if (in->current[0] == '{') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002104 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002105 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002106 } else if (in->current[0] == '}') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002107 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002108 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02002109 }
2110 }
David Sedlák23a59a62018-10-26 13:08:02 +02002111 break;
2112 }
2113
Michal Vasko63f3d842020-07-08 10:10:14 +02002114 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02002115 /* the keyword is not terminated */
2116 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002117 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02002118 }
2119
David Sedlák1bccdfa2019-06-17 15:55:27 +02002120#undef IF_KW
2121#undef IF_KW_PREFIX
2122#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01002123#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02002124 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01002125
David Sedlák1bccdfa2019-06-17 15:55:27 +02002126 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02002127}
David Sedlákecf5eb82019-06-03 14:12:44 +02002128
Radek Krejci85ac8312021-03-03 20:21:33 +01002129LY_ERR
2130lysp_ext_find_definition(const struct ly_ctx *ctx, const struct lysp_ext_instance *ext, const struct lys_module **ext_mod,
2131 struct lysp_ext **ext_def)
2132{
Radek Krejci85ac8312021-03-03 20:21:33 +01002133 const char *tmp, *name, *prefix;
2134 size_t pref_len, name_len;
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002135 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci85ac8312021-03-03 20:21:33 +01002136 const struct lys_module *mod = NULL;
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002137 const struct lysp_submodule *submod;
Radek Krejci85ac8312021-03-03 20:21:33 +01002138
Michal Vasko193dacd2022-10-13 08:43:05 +02002139 if (ext_def) {
2140 *ext_def = NULL;
Radek Krejci85ac8312021-03-03 20:21:33 +01002141 }
2142
Radek Krejci677abea2021-03-05 14:24:53 +01002143 /* parse the prefix, the nodeid was previously already parsed and checked */
Radek Krejci85ac8312021-03-03 20:21:33 +01002144 tmp = ext->name;
Radek Krejci677abea2021-03-05 14:24:53 +01002145 ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len);
Radek Krejci85ac8312021-03-03 20:21:33 +01002146
2147 /* get module where the extension definition should be placed */
Michal Vasko193dacd2022-10-13 08:43:05 +02002148 *ext_mod = mod = ly_resolve_prefix(ctx, prefix, pref_len, ext->format, ext->prefix_data);
Radek Krejci85ac8312021-03-03 20:21:33 +01002149 if (!mod) {
Radek Krejci422afb12021-03-04 16:38:16 +01002150 LOGVAL(ctx, LYVE_REFERENCE, "Invalid prefix \"%.*s\" used for extension instance identifier.", (int)pref_len, prefix);
Radek Krejci85ac8312021-03-03 20:21:33 +01002151 return LY_EVALID;
2152 } else if (!mod->parsed->extensions) {
2153 LOGVAL(ctx, LYVE_REFERENCE, "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
2154 ext->name, mod->name);
2155 return LY_EVALID;
2156 }
2157
Michal Vasko193dacd2022-10-13 08:43:05 +02002158 if (!ext_def) {
2159 /* we are done */
2160 return LY_SUCCESS;
2161 }
2162
Radek Krejci85ac8312021-03-03 20:21:33 +01002163 /* find the parsed extension definition there */
2164 LY_ARRAY_FOR(mod->parsed->extensions, v) {
2165 if (!strcmp(name, mod->parsed->extensions[v].name)) {
2166 *ext_def = &mod->parsed->extensions[v];
2167 break;
2168 }
2169 }
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002170 if (!*ext_def) {
2171 LY_ARRAY_FOR(mod->parsed->includes, u) {
2172 submod = mod->parsed->includes[u].submodule;
2173 LY_ARRAY_FOR(submod->extensions, v) {
2174 if (!strcmp(name, submod->extensions[v].name)) {
2175 *ext_def = &submod->extensions[v];
2176 break;
2177 }
2178 }
2179 }
2180 }
Radek Krejci85ac8312021-03-03 20:21:33 +01002181
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002182 if (!*ext_def) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002183 LOGVAL(ctx, LYVE_REFERENCE, "Extension definition of extension instance \"%s\" not found.", ext->name);
2184 return LY_EVALID;
2185 }
2186
Radek Krejci85ac8312021-03-03 20:21:33 +01002187 return LY_SUCCESS;
2188}
2189
2190LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002191lysp_ext_instance_resolve_argument(struct ly_ctx *ctx, struct lysp_ext_instance *ext_p)
Radek Krejci85ac8312021-03-03 20:21:33 +01002192{
Michal Vasko193dacd2022-10-13 08:43:05 +02002193 assert(ext_p->def);
2194
2195 if (!ext_p->def->argname || ext_p->argument) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002196 /* nothing to do */
2197 return LY_SUCCESS;
2198 }
2199
Radek Krejci8df109d2021-04-23 12:19:08 +02002200 if (ext_p->format == LY_VALUE_XML) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002201 /* schema was parsed from YIN and an argument is expected, ... */
Radek Krejci85ac8312021-03-03 20:21:33 +01002202 struct lysp_stmt *stmt = NULL;
2203
Michal Vasko193dacd2022-10-13 08:43:05 +02002204 if (ext_p->def->flags & LYS_YINELEM_TRUE) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002205 /* ... argument was the first XML child element */
2206 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {}
2207 if (stmt) {
2208 const char *arg, *ext, *name_arg, *name_ext, *prefix_arg, *prefix_ext;
2209 size_t name_arg_len, name_ext_len, prefix_arg_len, prefix_ext_len;
2210
2211 stmt = ext_p->child;
2212
2213 arg = stmt->stmt;
2214 ly_parse_nodeid(&arg, &prefix_arg, &prefix_arg_len, &name_arg, &name_arg_len);
Michal Vasko193dacd2022-10-13 08:43:05 +02002215 if (ly_strncmp(ext_p->def->argname, name_arg, name_arg_len)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002216 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" expects argument element \"%s\" as its first XML child, "
Michal Vasko193dacd2022-10-13 08:43:05 +02002217 "but \"%.*s\" element found.", ext_p->name, ext_p->def->argname, (int)name_arg_len, name_arg);
Radek Krejci85ac8312021-03-03 20:21:33 +01002218 return LY_EVALID;
2219 }
2220
2221 /* check namespace - all the extension instances must be qualified and argument element is expected in the same
2222 * namespace. Do not check just prefixes, there can be different prefixes pointing to the same namespace */
2223 ext = ext_p->name; /* include prefix */
2224 ly_parse_nodeid(&ext, &prefix_ext, &prefix_ext_len, &name_ext, &name_ext_len);
2225
2226 if (ly_resolve_prefix(ctx, prefix_ext, prefix_ext_len, ext_p->format, ext_p->prefix_data) !=
2227 ly_resolve_prefix(ctx, prefix_arg, prefix_arg_len, stmt->format, stmt->prefix_data)) {
2228 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" element and its argument element \"%s\" are "
Michal Vasko193dacd2022-10-13 08:43:05 +02002229 "expected in the same namespace, but they differ.", ext_p->name, ext_p->def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002230 return LY_EVALID;
2231 }
2232 }
2233 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +02002234 /* ... argument was one of the XML attributes which are represented as child stmt with LYS_YIN_ATTR flag */
Radek Krejci85ac8312021-03-03 20:21:33 +01002235 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002236 if (!strcmp(stmt->stmt, ext_p->def->argname)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002237 /* this is the extension's argument */
2238 break;
2239 }
2240 }
2241 }
2242
2243 if (stmt) {
2244 LY_CHECK_RET(lydict_insert(ctx, stmt->arg, 0, &ext_p->argument));
2245 stmt->flags |= LYS_YIN_ARGUMENT;
2246 }
2247 }
2248
2249 if (!ext_p->argument) {
2250 /* missing extension's argument */
Michal Vasko193dacd2022-10-13 08:43:05 +02002251 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" missing argument %s\"%s\".",
2252 ext_p->name, (ext_p->def->flags & LYS_YINELEM_TRUE) ? "element " : "", ext_p->def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002253 return LY_EVALID;
2254 }
2255
2256 return LY_SUCCESS;
2257}
2258
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002259LY_ARRAY_COUNT_TYPE
Radek Krejcifc596f92021-02-26 22:40:26 +01002260lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, enum ly_stmt substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002261{
2262 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
2263
Michal Vaskod989ba02020-08-24 10:59:24 +02002264 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejciab430862021-03-02 20:13:40 +01002265 if (ext[index].parent_stmt == substmt) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02002266 return index;
2267 }
2268 }
2269
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002270 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002271}
2272
Michal Vaskof8ded142022-02-17 10:48:01 +01002273LIBYANG_API_DEF const struct lysc_node *
Michal Vasko72244882021-01-12 15:21:05 +01002274lysc_data_node(const struct lysc_node *schema)
Michal Vasko62ed12d2020-05-21 10:08:25 +02002275{
2276 const struct lysc_node *parent;
2277
Michal Vasko72244882021-01-12 15:21:05 +01002278 parent = schema;
Radek Krejcidf549132021-01-21 10:32:32 +01002279 while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC |
2280 LYS_ACTION | LYS_NOTIF))) {
Radek Krejci7d95fbb2021-01-26 17:33:13 +01002281 parent = parent->parent;
Michal Vasko72244882021-01-12 15:21:05 +01002282 }
Michal Vasko62ed12d2020-05-21 10:08:25 +02002283
2284 return parent;
2285}
Michal Vaskof4258e12021-06-15 12:11:42 +02002286
2287ly_bool
2288lys_has_recompiled(const struct lys_module *mod)
2289{
2290 LY_ARRAY_COUNT_TYPE u;
2291
2292 if (LYSP_HAS_RECOMPILED(mod->parsed)) {
2293 return 1;
2294 }
2295
2296 LY_ARRAY_FOR(mod->parsed->includes, u) {
2297 if (LYSP_HAS_RECOMPILED(mod->parsed->includes[u].submodule)) {
2298 return 1;
2299 }
2300 }
2301
2302 return 0;
2303}
2304
2305ly_bool
2306lys_has_compiled(const struct lys_module *mod)
2307{
2308 LY_ARRAY_COUNT_TYPE u;
2309
2310 if (LYSP_HAS_COMPILED(mod->parsed)) {
2311 return 1;
2312 }
2313
2314 LY_ARRAY_FOR(mod->parsed->includes, u) {
2315 if (LYSP_HAS_COMPILED(mod->parsed->includes[u].submodule)) {
2316 return 1;
2317 }
2318 }
2319
2320 return 0;
2321}
Michal Vasko7ee5be22021-06-16 17:03:34 +02002322
2323ly_bool
Michal Vasko87cfdba2022-02-22 14:13:45 +01002324lys_has_dep_mods(const struct lys_module *mod)
Michal Vasko7ee5be22021-06-16 17:03:34 +02002325{
2326 LY_ARRAY_COUNT_TYPE u;
2327
Michal Vasko87cfdba2022-02-22 14:13:45 +01002328 /* features */
2329 if (mod->parsed->features) {
Michal Vasko7ee5be22021-06-16 17:03:34 +02002330 return 1;
2331 }
2332
Michal Vasko87cfdba2022-02-22 14:13:45 +01002333 /* groupings */
2334 if (mod->parsed->groupings) {
2335 return 1;
2336 }
Michal Vasko7ee5be22021-06-16 17:03:34 +02002337 LY_ARRAY_FOR(mod->parsed->includes, u) {
2338 if (mod->parsed->includes[u].submodule->groupings) {
2339 return 1;
2340 }
2341 }
2342
Michal Vasko87cfdba2022-02-22 14:13:45 +01002343 /* augments (adding nodes with leafrefs) */
2344 if (mod->parsed->augments) {
2345 return 1;
2346 }
2347 LY_ARRAY_FOR(mod->parsed->includes, u) {
2348 if (mod->parsed->includes[u].submodule->augments) {
2349 return 1;
2350 }
2351 }
2352
Michal Vasko7ee5be22021-06-16 17:03:34 +02002353 return 0;
2354}