blob: 67cbae67fdc461b745e7a6c0404066d9d387a808 [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
Michal Vasko69730152020-10-09 16:30:07 +020026#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "context.h"
Radek Krejci77114102021-03-10 15:21:57 +010028#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020029#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010030#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020031#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "log.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010033#include "ly_common.h"
Michal Vasko69730152020-10-09 16:30:07 +020034#include "parser_schema.h"
Michal Vasko510554a2023-09-11 09:03:29 +020035#include "path.h"
Michal Vasko962b6cd2020-12-08 10:07:49 +010036#include "schema_compile.h"
Michal Vasko79135ae2020-12-16 10:08:35 +010037#include "schema_features.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020038#include "set.h"
39#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010040#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020041#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020042#include "tree_schema_internal.h"
43
Radek Krejci85747952019-06-07 16:43:43 +020044LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +020045lysp_check_prefix(struct lysp_ctx *ctx, struct lysp_import *imports, const char *module_prefix, const char **value)
Radek Krejci86d106e2018-10-18 09:53:19 +020046{
47 struct lysp_import *i;
48
Michal Vasko69730152020-10-09 16:30:07 +020049 if (module_prefix && (&module_prefix != value) && !strcmp(module_prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010050 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value);
Radek Krejci86d106e2018-10-18 09:53:19 +020051 return LY_EEXIST;
52 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +010053 LY_ARRAY_FOR(imports, struct lysp_import, i) {
Michal Vasko69730152020-10-09 16:30:07 +020054 if (i->prefix && (&i->prefix != value) && !strcmp(i->prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010055 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +010056 return LY_EEXIST;
Radek Krejci86d106e2018-10-18 09:53:19 +020057 }
58 }
59 return LY_SUCCESS;
60}
61
62LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +020063lysp_check_date(struct lysp_ctx *ctx, const char *date, size_t date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +020064{
Radek Krejci86d106e2018-10-18 09:53:19 +020065 struct tm tm, tm_;
66 char *r;
67
Michal Vaskob425bb32022-11-08 10:49:36 +010068 LY_CHECK_ARG_RET(PARSER_CTX(ctx), date, LY_EINVAL);
69
70 if (date_len != LY_REV_SIZE - 1) {
71 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid length %" PRIu32 " of a date.", (uint32_t)date_len);
72 return LY_EINVAL;
73 }
Radek Krejci86d106e2018-10-18 09:53:19 +020074
Radek Krejcif13b87b2020-12-01 22:02:17 +010075 /* check format: YYYY-MM-DD */
Radek Krejci1deb5be2020-08-26 16:43:36 +020076 for (uint8_t i = 0; i < date_len; i++) {
Michal Vasko69730152020-10-09 16:30:07 +020077 if ((i == 4) || (i == 7)) {
Radek Krejci86d106e2018-10-18 09:53:19 +020078 if (date[i] != '-') {
79 goto error;
80 }
81 } else if (!isdigit(date[i])) {
82 goto error;
83 }
84 }
85
86 /* check content, e.g. 2018-02-31 */
87 memset(&tm, 0, sizeof tm);
88 r = strptime(date, "%Y-%m-%d", &tm);
Michal Vasko69730152020-10-09 16:30:07 +020089 if (!r || (r != &date[LY_REV_SIZE - 1])) {
Radek Krejci86d106e2018-10-18 09:53:19 +020090 goto error;
91 }
92 memcpy(&tm_, &tm, sizeof tm);
Michal Vasko82f32822022-08-17 10:54:35 +020093
94 /* DST may move the hour back resulting in a different day */
95 tm_.tm_hour = 1;
96
Radek Krejci86d106e2018-10-18 09:53:19 +020097 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
98 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
99 /* checking days is enough, since other errors
100 * have been checked by strptime() */
101 goto error;
102 }
103
104 return LY_SUCCESS;
105
106error:
Radek Krejcid33273d2018-10-25 14:55:52 +0200107 if (stmt) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200108 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)date_len, date, stmt);
Radek Krejcid33273d2018-10-25 14:55:52 +0200109 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200110 return LY_EINVAL;
111}
112
113void
114lysp_sort_revisions(struct lysp_revision *revs)
115{
Radek Krejci857189e2020-09-01 13:26:36 +0200116 LY_ARRAY_COUNT_TYPE i, r;
Radek Krejci86d106e2018-10-18 09:53:19 +0200117 struct lysp_revision rev;
118
Radek Krejcic7d13e32020-12-09 12:32:24 +0100119 for (i = 1, r = 0; i < LY_ARRAY_COUNT(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200120 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200121 r = i;
122 }
123 }
124
125 if (r) {
126 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200127 memcpy(&rev, &revs[0], sizeof rev);
128 memcpy(&revs[0], &revs[r], sizeof rev);
129 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200130 }
131}
Radek Krejci151a5b72018-10-19 14:21:44 +0200132
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200133LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200134lysp_check_enum_name(struct lysp_ctx *ctx, const char *name, size_t name_len)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100135{
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200136 if (!name_len) {
137 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
138 return LY_EVALID;
139 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
140 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
141 (int)name_len, name);
142 return LY_EVALID;
143 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200144 for (uint32_t u = 0; u < name_len; ++u) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200145 if (iscntrl(name[u])) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200146 LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided "
147 "(\"%.*s\", character number %" PRIu32 ").", (int)name_len, name, u + 1);
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200148 break;
149 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100150 }
151 }
152
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200153 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100154}
155
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200156/**
157 * @brief Learn built-in type from its name.
158 *
159 * @param[in] name Type name.
160 * @param[in] len Length of @p name.
161 * @return Built-in data type, ::LY_TYPE_UNKNOWN if none matches.
162 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100163static LY_DATA_TYPE
164lysp_type_str2builtin(const char *name, size_t len)
165{
166 if (len >= 4) { /* otherwise it does not match any built-in type */
167 if (name[0] == 'b') {
168 if (name[1] == 'i') {
Michal Vasko69730152020-10-09 16:30:07 +0200169 if ((len == 6) && !strncmp(&name[2], "nary", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100170 return LY_TYPE_BINARY;
Michal Vasko69730152020-10-09 16:30:07 +0200171 } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100172 return LY_TYPE_BITS;
173 }
Michal Vasko69730152020-10-09 16:30:07 +0200174 } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100175 return LY_TYPE_BOOL;
176 }
177 } else if (name[0] == 'd') {
Michal Vasko69730152020-10-09 16:30:07 +0200178 if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100179 return LY_TYPE_DEC64;
180 }
181 } else if (name[0] == 'e') {
Michal Vasko69730152020-10-09 16:30:07 +0200182 if ((len == 5) && !strncmp(&name[1], "mpty", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100183 return LY_TYPE_EMPTY;
Michal Vasko69730152020-10-09 16:30:07 +0200184 } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100185 return LY_TYPE_ENUM;
186 }
187 } else if (name[0] == 'i') {
188 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200189 if ((len == 4) && !strncmp(&name[2], "t8", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100190 return LY_TYPE_INT8;
191 } else if (len == 5) {
192 if (!strncmp(&name[2], "t16", 3)) {
193 return LY_TYPE_INT16;
194 } else if (!strncmp(&name[2], "t32", 3)) {
195 return LY_TYPE_INT32;
196 } else if (!strncmp(&name[2], "t64", 3)) {
197 return LY_TYPE_INT64;
198 }
Michal Vasko69730152020-10-09 16:30:07 +0200199 } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100200 return LY_TYPE_INST;
201 }
Michal Vasko69730152020-10-09 16:30:07 +0200202 } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100203 return LY_TYPE_IDENT;
204 }
205 } else if (name[0] == 'l') {
Michal Vasko69730152020-10-09 16:30:07 +0200206 if ((len == 7) && !strncmp(&name[1], "eafref", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100207 return LY_TYPE_LEAFREF;
208 }
209 } else if (name[0] == 's') {
Michal Vasko69730152020-10-09 16:30:07 +0200210 if ((len == 6) && !strncmp(&name[1], "tring", 5)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100211 return LY_TYPE_STRING;
212 }
213 } else if (name[0] == 'u') {
214 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200215 if ((len == 5) && !strncmp(&name[2], "ion", 3)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100216 return LY_TYPE_UNION;
217 }
Michal Vasko69730152020-10-09 16:30:07 +0200218 } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) {
219 if ((len == 5) && (name[4] == '8')) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100220 return LY_TYPE_UINT8;
221 } else if (len == 6) {
222 if (!strncmp(&name[4], "16", 2)) {
223 return LY_TYPE_UINT16;
224 } else if (!strncmp(&name[4], "32", 2)) {
225 return LY_TYPE_UINT32;
226 } else if (!strncmp(&name[4], "64", 2)) {
227 return LY_TYPE_UINT64;
228 }
229 }
230 }
231 }
232 }
233
234 return LY_TYPE_UNKNOWN;
235}
236
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200237/**
238 * @brief Find a typedef in a sized array.
239 *
240 * @param[in] name Typedef name.
241 * @param[in] typedefs Sized array of typedefs.
242 * @return Found typedef, NULL if none.
243 */
244static const struct lysp_tpdf *
245lysp_typedef_match(const char *name, const struct lysp_tpdf *typedefs)
246{
247 LY_ARRAY_COUNT_TYPE u;
248
249 LY_ARRAY_FOR(typedefs, u) {
250 if (!strcmp(name, typedefs[u].name)) {
251 /* match */
252 return &typedefs[u];
253 }
254 }
255 return NULL;
256}
257
Radek Krejcibbe09a92018-11-08 09:36:54 +0100258LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +0100259lysp_type_find(const char *id, struct lysp_node *start_node, const struct lysp_module *start_module,
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200260 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 +0100261{
262 const char *str, *name;
263 struct lysp_tpdf *typedefs;
Michal Vasko193dacd2022-10-13 08:43:05 +0200264 const struct lysp_tpdf *ext_typedefs;
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100265 const struct lys_module *mod;
Michal Vaskoa99b3572021-02-01 11:54:58 +0100266 const struct lysp_module *local_module;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200267 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100268
269 assert(id);
270 assert(start_module);
271 assert(tpdf);
272 assert(node);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100273
Radek Krejci4f28eda2018-11-12 11:46:16 +0100274 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100275 str = strchr(id, ':');
276 if (str) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200277 mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_VALUE_SCHEMA, (void *)start_module);
Michal Vaskoa99b3572021-02-01 11:54:58 +0100278 local_module = mod ? mod->parsed : NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100279 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100280 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100281 } else {
Michal Vaskoa99b3572021-02-01 11:54:58 +0100282 local_module = start_module;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100283 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100284
285 /* check for built-in types */
286 *type = lysp_type_str2builtin(name, strlen(name));
287 if (*type) {
288 *tpdf = NULL;
289 return LY_SUCCESS;
290 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100291 }
Michal Vaskoa99b3572021-02-01 11:54:58 +0100292 LY_CHECK_RET(!local_module, LY_ENOTFOUND);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100293
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200294 if (local_module == start_module) {
295 if (start_node) {
296 /* search typedefs in parent's nodes */
297 for (*node = start_node; *node; *node = (*node)->parent) {
298 *tpdf = lysp_typedef_match(name, lysp_node_typedefs(*node));
299 if (*tpdf) {
300 /* match */
301 return LY_SUCCESS;
302 }
303 }
304 }
305
306 if (ext) {
307 /* search typedefs directly in the extension */
Michal Vaskofbd037c2022-11-08 10:34:20 +0100308 lyplg_ext_parsed_get_storage(ext, LY_STMT_TYPEDEF, sizeof ext_typedefs, (const void **)&ext_typedefs);
Michal Vasko193dacd2022-10-13 08:43:05 +0200309 if ((*tpdf = lysp_typedef_match(name, ext_typedefs))) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100310 /* match */
311 return LY_SUCCESS;
312 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100313 }
314 }
315
Michal Vasko915e5442021-06-08 14:59:21 +0200316 /* go to main module if in submodule */
317 local_module = local_module->mod->parsed;
318
Radek Krejcibbe09a92018-11-08 09:36:54 +0100319 /* search in top-level typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100320 if (local_module->typedefs) {
321 LY_ARRAY_FOR(local_module->typedefs, u) {
322 if (!strcmp(name, local_module->typedefs[u].name)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100323 /* match */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100324 *tpdf = &local_module->typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100325 return LY_SUCCESS;
326 }
327 }
328 }
329
Michal Vasko915e5442021-06-08 14:59:21 +0200330 /* search in all submodules' typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100331 LY_ARRAY_FOR(local_module->includes, u) {
332 typedefs = local_module->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100333 LY_ARRAY_FOR(typedefs, v) {
334 if (!strcmp(name, typedefs[v].name)) {
335 /* match */
336 *tpdf = &typedefs[v];
337 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100338 }
339 }
340 }
341
342 return LY_ENOTFOUND;
343}
344
Michal Vaskob36053d2020-03-26 15:49:30 +0100345/**
aPiecekdc12b9f2021-06-25 10:55:47 +0200346 * @brief Insert @p name to hash table and if @p name has already
347 * been added, then log an error.
348 *
349 * This function is used to detect duplicate names.
350 *
351 * @param[in,out] ctx Context to log the error.
352 * @param[in,out] ht Hash table with top-level names.
353 * @param[in] name Inserted top-level identifier.
Michal Vasko8efac242023-03-30 08:24:56 +0200354 * @param[in] statement The name of the statement type from which @p name originated (eg typedef, feature, ...).
aPiecekdc12b9f2021-06-25 10:55:47 +0200355 * @param[in] err_detail Optional error specification.
356 * @return LY_ERR, but LY_EEXIST is mapped to LY_EVALID.
357 */
358static LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200359lysp_check_dup_ht_insert(struct lysp_ctx *ctx, struct ly_ht *ht, const char *name, const char *statement,
360 const char *err_detail)
aPiecekdc12b9f2021-06-25 10:55:47 +0200361{
362 LY_ERR ret;
363 uint32_t hash;
364
Michal Vaskoae130f52023-04-20 14:25:16 +0200365 hash = lyht_hash(name, strlen(name));
aPiecekdc12b9f2021-06-25 10:55:47 +0200366 ret = lyht_insert(ht, &name, hash, NULL);
367 if (ret == LY_EEXIST) {
368 if (err_detail) {
369 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT2, name, statement, err_detail);
370 } else {
371 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, name, statement);
372 }
373 ret = LY_EVALID;
374 }
375
376 return ret;
377}
378
379/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100380 * @brief Check name of a new type to avoid name collisions.
381 *
382 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
383 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
384 * @param[in] tpdf Typedef definition to check.
385 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
aPiecekc7594b72021-06-29 09:00:03 +0200386 * typedefs are checked, caller is supposed to free the table.
387 * @return LY_EVALID in case of collision, LY_SUCCESS otherwise.
Radek Krejcibbe09a92018-11-08 09:36:54 +0100388 */
389static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200390lysp_check_dup_typedef(struct lysp_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
Michal Vasko8efac242023-03-30 08:24:56 +0200391 struct ly_ht *tpdfs_global)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100392{
393 struct lysp_node *parent;
394 uint32_t hash;
395 size_t name_len;
396 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200397 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100398 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100399
400 assert(ctx);
401 assert(tpdf);
402
403 name = tpdf->name;
404 name_len = strlen(name);
405
Radek Krejci4f28eda2018-11-12 11:46:16 +0100406 if (lysp_type_str2builtin(name, name_len)) {
aPiecekc7594b72021-06-29 09:00:03 +0200407 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
408 "Duplicate identifier \"%s\" of typedef statement - name collision with a built-in type.", name);
409 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100410 }
411
412 /* check locally scoped typedefs (avoid name shadowing) */
413 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100414 typedefs = lysp_node_typedefs(node);
415 LY_ARRAY_FOR(typedefs, u) {
416 if (&typedefs[u] == tpdf) {
417 break;
418 }
419 if (!strcmp(name, typedefs[u].name)) {
aPiecekc7594b72021-06-29 09:00:03 +0200420 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
421 "Duplicate identifier \"%s\" of typedef statement - name collision with sibling type.", name);
422 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100423 }
424 }
425 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200426 for (parent = node->parent; parent; parent = parent->parent) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200427 if (lysp_typedef_match(name, lysp_node_typedefs(parent))) {
aPiecekc7594b72021-06-29 09:00:03 +0200428 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
429 "Duplicate identifier \"%s\" of typedef statement - name collision with another scoped type.", name);
430 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100431 }
432 }
433 }
434
435 /* check collision with the top-level typedefs */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100436 if (node) {
Michal Vaskoae130f52023-04-20 14:25:16 +0200437 hash = lyht_hash(name, name_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100438 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
aPiecekc7594b72021-06-29 09:00:03 +0200439 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
440 "Duplicate identifier \"%s\" of typedef statement - scoped type collide with a top-level type.", name);
441 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100442 }
443 } else {
aPiecekc7594b72021-06-29 09:00:03 +0200444 LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, tpdfs_global, name, "typedef",
445 "name collision with another top-level type"));
Radek Krejci3b1f9292018-11-08 10:58:35 +0100446 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
447 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
448 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100449 }
450
451 return LY_SUCCESS;
452}
453
Radek Krejci857189e2020-09-01 13:26:36 +0200454/**
455 * @brief Compare identifiers.
Michal Vasko62524a92021-02-26 10:08:50 +0100456 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200457 */
458static ly_bool
459lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Radek Krejcibbe09a92018-11-08 09:36:54 +0100460{
Michal Vasko11ac39a2021-07-23 12:46:56 +0200461 char *id1, *id2;
462
463 id1 = *(char **)val1;
464 id2 = *(char **)val2;
465
466 return strcmp(id1, id2) == 0 ? 1 : 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100467}
468
469LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200470lysp_check_dup_typedefs(struct lysp_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100471{
Michal Vasko8efac242023-03-30 08:24:56 +0200472 struct ly_ht *ids_global;
Radek Krejci0fb28562018-12-13 15:17:37 +0100473 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200474 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200475 uint32_t i;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100476 LY_ERR ret = LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100477
478 /* check name collisions - typedefs and groupings */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100479 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200480 LY_ARRAY_FOR(mod->typedefs, v) {
aPieceke1fbd952021-06-29 08:12:55 +0200481 ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100482 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100483 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200484 LY_ARRAY_FOR(mod->includes, v) {
485 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200486 ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100487 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci3b1f9292018-11-08 10:58:35 +0100488 }
489 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200490 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
491 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
492 LY_ARRAY_FOR(typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200493 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 +0100494 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100495 }
496 }
Michal Vasko405cc9e2020-12-01 12:01:27 +0100497
Radek Krejcibbe09a92018-11-08 09:36:54 +0100498cleanup:
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100499 lyht_free(ids_global, NULL);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100500 return ret;
501}
502
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200503static const struct lysp_node_grp *
504lysp_grouping_match(const char *name, struct lysp_node *node)
505{
506 const struct lysp_node_grp *groupings, *grp_iter;
507
508 groupings = lysp_node_groupings(node);
509 LY_LIST_FOR(groupings, grp_iter) {
510 if (!strcmp(name, grp_iter->name)) {
511 /* match */
512 return grp_iter;
513 }
514 }
515
516 return NULL;
517}
518
aPiecek63e080d2021-06-29 13:53:28 +0200519/**
520 * @brief Check name of a new grouping to avoid name collisions.
521 *
522 * @param[in] ctx Parser context, module where the grouping is being defined is taken from here.
523 * @param[in] node Schema node where the grouping is being defined, NULL in case of a top-level grouping.
524 * @param[in] grp Grouping definition to check.
525 * @param[in,out] grps_global Initialized hash table to store temporary data between calls. When the module's
526 * groupings are checked, caller is supposed to free the table.
527 * @return LY_EVALID in case of collision, LY_SUCCESS otherwise.
528 */
529static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200530lysp_check_dup_grouping(struct lysp_ctx *ctx, struct lysp_node *node, const struct lysp_node_grp *grp,
Michal Vasko8efac242023-03-30 08:24:56 +0200531 struct ly_ht *grps_global)
aPiecek63e080d2021-06-29 13:53:28 +0200532{
533 struct lysp_node *parent;
534 uint32_t hash;
535 size_t name_len;
536 const char *name;
537 const struct lysp_node_grp *groupings, *grp_iter;
538
539 assert(ctx);
540 assert(grp);
541
542 name = grp->name;
543 name_len = strlen(name);
544
545 /* check locally scoped groupings (avoid name shadowing) */
546 if (node) {
547 groupings = lysp_node_groupings(node);
548 LY_LIST_FOR(groupings, grp_iter) {
549 if (grp_iter == grp) {
550 break;
551 }
552 if (!strcmp(name, grp_iter->name)) {
553 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
554 "Duplicate identifier \"%s\" of grouping statement - name collision with sibling grouping.", name);
555 return LY_EVALID;
556 }
557 }
558 /* search grouping in parent's nodes */
559 for (parent = node->parent; parent; parent = parent->parent) {
560 if (lysp_grouping_match(name, parent)) {
561 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
562 "Duplicate identifier \"%s\" of grouping statement - name collision with another scoped grouping.", name);
563 return LY_EVALID;
564 }
565 }
566 }
567
568 /* check collision with the top-level groupings */
569 if (node) {
Michal Vaskoae130f52023-04-20 14:25:16 +0200570 hash = lyht_hash(name, name_len);
aPiecek63e080d2021-06-29 13:53:28 +0200571 if (!lyht_find(grps_global, &name, hash, NULL)) {
572 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
573 "Duplicate identifier \"%s\" of grouping statement - scoped grouping collide with a top-level grouping.", name);
574 return LY_EVALID;
575 }
576 } else {
577 LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, grps_global, name, "grouping",
578 "name collision with another top-level grouping"));
579 }
580
581 return LY_SUCCESS;
582}
583
584LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200585lysp_check_dup_groupings(struct lysp_ctx *ctx, struct lysp_module *mod)
aPiecek63e080d2021-06-29 13:53:28 +0200586{
Michal Vasko8efac242023-03-30 08:24:56 +0200587 struct ly_ht *ids_global;
aPiecek63e080d2021-06-29 13:53:28 +0200588 const struct lysp_node_grp *groupings, *grp_iter;
589 LY_ARRAY_COUNT_TYPE u;
590 uint32_t i;
591 LY_ERR ret = LY_SUCCESS;
592
593 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
594 LY_LIST_FOR(mod->groupings, grp_iter) {
595 ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global);
596 LY_CHECK_GOTO(ret, cleanup);
597 }
598 LY_ARRAY_FOR(mod->includes, u) {
599 LY_LIST_FOR(mod->includes[u].submodule->groupings, grp_iter) {
600 ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global);
601 LY_CHECK_GOTO(ret, cleanup);
602 }
603 }
604 for (i = 0; i < ctx->grps_nodes.count; ++i) {
605 groupings = lysp_node_groupings((struct lysp_node *)ctx->grps_nodes.objs[i]);
606 LY_LIST_FOR(groupings, grp_iter) {
607 ret = lysp_check_dup_grouping(ctx, (struct lysp_node *)ctx->grps_nodes.objs[i], grp_iter, ids_global);
608 LY_CHECK_GOTO(ret, cleanup);
609 }
610 }
611
612cleanup:
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100613 lyht_free(ids_global, NULL);
aPiecek63e080d2021-06-29 13:53:28 +0200614 return ret;
615}
616
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100617static ly_bool
618ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
619{
620 void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
621
622 return ptr1 == ptr2 ? 1 : 0;
623}
624
625LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200626lysp_check_dup_features(struct lysp_ctx *ctx, struct lysp_module *mod)
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100627{
628 LY_ARRAY_COUNT_TYPE u;
Michal Vasko8efac242023-03-30 08:24:56 +0200629 struct ly_ht *ht;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100630 struct lysp_feature *f;
aPiecekdc12b9f2021-06-25 10:55:47 +0200631 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100632
aPiecekf6203072021-06-25 10:58:26 +0200633 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100634 LY_CHECK_RET(!ht, LY_EMEM);
635
636 /* add all module features into a hash table */
637 LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200638 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
639 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200640 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100641 }
642
643 /* add all submodule features into a hash table */
644 LY_ARRAY_FOR(mod->includes, u) {
645 LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200646 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
647 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200648 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100649 }
650 }
651
652cleanup:
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100653 lyht_free(ht, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100654 return ret;
655}
656
657LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200658lysp_check_dup_identities(struct lysp_ctx *ctx, struct lysp_module *mod)
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100659{
660 LY_ARRAY_COUNT_TYPE u;
Michal Vasko8efac242023-03-30 08:24:56 +0200661 struct ly_ht *ht;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100662 struct lysp_ident *i;
aPiecekdc12b9f2021-06-25 10:55:47 +0200663 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100664
aPiecekf6203072021-06-25 10:58:26 +0200665 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100666 LY_CHECK_RET(!ht, LY_EMEM);
667
668 /* add all module identities into a hash table */
669 LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200670 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
671 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200672 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100673 }
674
675 /* add all submodule identities into a hash table */
676 LY_ARRAY_FOR(mod->includes, u) {
677 LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200678 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
679 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200680 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100681 }
682 }
683
684cleanup:
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100685 lyht_free(ht, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100686 return ret;
687}
688
Radek Krejci9ed7a192018-10-31 16:23:51 +0100689struct lysp_load_module_check_data {
690 const char *name;
691 const char *revision;
692 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200693 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100694};
695
696static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100697lysp_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 +0100698{
699 struct lysp_load_module_check_data *info = data;
Jan Kundrát026737b2023-09-05 13:03:30 +0200700 const char *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200701 uint8_t latest_revision;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100702 struct lysp_revision *revs;
703
704 name = mod ? mod->mod->name : submod->name;
705 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200706 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100707
708 if (info->name) {
709 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100710 if (strcmp(info->name, name)) {
711 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100712 return LY_EINVAL;
713 }
714 }
715 if (info->revision) {
716 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100717 if (!revs || strcmp(info->revision, revs[0].date)) {
718 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200719 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100720 return LY_EINVAL;
721 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200722 } else if (!latest_revision) {
723 /* do not log, we just need to drop the schema and use the latest revision from the context */
724 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100725 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100726 if (submod) {
727 assert(info->submoduleof);
728
Radek Krejci9ed7a192018-10-31 16:23:51 +0100729 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200730 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100731 LOGVAL(ctx, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200732 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100733 return LY_EVALID;
734 }
735 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100736 if (submod->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100737 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100738 return LY_EVALID;
739 }
740 }
741 if (info->path) {
Jan Kundrát026737b2023-09-05 13:03:30 +0200742 ly_check_module_filename(ctx, name, revs ? revs[0].date : NULL, info->path);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100743 }
744 return LY_SUCCESS;
745}
746
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200747/**
Michal Vasko4e205e82021-06-08 14:01:47 +0200748 * @brief Parse a (sub)module from a local file and add into the context.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200749 *
750 * This function does not check the presence of the (sub)module in context, it should be done before calling this function.
751 *
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200752 * @param[in] ctx libyang context where to work.
753 * @param[in] name Name of the (sub)module to load.
754 * @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 +0200755 * @param[in] main_ctx Parser context of the main module in case of loading submodule.
756 * @param[in] main_name Main module name in case of loading submodule.
757 * @param[in] required Module is required so error (even if the input file not found) are important. If 0, there is some
758 * 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 +0200759 * @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 +0200760 * @param[out] result Parsed YANG schema tree of the requested module (struct lys_module*) or submodule (struct lysp_submodule*).
761 * If it is a module, it is already in the context!
Michal Vasko4e205e82021-06-08 14:01:47 +0200762 * @return LY_SUCCESS on success.
Michal Vasko4e205e82021-06-08 14:01:47 +0200763 * @return LY_ERR on error.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200764 */
765static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200766lys_parse_localfile(struct ly_ctx *ctx, const char *name, const char *revision, struct lysp_ctx *main_ctx,
Michal Vaskodd992582021-06-10 14:34:57 +0200767 const char *main_name, ly_bool required, struct ly_set *new_mods, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100768{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200769 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100770 char *filepath = NULL;
771 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100772 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100773 LY_ERR ret = LY_SUCCESS;
774 struct lysp_load_module_check_data check_data = {0};
775
Michal Vasko87f1cf02021-06-08 14:02:47 +0200776 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name,
777 revision, &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200778 if (!filepath) {
779 if (required) {
780 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200781 revision ? revision : "");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200782 }
783 return LY_ENOTFOUND;
784 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100785
786 LOGVRB("Loading schema from \"%s\" file.", filepath);
787
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200788 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200789 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +0200790 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100791 check_data.name = name;
792 check_data.revision = revision;
793 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800794 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200795 if (main_ctx) {
aPiecekc3e26142021-06-22 14:25:49 +0200796 ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data, new_mods,
Michal Vasko69730152020-10-09 16:30:07 +0200797 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200798 } else {
Michal Vaskodd992582021-06-10 14:34:57 +0200799 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 +0200800
801 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200802 ly_in_free(in, 1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200803 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100804
805 *result = mod;
806
807 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200808
Radek Krejci9ed7a192018-10-31 16:23:51 +0100809cleanup:
810 free(filepath);
811 return ret;
812}
813
aPiecek4725aea2021-07-28 10:18:33 +0200814/**
815 * @brief Load module from searchdirs or from callback.
816 *
817 * @param[in] ctx libyang context where to work.
818 * @param[in] name Name of module to load.
819 * @param[in] revision Revision of module to load.
Michal Vaskobdac23f2022-01-12 14:02:35 +0100820 * @param[in] mod_latest Module with the latest revision found in context, otherwise set to NULL.
821 * @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 +0200822 * @param[out] mod Loaded module.
823 * @return LY_SUCCESS on success.
824 * @return LY_ERR on error.
825 */
826static LY_ERR
827lys_parse_load_from_clb_or_file(struct ly_ctx *ctx, const char *name, const char *revision,
828 struct lys_module *mod_latest, struct ly_set *new_mods, struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200829{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100830 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200831 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200832
Radek Krejci9ed7a192018-10-31 16:23:51 +0100833 void (*module_data_free)(void *module_data, void *user_data) = NULL;
834 struct lysp_load_module_check_data check_data = {0};
Michal Vasko63f3d842020-07-08 10:10:14 +0200835 struct ly_in *in;
Radek Krejci086c7132018-10-26 15:29:04 +0200836
Michal Vaskobdac23f2022-01-12 14:02:35 +0100837 *mod = NULL;
838
839 if (mod_latest && (!ctx->imp_clb || (mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB)) &&
840 ((ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) || (mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) {
841 /* we are not able to find a newer revision */
842 return LY_SUCCESS;
843 }
844
aPiecek4725aea2021-07-28 10:18:33 +0200845 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
846search_clb:
Michal Vaskobdac23f2022-01-12 14:02:35 +0100847 /* check there is a callback and should be called */
848 if (ctx->imp_clb && (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB))) {
849 if (!ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, &format, &module_data, &module_data_free)) {
850 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
851 check_data.name = name;
852 check_data.revision = revision;
853 lys_parse_in(ctx, in, format, lysp_load_module_check, &check_data, new_mods, mod);
854 ly_in_free(in, 0);
855 if (module_data_free) {
856 module_data_free((void *)module_data, ctx->imp_clb_data);
857 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200858 }
Radek Krejci0af46292019-01-11 16:02:31 +0100859 }
Michal Vaskobdac23f2022-01-12 14:02:35 +0100860 if (*mod && !revision) {
861 /* we got the latest revision module from the callback */
862 (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB;
863 } else if (!*mod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
aPiecek4725aea2021-07-28 10:18:33 +0200864 goto search_file;
865 }
866 } else {
867search_file:
Michal Vaskobdac23f2022-01-12 14:02:35 +0100868 /* check we can use searchdirs and that we should */
869 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) &&
870 (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) {
aPiecek4725aea2021-07-28 10:18:33 +0200871 lys_parse_localfile(ctx, name, revision, NULL, NULL, mod_latest ? 0 : 1, new_mods, (void **)mod);
872 }
Michal Vaskobdac23f2022-01-12 14:02:35 +0100873 if (*mod && !revision) {
874 /* we got the latest revision module in the searchdirs */
875 (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB;
876 } else if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
aPiecek4725aea2021-07-28 10:18:33 +0200877 goto search_clb;
878 }
Radek Krejci086c7132018-10-26 15:29:04 +0200879 }
880
aPiecek4725aea2021-07-28 10:18:33 +0200881 return LY_SUCCESS;
882}
883
884/**
aPiecekd4911ee2021-07-30 07:40:24 +0200885 * @brief Get module without revision according to priorities.
886 *
887 * 1. Search for the module with LYS_MOD_IMPORTED_REV.
888 * 2. Search for the implemented module.
889 * 3. Search for the latest module in the context.
890 *
891 * @param[in] ctx libyang context where module is searched.
892 * @param[in] name Name of the searched module.
aPiecekd4911ee2021-07-30 07:40:24 +0200893 * @return Found module from context or NULL.
894 */
895static struct lys_module *
Michal Vaskobdac23f2022-01-12 14:02:35 +0100896lys_get_module_without_revision(struct ly_ctx *ctx, const char *name)
aPiecekd4911ee2021-07-30 07:40:24 +0200897{
898 struct lys_module *mod, *mod_impl;
899 uint32_t index;
900
Michal Vaskocfcfd672023-02-14 11:20:42 +0100901 /* try to find module with LYS_MOD_IMPORTED_REV flag */
aPiecekd4911ee2021-07-30 07:40:24 +0200902 index = 0;
903 while ((mod = ly_ctx_get_module_iter(ctx, &index))) {
904 if (!strcmp(mod->name, name) && (mod->latest_revision & LYS_MOD_IMPORTED_REV)) {
905 break;
906 }
907 }
908
Michal Vaskocfcfd672023-02-14 11:20:42 +0100909 /* try to find the implemented module */
aPiecekd4911ee2021-07-30 07:40:24 +0200910 mod_impl = ly_ctx_get_module_implemented(ctx, name);
Michal Vaskocfcfd672023-02-14 11:20:42 +0100911 if (mod) {
912 if (mod_impl && (mod != mod_impl)) {
913 LOGVRB("Implemented module \"%s@%s\" is not used for import, revision \"%s\" is imported instead.",
914 mod_impl->name, mod_impl->revision, mod->revision);
915 }
aPiecekd4911ee2021-07-30 07:40:24 +0200916 return mod;
917 } else if (mod_impl) {
918 return mod_impl;
919 }
920
Michal Vaskocfcfd672023-02-14 11:20:42 +0100921 /* try to find the latest module in the current context */
aPiecekd4911ee2021-07-30 07:40:24 +0200922 mod = ly_ctx_get_module_latest(ctx, name);
aPiecekd4911ee2021-07-30 07:40:24 +0200923
924 return mod;
925}
926
927/**
aPiecek4725aea2021-07-28 10:18:33 +0200928 * @brief Check if a circular dependency exists between modules.
929 *
930 * @param[in] ctx libyang context for log an error.
Michal Vaskocfcfd672023-02-14 11:20:42 +0100931 * @param[in,out] mod Examined module which is set to NULL if the circular dependency is detected.
932 * @return LY_SUCCESS if no circular dependecy is detected, otherwise LY_EVALID.
aPiecek4725aea2021-07-28 10:18:33 +0200933 */
934static LY_ERR
935lys_check_circular_dependency(struct ly_ctx *ctx, struct lys_module **mod)
936{
937 if ((*mod) && (*mod)->parsed->parsing) {
938 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", (*mod)->name);
Michal Vasko4e205e82021-06-08 14:01:47 +0200939 *mod = NULL;
940 return LY_EVALID;
Michal Vasko0550b762020-11-24 18:04:08 +0100941 }
Radek Krejci086c7132018-10-26 15:29:04 +0200942
aPiecek4725aea2021-07-28 10:18:33 +0200943 return LY_SUCCESS;
944}
945
946LY_ERR
947lys_parse_load(struct ly_ctx *ctx, const char *name, const char *revision, struct ly_set *new_mods,
948 struct lys_module **mod)
949{
950 struct lys_module *mod_latest = NULL;
951
952 assert(mod && new_mods);
953
Michal Vasko0550b762020-11-24 18:04:08 +0100954 /*
aPiecek4725aea2021-07-28 10:18:33 +0200955 * Try to get the module from the context.
Michal Vasko0550b762020-11-24 18:04:08 +0100956 */
aPiecek4725aea2021-07-28 10:18:33 +0200957 if (revision) {
958 /* Get the specific revision. */
959 *mod = ly_ctx_get_module(ctx, name, revision);
960 } else {
Michal Vaskobdac23f2022-01-12 14:02:35 +0100961 /* Get the requested module in a suitable revision in the context. */
962 *mod = lys_get_module_without_revision(ctx, name);
963 if (*mod && !(*mod)->implemented && !((*mod)->latest_revision & LYS_MOD_IMPORTED_REV)) {
aPiecek4725aea2021-07-28 10:18:33 +0200964 /* Let us now search with callback and searchpaths to check
965 * if there is newer revision outside the context.
966 */
967 mod_latest = *mod;
968 *mod = NULL;
969 }
970 }
971
Michal Vasko0550b762020-11-24 18:04:08 +0100972 if (!*mod) {
aPiecek4725aea2021-07-28 10:18:33 +0200973 /* No suitable module in the context, try to load it. */
Michal Vaskobdac23f2022-01-12 14:02:35 +0100974 LY_CHECK_RET(lys_parse_load_from_clb_or_file(ctx, name, revision, mod_latest, new_mods, mod));
aPiecek4725aea2021-07-28 10:18:33 +0200975 if (!*mod && !mod_latest) {
Michal Vasko4e205e82021-06-08 14:01:47 +0200976 LOGVAL(ctx, LYVE_REFERENCE, "Loading \"%s\" module failed.", name);
Michal Vasko0550b762020-11-24 18:04:08 +0100977 return LY_EVALID;
978 }
aPiecek4725aea2021-07-28 10:18:33 +0200979
980 /* Update the latest_revision flag - here we have selected the latest available schema,
981 * consider that even the callback provides correct latest revision.
982 */
983 if (!*mod) {
984 LOGVRB("Newer revision than \"%s@%s\" not found, using this as the latest revision.",
985 mod_latest->name, mod_latest->revision);
986 assert(mod_latest->latest_revision & LYS_MOD_LATEST_REV);
987 mod_latest->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS;
988 *mod = mod_latest;
989 } else if (*mod && !revision && ((*mod)->latest_revision & LYS_MOD_LATEST_REV)) {
990 (*mod)->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS;
991 }
Radek Krejci086c7132018-10-26 15:29:04 +0200992 }
Radek Krejci086c7132018-10-26 15:29:04 +0200993
aPiecek9f8c7e72021-07-28 12:01:56 +0200994 /* Checking the circular dependence of imported modules. */
995 LY_CHECK_RET(lys_check_circular_dependency(ctx, mod));
996
Radek Krejci086c7132018-10-26 15:29:04 +0200997 return LY_SUCCESS;
998}
999
1000LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001001lysp_check_stringchar(struct lysp_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +02001002{
1003 if (!is_yangutf8char(c)) {
Michal Vasko21eaa392024-02-20 15:48:42 +01001004 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (char)c);
David Sedlák4a650532019-07-10 11:55:18 +02001005 return LY_EVALID;
1006 }
1007 return LY_SUCCESS;
1008}
1009
1010LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001011lysp_check_identifierchar(struct lysp_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +02001012{
Michal Vasko69730152020-10-09 16:30:07 +02001013 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +02001014 if (!is_yangidentstartchar(c)) {
aPiecekc89b2242021-05-14 14:19:11 +02001015 if ((c < UCHAR_MAX) && isprint(c)) {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001016 if (ctx) {
Michal Vasko21eaa392024-02-20 15:48:42 +01001017 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04" PRIx32 ").",
1018 (char)c, c);
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001019 }
Michal Vasko7c769042021-03-25 12:20:49 +01001020 } else {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001021 if (ctx) {
Michal Vasko21eaa392024-02-20 15:48:42 +01001022 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character 0x%04" PRIx32 ".", c);
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001023 }
Michal Vasko7c769042021-03-25 12:20:49 +01001024 }
David Sedlák4a650532019-07-10 11:55:18 +02001025 return LY_EVALID;
1026 }
1027 if (prefix) {
1028 if (first) {
1029 (*prefix) = 0;
1030 } else {
1031 (*prefix) = 2;
1032 }
1033 }
Michal Vasko69730152020-10-09 16:30:07 +02001034 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +02001035 (*prefix) = 1;
1036 } else if (!is_yangidentchar(c)) {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001037 if (ctx) {
Michal Vasko21eaa392024-02-20 15:48:42 +01001038 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04" PRIx32 ").", (char)c, c);
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001039 }
David Sedlák4a650532019-07-10 11:55:18 +02001040 return LY_EVALID;
1041 }
1042
1043 return LY_SUCCESS;
1044}
1045
Radek Krejci771928a2021-01-19 13:42:36 +01001046/**
1047 * @brief Try to find the parsed submodule in main module for the given include record.
1048 *
1049 * @param[in] pctx main parser context
1050 * @param[in] inc The include record with missing parsed submodule. According to include info try to find
1051 * the corresponding parsed submodule in main module's includes.
1052 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
1053 * @return LY_ENOT - the parsed module was not found.
1054 * @return LY_EVALID - YANG rule violation
1055 */
1056static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001057lysp_main_pmod_get_submodule(struct lysp_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +02001058{
Radek Krejci771928a2021-01-19 13:42:36 +01001059 LY_ARRAY_COUNT_TYPE i;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001060 struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed;
Michal Vasko69730152020-10-09 16:30:07 +02001061
Radek Krejci771928a2021-01-19 13:42:36 +01001062 LY_ARRAY_FOR(main_pmod->includes, i) {
1063 if (strcmp(main_pmod->includes[i].name, inc->name)) {
1064 continue;
1065 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001066
Radek Krejci771928a2021-01-19 13:42:36 +01001067 if (inc->rev[0] && strncmp(inc->rev, main_pmod->includes[i].rev, LY_REV_SIZE)) {
1068 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1069 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001070 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev,
Radek Krejci771928a2021-01-19 13:42:36 +01001071 main_pmod->includes[i].name, main_pmod->includes[i].rev, main_pmod->mod->name);
1072 return LY_EVALID;
1073 }
1074
1075 inc->submodule = main_pmod->includes[i].submodule;
1076 return inc->submodule ? LY_SUCCESS : LY_ENOT;
1077 }
1078
1079 if (main_pmod->version == LYS_VERSION_1_1) {
1080 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1081 "YANG 1.1 requires all submodules to be included from main module. "
1082 "But submodule \"%s\" includes submodule \"%s\" which is not included by main module \"%s\".",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001083 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->name, main_pmod->mod->name);
Radek Krejci771928a2021-01-19 13:42:36 +01001084 return LY_EVALID;
1085 } else {
1086 return LY_ENOT;
1087 }
1088}
1089
1090/**
Michal Vasko8a67eff2021-12-07 14:04:47 +01001091 * @brief Try to find the parsed submodule in currenlty parsed modules for the given include record.
1092 *
1093 * @param[in] pctx main parser context
1094 * @param[in] inc The include record with missing parsed submodule.
1095 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
1096 * @return LY_ENOT - the parsed module was not found.
1097 * @return LY_EVALID - YANG rule violation
1098 */
1099static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001100lysp_parsed_mods_get_submodule(struct lysp_ctx *pctx, struct lysp_include *inc)
Michal Vasko8a67eff2021-12-07 14:04:47 +01001101{
1102 uint32_t i;
1103 struct lysp_submodule *submod;
1104
1105 for (i = 0; i < pctx->parsed_mods->count - 1; ++i) {
1106 submod = pctx->parsed_mods->objs[i];
1107 if (!submod->is_submod) {
1108 continue;
1109 }
1110
1111 if (strcmp(submod->name, inc->name)) {
1112 continue;
1113 }
1114
1115 if (inc->rev[0] && submod->revs && strncmp(inc->rev, submod->revs[0].date, LY_REV_SIZE)) {
1116 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1117 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
1118 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev,
1119 submod->name, submod->revs[0].date, PARSER_CUR_PMOD(pctx)->mod->name);
1120 return LY_EVALID;
1121 }
1122
1123 inc->submodule = submod;
1124 return LY_SUCCESS;
1125 }
1126
1127 return LY_ENOT;
1128}
1129
1130/**
Radek Krejci771928a2021-01-19 13:42:36 +01001131 * @brief Make the copy of the given include record into the main module.
1132 *
1133 * YANG 1.0 does not require the main module to include all the submodules. Therefore, parsing submodules can cause
1134 * reallocating and extending the includes array in the main module by the submodules included only in submodules.
1135 *
1136 * @param[in] pctx main parser context
1137 * @param[in] inc Include record to copy into main module taken from @p pctx.
1138 * @return LY_ERR value.
1139 */
1140static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001141lysp_inject_submodule(struct lysp_ctx *pctx, struct lysp_include *inc)
Radek Krejci771928a2021-01-19 13:42:36 +01001142{
1143 LY_ARRAY_COUNT_TYPE i;
1144 struct lysp_include *inc_new, *inc_tofill = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001145 struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed;
Radek Krejci771928a2021-01-19 13:42:36 +01001146
1147 /* first, try to find the corresponding record with missing parsed submodule */
1148 LY_ARRAY_FOR(main_pmod->includes, i) {
1149 if (strcmp(main_pmod->includes[i].name, inc->name)) {
1150 continue;
1151 }
1152 inc_tofill = &main_pmod->includes[i];
1153 break;
1154 }
1155
1156 if (inc_tofill) {
1157 inc_tofill->submodule = inc->submodule;
1158 } else {
1159 LY_ARRAY_NEW_RET(PARSER_CTX(pctx), main_pmod->includes, inc_new, LY_EMEM);
1160
1161 inc_new->submodule = inc->submodule;
1162 DUP_STRING_RET(PARSER_CTX(pctx), inc->name, inc_new->name);
1163 DUP_STRING_RET(PARSER_CTX(pctx), inc->dsc, inc_new->dsc);
1164 DUP_STRING_RET(PARSER_CTX(pctx), inc->ref, inc_new->ref);
Radek Krejci771928a2021-01-19 13:42:36 +01001165 memcpy(inc_new->rev, inc->rev, LY_REV_SIZE);
1166 inc_new->injected = 1;
1167 }
1168 return LY_SUCCESS;
1169}
1170
1171LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001172lysp_load_submodules(struct lysp_ctx *pctx, struct lysp_module *pmod, struct ly_set *new_mods)
Radek Krejci771928a2021-01-19 13:42:36 +01001173{
1174 LY_ARRAY_COUNT_TYPE u;
1175 struct ly_ctx *ctx = PARSER_CTX(pctx);
1176
1177 LY_ARRAY_FOR(pmod->includes, u) {
Michal Vasko8a67eff2021-12-07 14:04:47 +01001178 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci771928a2021-01-19 13:42:36 +01001179 struct lysp_submodule *submod = NULL;
1180 struct lysp_include *inc = &pmod->includes[u];
1181
1182 if (inc->submodule) {
1183 continue;
1184 }
1185
1186 if (pmod->is_submod) {
1187 /* try to find the submodule in the main module or its submodules */
Michal Vasko8a67eff2021-12-07 14:04:47 +01001188 ret = lysp_main_pmod_get_submodule(pctx, inc);
1189 LY_CHECK_RET(ret != LY_ENOT, ret);
Radek Krejci771928a2021-01-19 13:42:36 +01001190 }
1191
Michal Vasko8a67eff2021-12-07 14:04:47 +01001192 /* try to use currently parsed submodule */
1193 r = lysp_parsed_mods_get_submodule(pctx, inc);
1194 LY_CHECK_RET(r != LY_ENOT, r);
1195
Radek Krejci771928a2021-01-19 13:42:36 +01001196 /* submodule not present in the main module, get the input data and parse it */
1197 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcidf549132021-01-21 10:32:32 +01001198search_clb:
Radek Krejci771928a2021-01-19 13:42:36 +01001199 if (ctx->imp_clb) {
1200 const char *submodule_data = NULL;
1201 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko26bbb272022-08-02 14:54:33 +02001202
Radek Krejci771928a2021-01-19 13:42:36 +01001203 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
1204 struct lysp_load_module_check_data check_data = {0};
1205 struct ly_in *in;
1206
Michal Vasko8a67eff2021-12-07 14:04:47 +01001207 if (ctx->imp_clb(PARSER_CUR_PMOD(pctx)->mod->name, NULL, inc->name,
Radek Krejci771928a2021-01-19 13:42:36 +01001208 inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
1209 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
1210 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
1211 check_data.name = inc->name;
1212 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001213 check_data.submoduleof = PARSER_CUR_PMOD(pctx)->mod->name;
aPiecekc3e26142021-06-22 14:25:49 +02001214 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, new_mods, &submod);
Radek Krejci771928a2021-01-19 13:42:36 +01001215
1216 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1217 * submodule's include into main module, where it is missing */
1218 inc = &pmod->includes[u];
1219
1220 ly_in_free(in, 0);
1221 if (submodule_data_free) {
1222 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
1223 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001224 }
1225 }
Radek Krejci771928a2021-01-19 13:42:36 +01001226 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1227 goto search_file;
1228 }
1229 } else {
Radek Krejcidf549132021-01-21 10:32:32 +01001230search_file:
Radek Krejci771928a2021-01-19 13:42:36 +01001231 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
1232 /* submodule was not received from the callback or there is no callback set */
Michal Vaskodd6d5a32022-07-14 13:54:58 +02001233 lys_parse_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, pctx->main_ctx,
1234 PARSER_CUR_PMOD(pctx->main_ctx)->mod->name, 1, new_mods, (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001235
Radek Krejci771928a2021-01-19 13:42:36 +01001236 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1237 * submodule's include into main module, where it is missing */
1238 inc = &pmod->includes[u];
1239 }
1240 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1241 goto search_clb;
1242 }
1243 }
1244 if (submod) {
1245 if (!inc->rev[0] && (submod->latest_revision == 1)) {
1246 /* update the latest_revision flag - here we have selected the latest available schema,
1247 * consider that even the callback provides correct latest revision */
1248 submod->latest_revision = 2;
1249 }
1250
1251 inc->submodule = submod;
1252 if (ret == LY_ENOT) {
1253 /* the submodule include is not present in YANG 1.0 main module - add it there */
1254 LY_CHECK_RET(lysp_inject_submodule(pctx, &pmod->includes[u]));
1255 }
1256 }
1257 if (!inc->submodule) {
1258 LOGVAL(ctx, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name,
Michal Vasko8a67eff2021-12-07 14:04:47 +01001259 PARSER_CUR_PMOD(pctx)->is_submod ? ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name :
1260 PARSER_CUR_PMOD(pctx)->mod->name);
Radek Krejci771928a2021-01-19 13:42:36 +01001261 return LY_EVALID;
1262 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001263 }
1264
1265 return LY_SUCCESS;
1266}
1267
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001268LIBYANG_API_DEF const struct lysc_when *
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001269lysc_has_when(const struct lysc_node *node)
1270{
Radek Krejci9a3823e2021-01-27 20:26:46 +01001271 struct lysc_when **when;
1272
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001273 if (!node) {
1274 return NULL;
1275 }
1276
1277 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001278 when = lysc_node_when(node);
1279 if (when) {
1280 return when[0];
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001281 }
1282 node = node->parent;
1283 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1284
1285 return NULL;
1286}
1287
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001288LIBYANG_API_DEF const struct lys_module *
Michal Vaskoef53c812021-10-13 10:21:03 +02001289lysc_owner_module(const struct lysc_node *node)
1290{
1291 if (!node) {
1292 return NULL;
1293 }
1294
1295 for ( ; node->parent; node = node->parent) {}
1296 return node->module;
1297}
1298
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001299LIBYANG_API_DEF const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001300lys_nodetype2str(uint16_t nodetype)
1301{
Michal Vaskod989ba02020-08-24 10:59:24 +02001302 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001303 case LYS_CONTAINER:
1304 return "container";
1305 case LYS_CHOICE:
1306 return "choice";
1307 case LYS_LEAF:
1308 return "leaf";
1309 case LYS_LEAFLIST:
1310 return "leaf-list";
1311 case LYS_LIST:
1312 return "list";
1313 case LYS_ANYXML:
1314 return "anyxml";
1315 case LYS_ANYDATA:
1316 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001317 case LYS_CASE:
1318 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001319 case LYS_RPC:
1320 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001321 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001322 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001323 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001324 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001325 case LYS_USES:
1326 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001327 default:
1328 return "unknown";
1329 }
1330}
1331
Radek Krejci693262f2019-04-29 15:23:20 +02001332const char *
1333lys_datatype2str(LY_DATA_TYPE basetype)
1334{
Michal Vaskod989ba02020-08-24 10:59:24 +02001335 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001336 case LY_TYPE_BINARY:
1337 return "binary";
1338 case LY_TYPE_UINT8:
1339 return "uint8";
1340 case LY_TYPE_UINT16:
1341 return "uint16";
1342 case LY_TYPE_UINT32:
1343 return "uint32";
1344 case LY_TYPE_UINT64:
1345 return "uint64";
1346 case LY_TYPE_STRING:
1347 return "string";
1348 case LY_TYPE_BITS:
1349 return "bits";
1350 case LY_TYPE_BOOL:
1351 return "boolean";
1352 case LY_TYPE_DEC64:
1353 return "decimal64";
1354 case LY_TYPE_EMPTY:
1355 return "empty";
1356 case LY_TYPE_ENUM:
1357 return "enumeration";
1358 case LY_TYPE_IDENT:
1359 return "identityref";
1360 case LY_TYPE_INST:
1361 return "instance-identifier";
1362 case LY_TYPE_LEAFREF:
1363 return "leafref";
1364 case LY_TYPE_UNION:
1365 return "union";
1366 case LY_TYPE_INT8:
1367 return "int8";
1368 case LY_TYPE_INT16:
1369 return "int16";
1370 case LY_TYPE_INT32:
1371 return "int32";
1372 case LY_TYPE_INT64:
1373 return "int64";
1374 default:
1375 return "unknown";
1376 }
1377}
1378
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001379LIBYANG_API_DEF const struct lysp_tpdf *
Radek Krejci056d0a82018-12-06 16:57:25 +01001380lysp_node_typedefs(const struct lysp_node *node)
1381{
Radek Krejci0fb28562018-12-13 15:17:37 +01001382 switch (node->nodetype) {
1383 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001384 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001385 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001386 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001387 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001388 return ((struct lysp_node_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001389 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001390 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001391 return ((struct lysp_node_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001392 case LYS_INPUT:
1393 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001394 return ((struct lysp_node_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001395 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001396 return ((struct lysp_node_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001397 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001398 return NULL;
1399 }
1400}
1401
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001402LIBYANG_API_DEF const struct lysp_node_grp *
Radek Krejci53ea6152018-12-13 15:21:15 +01001403lysp_node_groupings(const struct lysp_node *node)
1404{
1405 switch (node->nodetype) {
1406 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001407 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001408 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001409 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001410 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001411 return ((struct lysp_node_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001412 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001413 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001414 return ((struct lysp_node_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001415 case LYS_INPUT:
1416 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001417 return ((struct lysp_node_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001418 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001419 return ((struct lysp_node_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001420 default:
1421 return NULL;
1422 }
1423}
1424
Radek Krejci2a9fc652021-01-22 17:44:34 +01001425struct lysp_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001426lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001427{
1428 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001429
Radek Krejcibbe09a92018-11-08 09:36:54 +01001430 switch (node->nodetype) {
1431 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001432 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001433 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001434 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001435 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001436 return &((struct lysp_node_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001437 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001438 return &((struct lysp_node_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001439 default:
1440 return NULL;
1441 }
1442}
1443
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001444LIBYANG_API_DEF const struct lysp_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001445lysp_node_actions(const struct lysp_node *node)
1446{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001447 struct lysp_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001448
Michal Vasko22df3f02020-08-24 13:29:22 +02001449 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001450 if (actions) {
1451 return *actions;
1452 } else {
1453 return NULL;
1454 }
1455}
1456
Radek Krejci2a9fc652021-01-22 17:44:34 +01001457struct lysp_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001458lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001459{
1460 assert(node);
1461 switch (node->nodetype) {
1462 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001463 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001464 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001465 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001466 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001467 return &((struct lysp_node_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001468 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001469 return &((struct lysp_node_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001470 default:
1471 return NULL;
1472 }
1473}
1474
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001475LIBYANG_API_DEF const struct lysp_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001476lysp_node_notifs(const struct lysp_node *node)
1477{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001478 struct lysp_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001479
Michal Vasko22df3f02020-08-24 13:29:22 +02001480 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001481 if (notifs) {
1482 return *notifs;
1483 } else {
1484 return NULL;
1485 }
1486}
1487
Radek Krejcibbe09a92018-11-08 09:36:54 +01001488struct lysp_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001489lysp_node_child_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001490{
1491 assert(node);
1492 switch (node->nodetype) {
1493 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001494 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001495 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001496 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001497 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001498 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001499 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001500 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001501 case LYS_GROUPING:
Radek Krejci01180ac2021-01-27 08:48:22 +01001502 return &((struct lysp_node_grp *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001503 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001504 return &((struct lysp_node_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001505 case LYS_INPUT:
1506 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001507 return &((struct lysp_node_action_inout *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001508 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001509 return &((struct lysp_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001510 default:
1511 return NULL;
1512 }
1513}
1514
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001515LIBYANG_API_DEF const struct lysp_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001516lysp_node_child(const struct lysp_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01001517{
Michal Vasko544e58a2021-01-28 14:33:41 +01001518 struct lysp_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001519
1520 if (!node) {
1521 return NULL;
1522 }
1523
Michal Vasko544e58a2021-01-28 14:33:41 +01001524 child = lysp_node_child_p((struct lysp_node *)node);
1525 if (child) {
1526 return *child;
Radek Krejci056d0a82018-12-06 16:57:25 +01001527 } else {
1528 return NULL;
1529 }
1530}
1531
Radek Krejci9a3823e2021-01-27 20:26:46 +01001532struct lysp_restr **
1533lysp_node_musts_p(const struct lysp_node *node)
1534{
1535 if (!node) {
1536 return NULL;
1537 }
1538
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001539 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001540 case LYS_CONTAINER:
1541 return &((struct lysp_node_container *)node)->musts;
1542 case LYS_LEAF:
1543 return &((struct lysp_node_leaf *)node)->musts;
1544 case LYS_LEAFLIST:
1545 return &((struct lysp_node_leaflist *)node)->musts;
1546 case LYS_LIST:
1547 return &((struct lysp_node_list *)node)->musts;
1548 case LYS_ANYXML:
1549 case LYS_ANYDATA:
1550 return &((struct lysp_node_anydata *)node)->musts;
1551 case LYS_NOTIF:
1552 return &((struct lysp_node_notif *)node)->musts;
1553 case LYS_INPUT:
1554 case LYS_OUTPUT:
1555 return &((struct lysp_node_action_inout *)node)->musts;
1556 default:
1557 return NULL;
1558 }
1559}
1560
1561struct lysp_restr *
1562lysp_node_musts(const struct lysp_node *node)
1563{
1564 struct lysp_restr **musts;
1565
1566 musts = lysp_node_musts_p(node);
1567 if (musts) {
1568 return *musts;
1569 } else {
1570 return NULL;
1571 }
1572}
1573
1574struct lysp_when **
1575lysp_node_when_p(const struct lysp_node *node)
1576{
1577 if (!node) {
1578 return NULL;
1579 }
1580
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001581 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001582 case LYS_CONTAINER:
1583 return &((struct lysp_node_container *)node)->when;
1584 case LYS_CHOICE:
1585 return &((struct lysp_node_choice *)node)->when;
1586 case LYS_LEAF:
1587 return &((struct lysp_node_leaf *)node)->when;
1588 case LYS_LEAFLIST:
1589 return &((struct lysp_node_leaflist *)node)->when;
1590 case LYS_LIST:
1591 return &((struct lysp_node_list *)node)->when;
1592 case LYS_ANYXML:
1593 case LYS_ANYDATA:
1594 return &((struct lysp_node_anydata *)node)->when;
1595 case LYS_CASE:
1596 return &((struct lysp_node_case *)node)->when;
1597 case LYS_USES:
1598 return &((struct lysp_node_uses *)node)->when;
1599 case LYS_AUGMENT:
1600 return &((struct lysp_node_augment *)node)->when;
1601 default:
1602 return NULL;
1603 }
1604}
1605
1606struct lysp_when *
1607lysp_node_when(const struct lysp_node *node)
1608{
1609 struct lysp_when **when;
1610
1611 when = lysp_node_when_p(node);
1612 if (when) {
1613 return *when;
1614 } else {
1615 return NULL;
1616 }
1617}
1618
Radek Krejci2a9fc652021-01-22 17:44:34 +01001619struct lysc_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001620lysc_node_actions_p(struct lysc_node *node)
1621{
1622 assert(node);
1623 switch (node->nodetype) {
1624 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001625 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001626 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001627 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001628 default:
1629 return NULL;
1630 }
1631}
1632
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001633LIBYANG_API_DEF const struct lysc_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001634lysc_node_actions(const struct lysc_node *node)
1635{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001636 struct lysc_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001637
Michal Vasko22df3f02020-08-24 13:29:22 +02001638 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001639 if (actions) {
1640 return *actions;
1641 } else {
1642 return NULL;
1643 }
1644}
1645
Radek Krejci2a9fc652021-01-22 17:44:34 +01001646struct lysc_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001647lysc_node_notifs_p(struct lysc_node *node)
1648{
1649 assert(node);
1650 switch (node->nodetype) {
1651 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001652 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001653 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001654 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001655 default:
1656 return NULL;
1657 }
1658}
1659
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001660LIBYANG_API_DEF const struct lysc_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001661lysc_node_notifs(const struct lysc_node *node)
1662{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001663 struct lysc_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001664
Michal Vasko22df3f02020-08-24 13:29:22 +02001665 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001666 if (notifs) {
1667 return *notifs;
1668 } else {
1669 return NULL;
1670 }
1671}
1672
Radek Krejcibbe09a92018-11-08 09:36:54 +01001673struct lysc_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001674lysc_node_child_p(const struct lysc_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001675{
Michal Vasko544e58a2021-01-28 14:33:41 +01001676 assert(node && !(node->nodetype & (LYS_RPC | LYS_ACTION)));
1677
Radek Krejcibbe09a92018-11-08 09:36:54 +01001678 switch (node->nodetype) {
1679 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001680 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001681 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001682 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001683 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001684 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001685 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001686 return &((struct lysc_node_list *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001687 case LYS_INPUT:
1688 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001689 return &((struct lysc_node_action_inout *)node)->child;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001690 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001691 return &((struct lysc_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001692 default:
1693 return NULL;
1694 }
1695}
1696
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001697LIBYANG_API_DEF const struct lysc_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001698lysc_node_child(const struct lysc_node *node)
Radek Krejcia3045382018-11-22 14:30:31 +01001699{
Michal Vasko544e58a2021-01-28 14:33:41 +01001700 struct lysc_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001701
1702 if (!node) {
1703 return NULL;
1704 }
1705
Michal Vasko544e58a2021-01-28 14:33:41 +01001706 if (node->nodetype & (LYS_RPC | LYS_ACTION)) {
1707 return &((struct lysc_node_action *)node)->input.node;
Radek Krejcibe154442021-01-21 11:06:36 +01001708 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01001709 child = lysc_node_child_p(node);
1710 if (child) {
1711 return *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001712 }
Michal Vasko2a668712020-10-21 11:48:09 +02001713 }
Michal Vasko544e58a2021-01-28 14:33:41 +01001714
1715 return NULL;
Michal Vasko2a668712020-10-21 11:48:09 +02001716}
1717
Radek Krejci9a3823e2021-01-27 20:26:46 +01001718struct lysc_must **
1719lysc_node_musts_p(const struct lysc_node *node)
1720{
1721 if (!node) {
1722 return NULL;
1723 }
1724
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001725 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001726 case LYS_CONTAINER:
1727 return &((struct lysc_node_container *)node)->musts;
1728 case LYS_LEAF:
1729 return &((struct lysc_node_leaf *)node)->musts;
1730 case LYS_LEAFLIST:
1731 return &((struct lysc_node_leaflist *)node)->musts;
1732 case LYS_LIST:
1733 return &((struct lysc_node_list *)node)->musts;
1734 case LYS_ANYXML:
1735 case LYS_ANYDATA:
1736 return &((struct lysc_node_anydata *)node)->musts;
1737 case LYS_NOTIF:
1738 return &((struct lysc_node_notif *)node)->musts;
1739 case LYS_INPUT:
1740 case LYS_OUTPUT:
1741 return &((struct lysc_node_action_inout *)node)->musts;
1742 default:
1743 return NULL;
1744 }
1745}
1746
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001747LIBYANG_API_DEF struct lysc_must *
Radek Krejci9a3823e2021-01-27 20:26:46 +01001748lysc_node_musts(const struct lysc_node *node)
1749{
1750 struct lysc_must **must_p;
1751
1752 must_p = lysc_node_musts_p(node);
1753 if (must_p) {
1754 return *must_p;
1755 } else {
1756 return NULL;
1757 }
1758}
1759
1760struct lysc_when ***
1761lysc_node_when_p(const struct lysc_node *node)
1762{
1763 if (!node) {
1764 return NULL;
1765 }
1766
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001767 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001768 case LYS_CONTAINER:
1769 return &((struct lysc_node_container *)node)->when;
1770 case LYS_CHOICE:
1771 return &((struct lysc_node_choice *)node)->when;
1772 case LYS_LEAF:
1773 return &((struct lysc_node_leaf *)node)->when;
1774 case LYS_LEAFLIST:
1775 return &((struct lysc_node_leaflist *)node)->when;
1776 case LYS_LIST:
1777 return &((struct lysc_node_list *)node)->when;
1778 case LYS_ANYXML:
1779 case LYS_ANYDATA:
1780 return &((struct lysc_node_anydata *)node)->when;
1781 case LYS_CASE:
1782 return &((struct lysc_node_case *)node)->when;
1783 case LYS_NOTIF:
1784 return &((struct lysc_node_notif *)node)->when;
1785 case LYS_RPC:
1786 case LYS_ACTION:
1787 return &((struct lysc_node_action *)node)->when;
1788 default:
1789 return NULL;
1790 }
1791}
1792
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001793LIBYANG_API_DEF struct lysc_when **
Radek Krejci9a3823e2021-01-27 20:26:46 +01001794lysc_node_when(const struct lysc_node *node)
1795{
1796 struct lysc_when ***when_p;
1797
1798 when_p = lysc_node_when_p(node);
1799 if (when_p) {
1800 return *when_p;
1801 } else {
1802 return NULL;
1803 }
1804}
1805
Michal Vasko510554a2023-09-11 09:03:29 +02001806LIBYANG_API_DEF const struct lysc_node *
1807lysc_node_lref_target(const struct lysc_node *node)
1808{
1809 struct lysc_type_leafref *lref;
1810 struct ly_path *p;
1811 const struct lysc_node *target;
1812
1813 if (!node || !(node->nodetype & LYD_NODE_TERM)) {
1814 return NULL;
1815 }
1816
1817 lref = (struct lysc_type_leafref *)((struct lysc_node_leaf *)node)->type;
1818 if (lref->basetype != LY_TYPE_LEAFREF) {
1819 return NULL;
1820 }
1821
1822 /* compile the path */
1823 if (ly_path_compile_leafref(node->module->ctx, node, NULL, lref->path,
1824 (node->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
1825 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p)) {
1826 return NULL;
1827 }
1828
1829 /* get the target node */
1830 target = p[LY_ARRAY_COUNT(p) - 1].node;
1831 ly_path_free(node->module->ctx, p);
1832
1833 return target;
1834}
1835
Radek Krejcid6b76452019-09-03 17:03:03 +02001836enum ly_stmt
Radek Krejcid54412f2020-12-17 20:25:35 +01001837lysp_match_kw(struct ly_in *in, uint64_t *indent)
David Sedlákc10e7902018-12-17 02:17:59 +01001838{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001839/**
Radek Krejcid54412f2020-12-17 20:25:35 +01001840 * @brief Move the input by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001841 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001842 *
1843 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001844 */
Radek Krejcid54412f2020-12-17 20:25:35 +01001845#define MOVE_IN(COUNT) \
1846 ly_in_skip(in, COUNT); \
1847 if (indent) { \
1848 (*indent)+=COUNT; \
1849 }
1850#define IF_KW(STR, LEN, STMT) \
1851 if (!strncmp(in->current, STR, LEN)) { \
1852 MOVE_IN(LEN); \
1853 (*kw)=STMT; \
1854 }
1855#define IF_KW_PREFIX(STR, LEN) \
1856 if (!strncmp(in->current, STR, LEN)) { \
1857 MOVE_IN(LEN);
1858#define IF_KW_PREFIX_END \
1859 }
David Sedlák572e7ab2019-06-04 16:01:58 +02001860
Michal Vasko63f3d842020-07-08 10:10:14 +02001861 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001862 enum ly_stmt result = LY_STMT_NONE;
1863 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001864 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001865 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001866 case 'a':
Radek Krejcid54412f2020-12-17 20:25:35 +01001867 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001868 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1869 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1870 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001871 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001872 IF_KW("data", 4, LY_STMT_ANYDATA)
1873 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001874 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001875 break;
1876 case 'b':
Radek Krejcid54412f2020-12-17 20:25:35 +01001877 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001878 IF_KW("ase", 3, LY_STMT_BASE)
1879 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1880 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001881 break;
1882 case 'c':
Radek Krejcid54412f2020-12-17 20:25:35 +01001883 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001884 IF_KW("ase", 3, LY_STMT_CASE)
1885 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001886 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001887 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001888 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001889 IF_KW("ct", 2, LY_STMT_CONTACT)
1890 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001891 IF_KW_PREFIX_END
1892 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001893 break;
1894 case 'd':
Radek Krejcid54412f2020-12-17 20:25:35 +01001895 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001896 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001897 IF_KW("fault", 5, LY_STMT_DEFAULT)
1898 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001899 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001900 IF_KW("e", 1, LY_STMT_DEVIATE)
1901 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001902 IF_KW_PREFIX_END
1903 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001904 break;
1905 case 'e':
Radek Krejcid54412f2020-12-17 20:25:35 +01001906 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001907 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001908 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001909 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1910 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001911 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001912 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001913 break;
1914 case 'f':
Radek Krejcid54412f2020-12-17 20:25:35 +01001915 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001916 IF_KW("eature", 6, LY_STMT_FEATURE)
1917 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001918 break;
1919 case 'g':
Radek Krejcid54412f2020-12-17 20:25:35 +01001920 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001921 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001922 break;
1923 case 'i':
Radek Krejcid54412f2020-12-17 20:25:35 +01001924 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001925 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1926 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1927 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001928 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001929 IF_KW("clude", 5, LY_STMT_INCLUDE)
1930 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001931 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001932 break;
1933 case 'k':
Radek Krejcid54412f2020-12-17 20:25:35 +01001934 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001935 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001936 break;
1937 case 'l':
Radek Krejcid54412f2020-12-17 20:25:35 +01001938 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001939 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001940 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1941 else IF_KW("af", 2, LY_STMT_LEAF)
1942 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001943 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001944 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001945 break;
1946 case 'm':
Radek Krejcid54412f2020-12-17 20:25:35 +01001947 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001948 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001949 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1950 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001951 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001952 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1953 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001954 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001955 IF_KW("ule", 3, LY_STMT_MODULE)
1956 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001957 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001958 break;
1959 case 'n':
Radek Krejcid54412f2020-12-17 20:25:35 +01001960 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001961 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1962 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001963 break;
1964 case 'o':
Radek Krejcid54412f2020-12-17 20:25:35 +01001965 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001966 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001967 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1968 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001969 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001970 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001971 break;
1972 case 'p':
Radek Krejcid54412f2020-12-17 20:25:35 +01001973 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001974 IF_KW("ath", 3, LY_STMT_PATH)
1975 else IF_KW("attern", 6, LY_STMT_PATTERN)
1976 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001977 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001978 IF_KW("fix", 3, LY_STMT_PREFIX)
1979 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001980 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001981 break;
1982 case 'r':
Radek Krejcid54412f2020-12-17 20:25:35 +01001983 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001984 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001985 else IF_KW_PREFIX("e", 1)
1986 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001987 IF_KW("erence", 6, LY_STMT_REFERENCE)
1988 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001989 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001990 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1991 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1992 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001993 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001994 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001995 break;
1996 case 's':
Radek Krejcid54412f2020-12-17 20:25:35 +01001997 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001998 IF_KW("tatus", 5, LY_STMT_STATUS)
1999 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02002000 break;
2001 case 't':
Radek Krejcid54412f2020-12-17 20:25:35 +01002002 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002003 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
2004 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02002005 break;
2006 case 'u':
Radek Krejcid54412f2020-12-17 20:25:35 +01002007 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02002008 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02002009 IF_KW("que", 3, LY_STMT_UNIQUE)
2010 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002011 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002012 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02002013 break;
2014 case 'v':
Radek Krejcid54412f2020-12-17 20:25:35 +01002015 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002016 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02002017 break;
2018 case 'w':
Radek Krejcid54412f2020-12-17 20:25:35 +01002019 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002020 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02002021 break;
2022 case 'y':
Radek Krejcid54412f2020-12-17 20:25:35 +01002023 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002024 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
2025 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02002026 break;
David Sedlák23a59a62018-10-26 13:08:02 +02002027 default:
Radek Krejcid54412f2020-12-17 20:25:35 +01002028 /* if indent is not NULL we are matching keyword from YANG data */
2029 if (indent) {
Michal Vasko63f3d842020-07-08 10:10:14 +02002030 if (in->current[0] == ';') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002031 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002032 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02002033 } else if (in->current[0] == '{') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002034 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002035 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002036 } else if (in->current[0] == '}') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002037 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002038 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02002039 }
2040 }
David Sedlák23a59a62018-10-26 13:08:02 +02002041 break;
2042 }
2043
Michal Vasko63f3d842020-07-08 10:10:14 +02002044 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02002045 /* the keyword is not terminated */
2046 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002047 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02002048 }
2049
David Sedlák1bccdfa2019-06-17 15:55:27 +02002050#undef IF_KW
2051#undef IF_KW_PREFIX
2052#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01002053#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02002054 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01002055
David Sedlák1bccdfa2019-06-17 15:55:27 +02002056 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02002057}
David Sedlákecf5eb82019-06-03 14:12:44 +02002058
Radek Krejci85ac8312021-03-03 20:21:33 +01002059LY_ERR
2060lysp_ext_find_definition(const struct ly_ctx *ctx, const struct lysp_ext_instance *ext, const struct lys_module **ext_mod,
2061 struct lysp_ext **ext_def)
2062{
Radek Krejci85ac8312021-03-03 20:21:33 +01002063 const char *tmp, *name, *prefix;
2064 size_t pref_len, name_len;
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002065 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci85ac8312021-03-03 20:21:33 +01002066 const struct lys_module *mod = NULL;
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002067 const struct lysp_submodule *submod;
Radek Krejci85ac8312021-03-03 20:21:33 +01002068
Michal Vasko193dacd2022-10-13 08:43:05 +02002069 if (ext_def) {
2070 *ext_def = NULL;
Radek Krejci85ac8312021-03-03 20:21:33 +01002071 }
2072
Radek Krejci677abea2021-03-05 14:24:53 +01002073 /* parse the prefix, the nodeid was previously already parsed and checked */
Radek Krejci85ac8312021-03-03 20:21:33 +01002074 tmp = ext->name;
Radek Krejci677abea2021-03-05 14:24:53 +01002075 ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len);
Radek Krejci85ac8312021-03-03 20:21:33 +01002076
2077 /* get module where the extension definition should be placed */
Michal Vasko193dacd2022-10-13 08:43:05 +02002078 *ext_mod = mod = ly_resolve_prefix(ctx, prefix, pref_len, ext->format, ext->prefix_data);
Radek Krejci85ac8312021-03-03 20:21:33 +01002079 if (!mod) {
Radek Krejci422afb12021-03-04 16:38:16 +01002080 LOGVAL(ctx, LYVE_REFERENCE, "Invalid prefix \"%.*s\" used for extension instance identifier.", (int)pref_len, prefix);
Radek Krejci85ac8312021-03-03 20:21:33 +01002081 return LY_EVALID;
Radek Krejci85ac8312021-03-03 20:21:33 +01002082 }
2083
Michal Vasko193dacd2022-10-13 08:43:05 +02002084 if (!ext_def) {
2085 /* we are done */
2086 return LY_SUCCESS;
2087 }
2088
Radek Krejci85ac8312021-03-03 20:21:33 +01002089 /* find the parsed extension definition there */
2090 LY_ARRAY_FOR(mod->parsed->extensions, v) {
2091 if (!strcmp(name, mod->parsed->extensions[v].name)) {
2092 *ext_def = &mod->parsed->extensions[v];
2093 break;
2094 }
2095 }
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002096 if (!*ext_def) {
2097 LY_ARRAY_FOR(mod->parsed->includes, u) {
2098 submod = mod->parsed->includes[u].submodule;
2099 LY_ARRAY_FOR(submod->extensions, v) {
2100 if (!strcmp(name, submod->extensions[v].name)) {
2101 *ext_def = &submod->extensions[v];
2102 break;
2103 }
2104 }
2105 }
2106 }
Radek Krejci85ac8312021-03-03 20:21:33 +01002107
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002108 if (!*ext_def) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002109 LOGVAL(ctx, LYVE_REFERENCE, "Extension definition of extension instance \"%s\" not found.", ext->name);
2110 return LY_EVALID;
2111 }
2112
Radek Krejci85ac8312021-03-03 20:21:33 +01002113 return LY_SUCCESS;
2114}
2115
2116LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002117lysp_ext_instance_resolve_argument(struct ly_ctx *ctx, struct lysp_ext_instance *ext_p)
Radek Krejci85ac8312021-03-03 20:21:33 +01002118{
Michal Vasko193dacd2022-10-13 08:43:05 +02002119 assert(ext_p->def);
2120
2121 if (!ext_p->def->argname || ext_p->argument) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002122 /* nothing to do */
2123 return LY_SUCCESS;
2124 }
2125
Radek Krejci8df109d2021-04-23 12:19:08 +02002126 if (ext_p->format == LY_VALUE_XML) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002127 /* schema was parsed from YIN and an argument is expected, ... */
Radek Krejci85ac8312021-03-03 20:21:33 +01002128 struct lysp_stmt *stmt = NULL;
2129
Michal Vasko193dacd2022-10-13 08:43:05 +02002130 if (ext_p->def->flags & LYS_YINELEM_TRUE) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002131 /* ... argument was the first XML child element */
2132 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {}
2133 if (stmt) {
2134 const char *arg, *ext, *name_arg, *name_ext, *prefix_arg, *prefix_ext;
2135 size_t name_arg_len, name_ext_len, prefix_arg_len, prefix_ext_len;
2136
2137 stmt = ext_p->child;
2138
2139 arg = stmt->stmt;
2140 ly_parse_nodeid(&arg, &prefix_arg, &prefix_arg_len, &name_arg, &name_arg_len);
Michal Vasko193dacd2022-10-13 08:43:05 +02002141 if (ly_strncmp(ext_p->def->argname, name_arg, name_arg_len)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002142 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" expects argument element \"%s\" as its first XML child, "
Michal Vasko193dacd2022-10-13 08:43:05 +02002143 "but \"%.*s\" element found.", ext_p->name, ext_p->def->argname, (int)name_arg_len, name_arg);
Radek Krejci85ac8312021-03-03 20:21:33 +01002144 return LY_EVALID;
2145 }
2146
2147 /* check namespace - all the extension instances must be qualified and argument element is expected in the same
2148 * namespace. Do not check just prefixes, there can be different prefixes pointing to the same namespace */
2149 ext = ext_p->name; /* include prefix */
2150 ly_parse_nodeid(&ext, &prefix_ext, &prefix_ext_len, &name_ext, &name_ext_len);
2151
2152 if (ly_resolve_prefix(ctx, prefix_ext, prefix_ext_len, ext_p->format, ext_p->prefix_data) !=
2153 ly_resolve_prefix(ctx, prefix_arg, prefix_arg_len, stmt->format, stmt->prefix_data)) {
2154 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" element and its argument element \"%s\" are "
Michal Vasko193dacd2022-10-13 08:43:05 +02002155 "expected in the same namespace, but they differ.", ext_p->name, ext_p->def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002156 return LY_EVALID;
2157 }
2158 }
2159 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +02002160 /* ... 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 +01002161 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002162 if (!strcmp(stmt->stmt, ext_p->def->argname)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002163 /* this is the extension's argument */
2164 break;
2165 }
2166 }
2167 }
2168
2169 if (stmt) {
2170 LY_CHECK_RET(lydict_insert(ctx, stmt->arg, 0, &ext_p->argument));
2171 stmt->flags |= LYS_YIN_ARGUMENT;
2172 }
2173 }
2174
2175 if (!ext_p->argument) {
2176 /* missing extension's argument */
Michal Vasko193dacd2022-10-13 08:43:05 +02002177 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" missing argument %s\"%s\".",
2178 ext_p->name, (ext_p->def->flags & LYS_YINELEM_TRUE) ? "element " : "", ext_p->def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002179 return LY_EVALID;
2180 }
2181
2182 return LY_SUCCESS;
2183}
2184
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002185LY_ARRAY_COUNT_TYPE
Radek Krejcifc596f92021-02-26 22:40:26 +01002186lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, enum ly_stmt substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002187{
2188 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
2189
Michal Vaskod989ba02020-08-24 10:59:24 +02002190 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejciab430862021-03-02 20:13:40 +01002191 if (ext[index].parent_stmt == substmt) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02002192 return index;
2193 }
2194 }
2195
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002196 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002197}
2198
Michal Vaskof8ded142022-02-17 10:48:01 +01002199LIBYANG_API_DEF const struct lysc_node *
Michal Vasko72244882021-01-12 15:21:05 +01002200lysc_data_node(const struct lysc_node *schema)
Michal Vasko62ed12d2020-05-21 10:08:25 +02002201{
2202 const struct lysc_node *parent;
2203
Michal Vasko72244882021-01-12 15:21:05 +01002204 parent = schema;
Radek Krejcidf549132021-01-21 10:32:32 +01002205 while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC |
2206 LYS_ACTION | LYS_NOTIF))) {
Radek Krejci7d95fbb2021-01-26 17:33:13 +01002207 parent = parent->parent;
Michal Vasko72244882021-01-12 15:21:05 +01002208 }
Michal Vasko62ed12d2020-05-21 10:08:25 +02002209
2210 return parent;
2211}
Michal Vaskof4258e12021-06-15 12:11:42 +02002212
2213ly_bool
2214lys_has_recompiled(const struct lys_module *mod)
2215{
2216 LY_ARRAY_COUNT_TYPE u;
2217
2218 if (LYSP_HAS_RECOMPILED(mod->parsed)) {
2219 return 1;
2220 }
2221
2222 LY_ARRAY_FOR(mod->parsed->includes, u) {
2223 if (LYSP_HAS_RECOMPILED(mod->parsed->includes[u].submodule)) {
2224 return 1;
2225 }
2226 }
2227
2228 return 0;
2229}
2230
2231ly_bool
2232lys_has_compiled(const struct lys_module *mod)
2233{
2234 LY_ARRAY_COUNT_TYPE u;
2235
2236 if (LYSP_HAS_COMPILED(mod->parsed)) {
2237 return 1;
2238 }
2239
2240 LY_ARRAY_FOR(mod->parsed->includes, u) {
2241 if (LYSP_HAS_COMPILED(mod->parsed->includes[u].submodule)) {
2242 return 1;
2243 }
2244 }
2245
2246 return 0;
2247}
Michal Vasko7ee5be22021-06-16 17:03:34 +02002248
2249ly_bool
Michal Vasko87cfdba2022-02-22 14:13:45 +01002250lys_has_dep_mods(const struct lys_module *mod)
Michal Vasko7ee5be22021-06-16 17:03:34 +02002251{
2252 LY_ARRAY_COUNT_TYPE u;
2253
Michal Vasko87cfdba2022-02-22 14:13:45 +01002254 /* features */
2255 if (mod->parsed->features) {
Michal Vasko7ee5be22021-06-16 17:03:34 +02002256 return 1;
2257 }
2258
Michal Vasko87cfdba2022-02-22 14:13:45 +01002259 /* groupings */
2260 if (mod->parsed->groupings) {
2261 return 1;
2262 }
Michal Vasko7ee5be22021-06-16 17:03:34 +02002263 LY_ARRAY_FOR(mod->parsed->includes, u) {
2264 if (mod->parsed->includes[u].submodule->groupings) {
2265 return 1;
2266 }
2267 }
2268
Michal Vasko87cfdba2022-02-22 14:13:45 +01002269 /* augments (adding nodes with leafrefs) */
2270 if (mod->parsed->augments) {
2271 return 1;
2272 }
2273 LY_ARRAY_FOR(mod->parsed->includes, u) {
2274 if (mod->parsed->includes[u].submodule->augments) {
2275 return 1;
2276 }
2277 }
2278
Michal Vasko7ee5be22021-06-16 17:03:34 +02002279 return 0;
2280}
Michal Vaskob872d0f2022-12-08 09:36:54 +01002281
2282const char *
2283lys_stmt_str(enum ly_stmt stmt)
2284{
2285 switch (stmt) {
2286 case LY_STMT_NONE:
2287 return NULL;
2288 case LY_STMT_ACTION:
2289 return "action";
2290 case LY_STMT_ANYDATA:
2291 return "anydata";
2292 case LY_STMT_ANYXML:
2293 return "anyxml";
2294 case LY_STMT_ARGUMENT:
2295 return "argument";
2296 case LY_STMT_ARG_TEXT:
2297 return "text";
2298 case LY_STMT_ARG_VALUE:
2299 return "value";
2300 case LY_STMT_AUGMENT:
2301 return "augment";
2302 case LY_STMT_BASE:
2303 return "base";
2304 case LY_STMT_BELONGS_TO:
2305 return "belongs-to";
2306 case LY_STMT_BIT:
2307 return "bit";
2308 case LY_STMT_CASE:
2309 return "case";
2310 case LY_STMT_CHOICE:
2311 return "choice";
2312 case LY_STMT_CONFIG:
2313 return "config";
2314 case LY_STMT_CONTACT:
2315 return "contact";
2316 case LY_STMT_CONTAINER:
2317 return "container";
2318 case LY_STMT_DEFAULT:
2319 return "default";
2320 case LY_STMT_DESCRIPTION:
2321 return "description";
2322 case LY_STMT_DEVIATE:
2323 return "deviate";
2324 case LY_STMT_DEVIATION:
2325 return "deviation";
2326 case LY_STMT_ENUM:
2327 return "enum";
2328 case LY_STMT_ERROR_APP_TAG:
2329 return "error-app-tag";
2330 case LY_STMT_ERROR_MESSAGE:
2331 return "error-message";
2332 case LY_STMT_EXTENSION:
2333 return "extension";
2334 case LY_STMT_EXTENSION_INSTANCE:
2335 return NULL;
2336 case LY_STMT_FEATURE:
2337 return "feature";
2338 case LY_STMT_FRACTION_DIGITS:
2339 return "fraction-digits";
2340 case LY_STMT_GROUPING:
2341 return "grouping";
2342 case LY_STMT_IDENTITY:
2343 return "identity";
2344 case LY_STMT_IF_FEATURE:
2345 return "if-feature";
2346 case LY_STMT_IMPORT:
2347 return "import";
2348 case LY_STMT_INCLUDE:
2349 return "include";
2350 case LY_STMT_INPUT:
2351 return "input";
2352 case LY_STMT_KEY:
2353 return "key";
2354 case LY_STMT_LEAF:
2355 return "leaf";
2356 case LY_STMT_LEAF_LIST:
2357 return "leaf-list";
2358 case LY_STMT_LENGTH:
2359 return "length";
2360 case LY_STMT_LIST:
2361 return "list";
2362 case LY_STMT_MANDATORY:
2363 return "mandatory";
2364 case LY_STMT_MAX_ELEMENTS:
2365 return "max-elements";
2366 case LY_STMT_MIN_ELEMENTS:
2367 return "min-elements";
2368 case LY_STMT_MODIFIER:
2369 return "modifier";
2370 case LY_STMT_MODULE:
2371 return "module";
2372 case LY_STMT_MUST:
2373 return "must";
2374 case LY_STMT_NAMESPACE:
2375 return "namespace";
2376 case LY_STMT_NOTIFICATION:
2377 return "notification";
2378 case LY_STMT_ORDERED_BY:
2379 return "ordered-by";
2380 case LY_STMT_ORGANIZATION:
2381 return "organization";
2382 case LY_STMT_OUTPUT:
2383 return "output";
2384 case LY_STMT_PATH:
2385 return "path";
2386 case LY_STMT_PATTERN:
2387 return "pattern";
2388 case LY_STMT_POSITION:
2389 return "position";
2390 case LY_STMT_PREFIX:
2391 return "prefix";
2392 case LY_STMT_PRESENCE:
2393 return "presence";
2394 case LY_STMT_RANGE:
2395 return "range";
2396 case LY_STMT_REFERENCE:
2397 return "reference";
2398 case LY_STMT_REFINE:
2399 return "refine";
2400 case LY_STMT_REQUIRE_INSTANCE:
2401 return "require-instance";
2402 case LY_STMT_REVISION:
2403 return "revision";
2404 case LY_STMT_REVISION_DATE:
2405 return "revision-date";
2406 case LY_STMT_RPC:
2407 return "rpc";
2408 case LY_STMT_STATUS:
2409 return "status";
2410 case LY_STMT_SUBMODULE:
2411 return "submodule";
2412 case LY_STMT_SYNTAX_LEFT_BRACE:
2413 return "{";
2414 case LY_STMT_SYNTAX_RIGHT_BRACE:
2415 return "}";
2416 case LY_STMT_SYNTAX_SEMICOLON:
2417 return ";";
2418 case LY_STMT_TYPE:
2419 return "type";
2420 case LY_STMT_TYPEDEF:
2421 return "typedef";
2422 case LY_STMT_UNIQUE:
2423 return "unique";
2424 case LY_STMT_UNITS:
2425 return "units";
2426 case LY_STMT_USES:
2427 return "uses";
2428 case LY_STMT_VALUE:
2429 return "value";
2430 case LY_STMT_WHEN:
2431 return "when";
2432 case LY_STMT_YANG_VERSION:
2433 return "yang-version";
2434 case LY_STMT_YIN_ELEMENT:
2435 return "yin-element";
2436 }
2437
2438 return NULL;
2439}
2440
2441const char *
2442lys_stmt_arg(enum ly_stmt stmt)
2443{
2444 switch (stmt) {
2445 case LY_STMT_NONE:
2446 case LY_STMT_ARG_TEXT:
2447 case LY_STMT_ARG_VALUE:
2448 case LY_STMT_EXTENSION_INSTANCE:
2449 case LY_STMT_INPUT:
2450 case LY_STMT_OUTPUT:
2451 case LY_STMT_SYNTAX_LEFT_BRACE:
2452 case LY_STMT_SYNTAX_RIGHT_BRACE:
2453 case LY_STMT_SYNTAX_SEMICOLON:
2454 return NULL;
2455 case LY_STMT_ACTION:
2456 case LY_STMT_ANYDATA:
2457 case LY_STMT_ANYXML:
2458 case LY_STMT_ARGUMENT:
2459 case LY_STMT_BASE:
2460 case LY_STMT_BIT:
2461 case LY_STMT_CASE:
2462 case LY_STMT_CHOICE:
2463 case LY_STMT_CONTAINER:
2464 case LY_STMT_ENUM:
2465 case LY_STMT_EXTENSION:
2466 case LY_STMT_FEATURE:
2467 case LY_STMT_GROUPING:
2468 case LY_STMT_IDENTITY:
2469 case LY_STMT_IF_FEATURE:
2470 case LY_STMT_LEAF:
2471 case LY_STMT_LEAF_LIST:
2472 case LY_STMT_LIST:
2473 case LY_STMT_MODULE:
2474 case LY_STMT_NOTIFICATION:
2475 case LY_STMT_RPC:
2476 case LY_STMT_SUBMODULE:
2477 case LY_STMT_TYPE:
2478 case LY_STMT_TYPEDEF:
2479 case LY_STMT_UNITS:
2480 case LY_STMT_USES:
2481 return "name";
2482 case LY_STMT_AUGMENT:
2483 case LY_STMT_DEVIATION:
2484 case LY_STMT_REFINE:
2485 return "target-node";
2486 case LY_STMT_BELONGS_TO:
2487 case LY_STMT_IMPORT:
2488 case LY_STMT_INCLUDE:
2489 return "module";
2490 case LY_STMT_CONFIG:
2491 case LY_STMT_DEFAULT:
2492 case LY_STMT_DEVIATE:
2493 case LY_STMT_ERROR_APP_TAG:
2494 case LY_STMT_ERROR_MESSAGE:
2495 case LY_STMT_FRACTION_DIGITS:
2496 case LY_STMT_KEY:
2497 case LY_STMT_LENGTH:
2498 case LY_STMT_MANDATORY:
2499 case LY_STMT_MAX_ELEMENTS:
2500 case LY_STMT_MIN_ELEMENTS:
2501 case LY_STMT_MODIFIER:
2502 case LY_STMT_ORDERED_BY:
2503 case LY_STMT_PATH:
2504 case LY_STMT_PATTERN:
2505 case LY_STMT_POSITION:
2506 case LY_STMT_PREFIX:
2507 case LY_STMT_PRESENCE:
2508 case LY_STMT_RANGE:
2509 case LY_STMT_REQUIRE_INSTANCE:
2510 case LY_STMT_STATUS:
2511 case LY_STMT_VALUE:
2512 case LY_STMT_YANG_VERSION:
2513 case LY_STMT_YIN_ELEMENT:
2514 return "value";
2515 case LY_STMT_CONTACT:
2516 case LY_STMT_DESCRIPTION:
2517 case LY_STMT_ORGANIZATION:
2518 case LY_STMT_REFERENCE:
2519 return "text";
2520 case LY_STMT_MUST:
2521 case LY_STMT_WHEN:
2522 return "condition";
2523 case LY_STMT_NAMESPACE:
2524 return "uri";
2525 case LY_STMT_REVISION:
2526 case LY_STMT_REVISION_DATE:
2527 return "date";
2528 case LY_STMT_UNIQUE:
2529 return "tag";
2530 }
2531
2532 return NULL;
2533}
2534
2535uint8_t
2536lys_stmt_flags(enum ly_stmt stmt)
2537{
2538 switch (stmt) {
2539 case LY_STMT_NONE:
2540 case LY_STMT_ARG_TEXT:
2541 case LY_STMT_ARG_VALUE:
2542 case LY_STMT_DEFAULT:
2543 case LY_STMT_ERROR_APP_TAG:
2544 case LY_STMT_EXTENSION_INSTANCE:
2545 case LY_STMT_IF_FEATURE:
2546 case LY_STMT_INPUT:
2547 case LY_STMT_KEY:
2548 case LY_STMT_LENGTH:
2549 case LY_STMT_MUST:
2550 case LY_STMT_NAMESPACE:
2551 case LY_STMT_OUTPUT:
2552 case LY_STMT_PATH:
2553 case LY_STMT_PATTERN:
2554 case LY_STMT_PRESENCE:
2555 case LY_STMT_RANGE:
2556 case LY_STMT_SYNTAX_LEFT_BRACE:
2557 case LY_STMT_SYNTAX_RIGHT_BRACE:
2558 case LY_STMT_SYNTAX_SEMICOLON:
2559 case LY_STMT_UNIQUE:
2560 case LY_STMT_UNITS:
2561 case LY_STMT_WHEN:
2562 return 0;
2563 case LY_STMT_ACTION:
2564 case LY_STMT_ANYDATA:
2565 case LY_STMT_ANYXML:
2566 case LY_STMT_ARGUMENT:
2567 case LY_STMT_AUGMENT:
2568 case LY_STMT_BASE:
2569 case LY_STMT_BELONGS_TO:
2570 case LY_STMT_BIT:
2571 case LY_STMT_CASE:
2572 case LY_STMT_CHOICE:
2573 case LY_STMT_CONFIG:
2574 case LY_STMT_CONTAINER:
2575 case LY_STMT_DEVIATE:
2576 case LY_STMT_DEVIATION:
2577 case LY_STMT_ENUM:
2578 case LY_STMT_EXTENSION:
2579 case LY_STMT_FEATURE:
2580 case LY_STMT_FRACTION_DIGITS:
2581 case LY_STMT_GROUPING:
2582 case LY_STMT_IDENTITY:
2583 case LY_STMT_IMPORT:
2584 case LY_STMT_INCLUDE:
2585 case LY_STMT_LEAF:
2586 case LY_STMT_LEAF_LIST:
2587 case LY_STMT_LIST:
2588 case LY_STMT_MANDATORY:
2589 case LY_STMT_MAX_ELEMENTS:
2590 case LY_STMT_MIN_ELEMENTS:
2591 case LY_STMT_MODIFIER:
2592 case LY_STMT_MODULE:
2593 case LY_STMT_NOTIFICATION:
2594 case LY_STMT_ORDERED_BY:
2595 case LY_STMT_POSITION:
2596 case LY_STMT_PREFIX:
2597 case LY_STMT_REFINE:
2598 case LY_STMT_REQUIRE_INSTANCE:
2599 case LY_STMT_REVISION:
2600 case LY_STMT_REVISION_DATE:
2601 case LY_STMT_RPC:
2602 case LY_STMT_STATUS:
2603 case LY_STMT_SUBMODULE:
2604 case LY_STMT_TYPE:
2605 case LY_STMT_TYPEDEF:
2606 case LY_STMT_USES:
2607 case LY_STMT_VALUE:
2608 case LY_STMT_YANG_VERSION:
2609 case LY_STMT_YIN_ELEMENT:
2610 return LY_STMT_FLAG_ID;
2611 case LY_STMT_CONTACT:
2612 case LY_STMT_DESCRIPTION:
2613 case LY_STMT_ERROR_MESSAGE:
2614 case LY_STMT_ORGANIZATION:
2615 case LY_STMT_REFERENCE:
2616 return LY_STMT_FLAG_YIN;
2617 }
2618
2619 return 0;
2620}
Jan Kundrát026737b2023-09-05 13:03:30 +02002621
2622void
2623ly_check_module_filename(const struct ly_ctx *ctx, const char *name, const char *revision, const char *filename)
2624{
2625 const char *basename, *rev, *dot;
2626 size_t len;
2627
2628 /* check that name and revision match filename */
2629 basename = strrchr(filename, '/');
Jan Kundrát35c145a2023-08-31 21:36:18 +02002630#ifdef _WIN32
2631 const char *backslash = strrchr(filename, '\\');
2632
2633 if (!basename || (basename && backslash && (backslash > basename))) {
2634 basename = backslash;
2635 }
2636#endif
Jan Kundrát026737b2023-09-05 13:03:30 +02002637 if (!basename) {
2638 basename = filename;
2639 } else {
2640 basename++; /* leading slash */
2641 }
2642 rev = strchr(basename, '@');
2643 dot = strrchr(basename, '.');
2644
2645 /* name */
2646 len = strlen(name);
2647 if (strncmp(basename, name, len) ||
2648 ((rev && (rev != &basename[len])) || (!rev && (dot != &basename[len])))) {
2649 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", basename, name);
2650 }
2651 if (rev) {
2652 len = dot - ++rev;
2653 if (!revision || (len != LY_REV_SIZE - 1) || strncmp(revision, rev, len)) {
2654 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", basename,
2655 revision ? revision : "none");
2656 }
2657 }
2658}