blob: 427d4046a2c7c5eb5d82a3b4d2ea99067dced1e0 [file] [log] [blame]
Radek Krejci86d106e2018-10-18 09:53:19 +02001/**
Michal Vasko59892dd2022-05-13 11:02:30 +02002 * @file tree_schema_common.c
Radek Krejci86d106e2018-10-18 09:53:19 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko59892dd2022-05-13 11:02:30 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
5 * @brief Parsing and validation common functions for schema trees
Radek Krejci86d106e2018-10-18 09:53:19 +02006 *
Michal Vasko59892dd2022-05-13 11:02:30 +02007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejci86d106e2018-10-18 09:53:19 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
Radek Krejci535ea9f2020-05-29 16:01:05 +020015
16#define _GNU_SOURCE
Radek Krejci86d106e2018-10-18 09:53:19 +020017
Radek Krejcie7b95092019-05-15 11:03:07 +020018#include <assert.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020019#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010020#include <stddef.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <stdint.h>
Radek Krejci9ed7a192018-10-31 16:23:51 +010022#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020024#include <time.h>
25
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020027#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "context.h"
Radek Krejci77114102021-03-10 15:21:57 +010029#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010031#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020032#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010033#include "log.h"
Michal Vasko69730152020-10-09 16:30:07 +020034#include "parser_schema.h"
Michal Vasko962b6cd2020-12-08 10:07:49 +010035#include "schema_compile.h"
Michal Vasko79135ae2020-12-16 10:08:35 +010036#include "schema_features.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "set.h"
38#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010039#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020040#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020041#include "tree_schema_internal.h"
42
Radek Krejci85747952019-06-07 16:43:43 +020043LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +020044lysp_check_prefix(struct lysp_ctx *ctx, struct lysp_import *imports, const char *module_prefix, const char **value)
Radek Krejci86d106e2018-10-18 09:53:19 +020045{
46 struct lysp_import *i;
47
Michal Vasko69730152020-10-09 16:30:07 +020048 if (module_prefix && (&module_prefix != value) && !strcmp(module_prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010049 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value);
Radek Krejci86d106e2018-10-18 09:53:19 +020050 return LY_EEXIST;
51 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +010052 LY_ARRAY_FOR(imports, struct lysp_import, i) {
Michal Vasko69730152020-10-09 16:30:07 +020053 if (i->prefix && (&i->prefix != value) && !strcmp(i->prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010054 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +010055 return LY_EEXIST;
Radek Krejci86d106e2018-10-18 09:53:19 +020056 }
57 }
58 return LY_SUCCESS;
59}
60
61LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +020062lysp_check_date(struct lysp_ctx *ctx, const char *date, size_t date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +020063{
Radek Krejci86d106e2018-10-18 09:53:19 +020064 struct tm tm, tm_;
65 char *r;
66
Michal 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
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200128LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200129lysp_check_enum_name(struct lysp_ctx *ctx, const char *name, size_t name_len)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100130{
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200131 if (!name_len) {
132 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
133 return LY_EVALID;
134 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
135 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
136 (int)name_len, name);
137 return LY_EVALID;
138 } else {
139 for (size_t u = 0; u < name_len; ++u) {
140 if (iscntrl(name[u])) {
141 LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
142 (int)name_len, name, u + 1);
143 break;
144 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100145 }
146 }
147
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200148 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100149}
150
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200151/**
152 * @brief Learn built-in type from its name.
153 *
154 * @param[in] name Type name.
155 * @param[in] len Length of @p name.
156 * @return Built-in data type, ::LY_TYPE_UNKNOWN if none matches.
157 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100158static LY_DATA_TYPE
159lysp_type_str2builtin(const char *name, size_t len)
160{
161 if (len >= 4) { /* otherwise it does not match any built-in type */
162 if (name[0] == 'b') {
163 if (name[1] == 'i') {
Michal Vasko69730152020-10-09 16:30:07 +0200164 if ((len == 6) && !strncmp(&name[2], "nary", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100165 return LY_TYPE_BINARY;
Michal Vasko69730152020-10-09 16:30:07 +0200166 } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100167 return LY_TYPE_BITS;
168 }
Michal Vasko69730152020-10-09 16:30:07 +0200169 } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100170 return LY_TYPE_BOOL;
171 }
172 } else if (name[0] == 'd') {
Michal Vasko69730152020-10-09 16:30:07 +0200173 if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100174 return LY_TYPE_DEC64;
175 }
176 } else if (name[0] == 'e') {
Michal Vasko69730152020-10-09 16:30:07 +0200177 if ((len == 5) && !strncmp(&name[1], "mpty", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100178 return LY_TYPE_EMPTY;
Michal Vasko69730152020-10-09 16:30:07 +0200179 } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100180 return LY_TYPE_ENUM;
181 }
182 } else if (name[0] == 'i') {
183 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200184 if ((len == 4) && !strncmp(&name[2], "t8", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100185 return LY_TYPE_INT8;
186 } else if (len == 5) {
187 if (!strncmp(&name[2], "t16", 3)) {
188 return LY_TYPE_INT16;
189 } else if (!strncmp(&name[2], "t32", 3)) {
190 return LY_TYPE_INT32;
191 } else if (!strncmp(&name[2], "t64", 3)) {
192 return LY_TYPE_INT64;
193 }
Michal Vasko69730152020-10-09 16:30:07 +0200194 } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100195 return LY_TYPE_INST;
196 }
Michal Vasko69730152020-10-09 16:30:07 +0200197 } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100198 return LY_TYPE_IDENT;
199 }
200 } else if (name[0] == 'l') {
Michal Vasko69730152020-10-09 16:30:07 +0200201 if ((len == 7) && !strncmp(&name[1], "eafref", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100202 return LY_TYPE_LEAFREF;
203 }
204 } else if (name[0] == 's') {
Michal Vasko69730152020-10-09 16:30:07 +0200205 if ((len == 6) && !strncmp(&name[1], "tring", 5)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100206 return LY_TYPE_STRING;
207 }
208 } else if (name[0] == 'u') {
209 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200210 if ((len == 5) && !strncmp(&name[2], "ion", 3)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100211 return LY_TYPE_UNION;
212 }
Michal Vasko69730152020-10-09 16:30:07 +0200213 } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) {
214 if ((len == 5) && (name[4] == '8')) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100215 return LY_TYPE_UINT8;
216 } else if (len == 6) {
217 if (!strncmp(&name[4], "16", 2)) {
218 return LY_TYPE_UINT16;
219 } else if (!strncmp(&name[4], "32", 2)) {
220 return LY_TYPE_UINT32;
221 } else if (!strncmp(&name[4], "64", 2)) {
222 return LY_TYPE_UINT64;
223 }
224 }
225 }
226 }
227 }
228
229 return LY_TYPE_UNKNOWN;
230}
231
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200232/**
233 * @brief Find a typedef in a sized array.
234 *
235 * @param[in] name Typedef name.
236 * @param[in] typedefs Sized array of typedefs.
237 * @return Found typedef, NULL if none.
238 */
239static const struct lysp_tpdf *
240lysp_typedef_match(const char *name, const struct lysp_tpdf *typedefs)
241{
242 LY_ARRAY_COUNT_TYPE u;
243
244 LY_ARRAY_FOR(typedefs, u) {
245 if (!strcmp(name, typedefs[u].name)) {
246 /* match */
247 return &typedefs[u];
248 }
249 }
250 return NULL;
251}
252
Radek Krejcibbe09a92018-11-08 09:36:54 +0100253LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +0100254lysp_type_find(const char *id, struct lysp_node *start_node, const struct lysp_module *start_module,
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200255 const struct lysc_ext_instance *ext, LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100256{
257 const char *str, *name;
258 struct lysp_tpdf *typedefs;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200259 const struct lysp_tpdf **ext_typedefs;
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100260 const struct lys_module *mod;
Michal Vaskoa99b3572021-02-01 11:54:58 +0100261 const struct lysp_module *local_module;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200262 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100263
264 assert(id);
265 assert(start_module);
266 assert(tpdf);
267 assert(node);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100268
Radek Krejci4f28eda2018-11-12 11:46:16 +0100269 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100270 str = strchr(id, ':');
271 if (str) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200272 mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_VALUE_SCHEMA, (void *)start_module);
Michal Vaskoa99b3572021-02-01 11:54:58 +0100273 local_module = mod ? mod->parsed : NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100274 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100275 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100276 } else {
Michal Vaskoa99b3572021-02-01 11:54:58 +0100277 local_module = start_module;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100278 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100279
280 /* check for built-in types */
281 *type = lysp_type_str2builtin(name, strlen(name));
282 if (*type) {
283 *tpdf = NULL;
284 return LY_SUCCESS;
285 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100286 }
Michal Vaskoa99b3572021-02-01 11:54:58 +0100287 LY_CHECK_RET(!local_module, LY_ENOTFOUND);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100288
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200289 if (local_module == start_module) {
290 if (start_node) {
291 /* search typedefs in parent's nodes */
292 for (*node = start_node; *node; *node = (*node)->parent) {
293 *tpdf = lysp_typedef_match(name, lysp_node_typedefs(*node));
294 if (*tpdf) {
295 /* match */
296 return LY_SUCCESS;
297 }
298 }
299 }
300
301 if (ext) {
302 /* search typedefs directly in the extension */
303 ext_typedefs = (void *)lys_compile_ext_instance_get_storage(ext, LY_STMT_TYPEDEF);
304 if (ext_typedefs && (*tpdf = lysp_typedef_match(name, *ext_typedefs))) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100305 /* match */
306 return LY_SUCCESS;
307 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100308 }
309 }
310
Michal Vasko915e5442021-06-08 14:59:21 +0200311 /* go to main module if in submodule */
312 local_module = local_module->mod->parsed;
313
Radek Krejcibbe09a92018-11-08 09:36:54 +0100314 /* search in top-level typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100315 if (local_module->typedefs) {
316 LY_ARRAY_FOR(local_module->typedefs, u) {
317 if (!strcmp(name, local_module->typedefs[u].name)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100318 /* match */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100319 *tpdf = &local_module->typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100320 return LY_SUCCESS;
321 }
322 }
323 }
324
Michal Vasko915e5442021-06-08 14:59:21 +0200325 /* search in all submodules' typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100326 LY_ARRAY_FOR(local_module->includes, u) {
327 typedefs = local_module->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100328 LY_ARRAY_FOR(typedefs, v) {
329 if (!strcmp(name, typedefs[v].name)) {
330 /* match */
331 *tpdf = &typedefs[v];
332 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100333 }
334 }
335 }
336
337 return LY_ENOTFOUND;
338}
339
Michal Vaskob36053d2020-03-26 15:49:30 +0100340/**
aPiecekdc12b9f2021-06-25 10:55:47 +0200341 * @brief Insert @p name to hash table and if @p name has already
342 * been added, then log an error.
343 *
344 * This function is used to detect duplicate names.
345 *
346 * @param[in,out] ctx Context to log the error.
347 * @param[in,out] ht Hash table with top-level names.
348 * @param[in] name Inserted top-level identifier.
349 * @param[in] statement The name of the statement type from which
350 * @p name originated (eg typedef, feature, ...).
351 * @param[in] err_detail Optional error specification.
352 * @return LY_ERR, but LY_EEXIST is mapped to LY_EVALID.
353 */
354static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200355lysp_check_dup_ht_insert(struct lysp_ctx *ctx, struct hash_table *ht,
aPiecekdc12b9f2021-06-25 10:55:47 +0200356 const char *name, const char *statement, const char *err_detail)
357{
358 LY_ERR ret;
359 uint32_t hash;
360
361 hash = dict_hash(name, strlen(name));
362 ret = lyht_insert(ht, &name, hash, NULL);
363 if (ret == LY_EEXIST) {
364 if (err_detail) {
365 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT2, name, statement, err_detail);
366 } else {
367 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, name, statement);
368 }
369 ret = LY_EVALID;
370 }
371
372 return ret;
373}
374
375/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100376 * @brief Check name of a new type to avoid name collisions.
377 *
378 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
379 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
380 * @param[in] tpdf Typedef definition to check.
381 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
aPiecekc7594b72021-06-29 09:00:03 +0200382 * typedefs are checked, caller is supposed to free the table.
383 * @return LY_EVALID in case of collision, LY_SUCCESS otherwise.
Radek Krejcibbe09a92018-11-08 09:36:54 +0100384 */
385static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200386lysp_check_dup_typedef(struct lysp_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
aPieceke1fbd952021-06-29 08:12:55 +0200387 struct hash_table *tpdfs_global)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100388{
389 struct lysp_node *parent;
390 uint32_t hash;
391 size_t name_len;
392 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200393 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100394 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100395
396 assert(ctx);
397 assert(tpdf);
398
399 name = tpdf->name;
400 name_len = strlen(name);
401
Radek Krejci4f28eda2018-11-12 11:46:16 +0100402 if (lysp_type_str2builtin(name, name_len)) {
aPiecekc7594b72021-06-29 09:00:03 +0200403 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
404 "Duplicate identifier \"%s\" of typedef statement - name collision with a built-in type.", name);
405 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100406 }
407
408 /* check locally scoped typedefs (avoid name shadowing) */
409 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100410 typedefs = lysp_node_typedefs(node);
411 LY_ARRAY_FOR(typedefs, u) {
412 if (&typedefs[u] == tpdf) {
413 break;
414 }
415 if (!strcmp(name, typedefs[u].name)) {
aPiecekc7594b72021-06-29 09:00:03 +0200416 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
417 "Duplicate identifier \"%s\" of typedef statement - name collision with sibling type.", name);
418 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100419 }
420 }
421 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200422 for (parent = node->parent; parent; parent = parent->parent) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200423 if (lysp_typedef_match(name, lysp_node_typedefs(parent))) {
aPiecekc7594b72021-06-29 09:00:03 +0200424 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
425 "Duplicate identifier \"%s\" of typedef statement - name collision with another scoped type.", name);
426 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100427 }
428 }
429 }
430
431 /* check collision with the top-level typedefs */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100432 if (node) {
aPiecekc7594b72021-06-29 09:00:03 +0200433 hash = dict_hash(name, name_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100434 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
aPiecekc7594b72021-06-29 09:00:03 +0200435 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
436 "Duplicate identifier \"%s\" of typedef statement - scoped type collide with a top-level type.", name);
437 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100438 }
439 } else {
aPiecekc7594b72021-06-29 09:00:03 +0200440 LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, tpdfs_global, name, "typedef",
441 "name collision with another top-level type"));
Radek Krejci3b1f9292018-11-08 10:58:35 +0100442 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
443 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
444 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100445 }
446
447 return LY_SUCCESS;
448}
449
Radek Krejci857189e2020-09-01 13:26:36 +0200450/**
451 * @brief Compare identifiers.
Michal Vasko62524a92021-02-26 10:08:50 +0100452 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200453 */
454static ly_bool
455lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Radek Krejcibbe09a92018-11-08 09:36:54 +0100456{
Michal Vasko11ac39a2021-07-23 12:46:56 +0200457 char *id1, *id2;
458
459 id1 = *(char **)val1;
460 id2 = *(char **)val2;
461
462 return strcmp(id1, id2) == 0 ? 1 : 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100463}
464
465LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200466lysp_check_dup_typedefs(struct lysp_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100467{
468 struct hash_table *ids_global;
Radek Krejci0fb28562018-12-13 15:17:37 +0100469 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200470 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200471 uint32_t i;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100472 LY_ERR ret = LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100473
474 /* check name collisions - typedefs and groupings */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100475 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200476 LY_ARRAY_FOR(mod->typedefs, v) {
aPieceke1fbd952021-06-29 08:12:55 +0200477 ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100478 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100479 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200480 LY_ARRAY_FOR(mod->includes, v) {
481 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200482 ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100483 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci3b1f9292018-11-08 10:58:35 +0100484 }
485 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200486 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
487 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
488 LY_ARRAY_FOR(typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200489 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 +0100490 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100491 }
492 }
Michal Vasko405cc9e2020-12-01 12:01:27 +0100493
Radek Krejcibbe09a92018-11-08 09:36:54 +0100494cleanup:
495 lyht_free(ids_global);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100496 return ret;
497}
498
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200499static const struct lysp_node_grp *
500lysp_grouping_match(const char *name, struct lysp_node *node)
501{
502 const struct lysp_node_grp *groupings, *grp_iter;
503
504 groupings = lysp_node_groupings(node);
505 LY_LIST_FOR(groupings, grp_iter) {
506 if (!strcmp(name, grp_iter->name)) {
507 /* match */
508 return grp_iter;
509 }
510 }
511
512 return NULL;
513}
514
aPiecek63e080d2021-06-29 13:53:28 +0200515/**
516 * @brief Check name of a new grouping to avoid name collisions.
517 *
518 * @param[in] ctx Parser context, module where the grouping is being defined is taken from here.
519 * @param[in] node Schema node where the grouping is being defined, NULL in case of a top-level grouping.
520 * @param[in] grp Grouping definition to check.
521 * @param[in,out] grps_global Initialized hash table to store temporary data between calls. When the module's
522 * groupings are checked, caller is supposed to free the table.
523 * @return LY_EVALID in case of collision, LY_SUCCESS otherwise.
524 */
525static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200526lysp_check_dup_grouping(struct lysp_ctx *ctx, struct lysp_node *node, const struct lysp_node_grp *grp,
aPiecek63e080d2021-06-29 13:53:28 +0200527 struct hash_table *grps_global)
528{
529 struct lysp_node *parent;
530 uint32_t hash;
531 size_t name_len;
532 const char *name;
533 const struct lysp_node_grp *groupings, *grp_iter;
534
535 assert(ctx);
536 assert(grp);
537
538 name = grp->name;
539 name_len = strlen(name);
540
541 /* check locally scoped groupings (avoid name shadowing) */
542 if (node) {
543 groupings = lysp_node_groupings(node);
544 LY_LIST_FOR(groupings, grp_iter) {
545 if (grp_iter == grp) {
546 break;
547 }
548 if (!strcmp(name, grp_iter->name)) {
549 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
550 "Duplicate identifier \"%s\" of grouping statement - name collision with sibling grouping.", name);
551 return LY_EVALID;
552 }
553 }
554 /* search grouping in parent's nodes */
555 for (parent = node->parent; parent; parent = parent->parent) {
556 if (lysp_grouping_match(name, parent)) {
557 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
558 "Duplicate identifier \"%s\" of grouping statement - name collision with another scoped grouping.", name);
559 return LY_EVALID;
560 }
561 }
562 }
563
564 /* check collision with the top-level groupings */
565 if (node) {
566 hash = dict_hash(name, name_len);
567 if (!lyht_find(grps_global, &name, hash, NULL)) {
568 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
569 "Duplicate identifier \"%s\" of grouping statement - scoped grouping collide with a top-level grouping.", name);
570 return LY_EVALID;
571 }
572 } else {
573 LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, grps_global, name, "grouping",
574 "name collision with another top-level grouping"));
575 }
576
577 return LY_SUCCESS;
578}
579
580LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200581lysp_check_dup_groupings(struct lysp_ctx *ctx, struct lysp_module *mod)
aPiecek63e080d2021-06-29 13:53:28 +0200582{
583 struct hash_table *ids_global;
584 const struct lysp_node_grp *groupings, *grp_iter;
585 LY_ARRAY_COUNT_TYPE u;
586 uint32_t i;
587 LY_ERR ret = LY_SUCCESS;
588
589 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
590 LY_LIST_FOR(mod->groupings, grp_iter) {
591 ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global);
592 LY_CHECK_GOTO(ret, cleanup);
593 }
594 LY_ARRAY_FOR(mod->includes, u) {
595 LY_LIST_FOR(mod->includes[u].submodule->groupings, grp_iter) {
596 ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global);
597 LY_CHECK_GOTO(ret, cleanup);
598 }
599 }
600 for (i = 0; i < ctx->grps_nodes.count; ++i) {
601 groupings = lysp_node_groupings((struct lysp_node *)ctx->grps_nodes.objs[i]);
602 LY_LIST_FOR(groupings, grp_iter) {
603 ret = lysp_check_dup_grouping(ctx, (struct lysp_node *)ctx->grps_nodes.objs[i], grp_iter, ids_global);
604 LY_CHECK_GOTO(ret, cleanup);
605 }
606 }
607
608cleanup:
609 lyht_free(ids_global);
610 return ret;
611}
612
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100613static ly_bool
614ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
615{
616 void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
617
618 return ptr1 == ptr2 ? 1 : 0;
619}
620
621LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200622lysp_check_dup_features(struct lysp_ctx *ctx, struct lysp_module *mod)
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100623{
624 LY_ARRAY_COUNT_TYPE u;
625 struct hash_table *ht;
626 struct lysp_feature *f;
aPiecekdc12b9f2021-06-25 10:55:47 +0200627 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100628
aPiecekf6203072021-06-25 10:58:26 +0200629 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100630 LY_CHECK_RET(!ht, LY_EMEM);
631
632 /* add all module features into a hash table */
633 LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200634 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
635 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200636 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100637 }
638
639 /* add all submodule features into a hash table */
640 LY_ARRAY_FOR(mod->includes, u) {
641 LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200642 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
643 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200644 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100645 }
646 }
647
648cleanup:
649 lyht_free(ht);
650 return ret;
651}
652
653LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200654lysp_check_dup_identities(struct lysp_ctx *ctx, struct lysp_module *mod)
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100655{
656 LY_ARRAY_COUNT_TYPE u;
657 struct hash_table *ht;
658 struct lysp_ident *i;
aPiecekdc12b9f2021-06-25 10:55:47 +0200659 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100660
aPiecekf6203072021-06-25 10:58:26 +0200661 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100662 LY_CHECK_RET(!ht, LY_EMEM);
663
664 /* add all module identities into a hash table */
665 LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200666 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
667 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200668 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100669 }
670
671 /* add all submodule identities into a hash table */
672 LY_ARRAY_FOR(mod->includes, u) {
673 LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200674 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
675 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200676 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100677 }
678 }
679
680cleanup:
681 lyht_free(ht);
682 return ret;
683}
684
Radek Krejci9ed7a192018-10-31 16:23:51 +0100685struct lysp_load_module_check_data {
686 const char *name;
687 const char *revision;
688 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200689 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100690};
691
692static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100693lysp_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 +0100694{
695 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100696 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200697 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100698 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100699 struct lysp_revision *revs;
700
701 name = mod ? mod->mod->name : submod->name;
702 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200703 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100704
705 if (info->name) {
706 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100707 if (strcmp(info->name, name)) {
708 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100709 return LY_EINVAL;
710 }
711 }
712 if (info->revision) {
713 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100714 if (!revs || strcmp(info->revision, revs[0].date)) {
715 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200716 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100717 return LY_EINVAL;
718 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200719 } else if (!latest_revision) {
720 /* do not log, we just need to drop the schema and use the latest revision from the context */
721 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100722 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100723 if (submod) {
724 assert(info->submoduleof);
725
Radek Krejci9ed7a192018-10-31 16:23:51 +0100726 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200727 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100728 LOGVAL(ctx, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200729 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100730 return LY_EVALID;
731 }
732 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100733 if (submod->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100734 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100735 return LY_EVALID;
736 }
737 }
738 if (info->path) {
739 /* check that name and revision match filename */
740 filename = strrchr(info->path, '/');
741 if (!filename) {
742 filename = info->path;
743 } else {
744 filename++;
745 }
746 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100747 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100748 rev = strchr(filename, '@');
749 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100750 if (strncmp(filename, name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +0200751 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100752 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100753 }
754 /* revision */
755 if (rev) {
756 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100757 if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100758 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +0200759 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100760 }
761 }
762 }
763 return LY_SUCCESS;
764}
765
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200766/**
Michal Vasko4e205e82021-06-08 14:01:47 +0200767 * @brief Parse a (sub)module from a local file and add into the context.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200768 *
769 * This function does not check the presence of the (sub)module in context, it should be done before calling this function.
770 *
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200771 * @param[in] ctx libyang context where to work.
772 * @param[in] name Name of the (sub)module to load.
773 * @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 +0200774 * @param[in] main_ctx Parser context of the main module in case of loading submodule.
775 * @param[in] main_name Main module name in case of loading submodule.
776 * @param[in] required Module is required so error (even if the input file not found) are important. If 0, there is some
777 * 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 +0200778 * @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 +0200779 * @param[out] result Parsed YANG schema tree of the requested module (struct lys_module*) or submodule (struct lysp_submodule*).
780 * If it is a module, it is already in the context!
Michal Vasko4e205e82021-06-08 14:01:47 +0200781 * @return LY_SUCCESS on success.
Michal Vasko4e205e82021-06-08 14:01:47 +0200782 * @return LY_ERR on error.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200783 */
784static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200785lys_parse_localfile(struct ly_ctx *ctx, const char *name, const char *revision, struct lysp_ctx *main_ctx,
Michal Vaskodd992582021-06-10 14:34:57 +0200786 const char *main_name, ly_bool required, struct ly_set *new_mods, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100787{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200788 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100789 char *filepath = NULL;
790 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100791 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100792 LY_ERR ret = LY_SUCCESS;
793 struct lysp_load_module_check_data check_data = {0};
794
Michal Vasko87f1cf02021-06-08 14:02:47 +0200795 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name,
796 revision, &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200797 if (!filepath) {
798 if (required) {
799 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200800 revision ? revision : "");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200801 }
802 return LY_ENOTFOUND;
803 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100804
805 LOGVRB("Loading schema from \"%s\" file.", filepath);
806
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200807 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200808 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +0200809 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100810 check_data.name = name;
811 check_data.revision = revision;
812 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800813 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200814 if (main_ctx) {
aPiecekc3e26142021-06-22 14:25:49 +0200815 ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data, new_mods,
Michal Vasko69730152020-10-09 16:30:07 +0200816 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200817 } else {
Michal Vaskodd992582021-06-10 14:34:57 +0200818 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 +0200819
820 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200821 ly_in_free(in, 1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200822 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100823
824 *result = mod;
825
826 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200827
Radek Krejci9ed7a192018-10-31 16:23:51 +0100828cleanup:
829 free(filepath);
830 return ret;
831}
832
aPiecek4725aea2021-07-28 10:18:33 +0200833/**
834 * @brief Load module from searchdirs or from callback.
835 *
836 * @param[in] ctx libyang context where to work.
837 * @param[in] name Name of module to load.
838 * @param[in] revision Revision of module to load.
Michal Vaskobdac23f2022-01-12 14:02:35 +0100839 * @param[in] mod_latest Module with the latest revision found in context, otherwise set to NULL.
840 * @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 +0200841 * @param[out] mod Loaded module.
842 * @return LY_SUCCESS on success.
843 * @return LY_ERR on error.
844 */
845static LY_ERR
846lys_parse_load_from_clb_or_file(struct ly_ctx *ctx, const char *name, const char *revision,
847 struct lys_module *mod_latest, struct ly_set *new_mods, struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200848{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100849 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200850 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200851
Radek Krejci9ed7a192018-10-31 16:23:51 +0100852 void (*module_data_free)(void *module_data, void *user_data) = NULL;
853 struct lysp_load_module_check_data check_data = {0};
Michal Vasko63f3d842020-07-08 10:10:14 +0200854 struct ly_in *in;
Radek Krejci086c7132018-10-26 15:29:04 +0200855
Michal Vaskobdac23f2022-01-12 14:02:35 +0100856 *mod = NULL;
857
858 if (mod_latest && (!ctx->imp_clb || (mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB)) &&
859 ((ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) || (mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) {
860 /* we are not able to find a newer revision */
861 return LY_SUCCESS;
862 }
863
aPiecek4725aea2021-07-28 10:18:33 +0200864 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
865search_clb:
Michal Vaskobdac23f2022-01-12 14:02:35 +0100866 /* check there is a callback and should be called */
867 if (ctx->imp_clb && (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB))) {
868 if (!ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, &format, &module_data, &module_data_free)) {
869 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
870 check_data.name = name;
871 check_data.revision = revision;
872 lys_parse_in(ctx, in, format, lysp_load_module_check, &check_data, new_mods, mod);
873 ly_in_free(in, 0);
874 if (module_data_free) {
875 module_data_free((void *)module_data, ctx->imp_clb_data);
876 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200877 }
Radek Krejci0af46292019-01-11 16:02:31 +0100878 }
Michal Vaskobdac23f2022-01-12 14:02:35 +0100879 if (*mod && !revision) {
880 /* we got the latest revision module from the callback */
881 (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB;
882 } else if (!*mod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
aPiecek4725aea2021-07-28 10:18:33 +0200883 goto search_file;
884 }
885 } else {
886search_file:
Michal Vaskobdac23f2022-01-12 14:02:35 +0100887 /* check we can use searchdirs and that we should */
888 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) &&
889 (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) {
aPiecek4725aea2021-07-28 10:18:33 +0200890 lys_parse_localfile(ctx, name, revision, NULL, NULL, mod_latest ? 0 : 1, new_mods, (void **)mod);
891 }
Michal Vaskobdac23f2022-01-12 14:02:35 +0100892 if (*mod && !revision) {
893 /* we got the latest revision module in the searchdirs */
894 (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB;
895 } else if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
aPiecek4725aea2021-07-28 10:18:33 +0200896 goto search_clb;
897 }
Radek Krejci086c7132018-10-26 15:29:04 +0200898 }
899
aPiecek4725aea2021-07-28 10:18:33 +0200900 return LY_SUCCESS;
901}
902
903/**
aPiecekd4911ee2021-07-30 07:40:24 +0200904 * @brief Get module without revision according to priorities.
905 *
906 * 1. Search for the module with LYS_MOD_IMPORTED_REV.
907 * 2. Search for the implemented module.
908 * 3. Search for the latest module in the context.
909 *
910 * @param[in] ctx libyang context where module is searched.
911 * @param[in] name Name of the searched module.
aPiecekd4911ee2021-07-30 07:40:24 +0200912 * @return Found module from context or NULL.
913 */
914static struct lys_module *
Michal Vaskobdac23f2022-01-12 14:02:35 +0100915lys_get_module_without_revision(struct ly_ctx *ctx, const char *name)
aPiecekd4911ee2021-07-30 07:40:24 +0200916{
917 struct lys_module *mod, *mod_impl;
918 uint32_t index;
919
aPiecekd4911ee2021-07-30 07:40:24 +0200920 /* Try to find module with LYS_MOD_IMPORTED_REV flag. */
921 index = 0;
922 while ((mod = ly_ctx_get_module_iter(ctx, &index))) {
923 if (!strcmp(mod->name, name) && (mod->latest_revision & LYS_MOD_IMPORTED_REV)) {
924 break;
925 }
926 }
927
928 /* Try to find the implemented module. */
929 mod_impl = ly_ctx_get_module_implemented(ctx, name);
930 if (mod && mod_impl) {
Michal Vaskobdac23f2022-01-12 14:02:35 +0100931 LOGVRB("Implemented module \"%s@%s\" is not used for import, revision \"%s\" is imported instead.",
aPiecekd4911ee2021-07-30 07:40:24 +0200932 mod_impl->name, mod_impl->revision, mod->revision);
933 return mod;
934 } else if (mod_impl) {
935 return mod_impl;
936 }
937
938 /* Try to find the latest module in the current context. */
939 mod = ly_ctx_get_module_latest(ctx, name);
aPiecekd4911ee2021-07-30 07:40:24 +0200940
941 return mod;
942}
943
944/**
aPiecek4725aea2021-07-28 10:18:33 +0200945 * @brief Check if a circular dependency exists between modules.
946 *
947 * @param[in] ctx libyang context for log an error.
948 * @param[in,out] mod Examined module which is set to NULL
949 * if the circular dependency is detected.
950 * @return LY_SUCCESS if no circular dependecy is detected,
951 * otherwise LY_EVALID.
952 */
953static LY_ERR
954lys_check_circular_dependency(struct ly_ctx *ctx, struct lys_module **mod)
955{
956 if ((*mod) && (*mod)->parsed->parsing) {
957 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", (*mod)->name);
Michal Vasko4e205e82021-06-08 14:01:47 +0200958 *mod = NULL;
959 return LY_EVALID;
Michal Vasko0550b762020-11-24 18:04:08 +0100960 }
Radek Krejci086c7132018-10-26 15:29:04 +0200961
aPiecek4725aea2021-07-28 10:18:33 +0200962 return LY_SUCCESS;
963}
964
965LY_ERR
966lys_parse_load(struct ly_ctx *ctx, const char *name, const char *revision, struct ly_set *new_mods,
967 struct lys_module **mod)
968{
969 struct lys_module *mod_latest = NULL;
970
971 assert(mod && new_mods);
972
Michal Vasko0550b762020-11-24 18:04:08 +0100973 /*
aPiecek4725aea2021-07-28 10:18:33 +0200974 * Try to get the module from the context.
Michal Vasko0550b762020-11-24 18:04:08 +0100975 */
aPiecek4725aea2021-07-28 10:18:33 +0200976 if (revision) {
977 /* Get the specific revision. */
978 *mod = ly_ctx_get_module(ctx, name, revision);
979 } else {
Michal Vaskobdac23f2022-01-12 14:02:35 +0100980 /* Get the requested module in a suitable revision in the context. */
981 *mod = lys_get_module_without_revision(ctx, name);
982 if (*mod && !(*mod)->implemented && !((*mod)->latest_revision & LYS_MOD_IMPORTED_REV)) {
aPiecek4725aea2021-07-28 10:18:33 +0200983 /* Let us now search with callback and searchpaths to check
984 * if there is newer revision outside the context.
985 */
986 mod_latest = *mod;
987 *mod = NULL;
988 }
989 }
990
Michal Vasko0550b762020-11-24 18:04:08 +0100991 if (!*mod) {
aPiecek4725aea2021-07-28 10:18:33 +0200992 /* No suitable module in the context, try to load it. */
Michal Vaskobdac23f2022-01-12 14:02:35 +0100993 LY_CHECK_RET(lys_parse_load_from_clb_or_file(ctx, name, revision, mod_latest, new_mods, mod));
aPiecek4725aea2021-07-28 10:18:33 +0200994 if (!*mod && !mod_latest) {
Michal Vasko4e205e82021-06-08 14:01:47 +0200995 LOGVAL(ctx, LYVE_REFERENCE, "Loading \"%s\" module failed.", name);
Michal Vasko0550b762020-11-24 18:04:08 +0100996 return LY_EVALID;
997 }
aPiecek4725aea2021-07-28 10:18:33 +0200998
999 /* Update the latest_revision flag - here we have selected the latest available schema,
1000 * consider that even the callback provides correct latest revision.
1001 */
1002 if (!*mod) {
1003 LOGVRB("Newer revision than \"%s@%s\" not found, using this as the latest revision.",
1004 mod_latest->name, mod_latest->revision);
1005 assert(mod_latest->latest_revision & LYS_MOD_LATEST_REV);
1006 mod_latest->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS;
1007 *mod = mod_latest;
1008 } else if (*mod && !revision && ((*mod)->latest_revision & LYS_MOD_LATEST_REV)) {
1009 (*mod)->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS;
1010 }
Radek Krejci086c7132018-10-26 15:29:04 +02001011 }
Radek Krejci086c7132018-10-26 15:29:04 +02001012
aPiecek9f8c7e72021-07-28 12:01:56 +02001013 /* Checking the circular dependence of imported modules. */
1014 LY_CHECK_RET(lys_check_circular_dependency(ctx, mod));
1015
Radek Krejci086c7132018-10-26 15:29:04 +02001016 return LY_SUCCESS;
1017}
1018
1019LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001020lysp_check_stringchar(struct lysp_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +02001021{
1022 if (!is_yangutf8char(c)) {
1023 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
1024 return LY_EVALID;
1025 }
1026 return LY_SUCCESS;
1027}
1028
1029LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001030lysp_check_identifierchar(struct lysp_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +02001031{
Michal Vasko69730152020-10-09 16:30:07 +02001032 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +02001033 if (!is_yangidentstartchar(c)) {
aPiecekc89b2242021-05-14 14:19:11 +02001034 if ((c < UCHAR_MAX) && isprint(c)) {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001035 if (ctx) {
1036 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
1037 }
Michal Vasko7c769042021-03-25 12:20:49 +01001038 } else {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001039 if (ctx) {
1040 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character 0x%04x.", c);
1041 }
Michal Vasko7c769042021-03-25 12:20:49 +01001042 }
David Sedlák4a650532019-07-10 11:55:18 +02001043 return LY_EVALID;
1044 }
1045 if (prefix) {
1046 if (first) {
1047 (*prefix) = 0;
1048 } else {
1049 (*prefix) = 2;
1050 }
1051 }
Michal Vasko69730152020-10-09 16:30:07 +02001052 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +02001053 (*prefix) = 1;
1054 } else if (!is_yangidentchar(c)) {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001055 if (ctx) {
1056 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
1057 }
David Sedlák4a650532019-07-10 11:55:18 +02001058 return LY_EVALID;
1059 }
1060
1061 return LY_SUCCESS;
1062}
1063
Radek Krejci771928a2021-01-19 13:42:36 +01001064/**
1065 * @brief Try to find the parsed submodule in main module for the given include record.
1066 *
1067 * @param[in] pctx main parser context
1068 * @param[in] inc The include record with missing parsed submodule. According to include info try to find
1069 * the corresponding parsed submodule in main module's includes.
1070 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
1071 * @return LY_ENOT - the parsed module was not found.
1072 * @return LY_EVALID - YANG rule violation
1073 */
1074static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001075lysp_main_pmod_get_submodule(struct lysp_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +02001076{
Radek Krejci771928a2021-01-19 13:42:36 +01001077 LY_ARRAY_COUNT_TYPE i;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001078 struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed;
Michal Vasko69730152020-10-09 16:30:07 +02001079
Radek Krejci771928a2021-01-19 13:42:36 +01001080 LY_ARRAY_FOR(main_pmod->includes, i) {
1081 if (strcmp(main_pmod->includes[i].name, inc->name)) {
1082 continue;
1083 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001084
Radek Krejci771928a2021-01-19 13:42:36 +01001085 if (inc->rev[0] && strncmp(inc->rev, main_pmod->includes[i].rev, LY_REV_SIZE)) {
1086 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1087 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001088 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev,
Radek Krejci771928a2021-01-19 13:42:36 +01001089 main_pmod->includes[i].name, main_pmod->includes[i].rev, main_pmod->mod->name);
1090 return LY_EVALID;
1091 }
1092
1093 inc->submodule = main_pmod->includes[i].submodule;
1094 return inc->submodule ? LY_SUCCESS : LY_ENOT;
1095 }
1096
1097 if (main_pmod->version == LYS_VERSION_1_1) {
1098 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1099 "YANG 1.1 requires all submodules to be included from main module. "
1100 "But submodule \"%s\" includes submodule \"%s\" which is not included by main module \"%s\".",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001101 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->name, main_pmod->mod->name);
Radek Krejci771928a2021-01-19 13:42:36 +01001102 return LY_EVALID;
1103 } else {
1104 return LY_ENOT;
1105 }
1106}
1107
1108/**
Michal Vasko8a67eff2021-12-07 14:04:47 +01001109 * @brief Try to find the parsed submodule in currenlty parsed modules for the given include record.
1110 *
1111 * @param[in] pctx main parser context
1112 * @param[in] inc The include record with missing parsed submodule.
1113 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
1114 * @return LY_ENOT - the parsed module was not found.
1115 * @return LY_EVALID - YANG rule violation
1116 */
1117static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001118lysp_parsed_mods_get_submodule(struct lysp_ctx *pctx, struct lysp_include *inc)
Michal Vasko8a67eff2021-12-07 14:04:47 +01001119{
1120 uint32_t i;
1121 struct lysp_submodule *submod;
1122
1123 for (i = 0; i < pctx->parsed_mods->count - 1; ++i) {
1124 submod = pctx->parsed_mods->objs[i];
1125 if (!submod->is_submod) {
1126 continue;
1127 }
1128
1129 if (strcmp(submod->name, inc->name)) {
1130 continue;
1131 }
1132
1133 if (inc->rev[0] && submod->revs && strncmp(inc->rev, submod->revs[0].date, LY_REV_SIZE)) {
1134 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1135 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
1136 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev,
1137 submod->name, submod->revs[0].date, PARSER_CUR_PMOD(pctx)->mod->name);
1138 return LY_EVALID;
1139 }
1140
1141 inc->submodule = submod;
1142 return LY_SUCCESS;
1143 }
1144
1145 return LY_ENOT;
1146}
1147
1148/**
Radek Krejci771928a2021-01-19 13:42:36 +01001149 * @brief Make the copy of the given include record into the main module.
1150 *
1151 * YANG 1.0 does not require the main module to include all the submodules. Therefore, parsing submodules can cause
1152 * reallocating and extending the includes array in the main module by the submodules included only in submodules.
1153 *
1154 * @param[in] pctx main parser context
1155 * @param[in] inc Include record to copy into main module taken from @p pctx.
1156 * @return LY_ERR value.
1157 */
1158static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001159lysp_inject_submodule(struct lysp_ctx *pctx, struct lysp_include *inc)
Radek Krejci771928a2021-01-19 13:42:36 +01001160{
1161 LY_ARRAY_COUNT_TYPE i;
1162 struct lysp_include *inc_new, *inc_tofill = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001163 struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed;
Radek Krejci771928a2021-01-19 13:42:36 +01001164
1165 /* first, try to find the corresponding record with missing parsed submodule */
1166 LY_ARRAY_FOR(main_pmod->includes, i) {
1167 if (strcmp(main_pmod->includes[i].name, inc->name)) {
1168 continue;
1169 }
1170 inc_tofill = &main_pmod->includes[i];
1171 break;
1172 }
1173
1174 if (inc_tofill) {
1175 inc_tofill->submodule = inc->submodule;
1176 } else {
1177 LY_ARRAY_NEW_RET(PARSER_CTX(pctx), main_pmod->includes, inc_new, LY_EMEM);
1178
1179 inc_new->submodule = inc->submodule;
1180 DUP_STRING_RET(PARSER_CTX(pctx), inc->name, inc_new->name);
1181 DUP_STRING_RET(PARSER_CTX(pctx), inc->dsc, inc_new->dsc);
1182 DUP_STRING_RET(PARSER_CTX(pctx), inc->ref, inc_new->ref);
1183 /* TODO duplicate extensions */
1184 memcpy(inc_new->rev, inc->rev, LY_REV_SIZE);
1185 inc_new->injected = 1;
1186 }
1187 return LY_SUCCESS;
1188}
1189
1190LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001191lysp_load_submodules(struct lysp_ctx *pctx, struct lysp_module *pmod, struct ly_set *new_mods)
Radek Krejci771928a2021-01-19 13:42:36 +01001192{
1193 LY_ARRAY_COUNT_TYPE u;
1194 struct ly_ctx *ctx = PARSER_CTX(pctx);
1195
1196 LY_ARRAY_FOR(pmod->includes, u) {
Michal Vasko8a67eff2021-12-07 14:04:47 +01001197 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci771928a2021-01-19 13:42:36 +01001198 struct lysp_submodule *submod = NULL;
1199 struct lysp_include *inc = &pmod->includes[u];
1200
1201 if (inc->submodule) {
1202 continue;
1203 }
1204
1205 if (pmod->is_submod) {
1206 /* try to find the submodule in the main module or its submodules */
Michal Vasko8a67eff2021-12-07 14:04:47 +01001207 ret = lysp_main_pmod_get_submodule(pctx, inc);
1208 LY_CHECK_RET(ret != LY_ENOT, ret);
Radek Krejci771928a2021-01-19 13:42:36 +01001209 }
1210
Michal Vasko8a67eff2021-12-07 14:04:47 +01001211 /* try to use currently parsed submodule */
1212 r = lysp_parsed_mods_get_submodule(pctx, inc);
1213 LY_CHECK_RET(r != LY_ENOT, r);
1214
Radek Krejci771928a2021-01-19 13:42:36 +01001215 /* submodule not present in the main module, get the input data and parse it */
1216 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcidf549132021-01-21 10:32:32 +01001217search_clb:
Radek Krejci771928a2021-01-19 13:42:36 +01001218 if (ctx->imp_clb) {
1219 const char *submodule_data = NULL;
1220 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko26bbb272022-08-02 14:54:33 +02001221
Radek Krejci771928a2021-01-19 13:42:36 +01001222 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
1223 struct lysp_load_module_check_data check_data = {0};
1224 struct ly_in *in;
1225
Michal Vasko8a67eff2021-12-07 14:04:47 +01001226 if (ctx->imp_clb(PARSER_CUR_PMOD(pctx)->mod->name, NULL, inc->name,
Radek Krejci771928a2021-01-19 13:42:36 +01001227 inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
1228 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
1229 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
1230 check_data.name = inc->name;
1231 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001232 check_data.submoduleof = PARSER_CUR_PMOD(pctx)->mod->name;
aPiecekc3e26142021-06-22 14:25:49 +02001233 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, new_mods, &submod);
Radek Krejci771928a2021-01-19 13:42:36 +01001234
1235 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1236 * submodule's include into main module, where it is missing */
1237 inc = &pmod->includes[u];
1238
1239 ly_in_free(in, 0);
1240 if (submodule_data_free) {
1241 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
1242 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001243 }
1244 }
Radek Krejci771928a2021-01-19 13:42:36 +01001245 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1246 goto search_file;
1247 }
1248 } else {
Radek Krejcidf549132021-01-21 10:32:32 +01001249search_file:
Radek Krejci771928a2021-01-19 13:42:36 +01001250 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
1251 /* submodule was not received from the callback or there is no callback set */
Michal Vaskodd6d5a32022-07-14 13:54:58 +02001252 lys_parse_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, pctx->main_ctx,
1253 PARSER_CUR_PMOD(pctx->main_ctx)->mod->name, 1, new_mods, (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001254
Radek Krejci771928a2021-01-19 13:42:36 +01001255 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1256 * submodule's include into main module, where it is missing */
1257 inc = &pmod->includes[u];
1258 }
1259 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1260 goto search_clb;
1261 }
1262 }
1263 if (submod) {
1264 if (!inc->rev[0] && (submod->latest_revision == 1)) {
1265 /* update the latest_revision flag - here we have selected the latest available schema,
1266 * consider that even the callback provides correct latest revision */
1267 submod->latest_revision = 2;
1268 }
1269
1270 inc->submodule = submod;
1271 if (ret == LY_ENOT) {
1272 /* the submodule include is not present in YANG 1.0 main module - add it there */
1273 LY_CHECK_RET(lysp_inject_submodule(pctx, &pmod->includes[u]));
1274 }
1275 }
1276 if (!inc->submodule) {
1277 LOGVAL(ctx, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name,
Michal Vasko8a67eff2021-12-07 14:04:47 +01001278 PARSER_CUR_PMOD(pctx)->is_submod ? ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name :
1279 PARSER_CUR_PMOD(pctx)->mod->name);
Radek Krejci771928a2021-01-19 13:42:36 +01001280 return LY_EVALID;
1281 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001282 }
1283
1284 return LY_SUCCESS;
1285}
1286
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001287LIBYANG_API_DEF const struct lysc_when *
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001288lysc_has_when(const struct lysc_node *node)
1289{
Radek Krejci9a3823e2021-01-27 20:26:46 +01001290 struct lysc_when **when;
1291
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001292 if (!node) {
1293 return NULL;
1294 }
1295
1296 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001297 when = lysc_node_when(node);
1298 if (when) {
1299 return when[0];
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001300 }
1301 node = node->parent;
1302 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1303
1304 return NULL;
1305}
1306
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001307LIBYANG_API_DEF const struct lys_module *
Michal Vaskoef53c812021-10-13 10:21:03 +02001308lysc_owner_module(const struct lysc_node *node)
1309{
1310 if (!node) {
1311 return NULL;
1312 }
1313
1314 for ( ; node->parent; node = node->parent) {}
1315 return node->module;
1316}
1317
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001318LIBYANG_API_DEF const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001319lys_nodetype2str(uint16_t nodetype)
1320{
Michal Vaskod989ba02020-08-24 10:59:24 +02001321 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001322 case LYS_CONTAINER:
1323 return "container";
1324 case LYS_CHOICE:
1325 return "choice";
1326 case LYS_LEAF:
1327 return "leaf";
1328 case LYS_LEAFLIST:
1329 return "leaf-list";
1330 case LYS_LIST:
1331 return "list";
1332 case LYS_ANYXML:
1333 return "anyxml";
1334 case LYS_ANYDATA:
1335 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001336 case LYS_CASE:
1337 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001338 case LYS_RPC:
1339 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001340 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001341 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001342 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001343 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001344 case LYS_USES:
1345 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001346 default:
1347 return "unknown";
1348 }
1349}
1350
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001351LIBYANG_API_DEF enum ly_stmt
Radek Krejci39b7fc22021-02-26 23:29:18 +01001352lys_nodetype2stmt(uint16_t nodetype)
1353{
1354 switch (nodetype) {
1355 case LYS_CONTAINER:
1356 return LY_STMT_CONTAINER;
1357 case LYS_CHOICE:
1358 return LY_STMT_CHOICE;
1359 case LYS_LEAF:
1360 return LY_STMT_LEAF;
1361 case LYS_LEAFLIST:
1362 return LY_STMT_LEAF_LIST;
1363 case LYS_LIST:
1364 return LY_STMT_LIST;
1365 case LYS_ANYXML:
1366 return LY_STMT_ANYXML;
1367 case LYS_ANYDATA:
1368 return LY_STMT_ANYDATA;
1369 case LYS_CASE:
1370 return LY_STMT_CASE;
1371 case LYS_RPC:
1372 return LY_STMT_RPC;
1373 case LYS_ACTION:
1374 return LY_STMT_ACTION;
1375 case LYS_NOTIF:
1376 return LY_STMT_NOTIFICATION;
1377 case LYS_USES:
1378 return LY_STMT_USES;
1379 case LYS_INPUT:
1380 return LY_STMT_INPUT;
1381 case LYS_OUTPUT:
1382 return LY_STMT_OUTPUT;
1383 default:
1384 return LY_STMT_NONE;
1385 }
1386}
1387
Radek Krejci693262f2019-04-29 15:23:20 +02001388const char *
1389lys_datatype2str(LY_DATA_TYPE basetype)
1390{
Michal Vaskod989ba02020-08-24 10:59:24 +02001391 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001392 case LY_TYPE_BINARY:
1393 return "binary";
1394 case LY_TYPE_UINT8:
1395 return "uint8";
1396 case LY_TYPE_UINT16:
1397 return "uint16";
1398 case LY_TYPE_UINT32:
1399 return "uint32";
1400 case LY_TYPE_UINT64:
1401 return "uint64";
1402 case LY_TYPE_STRING:
1403 return "string";
1404 case LY_TYPE_BITS:
1405 return "bits";
1406 case LY_TYPE_BOOL:
1407 return "boolean";
1408 case LY_TYPE_DEC64:
1409 return "decimal64";
1410 case LY_TYPE_EMPTY:
1411 return "empty";
1412 case LY_TYPE_ENUM:
1413 return "enumeration";
1414 case LY_TYPE_IDENT:
1415 return "identityref";
1416 case LY_TYPE_INST:
1417 return "instance-identifier";
1418 case LY_TYPE_LEAFREF:
1419 return "leafref";
1420 case LY_TYPE_UNION:
1421 return "union";
1422 case LY_TYPE_INT8:
1423 return "int8";
1424 case LY_TYPE_INT16:
1425 return "int16";
1426 case LY_TYPE_INT32:
1427 return "int32";
1428 case LY_TYPE_INT64:
1429 return "int64";
1430 default:
1431 return "unknown";
1432 }
1433}
1434
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001435LIBYANG_API_DEF const struct lysp_tpdf *
Radek Krejci056d0a82018-12-06 16:57:25 +01001436lysp_node_typedefs(const struct lysp_node *node)
1437{
Radek Krejci0fb28562018-12-13 15:17:37 +01001438 switch (node->nodetype) {
1439 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001440 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001441 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001442 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001443 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001444 return ((struct lysp_node_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001445 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001446 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001447 return ((struct lysp_node_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001448 case LYS_INPUT:
1449 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001450 return ((struct lysp_node_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001451 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001452 return ((struct lysp_node_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001453 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001454 return NULL;
1455 }
1456}
1457
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001458LIBYANG_API_DEF const struct lysp_node_grp *
Radek Krejci53ea6152018-12-13 15:21:15 +01001459lysp_node_groupings(const struct lysp_node *node)
1460{
1461 switch (node->nodetype) {
1462 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001463 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001464 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001465 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001466 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001467 return ((struct lysp_node_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001468 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001469 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001470 return ((struct lysp_node_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001471 case LYS_INPUT:
1472 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001473 return ((struct lysp_node_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001474 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001475 return ((struct lysp_node_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001476 default:
1477 return NULL;
1478 }
1479}
1480
Radek Krejci2a9fc652021-01-22 17:44:34 +01001481struct lysp_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001482lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001483{
1484 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001485
Radek Krejcibbe09a92018-11-08 09:36:54 +01001486 switch (node->nodetype) {
1487 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001488 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001489 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001490 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001491 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001492 return &((struct lysp_node_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001493 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001494 return &((struct lysp_node_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001495 default:
1496 return NULL;
1497 }
1498}
1499
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001500LIBYANG_API_DEF const struct lysp_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001501lysp_node_actions(const struct lysp_node *node)
1502{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001503 struct lysp_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001504
Michal Vasko22df3f02020-08-24 13:29:22 +02001505 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001506 if (actions) {
1507 return *actions;
1508 } else {
1509 return NULL;
1510 }
1511}
1512
Radek Krejci2a9fc652021-01-22 17:44:34 +01001513struct lysp_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001514lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001515{
1516 assert(node);
1517 switch (node->nodetype) {
1518 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001519 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001520 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001521 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001522 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001523 return &((struct lysp_node_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001524 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001525 return &((struct lysp_node_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001526 default:
1527 return NULL;
1528 }
1529}
1530
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001531LIBYANG_API_DEF const struct lysp_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001532lysp_node_notifs(const struct lysp_node *node)
1533{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001534 struct lysp_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001535
Michal Vasko22df3f02020-08-24 13:29:22 +02001536 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001537 if (notifs) {
1538 return *notifs;
1539 } else {
1540 return NULL;
1541 }
1542}
1543
Radek Krejcibbe09a92018-11-08 09:36:54 +01001544struct lysp_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001545lysp_node_child_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001546{
1547 assert(node);
1548 switch (node->nodetype) {
1549 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001550 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001551 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001552 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001553 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001554 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001555 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001556 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001557 case LYS_GROUPING:
Radek Krejci01180ac2021-01-27 08:48:22 +01001558 return &((struct lysp_node_grp *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001559 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001560 return &((struct lysp_node_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001561 case LYS_INPUT:
1562 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001563 return &((struct lysp_node_action_inout *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001564 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001565 return &((struct lysp_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001566 default:
1567 return NULL;
1568 }
1569}
1570
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001571LIBYANG_API_DEF const struct lysp_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001572lysp_node_child(const struct lysp_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01001573{
Michal Vasko544e58a2021-01-28 14:33:41 +01001574 struct lysp_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001575
1576 if (!node) {
1577 return NULL;
1578 }
1579
Michal Vasko544e58a2021-01-28 14:33:41 +01001580 child = lysp_node_child_p((struct lysp_node *)node);
1581 if (child) {
1582 return *child;
Radek Krejci056d0a82018-12-06 16:57:25 +01001583 } else {
1584 return NULL;
1585 }
1586}
1587
Radek Krejci9a3823e2021-01-27 20:26:46 +01001588struct lysp_restr **
1589lysp_node_musts_p(const struct lysp_node *node)
1590{
1591 if (!node) {
1592 return NULL;
1593 }
1594
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001595 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001596 case LYS_CONTAINER:
1597 return &((struct lysp_node_container *)node)->musts;
1598 case LYS_LEAF:
1599 return &((struct lysp_node_leaf *)node)->musts;
1600 case LYS_LEAFLIST:
1601 return &((struct lysp_node_leaflist *)node)->musts;
1602 case LYS_LIST:
1603 return &((struct lysp_node_list *)node)->musts;
1604 case LYS_ANYXML:
1605 case LYS_ANYDATA:
1606 return &((struct lysp_node_anydata *)node)->musts;
1607 case LYS_NOTIF:
1608 return &((struct lysp_node_notif *)node)->musts;
1609 case LYS_INPUT:
1610 case LYS_OUTPUT:
1611 return &((struct lysp_node_action_inout *)node)->musts;
1612 default:
1613 return NULL;
1614 }
1615}
1616
1617struct lysp_restr *
1618lysp_node_musts(const struct lysp_node *node)
1619{
1620 struct lysp_restr **musts;
1621
1622 musts = lysp_node_musts_p(node);
1623 if (musts) {
1624 return *musts;
1625 } else {
1626 return NULL;
1627 }
1628}
1629
1630struct lysp_when **
1631lysp_node_when_p(const struct lysp_node *node)
1632{
1633 if (!node) {
1634 return NULL;
1635 }
1636
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001637 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001638 case LYS_CONTAINER:
1639 return &((struct lysp_node_container *)node)->when;
1640 case LYS_CHOICE:
1641 return &((struct lysp_node_choice *)node)->when;
1642 case LYS_LEAF:
1643 return &((struct lysp_node_leaf *)node)->when;
1644 case LYS_LEAFLIST:
1645 return &((struct lysp_node_leaflist *)node)->when;
1646 case LYS_LIST:
1647 return &((struct lysp_node_list *)node)->when;
1648 case LYS_ANYXML:
1649 case LYS_ANYDATA:
1650 return &((struct lysp_node_anydata *)node)->when;
1651 case LYS_CASE:
1652 return &((struct lysp_node_case *)node)->when;
1653 case LYS_USES:
1654 return &((struct lysp_node_uses *)node)->when;
1655 case LYS_AUGMENT:
1656 return &((struct lysp_node_augment *)node)->when;
1657 default:
1658 return NULL;
1659 }
1660}
1661
1662struct lysp_when *
1663lysp_node_when(const struct lysp_node *node)
1664{
1665 struct lysp_when **when;
1666
1667 when = lysp_node_when_p(node);
1668 if (when) {
1669 return *when;
1670 } else {
1671 return NULL;
1672 }
1673}
1674
Radek Krejci2a9fc652021-01-22 17:44:34 +01001675struct lysc_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001676lysc_node_actions_p(struct lysc_node *node)
1677{
1678 assert(node);
1679 switch (node->nodetype) {
1680 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001681 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001682 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001683 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001684 default:
1685 return NULL;
1686 }
1687}
1688
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001689LIBYANG_API_DEF const struct lysc_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001690lysc_node_actions(const struct lysc_node *node)
1691{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001692 struct lysc_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001693
Michal Vasko22df3f02020-08-24 13:29:22 +02001694 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001695 if (actions) {
1696 return *actions;
1697 } else {
1698 return NULL;
1699 }
1700}
1701
Radek Krejci2a9fc652021-01-22 17:44:34 +01001702struct lysc_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001703lysc_node_notifs_p(struct lysc_node *node)
1704{
1705 assert(node);
1706 switch (node->nodetype) {
1707 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001708 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001709 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001710 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001711 default:
1712 return NULL;
1713 }
1714}
1715
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001716LIBYANG_API_DEF const struct lysc_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001717lysc_node_notifs(const struct lysc_node *node)
1718{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001719 struct lysc_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001720
Michal Vasko22df3f02020-08-24 13:29:22 +02001721 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001722 if (notifs) {
1723 return *notifs;
1724 } else {
1725 return NULL;
1726 }
1727}
1728
Radek Krejcibbe09a92018-11-08 09:36:54 +01001729struct lysc_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001730lysc_node_child_p(const struct lysc_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001731{
Michal Vasko544e58a2021-01-28 14:33:41 +01001732 assert(node && !(node->nodetype & (LYS_RPC | LYS_ACTION)));
1733
Radek Krejcibbe09a92018-11-08 09:36:54 +01001734 switch (node->nodetype) {
1735 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001736 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001737 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001738 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001739 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001740 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001741 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001742 return &((struct lysc_node_list *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001743 case LYS_INPUT:
1744 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001745 return &((struct lysc_node_action_inout *)node)->child;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001746 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001747 return &((struct lysc_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001748 default:
1749 return NULL;
1750 }
1751}
1752
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001753LIBYANG_API_DEF const struct lysc_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001754lysc_node_child(const struct lysc_node *node)
Radek Krejcia3045382018-11-22 14:30:31 +01001755{
Michal Vasko544e58a2021-01-28 14:33:41 +01001756 struct lysc_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001757
1758 if (!node) {
1759 return NULL;
1760 }
1761
Michal Vasko544e58a2021-01-28 14:33:41 +01001762 if (node->nodetype & (LYS_RPC | LYS_ACTION)) {
1763 return &((struct lysc_node_action *)node)->input.node;
Radek Krejcibe154442021-01-21 11:06:36 +01001764 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01001765 child = lysc_node_child_p(node);
1766 if (child) {
1767 return *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001768 }
Michal Vasko2a668712020-10-21 11:48:09 +02001769 }
Michal Vasko544e58a2021-01-28 14:33:41 +01001770
1771 return NULL;
Michal Vasko2a668712020-10-21 11:48:09 +02001772}
1773
Radek Krejci9a3823e2021-01-27 20:26:46 +01001774struct lysc_must **
1775lysc_node_musts_p(const struct lysc_node *node)
1776{
1777 if (!node) {
1778 return NULL;
1779 }
1780
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001781 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001782 case LYS_CONTAINER:
1783 return &((struct lysc_node_container *)node)->musts;
1784 case LYS_LEAF:
1785 return &((struct lysc_node_leaf *)node)->musts;
1786 case LYS_LEAFLIST:
1787 return &((struct lysc_node_leaflist *)node)->musts;
1788 case LYS_LIST:
1789 return &((struct lysc_node_list *)node)->musts;
1790 case LYS_ANYXML:
1791 case LYS_ANYDATA:
1792 return &((struct lysc_node_anydata *)node)->musts;
1793 case LYS_NOTIF:
1794 return &((struct lysc_node_notif *)node)->musts;
1795 case LYS_INPUT:
1796 case LYS_OUTPUT:
1797 return &((struct lysc_node_action_inout *)node)->musts;
1798 default:
1799 return NULL;
1800 }
1801}
1802
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001803LIBYANG_API_DEF struct lysc_must *
Radek Krejci9a3823e2021-01-27 20:26:46 +01001804lysc_node_musts(const struct lysc_node *node)
1805{
1806 struct lysc_must **must_p;
1807
1808 must_p = lysc_node_musts_p(node);
1809 if (must_p) {
1810 return *must_p;
1811 } else {
1812 return NULL;
1813 }
1814}
1815
1816struct lysc_when ***
1817lysc_node_when_p(const struct lysc_node *node)
1818{
1819 if (!node) {
1820 return NULL;
1821 }
1822
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001823 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001824 case LYS_CONTAINER:
1825 return &((struct lysc_node_container *)node)->when;
1826 case LYS_CHOICE:
1827 return &((struct lysc_node_choice *)node)->when;
1828 case LYS_LEAF:
1829 return &((struct lysc_node_leaf *)node)->when;
1830 case LYS_LEAFLIST:
1831 return &((struct lysc_node_leaflist *)node)->when;
1832 case LYS_LIST:
1833 return &((struct lysc_node_list *)node)->when;
1834 case LYS_ANYXML:
1835 case LYS_ANYDATA:
1836 return &((struct lysc_node_anydata *)node)->when;
1837 case LYS_CASE:
1838 return &((struct lysc_node_case *)node)->when;
1839 case LYS_NOTIF:
1840 return &((struct lysc_node_notif *)node)->when;
1841 case LYS_RPC:
1842 case LYS_ACTION:
1843 return &((struct lysc_node_action *)node)->when;
1844 default:
1845 return NULL;
1846 }
1847}
1848
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001849LIBYANG_API_DEF struct lysc_when **
Radek Krejci9a3823e2021-01-27 20:26:46 +01001850lysc_node_when(const struct lysc_node *node)
1851{
1852 struct lysc_when ***when_p;
1853
1854 when_p = lysc_node_when_p(node);
1855 if (when_p) {
1856 return *when_p;
1857 } else {
1858 return NULL;
1859 }
1860}
1861
Radek Krejcid6b76452019-09-03 17:03:03 +02001862enum ly_stmt
Radek Krejcid54412f2020-12-17 20:25:35 +01001863lysp_match_kw(struct ly_in *in, uint64_t *indent)
David Sedlákc10e7902018-12-17 02:17:59 +01001864{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001865/**
Radek Krejcid54412f2020-12-17 20:25:35 +01001866 * @brief Move the input by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001867 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001868 *
1869 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001870 */
Radek Krejcid54412f2020-12-17 20:25:35 +01001871#define MOVE_IN(COUNT) \
1872 ly_in_skip(in, COUNT); \
1873 if (indent) { \
1874 (*indent)+=COUNT; \
1875 }
1876#define IF_KW(STR, LEN, STMT) \
1877 if (!strncmp(in->current, STR, LEN)) { \
1878 MOVE_IN(LEN); \
1879 (*kw)=STMT; \
1880 }
1881#define IF_KW_PREFIX(STR, LEN) \
1882 if (!strncmp(in->current, STR, LEN)) { \
1883 MOVE_IN(LEN);
1884#define IF_KW_PREFIX_END \
1885 }
David Sedlák572e7ab2019-06-04 16:01:58 +02001886
Michal Vasko63f3d842020-07-08 10:10:14 +02001887 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001888 enum ly_stmt result = LY_STMT_NONE;
1889 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001890 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001891 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001892 case 'a':
Radek Krejcid54412f2020-12-17 20:25:35 +01001893 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001894 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1895 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1896 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001897 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001898 IF_KW("data", 4, LY_STMT_ANYDATA)
1899 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001900 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001901 break;
1902 case 'b':
Radek Krejcid54412f2020-12-17 20:25:35 +01001903 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001904 IF_KW("ase", 3, LY_STMT_BASE)
1905 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1906 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001907 break;
1908 case 'c':
Radek Krejcid54412f2020-12-17 20:25:35 +01001909 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001910 IF_KW("ase", 3, LY_STMT_CASE)
1911 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001912 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001913 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001914 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001915 IF_KW("ct", 2, LY_STMT_CONTACT)
1916 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001917 IF_KW_PREFIX_END
1918 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001919 break;
1920 case 'd':
Radek Krejcid54412f2020-12-17 20:25:35 +01001921 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001922 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001923 IF_KW("fault", 5, LY_STMT_DEFAULT)
1924 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001925 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001926 IF_KW("e", 1, LY_STMT_DEVIATE)
1927 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001928 IF_KW_PREFIX_END
1929 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001930 break;
1931 case 'e':
Radek Krejcid54412f2020-12-17 20:25:35 +01001932 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001933 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001934 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001935 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1936 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001937 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001938 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001939 break;
1940 case 'f':
Radek Krejcid54412f2020-12-17 20:25:35 +01001941 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001942 IF_KW("eature", 6, LY_STMT_FEATURE)
1943 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001944 break;
1945 case 'g':
Radek Krejcid54412f2020-12-17 20:25:35 +01001946 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001947 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001948 break;
1949 case 'i':
Radek Krejcid54412f2020-12-17 20:25:35 +01001950 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001951 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1952 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1953 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001954 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001955 IF_KW("clude", 5, LY_STMT_INCLUDE)
1956 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001957 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001958 break;
1959 case 'k':
Radek Krejcid54412f2020-12-17 20:25:35 +01001960 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001961 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001962 break;
1963 case 'l':
Radek Krejcid54412f2020-12-17 20:25:35 +01001964 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001965 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001966 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1967 else IF_KW("af", 2, LY_STMT_LEAF)
1968 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001969 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001970 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001971 break;
1972 case 'm':
Radek Krejcid54412f2020-12-17 20:25:35 +01001973 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001974 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001975 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1976 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001977 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001978 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1979 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001980 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001981 IF_KW("ule", 3, LY_STMT_MODULE)
1982 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001983 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001984 break;
1985 case 'n':
Radek Krejcid54412f2020-12-17 20:25:35 +01001986 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001987 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1988 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001989 break;
1990 case 'o':
Radek Krejcid54412f2020-12-17 20:25:35 +01001991 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001992 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001993 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1994 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001995 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001996 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001997 break;
1998 case 'p':
Radek Krejcid54412f2020-12-17 20:25:35 +01001999 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002000 IF_KW("ath", 3, LY_STMT_PATH)
2001 else IF_KW("attern", 6, LY_STMT_PATTERN)
2002 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002003 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02002004 IF_KW("fix", 3, LY_STMT_PREFIX)
2005 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002006 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02002007 break;
2008 case 'r':
Radek Krejcid54412f2020-12-17 20:25:35 +01002009 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002010 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002011 else IF_KW_PREFIX("e", 1)
2012 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02002013 IF_KW("erence", 6, LY_STMT_REFERENCE)
2014 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002015 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002016 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
2017 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
2018 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002019 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002020 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02002021 break;
2022 case 's':
Radek Krejcid54412f2020-12-17 20:25:35 +01002023 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002024 IF_KW("tatus", 5, LY_STMT_STATUS)
2025 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02002026 break;
2027 case 't':
Radek Krejcid54412f2020-12-17 20:25:35 +01002028 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002029 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
2030 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02002031 break;
2032 case 'u':
Radek Krejcid54412f2020-12-17 20:25:35 +01002033 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02002034 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02002035 IF_KW("que", 3, LY_STMT_UNIQUE)
2036 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002037 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002038 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02002039 break;
2040 case 'v':
Radek Krejcid54412f2020-12-17 20:25:35 +01002041 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002042 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02002043 break;
2044 case 'w':
Radek Krejcid54412f2020-12-17 20:25:35 +01002045 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002046 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02002047 break;
2048 case 'y':
Radek Krejcid54412f2020-12-17 20:25:35 +01002049 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002050 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
2051 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02002052 break;
David Sedlák23a59a62018-10-26 13:08:02 +02002053 default:
Radek Krejcid54412f2020-12-17 20:25:35 +01002054 /* if indent is not NULL we are matching keyword from YANG data */
2055 if (indent) {
Michal Vasko63f3d842020-07-08 10:10:14 +02002056 if (in->current[0] == ';') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002057 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002058 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02002059 } else if (in->current[0] == '{') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002060 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002061 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002062 } else if (in->current[0] == '}') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002063 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002064 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02002065 }
2066 }
David Sedlák23a59a62018-10-26 13:08:02 +02002067 break;
2068 }
2069
Michal Vasko63f3d842020-07-08 10:10:14 +02002070 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02002071 /* the keyword is not terminated */
2072 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002073 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02002074 }
2075
David Sedlák1bccdfa2019-06-17 15:55:27 +02002076#undef IF_KW
2077#undef IF_KW_PREFIX
2078#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01002079#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02002080 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01002081
David Sedlák1bccdfa2019-06-17 15:55:27 +02002082 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02002083}
David Sedlákecf5eb82019-06-03 14:12:44 +02002084
Radek Krejci85ac8312021-03-03 20:21:33 +01002085LY_ERR
2086lysp_ext_find_definition(const struct ly_ctx *ctx, const struct lysp_ext_instance *ext, const struct lys_module **ext_mod,
2087 struct lysp_ext **ext_def)
2088{
Radek Krejci85ac8312021-03-03 20:21:33 +01002089 const char *tmp, *name, *prefix;
2090 size_t pref_len, name_len;
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002091 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci85ac8312021-03-03 20:21:33 +01002092 const struct lys_module *mod = NULL;
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002093 const struct lysp_submodule *submod;
Radek Krejci85ac8312021-03-03 20:21:33 +01002094
Radek Krejcicbb62422021-03-15 09:29:21 +01002095 assert(ext_def);
2096
Radek Krejci85ac8312021-03-03 20:21:33 +01002097 *ext_def = NULL;
2098 if (ext_mod) {
2099 *ext_mod = NULL;
2100 }
2101
Radek Krejci677abea2021-03-05 14:24:53 +01002102 /* parse the prefix, the nodeid was previously already parsed and checked */
Radek Krejci85ac8312021-03-03 20:21:33 +01002103 tmp = ext->name;
Radek Krejci677abea2021-03-05 14:24:53 +01002104 ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len);
Radek Krejci85ac8312021-03-03 20:21:33 +01002105
2106 /* get module where the extension definition should be placed */
2107 mod = ly_resolve_prefix(ctx, prefix, pref_len, ext->format, ext->prefix_data);
2108 if (!mod) {
Radek Krejci422afb12021-03-04 16:38:16 +01002109 LOGVAL(ctx, LYVE_REFERENCE, "Invalid prefix \"%.*s\" used for extension instance identifier.", (int)pref_len, prefix);
Radek Krejci85ac8312021-03-03 20:21:33 +01002110 return LY_EVALID;
2111 } else if (!mod->parsed->extensions) {
2112 LOGVAL(ctx, LYVE_REFERENCE, "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
2113 ext->name, mod->name);
2114 return LY_EVALID;
2115 }
2116
2117 /* find the parsed extension definition there */
2118 LY_ARRAY_FOR(mod->parsed->extensions, v) {
2119 if (!strcmp(name, mod->parsed->extensions[v].name)) {
2120 *ext_def = &mod->parsed->extensions[v];
2121 break;
2122 }
2123 }
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002124 if (!*ext_def) {
2125 LY_ARRAY_FOR(mod->parsed->includes, u) {
2126 submod = mod->parsed->includes[u].submodule;
2127 LY_ARRAY_FOR(submod->extensions, v) {
2128 if (!strcmp(name, submod->extensions[v].name)) {
2129 *ext_def = &submod->extensions[v];
2130 break;
2131 }
2132 }
2133 }
2134 }
Radek Krejci85ac8312021-03-03 20:21:33 +01002135
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002136 if (!*ext_def) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002137 LOGVAL(ctx, LYVE_REFERENCE, "Extension definition of extension instance \"%s\" not found.", ext->name);
2138 return LY_EVALID;
2139 }
2140
2141 if (ext_mod) {
2142 *ext_mod = mod;
2143 }
2144 return LY_SUCCESS;
2145}
2146
2147LY_ERR
2148lysp_ext_instance_resolve_argument(struct ly_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysp_ext *ext_def)
2149{
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002150 if (!ext_def->argname || ext_p->argument) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002151 /* nothing to do */
2152 return LY_SUCCESS;
2153 }
2154
Radek Krejci8df109d2021-04-23 12:19:08 +02002155 if (ext_p->format == LY_VALUE_XML) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002156 /* Schema was parsed from YIN and an argument is expected, ... */
2157 struct lysp_stmt *stmt = NULL;
2158
2159 if (ext_def->flags & LYS_YINELEM_TRUE) {
2160 /* ... argument was the first XML child element */
2161 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {}
2162 if (stmt) {
2163 const char *arg, *ext, *name_arg, *name_ext, *prefix_arg, *prefix_ext;
2164 size_t name_arg_len, name_ext_len, prefix_arg_len, prefix_ext_len;
2165
2166 stmt = ext_p->child;
2167
2168 arg = stmt->stmt;
2169 ly_parse_nodeid(&arg, &prefix_arg, &prefix_arg_len, &name_arg, &name_arg_len);
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002170 if (ly_strncmp(ext_def->argname, name_arg, name_arg_len)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002171 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" expects argument element \"%s\" as its first XML child, "
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002172 "but \"%.*s\" element found.", ext_p->name, ext_def->argname, (int)name_arg_len, name_arg);
Radek Krejci85ac8312021-03-03 20:21:33 +01002173 return LY_EVALID;
2174 }
2175
2176 /* check namespace - all the extension instances must be qualified and argument element is expected in the same
2177 * namespace. Do not check just prefixes, there can be different prefixes pointing to the same namespace */
2178 ext = ext_p->name; /* include prefix */
2179 ly_parse_nodeid(&ext, &prefix_ext, &prefix_ext_len, &name_ext, &name_ext_len);
2180
2181 if (ly_resolve_prefix(ctx, prefix_ext, prefix_ext_len, ext_p->format, ext_p->prefix_data) !=
2182 ly_resolve_prefix(ctx, prefix_arg, prefix_arg_len, stmt->format, stmt->prefix_data)) {
2183 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" element and its argument element \"%s\" are "
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002184 "expected in the same namespace, but they differ.", ext_p->name, ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002185 return LY_EVALID;
2186 }
2187 }
2188 } else {
2189 /* ... argument was one of the XML attributes which are represented as child stmt
2190 * with LYS_YIN_ATTR flag */
2191 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002192 if (!strcmp(stmt->stmt, ext_def->argname)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002193 /* this is the extension's argument */
2194 break;
2195 }
2196 }
2197 }
2198
2199 if (stmt) {
2200 LY_CHECK_RET(lydict_insert(ctx, stmt->arg, 0, &ext_p->argument));
2201 stmt->flags |= LYS_YIN_ARGUMENT;
2202 }
2203 }
2204
2205 if (!ext_p->argument) {
2206 /* missing extension's argument */
2207 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" misses argument %s\"%s\".",
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002208 ext_p->name, (ext_def->flags & LYS_YINELEM_TRUE) ? "element " : "", ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002209 return LY_EVALID;
2210 }
2211
2212 return LY_SUCCESS;
2213}
2214
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002215LY_ARRAY_COUNT_TYPE
Radek Krejcifc596f92021-02-26 22:40:26 +01002216lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, enum ly_stmt substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002217{
2218 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
2219
Michal Vaskod989ba02020-08-24 10:59:24 +02002220 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejciab430862021-03-02 20:13:40 +01002221 if (ext[index].parent_stmt == substmt) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02002222 return index;
2223 }
2224 }
2225
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002226 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002227}
2228
Michal Vaskof8ded142022-02-17 10:48:01 +01002229LIBYANG_API_DEF const struct lysc_node *
Michal Vasko72244882021-01-12 15:21:05 +01002230lysc_data_node(const struct lysc_node *schema)
Michal Vasko62ed12d2020-05-21 10:08:25 +02002231{
2232 const struct lysc_node *parent;
2233
Michal Vasko72244882021-01-12 15:21:05 +01002234 parent = schema;
Radek Krejcidf549132021-01-21 10:32:32 +01002235 while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC |
2236 LYS_ACTION | LYS_NOTIF))) {
Radek Krejci7d95fbb2021-01-26 17:33:13 +01002237 parent = parent->parent;
Michal Vasko72244882021-01-12 15:21:05 +01002238 }
Michal Vasko62ed12d2020-05-21 10:08:25 +02002239
2240 return parent;
2241}
Michal Vaskof4258e12021-06-15 12:11:42 +02002242
2243ly_bool
2244lys_has_recompiled(const struct lys_module *mod)
2245{
2246 LY_ARRAY_COUNT_TYPE u;
2247
2248 if (LYSP_HAS_RECOMPILED(mod->parsed)) {
2249 return 1;
2250 }
2251
2252 LY_ARRAY_FOR(mod->parsed->includes, u) {
2253 if (LYSP_HAS_RECOMPILED(mod->parsed->includes[u].submodule)) {
2254 return 1;
2255 }
2256 }
2257
2258 return 0;
2259}
2260
2261ly_bool
2262lys_has_compiled(const struct lys_module *mod)
2263{
2264 LY_ARRAY_COUNT_TYPE u;
2265
2266 if (LYSP_HAS_COMPILED(mod->parsed)) {
2267 return 1;
2268 }
2269
2270 LY_ARRAY_FOR(mod->parsed->includes, u) {
2271 if (LYSP_HAS_COMPILED(mod->parsed->includes[u].submodule)) {
2272 return 1;
2273 }
2274 }
2275
2276 return 0;
2277}
Michal Vasko7ee5be22021-06-16 17:03:34 +02002278
2279ly_bool
Michal Vasko87cfdba2022-02-22 14:13:45 +01002280lys_has_dep_mods(const struct lys_module *mod)
Michal Vasko7ee5be22021-06-16 17:03:34 +02002281{
2282 LY_ARRAY_COUNT_TYPE u;
2283
Michal Vasko87cfdba2022-02-22 14:13:45 +01002284 /* features */
2285 if (mod->parsed->features) {
Michal Vasko7ee5be22021-06-16 17:03:34 +02002286 return 1;
2287 }
2288
Michal Vasko87cfdba2022-02-22 14:13:45 +01002289 /* groupings */
2290 if (mod->parsed->groupings) {
2291 return 1;
2292 }
Michal Vasko7ee5be22021-06-16 17:03:34 +02002293 LY_ARRAY_FOR(mod->parsed->includes, u) {
2294 if (mod->parsed->includes[u].submodule->groupings) {
2295 return 1;
2296 }
2297 }
2298
Michal Vasko87cfdba2022-02-22 14:13:45 +01002299 /* augments (adding nodes with leafrefs) */
2300 if (mod->parsed->augments) {
2301 return 1;
2302 }
2303 LY_ARRAY_FOR(mod->parsed->includes, u) {
2304 if (mod->parsed->includes[u].submodule->augments) {
2305 return 1;
2306 }
2307 }
2308
Michal Vasko7ee5be22021-06-16 17:03:34 +02002309 return 0;
2310}