blob: 0bde0eb947d401c835c347c95bf090ff0b1166ca [file] [log] [blame]
Radek Krejci86d106e2018-10-18 09:53:19 +02001/**
2 * @file tree_schema_helpers.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejcie7b95092019-05-15 11:03:07 +02004 * @brief Parsing and validation helper functions for schema trees
Radek Krejci86d106e2018-10-18 09:53:19 +02005 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Krejci535ea9f2020-05-29 16:01:05 +020014
15#define _GNU_SOURCE
Radek Krejci86d106e2018-10-18 09:53:19 +020016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020018#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010019#include <stddef.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <stdint.h>
Radek Krejci9ed7a192018-10-31 16:23:51 +010021#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020023#include <time.h>
24
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020026#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "context.h"
Radek Krejci77114102021-03-10 15:21:57 +010028#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020029#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010030#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020031#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "log.h"
Michal Vasko69730152020-10-09 16:30:07 +020033#include "parser_schema.h"
Michal Vasko962b6cd2020-12-08 10:07:49 +010034#include "schema_compile.h"
Michal Vasko79135ae2020-12-16 10:08:35 +010035#include "schema_features.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020036#include "set.h"
37#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010038#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020039#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020040#include "tree_schema_internal.h"
41
Radek Krejci85747952019-06-07 16:43:43 +020042LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +020043lysp_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 +020044{
45 struct lysp_import *i;
46
Michal Vasko69730152020-10-09 16:30:07 +020047 if (module_prefix && (&module_prefix != value) && !strcmp(module_prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010048 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value);
Radek Krejci86d106e2018-10-18 09:53:19 +020049 return LY_EEXIST;
50 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +010051 LY_ARRAY_FOR(imports, struct lysp_import, i) {
Michal Vasko69730152020-10-09 16:30:07 +020052 if (i->prefix && (&i->prefix != value) && !strcmp(i->prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010053 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +010054 return LY_EEXIST;
Radek Krejci86d106e2018-10-18 09:53:19 +020055 }
56 }
57 return LY_SUCCESS;
58}
59
60LY_ERR
Juraj Vijtiuk74dad9e2021-05-26 12:42:14 +020061lysp_check_date(struct lys_parser_ctx *ctx, const char *date, size_t date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +020062{
Radek Krejci86d106e2018-10-18 09:53:19 +020063 struct tm tm, tm_;
64 char *r;
65
Michal Vaskob36053d2020-03-26 15:49:30 +010066 LY_CHECK_ARG_RET(ctx ? PARSER_CTX(ctx) : NULL, date, LY_EINVAL);
67 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 +020068
Radek Krejcif13b87b2020-12-01 22:02:17 +010069 /* check format: YYYY-MM-DD */
Radek Krejci1deb5be2020-08-26 16:43:36 +020070 for (uint8_t i = 0; i < date_len; i++) {
Michal Vasko69730152020-10-09 16:30:07 +020071 if ((i == 4) || (i == 7)) {
Radek Krejci86d106e2018-10-18 09:53:19 +020072 if (date[i] != '-') {
73 goto error;
74 }
75 } else if (!isdigit(date[i])) {
76 goto error;
77 }
78 }
79
80 /* check content, e.g. 2018-02-31 */
81 memset(&tm, 0, sizeof tm);
82 r = strptime(date, "%Y-%m-%d", &tm);
Michal Vasko69730152020-10-09 16:30:07 +020083 if (!r || (r != &date[LY_REV_SIZE - 1])) {
Radek Krejci86d106e2018-10-18 09:53:19 +020084 goto error;
85 }
86 memcpy(&tm_, &tm, sizeof tm);
87 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
88 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
89 /* checking days is enough, since other errors
90 * have been checked by strptime() */
91 goto error;
92 }
93
94 return LY_SUCCESS;
95
96error:
Radek Krejcid33273d2018-10-25 14:55:52 +020097 if (stmt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010098 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt);
Radek Krejcid33273d2018-10-25 14:55:52 +020099 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200100 return LY_EINVAL;
101}
102
103void
104lysp_sort_revisions(struct lysp_revision *revs)
105{
Radek Krejci857189e2020-09-01 13:26:36 +0200106 LY_ARRAY_COUNT_TYPE i, r;
Radek Krejci86d106e2018-10-18 09:53:19 +0200107 struct lysp_revision rev;
108
Radek Krejcic7d13e32020-12-09 12:32:24 +0100109 for (i = 1, r = 0; i < LY_ARRAY_COUNT(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200110 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200111 r = i;
112 }
113 }
114
115 if (r) {
116 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200117 memcpy(&rev, &revs[0], sizeof rev);
118 memcpy(&revs[0], &revs[r], sizeof rev);
119 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200120 }
121}
Radek Krejci151a5b72018-10-19 14:21:44 +0200122
Radek Krejcibbe09a92018-11-08 09:36:54 +0100123static const struct lysp_tpdf *
124lysp_type_match(const char *name, struct lysp_node *node)
125{
Radek Krejci0fb28562018-12-13 15:17:37 +0100126 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200127 LY_ARRAY_COUNT_TYPE u;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100128
Radek Krejci0fb28562018-12-13 15:17:37 +0100129 typedefs = lysp_node_typedefs(node);
130 LY_ARRAY_FOR(typedefs, u) {
131 if (!strcmp(name, typedefs[u].name)) {
132 /* match */
133 return &typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100134 }
135 }
136
137 return NULL;
138}
139
aPiecek63e080d2021-06-29 13:53:28 +0200140static const struct lysp_node_grp *
141lysp_grouping_match(const char *name, struct lysp_node *node)
142{
143 const struct lysp_node_grp *groupings, *grp_iter;
144
145 groupings = lysp_node_groupings(node);
146 LY_LIST_FOR(groupings, grp_iter) {
147 if (!strcmp(name, grp_iter->name)) {
148 /* match */
149 return grp_iter;
150 }
151 }
152
153 return NULL;
154}
155
Radek Krejci4f28eda2018-11-12 11:46:16 +0100156static LY_DATA_TYPE
157lysp_type_str2builtin(const char *name, size_t len)
158{
159 if (len >= 4) { /* otherwise it does not match any built-in type */
160 if (name[0] == 'b') {
161 if (name[1] == 'i') {
Michal Vasko69730152020-10-09 16:30:07 +0200162 if ((len == 6) && !strncmp(&name[2], "nary", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100163 return LY_TYPE_BINARY;
Michal Vasko69730152020-10-09 16:30:07 +0200164 } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100165 return LY_TYPE_BITS;
166 }
Michal Vasko69730152020-10-09 16:30:07 +0200167 } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100168 return LY_TYPE_BOOL;
169 }
170 } else if (name[0] == 'd') {
Michal Vasko69730152020-10-09 16:30:07 +0200171 if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100172 return LY_TYPE_DEC64;
173 }
174 } else if (name[0] == 'e') {
Michal Vasko69730152020-10-09 16:30:07 +0200175 if ((len == 5) && !strncmp(&name[1], "mpty", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100176 return LY_TYPE_EMPTY;
Michal Vasko69730152020-10-09 16:30:07 +0200177 } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100178 return LY_TYPE_ENUM;
179 }
180 } else if (name[0] == 'i') {
181 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200182 if ((len == 4) && !strncmp(&name[2], "t8", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100183 return LY_TYPE_INT8;
184 } else if (len == 5) {
185 if (!strncmp(&name[2], "t16", 3)) {
186 return LY_TYPE_INT16;
187 } else if (!strncmp(&name[2], "t32", 3)) {
188 return LY_TYPE_INT32;
189 } else if (!strncmp(&name[2], "t64", 3)) {
190 return LY_TYPE_INT64;
191 }
Michal Vasko69730152020-10-09 16:30:07 +0200192 } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100193 return LY_TYPE_INST;
194 }
Michal Vasko69730152020-10-09 16:30:07 +0200195 } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100196 return LY_TYPE_IDENT;
197 }
198 } else if (name[0] == 'l') {
Michal Vasko69730152020-10-09 16:30:07 +0200199 if ((len == 7) && !strncmp(&name[1], "eafref", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100200 return LY_TYPE_LEAFREF;
201 }
202 } else if (name[0] == 's') {
Michal Vasko69730152020-10-09 16:30:07 +0200203 if ((len == 6) && !strncmp(&name[1], "tring", 5)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100204 return LY_TYPE_STRING;
205 }
206 } else if (name[0] == 'u') {
207 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200208 if ((len == 5) && !strncmp(&name[2], "ion", 3)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100209 return LY_TYPE_UNION;
210 }
Michal Vasko69730152020-10-09 16:30:07 +0200211 } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) {
212 if ((len == 5) && (name[4] == '8')) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100213 return LY_TYPE_UINT8;
214 } else if (len == 6) {
215 if (!strncmp(&name[4], "16", 2)) {
216 return LY_TYPE_UINT16;
217 } else if (!strncmp(&name[4], "32", 2)) {
218 return LY_TYPE_UINT32;
219 } else if (!strncmp(&name[4], "64", 2)) {
220 return LY_TYPE_UINT64;
221 }
222 }
223 }
224 }
225 }
226
227 return LY_TYPE_UNKNOWN;
228}
229
Radek Krejcibbe09a92018-11-08 09:36:54 +0100230LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +0100231lysp_type_find(const char *id, struct lysp_node *start_node, const struct lysp_module *start_module,
232 LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100233{
234 const char *str, *name;
235 struct lysp_tpdf *typedefs;
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100236 const struct lys_module *mod;
Michal Vaskoa99b3572021-02-01 11:54:58 +0100237 const struct lysp_module *local_module;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200238 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100239
240 assert(id);
241 assert(start_module);
242 assert(tpdf);
243 assert(node);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100244
Radek Krejci4f28eda2018-11-12 11:46:16 +0100245 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100246 str = strchr(id, ':');
247 if (str) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200248 mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_VALUE_SCHEMA, (void *)start_module);
Michal Vaskoa99b3572021-02-01 11:54:58 +0100249 local_module = mod ? mod->parsed : NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100250 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100251 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100252 } else {
Michal Vaskoa99b3572021-02-01 11:54:58 +0100253 local_module = start_module;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100254 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100255
256 /* check for built-in types */
257 *type = lysp_type_str2builtin(name, strlen(name));
258 if (*type) {
259 *tpdf = NULL;
260 return LY_SUCCESS;
261 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100262 }
Michal Vaskoa99b3572021-02-01 11:54:58 +0100263 LY_CHECK_RET(!local_module, LY_ENOTFOUND);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100264
Michal Vaskoa99b3572021-02-01 11:54:58 +0100265 if (start_node && (local_module == start_module)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100266 /* search typedefs in parent's nodes */
267 *node = start_node;
268 while (*node) {
269 *tpdf = lysp_type_match(name, *node);
270 if (*tpdf) {
271 /* match */
272 return LY_SUCCESS;
273 }
274 *node = (*node)->parent;
275 }
276 }
277
Michal Vasko915e5442021-06-08 14:59:21 +0200278 /* go to main module if in submodule */
279 local_module = local_module->mod->parsed;
280
Radek Krejcibbe09a92018-11-08 09:36:54 +0100281 /* search in top-level typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100282 if (local_module->typedefs) {
283 LY_ARRAY_FOR(local_module->typedefs, u) {
284 if (!strcmp(name, local_module->typedefs[u].name)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100285 /* match */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100286 *tpdf = &local_module->typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100287 return LY_SUCCESS;
288 }
289 }
290 }
291
Michal Vasko915e5442021-06-08 14:59:21 +0200292 /* search in all submodules' typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100293 LY_ARRAY_FOR(local_module->includes, u) {
294 typedefs = local_module->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100295 LY_ARRAY_FOR(typedefs, v) {
296 if (!strcmp(name, typedefs[v].name)) {
297 /* match */
298 *tpdf = &typedefs[v];
299 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100300 }
301 }
302 }
303
304 return LY_ENOTFOUND;
305}
306
David Sedlák6544c182019-07-12 13:17:33 +0200307LY_ERR
David Sedlák07869a52019-07-12 14:28:19 +0200308lysp_check_enum_name(struct lys_parser_ctx *ctx, const char *name, size_t name_len)
David Sedlák6544c182019-07-12 13:17:33 +0200309{
310 if (!name_len) {
311 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
312 return LY_EVALID;
313 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
314 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
Radek Krejci422afb12021-03-04 16:38:16 +0100315 (int)name_len, name);
David Sedlák6544c182019-07-12 13:17:33 +0200316 return LY_EVALID;
317 } else {
318 for (size_t u = 0; u < name_len; ++u) {
319 if (iscntrl(name[u])) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100320 LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
Radek Krejci422afb12021-03-04 16:38:16 +0100321 (int)name_len, name, u + 1);
David Sedlák6544c182019-07-12 13:17:33 +0200322 break;
323 }
324 }
325 }
326
327 return LY_SUCCESS;
328}
329
Michal Vaskob36053d2020-03-26 15:49:30 +0100330/**
aPiecekdc12b9f2021-06-25 10:55:47 +0200331 * @brief Insert @p name to hash table and if @p name has already
332 * been added, then log an error.
333 *
334 * This function is used to detect duplicate names.
335 *
336 * @param[in,out] ctx Context to log the error.
337 * @param[in,out] ht Hash table with top-level names.
338 * @param[in] name Inserted top-level identifier.
339 * @param[in] statement The name of the statement type from which
340 * @p name originated (eg typedef, feature, ...).
341 * @param[in] err_detail Optional error specification.
342 * @return LY_ERR, but LY_EEXIST is mapped to LY_EVALID.
343 */
344static LY_ERR
345lysp_check_dup_ht_insert(struct lys_parser_ctx *ctx, struct hash_table *ht,
346 const char *name, const char *statement, const char *err_detail)
347{
348 LY_ERR ret;
349 uint32_t hash;
350
351 hash = dict_hash(name, strlen(name));
352 ret = lyht_insert(ht, &name, hash, NULL);
353 if (ret == LY_EEXIST) {
354 if (err_detail) {
355 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT2, name, statement, err_detail);
356 } else {
357 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, name, statement);
358 }
359 ret = LY_EVALID;
360 }
361
362 return ret;
363}
364
365/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100366 * @brief Check name of a new type to avoid name collisions.
367 *
368 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
369 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
370 * @param[in] tpdf Typedef definition to check.
371 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
aPiecekc7594b72021-06-29 09:00:03 +0200372 * typedefs are checked, caller is supposed to free the table.
373 * @return LY_EVALID in case of collision, LY_SUCCESS otherwise.
Radek Krejcibbe09a92018-11-08 09:36:54 +0100374 */
375static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100376lysp_check_dup_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
aPieceke1fbd952021-06-29 08:12:55 +0200377 struct hash_table *tpdfs_global)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100378{
379 struct lysp_node *parent;
380 uint32_t hash;
381 size_t name_len;
382 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200383 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100384 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100385
386 assert(ctx);
387 assert(tpdf);
388
389 name = tpdf->name;
390 name_len = strlen(name);
391
Radek Krejci4f28eda2018-11-12 11:46:16 +0100392 if (lysp_type_str2builtin(name, name_len)) {
aPiecekc7594b72021-06-29 09:00:03 +0200393 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
394 "Duplicate identifier \"%s\" of typedef statement - name collision with a built-in type.", name);
395 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100396 }
397
398 /* check locally scoped typedefs (avoid name shadowing) */
399 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100400 typedefs = lysp_node_typedefs(node);
401 LY_ARRAY_FOR(typedefs, u) {
402 if (&typedefs[u] == tpdf) {
403 break;
404 }
405 if (!strcmp(name, typedefs[u].name)) {
aPiecekc7594b72021-06-29 09:00:03 +0200406 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
407 "Duplicate identifier \"%s\" of typedef statement - name collision with sibling type.", name);
408 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100409 }
410 }
411 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200412 for (parent = node->parent; parent; parent = parent->parent) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100413 if (lysp_type_match(name, parent)) {
aPiecekc7594b72021-06-29 09:00:03 +0200414 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
415 "Duplicate identifier \"%s\" of typedef statement - name collision with another scoped type.", name);
416 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100417 }
418 }
419 }
420
421 /* check collision with the top-level typedefs */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100422 if (node) {
aPiecekc7594b72021-06-29 09:00:03 +0200423 hash = dict_hash(name, name_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100424 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
aPiecekc7594b72021-06-29 09:00:03 +0200425 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
426 "Duplicate identifier \"%s\" of typedef statement - scoped type collide with a top-level type.", name);
427 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100428 }
429 } else {
aPiecekc7594b72021-06-29 09:00:03 +0200430 LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, tpdfs_global, name, "typedef",
431 "name collision with another top-level type"));
Radek Krejci3b1f9292018-11-08 10:58:35 +0100432 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
433 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
434 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100435 }
436
437 return LY_SUCCESS;
438}
439
Radek Krejci857189e2020-09-01 13:26:36 +0200440/**
441 * @brief Compare identifiers.
Michal Vasko62524a92021-02-26 10:08:50 +0100442 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200443 */
444static ly_bool
445lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Radek Krejcibbe09a92018-11-08 09:36:54 +0100446{
Michal Vasko11ac39a2021-07-23 12:46:56 +0200447 char *id1, *id2;
448
449 id1 = *(char **)val1;
450 id2 = *(char **)val2;
451
452 return strcmp(id1, id2) == 0 ? 1 : 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100453}
454
455LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100456lysp_check_dup_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100457{
458 struct hash_table *ids_global;
Radek Krejci0fb28562018-12-13 15:17:37 +0100459 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200460 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200461 uint32_t i;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100462 LY_ERR ret = LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100463
464 /* check name collisions - typedefs and groupings */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100465 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200466 LY_ARRAY_FOR(mod->typedefs, v) {
aPieceke1fbd952021-06-29 08:12:55 +0200467 ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100468 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100469 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200470 LY_ARRAY_FOR(mod->includes, v) {
471 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200472 ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100473 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci3b1f9292018-11-08 10:58:35 +0100474 }
475 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200476 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
477 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
478 LY_ARRAY_FOR(typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200479 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 +0100480 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100481 }
482 }
Michal Vasko405cc9e2020-12-01 12:01:27 +0100483
Radek Krejcibbe09a92018-11-08 09:36:54 +0100484cleanup:
485 lyht_free(ids_global);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100486 return ret;
487}
488
aPiecek63e080d2021-06-29 13:53:28 +0200489/**
490 * @brief Check name of a new grouping to avoid name collisions.
491 *
492 * @param[in] ctx Parser context, module where the grouping is being defined is taken from here.
493 * @param[in] node Schema node where the grouping is being defined, NULL in case of a top-level grouping.
494 * @param[in] grp Grouping definition to check.
495 * @param[in,out] grps_global Initialized hash table to store temporary data between calls. When the module's
496 * groupings are checked, caller is supposed to free the table.
497 * @return LY_EVALID in case of collision, LY_SUCCESS otherwise.
498 */
499static LY_ERR
500lysp_check_dup_grouping(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_node_grp *grp,
501 struct hash_table *grps_global)
502{
503 struct lysp_node *parent;
504 uint32_t hash;
505 size_t name_len;
506 const char *name;
507 const struct lysp_node_grp *groupings, *grp_iter;
508
509 assert(ctx);
510 assert(grp);
511
512 name = grp->name;
513 name_len = strlen(name);
514
515 /* check locally scoped groupings (avoid name shadowing) */
516 if (node) {
517 groupings = lysp_node_groupings(node);
518 LY_LIST_FOR(groupings, grp_iter) {
519 if (grp_iter == grp) {
520 break;
521 }
522 if (!strcmp(name, grp_iter->name)) {
523 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
524 "Duplicate identifier \"%s\" of grouping statement - name collision with sibling grouping.", name);
525 return LY_EVALID;
526 }
527 }
528 /* search grouping in parent's nodes */
529 for (parent = node->parent; parent; parent = parent->parent) {
530 if (lysp_grouping_match(name, parent)) {
531 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
532 "Duplicate identifier \"%s\" of grouping statement - name collision with another scoped grouping.", name);
533 return LY_EVALID;
534 }
535 }
536 }
537
538 /* check collision with the top-level groupings */
539 if (node) {
540 hash = dict_hash(name, name_len);
541 if (!lyht_find(grps_global, &name, hash, NULL)) {
542 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
543 "Duplicate identifier \"%s\" of grouping statement - scoped grouping collide with a top-level grouping.", name);
544 return LY_EVALID;
545 }
546 } else {
547 LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, grps_global, name, "grouping",
548 "name collision with another top-level grouping"));
549 }
550
551 return LY_SUCCESS;
552}
553
554LY_ERR
555lysp_check_dup_groupings(struct lys_parser_ctx *ctx, struct lysp_module *mod)
556{
557 struct hash_table *ids_global;
558 const struct lysp_node_grp *groupings, *grp_iter;
559 LY_ARRAY_COUNT_TYPE u;
560 uint32_t i;
561 LY_ERR ret = LY_SUCCESS;
562
563 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
564 LY_LIST_FOR(mod->groupings, grp_iter) {
565 ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global);
566 LY_CHECK_GOTO(ret, cleanup);
567 }
568 LY_ARRAY_FOR(mod->includes, u) {
569 LY_LIST_FOR(mod->includes[u].submodule->groupings, grp_iter) {
570 ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global);
571 LY_CHECK_GOTO(ret, cleanup);
572 }
573 }
574 for (i = 0; i < ctx->grps_nodes.count; ++i) {
575 groupings = lysp_node_groupings((struct lysp_node *)ctx->grps_nodes.objs[i]);
576 LY_LIST_FOR(groupings, grp_iter) {
577 ret = lysp_check_dup_grouping(ctx, (struct lysp_node *)ctx->grps_nodes.objs[i], grp_iter, ids_global);
578 LY_CHECK_GOTO(ret, cleanup);
579 }
580 }
581
582cleanup:
583 lyht_free(ids_global);
584 return ret;
585}
586
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100587static ly_bool
588ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
589{
590 void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
591
592 return ptr1 == ptr2 ? 1 : 0;
593}
594
595LY_ERR
596lysp_check_dup_features(struct lys_parser_ctx *ctx, struct lysp_module *mod)
597{
598 LY_ARRAY_COUNT_TYPE u;
599 struct hash_table *ht;
600 struct lysp_feature *f;
aPiecekdc12b9f2021-06-25 10:55:47 +0200601 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100602
aPiecekf6203072021-06-25 10:58:26 +0200603 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100604 LY_CHECK_RET(!ht, LY_EMEM);
605
606 /* add all module features into a hash table */
607 LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200608 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
609 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200610 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100611 }
612
613 /* add all submodule features into a hash table */
614 LY_ARRAY_FOR(mod->includes, u) {
615 LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200616 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
617 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200618 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100619 }
620 }
621
622cleanup:
623 lyht_free(ht);
624 return ret;
625}
626
627LY_ERR
628lysp_check_dup_identities(struct lys_parser_ctx *ctx, struct lysp_module *mod)
629{
630 LY_ARRAY_COUNT_TYPE u;
631 struct hash_table *ht;
632 struct lysp_ident *i;
aPiecekdc12b9f2021-06-25 10:55:47 +0200633 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100634
aPiecekf6203072021-06-25 10:58:26 +0200635 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100636 LY_CHECK_RET(!ht, LY_EMEM);
637
638 /* add all module identities into a hash table */
639 LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200640 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
641 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200642 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100643 }
644
645 /* add all submodule identities into a hash table */
646 LY_ARRAY_FOR(mod->includes, u) {
647 LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200648 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
649 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200650 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100651 }
652 }
653
654cleanup:
655 lyht_free(ht);
656 return ret;
657}
658
Radek Krejci9ed7a192018-10-31 16:23:51 +0100659struct lysp_load_module_check_data {
660 const char *name;
661 const char *revision;
662 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200663 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100664};
665
666static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100667lysp_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 +0100668{
669 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100670 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200671 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100672 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100673 struct lysp_revision *revs;
674
675 name = mod ? mod->mod->name : submod->name;
676 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200677 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100678
679 if (info->name) {
680 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100681 if (strcmp(info->name, name)) {
682 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100683 return LY_EINVAL;
684 }
685 }
686 if (info->revision) {
687 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100688 if (!revs || strcmp(info->revision, revs[0].date)) {
689 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200690 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100691 return LY_EINVAL;
692 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200693 } else if (!latest_revision) {
694 /* do not log, we just need to drop the schema and use the latest revision from the context */
695 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100696 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100697 if (submod) {
698 assert(info->submoduleof);
699
Radek Krejci9ed7a192018-10-31 16:23:51 +0100700 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200701 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100702 LOGVAL(ctx, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200703 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100704 return LY_EVALID;
705 }
706 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100707 if (submod->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100708 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100709 return LY_EVALID;
710 }
711 }
712 if (info->path) {
713 /* check that name and revision match filename */
714 filename = strrchr(info->path, '/');
715 if (!filename) {
716 filename = info->path;
717 } else {
718 filename++;
719 }
720 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100721 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100722 rev = strchr(filename, '@');
723 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100724 if (strncmp(filename, name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +0200725 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100726 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100727 }
728 /* revision */
729 if (rev) {
730 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100731 if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100732 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +0200733 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100734 }
735 }
736 }
737 return LY_SUCCESS;
738}
739
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200740/**
Michal Vasko4e205e82021-06-08 14:01:47 +0200741 * @brief Parse a (sub)module from a local file and add into the context.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200742 *
743 * This function does not check the presence of the (sub)module in context, it should be done before calling this function.
744 *
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200745 * @param[in] ctx libyang context where to work.
746 * @param[in] name Name of the (sub)module to load.
747 * @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 +0200748 * @param[in] main_ctx Parser context of the main module in case of loading submodule.
749 * @param[in] main_name Main module name in case of loading submodule.
750 * @param[in] required Module is required so error (even if the input file not found) are important. If 0, there is some
751 * 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 +0200752 * @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 +0200753 * @param[out] result Parsed YANG schema tree of the requested module (struct lys_module*) or submodule (struct lysp_submodule*).
754 * If it is a module, it is already in the context!
Michal Vasko4e205e82021-06-08 14:01:47 +0200755 * @return LY_SUCCESS on success.
Michal Vasko4e205e82021-06-08 14:01:47 +0200756 * @return LY_ERR on error.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200757 */
758static LY_ERR
Michal Vasko4e205e82021-06-08 14:01:47 +0200759lys_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 +0200760 const char *main_name, ly_bool required, struct ly_set *new_mods, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100761{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200762 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100763 char *filepath = NULL;
764 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100765 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100766 LY_ERR ret = LY_SUCCESS;
767 struct lysp_load_module_check_data check_data = {0};
768
Michal Vasko87f1cf02021-06-08 14:02:47 +0200769 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name,
770 revision, &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200771 if (!filepath) {
772 if (required) {
773 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200774 revision ? revision : "");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200775 }
776 return LY_ENOTFOUND;
777 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100778
779 LOGVRB("Loading schema from \"%s\" file.", filepath);
780
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200781 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200782 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +0200783 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100784 check_data.name = name;
785 check_data.revision = revision;
786 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800787 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200788 if (main_ctx) {
aPiecekc3e26142021-06-22 14:25:49 +0200789 ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data, new_mods,
Michal Vasko69730152020-10-09 16:30:07 +0200790 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200791 } else {
Michal Vaskodd992582021-06-10 14:34:57 +0200792 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 +0200793
794 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200795 ly_in_free(in, 1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200796 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100797
798 *result = mod;
799
800 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200801
Radek Krejci9ed7a192018-10-31 16:23:51 +0100802cleanup:
803 free(filepath);
804 return ret;
805}
806
aPiecek4725aea2021-07-28 10:18:33 +0200807/**
808 * @brief Load module from searchdirs or from callback.
809 *
810 * @param[in] ctx libyang context where to work.
811 * @param[in] name Name of module to load.
812 * @param[in] revision Revision of module to load.
Michal Vaskobdac23f2022-01-12 14:02:35 +0100813 * @param[in] mod_latest Module with the latest revision found in context, otherwise set to NULL.
814 * @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 +0200815 * @param[out] mod Loaded module.
816 * @return LY_SUCCESS on success.
817 * @return LY_ERR on error.
818 */
819static LY_ERR
820lys_parse_load_from_clb_or_file(struct ly_ctx *ctx, const char *name, const char *revision,
821 struct lys_module *mod_latest, struct ly_set *new_mods, struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200822{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100823 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200824 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200825
Radek Krejci9ed7a192018-10-31 16:23:51 +0100826 void (*module_data_free)(void *module_data, void *user_data) = NULL;
827 struct lysp_load_module_check_data check_data = {0};
Michal Vasko63f3d842020-07-08 10:10:14 +0200828 struct ly_in *in;
Radek Krejci086c7132018-10-26 15:29:04 +0200829
Michal Vaskobdac23f2022-01-12 14:02:35 +0100830 *mod = NULL;
831
832 if (mod_latest && (!ctx->imp_clb || (mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB)) &&
833 ((ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) || (mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) {
834 /* we are not able to find a newer revision */
835 return LY_SUCCESS;
836 }
837
aPiecek4725aea2021-07-28 10:18:33 +0200838 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
839search_clb:
Michal Vaskobdac23f2022-01-12 14:02:35 +0100840 /* check there is a callback and should be called */
841 if (ctx->imp_clb && (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB))) {
842 if (!ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, &format, &module_data, &module_data_free)) {
843 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
844 check_data.name = name;
845 check_data.revision = revision;
846 lys_parse_in(ctx, in, format, lysp_load_module_check, &check_data, new_mods, mod);
847 ly_in_free(in, 0);
848 if (module_data_free) {
849 module_data_free((void *)module_data, ctx->imp_clb_data);
850 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200851 }
Radek Krejci0af46292019-01-11 16:02:31 +0100852 }
Michal Vaskobdac23f2022-01-12 14:02:35 +0100853 if (*mod && !revision) {
854 /* we got the latest revision module from the callback */
855 (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB;
856 } else if (!*mod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
aPiecek4725aea2021-07-28 10:18:33 +0200857 goto search_file;
858 }
859 } else {
860search_file:
Michal Vaskobdac23f2022-01-12 14:02:35 +0100861 /* check we can use searchdirs and that we should */
862 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) &&
863 (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) {
aPiecek4725aea2021-07-28 10:18:33 +0200864 lys_parse_localfile(ctx, name, revision, NULL, NULL, mod_latest ? 0 : 1, new_mods, (void **)mod);
865 }
Michal Vaskobdac23f2022-01-12 14:02:35 +0100866 if (*mod && !revision) {
867 /* we got the latest revision module in the searchdirs */
868 (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB;
869 } else if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
aPiecek4725aea2021-07-28 10:18:33 +0200870 goto search_clb;
871 }
Radek Krejci086c7132018-10-26 15:29:04 +0200872 }
873
aPiecek4725aea2021-07-28 10:18:33 +0200874 return LY_SUCCESS;
875}
876
877/**
aPiecekd4911ee2021-07-30 07:40:24 +0200878 * @brief Get module without revision according to priorities.
879 *
880 * 1. Search for the module with LYS_MOD_IMPORTED_REV.
881 * 2. Search for the implemented module.
882 * 3. Search for the latest module in the context.
883 *
884 * @param[in] ctx libyang context where module is searched.
885 * @param[in] name Name of the searched module.
aPiecekd4911ee2021-07-30 07:40:24 +0200886 * @return Found module from context or NULL.
887 */
888static struct lys_module *
Michal Vaskobdac23f2022-01-12 14:02:35 +0100889lys_get_module_without_revision(struct ly_ctx *ctx, const char *name)
aPiecekd4911ee2021-07-30 07:40:24 +0200890{
891 struct lys_module *mod, *mod_impl;
892 uint32_t index;
893
aPiecekd4911ee2021-07-30 07:40:24 +0200894 /* Try to find module with LYS_MOD_IMPORTED_REV flag. */
895 index = 0;
896 while ((mod = ly_ctx_get_module_iter(ctx, &index))) {
897 if (!strcmp(mod->name, name) && (mod->latest_revision & LYS_MOD_IMPORTED_REV)) {
898 break;
899 }
900 }
901
902 /* Try to find the implemented module. */
903 mod_impl = ly_ctx_get_module_implemented(ctx, name);
904 if (mod && mod_impl) {
Michal Vaskobdac23f2022-01-12 14:02:35 +0100905 LOGVRB("Implemented module \"%s@%s\" is not used for import, revision \"%s\" is imported instead.",
aPiecekd4911ee2021-07-30 07:40:24 +0200906 mod_impl->name, mod_impl->revision, mod->revision);
907 return mod;
908 } else if (mod_impl) {
909 return mod_impl;
910 }
911
912 /* Try to find the latest module in the current context. */
913 mod = ly_ctx_get_module_latest(ctx, name);
aPiecekd4911ee2021-07-30 07:40:24 +0200914
915 return mod;
916}
917
918/**
aPiecek4725aea2021-07-28 10:18:33 +0200919 * @brief Check if a circular dependency exists between modules.
920 *
921 * @param[in] ctx libyang context for log an error.
922 * @param[in,out] mod Examined module which is set to NULL
923 * if the circular dependency is detected.
924 * @return LY_SUCCESS if no circular dependecy is detected,
925 * otherwise LY_EVALID.
926 */
927static LY_ERR
928lys_check_circular_dependency(struct ly_ctx *ctx, struct lys_module **mod)
929{
930 if ((*mod) && (*mod)->parsed->parsing) {
931 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", (*mod)->name);
Michal Vasko4e205e82021-06-08 14:01:47 +0200932 *mod = NULL;
933 return LY_EVALID;
Michal Vasko0550b762020-11-24 18:04:08 +0100934 }
Radek Krejci086c7132018-10-26 15:29:04 +0200935
aPiecek4725aea2021-07-28 10:18:33 +0200936 return LY_SUCCESS;
937}
938
939LY_ERR
940lys_parse_load(struct ly_ctx *ctx, const char *name, const char *revision, struct ly_set *new_mods,
941 struct lys_module **mod)
942{
943 struct lys_module *mod_latest = NULL;
944
945 assert(mod && new_mods);
946
Michal Vasko0550b762020-11-24 18:04:08 +0100947 /*
aPiecek4725aea2021-07-28 10:18:33 +0200948 * Try to get the module from the context.
Michal Vasko0550b762020-11-24 18:04:08 +0100949 */
aPiecek4725aea2021-07-28 10:18:33 +0200950 if (revision) {
951 /* Get the specific revision. */
952 *mod = ly_ctx_get_module(ctx, name, revision);
953 } else {
Michal Vaskobdac23f2022-01-12 14:02:35 +0100954 /* Get the requested module in a suitable revision in the context. */
955 *mod = lys_get_module_without_revision(ctx, name);
956 if (*mod && !(*mod)->implemented && !((*mod)->latest_revision & LYS_MOD_IMPORTED_REV)) {
aPiecek4725aea2021-07-28 10:18:33 +0200957 /* Let us now search with callback and searchpaths to check
958 * if there is newer revision outside the context.
959 */
960 mod_latest = *mod;
961 *mod = NULL;
962 }
963 }
964
Michal Vasko0550b762020-11-24 18:04:08 +0100965 if (!*mod) {
aPiecek4725aea2021-07-28 10:18:33 +0200966 /* No suitable module in the context, try to load it. */
Michal Vaskobdac23f2022-01-12 14:02:35 +0100967 LY_CHECK_RET(lys_parse_load_from_clb_or_file(ctx, name, revision, mod_latest, new_mods, mod));
aPiecek4725aea2021-07-28 10:18:33 +0200968 if (!*mod && !mod_latest) {
Michal Vasko4e205e82021-06-08 14:01:47 +0200969 LOGVAL(ctx, LYVE_REFERENCE, "Loading \"%s\" module failed.", name);
Michal Vasko0550b762020-11-24 18:04:08 +0100970 return LY_EVALID;
971 }
aPiecek4725aea2021-07-28 10:18:33 +0200972
973 /* Update the latest_revision flag - here we have selected the latest available schema,
974 * consider that even the callback provides correct latest revision.
975 */
976 if (!*mod) {
977 LOGVRB("Newer revision than \"%s@%s\" not found, using this as the latest revision.",
978 mod_latest->name, mod_latest->revision);
979 assert(mod_latest->latest_revision & LYS_MOD_LATEST_REV);
980 mod_latest->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS;
981 *mod = mod_latest;
982 } else if (*mod && !revision && ((*mod)->latest_revision & LYS_MOD_LATEST_REV)) {
983 (*mod)->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS;
984 }
Radek Krejci086c7132018-10-26 15:29:04 +0200985 }
Radek Krejci086c7132018-10-26 15:29:04 +0200986
aPiecek9f8c7e72021-07-28 12:01:56 +0200987 /* Checking the circular dependence of imported modules. */
988 LY_CHECK_RET(lys_check_circular_dependency(ctx, mod));
989
Radek Krejci086c7132018-10-26 15:29:04 +0200990 return LY_SUCCESS;
991}
992
993LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200994lysp_check_stringchar(struct lys_parser_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +0200995{
996 if (!is_yangutf8char(c)) {
997 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
998 return LY_EVALID;
999 }
1000 return LY_SUCCESS;
1001}
1002
1003LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001004lysp_check_identifierchar(struct lys_parser_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +02001005{
Michal Vasko69730152020-10-09 16:30:07 +02001006 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +02001007 if (!is_yangidentstartchar(c)) {
aPiecekc89b2242021-05-14 14:19:11 +02001008 if ((c < UCHAR_MAX) && isprint(c)) {
Michal Vasko7c769042021-03-25 12:20:49 +01001009 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
1010 } else {
1011 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character 0x%04x.", c);
1012 }
David Sedlák4a650532019-07-10 11:55:18 +02001013 return LY_EVALID;
1014 }
1015 if (prefix) {
1016 if (first) {
1017 (*prefix) = 0;
1018 } else {
1019 (*prefix) = 2;
1020 }
1021 }
Michal Vasko69730152020-10-09 16:30:07 +02001022 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +02001023 (*prefix) = 1;
1024 } else if (!is_yangidentchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001025 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +02001026 return LY_EVALID;
1027 }
1028
1029 return LY_SUCCESS;
1030}
1031
Radek Krejci771928a2021-01-19 13:42:36 +01001032/**
1033 * @brief Try to find the parsed submodule in main module for the given include record.
1034 *
1035 * @param[in] pctx main parser context
1036 * @param[in] inc The include record with missing parsed submodule. According to include info try to find
1037 * the corresponding parsed submodule in main module's includes.
1038 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
1039 * @return LY_ENOT - the parsed module was not found.
1040 * @return LY_EVALID - YANG rule violation
1041 */
1042static LY_ERR
Michal Vasko8a67eff2021-12-07 14:04:47 +01001043lysp_main_pmod_get_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +02001044{
Radek Krejci771928a2021-01-19 13:42:36 +01001045 LY_ARRAY_COUNT_TYPE i;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001046 struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed;
Michal Vasko69730152020-10-09 16:30:07 +02001047
Radek Krejci771928a2021-01-19 13:42:36 +01001048 LY_ARRAY_FOR(main_pmod->includes, i) {
1049 if (strcmp(main_pmod->includes[i].name, inc->name)) {
1050 continue;
1051 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001052
Radek Krejci771928a2021-01-19 13:42:36 +01001053 if (inc->rev[0] && strncmp(inc->rev, main_pmod->includes[i].rev, LY_REV_SIZE)) {
1054 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1055 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001056 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev,
Radek Krejci771928a2021-01-19 13:42:36 +01001057 main_pmod->includes[i].name, main_pmod->includes[i].rev, main_pmod->mod->name);
1058 return LY_EVALID;
1059 }
1060
1061 inc->submodule = main_pmod->includes[i].submodule;
1062 return inc->submodule ? LY_SUCCESS : LY_ENOT;
1063 }
1064
1065 if (main_pmod->version == LYS_VERSION_1_1) {
1066 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1067 "YANG 1.1 requires all submodules to be included from main module. "
1068 "But submodule \"%s\" includes submodule \"%s\" which is not included by main module \"%s\".",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001069 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->name, main_pmod->mod->name);
Radek Krejci771928a2021-01-19 13:42:36 +01001070 return LY_EVALID;
1071 } else {
1072 return LY_ENOT;
1073 }
1074}
1075
1076/**
Michal Vasko8a67eff2021-12-07 14:04:47 +01001077 * @brief Try to find the parsed submodule in currenlty parsed modules for the given include record.
1078 *
1079 * @param[in] pctx main parser context
1080 * @param[in] inc The include record with missing parsed submodule.
1081 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
1082 * @return LY_ENOT - the parsed module was not found.
1083 * @return LY_EVALID - YANG rule violation
1084 */
1085static LY_ERR
1086lysp_parsed_mods_get_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
1087{
1088 uint32_t i;
1089 struct lysp_submodule *submod;
1090
1091 for (i = 0; i < pctx->parsed_mods->count - 1; ++i) {
1092 submod = pctx->parsed_mods->objs[i];
1093 if (!submod->is_submod) {
1094 continue;
1095 }
1096
1097 if (strcmp(submod->name, inc->name)) {
1098 continue;
1099 }
1100
1101 if (inc->rev[0] && submod->revs && strncmp(inc->rev, submod->revs[0].date, LY_REV_SIZE)) {
1102 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1103 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
1104 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev,
1105 submod->name, submod->revs[0].date, PARSER_CUR_PMOD(pctx)->mod->name);
1106 return LY_EVALID;
1107 }
1108
1109 inc->submodule = submod;
1110 return LY_SUCCESS;
1111 }
1112
1113 return LY_ENOT;
1114}
1115
1116/**
Radek Krejci771928a2021-01-19 13:42:36 +01001117 * @brief Make the copy of the given include record into the main module.
1118 *
1119 * YANG 1.0 does not require the main module to include all the submodules. Therefore, parsing submodules can cause
1120 * reallocating and extending the includes array in the main module by the submodules included only in submodules.
1121 *
1122 * @param[in] pctx main parser context
1123 * @param[in] inc Include record to copy into main module taken from @p pctx.
1124 * @return LY_ERR value.
1125 */
1126static LY_ERR
1127lysp_inject_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
1128{
1129 LY_ARRAY_COUNT_TYPE i;
1130 struct lysp_include *inc_new, *inc_tofill = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001131 struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed;
Radek Krejci771928a2021-01-19 13:42:36 +01001132
1133 /* first, try to find the corresponding record with missing parsed submodule */
1134 LY_ARRAY_FOR(main_pmod->includes, i) {
1135 if (strcmp(main_pmod->includes[i].name, inc->name)) {
1136 continue;
1137 }
1138 inc_tofill = &main_pmod->includes[i];
1139 break;
1140 }
1141
1142 if (inc_tofill) {
1143 inc_tofill->submodule = inc->submodule;
1144 } else {
1145 LY_ARRAY_NEW_RET(PARSER_CTX(pctx), main_pmod->includes, inc_new, LY_EMEM);
1146
1147 inc_new->submodule = inc->submodule;
1148 DUP_STRING_RET(PARSER_CTX(pctx), inc->name, inc_new->name);
1149 DUP_STRING_RET(PARSER_CTX(pctx), inc->dsc, inc_new->dsc);
1150 DUP_STRING_RET(PARSER_CTX(pctx), inc->ref, inc_new->ref);
1151 /* TODO duplicate extensions */
1152 memcpy(inc_new->rev, inc->rev, LY_REV_SIZE);
1153 inc_new->injected = 1;
1154 }
1155 return LY_SUCCESS;
1156}
1157
1158LY_ERR
aPiecekc3e26142021-06-22 14:25:49 +02001159lysp_load_submodules(struct lys_parser_ctx *pctx, struct lysp_module *pmod, struct ly_set *new_mods)
Radek Krejci771928a2021-01-19 13:42:36 +01001160{
1161 LY_ARRAY_COUNT_TYPE u;
1162 struct ly_ctx *ctx = PARSER_CTX(pctx);
1163
1164 LY_ARRAY_FOR(pmod->includes, u) {
Michal Vasko8a67eff2021-12-07 14:04:47 +01001165 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci771928a2021-01-19 13:42:36 +01001166 struct lysp_submodule *submod = NULL;
1167 struct lysp_include *inc = &pmod->includes[u];
1168
1169 if (inc->submodule) {
1170 continue;
1171 }
1172
1173 if (pmod->is_submod) {
1174 /* try to find the submodule in the main module or its submodules */
Michal Vasko8a67eff2021-12-07 14:04:47 +01001175 ret = lysp_main_pmod_get_submodule(pctx, inc);
1176 LY_CHECK_RET(ret != LY_ENOT, ret);
Radek Krejci771928a2021-01-19 13:42:36 +01001177 }
1178
Michal Vasko8a67eff2021-12-07 14:04:47 +01001179 /* try to use currently parsed submodule */
1180 r = lysp_parsed_mods_get_submodule(pctx, inc);
1181 LY_CHECK_RET(r != LY_ENOT, r);
1182
Radek Krejci771928a2021-01-19 13:42:36 +01001183 /* submodule not present in the main module, get the input data and parse it */
1184 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcidf549132021-01-21 10:32:32 +01001185search_clb:
Radek Krejci771928a2021-01-19 13:42:36 +01001186 if (ctx->imp_clb) {
1187 const char *submodule_data = NULL;
1188 LYS_INFORMAT format = LYS_IN_UNKNOWN;
1189 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
1190 struct lysp_load_module_check_data check_data = {0};
1191 struct ly_in *in;
1192
Michal Vasko8a67eff2021-12-07 14:04:47 +01001193 if (ctx->imp_clb(PARSER_CUR_PMOD(pctx)->mod->name, NULL, inc->name,
Radek Krejci771928a2021-01-19 13:42:36 +01001194 inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
1195 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
1196 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
1197 check_data.name = inc->name;
1198 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001199 check_data.submoduleof = PARSER_CUR_PMOD(pctx)->mod->name;
aPiecekc3e26142021-06-22 14:25:49 +02001200 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, new_mods, &submod);
Radek Krejci771928a2021-01-19 13:42:36 +01001201
1202 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1203 * submodule's include into main module, where it is missing */
1204 inc = &pmod->includes[u];
1205
1206 ly_in_free(in, 0);
1207 if (submodule_data_free) {
1208 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
1209 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001210 }
1211 }
Radek Krejci771928a2021-01-19 13:42:36 +01001212 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1213 goto search_file;
1214 }
1215 } else {
Radek Krejcidf549132021-01-21 10:32:32 +01001216search_file:
Radek Krejci771928a2021-01-19 13:42:36 +01001217 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
1218 /* submodule was not received from the callback or there is no callback set */
Michal Vasko4e205e82021-06-08 14:01:47 +02001219 lys_parse_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, pctx,
Michal Vasko8a67eff2021-12-07 14:04:47 +01001220 PARSER_CUR_PMOD(pctx)->mod->name, 1, new_mods, (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001221
Radek Krejci771928a2021-01-19 13:42:36 +01001222 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1223 * submodule's include into main module, where it is missing */
1224 inc = &pmod->includes[u];
1225 }
1226 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1227 goto search_clb;
1228 }
1229 }
1230 if (submod) {
1231 if (!inc->rev[0] && (submod->latest_revision == 1)) {
1232 /* update the latest_revision flag - here we have selected the latest available schema,
1233 * consider that even the callback provides correct latest revision */
1234 submod->latest_revision = 2;
1235 }
1236
1237 inc->submodule = submod;
1238 if (ret == LY_ENOT) {
1239 /* the submodule include is not present in YANG 1.0 main module - add it there */
1240 LY_CHECK_RET(lysp_inject_submodule(pctx, &pmod->includes[u]));
1241 }
1242 }
1243 if (!inc->submodule) {
1244 LOGVAL(ctx, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name,
Michal Vasko8a67eff2021-12-07 14:04:47 +01001245 PARSER_CUR_PMOD(pctx)->is_submod ? ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name :
1246 PARSER_CUR_PMOD(pctx)->mod->name);
Radek Krejci771928a2021-01-19 13:42:36 +01001247 return LY_EVALID;
1248 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001249 }
1250
1251 return LY_SUCCESS;
1252}
1253
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001254LIBYANG_API_DEF const struct lysc_when *
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001255lysc_has_when(const struct lysc_node *node)
1256{
Radek Krejci9a3823e2021-01-27 20:26:46 +01001257 struct lysc_when **when;
1258
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001259 if (!node) {
1260 return NULL;
1261 }
1262
1263 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001264 when = lysc_node_when(node);
1265 if (when) {
1266 return when[0];
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001267 }
1268 node = node->parent;
1269 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1270
1271 return NULL;
1272}
1273
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001274LIBYANG_API_DEF const struct lys_module *
Michal Vaskoef53c812021-10-13 10:21:03 +02001275lysc_owner_module(const struct lysc_node *node)
1276{
1277 if (!node) {
1278 return NULL;
1279 }
1280
1281 for ( ; node->parent; node = node->parent) {}
1282 return node->module;
1283}
1284
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001285LIBYANG_API_DEF const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001286lys_nodetype2str(uint16_t nodetype)
1287{
Michal Vaskod989ba02020-08-24 10:59:24 +02001288 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001289 case LYS_CONTAINER:
1290 return "container";
1291 case LYS_CHOICE:
1292 return "choice";
1293 case LYS_LEAF:
1294 return "leaf";
1295 case LYS_LEAFLIST:
1296 return "leaf-list";
1297 case LYS_LIST:
1298 return "list";
1299 case LYS_ANYXML:
1300 return "anyxml";
1301 case LYS_ANYDATA:
1302 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001303 case LYS_CASE:
1304 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001305 case LYS_RPC:
1306 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001307 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001308 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001309 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001310 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001311 case LYS_USES:
1312 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001313 default:
1314 return "unknown";
1315 }
1316}
1317
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001318LIBYANG_API_DEF enum ly_stmt
Radek Krejci39b7fc22021-02-26 23:29:18 +01001319lys_nodetype2stmt(uint16_t nodetype)
1320{
1321 switch (nodetype) {
1322 case LYS_CONTAINER:
1323 return LY_STMT_CONTAINER;
1324 case LYS_CHOICE:
1325 return LY_STMT_CHOICE;
1326 case LYS_LEAF:
1327 return LY_STMT_LEAF;
1328 case LYS_LEAFLIST:
1329 return LY_STMT_LEAF_LIST;
1330 case LYS_LIST:
1331 return LY_STMT_LIST;
1332 case LYS_ANYXML:
1333 return LY_STMT_ANYXML;
1334 case LYS_ANYDATA:
1335 return LY_STMT_ANYDATA;
1336 case LYS_CASE:
1337 return LY_STMT_CASE;
1338 case LYS_RPC:
1339 return LY_STMT_RPC;
1340 case LYS_ACTION:
1341 return LY_STMT_ACTION;
1342 case LYS_NOTIF:
1343 return LY_STMT_NOTIFICATION;
1344 case LYS_USES:
1345 return LY_STMT_USES;
1346 case LYS_INPUT:
1347 return LY_STMT_INPUT;
1348 case LYS_OUTPUT:
1349 return LY_STMT_OUTPUT;
1350 default:
1351 return LY_STMT_NONE;
1352 }
1353}
1354
Radek Krejci693262f2019-04-29 15:23:20 +02001355const char *
1356lys_datatype2str(LY_DATA_TYPE basetype)
1357{
Michal Vaskod989ba02020-08-24 10:59:24 +02001358 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001359 case LY_TYPE_BINARY:
1360 return "binary";
1361 case LY_TYPE_UINT8:
1362 return "uint8";
1363 case LY_TYPE_UINT16:
1364 return "uint16";
1365 case LY_TYPE_UINT32:
1366 return "uint32";
1367 case LY_TYPE_UINT64:
1368 return "uint64";
1369 case LY_TYPE_STRING:
1370 return "string";
1371 case LY_TYPE_BITS:
1372 return "bits";
1373 case LY_TYPE_BOOL:
1374 return "boolean";
1375 case LY_TYPE_DEC64:
1376 return "decimal64";
1377 case LY_TYPE_EMPTY:
1378 return "empty";
1379 case LY_TYPE_ENUM:
1380 return "enumeration";
1381 case LY_TYPE_IDENT:
1382 return "identityref";
1383 case LY_TYPE_INST:
1384 return "instance-identifier";
1385 case LY_TYPE_LEAFREF:
1386 return "leafref";
1387 case LY_TYPE_UNION:
1388 return "union";
1389 case LY_TYPE_INT8:
1390 return "int8";
1391 case LY_TYPE_INT16:
1392 return "int16";
1393 case LY_TYPE_INT32:
1394 return "int32";
1395 case LY_TYPE_INT64:
1396 return "int64";
1397 default:
1398 return "unknown";
1399 }
1400}
1401
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001402LIBYANG_API_DEF const struct lysp_tpdf *
Radek Krejci056d0a82018-12-06 16:57:25 +01001403lysp_node_typedefs(const struct lysp_node *node)
1404{
Radek Krejci0fb28562018-12-13 15:17:37 +01001405 switch (node->nodetype) {
1406 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001407 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001408 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001409 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001410 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001411 return ((struct lysp_node_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001412 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001413 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001414 return ((struct lysp_node_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001415 case LYS_INPUT:
1416 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001417 return ((struct lysp_node_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001418 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001419 return ((struct lysp_node_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001420 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001421 return NULL;
1422 }
1423}
1424
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001425LIBYANG_API_DEF const struct lysp_node_grp *
Radek Krejci53ea6152018-12-13 15:21:15 +01001426lysp_node_groupings(const struct lysp_node *node)
1427{
1428 switch (node->nodetype) {
1429 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001430 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001431 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001432 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001433 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001434 return ((struct lysp_node_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001435 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001436 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001437 return ((struct lysp_node_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001438 case LYS_INPUT:
1439 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001440 return ((struct lysp_node_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001441 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001442 return ((struct lysp_node_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001443 default:
1444 return NULL;
1445 }
1446}
1447
Radek Krejci2a9fc652021-01-22 17:44:34 +01001448struct lysp_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001449lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001450{
1451 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001452
Radek Krejcibbe09a92018-11-08 09:36:54 +01001453 switch (node->nodetype) {
1454 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001455 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001456 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001457 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001458 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001459 return &((struct lysp_node_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001460 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001461 return &((struct lysp_node_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001462 default:
1463 return NULL;
1464 }
1465}
1466
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001467LIBYANG_API_DEF const struct lysp_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001468lysp_node_actions(const struct lysp_node *node)
1469{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001470 struct lysp_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001471
Michal Vasko22df3f02020-08-24 13:29:22 +02001472 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001473 if (actions) {
1474 return *actions;
1475 } else {
1476 return NULL;
1477 }
1478}
1479
Radek Krejci2a9fc652021-01-22 17:44:34 +01001480struct lysp_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001481lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001482{
1483 assert(node);
1484 switch (node->nodetype) {
1485 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001486 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001487 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001488 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001489 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001490 return &((struct lysp_node_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001491 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001492 return &((struct lysp_node_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001493 default:
1494 return NULL;
1495 }
1496}
1497
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001498LIBYANG_API_DEF const struct lysp_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001499lysp_node_notifs(const struct lysp_node *node)
1500{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001501 struct lysp_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001502
Michal Vasko22df3f02020-08-24 13:29:22 +02001503 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001504 if (notifs) {
1505 return *notifs;
1506 } else {
1507 return NULL;
1508 }
1509}
1510
Radek Krejcibbe09a92018-11-08 09:36:54 +01001511struct lysp_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001512lysp_node_child_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001513{
1514 assert(node);
1515 switch (node->nodetype) {
1516 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001517 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001518 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001519 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001520 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001521 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001522 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001523 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001524 case LYS_GROUPING:
Radek Krejci01180ac2021-01-27 08:48:22 +01001525 return &((struct lysp_node_grp *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001526 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001527 return &((struct lysp_node_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001528 case LYS_INPUT:
1529 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001530 return &((struct lysp_node_action_inout *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001531 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001532 return &((struct lysp_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001533 default:
1534 return NULL;
1535 }
1536}
1537
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001538LIBYANG_API_DEF const struct lysp_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001539lysp_node_child(const struct lysp_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01001540{
Michal Vasko544e58a2021-01-28 14:33:41 +01001541 struct lysp_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001542
1543 if (!node) {
1544 return NULL;
1545 }
1546
Michal Vasko544e58a2021-01-28 14:33:41 +01001547 child = lysp_node_child_p((struct lysp_node *)node);
1548 if (child) {
1549 return *child;
Radek Krejci056d0a82018-12-06 16:57:25 +01001550 } else {
1551 return NULL;
1552 }
1553}
1554
Radek Krejci9a3823e2021-01-27 20:26:46 +01001555struct lysp_restr **
1556lysp_node_musts_p(const struct lysp_node *node)
1557{
1558 if (!node) {
1559 return NULL;
1560 }
1561
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001562 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001563 case LYS_CONTAINER:
1564 return &((struct lysp_node_container *)node)->musts;
1565 case LYS_LEAF:
1566 return &((struct lysp_node_leaf *)node)->musts;
1567 case LYS_LEAFLIST:
1568 return &((struct lysp_node_leaflist *)node)->musts;
1569 case LYS_LIST:
1570 return &((struct lysp_node_list *)node)->musts;
1571 case LYS_ANYXML:
1572 case LYS_ANYDATA:
1573 return &((struct lysp_node_anydata *)node)->musts;
1574 case LYS_NOTIF:
1575 return &((struct lysp_node_notif *)node)->musts;
1576 case LYS_INPUT:
1577 case LYS_OUTPUT:
1578 return &((struct lysp_node_action_inout *)node)->musts;
1579 default:
1580 return NULL;
1581 }
1582}
1583
1584struct lysp_restr *
1585lysp_node_musts(const struct lysp_node *node)
1586{
1587 struct lysp_restr **musts;
1588
1589 musts = lysp_node_musts_p(node);
1590 if (musts) {
1591 return *musts;
1592 } else {
1593 return NULL;
1594 }
1595}
1596
1597struct lysp_when **
1598lysp_node_when_p(const struct lysp_node *node)
1599{
1600 if (!node) {
1601 return NULL;
1602 }
1603
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001604 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001605 case LYS_CONTAINER:
1606 return &((struct lysp_node_container *)node)->when;
1607 case LYS_CHOICE:
1608 return &((struct lysp_node_choice *)node)->when;
1609 case LYS_LEAF:
1610 return &((struct lysp_node_leaf *)node)->when;
1611 case LYS_LEAFLIST:
1612 return &((struct lysp_node_leaflist *)node)->when;
1613 case LYS_LIST:
1614 return &((struct lysp_node_list *)node)->when;
1615 case LYS_ANYXML:
1616 case LYS_ANYDATA:
1617 return &((struct lysp_node_anydata *)node)->when;
1618 case LYS_CASE:
1619 return &((struct lysp_node_case *)node)->when;
1620 case LYS_USES:
1621 return &((struct lysp_node_uses *)node)->when;
1622 case LYS_AUGMENT:
1623 return &((struct lysp_node_augment *)node)->when;
1624 default:
1625 return NULL;
1626 }
1627}
1628
1629struct lysp_when *
1630lysp_node_when(const struct lysp_node *node)
1631{
1632 struct lysp_when **when;
1633
1634 when = lysp_node_when_p(node);
1635 if (when) {
1636 return *when;
1637 } else {
1638 return NULL;
1639 }
1640}
1641
Radek Krejci2a9fc652021-01-22 17:44:34 +01001642struct lysc_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001643lysc_node_actions_p(struct lysc_node *node)
1644{
1645 assert(node);
1646 switch (node->nodetype) {
1647 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001648 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001649 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001650 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001651 default:
1652 return NULL;
1653 }
1654}
1655
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001656LIBYANG_API_DEF const struct lysc_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001657lysc_node_actions(const struct lysc_node *node)
1658{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001659 struct lysc_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001660
Michal Vasko22df3f02020-08-24 13:29:22 +02001661 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001662 if (actions) {
1663 return *actions;
1664 } else {
1665 return NULL;
1666 }
1667}
1668
Radek Krejci2a9fc652021-01-22 17:44:34 +01001669struct lysc_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001670lysc_node_notifs_p(struct lysc_node *node)
1671{
1672 assert(node);
1673 switch (node->nodetype) {
1674 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001675 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001676 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001677 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001678 default:
1679 return NULL;
1680 }
1681}
1682
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001683LIBYANG_API_DEF const struct lysc_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001684lysc_node_notifs(const struct lysc_node *node)
1685{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001686 struct lysc_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001687
Michal Vasko22df3f02020-08-24 13:29:22 +02001688 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001689 if (notifs) {
1690 return *notifs;
1691 } else {
1692 return NULL;
1693 }
1694}
1695
Radek Krejcibbe09a92018-11-08 09:36:54 +01001696struct lysc_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001697lysc_node_child_p(const struct lysc_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001698{
Michal Vasko544e58a2021-01-28 14:33:41 +01001699 assert(node && !(node->nodetype & (LYS_RPC | LYS_ACTION)));
1700
Radek Krejcibbe09a92018-11-08 09:36:54 +01001701 switch (node->nodetype) {
1702 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001703 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001704 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001705 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001706 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001707 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001708 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001709 return &((struct lysc_node_list *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001710 case LYS_INPUT:
1711 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001712 return &((struct lysc_node_action_inout *)node)->child;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001713 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001714 return &((struct lysc_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001715 default:
1716 return NULL;
1717 }
1718}
1719
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001720LIBYANG_API_DEF const struct lysc_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001721lysc_node_child(const struct lysc_node *node)
Radek Krejcia3045382018-11-22 14:30:31 +01001722{
Michal Vasko544e58a2021-01-28 14:33:41 +01001723 struct lysc_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001724
1725 if (!node) {
1726 return NULL;
1727 }
1728
Michal Vasko544e58a2021-01-28 14:33:41 +01001729 if (node->nodetype & (LYS_RPC | LYS_ACTION)) {
1730 return &((struct lysc_node_action *)node)->input.node;
Radek Krejcibe154442021-01-21 11:06:36 +01001731 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01001732 child = lysc_node_child_p(node);
1733 if (child) {
1734 return *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001735 }
Michal Vasko2a668712020-10-21 11:48:09 +02001736 }
Michal Vasko544e58a2021-01-28 14:33:41 +01001737
1738 return NULL;
Michal Vasko2a668712020-10-21 11:48:09 +02001739}
1740
Radek Krejci9a3823e2021-01-27 20:26:46 +01001741struct lysc_must **
1742lysc_node_musts_p(const struct lysc_node *node)
1743{
1744 if (!node) {
1745 return NULL;
1746 }
1747
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001748 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001749 case LYS_CONTAINER:
1750 return &((struct lysc_node_container *)node)->musts;
1751 case LYS_LEAF:
1752 return &((struct lysc_node_leaf *)node)->musts;
1753 case LYS_LEAFLIST:
1754 return &((struct lysc_node_leaflist *)node)->musts;
1755 case LYS_LIST:
1756 return &((struct lysc_node_list *)node)->musts;
1757 case LYS_ANYXML:
1758 case LYS_ANYDATA:
1759 return &((struct lysc_node_anydata *)node)->musts;
1760 case LYS_NOTIF:
1761 return &((struct lysc_node_notif *)node)->musts;
1762 case LYS_INPUT:
1763 case LYS_OUTPUT:
1764 return &((struct lysc_node_action_inout *)node)->musts;
1765 default:
1766 return NULL;
1767 }
1768}
1769
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001770LIBYANG_API_DEF struct lysc_must *
Radek Krejci9a3823e2021-01-27 20:26:46 +01001771lysc_node_musts(const struct lysc_node *node)
1772{
1773 struct lysc_must **must_p;
1774
1775 must_p = lysc_node_musts_p(node);
1776 if (must_p) {
1777 return *must_p;
1778 } else {
1779 return NULL;
1780 }
1781}
1782
1783struct lysc_when ***
1784lysc_node_when_p(const struct lysc_node *node)
1785{
1786 if (!node) {
1787 return NULL;
1788 }
1789
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001790 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001791 case LYS_CONTAINER:
1792 return &((struct lysc_node_container *)node)->when;
1793 case LYS_CHOICE:
1794 return &((struct lysc_node_choice *)node)->when;
1795 case LYS_LEAF:
1796 return &((struct lysc_node_leaf *)node)->when;
1797 case LYS_LEAFLIST:
1798 return &((struct lysc_node_leaflist *)node)->when;
1799 case LYS_LIST:
1800 return &((struct lysc_node_list *)node)->when;
1801 case LYS_ANYXML:
1802 case LYS_ANYDATA:
1803 return &((struct lysc_node_anydata *)node)->when;
1804 case LYS_CASE:
1805 return &((struct lysc_node_case *)node)->when;
1806 case LYS_NOTIF:
1807 return &((struct lysc_node_notif *)node)->when;
1808 case LYS_RPC:
1809 case LYS_ACTION:
1810 return &((struct lysc_node_action *)node)->when;
1811 default:
1812 return NULL;
1813 }
1814}
1815
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001816LIBYANG_API_DEF struct lysc_when **
Radek Krejci9a3823e2021-01-27 20:26:46 +01001817lysc_node_when(const struct lysc_node *node)
1818{
1819 struct lysc_when ***when_p;
1820
1821 when_p = lysc_node_when_p(node);
1822 if (when_p) {
1823 return *when_p;
1824 } else {
1825 return NULL;
1826 }
1827}
1828
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001829struct lys_module *
1830lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1831{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001832 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001833 if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) {
Michal Vasko69730152020-10-09 16:30:07 +02001834 return (struct lys_module *)ctx->list.objs[u];
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001835 }
1836 }
1837 return NULL;
1838}
1839
Radek Krejcid6b76452019-09-03 17:03:03 +02001840enum ly_stmt
Radek Krejcid54412f2020-12-17 20:25:35 +01001841lysp_match_kw(struct ly_in *in, uint64_t *indent)
David Sedlákc10e7902018-12-17 02:17:59 +01001842{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001843/**
Radek Krejcid54412f2020-12-17 20:25:35 +01001844 * @brief Move the input by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001845 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001846 *
1847 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001848 */
Radek Krejcid54412f2020-12-17 20:25:35 +01001849#define MOVE_IN(COUNT) \
1850 ly_in_skip(in, COUNT); \
1851 if (indent) { \
1852 (*indent)+=COUNT; \
1853 }
1854#define IF_KW(STR, LEN, STMT) \
1855 if (!strncmp(in->current, STR, LEN)) { \
1856 MOVE_IN(LEN); \
1857 (*kw)=STMT; \
1858 }
1859#define IF_KW_PREFIX(STR, LEN) \
1860 if (!strncmp(in->current, STR, LEN)) { \
1861 MOVE_IN(LEN);
1862#define IF_KW_PREFIX_END \
1863 }
David Sedlák572e7ab2019-06-04 16:01:58 +02001864
Michal Vasko63f3d842020-07-08 10:10:14 +02001865 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001866 enum ly_stmt result = LY_STMT_NONE;
1867 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001868 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001869 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001870 case 'a':
Radek Krejcid54412f2020-12-17 20:25:35 +01001871 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001872 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1873 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1874 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001875 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001876 IF_KW("data", 4, LY_STMT_ANYDATA)
1877 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001878 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001879 break;
1880 case 'b':
Radek Krejcid54412f2020-12-17 20:25:35 +01001881 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001882 IF_KW("ase", 3, LY_STMT_BASE)
1883 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1884 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001885 break;
1886 case 'c':
Radek Krejcid54412f2020-12-17 20:25:35 +01001887 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001888 IF_KW("ase", 3, LY_STMT_CASE)
1889 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001890 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001891 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001892 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001893 IF_KW("ct", 2, LY_STMT_CONTACT)
1894 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001895 IF_KW_PREFIX_END
1896 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001897 break;
1898 case 'd':
Radek Krejcid54412f2020-12-17 20:25:35 +01001899 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001900 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001901 IF_KW("fault", 5, LY_STMT_DEFAULT)
1902 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001903 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001904 IF_KW("e", 1, LY_STMT_DEVIATE)
1905 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001906 IF_KW_PREFIX_END
1907 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001908 break;
1909 case 'e':
Radek Krejcid54412f2020-12-17 20:25:35 +01001910 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001911 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001912 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001913 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1914 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001915 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001916 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001917 break;
1918 case 'f':
Radek Krejcid54412f2020-12-17 20:25:35 +01001919 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001920 IF_KW("eature", 6, LY_STMT_FEATURE)
1921 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001922 break;
1923 case 'g':
Radek Krejcid54412f2020-12-17 20:25:35 +01001924 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001925 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001926 break;
1927 case 'i':
Radek Krejcid54412f2020-12-17 20:25:35 +01001928 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001929 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1930 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1931 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001932 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001933 IF_KW("clude", 5, LY_STMT_INCLUDE)
1934 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001935 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001936 break;
1937 case 'k':
Radek Krejcid54412f2020-12-17 20:25:35 +01001938 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001939 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001940 break;
1941 case 'l':
Radek Krejcid54412f2020-12-17 20:25:35 +01001942 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001943 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001944 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1945 else IF_KW("af", 2, LY_STMT_LEAF)
1946 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001947 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001948 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001949 break;
1950 case 'm':
Radek Krejcid54412f2020-12-17 20:25:35 +01001951 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001952 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001953 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1954 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001955 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001956 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1957 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001958 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001959 IF_KW("ule", 3, LY_STMT_MODULE)
1960 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001961 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001962 break;
1963 case 'n':
Radek Krejcid54412f2020-12-17 20:25:35 +01001964 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001965 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1966 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001967 break;
1968 case 'o':
Radek Krejcid54412f2020-12-17 20:25:35 +01001969 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001970 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001971 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1972 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001973 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001974 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001975 break;
1976 case 'p':
Radek Krejcid54412f2020-12-17 20:25:35 +01001977 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001978 IF_KW("ath", 3, LY_STMT_PATH)
1979 else IF_KW("attern", 6, LY_STMT_PATTERN)
1980 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001981 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001982 IF_KW("fix", 3, LY_STMT_PREFIX)
1983 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001984 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001985 break;
1986 case 'r':
Radek Krejcid54412f2020-12-17 20:25:35 +01001987 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001988 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001989 else IF_KW_PREFIX("e", 1)
1990 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001991 IF_KW("erence", 6, LY_STMT_REFERENCE)
1992 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001993 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001994 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1995 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1996 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001997 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001998 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001999 break;
2000 case 's':
Radek Krejcid54412f2020-12-17 20:25:35 +01002001 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002002 IF_KW("tatus", 5, LY_STMT_STATUS)
2003 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02002004 break;
2005 case 't':
Radek Krejcid54412f2020-12-17 20:25:35 +01002006 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002007 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
2008 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02002009 break;
2010 case 'u':
Radek Krejcid54412f2020-12-17 20:25:35 +01002011 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02002012 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02002013 IF_KW("que", 3, LY_STMT_UNIQUE)
2014 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002015 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002016 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02002017 break;
2018 case 'v':
Radek Krejcid54412f2020-12-17 20:25:35 +01002019 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002020 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02002021 break;
2022 case 'w':
Radek Krejcid54412f2020-12-17 20:25:35 +01002023 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002024 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02002025 break;
2026 case 'y':
Radek Krejcid54412f2020-12-17 20:25:35 +01002027 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002028 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
2029 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02002030 break;
David Sedlák23a59a62018-10-26 13:08:02 +02002031 default:
Radek Krejcid54412f2020-12-17 20:25:35 +01002032 /* if indent is not NULL we are matching keyword from YANG data */
2033 if (indent) {
Michal Vasko63f3d842020-07-08 10:10:14 +02002034 if (in->current[0] == ';') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002035 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002036 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02002037 } else if (in->current[0] == '{') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002038 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002039 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002040 } else if (in->current[0] == '}') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002041 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002042 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02002043 }
2044 }
David Sedlák23a59a62018-10-26 13:08:02 +02002045 break;
2046 }
2047
Michal Vasko63f3d842020-07-08 10:10:14 +02002048 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02002049 /* the keyword is not terminated */
2050 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002051 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02002052 }
2053
David Sedlák1bccdfa2019-06-17 15:55:27 +02002054#undef IF_KW
2055#undef IF_KW_PREFIX
2056#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01002057#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02002058 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01002059
David Sedlák1bccdfa2019-06-17 15:55:27 +02002060 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02002061}
David Sedlákecf5eb82019-06-03 14:12:44 +02002062
Radek Krejci85ac8312021-03-03 20:21:33 +01002063LY_ERR
2064lysp_ext_find_definition(const struct ly_ctx *ctx, const struct lysp_ext_instance *ext, const struct lys_module **ext_mod,
2065 struct lysp_ext **ext_def)
2066{
Radek Krejci85ac8312021-03-03 20:21:33 +01002067 const char *tmp, *name, *prefix;
2068 size_t pref_len, name_len;
2069 LY_ARRAY_COUNT_TYPE v;
2070 const struct lys_module *mod = NULL;
2071
Radek Krejcicbb62422021-03-15 09:29:21 +01002072 assert(ext_def);
2073
Radek Krejci85ac8312021-03-03 20:21:33 +01002074 *ext_def = NULL;
2075 if (ext_mod) {
2076 *ext_mod = NULL;
2077 }
2078
Radek Krejci677abea2021-03-05 14:24:53 +01002079 /* parse the prefix, the nodeid was previously already parsed and checked */
Radek Krejci85ac8312021-03-03 20:21:33 +01002080 tmp = ext->name;
Radek Krejci677abea2021-03-05 14:24:53 +01002081 ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len);
Radek Krejci85ac8312021-03-03 20:21:33 +01002082
2083 /* get module where the extension definition should be placed */
2084 mod = ly_resolve_prefix(ctx, prefix, pref_len, ext->format, ext->prefix_data);
2085 if (!mod) {
Radek Krejci422afb12021-03-04 16:38:16 +01002086 LOGVAL(ctx, LYVE_REFERENCE, "Invalid prefix \"%.*s\" used for extension instance identifier.", (int)pref_len, prefix);
Radek Krejci85ac8312021-03-03 20:21:33 +01002087 return LY_EVALID;
2088 } else if (!mod->parsed->extensions) {
2089 LOGVAL(ctx, LYVE_REFERENCE, "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
2090 ext->name, mod->name);
2091 return LY_EVALID;
2092 }
2093
2094 /* find the parsed extension definition there */
2095 LY_ARRAY_FOR(mod->parsed->extensions, v) {
2096 if (!strcmp(name, mod->parsed->extensions[v].name)) {
2097 *ext_def = &mod->parsed->extensions[v];
2098 break;
2099 }
2100 }
2101
Radek Krejcicbb62422021-03-15 09:29:21 +01002102 if (!(*ext_def)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002103 LOGVAL(ctx, LYVE_REFERENCE, "Extension definition of extension instance \"%s\" not found.", ext->name);
2104 return LY_EVALID;
2105 }
2106
2107 if (ext_mod) {
2108 *ext_mod = mod;
2109 }
2110 return LY_SUCCESS;
2111}
2112
2113LY_ERR
2114lysp_ext_instance_resolve_argument(struct ly_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysp_ext *ext_def)
2115{
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002116 if (!ext_def->argname || ext_p->argument) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002117 /* nothing to do */
2118 return LY_SUCCESS;
2119 }
2120
Radek Krejci8df109d2021-04-23 12:19:08 +02002121 if (ext_p->format == LY_VALUE_XML) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002122 /* Schema was parsed from YIN and an argument is expected, ... */
2123 struct lysp_stmt *stmt = NULL;
2124
2125 if (ext_def->flags & LYS_YINELEM_TRUE) {
2126 /* ... argument was the first XML child element */
2127 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {}
2128 if (stmt) {
2129 const char *arg, *ext, *name_arg, *name_ext, *prefix_arg, *prefix_ext;
2130 size_t name_arg_len, name_ext_len, prefix_arg_len, prefix_ext_len;
2131
2132 stmt = ext_p->child;
2133
2134 arg = stmt->stmt;
2135 ly_parse_nodeid(&arg, &prefix_arg, &prefix_arg_len, &name_arg, &name_arg_len);
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002136 if (ly_strncmp(ext_def->argname, name_arg, name_arg_len)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002137 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" expects argument element \"%s\" as its first XML child, "
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002138 "but \"%.*s\" element found.", ext_p->name, ext_def->argname, (int)name_arg_len, name_arg);
Radek Krejci85ac8312021-03-03 20:21:33 +01002139 return LY_EVALID;
2140 }
2141
2142 /* check namespace - all the extension instances must be qualified and argument element is expected in the same
2143 * namespace. Do not check just prefixes, there can be different prefixes pointing to the same namespace */
2144 ext = ext_p->name; /* include prefix */
2145 ly_parse_nodeid(&ext, &prefix_ext, &prefix_ext_len, &name_ext, &name_ext_len);
2146
2147 if (ly_resolve_prefix(ctx, prefix_ext, prefix_ext_len, ext_p->format, ext_p->prefix_data) !=
2148 ly_resolve_prefix(ctx, prefix_arg, prefix_arg_len, stmt->format, stmt->prefix_data)) {
2149 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" element and its argument element \"%s\" are "
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002150 "expected in the same namespace, but they differ.", ext_p->name, ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002151 return LY_EVALID;
2152 }
2153 }
2154 } else {
2155 /* ... argument was one of the XML attributes which are represented as child stmt
2156 * with LYS_YIN_ATTR flag */
2157 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002158 if (!strcmp(stmt->stmt, ext_def->argname)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002159 /* this is the extension's argument */
2160 break;
2161 }
2162 }
2163 }
2164
2165 if (stmt) {
2166 LY_CHECK_RET(lydict_insert(ctx, stmt->arg, 0, &ext_p->argument));
2167 stmt->flags |= LYS_YIN_ARGUMENT;
2168 }
2169 }
2170
2171 if (!ext_p->argument) {
2172 /* missing extension's argument */
2173 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" misses argument %s\"%s\".",
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002174 ext_p->name, (ext_def->flags & LYS_YINELEM_TRUE) ? "element " : "", ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002175 return LY_EVALID;
2176 }
2177
2178 return LY_SUCCESS;
2179}
2180
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002181LY_ARRAY_COUNT_TYPE
Radek Krejcifc596f92021-02-26 22:40:26 +01002182lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, enum ly_stmt substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002183{
2184 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
2185
Michal Vaskod989ba02020-08-24 10:59:24 +02002186 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejciab430862021-03-02 20:13:40 +01002187 if (ext[index].parent_stmt == substmt) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02002188 return index;
2189 }
2190 }
2191
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002192 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002193}
2194
Michal Vasko62ed12d2020-05-21 10:08:25 +02002195const struct lysc_node *
Michal Vasko72244882021-01-12 15:21:05 +01002196lysc_data_node(const struct lysc_node *schema)
Michal Vasko62ed12d2020-05-21 10:08:25 +02002197{
2198 const struct lysc_node *parent;
2199
Michal Vasko72244882021-01-12 15:21:05 +01002200 parent = schema;
Radek Krejcidf549132021-01-21 10:32:32 +01002201 while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC |
2202 LYS_ACTION | LYS_NOTIF))) {
Radek Krejci7d95fbb2021-01-26 17:33:13 +01002203 parent = parent->parent;
Michal Vasko72244882021-01-12 15:21:05 +01002204 }
Michal Vasko62ed12d2020-05-21 10:08:25 +02002205
2206 return parent;
2207}
Michal Vaskof4258e12021-06-15 12:11:42 +02002208
2209ly_bool
2210lys_has_recompiled(const struct lys_module *mod)
2211{
2212 LY_ARRAY_COUNT_TYPE u;
2213
2214 if (LYSP_HAS_RECOMPILED(mod->parsed)) {
2215 return 1;
2216 }
2217
2218 LY_ARRAY_FOR(mod->parsed->includes, u) {
2219 if (LYSP_HAS_RECOMPILED(mod->parsed->includes[u].submodule)) {
2220 return 1;
2221 }
2222 }
2223
2224 return 0;
2225}
2226
2227ly_bool
2228lys_has_compiled(const struct lys_module *mod)
2229{
2230 LY_ARRAY_COUNT_TYPE u;
2231
2232 if (LYSP_HAS_COMPILED(mod->parsed)) {
2233 return 1;
2234 }
2235
2236 LY_ARRAY_FOR(mod->parsed->includes, u) {
2237 if (LYSP_HAS_COMPILED(mod->parsed->includes[u].submodule)) {
2238 return 1;
2239 }
2240 }
2241
2242 return 0;
2243}
Michal Vasko7ee5be22021-06-16 17:03:34 +02002244
2245ly_bool
2246lys_has_groupings(const struct lys_module *mod)
2247{
2248 LY_ARRAY_COUNT_TYPE u;
2249
2250 if (mod->parsed->groupings) {
2251 return 1;
2252 }
2253
2254 LY_ARRAY_FOR(mod->parsed->includes, u) {
2255 if (mod->parsed->includes[u].submodule->groupings) {
2256 return 1;
2257 }
2258 }
2259
2260 return 0;
2261}