blob: 2dc47c8bfbf10b3a468dea6b0f59e497007b1435 [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
Radek Krejcie7b95092019-05-15 11:03:07 +020044lysp_check_prefix(struct lys_parser_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
Juraj Vijtiuk74dad9e2021-05-26 12:42:14 +020062lysp_check_date(struct lys_parser_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 Vaskob36053d2020-03-26 15:49:30 +010067 LY_CHECK_ARG_RET(ctx ? PARSER_CTX(ctx) : NULL, date, LY_EINVAL);
68 LY_CHECK_ERR_RET(date_len != LY_REV_SIZE - 1, LOGARG(ctx ? PARSER_CTX(ctx) : NULL, date_len), LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +020069
Radek Krejcif13b87b2020-12-01 22:02:17 +010070 /* check format: YYYY-MM-DD */
Radek Krejci1deb5be2020-08-26 16:43:36 +020071 for (uint8_t i = 0; i < date_len; i++) {
Michal Vasko69730152020-10-09 16:30:07 +020072 if ((i == 4) || (i == 7)) {
Radek Krejci86d106e2018-10-18 09:53:19 +020073 if (date[i] != '-') {
74 goto error;
75 }
76 } else if (!isdigit(date[i])) {
77 goto error;
78 }
79 }
80
81 /* check content, e.g. 2018-02-31 */
82 memset(&tm, 0, sizeof tm);
83 r = strptime(date, "%Y-%m-%d", &tm);
Michal Vasko69730152020-10-09 16:30:07 +020084 if (!r || (r != &date[LY_REV_SIZE - 1])) {
Radek Krejci86d106e2018-10-18 09:53:19 +020085 goto error;
86 }
87 memcpy(&tm_, &tm, sizeof tm);
Michal Vasko82f32822022-08-17 10:54:35 +020088
89 /* DST may move the hour back resulting in a different day */
90 tm_.tm_hour = 1;
91
Radek Krejci86d106e2018-10-18 09:53:19 +020092 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
93 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
94 /* checking days is enough, since other errors
95 * have been checked by strptime() */
96 goto error;
97 }
98
99 return LY_SUCCESS;
100
101error:
Radek Krejcid33273d2018-10-25 14:55:52 +0200102 if (stmt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100103 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt);
Radek Krejcid33273d2018-10-25 14:55:52 +0200104 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200105 return LY_EINVAL;
106}
107
108void
109lysp_sort_revisions(struct lysp_revision *revs)
110{
Radek Krejci857189e2020-09-01 13:26:36 +0200111 LY_ARRAY_COUNT_TYPE i, r;
Radek Krejci86d106e2018-10-18 09:53:19 +0200112 struct lysp_revision rev;
113
Radek Krejcic7d13e32020-12-09 12:32:24 +0100114 for (i = 1, r = 0; i < LY_ARRAY_COUNT(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200115 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200116 r = i;
117 }
118 }
119
120 if (r) {
121 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200122 memcpy(&rev, &revs[0], sizeof rev);
123 memcpy(&revs[0], &revs[r], sizeof rev);
124 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200125 }
126}
Radek Krejci151a5b72018-10-19 14:21:44 +0200127
Radek Krejcibbe09a92018-11-08 09:36:54 +0100128static const struct lysp_tpdf *
129lysp_type_match(const char *name, struct lysp_node *node)
130{
Radek Krejci0fb28562018-12-13 15:17:37 +0100131 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200132 LY_ARRAY_COUNT_TYPE u;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100133
Radek Krejci0fb28562018-12-13 15:17:37 +0100134 typedefs = lysp_node_typedefs(node);
135 LY_ARRAY_FOR(typedefs, u) {
136 if (!strcmp(name, typedefs[u].name)) {
137 /* match */
138 return &typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100139 }
140 }
141
142 return NULL;
143}
144
aPiecek63e080d2021-06-29 13:53:28 +0200145static const struct lysp_node_grp *
146lysp_grouping_match(const char *name, struct lysp_node *node)
147{
148 const struct lysp_node_grp *groupings, *grp_iter;
149
150 groupings = lysp_node_groupings(node);
151 LY_LIST_FOR(groupings, grp_iter) {
152 if (!strcmp(name, grp_iter->name)) {
153 /* match */
154 return grp_iter;
155 }
156 }
157
158 return NULL;
159}
160
Radek Krejci4f28eda2018-11-12 11:46:16 +0100161static LY_DATA_TYPE
162lysp_type_str2builtin(const char *name, size_t len)
163{
164 if (len >= 4) { /* otherwise it does not match any built-in type */
165 if (name[0] == 'b') {
166 if (name[1] == 'i') {
Michal Vasko69730152020-10-09 16:30:07 +0200167 if ((len == 6) && !strncmp(&name[2], "nary", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100168 return LY_TYPE_BINARY;
Michal Vasko69730152020-10-09 16:30:07 +0200169 } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100170 return LY_TYPE_BITS;
171 }
Michal Vasko69730152020-10-09 16:30:07 +0200172 } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100173 return LY_TYPE_BOOL;
174 }
175 } else if (name[0] == 'd') {
Michal Vasko69730152020-10-09 16:30:07 +0200176 if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100177 return LY_TYPE_DEC64;
178 }
179 } else if (name[0] == 'e') {
Michal Vasko69730152020-10-09 16:30:07 +0200180 if ((len == 5) && !strncmp(&name[1], "mpty", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100181 return LY_TYPE_EMPTY;
Michal Vasko69730152020-10-09 16:30:07 +0200182 } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100183 return LY_TYPE_ENUM;
184 }
185 } else if (name[0] == 'i') {
186 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200187 if ((len == 4) && !strncmp(&name[2], "t8", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100188 return LY_TYPE_INT8;
189 } else if (len == 5) {
190 if (!strncmp(&name[2], "t16", 3)) {
191 return LY_TYPE_INT16;
192 } else if (!strncmp(&name[2], "t32", 3)) {
193 return LY_TYPE_INT32;
194 } else if (!strncmp(&name[2], "t64", 3)) {
195 return LY_TYPE_INT64;
196 }
Michal Vasko69730152020-10-09 16:30:07 +0200197 } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100198 return LY_TYPE_INST;
199 }
Michal Vasko69730152020-10-09 16:30:07 +0200200 } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100201 return LY_TYPE_IDENT;
202 }
203 } else if (name[0] == 'l') {
Michal Vasko69730152020-10-09 16:30:07 +0200204 if ((len == 7) && !strncmp(&name[1], "eafref", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100205 return LY_TYPE_LEAFREF;
206 }
207 } else if (name[0] == 's') {
Michal Vasko69730152020-10-09 16:30:07 +0200208 if ((len == 6) && !strncmp(&name[1], "tring", 5)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100209 return LY_TYPE_STRING;
210 }
211 } else if (name[0] == 'u') {
212 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200213 if ((len == 5) && !strncmp(&name[2], "ion", 3)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100214 return LY_TYPE_UNION;
215 }
Michal Vasko69730152020-10-09 16:30:07 +0200216 } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) {
217 if ((len == 5) && (name[4] == '8')) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100218 return LY_TYPE_UINT8;
219 } else if (len == 6) {
220 if (!strncmp(&name[4], "16", 2)) {
221 return LY_TYPE_UINT16;
222 } else if (!strncmp(&name[4], "32", 2)) {
223 return LY_TYPE_UINT32;
224 } else if (!strncmp(&name[4], "64", 2)) {
225 return LY_TYPE_UINT64;
226 }
227 }
228 }
229 }
230 }
231
232 return LY_TYPE_UNKNOWN;
233}
234
Radek Krejcibbe09a92018-11-08 09:36:54 +0100235LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +0100236lysp_type_find(const char *id, struct lysp_node *start_node, const struct lysp_module *start_module,
237 LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100238{
239 const char *str, *name;
240 struct lysp_tpdf *typedefs;
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100241 const struct lys_module *mod;
Michal Vaskoa99b3572021-02-01 11:54:58 +0100242 const struct lysp_module *local_module;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200243 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100244
245 assert(id);
246 assert(start_module);
247 assert(tpdf);
248 assert(node);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100249
Radek Krejci4f28eda2018-11-12 11:46:16 +0100250 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100251 str = strchr(id, ':');
252 if (str) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200253 mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_VALUE_SCHEMA, (void *)start_module);
Michal Vaskoa99b3572021-02-01 11:54:58 +0100254 local_module = mod ? mod->parsed : NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100255 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100256 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100257 } else {
Michal Vaskoa99b3572021-02-01 11:54:58 +0100258 local_module = start_module;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100259 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100260
261 /* check for built-in types */
262 *type = lysp_type_str2builtin(name, strlen(name));
263 if (*type) {
264 *tpdf = NULL;
265 return LY_SUCCESS;
266 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100267 }
Michal Vaskoa99b3572021-02-01 11:54:58 +0100268 LY_CHECK_RET(!local_module, LY_ENOTFOUND);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100269
Michal Vaskoa99b3572021-02-01 11:54:58 +0100270 if (start_node && (local_module == start_module)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100271 /* search typedefs in parent's nodes */
272 *node = start_node;
273 while (*node) {
274 *tpdf = lysp_type_match(name, *node);
275 if (*tpdf) {
276 /* match */
277 return LY_SUCCESS;
278 }
279 *node = (*node)->parent;
280 }
281 }
282
Michal Vasko915e5442021-06-08 14:59:21 +0200283 /* go to main module if in submodule */
284 local_module = local_module->mod->parsed;
285
Radek Krejcibbe09a92018-11-08 09:36:54 +0100286 /* search in top-level typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100287 if (local_module->typedefs) {
288 LY_ARRAY_FOR(local_module->typedefs, u) {
289 if (!strcmp(name, local_module->typedefs[u].name)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100290 /* match */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100291 *tpdf = &local_module->typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100292 return LY_SUCCESS;
293 }
294 }
295 }
296
Michal Vasko915e5442021-06-08 14:59:21 +0200297 /* search in all submodules' typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100298 LY_ARRAY_FOR(local_module->includes, u) {
299 typedefs = local_module->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100300 LY_ARRAY_FOR(typedefs, v) {
301 if (!strcmp(name, typedefs[v].name)) {
302 /* match */
303 *tpdf = &typedefs[v];
304 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100305 }
306 }
307 }
308
309 return LY_ENOTFOUND;
310}
311
David Sedlák6544c182019-07-12 13:17:33 +0200312LY_ERR
David Sedlák07869a52019-07-12 14:28:19 +0200313lysp_check_enum_name(struct lys_parser_ctx *ctx, const char *name, size_t name_len)
David Sedlák6544c182019-07-12 13:17:33 +0200314{
315 if (!name_len) {
316 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
317 return LY_EVALID;
318 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
319 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
Radek Krejci422afb12021-03-04 16:38:16 +0100320 (int)name_len, name);
David Sedlák6544c182019-07-12 13:17:33 +0200321 return LY_EVALID;
322 } else {
323 for (size_t u = 0; u < name_len; ++u) {
324 if (iscntrl(name[u])) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100325 LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
Radek Krejci422afb12021-03-04 16:38:16 +0100326 (int)name_len, name, u + 1);
David Sedlák6544c182019-07-12 13:17:33 +0200327 break;
328 }
329 }
330 }
331
332 return LY_SUCCESS;
333}
334
Michal Vaskob36053d2020-03-26 15:49:30 +0100335/**
aPiecekdc12b9f2021-06-25 10:55:47 +0200336 * @brief Insert @p name to hash table and if @p name has already
337 * been added, then log an error.
338 *
339 * This function is used to detect duplicate names.
340 *
341 * @param[in,out] ctx Context to log the error.
342 * @param[in,out] ht Hash table with top-level names.
343 * @param[in] name Inserted top-level identifier.
344 * @param[in] statement The name of the statement type from which
345 * @p name originated (eg typedef, feature, ...).
346 * @param[in] err_detail Optional error specification.
347 * @return LY_ERR, but LY_EEXIST is mapped to LY_EVALID.
348 */
349static LY_ERR
350lysp_check_dup_ht_insert(struct lys_parser_ctx *ctx, struct hash_table *ht,
351 const char *name, const char *statement, const char *err_detail)
352{
353 LY_ERR ret;
354 uint32_t hash;
355
356 hash = dict_hash(name, strlen(name));
357 ret = lyht_insert(ht, &name, hash, NULL);
358 if (ret == LY_EEXIST) {
359 if (err_detail) {
360 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT2, name, statement, err_detail);
361 } else {
362 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, name, statement);
363 }
364 ret = LY_EVALID;
365 }
366
367 return ret;
368}
369
370/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100371 * @brief Check name of a new type to avoid name collisions.
372 *
373 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
374 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
375 * @param[in] tpdf Typedef definition to check.
376 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
aPiecekc7594b72021-06-29 09:00:03 +0200377 * typedefs are checked, caller is supposed to free the table.
378 * @return LY_EVALID in case of collision, LY_SUCCESS otherwise.
Radek Krejcibbe09a92018-11-08 09:36:54 +0100379 */
380static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100381lysp_check_dup_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
aPieceke1fbd952021-06-29 08:12:55 +0200382 struct hash_table *tpdfs_global)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100383{
384 struct lysp_node *parent;
385 uint32_t hash;
386 size_t name_len;
387 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200388 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100389 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100390
391 assert(ctx);
392 assert(tpdf);
393
394 name = tpdf->name;
395 name_len = strlen(name);
396
Radek Krejci4f28eda2018-11-12 11:46:16 +0100397 if (lysp_type_str2builtin(name, name_len)) {
aPiecekc7594b72021-06-29 09:00:03 +0200398 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
399 "Duplicate identifier \"%s\" of typedef statement - name collision with a built-in type.", name);
400 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100401 }
402
403 /* check locally scoped typedefs (avoid name shadowing) */
404 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100405 typedefs = lysp_node_typedefs(node);
406 LY_ARRAY_FOR(typedefs, u) {
407 if (&typedefs[u] == tpdf) {
408 break;
409 }
410 if (!strcmp(name, typedefs[u].name)) {
aPiecekc7594b72021-06-29 09:00:03 +0200411 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
412 "Duplicate identifier \"%s\" of typedef statement - name collision with sibling type.", name);
413 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100414 }
415 }
416 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200417 for (parent = node->parent; parent; parent = parent->parent) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100418 if (lysp_type_match(name, parent)) {
aPiecekc7594b72021-06-29 09:00:03 +0200419 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
420 "Duplicate identifier \"%s\" of typedef statement - name collision with another scoped type.", name);
421 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100422 }
423 }
424 }
425
426 /* check collision with the top-level typedefs */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100427 if (node) {
aPiecekc7594b72021-06-29 09:00:03 +0200428 hash = dict_hash(name, name_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100429 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
aPiecekc7594b72021-06-29 09:00:03 +0200430 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
431 "Duplicate identifier \"%s\" of typedef statement - scoped type collide with a top-level type.", name);
432 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100433 }
434 } else {
aPiecekc7594b72021-06-29 09:00:03 +0200435 LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, tpdfs_global, name, "typedef",
436 "name collision with another top-level type"));
Radek Krejci3b1f9292018-11-08 10:58:35 +0100437 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
438 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
439 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100440 }
441
442 return LY_SUCCESS;
443}
444
Radek Krejci857189e2020-09-01 13:26:36 +0200445/**
446 * @brief Compare identifiers.
Michal Vasko62524a92021-02-26 10:08:50 +0100447 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200448 */
449static ly_bool
450lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Radek Krejcibbe09a92018-11-08 09:36:54 +0100451{
Michal Vasko11ac39a2021-07-23 12:46:56 +0200452 char *id1, *id2;
453
454 id1 = *(char **)val1;
455 id2 = *(char **)val2;
456
457 return strcmp(id1, id2) == 0 ? 1 : 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100458}
459
460LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100461lysp_check_dup_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100462{
463 struct hash_table *ids_global;
Radek Krejci0fb28562018-12-13 15:17:37 +0100464 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200465 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200466 uint32_t i;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100467 LY_ERR ret = LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100468
469 /* check name collisions - typedefs and groupings */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100470 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200471 LY_ARRAY_FOR(mod->typedefs, v) {
aPieceke1fbd952021-06-29 08:12:55 +0200472 ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100473 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100474 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200475 LY_ARRAY_FOR(mod->includes, v) {
476 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200477 ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100478 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci3b1f9292018-11-08 10:58:35 +0100479 }
480 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200481 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
482 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
483 LY_ARRAY_FOR(typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200484 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 +0100485 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100486 }
487 }
Michal Vasko405cc9e2020-12-01 12:01:27 +0100488
Radek Krejcibbe09a92018-11-08 09:36:54 +0100489cleanup:
490 lyht_free(ids_global);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100491 return ret;
492}
493
aPiecek63e080d2021-06-29 13:53:28 +0200494/**
495 * @brief Check name of a new grouping to avoid name collisions.
496 *
497 * @param[in] ctx Parser context, module where the grouping is being defined is taken from here.
498 * @param[in] node Schema node where the grouping is being defined, NULL in case of a top-level grouping.
499 * @param[in] grp Grouping definition to check.
500 * @param[in,out] grps_global Initialized hash table to store temporary data between calls. When the module's
501 * groupings are checked, caller is supposed to free the table.
502 * @return LY_EVALID in case of collision, LY_SUCCESS otherwise.
503 */
504static LY_ERR
505lysp_check_dup_grouping(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_node_grp *grp,
506 struct hash_table *grps_global)
507{
508 struct lysp_node *parent;
509 uint32_t hash;
510 size_t name_len;
511 const char *name;
512 const struct lysp_node_grp *groupings, *grp_iter;
513
514 assert(ctx);
515 assert(grp);
516
517 name = grp->name;
518 name_len = strlen(name);
519
520 /* check locally scoped groupings (avoid name shadowing) */
521 if (node) {
522 groupings = lysp_node_groupings(node);
523 LY_LIST_FOR(groupings, grp_iter) {
524 if (grp_iter == grp) {
525 break;
526 }
527 if (!strcmp(name, grp_iter->name)) {
528 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
529 "Duplicate identifier \"%s\" of grouping statement - name collision with sibling grouping.", name);
530 return LY_EVALID;
531 }
532 }
533 /* search grouping in parent's nodes */
534 for (parent = node->parent; parent; parent = parent->parent) {
535 if (lysp_grouping_match(name, parent)) {
536 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
537 "Duplicate identifier \"%s\" of grouping statement - name collision with another scoped grouping.", name);
538 return LY_EVALID;
539 }
540 }
541 }
542
543 /* check collision with the top-level groupings */
544 if (node) {
545 hash = dict_hash(name, name_len);
546 if (!lyht_find(grps_global, &name, hash, NULL)) {
547 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
548 "Duplicate identifier \"%s\" of grouping statement - scoped grouping collide with a top-level grouping.", name);
549 return LY_EVALID;
550 }
551 } else {
552 LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, grps_global, name, "grouping",
553 "name collision with another top-level grouping"));
554 }
555
556 return LY_SUCCESS;
557}
558
559LY_ERR
560lysp_check_dup_groupings(struct lys_parser_ctx *ctx, struct lysp_module *mod)
561{
562 struct hash_table *ids_global;
563 const struct lysp_node_grp *groupings, *grp_iter;
564 LY_ARRAY_COUNT_TYPE u;
565 uint32_t i;
566 LY_ERR ret = LY_SUCCESS;
567
568 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
569 LY_LIST_FOR(mod->groupings, grp_iter) {
570 ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global);
571 LY_CHECK_GOTO(ret, cleanup);
572 }
573 LY_ARRAY_FOR(mod->includes, u) {
574 LY_LIST_FOR(mod->includes[u].submodule->groupings, grp_iter) {
575 ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global);
576 LY_CHECK_GOTO(ret, cleanup);
577 }
578 }
579 for (i = 0; i < ctx->grps_nodes.count; ++i) {
580 groupings = lysp_node_groupings((struct lysp_node *)ctx->grps_nodes.objs[i]);
581 LY_LIST_FOR(groupings, grp_iter) {
582 ret = lysp_check_dup_grouping(ctx, (struct lysp_node *)ctx->grps_nodes.objs[i], grp_iter, ids_global);
583 LY_CHECK_GOTO(ret, cleanup);
584 }
585 }
586
587cleanup:
588 lyht_free(ids_global);
589 return ret;
590}
591
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100592static ly_bool
593ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
594{
595 void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
596
597 return ptr1 == ptr2 ? 1 : 0;
598}
599
600LY_ERR
601lysp_check_dup_features(struct lys_parser_ctx *ctx, struct lysp_module *mod)
602{
603 LY_ARRAY_COUNT_TYPE u;
604 struct hash_table *ht;
605 struct lysp_feature *f;
aPiecekdc12b9f2021-06-25 10:55:47 +0200606 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100607
aPiecekf6203072021-06-25 10:58:26 +0200608 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100609 LY_CHECK_RET(!ht, LY_EMEM);
610
611 /* add all module features into a hash table */
612 LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200613 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
614 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200615 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100616 }
617
618 /* add all submodule features into a hash table */
619 LY_ARRAY_FOR(mod->includes, u) {
620 LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200621 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
622 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200623 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100624 }
625 }
626
627cleanup:
628 lyht_free(ht);
629 return ret;
630}
631
632LY_ERR
633lysp_check_dup_identities(struct lys_parser_ctx *ctx, struct lysp_module *mod)
634{
635 LY_ARRAY_COUNT_TYPE u;
636 struct hash_table *ht;
637 struct lysp_ident *i;
aPiecekdc12b9f2021-06-25 10:55:47 +0200638 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100639
aPiecekf6203072021-06-25 10:58:26 +0200640 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100641 LY_CHECK_RET(!ht, LY_EMEM);
642
643 /* add all module identities into a hash table */
644 LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200645 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
646 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200647 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100648 }
649
650 /* add all submodule identities into a hash table */
651 LY_ARRAY_FOR(mod->includes, u) {
652 LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200653 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
654 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200655 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100656 }
657 }
658
659cleanup:
660 lyht_free(ht);
661 return ret;
662}
663
Radek Krejci9ed7a192018-10-31 16:23:51 +0100664struct lysp_load_module_check_data {
665 const char *name;
666 const char *revision;
667 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200668 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100669};
670
671static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100672lysp_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 +0100673{
674 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100675 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200676 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100677 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100678 struct lysp_revision *revs;
679
680 name = mod ? mod->mod->name : submod->name;
681 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200682 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100683
684 if (info->name) {
685 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100686 if (strcmp(info->name, name)) {
687 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100688 return LY_EINVAL;
689 }
690 }
691 if (info->revision) {
692 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100693 if (!revs || strcmp(info->revision, revs[0].date)) {
694 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200695 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100696 return LY_EINVAL;
697 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200698 } else if (!latest_revision) {
699 /* do not log, we just need to drop the schema and use the latest revision from the context */
700 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100701 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100702 if (submod) {
703 assert(info->submoduleof);
704
Radek Krejci9ed7a192018-10-31 16:23:51 +0100705 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200706 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100707 LOGVAL(ctx, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200708 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100709 return LY_EVALID;
710 }
711 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100712 if (submod->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100713 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100714 return LY_EVALID;
715 }
716 }
717 if (info->path) {
718 /* check that name and revision match filename */
719 filename = strrchr(info->path, '/');
720 if (!filename) {
721 filename = info->path;
722 } else {
723 filename++;
724 }
725 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100726 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100727 rev = strchr(filename, '@');
728 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100729 if (strncmp(filename, name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +0200730 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100731 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100732 }
733 /* revision */
734 if (rev) {
735 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100736 if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100737 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +0200738 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100739 }
740 }
741 }
742 return LY_SUCCESS;
743}
744
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200745/**
Michal Vasko4e205e82021-06-08 14:01:47 +0200746 * @brief Parse a (sub)module from a local file and add into the context.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200747 *
748 * This function does not check the presence of the (sub)module in context, it should be done before calling this function.
749 *
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200750 * @param[in] ctx libyang context where to work.
751 * @param[in] name Name of the (sub)module to load.
752 * @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 +0200753 * @param[in] main_ctx Parser context of the main module in case of loading submodule.
754 * @param[in] main_name Main module name in case of loading submodule.
755 * @param[in] required Module is required so error (even if the input file not found) are important. If 0, there is some
756 * 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 +0200757 * @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 +0200758 * @param[out] result Parsed YANG schema tree of the requested module (struct lys_module*) or submodule (struct lysp_submodule*).
759 * If it is a module, it is already in the context!
Michal Vasko4e205e82021-06-08 14:01:47 +0200760 * @return LY_SUCCESS on success.
Michal Vasko4e205e82021-06-08 14:01:47 +0200761 * @return LY_ERR on error.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200762 */
763static LY_ERR
Michal Vasko4e205e82021-06-08 14:01:47 +0200764lys_parse_localfile(struct ly_ctx *ctx, const char *name, const char *revision, struct lys_parser_ctx *main_ctx,
Michal Vaskodd992582021-06-10 14:34:57 +0200765 const char *main_name, ly_bool required, struct ly_set *new_mods, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100766{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200767 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100768 char *filepath = NULL;
769 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100770 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100771 LY_ERR ret = LY_SUCCESS;
772 struct lysp_load_module_check_data check_data = {0};
773
Michal Vasko87f1cf02021-06-08 14:02:47 +0200774 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name,
775 revision, &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200776 if (!filepath) {
777 if (required) {
778 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200779 revision ? revision : "");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200780 }
781 return LY_ENOTFOUND;
782 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100783
784 LOGVRB("Loading schema from \"%s\" file.", filepath);
785
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200786 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200787 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +0200788 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100789 check_data.name = name;
790 check_data.revision = revision;
791 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800792 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200793 if (main_ctx) {
aPiecekc3e26142021-06-22 14:25:49 +0200794 ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data, new_mods,
Michal Vasko69730152020-10-09 16:30:07 +0200795 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200796 } else {
Michal Vaskodd992582021-06-10 14:34:57 +0200797 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 +0200798
799 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200800 ly_in_free(in, 1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200801 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100802
803 *result = mod;
804
805 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200806
Radek Krejci9ed7a192018-10-31 16:23:51 +0100807cleanup:
808 free(filepath);
809 return ret;
810}
811
aPiecek4725aea2021-07-28 10:18:33 +0200812/**
813 * @brief Load module from searchdirs or from callback.
814 *
815 * @param[in] ctx libyang context where to work.
816 * @param[in] name Name of module to load.
817 * @param[in] revision Revision of module to load.
Michal Vaskobdac23f2022-01-12 14:02:35 +0100818 * @param[in] mod_latest Module with the latest revision found in context, otherwise set to NULL.
819 * @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 +0200820 * @param[out] mod Loaded module.
821 * @return LY_SUCCESS on success.
822 * @return LY_ERR on error.
823 */
824static LY_ERR
825lys_parse_load_from_clb_or_file(struct ly_ctx *ctx, const char *name, const char *revision,
826 struct lys_module *mod_latest, struct ly_set *new_mods, struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200827{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100828 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200829 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200830
Radek Krejci9ed7a192018-10-31 16:23:51 +0100831 void (*module_data_free)(void *module_data, void *user_data) = NULL;
832 struct lysp_load_module_check_data check_data = {0};
Michal Vasko63f3d842020-07-08 10:10:14 +0200833 struct ly_in *in;
Radek Krejci086c7132018-10-26 15:29:04 +0200834
Michal Vaskobdac23f2022-01-12 14:02:35 +0100835 *mod = NULL;
836
837 if (mod_latest && (!ctx->imp_clb || (mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB)) &&
838 ((ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) || (mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) {
839 /* we are not able to find a newer revision */
840 return LY_SUCCESS;
841 }
842
aPiecek4725aea2021-07-28 10:18:33 +0200843 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
844search_clb:
Michal Vaskobdac23f2022-01-12 14:02:35 +0100845 /* check there is a callback and should be called */
846 if (ctx->imp_clb && (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB))) {
847 if (!ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, &format, &module_data, &module_data_free)) {
848 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
849 check_data.name = name;
850 check_data.revision = revision;
851 lys_parse_in(ctx, in, format, lysp_load_module_check, &check_data, new_mods, mod);
852 ly_in_free(in, 0);
853 if (module_data_free) {
854 module_data_free((void *)module_data, ctx->imp_clb_data);
855 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200856 }
Radek Krejci0af46292019-01-11 16:02:31 +0100857 }
Michal Vaskobdac23f2022-01-12 14:02:35 +0100858 if (*mod && !revision) {
859 /* we got the latest revision module from the callback */
860 (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB;
861 } else if (!*mod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
aPiecek4725aea2021-07-28 10:18:33 +0200862 goto search_file;
863 }
864 } else {
865search_file:
Michal Vaskobdac23f2022-01-12 14:02:35 +0100866 /* check we can use searchdirs and that we should */
867 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) &&
868 (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) {
aPiecek4725aea2021-07-28 10:18:33 +0200869 lys_parse_localfile(ctx, name, revision, NULL, NULL, mod_latest ? 0 : 1, new_mods, (void **)mod);
870 }
Michal Vaskobdac23f2022-01-12 14:02:35 +0100871 if (*mod && !revision) {
872 /* we got the latest revision module in the searchdirs */
873 (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB;
874 } else if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
aPiecek4725aea2021-07-28 10:18:33 +0200875 goto search_clb;
876 }
Radek Krejci086c7132018-10-26 15:29:04 +0200877 }
878
aPiecek4725aea2021-07-28 10:18:33 +0200879 return LY_SUCCESS;
880}
881
882/**
aPiecekd4911ee2021-07-30 07:40:24 +0200883 * @brief Get module without revision according to priorities.
884 *
885 * 1. Search for the module with LYS_MOD_IMPORTED_REV.
886 * 2. Search for the implemented module.
887 * 3. Search for the latest module in the context.
888 *
889 * @param[in] ctx libyang context where module is searched.
890 * @param[in] name Name of the searched module.
aPiecekd4911ee2021-07-30 07:40:24 +0200891 * @return Found module from context or NULL.
892 */
893static struct lys_module *
Michal Vaskobdac23f2022-01-12 14:02:35 +0100894lys_get_module_without_revision(struct ly_ctx *ctx, const char *name)
aPiecekd4911ee2021-07-30 07:40:24 +0200895{
896 struct lys_module *mod, *mod_impl;
897 uint32_t index;
898
aPiecekd4911ee2021-07-30 07:40:24 +0200899 /* Try to find module with LYS_MOD_IMPORTED_REV flag. */
900 index = 0;
901 while ((mod = ly_ctx_get_module_iter(ctx, &index))) {
902 if (!strcmp(mod->name, name) && (mod->latest_revision & LYS_MOD_IMPORTED_REV)) {
903 break;
904 }
905 }
906
907 /* Try to find the implemented module. */
908 mod_impl = ly_ctx_get_module_implemented(ctx, name);
909 if (mod && mod_impl) {
Michal Vaskobdac23f2022-01-12 14:02:35 +0100910 LOGVRB("Implemented module \"%s@%s\" is not used for import, revision \"%s\" is imported instead.",
aPiecekd4911ee2021-07-30 07:40:24 +0200911 mod_impl->name, mod_impl->revision, mod->revision);
912 return mod;
913 } else if (mod_impl) {
914 return mod_impl;
915 }
916
917 /* Try to find the latest module in the current context. */
918 mod = ly_ctx_get_module_latest(ctx, name);
aPiecekd4911ee2021-07-30 07:40:24 +0200919
920 return mod;
921}
922
923/**
aPiecek4725aea2021-07-28 10:18:33 +0200924 * @brief Check if a circular dependency exists between modules.
925 *
926 * @param[in] ctx libyang context for log an error.
927 * @param[in,out] mod Examined module which is set to NULL
928 * if the circular dependency is detected.
929 * @return LY_SUCCESS if no circular dependecy is detected,
930 * otherwise LY_EVALID.
931 */
932static LY_ERR
933lys_check_circular_dependency(struct ly_ctx *ctx, struct lys_module **mod)
934{
935 if ((*mod) && (*mod)->parsed->parsing) {
936 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", (*mod)->name);
Michal Vasko4e205e82021-06-08 14:01:47 +0200937 *mod = NULL;
938 return LY_EVALID;
Michal Vasko0550b762020-11-24 18:04:08 +0100939 }
Radek Krejci086c7132018-10-26 15:29:04 +0200940
aPiecek4725aea2021-07-28 10:18:33 +0200941 return LY_SUCCESS;
942}
943
944LY_ERR
945lys_parse_load(struct ly_ctx *ctx, const char *name, const char *revision, struct ly_set *new_mods,
946 struct lys_module **mod)
947{
948 struct lys_module *mod_latest = NULL;
949
950 assert(mod && new_mods);
951
Michal Vasko0550b762020-11-24 18:04:08 +0100952 /*
aPiecek4725aea2021-07-28 10:18:33 +0200953 * Try to get the module from the context.
Michal Vasko0550b762020-11-24 18:04:08 +0100954 */
aPiecek4725aea2021-07-28 10:18:33 +0200955 if (revision) {
956 /* Get the specific revision. */
957 *mod = ly_ctx_get_module(ctx, name, revision);
958 } else {
Michal Vaskobdac23f2022-01-12 14:02:35 +0100959 /* Get the requested module in a suitable revision in the context. */
960 *mod = lys_get_module_without_revision(ctx, name);
961 if (*mod && !(*mod)->implemented && !((*mod)->latest_revision & LYS_MOD_IMPORTED_REV)) {
aPiecek4725aea2021-07-28 10:18:33 +0200962 /* Let us now search with callback and searchpaths to check
963 * if there is newer revision outside the context.
964 */
965 mod_latest = *mod;
966 *mod = NULL;
967 }
968 }
969
Michal Vasko0550b762020-11-24 18:04:08 +0100970 if (!*mod) {
aPiecek4725aea2021-07-28 10:18:33 +0200971 /* No suitable module in the context, try to load it. */
Michal Vaskobdac23f2022-01-12 14:02:35 +0100972 LY_CHECK_RET(lys_parse_load_from_clb_or_file(ctx, name, revision, mod_latest, new_mods, mod));
aPiecek4725aea2021-07-28 10:18:33 +0200973 if (!*mod && !mod_latest) {
Michal Vasko4e205e82021-06-08 14:01:47 +0200974 LOGVAL(ctx, LYVE_REFERENCE, "Loading \"%s\" module failed.", name);
Michal Vasko0550b762020-11-24 18:04:08 +0100975 return LY_EVALID;
976 }
aPiecek4725aea2021-07-28 10:18:33 +0200977
978 /* Update the latest_revision flag - here we have selected the latest available schema,
979 * consider that even the callback provides correct latest revision.
980 */
981 if (!*mod) {
982 LOGVRB("Newer revision than \"%s@%s\" not found, using this as the latest revision.",
983 mod_latest->name, mod_latest->revision);
984 assert(mod_latest->latest_revision & LYS_MOD_LATEST_REV);
985 mod_latest->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS;
986 *mod = mod_latest;
987 } else if (*mod && !revision && ((*mod)->latest_revision & LYS_MOD_LATEST_REV)) {
988 (*mod)->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS;
989 }
Radek Krejci086c7132018-10-26 15:29:04 +0200990 }
Radek Krejci086c7132018-10-26 15:29:04 +0200991
aPiecek9f8c7e72021-07-28 12:01:56 +0200992 /* Checking the circular dependence of imported modules. */
993 LY_CHECK_RET(lys_check_circular_dependency(ctx, mod));
994
Radek Krejci086c7132018-10-26 15:29:04 +0200995 return LY_SUCCESS;
996}
997
998LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200999lysp_check_stringchar(struct lys_parser_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +02001000{
1001 if (!is_yangutf8char(c)) {
1002 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
1003 return LY_EVALID;
1004 }
1005 return LY_SUCCESS;
1006}
1007
1008LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001009lysp_check_identifierchar(struct lys_parser_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +02001010{
Michal Vasko69730152020-10-09 16:30:07 +02001011 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +02001012 if (!is_yangidentstartchar(c)) {
aPiecekc89b2242021-05-14 14:19:11 +02001013 if ((c < UCHAR_MAX) && isprint(c)) {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001014 if (ctx) {
1015 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
1016 }
Michal Vasko7c769042021-03-25 12:20:49 +01001017 } else {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001018 if (ctx) {
1019 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character 0x%04x.", c);
1020 }
Michal Vasko7c769042021-03-25 12:20:49 +01001021 }
David Sedlák4a650532019-07-10 11:55:18 +02001022 return LY_EVALID;
1023 }
1024 if (prefix) {
1025 if (first) {
1026 (*prefix) = 0;
1027 } else {
1028 (*prefix) = 2;
1029 }
1030 }
Michal Vasko69730152020-10-09 16:30:07 +02001031 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +02001032 (*prefix) = 1;
1033 } else if (!is_yangidentchar(c)) {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001034 if (ctx) {
1035 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
1036 }
David Sedlák4a650532019-07-10 11:55:18 +02001037 return LY_EVALID;
1038 }
1039
1040 return LY_SUCCESS;
1041}
1042
Radek Krejci771928a2021-01-19 13:42:36 +01001043/**
1044 * @brief Try to find the parsed submodule in main module for the given include record.
1045 *
1046 * @param[in] pctx main parser context
1047 * @param[in] inc The include record with missing parsed submodule. According to include info try to find
1048 * the corresponding parsed submodule in main module's includes.
1049 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
1050 * @return LY_ENOT - the parsed module was not found.
1051 * @return LY_EVALID - YANG rule violation
1052 */
1053static LY_ERR
Michal Vasko8a67eff2021-12-07 14:04:47 +01001054lysp_main_pmod_get_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +02001055{
Radek Krejci771928a2021-01-19 13:42:36 +01001056 LY_ARRAY_COUNT_TYPE i;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001057 struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed;
Michal Vasko69730152020-10-09 16:30:07 +02001058
Radek Krejci771928a2021-01-19 13:42:36 +01001059 LY_ARRAY_FOR(main_pmod->includes, i) {
1060 if (strcmp(main_pmod->includes[i].name, inc->name)) {
1061 continue;
1062 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001063
Radek Krejci771928a2021-01-19 13:42:36 +01001064 if (inc->rev[0] && strncmp(inc->rev, main_pmod->includes[i].rev, LY_REV_SIZE)) {
1065 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1066 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001067 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev,
Radek Krejci771928a2021-01-19 13:42:36 +01001068 main_pmod->includes[i].name, main_pmod->includes[i].rev, main_pmod->mod->name);
1069 return LY_EVALID;
1070 }
1071
1072 inc->submodule = main_pmod->includes[i].submodule;
1073 return inc->submodule ? LY_SUCCESS : LY_ENOT;
1074 }
1075
1076 if (main_pmod->version == LYS_VERSION_1_1) {
1077 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1078 "YANG 1.1 requires all submodules to be included from main module. "
1079 "But submodule \"%s\" includes submodule \"%s\" which is not included by main module \"%s\".",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001080 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->name, main_pmod->mod->name);
Radek Krejci771928a2021-01-19 13:42:36 +01001081 return LY_EVALID;
1082 } else {
1083 return LY_ENOT;
1084 }
1085}
1086
1087/**
Michal Vasko8a67eff2021-12-07 14:04:47 +01001088 * @brief Try to find the parsed submodule in currenlty parsed modules for the given include record.
1089 *
1090 * @param[in] pctx main parser context
1091 * @param[in] inc The include record with missing parsed submodule.
1092 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
1093 * @return LY_ENOT - the parsed module was not found.
1094 * @return LY_EVALID - YANG rule violation
1095 */
1096static LY_ERR
1097lysp_parsed_mods_get_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
1098{
1099 uint32_t i;
1100 struct lysp_submodule *submod;
1101
1102 for (i = 0; i < pctx->parsed_mods->count - 1; ++i) {
1103 submod = pctx->parsed_mods->objs[i];
1104 if (!submod->is_submod) {
1105 continue;
1106 }
1107
1108 if (strcmp(submod->name, inc->name)) {
1109 continue;
1110 }
1111
1112 if (inc->rev[0] && submod->revs && strncmp(inc->rev, submod->revs[0].date, LY_REV_SIZE)) {
1113 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1114 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
1115 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev,
1116 submod->name, submod->revs[0].date, PARSER_CUR_PMOD(pctx)->mod->name);
1117 return LY_EVALID;
1118 }
1119
1120 inc->submodule = submod;
1121 return LY_SUCCESS;
1122 }
1123
1124 return LY_ENOT;
1125}
1126
1127/**
Radek Krejci771928a2021-01-19 13:42:36 +01001128 * @brief Make the copy of the given include record into the main module.
1129 *
1130 * YANG 1.0 does not require the main module to include all the submodules. Therefore, parsing submodules can cause
1131 * reallocating and extending the includes array in the main module by the submodules included only in submodules.
1132 *
1133 * @param[in] pctx main parser context
1134 * @param[in] inc Include record to copy into main module taken from @p pctx.
1135 * @return LY_ERR value.
1136 */
1137static LY_ERR
1138lysp_inject_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
1139{
1140 LY_ARRAY_COUNT_TYPE i;
1141 struct lysp_include *inc_new, *inc_tofill = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001142 struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed;
Radek Krejci771928a2021-01-19 13:42:36 +01001143
1144 /* first, try to find the corresponding record with missing parsed submodule */
1145 LY_ARRAY_FOR(main_pmod->includes, i) {
1146 if (strcmp(main_pmod->includes[i].name, inc->name)) {
1147 continue;
1148 }
1149 inc_tofill = &main_pmod->includes[i];
1150 break;
1151 }
1152
1153 if (inc_tofill) {
1154 inc_tofill->submodule = inc->submodule;
1155 } else {
1156 LY_ARRAY_NEW_RET(PARSER_CTX(pctx), main_pmod->includes, inc_new, LY_EMEM);
1157
1158 inc_new->submodule = inc->submodule;
1159 DUP_STRING_RET(PARSER_CTX(pctx), inc->name, inc_new->name);
1160 DUP_STRING_RET(PARSER_CTX(pctx), inc->dsc, inc_new->dsc);
1161 DUP_STRING_RET(PARSER_CTX(pctx), inc->ref, inc_new->ref);
1162 /* TODO duplicate extensions */
1163 memcpy(inc_new->rev, inc->rev, LY_REV_SIZE);
1164 inc_new->injected = 1;
1165 }
1166 return LY_SUCCESS;
1167}
1168
1169LY_ERR
aPiecekc3e26142021-06-22 14:25:49 +02001170lysp_load_submodules(struct lys_parser_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
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001330LIBYANG_API_DEF enum ly_stmt
Radek Krejci39b7fc22021-02-26 23:29:18 +01001331lys_nodetype2stmt(uint16_t nodetype)
1332{
1333 switch (nodetype) {
1334 case LYS_CONTAINER:
1335 return LY_STMT_CONTAINER;
1336 case LYS_CHOICE:
1337 return LY_STMT_CHOICE;
1338 case LYS_LEAF:
1339 return LY_STMT_LEAF;
1340 case LYS_LEAFLIST:
1341 return LY_STMT_LEAF_LIST;
1342 case LYS_LIST:
1343 return LY_STMT_LIST;
1344 case LYS_ANYXML:
1345 return LY_STMT_ANYXML;
1346 case LYS_ANYDATA:
1347 return LY_STMT_ANYDATA;
1348 case LYS_CASE:
1349 return LY_STMT_CASE;
1350 case LYS_RPC:
1351 return LY_STMT_RPC;
1352 case LYS_ACTION:
1353 return LY_STMT_ACTION;
1354 case LYS_NOTIF:
1355 return LY_STMT_NOTIFICATION;
1356 case LYS_USES:
1357 return LY_STMT_USES;
1358 case LYS_INPUT:
1359 return LY_STMT_INPUT;
1360 case LYS_OUTPUT:
1361 return LY_STMT_OUTPUT;
1362 default:
1363 return LY_STMT_NONE;
1364 }
1365}
1366
Radek Krejci693262f2019-04-29 15:23:20 +02001367const char *
1368lys_datatype2str(LY_DATA_TYPE basetype)
1369{
Michal Vaskod989ba02020-08-24 10:59:24 +02001370 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001371 case LY_TYPE_BINARY:
1372 return "binary";
1373 case LY_TYPE_UINT8:
1374 return "uint8";
1375 case LY_TYPE_UINT16:
1376 return "uint16";
1377 case LY_TYPE_UINT32:
1378 return "uint32";
1379 case LY_TYPE_UINT64:
1380 return "uint64";
1381 case LY_TYPE_STRING:
1382 return "string";
1383 case LY_TYPE_BITS:
1384 return "bits";
1385 case LY_TYPE_BOOL:
1386 return "boolean";
1387 case LY_TYPE_DEC64:
1388 return "decimal64";
1389 case LY_TYPE_EMPTY:
1390 return "empty";
1391 case LY_TYPE_ENUM:
1392 return "enumeration";
1393 case LY_TYPE_IDENT:
1394 return "identityref";
1395 case LY_TYPE_INST:
1396 return "instance-identifier";
1397 case LY_TYPE_LEAFREF:
1398 return "leafref";
1399 case LY_TYPE_UNION:
1400 return "union";
1401 case LY_TYPE_INT8:
1402 return "int8";
1403 case LY_TYPE_INT16:
1404 return "int16";
1405 case LY_TYPE_INT32:
1406 return "int32";
1407 case LY_TYPE_INT64:
1408 return "int64";
1409 default:
1410 return "unknown";
1411 }
1412}
1413
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001414LIBYANG_API_DEF const struct lysp_tpdf *
Radek Krejci056d0a82018-12-06 16:57:25 +01001415lysp_node_typedefs(const struct lysp_node *node)
1416{
Radek Krejci0fb28562018-12-13 15:17:37 +01001417 switch (node->nodetype) {
1418 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001419 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001420 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001421 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001422 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001423 return ((struct lysp_node_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001424 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001425 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001426 return ((struct lysp_node_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001427 case LYS_INPUT:
1428 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001429 return ((struct lysp_node_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001430 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001431 return ((struct lysp_node_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001432 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001433 return NULL;
1434 }
1435}
1436
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001437LIBYANG_API_DEF const struct lysp_node_grp *
Radek Krejci53ea6152018-12-13 15:21:15 +01001438lysp_node_groupings(const struct lysp_node *node)
1439{
1440 switch (node->nodetype) {
1441 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001442 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001443 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001444 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001445 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001446 return ((struct lysp_node_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001447 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001448 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001449 return ((struct lysp_node_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001450 case LYS_INPUT:
1451 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001452 return ((struct lysp_node_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001453 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001454 return ((struct lysp_node_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001455 default:
1456 return NULL;
1457 }
1458}
1459
Radek Krejci2a9fc652021-01-22 17:44:34 +01001460struct lysp_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001461lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001462{
1463 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001464
Radek Krejcibbe09a92018-11-08 09:36:54 +01001465 switch (node->nodetype) {
1466 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001467 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001468 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001469 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001470 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001471 return &((struct lysp_node_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001472 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001473 return &((struct lysp_node_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001474 default:
1475 return NULL;
1476 }
1477}
1478
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001479LIBYANG_API_DEF const struct lysp_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001480lysp_node_actions(const struct lysp_node *node)
1481{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001482 struct lysp_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001483
Michal Vasko22df3f02020-08-24 13:29:22 +02001484 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001485 if (actions) {
1486 return *actions;
1487 } else {
1488 return NULL;
1489 }
1490}
1491
Radek Krejci2a9fc652021-01-22 17:44:34 +01001492struct lysp_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001493lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001494{
1495 assert(node);
1496 switch (node->nodetype) {
1497 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001498 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001499 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001500 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001501 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001502 return &((struct lysp_node_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001503 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001504 return &((struct lysp_node_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001505 default:
1506 return NULL;
1507 }
1508}
1509
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001510LIBYANG_API_DEF const struct lysp_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001511lysp_node_notifs(const struct lysp_node *node)
1512{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001513 struct lysp_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001514
Michal Vasko22df3f02020-08-24 13:29:22 +02001515 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001516 if (notifs) {
1517 return *notifs;
1518 } else {
1519 return NULL;
1520 }
1521}
1522
Radek Krejcibbe09a92018-11-08 09:36:54 +01001523struct lysp_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001524lysp_node_child_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001525{
1526 assert(node);
1527 switch (node->nodetype) {
1528 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001529 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001530 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001531 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001532 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001533 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001534 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001535 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001536 case LYS_GROUPING:
Radek Krejci01180ac2021-01-27 08:48:22 +01001537 return &((struct lysp_node_grp *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001538 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001539 return &((struct lysp_node_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001540 case LYS_INPUT:
1541 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001542 return &((struct lysp_node_action_inout *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001543 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001544 return &((struct lysp_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001545 default:
1546 return NULL;
1547 }
1548}
1549
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001550LIBYANG_API_DEF const struct lysp_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001551lysp_node_child(const struct lysp_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01001552{
Michal Vasko544e58a2021-01-28 14:33:41 +01001553 struct lysp_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001554
1555 if (!node) {
1556 return NULL;
1557 }
1558
Michal Vasko544e58a2021-01-28 14:33:41 +01001559 child = lysp_node_child_p((struct lysp_node *)node);
1560 if (child) {
1561 return *child;
Radek Krejci056d0a82018-12-06 16:57:25 +01001562 } else {
1563 return NULL;
1564 }
1565}
1566
Radek Krejci9a3823e2021-01-27 20:26:46 +01001567struct lysp_restr **
1568lysp_node_musts_p(const struct lysp_node *node)
1569{
1570 if (!node) {
1571 return NULL;
1572 }
1573
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001574 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001575 case LYS_CONTAINER:
1576 return &((struct lysp_node_container *)node)->musts;
1577 case LYS_LEAF:
1578 return &((struct lysp_node_leaf *)node)->musts;
1579 case LYS_LEAFLIST:
1580 return &((struct lysp_node_leaflist *)node)->musts;
1581 case LYS_LIST:
1582 return &((struct lysp_node_list *)node)->musts;
1583 case LYS_ANYXML:
1584 case LYS_ANYDATA:
1585 return &((struct lysp_node_anydata *)node)->musts;
1586 case LYS_NOTIF:
1587 return &((struct lysp_node_notif *)node)->musts;
1588 case LYS_INPUT:
1589 case LYS_OUTPUT:
1590 return &((struct lysp_node_action_inout *)node)->musts;
1591 default:
1592 return NULL;
1593 }
1594}
1595
1596struct lysp_restr *
1597lysp_node_musts(const struct lysp_node *node)
1598{
1599 struct lysp_restr **musts;
1600
1601 musts = lysp_node_musts_p(node);
1602 if (musts) {
1603 return *musts;
1604 } else {
1605 return NULL;
1606 }
1607}
1608
1609struct lysp_when **
1610lysp_node_when_p(const struct lysp_node *node)
1611{
1612 if (!node) {
1613 return NULL;
1614 }
1615
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001616 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001617 case LYS_CONTAINER:
1618 return &((struct lysp_node_container *)node)->when;
1619 case LYS_CHOICE:
1620 return &((struct lysp_node_choice *)node)->when;
1621 case LYS_LEAF:
1622 return &((struct lysp_node_leaf *)node)->when;
1623 case LYS_LEAFLIST:
1624 return &((struct lysp_node_leaflist *)node)->when;
1625 case LYS_LIST:
1626 return &((struct lysp_node_list *)node)->when;
1627 case LYS_ANYXML:
1628 case LYS_ANYDATA:
1629 return &((struct lysp_node_anydata *)node)->when;
1630 case LYS_CASE:
1631 return &((struct lysp_node_case *)node)->when;
1632 case LYS_USES:
1633 return &((struct lysp_node_uses *)node)->when;
1634 case LYS_AUGMENT:
1635 return &((struct lysp_node_augment *)node)->when;
1636 default:
1637 return NULL;
1638 }
1639}
1640
1641struct lysp_when *
1642lysp_node_when(const struct lysp_node *node)
1643{
1644 struct lysp_when **when;
1645
1646 when = lysp_node_when_p(node);
1647 if (when) {
1648 return *when;
1649 } else {
1650 return NULL;
1651 }
1652}
1653
Radek Krejci2a9fc652021-01-22 17:44:34 +01001654struct lysc_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001655lysc_node_actions_p(struct lysc_node *node)
1656{
1657 assert(node);
1658 switch (node->nodetype) {
1659 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001660 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001661 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001662 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001663 default:
1664 return NULL;
1665 }
1666}
1667
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001668LIBYANG_API_DEF const struct lysc_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001669lysc_node_actions(const struct lysc_node *node)
1670{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001671 struct lysc_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001672
Michal Vasko22df3f02020-08-24 13:29:22 +02001673 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001674 if (actions) {
1675 return *actions;
1676 } else {
1677 return NULL;
1678 }
1679}
1680
Radek Krejci2a9fc652021-01-22 17:44:34 +01001681struct lysc_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001682lysc_node_notifs_p(struct lysc_node *node)
1683{
1684 assert(node);
1685 switch (node->nodetype) {
1686 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001687 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001688 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001689 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001690 default:
1691 return NULL;
1692 }
1693}
1694
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001695LIBYANG_API_DEF const struct lysc_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001696lysc_node_notifs(const struct lysc_node *node)
1697{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001698 struct lysc_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001699
Michal Vasko22df3f02020-08-24 13:29:22 +02001700 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001701 if (notifs) {
1702 return *notifs;
1703 } else {
1704 return NULL;
1705 }
1706}
1707
Radek Krejcibbe09a92018-11-08 09:36:54 +01001708struct lysc_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001709lysc_node_child_p(const struct lysc_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001710{
Michal Vasko544e58a2021-01-28 14:33:41 +01001711 assert(node && !(node->nodetype & (LYS_RPC | LYS_ACTION)));
1712
Radek Krejcibbe09a92018-11-08 09:36:54 +01001713 switch (node->nodetype) {
1714 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001715 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001716 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001717 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001718 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001719 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001720 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001721 return &((struct lysc_node_list *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001722 case LYS_INPUT:
1723 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001724 return &((struct lysc_node_action_inout *)node)->child;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001725 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001726 return &((struct lysc_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001727 default:
1728 return NULL;
1729 }
1730}
1731
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001732LIBYANG_API_DEF const struct lysc_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001733lysc_node_child(const struct lysc_node *node)
Radek Krejcia3045382018-11-22 14:30:31 +01001734{
Michal Vasko544e58a2021-01-28 14:33:41 +01001735 struct lysc_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001736
1737 if (!node) {
1738 return NULL;
1739 }
1740
Michal Vasko544e58a2021-01-28 14:33:41 +01001741 if (node->nodetype & (LYS_RPC | LYS_ACTION)) {
1742 return &((struct lysc_node_action *)node)->input.node;
Radek Krejcibe154442021-01-21 11:06:36 +01001743 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01001744 child = lysc_node_child_p(node);
1745 if (child) {
1746 return *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001747 }
Michal Vasko2a668712020-10-21 11:48:09 +02001748 }
Michal Vasko544e58a2021-01-28 14:33:41 +01001749
1750 return NULL;
Michal Vasko2a668712020-10-21 11:48:09 +02001751}
1752
Radek Krejci9a3823e2021-01-27 20:26:46 +01001753struct lysc_must **
1754lysc_node_musts_p(const struct lysc_node *node)
1755{
1756 if (!node) {
1757 return NULL;
1758 }
1759
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001760 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001761 case LYS_CONTAINER:
1762 return &((struct lysc_node_container *)node)->musts;
1763 case LYS_LEAF:
1764 return &((struct lysc_node_leaf *)node)->musts;
1765 case LYS_LEAFLIST:
1766 return &((struct lysc_node_leaflist *)node)->musts;
1767 case LYS_LIST:
1768 return &((struct lysc_node_list *)node)->musts;
1769 case LYS_ANYXML:
1770 case LYS_ANYDATA:
1771 return &((struct lysc_node_anydata *)node)->musts;
1772 case LYS_NOTIF:
1773 return &((struct lysc_node_notif *)node)->musts;
1774 case LYS_INPUT:
1775 case LYS_OUTPUT:
1776 return &((struct lysc_node_action_inout *)node)->musts;
1777 default:
1778 return NULL;
1779 }
1780}
1781
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001782LIBYANG_API_DEF struct lysc_must *
Radek Krejci9a3823e2021-01-27 20:26:46 +01001783lysc_node_musts(const struct lysc_node *node)
1784{
1785 struct lysc_must **must_p;
1786
1787 must_p = lysc_node_musts_p(node);
1788 if (must_p) {
1789 return *must_p;
1790 } else {
1791 return NULL;
1792 }
1793}
1794
1795struct lysc_when ***
1796lysc_node_when_p(const struct lysc_node *node)
1797{
1798 if (!node) {
1799 return NULL;
1800 }
1801
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001802 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001803 case LYS_CONTAINER:
1804 return &((struct lysc_node_container *)node)->when;
1805 case LYS_CHOICE:
1806 return &((struct lysc_node_choice *)node)->when;
1807 case LYS_LEAF:
1808 return &((struct lysc_node_leaf *)node)->when;
1809 case LYS_LEAFLIST:
1810 return &((struct lysc_node_leaflist *)node)->when;
1811 case LYS_LIST:
1812 return &((struct lysc_node_list *)node)->when;
1813 case LYS_ANYXML:
1814 case LYS_ANYDATA:
1815 return &((struct lysc_node_anydata *)node)->when;
1816 case LYS_CASE:
1817 return &((struct lysc_node_case *)node)->when;
1818 case LYS_NOTIF:
1819 return &((struct lysc_node_notif *)node)->when;
1820 case LYS_RPC:
1821 case LYS_ACTION:
1822 return &((struct lysc_node_action *)node)->when;
1823 default:
1824 return NULL;
1825 }
1826}
1827
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001828LIBYANG_API_DEF struct lysc_when **
Radek Krejci9a3823e2021-01-27 20:26:46 +01001829lysc_node_when(const struct lysc_node *node)
1830{
1831 struct lysc_when ***when_p;
1832
1833 when_p = lysc_node_when_p(node);
1834 if (when_p) {
1835 return *when_p;
1836 } else {
1837 return NULL;
1838 }
1839}
1840
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001841struct lys_module *
1842lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1843{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001844 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001845 if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) {
Michal Vasko69730152020-10-09 16:30:07 +02001846 return (struct lys_module *)ctx->list.objs[u];
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001847 }
1848 }
1849 return NULL;
1850}
1851
Radek Krejcid6b76452019-09-03 17:03:03 +02001852enum ly_stmt
Radek Krejcid54412f2020-12-17 20:25:35 +01001853lysp_match_kw(struct ly_in *in, uint64_t *indent)
David Sedlákc10e7902018-12-17 02:17:59 +01001854{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001855/**
Radek Krejcid54412f2020-12-17 20:25:35 +01001856 * @brief Move the input by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001857 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001858 *
1859 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001860 */
Radek Krejcid54412f2020-12-17 20:25:35 +01001861#define MOVE_IN(COUNT) \
1862 ly_in_skip(in, COUNT); \
1863 if (indent) { \
1864 (*indent)+=COUNT; \
1865 }
1866#define IF_KW(STR, LEN, STMT) \
1867 if (!strncmp(in->current, STR, LEN)) { \
1868 MOVE_IN(LEN); \
1869 (*kw)=STMT; \
1870 }
1871#define IF_KW_PREFIX(STR, LEN) \
1872 if (!strncmp(in->current, STR, LEN)) { \
1873 MOVE_IN(LEN);
1874#define IF_KW_PREFIX_END \
1875 }
David Sedlák572e7ab2019-06-04 16:01:58 +02001876
Michal Vasko63f3d842020-07-08 10:10:14 +02001877 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001878 enum ly_stmt result = LY_STMT_NONE;
1879 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001880 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001881 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001882 case 'a':
Radek Krejcid54412f2020-12-17 20:25:35 +01001883 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001884 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1885 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1886 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001887 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001888 IF_KW("data", 4, LY_STMT_ANYDATA)
1889 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001890 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001891 break;
1892 case 'b':
Radek Krejcid54412f2020-12-17 20:25:35 +01001893 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001894 IF_KW("ase", 3, LY_STMT_BASE)
1895 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1896 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001897 break;
1898 case 'c':
Radek Krejcid54412f2020-12-17 20:25:35 +01001899 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001900 IF_KW("ase", 3, LY_STMT_CASE)
1901 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001902 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001903 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001904 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001905 IF_KW("ct", 2, LY_STMT_CONTACT)
1906 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001907 IF_KW_PREFIX_END
1908 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001909 break;
1910 case 'd':
Radek Krejcid54412f2020-12-17 20:25:35 +01001911 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001912 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001913 IF_KW("fault", 5, LY_STMT_DEFAULT)
1914 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001915 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001916 IF_KW("e", 1, LY_STMT_DEVIATE)
1917 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001918 IF_KW_PREFIX_END
1919 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001920 break;
1921 case 'e':
Radek Krejcid54412f2020-12-17 20:25:35 +01001922 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001923 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001924 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001925 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1926 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001927 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001928 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001929 break;
1930 case 'f':
Radek Krejcid54412f2020-12-17 20:25:35 +01001931 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001932 IF_KW("eature", 6, LY_STMT_FEATURE)
1933 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001934 break;
1935 case 'g':
Radek Krejcid54412f2020-12-17 20:25:35 +01001936 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001937 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001938 break;
1939 case 'i':
Radek Krejcid54412f2020-12-17 20:25:35 +01001940 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001941 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1942 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1943 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001944 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001945 IF_KW("clude", 5, LY_STMT_INCLUDE)
1946 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001947 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001948 break;
1949 case 'k':
Radek Krejcid54412f2020-12-17 20:25:35 +01001950 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001951 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001952 break;
1953 case 'l':
Radek Krejcid54412f2020-12-17 20:25:35 +01001954 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001955 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001956 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1957 else IF_KW("af", 2, LY_STMT_LEAF)
1958 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001959 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001960 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001961 break;
1962 case 'm':
Radek Krejcid54412f2020-12-17 20:25:35 +01001963 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001964 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001965 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1966 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001967 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001968 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1969 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001970 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001971 IF_KW("ule", 3, LY_STMT_MODULE)
1972 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001973 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001974 break;
1975 case 'n':
Radek Krejcid54412f2020-12-17 20:25:35 +01001976 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001977 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1978 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001979 break;
1980 case 'o':
Radek Krejcid54412f2020-12-17 20:25:35 +01001981 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001982 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001983 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1984 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001985 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001986 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001987 break;
1988 case 'p':
Radek Krejcid54412f2020-12-17 20:25:35 +01001989 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001990 IF_KW("ath", 3, LY_STMT_PATH)
1991 else IF_KW("attern", 6, LY_STMT_PATTERN)
1992 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001993 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001994 IF_KW("fix", 3, LY_STMT_PREFIX)
1995 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001996 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001997 break;
1998 case 'r':
Radek Krejcid54412f2020-12-17 20:25:35 +01001999 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002000 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002001 else IF_KW_PREFIX("e", 1)
2002 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02002003 IF_KW("erence", 6, LY_STMT_REFERENCE)
2004 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002005 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002006 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
2007 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
2008 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002009 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002010 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02002011 break;
2012 case 's':
Radek Krejcid54412f2020-12-17 20:25:35 +01002013 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002014 IF_KW("tatus", 5, LY_STMT_STATUS)
2015 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02002016 break;
2017 case 't':
Radek Krejcid54412f2020-12-17 20:25:35 +01002018 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002019 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
2020 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02002021 break;
2022 case 'u':
Radek Krejcid54412f2020-12-17 20:25:35 +01002023 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02002024 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02002025 IF_KW("que", 3, LY_STMT_UNIQUE)
2026 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002027 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002028 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02002029 break;
2030 case 'v':
Radek Krejcid54412f2020-12-17 20:25:35 +01002031 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002032 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02002033 break;
2034 case 'w':
Radek Krejcid54412f2020-12-17 20:25:35 +01002035 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002036 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02002037 break;
2038 case 'y':
Radek Krejcid54412f2020-12-17 20:25:35 +01002039 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002040 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
2041 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02002042 break;
David Sedlák23a59a62018-10-26 13:08:02 +02002043 default:
Radek Krejcid54412f2020-12-17 20:25:35 +01002044 /* if indent is not NULL we are matching keyword from YANG data */
2045 if (indent) {
Michal Vasko63f3d842020-07-08 10:10:14 +02002046 if (in->current[0] == ';') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002047 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002048 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02002049 } else if (in->current[0] == '{') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002050 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002051 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002052 } else if (in->current[0] == '}') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002053 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002054 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02002055 }
2056 }
David Sedlák23a59a62018-10-26 13:08:02 +02002057 break;
2058 }
2059
Michal Vasko63f3d842020-07-08 10:10:14 +02002060 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02002061 /* the keyword is not terminated */
2062 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002063 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02002064 }
2065
David Sedlák1bccdfa2019-06-17 15:55:27 +02002066#undef IF_KW
2067#undef IF_KW_PREFIX
2068#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01002069#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02002070 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01002071
David Sedlák1bccdfa2019-06-17 15:55:27 +02002072 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02002073}
David Sedlákecf5eb82019-06-03 14:12:44 +02002074
Radek Krejci85ac8312021-03-03 20:21:33 +01002075LY_ERR
2076lysp_ext_find_definition(const struct ly_ctx *ctx, const struct lysp_ext_instance *ext, const struct lys_module **ext_mod,
2077 struct lysp_ext **ext_def)
2078{
Radek Krejci85ac8312021-03-03 20:21:33 +01002079 const char *tmp, *name, *prefix;
2080 size_t pref_len, name_len;
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002081 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci85ac8312021-03-03 20:21:33 +01002082 const struct lys_module *mod = NULL;
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002083 const struct lysp_submodule *submod;
Radek Krejci85ac8312021-03-03 20:21:33 +01002084
Radek Krejcicbb62422021-03-15 09:29:21 +01002085 assert(ext_def);
2086
Radek Krejci85ac8312021-03-03 20:21:33 +01002087 *ext_def = NULL;
2088 if (ext_mod) {
2089 *ext_mod = NULL;
2090 }
2091
Radek Krejci677abea2021-03-05 14:24:53 +01002092 /* parse the prefix, the nodeid was previously already parsed and checked */
Radek Krejci85ac8312021-03-03 20:21:33 +01002093 tmp = ext->name;
Radek Krejci677abea2021-03-05 14:24:53 +01002094 ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len);
Radek Krejci85ac8312021-03-03 20:21:33 +01002095
2096 /* get module where the extension definition should be placed */
2097 mod = ly_resolve_prefix(ctx, prefix, pref_len, ext->format, ext->prefix_data);
2098 if (!mod) {
Radek Krejci422afb12021-03-04 16:38:16 +01002099 LOGVAL(ctx, LYVE_REFERENCE, "Invalid prefix \"%.*s\" used for extension instance identifier.", (int)pref_len, prefix);
Radek Krejci85ac8312021-03-03 20:21:33 +01002100 return LY_EVALID;
2101 } else if (!mod->parsed->extensions) {
2102 LOGVAL(ctx, LYVE_REFERENCE, "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
2103 ext->name, mod->name);
2104 return LY_EVALID;
2105 }
2106
2107 /* find the parsed extension definition there */
2108 LY_ARRAY_FOR(mod->parsed->extensions, v) {
2109 if (!strcmp(name, mod->parsed->extensions[v].name)) {
2110 *ext_def = &mod->parsed->extensions[v];
2111 break;
2112 }
2113 }
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002114 if (!*ext_def) {
2115 LY_ARRAY_FOR(mod->parsed->includes, u) {
2116 submod = mod->parsed->includes[u].submodule;
2117 LY_ARRAY_FOR(submod->extensions, v) {
2118 if (!strcmp(name, submod->extensions[v].name)) {
2119 *ext_def = &submod->extensions[v];
2120 break;
2121 }
2122 }
2123 }
2124 }
Radek Krejci85ac8312021-03-03 20:21:33 +01002125
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002126 if (!*ext_def) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002127 LOGVAL(ctx, LYVE_REFERENCE, "Extension definition of extension instance \"%s\" not found.", ext->name);
2128 return LY_EVALID;
2129 }
2130
2131 if (ext_mod) {
2132 *ext_mod = mod;
2133 }
2134 return LY_SUCCESS;
2135}
2136
2137LY_ERR
2138lysp_ext_instance_resolve_argument(struct ly_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysp_ext *ext_def)
2139{
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002140 if (!ext_def->argname || ext_p->argument) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002141 /* nothing to do */
2142 return LY_SUCCESS;
2143 }
2144
Radek Krejci8df109d2021-04-23 12:19:08 +02002145 if (ext_p->format == LY_VALUE_XML) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002146 /* Schema was parsed from YIN and an argument is expected, ... */
2147 struct lysp_stmt *stmt = NULL;
2148
2149 if (ext_def->flags & LYS_YINELEM_TRUE) {
2150 /* ... argument was the first XML child element */
2151 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {}
2152 if (stmt) {
2153 const char *arg, *ext, *name_arg, *name_ext, *prefix_arg, *prefix_ext;
2154 size_t name_arg_len, name_ext_len, prefix_arg_len, prefix_ext_len;
2155
2156 stmt = ext_p->child;
2157
2158 arg = stmt->stmt;
2159 ly_parse_nodeid(&arg, &prefix_arg, &prefix_arg_len, &name_arg, &name_arg_len);
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002160 if (ly_strncmp(ext_def->argname, name_arg, name_arg_len)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002161 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" expects argument element \"%s\" as its first XML child, "
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002162 "but \"%.*s\" element found.", ext_p->name, ext_def->argname, (int)name_arg_len, name_arg);
Radek Krejci85ac8312021-03-03 20:21:33 +01002163 return LY_EVALID;
2164 }
2165
2166 /* check namespace - all the extension instances must be qualified and argument element is expected in the same
2167 * namespace. Do not check just prefixes, there can be different prefixes pointing to the same namespace */
2168 ext = ext_p->name; /* include prefix */
2169 ly_parse_nodeid(&ext, &prefix_ext, &prefix_ext_len, &name_ext, &name_ext_len);
2170
2171 if (ly_resolve_prefix(ctx, prefix_ext, prefix_ext_len, ext_p->format, ext_p->prefix_data) !=
2172 ly_resolve_prefix(ctx, prefix_arg, prefix_arg_len, stmt->format, stmt->prefix_data)) {
2173 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" element and its argument element \"%s\" are "
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002174 "expected in the same namespace, but they differ.", ext_p->name, ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002175 return LY_EVALID;
2176 }
2177 }
2178 } else {
2179 /* ... argument was one of the XML attributes which are represented as child stmt
2180 * with LYS_YIN_ATTR flag */
2181 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002182 if (!strcmp(stmt->stmt, ext_def->argname)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002183 /* this is the extension's argument */
2184 break;
2185 }
2186 }
2187 }
2188
2189 if (stmt) {
2190 LY_CHECK_RET(lydict_insert(ctx, stmt->arg, 0, &ext_p->argument));
2191 stmt->flags |= LYS_YIN_ARGUMENT;
2192 }
2193 }
2194
2195 if (!ext_p->argument) {
2196 /* missing extension's argument */
2197 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" misses argument %s\"%s\".",
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002198 ext_p->name, (ext_def->flags & LYS_YINELEM_TRUE) ? "element " : "", ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002199 return LY_EVALID;
2200 }
2201
2202 return LY_SUCCESS;
2203}
2204
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002205LY_ARRAY_COUNT_TYPE
Radek Krejcifc596f92021-02-26 22:40:26 +01002206lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, enum ly_stmt substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002207{
2208 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
2209
Michal Vaskod989ba02020-08-24 10:59:24 +02002210 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejciab430862021-03-02 20:13:40 +01002211 if (ext[index].parent_stmt == substmt) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02002212 return index;
2213 }
2214 }
2215
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002216 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002217}
2218
Michal Vaskof8ded142022-02-17 10:48:01 +01002219LIBYANG_API_DEF const struct lysc_node *
Michal Vasko72244882021-01-12 15:21:05 +01002220lysc_data_node(const struct lysc_node *schema)
Michal Vasko62ed12d2020-05-21 10:08:25 +02002221{
2222 const struct lysc_node *parent;
2223
Michal Vasko72244882021-01-12 15:21:05 +01002224 parent = schema;
Radek Krejcidf549132021-01-21 10:32:32 +01002225 while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC |
2226 LYS_ACTION | LYS_NOTIF))) {
Radek Krejci7d95fbb2021-01-26 17:33:13 +01002227 parent = parent->parent;
Michal Vasko72244882021-01-12 15:21:05 +01002228 }
Michal Vasko62ed12d2020-05-21 10:08:25 +02002229
2230 return parent;
2231}
Michal Vaskof4258e12021-06-15 12:11:42 +02002232
2233ly_bool
2234lys_has_recompiled(const struct lys_module *mod)
2235{
2236 LY_ARRAY_COUNT_TYPE u;
2237
2238 if (LYSP_HAS_RECOMPILED(mod->parsed)) {
2239 return 1;
2240 }
2241
2242 LY_ARRAY_FOR(mod->parsed->includes, u) {
2243 if (LYSP_HAS_RECOMPILED(mod->parsed->includes[u].submodule)) {
2244 return 1;
2245 }
2246 }
2247
2248 return 0;
2249}
2250
2251ly_bool
2252lys_has_compiled(const struct lys_module *mod)
2253{
2254 LY_ARRAY_COUNT_TYPE u;
2255
2256 if (LYSP_HAS_COMPILED(mod->parsed)) {
2257 return 1;
2258 }
2259
2260 LY_ARRAY_FOR(mod->parsed->includes, u) {
2261 if (LYSP_HAS_COMPILED(mod->parsed->includes[u].submodule)) {
2262 return 1;
2263 }
2264 }
2265
2266 return 0;
2267}
Michal Vasko7ee5be22021-06-16 17:03:34 +02002268
2269ly_bool
Michal Vasko87cfdba2022-02-22 14:13:45 +01002270lys_has_dep_mods(const struct lys_module *mod)
Michal Vasko7ee5be22021-06-16 17:03:34 +02002271{
2272 LY_ARRAY_COUNT_TYPE u;
2273
Michal Vasko87cfdba2022-02-22 14:13:45 +01002274 /* features */
2275 if (mod->parsed->features) {
Michal Vasko7ee5be22021-06-16 17:03:34 +02002276 return 1;
2277 }
2278
Michal Vasko87cfdba2022-02-22 14:13:45 +01002279 /* groupings */
2280 if (mod->parsed->groupings) {
2281 return 1;
2282 }
Michal Vasko7ee5be22021-06-16 17:03:34 +02002283 LY_ARRAY_FOR(mod->parsed->includes, u) {
2284 if (mod->parsed->includes[u].submodule->groupings) {
2285 return 1;
2286 }
2287 }
2288
Michal Vasko87cfdba2022-02-22 14:13:45 +01002289 /* augments (adding nodes with leafrefs) */
2290 if (mod->parsed->augments) {
2291 return 1;
2292 }
2293 LY_ARRAY_FOR(mod->parsed->includes, u) {
2294 if (mod->parsed->includes[u].submodule->augments) {
2295 return 1;
2296 }
2297 }
2298
Michal Vasko7ee5be22021-06-16 17:03:34 +02002299 return 0;
2300}