blob: dcd05a440a2541e4585f23fe990dd2075479b438 [file] [log] [blame]
Radek Krejci86d106e2018-10-18 09:53:19 +02001/**
Michal Vasko59892dd2022-05-13 11:02:30 +02002 * @file tree_schema_common.c
Radek Krejci86d106e2018-10-18 09:53:19 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko59892dd2022-05-13 11:02:30 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
5 * @brief Parsing and validation common functions for schema trees
Radek Krejci86d106e2018-10-18 09:53:19 +02006 *
Michal Vasko59892dd2022-05-13 11:02:30 +02007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejci86d106e2018-10-18 09:53:19 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
Radek Krejci535ea9f2020-05-29 16:01:05 +020015
16#define _GNU_SOURCE
Radek Krejci86d106e2018-10-18 09:53:19 +020017
Radek Krejcie7b95092019-05-15 11:03:07 +020018#include <assert.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020019#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010020#include <stddef.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <stdint.h>
Radek Krejci9ed7a192018-10-31 16:23:51 +010022#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020024#include <time.h>
25
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020027#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "context.h"
Radek Krejci77114102021-03-10 15:21:57 +010029#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010031#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020032#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010033#include "log.h"
Michal Vasko69730152020-10-09 16:30:07 +020034#include "parser_schema.h"
Michal Vasko962b6cd2020-12-08 10:07:49 +010035#include "schema_compile.h"
Michal Vasko79135ae2020-12-16 10:08:35 +010036#include "schema_features.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "set.h"
38#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010039#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020040#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020041#include "tree_schema_internal.h"
42
Radek Krejci85747952019-06-07 16:43:43 +020043LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +020044lysp_check_prefix(struct lysp_ctx *ctx, struct lysp_import *imports, const char *module_prefix, const char **value)
Radek Krejci86d106e2018-10-18 09:53:19 +020045{
46 struct lysp_import *i;
47
Michal Vasko69730152020-10-09 16:30:07 +020048 if (module_prefix && (&module_prefix != value) && !strcmp(module_prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010049 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value);
Radek Krejci86d106e2018-10-18 09:53:19 +020050 return LY_EEXIST;
51 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +010052 LY_ARRAY_FOR(imports, struct lysp_import, i) {
Michal Vasko69730152020-10-09 16:30:07 +020053 if (i->prefix && (&i->prefix != value) && !strcmp(i->prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010054 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +010055 return LY_EEXIST;
Radek Krejci86d106e2018-10-18 09:53:19 +020056 }
57 }
58 return LY_SUCCESS;
59}
60
61LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +020062lysp_check_date(struct lysp_ctx *ctx, const char *date, size_t date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +020063{
Radek Krejci86d106e2018-10-18 09:53:19 +020064 struct tm tm, tm_;
65 char *r;
66
Michal Vaskob425bb32022-11-08 10:49:36 +010067 LY_CHECK_ARG_RET(PARSER_CTX(ctx), date, LY_EINVAL);
68
69 if (date_len != LY_REV_SIZE - 1) {
70 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid length %" PRIu32 " of a date.", (uint32_t)date_len);
71 return LY_EINVAL;
72 }
Radek Krejci86d106e2018-10-18 09:53:19 +020073
Radek Krejcif13b87b2020-12-01 22:02:17 +010074 /* check format: YYYY-MM-DD */
Radek Krejci1deb5be2020-08-26 16:43:36 +020075 for (uint8_t i = 0; i < date_len; i++) {
Michal Vasko69730152020-10-09 16:30:07 +020076 if ((i == 4) || (i == 7)) {
Radek Krejci86d106e2018-10-18 09:53:19 +020077 if (date[i] != '-') {
78 goto error;
79 }
80 } else if (!isdigit(date[i])) {
81 goto error;
82 }
83 }
84
85 /* check content, e.g. 2018-02-31 */
86 memset(&tm, 0, sizeof tm);
87 r = strptime(date, "%Y-%m-%d", &tm);
Michal Vasko69730152020-10-09 16:30:07 +020088 if (!r || (r != &date[LY_REV_SIZE - 1])) {
Radek Krejci86d106e2018-10-18 09:53:19 +020089 goto error;
90 }
91 memcpy(&tm_, &tm, sizeof tm);
Michal Vasko82f32822022-08-17 10:54:35 +020092
93 /* DST may move the hour back resulting in a different day */
94 tm_.tm_hour = 1;
95
Radek Krejci86d106e2018-10-18 09:53:19 +020096 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
97 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
98 /* checking days is enough, since other errors
99 * have been checked by strptime() */
100 goto error;
101 }
102
103 return LY_SUCCESS;
104
105error:
Radek Krejcid33273d2018-10-25 14:55:52 +0200106 if (stmt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100107 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt);
Radek Krejcid33273d2018-10-25 14:55:52 +0200108 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200109 return LY_EINVAL;
110}
111
112void
113lysp_sort_revisions(struct lysp_revision *revs)
114{
Radek Krejci857189e2020-09-01 13:26:36 +0200115 LY_ARRAY_COUNT_TYPE i, r;
Radek Krejci86d106e2018-10-18 09:53:19 +0200116 struct lysp_revision rev;
117
Radek Krejcic7d13e32020-12-09 12:32:24 +0100118 for (i = 1, r = 0; i < LY_ARRAY_COUNT(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200119 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200120 r = i;
121 }
122 }
123
124 if (r) {
125 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200126 memcpy(&rev, &revs[0], sizeof rev);
127 memcpy(&revs[0], &revs[r], sizeof rev);
128 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200129 }
130}
Radek Krejci151a5b72018-10-19 14:21:44 +0200131
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200132LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200133lysp_check_enum_name(struct lysp_ctx *ctx, const char *name, size_t name_len)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100134{
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200135 if (!name_len) {
136 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
137 return LY_EVALID;
138 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
139 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
140 (int)name_len, name);
141 return LY_EVALID;
142 } else {
143 for (size_t u = 0; u < name_len; ++u) {
144 if (iscntrl(name[u])) {
145 LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
146 (int)name_len, name, u + 1);
147 break;
148 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100149 }
150 }
151
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200152 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100153}
154
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200155/**
156 * @brief Learn built-in type from its name.
157 *
158 * @param[in] name Type name.
159 * @param[in] len Length of @p name.
160 * @return Built-in data type, ::LY_TYPE_UNKNOWN if none matches.
161 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100162static LY_DATA_TYPE
163lysp_type_str2builtin(const char *name, size_t len)
164{
165 if (len >= 4) { /* otherwise it does not match any built-in type */
166 if (name[0] == 'b') {
167 if (name[1] == 'i') {
Michal Vasko69730152020-10-09 16:30:07 +0200168 if ((len == 6) && !strncmp(&name[2], "nary", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100169 return LY_TYPE_BINARY;
Michal Vasko69730152020-10-09 16:30:07 +0200170 } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100171 return LY_TYPE_BITS;
172 }
Michal Vasko69730152020-10-09 16:30:07 +0200173 } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100174 return LY_TYPE_BOOL;
175 }
176 } else if (name[0] == 'd') {
Michal Vasko69730152020-10-09 16:30:07 +0200177 if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100178 return LY_TYPE_DEC64;
179 }
180 } else if (name[0] == 'e') {
Michal Vasko69730152020-10-09 16:30:07 +0200181 if ((len == 5) && !strncmp(&name[1], "mpty", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100182 return LY_TYPE_EMPTY;
Michal Vasko69730152020-10-09 16:30:07 +0200183 } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100184 return LY_TYPE_ENUM;
185 }
186 } else if (name[0] == 'i') {
187 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200188 if ((len == 4) && !strncmp(&name[2], "t8", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100189 return LY_TYPE_INT8;
190 } else if (len == 5) {
191 if (!strncmp(&name[2], "t16", 3)) {
192 return LY_TYPE_INT16;
193 } else if (!strncmp(&name[2], "t32", 3)) {
194 return LY_TYPE_INT32;
195 } else if (!strncmp(&name[2], "t64", 3)) {
196 return LY_TYPE_INT64;
197 }
Michal Vasko69730152020-10-09 16:30:07 +0200198 } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100199 return LY_TYPE_INST;
200 }
Michal Vasko69730152020-10-09 16:30:07 +0200201 } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100202 return LY_TYPE_IDENT;
203 }
204 } else if (name[0] == 'l') {
Michal Vasko69730152020-10-09 16:30:07 +0200205 if ((len == 7) && !strncmp(&name[1], "eafref", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100206 return LY_TYPE_LEAFREF;
207 }
208 } else if (name[0] == 's') {
Michal Vasko69730152020-10-09 16:30:07 +0200209 if ((len == 6) && !strncmp(&name[1], "tring", 5)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100210 return LY_TYPE_STRING;
211 }
212 } else if (name[0] == 'u') {
213 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200214 if ((len == 5) && !strncmp(&name[2], "ion", 3)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100215 return LY_TYPE_UNION;
216 }
Michal Vasko69730152020-10-09 16:30:07 +0200217 } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) {
218 if ((len == 5) && (name[4] == '8')) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100219 return LY_TYPE_UINT8;
220 } else if (len == 6) {
221 if (!strncmp(&name[4], "16", 2)) {
222 return LY_TYPE_UINT16;
223 } else if (!strncmp(&name[4], "32", 2)) {
224 return LY_TYPE_UINT32;
225 } else if (!strncmp(&name[4], "64", 2)) {
226 return LY_TYPE_UINT64;
227 }
228 }
229 }
230 }
231 }
232
233 return LY_TYPE_UNKNOWN;
234}
235
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200236/**
237 * @brief Find a typedef in a sized array.
238 *
239 * @param[in] name Typedef name.
240 * @param[in] typedefs Sized array of typedefs.
241 * @return Found typedef, NULL if none.
242 */
243static const struct lysp_tpdf *
244lysp_typedef_match(const char *name, const struct lysp_tpdf *typedefs)
245{
246 LY_ARRAY_COUNT_TYPE u;
247
248 LY_ARRAY_FOR(typedefs, u) {
249 if (!strcmp(name, typedefs[u].name)) {
250 /* match */
251 return &typedefs[u];
252 }
253 }
254 return NULL;
255}
256
Radek Krejcibbe09a92018-11-08 09:36:54 +0100257LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +0100258lysp_type_find(const char *id, struct lysp_node *start_node, const struct lysp_module *start_module,
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200259 const struct lysc_ext_instance *ext, LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100260{
261 const char *str, *name;
262 struct lysp_tpdf *typedefs;
Michal Vasko193dacd2022-10-13 08:43:05 +0200263 const struct lysp_tpdf *ext_typedefs;
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100264 const struct lys_module *mod;
Michal Vaskoa99b3572021-02-01 11:54:58 +0100265 const struct lysp_module *local_module;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200266 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100267
268 assert(id);
269 assert(start_module);
270 assert(tpdf);
271 assert(node);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100272
Radek Krejci4f28eda2018-11-12 11:46:16 +0100273 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100274 str = strchr(id, ':');
275 if (str) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200276 mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_VALUE_SCHEMA, (void *)start_module);
Michal Vaskoa99b3572021-02-01 11:54:58 +0100277 local_module = mod ? mod->parsed : NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100278 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100279 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100280 } else {
Michal Vaskoa99b3572021-02-01 11:54:58 +0100281 local_module = start_module;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100282 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100283
284 /* check for built-in types */
285 *type = lysp_type_str2builtin(name, strlen(name));
286 if (*type) {
287 *tpdf = NULL;
288 return LY_SUCCESS;
289 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100290 }
Michal Vaskoa99b3572021-02-01 11:54:58 +0100291 LY_CHECK_RET(!local_module, LY_ENOTFOUND);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100292
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200293 if (local_module == start_module) {
294 if (start_node) {
295 /* search typedefs in parent's nodes */
296 for (*node = start_node; *node; *node = (*node)->parent) {
297 *tpdf = lysp_typedef_match(name, lysp_node_typedefs(*node));
298 if (*tpdf) {
299 /* match */
300 return LY_SUCCESS;
301 }
302 }
303 }
304
305 if (ext) {
306 /* search typedefs directly in the extension */
Michal Vaskofbd037c2022-11-08 10:34:20 +0100307 lyplg_ext_parsed_get_storage(ext, LY_STMT_TYPEDEF, sizeof ext_typedefs, (const void **)&ext_typedefs);
Michal Vasko193dacd2022-10-13 08:43:05 +0200308 if ((*tpdf = lysp_typedef_match(name, ext_typedefs))) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100309 /* match */
310 return LY_SUCCESS;
311 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100312 }
313 }
314
Michal Vasko915e5442021-06-08 14:59:21 +0200315 /* go to main module if in submodule */
316 local_module = local_module->mod->parsed;
317
Radek Krejcibbe09a92018-11-08 09:36:54 +0100318 /* search in top-level typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100319 if (local_module->typedefs) {
320 LY_ARRAY_FOR(local_module->typedefs, u) {
321 if (!strcmp(name, local_module->typedefs[u].name)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100322 /* match */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100323 *tpdf = &local_module->typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100324 return LY_SUCCESS;
325 }
326 }
327 }
328
Michal Vasko915e5442021-06-08 14:59:21 +0200329 /* search in all submodules' typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100330 LY_ARRAY_FOR(local_module->includes, u) {
331 typedefs = local_module->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100332 LY_ARRAY_FOR(typedefs, v) {
333 if (!strcmp(name, typedefs[v].name)) {
334 /* match */
335 *tpdf = &typedefs[v];
336 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100337 }
338 }
339 }
340
341 return LY_ENOTFOUND;
342}
343
Michal Vaskob36053d2020-03-26 15:49:30 +0100344/**
aPiecekdc12b9f2021-06-25 10:55:47 +0200345 * @brief Insert @p name to hash table and if @p name has already
346 * been added, then log an error.
347 *
348 * This function is used to detect duplicate names.
349 *
350 * @param[in,out] ctx Context to log the error.
351 * @param[in,out] ht Hash table with top-level names.
352 * @param[in] name Inserted top-level identifier.
353 * @param[in] statement The name of the statement type from which
354 * @p name originated (eg typedef, feature, ...).
355 * @param[in] err_detail Optional error specification.
356 * @return LY_ERR, but LY_EEXIST is mapped to LY_EVALID.
357 */
358static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200359lysp_check_dup_ht_insert(struct lysp_ctx *ctx, struct hash_table *ht,
aPiecekdc12b9f2021-06-25 10:55:47 +0200360 const char *name, const char *statement, const char *err_detail)
361{
362 LY_ERR ret;
363 uint32_t hash;
364
365 hash = dict_hash(name, strlen(name));
366 ret = lyht_insert(ht, &name, hash, NULL);
367 if (ret == LY_EEXIST) {
368 if (err_detail) {
369 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT2, name, statement, err_detail);
370 } else {
371 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, name, statement);
372 }
373 ret = LY_EVALID;
374 }
375
376 return ret;
377}
378
379/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100380 * @brief Check name of a new type to avoid name collisions.
381 *
382 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
383 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
384 * @param[in] tpdf Typedef definition to check.
385 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
aPiecekc7594b72021-06-29 09:00:03 +0200386 * typedefs are checked, caller is supposed to free the table.
387 * @return LY_EVALID in case of collision, LY_SUCCESS otherwise.
Radek Krejcibbe09a92018-11-08 09:36:54 +0100388 */
389static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200390lysp_check_dup_typedef(struct lysp_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
aPieceke1fbd952021-06-29 08:12:55 +0200391 struct hash_table *tpdfs_global)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100392{
393 struct lysp_node *parent;
394 uint32_t hash;
395 size_t name_len;
396 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200397 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100398 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100399
400 assert(ctx);
401 assert(tpdf);
402
403 name = tpdf->name;
404 name_len = strlen(name);
405
Radek Krejci4f28eda2018-11-12 11:46:16 +0100406 if (lysp_type_str2builtin(name, name_len)) {
aPiecekc7594b72021-06-29 09:00:03 +0200407 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
408 "Duplicate identifier \"%s\" of typedef statement - name collision with a built-in type.", name);
409 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100410 }
411
412 /* check locally scoped typedefs (avoid name shadowing) */
413 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100414 typedefs = lysp_node_typedefs(node);
415 LY_ARRAY_FOR(typedefs, u) {
416 if (&typedefs[u] == tpdf) {
417 break;
418 }
419 if (!strcmp(name, typedefs[u].name)) {
aPiecekc7594b72021-06-29 09:00:03 +0200420 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
421 "Duplicate identifier \"%s\" of typedef statement - name collision with sibling type.", name);
422 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100423 }
424 }
425 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200426 for (parent = node->parent; parent; parent = parent->parent) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200427 if (lysp_typedef_match(name, lysp_node_typedefs(parent))) {
aPiecekc7594b72021-06-29 09:00:03 +0200428 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
429 "Duplicate identifier \"%s\" of typedef statement - name collision with another scoped type.", name);
430 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100431 }
432 }
433 }
434
435 /* check collision with the top-level typedefs */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100436 if (node) {
aPiecekc7594b72021-06-29 09:00:03 +0200437 hash = dict_hash(name, name_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100438 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
aPiecekc7594b72021-06-29 09:00:03 +0200439 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
440 "Duplicate identifier \"%s\" of typedef statement - scoped type collide with a top-level type.", name);
441 return LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100442 }
443 } else {
aPiecekc7594b72021-06-29 09:00:03 +0200444 LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, tpdfs_global, name, "typedef",
445 "name collision with another top-level type"));
Radek Krejci3b1f9292018-11-08 10:58:35 +0100446 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
447 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
448 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100449 }
450
451 return LY_SUCCESS;
452}
453
Radek Krejci857189e2020-09-01 13:26:36 +0200454/**
455 * @brief Compare identifiers.
Michal Vasko62524a92021-02-26 10:08:50 +0100456 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200457 */
458static ly_bool
459lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Radek Krejcibbe09a92018-11-08 09:36:54 +0100460{
Michal Vasko11ac39a2021-07-23 12:46:56 +0200461 char *id1, *id2;
462
463 id1 = *(char **)val1;
464 id2 = *(char **)val2;
465
466 return strcmp(id1, id2) == 0 ? 1 : 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100467}
468
469LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200470lysp_check_dup_typedefs(struct lysp_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100471{
472 struct hash_table *ids_global;
Radek Krejci0fb28562018-12-13 15:17:37 +0100473 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200474 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200475 uint32_t i;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100476 LY_ERR ret = LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100477
478 /* check name collisions - typedefs and groupings */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100479 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200480 LY_ARRAY_FOR(mod->typedefs, v) {
aPieceke1fbd952021-06-29 08:12:55 +0200481 ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100482 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100483 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200484 LY_ARRAY_FOR(mod->includes, v) {
485 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200486 ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100487 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci3b1f9292018-11-08 10:58:35 +0100488 }
489 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200490 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
491 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
492 LY_ARRAY_FOR(typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200493 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 +0100494 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100495 }
496 }
Michal Vasko405cc9e2020-12-01 12:01:27 +0100497
Radek Krejcibbe09a92018-11-08 09:36:54 +0100498cleanup:
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100499 lyht_free(ids_global, NULL);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100500 return ret;
501}
502
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200503static const struct lysp_node_grp *
504lysp_grouping_match(const char *name, struct lysp_node *node)
505{
506 const struct lysp_node_grp *groupings, *grp_iter;
507
508 groupings = lysp_node_groupings(node);
509 LY_LIST_FOR(groupings, grp_iter) {
510 if (!strcmp(name, grp_iter->name)) {
511 /* match */
512 return grp_iter;
513 }
514 }
515
516 return NULL;
517}
518
aPiecek63e080d2021-06-29 13:53:28 +0200519/**
520 * @brief Check name of a new grouping to avoid name collisions.
521 *
522 * @param[in] ctx Parser context, module where the grouping is being defined is taken from here.
523 * @param[in] node Schema node where the grouping is being defined, NULL in case of a top-level grouping.
524 * @param[in] grp Grouping definition to check.
525 * @param[in,out] grps_global Initialized hash table to store temporary data between calls. When the module's
526 * groupings are checked, caller is supposed to free the table.
527 * @return LY_EVALID in case of collision, LY_SUCCESS otherwise.
528 */
529static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200530lysp_check_dup_grouping(struct lysp_ctx *ctx, struct lysp_node *node, const struct lysp_node_grp *grp,
aPiecek63e080d2021-06-29 13:53:28 +0200531 struct hash_table *grps_global)
532{
533 struct lysp_node *parent;
534 uint32_t hash;
535 size_t name_len;
536 const char *name;
537 const struct lysp_node_grp *groupings, *grp_iter;
538
539 assert(ctx);
540 assert(grp);
541
542 name = grp->name;
543 name_len = strlen(name);
544
545 /* check locally scoped groupings (avoid name shadowing) */
546 if (node) {
547 groupings = lysp_node_groupings(node);
548 LY_LIST_FOR(groupings, grp_iter) {
549 if (grp_iter == grp) {
550 break;
551 }
552 if (!strcmp(name, grp_iter->name)) {
553 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
554 "Duplicate identifier \"%s\" of grouping statement - name collision with sibling grouping.", name);
555 return LY_EVALID;
556 }
557 }
558 /* search grouping in parent's nodes */
559 for (parent = node->parent; parent; parent = parent->parent) {
560 if (lysp_grouping_match(name, parent)) {
561 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
562 "Duplicate identifier \"%s\" of grouping statement - name collision with another scoped grouping.", name);
563 return LY_EVALID;
564 }
565 }
566 }
567
568 /* check collision with the top-level groupings */
569 if (node) {
570 hash = dict_hash(name, name_len);
571 if (!lyht_find(grps_global, &name, hash, NULL)) {
572 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
573 "Duplicate identifier \"%s\" of grouping statement - scoped grouping collide with a top-level grouping.", name);
574 return LY_EVALID;
575 }
576 } else {
577 LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, grps_global, name, "grouping",
578 "name collision with another top-level grouping"));
579 }
580
581 return LY_SUCCESS;
582}
583
584LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200585lysp_check_dup_groupings(struct lysp_ctx *ctx, struct lysp_module *mod)
aPiecek63e080d2021-06-29 13:53:28 +0200586{
587 struct hash_table *ids_global;
588 const struct lysp_node_grp *groupings, *grp_iter;
589 LY_ARRAY_COUNT_TYPE u;
590 uint32_t i;
591 LY_ERR ret = LY_SUCCESS;
592
593 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
594 LY_LIST_FOR(mod->groupings, grp_iter) {
595 ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global);
596 LY_CHECK_GOTO(ret, cleanup);
597 }
598 LY_ARRAY_FOR(mod->includes, u) {
599 LY_LIST_FOR(mod->includes[u].submodule->groupings, grp_iter) {
600 ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global);
601 LY_CHECK_GOTO(ret, cleanup);
602 }
603 }
604 for (i = 0; i < ctx->grps_nodes.count; ++i) {
605 groupings = lysp_node_groupings((struct lysp_node *)ctx->grps_nodes.objs[i]);
606 LY_LIST_FOR(groupings, grp_iter) {
607 ret = lysp_check_dup_grouping(ctx, (struct lysp_node *)ctx->grps_nodes.objs[i], grp_iter, ids_global);
608 LY_CHECK_GOTO(ret, cleanup);
609 }
610 }
611
612cleanup:
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100613 lyht_free(ids_global, NULL);
aPiecek63e080d2021-06-29 13:53:28 +0200614 return ret;
615}
616
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100617static ly_bool
618ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
619{
620 void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
621
622 return ptr1 == ptr2 ? 1 : 0;
623}
624
625LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200626lysp_check_dup_features(struct lysp_ctx *ctx, struct lysp_module *mod)
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100627{
628 LY_ARRAY_COUNT_TYPE u;
629 struct hash_table *ht;
630 struct lysp_feature *f;
aPiecekdc12b9f2021-06-25 10:55:47 +0200631 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100632
aPiecekf6203072021-06-25 10:58:26 +0200633 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100634 LY_CHECK_RET(!ht, LY_EMEM);
635
636 /* add all module features into a hash table */
637 LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200638 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
639 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200640 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100641 }
642
643 /* add all submodule features into a hash table */
644 LY_ARRAY_FOR(mod->includes, u) {
645 LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200646 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
647 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200648 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100649 }
650 }
651
652cleanup:
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100653 lyht_free(ht, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100654 return ret;
655}
656
657LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200658lysp_check_dup_identities(struct lysp_ctx *ctx, struct lysp_module *mod)
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100659{
660 LY_ARRAY_COUNT_TYPE u;
661 struct hash_table *ht;
662 struct lysp_ident *i;
aPiecekdc12b9f2021-06-25 10:55:47 +0200663 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100664
aPiecekf6203072021-06-25 10:58:26 +0200665 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100666 LY_CHECK_RET(!ht, LY_EMEM);
667
668 /* add all module identities into a hash table */
669 LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200670 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
671 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200672 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100673 }
674
675 /* add all submodule identities into a hash table */
676 LY_ARRAY_FOR(mod->includes, u) {
677 LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200678 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
679 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200680 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100681 }
682 }
683
684cleanup:
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100685 lyht_free(ht, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100686 return ret;
687}
688
Radek Krejci9ed7a192018-10-31 16:23:51 +0100689struct lysp_load_module_check_data {
690 const char *name;
691 const char *revision;
692 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200693 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100694};
695
696static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100697lysp_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 +0100698{
699 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100700 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200701 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100702 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100703 struct lysp_revision *revs;
704
705 name = mod ? mod->mod->name : submod->name;
706 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200707 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100708
709 if (info->name) {
710 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100711 if (strcmp(info->name, name)) {
712 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100713 return LY_EINVAL;
714 }
715 }
716 if (info->revision) {
717 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100718 if (!revs || strcmp(info->revision, revs[0].date)) {
719 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200720 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100721 return LY_EINVAL;
722 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200723 } else if (!latest_revision) {
724 /* do not log, we just need to drop the schema and use the latest revision from the context */
725 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100726 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100727 if (submod) {
728 assert(info->submoduleof);
729
Radek Krejci9ed7a192018-10-31 16:23:51 +0100730 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200731 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100732 LOGVAL(ctx, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200733 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100734 return LY_EVALID;
735 }
736 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100737 if (submod->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100738 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100739 return LY_EVALID;
740 }
741 }
742 if (info->path) {
743 /* check that name and revision match filename */
744 filename = strrchr(info->path, '/');
745 if (!filename) {
746 filename = info->path;
747 } else {
748 filename++;
749 }
750 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100751 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100752 rev = strchr(filename, '@');
753 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100754 if (strncmp(filename, name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +0200755 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100756 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100757 }
758 /* revision */
759 if (rev) {
760 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100761 if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100762 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +0200763 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100764 }
765 }
766 }
767 return LY_SUCCESS;
768}
769
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200770/**
Michal Vasko4e205e82021-06-08 14:01:47 +0200771 * @brief Parse a (sub)module from a local file and add into the context.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200772 *
773 * This function does not check the presence of the (sub)module in context, it should be done before calling this function.
774 *
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200775 * @param[in] ctx libyang context where to work.
776 * @param[in] name Name of the (sub)module to load.
777 * @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 +0200778 * @param[in] main_ctx Parser context of the main module in case of loading submodule.
779 * @param[in] main_name Main module name in case of loading submodule.
780 * @param[in] required Module is required so error (even if the input file not found) are important. If 0, there is some
781 * 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 +0200782 * @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 +0200783 * @param[out] result Parsed YANG schema tree of the requested module (struct lys_module*) or submodule (struct lysp_submodule*).
784 * If it is a module, it is already in the context!
Michal Vasko4e205e82021-06-08 14:01:47 +0200785 * @return LY_SUCCESS on success.
Michal Vasko4e205e82021-06-08 14:01:47 +0200786 * @return LY_ERR on error.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200787 */
788static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200789lys_parse_localfile(struct ly_ctx *ctx, const char *name, const char *revision, struct lysp_ctx *main_ctx,
Michal Vaskodd992582021-06-10 14:34:57 +0200790 const char *main_name, ly_bool required, struct ly_set *new_mods, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100791{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200792 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100793 char *filepath = NULL;
794 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100795 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100796 LY_ERR ret = LY_SUCCESS;
797 struct lysp_load_module_check_data check_data = {0};
798
Michal Vasko87f1cf02021-06-08 14:02:47 +0200799 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name,
800 revision, &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200801 if (!filepath) {
802 if (required) {
803 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200804 revision ? revision : "");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200805 }
806 return LY_ENOTFOUND;
807 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100808
809 LOGVRB("Loading schema from \"%s\" file.", filepath);
810
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200811 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200812 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +0200813 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100814 check_data.name = name;
815 check_data.revision = revision;
816 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800817 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200818 if (main_ctx) {
aPiecekc3e26142021-06-22 14:25:49 +0200819 ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data, new_mods,
Michal Vasko69730152020-10-09 16:30:07 +0200820 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200821 } else {
Michal Vaskodd992582021-06-10 14:34:57 +0200822 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 +0200823
824 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200825 ly_in_free(in, 1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200826 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100827
828 *result = mod;
829
830 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200831
Radek Krejci9ed7a192018-10-31 16:23:51 +0100832cleanup:
833 free(filepath);
834 return ret;
835}
836
aPiecek4725aea2021-07-28 10:18:33 +0200837/**
838 * @brief Load module from searchdirs or from callback.
839 *
840 * @param[in] ctx libyang context where to work.
841 * @param[in] name Name of module to load.
842 * @param[in] revision Revision of module to load.
Michal Vaskobdac23f2022-01-12 14:02:35 +0100843 * @param[in] mod_latest Module with the latest revision found in context, otherwise set to NULL.
844 * @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 +0200845 * @param[out] mod Loaded module.
846 * @return LY_SUCCESS on success.
847 * @return LY_ERR on error.
848 */
849static LY_ERR
850lys_parse_load_from_clb_or_file(struct ly_ctx *ctx, const char *name, const char *revision,
851 struct lys_module *mod_latest, struct ly_set *new_mods, struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200852{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100853 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200854 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200855
Radek Krejci9ed7a192018-10-31 16:23:51 +0100856 void (*module_data_free)(void *module_data, void *user_data) = NULL;
857 struct lysp_load_module_check_data check_data = {0};
Michal Vasko63f3d842020-07-08 10:10:14 +0200858 struct ly_in *in;
Radek Krejci086c7132018-10-26 15:29:04 +0200859
Michal Vaskobdac23f2022-01-12 14:02:35 +0100860 *mod = NULL;
861
862 if (mod_latest && (!ctx->imp_clb || (mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB)) &&
863 ((ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) || (mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) {
864 /* we are not able to find a newer revision */
865 return LY_SUCCESS;
866 }
867
aPiecek4725aea2021-07-28 10:18:33 +0200868 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
869search_clb:
Michal Vaskobdac23f2022-01-12 14:02:35 +0100870 /* check there is a callback and should be called */
871 if (ctx->imp_clb && (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB))) {
872 if (!ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, &format, &module_data, &module_data_free)) {
873 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
874 check_data.name = name;
875 check_data.revision = revision;
876 lys_parse_in(ctx, in, format, lysp_load_module_check, &check_data, new_mods, mod);
877 ly_in_free(in, 0);
878 if (module_data_free) {
879 module_data_free((void *)module_data, ctx->imp_clb_data);
880 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200881 }
Radek Krejci0af46292019-01-11 16:02:31 +0100882 }
Michal Vaskobdac23f2022-01-12 14:02:35 +0100883 if (*mod && !revision) {
884 /* we got the latest revision module from the callback */
885 (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB;
886 } else if (!*mod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
aPiecek4725aea2021-07-28 10:18:33 +0200887 goto search_file;
888 }
889 } else {
890search_file:
Michal Vaskobdac23f2022-01-12 14:02:35 +0100891 /* check we can use searchdirs and that we should */
892 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) &&
893 (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) {
aPiecek4725aea2021-07-28 10:18:33 +0200894 lys_parse_localfile(ctx, name, revision, NULL, NULL, mod_latest ? 0 : 1, new_mods, (void **)mod);
895 }
Michal Vaskobdac23f2022-01-12 14:02:35 +0100896 if (*mod && !revision) {
897 /* we got the latest revision module in the searchdirs */
898 (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB;
899 } else if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
aPiecek4725aea2021-07-28 10:18:33 +0200900 goto search_clb;
901 }
Radek Krejci086c7132018-10-26 15:29:04 +0200902 }
903
aPiecek4725aea2021-07-28 10:18:33 +0200904 return LY_SUCCESS;
905}
906
907/**
aPiecekd4911ee2021-07-30 07:40:24 +0200908 * @brief Get module without revision according to priorities.
909 *
910 * 1. Search for the module with LYS_MOD_IMPORTED_REV.
911 * 2. Search for the implemented module.
912 * 3. Search for the latest module in the context.
913 *
914 * @param[in] ctx libyang context where module is searched.
915 * @param[in] name Name of the searched module.
aPiecekd4911ee2021-07-30 07:40:24 +0200916 * @return Found module from context or NULL.
917 */
918static struct lys_module *
Michal Vaskobdac23f2022-01-12 14:02:35 +0100919lys_get_module_without_revision(struct ly_ctx *ctx, const char *name)
aPiecekd4911ee2021-07-30 07:40:24 +0200920{
921 struct lys_module *mod, *mod_impl;
922 uint32_t index;
923
Michal Vaskocfcfd672023-02-14 11:20:42 +0100924 /* try to find module with LYS_MOD_IMPORTED_REV flag */
aPiecekd4911ee2021-07-30 07:40:24 +0200925 index = 0;
926 while ((mod = ly_ctx_get_module_iter(ctx, &index))) {
927 if (!strcmp(mod->name, name) && (mod->latest_revision & LYS_MOD_IMPORTED_REV)) {
928 break;
929 }
930 }
931
Michal Vaskocfcfd672023-02-14 11:20:42 +0100932 /* try to find the implemented module */
aPiecekd4911ee2021-07-30 07:40:24 +0200933 mod_impl = ly_ctx_get_module_implemented(ctx, name);
Michal Vaskocfcfd672023-02-14 11:20:42 +0100934 if (mod) {
935 if (mod_impl && (mod != mod_impl)) {
936 LOGVRB("Implemented module \"%s@%s\" is not used for import, revision \"%s\" is imported instead.",
937 mod_impl->name, mod_impl->revision, mod->revision);
938 }
aPiecekd4911ee2021-07-30 07:40:24 +0200939 return mod;
940 } else if (mod_impl) {
941 return mod_impl;
942 }
943
Michal Vaskocfcfd672023-02-14 11:20:42 +0100944 /* try to find the latest module in the current context */
aPiecekd4911ee2021-07-30 07:40:24 +0200945 mod = ly_ctx_get_module_latest(ctx, name);
aPiecekd4911ee2021-07-30 07:40:24 +0200946
947 return mod;
948}
949
950/**
aPiecek4725aea2021-07-28 10:18:33 +0200951 * @brief Check if a circular dependency exists between modules.
952 *
953 * @param[in] ctx libyang context for log an error.
Michal Vaskocfcfd672023-02-14 11:20:42 +0100954 * @param[in,out] mod Examined module which is set to NULL if the circular dependency is detected.
955 * @return LY_SUCCESS if no circular dependecy is detected, otherwise LY_EVALID.
aPiecek4725aea2021-07-28 10:18:33 +0200956 */
957static LY_ERR
958lys_check_circular_dependency(struct ly_ctx *ctx, struct lys_module **mod)
959{
960 if ((*mod) && (*mod)->parsed->parsing) {
961 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", (*mod)->name);
Michal Vasko4e205e82021-06-08 14:01:47 +0200962 *mod = NULL;
963 return LY_EVALID;
Michal Vasko0550b762020-11-24 18:04:08 +0100964 }
Radek Krejci086c7132018-10-26 15:29:04 +0200965
aPiecek4725aea2021-07-28 10:18:33 +0200966 return LY_SUCCESS;
967}
968
969LY_ERR
970lys_parse_load(struct ly_ctx *ctx, const char *name, const char *revision, struct ly_set *new_mods,
971 struct lys_module **mod)
972{
973 struct lys_module *mod_latest = NULL;
974
975 assert(mod && new_mods);
976
Michal Vasko0550b762020-11-24 18:04:08 +0100977 /*
aPiecek4725aea2021-07-28 10:18:33 +0200978 * Try to get the module from the context.
Michal Vasko0550b762020-11-24 18:04:08 +0100979 */
aPiecek4725aea2021-07-28 10:18:33 +0200980 if (revision) {
981 /* Get the specific revision. */
982 *mod = ly_ctx_get_module(ctx, name, revision);
983 } else {
Michal Vaskobdac23f2022-01-12 14:02:35 +0100984 /* Get the requested module in a suitable revision in the context. */
985 *mod = lys_get_module_without_revision(ctx, name);
986 if (*mod && !(*mod)->implemented && !((*mod)->latest_revision & LYS_MOD_IMPORTED_REV)) {
aPiecek4725aea2021-07-28 10:18:33 +0200987 /* Let us now search with callback and searchpaths to check
988 * if there is newer revision outside the context.
989 */
990 mod_latest = *mod;
991 *mod = NULL;
992 }
993 }
994
Michal Vasko0550b762020-11-24 18:04:08 +0100995 if (!*mod) {
aPiecek4725aea2021-07-28 10:18:33 +0200996 /* No suitable module in the context, try to load it. */
Michal Vaskobdac23f2022-01-12 14:02:35 +0100997 LY_CHECK_RET(lys_parse_load_from_clb_or_file(ctx, name, revision, mod_latest, new_mods, mod));
aPiecek4725aea2021-07-28 10:18:33 +0200998 if (!*mod && !mod_latest) {
Michal Vasko4e205e82021-06-08 14:01:47 +0200999 LOGVAL(ctx, LYVE_REFERENCE, "Loading \"%s\" module failed.", name);
Michal Vasko0550b762020-11-24 18:04:08 +01001000 return LY_EVALID;
1001 }
aPiecek4725aea2021-07-28 10:18:33 +02001002
1003 /* Update the latest_revision flag - here we have selected the latest available schema,
1004 * consider that even the callback provides correct latest revision.
1005 */
1006 if (!*mod) {
1007 LOGVRB("Newer revision than \"%s@%s\" not found, using this as the latest revision.",
1008 mod_latest->name, mod_latest->revision);
1009 assert(mod_latest->latest_revision & LYS_MOD_LATEST_REV);
1010 mod_latest->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS;
1011 *mod = mod_latest;
1012 } else if (*mod && !revision && ((*mod)->latest_revision & LYS_MOD_LATEST_REV)) {
1013 (*mod)->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS;
1014 }
Radek Krejci086c7132018-10-26 15:29:04 +02001015 }
Radek Krejci086c7132018-10-26 15:29:04 +02001016
aPiecek9f8c7e72021-07-28 12:01:56 +02001017 /* Checking the circular dependence of imported modules. */
1018 LY_CHECK_RET(lys_check_circular_dependency(ctx, mod));
1019
Radek Krejci086c7132018-10-26 15:29:04 +02001020 return LY_SUCCESS;
1021}
1022
1023LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001024lysp_check_stringchar(struct lysp_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +02001025{
1026 if (!is_yangutf8char(c)) {
1027 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
1028 return LY_EVALID;
1029 }
1030 return LY_SUCCESS;
1031}
1032
1033LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001034lysp_check_identifierchar(struct lysp_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +02001035{
Michal Vasko69730152020-10-09 16:30:07 +02001036 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +02001037 if (!is_yangidentstartchar(c)) {
aPiecekc89b2242021-05-14 14:19:11 +02001038 if ((c < UCHAR_MAX) && isprint(c)) {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001039 if (ctx) {
1040 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
1041 }
Michal Vasko7c769042021-03-25 12:20:49 +01001042 } else {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001043 if (ctx) {
1044 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character 0x%04x.", c);
1045 }
Michal Vasko7c769042021-03-25 12:20:49 +01001046 }
David Sedlák4a650532019-07-10 11:55:18 +02001047 return LY_EVALID;
1048 }
1049 if (prefix) {
1050 if (first) {
1051 (*prefix) = 0;
1052 } else {
1053 (*prefix) = 2;
1054 }
1055 }
Michal Vasko69730152020-10-09 16:30:07 +02001056 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +02001057 (*prefix) = 1;
1058 } else if (!is_yangidentchar(c)) {
Michal Vasko9ff8d2d2022-09-29 13:41:14 +02001059 if (ctx) {
1060 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
1061 }
David Sedlák4a650532019-07-10 11:55:18 +02001062 return LY_EVALID;
1063 }
1064
1065 return LY_SUCCESS;
1066}
1067
Radek Krejci771928a2021-01-19 13:42:36 +01001068/**
1069 * @brief Try to find the parsed submodule in main module for the given include record.
1070 *
1071 * @param[in] pctx main parser context
1072 * @param[in] inc The include record with missing parsed submodule. According to include info try to find
1073 * the corresponding parsed submodule in main module's includes.
1074 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
1075 * @return LY_ENOT - the parsed module was not found.
1076 * @return LY_EVALID - YANG rule violation
1077 */
1078static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001079lysp_main_pmod_get_submodule(struct lysp_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +02001080{
Radek Krejci771928a2021-01-19 13:42:36 +01001081 LY_ARRAY_COUNT_TYPE i;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001082 struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed;
Michal Vasko69730152020-10-09 16:30:07 +02001083
Radek Krejci771928a2021-01-19 13:42:36 +01001084 LY_ARRAY_FOR(main_pmod->includes, i) {
1085 if (strcmp(main_pmod->includes[i].name, inc->name)) {
1086 continue;
1087 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001088
Radek Krejci771928a2021-01-19 13:42:36 +01001089 if (inc->rev[0] && strncmp(inc->rev, main_pmod->includes[i].rev, LY_REV_SIZE)) {
1090 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1091 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001092 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev,
Radek Krejci771928a2021-01-19 13:42:36 +01001093 main_pmod->includes[i].name, main_pmod->includes[i].rev, main_pmod->mod->name);
1094 return LY_EVALID;
1095 }
1096
1097 inc->submodule = main_pmod->includes[i].submodule;
1098 return inc->submodule ? LY_SUCCESS : LY_ENOT;
1099 }
1100
1101 if (main_pmod->version == LYS_VERSION_1_1) {
1102 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1103 "YANG 1.1 requires all submodules to be included from main module. "
1104 "But submodule \"%s\" includes submodule \"%s\" which is not included by main module \"%s\".",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001105 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->name, main_pmod->mod->name);
Radek Krejci771928a2021-01-19 13:42:36 +01001106 return LY_EVALID;
1107 } else {
1108 return LY_ENOT;
1109 }
1110}
1111
1112/**
Michal Vasko8a67eff2021-12-07 14:04:47 +01001113 * @brief Try to find the parsed submodule in currenlty parsed modules for the given include record.
1114 *
1115 * @param[in] pctx main parser context
1116 * @param[in] inc The include record with missing parsed submodule.
1117 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
1118 * @return LY_ENOT - the parsed module was not found.
1119 * @return LY_EVALID - YANG rule violation
1120 */
1121static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001122lysp_parsed_mods_get_submodule(struct lysp_ctx *pctx, struct lysp_include *inc)
Michal Vasko8a67eff2021-12-07 14:04:47 +01001123{
1124 uint32_t i;
1125 struct lysp_submodule *submod;
1126
1127 for (i = 0; i < pctx->parsed_mods->count - 1; ++i) {
1128 submod = pctx->parsed_mods->objs[i];
1129 if (!submod->is_submod) {
1130 continue;
1131 }
1132
1133 if (strcmp(submod->name, inc->name)) {
1134 continue;
1135 }
1136
1137 if (inc->rev[0] && submod->revs && strncmp(inc->rev, submod->revs[0].date, LY_REV_SIZE)) {
1138 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
1139 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
1140 ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev,
1141 submod->name, submod->revs[0].date, PARSER_CUR_PMOD(pctx)->mod->name);
1142 return LY_EVALID;
1143 }
1144
1145 inc->submodule = submod;
1146 return LY_SUCCESS;
1147 }
1148
1149 return LY_ENOT;
1150}
1151
1152/**
Radek Krejci771928a2021-01-19 13:42:36 +01001153 * @brief Make the copy of the given include record into the main module.
1154 *
1155 * YANG 1.0 does not require the main module to include all the submodules. Therefore, parsing submodules can cause
1156 * reallocating and extending the includes array in the main module by the submodules included only in submodules.
1157 *
1158 * @param[in] pctx main parser context
1159 * @param[in] inc Include record to copy into main module taken from @p pctx.
1160 * @return LY_ERR value.
1161 */
1162static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001163lysp_inject_submodule(struct lysp_ctx *pctx, struct lysp_include *inc)
Radek Krejci771928a2021-01-19 13:42:36 +01001164{
1165 LY_ARRAY_COUNT_TYPE i;
1166 struct lysp_include *inc_new, *inc_tofill = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001167 struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed;
Radek Krejci771928a2021-01-19 13:42:36 +01001168
1169 /* first, try to find the corresponding record with missing parsed submodule */
1170 LY_ARRAY_FOR(main_pmod->includes, i) {
1171 if (strcmp(main_pmod->includes[i].name, inc->name)) {
1172 continue;
1173 }
1174 inc_tofill = &main_pmod->includes[i];
1175 break;
1176 }
1177
1178 if (inc_tofill) {
1179 inc_tofill->submodule = inc->submodule;
1180 } else {
1181 LY_ARRAY_NEW_RET(PARSER_CTX(pctx), main_pmod->includes, inc_new, LY_EMEM);
1182
1183 inc_new->submodule = inc->submodule;
1184 DUP_STRING_RET(PARSER_CTX(pctx), inc->name, inc_new->name);
1185 DUP_STRING_RET(PARSER_CTX(pctx), inc->dsc, inc_new->dsc);
1186 DUP_STRING_RET(PARSER_CTX(pctx), inc->ref, inc_new->ref);
1187 /* TODO duplicate extensions */
1188 memcpy(inc_new->rev, inc->rev, LY_REV_SIZE);
1189 inc_new->injected = 1;
1190 }
1191 return LY_SUCCESS;
1192}
1193
1194LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001195lysp_load_submodules(struct lysp_ctx *pctx, struct lysp_module *pmod, struct ly_set *new_mods)
Radek Krejci771928a2021-01-19 13:42:36 +01001196{
1197 LY_ARRAY_COUNT_TYPE u;
1198 struct ly_ctx *ctx = PARSER_CTX(pctx);
1199
1200 LY_ARRAY_FOR(pmod->includes, u) {
Michal Vasko8a67eff2021-12-07 14:04:47 +01001201 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci771928a2021-01-19 13:42:36 +01001202 struct lysp_submodule *submod = NULL;
1203 struct lysp_include *inc = &pmod->includes[u];
1204
1205 if (inc->submodule) {
1206 continue;
1207 }
1208
1209 if (pmod->is_submod) {
1210 /* try to find the submodule in the main module or its submodules */
Michal Vasko8a67eff2021-12-07 14:04:47 +01001211 ret = lysp_main_pmod_get_submodule(pctx, inc);
1212 LY_CHECK_RET(ret != LY_ENOT, ret);
Radek Krejci771928a2021-01-19 13:42:36 +01001213 }
1214
Michal Vasko8a67eff2021-12-07 14:04:47 +01001215 /* try to use currently parsed submodule */
1216 r = lysp_parsed_mods_get_submodule(pctx, inc);
1217 LY_CHECK_RET(r != LY_ENOT, r);
1218
Radek Krejci771928a2021-01-19 13:42:36 +01001219 /* submodule not present in the main module, get the input data and parse it */
1220 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcidf549132021-01-21 10:32:32 +01001221search_clb:
Radek Krejci771928a2021-01-19 13:42:36 +01001222 if (ctx->imp_clb) {
1223 const char *submodule_data = NULL;
1224 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko26bbb272022-08-02 14:54:33 +02001225
Radek Krejci771928a2021-01-19 13:42:36 +01001226 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
1227 struct lysp_load_module_check_data check_data = {0};
1228 struct ly_in *in;
1229
Michal Vasko8a67eff2021-12-07 14:04:47 +01001230 if (ctx->imp_clb(PARSER_CUR_PMOD(pctx)->mod->name, NULL, inc->name,
Radek Krejci771928a2021-01-19 13:42:36 +01001231 inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
1232 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
1233 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
1234 check_data.name = inc->name;
1235 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +01001236 check_data.submoduleof = PARSER_CUR_PMOD(pctx)->mod->name;
aPiecekc3e26142021-06-22 14:25:49 +02001237 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, new_mods, &submod);
Radek Krejci771928a2021-01-19 13:42:36 +01001238
1239 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1240 * submodule's include into main module, where it is missing */
1241 inc = &pmod->includes[u];
1242
1243 ly_in_free(in, 0);
1244 if (submodule_data_free) {
1245 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
1246 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001247 }
1248 }
Radek Krejci771928a2021-01-19 13:42:36 +01001249 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1250 goto search_file;
1251 }
1252 } else {
Radek Krejcidf549132021-01-21 10:32:32 +01001253search_file:
Radek Krejci771928a2021-01-19 13:42:36 +01001254 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
1255 /* submodule was not received from the callback or there is no callback set */
Michal Vaskodd6d5a32022-07-14 13:54:58 +02001256 lys_parse_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, pctx->main_ctx,
1257 PARSER_CUR_PMOD(pctx->main_ctx)->mod->name, 1, new_mods, (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001258
Radek Krejci771928a2021-01-19 13:42:36 +01001259 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1260 * submodule's include into main module, where it is missing */
1261 inc = &pmod->includes[u];
1262 }
1263 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1264 goto search_clb;
1265 }
1266 }
1267 if (submod) {
1268 if (!inc->rev[0] && (submod->latest_revision == 1)) {
1269 /* update the latest_revision flag - here we have selected the latest available schema,
1270 * consider that even the callback provides correct latest revision */
1271 submod->latest_revision = 2;
1272 }
1273
1274 inc->submodule = submod;
1275 if (ret == LY_ENOT) {
1276 /* the submodule include is not present in YANG 1.0 main module - add it there */
1277 LY_CHECK_RET(lysp_inject_submodule(pctx, &pmod->includes[u]));
1278 }
1279 }
1280 if (!inc->submodule) {
1281 LOGVAL(ctx, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name,
Michal Vasko8a67eff2021-12-07 14:04:47 +01001282 PARSER_CUR_PMOD(pctx)->is_submod ? ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name :
1283 PARSER_CUR_PMOD(pctx)->mod->name);
Radek Krejci771928a2021-01-19 13:42:36 +01001284 return LY_EVALID;
1285 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001286 }
1287
1288 return LY_SUCCESS;
1289}
1290
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001291LIBYANG_API_DEF const struct lysc_when *
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001292lysc_has_when(const struct lysc_node *node)
1293{
Radek Krejci9a3823e2021-01-27 20:26:46 +01001294 struct lysc_when **when;
1295
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001296 if (!node) {
1297 return NULL;
1298 }
1299
1300 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001301 when = lysc_node_when(node);
1302 if (when) {
1303 return when[0];
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001304 }
1305 node = node->parent;
1306 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1307
1308 return NULL;
1309}
1310
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001311LIBYANG_API_DEF const struct lys_module *
Michal Vaskoef53c812021-10-13 10:21:03 +02001312lysc_owner_module(const struct lysc_node *node)
1313{
1314 if (!node) {
1315 return NULL;
1316 }
1317
1318 for ( ; node->parent; node = node->parent) {}
1319 return node->module;
1320}
1321
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001322LIBYANG_API_DEF const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001323lys_nodetype2str(uint16_t nodetype)
1324{
Michal Vaskod989ba02020-08-24 10:59:24 +02001325 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001326 case LYS_CONTAINER:
1327 return "container";
1328 case LYS_CHOICE:
1329 return "choice";
1330 case LYS_LEAF:
1331 return "leaf";
1332 case LYS_LEAFLIST:
1333 return "leaf-list";
1334 case LYS_LIST:
1335 return "list";
1336 case LYS_ANYXML:
1337 return "anyxml";
1338 case LYS_ANYDATA:
1339 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001340 case LYS_CASE:
1341 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001342 case LYS_RPC:
1343 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001344 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001345 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001346 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001347 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001348 case LYS_USES:
1349 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001350 default:
1351 return "unknown";
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 Krejcid6b76452019-09-03 17:03:03 +02001829enum ly_stmt
Radek Krejcid54412f2020-12-17 20:25:35 +01001830lysp_match_kw(struct ly_in *in, uint64_t *indent)
David Sedlákc10e7902018-12-17 02:17:59 +01001831{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001832/**
Radek Krejcid54412f2020-12-17 20:25:35 +01001833 * @brief Move the input by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001834 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001835 *
1836 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001837 */
Radek Krejcid54412f2020-12-17 20:25:35 +01001838#define MOVE_IN(COUNT) \
1839 ly_in_skip(in, COUNT); \
1840 if (indent) { \
1841 (*indent)+=COUNT; \
1842 }
1843#define IF_KW(STR, LEN, STMT) \
1844 if (!strncmp(in->current, STR, LEN)) { \
1845 MOVE_IN(LEN); \
1846 (*kw)=STMT; \
1847 }
1848#define IF_KW_PREFIX(STR, LEN) \
1849 if (!strncmp(in->current, STR, LEN)) { \
1850 MOVE_IN(LEN);
1851#define IF_KW_PREFIX_END \
1852 }
David Sedlák572e7ab2019-06-04 16:01:58 +02001853
Michal Vasko63f3d842020-07-08 10:10:14 +02001854 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001855 enum ly_stmt result = LY_STMT_NONE;
1856 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001857 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001858 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001859 case 'a':
Radek Krejcid54412f2020-12-17 20:25:35 +01001860 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001861 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1862 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1863 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001864 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001865 IF_KW("data", 4, LY_STMT_ANYDATA)
1866 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001867 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001868 break;
1869 case 'b':
Radek Krejcid54412f2020-12-17 20:25:35 +01001870 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001871 IF_KW("ase", 3, LY_STMT_BASE)
1872 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1873 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001874 break;
1875 case 'c':
Radek Krejcid54412f2020-12-17 20:25:35 +01001876 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001877 IF_KW("ase", 3, LY_STMT_CASE)
1878 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001879 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001880 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001881 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001882 IF_KW("ct", 2, LY_STMT_CONTACT)
1883 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001884 IF_KW_PREFIX_END
1885 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001886 break;
1887 case 'd':
Radek Krejcid54412f2020-12-17 20:25:35 +01001888 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001889 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001890 IF_KW("fault", 5, LY_STMT_DEFAULT)
1891 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001892 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001893 IF_KW("e", 1, LY_STMT_DEVIATE)
1894 else IF_KW("ion", 3, LY_STMT_DEVIATION)
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 'e':
Radek Krejcid54412f2020-12-17 20:25:35 +01001899 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001900 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001901 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001902 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1903 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001904 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001905 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001906 break;
1907 case 'f':
Radek Krejcid54412f2020-12-17 20:25:35 +01001908 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001909 IF_KW("eature", 6, LY_STMT_FEATURE)
1910 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001911 break;
1912 case 'g':
Radek Krejcid54412f2020-12-17 20:25:35 +01001913 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001914 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001915 break;
1916 case 'i':
Radek Krejcid54412f2020-12-17 20:25:35 +01001917 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001918 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1919 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1920 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001921 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001922 IF_KW("clude", 5, LY_STMT_INCLUDE)
1923 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001924 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001925 break;
1926 case 'k':
Radek Krejcid54412f2020-12-17 20:25:35 +01001927 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001928 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001929 break;
1930 case 'l':
Radek Krejcid54412f2020-12-17 20:25:35 +01001931 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001932 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001933 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1934 else IF_KW("af", 2, LY_STMT_LEAF)
1935 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001936 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001937 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001938 break;
1939 case 'm':
Radek Krejcid54412f2020-12-17 20:25:35 +01001940 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001941 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001942 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1943 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001944 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001945 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1946 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001947 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001948 IF_KW("ule", 3, LY_STMT_MODULE)
1949 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001950 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001951 break;
1952 case 'n':
Radek Krejcid54412f2020-12-17 20:25:35 +01001953 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001954 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1955 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001956 break;
1957 case 'o':
Radek Krejcid54412f2020-12-17 20:25:35 +01001958 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001959 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001960 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1961 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001962 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001963 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001964 break;
1965 case 'p':
Radek Krejcid54412f2020-12-17 20:25:35 +01001966 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001967 IF_KW("ath", 3, LY_STMT_PATH)
1968 else IF_KW("attern", 6, LY_STMT_PATTERN)
1969 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001970 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001971 IF_KW("fix", 3, LY_STMT_PREFIX)
1972 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001973 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001974 break;
1975 case 'r':
Radek Krejcid54412f2020-12-17 20:25:35 +01001976 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001977 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001978 else IF_KW_PREFIX("e", 1)
1979 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001980 IF_KW("erence", 6, LY_STMT_REFERENCE)
1981 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001982 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001983 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1984 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1985 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001986 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001987 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001988 break;
1989 case 's':
Radek Krejcid54412f2020-12-17 20:25:35 +01001990 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001991 IF_KW("tatus", 5, LY_STMT_STATUS)
1992 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001993 break;
1994 case 't':
Radek Krejcid54412f2020-12-17 20:25:35 +01001995 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001996 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1997 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001998 break;
1999 case 'u':
Radek Krejcid54412f2020-12-17 20:25:35 +01002000 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02002001 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02002002 IF_KW("que", 3, LY_STMT_UNIQUE)
2003 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02002004 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02002005 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02002006 break;
2007 case 'v':
Radek Krejcid54412f2020-12-17 20:25:35 +01002008 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002009 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02002010 break;
2011 case 'w':
Radek Krejcid54412f2020-12-17 20:25:35 +01002012 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002013 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02002014 break;
2015 case 'y':
Radek Krejcid54412f2020-12-17 20:25:35 +01002016 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002017 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
2018 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02002019 break;
David Sedlák23a59a62018-10-26 13:08:02 +02002020 default:
Radek Krejcid54412f2020-12-17 20:25:35 +01002021 /* if indent is not NULL we are matching keyword from YANG data */
2022 if (indent) {
Michal Vasko63f3d842020-07-08 10:10:14 +02002023 if (in->current[0] == ';') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002024 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002025 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02002026 } else if (in->current[0] == '{') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002027 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002028 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002029 } else if (in->current[0] == '}') {
Radek Krejcid54412f2020-12-17 20:25:35 +01002030 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02002031 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02002032 }
2033 }
David Sedlák23a59a62018-10-26 13:08:02 +02002034 break;
2035 }
2036
Michal Vasko63f3d842020-07-08 10:10:14 +02002037 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02002038 /* the keyword is not terminated */
2039 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02002040 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02002041 }
2042
David Sedlák1bccdfa2019-06-17 15:55:27 +02002043#undef IF_KW
2044#undef IF_KW_PREFIX
2045#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01002046#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02002047 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01002048
David Sedlák1bccdfa2019-06-17 15:55:27 +02002049 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02002050}
David Sedlákecf5eb82019-06-03 14:12:44 +02002051
Radek Krejci85ac8312021-03-03 20:21:33 +01002052LY_ERR
2053lysp_ext_find_definition(const struct ly_ctx *ctx, const struct lysp_ext_instance *ext, const struct lys_module **ext_mod,
2054 struct lysp_ext **ext_def)
2055{
Radek Krejci85ac8312021-03-03 20:21:33 +01002056 const char *tmp, *name, *prefix;
2057 size_t pref_len, name_len;
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002058 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci85ac8312021-03-03 20:21:33 +01002059 const struct lys_module *mod = NULL;
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002060 const struct lysp_submodule *submod;
Radek Krejci85ac8312021-03-03 20:21:33 +01002061
Michal Vasko193dacd2022-10-13 08:43:05 +02002062 if (ext_def) {
2063 *ext_def = NULL;
Radek Krejci85ac8312021-03-03 20:21:33 +01002064 }
2065
Radek Krejci677abea2021-03-05 14:24:53 +01002066 /* parse the prefix, the nodeid was previously already parsed and checked */
Radek Krejci85ac8312021-03-03 20:21:33 +01002067 tmp = ext->name;
Radek Krejci677abea2021-03-05 14:24:53 +01002068 ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len);
Radek Krejci85ac8312021-03-03 20:21:33 +01002069
2070 /* get module where the extension definition should be placed */
Michal Vasko193dacd2022-10-13 08:43:05 +02002071 *ext_mod = mod = ly_resolve_prefix(ctx, prefix, pref_len, ext->format, ext->prefix_data);
Radek Krejci85ac8312021-03-03 20:21:33 +01002072 if (!mod) {
Radek Krejci422afb12021-03-04 16:38:16 +01002073 LOGVAL(ctx, LYVE_REFERENCE, "Invalid prefix \"%.*s\" used for extension instance identifier.", (int)pref_len, prefix);
Radek Krejci85ac8312021-03-03 20:21:33 +01002074 return LY_EVALID;
2075 } else if (!mod->parsed->extensions) {
2076 LOGVAL(ctx, LYVE_REFERENCE, "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
2077 ext->name, mod->name);
2078 return LY_EVALID;
2079 }
2080
Michal Vasko193dacd2022-10-13 08:43:05 +02002081 if (!ext_def) {
2082 /* we are done */
2083 return LY_SUCCESS;
2084 }
2085
Radek Krejci85ac8312021-03-03 20:21:33 +01002086 /* find the parsed extension definition there */
2087 LY_ARRAY_FOR(mod->parsed->extensions, v) {
2088 if (!strcmp(name, mod->parsed->extensions[v].name)) {
2089 *ext_def = &mod->parsed->extensions[v];
2090 break;
2091 }
2092 }
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002093 if (!*ext_def) {
2094 LY_ARRAY_FOR(mod->parsed->includes, u) {
2095 submod = mod->parsed->includes[u].submodule;
2096 LY_ARRAY_FOR(submod->extensions, v) {
2097 if (!strcmp(name, submod->extensions[v].name)) {
2098 *ext_def = &submod->extensions[v];
2099 break;
2100 }
2101 }
2102 }
2103 }
Radek Krejci85ac8312021-03-03 20:21:33 +01002104
Michal Vaskoe168dbd2022-06-07 13:53:52 +02002105 if (!*ext_def) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002106 LOGVAL(ctx, LYVE_REFERENCE, "Extension definition of extension instance \"%s\" not found.", ext->name);
2107 return LY_EVALID;
2108 }
2109
Radek Krejci85ac8312021-03-03 20:21:33 +01002110 return LY_SUCCESS;
2111}
2112
2113LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002114lysp_ext_instance_resolve_argument(struct ly_ctx *ctx, struct lysp_ext_instance *ext_p)
Radek Krejci85ac8312021-03-03 20:21:33 +01002115{
Michal Vasko193dacd2022-10-13 08:43:05 +02002116 assert(ext_p->def);
2117
2118 if (!ext_p->def->argname || ext_p->argument) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002119 /* nothing to do */
2120 return LY_SUCCESS;
2121 }
2122
Radek Krejci8df109d2021-04-23 12:19:08 +02002123 if (ext_p->format == LY_VALUE_XML) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002124 /* schema was parsed from YIN and an argument is expected, ... */
Radek Krejci85ac8312021-03-03 20:21:33 +01002125 struct lysp_stmt *stmt = NULL;
2126
Michal Vasko193dacd2022-10-13 08:43:05 +02002127 if (ext_p->def->flags & LYS_YINELEM_TRUE) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002128 /* ... argument was the first XML child element */
2129 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {}
2130 if (stmt) {
2131 const char *arg, *ext, *name_arg, *name_ext, *prefix_arg, *prefix_ext;
2132 size_t name_arg_len, name_ext_len, prefix_arg_len, prefix_ext_len;
2133
2134 stmt = ext_p->child;
2135
2136 arg = stmt->stmt;
2137 ly_parse_nodeid(&arg, &prefix_arg, &prefix_arg_len, &name_arg, &name_arg_len);
Michal Vasko193dacd2022-10-13 08:43:05 +02002138 if (ly_strncmp(ext_p->def->argname, name_arg, name_arg_len)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002139 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" expects argument element \"%s\" as its first XML child, "
Michal Vasko193dacd2022-10-13 08:43:05 +02002140 "but \"%.*s\" element found.", ext_p->name, ext_p->def->argname, (int)name_arg_len, name_arg);
Radek Krejci85ac8312021-03-03 20:21:33 +01002141 return LY_EVALID;
2142 }
2143
2144 /* check namespace - all the extension instances must be qualified and argument element is expected in the same
2145 * namespace. Do not check just prefixes, there can be different prefixes pointing to the same namespace */
2146 ext = ext_p->name; /* include prefix */
2147 ly_parse_nodeid(&ext, &prefix_ext, &prefix_ext_len, &name_ext, &name_ext_len);
2148
2149 if (ly_resolve_prefix(ctx, prefix_ext, prefix_ext_len, ext_p->format, ext_p->prefix_data) !=
2150 ly_resolve_prefix(ctx, prefix_arg, prefix_arg_len, stmt->format, stmt->prefix_data)) {
2151 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" element and its argument element \"%s\" are "
Michal Vasko193dacd2022-10-13 08:43:05 +02002152 "expected in the same namespace, but they differ.", ext_p->name, ext_p->def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002153 return LY_EVALID;
2154 }
2155 }
2156 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +02002157 /* ... argument was one of the XML attributes which are represented as child stmt with LYS_YIN_ATTR flag */
Radek Krejci85ac8312021-03-03 20:21:33 +01002158 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002159 if (!strcmp(stmt->stmt, ext_p->def->argname)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01002160 /* this is the extension's argument */
2161 break;
2162 }
2163 }
2164 }
2165
2166 if (stmt) {
2167 LY_CHECK_RET(lydict_insert(ctx, stmt->arg, 0, &ext_p->argument));
2168 stmt->flags |= LYS_YIN_ARGUMENT;
2169 }
2170 }
2171
2172 if (!ext_p->argument) {
2173 /* missing extension's argument */
Michal Vasko193dacd2022-10-13 08:43:05 +02002174 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" missing argument %s\"%s\".",
2175 ext_p->name, (ext_p->def->flags & LYS_YINELEM_TRUE) ? "element " : "", ext_p->def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01002176 return LY_EVALID;
2177 }
2178
2179 return LY_SUCCESS;
2180}
2181
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002182LY_ARRAY_COUNT_TYPE
Radek Krejcifc596f92021-02-26 22:40:26 +01002183lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, enum ly_stmt substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002184{
2185 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
2186
Michal Vaskod989ba02020-08-24 10:59:24 +02002187 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejciab430862021-03-02 20:13:40 +01002188 if (ext[index].parent_stmt == substmt) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02002189 return index;
2190 }
2191 }
2192
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002193 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002194}
2195
Michal Vaskof8ded142022-02-17 10:48:01 +01002196LIBYANG_API_DEF const struct lysc_node *
Michal Vasko72244882021-01-12 15:21:05 +01002197lysc_data_node(const struct lysc_node *schema)
Michal Vasko62ed12d2020-05-21 10:08:25 +02002198{
2199 const struct lysc_node *parent;
2200
Michal Vasko72244882021-01-12 15:21:05 +01002201 parent = schema;
Radek Krejcidf549132021-01-21 10:32:32 +01002202 while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC |
2203 LYS_ACTION | LYS_NOTIF))) {
Radek Krejci7d95fbb2021-01-26 17:33:13 +01002204 parent = parent->parent;
Michal Vasko72244882021-01-12 15:21:05 +01002205 }
Michal Vasko62ed12d2020-05-21 10:08:25 +02002206
2207 return parent;
2208}
Michal Vaskof4258e12021-06-15 12:11:42 +02002209
2210ly_bool
2211lys_has_recompiled(const struct lys_module *mod)
2212{
2213 LY_ARRAY_COUNT_TYPE u;
2214
2215 if (LYSP_HAS_RECOMPILED(mod->parsed)) {
2216 return 1;
2217 }
2218
2219 LY_ARRAY_FOR(mod->parsed->includes, u) {
2220 if (LYSP_HAS_RECOMPILED(mod->parsed->includes[u].submodule)) {
2221 return 1;
2222 }
2223 }
2224
2225 return 0;
2226}
2227
2228ly_bool
2229lys_has_compiled(const struct lys_module *mod)
2230{
2231 LY_ARRAY_COUNT_TYPE u;
2232
2233 if (LYSP_HAS_COMPILED(mod->parsed)) {
2234 return 1;
2235 }
2236
2237 LY_ARRAY_FOR(mod->parsed->includes, u) {
2238 if (LYSP_HAS_COMPILED(mod->parsed->includes[u].submodule)) {
2239 return 1;
2240 }
2241 }
2242
2243 return 0;
2244}
Michal Vasko7ee5be22021-06-16 17:03:34 +02002245
2246ly_bool
Michal Vasko87cfdba2022-02-22 14:13:45 +01002247lys_has_dep_mods(const struct lys_module *mod)
Michal Vasko7ee5be22021-06-16 17:03:34 +02002248{
2249 LY_ARRAY_COUNT_TYPE u;
2250
Michal Vasko87cfdba2022-02-22 14:13:45 +01002251 /* features */
2252 if (mod->parsed->features) {
Michal Vasko7ee5be22021-06-16 17:03:34 +02002253 return 1;
2254 }
2255
Michal Vasko87cfdba2022-02-22 14:13:45 +01002256 /* groupings */
2257 if (mod->parsed->groupings) {
2258 return 1;
2259 }
Michal Vasko7ee5be22021-06-16 17:03:34 +02002260 LY_ARRAY_FOR(mod->parsed->includes, u) {
2261 if (mod->parsed->includes[u].submodule->groupings) {
2262 return 1;
2263 }
2264 }
2265
Michal Vasko87cfdba2022-02-22 14:13:45 +01002266 /* augments (adding nodes with leafrefs) */
2267 if (mod->parsed->augments) {
2268 return 1;
2269 }
2270 LY_ARRAY_FOR(mod->parsed->includes, u) {
2271 if (mod->parsed->includes[u].submodule->augments) {
2272 return 1;
2273 }
2274 }
2275
Michal Vasko7ee5be22021-06-16 17:03:34 +02002276 return 0;
2277}
Michal Vaskob872d0f2022-12-08 09:36:54 +01002278
2279const char *
2280lys_stmt_str(enum ly_stmt stmt)
2281{
2282 switch (stmt) {
2283 case LY_STMT_NONE:
2284 return NULL;
2285 case LY_STMT_ACTION:
2286 return "action";
2287 case LY_STMT_ANYDATA:
2288 return "anydata";
2289 case LY_STMT_ANYXML:
2290 return "anyxml";
2291 case LY_STMT_ARGUMENT:
2292 return "argument";
2293 case LY_STMT_ARG_TEXT:
2294 return "text";
2295 case LY_STMT_ARG_VALUE:
2296 return "value";
2297 case LY_STMT_AUGMENT:
2298 return "augment";
2299 case LY_STMT_BASE:
2300 return "base";
2301 case LY_STMT_BELONGS_TO:
2302 return "belongs-to";
2303 case LY_STMT_BIT:
2304 return "bit";
2305 case LY_STMT_CASE:
2306 return "case";
2307 case LY_STMT_CHOICE:
2308 return "choice";
2309 case LY_STMT_CONFIG:
2310 return "config";
2311 case LY_STMT_CONTACT:
2312 return "contact";
2313 case LY_STMT_CONTAINER:
2314 return "container";
2315 case LY_STMT_DEFAULT:
2316 return "default";
2317 case LY_STMT_DESCRIPTION:
2318 return "description";
2319 case LY_STMT_DEVIATE:
2320 return "deviate";
2321 case LY_STMT_DEVIATION:
2322 return "deviation";
2323 case LY_STMT_ENUM:
2324 return "enum";
2325 case LY_STMT_ERROR_APP_TAG:
2326 return "error-app-tag";
2327 case LY_STMT_ERROR_MESSAGE:
2328 return "error-message";
2329 case LY_STMT_EXTENSION:
2330 return "extension";
2331 case LY_STMT_EXTENSION_INSTANCE:
2332 return NULL;
2333 case LY_STMT_FEATURE:
2334 return "feature";
2335 case LY_STMT_FRACTION_DIGITS:
2336 return "fraction-digits";
2337 case LY_STMT_GROUPING:
2338 return "grouping";
2339 case LY_STMT_IDENTITY:
2340 return "identity";
2341 case LY_STMT_IF_FEATURE:
2342 return "if-feature";
2343 case LY_STMT_IMPORT:
2344 return "import";
2345 case LY_STMT_INCLUDE:
2346 return "include";
2347 case LY_STMT_INPUT:
2348 return "input";
2349 case LY_STMT_KEY:
2350 return "key";
2351 case LY_STMT_LEAF:
2352 return "leaf";
2353 case LY_STMT_LEAF_LIST:
2354 return "leaf-list";
2355 case LY_STMT_LENGTH:
2356 return "length";
2357 case LY_STMT_LIST:
2358 return "list";
2359 case LY_STMT_MANDATORY:
2360 return "mandatory";
2361 case LY_STMT_MAX_ELEMENTS:
2362 return "max-elements";
2363 case LY_STMT_MIN_ELEMENTS:
2364 return "min-elements";
2365 case LY_STMT_MODIFIER:
2366 return "modifier";
2367 case LY_STMT_MODULE:
2368 return "module";
2369 case LY_STMT_MUST:
2370 return "must";
2371 case LY_STMT_NAMESPACE:
2372 return "namespace";
2373 case LY_STMT_NOTIFICATION:
2374 return "notification";
2375 case LY_STMT_ORDERED_BY:
2376 return "ordered-by";
2377 case LY_STMT_ORGANIZATION:
2378 return "organization";
2379 case LY_STMT_OUTPUT:
2380 return "output";
2381 case LY_STMT_PATH:
2382 return "path";
2383 case LY_STMT_PATTERN:
2384 return "pattern";
2385 case LY_STMT_POSITION:
2386 return "position";
2387 case LY_STMT_PREFIX:
2388 return "prefix";
2389 case LY_STMT_PRESENCE:
2390 return "presence";
2391 case LY_STMT_RANGE:
2392 return "range";
2393 case LY_STMT_REFERENCE:
2394 return "reference";
2395 case LY_STMT_REFINE:
2396 return "refine";
2397 case LY_STMT_REQUIRE_INSTANCE:
2398 return "require-instance";
2399 case LY_STMT_REVISION:
2400 return "revision";
2401 case LY_STMT_REVISION_DATE:
2402 return "revision-date";
2403 case LY_STMT_RPC:
2404 return "rpc";
2405 case LY_STMT_STATUS:
2406 return "status";
2407 case LY_STMT_SUBMODULE:
2408 return "submodule";
2409 case LY_STMT_SYNTAX_LEFT_BRACE:
2410 return "{";
2411 case LY_STMT_SYNTAX_RIGHT_BRACE:
2412 return "}";
2413 case LY_STMT_SYNTAX_SEMICOLON:
2414 return ";";
2415 case LY_STMT_TYPE:
2416 return "type";
2417 case LY_STMT_TYPEDEF:
2418 return "typedef";
2419 case LY_STMT_UNIQUE:
2420 return "unique";
2421 case LY_STMT_UNITS:
2422 return "units";
2423 case LY_STMT_USES:
2424 return "uses";
2425 case LY_STMT_VALUE:
2426 return "value";
2427 case LY_STMT_WHEN:
2428 return "when";
2429 case LY_STMT_YANG_VERSION:
2430 return "yang-version";
2431 case LY_STMT_YIN_ELEMENT:
2432 return "yin-element";
2433 }
2434
2435 return NULL;
2436}
2437
2438const char *
2439lys_stmt_arg(enum ly_stmt stmt)
2440{
2441 switch (stmt) {
2442 case LY_STMT_NONE:
2443 case LY_STMT_ARG_TEXT:
2444 case LY_STMT_ARG_VALUE:
2445 case LY_STMT_EXTENSION_INSTANCE:
2446 case LY_STMT_INPUT:
2447 case LY_STMT_OUTPUT:
2448 case LY_STMT_SYNTAX_LEFT_BRACE:
2449 case LY_STMT_SYNTAX_RIGHT_BRACE:
2450 case LY_STMT_SYNTAX_SEMICOLON:
2451 return NULL;
2452 case LY_STMT_ACTION:
2453 case LY_STMT_ANYDATA:
2454 case LY_STMT_ANYXML:
2455 case LY_STMT_ARGUMENT:
2456 case LY_STMT_BASE:
2457 case LY_STMT_BIT:
2458 case LY_STMT_CASE:
2459 case LY_STMT_CHOICE:
2460 case LY_STMT_CONTAINER:
2461 case LY_STMT_ENUM:
2462 case LY_STMT_EXTENSION:
2463 case LY_STMT_FEATURE:
2464 case LY_STMT_GROUPING:
2465 case LY_STMT_IDENTITY:
2466 case LY_STMT_IF_FEATURE:
2467 case LY_STMT_LEAF:
2468 case LY_STMT_LEAF_LIST:
2469 case LY_STMT_LIST:
2470 case LY_STMT_MODULE:
2471 case LY_STMT_NOTIFICATION:
2472 case LY_STMT_RPC:
2473 case LY_STMT_SUBMODULE:
2474 case LY_STMT_TYPE:
2475 case LY_STMT_TYPEDEF:
2476 case LY_STMT_UNITS:
2477 case LY_STMT_USES:
2478 return "name";
2479 case LY_STMT_AUGMENT:
2480 case LY_STMT_DEVIATION:
2481 case LY_STMT_REFINE:
2482 return "target-node";
2483 case LY_STMT_BELONGS_TO:
2484 case LY_STMT_IMPORT:
2485 case LY_STMT_INCLUDE:
2486 return "module";
2487 case LY_STMT_CONFIG:
2488 case LY_STMT_DEFAULT:
2489 case LY_STMT_DEVIATE:
2490 case LY_STMT_ERROR_APP_TAG:
2491 case LY_STMT_ERROR_MESSAGE:
2492 case LY_STMT_FRACTION_DIGITS:
2493 case LY_STMT_KEY:
2494 case LY_STMT_LENGTH:
2495 case LY_STMT_MANDATORY:
2496 case LY_STMT_MAX_ELEMENTS:
2497 case LY_STMT_MIN_ELEMENTS:
2498 case LY_STMT_MODIFIER:
2499 case LY_STMT_ORDERED_BY:
2500 case LY_STMT_PATH:
2501 case LY_STMT_PATTERN:
2502 case LY_STMT_POSITION:
2503 case LY_STMT_PREFIX:
2504 case LY_STMT_PRESENCE:
2505 case LY_STMT_RANGE:
2506 case LY_STMT_REQUIRE_INSTANCE:
2507 case LY_STMT_STATUS:
2508 case LY_STMT_VALUE:
2509 case LY_STMT_YANG_VERSION:
2510 case LY_STMT_YIN_ELEMENT:
2511 return "value";
2512 case LY_STMT_CONTACT:
2513 case LY_STMT_DESCRIPTION:
2514 case LY_STMT_ORGANIZATION:
2515 case LY_STMT_REFERENCE:
2516 return "text";
2517 case LY_STMT_MUST:
2518 case LY_STMT_WHEN:
2519 return "condition";
2520 case LY_STMT_NAMESPACE:
2521 return "uri";
2522 case LY_STMT_REVISION:
2523 case LY_STMT_REVISION_DATE:
2524 return "date";
2525 case LY_STMT_UNIQUE:
2526 return "tag";
2527 }
2528
2529 return NULL;
2530}
2531
2532uint8_t
2533lys_stmt_flags(enum ly_stmt stmt)
2534{
2535 switch (stmt) {
2536 case LY_STMT_NONE:
2537 case LY_STMT_ARG_TEXT:
2538 case LY_STMT_ARG_VALUE:
2539 case LY_STMT_DEFAULT:
2540 case LY_STMT_ERROR_APP_TAG:
2541 case LY_STMT_EXTENSION_INSTANCE:
2542 case LY_STMT_IF_FEATURE:
2543 case LY_STMT_INPUT:
2544 case LY_STMT_KEY:
2545 case LY_STMT_LENGTH:
2546 case LY_STMT_MUST:
2547 case LY_STMT_NAMESPACE:
2548 case LY_STMT_OUTPUT:
2549 case LY_STMT_PATH:
2550 case LY_STMT_PATTERN:
2551 case LY_STMT_PRESENCE:
2552 case LY_STMT_RANGE:
2553 case LY_STMT_SYNTAX_LEFT_BRACE:
2554 case LY_STMT_SYNTAX_RIGHT_BRACE:
2555 case LY_STMT_SYNTAX_SEMICOLON:
2556 case LY_STMT_UNIQUE:
2557 case LY_STMT_UNITS:
2558 case LY_STMT_WHEN:
2559 return 0;
2560 case LY_STMT_ACTION:
2561 case LY_STMT_ANYDATA:
2562 case LY_STMT_ANYXML:
2563 case LY_STMT_ARGUMENT:
2564 case LY_STMT_AUGMENT:
2565 case LY_STMT_BASE:
2566 case LY_STMT_BELONGS_TO:
2567 case LY_STMT_BIT:
2568 case LY_STMT_CASE:
2569 case LY_STMT_CHOICE:
2570 case LY_STMT_CONFIG:
2571 case LY_STMT_CONTAINER:
2572 case LY_STMT_DEVIATE:
2573 case LY_STMT_DEVIATION:
2574 case LY_STMT_ENUM:
2575 case LY_STMT_EXTENSION:
2576 case LY_STMT_FEATURE:
2577 case LY_STMT_FRACTION_DIGITS:
2578 case LY_STMT_GROUPING:
2579 case LY_STMT_IDENTITY:
2580 case LY_STMT_IMPORT:
2581 case LY_STMT_INCLUDE:
2582 case LY_STMT_LEAF:
2583 case LY_STMT_LEAF_LIST:
2584 case LY_STMT_LIST:
2585 case LY_STMT_MANDATORY:
2586 case LY_STMT_MAX_ELEMENTS:
2587 case LY_STMT_MIN_ELEMENTS:
2588 case LY_STMT_MODIFIER:
2589 case LY_STMT_MODULE:
2590 case LY_STMT_NOTIFICATION:
2591 case LY_STMT_ORDERED_BY:
2592 case LY_STMT_POSITION:
2593 case LY_STMT_PREFIX:
2594 case LY_STMT_REFINE:
2595 case LY_STMT_REQUIRE_INSTANCE:
2596 case LY_STMT_REVISION:
2597 case LY_STMT_REVISION_DATE:
2598 case LY_STMT_RPC:
2599 case LY_STMT_STATUS:
2600 case LY_STMT_SUBMODULE:
2601 case LY_STMT_TYPE:
2602 case LY_STMT_TYPEDEF:
2603 case LY_STMT_USES:
2604 case LY_STMT_VALUE:
2605 case LY_STMT_YANG_VERSION:
2606 case LY_STMT_YIN_ELEMENT:
2607 return LY_STMT_FLAG_ID;
2608 case LY_STMT_CONTACT:
2609 case LY_STMT_DESCRIPTION:
2610 case LY_STMT_ERROR_MESSAGE:
2611 case LY_STMT_ORGANIZATION:
2612 case LY_STMT_REFERENCE:
2613 return LY_STMT_FLAG_YIN;
2614 }
2615
2616 return 0;
2617}