blob: 2f0bf43eb9d5d4e0a724afde23c8e8536978c176 [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 Krejcie7b95092019-05-15 11:03:07 +020028#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010029#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020030#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010031#include "log.h"
Michal Vasko69730152020-10-09 16:30:07 +020032#include "parser_schema.h"
Michal Vasko962b6cd2020-12-08 10:07:49 +010033#include "schema_compile.h"
Michal Vasko79135ae2020-12-16 10:08:35 +010034#include "schema_features.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020035#include "set.h"
36#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010037#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020038#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020039#include "tree_schema_internal.h"
40
Radek Krejci85747952019-06-07 16:43:43 +020041LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +020042lysp_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 +020043{
44 struct lysp_import *i;
45
Michal Vasko69730152020-10-09 16:30:07 +020046 if (module_prefix && (&module_prefix != value) && !strcmp(module_prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010047 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value);
Radek Krejci86d106e2018-10-18 09:53:19 +020048 return LY_EEXIST;
49 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +010050 LY_ARRAY_FOR(imports, struct lysp_import, i) {
Michal Vasko69730152020-10-09 16:30:07 +020051 if (i->prefix && (&i->prefix != value) && !strcmp(i->prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010052 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +010053 return LY_EEXIST;
Radek Krejci86d106e2018-10-18 09:53:19 +020054 }
55 }
56 return LY_SUCCESS;
57}
58
59LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020060lysp_check_date(struct lys_parser_ctx *ctx, const char *date, uint8_t date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +020061{
Radek Krejci86d106e2018-10-18 09:53:19 +020062 struct tm tm, tm_;
63 char *r;
64
Michal Vaskob36053d2020-03-26 15:49:30 +010065 LY_CHECK_ARG_RET(ctx ? PARSER_CTX(ctx) : NULL, date, LY_EINVAL);
66 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 +020067
Radek Krejcif13b87b2020-12-01 22:02:17 +010068 /* check format: YYYY-MM-DD */
Radek Krejci1deb5be2020-08-26 16:43:36 +020069 for (uint8_t i = 0; i < date_len; i++) {
Michal Vasko69730152020-10-09 16:30:07 +020070 if ((i == 4) || (i == 7)) {
Radek Krejci86d106e2018-10-18 09:53:19 +020071 if (date[i] != '-') {
72 goto error;
73 }
74 } else if (!isdigit(date[i])) {
75 goto error;
76 }
77 }
78
79 /* check content, e.g. 2018-02-31 */
80 memset(&tm, 0, sizeof tm);
81 r = strptime(date, "%Y-%m-%d", &tm);
Michal Vasko69730152020-10-09 16:30:07 +020082 if (!r || (r != &date[LY_REV_SIZE - 1])) {
Radek Krejci86d106e2018-10-18 09:53:19 +020083 goto error;
84 }
85 memcpy(&tm_, &tm, sizeof tm);
86 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
87 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
88 /* checking days is enough, since other errors
89 * have been checked by strptime() */
90 goto error;
91 }
92
93 return LY_SUCCESS;
94
95error:
Radek Krejcid33273d2018-10-25 14:55:52 +020096 if (stmt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010097 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt);
Radek Krejcid33273d2018-10-25 14:55:52 +020098 }
Radek Krejci86d106e2018-10-18 09:53:19 +020099 return LY_EINVAL;
100}
101
102void
103lysp_sort_revisions(struct lysp_revision *revs)
104{
Radek Krejci857189e2020-09-01 13:26:36 +0200105 LY_ARRAY_COUNT_TYPE i, r;
Radek Krejci86d106e2018-10-18 09:53:19 +0200106 struct lysp_revision rev;
107
Radek Krejcic7d13e32020-12-09 12:32:24 +0100108 for (i = 1, r = 0; i < LY_ARRAY_COUNT(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200109 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200110 r = i;
111 }
112 }
113
114 if (r) {
115 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200116 memcpy(&rev, &revs[0], sizeof rev);
117 memcpy(&revs[0], &revs[r], sizeof rev);
118 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200119 }
120}
Radek Krejci151a5b72018-10-19 14:21:44 +0200121
Radek Krejcibbe09a92018-11-08 09:36:54 +0100122static const struct lysp_tpdf *
123lysp_type_match(const char *name, struct lysp_node *node)
124{
Radek Krejci0fb28562018-12-13 15:17:37 +0100125 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200126 LY_ARRAY_COUNT_TYPE u;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100127
Radek Krejci0fb28562018-12-13 15:17:37 +0100128 typedefs = lysp_node_typedefs(node);
129 LY_ARRAY_FOR(typedefs, u) {
130 if (!strcmp(name, typedefs[u].name)) {
131 /* match */
132 return &typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100133 }
134 }
135
136 return NULL;
137}
138
Radek Krejci4f28eda2018-11-12 11:46:16 +0100139static LY_DATA_TYPE
140lysp_type_str2builtin(const char *name, size_t len)
141{
142 if (len >= 4) { /* otherwise it does not match any built-in type */
143 if (name[0] == 'b') {
144 if (name[1] == 'i') {
Michal Vasko69730152020-10-09 16:30:07 +0200145 if ((len == 6) && !strncmp(&name[2], "nary", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100146 return LY_TYPE_BINARY;
Michal Vasko69730152020-10-09 16:30:07 +0200147 } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100148 return LY_TYPE_BITS;
149 }
Michal Vasko69730152020-10-09 16:30:07 +0200150 } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100151 return LY_TYPE_BOOL;
152 }
153 } else if (name[0] == 'd') {
Michal Vasko69730152020-10-09 16:30:07 +0200154 if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100155 return LY_TYPE_DEC64;
156 }
157 } else if (name[0] == 'e') {
Michal Vasko69730152020-10-09 16:30:07 +0200158 if ((len == 5) && !strncmp(&name[1], "mpty", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100159 return LY_TYPE_EMPTY;
Michal Vasko69730152020-10-09 16:30:07 +0200160 } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100161 return LY_TYPE_ENUM;
162 }
163 } else if (name[0] == 'i') {
164 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200165 if ((len == 4) && !strncmp(&name[2], "t8", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100166 return LY_TYPE_INT8;
167 } else if (len == 5) {
168 if (!strncmp(&name[2], "t16", 3)) {
169 return LY_TYPE_INT16;
170 } else if (!strncmp(&name[2], "t32", 3)) {
171 return LY_TYPE_INT32;
172 } else if (!strncmp(&name[2], "t64", 3)) {
173 return LY_TYPE_INT64;
174 }
Michal Vasko69730152020-10-09 16:30:07 +0200175 } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100176 return LY_TYPE_INST;
177 }
Michal Vasko69730152020-10-09 16:30:07 +0200178 } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100179 return LY_TYPE_IDENT;
180 }
181 } else if (name[0] == 'l') {
Michal Vasko69730152020-10-09 16:30:07 +0200182 if ((len == 7) && !strncmp(&name[1], "eafref", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100183 return LY_TYPE_LEAFREF;
184 }
185 } else if (name[0] == 's') {
Michal Vasko69730152020-10-09 16:30:07 +0200186 if ((len == 6) && !strncmp(&name[1], "tring", 5)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100187 return LY_TYPE_STRING;
188 }
189 } else if (name[0] == 'u') {
190 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200191 if ((len == 5) && !strncmp(&name[2], "ion", 3)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100192 return LY_TYPE_UNION;
193 }
Michal Vasko69730152020-10-09 16:30:07 +0200194 } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) {
195 if ((len == 5) && (name[4] == '8')) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100196 return LY_TYPE_UINT8;
197 } else if (len == 6) {
198 if (!strncmp(&name[4], "16", 2)) {
199 return LY_TYPE_UINT16;
200 } else if (!strncmp(&name[4], "32", 2)) {
201 return LY_TYPE_UINT32;
202 } else if (!strncmp(&name[4], "64", 2)) {
203 return LY_TYPE_UINT64;
204 }
205 }
206 }
207 }
208 }
209
210 return LY_TYPE_UNKNOWN;
211}
212
Radek Krejcibbe09a92018-11-08 09:36:54 +0100213LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +0100214lysp_type_find(const char *id, struct lysp_node *start_node, const struct lysp_module *start_module,
215 LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100216{
217 const char *str, *name;
218 struct lysp_tpdf *typedefs;
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100219 const struct lys_module *mod;
Michal Vaskoa99b3572021-02-01 11:54:58 +0100220 const struct lysp_module *local_module;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200221 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100222
223 assert(id);
224 assert(start_module);
225 assert(tpdf);
226 assert(node);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100227
Radek Krejci4f28eda2018-11-12 11:46:16 +0100228 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100229 str = strchr(id, ':');
230 if (str) {
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100231 mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_PREF_SCHEMA, (void *)start_module);
Michal Vaskoa99b3572021-02-01 11:54:58 +0100232 local_module = mod ? mod->parsed : NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100233 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100234 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100235 } else {
Michal Vaskoa99b3572021-02-01 11:54:58 +0100236 local_module = start_module;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100237 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100238
239 /* check for built-in types */
240 *type = lysp_type_str2builtin(name, strlen(name));
241 if (*type) {
242 *tpdf = NULL;
243 return LY_SUCCESS;
244 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100245 }
Michal Vaskoa99b3572021-02-01 11:54:58 +0100246 LY_CHECK_RET(!local_module, LY_ENOTFOUND);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100247
Michal Vaskoa99b3572021-02-01 11:54:58 +0100248 if (start_node && (local_module == start_module)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100249 /* search typedefs in parent's nodes */
250 *node = start_node;
251 while (*node) {
252 *tpdf = lysp_type_match(name, *node);
253 if (*tpdf) {
254 /* match */
255 return LY_SUCCESS;
256 }
257 *node = (*node)->parent;
258 }
259 }
260
261 /* search in top-level typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100262 if (local_module->typedefs) {
263 LY_ARRAY_FOR(local_module->typedefs, u) {
264 if (!strcmp(name, local_module->typedefs[u].name)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100265 /* match */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100266 *tpdf = &local_module->typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100267 return LY_SUCCESS;
268 }
269 }
270 }
271
272 /* search in submodules' typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100273 LY_ARRAY_FOR(local_module->includes, u) {
274 typedefs = local_module->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100275 LY_ARRAY_FOR(typedefs, v) {
276 if (!strcmp(name, typedefs[v].name)) {
277 /* match */
278 *tpdf = &typedefs[v];
279 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100280 }
281 }
282 }
283
284 return LY_ENOTFOUND;
285}
286
David Sedlák6544c182019-07-12 13:17:33 +0200287LY_ERR
David Sedlák07869a52019-07-12 14:28:19 +0200288lysp_check_enum_name(struct lys_parser_ctx *ctx, const char *name, size_t name_len)
David Sedlák6544c182019-07-12 13:17:33 +0200289{
290 if (!name_len) {
291 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
292 return LY_EVALID;
293 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
294 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
Michal Vasko69730152020-10-09 16:30:07 +0200295 name_len, name);
David Sedlák6544c182019-07-12 13:17:33 +0200296 return LY_EVALID;
297 } else {
298 for (size_t u = 0; u < name_len; ++u) {
299 if (iscntrl(name[u])) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100300 LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
Michal Vasko69730152020-10-09 16:30:07 +0200301 name_len, name, u + 1);
David Sedlák6544c182019-07-12 13:17:33 +0200302 break;
303 }
304 }
305 }
306
307 return LY_SUCCESS;
308}
309
Michal Vaskob36053d2020-03-26 15:49:30 +0100310/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100311 * @brief Check name of a new type to avoid name collisions.
312 *
313 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
314 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
315 * @param[in] tpdf Typedef definition to check.
316 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
317 * typedefs are checked, caller is supposed to free the table.
318 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
319 * typedefs are checked, caller is supposed to free the table.
320 * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise.
321 */
322static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100323lysp_check_dup_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
Radek Krejci0f969882020-08-21 16:56:47 +0200324 struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100325{
326 struct lysp_node *parent;
327 uint32_t hash;
328 size_t name_len;
329 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200330 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100331 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100332
333 assert(ctx);
334 assert(tpdf);
335
336 name = tpdf->name;
337 name_len = strlen(name);
338
Radek Krejci4f28eda2018-11-12 11:46:16 +0100339 if (lysp_type_str2builtin(name, name_len)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100340 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 +0100341 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100342 }
343
344 /* check locally scoped typedefs (avoid name shadowing) */
345 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100346 typedefs = lysp_node_typedefs(node);
347 LY_ARRAY_FOR(typedefs, u) {
348 if (&typedefs[u] == tpdf) {
349 break;
350 }
351 if (!strcmp(name, typedefs[u].name)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100352 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with sibling type.", name);
Radek Krejci0fb28562018-12-13 15:17:37 +0100353 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100354 }
355 }
356 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200357 for (parent = node->parent; parent; parent = parent->parent) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100358 if (lysp_type_match(name, parent)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100359 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 +0100360 return LY_EEXIST;
361 }
362 }
363 }
364
365 /* check collision with the top-level typedefs */
366 hash = dict_hash(name, name_len);
367 if (node) {
368 lyht_insert(tpdfs_scoped, &name, hash, NULL);
369 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100370 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 +0100371 return LY_EEXIST;
372 }
373 } else {
374 if (lyht_insert(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100375 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 +0100376 return LY_EEXIST;
377 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100378 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
379 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
380 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100381 }
382
383 return LY_SUCCESS;
384}
385
Radek Krejci857189e2020-09-01 13:26:36 +0200386/**
387 * @brief Compare identifiers.
Michal Vasko62524a92021-02-26 10:08:50 +0100388 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200389 */
390static ly_bool
391lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Radek Krejcibbe09a92018-11-08 09:36:54 +0100392{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200393 return strcmp(val1, val2) == 0 ? 1 : 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100394}
395
396LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100397lysp_check_dup_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100398{
399 struct hash_table *ids_global;
400 struct hash_table *ids_scoped;
Radek Krejci0fb28562018-12-13 15:17:37 +0100401 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200402 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200403 uint32_t i;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100404 LY_ERR ret = LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100405
406 /* check name collisions - typedefs and groupings */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100407 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
408 ids_scoped = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200409 LY_ARRAY_FOR(mod->typedefs, v) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100410 ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global, ids_scoped);
411 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100412 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200413 LY_ARRAY_FOR(mod->includes, v) {
414 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100415 ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global, ids_scoped);
416 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci3b1f9292018-11-08 10:58:35 +0100417 }
418 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200419 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
420 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
421 LY_ARRAY_FOR(typedefs, u) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100422 ret = lysp_check_dup_typedef(ctx, (struct lysp_node *)ctx->tpdfs_nodes.objs[i], &typedefs[u], ids_global, ids_scoped);
423 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100424 }
425 }
Michal Vasko405cc9e2020-12-01 12:01:27 +0100426
Radek Krejcibbe09a92018-11-08 09:36:54 +0100427cleanup:
428 lyht_free(ids_global);
429 lyht_free(ids_scoped);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100430 return ret;
431}
432
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100433static ly_bool
434ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
435{
436 void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
437
438 return ptr1 == ptr2 ? 1 : 0;
439}
440
441LY_ERR
442lysp_check_dup_features(struct lys_parser_ctx *ctx, struct lysp_module *mod)
443{
444 LY_ARRAY_COUNT_TYPE u;
445 struct hash_table *ht;
446 struct lysp_feature *f;
447 uint32_t hash;
448 LY_ERR ret = LY_SUCCESS, r;
449
450 ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
451 LY_CHECK_RET(!ht, LY_EMEM);
452
453 /* add all module features into a hash table */
454 LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
455 hash = dict_hash(f->name, strlen(f->name));
456 r = lyht_insert(ht, &f->name, hash, NULL);
457 if (r == LY_EEXIST) {
458 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
459 ret = LY_EVALID;
460 goto cleanup;
461 } else if (r) {
462 ret = r;
463 goto cleanup;
464 }
465 }
466
467 /* add all submodule features into a hash table */
468 LY_ARRAY_FOR(mod->includes, u) {
469 LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
470 hash = dict_hash(f->name, strlen(f->name));
471 r = lyht_insert(ht, &f->name, hash, NULL);
472 if (r == LY_EEXIST) {
473 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
474 ret = LY_EVALID;
475 goto cleanup;
476 } else if (r) {
477 ret = r;
478 goto cleanup;
479 }
480 }
481 }
482
483cleanup:
484 lyht_free(ht);
485 return ret;
486}
487
488LY_ERR
489lysp_check_dup_identities(struct lys_parser_ctx *ctx, struct lysp_module *mod)
490{
491 LY_ARRAY_COUNT_TYPE u;
492 struct hash_table *ht;
493 struct lysp_ident *i;
494 uint32_t hash;
495 LY_ERR ret = LY_SUCCESS, r;
496
497 ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
498 LY_CHECK_RET(!ht, LY_EMEM);
499
500 /* add all module identities into a hash table */
501 LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
502 hash = dict_hash(i->name, strlen(i->name));
503 r = lyht_insert(ht, &i->name, hash, NULL);
504 if (r == LY_EEXIST) {
505 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
506 ret = LY_EVALID;
507 goto cleanup;
508 } else if (r) {
509 ret = r;
510 goto cleanup;
511 }
512 }
513
514 /* add all submodule identities into a hash table */
515 LY_ARRAY_FOR(mod->includes, u) {
516 LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
517 hash = dict_hash(i->name, strlen(i->name));
518 r = lyht_insert(ht, &i->name, hash, NULL);
519 if (r == LY_EEXIST) {
520 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
521 ret = LY_EVALID;
522 goto cleanup;
523 } else if (r) {
524 ret = r;
525 goto cleanup;
526 }
527 }
528 }
529
530cleanup:
531 lyht_free(ht);
532 return ret;
533}
534
Radek Krejci9ed7a192018-10-31 16:23:51 +0100535struct lysp_load_module_check_data {
536 const char *name;
537 const char *revision;
538 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200539 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100540};
541
542static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100543lysp_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 +0100544{
545 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100546 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200547 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100548 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100549 struct lysp_revision *revs;
550
551 name = mod ? mod->mod->name : submod->name;
552 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200553 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100554
555 if (info->name) {
556 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100557 if (strcmp(info->name, name)) {
558 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100559 return LY_EINVAL;
560 }
561 }
562 if (info->revision) {
563 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100564 if (!revs || strcmp(info->revision, revs[0].date)) {
565 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200566 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100567 return LY_EINVAL;
568 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200569 } else if (!latest_revision) {
570 /* do not log, we just need to drop the schema and use the latest revision from the context */
571 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100572 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100573 if (submod) {
574 assert(info->submoduleof);
575
Radek Krejci9ed7a192018-10-31 16:23:51 +0100576 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200577 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100578 LOGVAL(ctx, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200579 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100580 return LY_EVALID;
581 }
582 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100583 if (submod->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100584 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100585 return LY_EVALID;
586 }
587 }
588 if (info->path) {
589 /* check that name and revision match filename */
590 filename = strrchr(info->path, '/');
591 if (!filename) {
592 filename = info->path;
593 } else {
594 filename++;
595 }
596 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100597 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100598 rev = strchr(filename, '@');
599 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100600 if (strncmp(filename, name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +0200601 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100602 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100603 }
604 /* revision */
605 if (rev) {
606 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100607 if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100608 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +0200609 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100610 }
611 }
612 }
613 return LY_SUCCESS;
614}
615
616LY_ERR
Michal Vasko34e334d2021-01-25 16:12:31 +0100617lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, const char **features,
618 ly_bool need_implemented, struct lys_parser_ctx *main_ctx, const char *main_name, ly_bool required,
619 struct lys_glob_unres *unres, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100620{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200621 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100622 char *filepath = NULL;
623 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100624 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100625 LY_ERR ret = LY_SUCCESS;
626 struct lysp_load_module_check_data check_data = {0};
627
628 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, revision,
Michal Vasko69730152020-10-09 16:30:07 +0200629 &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200630 if (!filepath) {
631 if (required) {
632 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200633 revision ? revision : "");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200634 }
635 return LY_ENOTFOUND;
636 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100637
638 LOGVRB("Loading schema from \"%s\" file.", filepath);
639
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200640 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200641 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +0200642 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100643 check_data.name = name;
644 check_data.revision = revision;
645 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800646 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200647 if (main_ctx) {
Michal Vasko7a0b0762020-09-02 16:37:01 +0200648 ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data,
Michal Vasko69730152020-10-09 16:30:07 +0200649 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200650 } else {
Michal Vasko34e334d2021-01-25 16:12:31 +0100651 ret = lys_create_module(ctx, in, format, need_implemented, lysp_load_module_check, &check_data, features, unres,
Michal Vasko69730152020-10-09 16:30:07 +0200652 (struct lys_module **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200653
654 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200655 ly_in_free(in, 1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200656 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100657
658 *result = mod;
659
660 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200661
Radek Krejci9ed7a192018-10-31 16:23:51 +0100662cleanup:
663 free(filepath);
664 return ret;
665}
666
Radek Krejcid33273d2018-10-25 14:55:52 +0200667LY_ERR
Michal Vasko34e334d2021-01-25 16:12:31 +0100668lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, ly_bool need_implemented,
669 const char **features, struct lys_glob_unres *unres, struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200670{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100671 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200672 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200673
Radek Krejci9ed7a192018-10-31 16:23:51 +0100674 void (*module_data_free)(void *module_data, void *user_data) = NULL;
675 struct lysp_load_module_check_data check_data = {0};
Michal Vasko0550b762020-11-24 18:04:08 +0100676 struct lys_module *ctx_latest = NULL, *m;
Michal Vasko63f3d842020-07-08 10:10:14 +0200677 struct ly_in *in;
Michal Vasko0550b762020-11-24 18:04:08 +0100678 LY_ERR ret;
Michal Vasko34e334d2021-01-25 16:12:31 +0100679 ly_bool implement;
Radek Krejci086c7132018-10-26 15:29:04 +0200680
Michal Vasko405cc9e2020-12-01 12:01:27 +0100681 assert(mod && unres);
Radek Krejci0af46292019-01-11 16:02:31 +0100682
Michal Vasko25d6ad02020-10-22 12:20:22 +0200683 if (ctx->flags & LY_CTX_ALL_IMPLEMENTED) {
Radek Krejcia53d7c92020-08-21 11:30:56 +0200684 implement = 1;
Michal Vasko34e334d2021-01-25 16:12:31 +0100685 } else {
686 implement = need_implemented;
Radek Krejcia53d7c92020-08-21 11:30:56 +0200687 }
688
Michal Vasko0550b762020-11-24 18:04:08 +0100689 /*
690 * try to get the module from the context
691 */
Radek Krejci0af46292019-01-11 16:02:31 +0100692 if (!*mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100693 if (revision) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200694 /* get the specific revision */
Michal Vasko22df3f02020-08-24 13:29:22 +0200695 *mod = (struct lys_module *)ly_ctx_get_module(ctx, name, revision);
Radek Krejci0af46292019-01-11 16:02:31 +0100696 } else {
Michal Vasko0550b762020-11-24 18:04:08 +0100697 if (implement) {
698 /* prefer the implemented module instead of the latest one */
699 *mod = (struct lys_module *)ly_ctx_get_module_implemented(ctx, name);
700 }
701 if (!*mod) {
702 /* get the requested module of the latest revision in the context */
703 *mod = (struct lys_module *)ly_ctx_get_module_latest(ctx, name);
704 if (*mod && ((*mod)->latest_revision == 1)) {
705 /* let us now search with callback and searchpaths to check if there is newer revision outside the context */
706 ctx_latest = *mod;
707 *mod = NULL;
708 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200709 }
Radek Krejci0af46292019-01-11 16:02:31 +0100710 }
Radek Krejci086c7132018-10-26 15:29:04 +0200711 }
712
Michal Vasko0550b762020-11-24 18:04:08 +0100713 /* check collision with other implemented revision */
714 if (implement) {
715 m = ly_ctx_get_module_implemented(ctx, name);
716 if (m && (!*mod || (*mod && (m != *mod)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100717 LOGVAL(ctx, LYVE_REFERENCE, "Module \"%s\" is already present in other implemented revision.", name);
Michal Vasko0550b762020-11-24 18:04:08 +0100718 *mod = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200719 return LY_EDENIED;
720 }
Michal Vasko0550b762020-11-24 18:04:08 +0100721 }
Radek Krejci086c7132018-10-26 15:29:04 +0200722
Michal Vasko0550b762020-11-24 18:04:08 +0100723 /*
724 * no suitable module in the context, try to load it
725 */
726 if (!*mod) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100727 /* module not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200728 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
729search_clb:
730 if (ctx->imp_clb) {
731 if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
Michal Vasko69730152020-10-09 16:30:07 +0200732 &format, &module_data, &module_data_free) == LY_SUCCESS) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200733 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
Radek Krejci9ed7a192018-10-31 16:23:51 +0100734 check_data.name = name;
735 check_data.revision = revision;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100736 lys_create_module(ctx, in, format, implement, lysp_load_module_check, &check_data, features, unres, mod);
Michal Vasko63f3d842020-07-08 10:10:14 +0200737 ly_in_free(in, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100738 if (module_data_free) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200739 module_data_free((void *)module_data, ctx->imp_clb_data);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100740 }
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 Vasko405cc9e2020-12-01 12:01:27 +0100750 lys_module_localfile(ctx, name, revision, features, implement, NULL, NULL, ctx_latest ? 0 : 1, unres,
751 (void **)mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200752 }
Michal Vasko0550b762020-11-24 18:04:08 +0100753 if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejci086c7132018-10-26 15:29:04 +0200754 goto search_clb;
755 }
756 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100757
Radek Krejcib3289d62019-09-18 12:21:39 +0200758 /* update the latest_revision flag - here we have selected the latest available schema,
759 * consider that even the callback provides correct latest revision */
Michal Vasko0550b762020-11-24 18:04:08 +0100760 if (!*mod && ctx_latest) {
Michal Vasko003c44a2021-02-24 17:13:11 +0100761 LOGVRB("Newer revision than \"%s@%s\" not found, using this as the latest revision.", ctx_latest->name,
Michal Vasko0550b762020-11-24 18:04:08 +0100762 ctx_latest->revision);
763 ctx_latest->latest_revision = 2;
764 *mod = ctx_latest;
765 } else if (*mod && !revision && ((*mod)->latest_revision == 1)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100766 (*mod)->latest_revision = 2;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100767 }
Radek Krejci086c7132018-10-26 15:29:04 +0200768
Michal Vasko0550b762020-11-24 18:04:08 +0100769 if (!*mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100770 LOGVAL(ctx, LYVE_REFERENCE, "%s \"%s\" module failed.", implement ? "Loading" : "Importing", name);
Michal Vasko0550b762020-11-24 18:04:08 +0100771 return LY_EVALID;
772 }
773 } else {
774 /* we have module from the current context, circular check */
775 if ((*mod)->parsed->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100776 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
Radek Krejci086c7132018-10-26 15:29:04 +0200777 *mod = NULL;
778 return LY_EVALID;
779 }
780 }
Radek Krejci086c7132018-10-26 15:29:04 +0200781
Michal Vasko0550b762020-11-24 18:04:08 +0100782 /*
783 * module found, make sure it is implemented if should be
784 */
Michal Vasko962b6cd2020-12-08 10:07:49 +0100785 if (implement) {
Radek Krejci2415f882021-01-20 16:27:09 +0100786 if (!(*mod)->implemented) {
787 /* implement */
788 ret = lys_set_implemented_r(*mod, features, unres);
789 if (ret) {
790 *mod = NULL;
791 return ret;
792 }
793 } else if (features) {
Michal Vasko962b6cd2020-12-08 10:07:49 +0100794 /* set features if different */
795 ret = lys_set_features((*mod)->parsed, features);
796 if (!ret) {
797 /* context need to be recompiled so that feature changes are properly applied */
798 unres->recompile = 1;
799 } else if (ret != LY_EEXIST) {
800 /* error */
801 return ret;
802 } /* else no feature changes */
Michal Vaskoc5d64862020-11-24 18:04:45 +0100803 }
Radek Krejci086c7132018-10-26 15:29:04 +0200804 }
Radek Krejci086c7132018-10-26 15:29:04 +0200805
806 return LY_SUCCESS;
807}
808
809LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200810lysp_check_stringchar(struct lys_parser_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +0200811{
812 if (!is_yangutf8char(c)) {
813 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
814 return LY_EVALID;
815 }
816 return LY_SUCCESS;
817}
818
819LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200820lysp_check_identifierchar(struct lys_parser_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +0200821{
Michal Vasko69730152020-10-09 16:30:07 +0200822 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +0200823 if (!is_yangidentstartchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200824 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200825 return LY_EVALID;
826 }
827 if (prefix) {
828 if (first) {
829 (*prefix) = 0;
830 } else {
831 (*prefix) = 2;
832 }
833 }
Michal Vasko69730152020-10-09 16:30:07 +0200834 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +0200835 (*prefix) = 1;
836 } else if (!is_yangidentchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200837 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200838 return LY_EVALID;
839 }
840
841 return LY_SUCCESS;
842}
843
Radek Krejci771928a2021-01-19 13:42:36 +0100844/**
845 * @brief Try to find the parsed submodule in main module for the given include record.
846 *
847 * @param[in] pctx main parser context
848 * @param[in] inc The include record with missing parsed submodule. According to include info try to find
849 * the corresponding parsed submodule in main module's includes.
850 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
851 * @return LY_ENOT - the parsed module was not found.
852 * @return LY_EVALID - YANG rule violation
853 */
854static LY_ERR
855lysp_get_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200856{
Radek Krejci771928a2021-01-19 13:42:36 +0100857 LY_ARRAY_COUNT_TYPE i;
858 struct lysp_module *main_pmod = pctx->parsed_mod->mod->parsed;
Michal Vasko69730152020-10-09 16:30:07 +0200859
Radek Krejci771928a2021-01-19 13:42:36 +0100860 LY_ARRAY_FOR(main_pmod->includes, i) {
861 if (strcmp(main_pmod->includes[i].name, inc->name)) {
862 continue;
863 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200864
Radek Krejci771928a2021-01-19 13:42:36 +0100865 if (inc->rev[0] && strncmp(inc->rev, main_pmod->includes[i].rev, LY_REV_SIZE)) {
866 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
867 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
868 ((struct lysp_submodule *)pctx->parsed_mod)->name, inc->rev,
869 main_pmod->includes[i].name, main_pmod->includes[i].rev, main_pmod->mod->name);
870 return LY_EVALID;
871 }
872
873 inc->submodule = main_pmod->includes[i].submodule;
874 return inc->submodule ? LY_SUCCESS : LY_ENOT;
875 }
876
877 if (main_pmod->version == LYS_VERSION_1_1) {
878 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
879 "YANG 1.1 requires all submodules to be included from main module. "
880 "But submodule \"%s\" includes submodule \"%s\" which is not included by main module \"%s\".",
881 ((struct lysp_submodule *)pctx->parsed_mod)->name, inc->name, main_pmod->mod->name);
882 return LY_EVALID;
883 } else {
884 return LY_ENOT;
885 }
886}
887
888/**
889 * @brief Make the copy of the given include record into the main module.
890 *
891 * YANG 1.0 does not require the main module to include all the submodules. Therefore, parsing submodules can cause
892 * reallocating and extending the includes array in the main module by the submodules included only in submodules.
893 *
894 * @param[in] pctx main parser context
895 * @param[in] inc Include record to copy into main module taken from @p pctx.
896 * @return LY_ERR value.
897 */
898static LY_ERR
899lysp_inject_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
900{
901 LY_ARRAY_COUNT_TYPE i;
902 struct lysp_include *inc_new, *inc_tofill = NULL;
903 struct lysp_module *main_pmod = pctx->parsed_mod->mod->parsed;
904
905 /* first, try to find the corresponding record with missing parsed submodule */
906 LY_ARRAY_FOR(main_pmod->includes, i) {
907 if (strcmp(main_pmod->includes[i].name, inc->name)) {
908 continue;
909 }
910 inc_tofill = &main_pmod->includes[i];
911 break;
912 }
913
914 if (inc_tofill) {
915 inc_tofill->submodule = inc->submodule;
916 } else {
917 LY_ARRAY_NEW_RET(PARSER_CTX(pctx), main_pmod->includes, inc_new, LY_EMEM);
918
919 inc_new->submodule = inc->submodule;
920 DUP_STRING_RET(PARSER_CTX(pctx), inc->name, inc_new->name);
921 DUP_STRING_RET(PARSER_CTX(pctx), inc->dsc, inc_new->dsc);
922 DUP_STRING_RET(PARSER_CTX(pctx), inc->ref, inc_new->ref);
923 /* TODO duplicate extensions */
924 memcpy(inc_new->rev, inc->rev, LY_REV_SIZE);
925 inc_new->injected = 1;
926 }
927 return LY_SUCCESS;
928}
929
930LY_ERR
931lysp_load_submodules(struct lys_parser_ctx *pctx, struct lysp_module *pmod)
932{
933 LY_ARRAY_COUNT_TYPE u;
934 struct ly_ctx *ctx = PARSER_CTX(pctx);
935
936 LY_ARRAY_FOR(pmod->includes, u) {
937 LY_ERR ret = LY_SUCCESS;
938 struct lysp_submodule *submod = NULL;
939 struct lysp_include *inc = &pmod->includes[u];
940
941 if (inc->submodule) {
942 continue;
943 }
944
945 if (pmod->is_submod) {
946 /* try to find the submodule in the main module or its submodules */
947 ret = lysp_get_submodule(pctx, inc);
948 LY_CHECK_RET(ret && ret != LY_ENOT, ret);
949 LY_CHECK_RET(ret == LY_SUCCESS, LY_SUCCESS); /* submodule found in linked with the inc */
950 }
951
952 /* submodule not present in the main module, get the input data and parse it */
953 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcidf549132021-01-21 10:32:32 +0100954search_clb:
Radek Krejci771928a2021-01-19 13:42:36 +0100955 if (ctx->imp_clb) {
956 const char *submodule_data = NULL;
957 LYS_INFORMAT format = LYS_IN_UNKNOWN;
958 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
959 struct lysp_load_module_check_data check_data = {0};
960 struct ly_in *in;
961
962 if (ctx->imp_clb(pctx->parsed_mod->mod->name, NULL, inc->name,
963 inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
964 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
965 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
966 check_data.name = inc->name;
967 check_data.revision = inc->rev[0] ? inc->rev : NULL;
968 check_data.submoduleof = pctx->parsed_mod->mod->name;
969 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, &submod);
970
971 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
972 * submodule's include into main module, where it is missing */
973 inc = &pmod->includes[u];
974
975 ly_in_free(in, 0);
976 if (submodule_data_free) {
977 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
978 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200979 }
980 }
Radek Krejci771928a2021-01-19 13:42:36 +0100981 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
982 goto search_file;
983 }
984 } else {
Radek Krejcidf549132021-01-21 10:32:32 +0100985search_file:
Radek Krejci771928a2021-01-19 13:42:36 +0100986 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
987 /* submodule was not received from the callback or there is no callback set */
988 lys_module_localfile(ctx, inc->name,
989 inc->rev[0] ? inc->rev : NULL, NULL, 0, pctx,
990 pctx->parsed_mod->mod->name, 1, NULL, (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100991
Radek Krejci771928a2021-01-19 13:42:36 +0100992 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
993 * submodule's include into main module, where it is missing */
994 inc = &pmod->includes[u];
995 }
996 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
997 goto search_clb;
998 }
999 }
1000 if (submod) {
1001 if (!inc->rev[0] && (submod->latest_revision == 1)) {
1002 /* update the latest_revision flag - here we have selected the latest available schema,
1003 * consider that even the callback provides correct latest revision */
1004 submod->latest_revision = 2;
1005 }
1006
1007 inc->submodule = submod;
1008 if (ret == LY_ENOT) {
1009 /* the submodule include is not present in YANG 1.0 main module - add it there */
1010 LY_CHECK_RET(lysp_inject_submodule(pctx, &pmod->includes[u]));
1011 }
1012 }
1013 if (!inc->submodule) {
1014 LOGVAL(ctx, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name,
1015 pctx->parsed_mod->is_submod ? ((struct lysp_submodule *)pctx->parsed_mod)->name : pctx->parsed_mod->mod->name);
1016 return LY_EVALID;
1017 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001018 }
1019
1020 return LY_SUCCESS;
1021}
1022
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001023API const struct lysc_when *
1024lysc_has_when(const struct lysc_node *node)
1025{
Radek Krejci9a3823e2021-01-27 20:26:46 +01001026 struct lysc_when **when;
1027
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001028 if (!node) {
1029 return NULL;
1030 }
1031
1032 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001033 when = lysc_node_when(node);
1034 if (when) {
1035 return when[0];
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001036 }
1037 node = node->parent;
1038 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1039
1040 return NULL;
1041}
1042
Radek Krejci0935f412019-08-20 16:15:18 +02001043API const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001044lys_nodetype2str(uint16_t nodetype)
1045{
Michal Vaskod989ba02020-08-24 10:59:24 +02001046 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001047 case LYS_CONTAINER:
1048 return "container";
1049 case LYS_CHOICE:
1050 return "choice";
1051 case LYS_LEAF:
1052 return "leaf";
1053 case LYS_LEAFLIST:
1054 return "leaf-list";
1055 case LYS_LIST:
1056 return "list";
1057 case LYS_ANYXML:
1058 return "anyxml";
1059 case LYS_ANYDATA:
1060 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001061 case LYS_CASE:
1062 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001063 case LYS_RPC:
1064 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001065 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001066 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001067 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001068 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001069 case LYS_USES:
1070 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001071 default:
1072 return "unknown";
1073 }
1074}
1075
Radek Krejci693262f2019-04-29 15:23:20 +02001076const char *
1077lys_datatype2str(LY_DATA_TYPE basetype)
1078{
Michal Vaskod989ba02020-08-24 10:59:24 +02001079 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001080 case LY_TYPE_BINARY:
1081 return "binary";
1082 case LY_TYPE_UINT8:
1083 return "uint8";
1084 case LY_TYPE_UINT16:
1085 return "uint16";
1086 case LY_TYPE_UINT32:
1087 return "uint32";
1088 case LY_TYPE_UINT64:
1089 return "uint64";
1090 case LY_TYPE_STRING:
1091 return "string";
1092 case LY_TYPE_BITS:
1093 return "bits";
1094 case LY_TYPE_BOOL:
1095 return "boolean";
1096 case LY_TYPE_DEC64:
1097 return "decimal64";
1098 case LY_TYPE_EMPTY:
1099 return "empty";
1100 case LY_TYPE_ENUM:
1101 return "enumeration";
1102 case LY_TYPE_IDENT:
1103 return "identityref";
1104 case LY_TYPE_INST:
1105 return "instance-identifier";
1106 case LY_TYPE_LEAFREF:
1107 return "leafref";
1108 case LY_TYPE_UNION:
1109 return "union";
1110 case LY_TYPE_INT8:
1111 return "int8";
1112 case LY_TYPE_INT16:
1113 return "int16";
1114 case LY_TYPE_INT32:
1115 return "int32";
1116 case LY_TYPE_INT64:
1117 return "int64";
1118 default:
1119 return "unknown";
1120 }
1121}
1122
Radek Krejci056d0a82018-12-06 16:57:25 +01001123API const struct lysp_tpdf *
1124lysp_node_typedefs(const struct lysp_node *node)
1125{
Radek Krejci0fb28562018-12-13 15:17:37 +01001126 switch (node->nodetype) {
1127 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001128 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001129 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001130 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001131 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001132 return ((struct lysp_node_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001133 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001134 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001135 return ((struct lysp_node_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001136 case LYS_INPUT:
1137 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001138 return ((struct lysp_node_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001139 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001140 return ((struct lysp_node_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001141 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001142 return NULL;
1143 }
1144}
1145
Radek Krejci2a9fc652021-01-22 17:44:34 +01001146API const struct lysp_node_grp *
Radek Krejci53ea6152018-12-13 15:21:15 +01001147lysp_node_groupings(const struct lysp_node *node)
1148{
1149 switch (node->nodetype) {
1150 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001151 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001152 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001153 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001154 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001155 return ((struct lysp_node_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001156 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001157 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001158 return ((struct lysp_node_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001159 case LYS_INPUT:
1160 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001161 return ((struct lysp_node_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001162 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001163 return ((struct lysp_node_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001164 default:
1165 return NULL;
1166 }
1167}
1168
Radek Krejci2a9fc652021-01-22 17:44:34 +01001169struct lysp_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001170lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001171{
1172 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001173
Radek Krejcibbe09a92018-11-08 09:36:54 +01001174 switch (node->nodetype) {
1175 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001176 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001177 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001178 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001179 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001180 return &((struct lysp_node_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001181 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001182 return &((struct lysp_node_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001183 default:
1184 return NULL;
1185 }
1186}
1187
Radek Krejci2a9fc652021-01-22 17:44:34 +01001188API const struct lysp_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001189lysp_node_actions(const struct lysp_node *node)
1190{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001191 struct lysp_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001192
Michal Vasko22df3f02020-08-24 13:29:22 +02001193 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001194 if (actions) {
1195 return *actions;
1196 } else {
1197 return NULL;
1198 }
1199}
1200
Radek Krejci2a9fc652021-01-22 17:44:34 +01001201struct lysp_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001202lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001203{
1204 assert(node);
1205 switch (node->nodetype) {
1206 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001207 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001208 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001209 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001210 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001211 return &((struct lysp_node_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001212 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001213 return &((struct lysp_node_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001214 default:
1215 return NULL;
1216 }
1217}
1218
Radek Krejci2a9fc652021-01-22 17:44:34 +01001219API const struct lysp_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001220lysp_node_notifs(const struct lysp_node *node)
1221{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001222 struct lysp_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001223
Michal Vasko22df3f02020-08-24 13:29:22 +02001224 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001225 if (notifs) {
1226 return *notifs;
1227 } else {
1228 return NULL;
1229 }
1230}
1231
Radek Krejcibbe09a92018-11-08 09:36:54 +01001232struct lysp_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001233lysp_node_child_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001234{
1235 assert(node);
1236 switch (node->nodetype) {
1237 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001238 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001239 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001240 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001241 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001242 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001243 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001244 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001245 case LYS_GROUPING:
Radek Krejci01180ac2021-01-27 08:48:22 +01001246 return &((struct lysp_node_grp *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001247 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001248 return &((struct lysp_node_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001249 case LYS_INPUT:
1250 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001251 return &((struct lysp_node_action_inout *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001252 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001253 return &((struct lysp_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001254 default:
1255 return NULL;
1256 }
1257}
1258
Radek Krejci056d0a82018-12-06 16:57:25 +01001259API const struct lysp_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001260lysp_node_child(const struct lysp_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01001261{
Michal Vasko544e58a2021-01-28 14:33:41 +01001262 struct lysp_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001263
1264 if (!node) {
1265 return NULL;
1266 }
1267
Michal Vasko544e58a2021-01-28 14:33:41 +01001268 child = lysp_node_child_p((struct lysp_node *)node);
1269 if (child) {
1270 return *child;
Radek Krejci056d0a82018-12-06 16:57:25 +01001271 } else {
1272 return NULL;
1273 }
1274}
1275
Radek Krejci9a3823e2021-01-27 20:26:46 +01001276struct lysp_restr **
1277lysp_node_musts_p(const struct lysp_node *node)
1278{
1279 if (!node) {
1280 return NULL;
1281 }
1282
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001283 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001284 case LYS_CONTAINER:
1285 return &((struct lysp_node_container *)node)->musts;
1286 case LYS_LEAF:
1287 return &((struct lysp_node_leaf *)node)->musts;
1288 case LYS_LEAFLIST:
1289 return &((struct lysp_node_leaflist *)node)->musts;
1290 case LYS_LIST:
1291 return &((struct lysp_node_list *)node)->musts;
1292 case LYS_ANYXML:
1293 case LYS_ANYDATA:
1294 return &((struct lysp_node_anydata *)node)->musts;
1295 case LYS_NOTIF:
1296 return &((struct lysp_node_notif *)node)->musts;
1297 case LYS_INPUT:
1298 case LYS_OUTPUT:
1299 return &((struct lysp_node_action_inout *)node)->musts;
1300 default:
1301 return NULL;
1302 }
1303}
1304
1305struct lysp_restr *
1306lysp_node_musts(const struct lysp_node *node)
1307{
1308 struct lysp_restr **musts;
1309
1310 musts = lysp_node_musts_p(node);
1311 if (musts) {
1312 return *musts;
1313 } else {
1314 return NULL;
1315 }
1316}
1317
1318struct lysp_when **
1319lysp_node_when_p(const struct lysp_node *node)
1320{
1321 if (!node) {
1322 return NULL;
1323 }
1324
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001325 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001326 case LYS_CONTAINER:
1327 return &((struct lysp_node_container *)node)->when;
1328 case LYS_CHOICE:
1329 return &((struct lysp_node_choice *)node)->when;
1330 case LYS_LEAF:
1331 return &((struct lysp_node_leaf *)node)->when;
1332 case LYS_LEAFLIST:
1333 return &((struct lysp_node_leaflist *)node)->when;
1334 case LYS_LIST:
1335 return &((struct lysp_node_list *)node)->when;
1336 case LYS_ANYXML:
1337 case LYS_ANYDATA:
1338 return &((struct lysp_node_anydata *)node)->when;
1339 case LYS_CASE:
1340 return &((struct lysp_node_case *)node)->when;
1341 case LYS_USES:
1342 return &((struct lysp_node_uses *)node)->when;
1343 case LYS_AUGMENT:
1344 return &((struct lysp_node_augment *)node)->when;
1345 default:
1346 return NULL;
1347 }
1348}
1349
1350struct lysp_when *
1351lysp_node_when(const struct lysp_node *node)
1352{
1353 struct lysp_when **when;
1354
1355 when = lysp_node_when_p(node);
1356 if (when) {
1357 return *when;
1358 } else {
1359 return NULL;
1360 }
1361}
1362
Radek Krejci2a9fc652021-01-22 17:44:34 +01001363struct lysc_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001364lysc_node_actions_p(struct lysc_node *node)
1365{
1366 assert(node);
1367 switch (node->nodetype) {
1368 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001369 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001370 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001371 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001372 default:
1373 return NULL;
1374 }
1375}
1376
Radek Krejci2a9fc652021-01-22 17:44:34 +01001377API const struct lysc_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001378lysc_node_actions(const struct lysc_node *node)
1379{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001380 struct lysc_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001381
Michal Vasko22df3f02020-08-24 13:29:22 +02001382 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001383 if (actions) {
1384 return *actions;
1385 } else {
1386 return NULL;
1387 }
1388}
1389
Radek Krejci2a9fc652021-01-22 17:44:34 +01001390struct lysc_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001391lysc_node_notifs_p(struct lysc_node *node)
1392{
1393 assert(node);
1394 switch (node->nodetype) {
1395 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001396 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001397 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001398 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001399 default:
1400 return NULL;
1401 }
1402}
1403
Radek Krejci2a9fc652021-01-22 17:44:34 +01001404API const struct lysc_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001405lysc_node_notifs(const struct lysc_node *node)
1406{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001407 struct lysc_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001408
Michal Vasko22df3f02020-08-24 13:29:22 +02001409 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001410 if (notifs) {
1411 return *notifs;
1412 } else {
1413 return NULL;
1414 }
1415}
1416
Radek Krejcibbe09a92018-11-08 09:36:54 +01001417struct lysc_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001418lysc_node_child_p(const struct lysc_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001419{
Michal Vasko544e58a2021-01-28 14:33:41 +01001420 assert(node && !(node->nodetype & (LYS_RPC | LYS_ACTION)));
1421
Radek Krejcibbe09a92018-11-08 09:36:54 +01001422 switch (node->nodetype) {
1423 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001424 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001425 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001426 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001427 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001428 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001429 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001430 return &((struct lysc_node_list *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001431 case LYS_INPUT:
1432 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001433 return &((struct lysc_node_action_inout *)node)->child;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001434 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001435 return &((struct lysc_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001436 default:
1437 return NULL;
1438 }
1439}
1440
Radek Krejci056d0a82018-12-06 16:57:25 +01001441API const struct lysc_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001442lysc_node_child(const struct lysc_node *node)
Radek Krejcia3045382018-11-22 14:30:31 +01001443{
Michal Vasko544e58a2021-01-28 14:33:41 +01001444 struct lysc_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001445
1446 if (!node) {
1447 return NULL;
1448 }
1449
Michal Vasko544e58a2021-01-28 14:33:41 +01001450 if (node->nodetype & (LYS_RPC | LYS_ACTION)) {
1451 return &((struct lysc_node_action *)node)->input.node;
Radek Krejcibe154442021-01-21 11:06:36 +01001452 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01001453 child = lysc_node_child_p(node);
1454 if (child) {
1455 return *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001456 }
Michal Vasko2a668712020-10-21 11:48:09 +02001457 }
Michal Vasko544e58a2021-01-28 14:33:41 +01001458
1459 return NULL;
Michal Vasko2a668712020-10-21 11:48:09 +02001460}
1461
Radek Krejci9a3823e2021-01-27 20:26:46 +01001462struct lysc_must **
1463lysc_node_musts_p(const struct lysc_node *node)
1464{
1465 if (!node) {
1466 return NULL;
1467 }
1468
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001469 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001470 case LYS_CONTAINER:
1471 return &((struct lysc_node_container *)node)->musts;
1472 case LYS_LEAF:
1473 return &((struct lysc_node_leaf *)node)->musts;
1474 case LYS_LEAFLIST:
1475 return &((struct lysc_node_leaflist *)node)->musts;
1476 case LYS_LIST:
1477 return &((struct lysc_node_list *)node)->musts;
1478 case LYS_ANYXML:
1479 case LYS_ANYDATA:
1480 return &((struct lysc_node_anydata *)node)->musts;
1481 case LYS_NOTIF:
1482 return &((struct lysc_node_notif *)node)->musts;
1483 case LYS_INPUT:
1484 case LYS_OUTPUT:
1485 return &((struct lysc_node_action_inout *)node)->musts;
1486 default:
1487 return NULL;
1488 }
1489}
1490
1491API struct lysc_must *
1492lysc_node_musts(const struct lysc_node *node)
1493{
1494 struct lysc_must **must_p;
1495
1496 must_p = lysc_node_musts_p(node);
1497 if (must_p) {
1498 return *must_p;
1499 } else {
1500 return NULL;
1501 }
1502}
1503
1504struct lysc_when ***
1505lysc_node_when_p(const struct lysc_node *node)
1506{
1507 if (!node) {
1508 return NULL;
1509 }
1510
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001511 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001512 case LYS_CONTAINER:
1513 return &((struct lysc_node_container *)node)->when;
1514 case LYS_CHOICE:
1515 return &((struct lysc_node_choice *)node)->when;
1516 case LYS_LEAF:
1517 return &((struct lysc_node_leaf *)node)->when;
1518 case LYS_LEAFLIST:
1519 return &((struct lysc_node_leaflist *)node)->when;
1520 case LYS_LIST:
1521 return &((struct lysc_node_list *)node)->when;
1522 case LYS_ANYXML:
1523 case LYS_ANYDATA:
1524 return &((struct lysc_node_anydata *)node)->when;
1525 case LYS_CASE:
1526 return &((struct lysc_node_case *)node)->when;
1527 case LYS_NOTIF:
1528 return &((struct lysc_node_notif *)node)->when;
1529 case LYS_RPC:
1530 case LYS_ACTION:
1531 return &((struct lysc_node_action *)node)->when;
1532 default:
1533 return NULL;
1534 }
1535}
1536
1537API struct lysc_when **
1538lysc_node_when(const struct lysc_node *node)
1539{
1540 struct lysc_when ***when_p;
1541
1542 when_p = lysc_node_when_p(node);
1543 if (when_p) {
1544 return *when_p;
1545 } else {
1546 return NULL;
1547 }
1548}
1549
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001550struct lys_module *
1551lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1552{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001553 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001554 if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) {
Michal Vasko69730152020-10-09 16:30:07 +02001555 return (struct lys_module *)ctx->list.objs[u];
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001556 }
1557 }
1558 return NULL;
1559}
1560
Radek Krejcid6b76452019-09-03 17:03:03 +02001561enum ly_stmt
Radek Krejcid54412f2020-12-17 20:25:35 +01001562lysp_match_kw(struct ly_in *in, uint64_t *indent)
David Sedlákc10e7902018-12-17 02:17:59 +01001563{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001564/**
Radek Krejcid54412f2020-12-17 20:25:35 +01001565 * @brief Move the input by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001566 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001567 *
1568 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001569 */
Radek Krejcid54412f2020-12-17 20:25:35 +01001570#define MOVE_IN(COUNT) \
1571 ly_in_skip(in, COUNT); \
1572 if (indent) { \
1573 (*indent)+=COUNT; \
1574 }
1575#define IF_KW(STR, LEN, STMT) \
1576 if (!strncmp(in->current, STR, LEN)) { \
1577 MOVE_IN(LEN); \
1578 (*kw)=STMT; \
1579 }
1580#define IF_KW_PREFIX(STR, LEN) \
1581 if (!strncmp(in->current, STR, LEN)) { \
1582 MOVE_IN(LEN);
1583#define IF_KW_PREFIX_END \
1584 }
David Sedlák572e7ab2019-06-04 16:01:58 +02001585
Michal Vasko63f3d842020-07-08 10:10:14 +02001586 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001587 enum ly_stmt result = LY_STMT_NONE;
1588 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001589 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001590 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001591 case 'a':
Radek Krejcid54412f2020-12-17 20:25:35 +01001592 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001593 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1594 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1595 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001596 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001597 IF_KW("data", 4, LY_STMT_ANYDATA)
1598 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001599 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001600 break;
1601 case 'b':
Radek Krejcid54412f2020-12-17 20:25:35 +01001602 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001603 IF_KW("ase", 3, LY_STMT_BASE)
1604 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1605 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001606 break;
1607 case 'c':
Radek Krejcid54412f2020-12-17 20:25:35 +01001608 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001609 IF_KW("ase", 3, LY_STMT_CASE)
1610 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001611 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001612 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001613 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001614 IF_KW("ct", 2, LY_STMT_CONTACT)
1615 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001616 IF_KW_PREFIX_END
1617 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001618 break;
1619 case 'd':
Radek Krejcid54412f2020-12-17 20:25:35 +01001620 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001621 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001622 IF_KW("fault", 5, LY_STMT_DEFAULT)
1623 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001624 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001625 IF_KW("e", 1, LY_STMT_DEVIATE)
1626 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001627 IF_KW_PREFIX_END
1628 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001629 break;
1630 case 'e':
Radek Krejcid54412f2020-12-17 20:25:35 +01001631 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001632 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001633 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001634 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1635 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001636 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001637 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001638 break;
1639 case 'f':
Radek Krejcid54412f2020-12-17 20:25:35 +01001640 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001641 IF_KW("eature", 6, LY_STMT_FEATURE)
1642 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001643 break;
1644 case 'g':
Radek Krejcid54412f2020-12-17 20:25:35 +01001645 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001646 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001647 break;
1648 case 'i':
Radek Krejcid54412f2020-12-17 20:25:35 +01001649 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001650 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1651 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1652 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001653 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001654 IF_KW("clude", 5, LY_STMT_INCLUDE)
1655 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001656 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001657 break;
1658 case 'k':
Radek Krejcid54412f2020-12-17 20:25:35 +01001659 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001660 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001661 break;
1662 case 'l':
Radek Krejcid54412f2020-12-17 20:25:35 +01001663 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001664 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001665 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1666 else IF_KW("af", 2, LY_STMT_LEAF)
1667 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001668 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001669 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001670 break;
1671 case 'm':
Radek Krejcid54412f2020-12-17 20:25:35 +01001672 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001673 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001674 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1675 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001676 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001677 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1678 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001679 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001680 IF_KW("ule", 3, LY_STMT_MODULE)
1681 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001682 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001683 break;
1684 case 'n':
Radek Krejcid54412f2020-12-17 20:25:35 +01001685 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001686 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1687 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001688 break;
1689 case 'o':
Radek Krejcid54412f2020-12-17 20:25:35 +01001690 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001691 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001692 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1693 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001694 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001695 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001696 break;
1697 case 'p':
Radek Krejcid54412f2020-12-17 20:25:35 +01001698 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001699 IF_KW("ath", 3, LY_STMT_PATH)
1700 else IF_KW("attern", 6, LY_STMT_PATTERN)
1701 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001702 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001703 IF_KW("fix", 3, LY_STMT_PREFIX)
1704 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001705 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001706 break;
1707 case 'r':
Radek Krejcid54412f2020-12-17 20:25:35 +01001708 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001709 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001710 else IF_KW_PREFIX("e", 1)
1711 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001712 IF_KW("erence", 6, LY_STMT_REFERENCE)
1713 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001714 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001715 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1716 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1717 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001718 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001719 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001720 break;
1721 case 's':
Radek Krejcid54412f2020-12-17 20:25:35 +01001722 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001723 IF_KW("tatus", 5, LY_STMT_STATUS)
1724 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001725 break;
1726 case 't':
Radek Krejcid54412f2020-12-17 20:25:35 +01001727 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001728 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1729 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001730 break;
1731 case 'u':
Radek Krejcid54412f2020-12-17 20:25:35 +01001732 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001733 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001734 IF_KW("que", 3, LY_STMT_UNIQUE)
1735 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001736 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001737 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001738 break;
1739 case 'v':
Radek Krejcid54412f2020-12-17 20:25:35 +01001740 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001741 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001742 break;
1743 case 'w':
Radek Krejcid54412f2020-12-17 20:25:35 +01001744 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001745 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001746 break;
1747 case 'y':
Radek Krejcid54412f2020-12-17 20:25:35 +01001748 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001749 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
1750 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001751 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001752 default:
Radek Krejcid54412f2020-12-17 20:25:35 +01001753 /* if indent is not NULL we are matching keyword from YANG data */
1754 if (indent) {
Michal Vasko63f3d842020-07-08 10:10:14 +02001755 if (in->current[0] == ';') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001756 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001757 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02001758 } else if (in->current[0] == '{') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001759 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001760 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001761 } else if (in->current[0] == '}') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001762 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001763 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001764 }
1765 }
David Sedlák23a59a62018-10-26 13:08:02 +02001766 break;
1767 }
1768
Michal Vasko63f3d842020-07-08 10:10:14 +02001769 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02001770 /* the keyword is not terminated */
1771 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001772 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02001773 }
1774
David Sedlák1bccdfa2019-06-17 15:55:27 +02001775#undef IF_KW
1776#undef IF_KW_PREFIX
1777#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01001778#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02001779 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01001780
David Sedlák1bccdfa2019-06-17 15:55:27 +02001781 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02001782}
David Sedlákecf5eb82019-06-03 14:12:44 +02001783
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001784LY_ARRAY_COUNT_TYPE
1785lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, LYEXT_SUBSTMT substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001786{
1787 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1788
Michal Vaskod989ba02020-08-24 10:59:24 +02001789 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001790 if (ext[index].insubstmt == substmt) {
1791 return index;
1792 }
1793 }
1794
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001795 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001796}
1797
Michal Vasko62ed12d2020-05-21 10:08:25 +02001798const struct lysc_node *
Michal Vasko72244882021-01-12 15:21:05 +01001799lysc_data_node(const struct lysc_node *schema)
Michal Vasko62ed12d2020-05-21 10:08:25 +02001800{
1801 const struct lysc_node *parent;
1802
Michal Vasko72244882021-01-12 15:21:05 +01001803 parent = schema;
Radek Krejcidf549132021-01-21 10:32:32 +01001804 while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC |
1805 LYS_ACTION | LYS_NOTIF))) {
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001806 parent = parent->parent;
Michal Vasko72244882021-01-12 15:21:05 +01001807 }
Michal Vasko62ed12d2020-05-21 10:08:25 +02001808
1809 return parent;
1810}