blob: 7ecf7d7c5d1d0bcfcdf0c0bb38ceb6d6e2189459 [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
214lysp_type_find(const char *id, struct lysp_node *start_node, struct lysp_module *start_module,
Radek Krejci0f969882020-08-21 16:56:47 +0200215 LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node, struct lysp_module **module)
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 Vaskofd69e1d2020-07-03 11:57:17 +0200220 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100221
222 assert(id);
223 assert(start_module);
224 assert(tpdf);
225 assert(node);
226 assert(module);
227
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 Vasko7c8439f2020-08-05 13:25:19 +0200232 *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 {
236 *module = start_module;
237 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 }
246 LY_CHECK_RET(!(*module), LY_ENOTFOUND);
247
Michal Vasko69730152020-10-09 16:30:07 +0200248 if (start_node && (*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 */
262 if ((*module)->typedefs) {
263 LY_ARRAY_FOR((*module)->typedefs, u) {
264 if (!strcmp(name, (*module)->typedefs[u].name)) {
265 /* match */
266 *tpdf = &(*module)->typedefs[u];
267 return LY_SUCCESS;
268 }
269 }
270 }
271
272 /* search in submodules' typedefs */
273 LY_ARRAY_FOR((*module)->includes, u) {
274 typedefs = (*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.
388 * Implementation of ::values_equal_cb.
389 */
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
David Sedlákd2ebe572019-07-22 12:53:14 +0200397lysp_parse_finalize_reallocated(struct lys_parser_ctx *ctx, struct lysp_grp *groupings, struct lysp_augment *augments,
Radek Krejci0f969882020-08-21 16:56:47 +0200398 struct lysp_action *actions, struct lysp_notif *notifs)
David Sedlákd2ebe572019-07-22 12:53:14 +0200399{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200400 LY_ARRAY_COUNT_TYPE u, v;
David Sedlákd2ebe572019-07-22 12:53:14 +0200401 struct lysp_node *child;
402
403 /* finalize parent pointers to the reallocated items */
404
405 /* gropings */
406 LY_ARRAY_FOR(groupings, u) {
407 LY_LIST_FOR(groupings[u].data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200408 child->parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200409 }
410 LY_ARRAY_FOR(groupings[u].actions, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200411 groupings[u].actions[v].parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200412 }
413 LY_ARRAY_FOR(groupings[u].notifs, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200414 groupings[u].notifs[v].parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200415 }
416 LY_ARRAY_FOR(groupings[u].groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200417 groupings[u].groupings[v].parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200418 }
419 if (groupings[u].typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200420 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &groupings[u], 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200421 }
422 }
423
424 /* augments */
425 LY_ARRAY_FOR(augments, u) {
426 LY_LIST_FOR(augments[u].child, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200427 child->parent = (struct lysp_node *)&augments[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200428 }
429 LY_ARRAY_FOR(augments[u].actions, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200430 augments[u].actions[v].parent = (struct lysp_node *)&augments[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200431 }
432 LY_ARRAY_FOR(augments[u].notifs, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200433 augments[u].notifs[v].parent = (struct lysp_node *)&augments[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200434 }
435 }
436
437 /* actions */
438 LY_ARRAY_FOR(actions, u) {
439 if (actions[u].input.parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200440 actions[u].input.parent = (struct lysp_node *)&actions[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200441 LY_LIST_FOR(actions[u].input.data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200442 child->parent = (struct lysp_node *)&actions[u].input;
David Sedlákd2ebe572019-07-22 12:53:14 +0200443 }
444 LY_ARRAY_FOR(actions[u].input.groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200445 actions[u].input.groupings[v].parent = (struct lysp_node *)&actions[u].input;
David Sedlákd2ebe572019-07-22 12:53:14 +0200446 }
447 if (actions[u].input.typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200448 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &actions[u].input, 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200449 }
450 }
451 if (actions[u].output.parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200452 actions[u].output.parent = (struct lysp_node *)&actions[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200453 LY_LIST_FOR(actions[u].output.data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200454 child->parent = (struct lysp_node *)&actions[u].output;
David Sedlákd2ebe572019-07-22 12:53:14 +0200455 }
456 LY_ARRAY_FOR(actions[u].output.groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200457 actions[u].output.groupings[v].parent = (struct lysp_node *)&actions[u].output;
David Sedlákd2ebe572019-07-22 12:53:14 +0200458 }
459 if (actions[u].output.typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200460 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &actions[u].output, 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200461 }
462 }
463 LY_ARRAY_FOR(actions[u].groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200464 actions[u].groupings[v].parent = (struct lysp_node *)&actions[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200465 }
466 if (actions[u].typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200467 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &actions[u], 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200468 }
469 }
470
471 /* notifications */
472 LY_ARRAY_FOR(notifs, u) {
473 LY_LIST_FOR(notifs[u].data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200474 child->parent = (struct lysp_node *)&notifs[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200475 }
476 LY_ARRAY_FOR(notifs[u].groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200477 notifs[u].groupings[v].parent = (struct lysp_node *)&notifs[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200478 }
479 if (notifs[u].typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200480 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &notifs[u], 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200481 }
482 }
483
484 return LY_SUCCESS;
485}
486
487LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100488lysp_check_dup_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100489{
490 struct hash_table *ids_global;
491 struct hash_table *ids_scoped;
Radek Krejci0fb28562018-12-13 15:17:37 +0100492 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200493 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200494 uint32_t i;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100495 LY_ERR ret = LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100496
497 /* check name collisions - typedefs and groupings */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100498 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
499 ids_scoped = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200500 LY_ARRAY_FOR(mod->typedefs, v) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100501 ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global, ids_scoped);
502 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100503 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200504 LY_ARRAY_FOR(mod->includes, v) {
505 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100506 ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global, ids_scoped);
507 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci3b1f9292018-11-08 10:58:35 +0100508 }
509 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200510 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
511 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
512 LY_ARRAY_FOR(typedefs, u) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100513 ret = lysp_check_dup_typedef(ctx, (struct lysp_node *)ctx->tpdfs_nodes.objs[i], &typedefs[u], ids_global, ids_scoped);
514 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100515 }
516 }
Michal Vasko405cc9e2020-12-01 12:01:27 +0100517
Radek Krejcibbe09a92018-11-08 09:36:54 +0100518cleanup:
519 lyht_free(ids_global);
520 lyht_free(ids_scoped);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100521 return ret;
522}
523
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100524static ly_bool
525ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
526{
527 void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
528
529 return ptr1 == ptr2 ? 1 : 0;
530}
531
532LY_ERR
533lysp_check_dup_features(struct lys_parser_ctx *ctx, struct lysp_module *mod)
534{
535 LY_ARRAY_COUNT_TYPE u;
536 struct hash_table *ht;
537 struct lysp_feature *f;
538 uint32_t hash;
539 LY_ERR ret = LY_SUCCESS, r;
540
541 ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
542 LY_CHECK_RET(!ht, LY_EMEM);
543
544 /* add all module features into a hash table */
545 LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
546 hash = dict_hash(f->name, strlen(f->name));
547 r = lyht_insert(ht, &f->name, hash, NULL);
548 if (r == LY_EEXIST) {
549 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
550 ret = LY_EVALID;
551 goto cleanup;
552 } else if (r) {
553 ret = r;
554 goto cleanup;
555 }
556 }
557
558 /* add all submodule features into a hash table */
559 LY_ARRAY_FOR(mod->includes, u) {
560 LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
561 hash = dict_hash(f->name, strlen(f->name));
562 r = lyht_insert(ht, &f->name, hash, NULL);
563 if (r == LY_EEXIST) {
564 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
565 ret = LY_EVALID;
566 goto cleanup;
567 } else if (r) {
568 ret = r;
569 goto cleanup;
570 }
571 }
572 }
573
574cleanup:
575 lyht_free(ht);
576 return ret;
577}
578
579LY_ERR
580lysp_check_dup_identities(struct lys_parser_ctx *ctx, struct lysp_module *mod)
581{
582 LY_ARRAY_COUNT_TYPE u;
583 struct hash_table *ht;
584 struct lysp_ident *i;
585 uint32_t hash;
586 LY_ERR ret = LY_SUCCESS, r;
587
588 ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
589 LY_CHECK_RET(!ht, LY_EMEM);
590
591 /* add all module identities into a hash table */
592 LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
593 hash = dict_hash(i->name, strlen(i->name));
594 r = lyht_insert(ht, &i->name, hash, NULL);
595 if (r == LY_EEXIST) {
596 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
597 ret = LY_EVALID;
598 goto cleanup;
599 } else if (r) {
600 ret = r;
601 goto cleanup;
602 }
603 }
604
605 /* add all submodule identities into a hash table */
606 LY_ARRAY_FOR(mod->includes, u) {
607 LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
608 hash = dict_hash(i->name, strlen(i->name));
609 r = lyht_insert(ht, &i->name, hash, NULL);
610 if (r == LY_EEXIST) {
611 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
612 ret = LY_EVALID;
613 goto cleanup;
614 } else if (r) {
615 ret = r;
616 goto cleanup;
617 }
618 }
619 }
620
621cleanup:
622 lyht_free(ht);
623 return ret;
624}
625
Radek Krejci9ed7a192018-10-31 16:23:51 +0100626struct lysp_load_module_check_data {
627 const char *name;
628 const char *revision;
629 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200630 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100631};
632
633static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100634lysp_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 +0100635{
636 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100637 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200638 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100639 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100640 struct lysp_revision *revs;
641
642 name = mod ? mod->mod->name : submod->name;
643 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200644 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100645
646 if (info->name) {
647 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100648 if (strcmp(info->name, name)) {
649 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100650 return LY_EINVAL;
651 }
652 }
653 if (info->revision) {
654 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100655 if (!revs || strcmp(info->revision, revs[0].date)) {
656 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200657 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100658 return LY_EINVAL;
659 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200660 } else if (!latest_revision) {
661 /* do not log, we just need to drop the schema and use the latest revision from the context */
662 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100663 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100664 if (submod) {
665 assert(info->submoduleof);
666
Radek Krejci9ed7a192018-10-31 16:23:51 +0100667 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200668 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100669 LOGVAL(ctx, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200670 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100671 return LY_EVALID;
672 }
673 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100674 if (submod->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100675 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100676 return LY_EVALID;
677 }
678 }
679 if (info->path) {
680 /* check that name and revision match filename */
681 filename = strrchr(info->path, '/');
682 if (!filename) {
683 filename = info->path;
684 } else {
685 filename++;
686 }
687 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100688 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100689 rev = strchr(filename, '@');
690 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100691 if (strncmp(filename, name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +0200692 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100693 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100694 }
695 /* revision */
696 if (rev) {
697 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100698 if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100699 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +0200700 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100701 }
702 }
703 }
704 return LY_SUCCESS;
705}
706
707LY_ERR
Michal Vasko34e334d2021-01-25 16:12:31 +0100708lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, const char **features,
709 ly_bool need_implemented, struct lys_parser_ctx *main_ctx, const char *main_name, ly_bool required,
710 struct lys_glob_unres *unres, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100711{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200712 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100713 char *filepath = NULL;
714 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100715 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100716 LY_ERR ret = LY_SUCCESS;
717 struct lysp_load_module_check_data check_data = {0};
718
719 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 +0200720 &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200721 if (!filepath) {
722 if (required) {
723 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200724 revision ? revision : "");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200725 }
726 return LY_ENOTFOUND;
727 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100728
729 LOGVRB("Loading schema from \"%s\" file.", filepath);
730
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200731 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200732 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +0200733 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100734 check_data.name = name;
735 check_data.revision = revision;
736 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800737 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200738 if (main_ctx) {
Michal Vasko7a0b0762020-09-02 16:37:01 +0200739 ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data,
Michal Vasko69730152020-10-09 16:30:07 +0200740 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200741 } else {
Michal Vasko34e334d2021-01-25 16:12:31 +0100742 ret = lys_create_module(ctx, in, format, need_implemented, lysp_load_module_check, &check_data, features, unres,
Michal Vasko69730152020-10-09 16:30:07 +0200743 (struct lys_module **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200744
745 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200746 ly_in_free(in, 1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200747 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100748
749 *result = mod;
750
751 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200752
Radek Krejci9ed7a192018-10-31 16:23:51 +0100753cleanup:
754 free(filepath);
755 return ret;
756}
757
Radek Krejcid33273d2018-10-25 14:55:52 +0200758LY_ERR
Michal Vasko34e334d2021-01-25 16:12:31 +0100759lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, ly_bool need_implemented,
760 const char **features, struct lys_glob_unres *unres, struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200761{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100762 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200763 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200764
Radek Krejci9ed7a192018-10-31 16:23:51 +0100765 void (*module_data_free)(void *module_data, void *user_data) = NULL;
766 struct lysp_load_module_check_data check_data = {0};
Michal Vasko0550b762020-11-24 18:04:08 +0100767 struct lys_module *ctx_latest = NULL, *m;
Michal Vasko63f3d842020-07-08 10:10:14 +0200768 struct ly_in *in;
Michal Vasko0550b762020-11-24 18:04:08 +0100769 LY_ERR ret;
Michal Vasko34e334d2021-01-25 16:12:31 +0100770 ly_bool implement;
Radek Krejci086c7132018-10-26 15:29:04 +0200771
Michal Vasko405cc9e2020-12-01 12:01:27 +0100772 assert(mod && unres);
Radek Krejci0af46292019-01-11 16:02:31 +0100773
Michal Vasko25d6ad02020-10-22 12:20:22 +0200774 if (ctx->flags & LY_CTX_ALL_IMPLEMENTED) {
Radek Krejcia53d7c92020-08-21 11:30:56 +0200775 implement = 1;
Michal Vasko34e334d2021-01-25 16:12:31 +0100776 } else {
777 implement = need_implemented;
Radek Krejcia53d7c92020-08-21 11:30:56 +0200778 }
779
Michal Vasko0550b762020-11-24 18:04:08 +0100780 /*
781 * try to get the module from the context
782 */
Radek Krejci0af46292019-01-11 16:02:31 +0100783 if (!*mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100784 if (revision) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200785 /* get the specific revision */
Michal Vasko22df3f02020-08-24 13:29:22 +0200786 *mod = (struct lys_module *)ly_ctx_get_module(ctx, name, revision);
Radek Krejci0af46292019-01-11 16:02:31 +0100787 } else {
Michal Vasko0550b762020-11-24 18:04:08 +0100788 if (implement) {
789 /* prefer the implemented module instead of the latest one */
790 *mod = (struct lys_module *)ly_ctx_get_module_implemented(ctx, name);
791 }
792 if (!*mod) {
793 /* get the requested module of the latest revision in the context */
794 *mod = (struct lys_module *)ly_ctx_get_module_latest(ctx, name);
795 if (*mod && ((*mod)->latest_revision == 1)) {
796 /* let us now search with callback and searchpaths to check if there is newer revision outside the context */
797 ctx_latest = *mod;
798 *mod = NULL;
799 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200800 }
Radek Krejci0af46292019-01-11 16:02:31 +0100801 }
Radek Krejci086c7132018-10-26 15:29:04 +0200802 }
803
Michal Vasko0550b762020-11-24 18:04:08 +0100804 /* check collision with other implemented revision */
805 if (implement) {
806 m = ly_ctx_get_module_implemented(ctx, name);
807 if (m && (!*mod || (*mod && (m != *mod)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100808 LOGVAL(ctx, LYVE_REFERENCE, "Module \"%s\" is already present in other implemented revision.", name);
Michal Vasko0550b762020-11-24 18:04:08 +0100809 *mod = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200810 return LY_EDENIED;
811 }
Michal Vasko0550b762020-11-24 18:04:08 +0100812 }
Radek Krejci086c7132018-10-26 15:29:04 +0200813
Michal Vasko0550b762020-11-24 18:04:08 +0100814 /*
815 * no suitable module in the context, try to load it
816 */
817 if (!*mod) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100818 /* module not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200819 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
820search_clb:
821 if (ctx->imp_clb) {
822 if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
Michal Vasko69730152020-10-09 16:30:07 +0200823 &format, &module_data, &module_data_free) == LY_SUCCESS) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200824 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
Radek Krejci9ed7a192018-10-31 16:23:51 +0100825 check_data.name = name;
826 check_data.revision = revision;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100827 lys_create_module(ctx, in, format, implement, lysp_load_module_check, &check_data, features, unres, mod);
Michal Vasko63f3d842020-07-08 10:10:14 +0200828 ly_in_free(in, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100829 if (module_data_free) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200830 module_data_free((void *)module_data, ctx->imp_clb_data);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100831 }
Radek Krejci086c7132018-10-26 15:29:04 +0200832 }
833 }
834 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
835 goto search_file;
836 }
837 } else {
838search_file:
839 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
840 /* module was not received from the callback or there is no callback set */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100841 lys_module_localfile(ctx, name, revision, features, implement, NULL, NULL, ctx_latest ? 0 : 1, unres,
842 (void **)mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200843 }
Michal Vasko0550b762020-11-24 18:04:08 +0100844 if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejci086c7132018-10-26 15:29:04 +0200845 goto search_clb;
846 }
847 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100848
Radek Krejcib3289d62019-09-18 12:21:39 +0200849 /* update the latest_revision flag - here we have selected the latest available schema,
850 * consider that even the callback provides correct latest revision */
Michal Vasko0550b762020-11-24 18:04:08 +0100851 if (!*mod && ctx_latest) {
852 LOGVRB("Newer revision than %s-%s not found, using this as the latest revision.", ctx_latest->name,
853 ctx_latest->revision);
854 ctx_latest->latest_revision = 2;
855 *mod = ctx_latest;
856 } else if (*mod && !revision && ((*mod)->latest_revision == 1)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100857 (*mod)->latest_revision = 2;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100858 }
Radek Krejci086c7132018-10-26 15:29:04 +0200859
Michal Vasko0550b762020-11-24 18:04:08 +0100860 if (!*mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100861 LOGVAL(ctx, LYVE_REFERENCE, "%s \"%s\" module failed.", implement ? "Loading" : "Importing", name);
Michal Vasko0550b762020-11-24 18:04:08 +0100862 return LY_EVALID;
863 }
864 } else {
865 /* we have module from the current context, circular check */
866 if ((*mod)->parsed->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100867 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
Radek Krejci086c7132018-10-26 15:29:04 +0200868 *mod = NULL;
869 return LY_EVALID;
870 }
871 }
Radek Krejci086c7132018-10-26 15:29:04 +0200872
Michal Vasko0550b762020-11-24 18:04:08 +0100873 /*
874 * module found, make sure it is implemented if should be
875 */
Michal Vasko962b6cd2020-12-08 10:07:49 +0100876 if (implement) {
Radek Krejci2415f882021-01-20 16:27:09 +0100877 if (!(*mod)->implemented) {
878 /* implement */
879 ret = lys_set_implemented_r(*mod, features, unres);
880 if (ret) {
881 *mod = NULL;
882 return ret;
883 }
884 } else if (features) {
Michal Vasko962b6cd2020-12-08 10:07:49 +0100885 /* set features if different */
886 ret = lys_set_features((*mod)->parsed, features);
887 if (!ret) {
888 /* context need to be recompiled so that feature changes are properly applied */
889 unres->recompile = 1;
890 } else if (ret != LY_EEXIST) {
891 /* error */
892 return ret;
893 } /* else no feature changes */
Michal Vaskoc5d64862020-11-24 18:04:45 +0100894 }
Radek Krejci086c7132018-10-26 15:29:04 +0200895 }
Radek Krejci086c7132018-10-26 15:29:04 +0200896
897 return LY_SUCCESS;
898}
899
900LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200901lysp_check_stringchar(struct lys_parser_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +0200902{
903 if (!is_yangutf8char(c)) {
904 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
905 return LY_EVALID;
906 }
907 return LY_SUCCESS;
908}
909
910LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200911lysp_check_identifierchar(struct lys_parser_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +0200912{
Michal Vasko69730152020-10-09 16:30:07 +0200913 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +0200914 if (!is_yangidentstartchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200915 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200916 return LY_EVALID;
917 }
918 if (prefix) {
919 if (first) {
920 (*prefix) = 0;
921 } else {
922 (*prefix) = 2;
923 }
924 }
Michal Vasko69730152020-10-09 16:30:07 +0200925 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +0200926 (*prefix) = 1;
927 } else if (!is_yangidentchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200928 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200929 return LY_EVALID;
930 }
931
932 return LY_SUCCESS;
933}
934
Radek Krejci771928a2021-01-19 13:42:36 +0100935/**
936 * @brief Try to find the parsed submodule in main module for the given include record.
937 *
938 * @param[in] pctx main parser context
939 * @param[in] inc The include record with missing parsed submodule. According to include info try to find
940 * the corresponding parsed submodule in main module's includes.
941 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
942 * @return LY_ENOT - the parsed module was not found.
943 * @return LY_EVALID - YANG rule violation
944 */
945static LY_ERR
946lysp_get_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200947{
Radek Krejci771928a2021-01-19 13:42:36 +0100948 LY_ARRAY_COUNT_TYPE i;
949 struct lysp_module *main_pmod = pctx->parsed_mod->mod->parsed;
Michal Vasko69730152020-10-09 16:30:07 +0200950
Radek Krejci771928a2021-01-19 13:42:36 +0100951 LY_ARRAY_FOR(main_pmod->includes, i) {
952 if (strcmp(main_pmod->includes[i].name, inc->name)) {
953 continue;
954 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200955
Radek Krejci771928a2021-01-19 13:42:36 +0100956 if (inc->rev[0] && strncmp(inc->rev, main_pmod->includes[i].rev, LY_REV_SIZE)) {
957 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
958 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
959 ((struct lysp_submodule *)pctx->parsed_mod)->name, inc->rev,
960 main_pmod->includes[i].name, main_pmod->includes[i].rev, main_pmod->mod->name);
961 return LY_EVALID;
962 }
963
964 inc->submodule = main_pmod->includes[i].submodule;
965 return inc->submodule ? LY_SUCCESS : LY_ENOT;
966 }
967
968 if (main_pmod->version == LYS_VERSION_1_1) {
969 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
970 "YANG 1.1 requires all submodules to be included from main module. "
971 "But submodule \"%s\" includes submodule \"%s\" which is not included by main module \"%s\".",
972 ((struct lysp_submodule *)pctx->parsed_mod)->name, inc->name, main_pmod->mod->name);
973 return LY_EVALID;
974 } else {
975 return LY_ENOT;
976 }
977}
978
979/**
980 * @brief Make the copy of the given include record into the main module.
981 *
982 * YANG 1.0 does not require the main module to include all the submodules. Therefore, parsing submodules can cause
983 * reallocating and extending the includes array in the main module by the submodules included only in submodules.
984 *
985 * @param[in] pctx main parser context
986 * @param[in] inc Include record to copy into main module taken from @p pctx.
987 * @return LY_ERR value.
988 */
989static LY_ERR
990lysp_inject_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
991{
992 LY_ARRAY_COUNT_TYPE i;
993 struct lysp_include *inc_new, *inc_tofill = NULL;
994 struct lysp_module *main_pmod = pctx->parsed_mod->mod->parsed;
995
996 /* first, try to find the corresponding record with missing parsed submodule */
997 LY_ARRAY_FOR(main_pmod->includes, i) {
998 if (strcmp(main_pmod->includes[i].name, inc->name)) {
999 continue;
1000 }
1001 inc_tofill = &main_pmod->includes[i];
1002 break;
1003 }
1004
1005 if (inc_tofill) {
1006 inc_tofill->submodule = inc->submodule;
1007 } else {
1008 LY_ARRAY_NEW_RET(PARSER_CTX(pctx), main_pmod->includes, inc_new, LY_EMEM);
1009
1010 inc_new->submodule = inc->submodule;
1011 DUP_STRING_RET(PARSER_CTX(pctx), inc->name, inc_new->name);
1012 DUP_STRING_RET(PARSER_CTX(pctx), inc->dsc, inc_new->dsc);
1013 DUP_STRING_RET(PARSER_CTX(pctx), inc->ref, inc_new->ref);
1014 /* TODO duplicate extensions */
1015 memcpy(inc_new->rev, inc->rev, LY_REV_SIZE);
1016 inc_new->injected = 1;
1017 }
1018 return LY_SUCCESS;
1019}
1020
1021LY_ERR
1022lysp_load_submodules(struct lys_parser_ctx *pctx, struct lysp_module *pmod)
1023{
1024 LY_ARRAY_COUNT_TYPE u;
1025 struct ly_ctx *ctx = PARSER_CTX(pctx);
1026
1027 LY_ARRAY_FOR(pmod->includes, u) {
1028 LY_ERR ret = LY_SUCCESS;
1029 struct lysp_submodule *submod = NULL;
1030 struct lysp_include *inc = &pmod->includes[u];
1031
1032 if (inc->submodule) {
1033 continue;
1034 }
1035
1036 if (pmod->is_submod) {
1037 /* try to find the submodule in the main module or its submodules */
1038 ret = lysp_get_submodule(pctx, inc);
1039 LY_CHECK_RET(ret && ret != LY_ENOT, ret);
1040 LY_CHECK_RET(ret == LY_SUCCESS, LY_SUCCESS); /* submodule found in linked with the inc */
1041 }
1042
1043 /* submodule not present in the main module, get the input data and parse it */
1044 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcidf549132021-01-21 10:32:32 +01001045search_clb:
Radek Krejci771928a2021-01-19 13:42:36 +01001046 if (ctx->imp_clb) {
1047 const char *submodule_data = NULL;
1048 LYS_INFORMAT format = LYS_IN_UNKNOWN;
1049 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
1050 struct lysp_load_module_check_data check_data = {0};
1051 struct ly_in *in;
1052
1053 if (ctx->imp_clb(pctx->parsed_mod->mod->name, NULL, inc->name,
1054 inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
1055 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
1056 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
1057 check_data.name = inc->name;
1058 check_data.revision = inc->rev[0] ? inc->rev : NULL;
1059 check_data.submoduleof = pctx->parsed_mod->mod->name;
1060 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, &submod);
1061
1062 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1063 * submodule's include into main module, where it is missing */
1064 inc = &pmod->includes[u];
1065
1066 ly_in_free(in, 0);
1067 if (submodule_data_free) {
1068 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
1069 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001070 }
1071 }
Radek Krejci771928a2021-01-19 13:42:36 +01001072 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1073 goto search_file;
1074 }
1075 } else {
Radek Krejcidf549132021-01-21 10:32:32 +01001076search_file:
Radek Krejci771928a2021-01-19 13:42:36 +01001077 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
1078 /* submodule was not received from the callback or there is no callback set */
1079 lys_module_localfile(ctx, inc->name,
1080 inc->rev[0] ? inc->rev : NULL, NULL, 0, pctx,
1081 pctx->parsed_mod->mod->name, 1, NULL, (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001082
Radek Krejci771928a2021-01-19 13:42:36 +01001083 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1084 * submodule's include into main module, where it is missing */
1085 inc = &pmod->includes[u];
1086 }
1087 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1088 goto search_clb;
1089 }
1090 }
1091 if (submod) {
1092 if (!inc->rev[0] && (submod->latest_revision == 1)) {
1093 /* update the latest_revision flag - here we have selected the latest available schema,
1094 * consider that even the callback provides correct latest revision */
1095 submod->latest_revision = 2;
1096 }
1097
1098 inc->submodule = submod;
1099 if (ret == LY_ENOT) {
1100 /* the submodule include is not present in YANG 1.0 main module - add it there */
1101 LY_CHECK_RET(lysp_inject_submodule(pctx, &pmod->includes[u]));
1102 }
1103 }
1104 if (!inc->submodule) {
1105 LOGVAL(ctx, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name,
1106 pctx->parsed_mod->is_submod ? ((struct lysp_submodule *)pctx->parsed_mod)->name : pctx->parsed_mod->mod->name);
1107 return LY_EVALID;
1108 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001109 }
1110
1111 return LY_SUCCESS;
1112}
1113
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001114API const struct lysc_when *
1115lysc_has_when(const struct lysc_node *node)
1116{
1117 if (!node) {
1118 return NULL;
1119 }
1120
1121 do {
Michal Vaskoa5705e52020-12-09 18:15:14 +01001122 switch (node->nodetype) {
1123 case LYS_RPC:
1124 case LYS_ACTION:
1125 if (((struct lysc_action *)node)->when) {
1126 return *((struct lysc_action *)node)->when;
1127 }
1128 break;
1129 case LYS_NOTIF:
1130 if (((struct lysc_notif *)node)->when) {
1131 return *((struct lysc_notif *)node)->when;
1132 }
1133 break;
1134 default:
1135 if (node->when) {
1136 return *node->when;
1137 }
1138 break;
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001139 }
Michal Vaskoa5705e52020-12-09 18:15:14 +01001140
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001141 node = node->parent;
1142 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1143
1144 return NULL;
1145}
1146
Radek Krejci0935f412019-08-20 16:15:18 +02001147API const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001148lys_nodetype2str(uint16_t nodetype)
1149{
Michal Vaskod989ba02020-08-24 10:59:24 +02001150 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001151 case LYS_CONTAINER:
1152 return "container";
1153 case LYS_CHOICE:
1154 return "choice";
1155 case LYS_LEAF:
1156 return "leaf";
1157 case LYS_LEAFLIST:
1158 return "leaf-list";
1159 case LYS_LIST:
1160 return "list";
1161 case LYS_ANYXML:
1162 return "anyxml";
1163 case LYS_ANYDATA:
1164 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001165 case LYS_CASE:
1166 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001167 case LYS_RPC:
1168 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001169 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001170 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001171 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001172 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001173 case LYS_USES:
1174 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001175 default:
1176 return "unknown";
1177 }
1178}
1179
Radek Krejci693262f2019-04-29 15:23:20 +02001180const char *
1181lys_datatype2str(LY_DATA_TYPE basetype)
1182{
Michal Vaskod989ba02020-08-24 10:59:24 +02001183 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001184 case LY_TYPE_BINARY:
1185 return "binary";
1186 case LY_TYPE_UINT8:
1187 return "uint8";
1188 case LY_TYPE_UINT16:
1189 return "uint16";
1190 case LY_TYPE_UINT32:
1191 return "uint32";
1192 case LY_TYPE_UINT64:
1193 return "uint64";
1194 case LY_TYPE_STRING:
1195 return "string";
1196 case LY_TYPE_BITS:
1197 return "bits";
1198 case LY_TYPE_BOOL:
1199 return "boolean";
1200 case LY_TYPE_DEC64:
1201 return "decimal64";
1202 case LY_TYPE_EMPTY:
1203 return "empty";
1204 case LY_TYPE_ENUM:
1205 return "enumeration";
1206 case LY_TYPE_IDENT:
1207 return "identityref";
1208 case LY_TYPE_INST:
1209 return "instance-identifier";
1210 case LY_TYPE_LEAFREF:
1211 return "leafref";
1212 case LY_TYPE_UNION:
1213 return "union";
1214 case LY_TYPE_INT8:
1215 return "int8";
1216 case LY_TYPE_INT16:
1217 return "int16";
1218 case LY_TYPE_INT32:
1219 return "int32";
1220 case LY_TYPE_INT64:
1221 return "int64";
1222 default:
1223 return "unknown";
1224 }
1225}
1226
Radek Krejci056d0a82018-12-06 16:57:25 +01001227API const struct lysp_tpdf *
1228lysp_node_typedefs(const struct lysp_node *node)
1229{
Radek Krejci0fb28562018-12-13 15:17:37 +01001230 switch (node->nodetype) {
1231 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001232 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001233 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001234 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001235 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001236 return ((struct lysp_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001237 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001238 case LYS_ACTION:
Michal Vasko22df3f02020-08-24 13:29:22 +02001239 return ((struct lysp_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001240 case LYS_INPUT:
1241 case LYS_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001242 return ((struct lysp_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001243 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001244 return ((struct lysp_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001245 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001246 return NULL;
1247 }
1248}
1249
Radek Krejci53ea6152018-12-13 15:21:15 +01001250API const struct lysp_grp *
1251lysp_node_groupings(const struct lysp_node *node)
1252{
1253 switch (node->nodetype) {
1254 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001255 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001256 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001257 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001258 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001259 return ((struct lysp_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001260 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001261 case LYS_ACTION:
Michal Vasko22df3f02020-08-24 13:29:22 +02001262 return ((struct lysp_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001263 case LYS_INPUT:
1264 case LYS_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001265 return ((struct lysp_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001266 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001267 return ((struct lysp_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001268 default:
1269 return NULL;
1270 }
1271}
1272
Radek Krejcibbe09a92018-11-08 09:36:54 +01001273struct lysp_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001274lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001275{
1276 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001277
Radek Krejcibbe09a92018-11-08 09:36:54 +01001278 switch (node->nodetype) {
1279 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001280 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001281 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001282 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001283 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001284 return &((struct lysp_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001285 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001286 return &((struct lysp_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001287 default:
1288 return NULL;
1289 }
1290}
1291
Radek Krejci056d0a82018-12-06 16:57:25 +01001292API const struct lysp_action *
1293lysp_node_actions(const struct lysp_node *node)
1294{
1295 struct lysp_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001296
Michal Vasko22df3f02020-08-24 13:29:22 +02001297 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001298 if (actions) {
1299 return *actions;
1300 } else {
1301 return NULL;
1302 }
1303}
1304
Radek Krejcibbe09a92018-11-08 09:36:54 +01001305struct lysp_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001306lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001307{
1308 assert(node);
1309 switch (node->nodetype) {
1310 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001311 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001312 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001313 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001314 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001315 return &((struct lysp_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001316 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001317 return &((struct lysp_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001318 default:
1319 return NULL;
1320 }
1321}
1322
Radek Krejci056d0a82018-12-06 16:57:25 +01001323API const struct lysp_notif *
1324lysp_node_notifs(const struct lysp_node *node)
1325{
1326 struct lysp_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001327
Michal Vasko22df3f02020-08-24 13:29:22 +02001328 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001329 if (notifs) {
1330 return *notifs;
1331 } else {
1332 return NULL;
1333 }
1334}
1335
Radek Krejcibbe09a92018-11-08 09:36:54 +01001336struct lysp_node **
Radek Krejci056d0a82018-12-06 16:57:25 +01001337lysp_node_children_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001338{
1339 assert(node);
1340 switch (node->nodetype) {
1341 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001342 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001343 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001344 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001345 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001346 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001347 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001348 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001349 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001350 return &((struct lysp_grp *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001351 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001352 return &((struct lysp_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001353 case LYS_INPUT:
1354 case LYS_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001355 return &((struct lysp_action_inout *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001356 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001357 return &((struct lysp_notif *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001358 default:
1359 return NULL;
1360 }
1361}
1362
Radek Krejci056d0a82018-12-06 16:57:25 +01001363API const struct lysp_node *
1364lysp_node_children(const struct lysp_node *node)
1365{
1366 struct lysp_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001367
1368 if (!node) {
1369 return NULL;
1370 }
1371
Michal Vasko22df3f02020-08-24 13:29:22 +02001372 children = lysp_node_children_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001373 if (children) {
1374 return *children;
1375 } else {
1376 return NULL;
1377 }
1378}
1379
1380struct lysc_action **
1381lysc_node_actions_p(struct lysc_node *node)
1382{
1383 assert(node);
1384 switch (node->nodetype) {
1385 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001386 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001387 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001388 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001389 default:
1390 return NULL;
1391 }
1392}
1393
1394API const struct lysc_action *
1395lysc_node_actions(const struct lysc_node *node)
1396{
1397 struct lysc_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001398
Michal Vasko22df3f02020-08-24 13:29:22 +02001399 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001400 if (actions) {
1401 return *actions;
1402 } else {
1403 return NULL;
1404 }
1405}
1406
1407struct lysc_notif **
1408lysc_node_notifs_p(struct lysc_node *node)
1409{
1410 assert(node);
1411 switch (node->nodetype) {
1412 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001413 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001414 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001415 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001416 default:
1417 return NULL;
1418 }
1419}
1420
1421API const struct lysc_notif *
1422lysc_node_notifs(const struct lysc_node *node)
1423{
1424 struct lysc_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001425
Michal Vasko22df3f02020-08-24 13:29:22 +02001426 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001427 if (notifs) {
1428 return *notifs;
1429 } else {
1430 return NULL;
1431 }
1432}
1433
Radek Krejcibbe09a92018-11-08 09:36:54 +01001434struct lysc_node **
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001435lysc_node_children_p(const struct lysc_node *node, uint16_t flags)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001436{
1437 assert(node);
1438 switch (node->nodetype) {
1439 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001440 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001441 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001442 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001443 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001444 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001445 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001446 return &((struct lysc_node_list *)node)->child;
Michal Vasko1bf09392020-03-27 12:38:10 +01001447 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001448 case LYS_ACTION:
1449 if (flags & LYS_CONFIG_R) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001450 return &((struct lysc_action *)node)->output.data;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001451 } else {
1452 /* LYS_CONFIG_W, but also the default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02001453 return &((struct lysc_action *)node)->input.data;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001454 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02001455 case LYS_INPUT:
1456 case LYS_OUTPUT:
1457 return &((struct lysc_action_inout *)node)->data;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001458 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001459 return &((struct lysc_notif *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001460 default:
1461 return NULL;
1462 }
1463}
1464
Radek Krejci056d0a82018-12-06 16:57:25 +01001465API const struct lysc_node *
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001466lysc_node_children(const struct lysc_node *node, uint16_t flags)
Radek Krejcia3045382018-11-22 14:30:31 +01001467{
Radek Krejci056d0a82018-12-06 16:57:25 +01001468 struct lysc_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001469
1470 if (!node) {
1471 return NULL;
1472 }
1473
Radek Krejcibe154442021-01-21 11:06:36 +01001474 children = lysc_node_children_p(node, flags);
Radek Krejci056d0a82018-12-06 16:57:25 +01001475 if (children) {
1476 return *children;
1477 } else {
Radek Krejcia3045382018-11-22 14:30:31 +01001478 return NULL;
1479 }
1480}
1481
Michal Vasko2a668712020-10-21 11:48:09 +02001482API const struct lysc_node *
Michal Vasko208a04a2020-10-21 15:17:12 +02001483lysc_node_children_full(const struct lysc_node *node, uint16_t flags)
Michal Vasko2a668712020-10-21 11:48:09 +02001484{
Radek Krejcibe154442021-01-21 11:06:36 +01001485 if (!node) {
1486 return NULL;
1487 }
1488
1489 if (node->nodetype == LYS_RPC || node->nodetype == LYS_ACTION) {
Michal Vasko2a668712020-10-21 11:48:09 +02001490 if (flags & LYS_CONFIG_R) {
1491 return (struct lysc_node *)&((struct lysc_action *)node)->output;
1492 } else {
1493 /* LYS_CONFIG_W, but also the default case */
1494 return (struct lysc_node *)&((struct lysc_action *)node)->input;
1495 }
Radek Krejcibe154442021-01-21 11:06:36 +01001496 } else {
1497 return lysc_node_children(node, flags);
Michal Vasko2a668712020-10-21 11:48:09 +02001498 }
1499}
1500
1501API const struct lysc_node *
Michal Vasko208a04a2020-10-21 15:17:12 +02001502lysc_node_parent_full(const struct lysc_node *node)
Michal Vasko2a668712020-10-21 11:48:09 +02001503{
1504 if (!node) {
1505 return NULL;
1506 } else if (node->nodetype == LYS_INPUT) {
1507 return (struct lysc_node *)(((char *)node) - offsetof(struct lysc_action, input));
1508 } else if (node->nodetype == LYS_OUTPUT) {
1509 return (struct lysc_node *)(((char *)node) - offsetof(struct lysc_action, output));
1510 } else if (node->parent && (node->parent->nodetype & (LYS_RPC | LYS_ACTION))) {
1511 if (node->flags & LYS_CONFIG_W) {
1512 return (struct lysc_node *)&((struct lysc_action *)node->parent)->input;
1513 } else {
1514 return (struct lysc_node *)&((struct lysc_action *)node->parent)->output;
1515 }
1516 } else {
1517 return node->parent;
1518 }
1519}
1520
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001521struct lys_module *
1522lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1523{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001524 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001525 if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) {
Michal Vasko69730152020-10-09 16:30:07 +02001526 return (struct lys_module *)ctx->list.objs[u];
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001527 }
1528 }
1529 return NULL;
1530}
1531
Radek Krejcid6b76452019-09-03 17:03:03 +02001532enum ly_stmt
Radek Krejcid54412f2020-12-17 20:25:35 +01001533lysp_match_kw(struct ly_in *in, uint64_t *indent)
David Sedlákc10e7902018-12-17 02:17:59 +01001534{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001535/**
Radek Krejcid54412f2020-12-17 20:25:35 +01001536 * @brief Move the input by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001537 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001538 *
1539 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001540 */
Radek Krejcid54412f2020-12-17 20:25:35 +01001541#define MOVE_IN(COUNT) \
1542 ly_in_skip(in, COUNT); \
1543 if (indent) { \
1544 (*indent)+=COUNT; \
1545 }
1546#define IF_KW(STR, LEN, STMT) \
1547 if (!strncmp(in->current, STR, LEN)) { \
1548 MOVE_IN(LEN); \
1549 (*kw)=STMT; \
1550 }
1551#define IF_KW_PREFIX(STR, LEN) \
1552 if (!strncmp(in->current, STR, LEN)) { \
1553 MOVE_IN(LEN);
1554#define IF_KW_PREFIX_END \
1555 }
David Sedlák572e7ab2019-06-04 16:01:58 +02001556
Michal Vasko63f3d842020-07-08 10:10:14 +02001557 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001558 enum ly_stmt result = LY_STMT_NONE;
1559 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001560 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001561 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001562 case 'a':
Radek Krejcid54412f2020-12-17 20:25:35 +01001563 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001564 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1565 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1566 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001567 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001568 IF_KW("data", 4, LY_STMT_ANYDATA)
1569 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001570 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001571 break;
1572 case 'b':
Radek Krejcid54412f2020-12-17 20:25:35 +01001573 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001574 IF_KW("ase", 3, LY_STMT_BASE)
1575 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1576 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001577 break;
1578 case 'c':
Radek Krejcid54412f2020-12-17 20:25:35 +01001579 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001580 IF_KW("ase", 3, LY_STMT_CASE)
1581 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001582 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001583 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001584 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001585 IF_KW("ct", 2, LY_STMT_CONTACT)
1586 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001587 IF_KW_PREFIX_END
1588 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001589 break;
1590 case 'd':
Radek Krejcid54412f2020-12-17 20:25:35 +01001591 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001592 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001593 IF_KW("fault", 5, LY_STMT_DEFAULT)
1594 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001595 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001596 IF_KW("e", 1, LY_STMT_DEVIATE)
1597 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001598 IF_KW_PREFIX_END
1599 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001600 break;
1601 case 'e':
Radek Krejcid54412f2020-12-17 20:25:35 +01001602 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001603 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001604 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001605 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1606 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001607 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001608 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001609 break;
1610 case 'f':
Radek Krejcid54412f2020-12-17 20:25:35 +01001611 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001612 IF_KW("eature", 6, LY_STMT_FEATURE)
1613 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001614 break;
1615 case 'g':
Radek Krejcid54412f2020-12-17 20:25:35 +01001616 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001617 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001618 break;
1619 case 'i':
Radek Krejcid54412f2020-12-17 20:25:35 +01001620 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001621 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1622 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1623 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001624 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001625 IF_KW("clude", 5, LY_STMT_INCLUDE)
1626 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001627 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001628 break;
1629 case 'k':
Radek Krejcid54412f2020-12-17 20:25:35 +01001630 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001631 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001632 break;
1633 case 'l':
Radek Krejcid54412f2020-12-17 20:25:35 +01001634 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001635 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001636 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1637 else IF_KW("af", 2, LY_STMT_LEAF)
1638 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001639 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001640 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001641 break;
1642 case 'm':
Radek Krejcid54412f2020-12-17 20:25:35 +01001643 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001644 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001645 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1646 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001647 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001648 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1649 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001650 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001651 IF_KW("ule", 3, LY_STMT_MODULE)
1652 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001653 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001654 break;
1655 case 'n':
Radek Krejcid54412f2020-12-17 20:25:35 +01001656 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001657 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1658 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001659 break;
1660 case 'o':
Radek Krejcid54412f2020-12-17 20:25:35 +01001661 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001662 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001663 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1664 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001665 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001666 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001667 break;
1668 case 'p':
Radek Krejcid54412f2020-12-17 20:25:35 +01001669 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001670 IF_KW("ath", 3, LY_STMT_PATH)
1671 else IF_KW("attern", 6, LY_STMT_PATTERN)
1672 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001673 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001674 IF_KW("fix", 3, LY_STMT_PREFIX)
1675 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001676 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001677 break;
1678 case 'r':
Radek Krejcid54412f2020-12-17 20:25:35 +01001679 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001680 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001681 else IF_KW_PREFIX("e", 1)
1682 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001683 IF_KW("erence", 6, LY_STMT_REFERENCE)
1684 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001685 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001686 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1687 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1688 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001689 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001690 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001691 break;
1692 case 's':
Radek Krejcid54412f2020-12-17 20:25:35 +01001693 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001694 IF_KW("tatus", 5, LY_STMT_STATUS)
1695 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001696 break;
1697 case 't':
Radek Krejcid54412f2020-12-17 20:25:35 +01001698 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001699 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1700 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001701 break;
1702 case 'u':
Radek Krejcid54412f2020-12-17 20:25:35 +01001703 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001704 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001705 IF_KW("que", 3, LY_STMT_UNIQUE)
1706 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001707 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001708 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001709 break;
1710 case 'v':
Radek Krejcid54412f2020-12-17 20:25:35 +01001711 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001712 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001713 break;
1714 case 'w':
Radek Krejcid54412f2020-12-17 20:25:35 +01001715 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001716 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001717 break;
1718 case 'y':
Radek Krejcid54412f2020-12-17 20:25:35 +01001719 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001720 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
1721 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001722 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001723 default:
Radek Krejcid54412f2020-12-17 20:25:35 +01001724 /* if indent is not NULL we are matching keyword from YANG data */
1725 if (indent) {
Michal Vasko63f3d842020-07-08 10:10:14 +02001726 if (in->current[0] == ';') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001727 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001728 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02001729 } else if (in->current[0] == '{') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001730 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001731 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001732 } else if (in->current[0] == '}') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001733 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001734 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001735 }
1736 }
David Sedlák23a59a62018-10-26 13:08:02 +02001737 break;
1738 }
1739
Michal Vasko63f3d842020-07-08 10:10:14 +02001740 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02001741 /* the keyword is not terminated */
1742 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001743 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02001744 }
1745
David Sedlák1bccdfa2019-06-17 15:55:27 +02001746#undef IF_KW
1747#undef IF_KW_PREFIX
1748#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01001749#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02001750 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01001751
David Sedlák1bccdfa2019-06-17 15:55:27 +02001752 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02001753}
David Sedlákecf5eb82019-06-03 14:12:44 +02001754
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001755LY_ARRAY_COUNT_TYPE
1756lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, LYEXT_SUBSTMT substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001757{
1758 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1759
Michal Vaskod989ba02020-08-24 10:59:24 +02001760 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001761 if (ext[index].insubstmt == substmt) {
1762 return index;
1763 }
1764 }
1765
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001766 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001767}
1768
Michal Vasko62ed12d2020-05-21 10:08:25 +02001769const struct lysc_node *
Michal Vasko72244882021-01-12 15:21:05 +01001770lysc_data_node(const struct lysc_node *schema)
Michal Vasko62ed12d2020-05-21 10:08:25 +02001771{
1772 const struct lysc_node *parent;
1773
Michal Vasko72244882021-01-12 15:21:05 +01001774 parent = schema;
Radek Krejcidf549132021-01-21 10:32:32 +01001775 while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC |
1776 LYS_ACTION | LYS_NOTIF))) {
Michal Vasko72244882021-01-12 15:21:05 +01001777 parent = lysc_node_parent_full(parent);
1778 }
Michal Vasko62ed12d2020-05-21 10:08:25 +02001779
1780 return parent;
1781}
Michal Vasko00cbf532020-06-15 13:58:47 +02001782
Radek Krejci857189e2020-09-01 13:26:36 +02001783ly_bool
Michal Vasko00cbf532020-06-15 13:58:47 +02001784lysc_is_output(const struct lysc_node *schema)
1785{
1786 const struct lysc_node *parent;
1787
1788 assert(schema);
1789
Radek Krejci1e008d22020-08-17 11:37:37 +02001790 for (parent = schema->parent; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION)); parent = parent->parent) {}
Michal Vasko00cbf532020-06-15 13:58:47 +02001791 if (parent && (schema->flags & LYS_CONFIG_R)) {
1792 return 1;
1793 }
1794 return 0;
1795}