blob: a771f2666d04ccc8013d9032dce2b81a7b8165c9 [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 Vaskob425bb32022-11-08 10:49:36 +0100148 LY_CHECK_ARG_RET(PARSER_CTX(ctx), date, LY_EINVAL);
149
150 if (date_len != LY_REV_SIZE - 1) {
151 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid length %" PRIu32 " of a date.", (uint32_t)date_len);
152 return LY_EINVAL;
153 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200154
Radek Krejcif13b87b2020-12-01 22:02:17 +0100155 /* check format: YYYY-MM-DD */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200156 for (uint8_t i = 0; i < date_len; i++) {
Michal Vasko69730152020-10-09 16:30:07 +0200157 if ((i == 4) || (i == 7)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200158 if (date[i] != '-') {
159 goto error;
160 }
161 } else if (!isdigit(date[i])) {
162 goto error;
163 }
164 }
165
166 /* check content, e.g. 2018-02-31 */
167 memset(&tm, 0, sizeof tm);
168 r = strptime(date, "%Y-%m-%d", &tm);
Michal Vasko69730152020-10-09 16:30:07 +0200169 if (!r || (r != &date[LY_REV_SIZE - 1])) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200170 goto error;
171 }
172 memcpy(&tm_, &tm, sizeof tm);
Michal Vasko82f32822022-08-17 10:54:35 +0200173
174 /* DST may move the hour back resulting in a different day */
175 tm_.tm_hour = 1;
176
Radek Krejci86d106e2018-10-18 09:53:19 +0200177 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
178 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
179 /* checking days is enough, since other errors
180 * have been checked by strptime() */
181 goto error;
182 }
183
184 return LY_SUCCESS;
185
186error:
Radek Krejcid33273d2018-10-25 14:55:52 +0200187 if (stmt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100188 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt);
Radek Krejcid33273d2018-10-25 14:55:52 +0200189 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200190 return LY_EINVAL;
191}
192
193void
194lysp_sort_revisions(struct lysp_revision *revs)
195{
Radek Krejci857189e2020-09-01 13:26:36 +0200196 LY_ARRAY_COUNT_TYPE i, r;
Radek Krejci86d106e2018-10-18 09:53:19 +0200197 struct lysp_revision rev;
198
Radek Krejcic7d13e32020-12-09 12:32:24 +0100199 for (i = 1, r = 0; i < LY_ARRAY_COUNT(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200200 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200201 r = i;
202 }
203 }
204
205 if (r) {
206 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200207 memcpy(&rev, &revs[0], sizeof rev);
208 memcpy(&revs[0], &revs[r], sizeof rev);
209 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200210 }
211}
Radek Krejci151a5b72018-10-19 14:21:44 +0200212
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200213LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200214lysp_check_enum_name(struct lysp_ctx *ctx, const char *name, size_t name_len)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100215{
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200216 if (!name_len) {
217 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
218 return LY_EVALID;
219 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
220 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
221 (int)name_len, name);
222 return LY_EVALID;
223 } else {
224 for (size_t u = 0; u < name_len; ++u) {
225 if (iscntrl(name[u])) {
226 LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
227 (int)name_len, name, u + 1);
228 break;
229 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100230 }
231 }
232
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200233 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100234}
235
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200236/**
237 * @brief Learn built-in type from its name.
238 *
239 * @param[in] name Type name.
240 * @param[in] len Length of @p name.
241 * @return Built-in data type, ::LY_TYPE_UNKNOWN if none matches.
242 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100243static LY_DATA_TYPE
244lysp_type_str2builtin(const char *name, size_t len)
245{
246 if (len >= 4) { /* otherwise it does not match any built-in type */
247 if (name[0] == 'b') {
248 if (name[1] == 'i') {
Michal Vasko69730152020-10-09 16:30:07 +0200249 if ((len == 6) && !strncmp(&name[2], "nary", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100250 return LY_TYPE_BINARY;
Michal Vasko69730152020-10-09 16:30:07 +0200251 } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100252 return LY_TYPE_BITS;
253 }
Michal Vasko69730152020-10-09 16:30:07 +0200254 } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100255 return LY_TYPE_BOOL;
256 }
257 } else if (name[0] == 'd') {
Michal Vasko69730152020-10-09 16:30:07 +0200258 if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100259 return LY_TYPE_DEC64;
260 }
261 } else if (name[0] == 'e') {
Michal Vasko69730152020-10-09 16:30:07 +0200262 if ((len == 5) && !strncmp(&name[1], "mpty", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100263 return LY_TYPE_EMPTY;
Michal Vasko69730152020-10-09 16:30:07 +0200264 } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100265 return LY_TYPE_ENUM;
266 }
267 } else if (name[0] == 'i') {
268 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200269 if ((len == 4) && !strncmp(&name[2], "t8", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100270 return LY_TYPE_INT8;
271 } else if (len == 5) {
272 if (!strncmp(&name[2], "t16", 3)) {
273 return LY_TYPE_INT16;
274 } else if (!strncmp(&name[2], "t32", 3)) {
275 return LY_TYPE_INT32;
276 } else if (!strncmp(&name[2], "t64", 3)) {
277 return LY_TYPE_INT64;
278 }
Michal Vasko69730152020-10-09 16:30:07 +0200279 } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100280 return LY_TYPE_INST;
281 }
Michal Vasko69730152020-10-09 16:30:07 +0200282 } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100283 return LY_TYPE_IDENT;
284 }
285 } else if (name[0] == 'l') {
Michal Vasko69730152020-10-09 16:30:07 +0200286 if ((len == 7) && !strncmp(&name[1], "eafref", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100287 return LY_TYPE_LEAFREF;
288 }
289 } else if (name[0] == 's') {
Michal Vasko69730152020-10-09 16:30:07 +0200290 if ((len == 6) && !strncmp(&name[1], "tring", 5)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100291 return LY_TYPE_STRING;
292 }
293 } else if (name[0] == 'u') {
294 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200295 if ((len == 5) && !strncmp(&name[2], "ion", 3)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100296 return LY_TYPE_UNION;
297 }
Michal Vasko69730152020-10-09 16:30:07 +0200298 } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) {
299 if ((len == 5) && (name[4] == '8')) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100300 return LY_TYPE_UINT8;
301 } else if (len == 6) {
302 if (!strncmp(&name[4], "16", 2)) {
303 return LY_TYPE_UINT16;
304 } else if (!strncmp(&name[4], "32", 2)) {
305 return LY_TYPE_UINT32;
306 } else if (!strncmp(&name[4], "64", 2)) {
307 return LY_TYPE_UINT64;
308 }
309 }
310 }
311 }
312 }
313
314 return LY_TYPE_UNKNOWN;
315}
316
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200317/**
318 * @brief Find a typedef in a sized array.
319 *
320 * @param[in] name Typedef name.
321 * @param[in] typedefs Sized array of typedefs.
322 * @return Found typedef, NULL if none.
323 */
324static const struct lysp_tpdf *
325lysp_typedef_match(const char *name, const struct lysp_tpdf *typedefs)
326{
327 LY_ARRAY_COUNT_TYPE u;
328
329 LY_ARRAY_FOR(typedefs, u) {
330 if (!strcmp(name, typedefs[u].name)) {
331 /* match */
332 return &typedefs[u];
333 }
334 }
335 return NULL;
336}
337
Radek Krejcibbe09a92018-11-08 09:36:54 +0100338LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +0100339lysp_type_find(const char *id, struct lysp_node *start_node, const struct lysp_module *start_module,
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200340 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 +0100341{
342 const char *str, *name;
343 struct lysp_tpdf *typedefs;
Michal Vasko193dacd2022-10-13 08:43:05 +0200344 const struct lysp_tpdf *ext_typedefs;
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100345 const struct lys_module *mod;
Michal Vaskoa99b3572021-02-01 11:54:58 +0100346 const struct lysp_module *local_module;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200347 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100348
349 assert(id);
350 assert(start_module);
351 assert(tpdf);
352 assert(node);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100353
Radek Krejci4f28eda2018-11-12 11:46:16 +0100354 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100355 str = strchr(id, ':');
356 if (str) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200357 mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_VALUE_SCHEMA, (void *)start_module);
Michal Vaskoa99b3572021-02-01 11:54:58 +0100358 local_module = mod ? mod->parsed : NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100359 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100360 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100361 } else {
Michal Vaskoa99b3572021-02-01 11:54:58 +0100362 local_module = start_module;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100363 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100364
365 /* check for built-in types */
366 *type = lysp_type_str2builtin(name, strlen(name));
367 if (*type) {
368 *tpdf = NULL;
369 return LY_SUCCESS;
370 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100371 }
Michal Vaskoa99b3572021-02-01 11:54:58 +0100372 LY_CHECK_RET(!local_module, LY_ENOTFOUND);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100373
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200374 if (local_module == start_module) {
375 if (start_node) {
376 /* search typedefs in parent's nodes */
377 for (*node = start_node; *node; *node = (*node)->parent) {
378 *tpdf = lysp_typedef_match(name, lysp_node_typedefs(*node));
379 if (*tpdf) {
380 /* match */
381 return LY_SUCCESS;
382 }
383 }
384 }
385
386 if (ext) {
387 /* search typedefs directly in the extension */
Michal Vaskofbd037c2022-11-08 10:34:20 +0100388 lyplg_ext_parsed_get_storage(ext, LY_STMT_TYPEDEF, sizeof ext_typedefs, (const void **)&ext_typedefs);
Michal Vasko193dacd2022-10-13 08:43:05 +0200389 if ((*tpdf = lysp_typedef_match(name, ext_typedefs))) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100390 /* match */
391 return LY_SUCCESS;
392 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100393 }
394 }
395
Michal Vasko915e5442021-06-08 14:59:21 +0200396 /* go to main module if in submodule */
397 local_module = local_module->mod->parsed;
398
Radek Krejcibbe09a92018-11-08 09:36:54 +0100399 /* search in top-level typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100400 if (local_module->typedefs) {
401 LY_ARRAY_FOR(local_module->typedefs, u) {
402 if (!strcmp(name, local_module->typedefs[u].name)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100403 /* match */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100404 *tpdf = &local_module->typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100405 return LY_SUCCESS;
406 }
407 }
408 }
409
Michal Vasko915e5442021-06-08 14:59:21 +0200410 /* search in all submodules' typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100411 LY_ARRAY_FOR(local_module->includes, u) {
412 typedefs = local_module->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100413 LY_ARRAY_FOR(typedefs, v) {
414 if (!strcmp(name, typedefs[v].name)) {
415 /* match */
416 *tpdf = &typedefs[v];
417 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100418 }
419 }
420 }
421
422 return LY_ENOTFOUND;
423}
424
Michal Vaskob36053d2020-03-26 15:49:30 +0100425/**
aPiecekdc12b9f2021-06-25 10:55:47 +0200426 * @brief Insert @p name to hash table and if @p name has already
427 * been added, then log an error.
428 *
429 * This function is used to detect duplicate names.
430 *
431 * @param[in,out] ctx Context to log the error.
432 * @param[in,out] ht Hash table with top-level names.
433 * @param[in] name Inserted top-level identifier.
434 * @param[in] statement The name of the statement type from which
435 * @p name originated (eg typedef, feature, ...).
436 * @param[in] err_detail Optional error specification.
437 * @return LY_ERR, but LY_EEXIST is mapped to LY_EVALID.
438 */
439static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200440lysp_check_dup_ht_insert(struct lysp_ctx *ctx, struct hash_table *ht,
aPiecekdc12b9f2021-06-25 10:55:47 +0200441 const char *name, const char *statement, const char *err_detail)
442{
443 LY_ERR ret;
444 uint32_t hash;
445
446 hash = dict_hash(name, strlen(name));
447 ret = lyht_insert(ht, &name, hash, NULL);
448 if (ret == LY_EEXIST) {
449 if (err_detail) {
450 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT2, name, statement, err_detail);
451 } else {
452 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, name, statement);
453 }
454 ret = LY_EVALID;
455 }
456
457 return ret;
458}
459
460/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100461 * @brief Check name of a new type to avoid name collisions.
462 *
463 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
464 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
465 * @param[in] tpdf Typedef definition to check.
466 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
aPiecekc7594b72021-06-29 09:00:03 +0200467 * typedefs are checked, caller is supposed to free the table.
468 * @return LY_EVALID in case of collision, LY_SUCCESS otherwise.
Radek Krejcibbe09a92018-11-08 09:36:54 +0100469 */
470static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200471lysp_check_dup_typedef(struct lysp_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
aPieceke1fbd952021-06-29 08:12:55 +0200472 struct hash_table *tpdfs_global)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100473{
474 struct lysp_node *parent;
475 uint32_t hash;
476 size_t name_len;
477 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200478 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100479 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100480
481 assert(ctx);
482 assert(tpdf);
483
484 name = tpdf->name;
485 name_len = strlen(name);
486
Radek Krejci4f28eda2018-11-12 11:46:16 +0100487 if (lysp_type_str2builtin(name, name_len)) {
aPiecekc7594b72021-06-29 09:00:03 +0200488 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
489 "Duplicate identifier \"%s\" of typedef statement - name collision with a built-in type.", name);
490 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100491 }
492
493 /* check locally scoped typedefs (avoid name shadowing) */
494 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100495 typedefs = lysp_node_typedefs(node);
496 LY_ARRAY_FOR(typedefs, u) {
497 if (&typedefs[u] == tpdf) {
498 break;
499 }
500 if (!strcmp(name, typedefs[u].name)) {
aPiecekc7594b72021-06-29 09:00:03 +0200501 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
502 "Duplicate identifier \"%s\" of typedef statement - name collision with sibling type.", name);
503 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100504 }
505 }
506 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200507 for (parent = node->parent; parent; parent = parent->parent) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200508 if (lysp_typedef_match(name, lysp_node_typedefs(parent))) {
aPiecekc7594b72021-06-29 09:00:03 +0200509 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
510 "Duplicate identifier \"%s\" of typedef statement - name collision with another scoped type.", name);
511 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100512 }
513 }
514 }
515
516 /* check collision with the top-level typedefs */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100517 if (node) {
aPiecekc7594b72021-06-29 09:00:03 +0200518 hash = dict_hash(name, name_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100519 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
aPiecekc7594b72021-06-29 09:00:03 +0200520 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
521 "Duplicate identifier \"%s\" of typedef statement - scoped type collide with a top-level type.", name);
522 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100523 }
524 } else {
aPiecekc7594b72021-06-29 09:00:03 +0200525 LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, tpdfs_global, name, "typedef",
526 "name collision with another top-level type"));
Radek Krejci3b1f9292018-11-08 10:58:35 +0100527 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
528 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
529 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100530 }
531
532 return LY_SUCCESS;
533}
534
Radek Krejci857189e2020-09-01 13:26:36 +0200535/**
536 * @brief Compare identifiers.
Michal Vasko62524a92021-02-26 10:08:50 +0100537 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200538 */
539static ly_bool
540lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Radek Krejcibbe09a92018-11-08 09:36:54 +0100541{
Michal Vasko11ac39a2021-07-23 12:46:56 +0200542 char *id1, *id2;
543
544 id1 = *(char **)val1;
545 id2 = *(char **)val2;
546
547 return strcmp(id1, id2) == 0 ? 1 : 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100548}
549
550LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200551lysp_check_dup_typedefs(struct lysp_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100552{
553 struct hash_table *ids_global;
Radek Krejci0fb28562018-12-13 15:17:37 +0100554 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200555 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200556 uint32_t i;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100557 LY_ERR ret = LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100558
559 /* check name collisions - typedefs and groupings */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100560 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200561 LY_ARRAY_FOR(mod->typedefs, v) {
aPieceke1fbd952021-06-29 08:12:55 +0200562 ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100563 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100564 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200565 LY_ARRAY_FOR(mod->includes, v) {
566 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200567 ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100568 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci3b1f9292018-11-08 10:58:35 +0100569 }
570 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200571 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
572 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
573 LY_ARRAY_FOR(typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200574 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 +0100575 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100576 }
577 }
Michal Vasko405cc9e2020-12-01 12:01:27 +0100578
Radek Krejcibbe09a92018-11-08 09:36:54 +0100579cleanup:
580 lyht_free(ids_global);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100581 return ret;
582}
583
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200584static const struct lysp_node_grp *
585lysp_grouping_match(const char *name, struct lysp_node *node)
586{
587 const struct lysp_node_grp *groupings, *grp_iter;
588
589 groupings = lysp_node_groupings(node);
590 LY_LIST_FOR(groupings, grp_iter) {
591 if (!strcmp(name, grp_iter->name)) {
592 /* match */
593 return grp_iter;
594 }
595 }
596
597 return NULL;
598}
599
aPiecek63e080d2021-06-29 13:53:28 +0200600/**
601 * @brief Check name of a new grouping to avoid name collisions.
602 *
603 * @param[in] ctx Parser context, module where the grouping is being defined is taken from here.
604 * @param[in] node Schema node where the grouping is being defined, NULL in case of a top-level grouping.
605 * @param[in] grp Grouping definition to check.
606 * @param[in,out] grps_global Initialized hash table to store temporary data between calls. When the module's
607 * groupings are checked, caller is supposed to free the table.
608 * @return LY_EVALID in case of collision, LY_SUCCESS otherwise.
609 */
610static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200611lysp_check_dup_grouping(struct lysp_ctx *ctx, struct lysp_node *node, const struct lysp_node_grp *grp,
aPiecek63e080d2021-06-29 13:53:28 +0200612 struct hash_table *grps_global)
613{
614 struct lysp_node *parent;
615 uint32_t hash;
616 size_t name_len;
617 const char *name;
618 const struct lysp_node_grp *groupings, *grp_iter;
619
620 assert(ctx);
621 assert(grp);
622
623 name = grp->name;
624 name_len = strlen(name);
625
626 /* check locally scoped groupings (avoid name shadowing) */
627 if (node) {
628 groupings = lysp_node_groupings(node);
629 LY_LIST_FOR(groupings, grp_iter) {
630 if (grp_iter == grp) {
631 break;
632 }
633 if (!strcmp(name, grp_iter->name)) {
634 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
635 "Duplicate identifier \"%s\" of grouping statement - name collision with sibling grouping.", name);
636 return LY_EVALID;
637 }
638 }
639 /* search grouping in parent's nodes */
640 for (parent = node->parent; parent; parent = parent->parent) {
641 if (lysp_grouping_match(name, parent)) {
642 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
643 "Duplicate identifier \"%s\" of grouping statement - name collision with another scoped grouping.", name);
644 return LY_EVALID;
645 }
646 }
647 }
648
649 /* check collision with the top-level groupings */
650 if (node) {
651 hash = dict_hash(name, name_len);
652 if (!lyht_find(grps_global, &name, hash, NULL)) {
653 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
654 "Duplicate identifier \"%s\" of grouping statement - scoped grouping collide with a top-level grouping.", name);
655 return LY_EVALID;
656 }
657 } else {
658 LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, grps_global, name, "grouping",
659 "name collision with another top-level grouping"));
660 }
661
662 return LY_SUCCESS;
663}
664
665LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200666lysp_check_dup_groupings(struct lysp_ctx *ctx, struct lysp_module *mod)
aPiecek63e080d2021-06-29 13:53:28 +0200667{
668 struct hash_table *ids_global;
669 const struct lysp_node_grp *groupings, *grp_iter;
670 LY_ARRAY_COUNT_TYPE u;
671 uint32_t i;
672 LY_ERR ret = LY_SUCCESS;
673
674 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
675 LY_LIST_FOR(mod->groupings, grp_iter) {
676 ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global);
677 LY_CHECK_GOTO(ret, cleanup);
678 }
679 LY_ARRAY_FOR(mod->includes, u) {
680 LY_LIST_FOR(mod->includes[u].submodule->groupings, grp_iter) {
681 ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global);
682 LY_CHECK_GOTO(ret, cleanup);
683 }
684 }
685 for (i = 0; i < ctx->grps_nodes.count; ++i) {
686 groupings = lysp_node_groupings((struct lysp_node *)ctx->grps_nodes.objs[i]);
687 LY_LIST_FOR(groupings, grp_iter) {
688 ret = lysp_check_dup_grouping(ctx, (struct lysp_node *)ctx->grps_nodes.objs[i], grp_iter, ids_global);
689 LY_CHECK_GOTO(ret, cleanup);
690 }
691 }
692
693cleanup:
694 lyht_free(ids_global);
695 return ret;
696}
697
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100698static ly_bool
699ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
700{
701 void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
702
703 return ptr1 == ptr2 ? 1 : 0;
704}
705
706LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200707lysp_check_dup_features(struct lysp_ctx *ctx, struct lysp_module *mod)
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100708{
709 LY_ARRAY_COUNT_TYPE u;
710 struct hash_table *ht;
711 struct lysp_feature *f;
aPiecekdc12b9f2021-06-25 10:55:47 +0200712 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100713
aPiecekf6203072021-06-25 10:58:26 +0200714 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100715 LY_CHECK_RET(!ht, LY_EMEM);
716
717 /* add all module features into a hash table */
718 LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200719 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
720 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200721 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100722 }
723
724 /* add all submodule features into a hash table */
725 LY_ARRAY_FOR(mod->includes, u) {
726 LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200727 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
728 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200729 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100730 }
731 }
732
733cleanup:
734 lyht_free(ht);
735 return ret;
736}
737
738LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200739lysp_check_dup_identities(struct lysp_ctx *ctx, struct lysp_module *mod)
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100740{
741 LY_ARRAY_COUNT_TYPE u;
742 struct hash_table *ht;
743 struct lysp_ident *i;
aPiecekdc12b9f2021-06-25 10:55:47 +0200744 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100745
aPiecekf6203072021-06-25 10:58:26 +0200746 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100747 LY_CHECK_RET(!ht, LY_EMEM);
748
749 /* add all module identities into a hash table */
750 LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200751 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
752 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200753 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100754 }
755
756 /* add all submodule identities into a hash table */
757 LY_ARRAY_FOR(mod->includes, u) {
758 LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200759 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
760 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200761 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100762 }
763 }
764
765cleanup:
766 lyht_free(ht);
767 return ret;
768}
769
Radek Krejci9ed7a192018-10-31 16:23:51 +0100770struct lysp_load_module_check_data {
771 const char *name;
772 const char *revision;
773 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200774 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100775};
776
777static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100778lysp_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 +0100779{
780 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100781 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200782 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100783 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100784 struct lysp_revision *revs;
785
786 name = mod ? mod->mod->name : submod->name;
787 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200788 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100789
790 if (info->name) {
791 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100792 if (strcmp(info->name, name)) {
793 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100794 return LY_EINVAL;
795 }
796 }
797 if (info->revision) {
798 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100799 if (!revs || strcmp(info->revision, revs[0].date)) {
800 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200801 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100802 return LY_EINVAL;
803 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200804 } else if (!latest_revision) {
805 /* do not log, we just need to drop the schema and use the latest revision from the context */
806 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100807 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100808 if (submod) {
809 assert(info->submoduleof);
810
Radek Krejci9ed7a192018-10-31 16:23:51 +0100811 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200812 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100813 LOGVAL(ctx, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200814 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100815 return LY_EVALID;
816 }
817 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100818 if (submod->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100819 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100820 return LY_EVALID;
821 }
822 }
823 if (info->path) {
824 /* check that name and revision match filename */
825 filename = strrchr(info->path, '/');
826 if (!filename) {
827 filename = info->path;
828 } else {
829 filename++;
830 }
831 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100832 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100833 rev = strchr(filename, '@');
834 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100835 if (strncmp(filename, name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +0200836 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100837 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100838 }
839 /* revision */
840 if (rev) {
841 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100842 if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100843 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +0200844 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100845 }
846 }
847 }
848 return LY_SUCCESS;
849}
850
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200851/**
Michal Vasko4e205e82021-06-08 14:01:47 +0200852 * @brief Parse a (sub)module from a local file and add into the context.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200853 *
854 * This function does not check the presence of the (sub)module in context, it should be done before calling this function.
855 *
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200856 * @param[in] ctx libyang context where to work.
857 * @param[in] name Name of the (sub)module to load.
858 * @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 +0200859 * @param[in] main_ctx Parser context of the main module in case of loading submodule.
860 * @param[in] main_name Main module name in case of loading submodule.
861 * @param[in] required Module is required so error (even if the input file not found) are important. If 0, there is some
862 * 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 +0200863 * @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 +0200864 * @param[out] result Parsed YANG schema tree of the requested module (struct lys_module*) or submodule (struct lysp_submodule*).
865 * If it is a module, it is already in the context!
Michal Vasko4e205e82021-06-08 14:01:47 +0200866 * @return LY_SUCCESS on success.
Michal Vasko4e205e82021-06-08 14:01:47 +0200867 * @return LY_ERR on error.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200868 */
869static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200870lys_parse_localfile(struct ly_ctx *ctx, const char *name, const char *revision, struct lysp_ctx *main_ctx,
Michal Vaskodd992582021-06-10 14:34:57 +0200871 const char *main_name, ly_bool required, struct ly_set *new_mods, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100872{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200873 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100874 char *filepath = NULL;
875 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100876 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100877 LY_ERR ret = LY_SUCCESS;
878 struct lysp_load_module_check_data check_data = {0};
879
Michal Vasko87f1cf02021-06-08 14:02:47 +0200880 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name,
881 revision, &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200882 if (!filepath) {
883 if (required) {
884 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200885 revision ? revision : "");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200886 }
887 return LY_ENOTFOUND;
888 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100889
890 LOGVRB("Loading schema from \"%s\" file.", filepath);
891
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200892 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200893 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +0200894 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100895 check_data.name = name;
896 check_data.revision = revision;
897 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800898 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200899 if (main_ctx) {
aPiecekc3e26142021-06-22 14:25:49 +0200900 ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data, new_mods,
Michal Vasko69730152020-10-09 16:30:07 +0200901 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200902 } else {
Michal Vaskodd992582021-06-10 14:34:57 +0200903 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 +0200904
905 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200906 ly_in_free(in, 1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200907 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100908
909 *result = mod;
910
911 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200912
Radek Krejci9ed7a192018-10-31 16:23:51 +0100913cleanup:
914 free(filepath);
915 return ret;
916}
917
aPiecek4725aea2021-07-28 10:18:33 +0200918/**
919 * @brief Load module from searchdirs or from callback.
920 *
921 * @param[in] ctx libyang context where to work.
922 * @param[in] name Name of module to load.
923 * @param[in] revision Revision of module to load.
Michal Vaskobdac23f2022-01-12 14:02:35 +0100924 * @param[in] mod_latest Module with the latest revision found in context, otherwise set to NULL.
925 * @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 +0200926 * @param[out] mod Loaded module.
927 * @return LY_SUCCESS on success.
928 * @return LY_ERR on error.
929 */
930static LY_ERR
931lys_parse_load_from_clb_or_file(struct ly_ctx *ctx, const char *name, const char *revision,
932 struct lys_module *mod_latest, struct ly_set *new_mods, struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200933{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100934 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200935 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200936
Radek Krejci9ed7a192018-10-31 16:23:51 +0100937 void (*module_data_free)(void *module_data, void *user_data) = NULL;
938 struct lysp_load_module_check_data check_data = {0};
Michal Vasko63f3d842020-07-08 10:10:14 +0200939 struct ly_in *in;
Radek Krejci086c7132018-10-26 15:29:04 +0200940
Michal Vaskobdac23f2022-01-12 14:02:35 +0100941 *mod = NULL;
942
943 if (mod_latest && (!ctx->imp_clb || (mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB)) &&
944 ((ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) || (mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) {
945 /* we are not able to find a newer revision */
946 return LY_SUCCESS;
947 }
948
aPiecek4725aea2021-07-28 10:18:33 +0200949 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
950search_clb:
Michal Vaskobdac23f2022-01-12 14:02:35 +0100951 /* check there is a callback and should be called */
952 if (ctx->imp_clb && (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB))) {
953 if (!ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, &format, &module_data, &module_data_free)) {
954 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
955 check_data.name = name;
956 check_data.revision = revision;
957 lys_parse_in(ctx, in, format, lysp_load_module_check, &check_data, new_mods, mod);
958 ly_in_free(in, 0);
959 if (module_data_free) {
960 module_data_free((void *)module_data, ctx->imp_clb_data);
961 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200962 }
Radek Krejci0af46292019-01-11 16:02:31 +0100963 }
Michal Vaskobdac23f2022-01-12 14:02:35 +0100964 if (*mod && !revision) {
965 /* we got the latest revision module from the callback */
966 (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB;
967 } else if (!*mod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
aPiecek4725aea2021-07-28 10:18:33 +0200968 goto search_file;
969 }
970 } else {
971search_file:
Michal Vaskobdac23f2022-01-12 14:02:35 +0100972 /* check we can use searchdirs and that we should */
973 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) &&
974 (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) {
aPiecek4725aea2021-07-28 10:18:33 +0200975 lys_parse_localfile(ctx, name, revision, NULL, NULL, mod_latest ? 0 : 1, new_mods, (void **)mod);
976 }
Michal Vaskobdac23f2022-01-12 14:02:35 +0100977 if (*mod && !revision) {
978 /* we got the latest revision module in the searchdirs */
979 (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB;
980 } else if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
aPiecek4725aea2021-07-28 10:18:33 +0200981 goto search_clb;
982 }
Radek Krejci086c7132018-10-26 15:29:04 +0200983 }
984
aPiecek4725aea2021-07-28 10:18:33 +0200985 return LY_SUCCESS;
986}
987
988/**
aPiecekd4911ee2021-07-30 07:40:24 +0200989 * @brief Get module without revision according to priorities.
990 *
991 * 1. Search for the module with LYS_MOD_IMPORTED_REV.
992 * 2. Search for the implemented module.
993 * 3. Search for the latest module in the context.
994 *
995 * @param[in] ctx libyang context where module is searched.
996 * @param[in] name Name of the searched module.
aPiecekd4911ee2021-07-30 07:40:24 +0200997 * @return Found module from context or NULL.
998 */
999static struct lys_module *
Michal Vaskobdac23f2022-01-12 14:02:35 +01001000lys_get_module_without_revision(struct ly_ctx *ctx, const char *name)
aPiecekd4911ee2021-07-30 07:40:24 +02001001{
1002 struct lys_module *mod, *mod_impl;
1003 uint32_t index;
1004
aPiecekd4911ee2021-07-30 07:40:24 +02001005 /* Try to find module with LYS_MOD_IMPORTED_REV flag. */
1006 index = 0;
1007 while ((mod = ly_ctx_get_module_iter(ctx, &index))) {
1008 if (!strcmp(mod->name, name) && (mod->latest_revision & LYS_MOD_IMPORTED_REV)) {
1009 break;
1010 }
1011 }
1012
1013 /* Try to find the implemented module. */
1014 mod_impl = ly_ctx_get_module_implemented(ctx, name);
1015 if (mod && mod_impl) {
Michal Vaskobdac23f2022-01-12 14:02:35 +01001016 LOGVRB("Implemented module \"%s@%s\" is not used for import, revision \"%s\" is imported instead.",
aPiecekd4911ee2021-07-30 07:40:24 +02001017 mod_impl->name, mod_impl->revision, mod->revision);
1018 return mod;
1019 } else if (mod_impl) {
1020 return mod_impl;
1021 }
1022
1023 /* Try to find the latest module in the current context. */
1024 mod = ly_ctx_get_module_latest(ctx, name);
aPiecekd4911ee2021-07-30 07:40:24 +02001025
1026 return mod;
1027}
1028
1029/**
aPiecek4725aea2021-07-28 10:18:33 +02001030 * @brief Check if a circular dependency exists between modules.
1031 *
1032 * @param[in] ctx libyang context for log an error.
1033 * @param[in,out] mod Examined module which is set to NULL
1034 * if the circular dependency is detected.
1035 * @return LY_SUCCESS if no circular dependecy is detected,
1036 * otherwise LY_EVALID.
1037 */
1038static LY_ERR
1039lys_check_circular_dependency(struct ly_ctx *ctx, struct lys_module **mod)
1040{
1041 if ((*mod) && (*mod)->parsed->parsing) {
1042 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", (*mod)->name);
Michal Vasko4e205e82021-06-08 14:01:47 +02001043 *mod = NULL;
1044 return LY_EVALID;
Michal Vasko0550b762020-11-24 18:04:08 +01001045 }
Radek Krejci086c7132018-10-26 15:29:04 +02001046
aPiecek4725aea2021-07-28 10:18:33 +02001047 return LY_SUCCESS;
1048}
1049
1050LY_ERR
1051lys_parse_load(struct ly_ctx *ctx, const char *name, const char *revision, struct ly_set *new_mods,
1052 struct lys_module **mod)
1053{
1054 struct lys_module *mod_latest = NULL;
1055
1056 assert(mod && new_mods);
1057
Michal Vasko0550b762020-11-24 18:04:08 +01001058 /*
aPiecek4725aea2021-07-28 10:18:33 +02001059 * Try to get the module from the context.
Michal Vasko0550b762020-11-24 18:04:08 +01001060 */
aPiecek4725aea2021-07-28 10:18:33 +02001061 if (revision) {
1062 /* Get the specific revision. */
1063 *mod = ly_ctx_get_module(ctx, name, revision);
1064 } else {
Michal Vaskobdac23f2022-01-12 14:02:35 +01001065 /* Get the requested module in a suitable revision in the context. */
1066 *mod = lys_get_module_without_revision(ctx, name);
1067 if (*mod && !(*mod)->implemented && !((*mod)->latest_revision & LYS_MOD_IMPORTED_REV)) {
aPiecek4725aea2021-07-28 10:18:33 +02001068 /* Let us now search with callback and searchpaths to check
1069 * if there is newer revision outside the context.
1070 */
1071 mod_latest = *mod;
1072 *mod = NULL;
1073 }
1074 }
1075
Michal Vasko0550b762020-11-24 18:04:08 +01001076 if (!*mod) {
aPiecek4725aea2021-07-28 10:18:33 +02001077 /* No suitable module in the context, try to load it. */
Michal Vaskobdac23f2022-01-12 14:02:35 +01001078 LY_CHECK_RET(lys_parse_load_from_clb_or_file(ctx, name, revision, mod_latest, new_mods, mod));
aPiecek4725aea2021-07-28 10:18:33 +02001079 if (!*mod && !mod_latest) {
Michal Vasko4e205e82021-06-08 14:01:47 +02001080 LOGVAL(ctx, LYVE_REFERENCE, "Loading \"%s\" module failed.", name);
Michal Vasko0550b762020-11-24 18:04:08 +01001081 return LY_EVALID;
1082 }
aPiecek4725aea2021-07-28 10:18:33 +02001083
1084 /* Update the latest_revision flag - here we have selected the latest available schema,
1085 * consider that even the callback provides correct latest revision.
1086 */
1087 if (!*mod) {
1088 LOGVRB("Newer revision than \"%s@%s\" not found, using this as the latest revision.",
1089 mod_latest->name, mod_latest->revision);
1090 assert(mod_latest->latest_revision & LYS_MOD_LATEST_REV);
1091 mod_latest->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS;
1092 *mod = mod_latest;
1093 } else if (*mod && !revision && ((*mod)->latest_revision & LYS_MOD_LATEST_REV)) {
1094 (*mod)->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS;
1095 }
Radek Krejci086c7132018-10-26 15:29:04 +02001096 }
Radek Krejci086c7132018-10-26 15:29:04 +02001097
aPiecek9f8c7e72021-07-28 12:01:56 +02001098 /* Checking the circular dependence of imported modules. */
1099 LY_CHECK_RET(lys_check_circular_dependency(ctx, mod));
1100
Radek Krejci086c7132018-10-26 15:29:04 +02001101 return LY_SUCCESS;
1102}
1103
1104LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001105lysp_check_stringchar(struct lysp_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +02001106{
1107 if (!is_yangutf8char(c)) {
1108 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
1109 return LY_EVALID;
1110 }
1111 return LY_SUCCESS;
1112}
1113
1114LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001115lysp_check_identifierchar(struct lysp_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +02001116{
Michal Vasko69730152020-10-09 16:30:07 +02001117 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +02001118 if (!is_yangidentstartchar(c)) {
aPiecekc89b2242021-05-14 14:19:11 +02001119 if ((c < UCHAR_MAX) && isprint(c)) {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001120 if (ctx) {
1121 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
1122 }
Michal Vasko7c769042021-03-25 12:20:49 +01001123 } else {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001124 if (ctx) {
1125 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character 0x%04x.", c);
1126 }
Michal Vasko7c769042021-03-25 12:20:49 +01001127 }
David Sedlák4a650532019-07-10 11:55:18 +02001128 return LY_EVALID;
1129 }
1130 if (prefix) {
1131 if (first) {
1132 (*prefix) = 0;
1133 } else {
1134 (*prefix) = 2;
1135 }
1136 }
Michal Vasko69730152020-10-09 16:30:07 +02001137 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +02001138 (*prefix) = 1;
1139 } else if (!is_yangidentchar(c)) {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001140 if (ctx) {
1141 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
1142 }
David Sedlák4a650532019-07-10 11:55:18 +02001143 return LY_EVALID;
1144 }
1145
1146 return LY_SUCCESS;
1147}
1148
Radek Krejci771928a2021-01-19 13:42:36 +01001149/**
1150 * @brief Try to find the parsed submodule in main module for the given include record.
1151 *
1152 * @param[in] pctx main parser context
1153 * @param[in] inc The include record with missing parsed submodule. According to include info try to find
1154 * the corresponding parsed submodule in main module's includes.
1155 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
1156 * @return LY_ENOT - the parsed module was not found.
1157 * @return LY_EVALID - YANG rule violation
1158 */
1159static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001160lysp_main_pmod_get_submodule(struct lysp_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +02001161{
Radek Krejci771928a2021-01-19 13:42:36 +01001162 LY_ARRAY_COUNT_TYPE i;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001163 struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed;
Michal Vasko69730152020-10-09 16:30:07 +02001164
Radek Krejci771928a2021-01-19 13:42:36 +01001165 LY_ARRAY_FOR(main_pmod->includes, i) {
1166 if (strcmp(main_pmod->includes[i].name, inc->name)) {
1167 continue;
1168 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001169
Radek Krejci771928a2021-01-19 13:42:36 +01001170 if (inc->rev[0] && strncmp(inc->rev, main_pmod->includes[i].rev, LY_REV_SIZE)) {
1171 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1172 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001173 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev,
Radek Krejci771928a2021-01-19 13:42:36 +01001174 main_pmod->includes[i].name, main_pmod->includes[i].rev, main_pmod->mod->name);
1175 return LY_EVALID;
1176 }
1177
1178 inc->submodule = main_pmod->includes[i].submodule;
1179 return inc->submodule ? LY_SUCCESS : LY_ENOT;
1180 }
1181
1182 if (main_pmod->version == LYS_VERSION_1_1) {
1183 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1184 "YANG 1.1 requires all submodules to be included from main module. "
1185 "But submodule \"%s\" includes submodule \"%s\" which is not included by main module \"%s\".",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001186 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->name, main_pmod->mod->name);
Radek Krejci771928a2021-01-19 13:42:36 +01001187 return LY_EVALID;
1188 } else {
1189 return LY_ENOT;
1190 }
1191}
1192
1193/**
Michal Vasko8a67eff2021-12-07 14:04:47 +01001194 * @brief Try to find the parsed submodule in currenlty parsed modules for the given include record.
1195 *
1196 * @param[in] pctx main parser context
1197 * @param[in] inc The include record with missing parsed submodule.
1198 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
1199 * @return LY_ENOT - the parsed module was not found.
1200 * @return LY_EVALID - YANG rule violation
1201 */
1202static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001203lysp_parsed_mods_get_submodule(struct lysp_ctx *pctx, struct lysp_include *inc)
Michal Vasko8a67eff2021-12-07 14:04:47 +01001204{
1205 uint32_t i;
1206 struct lysp_submodule *submod;
1207
1208 for (i = 0; i < pctx->parsed_mods->count - 1; ++i) {
1209 submod = pctx->parsed_mods->objs[i];
1210 if (!submod->is_submod) {
1211 continue;
1212 }
1213
1214 if (strcmp(submod->name, inc->name)) {
1215 continue;
1216 }
1217
1218 if (inc->rev[0] && submod->revs && strncmp(inc->rev, submod->revs[0].date, LY_REV_SIZE)) {
1219 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1220 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
1221 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev,
1222 submod->name, submod->revs[0].date, PARSER_CUR_PMOD(pctx)->mod->name);
1223 return LY_EVALID;
1224 }
1225
1226 inc->submodule = submod;
1227 return LY_SUCCESS;
1228 }
1229
1230 return LY_ENOT;
1231}
1232
1233/**
Radek Krejci771928a2021-01-19 13:42:36 +01001234 * @brief Make the copy of the given include record into the main module.
1235 *
1236 * YANG 1.0 does not require the main module to include all the submodules. Therefore, parsing submodules can cause
1237 * reallocating and extending the includes array in the main module by the submodules included only in submodules.
1238 *
1239 * @param[in] pctx main parser context
1240 * @param[in] inc Include record to copy into main module taken from @p pctx.
1241 * @return LY_ERR value.
1242 */
1243static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001244lysp_inject_submodule(struct lysp_ctx *pctx, struct lysp_include *inc)
Radek Krejci771928a2021-01-19 13:42:36 +01001245{
1246 LY_ARRAY_COUNT_TYPE i;
1247 struct lysp_include *inc_new, *inc_tofill = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001248 struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed;
Radek Krejci771928a2021-01-19 13:42:36 +01001249
1250 /* first, try to find the corresponding record with missing parsed submodule */
1251 LY_ARRAY_FOR(main_pmod->includes, i) {
1252 if (strcmp(main_pmod->includes[i].name, inc->name)) {
1253 continue;
1254 }
1255 inc_tofill = &main_pmod->includes[i];
1256 break;
1257 }
1258
1259 if (inc_tofill) {
1260 inc_tofill->submodule = inc->submodule;
1261 } else {
1262 LY_ARRAY_NEW_RET(PARSER_CTX(pctx), main_pmod->includes, inc_new, LY_EMEM);
1263
1264 inc_new->submodule = inc->submodule;
1265 DUP_STRING_RET(PARSER_CTX(pctx), inc->name, inc_new->name);
1266 DUP_STRING_RET(PARSER_CTX(pctx), inc->dsc, inc_new->dsc);
1267 DUP_STRING_RET(PARSER_CTX(pctx), inc->ref, inc_new->ref);
1268 /* TODO duplicate extensions */
1269 memcpy(inc_new->rev, inc->rev, LY_REV_SIZE);
1270 inc_new->injected = 1;
1271 }
1272 return LY_SUCCESS;
1273}
1274
1275LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001276lysp_load_submodules(struct lysp_ctx *pctx, struct lysp_module *pmod, struct ly_set *new_mods)
Radek Krejci771928a2021-01-19 13:42:36 +01001277{
1278 LY_ARRAY_COUNT_TYPE u;
1279 struct ly_ctx *ctx = PARSER_CTX(pctx);
1280
1281 LY_ARRAY_FOR(pmod->includes, u) {
Michal Vasko8a67eff2021-12-07 14:04:47 +01001282 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci771928a2021-01-19 13:42:36 +01001283 struct lysp_submodule *submod = NULL;
1284 struct lysp_include *inc = &pmod->includes[u];
1285
1286 if (inc->submodule) {
1287 continue;
1288 }
1289
1290 if (pmod->is_submod) {
1291 /* try to find the submodule in the main module or its submodules */
Michal Vasko8a67eff2021-12-07 14:04:47 +01001292 ret = lysp_main_pmod_get_submodule(pctx, inc);
1293 LY_CHECK_RET(ret != LY_ENOT, ret);
Radek Krejci771928a2021-01-19 13:42:36 +01001294 }
1295
Michal Vasko8a67eff2021-12-07 14:04:47 +01001296 /* try to use currently parsed submodule */
1297 r = lysp_parsed_mods_get_submodule(pctx, inc);
1298 LY_CHECK_RET(r != LY_ENOT, r);
1299
Radek Krejci771928a2021-01-19 13:42:36 +01001300 /* submodule not present in the main module, get the input data and parse it */
1301 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcidf549132021-01-21 10:32:32 +01001302search_clb:
Radek Krejci771928a2021-01-19 13:42:36 +01001303 if (ctx->imp_clb) {
1304 const char *submodule_data = NULL;
1305 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko26bbb272022-08-02 14:54:33 +02001306
Radek Krejci771928a2021-01-19 13:42:36 +01001307 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
1308 struct lysp_load_module_check_data check_data = {0};
1309 struct ly_in *in;
1310
Michal Vasko8a67eff2021-12-07 14:04:47 +01001311 if (ctx->imp_clb(PARSER_CUR_PMOD(pctx)->mod->name, NULL, inc->name,
Radek Krejci771928a2021-01-19 13:42:36 +01001312 inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
1313 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
1314 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
1315 check_data.name = inc->name;
1316 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001317 check_data.submoduleof = PARSER_CUR_PMOD(pctx)->mod->name;
aPiecekc3e26142021-06-22 14:25:49 +02001318 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, new_mods, &submod);
Radek Krejci771928a2021-01-19 13:42:36 +01001319
1320 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1321 * submodule's include into main module, where it is missing */
1322 inc = &pmod->includes[u];
1323
1324 ly_in_free(in, 0);
1325 if (submodule_data_free) {
1326 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
1327 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001328 }
1329 }
Radek Krejci771928a2021-01-19 13:42:36 +01001330 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1331 goto search_file;
1332 }
1333 } else {
Radek Krejcidf549132021-01-21 10:32:32 +01001334search_file:
Radek Krejci771928a2021-01-19 13:42:36 +01001335 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
1336 /* submodule was not received from the callback or there is no callback set */
Michal Vaskodd6d5a32022-07-14 13:54:58 +02001337 lys_parse_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, pctx->main_ctx,
1338 PARSER_CUR_PMOD(pctx->main_ctx)->mod->name, 1, new_mods, (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001339
Radek Krejci771928a2021-01-19 13:42:36 +01001340 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1341 * submodule's include into main module, where it is missing */
1342 inc = &pmod->includes[u];
1343 }
1344 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1345 goto search_clb;
1346 }
1347 }
1348 if (submod) {
1349 if (!inc->rev[0] && (submod->latest_revision == 1)) {
1350 /* update the latest_revision flag - here we have selected the latest available schema,
1351 * consider that even the callback provides correct latest revision */
1352 submod->latest_revision = 2;
1353 }
1354
1355 inc->submodule = submod;
1356 if (ret == LY_ENOT) {
1357 /* the submodule include is not present in YANG 1.0 main module - add it there */
1358 LY_CHECK_RET(lysp_inject_submodule(pctx, &pmod->includes[u]));
1359 }
1360 }
1361 if (!inc->submodule) {
1362 LOGVAL(ctx, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name,
Michal Vasko8a67eff2021-12-07 14:04:47 +01001363 PARSER_CUR_PMOD(pctx)->is_submod ? ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name :
1364 PARSER_CUR_PMOD(pctx)->mod->name);
Radek Krejci771928a2021-01-19 13:42:36 +01001365 return LY_EVALID;
1366 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001367 }
1368
1369 return LY_SUCCESS;
1370}
1371
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001372LIBYANG_API_DEF const struct lysc_when *
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001373lysc_has_when(const struct lysc_node *node)
1374{
Radek Krejci9a3823e2021-01-27 20:26:46 +01001375 struct lysc_when **when;
1376
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001377 if (!node) {
1378 return NULL;
1379 }
1380
1381 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001382 when = lysc_node_when(node);
1383 if (when) {
1384 return when[0];
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001385 }
1386 node = node->parent;
1387 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1388
1389 return NULL;
1390}
1391
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001392LIBYANG_API_DEF const struct lys_module *
Michal Vaskoef53c812021-10-13 10:21:03 +02001393lysc_owner_module(const struct lysc_node *node)
1394{
1395 if (!node) {
1396 return NULL;
1397 }
1398
1399 for ( ; node->parent; node = node->parent) {}
1400 return node->module;
1401}
1402
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001403LIBYANG_API_DEF const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001404lys_nodetype2str(uint16_t nodetype)
1405{
Michal Vaskod989ba02020-08-24 10:59:24 +02001406 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001407 case LYS_CONTAINER:
1408 return "container";
1409 case LYS_CHOICE:
1410 return "choice";
1411 case LYS_LEAF:
1412 return "leaf";
1413 case LYS_LEAFLIST:
1414 return "leaf-list";
1415 case LYS_LIST:
1416 return "list";
1417 case LYS_ANYXML:
1418 return "anyxml";
1419 case LYS_ANYDATA:
1420 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001421 case LYS_CASE:
1422 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001423 case LYS_RPC:
1424 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001425 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001426 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001427 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001428 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001429 case LYS_USES:
1430 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001431 default:
1432 return "unknown";
1433 }
1434}
1435
Radek Krejci693262f2019-04-29 15:23:20 +02001436const char *
1437lys_datatype2str(LY_DATA_TYPE basetype)
1438{
Michal Vaskod989ba02020-08-24 10:59:24 +02001439 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001440 case LY_TYPE_BINARY:
1441 return "binary";
1442 case LY_TYPE_UINT8:
1443 return "uint8";
1444 case LY_TYPE_UINT16:
1445 return "uint16";
1446 case LY_TYPE_UINT32:
1447 return "uint32";
1448 case LY_TYPE_UINT64:
1449 return "uint64";
1450 case LY_TYPE_STRING:
1451 return "string";
1452 case LY_TYPE_BITS:
1453 return "bits";
1454 case LY_TYPE_BOOL:
1455 return "boolean";
1456 case LY_TYPE_DEC64:
1457 return "decimal64";
1458 case LY_TYPE_EMPTY:
1459 return "empty";
1460 case LY_TYPE_ENUM:
1461 return "enumeration";
1462 case LY_TYPE_IDENT:
1463 return "identityref";
1464 case LY_TYPE_INST:
1465 return "instance-identifier";
1466 case LY_TYPE_LEAFREF:
1467 return "leafref";
1468 case LY_TYPE_UNION:
1469 return "union";
1470 case LY_TYPE_INT8:
1471 return "int8";
1472 case LY_TYPE_INT16:
1473 return "int16";
1474 case LY_TYPE_INT32:
1475 return "int32";
1476 case LY_TYPE_INT64:
1477 return "int64";
1478 default:
1479 return "unknown";
1480 }
1481}
1482
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001483LIBYANG_API_DEF const struct lysp_tpdf *
Radek Krejci056d0a82018-12-06 16:57:25 +01001484lysp_node_typedefs(const struct lysp_node *node)
1485{
Radek Krejci0fb28562018-12-13 15:17:37 +01001486 switch (node->nodetype) {
1487 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001488 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001489 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001490 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001491 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001492 return ((struct lysp_node_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001493 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001494 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001495 return ((struct lysp_node_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001496 case LYS_INPUT:
1497 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001498 return ((struct lysp_node_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001499 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001500 return ((struct lysp_node_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001501 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001502 return NULL;
1503 }
1504}
1505
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001506LIBYANG_API_DEF const struct lysp_node_grp *
Radek Krejci53ea6152018-12-13 15:21:15 +01001507lysp_node_groupings(const struct lysp_node *node)
1508{
1509 switch (node->nodetype) {
1510 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001511 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001512 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001513 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001514 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001515 return ((struct lysp_node_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001516 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001517 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001518 return ((struct lysp_node_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001519 case LYS_INPUT:
1520 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001521 return ((struct lysp_node_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001522 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001523 return ((struct lysp_node_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001524 default:
1525 return NULL;
1526 }
1527}
1528
Radek Krejci2a9fc652021-01-22 17:44:34 +01001529struct lysp_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001530lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001531{
1532 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001533
Radek Krejcibbe09a92018-11-08 09:36:54 +01001534 switch (node->nodetype) {
1535 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001536 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001537 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001538 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001539 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001540 return &((struct lysp_node_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001541 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001542 return &((struct lysp_node_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001543 default:
1544 return NULL;
1545 }
1546}
1547
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001548LIBYANG_API_DEF const struct lysp_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001549lysp_node_actions(const struct lysp_node *node)
1550{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001551 struct lysp_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001552
Michal Vasko22df3f02020-08-24 13:29:22 +02001553 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001554 if (actions) {
1555 return *actions;
1556 } else {
1557 return NULL;
1558 }
1559}
1560
Radek Krejci2a9fc652021-01-22 17:44:34 +01001561struct lysp_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001562lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001563{
1564 assert(node);
1565 switch (node->nodetype) {
1566 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001567 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001568 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001569 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001570 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001571 return &((struct lysp_node_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001572 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001573 return &((struct lysp_node_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001574 default:
1575 return NULL;
1576 }
1577}
1578
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001579LIBYANG_API_DEF const struct lysp_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001580lysp_node_notifs(const struct lysp_node *node)
1581{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001582 struct lysp_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001583
Michal Vasko22df3f02020-08-24 13:29:22 +02001584 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001585 if (notifs) {
1586 return *notifs;
1587 } else {
1588 return NULL;
1589 }
1590}
1591
Radek Krejcibbe09a92018-11-08 09:36:54 +01001592struct lysp_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001593lysp_node_child_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001594{
1595 assert(node);
1596 switch (node->nodetype) {
1597 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001598 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001599 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001600 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001601 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001602 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001603 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001604 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001605 case LYS_GROUPING:
Radek Krejci01180ac2021-01-27 08:48:22 +01001606 return &((struct lysp_node_grp *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001607 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001608 return &((struct lysp_node_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001609 case LYS_INPUT:
1610 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001611 return &((struct lysp_node_action_inout *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001612 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001613 return &((struct lysp_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001614 default:
1615 return NULL;
1616 }
1617}
1618
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001619LIBYANG_API_DEF const struct lysp_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001620lysp_node_child(const struct lysp_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01001621{
Michal Vasko544e58a2021-01-28 14:33:41 +01001622 struct lysp_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001623
1624 if (!node) {
1625 return NULL;
1626 }
1627
Michal Vasko544e58a2021-01-28 14:33:41 +01001628 child = lysp_node_child_p((struct lysp_node *)node);
1629 if (child) {
1630 return *child;
Radek Krejci056d0a82018-12-06 16:57:25 +01001631 } else {
1632 return NULL;
1633 }
1634}
1635
Radek Krejci9a3823e2021-01-27 20:26:46 +01001636struct lysp_restr **
1637lysp_node_musts_p(const struct lysp_node *node)
1638{
1639 if (!node) {
1640 return NULL;
1641 }
1642
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001643 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001644 case LYS_CONTAINER:
1645 return &((struct lysp_node_container *)node)->musts;
1646 case LYS_LEAF:
1647 return &((struct lysp_node_leaf *)node)->musts;
1648 case LYS_LEAFLIST:
1649 return &((struct lysp_node_leaflist *)node)->musts;
1650 case LYS_LIST:
1651 return &((struct lysp_node_list *)node)->musts;
1652 case LYS_ANYXML:
1653 case LYS_ANYDATA:
1654 return &((struct lysp_node_anydata *)node)->musts;
1655 case LYS_NOTIF:
1656 return &((struct lysp_node_notif *)node)->musts;
1657 case LYS_INPUT:
1658 case LYS_OUTPUT:
1659 return &((struct lysp_node_action_inout *)node)->musts;
1660 default:
1661 return NULL;
1662 }
1663}
1664
1665struct lysp_restr *
1666lysp_node_musts(const struct lysp_node *node)
1667{
1668 struct lysp_restr **musts;
1669
1670 musts = lysp_node_musts_p(node);
1671 if (musts) {
1672 return *musts;
1673 } else {
1674 return NULL;
1675 }
1676}
1677
1678struct lysp_when **
1679lysp_node_when_p(const struct lysp_node *node)
1680{
1681 if (!node) {
1682 return NULL;
1683 }
1684
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001685 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001686 case LYS_CONTAINER:
1687 return &((struct lysp_node_container *)node)->when;
1688 case LYS_CHOICE:
1689 return &((struct lysp_node_choice *)node)->when;
1690 case LYS_LEAF:
1691 return &((struct lysp_node_leaf *)node)->when;
1692 case LYS_LEAFLIST:
1693 return &((struct lysp_node_leaflist *)node)->when;
1694 case LYS_LIST:
1695 return &((struct lysp_node_list *)node)->when;
1696 case LYS_ANYXML:
1697 case LYS_ANYDATA:
1698 return &((struct lysp_node_anydata *)node)->when;
1699 case LYS_CASE:
1700 return &((struct lysp_node_case *)node)->when;
1701 case LYS_USES:
1702 return &((struct lysp_node_uses *)node)->when;
1703 case LYS_AUGMENT:
1704 return &((struct lysp_node_augment *)node)->when;
1705 default:
1706 return NULL;
1707 }
1708}
1709
1710struct lysp_when *
1711lysp_node_when(const struct lysp_node *node)
1712{
1713 struct lysp_when **when;
1714
1715 when = lysp_node_when_p(node);
1716 if (when) {
1717 return *when;
1718 } else {
1719 return NULL;
1720 }
1721}
1722
Radek Krejci2a9fc652021-01-22 17:44:34 +01001723struct lysc_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001724lysc_node_actions_p(struct lysc_node *node)
1725{
1726 assert(node);
1727 switch (node->nodetype) {
1728 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001729 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001730 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001731 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001732 default:
1733 return NULL;
1734 }
1735}
1736
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001737LIBYANG_API_DEF const struct lysc_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001738lysc_node_actions(const struct lysc_node *node)
1739{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001740 struct lysc_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001741
Michal Vasko22df3f02020-08-24 13:29:22 +02001742 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001743 if (actions) {
1744 return *actions;
1745 } else {
1746 return NULL;
1747 }
1748}
1749
Radek Krejci2a9fc652021-01-22 17:44:34 +01001750struct lysc_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001751lysc_node_notifs_p(struct lysc_node *node)
1752{
1753 assert(node);
1754 switch (node->nodetype) {
1755 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001756 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001757 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001758 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001759 default:
1760 return NULL;
1761 }
1762}
1763
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001764LIBYANG_API_DEF const struct lysc_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001765lysc_node_notifs(const struct lysc_node *node)
1766{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001767 struct lysc_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001768
Michal Vasko22df3f02020-08-24 13:29:22 +02001769 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001770 if (notifs) {
1771 return *notifs;
1772 } else {
1773 return NULL;
1774 }
1775}
1776
Radek Krejcibbe09a92018-11-08 09:36:54 +01001777struct lysc_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001778lysc_node_child_p(const struct lysc_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001779{
Michal Vasko544e58a2021-01-28 14:33:41 +01001780 assert(node && !(node->nodetype & (LYS_RPC | LYS_ACTION)));
1781
Radek Krejcibbe09a92018-11-08 09:36:54 +01001782 switch (node->nodetype) {
1783 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001784 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001785 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001786 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001787 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001788 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001789 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001790 return &((struct lysc_node_list *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001791 case LYS_INPUT:
1792 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001793 return &((struct lysc_node_action_inout *)node)->child;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001794 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001795 return &((struct lysc_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001796 default:
1797 return NULL;
1798 }
1799}
1800
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001801LIBYANG_API_DEF const struct lysc_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001802lysc_node_child(const struct lysc_node *node)
Radek Krejcia3045382018-11-22 14:30:31 +01001803{
Michal Vasko544e58a2021-01-28 14:33:41 +01001804 struct lysc_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001805
1806 if (!node) {
1807 return NULL;
1808 }
1809
Michal Vasko544e58a2021-01-28 14:33:41 +01001810 if (node->nodetype & (LYS_RPC | LYS_ACTION)) {
1811 return &((struct lysc_node_action *)node)->input.node;
Radek Krejcibe154442021-01-21 11:06:36 +01001812 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01001813 child = lysc_node_child_p(node);
1814 if (child) {
1815 return *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001816 }
Michal Vasko2a668712020-10-21 11:48:09 +02001817 }
Michal Vasko544e58a2021-01-28 14:33:41 +01001818
1819 return NULL;
Michal Vasko2a668712020-10-21 11:48:09 +02001820}
1821
Radek Krejci9a3823e2021-01-27 20:26:46 +01001822struct lysc_must **
1823lysc_node_musts_p(const struct lysc_node *node)
1824{
1825 if (!node) {
1826 return NULL;
1827 }
1828
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001829 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001830 case LYS_CONTAINER:
1831 return &((struct lysc_node_container *)node)->musts;
1832 case LYS_LEAF:
1833 return &((struct lysc_node_leaf *)node)->musts;
1834 case LYS_LEAFLIST:
1835 return &((struct lysc_node_leaflist *)node)->musts;
1836 case LYS_LIST:
1837 return &((struct lysc_node_list *)node)->musts;
1838 case LYS_ANYXML:
1839 case LYS_ANYDATA:
1840 return &((struct lysc_node_anydata *)node)->musts;
1841 case LYS_NOTIF:
1842 return &((struct lysc_node_notif *)node)->musts;
1843 case LYS_INPUT:
1844 case LYS_OUTPUT:
1845 return &((struct lysc_node_action_inout *)node)->musts;
1846 default:
1847 return NULL;
1848 }
1849}
1850
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001851LIBYANG_API_DEF struct lysc_must *
Radek Krejci9a3823e2021-01-27 20:26:46 +01001852lysc_node_musts(const struct lysc_node *node)
1853{
1854 struct lysc_must **must_p;
1855
1856 must_p = lysc_node_musts_p(node);
1857 if (must_p) {
1858 return *must_p;
1859 } else {
1860 return NULL;
1861 }
1862}
1863
1864struct lysc_when ***
1865lysc_node_when_p(const struct lysc_node *node)
1866{
1867 if (!node) {
1868 return NULL;
1869 }
1870
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001871 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001872 case LYS_CONTAINER:
1873 return &((struct lysc_node_container *)node)->when;
1874 case LYS_CHOICE:
1875 return &((struct lysc_node_choice *)node)->when;
1876 case LYS_LEAF:
1877 return &((struct lysc_node_leaf *)node)->when;
1878 case LYS_LEAFLIST:
1879 return &((struct lysc_node_leaflist *)node)->when;
1880 case LYS_LIST:
1881 return &((struct lysc_node_list *)node)->when;
1882 case LYS_ANYXML:
1883 case LYS_ANYDATA:
1884 return &((struct lysc_node_anydata *)node)->when;
1885 case LYS_CASE:
1886 return &((struct lysc_node_case *)node)->when;
1887 case LYS_NOTIF:
1888 return &((struct lysc_node_notif *)node)->when;
1889 case LYS_RPC:
1890 case LYS_ACTION:
1891 return &((struct lysc_node_action *)node)->when;
1892 default:
1893 return NULL;
1894 }
1895}
1896
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001897LIBYANG_API_DEF struct lysc_when **
Radek Krejci9a3823e2021-01-27 20:26:46 +01001898lysc_node_when(const struct lysc_node *node)
1899{
1900 struct lysc_when ***when_p;
1901
1902 when_p = lysc_node_when_p(node);
1903 if (when_p) {
1904 return *when_p;
1905 } else {
1906 return NULL;
1907 }
1908}
1909
Radek Krejcid6b76452019-09-03 17:03:03 +02001910enum ly_stmt
Radek Krejcid54412f2020-12-17 20:25:35 +01001911lysp_match_kw(struct ly_in *in, uint64_t *indent)
David Sedlákc10e7902018-12-17 02:17:59 +01001912{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001913/**
Radek Krejcid54412f2020-12-17 20:25:35 +01001914 * @brief Move the input by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001915 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001916 *
1917 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001918 */
Radek Krejcid54412f2020-12-17 20:25:35 +01001919#define MOVE_IN(COUNT) \
1920 ly_in_skip(in, COUNT); \
1921 if (indent) { \
1922 (*indent)+=COUNT; \
1923 }
1924#define IF_KW(STR, LEN, STMT) \
1925 if (!strncmp(in->current, STR, LEN)) { \
1926 MOVE_IN(LEN); \
1927 (*kw)=STMT; \
1928 }
1929#define IF_KW_PREFIX(STR, LEN) \
1930 if (!strncmp(in->current, STR, LEN)) { \
1931 MOVE_IN(LEN);
1932#define IF_KW_PREFIX_END \
1933 }
David Sedlák572e7ab2019-06-04 16:01:58 +02001934
Michal Vasko63f3d842020-07-08 10:10:14 +02001935 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001936 enum ly_stmt result = LY_STMT_NONE;
1937 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001938 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001939 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001940 case 'a':
Radek Krejcid54412f2020-12-17 20:25:35 +01001941 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001942 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1943 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1944 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001945 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001946 IF_KW("data", 4, LY_STMT_ANYDATA)
1947 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001948 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001949 break;
1950 case 'b':
Radek Krejcid54412f2020-12-17 20:25:35 +01001951 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001952 IF_KW("ase", 3, LY_STMT_BASE)
1953 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1954 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001955 break;
1956 case 'c':
Radek Krejcid54412f2020-12-17 20:25:35 +01001957 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001958 IF_KW("ase", 3, LY_STMT_CASE)
1959 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001960 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001961 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001962 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001963 IF_KW("ct", 2, LY_STMT_CONTACT)
1964 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001965 IF_KW_PREFIX_END
1966 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001967 break;
1968 case 'd':
Radek Krejcid54412f2020-12-17 20:25:35 +01001969 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001970 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001971 IF_KW("fault", 5, LY_STMT_DEFAULT)
1972 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001973 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001974 IF_KW("e", 1, LY_STMT_DEVIATE)
1975 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001976 IF_KW_PREFIX_END
1977 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001978 break;
1979 case 'e':
Radek Krejcid54412f2020-12-17 20:25:35 +01001980 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001981 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001982 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001983 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1984 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001985 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001986 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001987 break;
1988 case 'f':
Radek Krejcid54412f2020-12-17 20:25:35 +01001989 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001990 IF_KW("eature", 6, LY_STMT_FEATURE)
1991 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001992 break;
1993 case 'g':
Radek Krejcid54412f2020-12-17 20:25:35 +01001994 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001995 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001996 break;
1997 case 'i':
Radek Krejcid54412f2020-12-17 20:25:35 +01001998 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001999 IF_KW("dentity", 7, LY_STMT_IDENTITY)
2000 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
2001 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002002 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02002003 IF_KW("clude", 5, LY_STMT_INCLUDE)
2004 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002005 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02002006 break;
2007 case 'k':
Radek Krejcid54412f2020-12-17 20:25:35 +01002008 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002009 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02002010 break;
2011 case 'l':
Radek Krejcid54412f2020-12-17 20:25:35 +01002012 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02002013 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02002014 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
2015 else IF_KW("af", 2, LY_STMT_LEAF)
2016 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002017 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002018 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02002019 break;
2020 case 'm':
Radek Krejcid54412f2020-12-17 20:25:35 +01002021 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02002022 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02002023 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
2024 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002025 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002026 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
2027 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002028 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02002029 IF_KW("ule", 3, LY_STMT_MODULE)
2030 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002031 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02002032 break;
2033 case 'n':
Radek Krejcid54412f2020-12-17 20:25:35 +01002034 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002035 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
2036 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02002037 break;
2038 case 'o':
Radek Krejcid54412f2020-12-17 20:25:35 +01002039 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02002040 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02002041 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
2042 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002043 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002044 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02002045 break;
2046 case 'p':
Radek Krejcid54412f2020-12-17 20:25:35 +01002047 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002048 IF_KW("ath", 3, LY_STMT_PATH)
2049 else IF_KW("attern", 6, LY_STMT_PATTERN)
2050 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002051 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02002052 IF_KW("fix", 3, LY_STMT_PREFIX)
2053 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002054 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02002055 break;
2056 case 'r':
Radek Krejcid54412f2020-12-17 20:25:35 +01002057 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002058 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002059 else IF_KW_PREFIX("e", 1)
2060 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02002061 IF_KW("erence", 6, LY_STMT_REFERENCE)
2062 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002063 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002064 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
2065 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
2066 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002067 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002068 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02002069 break;
2070 case 's':
Radek Krejcid54412f2020-12-17 20:25:35 +01002071 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002072 IF_KW("tatus", 5, LY_STMT_STATUS)
2073 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02002074 break;
2075 case 't':
Radek Krejcid54412f2020-12-17 20:25:35 +01002076 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002077 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
2078 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02002079 break;
2080 case 'u':
Radek Krejcid54412f2020-12-17 20:25:35 +01002081 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02002082 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02002083 IF_KW("que", 3, LY_STMT_UNIQUE)
2084 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002085 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002086 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02002087 break;
2088 case 'v':
Radek Krejcid54412f2020-12-17 20:25:35 +01002089 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002090 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02002091 break;
2092 case 'w':
Radek Krejcid54412f2020-12-17 20:25:35 +01002093 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002094 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02002095 break;
2096 case 'y':
Radek Krejcid54412f2020-12-17 20:25:35 +01002097 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002098 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
2099 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02002100 break;
David Sedlák23a59a62018-10-26 13:08:02 +02002101 default:
Radek Krejcid54412f2020-12-17 20:25:35 +01002102 /* if indent is not NULL we are matching keyword from YANG data */
2103 if (indent) {
Michal Vasko63f3d842020-07-08 10:10:14 +02002104 if (in->current[0] == ';') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002105 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002106 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02002107 } else if (in->current[0] == '{') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002108 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002109 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002110 } else if (in->current[0] == '}') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002111 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002112 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02002113 }
2114 }
David Sedlák23a59a62018-10-26 13:08:02 +02002115 break;
2116 }
2117
Michal Vasko63f3d842020-07-08 10:10:14 +02002118 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02002119 /* the keyword is not terminated */
2120 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002121 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02002122 }
2123
David Sedlák1bccdfa2019-06-17 15:55:27 +02002124#undef IF_KW
2125#undef IF_KW_PREFIX
2126#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01002127#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02002128 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01002129
David Sedlák1bccdfa2019-06-17 15:55:27 +02002130 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02002131}
David Sedlákecf5eb82019-06-03 14:12:44 +02002132
Radek Krejci85ac8312021-03-03 20:21:33 +01002133LY_ERR
2134lysp_ext_find_definition(const struct ly_ctx *ctx, const struct lysp_ext_instance *ext, const struct lys_module **ext_mod,
2135 struct lysp_ext **ext_def)
2136{
Radek Krejci85ac8312021-03-03 20:21:33 +01002137 const char *tmp, *name, *prefix;
2138 size_t pref_len, name_len;
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002139 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci85ac8312021-03-03 20:21:33 +01002140 const struct lys_module *mod = NULL;
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002141 const struct lysp_submodule *submod;
Radek Krejci85ac8312021-03-03 20:21:33 +01002142
Michal Vasko193dacd2022-10-13 08:43:05 +02002143 if (ext_def) {
2144 *ext_def = NULL;
Radek Krejci85ac8312021-03-03 20:21:33 +01002145 }
2146
Radek Krejci677abea2021-03-05 14:24:53 +01002147 /* parse the prefix, the nodeid was previously already parsed and checked */
Radek Krejci85ac8312021-03-03 20:21:33 +01002148 tmp = ext->name;
Radek Krejci677abea2021-03-05 14:24:53 +01002149 ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len);
Radek Krejci85ac8312021-03-03 20:21:33 +01002150
2151 /* get module where the extension definition should be placed */
Michal Vasko193dacd2022-10-13 08:43:05 +02002152 *ext_mod = mod = ly_resolve_prefix(ctx, prefix, pref_len, ext->format, ext->prefix_data);
Radek Krejci85ac8312021-03-03 20:21:33 +01002153 if (!mod) {
Radek Krejci422afb12021-03-04 16:38:16 +01002154 LOGVAL(ctx, LYVE_REFERENCE, "Invalid prefix \"%.*s\" used for extension instance identifier.", (int)pref_len, prefix);
Radek Krejci85ac8312021-03-03 20:21:33 +01002155 return LY_EVALID;
2156 } else if (!mod->parsed->extensions) {
2157 LOGVAL(ctx, LYVE_REFERENCE, "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
2158 ext->name, mod->name);
2159 return LY_EVALID;
2160 }
2161
Michal Vasko193dacd2022-10-13 08:43:05 +02002162 if (!ext_def) {
2163 /* we are done */
2164 return LY_SUCCESS;
2165 }
2166
Radek Krejci85ac8312021-03-03 20:21:33 +01002167 /* find the parsed extension definition there */
2168 LY_ARRAY_FOR(mod->parsed->extensions, v) {
2169 if (!strcmp(name, mod->parsed->extensions[v].name)) {
2170 *ext_def = &mod->parsed->extensions[v];
2171 break;
2172 }
2173 }
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002174 if (!*ext_def) {
2175 LY_ARRAY_FOR(mod->parsed->includes, u) {
2176 submod = mod->parsed->includes[u].submodule;
2177 LY_ARRAY_FOR(submod->extensions, v) {
2178 if (!strcmp(name, submod->extensions[v].name)) {
2179 *ext_def = &submod->extensions[v];
2180 break;
2181 }
2182 }
2183 }
2184 }
Radek Krejci85ac8312021-03-03 20:21:33 +01002185
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002186 if (!*ext_def) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002187 LOGVAL(ctx, LYVE_REFERENCE, "Extension definition of extension instance \"%s\" not found.", ext->name);
2188 return LY_EVALID;
2189 }
2190
Radek Krejci85ac8312021-03-03 20:21:33 +01002191 return LY_SUCCESS;
2192}
2193
2194LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002195lysp_ext_instance_resolve_argument(struct ly_ctx *ctx, struct lysp_ext_instance *ext_p)
Radek Krejci85ac8312021-03-03 20:21:33 +01002196{
Michal Vasko193dacd2022-10-13 08:43:05 +02002197 assert(ext_p->def);
2198
2199 if (!ext_p->def->argname || ext_p->argument) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002200 /* nothing to do */
2201 return LY_SUCCESS;
2202 }
2203
Radek Krejci8df109d2021-04-23 12:19:08 +02002204 if (ext_p->format == LY_VALUE_XML) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002205 /* schema was parsed from YIN and an argument is expected, ... */
Radek Krejci85ac8312021-03-03 20:21:33 +01002206 struct lysp_stmt *stmt = NULL;
2207
Michal Vasko193dacd2022-10-13 08:43:05 +02002208 if (ext_p->def->flags & LYS_YINELEM_TRUE) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002209 /* ... argument was the first XML child element */
2210 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {}
2211 if (stmt) {
2212 const char *arg, *ext, *name_arg, *name_ext, *prefix_arg, *prefix_ext;
2213 size_t name_arg_len, name_ext_len, prefix_arg_len, prefix_ext_len;
2214
2215 stmt = ext_p->child;
2216
2217 arg = stmt->stmt;
2218 ly_parse_nodeid(&arg, &prefix_arg, &prefix_arg_len, &name_arg, &name_arg_len);
Michal Vasko193dacd2022-10-13 08:43:05 +02002219 if (ly_strncmp(ext_p->def->argname, name_arg, name_arg_len)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002220 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" expects argument element \"%s\" as its first XML child, "
Michal Vasko193dacd2022-10-13 08:43:05 +02002221 "but \"%.*s\" element found.", ext_p->name, ext_p->def->argname, (int)name_arg_len, name_arg);
Radek Krejci85ac8312021-03-03 20:21:33 +01002222 return LY_EVALID;
2223 }
2224
2225 /* check namespace - all the extension instances must be qualified and argument element is expected in the same
2226 * namespace. Do not check just prefixes, there can be different prefixes pointing to the same namespace */
2227 ext = ext_p->name; /* include prefix */
2228 ly_parse_nodeid(&ext, &prefix_ext, &prefix_ext_len, &name_ext, &name_ext_len);
2229
2230 if (ly_resolve_prefix(ctx, prefix_ext, prefix_ext_len, ext_p->format, ext_p->prefix_data) !=
2231 ly_resolve_prefix(ctx, prefix_arg, prefix_arg_len, stmt->format, stmt->prefix_data)) {
2232 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" element and its argument element \"%s\" are "
Michal Vasko193dacd2022-10-13 08:43:05 +02002233 "expected in the same namespace, but they differ.", ext_p->name, ext_p->def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002234 return LY_EVALID;
2235 }
2236 }
2237 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +02002238 /* ... 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 +01002239 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002240 if (!strcmp(stmt->stmt, ext_p->def->argname)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002241 /* this is the extension's argument */
2242 break;
2243 }
2244 }
2245 }
2246
2247 if (stmt) {
2248 LY_CHECK_RET(lydict_insert(ctx, stmt->arg, 0, &ext_p->argument));
2249 stmt->flags |= LYS_YIN_ARGUMENT;
2250 }
2251 }
2252
2253 if (!ext_p->argument) {
2254 /* missing extension's argument */
Michal Vasko193dacd2022-10-13 08:43:05 +02002255 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" missing argument %s\"%s\".",
2256 ext_p->name, (ext_p->def->flags & LYS_YINELEM_TRUE) ? "element " : "", ext_p->def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002257 return LY_EVALID;
2258 }
2259
2260 return LY_SUCCESS;
2261}
2262
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002263LY_ARRAY_COUNT_TYPE
Radek Krejcifc596f92021-02-26 22:40:26 +01002264lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, enum ly_stmt substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002265{
2266 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
2267
Michal Vaskod989ba02020-08-24 10:59:24 +02002268 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejciab430862021-03-02 20:13:40 +01002269 if (ext[index].parent_stmt == substmt) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02002270 return index;
2271 }
2272 }
2273
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002274 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002275}
2276
Michal Vaskof8ded142022-02-17 10:48:01 +01002277LIBYANG_API_DEF const struct lysc_node *
Michal Vasko72244882021-01-12 15:21:05 +01002278lysc_data_node(const struct lysc_node *schema)
Michal Vasko62ed12d2020-05-21 10:08:25 +02002279{
2280 const struct lysc_node *parent;
2281
Michal Vasko72244882021-01-12 15:21:05 +01002282 parent = schema;
Radek Krejcidf549132021-01-21 10:32:32 +01002283 while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC |
2284 LYS_ACTION | LYS_NOTIF))) {
Radek Krejci7d95fbb2021-01-26 17:33:13 +01002285 parent = parent->parent;
Michal Vasko72244882021-01-12 15:21:05 +01002286 }
Michal Vasko62ed12d2020-05-21 10:08:25 +02002287
2288 return parent;
2289}
Michal Vaskof4258e12021-06-15 12:11:42 +02002290
2291ly_bool
2292lys_has_recompiled(const struct lys_module *mod)
2293{
2294 LY_ARRAY_COUNT_TYPE u;
2295
2296 if (LYSP_HAS_RECOMPILED(mod->parsed)) {
2297 return 1;
2298 }
2299
2300 LY_ARRAY_FOR(mod->parsed->includes, u) {
2301 if (LYSP_HAS_RECOMPILED(mod->parsed->includes[u].submodule)) {
2302 return 1;
2303 }
2304 }
2305
2306 return 0;
2307}
2308
2309ly_bool
2310lys_has_compiled(const struct lys_module *mod)
2311{
2312 LY_ARRAY_COUNT_TYPE u;
2313
2314 if (LYSP_HAS_COMPILED(mod->parsed)) {
2315 return 1;
2316 }
2317
2318 LY_ARRAY_FOR(mod->parsed->includes, u) {
2319 if (LYSP_HAS_COMPILED(mod->parsed->includes[u].submodule)) {
2320 return 1;
2321 }
2322 }
2323
2324 return 0;
2325}
Michal Vasko7ee5be22021-06-16 17:03:34 +02002326
2327ly_bool
Michal Vasko87cfdba2022-02-22 14:13:45 +01002328lys_has_dep_mods(const struct lys_module *mod)
Michal Vasko7ee5be22021-06-16 17:03:34 +02002329{
2330 LY_ARRAY_COUNT_TYPE u;
2331
Michal Vasko87cfdba2022-02-22 14:13:45 +01002332 /* features */
2333 if (mod->parsed->features) {
Michal Vasko7ee5be22021-06-16 17:03:34 +02002334 return 1;
2335 }
2336
Michal Vasko87cfdba2022-02-22 14:13:45 +01002337 /* groupings */
2338 if (mod->parsed->groupings) {
2339 return 1;
2340 }
Michal Vasko7ee5be22021-06-16 17:03:34 +02002341 LY_ARRAY_FOR(mod->parsed->includes, u) {
2342 if (mod->parsed->includes[u].submodule->groupings) {
2343 return 1;
2344 }
2345 }
2346
Michal Vasko87cfdba2022-02-22 14:13:45 +01002347 /* augments (adding nodes with leafrefs) */
2348 if (mod->parsed->augments) {
2349 return 1;
2350 }
2351 LY_ARRAY_FOR(mod->parsed->includes, u) {
2352 if (mod->parsed->includes[u].submodule->augments) {
2353 return 1;
2354 }
2355 }
2356
Michal Vasko7ee5be22021-06-16 17:03:34 +02002357 return 0;
2358}