blob: 8c16672e9023c5ea822c5aad12cd0fee690a9aad [file] [log] [blame]
Radek Krejci86d106e2018-10-18 09:53:19 +02001/**
2 * @file tree_schema_helpers.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejcie7b95092019-05-15 11:03:07 +02004 * @brief Parsing and validation helper functions for schema trees
Radek Krejci86d106e2018-10-18 09:53:19 +02005 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Krejci535ea9f2020-05-29 16:01:05 +020014
15#define _GNU_SOURCE
Radek Krejci86d106e2018-10-18 09:53:19 +020016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020018#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010019#include <stddef.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <stdint.h>
Radek Krejci9ed7a192018-10-31 16:23:51 +010021#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020023#include <time.h>
24
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020026#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "context.h"
Radek Krejci77114102021-03-10 15:21:57 +010028#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020029#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010030#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020031#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "log.h"
Michal Vasko69730152020-10-09 16:30:07 +020033#include "parser_schema.h"
Michal Vasko962b6cd2020-12-08 10:07:49 +010034#include "schema_compile.h"
Michal Vasko79135ae2020-12-16 10:08:35 +010035#include "schema_features.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020036#include "set.h"
37#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010038#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020039#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020040#include "tree_schema_internal.h"
41
Radek Krejci85747952019-06-07 16:43:43 +020042LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +020043lysp_check_prefix(struct lys_parser_ctx *ctx, struct lysp_import *imports, const char *module_prefix, const char **value)
Radek Krejci86d106e2018-10-18 09:53:19 +020044{
45 struct lysp_import *i;
46
Michal Vasko69730152020-10-09 16:30:07 +020047 if (module_prefix && (&module_prefix != value) && !strcmp(module_prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010048 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value);
Radek Krejci86d106e2018-10-18 09:53:19 +020049 return LY_EEXIST;
50 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +010051 LY_ARRAY_FOR(imports, struct lysp_import, i) {
Michal Vasko69730152020-10-09 16:30:07 +020052 if (i->prefix && (&i->prefix != value) && !strcmp(i->prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010053 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +010054 return LY_EEXIST;
Radek Krejci86d106e2018-10-18 09:53:19 +020055 }
56 }
57 return LY_SUCCESS;
58}
59
60LY_ERR
Juraj Vijtiuk74dad9e2021-05-26 12:42:14 +020061lysp_check_date(struct lys_parser_ctx *ctx, const char *date, size_t date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +020062{
Radek Krejci86d106e2018-10-18 09:53:19 +020063 struct tm tm, tm_;
64 char *r;
65
Michal Vaskob36053d2020-03-26 15:49:30 +010066 LY_CHECK_ARG_RET(ctx ? PARSER_CTX(ctx) : NULL, date, LY_EINVAL);
67 LY_CHECK_ERR_RET(date_len != LY_REV_SIZE - 1, LOGARG(ctx ? PARSER_CTX(ctx) : NULL, date_len), LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +020068
Radek Krejcif13b87b2020-12-01 22:02:17 +010069 /* check format: YYYY-MM-DD */
Radek Krejci1deb5be2020-08-26 16:43:36 +020070 for (uint8_t i = 0; i < date_len; i++) {
Michal Vasko69730152020-10-09 16:30:07 +020071 if ((i == 4) || (i == 7)) {
Radek Krejci86d106e2018-10-18 09:53:19 +020072 if (date[i] != '-') {
73 goto error;
74 }
75 } else if (!isdigit(date[i])) {
76 goto error;
77 }
78 }
79
80 /* check content, e.g. 2018-02-31 */
81 memset(&tm, 0, sizeof tm);
82 r = strptime(date, "%Y-%m-%d", &tm);
Michal Vasko69730152020-10-09 16:30:07 +020083 if (!r || (r != &date[LY_REV_SIZE - 1])) {
Radek Krejci86d106e2018-10-18 09:53:19 +020084 goto error;
85 }
86 memcpy(&tm_, &tm, sizeof tm);
87 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
88 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
89 /* checking days is enough, since other errors
90 * have been checked by strptime() */
91 goto error;
92 }
93
94 return LY_SUCCESS;
95
96error:
Radek Krejcid33273d2018-10-25 14:55:52 +020097 if (stmt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010098 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt);
Radek Krejcid33273d2018-10-25 14:55:52 +020099 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200100 return LY_EINVAL;
101}
102
103void
104lysp_sort_revisions(struct lysp_revision *revs)
105{
Radek Krejci857189e2020-09-01 13:26:36 +0200106 LY_ARRAY_COUNT_TYPE i, r;
Radek Krejci86d106e2018-10-18 09:53:19 +0200107 struct lysp_revision rev;
108
Radek Krejcic7d13e32020-12-09 12:32:24 +0100109 for (i = 1, r = 0; i < LY_ARRAY_COUNT(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200110 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200111 r = i;
112 }
113 }
114
115 if (r) {
116 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200117 memcpy(&rev, &revs[0], sizeof rev);
118 memcpy(&revs[0], &revs[r], sizeof rev);
119 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200120 }
121}
Radek Krejci151a5b72018-10-19 14:21:44 +0200122
Radek Krejcibbe09a92018-11-08 09:36:54 +0100123static const struct lysp_tpdf *
124lysp_type_match(const char *name, struct lysp_node *node)
125{
Radek Krejci0fb28562018-12-13 15:17:37 +0100126 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200127 LY_ARRAY_COUNT_TYPE u;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100128
Radek Krejci0fb28562018-12-13 15:17:37 +0100129 typedefs = lysp_node_typedefs(node);
130 LY_ARRAY_FOR(typedefs, u) {
131 if (!strcmp(name, typedefs[u].name)) {
132 /* match */
133 return &typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100134 }
135 }
136
137 return NULL;
138}
139
Radek Krejci4f28eda2018-11-12 11:46:16 +0100140static LY_DATA_TYPE
141lysp_type_str2builtin(const char *name, size_t len)
142{
143 if (len >= 4) { /* otherwise it does not match any built-in type */
144 if (name[0] == 'b') {
145 if (name[1] == 'i') {
Michal Vasko69730152020-10-09 16:30:07 +0200146 if ((len == 6) && !strncmp(&name[2], "nary", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100147 return LY_TYPE_BINARY;
Michal Vasko69730152020-10-09 16:30:07 +0200148 } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100149 return LY_TYPE_BITS;
150 }
Michal Vasko69730152020-10-09 16:30:07 +0200151 } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100152 return LY_TYPE_BOOL;
153 }
154 } else if (name[0] == 'd') {
Michal Vasko69730152020-10-09 16:30:07 +0200155 if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100156 return LY_TYPE_DEC64;
157 }
158 } else if (name[0] == 'e') {
Michal Vasko69730152020-10-09 16:30:07 +0200159 if ((len == 5) && !strncmp(&name[1], "mpty", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100160 return LY_TYPE_EMPTY;
Michal Vasko69730152020-10-09 16:30:07 +0200161 } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100162 return LY_TYPE_ENUM;
163 }
164 } else if (name[0] == 'i') {
165 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200166 if ((len == 4) && !strncmp(&name[2], "t8", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100167 return LY_TYPE_INT8;
168 } else if (len == 5) {
169 if (!strncmp(&name[2], "t16", 3)) {
170 return LY_TYPE_INT16;
171 } else if (!strncmp(&name[2], "t32", 3)) {
172 return LY_TYPE_INT32;
173 } else if (!strncmp(&name[2], "t64", 3)) {
174 return LY_TYPE_INT64;
175 }
Michal Vasko69730152020-10-09 16:30:07 +0200176 } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100177 return LY_TYPE_INST;
178 }
Michal Vasko69730152020-10-09 16:30:07 +0200179 } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100180 return LY_TYPE_IDENT;
181 }
182 } else if (name[0] == 'l') {
Michal Vasko69730152020-10-09 16:30:07 +0200183 if ((len == 7) && !strncmp(&name[1], "eafref", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100184 return LY_TYPE_LEAFREF;
185 }
186 } else if (name[0] == 's') {
Michal Vasko69730152020-10-09 16:30:07 +0200187 if ((len == 6) && !strncmp(&name[1], "tring", 5)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100188 return LY_TYPE_STRING;
189 }
190 } else if (name[0] == 'u') {
191 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200192 if ((len == 5) && !strncmp(&name[2], "ion", 3)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100193 return LY_TYPE_UNION;
194 }
Michal Vasko69730152020-10-09 16:30:07 +0200195 } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) {
196 if ((len == 5) && (name[4] == '8')) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100197 return LY_TYPE_UINT8;
198 } else if (len == 6) {
199 if (!strncmp(&name[4], "16", 2)) {
200 return LY_TYPE_UINT16;
201 } else if (!strncmp(&name[4], "32", 2)) {
202 return LY_TYPE_UINT32;
203 } else if (!strncmp(&name[4], "64", 2)) {
204 return LY_TYPE_UINT64;
205 }
206 }
207 }
208 }
209 }
210
211 return LY_TYPE_UNKNOWN;
212}
213
Radek Krejcibbe09a92018-11-08 09:36:54 +0100214LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +0100215lysp_type_find(const char *id, struct lysp_node *start_node, const struct lysp_module *start_module,
216 LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100217{
218 const char *str, *name;
219 struct lysp_tpdf *typedefs;
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100220 const struct lys_module *mod;
Michal Vaskoa99b3572021-02-01 11:54:58 +0100221 const struct lysp_module *local_module;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200222 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100223
224 assert(id);
225 assert(start_module);
226 assert(tpdf);
227 assert(node);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100228
Radek Krejci4f28eda2018-11-12 11:46:16 +0100229 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100230 str = strchr(id, ':');
231 if (str) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200232 mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_VALUE_SCHEMA, (void *)start_module);
Michal Vaskoa99b3572021-02-01 11:54:58 +0100233 local_module = mod ? mod->parsed : NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100234 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100235 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100236 } else {
Michal Vaskoa99b3572021-02-01 11:54:58 +0100237 local_module = start_module;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100238 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100239
240 /* check for built-in types */
241 *type = lysp_type_str2builtin(name, strlen(name));
242 if (*type) {
243 *tpdf = NULL;
244 return LY_SUCCESS;
245 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100246 }
Michal Vaskoa99b3572021-02-01 11:54:58 +0100247 LY_CHECK_RET(!local_module, LY_ENOTFOUND);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100248
Michal Vaskoa99b3572021-02-01 11:54:58 +0100249 if (start_node && (local_module == start_module)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100250 /* search typedefs in parent's nodes */
251 *node = start_node;
252 while (*node) {
253 *tpdf = lysp_type_match(name, *node);
254 if (*tpdf) {
255 /* match */
256 return LY_SUCCESS;
257 }
258 *node = (*node)->parent;
259 }
260 }
261
Michal Vasko915e5442021-06-08 14:59:21 +0200262 /* go to main module if in submodule */
263 local_module = local_module->mod->parsed;
264
Radek Krejcibbe09a92018-11-08 09:36:54 +0100265 /* search in top-level typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100266 if (local_module->typedefs) {
267 LY_ARRAY_FOR(local_module->typedefs, u) {
268 if (!strcmp(name, local_module->typedefs[u].name)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100269 /* match */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100270 *tpdf = &local_module->typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100271 return LY_SUCCESS;
272 }
273 }
274 }
275
Michal Vasko915e5442021-06-08 14:59:21 +0200276 /* search in all submodules' typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100277 LY_ARRAY_FOR(local_module->includes, u) {
278 typedefs = local_module->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100279 LY_ARRAY_FOR(typedefs, v) {
280 if (!strcmp(name, typedefs[v].name)) {
281 /* match */
282 *tpdf = &typedefs[v];
283 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100284 }
285 }
286 }
287
288 return LY_ENOTFOUND;
289}
290
David Sedlák6544c182019-07-12 13:17:33 +0200291LY_ERR
David Sedlák07869a52019-07-12 14:28:19 +0200292lysp_check_enum_name(struct lys_parser_ctx *ctx, const char *name, size_t name_len)
David Sedlák6544c182019-07-12 13:17:33 +0200293{
294 if (!name_len) {
295 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
296 return LY_EVALID;
297 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
298 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
Radek Krejci422afb12021-03-04 16:38:16 +0100299 (int)name_len, name);
David Sedlák6544c182019-07-12 13:17:33 +0200300 return LY_EVALID;
301 } else {
302 for (size_t u = 0; u < name_len; ++u) {
303 if (iscntrl(name[u])) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100304 LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
Radek Krejci422afb12021-03-04 16:38:16 +0100305 (int)name_len, name, u + 1);
David Sedlák6544c182019-07-12 13:17:33 +0200306 break;
307 }
308 }
309 }
310
311 return LY_SUCCESS;
312}
313
Michal Vaskob36053d2020-03-26 15:49:30 +0100314/**
aPiecekdc12b9f2021-06-25 10:55:47 +0200315 * @brief Insert @p name to hash table and if @p name has already
316 * been added, then log an error.
317 *
318 * This function is used to detect duplicate names.
319 *
320 * @param[in,out] ctx Context to log the error.
321 * @param[in,out] ht Hash table with top-level names.
322 * @param[in] name Inserted top-level identifier.
323 * @param[in] statement The name of the statement type from which
324 * @p name originated (eg typedef, feature, ...).
325 * @param[in] err_detail Optional error specification.
326 * @return LY_ERR, but LY_EEXIST is mapped to LY_EVALID.
327 */
328static LY_ERR
329lysp_check_dup_ht_insert(struct lys_parser_ctx *ctx, struct hash_table *ht,
330 const char *name, const char *statement, const char *err_detail)
331{
332 LY_ERR ret;
333 uint32_t hash;
334
335 hash = dict_hash(name, strlen(name));
336 ret = lyht_insert(ht, &name, hash, NULL);
337 if (ret == LY_EEXIST) {
338 if (err_detail) {
339 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT2, name, statement, err_detail);
340 } else {
341 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, name, statement);
342 }
343 ret = LY_EVALID;
344 }
345
346 return ret;
347}
348
349/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100350 * @brief Check name of a new type to avoid name collisions.
351 *
352 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
353 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
354 * @param[in] tpdf Typedef definition to check.
355 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
356 * typedefs are checked, caller is supposed to free the table.
Radek Krejcibbe09a92018-11-08 09:36:54 +0100357 * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise.
358 */
359static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100360lysp_check_dup_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
aPieceke1fbd952021-06-29 08:12:55 +0200361 struct hash_table *tpdfs_global)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100362{
363 struct lysp_node *parent;
364 uint32_t hash;
365 size_t name_len;
366 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200367 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100368 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100369
370 assert(ctx);
371 assert(tpdf);
372
373 name = tpdf->name;
374 name_len = strlen(name);
375
Radek Krejci4f28eda2018-11-12 11:46:16 +0100376 if (lysp_type_str2builtin(name, name_len)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100377 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with a built-in type.", name);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100378 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100379 }
380
381 /* check locally scoped typedefs (avoid name shadowing) */
382 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100383 typedefs = lysp_node_typedefs(node);
384 LY_ARRAY_FOR(typedefs, u) {
385 if (&typedefs[u] == tpdf) {
386 break;
387 }
388 if (!strcmp(name, typedefs[u].name)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100389 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with sibling type.", name);
Radek Krejci0fb28562018-12-13 15:17:37 +0100390 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100391 }
392 }
393 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200394 for (parent = node->parent; parent; parent = parent->parent) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100395 if (lysp_type_match(name, parent)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100396 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with another scoped type.", name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100397 return LY_EEXIST;
398 }
399 }
400 }
401
402 /* check collision with the top-level typedefs */
403 hash = dict_hash(name, name_len);
404 if (node) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100405 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100406 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - scoped type collide with a top-level type.", name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100407 return LY_EEXIST;
408 }
409 } else {
410 if (lyht_insert(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100411 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with another top-level type.", name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100412 return LY_EEXIST;
413 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100414 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
415 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
416 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100417 }
418
419 return LY_SUCCESS;
420}
421
Radek Krejci857189e2020-09-01 13:26:36 +0200422/**
423 * @brief Compare identifiers.
Michal Vasko62524a92021-02-26 10:08:50 +0100424 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200425 */
426static ly_bool
427lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Radek Krejcibbe09a92018-11-08 09:36:54 +0100428{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200429 return strcmp(val1, val2) == 0 ? 1 : 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100430}
431
432LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100433lysp_check_dup_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100434{
435 struct hash_table *ids_global;
Radek Krejci0fb28562018-12-13 15:17:37 +0100436 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200437 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200438 uint32_t i;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100439 LY_ERR ret = LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100440
441 /* check name collisions - typedefs and groupings */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100442 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200443 LY_ARRAY_FOR(mod->typedefs, v) {
aPieceke1fbd952021-06-29 08:12:55 +0200444 ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100445 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100446 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200447 LY_ARRAY_FOR(mod->includes, v) {
448 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200449 ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100450 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci3b1f9292018-11-08 10:58:35 +0100451 }
452 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200453 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
454 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
455 LY_ARRAY_FOR(typedefs, u) {
aPieceke1fbd952021-06-29 08:12:55 +0200456 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 +0100457 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100458 }
459 }
Michal Vasko405cc9e2020-12-01 12:01:27 +0100460
Radek Krejcibbe09a92018-11-08 09:36:54 +0100461cleanup:
462 lyht_free(ids_global);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100463 return ret;
464}
465
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100466static ly_bool
467ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
468{
469 void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
470
471 return ptr1 == ptr2 ? 1 : 0;
472}
473
474LY_ERR
475lysp_check_dup_features(struct lys_parser_ctx *ctx, struct lysp_module *mod)
476{
477 LY_ARRAY_COUNT_TYPE u;
478 struct hash_table *ht;
479 struct lysp_feature *f;
aPiecekdc12b9f2021-06-25 10:55:47 +0200480 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100481
aPiecekf6203072021-06-25 10:58:26 +0200482 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100483 LY_CHECK_RET(!ht, LY_EMEM);
484
485 /* add all module features into a hash table */
486 LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200487 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
488 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200489 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100490 }
491
492 /* add all submodule features into a hash table */
493 LY_ARRAY_FOR(mod->includes, u) {
494 LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
aPiecekea147b32021-06-29 07:52:47 +0200495 ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature",
496 "name collision with another top-level feature");
aPiecekdc12b9f2021-06-25 10:55:47 +0200497 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100498 }
499 }
500
501cleanup:
502 lyht_free(ht);
503 return ret;
504}
505
506LY_ERR
507lysp_check_dup_identities(struct lys_parser_ctx *ctx, struct lysp_module *mod)
508{
509 LY_ARRAY_COUNT_TYPE u;
510 struct hash_table *ht;
511 struct lysp_ident *i;
aPiecekdc12b9f2021-06-25 10:55:47 +0200512 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100513
aPiecekf6203072021-06-25 10:58:26 +0200514 ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100515 LY_CHECK_RET(!ht, LY_EMEM);
516
517 /* add all module identities into a hash table */
518 LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200519 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
520 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200521 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100522 }
523
524 /* add all submodule identities into a hash table */
525 LY_ARRAY_FOR(mod->includes, u) {
526 LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
aPiecekea147b32021-06-29 07:52:47 +0200527 ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity",
528 "name collision with another top-level identity");
aPiecekdc12b9f2021-06-25 10:55:47 +0200529 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100530 }
531 }
532
533cleanup:
534 lyht_free(ht);
535 return ret;
536}
537
Radek Krejci9ed7a192018-10-31 16:23:51 +0100538struct lysp_load_module_check_data {
539 const char *name;
540 const char *revision;
541 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200542 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100543};
544
545static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100546lysp_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 +0100547{
548 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100549 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200550 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100551 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100552 struct lysp_revision *revs;
553
554 name = mod ? mod->mod->name : submod->name;
555 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200556 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100557
558 if (info->name) {
559 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100560 if (strcmp(info->name, name)) {
561 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100562 return LY_EINVAL;
563 }
564 }
565 if (info->revision) {
566 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100567 if (!revs || strcmp(info->revision, revs[0].date)) {
568 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200569 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100570 return LY_EINVAL;
571 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200572 } else if (!latest_revision) {
573 /* do not log, we just need to drop the schema and use the latest revision from the context */
574 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100575 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100576 if (submod) {
577 assert(info->submoduleof);
578
Radek Krejci9ed7a192018-10-31 16:23:51 +0100579 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200580 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100581 LOGVAL(ctx, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200582 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100583 return LY_EVALID;
584 }
585 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100586 if (submod->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100587 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100588 return LY_EVALID;
589 }
590 }
591 if (info->path) {
592 /* check that name and revision match filename */
593 filename = strrchr(info->path, '/');
594 if (!filename) {
595 filename = info->path;
596 } else {
597 filename++;
598 }
599 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100600 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100601 rev = strchr(filename, '@');
602 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100603 if (strncmp(filename, name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +0200604 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100605 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100606 }
607 /* revision */
608 if (rev) {
609 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100610 if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100611 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +0200612 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100613 }
614 }
615 }
616 return LY_SUCCESS;
617}
618
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200619/**
Michal Vasko4e205e82021-06-08 14:01:47 +0200620 * @brief Parse a (sub)module from a local file and add into the context.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200621 *
622 * This function does not check the presence of the (sub)module in context, it should be done before calling this function.
623 *
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200624 * @param[in] ctx libyang context where to work.
625 * @param[in] name Name of the (sub)module to load.
626 * @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 +0200627 * @param[in] main_ctx Parser context of the main module in case of loading submodule.
628 * @param[in] main_name Main module name in case of loading submodule.
629 * @param[in] required Module is required so error (even if the input file not found) are important. If 0, there is some
630 * 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 +0200631 * @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 +0200632 * @param[out] result Parsed YANG schema tree of the requested module (struct lys_module*) or submodule (struct lysp_submodule*).
633 * If it is a module, it is already in the context!
Michal Vasko4e205e82021-06-08 14:01:47 +0200634 * @return LY_SUCCESS on success.
Michal Vasko4e205e82021-06-08 14:01:47 +0200635 * @return LY_ERR on error.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200636 */
637static LY_ERR
Michal Vasko4e205e82021-06-08 14:01:47 +0200638lys_parse_localfile(struct ly_ctx *ctx, const char *name, const char *revision, struct lys_parser_ctx *main_ctx,
Michal Vaskodd992582021-06-10 14:34:57 +0200639 const char *main_name, ly_bool required, struct ly_set *new_mods, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100640{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200641 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100642 char *filepath = NULL;
643 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100644 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100645 LY_ERR ret = LY_SUCCESS;
646 struct lysp_load_module_check_data check_data = {0};
647
Michal Vasko87f1cf02021-06-08 14:02:47 +0200648 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name,
649 revision, &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200650 if (!filepath) {
651 if (required) {
652 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200653 revision ? revision : "");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200654 }
655 return LY_ENOTFOUND;
656 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100657
658 LOGVRB("Loading schema from \"%s\" file.", filepath);
659
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200660 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200661 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +0200662 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100663 check_data.name = name;
664 check_data.revision = revision;
665 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800666 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200667 if (main_ctx) {
aPiecekc3e26142021-06-22 14:25:49 +0200668 ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data, new_mods,
Michal Vasko69730152020-10-09 16:30:07 +0200669 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200670 } else {
Michal Vaskodd992582021-06-10 14:34:57 +0200671 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 +0200672
673 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200674 ly_in_free(in, 1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200675 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100676
677 *result = mod;
678
679 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200680
Radek Krejci9ed7a192018-10-31 16:23:51 +0100681cleanup:
682 free(filepath);
683 return ret;
684}
685
Radek Krejcid33273d2018-10-25 14:55:52 +0200686LY_ERR
Michal Vaskodd992582021-06-10 14:34:57 +0200687lys_parse_load(struct ly_ctx *ctx, const char *name, const char *revision, struct ly_set *new_mods,
Michal Vasko4e205e82021-06-08 14:01:47 +0200688 struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200689{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100690 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200691 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200692
Radek Krejci9ed7a192018-10-31 16:23:51 +0100693 void (*module_data_free)(void *module_data, void *user_data) = NULL;
694 struct lysp_load_module_check_data check_data = {0};
Michal Vasko4e205e82021-06-08 14:01:47 +0200695 struct lys_module *ctx_latest = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200696 struct ly_in *in;
Radek Krejci086c7132018-10-26 15:29:04 +0200697
Michal Vaskodd992582021-06-10 14:34:57 +0200698 assert(mod && new_mods);
Radek Krejci0af46292019-01-11 16:02:31 +0100699
Michal Vasko0550b762020-11-24 18:04:08 +0100700 /*
701 * try to get the module from the context
702 */
Radek Krejci0af46292019-01-11 16:02:31 +0100703 if (!*mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100704 if (revision) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200705 /* get the specific revision */
Michal Vasko22df3f02020-08-24 13:29:22 +0200706 *mod = (struct lys_module *)ly_ctx_get_module(ctx, name, revision);
Radek Krejci0af46292019-01-11 16:02:31 +0100707 } else {
Michal Vasko4e205e82021-06-08 14:01:47 +0200708 /* get the requested module of the latest revision in the context */
709 *mod = (struct lys_module *)ly_ctx_get_module_latest(ctx, name);
710 if (*mod && ((*mod)->latest_revision == 1)) {
711 /* let us now search with callback and searchpaths to check if there is newer revision outside the context */
712 ctx_latest = *mod;
713 *mod = NULL;
Radek Krejcib3289d62019-09-18 12:21:39 +0200714 }
Radek Krejci0af46292019-01-11 16:02:31 +0100715 }
Radek Krejci086c7132018-10-26 15:29:04 +0200716 }
717
Michal Vasko4e205e82021-06-08 14:01:47 +0200718 /* we have module from the current context, circular check */
719 if (*mod && (*mod)->parsed->parsing) {
720 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
721 *mod = NULL;
722 return LY_EVALID;
Michal Vasko0550b762020-11-24 18:04:08 +0100723 }
Radek Krejci086c7132018-10-26 15:29:04 +0200724
Michal Vasko0550b762020-11-24 18:04:08 +0100725 /*
726 * no suitable module in the context, try to load it
727 */
728 if (!*mod) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100729 /* module not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200730 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
731search_clb:
Michal Vasko87f1cf02021-06-08 14:02:47 +0200732 if (ctx->imp_clb && !ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, &format, &module_data,
733 &module_data_free)) {
734 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
735 check_data.name = name;
736 check_data.revision = revision;
Michal Vaskodd992582021-06-10 14:34:57 +0200737 lys_parse_in(ctx, in, format, lysp_load_module_check, &check_data, new_mods, mod);
Michal Vasko87f1cf02021-06-08 14:02:47 +0200738 ly_in_free(in, 0);
739 if (module_data_free) {
740 module_data_free((void *)module_data, ctx->imp_clb_data);
Radek Krejci086c7132018-10-26 15:29:04 +0200741 }
742 }
743 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
744 goto search_file;
745 }
746 } else {
747search_file:
748 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
749 /* module was not received from the callback or there is no callback set */
Michal Vaskodd992582021-06-10 14:34:57 +0200750 lys_parse_localfile(ctx, name, revision, NULL, NULL, ctx_latest ? 0 : 1, new_mods, (void **)mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200751 }
Michal Vasko0550b762020-11-24 18:04:08 +0100752 if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejci086c7132018-10-26 15:29:04 +0200753 goto search_clb;
754 }
755 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100756
Radek Krejcib3289d62019-09-18 12:21:39 +0200757 /* update the latest_revision flag - here we have selected the latest available schema,
758 * consider that even the callback provides correct latest revision */
Michal Vasko0550b762020-11-24 18:04:08 +0100759 if (!*mod && ctx_latest) {
Michal Vasko003c44a2021-02-24 17:13:11 +0100760 LOGVRB("Newer revision than \"%s@%s\" not found, using this as the latest revision.", ctx_latest->name,
Michal Vasko0550b762020-11-24 18:04:08 +0100761 ctx_latest->revision);
762 ctx_latest->latest_revision = 2;
763 *mod = ctx_latest;
764 } else if (*mod && !revision && ((*mod)->latest_revision == 1)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100765 (*mod)->latest_revision = 2;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100766 }
Radek Krejci086c7132018-10-26 15:29:04 +0200767
Michal Vasko0550b762020-11-24 18:04:08 +0100768 if (!*mod) {
Michal Vasko4e205e82021-06-08 14:01:47 +0200769 LOGVAL(ctx, LYVE_REFERENCE, "Loading \"%s\" module failed.", name);
Michal Vasko0550b762020-11-24 18:04:08 +0100770 return LY_EVALID;
771 }
Radek Krejci086c7132018-10-26 15:29:04 +0200772 }
Radek Krejci086c7132018-10-26 15:29:04 +0200773
774 return LY_SUCCESS;
775}
776
777LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200778lysp_check_stringchar(struct lys_parser_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +0200779{
780 if (!is_yangutf8char(c)) {
781 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
782 return LY_EVALID;
783 }
784 return LY_SUCCESS;
785}
786
787LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200788lysp_check_identifierchar(struct lys_parser_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +0200789{
Michal Vasko69730152020-10-09 16:30:07 +0200790 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +0200791 if (!is_yangidentstartchar(c)) {
aPiecekc89b2242021-05-14 14:19:11 +0200792 if ((c < UCHAR_MAX) && isprint(c)) {
Michal Vasko7c769042021-03-25 12:20:49 +0100793 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
794 } else {
795 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character 0x%04x.", c);
796 }
David Sedlák4a650532019-07-10 11:55:18 +0200797 return LY_EVALID;
798 }
799 if (prefix) {
800 if (first) {
801 (*prefix) = 0;
802 } else {
803 (*prefix) = 2;
804 }
805 }
Michal Vasko69730152020-10-09 16:30:07 +0200806 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +0200807 (*prefix) = 1;
808 } else if (!is_yangidentchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200809 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200810 return LY_EVALID;
811 }
812
813 return LY_SUCCESS;
814}
815
Radek Krejci771928a2021-01-19 13:42:36 +0100816/**
817 * @brief Try to find the parsed submodule in main module for the given include record.
818 *
819 * @param[in] pctx main parser context
820 * @param[in] inc The include record with missing parsed submodule. According to include info try to find
821 * the corresponding parsed submodule in main module's includes.
822 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
823 * @return LY_ENOT - the parsed module was not found.
824 * @return LY_EVALID - YANG rule violation
825 */
826static LY_ERR
827lysp_get_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200828{
Radek Krejci771928a2021-01-19 13:42:36 +0100829 LY_ARRAY_COUNT_TYPE i;
830 struct lysp_module *main_pmod = pctx->parsed_mod->mod->parsed;
Michal Vasko69730152020-10-09 16:30:07 +0200831
Radek Krejci771928a2021-01-19 13:42:36 +0100832 LY_ARRAY_FOR(main_pmod->includes, i) {
833 if (strcmp(main_pmod->includes[i].name, inc->name)) {
834 continue;
835 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200836
Radek Krejci771928a2021-01-19 13:42:36 +0100837 if (inc->rev[0] && strncmp(inc->rev, main_pmod->includes[i].rev, LY_REV_SIZE)) {
838 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
839 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
840 ((struct lysp_submodule *)pctx->parsed_mod)->name, inc->rev,
841 main_pmod->includes[i].name, main_pmod->includes[i].rev, main_pmod->mod->name);
842 return LY_EVALID;
843 }
844
845 inc->submodule = main_pmod->includes[i].submodule;
846 return inc->submodule ? LY_SUCCESS : LY_ENOT;
847 }
848
849 if (main_pmod->version == LYS_VERSION_1_1) {
850 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
851 "YANG 1.1 requires all submodules to be included from main module. "
852 "But submodule \"%s\" includes submodule \"%s\" which is not included by main module \"%s\".",
853 ((struct lysp_submodule *)pctx->parsed_mod)->name, inc->name, main_pmod->mod->name);
854 return LY_EVALID;
855 } else {
856 return LY_ENOT;
857 }
858}
859
860/**
861 * @brief Make the copy of the given include record into the main module.
862 *
863 * YANG 1.0 does not require the main module to include all the submodules. Therefore, parsing submodules can cause
864 * reallocating and extending the includes array in the main module by the submodules included only in submodules.
865 *
866 * @param[in] pctx main parser context
867 * @param[in] inc Include record to copy into main module taken from @p pctx.
868 * @return LY_ERR value.
869 */
870static LY_ERR
871lysp_inject_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
872{
873 LY_ARRAY_COUNT_TYPE i;
874 struct lysp_include *inc_new, *inc_tofill = NULL;
875 struct lysp_module *main_pmod = pctx->parsed_mod->mod->parsed;
876
877 /* first, try to find the corresponding record with missing parsed submodule */
878 LY_ARRAY_FOR(main_pmod->includes, i) {
879 if (strcmp(main_pmod->includes[i].name, inc->name)) {
880 continue;
881 }
882 inc_tofill = &main_pmod->includes[i];
883 break;
884 }
885
886 if (inc_tofill) {
887 inc_tofill->submodule = inc->submodule;
888 } else {
889 LY_ARRAY_NEW_RET(PARSER_CTX(pctx), main_pmod->includes, inc_new, LY_EMEM);
890
891 inc_new->submodule = inc->submodule;
892 DUP_STRING_RET(PARSER_CTX(pctx), inc->name, inc_new->name);
893 DUP_STRING_RET(PARSER_CTX(pctx), inc->dsc, inc_new->dsc);
894 DUP_STRING_RET(PARSER_CTX(pctx), inc->ref, inc_new->ref);
895 /* TODO duplicate extensions */
896 memcpy(inc_new->rev, inc->rev, LY_REV_SIZE);
897 inc_new->injected = 1;
898 }
899 return LY_SUCCESS;
900}
901
902LY_ERR
aPiecekc3e26142021-06-22 14:25:49 +0200903lysp_load_submodules(struct lys_parser_ctx *pctx, struct lysp_module *pmod, struct ly_set *new_mods)
Radek Krejci771928a2021-01-19 13:42:36 +0100904{
905 LY_ARRAY_COUNT_TYPE u;
906 struct ly_ctx *ctx = PARSER_CTX(pctx);
907
908 LY_ARRAY_FOR(pmod->includes, u) {
909 LY_ERR ret = LY_SUCCESS;
910 struct lysp_submodule *submod = NULL;
911 struct lysp_include *inc = &pmod->includes[u];
912
913 if (inc->submodule) {
914 continue;
915 }
916
917 if (pmod->is_submod) {
918 /* try to find the submodule in the main module or its submodules */
919 ret = lysp_get_submodule(pctx, inc);
920 LY_CHECK_RET(ret && ret != LY_ENOT, ret);
921 LY_CHECK_RET(ret == LY_SUCCESS, LY_SUCCESS); /* submodule found in linked with the inc */
922 }
923
924 /* submodule not present in the main module, get the input data and parse it */
925 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcidf549132021-01-21 10:32:32 +0100926search_clb:
Radek Krejci771928a2021-01-19 13:42:36 +0100927 if (ctx->imp_clb) {
928 const char *submodule_data = NULL;
929 LYS_INFORMAT format = LYS_IN_UNKNOWN;
930 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
931 struct lysp_load_module_check_data check_data = {0};
932 struct ly_in *in;
933
934 if (ctx->imp_clb(pctx->parsed_mod->mod->name, NULL, inc->name,
935 inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
936 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
937 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
938 check_data.name = inc->name;
939 check_data.revision = inc->rev[0] ? inc->rev : NULL;
940 check_data.submoduleof = pctx->parsed_mod->mod->name;
aPiecekc3e26142021-06-22 14:25:49 +0200941 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, new_mods, &submod);
Radek Krejci771928a2021-01-19 13:42:36 +0100942
943 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
944 * submodule's include into main module, where it is missing */
945 inc = &pmod->includes[u];
946
947 ly_in_free(in, 0);
948 if (submodule_data_free) {
949 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
950 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200951 }
952 }
Radek Krejci771928a2021-01-19 13:42:36 +0100953 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
954 goto search_file;
955 }
956 } else {
Radek Krejcidf549132021-01-21 10:32:32 +0100957search_file:
Radek Krejci771928a2021-01-19 13:42:36 +0100958 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
959 /* submodule was not received from the callback or there is no callback set */
Michal Vasko4e205e82021-06-08 14:01:47 +0200960 lys_parse_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, pctx,
Radek Krejci771928a2021-01-19 13:42:36 +0100961 pctx->parsed_mod->mod->name, 1, NULL, (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100962
Radek Krejci771928a2021-01-19 13:42:36 +0100963 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
964 * submodule's include into main module, where it is missing */
965 inc = &pmod->includes[u];
966 }
967 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
968 goto search_clb;
969 }
970 }
971 if (submod) {
972 if (!inc->rev[0] && (submod->latest_revision == 1)) {
973 /* update the latest_revision flag - here we have selected the latest available schema,
974 * consider that even the callback provides correct latest revision */
975 submod->latest_revision = 2;
976 }
977
978 inc->submodule = submod;
979 if (ret == LY_ENOT) {
980 /* the submodule include is not present in YANG 1.0 main module - add it there */
981 LY_CHECK_RET(lysp_inject_submodule(pctx, &pmod->includes[u]));
982 }
983 }
984 if (!inc->submodule) {
985 LOGVAL(ctx, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name,
986 pctx->parsed_mod->is_submod ? ((struct lysp_submodule *)pctx->parsed_mod)->name : pctx->parsed_mod->mod->name);
987 return LY_EVALID;
988 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200989 }
990
991 return LY_SUCCESS;
992}
993
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100994API const struct lysc_when *
995lysc_has_when(const struct lysc_node *node)
996{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100997 struct lysc_when **when;
998
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100999 if (!node) {
1000 return NULL;
1001 }
1002
1003 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001004 when = lysc_node_when(node);
1005 if (when) {
1006 return when[0];
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001007 }
1008 node = node->parent;
1009 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1010
1011 return NULL;
1012}
1013
Radek Krejci0935f412019-08-20 16:15:18 +02001014API const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001015lys_nodetype2str(uint16_t nodetype)
1016{
Michal Vaskod989ba02020-08-24 10:59:24 +02001017 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001018 case LYS_CONTAINER:
1019 return "container";
1020 case LYS_CHOICE:
1021 return "choice";
1022 case LYS_LEAF:
1023 return "leaf";
1024 case LYS_LEAFLIST:
1025 return "leaf-list";
1026 case LYS_LIST:
1027 return "list";
1028 case LYS_ANYXML:
1029 return "anyxml";
1030 case LYS_ANYDATA:
1031 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001032 case LYS_CASE:
1033 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001034 case LYS_RPC:
1035 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001036 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001037 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001038 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001039 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001040 case LYS_USES:
1041 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001042 default:
1043 return "unknown";
1044 }
1045}
1046
Radek Krejci39b7fc22021-02-26 23:29:18 +01001047API enum ly_stmt
1048lys_nodetype2stmt(uint16_t nodetype)
1049{
1050 switch (nodetype) {
1051 case LYS_CONTAINER:
1052 return LY_STMT_CONTAINER;
1053 case LYS_CHOICE:
1054 return LY_STMT_CHOICE;
1055 case LYS_LEAF:
1056 return LY_STMT_LEAF;
1057 case LYS_LEAFLIST:
1058 return LY_STMT_LEAF_LIST;
1059 case LYS_LIST:
1060 return LY_STMT_LIST;
1061 case LYS_ANYXML:
1062 return LY_STMT_ANYXML;
1063 case LYS_ANYDATA:
1064 return LY_STMT_ANYDATA;
1065 case LYS_CASE:
1066 return LY_STMT_CASE;
1067 case LYS_RPC:
1068 return LY_STMT_RPC;
1069 case LYS_ACTION:
1070 return LY_STMT_ACTION;
1071 case LYS_NOTIF:
1072 return LY_STMT_NOTIFICATION;
1073 case LYS_USES:
1074 return LY_STMT_USES;
1075 case LYS_INPUT:
1076 return LY_STMT_INPUT;
1077 case LYS_OUTPUT:
1078 return LY_STMT_OUTPUT;
1079 default:
1080 return LY_STMT_NONE;
1081 }
1082}
1083
Radek Krejci693262f2019-04-29 15:23:20 +02001084const char *
1085lys_datatype2str(LY_DATA_TYPE basetype)
1086{
Michal Vaskod989ba02020-08-24 10:59:24 +02001087 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001088 case LY_TYPE_BINARY:
1089 return "binary";
1090 case LY_TYPE_UINT8:
1091 return "uint8";
1092 case LY_TYPE_UINT16:
1093 return "uint16";
1094 case LY_TYPE_UINT32:
1095 return "uint32";
1096 case LY_TYPE_UINT64:
1097 return "uint64";
1098 case LY_TYPE_STRING:
1099 return "string";
1100 case LY_TYPE_BITS:
1101 return "bits";
1102 case LY_TYPE_BOOL:
1103 return "boolean";
1104 case LY_TYPE_DEC64:
1105 return "decimal64";
1106 case LY_TYPE_EMPTY:
1107 return "empty";
1108 case LY_TYPE_ENUM:
1109 return "enumeration";
1110 case LY_TYPE_IDENT:
1111 return "identityref";
1112 case LY_TYPE_INST:
1113 return "instance-identifier";
1114 case LY_TYPE_LEAFREF:
1115 return "leafref";
1116 case LY_TYPE_UNION:
1117 return "union";
1118 case LY_TYPE_INT8:
1119 return "int8";
1120 case LY_TYPE_INT16:
1121 return "int16";
1122 case LY_TYPE_INT32:
1123 return "int32";
1124 case LY_TYPE_INT64:
1125 return "int64";
1126 default:
1127 return "unknown";
1128 }
1129}
1130
Radek Krejci056d0a82018-12-06 16:57:25 +01001131API const struct lysp_tpdf *
1132lysp_node_typedefs(const struct lysp_node *node)
1133{
Radek Krejci0fb28562018-12-13 15:17:37 +01001134 switch (node->nodetype) {
1135 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001136 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001137 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001138 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001139 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001140 return ((struct lysp_node_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001141 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001142 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001143 return ((struct lysp_node_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001144 case LYS_INPUT:
1145 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001146 return ((struct lysp_node_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001147 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001148 return ((struct lysp_node_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001149 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001150 return NULL;
1151 }
1152}
1153
Radek Krejci2a9fc652021-01-22 17:44:34 +01001154API const struct lysp_node_grp *
Radek Krejci53ea6152018-12-13 15:21:15 +01001155lysp_node_groupings(const struct lysp_node *node)
1156{
1157 switch (node->nodetype) {
1158 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001159 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001160 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001161 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001162 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001163 return ((struct lysp_node_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001164 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001165 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001166 return ((struct lysp_node_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001167 case LYS_INPUT:
1168 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001169 return ((struct lysp_node_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001170 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001171 return ((struct lysp_node_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001172 default:
1173 return NULL;
1174 }
1175}
1176
Radek Krejci2a9fc652021-01-22 17:44:34 +01001177struct lysp_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001178lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001179{
1180 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001181
Radek Krejcibbe09a92018-11-08 09:36:54 +01001182 switch (node->nodetype) {
1183 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001184 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001185 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001186 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001187 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001188 return &((struct lysp_node_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001189 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001190 return &((struct lysp_node_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001191 default:
1192 return NULL;
1193 }
1194}
1195
Radek Krejci2a9fc652021-01-22 17:44:34 +01001196API const struct lysp_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001197lysp_node_actions(const struct lysp_node *node)
1198{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001199 struct lysp_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001200
Michal Vasko22df3f02020-08-24 13:29:22 +02001201 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001202 if (actions) {
1203 return *actions;
1204 } else {
1205 return NULL;
1206 }
1207}
1208
Radek Krejci2a9fc652021-01-22 17:44:34 +01001209struct lysp_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001210lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001211{
1212 assert(node);
1213 switch (node->nodetype) {
1214 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001215 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001216 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001217 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001218 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001219 return &((struct lysp_node_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001220 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001221 return &((struct lysp_node_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001222 default:
1223 return NULL;
1224 }
1225}
1226
Radek Krejci2a9fc652021-01-22 17:44:34 +01001227API const struct lysp_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001228lysp_node_notifs(const struct lysp_node *node)
1229{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001230 struct lysp_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001231
Michal Vasko22df3f02020-08-24 13:29:22 +02001232 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001233 if (notifs) {
1234 return *notifs;
1235 } else {
1236 return NULL;
1237 }
1238}
1239
Radek Krejcibbe09a92018-11-08 09:36:54 +01001240struct lysp_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001241lysp_node_child_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001242{
1243 assert(node);
1244 switch (node->nodetype) {
1245 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001246 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001247 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001248 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001249 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001250 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001251 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001252 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001253 case LYS_GROUPING:
Radek Krejci01180ac2021-01-27 08:48:22 +01001254 return &((struct lysp_node_grp *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001255 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001256 return &((struct lysp_node_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001257 case LYS_INPUT:
1258 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001259 return &((struct lysp_node_action_inout *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001260 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001261 return &((struct lysp_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001262 default:
1263 return NULL;
1264 }
1265}
1266
Radek Krejci056d0a82018-12-06 16:57:25 +01001267API const struct lysp_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001268lysp_node_child(const struct lysp_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01001269{
Michal Vasko544e58a2021-01-28 14:33:41 +01001270 struct lysp_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001271
1272 if (!node) {
1273 return NULL;
1274 }
1275
Michal Vasko544e58a2021-01-28 14:33:41 +01001276 child = lysp_node_child_p((struct lysp_node *)node);
1277 if (child) {
1278 return *child;
Radek Krejci056d0a82018-12-06 16:57:25 +01001279 } else {
1280 return NULL;
1281 }
1282}
1283
Radek Krejci9a3823e2021-01-27 20:26:46 +01001284struct lysp_restr **
1285lysp_node_musts_p(const struct lysp_node *node)
1286{
1287 if (!node) {
1288 return NULL;
1289 }
1290
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001291 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001292 case LYS_CONTAINER:
1293 return &((struct lysp_node_container *)node)->musts;
1294 case LYS_LEAF:
1295 return &((struct lysp_node_leaf *)node)->musts;
1296 case LYS_LEAFLIST:
1297 return &((struct lysp_node_leaflist *)node)->musts;
1298 case LYS_LIST:
1299 return &((struct lysp_node_list *)node)->musts;
1300 case LYS_ANYXML:
1301 case LYS_ANYDATA:
1302 return &((struct lysp_node_anydata *)node)->musts;
1303 case LYS_NOTIF:
1304 return &((struct lysp_node_notif *)node)->musts;
1305 case LYS_INPUT:
1306 case LYS_OUTPUT:
1307 return &((struct lysp_node_action_inout *)node)->musts;
1308 default:
1309 return NULL;
1310 }
1311}
1312
1313struct lysp_restr *
1314lysp_node_musts(const struct lysp_node *node)
1315{
1316 struct lysp_restr **musts;
1317
1318 musts = lysp_node_musts_p(node);
1319 if (musts) {
1320 return *musts;
1321 } else {
1322 return NULL;
1323 }
1324}
1325
1326struct lysp_when **
1327lysp_node_when_p(const struct lysp_node *node)
1328{
1329 if (!node) {
1330 return NULL;
1331 }
1332
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001333 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001334 case LYS_CONTAINER:
1335 return &((struct lysp_node_container *)node)->when;
1336 case LYS_CHOICE:
1337 return &((struct lysp_node_choice *)node)->when;
1338 case LYS_LEAF:
1339 return &((struct lysp_node_leaf *)node)->when;
1340 case LYS_LEAFLIST:
1341 return &((struct lysp_node_leaflist *)node)->when;
1342 case LYS_LIST:
1343 return &((struct lysp_node_list *)node)->when;
1344 case LYS_ANYXML:
1345 case LYS_ANYDATA:
1346 return &((struct lysp_node_anydata *)node)->when;
1347 case LYS_CASE:
1348 return &((struct lysp_node_case *)node)->when;
1349 case LYS_USES:
1350 return &((struct lysp_node_uses *)node)->when;
1351 case LYS_AUGMENT:
1352 return &((struct lysp_node_augment *)node)->when;
1353 default:
1354 return NULL;
1355 }
1356}
1357
1358struct lysp_when *
1359lysp_node_when(const struct lysp_node *node)
1360{
1361 struct lysp_when **when;
1362
1363 when = lysp_node_when_p(node);
1364 if (when) {
1365 return *when;
1366 } else {
1367 return NULL;
1368 }
1369}
1370
Radek Krejci2a9fc652021-01-22 17:44:34 +01001371struct lysc_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001372lysc_node_actions_p(struct lysc_node *node)
1373{
1374 assert(node);
1375 switch (node->nodetype) {
1376 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001377 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001378 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001379 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001380 default:
1381 return NULL;
1382 }
1383}
1384
Radek Krejci2a9fc652021-01-22 17:44:34 +01001385API const struct lysc_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001386lysc_node_actions(const struct lysc_node *node)
1387{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001388 struct lysc_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001389
Michal Vasko22df3f02020-08-24 13:29:22 +02001390 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001391 if (actions) {
1392 return *actions;
1393 } else {
1394 return NULL;
1395 }
1396}
1397
Radek Krejci2a9fc652021-01-22 17:44:34 +01001398struct lysc_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001399lysc_node_notifs_p(struct lysc_node *node)
1400{
1401 assert(node);
1402 switch (node->nodetype) {
1403 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001404 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001405 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001406 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001407 default:
1408 return NULL;
1409 }
1410}
1411
Radek Krejci2a9fc652021-01-22 17:44:34 +01001412API const struct lysc_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001413lysc_node_notifs(const struct lysc_node *node)
1414{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001415 struct lysc_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001416
Michal Vasko22df3f02020-08-24 13:29:22 +02001417 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001418 if (notifs) {
1419 return *notifs;
1420 } else {
1421 return NULL;
1422 }
1423}
1424
Radek Krejcibbe09a92018-11-08 09:36:54 +01001425struct lysc_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001426lysc_node_child_p(const struct lysc_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001427{
Michal Vasko544e58a2021-01-28 14:33:41 +01001428 assert(node && !(node->nodetype & (LYS_RPC | LYS_ACTION)));
1429
Radek Krejcibbe09a92018-11-08 09:36:54 +01001430 switch (node->nodetype) {
1431 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001432 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001433 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001434 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001435 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001436 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001437 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001438 return &((struct lysc_node_list *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001439 case LYS_INPUT:
1440 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001441 return &((struct lysc_node_action_inout *)node)->child;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001442 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001443 return &((struct lysc_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001444 default:
1445 return NULL;
1446 }
1447}
1448
Radek Krejci056d0a82018-12-06 16:57:25 +01001449API const struct lysc_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001450lysc_node_child(const struct lysc_node *node)
Radek Krejcia3045382018-11-22 14:30:31 +01001451{
Michal Vasko544e58a2021-01-28 14:33:41 +01001452 struct lysc_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001453
1454 if (!node) {
1455 return NULL;
1456 }
1457
Michal Vasko544e58a2021-01-28 14:33:41 +01001458 if (node->nodetype & (LYS_RPC | LYS_ACTION)) {
1459 return &((struct lysc_node_action *)node)->input.node;
Radek Krejcibe154442021-01-21 11:06:36 +01001460 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01001461 child = lysc_node_child_p(node);
1462 if (child) {
1463 return *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001464 }
Michal Vasko2a668712020-10-21 11:48:09 +02001465 }
Michal Vasko544e58a2021-01-28 14:33:41 +01001466
1467 return NULL;
Michal Vasko2a668712020-10-21 11:48:09 +02001468}
1469
Radek Krejci9a3823e2021-01-27 20:26:46 +01001470struct lysc_must **
1471lysc_node_musts_p(const struct lysc_node *node)
1472{
1473 if (!node) {
1474 return NULL;
1475 }
1476
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001477 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001478 case LYS_CONTAINER:
1479 return &((struct lysc_node_container *)node)->musts;
1480 case LYS_LEAF:
1481 return &((struct lysc_node_leaf *)node)->musts;
1482 case LYS_LEAFLIST:
1483 return &((struct lysc_node_leaflist *)node)->musts;
1484 case LYS_LIST:
1485 return &((struct lysc_node_list *)node)->musts;
1486 case LYS_ANYXML:
1487 case LYS_ANYDATA:
1488 return &((struct lysc_node_anydata *)node)->musts;
1489 case LYS_NOTIF:
1490 return &((struct lysc_node_notif *)node)->musts;
1491 case LYS_INPUT:
1492 case LYS_OUTPUT:
1493 return &((struct lysc_node_action_inout *)node)->musts;
1494 default:
1495 return NULL;
1496 }
1497}
1498
1499API struct lysc_must *
1500lysc_node_musts(const struct lysc_node *node)
1501{
1502 struct lysc_must **must_p;
1503
1504 must_p = lysc_node_musts_p(node);
1505 if (must_p) {
1506 return *must_p;
1507 } else {
1508 return NULL;
1509 }
1510}
1511
1512struct lysc_when ***
1513lysc_node_when_p(const struct lysc_node *node)
1514{
1515 if (!node) {
1516 return NULL;
1517 }
1518
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001519 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001520 case LYS_CONTAINER:
1521 return &((struct lysc_node_container *)node)->when;
1522 case LYS_CHOICE:
1523 return &((struct lysc_node_choice *)node)->when;
1524 case LYS_LEAF:
1525 return &((struct lysc_node_leaf *)node)->when;
1526 case LYS_LEAFLIST:
1527 return &((struct lysc_node_leaflist *)node)->when;
1528 case LYS_LIST:
1529 return &((struct lysc_node_list *)node)->when;
1530 case LYS_ANYXML:
1531 case LYS_ANYDATA:
1532 return &((struct lysc_node_anydata *)node)->when;
1533 case LYS_CASE:
1534 return &((struct lysc_node_case *)node)->when;
1535 case LYS_NOTIF:
1536 return &((struct lysc_node_notif *)node)->when;
1537 case LYS_RPC:
1538 case LYS_ACTION:
1539 return &((struct lysc_node_action *)node)->when;
1540 default:
1541 return NULL;
1542 }
1543}
1544
1545API struct lysc_when **
1546lysc_node_when(const struct lysc_node *node)
1547{
1548 struct lysc_when ***when_p;
1549
1550 when_p = lysc_node_when_p(node);
1551 if (when_p) {
1552 return *when_p;
1553 } else {
1554 return NULL;
1555 }
1556}
1557
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001558struct lys_module *
1559lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1560{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001561 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001562 if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) {
Michal Vasko69730152020-10-09 16:30:07 +02001563 return (struct lys_module *)ctx->list.objs[u];
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001564 }
1565 }
1566 return NULL;
1567}
1568
Radek Krejcid6b76452019-09-03 17:03:03 +02001569enum ly_stmt
Radek Krejcid54412f2020-12-17 20:25:35 +01001570lysp_match_kw(struct ly_in *in, uint64_t *indent)
David Sedlákc10e7902018-12-17 02:17:59 +01001571{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001572/**
Radek Krejcid54412f2020-12-17 20:25:35 +01001573 * @brief Move the input by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001574 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001575 *
1576 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001577 */
Radek Krejcid54412f2020-12-17 20:25:35 +01001578#define MOVE_IN(COUNT) \
1579 ly_in_skip(in, COUNT); \
1580 if (indent) { \
1581 (*indent)+=COUNT; \
1582 }
1583#define IF_KW(STR, LEN, STMT) \
1584 if (!strncmp(in->current, STR, LEN)) { \
1585 MOVE_IN(LEN); \
1586 (*kw)=STMT; \
1587 }
1588#define IF_KW_PREFIX(STR, LEN) \
1589 if (!strncmp(in->current, STR, LEN)) { \
1590 MOVE_IN(LEN);
1591#define IF_KW_PREFIX_END \
1592 }
David Sedlák572e7ab2019-06-04 16:01:58 +02001593
Michal Vasko63f3d842020-07-08 10:10:14 +02001594 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001595 enum ly_stmt result = LY_STMT_NONE;
1596 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001597 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001598 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001599 case 'a':
Radek Krejcid54412f2020-12-17 20:25:35 +01001600 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001601 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1602 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1603 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001604 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001605 IF_KW("data", 4, LY_STMT_ANYDATA)
1606 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001607 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001608 break;
1609 case 'b':
Radek Krejcid54412f2020-12-17 20:25:35 +01001610 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001611 IF_KW("ase", 3, LY_STMT_BASE)
1612 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1613 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001614 break;
1615 case 'c':
Radek Krejcid54412f2020-12-17 20:25:35 +01001616 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001617 IF_KW("ase", 3, LY_STMT_CASE)
1618 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001619 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001620 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001621 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001622 IF_KW("ct", 2, LY_STMT_CONTACT)
1623 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001624 IF_KW_PREFIX_END
1625 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001626 break;
1627 case 'd':
Radek Krejcid54412f2020-12-17 20:25:35 +01001628 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001629 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001630 IF_KW("fault", 5, LY_STMT_DEFAULT)
1631 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001632 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001633 IF_KW("e", 1, LY_STMT_DEVIATE)
1634 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001635 IF_KW_PREFIX_END
1636 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001637 break;
1638 case 'e':
Radek Krejcid54412f2020-12-17 20:25:35 +01001639 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001640 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001641 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001642 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1643 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001644 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001645 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001646 break;
1647 case 'f':
Radek Krejcid54412f2020-12-17 20:25:35 +01001648 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001649 IF_KW("eature", 6, LY_STMT_FEATURE)
1650 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001651 break;
1652 case 'g':
Radek Krejcid54412f2020-12-17 20:25:35 +01001653 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001654 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001655 break;
1656 case 'i':
Radek Krejcid54412f2020-12-17 20:25:35 +01001657 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001658 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1659 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1660 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001661 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001662 IF_KW("clude", 5, LY_STMT_INCLUDE)
1663 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001664 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001665 break;
1666 case 'k':
Radek Krejcid54412f2020-12-17 20:25:35 +01001667 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001668 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001669 break;
1670 case 'l':
Radek Krejcid54412f2020-12-17 20:25:35 +01001671 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001672 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001673 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1674 else IF_KW("af", 2, LY_STMT_LEAF)
1675 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001676 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001677 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001678 break;
1679 case 'm':
Radek Krejcid54412f2020-12-17 20:25:35 +01001680 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001681 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001682 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1683 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001684 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001685 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1686 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001687 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001688 IF_KW("ule", 3, LY_STMT_MODULE)
1689 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001690 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001691 break;
1692 case 'n':
Radek Krejcid54412f2020-12-17 20:25:35 +01001693 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001694 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1695 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001696 break;
1697 case 'o':
Radek Krejcid54412f2020-12-17 20:25:35 +01001698 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001699 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001700 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1701 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001702 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001703 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001704 break;
1705 case 'p':
Radek Krejcid54412f2020-12-17 20:25:35 +01001706 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001707 IF_KW("ath", 3, LY_STMT_PATH)
1708 else IF_KW("attern", 6, LY_STMT_PATTERN)
1709 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001710 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001711 IF_KW("fix", 3, LY_STMT_PREFIX)
1712 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001713 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001714 break;
1715 case 'r':
Radek Krejcid54412f2020-12-17 20:25:35 +01001716 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001717 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001718 else IF_KW_PREFIX("e", 1)
1719 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001720 IF_KW("erence", 6, LY_STMT_REFERENCE)
1721 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001722 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001723 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1724 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1725 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001726 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001727 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001728 break;
1729 case 's':
Radek Krejcid54412f2020-12-17 20:25:35 +01001730 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001731 IF_KW("tatus", 5, LY_STMT_STATUS)
1732 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001733 break;
1734 case 't':
Radek Krejcid54412f2020-12-17 20:25:35 +01001735 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001736 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1737 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001738 break;
1739 case 'u':
Radek Krejcid54412f2020-12-17 20:25:35 +01001740 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001741 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001742 IF_KW("que", 3, LY_STMT_UNIQUE)
1743 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001744 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001745 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001746 break;
1747 case 'v':
Radek Krejcid54412f2020-12-17 20:25:35 +01001748 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001749 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001750 break;
1751 case 'w':
Radek Krejcid54412f2020-12-17 20:25:35 +01001752 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001753 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001754 break;
1755 case 'y':
Radek Krejcid54412f2020-12-17 20:25:35 +01001756 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001757 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
1758 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001759 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001760 default:
Radek Krejcid54412f2020-12-17 20:25:35 +01001761 /* if indent is not NULL we are matching keyword from YANG data */
1762 if (indent) {
Michal Vasko63f3d842020-07-08 10:10:14 +02001763 if (in->current[0] == ';') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001764 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001765 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02001766 } else if (in->current[0] == '{') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001767 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001768 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001769 } else if (in->current[0] == '}') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001770 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001771 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001772 }
1773 }
David Sedlák23a59a62018-10-26 13:08:02 +02001774 break;
1775 }
1776
Michal Vasko63f3d842020-07-08 10:10:14 +02001777 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02001778 /* the keyword is not terminated */
1779 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001780 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02001781 }
1782
David Sedlák1bccdfa2019-06-17 15:55:27 +02001783#undef IF_KW
1784#undef IF_KW_PREFIX
1785#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01001786#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02001787 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01001788
David Sedlák1bccdfa2019-06-17 15:55:27 +02001789 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02001790}
David Sedlákecf5eb82019-06-03 14:12:44 +02001791
Radek Krejci85ac8312021-03-03 20:21:33 +01001792LY_ERR
1793lysp_ext_find_definition(const struct ly_ctx *ctx, const struct lysp_ext_instance *ext, const struct lys_module **ext_mod,
1794 struct lysp_ext **ext_def)
1795{
Radek Krejci85ac8312021-03-03 20:21:33 +01001796 const char *tmp, *name, *prefix;
1797 size_t pref_len, name_len;
1798 LY_ARRAY_COUNT_TYPE v;
1799 const struct lys_module *mod = NULL;
1800
Radek Krejcicbb62422021-03-15 09:29:21 +01001801 assert(ext_def);
1802
Radek Krejci85ac8312021-03-03 20:21:33 +01001803 *ext_def = NULL;
1804 if (ext_mod) {
1805 *ext_mod = NULL;
1806 }
1807
Radek Krejci677abea2021-03-05 14:24:53 +01001808 /* parse the prefix, the nodeid was previously already parsed and checked */
Radek Krejci85ac8312021-03-03 20:21:33 +01001809 tmp = ext->name;
Radek Krejci677abea2021-03-05 14:24:53 +01001810 ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len);
Radek Krejci85ac8312021-03-03 20:21:33 +01001811
1812 /* get module where the extension definition should be placed */
1813 mod = ly_resolve_prefix(ctx, prefix, pref_len, ext->format, ext->prefix_data);
1814 if (!mod) {
Radek Krejci422afb12021-03-04 16:38:16 +01001815 LOGVAL(ctx, LYVE_REFERENCE, "Invalid prefix \"%.*s\" used for extension instance identifier.", (int)pref_len, prefix);
Radek Krejci85ac8312021-03-03 20:21:33 +01001816 return LY_EVALID;
1817 } else if (!mod->parsed->extensions) {
1818 LOGVAL(ctx, LYVE_REFERENCE, "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
1819 ext->name, mod->name);
1820 return LY_EVALID;
1821 }
1822
1823 /* find the parsed extension definition there */
1824 LY_ARRAY_FOR(mod->parsed->extensions, v) {
1825 if (!strcmp(name, mod->parsed->extensions[v].name)) {
1826 *ext_def = &mod->parsed->extensions[v];
1827 break;
1828 }
1829 }
1830
Radek Krejcicbb62422021-03-15 09:29:21 +01001831 if (!(*ext_def)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001832 LOGVAL(ctx, LYVE_REFERENCE, "Extension definition of extension instance \"%s\" not found.", ext->name);
1833 return LY_EVALID;
1834 }
1835
1836 if (ext_mod) {
1837 *ext_mod = mod;
1838 }
1839 return LY_SUCCESS;
1840}
1841
1842LY_ERR
1843lysp_ext_instance_resolve_argument(struct ly_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysp_ext *ext_def)
1844{
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001845 if (!ext_def->argname || ext_p->argument) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001846 /* nothing to do */
1847 return LY_SUCCESS;
1848 }
1849
Radek Krejci8df109d2021-04-23 12:19:08 +02001850 if (ext_p->format == LY_VALUE_XML) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001851 /* Schema was parsed from YIN and an argument is expected, ... */
1852 struct lysp_stmt *stmt = NULL;
1853
1854 if (ext_def->flags & LYS_YINELEM_TRUE) {
1855 /* ... argument was the first XML child element */
1856 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {}
1857 if (stmt) {
1858 const char *arg, *ext, *name_arg, *name_ext, *prefix_arg, *prefix_ext;
1859 size_t name_arg_len, name_ext_len, prefix_arg_len, prefix_ext_len;
1860
1861 stmt = ext_p->child;
1862
1863 arg = stmt->stmt;
1864 ly_parse_nodeid(&arg, &prefix_arg, &prefix_arg_len, &name_arg, &name_arg_len);
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001865 if (ly_strncmp(ext_def->argname, name_arg, name_arg_len)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001866 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" expects argument element \"%s\" as its first XML child, "
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001867 "but \"%.*s\" element found.", ext_p->name, ext_def->argname, (int)name_arg_len, name_arg);
Radek Krejci85ac8312021-03-03 20:21:33 +01001868 return LY_EVALID;
1869 }
1870
1871 /* check namespace - all the extension instances must be qualified and argument element is expected in the same
1872 * namespace. Do not check just prefixes, there can be different prefixes pointing to the same namespace */
1873 ext = ext_p->name; /* include prefix */
1874 ly_parse_nodeid(&ext, &prefix_ext, &prefix_ext_len, &name_ext, &name_ext_len);
1875
1876 if (ly_resolve_prefix(ctx, prefix_ext, prefix_ext_len, ext_p->format, ext_p->prefix_data) !=
1877 ly_resolve_prefix(ctx, prefix_arg, prefix_arg_len, stmt->format, stmt->prefix_data)) {
1878 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" element and its argument element \"%s\" are "
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001879 "expected in the same namespace, but they differ.", ext_p->name, ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01001880 return LY_EVALID;
1881 }
1882 }
1883 } else {
1884 /* ... argument was one of the XML attributes which are represented as child stmt
1885 * with LYS_YIN_ATTR flag */
1886 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001887 if (!strcmp(stmt->stmt, ext_def->argname)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001888 /* this is the extension's argument */
1889 break;
1890 }
1891 }
1892 }
1893
1894 if (stmt) {
1895 LY_CHECK_RET(lydict_insert(ctx, stmt->arg, 0, &ext_p->argument));
1896 stmt->flags |= LYS_YIN_ARGUMENT;
1897 }
1898 }
1899
1900 if (!ext_p->argument) {
1901 /* missing extension's argument */
1902 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" misses argument %s\"%s\".",
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001903 ext_p->name, (ext_def->flags & LYS_YINELEM_TRUE) ? "element " : "", ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01001904 return LY_EVALID;
1905 }
1906
1907 return LY_SUCCESS;
1908}
1909
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001910LY_ARRAY_COUNT_TYPE
Radek Krejcifc596f92021-02-26 22:40:26 +01001911lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, enum ly_stmt substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001912{
1913 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1914
Michal Vaskod989ba02020-08-24 10:59:24 +02001915 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejciab430862021-03-02 20:13:40 +01001916 if (ext[index].parent_stmt == substmt) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001917 return index;
1918 }
1919 }
1920
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001921 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001922}
1923
Michal Vasko62ed12d2020-05-21 10:08:25 +02001924const struct lysc_node *
Michal Vasko72244882021-01-12 15:21:05 +01001925lysc_data_node(const struct lysc_node *schema)
Michal Vasko62ed12d2020-05-21 10:08:25 +02001926{
1927 const struct lysc_node *parent;
1928
Michal Vasko72244882021-01-12 15:21:05 +01001929 parent = schema;
Radek Krejcidf549132021-01-21 10:32:32 +01001930 while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC |
1931 LYS_ACTION | LYS_NOTIF))) {
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001932 parent = parent->parent;
Michal Vasko72244882021-01-12 15:21:05 +01001933 }
Michal Vasko62ed12d2020-05-21 10:08:25 +02001934
1935 return parent;
1936}
Michal Vaskof4258e12021-06-15 12:11:42 +02001937
1938ly_bool
1939lys_has_recompiled(const struct lys_module *mod)
1940{
1941 LY_ARRAY_COUNT_TYPE u;
1942
1943 if (LYSP_HAS_RECOMPILED(mod->parsed)) {
1944 return 1;
1945 }
1946
1947 LY_ARRAY_FOR(mod->parsed->includes, u) {
1948 if (LYSP_HAS_RECOMPILED(mod->parsed->includes[u].submodule)) {
1949 return 1;
1950 }
1951 }
1952
1953 return 0;
1954}
1955
1956ly_bool
1957lys_has_compiled(const struct lys_module *mod)
1958{
1959 LY_ARRAY_COUNT_TYPE u;
1960
1961 if (LYSP_HAS_COMPILED(mod->parsed)) {
1962 return 1;
1963 }
1964
1965 LY_ARRAY_FOR(mod->parsed->includes, u) {
1966 if (LYSP_HAS_COMPILED(mod->parsed->includes[u].submodule)) {
1967 return 1;
1968 }
1969 }
1970
1971 return 0;
1972}
Michal Vasko7ee5be22021-06-16 17:03:34 +02001973
1974ly_bool
1975lys_has_groupings(const struct lys_module *mod)
1976{
1977 LY_ARRAY_COUNT_TYPE u;
1978
1979 if (mod->parsed->groupings) {
1980 return 1;
1981 }
1982
1983 LY_ARRAY_FOR(mod->parsed->includes, u) {
1984 if (mod->parsed->includes[u].submodule->groupings) {
1985 return 1;
1986 }
1987 }
1988
1989 return 0;
1990}