blob: 1d2a8f21484ab93332a7826cae6a8a2564cd9da1 [file] [log] [blame]
Radek Krejci86d106e2018-10-18 09:53:19 +02001/**
2 * @file tree_schema_helpers.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejcie7b95092019-05-15 11:03:07 +02004 * @brief Parsing and validation helper functions for schema trees
Radek Krejci86d106e2018-10-18 09:53:19 +02005 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Krejci535ea9f2020-05-29 16:01:05 +020014
15#define _GNU_SOURCE
Radek Krejci86d106e2018-10-18 09:53:19 +020016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020018#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010019#include <stddef.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <stdint.h>
Radek Krejci9ed7a192018-10-31 16:23:51 +010021#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020023#include <time.h>
24
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020026#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "context.h"
Radek Krejci77114102021-03-10 15:21:57 +010028#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020029#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010030#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020031#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "log.h"
Michal Vasko69730152020-10-09 16:30:07 +020033#include "parser_schema.h"
Michal Vasko962b6cd2020-12-08 10:07:49 +010034#include "schema_compile.h"
Michal Vasko79135ae2020-12-16 10:08:35 +010035#include "schema_features.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020036#include "set.h"
37#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010038#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020039#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020040#include "tree_schema_internal.h"
41
Radek Krejci85747952019-06-07 16:43:43 +020042LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +020043lysp_check_prefix(struct lys_parser_ctx *ctx, struct lysp_import *imports, const char *module_prefix, const char **value)
Radek Krejci86d106e2018-10-18 09:53:19 +020044{
45 struct lysp_import *i;
46
Michal Vasko69730152020-10-09 16:30:07 +020047 if (module_prefix && (&module_prefix != value) && !strcmp(module_prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010048 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value);
Radek Krejci86d106e2018-10-18 09:53:19 +020049 return LY_EEXIST;
50 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +010051 LY_ARRAY_FOR(imports, struct lysp_import, i) {
Michal Vasko69730152020-10-09 16:30:07 +020052 if (i->prefix && (&i->prefix != value) && !strcmp(i->prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010053 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +010054 return LY_EEXIST;
Radek Krejci86d106e2018-10-18 09:53:19 +020055 }
56 }
57 return LY_SUCCESS;
58}
59
60LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020061lysp_check_date(struct lys_parser_ctx *ctx, const char *date, uint8_t date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +020062{
Radek Krejci86d106e2018-10-18 09:53:19 +020063 struct tm tm, tm_;
64 char *r;
65
Michal Vaskob36053d2020-03-26 15:49:30 +010066 LY_CHECK_ARG_RET(ctx ? PARSER_CTX(ctx) : NULL, date, LY_EINVAL);
67 LY_CHECK_ERR_RET(date_len != LY_REV_SIZE - 1, LOGARG(ctx ? PARSER_CTX(ctx) : NULL, date_len), LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +020068
Radek Krejcif13b87b2020-12-01 22:02:17 +010069 /* check format: YYYY-MM-DD */
Radek Krejci1deb5be2020-08-26 16:43:36 +020070 for (uint8_t i = 0; i < date_len; i++) {
Michal Vasko69730152020-10-09 16:30:07 +020071 if ((i == 4) || (i == 7)) {
Radek Krejci86d106e2018-10-18 09:53:19 +020072 if (date[i] != '-') {
73 goto error;
74 }
75 } else if (!isdigit(date[i])) {
76 goto error;
77 }
78 }
79
80 /* check content, e.g. 2018-02-31 */
81 memset(&tm, 0, sizeof tm);
82 r = strptime(date, "%Y-%m-%d", &tm);
Michal Vasko69730152020-10-09 16:30:07 +020083 if (!r || (r != &date[LY_REV_SIZE - 1])) {
Radek Krejci86d106e2018-10-18 09:53:19 +020084 goto error;
85 }
86 memcpy(&tm_, &tm, sizeof tm);
87 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
88 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
89 /* checking days is enough, since other errors
90 * have been checked by strptime() */
91 goto error;
92 }
93
94 return LY_SUCCESS;
95
96error:
Radek Krejcid33273d2018-10-25 14:55:52 +020097 if (stmt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010098 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt);
Radek Krejcid33273d2018-10-25 14:55:52 +020099 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200100 return LY_EINVAL;
101}
102
103void
104lysp_sort_revisions(struct lysp_revision *revs)
105{
Radek Krejci857189e2020-09-01 13:26:36 +0200106 LY_ARRAY_COUNT_TYPE i, r;
Radek Krejci86d106e2018-10-18 09:53:19 +0200107 struct lysp_revision rev;
108
Radek Krejcic7d13e32020-12-09 12:32:24 +0100109 for (i = 1, r = 0; i < LY_ARRAY_COUNT(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200110 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200111 r = i;
112 }
113 }
114
115 if (r) {
116 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200117 memcpy(&rev, &revs[0], sizeof rev);
118 memcpy(&revs[0], &revs[r], sizeof rev);
119 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200120 }
121}
Radek Krejci151a5b72018-10-19 14:21:44 +0200122
Radek Krejcibbe09a92018-11-08 09:36:54 +0100123static const struct lysp_tpdf *
124lysp_type_match(const char *name, struct lysp_node *node)
125{
Radek Krejci0fb28562018-12-13 15:17:37 +0100126 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200127 LY_ARRAY_COUNT_TYPE u;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100128
Radek Krejci0fb28562018-12-13 15:17:37 +0100129 typedefs = lysp_node_typedefs(node);
130 LY_ARRAY_FOR(typedefs, u) {
131 if (!strcmp(name, typedefs[u].name)) {
132 /* match */
133 return &typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100134 }
135 }
136
137 return NULL;
138}
139
Radek Krejci4f28eda2018-11-12 11:46:16 +0100140static LY_DATA_TYPE
141lysp_type_str2builtin(const char *name, size_t len)
142{
143 if (len >= 4) { /* otherwise it does not match any built-in type */
144 if (name[0] == 'b') {
145 if (name[1] == 'i') {
Michal Vasko69730152020-10-09 16:30:07 +0200146 if ((len == 6) && !strncmp(&name[2], "nary", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100147 return LY_TYPE_BINARY;
Michal Vasko69730152020-10-09 16:30:07 +0200148 } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100149 return LY_TYPE_BITS;
150 }
Michal Vasko69730152020-10-09 16:30:07 +0200151 } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100152 return LY_TYPE_BOOL;
153 }
154 } else if (name[0] == 'd') {
Michal Vasko69730152020-10-09 16:30:07 +0200155 if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100156 return LY_TYPE_DEC64;
157 }
158 } else if (name[0] == 'e') {
Michal Vasko69730152020-10-09 16:30:07 +0200159 if ((len == 5) && !strncmp(&name[1], "mpty", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100160 return LY_TYPE_EMPTY;
Michal Vasko69730152020-10-09 16:30:07 +0200161 } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100162 return LY_TYPE_ENUM;
163 }
164 } else if (name[0] == 'i') {
165 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200166 if ((len == 4) && !strncmp(&name[2], "t8", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100167 return LY_TYPE_INT8;
168 } else if (len == 5) {
169 if (!strncmp(&name[2], "t16", 3)) {
170 return LY_TYPE_INT16;
171 } else if (!strncmp(&name[2], "t32", 3)) {
172 return LY_TYPE_INT32;
173 } else if (!strncmp(&name[2], "t64", 3)) {
174 return LY_TYPE_INT64;
175 }
Michal Vasko69730152020-10-09 16:30:07 +0200176 } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100177 return LY_TYPE_INST;
178 }
Michal Vasko69730152020-10-09 16:30:07 +0200179 } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100180 return LY_TYPE_IDENT;
181 }
182 } else if (name[0] == 'l') {
Michal Vasko69730152020-10-09 16:30:07 +0200183 if ((len == 7) && !strncmp(&name[1], "eafref", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100184 return LY_TYPE_LEAFREF;
185 }
186 } else if (name[0] == 's') {
Michal Vasko69730152020-10-09 16:30:07 +0200187 if ((len == 6) && !strncmp(&name[1], "tring", 5)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100188 return LY_TYPE_STRING;
189 }
190 } else if (name[0] == 'u') {
191 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200192 if ((len == 5) && !strncmp(&name[2], "ion", 3)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100193 return LY_TYPE_UNION;
194 }
Michal Vasko69730152020-10-09 16:30:07 +0200195 } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) {
196 if ((len == 5) && (name[4] == '8')) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100197 return LY_TYPE_UINT8;
198 } else if (len == 6) {
199 if (!strncmp(&name[4], "16", 2)) {
200 return LY_TYPE_UINT16;
201 } else if (!strncmp(&name[4], "32", 2)) {
202 return LY_TYPE_UINT32;
203 } else if (!strncmp(&name[4], "64", 2)) {
204 return LY_TYPE_UINT64;
205 }
206 }
207 }
208 }
209 }
210
211 return LY_TYPE_UNKNOWN;
212}
213
Radek Krejcibbe09a92018-11-08 09:36:54 +0100214LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +0100215lysp_type_find(const char *id, struct lysp_node *start_node, const struct lysp_module *start_module,
216 LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100217{
218 const char *str, *name;
219 struct lysp_tpdf *typedefs;
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100220 const struct lys_module *mod;
Michal Vaskoa99b3572021-02-01 11:54:58 +0100221 const struct lysp_module *local_module;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200222 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100223
224 assert(id);
225 assert(start_module);
226 assert(tpdf);
227 assert(node);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100228
Radek Krejci4f28eda2018-11-12 11:46:16 +0100229 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100230 str = strchr(id, ':');
231 if (str) {
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100232 mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_PREF_SCHEMA, (void *)start_module);
Michal Vaskoa99b3572021-02-01 11:54:58 +0100233 local_module = mod ? mod->parsed : NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100234 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100235 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100236 } else {
Michal Vaskoa99b3572021-02-01 11:54:58 +0100237 local_module = start_module;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100238 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100239
240 /* check for built-in types */
241 *type = lysp_type_str2builtin(name, strlen(name));
242 if (*type) {
243 *tpdf = NULL;
244 return LY_SUCCESS;
245 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100246 }
Michal Vaskoa99b3572021-02-01 11:54:58 +0100247 LY_CHECK_RET(!local_module, LY_ENOTFOUND);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100248
Michal Vaskoa99b3572021-02-01 11:54:58 +0100249 if (start_node && (local_module == start_module)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100250 /* search typedefs in parent's nodes */
251 *node = start_node;
252 while (*node) {
253 *tpdf = lysp_type_match(name, *node);
254 if (*tpdf) {
255 /* match */
256 return LY_SUCCESS;
257 }
258 *node = (*node)->parent;
259 }
260 }
261
262 /* search in top-level typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100263 if (local_module->typedefs) {
264 LY_ARRAY_FOR(local_module->typedefs, u) {
265 if (!strcmp(name, local_module->typedefs[u].name)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100266 /* match */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100267 *tpdf = &local_module->typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100268 return LY_SUCCESS;
269 }
270 }
271 }
272
273 /* search in submodules' typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100274 LY_ARRAY_FOR(local_module->includes, u) {
275 typedefs = local_module->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100276 LY_ARRAY_FOR(typedefs, v) {
277 if (!strcmp(name, typedefs[v].name)) {
278 /* match */
279 *tpdf = &typedefs[v];
280 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100281 }
282 }
283 }
284
285 return LY_ENOTFOUND;
286}
287
David Sedlák6544c182019-07-12 13:17:33 +0200288LY_ERR
David Sedlák07869a52019-07-12 14:28:19 +0200289lysp_check_enum_name(struct lys_parser_ctx *ctx, const char *name, size_t name_len)
David Sedlák6544c182019-07-12 13:17:33 +0200290{
291 if (!name_len) {
292 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
293 return LY_EVALID;
294 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
295 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
Radek Krejci422afb12021-03-04 16:38:16 +0100296 (int)name_len, name);
David Sedlák6544c182019-07-12 13:17:33 +0200297 return LY_EVALID;
298 } else {
299 for (size_t u = 0; u < name_len; ++u) {
300 if (iscntrl(name[u])) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100301 LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
Radek Krejci422afb12021-03-04 16:38:16 +0100302 (int)name_len, name, u + 1);
David Sedlák6544c182019-07-12 13:17:33 +0200303 break;
304 }
305 }
306 }
307
308 return LY_SUCCESS;
309}
310
Michal Vaskob36053d2020-03-26 15:49:30 +0100311/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100312 * @brief Check name of a new type to avoid name collisions.
313 *
314 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
315 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
316 * @param[in] tpdf Typedef definition to check.
317 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
318 * typedefs are checked, caller is supposed to free the table.
319 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
320 * typedefs are checked, caller is supposed to free the table.
321 * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise.
322 */
323static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100324lysp_check_dup_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
Radek Krejci0f969882020-08-21 16:56:47 +0200325 struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100326{
327 struct lysp_node *parent;
328 uint32_t hash;
329 size_t name_len;
330 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200331 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100332 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100333
334 assert(ctx);
335 assert(tpdf);
336
337 name = tpdf->name;
338 name_len = strlen(name);
339
Radek Krejci4f28eda2018-11-12 11:46:16 +0100340 if (lysp_type_str2builtin(name, name_len)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100341 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 +0100342 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100343 }
344
345 /* check locally scoped typedefs (avoid name shadowing) */
346 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100347 typedefs = lysp_node_typedefs(node);
348 LY_ARRAY_FOR(typedefs, u) {
349 if (&typedefs[u] == tpdf) {
350 break;
351 }
352 if (!strcmp(name, typedefs[u].name)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100353 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with sibling type.", name);
Radek Krejci0fb28562018-12-13 15:17:37 +0100354 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100355 }
356 }
357 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200358 for (parent = node->parent; parent; parent = parent->parent) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100359 if (lysp_type_match(name, parent)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100360 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 +0100361 return LY_EEXIST;
362 }
363 }
364 }
365
366 /* check collision with the top-level typedefs */
367 hash = dict_hash(name, name_len);
368 if (node) {
369 lyht_insert(tpdfs_scoped, &name, hash, NULL);
370 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100371 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 +0100372 return LY_EEXIST;
373 }
374 } else {
375 if (lyht_insert(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100376 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 +0100377 return LY_EEXIST;
378 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100379 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
380 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
381 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100382 }
383
384 return LY_SUCCESS;
385}
386
Radek Krejci857189e2020-09-01 13:26:36 +0200387/**
388 * @brief Compare identifiers.
Michal Vasko62524a92021-02-26 10:08:50 +0100389 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200390 */
391static ly_bool
392lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Radek Krejcibbe09a92018-11-08 09:36:54 +0100393{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200394 return strcmp(val1, val2) == 0 ? 1 : 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100395}
396
397LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100398lysp_check_dup_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100399{
400 struct hash_table *ids_global;
401 struct hash_table *ids_scoped;
Radek Krejci0fb28562018-12-13 15:17:37 +0100402 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200403 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200404 uint32_t i;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100405 LY_ERR ret = LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100406
407 /* check name collisions - typedefs and groupings */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100408 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
409 ids_scoped = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200410 LY_ARRAY_FOR(mod->typedefs, v) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100411 ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global, ids_scoped);
412 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100413 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200414 LY_ARRAY_FOR(mod->includes, v) {
415 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100416 ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global, ids_scoped);
417 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci3b1f9292018-11-08 10:58:35 +0100418 }
419 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200420 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
421 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
422 LY_ARRAY_FOR(typedefs, u) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100423 ret = lysp_check_dup_typedef(ctx, (struct lysp_node *)ctx->tpdfs_nodes.objs[i], &typedefs[u], ids_global, ids_scoped);
424 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100425 }
426 }
Michal Vasko405cc9e2020-12-01 12:01:27 +0100427
Radek Krejcibbe09a92018-11-08 09:36:54 +0100428cleanup:
429 lyht_free(ids_global);
430 lyht_free(ids_scoped);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100431 return ret;
432}
433
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100434static ly_bool
435ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
436{
437 void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
438
439 return ptr1 == ptr2 ? 1 : 0;
440}
441
442LY_ERR
443lysp_check_dup_features(struct lys_parser_ctx *ctx, struct lysp_module *mod)
444{
445 LY_ARRAY_COUNT_TYPE u;
446 struct hash_table *ht;
447 struct lysp_feature *f;
448 uint32_t hash;
449 LY_ERR ret = LY_SUCCESS, r;
450
451 ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
452 LY_CHECK_RET(!ht, LY_EMEM);
453
454 /* add all module features into a hash table */
455 LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
456 hash = dict_hash(f->name, strlen(f->name));
457 r = lyht_insert(ht, &f->name, hash, NULL);
458 if (r == LY_EEXIST) {
459 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
460 ret = LY_EVALID;
461 goto cleanup;
462 } else if (r) {
463 ret = r;
464 goto cleanup;
465 }
466 }
467
468 /* add all submodule features into a hash table */
469 LY_ARRAY_FOR(mod->includes, u) {
470 LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
471 hash = dict_hash(f->name, strlen(f->name));
472 r = lyht_insert(ht, &f->name, hash, NULL);
473 if (r == LY_EEXIST) {
474 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
475 ret = LY_EVALID;
476 goto cleanup;
477 } else if (r) {
478 ret = r;
479 goto cleanup;
480 }
481 }
482 }
483
484cleanup:
485 lyht_free(ht);
486 return ret;
487}
488
489LY_ERR
490lysp_check_dup_identities(struct lys_parser_ctx *ctx, struct lysp_module *mod)
491{
492 LY_ARRAY_COUNT_TYPE u;
493 struct hash_table *ht;
494 struct lysp_ident *i;
495 uint32_t hash;
496 LY_ERR ret = LY_SUCCESS, r;
497
498 ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
499 LY_CHECK_RET(!ht, LY_EMEM);
500
501 /* add all module identities into a hash table */
502 LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
503 hash = dict_hash(i->name, strlen(i->name));
504 r = lyht_insert(ht, &i->name, hash, NULL);
505 if (r == LY_EEXIST) {
506 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
507 ret = LY_EVALID;
508 goto cleanup;
509 } else if (r) {
510 ret = r;
511 goto cleanup;
512 }
513 }
514
515 /* add all submodule identities into a hash table */
516 LY_ARRAY_FOR(mod->includes, u) {
517 LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
518 hash = dict_hash(i->name, strlen(i->name));
519 r = lyht_insert(ht, &i->name, hash, NULL);
520 if (r == LY_EEXIST) {
521 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
522 ret = LY_EVALID;
523 goto cleanup;
524 } else if (r) {
525 ret = r;
526 goto cleanup;
527 }
528 }
529 }
530
531cleanup:
532 lyht_free(ht);
533 return ret;
534}
535
Radek Krejci9ed7a192018-10-31 16:23:51 +0100536struct lysp_load_module_check_data {
537 const char *name;
538 const char *revision;
539 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200540 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100541};
542
543static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100544lysp_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 +0100545{
546 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100547 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200548 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100549 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100550 struct lysp_revision *revs;
551
552 name = mod ? mod->mod->name : submod->name;
553 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200554 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100555
556 if (info->name) {
557 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100558 if (strcmp(info->name, name)) {
559 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100560 return LY_EINVAL;
561 }
562 }
563 if (info->revision) {
564 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100565 if (!revs || strcmp(info->revision, revs[0].date)) {
566 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200567 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100568 return LY_EINVAL;
569 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200570 } else if (!latest_revision) {
571 /* do not log, we just need to drop the schema and use the latest revision from the context */
572 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100573 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100574 if (submod) {
575 assert(info->submoduleof);
576
Radek Krejci9ed7a192018-10-31 16:23:51 +0100577 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200578 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100579 LOGVAL(ctx, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200580 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100581 return LY_EVALID;
582 }
583 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100584 if (submod->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100585 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100586 return LY_EVALID;
587 }
588 }
589 if (info->path) {
590 /* check that name and revision match filename */
591 filename = strrchr(info->path, '/');
592 if (!filename) {
593 filename = info->path;
594 } else {
595 filename++;
596 }
597 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100598 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100599 rev = strchr(filename, '@');
600 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100601 if (strncmp(filename, name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +0200602 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100603 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100604 }
605 /* revision */
606 if (rev) {
607 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100608 if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100609 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +0200610 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100611 }
612 }
613 }
614 return LY_SUCCESS;
615}
616
617LY_ERR
Michal Vasko34e334d2021-01-25 16:12:31 +0100618lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, const char **features,
619 ly_bool need_implemented, struct lys_parser_ctx *main_ctx, const char *main_name, ly_bool required,
620 struct lys_glob_unres *unres, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100621{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200622 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100623 char *filepath = NULL;
624 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100625 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100626 LY_ERR ret = LY_SUCCESS;
627 struct lysp_load_module_check_data check_data = {0};
628
629 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 +0200630 &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200631 if (!filepath) {
632 if (required) {
633 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200634 revision ? revision : "");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200635 }
636 return LY_ENOTFOUND;
637 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100638
639 LOGVRB("Loading schema from \"%s\" file.", filepath);
640
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200641 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200642 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +0200643 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100644 check_data.name = name;
645 check_data.revision = revision;
646 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800647 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200648 if (main_ctx) {
Michal Vasko7a0b0762020-09-02 16:37:01 +0200649 ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data,
Michal Vasko69730152020-10-09 16:30:07 +0200650 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200651 } else {
Michal Vasko34e334d2021-01-25 16:12:31 +0100652 ret = lys_create_module(ctx, in, format, need_implemented, lysp_load_module_check, &check_data, features, unres,
Michal Vasko69730152020-10-09 16:30:07 +0200653 (struct lys_module **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200654
655 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200656 ly_in_free(in, 1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200657 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100658
659 *result = mod;
660
661 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200662
Radek Krejci9ed7a192018-10-31 16:23:51 +0100663cleanup:
664 free(filepath);
665 return ret;
666}
667
Radek Krejcid33273d2018-10-25 14:55:52 +0200668LY_ERR
Michal Vasko34e334d2021-01-25 16:12:31 +0100669lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, ly_bool need_implemented,
670 const char **features, struct lys_glob_unres *unres, struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200671{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100672 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200673 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200674
Radek Krejci9ed7a192018-10-31 16:23:51 +0100675 void (*module_data_free)(void *module_data, void *user_data) = NULL;
676 struct lysp_load_module_check_data check_data = {0};
Michal Vasko0550b762020-11-24 18:04:08 +0100677 struct lys_module *ctx_latest = NULL, *m;
Michal Vasko63f3d842020-07-08 10:10:14 +0200678 struct ly_in *in;
Michal Vasko0550b762020-11-24 18:04:08 +0100679 LY_ERR ret;
Michal Vasko34e334d2021-01-25 16:12:31 +0100680 ly_bool implement;
Radek Krejci086c7132018-10-26 15:29:04 +0200681
Michal Vasko405cc9e2020-12-01 12:01:27 +0100682 assert(mod && unres);
Radek Krejci0af46292019-01-11 16:02:31 +0100683
Michal Vasko25d6ad02020-10-22 12:20:22 +0200684 if (ctx->flags & LY_CTX_ALL_IMPLEMENTED) {
Radek Krejcia53d7c92020-08-21 11:30:56 +0200685 implement = 1;
Michal Vasko34e334d2021-01-25 16:12:31 +0100686 } else {
687 implement = need_implemented;
Radek Krejcia53d7c92020-08-21 11:30:56 +0200688 }
689
Michal Vasko0550b762020-11-24 18:04:08 +0100690 /*
691 * try to get the module from the context
692 */
Radek Krejci0af46292019-01-11 16:02:31 +0100693 if (!*mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100694 if (revision) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200695 /* get the specific revision */
Michal Vasko22df3f02020-08-24 13:29:22 +0200696 *mod = (struct lys_module *)ly_ctx_get_module(ctx, name, revision);
Radek Krejci0af46292019-01-11 16:02:31 +0100697 } else {
Michal Vasko0550b762020-11-24 18:04:08 +0100698 if (implement) {
699 /* prefer the implemented module instead of the latest one */
700 *mod = (struct lys_module *)ly_ctx_get_module_implemented(ctx, name);
701 }
702 if (!*mod) {
703 /* get the requested module of the latest revision in the context */
704 *mod = (struct lys_module *)ly_ctx_get_module_latest(ctx, name);
705 if (*mod && ((*mod)->latest_revision == 1)) {
706 /* let us now search with callback and searchpaths to check if there is newer revision outside the context */
707 ctx_latest = *mod;
708 *mod = NULL;
709 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200710 }
Radek Krejci0af46292019-01-11 16:02:31 +0100711 }
Radek Krejci086c7132018-10-26 15:29:04 +0200712 }
713
Michal Vasko0550b762020-11-24 18:04:08 +0100714 /* check collision with other implemented revision */
715 if (implement) {
716 m = ly_ctx_get_module_implemented(ctx, name);
717 if (m && (!*mod || (*mod && (m != *mod)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100718 LOGVAL(ctx, LYVE_REFERENCE, "Module \"%s\" is already present in other implemented revision.", name);
Michal Vasko0550b762020-11-24 18:04:08 +0100719 *mod = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200720 return LY_EDENIED;
721 }
Michal Vasko0550b762020-11-24 18:04:08 +0100722 }
Radek Krejci086c7132018-10-26 15:29:04 +0200723
Michal Vasko0550b762020-11-24 18:04:08 +0100724 /*
725 * no suitable module in the context, try to load it
726 */
727 if (!*mod) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100728 /* module not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200729 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
730search_clb:
731 if (ctx->imp_clb) {
732 if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
Michal Vasko69730152020-10-09 16:30:07 +0200733 &format, &module_data, &module_data_free) == LY_SUCCESS) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200734 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
Radek Krejci9ed7a192018-10-31 16:23:51 +0100735 check_data.name = name;
736 check_data.revision = revision;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100737 lys_create_module(ctx, in, format, implement, lysp_load_module_check, &check_data, features, unres, mod);
Michal Vasko63f3d842020-07-08 10:10:14 +0200738 ly_in_free(in, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100739 if (module_data_free) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200740 module_data_free((void *)module_data, ctx->imp_clb_data);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100741 }
Radek Krejci086c7132018-10-26 15:29:04 +0200742 }
743 }
744 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
745 goto search_file;
746 }
747 } else {
748search_file:
749 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
750 /* module was not received from the callback or there is no callback set */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100751 lys_module_localfile(ctx, name, revision, features, implement, NULL, NULL, ctx_latest ? 0 : 1, unres,
752 (void **)mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200753 }
Michal Vasko0550b762020-11-24 18:04:08 +0100754 if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejci086c7132018-10-26 15:29:04 +0200755 goto search_clb;
756 }
757 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100758
Radek Krejcib3289d62019-09-18 12:21:39 +0200759 /* update the latest_revision flag - here we have selected the latest available schema,
760 * consider that even the callback provides correct latest revision */
Michal Vasko0550b762020-11-24 18:04:08 +0100761 if (!*mod && ctx_latest) {
Michal Vasko003c44a2021-02-24 17:13:11 +0100762 LOGVRB("Newer revision than \"%s@%s\" not found, using this as the latest revision.", ctx_latest->name,
Michal Vasko0550b762020-11-24 18:04:08 +0100763 ctx_latest->revision);
764 ctx_latest->latest_revision = 2;
765 *mod = ctx_latest;
766 } else if (*mod && !revision && ((*mod)->latest_revision == 1)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100767 (*mod)->latest_revision = 2;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100768 }
Radek Krejci086c7132018-10-26 15:29:04 +0200769
Michal Vasko0550b762020-11-24 18:04:08 +0100770 if (!*mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100771 LOGVAL(ctx, LYVE_REFERENCE, "%s \"%s\" module failed.", implement ? "Loading" : "Importing", name);
Michal Vasko0550b762020-11-24 18:04:08 +0100772 return LY_EVALID;
773 }
774 } else {
775 /* we have module from the current context, circular check */
776 if ((*mod)->parsed->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100777 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
Radek Krejci086c7132018-10-26 15:29:04 +0200778 *mod = NULL;
779 return LY_EVALID;
780 }
781 }
Radek Krejci086c7132018-10-26 15:29:04 +0200782
Michal Vasko0550b762020-11-24 18:04:08 +0100783 /*
784 * module found, make sure it is implemented if should be
785 */
Michal Vasko962b6cd2020-12-08 10:07:49 +0100786 if (implement) {
Radek Krejci2415f882021-01-20 16:27:09 +0100787 if (!(*mod)->implemented) {
788 /* implement */
789 ret = lys_set_implemented_r(*mod, features, unres);
790 if (ret) {
791 *mod = NULL;
792 return ret;
793 }
794 } else if (features) {
Michal Vasko962b6cd2020-12-08 10:07:49 +0100795 /* set features if different */
796 ret = lys_set_features((*mod)->parsed, features);
797 if (!ret) {
798 /* context need to be recompiled so that feature changes are properly applied */
799 unres->recompile = 1;
800 } else if (ret != LY_EEXIST) {
801 /* error */
802 return ret;
803 } /* else no feature changes */
Michal Vaskoc5d64862020-11-24 18:04:45 +0100804 }
Radek Krejci086c7132018-10-26 15:29:04 +0200805 }
Radek Krejci086c7132018-10-26 15:29:04 +0200806
807 return LY_SUCCESS;
808}
809
810LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200811lysp_check_stringchar(struct lys_parser_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +0200812{
813 if (!is_yangutf8char(c)) {
814 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
815 return LY_EVALID;
816 }
817 return LY_SUCCESS;
818}
819
820LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200821lysp_check_identifierchar(struct lys_parser_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +0200822{
Michal Vasko69730152020-10-09 16:30:07 +0200823 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +0200824 if (!is_yangidentstartchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200825 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200826 return LY_EVALID;
827 }
828 if (prefix) {
829 if (first) {
830 (*prefix) = 0;
831 } else {
832 (*prefix) = 2;
833 }
834 }
Michal Vasko69730152020-10-09 16:30:07 +0200835 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +0200836 (*prefix) = 1;
837 } else if (!is_yangidentchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200838 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200839 return LY_EVALID;
840 }
841
842 return LY_SUCCESS;
843}
844
Radek Krejci771928a2021-01-19 13:42:36 +0100845/**
846 * @brief Try to find the parsed submodule in main module for the given include record.
847 *
848 * @param[in] pctx main parser context
849 * @param[in] inc The include record with missing parsed submodule. According to include info try to find
850 * the corresponding parsed submodule in main module's includes.
851 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
852 * @return LY_ENOT - the parsed module was not found.
853 * @return LY_EVALID - YANG rule violation
854 */
855static LY_ERR
856lysp_get_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200857{
Radek Krejci771928a2021-01-19 13:42:36 +0100858 LY_ARRAY_COUNT_TYPE i;
859 struct lysp_module *main_pmod = pctx->parsed_mod->mod->parsed;
Michal Vasko69730152020-10-09 16:30:07 +0200860
Radek Krejci771928a2021-01-19 13:42:36 +0100861 LY_ARRAY_FOR(main_pmod->includes, i) {
862 if (strcmp(main_pmod->includes[i].name, inc->name)) {
863 continue;
864 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200865
Radek Krejci771928a2021-01-19 13:42:36 +0100866 if (inc->rev[0] && strncmp(inc->rev, main_pmod->includes[i].rev, LY_REV_SIZE)) {
867 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
868 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
869 ((struct lysp_submodule *)pctx->parsed_mod)->name, inc->rev,
870 main_pmod->includes[i].name, main_pmod->includes[i].rev, main_pmod->mod->name);
871 return LY_EVALID;
872 }
873
874 inc->submodule = main_pmod->includes[i].submodule;
875 return inc->submodule ? LY_SUCCESS : LY_ENOT;
876 }
877
878 if (main_pmod->version == LYS_VERSION_1_1) {
879 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
880 "YANG 1.1 requires all submodules to be included from main module. "
881 "But submodule \"%s\" includes submodule \"%s\" which is not included by main module \"%s\".",
882 ((struct lysp_submodule *)pctx->parsed_mod)->name, inc->name, main_pmod->mod->name);
883 return LY_EVALID;
884 } else {
885 return LY_ENOT;
886 }
887}
888
889/**
890 * @brief Make the copy of the given include record into the main module.
891 *
892 * YANG 1.0 does not require the main module to include all the submodules. Therefore, parsing submodules can cause
893 * reallocating and extending the includes array in the main module by the submodules included only in submodules.
894 *
895 * @param[in] pctx main parser context
896 * @param[in] inc Include record to copy into main module taken from @p pctx.
897 * @return LY_ERR value.
898 */
899static LY_ERR
900lysp_inject_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
901{
902 LY_ARRAY_COUNT_TYPE i;
903 struct lysp_include *inc_new, *inc_tofill = NULL;
904 struct lysp_module *main_pmod = pctx->parsed_mod->mod->parsed;
905
906 /* first, try to find the corresponding record with missing parsed submodule */
907 LY_ARRAY_FOR(main_pmod->includes, i) {
908 if (strcmp(main_pmod->includes[i].name, inc->name)) {
909 continue;
910 }
911 inc_tofill = &main_pmod->includes[i];
912 break;
913 }
914
915 if (inc_tofill) {
916 inc_tofill->submodule = inc->submodule;
917 } else {
918 LY_ARRAY_NEW_RET(PARSER_CTX(pctx), main_pmod->includes, inc_new, LY_EMEM);
919
920 inc_new->submodule = inc->submodule;
921 DUP_STRING_RET(PARSER_CTX(pctx), inc->name, inc_new->name);
922 DUP_STRING_RET(PARSER_CTX(pctx), inc->dsc, inc_new->dsc);
923 DUP_STRING_RET(PARSER_CTX(pctx), inc->ref, inc_new->ref);
924 /* TODO duplicate extensions */
925 memcpy(inc_new->rev, inc->rev, LY_REV_SIZE);
926 inc_new->injected = 1;
927 }
928 return LY_SUCCESS;
929}
930
931LY_ERR
932lysp_load_submodules(struct lys_parser_ctx *pctx, struct lysp_module *pmod)
933{
934 LY_ARRAY_COUNT_TYPE u;
935 struct ly_ctx *ctx = PARSER_CTX(pctx);
936
937 LY_ARRAY_FOR(pmod->includes, u) {
938 LY_ERR ret = LY_SUCCESS;
939 struct lysp_submodule *submod = NULL;
940 struct lysp_include *inc = &pmod->includes[u];
941
942 if (inc->submodule) {
943 continue;
944 }
945
946 if (pmod->is_submod) {
947 /* try to find the submodule in the main module or its submodules */
948 ret = lysp_get_submodule(pctx, inc);
949 LY_CHECK_RET(ret && ret != LY_ENOT, ret);
950 LY_CHECK_RET(ret == LY_SUCCESS, LY_SUCCESS); /* submodule found in linked with the inc */
951 }
952
953 /* submodule not present in the main module, get the input data and parse it */
954 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcidf549132021-01-21 10:32:32 +0100955search_clb:
Radek Krejci771928a2021-01-19 13:42:36 +0100956 if (ctx->imp_clb) {
957 const char *submodule_data = NULL;
958 LYS_INFORMAT format = LYS_IN_UNKNOWN;
959 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
960 struct lysp_load_module_check_data check_data = {0};
961 struct ly_in *in;
962
963 if (ctx->imp_clb(pctx->parsed_mod->mod->name, NULL, inc->name,
964 inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
965 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
966 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
967 check_data.name = inc->name;
968 check_data.revision = inc->rev[0] ? inc->rev : NULL;
969 check_data.submoduleof = pctx->parsed_mod->mod->name;
970 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, &submod);
971
972 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
973 * submodule's include into main module, where it is missing */
974 inc = &pmod->includes[u];
975
976 ly_in_free(in, 0);
977 if (submodule_data_free) {
978 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
979 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200980 }
981 }
Radek Krejci771928a2021-01-19 13:42:36 +0100982 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
983 goto search_file;
984 }
985 } else {
Radek Krejcidf549132021-01-21 10:32:32 +0100986search_file:
Radek Krejci771928a2021-01-19 13:42:36 +0100987 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
988 /* submodule was not received from the callback or there is no callback set */
989 lys_module_localfile(ctx, inc->name,
990 inc->rev[0] ? inc->rev : NULL, NULL, 0, pctx,
991 pctx->parsed_mod->mod->name, 1, NULL, (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100992
Radek Krejci771928a2021-01-19 13:42:36 +0100993 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
994 * submodule's include into main module, where it is missing */
995 inc = &pmod->includes[u];
996 }
997 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
998 goto search_clb;
999 }
1000 }
1001 if (submod) {
1002 if (!inc->rev[0] && (submod->latest_revision == 1)) {
1003 /* update the latest_revision flag - here we have selected the latest available schema,
1004 * consider that even the callback provides correct latest revision */
1005 submod->latest_revision = 2;
1006 }
1007
1008 inc->submodule = submod;
1009 if (ret == LY_ENOT) {
1010 /* the submodule include is not present in YANG 1.0 main module - add it there */
1011 LY_CHECK_RET(lysp_inject_submodule(pctx, &pmod->includes[u]));
1012 }
1013 }
1014 if (!inc->submodule) {
1015 LOGVAL(ctx, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name,
1016 pctx->parsed_mod->is_submod ? ((struct lysp_submodule *)pctx->parsed_mod)->name : pctx->parsed_mod->mod->name);
1017 return LY_EVALID;
1018 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001019 }
1020
1021 return LY_SUCCESS;
1022}
1023
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001024API const struct lysc_when *
1025lysc_has_when(const struct lysc_node *node)
1026{
Radek Krejci9a3823e2021-01-27 20:26:46 +01001027 struct lysc_when **when;
1028
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001029 if (!node) {
1030 return NULL;
1031 }
1032
1033 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001034 when = lysc_node_when(node);
1035 if (when) {
1036 return when[0];
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001037 }
1038 node = node->parent;
1039 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1040
1041 return NULL;
1042}
1043
Radek Krejci0935f412019-08-20 16:15:18 +02001044API const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001045lys_nodetype2str(uint16_t nodetype)
1046{
Michal Vaskod989ba02020-08-24 10:59:24 +02001047 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001048 case LYS_CONTAINER:
1049 return "container";
1050 case LYS_CHOICE:
1051 return "choice";
1052 case LYS_LEAF:
1053 return "leaf";
1054 case LYS_LEAFLIST:
1055 return "leaf-list";
1056 case LYS_LIST:
1057 return "list";
1058 case LYS_ANYXML:
1059 return "anyxml";
1060 case LYS_ANYDATA:
1061 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001062 case LYS_CASE:
1063 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001064 case LYS_RPC:
1065 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001066 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001067 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001068 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001069 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001070 case LYS_USES:
1071 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001072 default:
1073 return "unknown";
1074 }
1075}
1076
Radek Krejci39b7fc22021-02-26 23:29:18 +01001077API enum ly_stmt
1078lys_nodetype2stmt(uint16_t nodetype)
1079{
1080 switch (nodetype) {
1081 case LYS_CONTAINER:
1082 return LY_STMT_CONTAINER;
1083 case LYS_CHOICE:
1084 return LY_STMT_CHOICE;
1085 case LYS_LEAF:
1086 return LY_STMT_LEAF;
1087 case LYS_LEAFLIST:
1088 return LY_STMT_LEAF_LIST;
1089 case LYS_LIST:
1090 return LY_STMT_LIST;
1091 case LYS_ANYXML:
1092 return LY_STMT_ANYXML;
1093 case LYS_ANYDATA:
1094 return LY_STMT_ANYDATA;
1095 case LYS_CASE:
1096 return LY_STMT_CASE;
1097 case LYS_RPC:
1098 return LY_STMT_RPC;
1099 case LYS_ACTION:
1100 return LY_STMT_ACTION;
1101 case LYS_NOTIF:
1102 return LY_STMT_NOTIFICATION;
1103 case LYS_USES:
1104 return LY_STMT_USES;
1105 case LYS_INPUT:
1106 return LY_STMT_INPUT;
1107 case LYS_OUTPUT:
1108 return LY_STMT_OUTPUT;
1109 default:
1110 return LY_STMT_NONE;
1111 }
1112}
1113
Radek Krejci693262f2019-04-29 15:23:20 +02001114const char *
1115lys_datatype2str(LY_DATA_TYPE basetype)
1116{
Michal Vaskod989ba02020-08-24 10:59:24 +02001117 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001118 case LY_TYPE_BINARY:
1119 return "binary";
1120 case LY_TYPE_UINT8:
1121 return "uint8";
1122 case LY_TYPE_UINT16:
1123 return "uint16";
1124 case LY_TYPE_UINT32:
1125 return "uint32";
1126 case LY_TYPE_UINT64:
1127 return "uint64";
1128 case LY_TYPE_STRING:
1129 return "string";
1130 case LY_TYPE_BITS:
1131 return "bits";
1132 case LY_TYPE_BOOL:
1133 return "boolean";
1134 case LY_TYPE_DEC64:
1135 return "decimal64";
1136 case LY_TYPE_EMPTY:
1137 return "empty";
1138 case LY_TYPE_ENUM:
1139 return "enumeration";
1140 case LY_TYPE_IDENT:
1141 return "identityref";
1142 case LY_TYPE_INST:
1143 return "instance-identifier";
1144 case LY_TYPE_LEAFREF:
1145 return "leafref";
1146 case LY_TYPE_UNION:
1147 return "union";
1148 case LY_TYPE_INT8:
1149 return "int8";
1150 case LY_TYPE_INT16:
1151 return "int16";
1152 case LY_TYPE_INT32:
1153 return "int32";
1154 case LY_TYPE_INT64:
1155 return "int64";
1156 default:
1157 return "unknown";
1158 }
1159}
1160
Radek Krejci056d0a82018-12-06 16:57:25 +01001161API const struct lysp_tpdf *
1162lysp_node_typedefs(const struct lysp_node *node)
1163{
Radek Krejci0fb28562018-12-13 15:17:37 +01001164 switch (node->nodetype) {
1165 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001166 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001167 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001168 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001169 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001170 return ((struct lysp_node_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001171 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001172 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001173 return ((struct lysp_node_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001174 case LYS_INPUT:
1175 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001176 return ((struct lysp_node_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001177 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001178 return ((struct lysp_node_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001179 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001180 return NULL;
1181 }
1182}
1183
Radek Krejci2a9fc652021-01-22 17:44:34 +01001184API const struct lysp_node_grp *
Radek Krejci53ea6152018-12-13 15:21:15 +01001185lysp_node_groupings(const struct lysp_node *node)
1186{
1187 switch (node->nodetype) {
1188 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001189 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001190 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001191 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001192 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001193 return ((struct lysp_node_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001194 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001195 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001196 return ((struct lysp_node_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001197 case LYS_INPUT:
1198 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001199 return ((struct lysp_node_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001200 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001201 return ((struct lysp_node_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001202 default:
1203 return NULL;
1204 }
1205}
1206
Radek Krejci2a9fc652021-01-22 17:44:34 +01001207struct lysp_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001208lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001209{
1210 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001211
Radek Krejcibbe09a92018-11-08 09:36:54 +01001212 switch (node->nodetype) {
1213 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001214 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001215 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001216 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001217 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001218 return &((struct lysp_node_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001219 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001220 return &((struct lysp_node_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001221 default:
1222 return NULL;
1223 }
1224}
1225
Radek Krejci2a9fc652021-01-22 17:44:34 +01001226API const struct lysp_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001227lysp_node_actions(const struct lysp_node *node)
1228{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001229 struct lysp_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001230
Michal Vasko22df3f02020-08-24 13:29:22 +02001231 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001232 if (actions) {
1233 return *actions;
1234 } else {
1235 return NULL;
1236 }
1237}
1238
Radek Krejci2a9fc652021-01-22 17:44:34 +01001239struct lysp_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001240lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001241{
1242 assert(node);
1243 switch (node->nodetype) {
1244 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001245 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001246 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001247 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001248 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001249 return &((struct lysp_node_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001250 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001251 return &((struct lysp_node_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001252 default:
1253 return NULL;
1254 }
1255}
1256
Radek Krejci2a9fc652021-01-22 17:44:34 +01001257API const struct lysp_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001258lysp_node_notifs(const struct lysp_node *node)
1259{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001260 struct lysp_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001261
Michal Vasko22df3f02020-08-24 13:29:22 +02001262 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001263 if (notifs) {
1264 return *notifs;
1265 } else {
1266 return NULL;
1267 }
1268}
1269
Radek Krejcibbe09a92018-11-08 09:36:54 +01001270struct lysp_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001271lysp_node_child_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001272{
1273 assert(node);
1274 switch (node->nodetype) {
1275 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001276 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001277 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001278 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001279 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001280 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001281 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001282 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001283 case LYS_GROUPING:
Radek Krejci01180ac2021-01-27 08:48:22 +01001284 return &((struct lysp_node_grp *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001285 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001286 return &((struct lysp_node_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001287 case LYS_INPUT:
1288 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001289 return &((struct lysp_node_action_inout *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001290 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001291 return &((struct lysp_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001292 default:
1293 return NULL;
1294 }
1295}
1296
Radek Krejci056d0a82018-12-06 16:57:25 +01001297API const struct lysp_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001298lysp_node_child(const struct lysp_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01001299{
Michal Vasko544e58a2021-01-28 14:33:41 +01001300 struct lysp_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001301
1302 if (!node) {
1303 return NULL;
1304 }
1305
Michal Vasko544e58a2021-01-28 14:33:41 +01001306 child = lysp_node_child_p((struct lysp_node *)node);
1307 if (child) {
1308 return *child;
Radek Krejci056d0a82018-12-06 16:57:25 +01001309 } else {
1310 return NULL;
1311 }
1312}
1313
Radek Krejci9a3823e2021-01-27 20:26:46 +01001314struct lysp_restr **
1315lysp_node_musts_p(const struct lysp_node *node)
1316{
1317 if (!node) {
1318 return NULL;
1319 }
1320
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001321 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001322 case LYS_CONTAINER:
1323 return &((struct lysp_node_container *)node)->musts;
1324 case LYS_LEAF:
1325 return &((struct lysp_node_leaf *)node)->musts;
1326 case LYS_LEAFLIST:
1327 return &((struct lysp_node_leaflist *)node)->musts;
1328 case LYS_LIST:
1329 return &((struct lysp_node_list *)node)->musts;
1330 case LYS_ANYXML:
1331 case LYS_ANYDATA:
1332 return &((struct lysp_node_anydata *)node)->musts;
1333 case LYS_NOTIF:
1334 return &((struct lysp_node_notif *)node)->musts;
1335 case LYS_INPUT:
1336 case LYS_OUTPUT:
1337 return &((struct lysp_node_action_inout *)node)->musts;
1338 default:
1339 return NULL;
1340 }
1341}
1342
1343struct lysp_restr *
1344lysp_node_musts(const struct lysp_node *node)
1345{
1346 struct lysp_restr **musts;
1347
1348 musts = lysp_node_musts_p(node);
1349 if (musts) {
1350 return *musts;
1351 } else {
1352 return NULL;
1353 }
1354}
1355
1356struct lysp_when **
1357lysp_node_when_p(const struct lysp_node *node)
1358{
1359 if (!node) {
1360 return NULL;
1361 }
1362
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001363 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001364 case LYS_CONTAINER:
1365 return &((struct lysp_node_container *)node)->when;
1366 case LYS_CHOICE:
1367 return &((struct lysp_node_choice *)node)->when;
1368 case LYS_LEAF:
1369 return &((struct lysp_node_leaf *)node)->when;
1370 case LYS_LEAFLIST:
1371 return &((struct lysp_node_leaflist *)node)->when;
1372 case LYS_LIST:
1373 return &((struct lysp_node_list *)node)->when;
1374 case LYS_ANYXML:
1375 case LYS_ANYDATA:
1376 return &((struct lysp_node_anydata *)node)->when;
1377 case LYS_CASE:
1378 return &((struct lysp_node_case *)node)->when;
1379 case LYS_USES:
1380 return &((struct lysp_node_uses *)node)->when;
1381 case LYS_AUGMENT:
1382 return &((struct lysp_node_augment *)node)->when;
1383 default:
1384 return NULL;
1385 }
1386}
1387
1388struct lysp_when *
1389lysp_node_when(const struct lysp_node *node)
1390{
1391 struct lysp_when **when;
1392
1393 when = lysp_node_when_p(node);
1394 if (when) {
1395 return *when;
1396 } else {
1397 return NULL;
1398 }
1399}
1400
Radek Krejci2a9fc652021-01-22 17:44:34 +01001401struct lysc_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001402lysc_node_actions_p(struct lysc_node *node)
1403{
1404 assert(node);
1405 switch (node->nodetype) {
1406 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001407 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001408 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001409 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001410 default:
1411 return NULL;
1412 }
1413}
1414
Radek Krejci2a9fc652021-01-22 17:44:34 +01001415API const struct lysc_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001416lysc_node_actions(const struct lysc_node *node)
1417{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001418 struct lysc_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001419
Michal Vasko22df3f02020-08-24 13:29:22 +02001420 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001421 if (actions) {
1422 return *actions;
1423 } else {
1424 return NULL;
1425 }
1426}
1427
Radek Krejci2a9fc652021-01-22 17:44:34 +01001428struct lysc_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001429lysc_node_notifs_p(struct lysc_node *node)
1430{
1431 assert(node);
1432 switch (node->nodetype) {
1433 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001434 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001435 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001436 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001437 default:
1438 return NULL;
1439 }
1440}
1441
Radek Krejci2a9fc652021-01-22 17:44:34 +01001442API const struct lysc_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001443lysc_node_notifs(const struct lysc_node *node)
1444{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001445 struct lysc_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001446
Michal Vasko22df3f02020-08-24 13:29:22 +02001447 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001448 if (notifs) {
1449 return *notifs;
1450 } else {
1451 return NULL;
1452 }
1453}
1454
Radek Krejcibbe09a92018-11-08 09:36:54 +01001455struct lysc_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001456lysc_node_child_p(const struct lysc_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001457{
Michal Vasko544e58a2021-01-28 14:33:41 +01001458 assert(node && !(node->nodetype & (LYS_RPC | LYS_ACTION)));
1459
Radek Krejcibbe09a92018-11-08 09:36:54 +01001460 switch (node->nodetype) {
1461 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001462 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001463 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001464 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001465 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001466 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001467 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001468 return &((struct lysc_node_list *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001469 case LYS_INPUT:
1470 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001471 return &((struct lysc_node_action_inout *)node)->child;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001472 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001473 return &((struct lysc_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001474 default:
1475 return NULL;
1476 }
1477}
1478
Radek Krejci056d0a82018-12-06 16:57:25 +01001479API const struct lysc_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001480lysc_node_child(const struct lysc_node *node)
Radek Krejcia3045382018-11-22 14:30:31 +01001481{
Michal Vasko544e58a2021-01-28 14:33:41 +01001482 struct lysc_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001483
1484 if (!node) {
1485 return NULL;
1486 }
1487
Michal Vasko544e58a2021-01-28 14:33:41 +01001488 if (node->nodetype & (LYS_RPC | LYS_ACTION)) {
1489 return &((struct lysc_node_action *)node)->input.node;
Radek Krejcibe154442021-01-21 11:06:36 +01001490 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01001491 child = lysc_node_child_p(node);
1492 if (child) {
1493 return *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001494 }
Michal Vasko2a668712020-10-21 11:48:09 +02001495 }
Michal Vasko544e58a2021-01-28 14:33:41 +01001496
1497 return NULL;
Michal Vasko2a668712020-10-21 11:48:09 +02001498}
1499
Radek Krejci9a3823e2021-01-27 20:26:46 +01001500struct lysc_must **
1501lysc_node_musts_p(const struct lysc_node *node)
1502{
1503 if (!node) {
1504 return NULL;
1505 }
1506
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001507 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001508 case LYS_CONTAINER:
1509 return &((struct lysc_node_container *)node)->musts;
1510 case LYS_LEAF:
1511 return &((struct lysc_node_leaf *)node)->musts;
1512 case LYS_LEAFLIST:
1513 return &((struct lysc_node_leaflist *)node)->musts;
1514 case LYS_LIST:
1515 return &((struct lysc_node_list *)node)->musts;
1516 case LYS_ANYXML:
1517 case LYS_ANYDATA:
1518 return &((struct lysc_node_anydata *)node)->musts;
1519 case LYS_NOTIF:
1520 return &((struct lysc_node_notif *)node)->musts;
1521 case LYS_INPUT:
1522 case LYS_OUTPUT:
1523 return &((struct lysc_node_action_inout *)node)->musts;
1524 default:
1525 return NULL;
1526 }
1527}
1528
1529API struct lysc_must *
1530lysc_node_musts(const struct lysc_node *node)
1531{
1532 struct lysc_must **must_p;
1533
1534 must_p = lysc_node_musts_p(node);
1535 if (must_p) {
1536 return *must_p;
1537 } else {
1538 return NULL;
1539 }
1540}
1541
1542struct lysc_when ***
1543lysc_node_when_p(const struct lysc_node *node)
1544{
1545 if (!node) {
1546 return NULL;
1547 }
1548
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001549 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001550 case LYS_CONTAINER:
1551 return &((struct lysc_node_container *)node)->when;
1552 case LYS_CHOICE:
1553 return &((struct lysc_node_choice *)node)->when;
1554 case LYS_LEAF:
1555 return &((struct lysc_node_leaf *)node)->when;
1556 case LYS_LEAFLIST:
1557 return &((struct lysc_node_leaflist *)node)->when;
1558 case LYS_LIST:
1559 return &((struct lysc_node_list *)node)->when;
1560 case LYS_ANYXML:
1561 case LYS_ANYDATA:
1562 return &((struct lysc_node_anydata *)node)->when;
1563 case LYS_CASE:
1564 return &((struct lysc_node_case *)node)->when;
1565 case LYS_NOTIF:
1566 return &((struct lysc_node_notif *)node)->when;
1567 case LYS_RPC:
1568 case LYS_ACTION:
1569 return &((struct lysc_node_action *)node)->when;
1570 default:
1571 return NULL;
1572 }
1573}
1574
1575API struct lysc_when **
1576lysc_node_when(const struct lysc_node *node)
1577{
1578 struct lysc_when ***when_p;
1579
1580 when_p = lysc_node_when_p(node);
1581 if (when_p) {
1582 return *when_p;
1583 } else {
1584 return NULL;
1585 }
1586}
1587
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001588struct lys_module *
1589lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1590{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001591 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001592 if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) {
Michal Vasko69730152020-10-09 16:30:07 +02001593 return (struct lys_module *)ctx->list.objs[u];
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001594 }
1595 }
1596 return NULL;
1597}
1598
Radek Krejcid6b76452019-09-03 17:03:03 +02001599enum ly_stmt
Radek Krejcid54412f2020-12-17 20:25:35 +01001600lysp_match_kw(struct ly_in *in, uint64_t *indent)
David Sedlákc10e7902018-12-17 02:17:59 +01001601{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001602/**
Radek Krejcid54412f2020-12-17 20:25:35 +01001603 * @brief Move the input by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001604 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001605 *
1606 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001607 */
Radek Krejcid54412f2020-12-17 20:25:35 +01001608#define MOVE_IN(COUNT) \
1609 ly_in_skip(in, COUNT); \
1610 if (indent) { \
1611 (*indent)+=COUNT; \
1612 }
1613#define IF_KW(STR, LEN, STMT) \
1614 if (!strncmp(in->current, STR, LEN)) { \
1615 MOVE_IN(LEN); \
1616 (*kw)=STMT; \
1617 }
1618#define IF_KW_PREFIX(STR, LEN) \
1619 if (!strncmp(in->current, STR, LEN)) { \
1620 MOVE_IN(LEN);
1621#define IF_KW_PREFIX_END \
1622 }
David Sedlák572e7ab2019-06-04 16:01:58 +02001623
Michal Vasko63f3d842020-07-08 10:10:14 +02001624 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001625 enum ly_stmt result = LY_STMT_NONE;
1626 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001627 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001628 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001629 case 'a':
Radek Krejcid54412f2020-12-17 20:25:35 +01001630 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001631 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1632 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1633 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001634 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001635 IF_KW("data", 4, LY_STMT_ANYDATA)
1636 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001637 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001638 break;
1639 case 'b':
Radek Krejcid54412f2020-12-17 20:25:35 +01001640 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001641 IF_KW("ase", 3, LY_STMT_BASE)
1642 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1643 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001644 break;
1645 case 'c':
Radek Krejcid54412f2020-12-17 20:25:35 +01001646 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001647 IF_KW("ase", 3, LY_STMT_CASE)
1648 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001649 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001650 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001651 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001652 IF_KW("ct", 2, LY_STMT_CONTACT)
1653 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001654 IF_KW_PREFIX_END
1655 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001656 break;
1657 case 'd':
Radek Krejcid54412f2020-12-17 20:25:35 +01001658 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001659 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001660 IF_KW("fault", 5, LY_STMT_DEFAULT)
1661 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001662 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001663 IF_KW("e", 1, LY_STMT_DEVIATE)
1664 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001665 IF_KW_PREFIX_END
1666 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001667 break;
1668 case 'e':
Radek Krejcid54412f2020-12-17 20:25:35 +01001669 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001670 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001671 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001672 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1673 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001674 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001675 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001676 break;
1677 case 'f':
Radek Krejcid54412f2020-12-17 20:25:35 +01001678 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001679 IF_KW("eature", 6, LY_STMT_FEATURE)
1680 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001681 break;
1682 case 'g':
Radek Krejcid54412f2020-12-17 20:25:35 +01001683 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001684 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001685 break;
1686 case 'i':
Radek Krejcid54412f2020-12-17 20:25:35 +01001687 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001688 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1689 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1690 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001691 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001692 IF_KW("clude", 5, LY_STMT_INCLUDE)
1693 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001694 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001695 break;
1696 case 'k':
Radek Krejcid54412f2020-12-17 20:25:35 +01001697 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001698 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001699 break;
1700 case 'l':
Radek Krejcid54412f2020-12-17 20:25:35 +01001701 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001702 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001703 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1704 else IF_KW("af", 2, LY_STMT_LEAF)
1705 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001706 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001707 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001708 break;
1709 case 'm':
Radek Krejcid54412f2020-12-17 20:25:35 +01001710 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001711 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001712 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1713 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001714 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001715 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1716 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001717 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001718 IF_KW("ule", 3, LY_STMT_MODULE)
1719 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001720 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001721 break;
1722 case 'n':
Radek Krejcid54412f2020-12-17 20:25:35 +01001723 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001724 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1725 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001726 break;
1727 case 'o':
Radek Krejcid54412f2020-12-17 20:25:35 +01001728 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001729 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001730 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1731 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001732 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001733 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001734 break;
1735 case 'p':
Radek Krejcid54412f2020-12-17 20:25:35 +01001736 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001737 IF_KW("ath", 3, LY_STMT_PATH)
1738 else IF_KW("attern", 6, LY_STMT_PATTERN)
1739 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001740 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001741 IF_KW("fix", 3, LY_STMT_PREFIX)
1742 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001743 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001744 break;
1745 case 'r':
Radek Krejcid54412f2020-12-17 20:25:35 +01001746 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001747 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001748 else IF_KW_PREFIX("e", 1)
1749 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001750 IF_KW("erence", 6, LY_STMT_REFERENCE)
1751 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001752 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001753 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1754 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1755 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001756 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001757 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001758 break;
1759 case 's':
Radek Krejcid54412f2020-12-17 20:25:35 +01001760 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001761 IF_KW("tatus", 5, LY_STMT_STATUS)
1762 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001763 break;
1764 case 't':
Radek Krejcid54412f2020-12-17 20:25:35 +01001765 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001766 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1767 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001768 break;
1769 case 'u':
Radek Krejcid54412f2020-12-17 20:25:35 +01001770 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001771 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001772 IF_KW("que", 3, LY_STMT_UNIQUE)
1773 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001774 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001775 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001776 break;
1777 case 'v':
Radek Krejcid54412f2020-12-17 20:25:35 +01001778 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001779 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001780 break;
1781 case 'w':
Radek Krejcid54412f2020-12-17 20:25:35 +01001782 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001783 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001784 break;
1785 case 'y':
Radek Krejcid54412f2020-12-17 20:25:35 +01001786 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001787 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
1788 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001789 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001790 default:
Radek Krejcid54412f2020-12-17 20:25:35 +01001791 /* if indent is not NULL we are matching keyword from YANG data */
1792 if (indent) {
Michal Vasko63f3d842020-07-08 10:10:14 +02001793 if (in->current[0] == ';') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001794 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001795 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02001796 } else if (in->current[0] == '{') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001797 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001798 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001799 } else if (in->current[0] == '}') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001800 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001801 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001802 }
1803 }
David Sedlák23a59a62018-10-26 13:08:02 +02001804 break;
1805 }
1806
Michal Vasko63f3d842020-07-08 10:10:14 +02001807 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02001808 /* the keyword is not terminated */
1809 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001810 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02001811 }
1812
David Sedlák1bccdfa2019-06-17 15:55:27 +02001813#undef IF_KW
1814#undef IF_KW_PREFIX
1815#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01001816#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02001817 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01001818
David Sedlák1bccdfa2019-06-17 15:55:27 +02001819 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02001820}
David Sedlákecf5eb82019-06-03 14:12:44 +02001821
Radek Krejci85ac8312021-03-03 20:21:33 +01001822LY_ERR
1823lysp_ext_find_definition(const struct ly_ctx *ctx, const struct lysp_ext_instance *ext, const struct lys_module **ext_mod,
1824 struct lysp_ext **ext_def)
1825{
Radek Krejci85ac8312021-03-03 20:21:33 +01001826 const char *tmp, *name, *prefix;
1827 size_t pref_len, name_len;
1828 LY_ARRAY_COUNT_TYPE v;
1829 const struct lys_module *mod = NULL;
1830
Radek Krejcicbb62422021-03-15 09:29:21 +01001831 assert(ext_def);
1832
Radek Krejci85ac8312021-03-03 20:21:33 +01001833 *ext_def = NULL;
1834 if (ext_mod) {
1835 *ext_mod = NULL;
1836 }
1837
Radek Krejci677abea2021-03-05 14:24:53 +01001838 /* parse the prefix, the nodeid was previously already parsed and checked */
Radek Krejci85ac8312021-03-03 20:21:33 +01001839 tmp = ext->name;
Radek Krejci677abea2021-03-05 14:24:53 +01001840 ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len);
Radek Krejci85ac8312021-03-03 20:21:33 +01001841
1842 /* get module where the extension definition should be placed */
1843 mod = ly_resolve_prefix(ctx, prefix, pref_len, ext->format, ext->prefix_data);
1844 if (!mod) {
Radek Krejci422afb12021-03-04 16:38:16 +01001845 LOGVAL(ctx, LYVE_REFERENCE, "Invalid prefix \"%.*s\" used for extension instance identifier.", (int)pref_len, prefix);
Radek Krejci85ac8312021-03-03 20:21:33 +01001846 return LY_EVALID;
1847 } else if (!mod->parsed->extensions) {
1848 LOGVAL(ctx, LYVE_REFERENCE, "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
1849 ext->name, mod->name);
1850 return LY_EVALID;
1851 }
1852
1853 /* find the parsed extension definition there */
1854 LY_ARRAY_FOR(mod->parsed->extensions, v) {
1855 if (!strcmp(name, mod->parsed->extensions[v].name)) {
1856 *ext_def = &mod->parsed->extensions[v];
1857 break;
1858 }
1859 }
1860
Radek Krejcicbb62422021-03-15 09:29:21 +01001861 if (!(*ext_def)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001862 LOGVAL(ctx, LYVE_REFERENCE, "Extension definition of extension instance \"%s\" not found.", ext->name);
1863 return LY_EVALID;
1864 }
1865
1866 if (ext_mod) {
1867 *ext_mod = mod;
1868 }
1869 return LY_SUCCESS;
1870}
1871
1872LY_ERR
1873lysp_ext_instance_resolve_argument(struct ly_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysp_ext *ext_def)
1874{
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001875 if (!ext_def->argname || ext_p->argument) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001876 /* nothing to do */
1877 return LY_SUCCESS;
1878 }
1879
1880 if (ext_p->format == LY_PREF_XML) {
1881 /* Schema was parsed from YIN and an argument is expected, ... */
1882 struct lysp_stmt *stmt = NULL;
1883
1884 if (ext_def->flags & LYS_YINELEM_TRUE) {
1885 /* ... argument was the first XML child element */
1886 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {}
1887 if (stmt) {
1888 const char *arg, *ext, *name_arg, *name_ext, *prefix_arg, *prefix_ext;
1889 size_t name_arg_len, name_ext_len, prefix_arg_len, prefix_ext_len;
1890
1891 stmt = ext_p->child;
1892
1893 arg = stmt->stmt;
1894 ly_parse_nodeid(&arg, &prefix_arg, &prefix_arg_len, &name_arg, &name_arg_len);
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001895 if (ly_strncmp(ext_def->argname, name_arg, name_arg_len)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001896 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" expects argument element \"%s\" as its first XML child, "
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001897 "but \"%.*s\" element found.", ext_p->name, ext_def->argname, (int)name_arg_len, name_arg);
Radek Krejci85ac8312021-03-03 20:21:33 +01001898 return LY_EVALID;
1899 }
1900
1901 /* check namespace - all the extension instances must be qualified and argument element is expected in the same
1902 * namespace. Do not check just prefixes, there can be different prefixes pointing to the same namespace */
1903 ext = ext_p->name; /* include prefix */
1904 ly_parse_nodeid(&ext, &prefix_ext, &prefix_ext_len, &name_ext, &name_ext_len);
1905
1906 if (ly_resolve_prefix(ctx, prefix_ext, prefix_ext_len, ext_p->format, ext_p->prefix_data) !=
1907 ly_resolve_prefix(ctx, prefix_arg, prefix_arg_len, stmt->format, stmt->prefix_data)) {
1908 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" element and its argument element \"%s\" are "
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001909 "expected in the same namespace, but they differ.", ext_p->name, ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01001910 return LY_EVALID;
1911 }
1912 }
1913 } else {
1914 /* ... argument was one of the XML attributes which are represented as child stmt
1915 * with LYS_YIN_ATTR flag */
1916 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001917 if (!strcmp(stmt->stmt, ext_def->argname)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001918 /* this is the extension's argument */
1919 break;
1920 }
1921 }
1922 }
1923
1924 if (stmt) {
1925 LY_CHECK_RET(lydict_insert(ctx, stmt->arg, 0, &ext_p->argument));
1926 stmt->flags |= LYS_YIN_ARGUMENT;
1927 }
1928 }
1929
1930 if (!ext_p->argument) {
1931 /* missing extension's argument */
1932 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" misses argument %s\"%s\".",
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001933 ext_p->name, (ext_def->flags & LYS_YINELEM_TRUE) ? "element " : "", ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01001934 return LY_EVALID;
1935 }
1936
1937 return LY_SUCCESS;
1938}
1939
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001940LY_ARRAY_COUNT_TYPE
Radek Krejcifc596f92021-02-26 22:40:26 +01001941lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, enum ly_stmt substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001942{
1943 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1944
Michal Vaskod989ba02020-08-24 10:59:24 +02001945 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejciab430862021-03-02 20:13:40 +01001946 if (ext[index].parent_stmt == substmt) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001947 return index;
1948 }
1949 }
1950
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001951 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001952}
1953
Michal Vasko62ed12d2020-05-21 10:08:25 +02001954const struct lysc_node *
Michal Vasko72244882021-01-12 15:21:05 +01001955lysc_data_node(const struct lysc_node *schema)
Michal Vasko62ed12d2020-05-21 10:08:25 +02001956{
1957 const struct lysc_node *parent;
1958
Michal Vasko72244882021-01-12 15:21:05 +01001959 parent = schema;
Radek Krejcidf549132021-01-21 10:32:32 +01001960 while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC |
1961 LYS_ACTION | LYS_NOTIF))) {
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001962 parent = parent->parent;
Michal Vasko72244882021-01-12 15:21:05 +01001963 }
Michal Vasko62ed12d2020-05-21 10:08:25 +02001964
1965 return parent;
1966}