blob: 7ba1b2318be710004df9d0499917657afeff056e [file] [log] [blame]
Radek Krejci86d106e2018-10-18 09:53:19 +02001/**
Michal Vasko59892dd2022-05-13 11:02:30 +02002 * @file tree_schema_common.c
Radek Krejci86d106e2018-10-18 09:53:19 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko59892dd2022-05-13 11:02:30 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
5 * @brief Parsing and validation common functions for schema trees
Radek Krejci86d106e2018-10-18 09:53:19 +02006 *
Michal Vasko59892dd2022-05-13 11:02:30 +02007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejci86d106e2018-10-18 09:53:19 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
Radek Krejci535ea9f2020-05-29 16:01:05 +020015
16#define _GNU_SOURCE
Radek Krejci86d106e2018-10-18 09:53:19 +020017
Radek Krejcie7b95092019-05-15 11:03:07 +020018#include <assert.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020019#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010020#include <stddef.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <stdint.h>
Radek Krejci9ed7a192018-10-31 16:23:51 +010022#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020024#include <time.h>
25
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020027#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "context.h"
Radek Krejci77114102021-03-10 15:21:57 +010029#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010031#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020032#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010033#include "log.h"
Michal Vasko69730152020-10-09 16:30:07 +020034#include "parser_schema.h"
Michal Vasko962b6cd2020-12-08 10:07:49 +010035#include "schema_compile.h"
Michal Vasko79135ae2020-12-16 10:08:35 +010036#include "schema_features.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "set.h"
38#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010039#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020040#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020041#include "tree_schema_internal.h"
42
Radek Krejci85747952019-06-07 16:43:43 +020043LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +020044lysp_check_prefix(struct lys_parser_ctx *ctx, struct lysp_import *imports, const char *module_prefix, const char **value)
Radek Krejci86d106e2018-10-18 09:53:19 +020045{
46 struct lysp_import *i;
47
Michal Vasko69730152020-10-09 16:30:07 +020048 if (module_prefix && (&module_prefix != value) && !strcmp(module_prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010049 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value);
Radek Krejci86d106e2018-10-18 09:53:19 +020050 return LY_EEXIST;
51 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +010052 LY_ARRAY_FOR(imports, struct lysp_import, i) {
Michal Vasko69730152020-10-09 16:30:07 +020053 if (i->prefix && (&i->prefix != value) && !strcmp(i->prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010054 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +010055 return LY_EEXIST;
Radek Krejci86d106e2018-10-18 09:53:19 +020056 }
57 }
58 return LY_SUCCESS;
59}
60
61LY_ERR
Juraj Vijtiuk74dad9e2021-05-26 12:42:14 +020062lysp_check_date(struct lys_parser_ctx *ctx, const char *date, size_t date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +020063{
Radek Krejci86d106e2018-10-18 09:53:19 +020064 struct tm tm, tm_;
65 char *r;
66
Michal Vaskob36053d2020-03-26 15:49:30 +010067 LY_CHECK_ARG_RET(ctx ? PARSER_CTX(ctx) : NULL, date, LY_EINVAL);
68 LY_CHECK_ERR_RET(date_len != LY_REV_SIZE - 1, LOGARG(ctx ? PARSER_CTX(ctx) : NULL, date_len), LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +020069
Radek Krejcif13b87b2020-12-01 22:02:17 +010070 /* check format: YYYY-MM-DD */
Radek Krejci1deb5be2020-08-26 16:43:36 +020071 for (uint8_t i = 0; i < date_len; i++) {
Michal Vasko69730152020-10-09 16:30:07 +020072 if ((i == 4) || (i == 7)) {
Radek Krejci86d106e2018-10-18 09:53:19 +020073 if (date[i] != '-') {
74 goto error;
75 }
76 } else if (!isdigit(date[i])) {
77 goto error;
78 }
79 }
80
81 /* check content, e.g. 2018-02-31 */
82 memset(&tm, 0, sizeof tm);
83 r = strptime(date, "%Y-%m-%d", &tm);
Michal Vasko69730152020-10-09 16:30:07 +020084 if (!r || (r != &date[LY_REV_SIZE - 1])) {
Radek Krejci86d106e2018-10-18 09:53:19 +020085 goto error;
86 }
87 memcpy(&tm_, &tm, sizeof tm);
88 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
89 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
90 /* checking days is enough, since other errors
91 * have been checked by strptime() */
92 goto error;
93 }
94
95 return LY_SUCCESS;
96
97error:
Radek Krejcid33273d2018-10-25 14:55:52 +020098 if (stmt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010099 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt);
Radek Krejcid33273d2018-10-25 14:55:52 +0200100 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200101 return LY_EINVAL;
102}
103
104void
105lysp_sort_revisions(struct lysp_revision *revs)
106{
Radek Krejci857189e2020-09-01 13:26:36 +0200107 LY_ARRAY_COUNT_TYPE i, r;
Radek Krejci86d106e2018-10-18 09:53:19 +0200108 struct lysp_revision rev;
109
Radek Krejcic7d13e32020-12-09 12:32:24 +0100110 for (i = 1, r = 0; i < LY_ARRAY_COUNT(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200111 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200112 r = i;
113 }
114 }
115
116 if (r) {
117 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200118 memcpy(&rev, &revs[0], sizeof rev);
119 memcpy(&revs[0], &revs[r], sizeof rev);
120 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200121 }
122}
Radek Krejci151a5b72018-10-19 14:21:44 +0200123
Radek Krejcibbe09a92018-11-08 09:36:54 +0100124static const struct lysp_tpdf *
125lysp_type_match(const char *name, struct lysp_node *node)
126{
Radek Krejci0fb28562018-12-13 15:17:37 +0100127 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200128 LY_ARRAY_COUNT_TYPE u;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100129
Radek Krejci0fb28562018-12-13 15:17:37 +0100130 typedefs = lysp_node_typedefs(node);
131 LY_ARRAY_FOR(typedefs, u) {
132 if (!strcmp(name, typedefs[u].name)) {
133 /* match */
134 return &typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100135 }
136 }
137
138 return NULL;
139}
140
aPiecek63e080d2021-06-29 13:53:28 +0200141static const struct lysp_node_grp *
142lysp_grouping_match(const char *name, struct lysp_node *node)
143{
144 const struct lysp_node_grp *groupings, *grp_iter;
145
146 groupings = lysp_node_groupings(node);
147 LY_LIST_FOR(groupings, grp_iter) {
148 if (!strcmp(name, grp_iter->name)) {
149 /* match */
150 return grp_iter;
151 }
152 }
153
154 return NULL;
155}
156
Radek Krejci4f28eda2018-11-12 11:46:16 +0100157static LY_DATA_TYPE
158lysp_type_str2builtin(const char *name, size_t len)
159{
160 if (len >= 4) { /* otherwise it does not match any built-in type */
161 if (name[0] == 'b') {
162 if (name[1] == 'i') {
Michal Vasko69730152020-10-09 16:30:07 +0200163 if ((len == 6) && !strncmp(&name[2], "nary", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100164 return LY_TYPE_BINARY;
Michal Vasko69730152020-10-09 16:30:07 +0200165 } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100166 return LY_TYPE_BITS;
167 }
Michal Vasko69730152020-10-09 16:30:07 +0200168 } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100169 return LY_TYPE_BOOL;
170 }
171 } else if (name[0] == 'd') {
Michal Vasko69730152020-10-09 16:30:07 +0200172 if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100173 return LY_TYPE_DEC64;
174 }
175 } else if (name[0] == 'e') {
Michal Vasko69730152020-10-09 16:30:07 +0200176 if ((len == 5) && !strncmp(&name[1], "mpty", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100177 return LY_TYPE_EMPTY;
Michal Vasko69730152020-10-09 16:30:07 +0200178 } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100179 return LY_TYPE_ENUM;
180 }
181 } else if (name[0] == 'i') {
182 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200183 if ((len == 4) && !strncmp(&name[2], "t8", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100184 return LY_TYPE_INT8;
185 } else if (len == 5) {
186 if (!strncmp(&name[2], "t16", 3)) {
187 return LY_TYPE_INT16;
188 } else if (!strncmp(&name[2], "t32", 3)) {
189 return LY_TYPE_INT32;
190 } else if (!strncmp(&name[2], "t64", 3)) {
191 return LY_TYPE_INT64;
192 }
Michal Vasko69730152020-10-09 16:30:07 +0200193 } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100194 return LY_TYPE_INST;
195 }
Michal Vasko69730152020-10-09 16:30:07 +0200196 } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100197 return LY_TYPE_IDENT;
198 }
199 } else if (name[0] == 'l') {
Michal Vasko69730152020-10-09 16:30:07 +0200200 if ((len == 7) && !strncmp(&name[1], "eafref", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100201 return LY_TYPE_LEAFREF;
202 }
203 } else if (name[0] == 's') {
Michal Vasko69730152020-10-09 16:30:07 +0200204 if ((len == 6) && !strncmp(&name[1], "tring", 5)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100205 return LY_TYPE_STRING;
206 }
207 } else if (name[0] == 'u') {
208 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200209 if ((len == 5) && !strncmp(&name[2], "ion", 3)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100210 return LY_TYPE_UNION;
211 }
Michal Vasko69730152020-10-09 16:30:07 +0200212 } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) {
213 if ((len == 5) && (name[4] == '8')) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100214 return LY_TYPE_UINT8;
215 } else if (len == 6) {
216 if (!strncmp(&name[4], "16", 2)) {
217 return LY_TYPE_UINT16;
218 } else if (!strncmp(&name[4], "32", 2)) {
219 return LY_TYPE_UINT32;
220 } else if (!strncmp(&name[4], "64", 2)) {
221 return LY_TYPE_UINT64;
222 }
223 }
224 }
225 }
226 }
227
228 return LY_TYPE_UNKNOWN;
229}
230
Radek Krejcibbe09a92018-11-08 09:36:54 +0100231LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +0100232lysp_type_find(const char *id, struct lysp_node *start_node, const struct lysp_module *start_module,
233 LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100234{
235 const char *str, *name;
236 struct lysp_tpdf *typedefs;
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100237 const struct lys_module *mod;
Michal Vaskoa99b3572021-02-01 11:54:58 +0100238 const struct lysp_module *local_module;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200239 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100240
241 assert(id);
242 assert(start_module);
243 assert(tpdf);
244 assert(node);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100245
Radek Krejci4f28eda2018-11-12 11:46:16 +0100246 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100247 str = strchr(id, ':');
248 if (str) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200249 mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_VALUE_SCHEMA, (void *)start_module);
Michal Vaskoa99b3572021-02-01 11:54:58 +0100250 local_module = mod ? mod->parsed : NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100251 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100252 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100253 } else {
Michal Vaskoa99b3572021-02-01 11:54:58 +0100254 local_module = start_module;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100255 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100256
257 /* check for built-in types */
258 *type = lysp_type_str2builtin(name, strlen(name));
259 if (*type) {
260 *tpdf = NULL;
261 return LY_SUCCESS;
262 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100263 }
Michal Vaskoa99b3572021-02-01 11:54:58 +0100264 LY_CHECK_RET(!local_module, LY_ENOTFOUND);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100265
Michal Vaskoa99b3572021-02-01 11:54:58 +0100266 if (start_node && (local_module == start_module)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100267 /* search typedefs in parent's nodes */
268 *node = start_node;
269 while (*node) {
270 *tpdf = lysp_type_match(name, *node);
271 if (*tpdf) {
272 /* match */
273 return LY_SUCCESS;
274 }
275 *node = (*node)->parent;
276 }
277 }
278
Michal Vasko915e5442021-06-08 14:59:21 +0200279 /* go to main module if in submodule */
280 local_module = local_module->mod->parsed;
281
Radek Krejcibbe09a92018-11-08 09:36:54 +0100282 /* search in top-level typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100283 if (local_module->typedefs) {
284 LY_ARRAY_FOR(local_module->typedefs, u) {
285 if (!strcmp(name, local_module->typedefs[u].name)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100286 /* match */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100287 *tpdf = &local_module->typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100288 return LY_SUCCESS;
289 }
290 }
291 }
292
Michal Vasko915e5442021-06-08 14:59:21 +0200293 /* search in all submodules' typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100294 LY_ARRAY_FOR(local_module->includes, u) {
295 typedefs = local_module->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100296 LY_ARRAY_FOR(typedefs, v) {
297 if (!strcmp(name, typedefs[v].name)) {
298 /* match */
299 *tpdf = &typedefs[v];
300 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100301 }
302 }
303 }
304
305 return LY_ENOTFOUND;
306}
307
David Sedlák6544c182019-07-12 13:17:33 +0200308LY_ERR
David Sedlák07869a52019-07-12 14:28:19 +0200309lysp_check_enum_name(struct lys_parser_ctx *ctx, const char *name, size_t name_len)
David Sedlák6544c182019-07-12 13:17:33 +0200310{
311 if (!name_len) {
312 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
313 return LY_EVALID;
314 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
315 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
Radek Krejci422afb12021-03-04 16:38:16 +0100316 (int)name_len, name);
David Sedlák6544c182019-07-12 13:17:33 +0200317 return LY_EVALID;
318 } else {
319 for (size_t u = 0; u < name_len; ++u) {
320 if (iscntrl(name[u])) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100321 LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
Radek Krejci422afb12021-03-04 16:38:16 +0100322 (int)name_len, name, u + 1);
David Sedlák6544c182019-07-12 13:17:33 +0200323 break;
324 }
325 }
326 }
327
328 return LY_SUCCESS;
329}
330
Michal Vaskob36053d2020-03-26 15:49:30 +0100331/**
aPiecekdc12b9f2021-06-25 10:55:47 +0200332 * @brief Insert @p name to hash table and if @p name has already
333 * been added, then log an error.
334 *
335 * This function is used to detect duplicate names.
336 *
337 * @param[in,out] ctx Context to log the error.
338 * @param[in,out] ht Hash table with top-level names.
339 * @param[in] name Inserted top-level identifier.
340 * @param[in] statement The name of the statement type from which
341 * @p name originated (eg typedef, feature, ...).
342 * @param[in] err_detail Optional error specification.
343 * @return LY_ERR, but LY_EEXIST is mapped to LY_EVALID.
344 */
345static LY_ERR
346lysp_check_dup_ht_insert(struct lys_parser_ctx *ctx, struct hash_table *ht,
347 const char *name, const char *statement, const char *err_detail)
348{
349 LY_ERR ret;
350 uint32_t hash;
351
352 hash = dict_hash(name, strlen(name));
353 ret = lyht_insert(ht, &name, hash, NULL);
354 if (ret == LY_EEXIST) {
355 if (err_detail) {
356 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT2, name, statement, err_detail);
357 } else {
358 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, name, statement);
359 }
360 ret = LY_EVALID;
361 }
362
363 return ret;
364}
365
366/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100367 * @brief Check name of a new type to avoid name collisions.
368 *
369 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
370 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
371 * @param[in] tpdf Typedef definition to check.
372 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
aPiecekc7594b72021-06-29 09:00:03 +0200373 * typedefs are checked, caller is supposed to free the table.
374 * @return LY_EVALID in case of collision, LY_SUCCESS otherwise.
Radek Krejcibbe09a92018-11-08 09:36:54 +0100375 */
376static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100377lysp_check_dup_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
aPieceke1fbd952021-06-29 08:12:55 +0200378 struct hash_table *tpdfs_global)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100379{
380 struct lysp_node *parent;
381 uint32_t hash;
382 size_t name_len;
383 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200384 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100385 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100386
387 assert(ctx);
388 assert(tpdf);
389
390 name = tpdf->name;
391 name_len = strlen(name);
392
Radek Krejci4f28eda2018-11-12 11:46:16 +0100393 if (lysp_type_str2builtin(name, name_len)) {
aPiecekc7594b72021-06-29 09:00:03 +0200394 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
395 "Duplicate identifier \"%s\" of typedef statement - name collision with a built-in type.", name);
396 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100397 }
398
399 /* check locally scoped typedefs (avoid name shadowing) */
400 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100401 typedefs = lysp_node_typedefs(node);
402 LY_ARRAY_FOR(typedefs, u) {
403 if (&typedefs[u] == tpdf) {
404 break;
405 }
406 if (!strcmp(name, typedefs[u].name)) {
aPiecekc7594b72021-06-29 09:00:03 +0200407 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
408 "Duplicate identifier \"%s\" of typedef statement - name collision with sibling type.", name);
409 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100410 }
411 }
412 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200413 for (parent = node->parent; parent; parent = parent->parent) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100414 if (lysp_type_match(name, parent)) {
aPiecekc7594b72021-06-29 09:00:03 +0200415 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
416 "Duplicate identifier \"%s\" of typedef statement - name collision with another scoped type.", name);
417 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100418 }
419 }
420 }
421
422 /* check collision with the top-level typedefs */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100423 if (node) {
aPiecekc7594b72021-06-29 09:00:03 +0200424 hash = dict_hash(name, name_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100425 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
aPiecekc7594b72021-06-29 09:00:03 +0200426 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
427 "Duplicate identifier \"%s\" of typedef statement - scoped type collide with a top-level type.", name);
428 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100429 }
430 } else {
aPiecekc7594b72021-06-29 09:00:03 +0200431 LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, tpdfs_global, name, "typedef",
432 "name collision with another top-level type"));
Radek Krejci3b1f9292018-11-08 10:58:35 +0100433 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
434 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
435 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100436 }
437
438 return LY_SUCCESS;
439}
440
Radek Krejci857189e2020-09-01 13:26:36 +0200441/**
442 * @brief Compare identifiers.
Michal Vasko62524a92021-02-26 10:08:50 +0100443 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200444 */
445static ly_bool
446lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Radek Krejcibbe09a92018-11-08 09:36:54 +0100447{
Michal Vasko11ac39a2021-07-23 12:46:56 +0200448 char *id1, *id2;
449
450 id1 = *(char **)val1;
451 id2 = *(char **)val2;
452
453 return strcmp(id1, id2) == 0 ? 1 : 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100454}
455
456LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100457lysp_check_dup_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100458{
459 struct hash_table *ids_global;
Radek Krejci0fb28562018-12-13 15:17:37 +0100460 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200461 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200462 uint32_t i;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100463 LY_ERR ret = LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100464
465 /* check name collisions - typedefs and groupings */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100466 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200467 LY_ARRAY_FOR(mod->typedefs, v) {
aPieceke1fbd952021-06-29 08:12:55 +0200468 ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100469 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100470 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200471 LY_ARRAY_FOR(mod->includes, v) {
472 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200473 ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100474 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci3b1f9292018-11-08 10:58:35 +0100475 }
476 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200477 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
478 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
479 LY_ARRAY_FOR(typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200480 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 +0100481 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100482 }
483 }
Michal Vasko405cc9e2020-12-01 12:01:27 +0100484
Radek Krejcibbe09a92018-11-08 09:36:54 +0100485cleanup:
486 lyht_free(ids_global);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100487 return ret;
488}
489
aPiecek63e080d2021-06-29 13:53:28 +0200490/**
491 * @brief Check name of a new grouping to avoid name collisions.
492 *
493 * @param[in] ctx Parser context, module where the grouping is being defined is taken from here.
494 * @param[in] node Schema node where the grouping is being defined, NULL in case of a top-level grouping.
495 * @param[in] grp Grouping definition to check.
496 * @param[in,out] grps_global Initialized hash table to store temporary data between calls. When the module's
497 * groupings are checked, caller is supposed to free the table.
498 * @return LY_EVALID in case of collision, LY_SUCCESS otherwise.
499 */
500static LY_ERR
501lysp_check_dup_grouping(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_node_grp *grp,
502 struct hash_table *grps_global)
503{
504 struct lysp_node *parent;
505 uint32_t hash;
506 size_t name_len;
507 const char *name;
508 const struct lysp_node_grp *groupings, *grp_iter;
509
510 assert(ctx);
511 assert(grp);
512
513 name = grp->name;
514 name_len = strlen(name);
515
516 /* check locally scoped groupings (avoid name shadowing) */
517 if (node) {
518 groupings = lysp_node_groupings(node);
519 LY_LIST_FOR(groupings, grp_iter) {
520 if (grp_iter == grp) {
521 break;
522 }
523 if (!strcmp(name, grp_iter->name)) {
524 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
525 "Duplicate identifier \"%s\" of grouping statement - name collision with sibling grouping.", name);
526 return LY_EVALID;
527 }
528 }
529 /* search grouping in parent's nodes */
530 for (parent = node->parent; parent; parent = parent->parent) {
531 if (lysp_grouping_match(name, parent)) {
532 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
533 "Duplicate identifier \"%s\" of grouping statement - name collision with another scoped grouping.", name);
534 return LY_EVALID;
535 }
536 }
537 }
538
539 /* check collision with the top-level groupings */
540 if (node) {
541 hash = dict_hash(name, name_len);
542 if (!lyht_find(grps_global, &name, hash, NULL)) {
543 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
544 "Duplicate identifier \"%s\" of grouping statement - scoped grouping collide with a top-level grouping.", name);
545 return LY_EVALID;
546 }
547 } else {
548 LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, grps_global, name, "grouping",
549 "name collision with another top-level grouping"));
550 }
551
552 return LY_SUCCESS;
553}
554
555LY_ERR
556lysp_check_dup_groupings(struct lys_parser_ctx *ctx, struct lysp_module *mod)
557{
558 struct hash_table *ids_global;
559 const struct lysp_node_grp *groupings, *grp_iter;
560 LY_ARRAY_COUNT_TYPE u;
561 uint32_t i;
562 LY_ERR ret = LY_SUCCESS;
563
564 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
565 LY_LIST_FOR(mod->groupings, grp_iter) {
566 ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global);
567 LY_CHECK_GOTO(ret, cleanup);
568 }
569 LY_ARRAY_FOR(mod->includes, u) {
570 LY_LIST_FOR(mod->includes[u].submodule->groupings, grp_iter) {
571 ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global);
572 LY_CHECK_GOTO(ret, cleanup);
573 }
574 }
575 for (i = 0; i < ctx->grps_nodes.count; ++i) {
576 groupings = lysp_node_groupings((struct lysp_node *)ctx->grps_nodes.objs[i]);
577 LY_LIST_FOR(groupings, grp_iter) {
578 ret = lysp_check_dup_grouping(ctx, (struct lysp_node *)ctx->grps_nodes.objs[i], grp_iter, ids_global);
579 LY_CHECK_GOTO(ret, cleanup);
580 }
581 }
582
583cleanup:
584 lyht_free(ids_global);
585 return ret;
586}
587
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100588static ly_bool
589ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
590{
591 void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
592
593 return ptr1 == ptr2 ? 1 : 0;
594}
595
596LY_ERR
597lysp_check_dup_features(struct lys_parser_ctx *ctx, struct lysp_module *mod)
598{
599 LY_ARRAY_COUNT_TYPE u;
600 struct hash_table *ht;
601 struct lysp_feature *f;
aPiecekdc12b9f2021-06-25 10:55:47 +0200602 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100603
aPiecekf6203072021-06-25 10:58:26 +0200604 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100605 LY_CHECK_RET(!ht, LY_EMEM);
606
607 /* add all module features into a hash table */
608 LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200609 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
610 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200611 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100612 }
613
614 /* add all submodule features into a hash table */
615 LY_ARRAY_FOR(mod->includes, u) {
616 LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200617 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
618 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200619 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100620 }
621 }
622
623cleanup:
624 lyht_free(ht);
625 return ret;
626}
627
628LY_ERR
629lysp_check_dup_identities(struct lys_parser_ctx *ctx, struct lysp_module *mod)
630{
631 LY_ARRAY_COUNT_TYPE u;
632 struct hash_table *ht;
633 struct lysp_ident *i;
aPiecekdc12b9f2021-06-25 10:55:47 +0200634 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100635
aPiecekf6203072021-06-25 10:58:26 +0200636 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100637 LY_CHECK_RET(!ht, LY_EMEM);
638
639 /* add all module identities into a hash table */
640 LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200641 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
642 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200643 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100644 }
645
646 /* add all submodule identities into a hash table */
647 LY_ARRAY_FOR(mod->includes, u) {
648 LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200649 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
650 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200651 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100652 }
653 }
654
655cleanup:
656 lyht_free(ht);
657 return ret;
658}
659
Radek Krejci9ed7a192018-10-31 16:23:51 +0100660struct lysp_load_module_check_data {
661 const char *name;
662 const char *revision;
663 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200664 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100665};
666
667static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100668lysp_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 +0100669{
670 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100671 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200672 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100673 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100674 struct lysp_revision *revs;
675
676 name = mod ? mod->mod->name : submod->name;
677 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200678 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100679
680 if (info->name) {
681 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100682 if (strcmp(info->name, name)) {
683 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100684 return LY_EINVAL;
685 }
686 }
687 if (info->revision) {
688 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100689 if (!revs || strcmp(info->revision, revs[0].date)) {
690 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200691 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100692 return LY_EINVAL;
693 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200694 } else if (!latest_revision) {
695 /* do not log, we just need to drop the schema and use the latest revision from the context */
696 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100697 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100698 if (submod) {
699 assert(info->submoduleof);
700
Radek Krejci9ed7a192018-10-31 16:23:51 +0100701 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200702 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100703 LOGVAL(ctx, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200704 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100705 return LY_EVALID;
706 }
707 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100708 if (submod->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100709 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100710 return LY_EVALID;
711 }
712 }
713 if (info->path) {
714 /* check that name and revision match filename */
715 filename = strrchr(info->path, '/');
716 if (!filename) {
717 filename = info->path;
718 } else {
719 filename++;
720 }
721 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100722 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100723 rev = strchr(filename, '@');
724 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100725 if (strncmp(filename, name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +0200726 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100727 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100728 }
729 /* revision */
730 if (rev) {
731 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100732 if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100733 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +0200734 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100735 }
736 }
737 }
738 return LY_SUCCESS;
739}
740
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200741/**
Michal Vasko4e205e82021-06-08 14:01:47 +0200742 * @brief Parse a (sub)module from a local file and add into the context.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200743 *
744 * This function does not check the presence of the (sub)module in context, it should be done before calling this function.
745 *
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200746 * @param[in] ctx libyang context where to work.
747 * @param[in] name Name of the (sub)module to load.
748 * @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 +0200749 * @param[in] main_ctx Parser context of the main module in case of loading submodule.
750 * @param[in] main_name Main module name in case of loading submodule.
751 * @param[in] required Module is required so error (even if the input file not found) are important. If 0, there is some
752 * 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 +0200753 * @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 +0200754 * @param[out] result Parsed YANG schema tree of the requested module (struct lys_module*) or submodule (struct lysp_submodule*).
755 * If it is a module, it is already in the context!
Michal Vasko4e205e82021-06-08 14:01:47 +0200756 * @return LY_SUCCESS on success.
Michal Vasko4e205e82021-06-08 14:01:47 +0200757 * @return LY_ERR on error.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200758 */
759static LY_ERR
Michal Vasko4e205e82021-06-08 14:01:47 +0200760lys_parse_localfile(struct ly_ctx *ctx, const char *name, const char *revision, struct lys_parser_ctx *main_ctx,
Michal Vaskodd992582021-06-10 14:34:57 +0200761 const char *main_name, ly_bool required, struct ly_set *new_mods, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100762{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200763 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100764 char *filepath = NULL;
765 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100766 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100767 LY_ERR ret = LY_SUCCESS;
768 struct lysp_load_module_check_data check_data = {0};
769
Michal Vasko87f1cf02021-06-08 14:02:47 +0200770 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name,
771 revision, &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200772 if (!filepath) {
773 if (required) {
774 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200775 revision ? revision : "");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200776 }
777 return LY_ENOTFOUND;
778 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100779
780 LOGVRB("Loading schema from \"%s\" file.", filepath);
781
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200782 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200783 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +0200784 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100785 check_data.name = name;
786 check_data.revision = revision;
787 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800788 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200789 if (main_ctx) {
aPiecekc3e26142021-06-22 14:25:49 +0200790 ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data, new_mods,
Michal Vasko69730152020-10-09 16:30:07 +0200791 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200792 } else {
Michal Vaskodd992582021-06-10 14:34:57 +0200793 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 +0200794
795 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200796 ly_in_free(in, 1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200797 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100798
799 *result = mod;
800
801 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200802
Radek Krejci9ed7a192018-10-31 16:23:51 +0100803cleanup:
804 free(filepath);
805 return ret;
806}
807
aPiecek4725aea2021-07-28 10:18:33 +0200808/**
809 * @brief Load module from searchdirs or from callback.
810 *
811 * @param[in] ctx libyang context where to work.
812 * @param[in] name Name of module to load.
813 * @param[in] revision Revision of module to load.
Michal Vaskobdac23f2022-01-12 14:02:35 +0100814 * @param[in] mod_latest Module with the latest revision found in context, otherwise set to NULL.
815 * @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 +0200816 * @param[out] mod Loaded module.
817 * @return LY_SUCCESS on success.
818 * @return LY_ERR on error.
819 */
820static LY_ERR
821lys_parse_load_from_clb_or_file(struct ly_ctx *ctx, const char *name, const char *revision,
822 struct lys_module *mod_latest, struct ly_set *new_mods, struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200823{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100824 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200825 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200826
Radek Krejci9ed7a192018-10-31 16:23:51 +0100827 void (*module_data_free)(void *module_data, void *user_data) = NULL;
828 struct lysp_load_module_check_data check_data = {0};
Michal Vasko63f3d842020-07-08 10:10:14 +0200829 struct ly_in *in;
Radek Krejci086c7132018-10-26 15:29:04 +0200830
Michal Vaskobdac23f2022-01-12 14:02:35 +0100831 *mod = NULL;
832
833 if (mod_latest && (!ctx->imp_clb || (mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB)) &&
834 ((ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) || (mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) {
835 /* we are not able to find a newer revision */
836 return LY_SUCCESS;
837 }
838
aPiecek4725aea2021-07-28 10:18:33 +0200839 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
840search_clb:
Michal Vaskobdac23f2022-01-12 14:02:35 +0100841 /* check there is a callback and should be called */
842 if (ctx->imp_clb && (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB))) {
843 if (!ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, &format, &module_data, &module_data_free)) {
844 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
845 check_data.name = name;
846 check_data.revision = revision;
847 lys_parse_in(ctx, in, format, lysp_load_module_check, &check_data, new_mods, mod);
848 ly_in_free(in, 0);
849 if (module_data_free) {
850 module_data_free((void *)module_data, ctx->imp_clb_data);
851 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200852 }
Radek Krejci0af46292019-01-11 16:02:31 +0100853 }
Michal Vaskobdac23f2022-01-12 14:02:35 +0100854 if (*mod && !revision) {
855 /* we got the latest revision module from the callback */
856 (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB;
857 } else if (!*mod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
aPiecek4725aea2021-07-28 10:18:33 +0200858 goto search_file;
859 }
860 } else {
861search_file:
Michal Vaskobdac23f2022-01-12 14:02:35 +0100862 /* check we can use searchdirs and that we should */
863 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) &&
864 (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) {
aPiecek4725aea2021-07-28 10:18:33 +0200865 lys_parse_localfile(ctx, name, revision, NULL, NULL, mod_latest ? 0 : 1, new_mods, (void **)mod);
866 }
Michal Vaskobdac23f2022-01-12 14:02:35 +0100867 if (*mod && !revision) {
868 /* we got the latest revision module in the searchdirs */
869 (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB;
870 } else if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
aPiecek4725aea2021-07-28 10:18:33 +0200871 goto search_clb;
872 }
Radek Krejci086c7132018-10-26 15:29:04 +0200873 }
874
aPiecek4725aea2021-07-28 10:18:33 +0200875 return LY_SUCCESS;
876}
877
878/**
aPiecekd4911ee2021-07-30 07:40:24 +0200879 * @brief Get module without revision according to priorities.
880 *
881 * 1. Search for the module with LYS_MOD_IMPORTED_REV.
882 * 2. Search for the implemented module.
883 * 3. Search for the latest module in the context.
884 *
885 * @param[in] ctx libyang context where module is searched.
886 * @param[in] name Name of the searched module.
aPiecekd4911ee2021-07-30 07:40:24 +0200887 * @return Found module from context or NULL.
888 */
889static struct lys_module *
Michal Vaskobdac23f2022-01-12 14:02:35 +0100890lys_get_module_without_revision(struct ly_ctx *ctx, const char *name)
aPiecekd4911ee2021-07-30 07:40:24 +0200891{
892 struct lys_module *mod, *mod_impl;
893 uint32_t index;
894
aPiecekd4911ee2021-07-30 07:40:24 +0200895 /* Try to find module with LYS_MOD_IMPORTED_REV flag. */
896 index = 0;
897 while ((mod = ly_ctx_get_module_iter(ctx, &index))) {
898 if (!strcmp(mod->name, name) && (mod->latest_revision & LYS_MOD_IMPORTED_REV)) {
899 break;
900 }
901 }
902
903 /* Try to find the implemented module. */
904 mod_impl = ly_ctx_get_module_implemented(ctx, name);
905 if (mod && mod_impl) {
Michal Vaskobdac23f2022-01-12 14:02:35 +0100906 LOGVRB("Implemented module \"%s@%s\" is not used for import, revision \"%s\" is imported instead.",
aPiecekd4911ee2021-07-30 07:40:24 +0200907 mod_impl->name, mod_impl->revision, mod->revision);
908 return mod;
909 } else if (mod_impl) {
910 return mod_impl;
911 }
912
913 /* Try to find the latest module in the current context. */
914 mod = ly_ctx_get_module_latest(ctx, name);
aPiecekd4911ee2021-07-30 07:40:24 +0200915
916 return mod;
917}
918
919/**
aPiecek4725aea2021-07-28 10:18:33 +0200920 * @brief Check if a circular dependency exists between modules.
921 *
922 * @param[in] ctx libyang context for log an error.
923 * @param[in,out] mod Examined module which is set to NULL
924 * if the circular dependency is detected.
925 * @return LY_SUCCESS if no circular dependecy is detected,
926 * otherwise LY_EVALID.
927 */
928static LY_ERR
929lys_check_circular_dependency(struct ly_ctx *ctx, struct lys_module **mod)
930{
931 if ((*mod) && (*mod)->parsed->parsing) {
932 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", (*mod)->name);
Michal Vasko4e205e82021-06-08 14:01:47 +0200933 *mod = NULL;
934 return LY_EVALID;
Michal Vasko0550b762020-11-24 18:04:08 +0100935 }
Radek Krejci086c7132018-10-26 15:29:04 +0200936
aPiecek4725aea2021-07-28 10:18:33 +0200937 return LY_SUCCESS;
938}
939
940LY_ERR
941lys_parse_load(struct ly_ctx *ctx, const char *name, const char *revision, struct ly_set *new_mods,
942 struct lys_module **mod)
943{
944 struct lys_module *mod_latest = NULL;
945
946 assert(mod && new_mods);
947
Michal Vasko0550b762020-11-24 18:04:08 +0100948 /*
aPiecek4725aea2021-07-28 10:18:33 +0200949 * Try to get the module from the context.
Michal Vasko0550b762020-11-24 18:04:08 +0100950 */
aPiecek4725aea2021-07-28 10:18:33 +0200951 if (revision) {
952 /* Get the specific revision. */
953 *mod = ly_ctx_get_module(ctx, name, revision);
954 } else {
Michal Vaskobdac23f2022-01-12 14:02:35 +0100955 /* Get the requested module in a suitable revision in the context. */
956 *mod = lys_get_module_without_revision(ctx, name);
957 if (*mod && !(*mod)->implemented && !((*mod)->latest_revision & LYS_MOD_IMPORTED_REV)) {
aPiecek4725aea2021-07-28 10:18:33 +0200958 /* Let us now search with callback and searchpaths to check
959 * if there is newer revision outside the context.
960 */
961 mod_latest = *mod;
962 *mod = NULL;
963 }
964 }
965
Michal Vasko0550b762020-11-24 18:04:08 +0100966 if (!*mod) {
aPiecek4725aea2021-07-28 10:18:33 +0200967 /* No suitable module in the context, try to load it. */
Michal Vaskobdac23f2022-01-12 14:02:35 +0100968 LY_CHECK_RET(lys_parse_load_from_clb_or_file(ctx, name, revision, mod_latest, new_mods, mod));
aPiecek4725aea2021-07-28 10:18:33 +0200969 if (!*mod && !mod_latest) {
Michal Vasko4e205e82021-06-08 14:01:47 +0200970 LOGVAL(ctx, LYVE_REFERENCE, "Loading \"%s\" module failed.", name);
Michal Vasko0550b762020-11-24 18:04:08 +0100971 return LY_EVALID;
972 }
aPiecek4725aea2021-07-28 10:18:33 +0200973
974 /* Update the latest_revision flag - here we have selected the latest available schema,
975 * consider that even the callback provides correct latest revision.
976 */
977 if (!*mod) {
978 LOGVRB("Newer revision than \"%s@%s\" not found, using this as the latest revision.",
979 mod_latest->name, mod_latest->revision);
980 assert(mod_latest->latest_revision & LYS_MOD_LATEST_REV);
981 mod_latest->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS;
982 *mod = mod_latest;
983 } else if (*mod && !revision && ((*mod)->latest_revision & LYS_MOD_LATEST_REV)) {
984 (*mod)->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS;
985 }
Radek Krejci086c7132018-10-26 15:29:04 +0200986 }
Radek Krejci086c7132018-10-26 15:29:04 +0200987
aPiecek9f8c7e72021-07-28 12:01:56 +0200988 /* Checking the circular dependence of imported modules. */
989 LY_CHECK_RET(lys_check_circular_dependency(ctx, mod));
990
Radek Krejci086c7132018-10-26 15:29:04 +0200991 return LY_SUCCESS;
992}
993
994LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200995lysp_check_stringchar(struct lys_parser_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +0200996{
997 if (!is_yangutf8char(c)) {
998 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
999 return LY_EVALID;
1000 }
1001 return LY_SUCCESS;
1002}
1003
1004LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001005lysp_check_identifierchar(struct lys_parser_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +02001006{
Michal Vasko69730152020-10-09 16:30:07 +02001007 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +02001008 if (!is_yangidentstartchar(c)) {
aPiecekc89b2242021-05-14 14:19:11 +02001009 if ((c < UCHAR_MAX) && isprint(c)) {
Michal Vasko7c769042021-03-25 12:20:49 +01001010 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
1011 } else {
1012 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character 0x%04x.", c);
1013 }
David Sedlák4a650532019-07-10 11:55:18 +02001014 return LY_EVALID;
1015 }
1016 if (prefix) {
1017 if (first) {
1018 (*prefix) = 0;
1019 } else {
1020 (*prefix) = 2;
1021 }
1022 }
Michal Vasko69730152020-10-09 16:30:07 +02001023 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +02001024 (*prefix) = 1;
1025 } else if (!is_yangidentchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001026 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +02001027 return LY_EVALID;
1028 }
1029
1030 return LY_SUCCESS;
1031}
1032
Radek Krejci771928a2021-01-19 13:42:36 +01001033/**
1034 * @brief Try to find the parsed submodule in main module for the given include record.
1035 *
1036 * @param[in] pctx main parser context
1037 * @param[in] inc The include record with missing parsed submodule. According to include info try to find
1038 * the corresponding parsed submodule in main module's includes.
1039 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
1040 * @return LY_ENOT - the parsed module was not found.
1041 * @return LY_EVALID - YANG rule violation
1042 */
1043static LY_ERR
Michal Vasko8a67eff2021-12-07 14:04:47 +01001044lysp_main_pmod_get_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +02001045{
Radek Krejci771928a2021-01-19 13:42:36 +01001046 LY_ARRAY_COUNT_TYPE i;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001047 struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed;
Michal Vasko69730152020-10-09 16:30:07 +02001048
Radek Krejci771928a2021-01-19 13:42:36 +01001049 LY_ARRAY_FOR(main_pmod->includes, i) {
1050 if (strcmp(main_pmod->includes[i].name, inc->name)) {
1051 continue;
1052 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001053
Radek Krejci771928a2021-01-19 13:42:36 +01001054 if (inc->rev[0] && strncmp(inc->rev, main_pmod->includes[i].rev, LY_REV_SIZE)) {
1055 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1056 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001057 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev,
Radek Krejci771928a2021-01-19 13:42:36 +01001058 main_pmod->includes[i].name, main_pmod->includes[i].rev, main_pmod->mod->name);
1059 return LY_EVALID;
1060 }
1061
1062 inc->submodule = main_pmod->includes[i].submodule;
1063 return inc->submodule ? LY_SUCCESS : LY_ENOT;
1064 }
1065
1066 if (main_pmod->version == LYS_VERSION_1_1) {
1067 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1068 "YANG 1.1 requires all submodules to be included from main module. "
1069 "But submodule \"%s\" includes submodule \"%s\" which is not included by main module \"%s\".",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001070 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->name, main_pmod->mod->name);
Radek Krejci771928a2021-01-19 13:42:36 +01001071 return LY_EVALID;
1072 } else {
1073 return LY_ENOT;
1074 }
1075}
1076
1077/**
Michal Vasko8a67eff2021-12-07 14:04:47 +01001078 * @brief Try to find the parsed submodule in currenlty parsed modules for the given include record.
1079 *
1080 * @param[in] pctx main parser context
1081 * @param[in] inc The include record with missing parsed submodule.
1082 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
1083 * @return LY_ENOT - the parsed module was not found.
1084 * @return LY_EVALID - YANG rule violation
1085 */
1086static LY_ERR
1087lysp_parsed_mods_get_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
1088{
1089 uint32_t i;
1090 struct lysp_submodule *submod;
1091
1092 for (i = 0; i < pctx->parsed_mods->count - 1; ++i) {
1093 submod = pctx->parsed_mods->objs[i];
1094 if (!submod->is_submod) {
1095 continue;
1096 }
1097
1098 if (strcmp(submod->name, inc->name)) {
1099 continue;
1100 }
1101
1102 if (inc->rev[0] && submod->revs && strncmp(inc->rev, submod->revs[0].date, LY_REV_SIZE)) {
1103 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1104 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
1105 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev,
1106 submod->name, submod->revs[0].date, PARSER_CUR_PMOD(pctx)->mod->name);
1107 return LY_EVALID;
1108 }
1109
1110 inc->submodule = submod;
1111 return LY_SUCCESS;
1112 }
1113
1114 return LY_ENOT;
1115}
1116
1117/**
Radek Krejci771928a2021-01-19 13:42:36 +01001118 * @brief Make the copy of the given include record into the main module.
1119 *
1120 * YANG 1.0 does not require the main module to include all the submodules. Therefore, parsing submodules can cause
1121 * reallocating and extending the includes array in the main module by the submodules included only in submodules.
1122 *
1123 * @param[in] pctx main parser context
1124 * @param[in] inc Include record to copy into main module taken from @p pctx.
1125 * @return LY_ERR value.
1126 */
1127static LY_ERR
1128lysp_inject_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
1129{
1130 LY_ARRAY_COUNT_TYPE i;
1131 struct lysp_include *inc_new, *inc_tofill = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001132 struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed;
Radek Krejci771928a2021-01-19 13:42:36 +01001133
1134 /* first, try to find the corresponding record with missing parsed submodule */
1135 LY_ARRAY_FOR(main_pmod->includes, i) {
1136 if (strcmp(main_pmod->includes[i].name, inc->name)) {
1137 continue;
1138 }
1139 inc_tofill = &main_pmod->includes[i];
1140 break;
1141 }
1142
1143 if (inc_tofill) {
1144 inc_tofill->submodule = inc->submodule;
1145 } else {
1146 LY_ARRAY_NEW_RET(PARSER_CTX(pctx), main_pmod->includes, inc_new, LY_EMEM);
1147
1148 inc_new->submodule = inc->submodule;
1149 DUP_STRING_RET(PARSER_CTX(pctx), inc->name, inc_new->name);
1150 DUP_STRING_RET(PARSER_CTX(pctx), inc->dsc, inc_new->dsc);
1151 DUP_STRING_RET(PARSER_CTX(pctx), inc->ref, inc_new->ref);
1152 /* TODO duplicate extensions */
1153 memcpy(inc_new->rev, inc->rev, LY_REV_SIZE);
1154 inc_new->injected = 1;
1155 }
1156 return LY_SUCCESS;
1157}
1158
1159LY_ERR
aPiecekc3e26142021-06-22 14:25:49 +02001160lysp_load_submodules(struct lys_parser_ctx *pctx, struct lysp_module *pmod, struct ly_set *new_mods)
Radek Krejci771928a2021-01-19 13:42:36 +01001161{
1162 LY_ARRAY_COUNT_TYPE u;
1163 struct ly_ctx *ctx = PARSER_CTX(pctx);
1164
1165 LY_ARRAY_FOR(pmod->includes, u) {
Michal Vasko8a67eff2021-12-07 14:04:47 +01001166 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci771928a2021-01-19 13:42:36 +01001167 struct lysp_submodule *submod = NULL;
1168 struct lysp_include *inc = &pmod->includes[u];
1169
1170 if (inc->submodule) {
1171 continue;
1172 }
1173
1174 if (pmod->is_submod) {
1175 /* try to find the submodule in the main module or its submodules */
Michal Vasko8a67eff2021-12-07 14:04:47 +01001176 ret = lysp_main_pmod_get_submodule(pctx, inc);
1177 LY_CHECK_RET(ret != LY_ENOT, ret);
Radek Krejci771928a2021-01-19 13:42:36 +01001178 }
1179
Michal Vasko8a67eff2021-12-07 14:04:47 +01001180 /* try to use currently parsed submodule */
1181 r = lysp_parsed_mods_get_submodule(pctx, inc);
1182 LY_CHECK_RET(r != LY_ENOT, r);
1183
Radek Krejci771928a2021-01-19 13:42:36 +01001184 /* submodule not present in the main module, get the input data and parse it */
1185 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcidf549132021-01-21 10:32:32 +01001186search_clb:
Radek Krejci771928a2021-01-19 13:42:36 +01001187 if (ctx->imp_clb) {
1188 const char *submodule_data = NULL;
1189 LYS_INFORMAT format = LYS_IN_UNKNOWN;
1190 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
1191 struct lysp_load_module_check_data check_data = {0};
1192 struct ly_in *in;
1193
Michal Vasko8a67eff2021-12-07 14:04:47 +01001194 if (ctx->imp_clb(PARSER_CUR_PMOD(pctx)->mod->name, NULL, inc->name,
Radek Krejci771928a2021-01-19 13:42:36 +01001195 inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
1196 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
1197 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
1198 check_data.name = inc->name;
1199 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001200 check_data.submoduleof = PARSER_CUR_PMOD(pctx)->mod->name;
aPiecekc3e26142021-06-22 14:25:49 +02001201 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, new_mods, &submod);
Radek Krejci771928a2021-01-19 13:42:36 +01001202
1203 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1204 * submodule's include into main module, where it is missing */
1205 inc = &pmod->includes[u];
1206
1207 ly_in_free(in, 0);
1208 if (submodule_data_free) {
1209 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
1210 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001211 }
1212 }
Radek Krejci771928a2021-01-19 13:42:36 +01001213 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1214 goto search_file;
1215 }
1216 } else {
Radek Krejcidf549132021-01-21 10:32:32 +01001217search_file:
Radek Krejci771928a2021-01-19 13:42:36 +01001218 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
1219 /* submodule was not received from the callback or there is no callback set */
Michal Vasko4e205e82021-06-08 14:01:47 +02001220 lys_parse_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, pctx,
Michal Vasko8a67eff2021-12-07 14:04:47 +01001221 PARSER_CUR_PMOD(pctx)->mod->name, 1, new_mods, (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001222
Radek Krejci771928a2021-01-19 13:42:36 +01001223 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1224 * submodule's include into main module, where it is missing */
1225 inc = &pmod->includes[u];
1226 }
1227 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1228 goto search_clb;
1229 }
1230 }
1231 if (submod) {
1232 if (!inc->rev[0] && (submod->latest_revision == 1)) {
1233 /* update the latest_revision flag - here we have selected the latest available schema,
1234 * consider that even the callback provides correct latest revision */
1235 submod->latest_revision = 2;
1236 }
1237
1238 inc->submodule = submod;
1239 if (ret == LY_ENOT) {
1240 /* the submodule include is not present in YANG 1.0 main module - add it there */
1241 LY_CHECK_RET(lysp_inject_submodule(pctx, &pmod->includes[u]));
1242 }
1243 }
1244 if (!inc->submodule) {
1245 LOGVAL(ctx, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name,
Michal Vasko8a67eff2021-12-07 14:04:47 +01001246 PARSER_CUR_PMOD(pctx)->is_submod ? ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name :
1247 PARSER_CUR_PMOD(pctx)->mod->name);
Radek Krejci771928a2021-01-19 13:42:36 +01001248 return LY_EVALID;
1249 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001250 }
1251
1252 return LY_SUCCESS;
1253}
1254
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001255LIBYANG_API_DEF const struct lysc_when *
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001256lysc_has_when(const struct lysc_node *node)
1257{
Radek Krejci9a3823e2021-01-27 20:26:46 +01001258 struct lysc_when **when;
1259
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001260 if (!node) {
1261 return NULL;
1262 }
1263
1264 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001265 when = lysc_node_when(node);
1266 if (when) {
1267 return when[0];
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001268 }
1269 node = node->parent;
1270 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1271
1272 return NULL;
1273}
1274
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001275LIBYANG_API_DEF const struct lys_module *
Michal Vaskoef53c812021-10-13 10:21:03 +02001276lysc_owner_module(const struct lysc_node *node)
1277{
1278 if (!node) {
1279 return NULL;
1280 }
1281
1282 for ( ; node->parent; node = node->parent) {}
1283 return node->module;
1284}
1285
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001286LIBYANG_API_DEF const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001287lys_nodetype2str(uint16_t nodetype)
1288{
Michal Vaskod989ba02020-08-24 10:59:24 +02001289 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001290 case LYS_CONTAINER:
1291 return "container";
1292 case LYS_CHOICE:
1293 return "choice";
1294 case LYS_LEAF:
1295 return "leaf";
1296 case LYS_LEAFLIST:
1297 return "leaf-list";
1298 case LYS_LIST:
1299 return "list";
1300 case LYS_ANYXML:
1301 return "anyxml";
1302 case LYS_ANYDATA:
1303 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001304 case LYS_CASE:
1305 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001306 case LYS_RPC:
1307 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001308 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001309 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001310 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001311 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001312 case LYS_USES:
1313 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001314 default:
1315 return "unknown";
1316 }
1317}
1318
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001319LIBYANG_API_DEF enum ly_stmt
Radek Krejci39b7fc22021-02-26 23:29:18 +01001320lys_nodetype2stmt(uint16_t nodetype)
1321{
1322 switch (nodetype) {
1323 case LYS_CONTAINER:
1324 return LY_STMT_CONTAINER;
1325 case LYS_CHOICE:
1326 return LY_STMT_CHOICE;
1327 case LYS_LEAF:
1328 return LY_STMT_LEAF;
1329 case LYS_LEAFLIST:
1330 return LY_STMT_LEAF_LIST;
1331 case LYS_LIST:
1332 return LY_STMT_LIST;
1333 case LYS_ANYXML:
1334 return LY_STMT_ANYXML;
1335 case LYS_ANYDATA:
1336 return LY_STMT_ANYDATA;
1337 case LYS_CASE:
1338 return LY_STMT_CASE;
1339 case LYS_RPC:
1340 return LY_STMT_RPC;
1341 case LYS_ACTION:
1342 return LY_STMT_ACTION;
1343 case LYS_NOTIF:
1344 return LY_STMT_NOTIFICATION;
1345 case LYS_USES:
1346 return LY_STMT_USES;
1347 case LYS_INPUT:
1348 return LY_STMT_INPUT;
1349 case LYS_OUTPUT:
1350 return LY_STMT_OUTPUT;
1351 default:
1352 return LY_STMT_NONE;
1353 }
1354}
1355
Radek Krejci693262f2019-04-29 15:23:20 +02001356const char *
1357lys_datatype2str(LY_DATA_TYPE basetype)
1358{
Michal Vaskod989ba02020-08-24 10:59:24 +02001359 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001360 case LY_TYPE_BINARY:
1361 return "binary";
1362 case LY_TYPE_UINT8:
1363 return "uint8";
1364 case LY_TYPE_UINT16:
1365 return "uint16";
1366 case LY_TYPE_UINT32:
1367 return "uint32";
1368 case LY_TYPE_UINT64:
1369 return "uint64";
1370 case LY_TYPE_STRING:
1371 return "string";
1372 case LY_TYPE_BITS:
1373 return "bits";
1374 case LY_TYPE_BOOL:
1375 return "boolean";
1376 case LY_TYPE_DEC64:
1377 return "decimal64";
1378 case LY_TYPE_EMPTY:
1379 return "empty";
1380 case LY_TYPE_ENUM:
1381 return "enumeration";
1382 case LY_TYPE_IDENT:
1383 return "identityref";
1384 case LY_TYPE_INST:
1385 return "instance-identifier";
1386 case LY_TYPE_LEAFREF:
1387 return "leafref";
1388 case LY_TYPE_UNION:
1389 return "union";
1390 case LY_TYPE_INT8:
1391 return "int8";
1392 case LY_TYPE_INT16:
1393 return "int16";
1394 case LY_TYPE_INT32:
1395 return "int32";
1396 case LY_TYPE_INT64:
1397 return "int64";
1398 default:
1399 return "unknown";
1400 }
1401}
1402
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001403LIBYANG_API_DEF const struct lysp_tpdf *
Radek Krejci056d0a82018-12-06 16:57:25 +01001404lysp_node_typedefs(const struct lysp_node *node)
1405{
Radek Krejci0fb28562018-12-13 15:17:37 +01001406 switch (node->nodetype) {
1407 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001408 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001409 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001410 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001411 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001412 return ((struct lysp_node_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001413 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001414 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001415 return ((struct lysp_node_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001416 case LYS_INPUT:
1417 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001418 return ((struct lysp_node_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001419 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001420 return ((struct lysp_node_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001421 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001422 return NULL;
1423 }
1424}
1425
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001426LIBYANG_API_DEF const struct lysp_node_grp *
Radek Krejci53ea6152018-12-13 15:21:15 +01001427lysp_node_groupings(const struct lysp_node *node)
1428{
1429 switch (node->nodetype) {
1430 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001431 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001432 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001433 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001434 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001435 return ((struct lysp_node_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001436 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001437 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001438 return ((struct lysp_node_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001439 case LYS_INPUT:
1440 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001441 return ((struct lysp_node_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001442 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001443 return ((struct lysp_node_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001444 default:
1445 return NULL;
1446 }
1447}
1448
Radek Krejci2a9fc652021-01-22 17:44:34 +01001449struct lysp_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001450lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001451{
1452 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001453
Radek Krejcibbe09a92018-11-08 09:36:54 +01001454 switch (node->nodetype) {
1455 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001456 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001457 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001458 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001459 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001460 return &((struct lysp_node_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001461 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001462 return &((struct lysp_node_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001463 default:
1464 return NULL;
1465 }
1466}
1467
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001468LIBYANG_API_DEF const struct lysp_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001469lysp_node_actions(const struct lysp_node *node)
1470{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001471 struct lysp_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001472
Michal Vasko22df3f02020-08-24 13:29:22 +02001473 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001474 if (actions) {
1475 return *actions;
1476 } else {
1477 return NULL;
1478 }
1479}
1480
Radek Krejci2a9fc652021-01-22 17:44:34 +01001481struct lysp_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001482lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001483{
1484 assert(node);
1485 switch (node->nodetype) {
1486 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001487 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001488 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001489 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001490 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001491 return &((struct lysp_node_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001492 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001493 return &((struct lysp_node_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001494 default:
1495 return NULL;
1496 }
1497}
1498
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001499LIBYANG_API_DEF const struct lysp_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001500lysp_node_notifs(const struct lysp_node *node)
1501{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001502 struct lysp_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001503
Michal Vasko22df3f02020-08-24 13:29:22 +02001504 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001505 if (notifs) {
1506 return *notifs;
1507 } else {
1508 return NULL;
1509 }
1510}
1511
Radek Krejcibbe09a92018-11-08 09:36:54 +01001512struct lysp_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001513lysp_node_child_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001514{
1515 assert(node);
1516 switch (node->nodetype) {
1517 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001518 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001519 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001520 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001521 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001522 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001523 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001524 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001525 case LYS_GROUPING:
Radek Krejci01180ac2021-01-27 08:48:22 +01001526 return &((struct lysp_node_grp *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001527 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001528 return &((struct lysp_node_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001529 case LYS_INPUT:
1530 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001531 return &((struct lysp_node_action_inout *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001532 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001533 return &((struct lysp_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001534 default:
1535 return NULL;
1536 }
1537}
1538
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001539LIBYANG_API_DEF const struct lysp_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001540lysp_node_child(const struct lysp_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01001541{
Michal Vasko544e58a2021-01-28 14:33:41 +01001542 struct lysp_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001543
1544 if (!node) {
1545 return NULL;
1546 }
1547
Michal Vasko544e58a2021-01-28 14:33:41 +01001548 child = lysp_node_child_p((struct lysp_node *)node);
1549 if (child) {
1550 return *child;
Radek Krejci056d0a82018-12-06 16:57:25 +01001551 } else {
1552 return NULL;
1553 }
1554}
1555
Radek Krejci9a3823e2021-01-27 20:26:46 +01001556struct lysp_restr **
1557lysp_node_musts_p(const struct lysp_node *node)
1558{
1559 if (!node) {
1560 return NULL;
1561 }
1562
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001563 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001564 case LYS_CONTAINER:
1565 return &((struct lysp_node_container *)node)->musts;
1566 case LYS_LEAF:
1567 return &((struct lysp_node_leaf *)node)->musts;
1568 case LYS_LEAFLIST:
1569 return &((struct lysp_node_leaflist *)node)->musts;
1570 case LYS_LIST:
1571 return &((struct lysp_node_list *)node)->musts;
1572 case LYS_ANYXML:
1573 case LYS_ANYDATA:
1574 return &((struct lysp_node_anydata *)node)->musts;
1575 case LYS_NOTIF:
1576 return &((struct lysp_node_notif *)node)->musts;
1577 case LYS_INPUT:
1578 case LYS_OUTPUT:
1579 return &((struct lysp_node_action_inout *)node)->musts;
1580 default:
1581 return NULL;
1582 }
1583}
1584
1585struct lysp_restr *
1586lysp_node_musts(const struct lysp_node *node)
1587{
1588 struct lysp_restr **musts;
1589
1590 musts = lysp_node_musts_p(node);
1591 if (musts) {
1592 return *musts;
1593 } else {
1594 return NULL;
1595 }
1596}
1597
1598struct lysp_when **
1599lysp_node_when_p(const struct lysp_node *node)
1600{
1601 if (!node) {
1602 return NULL;
1603 }
1604
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001605 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001606 case LYS_CONTAINER:
1607 return &((struct lysp_node_container *)node)->when;
1608 case LYS_CHOICE:
1609 return &((struct lysp_node_choice *)node)->when;
1610 case LYS_LEAF:
1611 return &((struct lysp_node_leaf *)node)->when;
1612 case LYS_LEAFLIST:
1613 return &((struct lysp_node_leaflist *)node)->when;
1614 case LYS_LIST:
1615 return &((struct lysp_node_list *)node)->when;
1616 case LYS_ANYXML:
1617 case LYS_ANYDATA:
1618 return &((struct lysp_node_anydata *)node)->when;
1619 case LYS_CASE:
1620 return &((struct lysp_node_case *)node)->when;
1621 case LYS_USES:
1622 return &((struct lysp_node_uses *)node)->when;
1623 case LYS_AUGMENT:
1624 return &((struct lysp_node_augment *)node)->when;
1625 default:
1626 return NULL;
1627 }
1628}
1629
1630struct lysp_when *
1631lysp_node_when(const struct lysp_node *node)
1632{
1633 struct lysp_when **when;
1634
1635 when = lysp_node_when_p(node);
1636 if (when) {
1637 return *when;
1638 } else {
1639 return NULL;
1640 }
1641}
1642
Radek Krejci2a9fc652021-01-22 17:44:34 +01001643struct lysc_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001644lysc_node_actions_p(struct lysc_node *node)
1645{
1646 assert(node);
1647 switch (node->nodetype) {
1648 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001649 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001650 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001651 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001652 default:
1653 return NULL;
1654 }
1655}
1656
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001657LIBYANG_API_DEF const struct lysc_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001658lysc_node_actions(const struct lysc_node *node)
1659{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001660 struct lysc_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001661
Michal Vasko22df3f02020-08-24 13:29:22 +02001662 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001663 if (actions) {
1664 return *actions;
1665 } else {
1666 return NULL;
1667 }
1668}
1669
Radek Krejci2a9fc652021-01-22 17:44:34 +01001670struct lysc_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001671lysc_node_notifs_p(struct lysc_node *node)
1672{
1673 assert(node);
1674 switch (node->nodetype) {
1675 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001676 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001677 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001678 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001679 default:
1680 return NULL;
1681 }
1682}
1683
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001684LIBYANG_API_DEF const struct lysc_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001685lysc_node_notifs(const struct lysc_node *node)
1686{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001687 struct lysc_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001688
Michal Vasko22df3f02020-08-24 13:29:22 +02001689 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001690 if (notifs) {
1691 return *notifs;
1692 } else {
1693 return NULL;
1694 }
1695}
1696
Radek Krejcibbe09a92018-11-08 09:36:54 +01001697struct lysc_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001698lysc_node_child_p(const struct lysc_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001699{
Michal Vasko544e58a2021-01-28 14:33:41 +01001700 assert(node && !(node->nodetype & (LYS_RPC | LYS_ACTION)));
1701
Radek Krejcibbe09a92018-11-08 09:36:54 +01001702 switch (node->nodetype) {
1703 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001704 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001705 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001706 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001707 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001708 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001709 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001710 return &((struct lysc_node_list *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001711 case LYS_INPUT:
1712 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001713 return &((struct lysc_node_action_inout *)node)->child;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001714 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001715 return &((struct lysc_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001716 default:
1717 return NULL;
1718 }
1719}
1720
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001721LIBYANG_API_DEF const struct lysc_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001722lysc_node_child(const struct lysc_node *node)
Radek Krejcia3045382018-11-22 14:30:31 +01001723{
Michal Vasko544e58a2021-01-28 14:33:41 +01001724 struct lysc_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001725
1726 if (!node) {
1727 return NULL;
1728 }
1729
Michal Vasko544e58a2021-01-28 14:33:41 +01001730 if (node->nodetype & (LYS_RPC | LYS_ACTION)) {
1731 return &((struct lysc_node_action *)node)->input.node;
Radek Krejcibe154442021-01-21 11:06:36 +01001732 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01001733 child = lysc_node_child_p(node);
1734 if (child) {
1735 return *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001736 }
Michal Vasko2a668712020-10-21 11:48:09 +02001737 }
Michal Vasko544e58a2021-01-28 14:33:41 +01001738
1739 return NULL;
Michal Vasko2a668712020-10-21 11:48:09 +02001740}
1741
Radek Krejci9a3823e2021-01-27 20:26:46 +01001742struct lysc_must **
1743lysc_node_musts_p(const struct lysc_node *node)
1744{
1745 if (!node) {
1746 return NULL;
1747 }
1748
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001749 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001750 case LYS_CONTAINER:
1751 return &((struct lysc_node_container *)node)->musts;
1752 case LYS_LEAF:
1753 return &((struct lysc_node_leaf *)node)->musts;
1754 case LYS_LEAFLIST:
1755 return &((struct lysc_node_leaflist *)node)->musts;
1756 case LYS_LIST:
1757 return &((struct lysc_node_list *)node)->musts;
1758 case LYS_ANYXML:
1759 case LYS_ANYDATA:
1760 return &((struct lysc_node_anydata *)node)->musts;
1761 case LYS_NOTIF:
1762 return &((struct lysc_node_notif *)node)->musts;
1763 case LYS_INPUT:
1764 case LYS_OUTPUT:
1765 return &((struct lysc_node_action_inout *)node)->musts;
1766 default:
1767 return NULL;
1768 }
1769}
1770
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001771LIBYANG_API_DEF struct lysc_must *
Radek Krejci9a3823e2021-01-27 20:26:46 +01001772lysc_node_musts(const struct lysc_node *node)
1773{
1774 struct lysc_must **must_p;
1775
1776 must_p = lysc_node_musts_p(node);
1777 if (must_p) {
1778 return *must_p;
1779 } else {
1780 return NULL;
1781 }
1782}
1783
1784struct lysc_when ***
1785lysc_node_when_p(const struct lysc_node *node)
1786{
1787 if (!node) {
1788 return NULL;
1789 }
1790
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001791 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001792 case LYS_CONTAINER:
1793 return &((struct lysc_node_container *)node)->when;
1794 case LYS_CHOICE:
1795 return &((struct lysc_node_choice *)node)->when;
1796 case LYS_LEAF:
1797 return &((struct lysc_node_leaf *)node)->when;
1798 case LYS_LEAFLIST:
1799 return &((struct lysc_node_leaflist *)node)->when;
1800 case LYS_LIST:
1801 return &((struct lysc_node_list *)node)->when;
1802 case LYS_ANYXML:
1803 case LYS_ANYDATA:
1804 return &((struct lysc_node_anydata *)node)->when;
1805 case LYS_CASE:
1806 return &((struct lysc_node_case *)node)->when;
1807 case LYS_NOTIF:
1808 return &((struct lysc_node_notif *)node)->when;
1809 case LYS_RPC:
1810 case LYS_ACTION:
1811 return &((struct lysc_node_action *)node)->when;
1812 default:
1813 return NULL;
1814 }
1815}
1816
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001817LIBYANG_API_DEF struct lysc_when **
Radek Krejci9a3823e2021-01-27 20:26:46 +01001818lysc_node_when(const struct lysc_node *node)
1819{
1820 struct lysc_when ***when_p;
1821
1822 when_p = lysc_node_when_p(node);
1823 if (when_p) {
1824 return *when_p;
1825 } else {
1826 return NULL;
1827 }
1828}
1829
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001830struct lys_module *
1831lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1832{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001833 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001834 if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) {
Michal Vasko69730152020-10-09 16:30:07 +02001835 return (struct lys_module *)ctx->list.objs[u];
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001836 }
1837 }
1838 return NULL;
1839}
1840
Radek Krejcid6b76452019-09-03 17:03:03 +02001841enum ly_stmt
Radek Krejcid54412f2020-12-17 20:25:35 +01001842lysp_match_kw(struct ly_in *in, uint64_t *indent)
David Sedlákc10e7902018-12-17 02:17:59 +01001843{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001844/**
Radek Krejcid54412f2020-12-17 20:25:35 +01001845 * @brief Move the input by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001846 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001847 *
1848 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001849 */
Radek Krejcid54412f2020-12-17 20:25:35 +01001850#define MOVE_IN(COUNT) \
1851 ly_in_skip(in, COUNT); \
1852 if (indent) { \
1853 (*indent)+=COUNT; \
1854 }
1855#define IF_KW(STR, LEN, STMT) \
1856 if (!strncmp(in->current, STR, LEN)) { \
1857 MOVE_IN(LEN); \
1858 (*kw)=STMT; \
1859 }
1860#define IF_KW_PREFIX(STR, LEN) \
1861 if (!strncmp(in->current, STR, LEN)) { \
1862 MOVE_IN(LEN);
1863#define IF_KW_PREFIX_END \
1864 }
David Sedlák572e7ab2019-06-04 16:01:58 +02001865
Michal Vasko63f3d842020-07-08 10:10:14 +02001866 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001867 enum ly_stmt result = LY_STMT_NONE;
1868 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001869 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001870 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001871 case 'a':
Radek Krejcid54412f2020-12-17 20:25:35 +01001872 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001873 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1874 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1875 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001876 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001877 IF_KW("data", 4, LY_STMT_ANYDATA)
1878 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001879 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001880 break;
1881 case 'b':
Radek Krejcid54412f2020-12-17 20:25:35 +01001882 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001883 IF_KW("ase", 3, LY_STMT_BASE)
1884 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1885 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001886 break;
1887 case 'c':
Radek Krejcid54412f2020-12-17 20:25:35 +01001888 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001889 IF_KW("ase", 3, LY_STMT_CASE)
1890 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001891 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001892 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001893 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001894 IF_KW("ct", 2, LY_STMT_CONTACT)
1895 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001896 IF_KW_PREFIX_END
1897 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001898 break;
1899 case 'd':
Radek Krejcid54412f2020-12-17 20:25:35 +01001900 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001901 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001902 IF_KW("fault", 5, LY_STMT_DEFAULT)
1903 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001904 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001905 IF_KW("e", 1, LY_STMT_DEVIATE)
1906 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001907 IF_KW_PREFIX_END
1908 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001909 break;
1910 case 'e':
Radek Krejcid54412f2020-12-17 20:25:35 +01001911 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001912 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001913 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001914 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1915 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001916 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001917 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001918 break;
1919 case 'f':
Radek Krejcid54412f2020-12-17 20:25:35 +01001920 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001921 IF_KW("eature", 6, LY_STMT_FEATURE)
1922 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001923 break;
1924 case 'g':
Radek Krejcid54412f2020-12-17 20:25:35 +01001925 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001926 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001927 break;
1928 case 'i':
Radek Krejcid54412f2020-12-17 20:25:35 +01001929 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001930 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1931 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1932 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001933 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001934 IF_KW("clude", 5, LY_STMT_INCLUDE)
1935 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001936 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001937 break;
1938 case 'k':
Radek Krejcid54412f2020-12-17 20:25:35 +01001939 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001940 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001941 break;
1942 case 'l':
Radek Krejcid54412f2020-12-17 20:25:35 +01001943 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001944 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001945 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1946 else IF_KW("af", 2, LY_STMT_LEAF)
1947 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001948 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001949 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001950 break;
1951 case 'm':
Radek Krejcid54412f2020-12-17 20:25:35 +01001952 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001953 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001954 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1955 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001956 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001957 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1958 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001959 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001960 IF_KW("ule", 3, LY_STMT_MODULE)
1961 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001962 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001963 break;
1964 case 'n':
Radek Krejcid54412f2020-12-17 20:25:35 +01001965 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001966 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1967 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001968 break;
1969 case 'o':
Radek Krejcid54412f2020-12-17 20:25:35 +01001970 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001971 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001972 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1973 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001974 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001975 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001976 break;
1977 case 'p':
Radek Krejcid54412f2020-12-17 20:25:35 +01001978 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001979 IF_KW("ath", 3, LY_STMT_PATH)
1980 else IF_KW("attern", 6, LY_STMT_PATTERN)
1981 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001982 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001983 IF_KW("fix", 3, LY_STMT_PREFIX)
1984 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001985 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001986 break;
1987 case 'r':
Radek Krejcid54412f2020-12-17 20:25:35 +01001988 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001989 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001990 else IF_KW_PREFIX("e", 1)
1991 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001992 IF_KW("erence", 6, LY_STMT_REFERENCE)
1993 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001994 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001995 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1996 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1997 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001998 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001999 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02002000 break;
2001 case 's':
Radek Krejcid54412f2020-12-17 20:25:35 +01002002 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002003 IF_KW("tatus", 5, LY_STMT_STATUS)
2004 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02002005 break;
2006 case 't':
Radek Krejcid54412f2020-12-17 20:25:35 +01002007 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002008 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
2009 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02002010 break;
2011 case 'u':
Radek Krejcid54412f2020-12-17 20:25:35 +01002012 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02002013 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02002014 IF_KW("que", 3, LY_STMT_UNIQUE)
2015 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002016 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002017 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02002018 break;
2019 case 'v':
Radek Krejcid54412f2020-12-17 20:25:35 +01002020 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002021 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02002022 break;
2023 case 'w':
Radek Krejcid54412f2020-12-17 20:25:35 +01002024 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002025 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02002026 break;
2027 case 'y':
Radek Krejcid54412f2020-12-17 20:25:35 +01002028 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002029 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
2030 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02002031 break;
David Sedlák23a59a62018-10-26 13:08:02 +02002032 default:
Radek Krejcid54412f2020-12-17 20:25:35 +01002033 /* if indent is not NULL we are matching keyword from YANG data */
2034 if (indent) {
Michal Vasko63f3d842020-07-08 10:10:14 +02002035 if (in->current[0] == ';') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002036 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002037 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02002038 } else if (in->current[0] == '{') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002039 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002040 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002041 } else if (in->current[0] == '}') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002042 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002043 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02002044 }
2045 }
David Sedlák23a59a62018-10-26 13:08:02 +02002046 break;
2047 }
2048
Michal Vasko63f3d842020-07-08 10:10:14 +02002049 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02002050 /* the keyword is not terminated */
2051 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002052 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02002053 }
2054
David Sedlák1bccdfa2019-06-17 15:55:27 +02002055#undef IF_KW
2056#undef IF_KW_PREFIX
2057#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01002058#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02002059 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01002060
David Sedlák1bccdfa2019-06-17 15:55:27 +02002061 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02002062}
David Sedlákecf5eb82019-06-03 14:12:44 +02002063
Radek Krejci85ac8312021-03-03 20:21:33 +01002064LY_ERR
2065lysp_ext_find_definition(const struct ly_ctx *ctx, const struct lysp_ext_instance *ext, const struct lys_module **ext_mod,
2066 struct lysp_ext **ext_def)
2067{
Radek Krejci85ac8312021-03-03 20:21:33 +01002068 const char *tmp, *name, *prefix;
2069 size_t pref_len, name_len;
2070 LY_ARRAY_COUNT_TYPE v;
2071 const struct lys_module *mod = NULL;
2072
Radek Krejcicbb62422021-03-15 09:29:21 +01002073 assert(ext_def);
2074
Radek Krejci85ac8312021-03-03 20:21:33 +01002075 *ext_def = NULL;
2076 if (ext_mod) {
2077 *ext_mod = NULL;
2078 }
2079
Radek Krejci677abea2021-03-05 14:24:53 +01002080 /* parse the prefix, the nodeid was previously already parsed and checked */
Radek Krejci85ac8312021-03-03 20:21:33 +01002081 tmp = ext->name;
Radek Krejci677abea2021-03-05 14:24:53 +01002082 ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len);
Radek Krejci85ac8312021-03-03 20:21:33 +01002083
2084 /* get module where the extension definition should be placed */
2085 mod = ly_resolve_prefix(ctx, prefix, pref_len, ext->format, ext->prefix_data);
2086 if (!mod) {
Radek Krejci422afb12021-03-04 16:38:16 +01002087 LOGVAL(ctx, LYVE_REFERENCE, "Invalid prefix \"%.*s\" used for extension instance identifier.", (int)pref_len, prefix);
Radek Krejci85ac8312021-03-03 20:21:33 +01002088 return LY_EVALID;
2089 } else if (!mod->parsed->extensions) {
2090 LOGVAL(ctx, LYVE_REFERENCE, "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
2091 ext->name, mod->name);
2092 return LY_EVALID;
2093 }
2094
2095 /* find the parsed extension definition there */
2096 LY_ARRAY_FOR(mod->parsed->extensions, v) {
2097 if (!strcmp(name, mod->parsed->extensions[v].name)) {
2098 *ext_def = &mod->parsed->extensions[v];
2099 break;
2100 }
2101 }
2102
Radek Krejcicbb62422021-03-15 09:29:21 +01002103 if (!(*ext_def)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002104 LOGVAL(ctx, LYVE_REFERENCE, "Extension definition of extension instance \"%s\" not found.", ext->name);
2105 return LY_EVALID;
2106 }
2107
2108 if (ext_mod) {
2109 *ext_mod = mod;
2110 }
2111 return LY_SUCCESS;
2112}
2113
2114LY_ERR
2115lysp_ext_instance_resolve_argument(struct ly_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysp_ext *ext_def)
2116{
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002117 if (!ext_def->argname || ext_p->argument) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002118 /* nothing to do */
2119 return LY_SUCCESS;
2120 }
2121
Radek Krejci8df109d2021-04-23 12:19:08 +02002122 if (ext_p->format == LY_VALUE_XML) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002123 /* Schema was parsed from YIN and an argument is expected, ... */
2124 struct lysp_stmt *stmt = NULL;
2125
2126 if (ext_def->flags & LYS_YINELEM_TRUE) {
2127 /* ... argument was the first XML child element */
2128 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {}
2129 if (stmt) {
2130 const char *arg, *ext, *name_arg, *name_ext, *prefix_arg, *prefix_ext;
2131 size_t name_arg_len, name_ext_len, prefix_arg_len, prefix_ext_len;
2132
2133 stmt = ext_p->child;
2134
2135 arg = stmt->stmt;
2136 ly_parse_nodeid(&arg, &prefix_arg, &prefix_arg_len, &name_arg, &name_arg_len);
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002137 if (ly_strncmp(ext_def->argname, name_arg, name_arg_len)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002138 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" expects argument element \"%s\" as its first XML child, "
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002139 "but \"%.*s\" element found.", ext_p->name, ext_def->argname, (int)name_arg_len, name_arg);
Radek Krejci85ac8312021-03-03 20:21:33 +01002140 return LY_EVALID;
2141 }
2142
2143 /* check namespace - all the extension instances must be qualified and argument element is expected in the same
2144 * namespace. Do not check just prefixes, there can be different prefixes pointing to the same namespace */
2145 ext = ext_p->name; /* include prefix */
2146 ly_parse_nodeid(&ext, &prefix_ext, &prefix_ext_len, &name_ext, &name_ext_len);
2147
2148 if (ly_resolve_prefix(ctx, prefix_ext, prefix_ext_len, ext_p->format, ext_p->prefix_data) !=
2149 ly_resolve_prefix(ctx, prefix_arg, prefix_arg_len, stmt->format, stmt->prefix_data)) {
2150 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" element and its argument element \"%s\" are "
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002151 "expected in the same namespace, but they differ.", ext_p->name, ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002152 return LY_EVALID;
2153 }
2154 }
2155 } else {
2156 /* ... argument was one of the XML attributes which are represented as child stmt
2157 * with LYS_YIN_ATTR flag */
2158 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002159 if (!strcmp(stmt->stmt, ext_def->argname)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002160 /* this is the extension's argument */
2161 break;
2162 }
2163 }
2164 }
2165
2166 if (stmt) {
2167 LY_CHECK_RET(lydict_insert(ctx, stmt->arg, 0, &ext_p->argument));
2168 stmt->flags |= LYS_YIN_ARGUMENT;
2169 }
2170 }
2171
2172 if (!ext_p->argument) {
2173 /* missing extension's argument */
2174 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" misses argument %s\"%s\".",
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002175 ext_p->name, (ext_def->flags & LYS_YINELEM_TRUE) ? "element " : "", ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002176 return LY_EVALID;
2177 }
2178
2179 return LY_SUCCESS;
2180}
2181
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002182LY_ARRAY_COUNT_TYPE
Radek Krejcifc596f92021-02-26 22:40:26 +01002183lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, enum ly_stmt substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002184{
2185 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
2186
Michal Vaskod989ba02020-08-24 10:59:24 +02002187 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejciab430862021-03-02 20:13:40 +01002188 if (ext[index].parent_stmt == substmt) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02002189 return index;
2190 }
2191 }
2192
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002193 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002194}
2195
Michal Vaskof8ded142022-02-17 10:48:01 +01002196LIBYANG_API_DEF const struct lysc_node *
Michal Vasko72244882021-01-12 15:21:05 +01002197lysc_data_node(const struct lysc_node *schema)
Michal Vasko62ed12d2020-05-21 10:08:25 +02002198{
2199 const struct lysc_node *parent;
2200
Michal Vasko72244882021-01-12 15:21:05 +01002201 parent = schema;
Radek Krejcidf549132021-01-21 10:32:32 +01002202 while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC |
2203 LYS_ACTION | LYS_NOTIF))) {
Radek Krejci7d95fbb2021-01-26 17:33:13 +01002204 parent = parent->parent;
Michal Vasko72244882021-01-12 15:21:05 +01002205 }
Michal Vasko62ed12d2020-05-21 10:08:25 +02002206
2207 return parent;
2208}
Michal Vaskof4258e12021-06-15 12:11:42 +02002209
2210ly_bool
2211lys_has_recompiled(const struct lys_module *mod)
2212{
2213 LY_ARRAY_COUNT_TYPE u;
2214
2215 if (LYSP_HAS_RECOMPILED(mod->parsed)) {
2216 return 1;
2217 }
2218
2219 LY_ARRAY_FOR(mod->parsed->includes, u) {
2220 if (LYSP_HAS_RECOMPILED(mod->parsed->includes[u].submodule)) {
2221 return 1;
2222 }
2223 }
2224
2225 return 0;
2226}
2227
2228ly_bool
2229lys_has_compiled(const struct lys_module *mod)
2230{
2231 LY_ARRAY_COUNT_TYPE u;
2232
2233 if (LYSP_HAS_COMPILED(mod->parsed)) {
2234 return 1;
2235 }
2236
2237 LY_ARRAY_FOR(mod->parsed->includes, u) {
2238 if (LYSP_HAS_COMPILED(mod->parsed->includes[u].submodule)) {
2239 return 1;
2240 }
2241 }
2242
2243 return 0;
2244}
Michal Vasko7ee5be22021-06-16 17:03:34 +02002245
2246ly_bool
Michal Vasko87cfdba2022-02-22 14:13:45 +01002247lys_has_dep_mods(const struct lys_module *mod)
Michal Vasko7ee5be22021-06-16 17:03:34 +02002248{
2249 LY_ARRAY_COUNT_TYPE u;
2250
Michal Vasko87cfdba2022-02-22 14:13:45 +01002251 /* features */
2252 if (mod->parsed->features) {
Michal Vasko7ee5be22021-06-16 17:03:34 +02002253 return 1;
2254 }
2255
Michal Vasko87cfdba2022-02-22 14:13:45 +01002256 /* groupings */
2257 if (mod->parsed->groupings) {
2258 return 1;
2259 }
Michal Vasko7ee5be22021-06-16 17:03:34 +02002260 LY_ARRAY_FOR(mod->parsed->includes, u) {
2261 if (mod->parsed->includes[u].submodule->groupings) {
2262 return 1;
2263 }
2264 }
2265
Michal Vasko87cfdba2022-02-22 14:13:45 +01002266 /* augments (adding nodes with leafrefs) */
2267 if (mod->parsed->augments) {
2268 return 1;
2269 }
2270 LY_ARRAY_FOR(mod->parsed->includes, u) {
2271 if (mod->parsed->includes[u].submodule->augments) {
2272 return 1;
2273 }
2274 }
2275
Michal Vasko7ee5be22021-06-16 17:03:34 +02002276 return 0;
2277}