blob: 4eea7f0d37796c48743032eb0e6803ef5634ce27 [file] [log] [blame]
Radek Krejci86d106e2018-10-18 09:53:19 +02001/**
2 * @file tree_schema_helpers.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejcie7b95092019-05-15 11:03:07 +02004 * @brief Parsing and validation helper functions for schema trees
Radek Krejci86d106e2018-10-18 09:53:19 +02005 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Krejci535ea9f2020-05-29 16:01:05 +020014
15#define _GNU_SOURCE
Radek Krejci86d106e2018-10-18 09:53:19 +020016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020018#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010019#include <stddef.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <stdint.h>
Radek Krejci9ed7a192018-10-31 16:23:51 +010021#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020023#include <time.h>
24
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020026#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "context.h"
Radek Krejci77114102021-03-10 15:21:57 +010028#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020029#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010030#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020031#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "log.h"
Michal Vasko69730152020-10-09 16:30:07 +020033#include "parser_schema.h"
Michal Vasko962b6cd2020-12-08 10:07:49 +010034#include "schema_compile.h"
Michal Vasko79135ae2020-12-16 10:08:35 +010035#include "schema_features.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020036#include "set.h"
37#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010038#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020039#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020040#include "tree_schema_internal.h"
41
Radek Krejci85747952019-06-07 16:43:43 +020042LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +020043lysp_check_prefix(struct lys_parser_ctx *ctx, struct lysp_import *imports, const char *module_prefix, const char **value)
Radek Krejci86d106e2018-10-18 09:53:19 +020044{
45 struct lysp_import *i;
46
Michal Vasko69730152020-10-09 16:30:07 +020047 if (module_prefix && (&module_prefix != value) && !strcmp(module_prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010048 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value);
Radek Krejci86d106e2018-10-18 09:53:19 +020049 return LY_EEXIST;
50 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +010051 LY_ARRAY_FOR(imports, struct lysp_import, i) {
Michal Vasko69730152020-10-09 16:30:07 +020052 if (i->prefix && (&i->prefix != value) && !strcmp(i->prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010053 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +010054 return LY_EEXIST;
Radek Krejci86d106e2018-10-18 09:53:19 +020055 }
56 }
57 return LY_SUCCESS;
58}
59
60LY_ERR
Juraj Vijtiuk74dad9e2021-05-26 12:42:14 +020061lysp_check_date(struct lys_parser_ctx *ctx, const char *date, size_t date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +020062{
Radek Krejci86d106e2018-10-18 09:53:19 +020063 struct tm tm, tm_;
64 char *r;
65
Michal Vaskob36053d2020-03-26 15:49:30 +010066 LY_CHECK_ARG_RET(ctx ? PARSER_CTX(ctx) : NULL, date, LY_EINVAL);
67 LY_CHECK_ERR_RET(date_len != LY_REV_SIZE - 1, LOGARG(ctx ? PARSER_CTX(ctx) : NULL, date_len), LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +020068
Radek Krejcif13b87b2020-12-01 22:02:17 +010069 /* check format: YYYY-MM-DD */
Radek Krejci1deb5be2020-08-26 16:43:36 +020070 for (uint8_t i = 0; i < date_len; i++) {
Michal Vasko69730152020-10-09 16:30:07 +020071 if ((i == 4) || (i == 7)) {
Radek Krejci86d106e2018-10-18 09:53:19 +020072 if (date[i] != '-') {
73 goto error;
74 }
75 } else if (!isdigit(date[i])) {
76 goto error;
77 }
78 }
79
80 /* check content, e.g. 2018-02-31 */
81 memset(&tm, 0, sizeof tm);
82 r = strptime(date, "%Y-%m-%d", &tm);
Michal Vasko69730152020-10-09 16:30:07 +020083 if (!r || (r != &date[LY_REV_SIZE - 1])) {
Radek Krejci86d106e2018-10-18 09:53:19 +020084 goto error;
85 }
86 memcpy(&tm_, &tm, sizeof tm);
87 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
88 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
89 /* checking days is enough, since other errors
90 * have been checked by strptime() */
91 goto error;
92 }
93
94 return LY_SUCCESS;
95
96error:
Radek Krejcid33273d2018-10-25 14:55:52 +020097 if (stmt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010098 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt);
Radek Krejcid33273d2018-10-25 14:55:52 +020099 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200100 return LY_EINVAL;
101}
102
103void
104lysp_sort_revisions(struct lysp_revision *revs)
105{
Radek Krejci857189e2020-09-01 13:26:36 +0200106 LY_ARRAY_COUNT_TYPE i, r;
Radek Krejci86d106e2018-10-18 09:53:19 +0200107 struct lysp_revision rev;
108
Radek Krejcic7d13e32020-12-09 12:32:24 +0100109 for (i = 1, r = 0; i < LY_ARRAY_COUNT(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200110 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200111 r = i;
112 }
113 }
114
115 if (r) {
116 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200117 memcpy(&rev, &revs[0], sizeof rev);
118 memcpy(&revs[0], &revs[r], sizeof rev);
119 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200120 }
121}
Radek Krejci151a5b72018-10-19 14:21:44 +0200122
Radek Krejcibbe09a92018-11-08 09:36:54 +0100123static const struct lysp_tpdf *
124lysp_type_match(const char *name, struct lysp_node *node)
125{
Radek Krejci0fb28562018-12-13 15:17:37 +0100126 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200127 LY_ARRAY_COUNT_TYPE u;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100128
Radek Krejci0fb28562018-12-13 15:17:37 +0100129 typedefs = lysp_node_typedefs(node);
130 LY_ARRAY_FOR(typedefs, u) {
131 if (!strcmp(name, typedefs[u].name)) {
132 /* match */
133 return &typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100134 }
135 }
136
137 return NULL;
138}
139
Radek Krejci4f28eda2018-11-12 11:46:16 +0100140static LY_DATA_TYPE
141lysp_type_str2builtin(const char *name, size_t len)
142{
143 if (len >= 4) { /* otherwise it does not match any built-in type */
144 if (name[0] == 'b') {
145 if (name[1] == 'i') {
Michal Vasko69730152020-10-09 16:30:07 +0200146 if ((len == 6) && !strncmp(&name[2], "nary", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100147 return LY_TYPE_BINARY;
Michal Vasko69730152020-10-09 16:30:07 +0200148 } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100149 return LY_TYPE_BITS;
150 }
Michal Vasko69730152020-10-09 16:30:07 +0200151 } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100152 return LY_TYPE_BOOL;
153 }
154 } else if (name[0] == 'd') {
Michal Vasko69730152020-10-09 16:30:07 +0200155 if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100156 return LY_TYPE_DEC64;
157 }
158 } else if (name[0] == 'e') {
Michal Vasko69730152020-10-09 16:30:07 +0200159 if ((len == 5) && !strncmp(&name[1], "mpty", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100160 return LY_TYPE_EMPTY;
Michal Vasko69730152020-10-09 16:30:07 +0200161 } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100162 return LY_TYPE_ENUM;
163 }
164 } else if (name[0] == 'i') {
165 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200166 if ((len == 4) && !strncmp(&name[2], "t8", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100167 return LY_TYPE_INT8;
168 } else if (len == 5) {
169 if (!strncmp(&name[2], "t16", 3)) {
170 return LY_TYPE_INT16;
171 } else if (!strncmp(&name[2], "t32", 3)) {
172 return LY_TYPE_INT32;
173 } else if (!strncmp(&name[2], "t64", 3)) {
174 return LY_TYPE_INT64;
175 }
Michal Vasko69730152020-10-09 16:30:07 +0200176 } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100177 return LY_TYPE_INST;
178 }
Michal Vasko69730152020-10-09 16:30:07 +0200179 } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100180 return LY_TYPE_IDENT;
181 }
182 } else if (name[0] == 'l') {
Michal Vasko69730152020-10-09 16:30:07 +0200183 if ((len == 7) && !strncmp(&name[1], "eafref", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100184 return LY_TYPE_LEAFREF;
185 }
186 } else if (name[0] == 's') {
Michal Vasko69730152020-10-09 16:30:07 +0200187 if ((len == 6) && !strncmp(&name[1], "tring", 5)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100188 return LY_TYPE_STRING;
189 }
190 } else if (name[0] == 'u') {
191 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200192 if ((len == 5) && !strncmp(&name[2], "ion", 3)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100193 return LY_TYPE_UNION;
194 }
Michal Vasko69730152020-10-09 16:30:07 +0200195 } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) {
196 if ((len == 5) && (name[4] == '8')) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100197 return LY_TYPE_UINT8;
198 } else if (len == 6) {
199 if (!strncmp(&name[4], "16", 2)) {
200 return LY_TYPE_UINT16;
201 } else if (!strncmp(&name[4], "32", 2)) {
202 return LY_TYPE_UINT32;
203 } else if (!strncmp(&name[4], "64", 2)) {
204 return LY_TYPE_UINT64;
205 }
206 }
207 }
208 }
209 }
210
211 return LY_TYPE_UNKNOWN;
212}
213
Radek Krejcibbe09a92018-11-08 09:36:54 +0100214LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +0100215lysp_type_find(const char *id, struct lysp_node *start_node, const struct lysp_module *start_module,
216 LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100217{
218 const char *str, *name;
219 struct lysp_tpdf *typedefs;
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100220 const struct lys_module *mod;
Michal Vaskoa99b3572021-02-01 11:54:58 +0100221 const struct lysp_module *local_module;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200222 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100223
224 assert(id);
225 assert(start_module);
226 assert(tpdf);
227 assert(node);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100228
Radek Krejci4f28eda2018-11-12 11:46:16 +0100229 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100230 str = strchr(id, ':');
231 if (str) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200232 mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_VALUE_SCHEMA, (void *)start_module);
Michal Vaskoa99b3572021-02-01 11:54:58 +0100233 local_module = mod ? mod->parsed : NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100234 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100235 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100236 } else {
Michal Vaskoa99b3572021-02-01 11:54:58 +0100237 local_module = start_module;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100238 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100239
240 /* check for built-in types */
241 *type = lysp_type_str2builtin(name, strlen(name));
242 if (*type) {
243 *tpdf = NULL;
244 return LY_SUCCESS;
245 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100246 }
Michal Vaskoa99b3572021-02-01 11:54:58 +0100247 LY_CHECK_RET(!local_module, LY_ENOTFOUND);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100248
Michal Vaskoa99b3572021-02-01 11:54:58 +0100249 if (start_node && (local_module == start_module)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100250 /* search typedefs in parent's nodes */
251 *node = start_node;
252 while (*node) {
253 *tpdf = lysp_type_match(name, *node);
254 if (*tpdf) {
255 /* match */
256 return LY_SUCCESS;
257 }
258 *node = (*node)->parent;
259 }
260 }
261
Michal Vasko915e5442021-06-08 14:59:21 +0200262 /* go to main module if in submodule */
263 local_module = local_module->mod->parsed;
264
Radek Krejcibbe09a92018-11-08 09:36:54 +0100265 /* search in top-level typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100266 if (local_module->typedefs) {
267 LY_ARRAY_FOR(local_module->typedefs, u) {
268 if (!strcmp(name, local_module->typedefs[u].name)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100269 /* match */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100270 *tpdf = &local_module->typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100271 return LY_SUCCESS;
272 }
273 }
274 }
275
Michal Vasko915e5442021-06-08 14:59:21 +0200276 /* search in all submodules' typedefs */
Michal Vaskoa99b3572021-02-01 11:54:58 +0100277 LY_ARRAY_FOR(local_module->includes, u) {
278 typedefs = local_module->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100279 LY_ARRAY_FOR(typedefs, v) {
280 if (!strcmp(name, typedefs[v].name)) {
281 /* match */
282 *tpdf = &typedefs[v];
283 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100284 }
285 }
286 }
287
288 return LY_ENOTFOUND;
289}
290
David Sedlák6544c182019-07-12 13:17:33 +0200291LY_ERR
David Sedlák07869a52019-07-12 14:28:19 +0200292lysp_check_enum_name(struct lys_parser_ctx *ctx, const char *name, size_t name_len)
David Sedlák6544c182019-07-12 13:17:33 +0200293{
294 if (!name_len) {
295 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
296 return LY_EVALID;
297 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
298 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
Radek Krejci422afb12021-03-04 16:38:16 +0100299 (int)name_len, name);
David Sedlák6544c182019-07-12 13:17:33 +0200300 return LY_EVALID;
301 } else {
302 for (size_t u = 0; u < name_len; ++u) {
303 if (iscntrl(name[u])) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100304 LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
Radek Krejci422afb12021-03-04 16:38:16 +0100305 (int)name_len, name, u + 1);
David Sedlák6544c182019-07-12 13:17:33 +0200306 break;
307 }
308 }
309 }
310
311 return LY_SUCCESS;
312}
313
Michal Vaskob36053d2020-03-26 15:49:30 +0100314/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100315 * @brief Check name of a new type to avoid name collisions.
316 *
317 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
318 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
319 * @param[in] tpdf Typedef definition to check.
320 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
321 * typedefs are checked, caller is supposed to free the table.
322 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
323 * typedefs are checked, caller is supposed to free the table.
324 * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise.
325 */
326static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100327lysp_check_dup_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
Radek Krejci0f969882020-08-21 16:56:47 +0200328 struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100329{
Michal Vasko6adfce62021-06-16 11:57:13 +0200330 LY_ERR ret;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100331 struct lysp_node *parent;
332 uint32_t hash;
333 size_t name_len;
334 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200335 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100336 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100337
338 assert(ctx);
339 assert(tpdf);
340
341 name = tpdf->name;
342 name_len = strlen(name);
343
Radek Krejci4f28eda2018-11-12 11:46:16 +0100344 if (lysp_type_str2builtin(name, name_len)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100345 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 +0100346 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100347 }
348
349 /* check locally scoped typedefs (avoid name shadowing) */
350 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100351 typedefs = lysp_node_typedefs(node);
352 LY_ARRAY_FOR(typedefs, u) {
353 if (&typedefs[u] == tpdf) {
354 break;
355 }
356 if (!strcmp(name, typedefs[u].name)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100357 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with sibling type.", name);
Radek Krejci0fb28562018-12-13 15:17:37 +0100358 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100359 }
360 }
361 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200362 for (parent = node->parent; parent; parent = parent->parent) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100363 if (lysp_type_match(name, parent)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100364 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 +0100365 return LY_EEXIST;
366 }
367 }
368 }
369
370 /* check collision with the top-level typedefs */
371 hash = dict_hash(name, name_len);
372 if (node) {
Michal Vasko6adfce62021-06-16 11:57:13 +0200373 ret = lyht_insert(tpdfs_scoped, &name, hash, NULL);
374 LY_CHECK_ERR_RET(ret && (ret != LY_EEXIST), LOGINT(PARSER_CTX(ctx)), LY_EINT);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100375 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100376 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 +0100377 return LY_EEXIST;
378 }
379 } else {
380 if (lyht_insert(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100381 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 +0100382 return LY_EEXIST;
383 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100384 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
385 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
386 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100387 }
388
389 return LY_SUCCESS;
390}
391
Radek Krejci857189e2020-09-01 13:26:36 +0200392/**
393 * @brief Compare identifiers.
Michal Vasko62524a92021-02-26 10:08:50 +0100394 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200395 */
396static ly_bool
397lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Radek Krejcibbe09a92018-11-08 09:36:54 +0100398{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200399 return strcmp(val1, val2) == 0 ? 1 : 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100400}
401
402LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100403lysp_check_dup_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100404{
405 struct hash_table *ids_global;
406 struct hash_table *ids_scoped;
Radek Krejci0fb28562018-12-13 15:17:37 +0100407 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200408 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200409 uint32_t i;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100410 LY_ERR ret = LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100411
412 /* check name collisions - typedefs and groupings */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100413 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
414 ids_scoped = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200415 LY_ARRAY_FOR(mod->typedefs, v) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100416 ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global, ids_scoped);
417 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100418 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200419 LY_ARRAY_FOR(mod->includes, v) {
420 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100421 ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global, ids_scoped);
422 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci3b1f9292018-11-08 10:58:35 +0100423 }
424 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200425 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
426 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
427 LY_ARRAY_FOR(typedefs, u) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100428 ret = lysp_check_dup_typedef(ctx, (struct lysp_node *)ctx->tpdfs_nodes.objs[i], &typedefs[u], ids_global, ids_scoped);
429 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100430 }
431 }
Michal Vasko405cc9e2020-12-01 12:01:27 +0100432
Radek Krejcibbe09a92018-11-08 09:36:54 +0100433cleanup:
434 lyht_free(ids_global);
435 lyht_free(ids_scoped);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100436 return ret;
437}
438
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100439static ly_bool
440ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
441{
442 void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
443
444 return ptr1 == ptr2 ? 1 : 0;
445}
446
447LY_ERR
448lysp_check_dup_features(struct lys_parser_ctx *ctx, struct lysp_module *mod)
449{
450 LY_ARRAY_COUNT_TYPE u;
451 struct hash_table *ht;
452 struct lysp_feature *f;
453 uint32_t hash;
454 LY_ERR ret = LY_SUCCESS, r;
455
456 ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
457 LY_CHECK_RET(!ht, LY_EMEM);
458
459 /* add all module features into a hash table */
460 LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
461 hash = dict_hash(f->name, strlen(f->name));
462 r = lyht_insert(ht, &f->name, hash, NULL);
463 if (r == LY_EEXIST) {
464 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
465 ret = LY_EVALID;
466 goto cleanup;
467 } else if (r) {
468 ret = r;
469 goto cleanup;
470 }
471 }
472
473 /* add all submodule features into a hash table */
474 LY_ARRAY_FOR(mod->includes, u) {
475 LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
476 hash = dict_hash(f->name, strlen(f->name));
477 r = lyht_insert(ht, &f->name, hash, NULL);
478 if (r == LY_EEXIST) {
479 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
480 ret = LY_EVALID;
481 goto cleanup;
482 } else if (r) {
483 ret = r;
484 goto cleanup;
485 }
486 }
487 }
488
489cleanup:
490 lyht_free(ht);
491 return ret;
492}
493
494LY_ERR
495lysp_check_dup_identities(struct lys_parser_ctx *ctx, struct lysp_module *mod)
496{
497 LY_ARRAY_COUNT_TYPE u;
498 struct hash_table *ht;
499 struct lysp_ident *i;
500 uint32_t hash;
501 LY_ERR ret = LY_SUCCESS, r;
502
503 ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
504 LY_CHECK_RET(!ht, LY_EMEM);
505
506 /* add all module identities into a hash table */
507 LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
508 hash = dict_hash(i->name, strlen(i->name));
509 r = lyht_insert(ht, &i->name, hash, NULL);
510 if (r == LY_EEXIST) {
511 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
512 ret = LY_EVALID;
513 goto cleanup;
514 } else if (r) {
515 ret = r;
516 goto cleanup;
517 }
518 }
519
520 /* add all submodule identities into a hash table */
521 LY_ARRAY_FOR(mod->includes, u) {
522 LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
523 hash = dict_hash(i->name, strlen(i->name));
524 r = lyht_insert(ht, &i->name, hash, NULL);
525 if (r == LY_EEXIST) {
526 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
527 ret = LY_EVALID;
528 goto cleanup;
529 } else if (r) {
530 ret = r;
531 goto cleanup;
532 }
533 }
534 }
535
536cleanup:
537 lyht_free(ht);
538 return ret;
539}
540
Radek Krejci9ed7a192018-10-31 16:23:51 +0100541struct lysp_load_module_check_data {
542 const char *name;
543 const char *revision;
544 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200545 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100546};
547
548static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100549lysp_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 +0100550{
551 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100552 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200553 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100554 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100555 struct lysp_revision *revs;
556
557 name = mod ? mod->mod->name : submod->name;
558 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200559 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100560
561 if (info->name) {
562 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100563 if (strcmp(info->name, name)) {
564 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100565 return LY_EINVAL;
566 }
567 }
568 if (info->revision) {
569 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100570 if (!revs || strcmp(info->revision, revs[0].date)) {
571 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200572 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100573 return LY_EINVAL;
574 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200575 } else if (!latest_revision) {
576 /* do not log, we just need to drop the schema and use the latest revision from the context */
577 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100578 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100579 if (submod) {
580 assert(info->submoduleof);
581
Radek Krejci9ed7a192018-10-31 16:23:51 +0100582 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200583 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100584 LOGVAL(ctx, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200585 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100586 return LY_EVALID;
587 }
588 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100589 if (submod->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100590 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100591 return LY_EVALID;
592 }
593 }
594 if (info->path) {
595 /* check that name and revision match filename */
596 filename = strrchr(info->path, '/');
597 if (!filename) {
598 filename = info->path;
599 } else {
600 filename++;
601 }
602 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100603 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100604 rev = strchr(filename, '@');
605 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100606 if (strncmp(filename, name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +0200607 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100608 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100609 }
610 /* revision */
611 if (rev) {
612 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100613 if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100614 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +0200615 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100616 }
617 }
618 }
619 return LY_SUCCESS;
620}
621
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200622/**
Michal Vasko4e205e82021-06-08 14:01:47 +0200623 * @brief Parse a (sub)module from a local file and add into the context.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200624 *
625 * This function does not check the presence of the (sub)module in context, it should be done before calling this function.
626 *
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200627 * @param[in] ctx libyang context where to work.
628 * @param[in] name Name of the (sub)module to load.
629 * @param[in] revision Optional revision of the (sub)module to load, if NULL the newest revision is being loaded.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200630 * @param[in] main_ctx Parser context of the main module in case of loading submodule.
631 * @param[in] main_name Main module name in case of loading submodule.
632 * @param[in] required Module is required so error (even if the input file not found) are important. If 0, there is some
633 * backup and it is actually ok if the input data are not found. However, parser reports errors even in this case.
Michal Vaskodd992582021-06-10 14:34:57 +0200634 * @param[in,out] new_mods Set of all the new mods added to the context. Includes this module and all of its imports.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200635 * @param[out] result Parsed YANG schema tree of the requested module (struct lys_module*) or submodule (struct lysp_submodule*).
636 * If it is a module, it is already in the context!
Michal Vasko4e205e82021-06-08 14:01:47 +0200637 * @return LY_SUCCESS on success.
Michal Vasko4e205e82021-06-08 14:01:47 +0200638 * @return LY_ERR on error.
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200639 */
640static LY_ERR
Michal Vasko4e205e82021-06-08 14:01:47 +0200641lys_parse_localfile(struct ly_ctx *ctx, const char *name, const char *revision, struct lys_parser_ctx *main_ctx,
Michal Vaskodd992582021-06-10 14:34:57 +0200642 const char *main_name, ly_bool required, struct ly_set *new_mods, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100643{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200644 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100645 char *filepath = NULL;
646 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100647 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100648 LY_ERR ret = LY_SUCCESS;
649 struct lysp_load_module_check_data check_data = {0};
650
Michal Vasko87f1cf02021-06-08 14:02:47 +0200651 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name,
652 revision, &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200653 if (!filepath) {
654 if (required) {
655 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200656 revision ? revision : "");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200657 }
658 return LY_ENOTFOUND;
659 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100660
661 LOGVRB("Loading schema from \"%s\" file.", filepath);
662
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200663 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200664 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +0200665 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100666 check_data.name = name;
667 check_data.revision = revision;
668 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800669 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200670 if (main_ctx) {
aPiecekc3e26142021-06-22 14:25:49 +0200671 ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data, new_mods,
Michal Vasko69730152020-10-09 16:30:07 +0200672 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200673 } else {
Michal Vaskodd992582021-06-10 14:34:57 +0200674 ret = lys_parse_in(ctx, in, format, lysp_load_module_check, &check_data, new_mods, (struct lys_module **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200675
676 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200677 ly_in_free(in, 1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200678 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100679
680 *result = mod;
681
682 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200683
Radek Krejci9ed7a192018-10-31 16:23:51 +0100684cleanup:
685 free(filepath);
686 return ret;
687}
688
Radek Krejcid33273d2018-10-25 14:55:52 +0200689LY_ERR
Michal Vaskodd992582021-06-10 14:34:57 +0200690lys_parse_load(struct ly_ctx *ctx, const char *name, const char *revision, struct ly_set *new_mods,
Michal Vasko4e205e82021-06-08 14:01:47 +0200691 struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200692{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100693 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200694 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200695
Radek Krejci9ed7a192018-10-31 16:23:51 +0100696 void (*module_data_free)(void *module_data, void *user_data) = NULL;
697 struct lysp_load_module_check_data check_data = {0};
Michal Vasko4e205e82021-06-08 14:01:47 +0200698 struct lys_module *ctx_latest = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200699 struct ly_in *in;
Radek Krejci086c7132018-10-26 15:29:04 +0200700
Michal Vaskodd992582021-06-10 14:34:57 +0200701 assert(mod && new_mods);
Radek Krejci0af46292019-01-11 16:02:31 +0100702
Michal Vasko0550b762020-11-24 18:04:08 +0100703 /*
704 * try to get the module from the context
705 */
Radek Krejci0af46292019-01-11 16:02:31 +0100706 if (!*mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100707 if (revision) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200708 /* get the specific revision */
Michal Vasko22df3f02020-08-24 13:29:22 +0200709 *mod = (struct lys_module *)ly_ctx_get_module(ctx, name, revision);
Radek Krejci0af46292019-01-11 16:02:31 +0100710 } else {
Michal Vasko4e205e82021-06-08 14:01:47 +0200711 /* get the requested module of the latest revision in the context */
712 *mod = (struct lys_module *)ly_ctx_get_module_latest(ctx, name);
713 if (*mod && ((*mod)->latest_revision == 1)) {
714 /* let us now search with callback and searchpaths to check if there is newer revision outside the context */
715 ctx_latest = *mod;
716 *mod = NULL;
Radek Krejcib3289d62019-09-18 12:21:39 +0200717 }
Radek Krejci0af46292019-01-11 16:02:31 +0100718 }
Radek Krejci086c7132018-10-26 15:29:04 +0200719 }
720
Michal Vasko4e205e82021-06-08 14:01:47 +0200721 /* we have module from the current context, circular check */
722 if (*mod && (*mod)->parsed->parsing) {
723 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
724 *mod = NULL;
725 return LY_EVALID;
Michal Vasko0550b762020-11-24 18:04:08 +0100726 }
Radek Krejci086c7132018-10-26 15:29:04 +0200727
Michal Vasko0550b762020-11-24 18:04:08 +0100728 /*
729 * no suitable module in the context, try to load it
730 */
731 if (!*mod) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100732 /* module not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200733 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
734search_clb:
Michal Vasko87f1cf02021-06-08 14:02:47 +0200735 if (ctx->imp_clb && !ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, &format, &module_data,
736 &module_data_free)) {
737 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
738 check_data.name = name;
739 check_data.revision = revision;
Michal Vaskodd992582021-06-10 14:34:57 +0200740 lys_parse_in(ctx, in, format, lysp_load_module_check, &check_data, new_mods, mod);
Michal Vasko87f1cf02021-06-08 14:02:47 +0200741 ly_in_free(in, 0);
742 if (module_data_free) {
743 module_data_free((void *)module_data, ctx->imp_clb_data);
Radek Krejci086c7132018-10-26 15:29:04 +0200744 }
745 }
746 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
747 goto search_file;
748 }
749 } else {
750search_file:
751 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
752 /* module was not received from the callback or there is no callback set */
Michal Vaskodd992582021-06-10 14:34:57 +0200753 lys_parse_localfile(ctx, name, revision, NULL, NULL, ctx_latest ? 0 : 1, new_mods, (void **)mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200754 }
Michal Vasko0550b762020-11-24 18:04:08 +0100755 if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejci086c7132018-10-26 15:29:04 +0200756 goto search_clb;
757 }
758 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100759
Radek Krejcib3289d62019-09-18 12:21:39 +0200760 /* update the latest_revision flag - here we have selected the latest available schema,
761 * consider that even the callback provides correct latest revision */
Michal Vasko0550b762020-11-24 18:04:08 +0100762 if (!*mod && ctx_latest) {
Michal Vasko003c44a2021-02-24 17:13:11 +0100763 LOGVRB("Newer revision than \"%s@%s\" not found, using this as the latest revision.", ctx_latest->name,
Michal Vasko0550b762020-11-24 18:04:08 +0100764 ctx_latest->revision);
765 ctx_latest->latest_revision = 2;
766 *mod = ctx_latest;
767 } else if (*mod && !revision && ((*mod)->latest_revision == 1)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100768 (*mod)->latest_revision = 2;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100769 }
Radek Krejci086c7132018-10-26 15:29:04 +0200770
Michal Vasko0550b762020-11-24 18:04:08 +0100771 if (!*mod) {
Michal Vasko4e205e82021-06-08 14:01:47 +0200772 LOGVAL(ctx, LYVE_REFERENCE, "Loading \"%s\" module failed.", name);
Michal Vasko0550b762020-11-24 18:04:08 +0100773 return LY_EVALID;
774 }
Radek Krejci086c7132018-10-26 15:29:04 +0200775 }
Radek Krejci086c7132018-10-26 15:29:04 +0200776
777 return LY_SUCCESS;
778}
779
780LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200781lysp_check_stringchar(struct lys_parser_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +0200782{
783 if (!is_yangutf8char(c)) {
784 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
785 return LY_EVALID;
786 }
787 return LY_SUCCESS;
788}
789
790LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200791lysp_check_identifierchar(struct lys_parser_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +0200792{
Michal Vasko69730152020-10-09 16:30:07 +0200793 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +0200794 if (!is_yangidentstartchar(c)) {
aPiecekc89b2242021-05-14 14:19:11 +0200795 if ((c < UCHAR_MAX) && isprint(c)) {
Michal Vasko7c769042021-03-25 12:20:49 +0100796 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
797 } else {
798 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character 0x%04x.", c);
799 }
David Sedlák4a650532019-07-10 11:55:18 +0200800 return LY_EVALID;
801 }
802 if (prefix) {
803 if (first) {
804 (*prefix) = 0;
805 } else {
806 (*prefix) = 2;
807 }
808 }
Michal Vasko69730152020-10-09 16:30:07 +0200809 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +0200810 (*prefix) = 1;
811 } else if (!is_yangidentchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200812 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200813 return LY_EVALID;
814 }
815
816 return LY_SUCCESS;
817}
818
Radek Krejci771928a2021-01-19 13:42:36 +0100819/**
820 * @brief Try to find the parsed submodule in main module for the given include record.
821 *
822 * @param[in] pctx main parser context
823 * @param[in] inc The include record with missing parsed submodule. According to include info try to find
824 * the corresponding parsed submodule in main module's includes.
825 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
826 * @return LY_ENOT - the parsed module was not found.
827 * @return LY_EVALID - YANG rule violation
828 */
829static LY_ERR
830lysp_get_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200831{
Radek Krejci771928a2021-01-19 13:42:36 +0100832 LY_ARRAY_COUNT_TYPE i;
833 struct lysp_module *main_pmod = pctx->parsed_mod->mod->parsed;
Michal Vasko69730152020-10-09 16:30:07 +0200834
Radek Krejci771928a2021-01-19 13:42:36 +0100835 LY_ARRAY_FOR(main_pmod->includes, i) {
836 if (strcmp(main_pmod->includes[i].name, inc->name)) {
837 continue;
838 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200839
Radek Krejci771928a2021-01-19 13:42:36 +0100840 if (inc->rev[0] && strncmp(inc->rev, main_pmod->includes[i].rev, LY_REV_SIZE)) {
841 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
842 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
843 ((struct lysp_submodule *)pctx->parsed_mod)->name, inc->rev,
844 main_pmod->includes[i].name, main_pmod->includes[i].rev, main_pmod->mod->name);
845 return LY_EVALID;
846 }
847
848 inc->submodule = main_pmod->includes[i].submodule;
849 return inc->submodule ? LY_SUCCESS : LY_ENOT;
850 }
851
852 if (main_pmod->version == LYS_VERSION_1_1) {
853 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
854 "YANG 1.1 requires all submodules to be included from main module. "
855 "But submodule \"%s\" includes submodule \"%s\" which is not included by main module \"%s\".",
856 ((struct lysp_submodule *)pctx->parsed_mod)->name, inc->name, main_pmod->mod->name);
857 return LY_EVALID;
858 } else {
859 return LY_ENOT;
860 }
861}
862
863/**
864 * @brief Make the copy of the given include record into the main module.
865 *
866 * YANG 1.0 does not require the main module to include all the submodules. Therefore, parsing submodules can cause
867 * reallocating and extending the includes array in the main module by the submodules included only in submodules.
868 *
869 * @param[in] pctx main parser context
870 * @param[in] inc Include record to copy into main module taken from @p pctx.
871 * @return LY_ERR value.
872 */
873static LY_ERR
874lysp_inject_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
875{
876 LY_ARRAY_COUNT_TYPE i;
877 struct lysp_include *inc_new, *inc_tofill = NULL;
878 struct lysp_module *main_pmod = pctx->parsed_mod->mod->parsed;
879
880 /* first, try to find the corresponding record with missing parsed submodule */
881 LY_ARRAY_FOR(main_pmod->includes, i) {
882 if (strcmp(main_pmod->includes[i].name, inc->name)) {
883 continue;
884 }
885 inc_tofill = &main_pmod->includes[i];
886 break;
887 }
888
889 if (inc_tofill) {
890 inc_tofill->submodule = inc->submodule;
891 } else {
892 LY_ARRAY_NEW_RET(PARSER_CTX(pctx), main_pmod->includes, inc_new, LY_EMEM);
893
894 inc_new->submodule = inc->submodule;
895 DUP_STRING_RET(PARSER_CTX(pctx), inc->name, inc_new->name);
896 DUP_STRING_RET(PARSER_CTX(pctx), inc->dsc, inc_new->dsc);
897 DUP_STRING_RET(PARSER_CTX(pctx), inc->ref, inc_new->ref);
898 /* TODO duplicate extensions */
899 memcpy(inc_new->rev, inc->rev, LY_REV_SIZE);
900 inc_new->injected = 1;
901 }
902 return LY_SUCCESS;
903}
904
905LY_ERR
aPiecekc3e26142021-06-22 14:25:49 +0200906lysp_load_submodules(struct lys_parser_ctx *pctx, struct lysp_module *pmod, struct ly_set *new_mods)
Radek Krejci771928a2021-01-19 13:42:36 +0100907{
908 LY_ARRAY_COUNT_TYPE u;
909 struct ly_ctx *ctx = PARSER_CTX(pctx);
910
911 LY_ARRAY_FOR(pmod->includes, u) {
912 LY_ERR ret = LY_SUCCESS;
913 struct lysp_submodule *submod = NULL;
914 struct lysp_include *inc = &pmod->includes[u];
915
916 if (inc->submodule) {
917 continue;
918 }
919
920 if (pmod->is_submod) {
921 /* try to find the submodule in the main module or its submodules */
922 ret = lysp_get_submodule(pctx, inc);
923 LY_CHECK_RET(ret && ret != LY_ENOT, ret);
924 LY_CHECK_RET(ret == LY_SUCCESS, LY_SUCCESS); /* submodule found in linked with the inc */
925 }
926
927 /* submodule not present in the main module, get the input data and parse it */
928 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcidf549132021-01-21 10:32:32 +0100929search_clb:
Radek Krejci771928a2021-01-19 13:42:36 +0100930 if (ctx->imp_clb) {
931 const char *submodule_data = NULL;
932 LYS_INFORMAT format = LYS_IN_UNKNOWN;
933 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
934 struct lysp_load_module_check_data check_data = {0};
935 struct ly_in *in;
936
937 if (ctx->imp_clb(pctx->parsed_mod->mod->name, NULL, inc->name,
938 inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
939 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
940 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
941 check_data.name = inc->name;
942 check_data.revision = inc->rev[0] ? inc->rev : NULL;
943 check_data.submoduleof = pctx->parsed_mod->mod->name;
aPiecekc3e26142021-06-22 14:25:49 +0200944 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, new_mods, &submod);
Radek Krejci771928a2021-01-19 13:42:36 +0100945
946 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
947 * submodule's include into main module, where it is missing */
948 inc = &pmod->includes[u];
949
950 ly_in_free(in, 0);
951 if (submodule_data_free) {
952 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
953 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200954 }
955 }
Radek Krejci771928a2021-01-19 13:42:36 +0100956 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
957 goto search_file;
958 }
959 } else {
Radek Krejcidf549132021-01-21 10:32:32 +0100960search_file:
Radek Krejci771928a2021-01-19 13:42:36 +0100961 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
962 /* submodule was not received from the callback or there is no callback set */
Michal Vasko4e205e82021-06-08 14:01:47 +0200963 lys_parse_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, pctx,
Radek Krejci771928a2021-01-19 13:42:36 +0100964 pctx->parsed_mod->mod->name, 1, NULL, (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100965
Radek Krejci771928a2021-01-19 13:42:36 +0100966 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
967 * submodule's include into main module, where it is missing */
968 inc = &pmod->includes[u];
969 }
970 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
971 goto search_clb;
972 }
973 }
974 if (submod) {
975 if (!inc->rev[0] && (submod->latest_revision == 1)) {
976 /* update the latest_revision flag - here we have selected the latest available schema,
977 * consider that even the callback provides correct latest revision */
978 submod->latest_revision = 2;
979 }
980
981 inc->submodule = submod;
982 if (ret == LY_ENOT) {
983 /* the submodule include is not present in YANG 1.0 main module - add it there */
984 LY_CHECK_RET(lysp_inject_submodule(pctx, &pmod->includes[u]));
985 }
986 }
987 if (!inc->submodule) {
988 LOGVAL(ctx, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name,
989 pctx->parsed_mod->is_submod ? ((struct lysp_submodule *)pctx->parsed_mod)->name : pctx->parsed_mod->mod->name);
990 return LY_EVALID;
991 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200992 }
993
994 return LY_SUCCESS;
995}
996
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100997API const struct lysc_when *
998lysc_has_when(const struct lysc_node *node)
999{
Radek Krejci9a3823e2021-01-27 20:26:46 +01001000 struct lysc_when **when;
1001
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001002 if (!node) {
1003 return NULL;
1004 }
1005
1006 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001007 when = lysc_node_when(node);
1008 if (when) {
1009 return when[0];
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001010 }
1011 node = node->parent;
1012 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1013
1014 return NULL;
1015}
1016
Radek Krejci0935f412019-08-20 16:15:18 +02001017API const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001018lys_nodetype2str(uint16_t nodetype)
1019{
Michal Vaskod989ba02020-08-24 10:59:24 +02001020 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001021 case LYS_CONTAINER:
1022 return "container";
1023 case LYS_CHOICE:
1024 return "choice";
1025 case LYS_LEAF:
1026 return "leaf";
1027 case LYS_LEAFLIST:
1028 return "leaf-list";
1029 case LYS_LIST:
1030 return "list";
1031 case LYS_ANYXML:
1032 return "anyxml";
1033 case LYS_ANYDATA:
1034 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001035 case LYS_CASE:
1036 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001037 case LYS_RPC:
1038 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001039 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001040 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001041 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001042 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001043 case LYS_USES:
1044 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001045 default:
1046 return "unknown";
1047 }
1048}
1049
Radek Krejci39b7fc22021-02-26 23:29:18 +01001050API enum ly_stmt
1051lys_nodetype2stmt(uint16_t nodetype)
1052{
1053 switch (nodetype) {
1054 case LYS_CONTAINER:
1055 return LY_STMT_CONTAINER;
1056 case LYS_CHOICE:
1057 return LY_STMT_CHOICE;
1058 case LYS_LEAF:
1059 return LY_STMT_LEAF;
1060 case LYS_LEAFLIST:
1061 return LY_STMT_LEAF_LIST;
1062 case LYS_LIST:
1063 return LY_STMT_LIST;
1064 case LYS_ANYXML:
1065 return LY_STMT_ANYXML;
1066 case LYS_ANYDATA:
1067 return LY_STMT_ANYDATA;
1068 case LYS_CASE:
1069 return LY_STMT_CASE;
1070 case LYS_RPC:
1071 return LY_STMT_RPC;
1072 case LYS_ACTION:
1073 return LY_STMT_ACTION;
1074 case LYS_NOTIF:
1075 return LY_STMT_NOTIFICATION;
1076 case LYS_USES:
1077 return LY_STMT_USES;
1078 case LYS_INPUT:
1079 return LY_STMT_INPUT;
1080 case LYS_OUTPUT:
1081 return LY_STMT_OUTPUT;
1082 default:
1083 return LY_STMT_NONE;
1084 }
1085}
1086
Radek Krejci693262f2019-04-29 15:23:20 +02001087const char *
1088lys_datatype2str(LY_DATA_TYPE basetype)
1089{
Michal Vaskod989ba02020-08-24 10:59:24 +02001090 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001091 case LY_TYPE_BINARY:
1092 return "binary";
1093 case LY_TYPE_UINT8:
1094 return "uint8";
1095 case LY_TYPE_UINT16:
1096 return "uint16";
1097 case LY_TYPE_UINT32:
1098 return "uint32";
1099 case LY_TYPE_UINT64:
1100 return "uint64";
1101 case LY_TYPE_STRING:
1102 return "string";
1103 case LY_TYPE_BITS:
1104 return "bits";
1105 case LY_TYPE_BOOL:
1106 return "boolean";
1107 case LY_TYPE_DEC64:
1108 return "decimal64";
1109 case LY_TYPE_EMPTY:
1110 return "empty";
1111 case LY_TYPE_ENUM:
1112 return "enumeration";
1113 case LY_TYPE_IDENT:
1114 return "identityref";
1115 case LY_TYPE_INST:
1116 return "instance-identifier";
1117 case LY_TYPE_LEAFREF:
1118 return "leafref";
1119 case LY_TYPE_UNION:
1120 return "union";
1121 case LY_TYPE_INT8:
1122 return "int8";
1123 case LY_TYPE_INT16:
1124 return "int16";
1125 case LY_TYPE_INT32:
1126 return "int32";
1127 case LY_TYPE_INT64:
1128 return "int64";
1129 default:
1130 return "unknown";
1131 }
1132}
1133
Radek Krejci056d0a82018-12-06 16:57:25 +01001134API const struct lysp_tpdf *
1135lysp_node_typedefs(const struct lysp_node *node)
1136{
Radek Krejci0fb28562018-12-13 15:17:37 +01001137 switch (node->nodetype) {
1138 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001139 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001140 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001141 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001142 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001143 return ((struct lysp_node_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001144 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001145 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001146 return ((struct lysp_node_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001147 case LYS_INPUT:
1148 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001149 return ((struct lysp_node_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001150 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001151 return ((struct lysp_node_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001152 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001153 return NULL;
1154 }
1155}
1156
Radek Krejci2a9fc652021-01-22 17:44:34 +01001157API const struct lysp_node_grp *
Radek Krejci53ea6152018-12-13 15:21:15 +01001158lysp_node_groupings(const struct lysp_node *node)
1159{
1160 switch (node->nodetype) {
1161 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001162 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001163 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001164 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001165 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001166 return ((struct lysp_node_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001167 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001168 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001169 return ((struct lysp_node_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001170 case LYS_INPUT:
1171 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001172 return ((struct lysp_node_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001173 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001174 return ((struct lysp_node_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001175 default:
1176 return NULL;
1177 }
1178}
1179
Radek Krejci2a9fc652021-01-22 17:44:34 +01001180struct lysp_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001181lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001182{
1183 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001184
Radek Krejcibbe09a92018-11-08 09:36:54 +01001185 switch (node->nodetype) {
1186 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001187 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001188 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001189 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001190 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001191 return &((struct lysp_node_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001192 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001193 return &((struct lysp_node_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001194 default:
1195 return NULL;
1196 }
1197}
1198
Radek Krejci2a9fc652021-01-22 17:44:34 +01001199API const struct lysp_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001200lysp_node_actions(const struct lysp_node *node)
1201{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001202 struct lysp_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001203
Michal Vasko22df3f02020-08-24 13:29:22 +02001204 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001205 if (actions) {
1206 return *actions;
1207 } else {
1208 return NULL;
1209 }
1210}
1211
Radek Krejci2a9fc652021-01-22 17:44:34 +01001212struct lysp_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001213lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001214{
1215 assert(node);
1216 switch (node->nodetype) {
1217 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001218 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001219 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001220 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001221 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001222 return &((struct lysp_node_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001223 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001224 return &((struct lysp_node_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001225 default:
1226 return NULL;
1227 }
1228}
1229
Radek Krejci2a9fc652021-01-22 17:44:34 +01001230API const struct lysp_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001231lysp_node_notifs(const struct lysp_node *node)
1232{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001233 struct lysp_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001234
Michal Vasko22df3f02020-08-24 13:29:22 +02001235 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001236 if (notifs) {
1237 return *notifs;
1238 } else {
1239 return NULL;
1240 }
1241}
1242
Radek Krejcibbe09a92018-11-08 09:36:54 +01001243struct lysp_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001244lysp_node_child_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001245{
1246 assert(node);
1247 switch (node->nodetype) {
1248 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001249 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001250 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001251 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001252 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001253 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001254 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001255 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001256 case LYS_GROUPING:
Radek Krejci01180ac2021-01-27 08:48:22 +01001257 return &((struct lysp_node_grp *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001258 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001259 return &((struct lysp_node_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001260 case LYS_INPUT:
1261 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001262 return &((struct lysp_node_action_inout *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001263 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001264 return &((struct lysp_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001265 default:
1266 return NULL;
1267 }
1268}
1269
Radek Krejci056d0a82018-12-06 16:57:25 +01001270API const struct lysp_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001271lysp_node_child(const struct lysp_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01001272{
Michal Vasko544e58a2021-01-28 14:33:41 +01001273 struct lysp_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001274
1275 if (!node) {
1276 return NULL;
1277 }
1278
Michal Vasko544e58a2021-01-28 14:33:41 +01001279 child = lysp_node_child_p((struct lysp_node *)node);
1280 if (child) {
1281 return *child;
Radek Krejci056d0a82018-12-06 16:57:25 +01001282 } else {
1283 return NULL;
1284 }
1285}
1286
Radek Krejci9a3823e2021-01-27 20:26:46 +01001287struct lysp_restr **
1288lysp_node_musts_p(const struct lysp_node *node)
1289{
1290 if (!node) {
1291 return NULL;
1292 }
1293
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001294 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001295 case LYS_CONTAINER:
1296 return &((struct lysp_node_container *)node)->musts;
1297 case LYS_LEAF:
1298 return &((struct lysp_node_leaf *)node)->musts;
1299 case LYS_LEAFLIST:
1300 return &((struct lysp_node_leaflist *)node)->musts;
1301 case LYS_LIST:
1302 return &((struct lysp_node_list *)node)->musts;
1303 case LYS_ANYXML:
1304 case LYS_ANYDATA:
1305 return &((struct lysp_node_anydata *)node)->musts;
1306 case LYS_NOTIF:
1307 return &((struct lysp_node_notif *)node)->musts;
1308 case LYS_INPUT:
1309 case LYS_OUTPUT:
1310 return &((struct lysp_node_action_inout *)node)->musts;
1311 default:
1312 return NULL;
1313 }
1314}
1315
1316struct lysp_restr *
1317lysp_node_musts(const struct lysp_node *node)
1318{
1319 struct lysp_restr **musts;
1320
1321 musts = lysp_node_musts_p(node);
1322 if (musts) {
1323 return *musts;
1324 } else {
1325 return NULL;
1326 }
1327}
1328
1329struct lysp_when **
1330lysp_node_when_p(const struct lysp_node *node)
1331{
1332 if (!node) {
1333 return NULL;
1334 }
1335
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001336 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001337 case LYS_CONTAINER:
1338 return &((struct lysp_node_container *)node)->when;
1339 case LYS_CHOICE:
1340 return &((struct lysp_node_choice *)node)->when;
1341 case LYS_LEAF:
1342 return &((struct lysp_node_leaf *)node)->when;
1343 case LYS_LEAFLIST:
1344 return &((struct lysp_node_leaflist *)node)->when;
1345 case LYS_LIST:
1346 return &((struct lysp_node_list *)node)->when;
1347 case LYS_ANYXML:
1348 case LYS_ANYDATA:
1349 return &((struct lysp_node_anydata *)node)->when;
1350 case LYS_CASE:
1351 return &((struct lysp_node_case *)node)->when;
1352 case LYS_USES:
1353 return &((struct lysp_node_uses *)node)->when;
1354 case LYS_AUGMENT:
1355 return &((struct lysp_node_augment *)node)->when;
1356 default:
1357 return NULL;
1358 }
1359}
1360
1361struct lysp_when *
1362lysp_node_when(const struct lysp_node *node)
1363{
1364 struct lysp_when **when;
1365
1366 when = lysp_node_when_p(node);
1367 if (when) {
1368 return *when;
1369 } else {
1370 return NULL;
1371 }
1372}
1373
Radek Krejci2a9fc652021-01-22 17:44:34 +01001374struct lysc_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001375lysc_node_actions_p(struct lysc_node *node)
1376{
1377 assert(node);
1378 switch (node->nodetype) {
1379 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001380 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001381 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001382 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001383 default:
1384 return NULL;
1385 }
1386}
1387
Radek Krejci2a9fc652021-01-22 17:44:34 +01001388API const struct lysc_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001389lysc_node_actions(const struct lysc_node *node)
1390{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001391 struct lysc_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001392
Michal Vasko22df3f02020-08-24 13:29:22 +02001393 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001394 if (actions) {
1395 return *actions;
1396 } else {
1397 return NULL;
1398 }
1399}
1400
Radek Krejci2a9fc652021-01-22 17:44:34 +01001401struct lysc_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001402lysc_node_notifs_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)->notifs;
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)->notifs;
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_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001416lysc_node_notifs(const struct lysc_node *node)
1417{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001418 struct lysc_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001419
Michal Vasko22df3f02020-08-24 13:29:22 +02001420 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001421 if (notifs) {
1422 return *notifs;
1423 } else {
1424 return NULL;
1425 }
1426}
1427
Radek Krejcibbe09a92018-11-08 09:36:54 +01001428struct lysc_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001429lysc_node_child_p(const struct lysc_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001430{
Michal Vasko544e58a2021-01-28 14:33:41 +01001431 assert(node && !(node->nodetype & (LYS_RPC | LYS_ACTION)));
1432
Radek Krejcibbe09a92018-11-08 09:36:54 +01001433 switch (node->nodetype) {
1434 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001435 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001436 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001437 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001438 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001439 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001440 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001441 return &((struct lysc_node_list *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001442 case LYS_INPUT:
1443 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001444 return &((struct lysc_node_action_inout *)node)->child;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001445 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001446 return &((struct lysc_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001447 default:
1448 return NULL;
1449 }
1450}
1451
Radek Krejci056d0a82018-12-06 16:57:25 +01001452API const struct lysc_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001453lysc_node_child(const struct lysc_node *node)
Radek Krejcia3045382018-11-22 14:30:31 +01001454{
Michal Vasko544e58a2021-01-28 14:33:41 +01001455 struct lysc_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001456
1457 if (!node) {
1458 return NULL;
1459 }
1460
Michal Vasko544e58a2021-01-28 14:33:41 +01001461 if (node->nodetype & (LYS_RPC | LYS_ACTION)) {
1462 return &((struct lysc_node_action *)node)->input.node;
Radek Krejcibe154442021-01-21 11:06:36 +01001463 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01001464 child = lysc_node_child_p(node);
1465 if (child) {
1466 return *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001467 }
Michal Vasko2a668712020-10-21 11:48:09 +02001468 }
Michal Vasko544e58a2021-01-28 14:33:41 +01001469
1470 return NULL;
Michal Vasko2a668712020-10-21 11:48:09 +02001471}
1472
Radek Krejci9a3823e2021-01-27 20:26:46 +01001473struct lysc_must **
1474lysc_node_musts_p(const struct lysc_node *node)
1475{
1476 if (!node) {
1477 return NULL;
1478 }
1479
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001480 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001481 case LYS_CONTAINER:
1482 return &((struct lysc_node_container *)node)->musts;
1483 case LYS_LEAF:
1484 return &((struct lysc_node_leaf *)node)->musts;
1485 case LYS_LEAFLIST:
1486 return &((struct lysc_node_leaflist *)node)->musts;
1487 case LYS_LIST:
1488 return &((struct lysc_node_list *)node)->musts;
1489 case LYS_ANYXML:
1490 case LYS_ANYDATA:
1491 return &((struct lysc_node_anydata *)node)->musts;
1492 case LYS_NOTIF:
1493 return &((struct lysc_node_notif *)node)->musts;
1494 case LYS_INPUT:
1495 case LYS_OUTPUT:
1496 return &((struct lysc_node_action_inout *)node)->musts;
1497 default:
1498 return NULL;
1499 }
1500}
1501
1502API struct lysc_must *
1503lysc_node_musts(const struct lysc_node *node)
1504{
1505 struct lysc_must **must_p;
1506
1507 must_p = lysc_node_musts_p(node);
1508 if (must_p) {
1509 return *must_p;
1510 } else {
1511 return NULL;
1512 }
1513}
1514
1515struct lysc_when ***
1516lysc_node_when_p(const struct lysc_node *node)
1517{
1518 if (!node) {
1519 return NULL;
1520 }
1521
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001522 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001523 case LYS_CONTAINER:
1524 return &((struct lysc_node_container *)node)->when;
1525 case LYS_CHOICE:
1526 return &((struct lysc_node_choice *)node)->when;
1527 case LYS_LEAF:
1528 return &((struct lysc_node_leaf *)node)->when;
1529 case LYS_LEAFLIST:
1530 return &((struct lysc_node_leaflist *)node)->when;
1531 case LYS_LIST:
1532 return &((struct lysc_node_list *)node)->when;
1533 case LYS_ANYXML:
1534 case LYS_ANYDATA:
1535 return &((struct lysc_node_anydata *)node)->when;
1536 case LYS_CASE:
1537 return &((struct lysc_node_case *)node)->when;
1538 case LYS_NOTIF:
1539 return &((struct lysc_node_notif *)node)->when;
1540 case LYS_RPC:
1541 case LYS_ACTION:
1542 return &((struct lysc_node_action *)node)->when;
1543 default:
1544 return NULL;
1545 }
1546}
1547
1548API struct lysc_when **
1549lysc_node_when(const struct lysc_node *node)
1550{
1551 struct lysc_when ***when_p;
1552
1553 when_p = lysc_node_when_p(node);
1554 if (when_p) {
1555 return *when_p;
1556 } else {
1557 return NULL;
1558 }
1559}
1560
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001561struct lys_module *
1562lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1563{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001564 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001565 if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) {
Michal Vasko69730152020-10-09 16:30:07 +02001566 return (struct lys_module *)ctx->list.objs[u];
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001567 }
1568 }
1569 return NULL;
1570}
1571
Radek Krejcid6b76452019-09-03 17:03:03 +02001572enum ly_stmt
Radek Krejcid54412f2020-12-17 20:25:35 +01001573lysp_match_kw(struct ly_in *in, uint64_t *indent)
David Sedlákc10e7902018-12-17 02:17:59 +01001574{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001575/**
Radek Krejcid54412f2020-12-17 20:25:35 +01001576 * @brief Move the input by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001577 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001578 *
1579 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001580 */
Radek Krejcid54412f2020-12-17 20:25:35 +01001581#define MOVE_IN(COUNT) \
1582 ly_in_skip(in, COUNT); \
1583 if (indent) { \
1584 (*indent)+=COUNT; \
1585 }
1586#define IF_KW(STR, LEN, STMT) \
1587 if (!strncmp(in->current, STR, LEN)) { \
1588 MOVE_IN(LEN); \
1589 (*kw)=STMT; \
1590 }
1591#define IF_KW_PREFIX(STR, LEN) \
1592 if (!strncmp(in->current, STR, LEN)) { \
1593 MOVE_IN(LEN);
1594#define IF_KW_PREFIX_END \
1595 }
David Sedlák572e7ab2019-06-04 16:01:58 +02001596
Michal Vasko63f3d842020-07-08 10:10:14 +02001597 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001598 enum ly_stmt result = LY_STMT_NONE;
1599 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001600 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001601 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001602 case 'a':
Radek Krejcid54412f2020-12-17 20:25:35 +01001603 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001604 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1605 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1606 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001607 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001608 IF_KW("data", 4, LY_STMT_ANYDATA)
1609 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001610 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001611 break;
1612 case 'b':
Radek Krejcid54412f2020-12-17 20:25:35 +01001613 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001614 IF_KW("ase", 3, LY_STMT_BASE)
1615 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1616 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001617 break;
1618 case 'c':
Radek Krejcid54412f2020-12-17 20:25:35 +01001619 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001620 IF_KW("ase", 3, LY_STMT_CASE)
1621 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001622 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001623 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001624 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001625 IF_KW("ct", 2, LY_STMT_CONTACT)
1626 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001627 IF_KW_PREFIX_END
1628 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001629 break;
1630 case 'd':
Radek Krejcid54412f2020-12-17 20:25:35 +01001631 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001632 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001633 IF_KW("fault", 5, LY_STMT_DEFAULT)
1634 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001635 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001636 IF_KW("e", 1, LY_STMT_DEVIATE)
1637 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001638 IF_KW_PREFIX_END
1639 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001640 break;
1641 case 'e':
Radek Krejcid54412f2020-12-17 20:25:35 +01001642 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001643 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001644 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001645 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1646 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001647 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001648 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001649 break;
1650 case 'f':
Radek Krejcid54412f2020-12-17 20:25:35 +01001651 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001652 IF_KW("eature", 6, LY_STMT_FEATURE)
1653 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001654 break;
1655 case 'g':
Radek Krejcid54412f2020-12-17 20:25:35 +01001656 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001657 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001658 break;
1659 case 'i':
Radek Krejcid54412f2020-12-17 20:25:35 +01001660 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001661 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1662 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1663 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001664 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001665 IF_KW("clude", 5, LY_STMT_INCLUDE)
1666 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001667 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001668 break;
1669 case 'k':
Radek Krejcid54412f2020-12-17 20:25:35 +01001670 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001671 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001672 break;
1673 case 'l':
Radek Krejcid54412f2020-12-17 20:25:35 +01001674 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001675 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001676 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1677 else IF_KW("af", 2, LY_STMT_LEAF)
1678 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001679 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001680 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001681 break;
1682 case 'm':
Radek Krejcid54412f2020-12-17 20:25:35 +01001683 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001684 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001685 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1686 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001687 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001688 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1689 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001690 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001691 IF_KW("ule", 3, LY_STMT_MODULE)
1692 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001693 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001694 break;
1695 case 'n':
Radek Krejcid54412f2020-12-17 20:25:35 +01001696 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001697 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1698 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001699 break;
1700 case 'o':
Radek Krejcid54412f2020-12-17 20:25:35 +01001701 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001702 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001703 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1704 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001705 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001706 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001707 break;
1708 case 'p':
Radek Krejcid54412f2020-12-17 20:25:35 +01001709 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001710 IF_KW("ath", 3, LY_STMT_PATH)
1711 else IF_KW("attern", 6, LY_STMT_PATTERN)
1712 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001713 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001714 IF_KW("fix", 3, LY_STMT_PREFIX)
1715 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001716 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001717 break;
1718 case 'r':
Radek Krejcid54412f2020-12-17 20:25:35 +01001719 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001720 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001721 else IF_KW_PREFIX("e", 1)
1722 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001723 IF_KW("erence", 6, LY_STMT_REFERENCE)
1724 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001725 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001726 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1727 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1728 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001729 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001730 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001731 break;
1732 case 's':
Radek Krejcid54412f2020-12-17 20:25:35 +01001733 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001734 IF_KW("tatus", 5, LY_STMT_STATUS)
1735 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001736 break;
1737 case 't':
Radek Krejcid54412f2020-12-17 20:25:35 +01001738 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001739 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1740 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001741 break;
1742 case 'u':
Radek Krejcid54412f2020-12-17 20:25:35 +01001743 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001744 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001745 IF_KW("que", 3, LY_STMT_UNIQUE)
1746 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001747 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001748 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001749 break;
1750 case 'v':
Radek Krejcid54412f2020-12-17 20:25:35 +01001751 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001752 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001753 break;
1754 case 'w':
Radek Krejcid54412f2020-12-17 20:25:35 +01001755 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001756 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001757 break;
1758 case 'y':
Radek Krejcid54412f2020-12-17 20:25:35 +01001759 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001760 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
1761 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001762 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001763 default:
Radek Krejcid54412f2020-12-17 20:25:35 +01001764 /* if indent is not NULL we are matching keyword from YANG data */
1765 if (indent) {
Michal Vasko63f3d842020-07-08 10:10:14 +02001766 if (in->current[0] == ';') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001767 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001768 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02001769 } else if (in->current[0] == '{') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001770 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001771 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001772 } else if (in->current[0] == '}') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001773 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001774 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001775 }
1776 }
David Sedlák23a59a62018-10-26 13:08:02 +02001777 break;
1778 }
1779
Michal Vasko63f3d842020-07-08 10:10:14 +02001780 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02001781 /* the keyword is not terminated */
1782 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001783 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02001784 }
1785
David Sedlák1bccdfa2019-06-17 15:55:27 +02001786#undef IF_KW
1787#undef IF_KW_PREFIX
1788#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01001789#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02001790 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01001791
David Sedlák1bccdfa2019-06-17 15:55:27 +02001792 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02001793}
David Sedlákecf5eb82019-06-03 14:12:44 +02001794
Radek Krejci85ac8312021-03-03 20:21:33 +01001795LY_ERR
1796lysp_ext_find_definition(const struct ly_ctx *ctx, const struct lysp_ext_instance *ext, const struct lys_module **ext_mod,
1797 struct lysp_ext **ext_def)
1798{
Radek Krejci85ac8312021-03-03 20:21:33 +01001799 const char *tmp, *name, *prefix;
1800 size_t pref_len, name_len;
1801 LY_ARRAY_COUNT_TYPE v;
1802 const struct lys_module *mod = NULL;
1803
Radek Krejcicbb62422021-03-15 09:29:21 +01001804 assert(ext_def);
1805
Radek Krejci85ac8312021-03-03 20:21:33 +01001806 *ext_def = NULL;
1807 if (ext_mod) {
1808 *ext_mod = NULL;
1809 }
1810
Radek Krejci677abea2021-03-05 14:24:53 +01001811 /* parse the prefix, the nodeid was previously already parsed and checked */
Radek Krejci85ac8312021-03-03 20:21:33 +01001812 tmp = ext->name;
Radek Krejci677abea2021-03-05 14:24:53 +01001813 ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len);
Radek Krejci85ac8312021-03-03 20:21:33 +01001814
1815 /* get module where the extension definition should be placed */
1816 mod = ly_resolve_prefix(ctx, prefix, pref_len, ext->format, ext->prefix_data);
1817 if (!mod) {
Radek Krejci422afb12021-03-04 16:38:16 +01001818 LOGVAL(ctx, LYVE_REFERENCE, "Invalid prefix \"%.*s\" used for extension instance identifier.", (int)pref_len, prefix);
Radek Krejci85ac8312021-03-03 20:21:33 +01001819 return LY_EVALID;
1820 } else if (!mod->parsed->extensions) {
1821 LOGVAL(ctx, LYVE_REFERENCE, "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
1822 ext->name, mod->name);
1823 return LY_EVALID;
1824 }
1825
1826 /* find the parsed extension definition there */
1827 LY_ARRAY_FOR(mod->parsed->extensions, v) {
1828 if (!strcmp(name, mod->parsed->extensions[v].name)) {
1829 *ext_def = &mod->parsed->extensions[v];
1830 break;
1831 }
1832 }
1833
Radek Krejcicbb62422021-03-15 09:29:21 +01001834 if (!(*ext_def)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001835 LOGVAL(ctx, LYVE_REFERENCE, "Extension definition of extension instance \"%s\" not found.", ext->name);
1836 return LY_EVALID;
1837 }
1838
1839 if (ext_mod) {
1840 *ext_mod = mod;
1841 }
1842 return LY_SUCCESS;
1843}
1844
1845LY_ERR
1846lysp_ext_instance_resolve_argument(struct ly_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysp_ext *ext_def)
1847{
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001848 if (!ext_def->argname || ext_p->argument) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001849 /* nothing to do */
1850 return LY_SUCCESS;
1851 }
1852
Radek Krejci8df109d2021-04-23 12:19:08 +02001853 if (ext_p->format == LY_VALUE_XML) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001854 /* Schema was parsed from YIN and an argument is expected, ... */
1855 struct lysp_stmt *stmt = NULL;
1856
1857 if (ext_def->flags & LYS_YINELEM_TRUE) {
1858 /* ... argument was the first XML child element */
1859 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {}
1860 if (stmt) {
1861 const char *arg, *ext, *name_arg, *name_ext, *prefix_arg, *prefix_ext;
1862 size_t name_arg_len, name_ext_len, prefix_arg_len, prefix_ext_len;
1863
1864 stmt = ext_p->child;
1865
1866 arg = stmt->stmt;
1867 ly_parse_nodeid(&arg, &prefix_arg, &prefix_arg_len, &name_arg, &name_arg_len);
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001868 if (ly_strncmp(ext_def->argname, name_arg, name_arg_len)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001869 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" expects argument element \"%s\" as its first XML child, "
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001870 "but \"%.*s\" element found.", ext_p->name, ext_def->argname, (int)name_arg_len, name_arg);
Radek Krejci85ac8312021-03-03 20:21:33 +01001871 return LY_EVALID;
1872 }
1873
1874 /* check namespace - all the extension instances must be qualified and argument element is expected in the same
1875 * namespace. Do not check just prefixes, there can be different prefixes pointing to the same namespace */
1876 ext = ext_p->name; /* include prefix */
1877 ly_parse_nodeid(&ext, &prefix_ext, &prefix_ext_len, &name_ext, &name_ext_len);
1878
1879 if (ly_resolve_prefix(ctx, prefix_ext, prefix_ext_len, ext_p->format, ext_p->prefix_data) !=
1880 ly_resolve_prefix(ctx, prefix_arg, prefix_arg_len, stmt->format, stmt->prefix_data)) {
1881 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" element and its argument element \"%s\" are "
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001882 "expected in the same namespace, but they differ.", ext_p->name, ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01001883 return LY_EVALID;
1884 }
1885 }
1886 } else {
1887 /* ... argument was one of the XML attributes which are represented as child stmt
1888 * with LYS_YIN_ATTR flag */
1889 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001890 if (!strcmp(stmt->stmt, ext_def->argname)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001891 /* this is the extension's argument */
1892 break;
1893 }
1894 }
1895 }
1896
1897 if (stmt) {
1898 LY_CHECK_RET(lydict_insert(ctx, stmt->arg, 0, &ext_p->argument));
1899 stmt->flags |= LYS_YIN_ARGUMENT;
1900 }
1901 }
1902
1903 if (!ext_p->argument) {
1904 /* missing extension's argument */
1905 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" misses argument %s\"%s\".",
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001906 ext_p->name, (ext_def->flags & LYS_YINELEM_TRUE) ? "element " : "", ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01001907 return LY_EVALID;
1908 }
1909
1910 return LY_SUCCESS;
1911}
1912
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001913LY_ARRAY_COUNT_TYPE
Radek Krejcifc596f92021-02-26 22:40:26 +01001914lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, enum ly_stmt substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001915{
1916 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1917
Michal Vaskod989ba02020-08-24 10:59:24 +02001918 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejciab430862021-03-02 20:13:40 +01001919 if (ext[index].parent_stmt == substmt) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001920 return index;
1921 }
1922 }
1923
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001924 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001925}
1926
Michal Vasko62ed12d2020-05-21 10:08:25 +02001927const struct lysc_node *
Michal Vasko72244882021-01-12 15:21:05 +01001928lysc_data_node(const struct lysc_node *schema)
Michal Vasko62ed12d2020-05-21 10:08:25 +02001929{
1930 const struct lysc_node *parent;
1931
Michal Vasko72244882021-01-12 15:21:05 +01001932 parent = schema;
Radek Krejcidf549132021-01-21 10:32:32 +01001933 while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC |
1934 LYS_ACTION | LYS_NOTIF))) {
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001935 parent = parent->parent;
Michal Vasko72244882021-01-12 15:21:05 +01001936 }
Michal Vasko62ed12d2020-05-21 10:08:25 +02001937
1938 return parent;
1939}
Michal Vaskof4258e12021-06-15 12:11:42 +02001940
1941ly_bool
1942lys_has_recompiled(const struct lys_module *mod)
1943{
1944 LY_ARRAY_COUNT_TYPE u;
1945
1946 if (LYSP_HAS_RECOMPILED(mod->parsed)) {
1947 return 1;
1948 }
1949
1950 LY_ARRAY_FOR(mod->parsed->includes, u) {
1951 if (LYSP_HAS_RECOMPILED(mod->parsed->includes[u].submodule)) {
1952 return 1;
1953 }
1954 }
1955
1956 return 0;
1957}
1958
1959ly_bool
1960lys_has_compiled(const struct lys_module *mod)
1961{
1962 LY_ARRAY_COUNT_TYPE u;
1963
1964 if (LYSP_HAS_COMPILED(mod->parsed)) {
1965 return 1;
1966 }
1967
1968 LY_ARRAY_FOR(mod->parsed->includes, u) {
1969 if (LYSP_HAS_COMPILED(mod->parsed->includes[u].submodule)) {
1970 return 1;
1971 }
1972 }
1973
1974 return 0;
1975}
Michal Vasko7ee5be22021-06-16 17:03:34 +02001976
1977ly_bool
1978lys_has_groupings(const struct lys_module *mod)
1979{
1980 LY_ARRAY_COUNT_TYPE u;
1981
1982 if (mod->parsed->groupings) {
1983 return 1;
1984 }
1985
1986 LY_ARRAY_FOR(mod->parsed->includes, u) {
1987 if (mod->parsed->includes[u].submodule->groupings) {
1988 return 1;
1989 }
1990 }
1991
1992 return 0;
1993}