blob: b676cba8582d5007dc64ab48b97eb588e35e53ec [file] [log] [blame]
Radek Krejci86d106e2018-10-18 09:53:19 +02001/**
Michal Vasko59892dd2022-05-13 11:02:30 +02002 * @file tree_schema_common.c
Radek Krejci86d106e2018-10-18 09:53:19 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko59892dd2022-05-13 11:02:30 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
5 * @brief Parsing and validation common functions for schema trees
Radek Krejci86d106e2018-10-18 09:53:19 +02006 *
Michal Vasko59892dd2022-05-13 11:02:30 +02007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejci86d106e2018-10-18 09:53:19 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
Radek Krejci535ea9f2020-05-29 16:01:05 +020015
16#define _GNU_SOURCE
Radek Krejci86d106e2018-10-18 09:53:19 +020017
Radek Krejcie7b95092019-05-15 11:03:07 +020018#include <assert.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020019#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010020#include <stddef.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <stdint.h>
Radek Krejci9ed7a192018-10-31 16:23:51 +010022#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020024#include <time.h>
25
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020027#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "context.h"
Radek Krejci77114102021-03-10 15:21:57 +010029#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010031#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020032#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010033#include "log.h"
Michal Vasko69730152020-10-09 16:30:07 +020034#include "parser_schema.h"
Michal Vasko962b6cd2020-12-08 10:07:49 +010035#include "schema_compile.h"
Michal Vasko79135ae2020-12-16 10:08:35 +010036#include "schema_features.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "set.h"
38#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010039#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020040#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020041#include "tree_schema_internal.h"
42
Radek Krejci85747952019-06-07 16:43:43 +020043LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +020044lysp_check_prefix(struct lysp_ctx *ctx, struct lysp_import *imports, const char *module_prefix, const char **value)
Radek Krejci86d106e2018-10-18 09:53:19 +020045{
46 struct lysp_import *i;
47
Michal Vasko69730152020-10-09 16:30:07 +020048 if (module_prefix && (&module_prefix != value) && !strcmp(module_prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010049 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value);
Radek Krejci86d106e2018-10-18 09:53:19 +020050 return LY_EEXIST;
51 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +010052 LY_ARRAY_FOR(imports, struct lysp_import, i) {
Michal Vasko69730152020-10-09 16:30:07 +020053 if (i->prefix && (&i->prefix != value) && !strcmp(i->prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010054 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +010055 return LY_EEXIST;
Radek Krejci86d106e2018-10-18 09:53:19 +020056 }
57 }
58 return LY_SUCCESS;
59}
60
61LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +020062lysp_check_date(struct lysp_ctx *ctx, const char *date, size_t date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +020063{
Radek Krejci86d106e2018-10-18 09:53:19 +020064 struct tm tm, tm_;
65 char *r;
66
Michal Vaskob425bb32022-11-08 10:49:36 +010067 LY_CHECK_ARG_RET(PARSER_CTX(ctx), date, LY_EINVAL);
68
69 if (date_len != LY_REV_SIZE - 1) {
70 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid length %" PRIu32 " of a date.", (uint32_t)date_len);
71 return LY_EINVAL;
72 }
Radek Krejci86d106e2018-10-18 09:53:19 +020073
Radek Krejcif13b87b2020-12-01 22:02:17 +010074 /* check format: YYYY-MM-DD */
Radek Krejci1deb5be2020-08-26 16:43:36 +020075 for (uint8_t i = 0; i < date_len; i++) {
Michal Vasko69730152020-10-09 16:30:07 +020076 if ((i == 4) || (i == 7)) {
Radek Krejci86d106e2018-10-18 09:53:19 +020077 if (date[i] != '-') {
78 goto error;
79 }
80 } else if (!isdigit(date[i])) {
81 goto error;
82 }
83 }
84
85 /* check content, e.g. 2018-02-31 */
86 memset(&tm, 0, sizeof tm);
87 r = strptime(date, "%Y-%m-%d", &tm);
Michal Vasko69730152020-10-09 16:30:07 +020088 if (!r || (r != &date[LY_REV_SIZE - 1])) {
Radek Krejci86d106e2018-10-18 09:53:19 +020089 goto error;
90 }
91 memcpy(&tm_, &tm, sizeof tm);
Michal Vasko82f32822022-08-17 10:54:35 +020092
93 /* DST may move the hour back resulting in a different day */
94 tm_.tm_hour = 1;
95
Radek Krejci86d106e2018-10-18 09:53:19 +020096 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
97 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
98 /* checking days is enough, since other errors
99 * have been checked by strptime() */
100 goto error;
101 }
102
103 return LY_SUCCESS;
104
105error:
Radek Krejcid33273d2018-10-25 14:55:52 +0200106 if (stmt) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200107 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)date_len, date, stmt);
Radek Krejcid33273d2018-10-25 14:55:52 +0200108 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200109 return LY_EINVAL;
110}
111
112void
113lysp_sort_revisions(struct lysp_revision *revs)
114{
Radek Krejci857189e2020-09-01 13:26:36 +0200115 LY_ARRAY_COUNT_TYPE i, r;
Radek Krejci86d106e2018-10-18 09:53:19 +0200116 struct lysp_revision rev;
117
Radek Krejcic7d13e32020-12-09 12:32:24 +0100118 for (i = 1, r = 0; i < LY_ARRAY_COUNT(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200119 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200120 r = i;
121 }
122 }
123
124 if (r) {
125 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200126 memcpy(&rev, &revs[0], sizeof rev);
127 memcpy(&revs[0], &revs[r], sizeof rev);
128 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200129 }
130}
Radek Krejci151a5b72018-10-19 14:21:44 +0200131
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200132LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200133lysp_check_enum_name(struct lysp_ctx *ctx, const char *name, size_t name_len)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100134{
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200135 if (!name_len) {
136 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
137 return LY_EVALID;
138 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
139 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
140 (int)name_len, name);
141 return LY_EVALID;
142 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200143 for (uint32_t u = 0; u < name_len; ++u) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200144 if (iscntrl(name[u])) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200145 LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided "
146 "(\"%.*s\", character number %" PRIu32 ").", (int)name_len, name, u + 1);
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200147 break;
148 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100149 }
150 }
151
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200152 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100153}
154
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200155/**
156 * @brief Learn built-in type from its name.
157 *
158 * @param[in] name Type name.
159 * @param[in] len Length of @p name.
160 * @return Built-in data type, ::LY_TYPE_UNKNOWN if none matches.
161 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100162static LY_DATA_TYPE
163lysp_type_str2builtin(const char *name, size_t len)
164{
165 if (len >= 4) { /* otherwise it does not match any built-in type */
166 if (name[0] == 'b') {
167 if (name[1] == 'i') {
Michal Vasko69730152020-10-09 16:30:07 +0200168 if ((len == 6) && !strncmp(&name[2], "nary", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100169 return LY_TYPE_BINARY;
Michal Vasko69730152020-10-09 16:30:07 +0200170 } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100171 return LY_TYPE_BITS;
172 }
Michal Vasko69730152020-10-09 16:30:07 +0200173 } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100174 return LY_TYPE_BOOL;
175 }
176 } else if (name[0] == 'd') {
Michal Vasko69730152020-10-09 16:30:07 +0200177 if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100178 return LY_TYPE_DEC64;
179 }
180 } else if (name[0] == 'e') {
Michal Vasko69730152020-10-09 16:30:07 +0200181 if ((len == 5) && !strncmp(&name[1], "mpty", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100182 return LY_TYPE_EMPTY;
Michal Vasko69730152020-10-09 16:30:07 +0200183 } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100184 return LY_TYPE_ENUM;
185 }
186 } else if (name[0] == 'i') {
187 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200188 if ((len == 4) && !strncmp(&name[2], "t8", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100189 return LY_TYPE_INT8;
190 } else if (len == 5) {
191 if (!strncmp(&name[2], "t16", 3)) {
192 return LY_TYPE_INT16;
193 } else if (!strncmp(&name[2], "t32", 3)) {
194 return LY_TYPE_INT32;
195 } else if (!strncmp(&name[2], "t64", 3)) {
196 return LY_TYPE_INT64;
197 }
Michal Vasko69730152020-10-09 16:30:07 +0200198 } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100199 return LY_TYPE_INST;
200 }
Michal Vasko69730152020-10-09 16:30:07 +0200201 } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100202 return LY_TYPE_IDENT;
203 }
204 } else if (name[0] == 'l') {
Michal Vasko69730152020-10-09 16:30:07 +0200205 if ((len == 7) && !strncmp(&name[1], "eafref", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100206 return LY_TYPE_LEAFREF;
207 }
208 } else if (name[0] == 's') {
Michal Vasko69730152020-10-09 16:30:07 +0200209 if ((len == 6) && !strncmp(&name[1], "tring", 5)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100210 return LY_TYPE_STRING;
211 }
212 } else if (name[0] == 'u') {
213 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200214 if ((len == 5) && !strncmp(&name[2], "ion", 3)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100215 return LY_TYPE_UNION;
216 }
Michal Vasko69730152020-10-09 16:30:07 +0200217 } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) {
218 if ((len == 5) && (name[4] == '8')) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100219 return LY_TYPE_UINT8;
220 } else if (len == 6) {
221 if (!strncmp(&name[4], "16", 2)) {
222 return LY_TYPE_UINT16;
223 } else if (!strncmp(&name[4], "32", 2)) {
224 return LY_TYPE_UINT32;
225 } else if (!strncmp(&name[4], "64", 2)) {
226 return LY_TYPE_UINT64;
227 }
228 }
229 }
230 }
231 }
232
233 return LY_TYPE_UNKNOWN;
234}
235
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200236/**
237 * @brief Find a typedef in a sized array.
238 *
239 * @param[in] name Typedef name.
240 * @param[in] typedefs Sized array of typedefs.
241 * @return Found typedef, NULL if none.
242 */
243static const struct lysp_tpdf *
244lysp_typedef_match(const char *name, const struct lysp_tpdf *typedefs)
245{
246 LY_ARRAY_COUNT_TYPE u;
247
248 LY_ARRAY_FOR(typedefs, u) {
249 if (!strcmp(name, typedefs[u].name)) {
250 /* match */
251 return &typedefs[u];
252 }
253 }
254 return NULL;
255}
256
Radek Krejcibbe09a92018-11-08 09:36:54 +0100257LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +0100258lysp_type_find(const char *id, struct lysp_node *start_node, const struct lysp_module *start_module,
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200259 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 +0100260{
261 const char *str, *name;
262 struct lysp_tpdf *typedefs;
Michal Vasko193dacd2022-10-13 08:43:05 +0200263 const struct lysp_tpdf *ext_typedefs;
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100264 const struct lys_module *mod;
Michal Vaskoa99b3572021-02-01 11:54:58 +0100265 const struct lysp_module *local_module;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200266 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100267
268 assert(id);
269 assert(start_module);
270 assert(tpdf);
271 assert(node);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100272
Radek Krejci4f28eda2018-11-12 11:46:16 +0100273 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100274 str = strchr(id, ':');
275 if (str) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200276 mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_VALUE_SCHEMA, (void *)start_module);
Michal Vaskoa99b3572021-02-01 11:54:58 +0100277 local_module = mod ? mod->parsed : NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100278 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100279 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100280 } else {
Michal Vaskoa99b3572021-02-01 11:54:58 +0100281 local_module = start_module;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100282 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100283
284 /* check for built-in types */
285 *type = lysp_type_str2builtin(name, strlen(name));
286 if (*type) {
287 *tpdf = NULL;
288 return LY_SUCCESS;
289 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100290 }
Michal Vaskoa99b3572021-02-01 11:54:58 +0100291 LY_CHECK_RET(!local_module, LY_ENOTFOUND);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100292
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200293 if (local_module == start_module) {
294 if (start_node) {
295 /* search typedefs in parent's nodes */
296 for (*node = start_node; *node; *node = (*node)->parent) {
297 *tpdf = lysp_typedef_match(name, lysp_node_typedefs(*node));
298 if (*tpdf) {
299 /* match */
300 return LY_SUCCESS;
301 }
302 }
303 }
304
305 if (ext) {
306 /* search typedefs directly in the extension */
Michal Vaskofbd037c2022-11-08 10:34:20 +0100307 lyplg_ext_parsed_get_storage(ext, LY_STMT_TYPEDEF, sizeof ext_typedefs, (const void **)&ext_typedefs);
Michal Vasko193dacd2022-10-13 08:43:05 +0200308 if ((*tpdf = lysp_typedef_match(name, ext_typedefs))) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100309 /* match */
310 return LY_SUCCESS;
311 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100312 }
313 }
314
Michal Vasko915e5442021-06-08 14:59:21 +0200315 /* go to main module if in submodule */
316 local_module = local_module->mod->parsed;
317
Radek Krejcibbe09a92018-11-08 09:36:54 +0100318 /* search in top-level typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100319 if (local_module->typedefs) {
320 LY_ARRAY_FOR(local_module->typedefs, u) {
321 if (!strcmp(name, local_module->typedefs[u].name)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100322 /* match */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100323 *tpdf = &local_module->typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100324 return LY_SUCCESS;
325 }
326 }
327 }
328
Michal Vasko915e5442021-06-08 14:59:21 +0200329 /* search in all submodules' typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100330 LY_ARRAY_FOR(local_module->includes, u) {
331 typedefs = local_module->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100332 LY_ARRAY_FOR(typedefs, v) {
333 if (!strcmp(name, typedefs[v].name)) {
334 /* match */
335 *tpdf = &typedefs[v];
336 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100337 }
338 }
339 }
340
341 return LY_ENOTFOUND;
342}
343
Michal Vaskob36053d2020-03-26 15:49:30 +0100344/**
aPiecekdc12b9f2021-06-25 10:55:47 +0200345 * @brief Insert @p name to hash table and if @p name has already
346 * been added, then log an error.
347 *
348 * This function is used to detect duplicate names.
349 *
350 * @param[in,out] ctx Context to log the error.
351 * @param[in,out] ht Hash table with top-level names.
352 * @param[in] name Inserted top-level identifier.
Michal Vasko8efac242023-03-30 08:24:56 +0200353 * @param[in] statement The name of the statement type from which @p name originated (eg typedef, feature, ...).
aPiecekdc12b9f2021-06-25 10:55:47 +0200354 * @param[in] err_detail Optional error specification.
355 * @return LY_ERR, but LY_EEXIST is mapped to LY_EVALID.
356 */
357static LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200358lysp_check_dup_ht_insert(struct lysp_ctx *ctx, struct ly_ht *ht, const char *name, const char *statement,
359 const char *err_detail)
aPiecekdc12b9f2021-06-25 10:55:47 +0200360{
361 LY_ERR ret;
362 uint32_t hash;
363
Michal Vaskoae130f52023-04-20 14:25:16 +0200364 hash = lyht_hash(name, strlen(name));
aPiecekdc12b9f2021-06-25 10:55:47 +0200365 ret = lyht_insert(ht, &name, hash, NULL);
366 if (ret == LY_EEXIST) {
367 if (err_detail) {
368 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT2, name, statement, err_detail);
369 } else {
370 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, name, statement);
371 }
372 ret = LY_EVALID;
373 }
374
375 return ret;
376}
377
378/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100379 * @brief Check name of a new type to avoid name collisions.
380 *
381 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
382 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
383 * @param[in] tpdf Typedef definition to check.
384 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
aPiecekc7594b72021-06-29 09:00:03 +0200385 * typedefs are checked, caller is supposed to free the table.
386 * @return LY_EVALID in case of collision, LY_SUCCESS otherwise.
Radek Krejcibbe09a92018-11-08 09:36:54 +0100387 */
388static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200389lysp_check_dup_typedef(struct lysp_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
Michal Vasko8efac242023-03-30 08:24:56 +0200390 struct ly_ht *tpdfs_global)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100391{
392 struct lysp_node *parent;
393 uint32_t hash;
394 size_t name_len;
395 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200396 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100397 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100398
399 assert(ctx);
400 assert(tpdf);
401
402 name = tpdf->name;
403 name_len = strlen(name);
404
Radek Krejci4f28eda2018-11-12 11:46:16 +0100405 if (lysp_type_str2builtin(name, name_len)) {
aPiecekc7594b72021-06-29 09:00:03 +0200406 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
407 "Duplicate identifier \"%s\" of typedef statement - name collision with a built-in type.", name);
408 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100409 }
410
411 /* check locally scoped typedefs (avoid name shadowing) */
412 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100413 typedefs = lysp_node_typedefs(node);
414 LY_ARRAY_FOR(typedefs, u) {
415 if (&typedefs[u] == tpdf) {
416 break;
417 }
418 if (!strcmp(name, typedefs[u].name)) {
aPiecekc7594b72021-06-29 09:00:03 +0200419 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
420 "Duplicate identifier \"%s\" of typedef statement - name collision with sibling type.", name);
421 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100422 }
423 }
424 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200425 for (parent = node->parent; parent; parent = parent->parent) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200426 if (lysp_typedef_match(name, lysp_node_typedefs(parent))) {
aPiecekc7594b72021-06-29 09:00:03 +0200427 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
428 "Duplicate identifier \"%s\" of typedef statement - name collision with another scoped type.", name);
429 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100430 }
431 }
432 }
433
434 /* check collision with the top-level typedefs */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100435 if (node) {
Michal Vaskoae130f52023-04-20 14:25:16 +0200436 hash = lyht_hash(name, name_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100437 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
aPiecekc7594b72021-06-29 09:00:03 +0200438 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
439 "Duplicate identifier \"%s\" of typedef statement - scoped type collide with a top-level type.", name);
440 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100441 }
442 } else {
aPiecekc7594b72021-06-29 09:00:03 +0200443 LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, tpdfs_global, name, "typedef",
444 "name collision with another top-level type"));
Radek Krejci3b1f9292018-11-08 10:58:35 +0100445 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
446 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
447 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100448 }
449
450 return LY_SUCCESS;
451}
452
Radek Krejci857189e2020-09-01 13:26:36 +0200453/**
454 * @brief Compare identifiers.
Michal Vasko62524a92021-02-26 10:08:50 +0100455 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200456 */
457static ly_bool
458lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Radek Krejcibbe09a92018-11-08 09:36:54 +0100459{
Michal Vasko11ac39a2021-07-23 12:46:56 +0200460 char *id1, *id2;
461
462 id1 = *(char **)val1;
463 id2 = *(char **)val2;
464
465 return strcmp(id1, id2) == 0 ? 1 : 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100466}
467
468LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200469lysp_check_dup_typedefs(struct lysp_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100470{
Michal Vasko8efac242023-03-30 08:24:56 +0200471 struct ly_ht *ids_global;
Radek Krejci0fb28562018-12-13 15:17:37 +0100472 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200473 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200474 uint32_t i;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100475 LY_ERR ret = LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100476
477 /* check name collisions - typedefs and groupings */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100478 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200479 LY_ARRAY_FOR(mod->typedefs, v) {
aPieceke1fbd952021-06-29 08:12:55 +0200480 ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100481 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100482 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200483 LY_ARRAY_FOR(mod->includes, v) {
484 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200485 ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100486 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci3b1f9292018-11-08 10:58:35 +0100487 }
488 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200489 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
490 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
491 LY_ARRAY_FOR(typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200492 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 +0100493 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100494 }
495 }
Michal Vasko405cc9e2020-12-01 12:01:27 +0100496
Radek Krejcibbe09a92018-11-08 09:36:54 +0100497cleanup:
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100498 lyht_free(ids_global, NULL);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100499 return ret;
500}
501
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200502static const struct lysp_node_grp *
503lysp_grouping_match(const char *name, struct lysp_node *node)
504{
505 const struct lysp_node_grp *groupings, *grp_iter;
506
507 groupings = lysp_node_groupings(node);
508 LY_LIST_FOR(groupings, grp_iter) {
509 if (!strcmp(name, grp_iter->name)) {
510 /* match */
511 return grp_iter;
512 }
513 }
514
515 return NULL;
516}
517
aPiecek63e080d2021-06-29 13:53:28 +0200518/**
519 * @brief Check name of a new grouping to avoid name collisions.
520 *
521 * @param[in] ctx Parser context, module where the grouping is being defined is taken from here.
522 * @param[in] node Schema node where the grouping is being defined, NULL in case of a top-level grouping.
523 * @param[in] grp Grouping definition to check.
524 * @param[in,out] grps_global Initialized hash table to store temporary data between calls. When the module's
525 * groupings are checked, caller is supposed to free the table.
526 * @return LY_EVALID in case of collision, LY_SUCCESS otherwise.
527 */
528static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200529lysp_check_dup_grouping(struct lysp_ctx *ctx, struct lysp_node *node, const struct lysp_node_grp *grp,
Michal Vasko8efac242023-03-30 08:24:56 +0200530 struct ly_ht *grps_global)
aPiecek63e080d2021-06-29 13:53:28 +0200531{
532 struct lysp_node *parent;
533 uint32_t hash;
534 size_t name_len;
535 const char *name;
536 const struct lysp_node_grp *groupings, *grp_iter;
537
538 assert(ctx);
539 assert(grp);
540
541 name = grp->name;
542 name_len = strlen(name);
543
544 /* check locally scoped groupings (avoid name shadowing) */
545 if (node) {
546 groupings = lysp_node_groupings(node);
547 LY_LIST_FOR(groupings, grp_iter) {
548 if (grp_iter == grp) {
549 break;
550 }
551 if (!strcmp(name, grp_iter->name)) {
552 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
553 "Duplicate identifier \"%s\" of grouping statement - name collision with sibling grouping.", name);
554 return LY_EVALID;
555 }
556 }
557 /* search grouping in parent's nodes */
558 for (parent = node->parent; parent; parent = parent->parent) {
559 if (lysp_grouping_match(name, parent)) {
560 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
561 "Duplicate identifier \"%s\" of grouping statement - name collision with another scoped grouping.", name);
562 return LY_EVALID;
563 }
564 }
565 }
566
567 /* check collision with the top-level groupings */
568 if (node) {
Michal Vaskoae130f52023-04-20 14:25:16 +0200569 hash = lyht_hash(name, name_len);
aPiecek63e080d2021-06-29 13:53:28 +0200570 if (!lyht_find(grps_global, &name, hash, NULL)) {
571 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
572 "Duplicate identifier \"%s\" of grouping statement - scoped grouping collide with a top-level grouping.", name);
573 return LY_EVALID;
574 }
575 } else {
576 LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, grps_global, name, "grouping",
577 "name collision with another top-level grouping"));
578 }
579
580 return LY_SUCCESS;
581}
582
583LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200584lysp_check_dup_groupings(struct lysp_ctx *ctx, struct lysp_module *mod)
aPiecek63e080d2021-06-29 13:53:28 +0200585{
Michal Vasko8efac242023-03-30 08:24:56 +0200586 struct ly_ht *ids_global;
aPiecek63e080d2021-06-29 13:53:28 +0200587 const struct lysp_node_grp *groupings, *grp_iter;
588 LY_ARRAY_COUNT_TYPE u;
589 uint32_t i;
590 LY_ERR ret = LY_SUCCESS;
591
592 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
593 LY_LIST_FOR(mod->groupings, grp_iter) {
594 ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global);
595 LY_CHECK_GOTO(ret, cleanup);
596 }
597 LY_ARRAY_FOR(mod->includes, u) {
598 LY_LIST_FOR(mod->includes[u].submodule->groupings, grp_iter) {
599 ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global);
600 LY_CHECK_GOTO(ret, cleanup);
601 }
602 }
603 for (i = 0; i < ctx->grps_nodes.count; ++i) {
604 groupings = lysp_node_groupings((struct lysp_node *)ctx->grps_nodes.objs[i]);
605 LY_LIST_FOR(groupings, grp_iter) {
606 ret = lysp_check_dup_grouping(ctx, (struct lysp_node *)ctx->grps_nodes.objs[i], grp_iter, ids_global);
607 LY_CHECK_GOTO(ret, cleanup);
608 }
609 }
610
611cleanup:
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100612 lyht_free(ids_global, NULL);
aPiecek63e080d2021-06-29 13:53:28 +0200613 return ret;
614}
615
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100616static ly_bool
617ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
618{
619 void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
620
621 return ptr1 == ptr2 ? 1 : 0;
622}
623
624LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200625lysp_check_dup_features(struct lysp_ctx *ctx, struct lysp_module *mod)
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100626{
627 LY_ARRAY_COUNT_TYPE u;
Michal Vasko8efac242023-03-30 08:24:56 +0200628 struct ly_ht *ht;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100629 struct lysp_feature *f;
aPiecekdc12b9f2021-06-25 10:55:47 +0200630 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100631
aPiecekf6203072021-06-25 10:58:26 +0200632 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100633 LY_CHECK_RET(!ht, LY_EMEM);
634
635 /* add all module features into a hash table */
636 LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200637 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
638 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200639 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100640 }
641
642 /* add all submodule features into a hash table */
643 LY_ARRAY_FOR(mod->includes, u) {
644 LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200645 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
646 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200647 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100648 }
649 }
650
651cleanup:
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100652 lyht_free(ht, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100653 return ret;
654}
655
656LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200657lysp_check_dup_identities(struct lysp_ctx *ctx, struct lysp_module *mod)
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100658{
659 LY_ARRAY_COUNT_TYPE u;
Michal Vasko8efac242023-03-30 08:24:56 +0200660 struct ly_ht *ht;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100661 struct lysp_ident *i;
aPiecekdc12b9f2021-06-25 10:55:47 +0200662 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100663
aPiecekf6203072021-06-25 10:58:26 +0200664 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100665 LY_CHECK_RET(!ht, LY_EMEM);
666
667 /* add all module identities into a hash table */
668 LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200669 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
670 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200671 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100672 }
673
674 /* add all submodule identities into a hash table */
675 LY_ARRAY_FOR(mod->includes, u) {
676 LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200677 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
678 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200679 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100680 }
681 }
682
683cleanup:
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100684 lyht_free(ht, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100685 return ret;
686}
687
Radek Krejci9ed7a192018-10-31 16:23:51 +0100688struct lysp_load_module_check_data {
689 const char *name;
690 const char *revision;
691 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200692 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100693};
694
695static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100696lysp_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 +0100697{
698 struct lysp_load_module_check_data *info = data;
Jan Kundrát026737b2023-09-05 13:03:30 +0200699 const char *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200700 uint8_t latest_revision;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100701 struct lysp_revision *revs;
702
703 name = mod ? mod->mod->name : submod->name;
704 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200705 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100706
707 if (info->name) {
708 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100709 if (strcmp(info->name, name)) {
710 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100711 return LY_EINVAL;
712 }
713 }
714 if (info->revision) {
715 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100716 if (!revs || strcmp(info->revision, revs[0].date)) {
717 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200718 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100719 return LY_EINVAL;
720 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200721 } else if (!latest_revision) {
722 /* do not log, we just need to drop the schema and use the latest revision from the context */
723 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100724 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100725 if (submod) {
726 assert(info->submoduleof);
727
Radek Krejci9ed7a192018-10-31 16:23:51 +0100728 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200729 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100730 LOGVAL(ctx, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200731 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100732 return LY_EVALID;
733 }
734 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100735 if (submod->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100736 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100737 return LY_EVALID;
738 }
739 }
740 if (info->path) {
Jan Kundrát026737b2023-09-05 13:03:30 +0200741 ly_check_module_filename(ctx, name, revs ? revs[0].date : NULL, info->path);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100742 }
743 return LY_SUCCESS;
744}
745
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200746/**
Michal Vasko4e205e82021-06-08 14:01:47 +0200747 * @brief Parse a (sub)module from a local file and add into the context.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200748 *
749 * This function does not check the presence of the (sub)module in context, it should be done before calling this function.
750 *
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200751 * @param[in] ctx libyang context where to work.
752 * @param[in] name Name of the (sub)module to load.
753 * @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 +0200754 * @param[in] main_ctx Parser context of the main module in case of loading submodule.
755 * @param[in] main_name Main module name in case of loading submodule.
756 * @param[in] required Module is required so error (even if the input file not found) are important. If 0, there is some
757 * 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 +0200758 * @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 +0200759 * @param[out] result Parsed YANG schema tree of the requested module (struct lys_module*) or submodule (struct lysp_submodule*).
760 * If it is a module, it is already in the context!
Michal Vasko4e205e82021-06-08 14:01:47 +0200761 * @return LY_SUCCESS on success.
Michal Vasko4e205e82021-06-08 14:01:47 +0200762 * @return LY_ERR on error.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200763 */
764static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200765lys_parse_localfile(struct ly_ctx *ctx, const char *name, const char *revision, struct lysp_ctx *main_ctx,
Michal Vaskodd992582021-06-10 14:34:57 +0200766 const char *main_name, ly_bool required, struct ly_set *new_mods, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100767{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200768 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100769 char *filepath = NULL;
770 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100771 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100772 LY_ERR ret = LY_SUCCESS;
773 struct lysp_load_module_check_data check_data = {0};
774
Michal Vasko87f1cf02021-06-08 14:02:47 +0200775 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name,
776 revision, &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200777 if (!filepath) {
778 if (required) {
779 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200780 revision ? revision : "");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200781 }
782 return LY_ENOTFOUND;
783 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100784
785 LOGVRB("Loading schema from \"%s\" file.", filepath);
786
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200787 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200788 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +0200789 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100790 check_data.name = name;
791 check_data.revision = revision;
792 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800793 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200794 if (main_ctx) {
aPiecekc3e26142021-06-22 14:25:49 +0200795 ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data, new_mods,
Michal Vasko69730152020-10-09 16:30:07 +0200796 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200797 } else {
Michal Vaskodd992582021-06-10 14:34:57 +0200798 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 +0200799
800 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200801 ly_in_free(in, 1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200802 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100803
804 *result = mod;
805
806 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200807
Radek Krejci9ed7a192018-10-31 16:23:51 +0100808cleanup:
809 free(filepath);
810 return ret;
811}
812
aPiecek4725aea2021-07-28 10:18:33 +0200813/**
814 * @brief Load module from searchdirs or from callback.
815 *
816 * @param[in] ctx libyang context where to work.
817 * @param[in] name Name of module to load.
818 * @param[in] revision Revision of module to load.
Michal Vaskobdac23f2022-01-12 14:02:35 +0100819 * @param[in] mod_latest Module with the latest revision found in context, otherwise set to NULL.
820 * @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 +0200821 * @param[out] mod Loaded module.
822 * @return LY_SUCCESS on success.
823 * @return LY_ERR on error.
824 */
825static LY_ERR
826lys_parse_load_from_clb_or_file(struct ly_ctx *ctx, const char *name, const char *revision,
827 struct lys_module *mod_latest, struct ly_set *new_mods, struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200828{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100829 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200830 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200831
Radek Krejci9ed7a192018-10-31 16:23:51 +0100832 void (*module_data_free)(void *module_data, void *user_data) = NULL;
833 struct lysp_load_module_check_data check_data = {0};
Michal Vasko63f3d842020-07-08 10:10:14 +0200834 struct ly_in *in;
Radek Krejci086c7132018-10-26 15:29:04 +0200835
Michal Vaskobdac23f2022-01-12 14:02:35 +0100836 *mod = NULL;
837
838 if (mod_latest && (!ctx->imp_clb || (mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB)) &&
839 ((ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) || (mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) {
840 /* we are not able to find a newer revision */
841 return LY_SUCCESS;
842 }
843
aPiecek4725aea2021-07-28 10:18:33 +0200844 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
845search_clb:
Michal Vaskobdac23f2022-01-12 14:02:35 +0100846 /* check there is a callback and should be called */
847 if (ctx->imp_clb && (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB))) {
848 if (!ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, &format, &module_data, &module_data_free)) {
849 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
850 check_data.name = name;
851 check_data.revision = revision;
852 lys_parse_in(ctx, in, format, lysp_load_module_check, &check_data, new_mods, mod);
853 ly_in_free(in, 0);
854 if (module_data_free) {
855 module_data_free((void *)module_data, ctx->imp_clb_data);
856 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200857 }
Radek Krejci0af46292019-01-11 16:02:31 +0100858 }
Michal Vaskobdac23f2022-01-12 14:02:35 +0100859 if (*mod && !revision) {
860 /* we got the latest revision module from the callback */
861 (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB;
862 } else if (!*mod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
aPiecek4725aea2021-07-28 10:18:33 +0200863 goto search_file;
864 }
865 } else {
866search_file:
Michal Vaskobdac23f2022-01-12 14:02:35 +0100867 /* check we can use searchdirs and that we should */
868 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) &&
869 (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) {
aPiecek4725aea2021-07-28 10:18:33 +0200870 lys_parse_localfile(ctx, name, revision, NULL, NULL, mod_latest ? 0 : 1, new_mods, (void **)mod);
871 }
Michal Vaskobdac23f2022-01-12 14:02:35 +0100872 if (*mod && !revision) {
873 /* we got the latest revision module in the searchdirs */
874 (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB;
875 } else if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
aPiecek4725aea2021-07-28 10:18:33 +0200876 goto search_clb;
877 }
Radek Krejci086c7132018-10-26 15:29:04 +0200878 }
879
aPiecek4725aea2021-07-28 10:18:33 +0200880 return LY_SUCCESS;
881}
882
883/**
aPiecekd4911ee2021-07-30 07:40:24 +0200884 * @brief Get module without revision according to priorities.
885 *
886 * 1. Search for the module with LYS_MOD_IMPORTED_REV.
887 * 2. Search for the implemented module.
888 * 3. Search for the latest module in the context.
889 *
890 * @param[in] ctx libyang context where module is searched.
891 * @param[in] name Name of the searched module.
aPiecekd4911ee2021-07-30 07:40:24 +0200892 * @return Found module from context or NULL.
893 */
894static struct lys_module *
Michal Vaskobdac23f2022-01-12 14:02:35 +0100895lys_get_module_without_revision(struct ly_ctx *ctx, const char *name)
aPiecekd4911ee2021-07-30 07:40:24 +0200896{
897 struct lys_module *mod, *mod_impl;
898 uint32_t index;
899
Michal Vaskocfcfd672023-02-14 11:20:42 +0100900 /* try to find module with LYS_MOD_IMPORTED_REV flag */
aPiecekd4911ee2021-07-30 07:40:24 +0200901 index = 0;
902 while ((mod = ly_ctx_get_module_iter(ctx, &index))) {
903 if (!strcmp(mod->name, name) && (mod->latest_revision & LYS_MOD_IMPORTED_REV)) {
904 break;
905 }
906 }
907
Michal Vaskocfcfd672023-02-14 11:20:42 +0100908 /* try to find the implemented module */
aPiecekd4911ee2021-07-30 07:40:24 +0200909 mod_impl = ly_ctx_get_module_implemented(ctx, name);
Michal Vaskocfcfd672023-02-14 11:20:42 +0100910 if (mod) {
911 if (mod_impl && (mod != mod_impl)) {
912 LOGVRB("Implemented module \"%s@%s\" is not used for import, revision \"%s\" is imported instead.",
913 mod_impl->name, mod_impl->revision, mod->revision);
914 }
aPiecekd4911ee2021-07-30 07:40:24 +0200915 return mod;
916 } else if (mod_impl) {
917 return mod_impl;
918 }
919
Michal Vaskocfcfd672023-02-14 11:20:42 +0100920 /* try to find the latest module in the current context */
aPiecekd4911ee2021-07-30 07:40:24 +0200921 mod = ly_ctx_get_module_latest(ctx, name);
aPiecekd4911ee2021-07-30 07:40:24 +0200922
923 return mod;
924}
925
926/**
aPiecek4725aea2021-07-28 10:18:33 +0200927 * @brief Check if a circular dependency exists between modules.
928 *
929 * @param[in] ctx libyang context for log an error.
Michal Vaskocfcfd672023-02-14 11:20:42 +0100930 * @param[in,out] mod Examined module which is set to NULL if the circular dependency is detected.
931 * @return LY_SUCCESS if no circular dependecy is detected, otherwise LY_EVALID.
aPiecek4725aea2021-07-28 10:18:33 +0200932 */
933static LY_ERR
934lys_check_circular_dependency(struct ly_ctx *ctx, struct lys_module **mod)
935{
936 if ((*mod) && (*mod)->parsed->parsing) {
937 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", (*mod)->name);
Michal Vasko4e205e82021-06-08 14:01:47 +0200938 *mod = NULL;
939 return LY_EVALID;
Michal Vasko0550b762020-11-24 18:04:08 +0100940 }
Radek Krejci086c7132018-10-26 15:29:04 +0200941
aPiecek4725aea2021-07-28 10:18:33 +0200942 return LY_SUCCESS;
943}
944
945LY_ERR
946lys_parse_load(struct ly_ctx *ctx, const char *name, const char *revision, struct ly_set *new_mods,
947 struct lys_module **mod)
948{
949 struct lys_module *mod_latest = NULL;
950
951 assert(mod && new_mods);
952
Michal Vasko0550b762020-11-24 18:04:08 +0100953 /*
aPiecek4725aea2021-07-28 10:18:33 +0200954 * Try to get the module from the context.
Michal Vasko0550b762020-11-24 18:04:08 +0100955 */
aPiecek4725aea2021-07-28 10:18:33 +0200956 if (revision) {
957 /* Get the specific revision. */
958 *mod = ly_ctx_get_module(ctx, name, revision);
959 } else {
Michal Vaskobdac23f2022-01-12 14:02:35 +0100960 /* Get the requested module in a suitable revision in the context. */
961 *mod = lys_get_module_without_revision(ctx, name);
962 if (*mod && !(*mod)->implemented && !((*mod)->latest_revision & LYS_MOD_IMPORTED_REV)) {
aPiecek4725aea2021-07-28 10:18:33 +0200963 /* Let us now search with callback and searchpaths to check
964 * if there is newer revision outside the context.
965 */
966 mod_latest = *mod;
967 *mod = NULL;
968 }
969 }
970
Michal Vasko0550b762020-11-24 18:04:08 +0100971 if (!*mod) {
aPiecek4725aea2021-07-28 10:18:33 +0200972 /* No suitable module in the context, try to load it. */
Michal Vaskobdac23f2022-01-12 14:02:35 +0100973 LY_CHECK_RET(lys_parse_load_from_clb_or_file(ctx, name, revision, mod_latest, new_mods, mod));
aPiecek4725aea2021-07-28 10:18:33 +0200974 if (!*mod && !mod_latest) {
Michal Vasko4e205e82021-06-08 14:01:47 +0200975 LOGVAL(ctx, LYVE_REFERENCE, "Loading \"%s\" module failed.", name);
Michal Vasko0550b762020-11-24 18:04:08 +0100976 return LY_EVALID;
977 }
aPiecek4725aea2021-07-28 10:18:33 +0200978
979 /* Update the latest_revision flag - here we have selected the latest available schema,
980 * consider that even the callback provides correct latest revision.
981 */
982 if (!*mod) {
983 LOGVRB("Newer revision than \"%s@%s\" not found, using this as the latest revision.",
984 mod_latest->name, mod_latest->revision);
985 assert(mod_latest->latest_revision & LYS_MOD_LATEST_REV);
986 mod_latest->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS;
987 *mod = mod_latest;
988 } else if (*mod && !revision && ((*mod)->latest_revision & LYS_MOD_LATEST_REV)) {
989 (*mod)->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS;
990 }
Radek Krejci086c7132018-10-26 15:29:04 +0200991 }
Radek Krejci086c7132018-10-26 15:29:04 +0200992
aPiecek9f8c7e72021-07-28 12:01:56 +0200993 /* Checking the circular dependence of imported modules. */
994 LY_CHECK_RET(lys_check_circular_dependency(ctx, mod));
995
Radek Krejci086c7132018-10-26 15:29:04 +0200996 return LY_SUCCESS;
997}
998
999LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001000lysp_check_stringchar(struct lysp_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +02001001{
1002 if (!is_yangutf8char(c)) {
1003 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
1004 return LY_EVALID;
1005 }
1006 return LY_SUCCESS;
1007}
1008
1009LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001010lysp_check_identifierchar(struct lysp_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +02001011{
Michal Vasko69730152020-10-09 16:30:07 +02001012 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +02001013 if (!is_yangidentstartchar(c)) {
aPiecekc89b2242021-05-14 14:19:11 +02001014 if ((c < UCHAR_MAX) && isprint(c)) {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001015 if (ctx) {
1016 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
1017 }
Michal Vasko7c769042021-03-25 12:20:49 +01001018 } else {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001019 if (ctx) {
1020 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character 0x%04x.", c);
1021 }
Michal Vasko7c769042021-03-25 12:20:49 +01001022 }
David Sedlák4a650532019-07-10 11:55:18 +02001023 return LY_EVALID;
1024 }
1025 if (prefix) {
1026 if (first) {
1027 (*prefix) = 0;
1028 } else {
1029 (*prefix) = 2;
1030 }
1031 }
Michal Vasko69730152020-10-09 16:30:07 +02001032 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +02001033 (*prefix) = 1;
1034 } else if (!is_yangidentchar(c)) {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001035 if (ctx) {
1036 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
1037 }
David Sedlák4a650532019-07-10 11:55:18 +02001038 return LY_EVALID;
1039 }
1040
1041 return LY_SUCCESS;
1042}
1043
Radek Krejci771928a2021-01-19 13:42:36 +01001044/**
1045 * @brief Try to find the parsed submodule in main module for the given include record.
1046 *
1047 * @param[in] pctx main parser context
1048 * @param[in] inc The include record with missing parsed submodule. According to include info try to find
1049 * the corresponding parsed submodule in main module's includes.
1050 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
1051 * @return LY_ENOT - the parsed module was not found.
1052 * @return LY_EVALID - YANG rule violation
1053 */
1054static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001055lysp_main_pmod_get_submodule(struct lysp_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +02001056{
Radek Krejci771928a2021-01-19 13:42:36 +01001057 LY_ARRAY_COUNT_TYPE i;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001058 struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed;
Michal Vasko69730152020-10-09 16:30:07 +02001059
Radek Krejci771928a2021-01-19 13:42:36 +01001060 LY_ARRAY_FOR(main_pmod->includes, i) {
1061 if (strcmp(main_pmod->includes[i].name, inc->name)) {
1062 continue;
1063 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001064
Radek Krejci771928a2021-01-19 13:42:36 +01001065 if (inc->rev[0] && strncmp(inc->rev, main_pmod->includes[i].rev, LY_REV_SIZE)) {
1066 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1067 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001068 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev,
Radek Krejci771928a2021-01-19 13:42:36 +01001069 main_pmod->includes[i].name, main_pmod->includes[i].rev, main_pmod->mod->name);
1070 return LY_EVALID;
1071 }
1072
1073 inc->submodule = main_pmod->includes[i].submodule;
1074 return inc->submodule ? LY_SUCCESS : LY_ENOT;
1075 }
1076
1077 if (main_pmod->version == LYS_VERSION_1_1) {
1078 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1079 "YANG 1.1 requires all submodules to be included from main module. "
1080 "But submodule \"%s\" includes submodule \"%s\" which is not included by main module \"%s\".",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001081 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->name, main_pmod->mod->name);
Radek Krejci771928a2021-01-19 13:42:36 +01001082 return LY_EVALID;
1083 } else {
1084 return LY_ENOT;
1085 }
1086}
1087
1088/**
Michal Vasko8a67eff2021-12-07 14:04:47 +01001089 * @brief Try to find the parsed submodule in currenlty parsed modules for the given include record.
1090 *
1091 * @param[in] pctx main parser context
1092 * @param[in] inc The include record with missing parsed submodule.
1093 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
1094 * @return LY_ENOT - the parsed module was not found.
1095 * @return LY_EVALID - YANG rule violation
1096 */
1097static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001098lysp_parsed_mods_get_submodule(struct lysp_ctx *pctx, struct lysp_include *inc)
Michal Vasko8a67eff2021-12-07 14:04:47 +01001099{
1100 uint32_t i;
1101 struct lysp_submodule *submod;
1102
1103 for (i = 0; i < pctx->parsed_mods->count - 1; ++i) {
1104 submod = pctx->parsed_mods->objs[i];
1105 if (!submod->is_submod) {
1106 continue;
1107 }
1108
1109 if (strcmp(submod->name, inc->name)) {
1110 continue;
1111 }
1112
1113 if (inc->rev[0] && submod->revs && strncmp(inc->rev, submod->revs[0].date, LY_REV_SIZE)) {
1114 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1115 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
1116 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev,
1117 submod->name, submod->revs[0].date, PARSER_CUR_PMOD(pctx)->mod->name);
1118 return LY_EVALID;
1119 }
1120
1121 inc->submodule = submod;
1122 return LY_SUCCESS;
1123 }
1124
1125 return LY_ENOT;
1126}
1127
1128/**
Radek Krejci771928a2021-01-19 13:42:36 +01001129 * @brief Make the copy of the given include record into the main module.
1130 *
1131 * YANG 1.0 does not require the main module to include all the submodules. Therefore, parsing submodules can cause
1132 * reallocating and extending the includes array in the main module by the submodules included only in submodules.
1133 *
1134 * @param[in] pctx main parser context
1135 * @param[in] inc Include record to copy into main module taken from @p pctx.
1136 * @return LY_ERR value.
1137 */
1138static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001139lysp_inject_submodule(struct lysp_ctx *pctx, struct lysp_include *inc)
Radek Krejci771928a2021-01-19 13:42:36 +01001140{
1141 LY_ARRAY_COUNT_TYPE i;
1142 struct lysp_include *inc_new, *inc_tofill = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001143 struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed;
Radek Krejci771928a2021-01-19 13:42:36 +01001144
1145 /* first, try to find the corresponding record with missing parsed submodule */
1146 LY_ARRAY_FOR(main_pmod->includes, i) {
1147 if (strcmp(main_pmod->includes[i].name, inc->name)) {
1148 continue;
1149 }
1150 inc_tofill = &main_pmod->includes[i];
1151 break;
1152 }
1153
1154 if (inc_tofill) {
1155 inc_tofill->submodule = inc->submodule;
1156 } else {
1157 LY_ARRAY_NEW_RET(PARSER_CTX(pctx), main_pmod->includes, inc_new, LY_EMEM);
1158
1159 inc_new->submodule = inc->submodule;
1160 DUP_STRING_RET(PARSER_CTX(pctx), inc->name, inc_new->name);
1161 DUP_STRING_RET(PARSER_CTX(pctx), inc->dsc, inc_new->dsc);
1162 DUP_STRING_RET(PARSER_CTX(pctx), inc->ref, inc_new->ref);
Radek Krejci771928a2021-01-19 13:42:36 +01001163 memcpy(inc_new->rev, inc->rev, LY_REV_SIZE);
1164 inc_new->injected = 1;
1165 }
1166 return LY_SUCCESS;
1167}
1168
1169LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001170lysp_load_submodules(struct lysp_ctx *pctx, struct lysp_module *pmod, struct ly_set *new_mods)
Radek Krejci771928a2021-01-19 13:42:36 +01001171{
1172 LY_ARRAY_COUNT_TYPE u;
1173 struct ly_ctx *ctx = PARSER_CTX(pctx);
1174
1175 LY_ARRAY_FOR(pmod->includes, u) {
Michal Vasko8a67eff2021-12-07 14:04:47 +01001176 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci771928a2021-01-19 13:42:36 +01001177 struct lysp_submodule *submod = NULL;
1178 struct lysp_include *inc = &pmod->includes[u];
1179
1180 if (inc->submodule) {
1181 continue;
1182 }
1183
1184 if (pmod->is_submod) {
1185 /* try to find the submodule in the main module or its submodules */
Michal Vasko8a67eff2021-12-07 14:04:47 +01001186 ret = lysp_main_pmod_get_submodule(pctx, inc);
1187 LY_CHECK_RET(ret != LY_ENOT, ret);
Radek Krejci771928a2021-01-19 13:42:36 +01001188 }
1189
Michal Vasko8a67eff2021-12-07 14:04:47 +01001190 /* try to use currently parsed submodule */
1191 r = lysp_parsed_mods_get_submodule(pctx, inc);
1192 LY_CHECK_RET(r != LY_ENOT, r);
1193
Radek Krejci771928a2021-01-19 13:42:36 +01001194 /* submodule not present in the main module, get the input data and parse it */
1195 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcidf549132021-01-21 10:32:32 +01001196search_clb:
Radek Krejci771928a2021-01-19 13:42:36 +01001197 if (ctx->imp_clb) {
1198 const char *submodule_data = NULL;
1199 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko26bbb272022-08-02 14:54:33 +02001200
Radek Krejci771928a2021-01-19 13:42:36 +01001201 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
1202 struct lysp_load_module_check_data check_data = {0};
1203 struct ly_in *in;
1204
Michal Vasko8a67eff2021-12-07 14:04:47 +01001205 if (ctx->imp_clb(PARSER_CUR_PMOD(pctx)->mod->name, NULL, inc->name,
Radek Krejci771928a2021-01-19 13:42:36 +01001206 inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
1207 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
1208 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
1209 check_data.name = inc->name;
1210 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001211 check_data.submoduleof = PARSER_CUR_PMOD(pctx)->mod->name;
aPiecekc3e26142021-06-22 14:25:49 +02001212 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, new_mods, &submod);
Radek Krejci771928a2021-01-19 13:42:36 +01001213
1214 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1215 * submodule's include into main module, where it is missing */
1216 inc = &pmod->includes[u];
1217
1218 ly_in_free(in, 0);
1219 if (submodule_data_free) {
1220 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
1221 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001222 }
1223 }
Radek Krejci771928a2021-01-19 13:42:36 +01001224 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1225 goto search_file;
1226 }
1227 } else {
Radek Krejcidf549132021-01-21 10:32:32 +01001228search_file:
Radek Krejci771928a2021-01-19 13:42:36 +01001229 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
1230 /* submodule was not received from the callback or there is no callback set */
Michal Vaskodd6d5a32022-07-14 13:54:58 +02001231 lys_parse_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, pctx->main_ctx,
1232 PARSER_CUR_PMOD(pctx->main_ctx)->mod->name, 1, new_mods, (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001233
Radek Krejci771928a2021-01-19 13:42:36 +01001234 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1235 * submodule's include into main module, where it is missing */
1236 inc = &pmod->includes[u];
1237 }
1238 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1239 goto search_clb;
1240 }
1241 }
1242 if (submod) {
1243 if (!inc->rev[0] && (submod->latest_revision == 1)) {
1244 /* update the latest_revision flag - here we have selected the latest available schema,
1245 * consider that even the callback provides correct latest revision */
1246 submod->latest_revision = 2;
1247 }
1248
1249 inc->submodule = submod;
1250 if (ret == LY_ENOT) {
1251 /* the submodule include is not present in YANG 1.0 main module - add it there */
1252 LY_CHECK_RET(lysp_inject_submodule(pctx, &pmod->includes[u]));
1253 }
1254 }
1255 if (!inc->submodule) {
1256 LOGVAL(ctx, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name,
Michal Vasko8a67eff2021-12-07 14:04:47 +01001257 PARSER_CUR_PMOD(pctx)->is_submod ? ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name :
1258 PARSER_CUR_PMOD(pctx)->mod->name);
Radek Krejci771928a2021-01-19 13:42:36 +01001259 return LY_EVALID;
1260 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001261 }
1262
1263 return LY_SUCCESS;
1264}
1265
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001266LIBYANG_API_DEF const struct lysc_when *
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001267lysc_has_when(const struct lysc_node *node)
1268{
Radek Krejci9a3823e2021-01-27 20:26:46 +01001269 struct lysc_when **when;
1270
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001271 if (!node) {
1272 return NULL;
1273 }
1274
1275 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001276 when = lysc_node_when(node);
1277 if (when) {
1278 return when[0];
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001279 }
1280 node = node->parent;
1281 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1282
1283 return NULL;
1284}
1285
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001286LIBYANG_API_DEF const struct lys_module *
Michal Vaskoef53c812021-10-13 10:21:03 +02001287lysc_owner_module(const struct lysc_node *node)
1288{
1289 if (!node) {
1290 return NULL;
1291 }
1292
1293 for ( ; node->parent; node = node->parent) {}
1294 return node->module;
1295}
1296
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001297LIBYANG_API_DEF const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001298lys_nodetype2str(uint16_t nodetype)
1299{
Michal Vaskod989ba02020-08-24 10:59:24 +02001300 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001301 case LYS_CONTAINER:
1302 return "container";
1303 case LYS_CHOICE:
1304 return "choice";
1305 case LYS_LEAF:
1306 return "leaf";
1307 case LYS_LEAFLIST:
1308 return "leaf-list";
1309 case LYS_LIST:
1310 return "list";
1311 case LYS_ANYXML:
1312 return "anyxml";
1313 case LYS_ANYDATA:
1314 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001315 case LYS_CASE:
1316 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001317 case LYS_RPC:
1318 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001319 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001320 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001321 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001322 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001323 case LYS_USES:
1324 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001325 default:
1326 return "unknown";
1327 }
1328}
1329
Radek Krejci693262f2019-04-29 15:23:20 +02001330const char *
1331lys_datatype2str(LY_DATA_TYPE basetype)
1332{
Michal Vaskod989ba02020-08-24 10:59:24 +02001333 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001334 case LY_TYPE_BINARY:
1335 return "binary";
1336 case LY_TYPE_UINT8:
1337 return "uint8";
1338 case LY_TYPE_UINT16:
1339 return "uint16";
1340 case LY_TYPE_UINT32:
1341 return "uint32";
1342 case LY_TYPE_UINT64:
1343 return "uint64";
1344 case LY_TYPE_STRING:
1345 return "string";
1346 case LY_TYPE_BITS:
1347 return "bits";
1348 case LY_TYPE_BOOL:
1349 return "boolean";
1350 case LY_TYPE_DEC64:
1351 return "decimal64";
1352 case LY_TYPE_EMPTY:
1353 return "empty";
1354 case LY_TYPE_ENUM:
1355 return "enumeration";
1356 case LY_TYPE_IDENT:
1357 return "identityref";
1358 case LY_TYPE_INST:
1359 return "instance-identifier";
1360 case LY_TYPE_LEAFREF:
1361 return "leafref";
1362 case LY_TYPE_UNION:
1363 return "union";
1364 case LY_TYPE_INT8:
1365 return "int8";
1366 case LY_TYPE_INT16:
1367 return "int16";
1368 case LY_TYPE_INT32:
1369 return "int32";
1370 case LY_TYPE_INT64:
1371 return "int64";
1372 default:
1373 return "unknown";
1374 }
1375}
1376
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001377LIBYANG_API_DEF const struct lysp_tpdf *
Radek Krejci056d0a82018-12-06 16:57:25 +01001378lysp_node_typedefs(const struct lysp_node *node)
1379{
Radek Krejci0fb28562018-12-13 15:17:37 +01001380 switch (node->nodetype) {
1381 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001382 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001383 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001384 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001385 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001386 return ((struct lysp_node_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001387 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001388 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001389 return ((struct lysp_node_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001390 case LYS_INPUT:
1391 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001392 return ((struct lysp_node_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001393 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001394 return ((struct lysp_node_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001395 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001396 return NULL;
1397 }
1398}
1399
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001400LIBYANG_API_DEF const struct lysp_node_grp *
Radek Krejci53ea6152018-12-13 15:21:15 +01001401lysp_node_groupings(const struct lysp_node *node)
1402{
1403 switch (node->nodetype) {
1404 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001405 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001406 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001407 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001408 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001409 return ((struct lysp_node_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001410 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001411 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001412 return ((struct lysp_node_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001413 case LYS_INPUT:
1414 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001415 return ((struct lysp_node_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001416 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001417 return ((struct lysp_node_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001418 default:
1419 return NULL;
1420 }
1421}
1422
Radek Krejci2a9fc652021-01-22 17:44:34 +01001423struct lysp_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001424lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001425{
1426 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001427
Radek Krejcibbe09a92018-11-08 09:36:54 +01001428 switch (node->nodetype) {
1429 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001430 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001431 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001432 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001433 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001434 return &((struct lysp_node_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001435 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001436 return &((struct lysp_node_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001437 default:
1438 return NULL;
1439 }
1440}
1441
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001442LIBYANG_API_DEF const struct lysp_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001443lysp_node_actions(const struct lysp_node *node)
1444{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001445 struct lysp_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001446
Michal Vasko22df3f02020-08-24 13:29:22 +02001447 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001448 if (actions) {
1449 return *actions;
1450 } else {
1451 return NULL;
1452 }
1453}
1454
Radek Krejci2a9fc652021-01-22 17:44:34 +01001455struct lysp_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001456lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001457{
1458 assert(node);
1459 switch (node->nodetype) {
1460 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001461 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001462 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001463 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001464 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001465 return &((struct lysp_node_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001466 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001467 return &((struct lysp_node_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001468 default:
1469 return NULL;
1470 }
1471}
1472
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001473LIBYANG_API_DEF const struct lysp_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001474lysp_node_notifs(const struct lysp_node *node)
1475{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001476 struct lysp_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001477
Michal Vasko22df3f02020-08-24 13:29:22 +02001478 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001479 if (notifs) {
1480 return *notifs;
1481 } else {
1482 return NULL;
1483 }
1484}
1485
Radek Krejcibbe09a92018-11-08 09:36:54 +01001486struct lysp_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001487lysp_node_child_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001488{
1489 assert(node);
1490 switch (node->nodetype) {
1491 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001492 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001493 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001494 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001495 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001496 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001497 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001498 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001499 case LYS_GROUPING:
Radek Krejci01180ac2021-01-27 08:48:22 +01001500 return &((struct lysp_node_grp *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001501 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001502 return &((struct lysp_node_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001503 case LYS_INPUT:
1504 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001505 return &((struct lysp_node_action_inout *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001506 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001507 return &((struct lysp_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001508 default:
1509 return NULL;
1510 }
1511}
1512
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001513LIBYANG_API_DEF const struct lysp_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001514lysp_node_child(const struct lysp_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01001515{
Michal Vasko544e58a2021-01-28 14:33:41 +01001516 struct lysp_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001517
1518 if (!node) {
1519 return NULL;
1520 }
1521
Michal Vasko544e58a2021-01-28 14:33:41 +01001522 child = lysp_node_child_p((struct lysp_node *)node);
1523 if (child) {
1524 return *child;
Radek Krejci056d0a82018-12-06 16:57:25 +01001525 } else {
1526 return NULL;
1527 }
1528}
1529
Radek Krejci9a3823e2021-01-27 20:26:46 +01001530struct lysp_restr **
1531lysp_node_musts_p(const struct lysp_node *node)
1532{
1533 if (!node) {
1534 return NULL;
1535 }
1536
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001537 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001538 case LYS_CONTAINER:
1539 return &((struct lysp_node_container *)node)->musts;
1540 case LYS_LEAF:
1541 return &((struct lysp_node_leaf *)node)->musts;
1542 case LYS_LEAFLIST:
1543 return &((struct lysp_node_leaflist *)node)->musts;
1544 case LYS_LIST:
1545 return &((struct lysp_node_list *)node)->musts;
1546 case LYS_ANYXML:
1547 case LYS_ANYDATA:
1548 return &((struct lysp_node_anydata *)node)->musts;
1549 case LYS_NOTIF:
1550 return &((struct lysp_node_notif *)node)->musts;
1551 case LYS_INPUT:
1552 case LYS_OUTPUT:
1553 return &((struct lysp_node_action_inout *)node)->musts;
1554 default:
1555 return NULL;
1556 }
1557}
1558
1559struct lysp_restr *
1560lysp_node_musts(const struct lysp_node *node)
1561{
1562 struct lysp_restr **musts;
1563
1564 musts = lysp_node_musts_p(node);
1565 if (musts) {
1566 return *musts;
1567 } else {
1568 return NULL;
1569 }
1570}
1571
1572struct lysp_when **
1573lysp_node_when_p(const struct lysp_node *node)
1574{
1575 if (!node) {
1576 return NULL;
1577 }
1578
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001579 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001580 case LYS_CONTAINER:
1581 return &((struct lysp_node_container *)node)->when;
1582 case LYS_CHOICE:
1583 return &((struct lysp_node_choice *)node)->when;
1584 case LYS_LEAF:
1585 return &((struct lysp_node_leaf *)node)->when;
1586 case LYS_LEAFLIST:
1587 return &((struct lysp_node_leaflist *)node)->when;
1588 case LYS_LIST:
1589 return &((struct lysp_node_list *)node)->when;
1590 case LYS_ANYXML:
1591 case LYS_ANYDATA:
1592 return &((struct lysp_node_anydata *)node)->when;
1593 case LYS_CASE:
1594 return &((struct lysp_node_case *)node)->when;
1595 case LYS_USES:
1596 return &((struct lysp_node_uses *)node)->when;
1597 case LYS_AUGMENT:
1598 return &((struct lysp_node_augment *)node)->when;
1599 default:
1600 return NULL;
1601 }
1602}
1603
1604struct lysp_when *
1605lysp_node_when(const struct lysp_node *node)
1606{
1607 struct lysp_when **when;
1608
1609 when = lysp_node_when_p(node);
1610 if (when) {
1611 return *when;
1612 } else {
1613 return NULL;
1614 }
1615}
1616
Radek Krejci2a9fc652021-01-22 17:44:34 +01001617struct lysc_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001618lysc_node_actions_p(struct lysc_node *node)
1619{
1620 assert(node);
1621 switch (node->nodetype) {
1622 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001623 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001624 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001625 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001626 default:
1627 return NULL;
1628 }
1629}
1630
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001631LIBYANG_API_DEF const struct lysc_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001632lysc_node_actions(const struct lysc_node *node)
1633{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001634 struct lysc_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001635
Michal Vasko22df3f02020-08-24 13:29:22 +02001636 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001637 if (actions) {
1638 return *actions;
1639 } else {
1640 return NULL;
1641 }
1642}
1643
Radek Krejci2a9fc652021-01-22 17:44:34 +01001644struct lysc_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001645lysc_node_notifs_p(struct lysc_node *node)
1646{
1647 assert(node);
1648 switch (node->nodetype) {
1649 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001650 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001651 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001652 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001653 default:
1654 return NULL;
1655 }
1656}
1657
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001658LIBYANG_API_DEF const struct lysc_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001659lysc_node_notifs(const struct lysc_node *node)
1660{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001661 struct lysc_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001662
Michal Vasko22df3f02020-08-24 13:29:22 +02001663 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001664 if (notifs) {
1665 return *notifs;
1666 } else {
1667 return NULL;
1668 }
1669}
1670
Radek Krejcibbe09a92018-11-08 09:36:54 +01001671struct lysc_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001672lysc_node_child_p(const struct lysc_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001673{
Michal Vasko544e58a2021-01-28 14:33:41 +01001674 assert(node && !(node->nodetype & (LYS_RPC | LYS_ACTION)));
1675
Radek Krejcibbe09a92018-11-08 09:36:54 +01001676 switch (node->nodetype) {
1677 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001678 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001679 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001680 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001681 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001682 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001683 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001684 return &((struct lysc_node_list *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001685 case LYS_INPUT:
1686 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001687 return &((struct lysc_node_action_inout *)node)->child;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001688 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001689 return &((struct lysc_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001690 default:
1691 return NULL;
1692 }
1693}
1694
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001695LIBYANG_API_DEF const struct lysc_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001696lysc_node_child(const struct lysc_node *node)
Radek Krejcia3045382018-11-22 14:30:31 +01001697{
Michal Vasko544e58a2021-01-28 14:33:41 +01001698 struct lysc_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001699
1700 if (!node) {
1701 return NULL;
1702 }
1703
Michal Vasko544e58a2021-01-28 14:33:41 +01001704 if (node->nodetype & (LYS_RPC | LYS_ACTION)) {
1705 return &((struct lysc_node_action *)node)->input.node;
Radek Krejcibe154442021-01-21 11:06:36 +01001706 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01001707 child = lysc_node_child_p(node);
1708 if (child) {
1709 return *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001710 }
Michal Vasko2a668712020-10-21 11:48:09 +02001711 }
Michal Vasko544e58a2021-01-28 14:33:41 +01001712
1713 return NULL;
Michal Vasko2a668712020-10-21 11:48:09 +02001714}
1715
Radek Krejci9a3823e2021-01-27 20:26:46 +01001716struct lysc_must **
1717lysc_node_musts_p(const struct lysc_node *node)
1718{
1719 if (!node) {
1720 return NULL;
1721 }
1722
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001723 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001724 case LYS_CONTAINER:
1725 return &((struct lysc_node_container *)node)->musts;
1726 case LYS_LEAF:
1727 return &((struct lysc_node_leaf *)node)->musts;
1728 case LYS_LEAFLIST:
1729 return &((struct lysc_node_leaflist *)node)->musts;
1730 case LYS_LIST:
1731 return &((struct lysc_node_list *)node)->musts;
1732 case LYS_ANYXML:
1733 case LYS_ANYDATA:
1734 return &((struct lysc_node_anydata *)node)->musts;
1735 case LYS_NOTIF:
1736 return &((struct lysc_node_notif *)node)->musts;
1737 case LYS_INPUT:
1738 case LYS_OUTPUT:
1739 return &((struct lysc_node_action_inout *)node)->musts;
1740 default:
1741 return NULL;
1742 }
1743}
1744
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001745LIBYANG_API_DEF struct lysc_must *
Radek Krejci9a3823e2021-01-27 20:26:46 +01001746lysc_node_musts(const struct lysc_node *node)
1747{
1748 struct lysc_must **must_p;
1749
1750 must_p = lysc_node_musts_p(node);
1751 if (must_p) {
1752 return *must_p;
1753 } else {
1754 return NULL;
1755 }
1756}
1757
1758struct lysc_when ***
1759lysc_node_when_p(const struct lysc_node *node)
1760{
1761 if (!node) {
1762 return NULL;
1763 }
1764
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001765 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001766 case LYS_CONTAINER:
1767 return &((struct lysc_node_container *)node)->when;
1768 case LYS_CHOICE:
1769 return &((struct lysc_node_choice *)node)->when;
1770 case LYS_LEAF:
1771 return &((struct lysc_node_leaf *)node)->when;
1772 case LYS_LEAFLIST:
1773 return &((struct lysc_node_leaflist *)node)->when;
1774 case LYS_LIST:
1775 return &((struct lysc_node_list *)node)->when;
1776 case LYS_ANYXML:
1777 case LYS_ANYDATA:
1778 return &((struct lysc_node_anydata *)node)->when;
1779 case LYS_CASE:
1780 return &((struct lysc_node_case *)node)->when;
1781 case LYS_NOTIF:
1782 return &((struct lysc_node_notif *)node)->when;
1783 case LYS_RPC:
1784 case LYS_ACTION:
1785 return &((struct lysc_node_action *)node)->when;
1786 default:
1787 return NULL;
1788 }
1789}
1790
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001791LIBYANG_API_DEF struct lysc_when **
Radek Krejci9a3823e2021-01-27 20:26:46 +01001792lysc_node_when(const struct lysc_node *node)
1793{
1794 struct lysc_when ***when_p;
1795
1796 when_p = lysc_node_when_p(node);
1797 if (when_p) {
1798 return *when_p;
1799 } else {
1800 return NULL;
1801 }
1802}
1803
Radek Krejcid6b76452019-09-03 17:03:03 +02001804enum ly_stmt
Radek Krejcid54412f2020-12-17 20:25:35 +01001805lysp_match_kw(struct ly_in *in, uint64_t *indent)
David Sedlákc10e7902018-12-17 02:17:59 +01001806{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001807/**
Radek Krejcid54412f2020-12-17 20:25:35 +01001808 * @brief Move the input by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001809 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001810 *
1811 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001812 */
Radek Krejcid54412f2020-12-17 20:25:35 +01001813#define MOVE_IN(COUNT) \
1814 ly_in_skip(in, COUNT); \
1815 if (indent) { \
1816 (*indent)+=COUNT; \
1817 }
1818#define IF_KW(STR, LEN, STMT) \
1819 if (!strncmp(in->current, STR, LEN)) { \
1820 MOVE_IN(LEN); \
1821 (*kw)=STMT; \
1822 }
1823#define IF_KW_PREFIX(STR, LEN) \
1824 if (!strncmp(in->current, STR, LEN)) { \
1825 MOVE_IN(LEN);
1826#define IF_KW_PREFIX_END \
1827 }
David Sedlák572e7ab2019-06-04 16:01:58 +02001828
Michal Vasko63f3d842020-07-08 10:10:14 +02001829 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001830 enum ly_stmt result = LY_STMT_NONE;
1831 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001832 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001833 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001834 case 'a':
Radek Krejcid54412f2020-12-17 20:25:35 +01001835 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001836 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1837 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1838 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001839 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001840 IF_KW("data", 4, LY_STMT_ANYDATA)
1841 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001842 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001843 break;
1844 case 'b':
Radek Krejcid54412f2020-12-17 20:25:35 +01001845 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001846 IF_KW("ase", 3, LY_STMT_BASE)
1847 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1848 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001849 break;
1850 case 'c':
Radek Krejcid54412f2020-12-17 20:25:35 +01001851 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001852 IF_KW("ase", 3, LY_STMT_CASE)
1853 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001854 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001855 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001856 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001857 IF_KW("ct", 2, LY_STMT_CONTACT)
1858 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001859 IF_KW_PREFIX_END
1860 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001861 break;
1862 case 'd':
Radek Krejcid54412f2020-12-17 20:25:35 +01001863 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001864 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001865 IF_KW("fault", 5, LY_STMT_DEFAULT)
1866 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001867 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001868 IF_KW("e", 1, LY_STMT_DEVIATE)
1869 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001870 IF_KW_PREFIX_END
1871 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001872 break;
1873 case 'e':
Radek Krejcid54412f2020-12-17 20:25:35 +01001874 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001875 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001876 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001877 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1878 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001879 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001880 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001881 break;
1882 case 'f':
Radek Krejcid54412f2020-12-17 20:25:35 +01001883 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001884 IF_KW("eature", 6, LY_STMT_FEATURE)
1885 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001886 break;
1887 case 'g':
Radek Krejcid54412f2020-12-17 20:25:35 +01001888 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001889 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001890 break;
1891 case 'i':
Radek Krejcid54412f2020-12-17 20:25:35 +01001892 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001893 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1894 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1895 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001896 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001897 IF_KW("clude", 5, LY_STMT_INCLUDE)
1898 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001899 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001900 break;
1901 case 'k':
Radek Krejcid54412f2020-12-17 20:25:35 +01001902 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001903 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001904 break;
1905 case 'l':
Radek Krejcid54412f2020-12-17 20:25:35 +01001906 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001907 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001908 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1909 else IF_KW("af", 2, LY_STMT_LEAF)
1910 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001911 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001912 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001913 break;
1914 case 'm':
Radek Krejcid54412f2020-12-17 20:25:35 +01001915 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001916 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001917 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1918 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001919 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001920 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1921 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001922 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001923 IF_KW("ule", 3, LY_STMT_MODULE)
1924 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001925 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001926 break;
1927 case 'n':
Radek Krejcid54412f2020-12-17 20:25:35 +01001928 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001929 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1930 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001931 break;
1932 case 'o':
Radek Krejcid54412f2020-12-17 20:25:35 +01001933 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001934 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001935 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1936 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001937 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001938 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001939 break;
1940 case 'p':
Radek Krejcid54412f2020-12-17 20:25:35 +01001941 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001942 IF_KW("ath", 3, LY_STMT_PATH)
1943 else IF_KW("attern", 6, LY_STMT_PATTERN)
1944 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001945 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001946 IF_KW("fix", 3, LY_STMT_PREFIX)
1947 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001948 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001949 break;
1950 case 'r':
Radek Krejcid54412f2020-12-17 20:25:35 +01001951 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001952 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001953 else IF_KW_PREFIX("e", 1)
1954 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001955 IF_KW("erence", 6, LY_STMT_REFERENCE)
1956 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001957 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001958 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1959 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1960 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001961 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001962 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001963 break;
1964 case 's':
Radek Krejcid54412f2020-12-17 20:25:35 +01001965 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001966 IF_KW("tatus", 5, LY_STMT_STATUS)
1967 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001968 break;
1969 case 't':
Radek Krejcid54412f2020-12-17 20:25:35 +01001970 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001971 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1972 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001973 break;
1974 case 'u':
Radek Krejcid54412f2020-12-17 20:25:35 +01001975 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001976 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001977 IF_KW("que", 3, LY_STMT_UNIQUE)
1978 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001979 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001980 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001981 break;
1982 case 'v':
Radek Krejcid54412f2020-12-17 20:25:35 +01001983 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001984 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001985 break;
1986 case 'w':
Radek Krejcid54412f2020-12-17 20:25:35 +01001987 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001988 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001989 break;
1990 case 'y':
Radek Krejcid54412f2020-12-17 20:25:35 +01001991 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001992 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
1993 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001994 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001995 default:
Radek Krejcid54412f2020-12-17 20:25:35 +01001996 /* if indent is not NULL we are matching keyword from YANG data */
1997 if (indent) {
Michal Vasko63f3d842020-07-08 10:10:14 +02001998 if (in->current[0] == ';') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001999 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002000 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02002001 } else if (in->current[0] == '{') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002002 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002003 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002004 } else if (in->current[0] == '}') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002005 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002006 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02002007 }
2008 }
David Sedlák23a59a62018-10-26 13:08:02 +02002009 break;
2010 }
2011
Michal Vasko63f3d842020-07-08 10:10:14 +02002012 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02002013 /* the keyword is not terminated */
2014 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002015 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02002016 }
2017
David Sedlák1bccdfa2019-06-17 15:55:27 +02002018#undef IF_KW
2019#undef IF_KW_PREFIX
2020#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01002021#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02002022 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01002023
David Sedlák1bccdfa2019-06-17 15:55:27 +02002024 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02002025}
David Sedlákecf5eb82019-06-03 14:12:44 +02002026
Radek Krejci85ac8312021-03-03 20:21:33 +01002027LY_ERR
2028lysp_ext_find_definition(const struct ly_ctx *ctx, const struct lysp_ext_instance *ext, const struct lys_module **ext_mod,
2029 struct lysp_ext **ext_def)
2030{
Radek Krejci85ac8312021-03-03 20:21:33 +01002031 const char *tmp, *name, *prefix;
2032 size_t pref_len, name_len;
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002033 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci85ac8312021-03-03 20:21:33 +01002034 const struct lys_module *mod = NULL;
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002035 const struct lysp_submodule *submod;
Radek Krejci85ac8312021-03-03 20:21:33 +01002036
Michal Vasko193dacd2022-10-13 08:43:05 +02002037 if (ext_def) {
2038 *ext_def = NULL;
Radek Krejci85ac8312021-03-03 20:21:33 +01002039 }
2040
Radek Krejci677abea2021-03-05 14:24:53 +01002041 /* parse the prefix, the nodeid was previously already parsed and checked */
Radek Krejci85ac8312021-03-03 20:21:33 +01002042 tmp = ext->name;
Radek Krejci677abea2021-03-05 14:24:53 +01002043 ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len);
Radek Krejci85ac8312021-03-03 20:21:33 +01002044
2045 /* get module where the extension definition should be placed */
Michal Vasko193dacd2022-10-13 08:43:05 +02002046 *ext_mod = mod = ly_resolve_prefix(ctx, prefix, pref_len, ext->format, ext->prefix_data);
Radek Krejci85ac8312021-03-03 20:21:33 +01002047 if (!mod) {
Radek Krejci422afb12021-03-04 16:38:16 +01002048 LOGVAL(ctx, LYVE_REFERENCE, "Invalid prefix \"%.*s\" used for extension instance identifier.", (int)pref_len, prefix);
Radek Krejci85ac8312021-03-03 20:21:33 +01002049 return LY_EVALID;
2050 } else if (!mod->parsed->extensions) {
2051 LOGVAL(ctx, LYVE_REFERENCE, "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
2052 ext->name, mod->name);
2053 return LY_EVALID;
2054 }
2055
Michal Vasko193dacd2022-10-13 08:43:05 +02002056 if (!ext_def) {
2057 /* we are done */
2058 return LY_SUCCESS;
2059 }
2060
Radek Krejci85ac8312021-03-03 20:21:33 +01002061 /* find the parsed extension definition there */
2062 LY_ARRAY_FOR(mod->parsed->extensions, v) {
2063 if (!strcmp(name, mod->parsed->extensions[v].name)) {
2064 *ext_def = &mod->parsed->extensions[v];
2065 break;
2066 }
2067 }
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002068 if (!*ext_def) {
2069 LY_ARRAY_FOR(mod->parsed->includes, u) {
2070 submod = mod->parsed->includes[u].submodule;
2071 LY_ARRAY_FOR(submod->extensions, v) {
2072 if (!strcmp(name, submod->extensions[v].name)) {
2073 *ext_def = &submod->extensions[v];
2074 break;
2075 }
2076 }
2077 }
2078 }
Radek Krejci85ac8312021-03-03 20:21:33 +01002079
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002080 if (!*ext_def) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002081 LOGVAL(ctx, LYVE_REFERENCE, "Extension definition of extension instance \"%s\" not found.", ext->name);
2082 return LY_EVALID;
2083 }
2084
Radek Krejci85ac8312021-03-03 20:21:33 +01002085 return LY_SUCCESS;
2086}
2087
2088LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002089lysp_ext_instance_resolve_argument(struct ly_ctx *ctx, struct lysp_ext_instance *ext_p)
Radek Krejci85ac8312021-03-03 20:21:33 +01002090{
Michal Vasko193dacd2022-10-13 08:43:05 +02002091 assert(ext_p->def);
2092
2093 if (!ext_p->def->argname || ext_p->argument) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002094 /* nothing to do */
2095 return LY_SUCCESS;
2096 }
2097
Radek Krejci8df109d2021-04-23 12:19:08 +02002098 if (ext_p->format == LY_VALUE_XML) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002099 /* schema was parsed from YIN and an argument is expected, ... */
Radek Krejci85ac8312021-03-03 20:21:33 +01002100 struct lysp_stmt *stmt = NULL;
2101
Michal Vasko193dacd2022-10-13 08:43:05 +02002102 if (ext_p->def->flags & LYS_YINELEM_TRUE) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002103 /* ... argument was the first XML child element */
2104 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {}
2105 if (stmt) {
2106 const char *arg, *ext, *name_arg, *name_ext, *prefix_arg, *prefix_ext;
2107 size_t name_arg_len, name_ext_len, prefix_arg_len, prefix_ext_len;
2108
2109 stmt = ext_p->child;
2110
2111 arg = stmt->stmt;
2112 ly_parse_nodeid(&arg, &prefix_arg, &prefix_arg_len, &name_arg, &name_arg_len);
Michal Vasko193dacd2022-10-13 08:43:05 +02002113 if (ly_strncmp(ext_p->def->argname, name_arg, name_arg_len)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002114 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" expects argument element \"%s\" as its first XML child, "
Michal Vasko193dacd2022-10-13 08:43:05 +02002115 "but \"%.*s\" element found.", ext_p->name, ext_p->def->argname, (int)name_arg_len, name_arg);
Radek Krejci85ac8312021-03-03 20:21:33 +01002116 return LY_EVALID;
2117 }
2118
2119 /* check namespace - all the extension instances must be qualified and argument element is expected in the same
2120 * namespace. Do not check just prefixes, there can be different prefixes pointing to the same namespace */
2121 ext = ext_p->name; /* include prefix */
2122 ly_parse_nodeid(&ext, &prefix_ext, &prefix_ext_len, &name_ext, &name_ext_len);
2123
2124 if (ly_resolve_prefix(ctx, prefix_ext, prefix_ext_len, ext_p->format, ext_p->prefix_data) !=
2125 ly_resolve_prefix(ctx, prefix_arg, prefix_arg_len, stmt->format, stmt->prefix_data)) {
2126 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" element and its argument element \"%s\" are "
Michal Vasko193dacd2022-10-13 08:43:05 +02002127 "expected in the same namespace, but they differ.", ext_p->name, ext_p->def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002128 return LY_EVALID;
2129 }
2130 }
2131 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +02002132 /* ... 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 +01002133 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002134 if (!strcmp(stmt->stmt, ext_p->def->argname)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002135 /* this is the extension's argument */
2136 break;
2137 }
2138 }
2139 }
2140
2141 if (stmt) {
2142 LY_CHECK_RET(lydict_insert(ctx, stmt->arg, 0, &ext_p->argument));
2143 stmt->flags |= LYS_YIN_ARGUMENT;
2144 }
2145 }
2146
2147 if (!ext_p->argument) {
2148 /* missing extension's argument */
Michal Vasko193dacd2022-10-13 08:43:05 +02002149 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" missing argument %s\"%s\".",
2150 ext_p->name, (ext_p->def->flags & LYS_YINELEM_TRUE) ? "element " : "", ext_p->def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002151 return LY_EVALID;
2152 }
2153
2154 return LY_SUCCESS;
2155}
2156
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002157LY_ARRAY_COUNT_TYPE
Radek Krejcifc596f92021-02-26 22:40:26 +01002158lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, enum ly_stmt substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002159{
2160 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
2161
Michal Vaskod989ba02020-08-24 10:59:24 +02002162 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejciab430862021-03-02 20:13:40 +01002163 if (ext[index].parent_stmt == substmt) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02002164 return index;
2165 }
2166 }
2167
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002168 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002169}
2170
Michal Vaskof8ded142022-02-17 10:48:01 +01002171LIBYANG_API_DEF const struct lysc_node *
Michal Vasko72244882021-01-12 15:21:05 +01002172lysc_data_node(const struct lysc_node *schema)
Michal Vasko62ed12d2020-05-21 10:08:25 +02002173{
2174 const struct lysc_node *parent;
2175
Michal Vasko72244882021-01-12 15:21:05 +01002176 parent = schema;
Radek Krejcidf549132021-01-21 10:32:32 +01002177 while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC |
2178 LYS_ACTION | LYS_NOTIF))) {
Radek Krejci7d95fbb2021-01-26 17:33:13 +01002179 parent = parent->parent;
Michal Vasko72244882021-01-12 15:21:05 +01002180 }
Michal Vasko62ed12d2020-05-21 10:08:25 +02002181
2182 return parent;
2183}
Michal Vaskof4258e12021-06-15 12:11:42 +02002184
2185ly_bool
2186lys_has_recompiled(const struct lys_module *mod)
2187{
2188 LY_ARRAY_COUNT_TYPE u;
2189
2190 if (LYSP_HAS_RECOMPILED(mod->parsed)) {
2191 return 1;
2192 }
2193
2194 LY_ARRAY_FOR(mod->parsed->includes, u) {
2195 if (LYSP_HAS_RECOMPILED(mod->parsed->includes[u].submodule)) {
2196 return 1;
2197 }
2198 }
2199
2200 return 0;
2201}
2202
2203ly_bool
2204lys_has_compiled(const struct lys_module *mod)
2205{
2206 LY_ARRAY_COUNT_TYPE u;
2207
2208 if (LYSP_HAS_COMPILED(mod->parsed)) {
2209 return 1;
2210 }
2211
2212 LY_ARRAY_FOR(mod->parsed->includes, u) {
2213 if (LYSP_HAS_COMPILED(mod->parsed->includes[u].submodule)) {
2214 return 1;
2215 }
2216 }
2217
2218 return 0;
2219}
Michal Vasko7ee5be22021-06-16 17:03:34 +02002220
2221ly_bool
Michal Vasko87cfdba2022-02-22 14:13:45 +01002222lys_has_dep_mods(const struct lys_module *mod)
Michal Vasko7ee5be22021-06-16 17:03:34 +02002223{
2224 LY_ARRAY_COUNT_TYPE u;
2225
Michal Vasko87cfdba2022-02-22 14:13:45 +01002226 /* features */
2227 if (mod->parsed->features) {
Michal Vasko7ee5be22021-06-16 17:03:34 +02002228 return 1;
2229 }
2230
Michal Vasko87cfdba2022-02-22 14:13:45 +01002231 /* groupings */
2232 if (mod->parsed->groupings) {
2233 return 1;
2234 }
Michal Vasko7ee5be22021-06-16 17:03:34 +02002235 LY_ARRAY_FOR(mod->parsed->includes, u) {
2236 if (mod->parsed->includes[u].submodule->groupings) {
2237 return 1;
2238 }
2239 }
2240
Michal Vasko87cfdba2022-02-22 14:13:45 +01002241 /* augments (adding nodes with leafrefs) */
2242 if (mod->parsed->augments) {
2243 return 1;
2244 }
2245 LY_ARRAY_FOR(mod->parsed->includes, u) {
2246 if (mod->parsed->includes[u].submodule->augments) {
2247 return 1;
2248 }
2249 }
2250
Michal Vasko7ee5be22021-06-16 17:03:34 +02002251 return 0;
2252}
Michal Vaskob872d0f2022-12-08 09:36:54 +01002253
2254const char *
2255lys_stmt_str(enum ly_stmt stmt)
2256{
2257 switch (stmt) {
2258 case LY_STMT_NONE:
2259 return NULL;
2260 case LY_STMT_ACTION:
2261 return "action";
2262 case LY_STMT_ANYDATA:
2263 return "anydata";
2264 case LY_STMT_ANYXML:
2265 return "anyxml";
2266 case LY_STMT_ARGUMENT:
2267 return "argument";
2268 case LY_STMT_ARG_TEXT:
2269 return "text";
2270 case LY_STMT_ARG_VALUE:
2271 return "value";
2272 case LY_STMT_AUGMENT:
2273 return "augment";
2274 case LY_STMT_BASE:
2275 return "base";
2276 case LY_STMT_BELONGS_TO:
2277 return "belongs-to";
2278 case LY_STMT_BIT:
2279 return "bit";
2280 case LY_STMT_CASE:
2281 return "case";
2282 case LY_STMT_CHOICE:
2283 return "choice";
2284 case LY_STMT_CONFIG:
2285 return "config";
2286 case LY_STMT_CONTACT:
2287 return "contact";
2288 case LY_STMT_CONTAINER:
2289 return "container";
2290 case LY_STMT_DEFAULT:
2291 return "default";
2292 case LY_STMT_DESCRIPTION:
2293 return "description";
2294 case LY_STMT_DEVIATE:
2295 return "deviate";
2296 case LY_STMT_DEVIATION:
2297 return "deviation";
2298 case LY_STMT_ENUM:
2299 return "enum";
2300 case LY_STMT_ERROR_APP_TAG:
2301 return "error-app-tag";
2302 case LY_STMT_ERROR_MESSAGE:
2303 return "error-message";
2304 case LY_STMT_EXTENSION:
2305 return "extension";
2306 case LY_STMT_EXTENSION_INSTANCE:
2307 return NULL;
2308 case LY_STMT_FEATURE:
2309 return "feature";
2310 case LY_STMT_FRACTION_DIGITS:
2311 return "fraction-digits";
2312 case LY_STMT_GROUPING:
2313 return "grouping";
2314 case LY_STMT_IDENTITY:
2315 return "identity";
2316 case LY_STMT_IF_FEATURE:
2317 return "if-feature";
2318 case LY_STMT_IMPORT:
2319 return "import";
2320 case LY_STMT_INCLUDE:
2321 return "include";
2322 case LY_STMT_INPUT:
2323 return "input";
2324 case LY_STMT_KEY:
2325 return "key";
2326 case LY_STMT_LEAF:
2327 return "leaf";
2328 case LY_STMT_LEAF_LIST:
2329 return "leaf-list";
2330 case LY_STMT_LENGTH:
2331 return "length";
2332 case LY_STMT_LIST:
2333 return "list";
2334 case LY_STMT_MANDATORY:
2335 return "mandatory";
2336 case LY_STMT_MAX_ELEMENTS:
2337 return "max-elements";
2338 case LY_STMT_MIN_ELEMENTS:
2339 return "min-elements";
2340 case LY_STMT_MODIFIER:
2341 return "modifier";
2342 case LY_STMT_MODULE:
2343 return "module";
2344 case LY_STMT_MUST:
2345 return "must";
2346 case LY_STMT_NAMESPACE:
2347 return "namespace";
2348 case LY_STMT_NOTIFICATION:
2349 return "notification";
2350 case LY_STMT_ORDERED_BY:
2351 return "ordered-by";
2352 case LY_STMT_ORGANIZATION:
2353 return "organization";
2354 case LY_STMT_OUTPUT:
2355 return "output";
2356 case LY_STMT_PATH:
2357 return "path";
2358 case LY_STMT_PATTERN:
2359 return "pattern";
2360 case LY_STMT_POSITION:
2361 return "position";
2362 case LY_STMT_PREFIX:
2363 return "prefix";
2364 case LY_STMT_PRESENCE:
2365 return "presence";
2366 case LY_STMT_RANGE:
2367 return "range";
2368 case LY_STMT_REFERENCE:
2369 return "reference";
2370 case LY_STMT_REFINE:
2371 return "refine";
2372 case LY_STMT_REQUIRE_INSTANCE:
2373 return "require-instance";
2374 case LY_STMT_REVISION:
2375 return "revision";
2376 case LY_STMT_REVISION_DATE:
2377 return "revision-date";
2378 case LY_STMT_RPC:
2379 return "rpc";
2380 case LY_STMT_STATUS:
2381 return "status";
2382 case LY_STMT_SUBMODULE:
2383 return "submodule";
2384 case LY_STMT_SYNTAX_LEFT_BRACE:
2385 return "{";
2386 case LY_STMT_SYNTAX_RIGHT_BRACE:
2387 return "}";
2388 case LY_STMT_SYNTAX_SEMICOLON:
2389 return ";";
2390 case LY_STMT_TYPE:
2391 return "type";
2392 case LY_STMT_TYPEDEF:
2393 return "typedef";
2394 case LY_STMT_UNIQUE:
2395 return "unique";
2396 case LY_STMT_UNITS:
2397 return "units";
2398 case LY_STMT_USES:
2399 return "uses";
2400 case LY_STMT_VALUE:
2401 return "value";
2402 case LY_STMT_WHEN:
2403 return "when";
2404 case LY_STMT_YANG_VERSION:
2405 return "yang-version";
2406 case LY_STMT_YIN_ELEMENT:
2407 return "yin-element";
2408 }
2409
2410 return NULL;
2411}
2412
2413const char *
2414lys_stmt_arg(enum ly_stmt stmt)
2415{
2416 switch (stmt) {
2417 case LY_STMT_NONE:
2418 case LY_STMT_ARG_TEXT:
2419 case LY_STMT_ARG_VALUE:
2420 case LY_STMT_EXTENSION_INSTANCE:
2421 case LY_STMT_INPUT:
2422 case LY_STMT_OUTPUT:
2423 case LY_STMT_SYNTAX_LEFT_BRACE:
2424 case LY_STMT_SYNTAX_RIGHT_BRACE:
2425 case LY_STMT_SYNTAX_SEMICOLON:
2426 return NULL;
2427 case LY_STMT_ACTION:
2428 case LY_STMT_ANYDATA:
2429 case LY_STMT_ANYXML:
2430 case LY_STMT_ARGUMENT:
2431 case LY_STMT_BASE:
2432 case LY_STMT_BIT:
2433 case LY_STMT_CASE:
2434 case LY_STMT_CHOICE:
2435 case LY_STMT_CONTAINER:
2436 case LY_STMT_ENUM:
2437 case LY_STMT_EXTENSION:
2438 case LY_STMT_FEATURE:
2439 case LY_STMT_GROUPING:
2440 case LY_STMT_IDENTITY:
2441 case LY_STMT_IF_FEATURE:
2442 case LY_STMT_LEAF:
2443 case LY_STMT_LEAF_LIST:
2444 case LY_STMT_LIST:
2445 case LY_STMT_MODULE:
2446 case LY_STMT_NOTIFICATION:
2447 case LY_STMT_RPC:
2448 case LY_STMT_SUBMODULE:
2449 case LY_STMT_TYPE:
2450 case LY_STMT_TYPEDEF:
2451 case LY_STMT_UNITS:
2452 case LY_STMT_USES:
2453 return "name";
2454 case LY_STMT_AUGMENT:
2455 case LY_STMT_DEVIATION:
2456 case LY_STMT_REFINE:
2457 return "target-node";
2458 case LY_STMT_BELONGS_TO:
2459 case LY_STMT_IMPORT:
2460 case LY_STMT_INCLUDE:
2461 return "module";
2462 case LY_STMT_CONFIG:
2463 case LY_STMT_DEFAULT:
2464 case LY_STMT_DEVIATE:
2465 case LY_STMT_ERROR_APP_TAG:
2466 case LY_STMT_ERROR_MESSAGE:
2467 case LY_STMT_FRACTION_DIGITS:
2468 case LY_STMT_KEY:
2469 case LY_STMT_LENGTH:
2470 case LY_STMT_MANDATORY:
2471 case LY_STMT_MAX_ELEMENTS:
2472 case LY_STMT_MIN_ELEMENTS:
2473 case LY_STMT_MODIFIER:
2474 case LY_STMT_ORDERED_BY:
2475 case LY_STMT_PATH:
2476 case LY_STMT_PATTERN:
2477 case LY_STMT_POSITION:
2478 case LY_STMT_PREFIX:
2479 case LY_STMT_PRESENCE:
2480 case LY_STMT_RANGE:
2481 case LY_STMT_REQUIRE_INSTANCE:
2482 case LY_STMT_STATUS:
2483 case LY_STMT_VALUE:
2484 case LY_STMT_YANG_VERSION:
2485 case LY_STMT_YIN_ELEMENT:
2486 return "value";
2487 case LY_STMT_CONTACT:
2488 case LY_STMT_DESCRIPTION:
2489 case LY_STMT_ORGANIZATION:
2490 case LY_STMT_REFERENCE:
2491 return "text";
2492 case LY_STMT_MUST:
2493 case LY_STMT_WHEN:
2494 return "condition";
2495 case LY_STMT_NAMESPACE:
2496 return "uri";
2497 case LY_STMT_REVISION:
2498 case LY_STMT_REVISION_DATE:
2499 return "date";
2500 case LY_STMT_UNIQUE:
2501 return "tag";
2502 }
2503
2504 return NULL;
2505}
2506
2507uint8_t
2508lys_stmt_flags(enum ly_stmt stmt)
2509{
2510 switch (stmt) {
2511 case LY_STMT_NONE:
2512 case LY_STMT_ARG_TEXT:
2513 case LY_STMT_ARG_VALUE:
2514 case LY_STMT_DEFAULT:
2515 case LY_STMT_ERROR_APP_TAG:
2516 case LY_STMT_EXTENSION_INSTANCE:
2517 case LY_STMT_IF_FEATURE:
2518 case LY_STMT_INPUT:
2519 case LY_STMT_KEY:
2520 case LY_STMT_LENGTH:
2521 case LY_STMT_MUST:
2522 case LY_STMT_NAMESPACE:
2523 case LY_STMT_OUTPUT:
2524 case LY_STMT_PATH:
2525 case LY_STMT_PATTERN:
2526 case LY_STMT_PRESENCE:
2527 case LY_STMT_RANGE:
2528 case LY_STMT_SYNTAX_LEFT_BRACE:
2529 case LY_STMT_SYNTAX_RIGHT_BRACE:
2530 case LY_STMT_SYNTAX_SEMICOLON:
2531 case LY_STMT_UNIQUE:
2532 case LY_STMT_UNITS:
2533 case LY_STMT_WHEN:
2534 return 0;
2535 case LY_STMT_ACTION:
2536 case LY_STMT_ANYDATA:
2537 case LY_STMT_ANYXML:
2538 case LY_STMT_ARGUMENT:
2539 case LY_STMT_AUGMENT:
2540 case LY_STMT_BASE:
2541 case LY_STMT_BELONGS_TO:
2542 case LY_STMT_BIT:
2543 case LY_STMT_CASE:
2544 case LY_STMT_CHOICE:
2545 case LY_STMT_CONFIG:
2546 case LY_STMT_CONTAINER:
2547 case LY_STMT_DEVIATE:
2548 case LY_STMT_DEVIATION:
2549 case LY_STMT_ENUM:
2550 case LY_STMT_EXTENSION:
2551 case LY_STMT_FEATURE:
2552 case LY_STMT_FRACTION_DIGITS:
2553 case LY_STMT_GROUPING:
2554 case LY_STMT_IDENTITY:
2555 case LY_STMT_IMPORT:
2556 case LY_STMT_INCLUDE:
2557 case LY_STMT_LEAF:
2558 case LY_STMT_LEAF_LIST:
2559 case LY_STMT_LIST:
2560 case LY_STMT_MANDATORY:
2561 case LY_STMT_MAX_ELEMENTS:
2562 case LY_STMT_MIN_ELEMENTS:
2563 case LY_STMT_MODIFIER:
2564 case LY_STMT_MODULE:
2565 case LY_STMT_NOTIFICATION:
2566 case LY_STMT_ORDERED_BY:
2567 case LY_STMT_POSITION:
2568 case LY_STMT_PREFIX:
2569 case LY_STMT_REFINE:
2570 case LY_STMT_REQUIRE_INSTANCE:
2571 case LY_STMT_REVISION:
2572 case LY_STMT_REVISION_DATE:
2573 case LY_STMT_RPC:
2574 case LY_STMT_STATUS:
2575 case LY_STMT_SUBMODULE:
2576 case LY_STMT_TYPE:
2577 case LY_STMT_TYPEDEF:
2578 case LY_STMT_USES:
2579 case LY_STMT_VALUE:
2580 case LY_STMT_YANG_VERSION:
2581 case LY_STMT_YIN_ELEMENT:
2582 return LY_STMT_FLAG_ID;
2583 case LY_STMT_CONTACT:
2584 case LY_STMT_DESCRIPTION:
2585 case LY_STMT_ERROR_MESSAGE:
2586 case LY_STMT_ORGANIZATION:
2587 case LY_STMT_REFERENCE:
2588 return LY_STMT_FLAG_YIN;
2589 }
2590
2591 return 0;
2592}
Jan Kundrát026737b2023-09-05 13:03:30 +02002593
2594void
2595ly_check_module_filename(const struct ly_ctx *ctx, const char *name, const char *revision, const char *filename)
2596{
2597 const char *basename, *rev, *dot;
2598 size_t len;
2599
2600 /* check that name and revision match filename */
2601 basename = strrchr(filename, '/');
2602 if (!basename) {
2603 basename = filename;
2604 } else {
2605 basename++; /* leading slash */
2606 }
2607 rev = strchr(basename, '@');
2608 dot = strrchr(basename, '.');
2609
2610 /* name */
2611 len = strlen(name);
2612 if (strncmp(basename, name, len) ||
2613 ((rev && (rev != &basename[len])) || (!rev && (dot != &basename[len])))) {
2614 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", basename, name);
2615 }
2616 if (rev) {
2617 len = dot - ++rev;
2618 if (!revision || (len != LY_REV_SIZE - 1) || strncmp(revision, rev, len)) {
2619 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", basename,
2620 revision ? revision : "none");
2621 }
2622 }
2623}