blob: 90627ec01c01c0787475614b114ded1a1530f2b4 [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;
Michal Vasko26bbb272022-08-02 14:54:33 +02001190
Radek Krejci771928a2021-01-19 13:42:36 +01001191 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
1192 struct lysp_load_module_check_data check_data = {0};
1193 struct ly_in *in;
1194
Michal Vasko8a67eff2021-12-07 14:04:47 +01001195 if (ctx->imp_clb(PARSER_CUR_PMOD(pctx)->mod->name, NULL, inc->name,
Radek Krejci771928a2021-01-19 13:42:36 +01001196 inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
1197 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
1198 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
1199 check_data.name = inc->name;
1200 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001201 check_data.submoduleof = PARSER_CUR_PMOD(pctx)->mod->name;
aPiecekc3e26142021-06-22 14:25:49 +02001202 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, new_mods, &submod);
Radek Krejci771928a2021-01-19 13:42:36 +01001203
1204 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1205 * submodule's include into main module, where it is missing */
1206 inc = &pmod->includes[u];
1207
1208 ly_in_free(in, 0);
1209 if (submodule_data_free) {
1210 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
1211 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001212 }
1213 }
Radek Krejci771928a2021-01-19 13:42:36 +01001214 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1215 goto search_file;
1216 }
1217 } else {
Radek Krejcidf549132021-01-21 10:32:32 +01001218search_file:
Radek Krejci771928a2021-01-19 13:42:36 +01001219 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
1220 /* submodule was not received from the callback or there is no callback set */
Michal Vaskodd6d5a32022-07-14 13:54:58 +02001221 lys_parse_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, pctx->main_ctx,
1222 PARSER_CUR_PMOD(pctx->main_ctx)->mod->name, 1, new_mods, (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001223
Radek Krejci771928a2021-01-19 13:42:36 +01001224 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1225 * submodule's include into main module, where it is missing */
1226 inc = &pmod->includes[u];
1227 }
1228 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1229 goto search_clb;
1230 }
1231 }
1232 if (submod) {
1233 if (!inc->rev[0] && (submod->latest_revision == 1)) {
1234 /* update the latest_revision flag - here we have selected the latest available schema,
1235 * consider that even the callback provides correct latest revision */
1236 submod->latest_revision = 2;
1237 }
1238
1239 inc->submodule = submod;
1240 if (ret == LY_ENOT) {
1241 /* the submodule include is not present in YANG 1.0 main module - add it there */
1242 LY_CHECK_RET(lysp_inject_submodule(pctx, &pmod->includes[u]));
1243 }
1244 }
1245 if (!inc->submodule) {
1246 LOGVAL(ctx, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name,
Michal Vasko8a67eff2021-12-07 14:04:47 +01001247 PARSER_CUR_PMOD(pctx)->is_submod ? ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name :
1248 PARSER_CUR_PMOD(pctx)->mod->name);
Radek Krejci771928a2021-01-19 13:42:36 +01001249 return LY_EVALID;
1250 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001251 }
1252
1253 return LY_SUCCESS;
1254}
1255
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001256LIBYANG_API_DEF const struct lysc_when *
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001257lysc_has_when(const struct lysc_node *node)
1258{
Radek Krejci9a3823e2021-01-27 20:26:46 +01001259 struct lysc_when **when;
1260
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001261 if (!node) {
1262 return NULL;
1263 }
1264
1265 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001266 when = lysc_node_when(node);
1267 if (when) {
1268 return when[0];
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001269 }
1270 node = node->parent;
1271 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1272
1273 return NULL;
1274}
1275
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001276LIBYANG_API_DEF const struct lys_module *
Michal Vaskoef53c812021-10-13 10:21:03 +02001277lysc_owner_module(const struct lysc_node *node)
1278{
1279 if (!node) {
1280 return NULL;
1281 }
1282
1283 for ( ; node->parent; node = node->parent) {}
1284 return node->module;
1285}
1286
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001287LIBYANG_API_DEF const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001288lys_nodetype2str(uint16_t nodetype)
1289{
Michal Vaskod989ba02020-08-24 10:59:24 +02001290 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001291 case LYS_CONTAINER:
1292 return "container";
1293 case LYS_CHOICE:
1294 return "choice";
1295 case LYS_LEAF:
1296 return "leaf";
1297 case LYS_LEAFLIST:
1298 return "leaf-list";
1299 case LYS_LIST:
1300 return "list";
1301 case LYS_ANYXML:
1302 return "anyxml";
1303 case LYS_ANYDATA:
1304 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001305 case LYS_CASE:
1306 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001307 case LYS_RPC:
1308 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001309 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001310 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001311 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001312 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001313 case LYS_USES:
1314 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001315 default:
1316 return "unknown";
1317 }
1318}
1319
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001320LIBYANG_API_DEF enum ly_stmt
Radek Krejci39b7fc22021-02-26 23:29:18 +01001321lys_nodetype2stmt(uint16_t nodetype)
1322{
1323 switch (nodetype) {
1324 case LYS_CONTAINER:
1325 return LY_STMT_CONTAINER;
1326 case LYS_CHOICE:
1327 return LY_STMT_CHOICE;
1328 case LYS_LEAF:
1329 return LY_STMT_LEAF;
1330 case LYS_LEAFLIST:
1331 return LY_STMT_LEAF_LIST;
1332 case LYS_LIST:
1333 return LY_STMT_LIST;
1334 case LYS_ANYXML:
1335 return LY_STMT_ANYXML;
1336 case LYS_ANYDATA:
1337 return LY_STMT_ANYDATA;
1338 case LYS_CASE:
1339 return LY_STMT_CASE;
1340 case LYS_RPC:
1341 return LY_STMT_RPC;
1342 case LYS_ACTION:
1343 return LY_STMT_ACTION;
1344 case LYS_NOTIF:
1345 return LY_STMT_NOTIFICATION;
1346 case LYS_USES:
1347 return LY_STMT_USES;
1348 case LYS_INPUT:
1349 return LY_STMT_INPUT;
1350 case LYS_OUTPUT:
1351 return LY_STMT_OUTPUT;
1352 default:
1353 return LY_STMT_NONE;
1354 }
1355}
1356
Radek Krejci693262f2019-04-29 15:23:20 +02001357const char *
1358lys_datatype2str(LY_DATA_TYPE basetype)
1359{
Michal Vaskod989ba02020-08-24 10:59:24 +02001360 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001361 case LY_TYPE_BINARY:
1362 return "binary";
1363 case LY_TYPE_UINT8:
1364 return "uint8";
1365 case LY_TYPE_UINT16:
1366 return "uint16";
1367 case LY_TYPE_UINT32:
1368 return "uint32";
1369 case LY_TYPE_UINT64:
1370 return "uint64";
1371 case LY_TYPE_STRING:
1372 return "string";
1373 case LY_TYPE_BITS:
1374 return "bits";
1375 case LY_TYPE_BOOL:
1376 return "boolean";
1377 case LY_TYPE_DEC64:
1378 return "decimal64";
1379 case LY_TYPE_EMPTY:
1380 return "empty";
1381 case LY_TYPE_ENUM:
1382 return "enumeration";
1383 case LY_TYPE_IDENT:
1384 return "identityref";
1385 case LY_TYPE_INST:
1386 return "instance-identifier";
1387 case LY_TYPE_LEAFREF:
1388 return "leafref";
1389 case LY_TYPE_UNION:
1390 return "union";
1391 case LY_TYPE_INT8:
1392 return "int8";
1393 case LY_TYPE_INT16:
1394 return "int16";
1395 case LY_TYPE_INT32:
1396 return "int32";
1397 case LY_TYPE_INT64:
1398 return "int64";
1399 default:
1400 return "unknown";
1401 }
1402}
1403
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001404LIBYANG_API_DEF const struct lysp_tpdf *
Radek Krejci056d0a82018-12-06 16:57:25 +01001405lysp_node_typedefs(const struct lysp_node *node)
1406{
Radek Krejci0fb28562018-12-13 15:17:37 +01001407 switch (node->nodetype) {
1408 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001409 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001410 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001411 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001412 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001413 return ((struct lysp_node_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001414 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001415 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001416 return ((struct lysp_node_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001417 case LYS_INPUT:
1418 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001419 return ((struct lysp_node_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001420 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001421 return ((struct lysp_node_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001422 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001423 return NULL;
1424 }
1425}
1426
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001427LIBYANG_API_DEF const struct lysp_node_grp *
Radek Krejci53ea6152018-12-13 15:21:15 +01001428lysp_node_groupings(const struct lysp_node *node)
1429{
1430 switch (node->nodetype) {
1431 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001432 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001433 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001434 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001435 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001436 return ((struct lysp_node_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001437 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001438 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001439 return ((struct lysp_node_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001440 case LYS_INPUT:
1441 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001442 return ((struct lysp_node_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001443 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001444 return ((struct lysp_node_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001445 default:
1446 return NULL;
1447 }
1448}
1449
Radek Krejci2a9fc652021-01-22 17:44:34 +01001450struct lysp_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001451lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001452{
1453 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001454
Radek Krejcibbe09a92018-11-08 09:36:54 +01001455 switch (node->nodetype) {
1456 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001457 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001458 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001459 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001460 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001461 return &((struct lysp_node_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001462 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001463 return &((struct lysp_node_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001464 default:
1465 return NULL;
1466 }
1467}
1468
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001469LIBYANG_API_DEF const struct lysp_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001470lysp_node_actions(const struct lysp_node *node)
1471{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001472 struct lysp_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001473
Michal Vasko22df3f02020-08-24 13:29:22 +02001474 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001475 if (actions) {
1476 return *actions;
1477 } else {
1478 return NULL;
1479 }
1480}
1481
Radek Krejci2a9fc652021-01-22 17:44:34 +01001482struct lysp_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001483lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001484{
1485 assert(node);
1486 switch (node->nodetype) {
1487 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001488 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001489 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001490 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001491 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001492 return &((struct lysp_node_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001493 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001494 return &((struct lysp_node_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001495 default:
1496 return NULL;
1497 }
1498}
1499
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001500LIBYANG_API_DEF const struct lysp_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001501lysp_node_notifs(const struct lysp_node *node)
1502{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001503 struct lysp_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001504
Michal Vasko22df3f02020-08-24 13:29:22 +02001505 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001506 if (notifs) {
1507 return *notifs;
1508 } else {
1509 return NULL;
1510 }
1511}
1512
Radek Krejcibbe09a92018-11-08 09:36:54 +01001513struct lysp_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001514lysp_node_child_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001515{
1516 assert(node);
1517 switch (node->nodetype) {
1518 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001519 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001520 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001521 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001522 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001523 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001524 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001525 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001526 case LYS_GROUPING:
Radek Krejci01180ac2021-01-27 08:48:22 +01001527 return &((struct lysp_node_grp *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001528 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001529 return &((struct lysp_node_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001530 case LYS_INPUT:
1531 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001532 return &((struct lysp_node_action_inout *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001533 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001534 return &((struct lysp_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001535 default:
1536 return NULL;
1537 }
1538}
1539
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001540LIBYANG_API_DEF const struct lysp_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001541lysp_node_child(const struct lysp_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01001542{
Michal Vasko544e58a2021-01-28 14:33:41 +01001543 struct lysp_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001544
1545 if (!node) {
1546 return NULL;
1547 }
1548
Michal Vasko544e58a2021-01-28 14:33:41 +01001549 child = lysp_node_child_p((struct lysp_node *)node);
1550 if (child) {
1551 return *child;
Radek Krejci056d0a82018-12-06 16:57:25 +01001552 } else {
1553 return NULL;
1554 }
1555}
1556
Radek Krejci9a3823e2021-01-27 20:26:46 +01001557struct lysp_restr **
1558lysp_node_musts_p(const struct lysp_node *node)
1559{
1560 if (!node) {
1561 return NULL;
1562 }
1563
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001564 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001565 case LYS_CONTAINER:
1566 return &((struct lysp_node_container *)node)->musts;
1567 case LYS_LEAF:
1568 return &((struct lysp_node_leaf *)node)->musts;
1569 case LYS_LEAFLIST:
1570 return &((struct lysp_node_leaflist *)node)->musts;
1571 case LYS_LIST:
1572 return &((struct lysp_node_list *)node)->musts;
1573 case LYS_ANYXML:
1574 case LYS_ANYDATA:
1575 return &((struct lysp_node_anydata *)node)->musts;
1576 case LYS_NOTIF:
1577 return &((struct lysp_node_notif *)node)->musts;
1578 case LYS_INPUT:
1579 case LYS_OUTPUT:
1580 return &((struct lysp_node_action_inout *)node)->musts;
1581 default:
1582 return NULL;
1583 }
1584}
1585
1586struct lysp_restr *
1587lysp_node_musts(const struct lysp_node *node)
1588{
1589 struct lysp_restr **musts;
1590
1591 musts = lysp_node_musts_p(node);
1592 if (musts) {
1593 return *musts;
1594 } else {
1595 return NULL;
1596 }
1597}
1598
1599struct lysp_when **
1600lysp_node_when_p(const struct lysp_node *node)
1601{
1602 if (!node) {
1603 return NULL;
1604 }
1605
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001606 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001607 case LYS_CONTAINER:
1608 return &((struct lysp_node_container *)node)->when;
1609 case LYS_CHOICE:
1610 return &((struct lysp_node_choice *)node)->when;
1611 case LYS_LEAF:
1612 return &((struct lysp_node_leaf *)node)->when;
1613 case LYS_LEAFLIST:
1614 return &((struct lysp_node_leaflist *)node)->when;
1615 case LYS_LIST:
1616 return &((struct lysp_node_list *)node)->when;
1617 case LYS_ANYXML:
1618 case LYS_ANYDATA:
1619 return &((struct lysp_node_anydata *)node)->when;
1620 case LYS_CASE:
1621 return &((struct lysp_node_case *)node)->when;
1622 case LYS_USES:
1623 return &((struct lysp_node_uses *)node)->when;
1624 case LYS_AUGMENT:
1625 return &((struct lysp_node_augment *)node)->when;
1626 default:
1627 return NULL;
1628 }
1629}
1630
1631struct lysp_when *
1632lysp_node_when(const struct lysp_node *node)
1633{
1634 struct lysp_when **when;
1635
1636 when = lysp_node_when_p(node);
1637 if (when) {
1638 return *when;
1639 } else {
1640 return NULL;
1641 }
1642}
1643
Radek Krejci2a9fc652021-01-22 17:44:34 +01001644struct lysc_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001645lysc_node_actions_p(struct lysc_node *node)
1646{
1647 assert(node);
1648 switch (node->nodetype) {
1649 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001650 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001651 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001652 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001653 default:
1654 return NULL;
1655 }
1656}
1657
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001658LIBYANG_API_DEF const struct lysc_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001659lysc_node_actions(const struct lysc_node *node)
1660{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001661 struct lysc_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001662
Michal Vasko22df3f02020-08-24 13:29:22 +02001663 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001664 if (actions) {
1665 return *actions;
1666 } else {
1667 return NULL;
1668 }
1669}
1670
Radek Krejci2a9fc652021-01-22 17:44:34 +01001671struct lysc_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001672lysc_node_notifs_p(struct lysc_node *node)
1673{
1674 assert(node);
1675 switch (node->nodetype) {
1676 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001677 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001678 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001679 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001680 default:
1681 return NULL;
1682 }
1683}
1684
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001685LIBYANG_API_DEF const struct lysc_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001686lysc_node_notifs(const struct lysc_node *node)
1687{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001688 struct lysc_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001689
Michal Vasko22df3f02020-08-24 13:29:22 +02001690 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001691 if (notifs) {
1692 return *notifs;
1693 } else {
1694 return NULL;
1695 }
1696}
1697
Radek Krejcibbe09a92018-11-08 09:36:54 +01001698struct lysc_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001699lysc_node_child_p(const struct lysc_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001700{
Michal Vasko544e58a2021-01-28 14:33:41 +01001701 assert(node && !(node->nodetype & (LYS_RPC | LYS_ACTION)));
1702
Radek Krejcibbe09a92018-11-08 09:36:54 +01001703 switch (node->nodetype) {
1704 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001705 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001706 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001707 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001708 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001709 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001710 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001711 return &((struct lysc_node_list *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001712 case LYS_INPUT:
1713 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001714 return &((struct lysc_node_action_inout *)node)->child;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001715 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001716 return &((struct lysc_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001717 default:
1718 return NULL;
1719 }
1720}
1721
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001722LIBYANG_API_DEF const struct lysc_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001723lysc_node_child(const struct lysc_node *node)
Radek Krejcia3045382018-11-22 14:30:31 +01001724{
Michal Vasko544e58a2021-01-28 14:33:41 +01001725 struct lysc_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001726
1727 if (!node) {
1728 return NULL;
1729 }
1730
Michal Vasko544e58a2021-01-28 14:33:41 +01001731 if (node->nodetype & (LYS_RPC | LYS_ACTION)) {
1732 return &((struct lysc_node_action *)node)->input.node;
Radek Krejcibe154442021-01-21 11:06:36 +01001733 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01001734 child = lysc_node_child_p(node);
1735 if (child) {
1736 return *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001737 }
Michal Vasko2a668712020-10-21 11:48:09 +02001738 }
Michal Vasko544e58a2021-01-28 14:33:41 +01001739
1740 return NULL;
Michal Vasko2a668712020-10-21 11:48:09 +02001741}
1742
Radek Krejci9a3823e2021-01-27 20:26:46 +01001743struct lysc_must **
1744lysc_node_musts_p(const struct lysc_node *node)
1745{
1746 if (!node) {
1747 return NULL;
1748 }
1749
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001750 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001751 case LYS_CONTAINER:
1752 return &((struct lysc_node_container *)node)->musts;
1753 case LYS_LEAF:
1754 return &((struct lysc_node_leaf *)node)->musts;
1755 case LYS_LEAFLIST:
1756 return &((struct lysc_node_leaflist *)node)->musts;
1757 case LYS_LIST:
1758 return &((struct lysc_node_list *)node)->musts;
1759 case LYS_ANYXML:
1760 case LYS_ANYDATA:
1761 return &((struct lysc_node_anydata *)node)->musts;
1762 case LYS_NOTIF:
1763 return &((struct lysc_node_notif *)node)->musts;
1764 case LYS_INPUT:
1765 case LYS_OUTPUT:
1766 return &((struct lysc_node_action_inout *)node)->musts;
1767 default:
1768 return NULL;
1769 }
1770}
1771
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001772LIBYANG_API_DEF struct lysc_must *
Radek Krejci9a3823e2021-01-27 20:26:46 +01001773lysc_node_musts(const struct lysc_node *node)
1774{
1775 struct lysc_must **must_p;
1776
1777 must_p = lysc_node_musts_p(node);
1778 if (must_p) {
1779 return *must_p;
1780 } else {
1781 return NULL;
1782 }
1783}
1784
1785struct lysc_when ***
1786lysc_node_when_p(const struct lysc_node *node)
1787{
1788 if (!node) {
1789 return NULL;
1790 }
1791
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001792 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001793 case LYS_CONTAINER:
1794 return &((struct lysc_node_container *)node)->when;
1795 case LYS_CHOICE:
1796 return &((struct lysc_node_choice *)node)->when;
1797 case LYS_LEAF:
1798 return &((struct lysc_node_leaf *)node)->when;
1799 case LYS_LEAFLIST:
1800 return &((struct lysc_node_leaflist *)node)->when;
1801 case LYS_LIST:
1802 return &((struct lysc_node_list *)node)->when;
1803 case LYS_ANYXML:
1804 case LYS_ANYDATA:
1805 return &((struct lysc_node_anydata *)node)->when;
1806 case LYS_CASE:
1807 return &((struct lysc_node_case *)node)->when;
1808 case LYS_NOTIF:
1809 return &((struct lysc_node_notif *)node)->when;
1810 case LYS_RPC:
1811 case LYS_ACTION:
1812 return &((struct lysc_node_action *)node)->when;
1813 default:
1814 return NULL;
1815 }
1816}
1817
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001818LIBYANG_API_DEF struct lysc_when **
Radek Krejci9a3823e2021-01-27 20:26:46 +01001819lysc_node_when(const struct lysc_node *node)
1820{
1821 struct lysc_when ***when_p;
1822
1823 when_p = lysc_node_when_p(node);
1824 if (when_p) {
1825 return *when_p;
1826 } else {
1827 return NULL;
1828 }
1829}
1830
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001831struct lys_module *
1832lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1833{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001834 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001835 if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) {
Michal Vasko69730152020-10-09 16:30:07 +02001836 return (struct lys_module *)ctx->list.objs[u];
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001837 }
1838 }
1839 return NULL;
1840}
1841
Radek Krejcid6b76452019-09-03 17:03:03 +02001842enum ly_stmt
Radek Krejcid54412f2020-12-17 20:25:35 +01001843lysp_match_kw(struct ly_in *in, uint64_t *indent)
David Sedlákc10e7902018-12-17 02:17:59 +01001844{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001845/**
Radek Krejcid54412f2020-12-17 20:25:35 +01001846 * @brief Move the input by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001847 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001848 *
1849 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001850 */
Radek Krejcid54412f2020-12-17 20:25:35 +01001851#define MOVE_IN(COUNT) \
1852 ly_in_skip(in, COUNT); \
1853 if (indent) { \
1854 (*indent)+=COUNT; \
1855 }
1856#define IF_KW(STR, LEN, STMT) \
1857 if (!strncmp(in->current, STR, LEN)) { \
1858 MOVE_IN(LEN); \
1859 (*kw)=STMT; \
1860 }
1861#define IF_KW_PREFIX(STR, LEN) \
1862 if (!strncmp(in->current, STR, LEN)) { \
1863 MOVE_IN(LEN);
1864#define IF_KW_PREFIX_END \
1865 }
David Sedlák572e7ab2019-06-04 16:01:58 +02001866
Michal Vasko63f3d842020-07-08 10:10:14 +02001867 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001868 enum ly_stmt result = LY_STMT_NONE;
1869 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001870 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001871 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001872 case 'a':
Radek Krejcid54412f2020-12-17 20:25:35 +01001873 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001874 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1875 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1876 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001877 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001878 IF_KW("data", 4, LY_STMT_ANYDATA)
1879 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001880 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001881 break;
1882 case 'b':
Radek Krejcid54412f2020-12-17 20:25:35 +01001883 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001884 IF_KW("ase", 3, LY_STMT_BASE)
1885 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1886 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001887 break;
1888 case 'c':
Radek Krejcid54412f2020-12-17 20:25:35 +01001889 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001890 IF_KW("ase", 3, LY_STMT_CASE)
1891 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001892 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001893 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001894 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001895 IF_KW("ct", 2, LY_STMT_CONTACT)
1896 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001897 IF_KW_PREFIX_END
1898 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001899 break;
1900 case 'd':
Radek Krejcid54412f2020-12-17 20:25:35 +01001901 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001902 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001903 IF_KW("fault", 5, LY_STMT_DEFAULT)
1904 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001905 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001906 IF_KW("e", 1, LY_STMT_DEVIATE)
1907 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001908 IF_KW_PREFIX_END
1909 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001910 break;
1911 case 'e':
Radek Krejcid54412f2020-12-17 20:25:35 +01001912 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001913 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001914 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001915 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1916 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001917 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001918 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001919 break;
1920 case 'f':
Radek Krejcid54412f2020-12-17 20:25:35 +01001921 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001922 IF_KW("eature", 6, LY_STMT_FEATURE)
1923 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001924 break;
1925 case 'g':
Radek Krejcid54412f2020-12-17 20:25:35 +01001926 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001927 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001928 break;
1929 case 'i':
Radek Krejcid54412f2020-12-17 20:25:35 +01001930 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001931 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1932 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1933 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001934 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001935 IF_KW("clude", 5, LY_STMT_INCLUDE)
1936 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001937 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001938 break;
1939 case 'k':
Radek Krejcid54412f2020-12-17 20:25:35 +01001940 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001941 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001942 break;
1943 case 'l':
Radek Krejcid54412f2020-12-17 20:25:35 +01001944 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001945 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001946 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1947 else IF_KW("af", 2, LY_STMT_LEAF)
1948 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001949 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001950 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001951 break;
1952 case 'm':
Radek Krejcid54412f2020-12-17 20:25:35 +01001953 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001954 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001955 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1956 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001957 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001958 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1959 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001960 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001961 IF_KW("ule", 3, LY_STMT_MODULE)
1962 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001963 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001964 break;
1965 case 'n':
Radek Krejcid54412f2020-12-17 20:25:35 +01001966 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001967 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1968 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001969 break;
1970 case 'o':
Radek Krejcid54412f2020-12-17 20:25:35 +01001971 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001972 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001973 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1974 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001975 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001976 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001977 break;
1978 case 'p':
Radek Krejcid54412f2020-12-17 20:25:35 +01001979 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001980 IF_KW("ath", 3, LY_STMT_PATH)
1981 else IF_KW("attern", 6, LY_STMT_PATTERN)
1982 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001983 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001984 IF_KW("fix", 3, LY_STMT_PREFIX)
1985 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001986 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001987 break;
1988 case 'r':
Radek Krejcid54412f2020-12-17 20:25:35 +01001989 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001990 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001991 else IF_KW_PREFIX("e", 1)
1992 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001993 IF_KW("erence", 6, LY_STMT_REFERENCE)
1994 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001995 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001996 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1997 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1998 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001999 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002000 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02002001 break;
2002 case 's':
Radek Krejcid54412f2020-12-17 20:25:35 +01002003 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002004 IF_KW("tatus", 5, LY_STMT_STATUS)
2005 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02002006 break;
2007 case 't':
Radek Krejcid54412f2020-12-17 20:25:35 +01002008 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002009 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
2010 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02002011 break;
2012 case 'u':
Radek Krejcid54412f2020-12-17 20:25:35 +01002013 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02002014 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02002015 IF_KW("que", 3, LY_STMT_UNIQUE)
2016 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002017 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002018 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02002019 break;
2020 case 'v':
Radek Krejcid54412f2020-12-17 20:25:35 +01002021 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002022 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02002023 break;
2024 case 'w':
Radek Krejcid54412f2020-12-17 20:25:35 +01002025 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002026 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02002027 break;
2028 case 'y':
Radek Krejcid54412f2020-12-17 20:25:35 +01002029 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002030 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
2031 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02002032 break;
David Sedlák23a59a62018-10-26 13:08:02 +02002033 default:
Radek Krejcid54412f2020-12-17 20:25:35 +01002034 /* if indent is not NULL we are matching keyword from YANG data */
2035 if (indent) {
Michal Vasko63f3d842020-07-08 10:10:14 +02002036 if (in->current[0] == ';') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002037 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002038 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02002039 } else if (in->current[0] == '{') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002040 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002041 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002042 } else if (in->current[0] == '}') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002043 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002044 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02002045 }
2046 }
David Sedlák23a59a62018-10-26 13:08:02 +02002047 break;
2048 }
2049
Michal Vasko63f3d842020-07-08 10:10:14 +02002050 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02002051 /* the keyword is not terminated */
2052 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002053 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02002054 }
2055
David Sedlák1bccdfa2019-06-17 15:55:27 +02002056#undef IF_KW
2057#undef IF_KW_PREFIX
2058#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01002059#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02002060 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01002061
David Sedlák1bccdfa2019-06-17 15:55:27 +02002062 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02002063}
David Sedlákecf5eb82019-06-03 14:12:44 +02002064
Radek Krejci85ac8312021-03-03 20:21:33 +01002065LY_ERR
2066lysp_ext_find_definition(const struct ly_ctx *ctx, const struct lysp_ext_instance *ext, const struct lys_module **ext_mod,
2067 struct lysp_ext **ext_def)
2068{
Radek Krejci85ac8312021-03-03 20:21:33 +01002069 const char *tmp, *name, *prefix;
2070 size_t pref_len, name_len;
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002071 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci85ac8312021-03-03 20:21:33 +01002072 const struct lys_module *mod = NULL;
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002073 const struct lysp_submodule *submod;
Radek Krejci85ac8312021-03-03 20:21:33 +01002074
Radek Krejcicbb62422021-03-15 09:29:21 +01002075 assert(ext_def);
2076
Radek Krejci85ac8312021-03-03 20:21:33 +01002077 *ext_def = NULL;
2078 if (ext_mod) {
2079 *ext_mod = NULL;
2080 }
2081
Radek Krejci677abea2021-03-05 14:24:53 +01002082 /* parse the prefix, the nodeid was previously already parsed and checked */
Radek Krejci85ac8312021-03-03 20:21:33 +01002083 tmp = ext->name;
Radek Krejci677abea2021-03-05 14:24:53 +01002084 ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len);
Radek Krejci85ac8312021-03-03 20:21:33 +01002085
2086 /* get module where the extension definition should be placed */
2087 mod = ly_resolve_prefix(ctx, prefix, pref_len, ext->format, ext->prefix_data);
2088 if (!mod) {
Radek Krejci422afb12021-03-04 16:38:16 +01002089 LOGVAL(ctx, LYVE_REFERENCE, "Invalid prefix \"%.*s\" used for extension instance identifier.", (int)pref_len, prefix);
Radek Krejci85ac8312021-03-03 20:21:33 +01002090 return LY_EVALID;
2091 } else if (!mod->parsed->extensions) {
2092 LOGVAL(ctx, LYVE_REFERENCE, "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
2093 ext->name, mod->name);
2094 return LY_EVALID;
2095 }
2096
2097 /* find the parsed extension definition there */
2098 LY_ARRAY_FOR(mod->parsed->extensions, v) {
2099 if (!strcmp(name, mod->parsed->extensions[v].name)) {
2100 *ext_def = &mod->parsed->extensions[v];
2101 break;
2102 }
2103 }
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002104 if (!*ext_def) {
2105 LY_ARRAY_FOR(mod->parsed->includes, u) {
2106 submod = mod->parsed->includes[u].submodule;
2107 LY_ARRAY_FOR(submod->extensions, v) {
2108 if (!strcmp(name, submod->extensions[v].name)) {
2109 *ext_def = &submod->extensions[v];
2110 break;
2111 }
2112 }
2113 }
2114 }
Radek Krejci85ac8312021-03-03 20:21:33 +01002115
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002116 if (!*ext_def) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002117 LOGVAL(ctx, LYVE_REFERENCE, "Extension definition of extension instance \"%s\" not found.", ext->name);
2118 return LY_EVALID;
2119 }
2120
2121 if (ext_mod) {
2122 *ext_mod = mod;
2123 }
2124 return LY_SUCCESS;
2125}
2126
2127LY_ERR
2128lysp_ext_instance_resolve_argument(struct ly_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysp_ext *ext_def)
2129{
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002130 if (!ext_def->argname || ext_p->argument) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002131 /* nothing to do */
2132 return LY_SUCCESS;
2133 }
2134
Radek Krejci8df109d2021-04-23 12:19:08 +02002135 if (ext_p->format == LY_VALUE_XML) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002136 /* Schema was parsed from YIN and an argument is expected, ... */
2137 struct lysp_stmt *stmt = NULL;
2138
2139 if (ext_def->flags & LYS_YINELEM_TRUE) {
2140 /* ... argument was the first XML child element */
2141 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {}
2142 if (stmt) {
2143 const char *arg, *ext, *name_arg, *name_ext, *prefix_arg, *prefix_ext;
2144 size_t name_arg_len, name_ext_len, prefix_arg_len, prefix_ext_len;
2145
2146 stmt = ext_p->child;
2147
2148 arg = stmt->stmt;
2149 ly_parse_nodeid(&arg, &prefix_arg, &prefix_arg_len, &name_arg, &name_arg_len);
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002150 if (ly_strncmp(ext_def->argname, name_arg, name_arg_len)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002151 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" expects argument element \"%s\" as its first XML child, "
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002152 "but \"%.*s\" element found.", ext_p->name, ext_def->argname, (int)name_arg_len, name_arg);
Radek Krejci85ac8312021-03-03 20:21:33 +01002153 return LY_EVALID;
2154 }
2155
2156 /* check namespace - all the extension instances must be qualified and argument element is expected in the same
2157 * namespace. Do not check just prefixes, there can be different prefixes pointing to the same namespace */
2158 ext = ext_p->name; /* include prefix */
2159 ly_parse_nodeid(&ext, &prefix_ext, &prefix_ext_len, &name_ext, &name_ext_len);
2160
2161 if (ly_resolve_prefix(ctx, prefix_ext, prefix_ext_len, ext_p->format, ext_p->prefix_data) !=
2162 ly_resolve_prefix(ctx, prefix_arg, prefix_arg_len, stmt->format, stmt->prefix_data)) {
2163 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" element and its argument element \"%s\" are "
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002164 "expected in the same namespace, but they differ.", ext_p->name, ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002165 return LY_EVALID;
2166 }
2167 }
2168 } else {
2169 /* ... argument was one of the XML attributes which are represented as child stmt
2170 * with LYS_YIN_ATTR flag */
2171 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002172 if (!strcmp(stmt->stmt, ext_def->argname)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002173 /* this is the extension's argument */
2174 break;
2175 }
2176 }
2177 }
2178
2179 if (stmt) {
2180 LY_CHECK_RET(lydict_insert(ctx, stmt->arg, 0, &ext_p->argument));
2181 stmt->flags |= LYS_YIN_ARGUMENT;
2182 }
2183 }
2184
2185 if (!ext_p->argument) {
2186 /* missing extension's argument */
2187 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" misses argument %s\"%s\".",
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002188 ext_p->name, (ext_def->flags & LYS_YINELEM_TRUE) ? "element " : "", ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002189 return LY_EVALID;
2190 }
2191
2192 return LY_SUCCESS;
2193}
2194
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002195LY_ARRAY_COUNT_TYPE
Radek Krejcifc596f92021-02-26 22:40:26 +01002196lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, enum ly_stmt substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002197{
2198 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
2199
Michal Vaskod989ba02020-08-24 10:59:24 +02002200 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejciab430862021-03-02 20:13:40 +01002201 if (ext[index].parent_stmt == substmt) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02002202 return index;
2203 }
2204 }
2205
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002206 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002207}
2208
Michal Vaskof8ded142022-02-17 10:48:01 +01002209LIBYANG_API_DEF const struct lysc_node *
Michal Vasko72244882021-01-12 15:21:05 +01002210lysc_data_node(const struct lysc_node *schema)
Michal Vasko62ed12d2020-05-21 10:08:25 +02002211{
2212 const struct lysc_node *parent;
2213
Michal Vasko72244882021-01-12 15:21:05 +01002214 parent = schema;
Radek Krejcidf549132021-01-21 10:32:32 +01002215 while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC |
2216 LYS_ACTION | LYS_NOTIF))) {
Radek Krejci7d95fbb2021-01-26 17:33:13 +01002217 parent = parent->parent;
Michal Vasko72244882021-01-12 15:21:05 +01002218 }
Michal Vasko62ed12d2020-05-21 10:08:25 +02002219
2220 return parent;
2221}
Michal Vaskof4258e12021-06-15 12:11:42 +02002222
2223ly_bool
2224lys_has_recompiled(const struct lys_module *mod)
2225{
2226 LY_ARRAY_COUNT_TYPE u;
2227
2228 if (LYSP_HAS_RECOMPILED(mod->parsed)) {
2229 return 1;
2230 }
2231
2232 LY_ARRAY_FOR(mod->parsed->includes, u) {
2233 if (LYSP_HAS_RECOMPILED(mod->parsed->includes[u].submodule)) {
2234 return 1;
2235 }
2236 }
2237
2238 return 0;
2239}
2240
2241ly_bool
2242lys_has_compiled(const struct lys_module *mod)
2243{
2244 LY_ARRAY_COUNT_TYPE u;
2245
2246 if (LYSP_HAS_COMPILED(mod->parsed)) {
2247 return 1;
2248 }
2249
2250 LY_ARRAY_FOR(mod->parsed->includes, u) {
2251 if (LYSP_HAS_COMPILED(mod->parsed->includes[u].submodule)) {
2252 return 1;
2253 }
2254 }
2255
2256 return 0;
2257}
Michal Vasko7ee5be22021-06-16 17:03:34 +02002258
2259ly_bool
Michal Vasko87cfdba2022-02-22 14:13:45 +01002260lys_has_dep_mods(const struct lys_module *mod)
Michal Vasko7ee5be22021-06-16 17:03:34 +02002261{
2262 LY_ARRAY_COUNT_TYPE u;
2263
Michal Vasko87cfdba2022-02-22 14:13:45 +01002264 /* features */
2265 if (mod->parsed->features) {
Michal Vasko7ee5be22021-06-16 17:03:34 +02002266 return 1;
2267 }
2268
Michal Vasko87cfdba2022-02-22 14:13:45 +01002269 /* groupings */
2270 if (mod->parsed->groupings) {
2271 return 1;
2272 }
Michal Vasko7ee5be22021-06-16 17:03:34 +02002273 LY_ARRAY_FOR(mod->parsed->includes, u) {
2274 if (mod->parsed->includes[u].submodule->groupings) {
2275 return 1;
2276 }
2277 }
2278
Michal Vasko87cfdba2022-02-22 14:13:45 +01002279 /* augments (adding nodes with leafrefs) */
2280 if (mod->parsed->augments) {
2281 return 1;
2282 }
2283 LY_ARRAY_FOR(mod->parsed->includes, u) {
2284 if (mod->parsed->includes[u].submodule->augments) {
2285 return 1;
2286 }
2287 }
2288
Michal Vasko7ee5be22021-06-16 17:03:34 +02002289 return 0;
2290}