blob: 14572ceda21ad6c55b12ad1bbf77f67224a6d156 [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{
330 struct lysp_node *parent;
331 uint32_t hash;
332 size_t name_len;
333 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200334 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100335 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100336
337 assert(ctx);
338 assert(tpdf);
339
340 name = tpdf->name;
341 name_len = strlen(name);
342
Radek Krejci4f28eda2018-11-12 11:46:16 +0100343 if (lysp_type_str2builtin(name, name_len)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100344 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 +0100345 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100346 }
347
348 /* check locally scoped typedefs (avoid name shadowing) */
349 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100350 typedefs = lysp_node_typedefs(node);
351 LY_ARRAY_FOR(typedefs, u) {
352 if (&typedefs[u] == tpdf) {
353 break;
354 }
355 if (!strcmp(name, typedefs[u].name)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100356 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with sibling type.", name);
Radek Krejci0fb28562018-12-13 15:17:37 +0100357 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100358 }
359 }
360 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200361 for (parent = node->parent; parent; parent = parent->parent) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100362 if (lysp_type_match(name, parent)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100363 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 +0100364 return LY_EEXIST;
365 }
366 }
367 }
368
369 /* check collision with the top-level typedefs */
370 hash = dict_hash(name, name_len);
371 if (node) {
372 lyht_insert(tpdfs_scoped, &name, hash, NULL);
373 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100374 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 +0100375 return LY_EEXIST;
376 }
377 } else {
378 if (lyht_insert(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100379 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 +0100380 return LY_EEXIST;
381 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100382 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
383 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
384 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100385 }
386
387 return LY_SUCCESS;
388}
389
Radek Krejci857189e2020-09-01 13:26:36 +0200390/**
391 * @brief Compare identifiers.
Michal Vasko62524a92021-02-26 10:08:50 +0100392 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200393 */
394static ly_bool
395lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Radek Krejcibbe09a92018-11-08 09:36:54 +0100396{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200397 return strcmp(val1, val2) == 0 ? 1 : 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100398}
399
400LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100401lysp_check_dup_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100402{
403 struct hash_table *ids_global;
404 struct hash_table *ids_scoped;
Radek Krejci0fb28562018-12-13 15:17:37 +0100405 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200406 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200407 uint32_t i;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100408 LY_ERR ret = LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100409
410 /* check name collisions - typedefs and groupings */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100411 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
412 ids_scoped = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200413 LY_ARRAY_FOR(mod->typedefs, v) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100414 ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global, ids_scoped);
415 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100416 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200417 LY_ARRAY_FOR(mod->includes, v) {
418 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100419 ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global, ids_scoped);
420 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci3b1f9292018-11-08 10:58:35 +0100421 }
422 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200423 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
424 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
425 LY_ARRAY_FOR(typedefs, u) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100426 ret = lysp_check_dup_typedef(ctx, (struct lysp_node *)ctx->tpdfs_nodes.objs[i], &typedefs[u], ids_global, ids_scoped);
427 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100428 }
429 }
Michal Vasko405cc9e2020-12-01 12:01:27 +0100430
Radek Krejcibbe09a92018-11-08 09:36:54 +0100431cleanup:
432 lyht_free(ids_global);
433 lyht_free(ids_scoped);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100434 return ret;
435}
436
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100437static ly_bool
438ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
439{
440 void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
441
442 return ptr1 == ptr2 ? 1 : 0;
443}
444
445LY_ERR
446lysp_check_dup_features(struct lys_parser_ctx *ctx, struct lysp_module *mod)
447{
448 LY_ARRAY_COUNT_TYPE u;
449 struct hash_table *ht;
450 struct lysp_feature *f;
451 uint32_t hash;
452 LY_ERR ret = LY_SUCCESS, r;
453
454 ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
455 LY_CHECK_RET(!ht, LY_EMEM);
456
457 /* add all module features into a hash table */
458 LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
459 hash = dict_hash(f->name, strlen(f->name));
460 r = lyht_insert(ht, &f->name, hash, NULL);
461 if (r == LY_EEXIST) {
462 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
463 ret = LY_EVALID;
464 goto cleanup;
465 } else if (r) {
466 ret = r;
467 goto cleanup;
468 }
469 }
470
471 /* add all submodule features into a hash table */
472 LY_ARRAY_FOR(mod->includes, u) {
473 LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
474 hash = dict_hash(f->name, strlen(f->name));
475 r = lyht_insert(ht, &f->name, hash, NULL);
476 if (r == LY_EEXIST) {
477 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
478 ret = LY_EVALID;
479 goto cleanup;
480 } else if (r) {
481 ret = r;
482 goto cleanup;
483 }
484 }
485 }
486
487cleanup:
488 lyht_free(ht);
489 return ret;
490}
491
492LY_ERR
493lysp_check_dup_identities(struct lys_parser_ctx *ctx, struct lysp_module *mod)
494{
495 LY_ARRAY_COUNT_TYPE u;
496 struct hash_table *ht;
497 struct lysp_ident *i;
498 uint32_t hash;
499 LY_ERR ret = LY_SUCCESS, r;
500
501 ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
502 LY_CHECK_RET(!ht, LY_EMEM);
503
504 /* add all module identities into a hash table */
505 LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
506 hash = dict_hash(i->name, strlen(i->name));
507 r = lyht_insert(ht, &i->name, hash, NULL);
508 if (r == LY_EEXIST) {
509 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
510 ret = LY_EVALID;
511 goto cleanup;
512 } else if (r) {
513 ret = r;
514 goto cleanup;
515 }
516 }
517
518 /* add all submodule identities into a hash table */
519 LY_ARRAY_FOR(mod->includes, u) {
520 LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
521 hash = dict_hash(i->name, strlen(i->name));
522 r = lyht_insert(ht, &i->name, hash, NULL);
523 if (r == LY_EEXIST) {
524 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
525 ret = LY_EVALID;
526 goto cleanup;
527 } else if (r) {
528 ret = r;
529 goto cleanup;
530 }
531 }
532 }
533
534cleanup:
535 lyht_free(ht);
536 return ret;
537}
538
Radek Krejci9ed7a192018-10-31 16:23:51 +0100539struct lysp_load_module_check_data {
540 const char *name;
541 const char *revision;
542 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200543 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100544};
545
546static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100547lysp_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 +0100548{
549 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100550 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200551 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100552 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100553 struct lysp_revision *revs;
554
555 name = mod ? mod->mod->name : submod->name;
556 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200557 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100558
559 if (info->name) {
560 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100561 if (strcmp(info->name, name)) {
562 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100563 return LY_EINVAL;
564 }
565 }
566 if (info->revision) {
567 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100568 if (!revs || strcmp(info->revision, revs[0].date)) {
569 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200570 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100571 return LY_EINVAL;
572 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200573 } else if (!latest_revision) {
574 /* do not log, we just need to drop the schema and use the latest revision from the context */
575 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100576 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100577 if (submod) {
578 assert(info->submoduleof);
579
Radek Krejci9ed7a192018-10-31 16:23:51 +0100580 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200581 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100582 LOGVAL(ctx, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200583 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100584 return LY_EVALID;
585 }
586 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100587 if (submod->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100588 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100589 return LY_EVALID;
590 }
591 }
592 if (info->path) {
593 /* check that name and revision match filename */
594 filename = strrchr(info->path, '/');
595 if (!filename) {
596 filename = info->path;
597 } else {
598 filename++;
599 }
600 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100601 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100602 rev = strchr(filename, '@');
603 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100604 if (strncmp(filename, name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +0200605 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100606 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100607 }
608 /* revision */
609 if (rev) {
610 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100611 if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100612 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +0200613 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100614 }
615 }
616 }
617 return LY_SUCCESS;
618}
619
Michal Vaskoe7a1daf2021-04-19 18:04:27 +0200620/**
621 * @brief Load the (sub)module into the context.
622 *
623 * This function does not check the presence of the (sub)module in context, it should be done before calling this function.
624 *
625 * module_name and submodule_name are alternatives - only one of the
626 *
627 * @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.
630 * @param[in] features Array of enabled features ended with NULL.
631 * @param[in] need_implemented Whether the (sub)module is needed implemented or not.
632 * @param[in] main_ctx Parser context of the main module in case of loading submodule.
633 * @param[in] main_name Main module name in case of loading submodule.
634 * @param[in] required Module is required so error (even if the input file not found) are important. If 0, there is some
635 * backup and it is actually ok if the input data are not found. However, parser reports errors even in this case.
636 * @param[in,out] unres Global unres structure for newly implemented modules.
637 * @param[out] result Parsed YANG schema tree of the requested module (struct lys_module*) or submodule (struct lysp_submodule*).
638 * If it is a module, it is already in the context!
639 * @return LY_ERR value, in case of LY_SUCCESS, the \arg result is always provided.
640 */
641static LY_ERR
Michal Vasko34e334d2021-01-25 16:12:31 +0100642lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, const char **features,
643 ly_bool need_implemented, struct lys_parser_ctx *main_ctx, const char *main_name, ly_bool required,
644 struct lys_glob_unres *unres, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100645{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200646 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100647 char *filepath = NULL;
648 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100649 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100650 LY_ERR ret = LY_SUCCESS;
651 struct lysp_load_module_check_data check_data = {0};
652
653 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 +0200654 &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200655 if (!filepath) {
656 if (required) {
657 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200658 revision ? revision : "");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200659 }
660 return LY_ENOTFOUND;
661 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100662
663 LOGVRB("Loading schema from \"%s\" file.", filepath);
664
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200665 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200666 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +0200667 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100668 check_data.name = name;
669 check_data.revision = revision;
670 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800671 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200672 if (main_ctx) {
Michal Vasko7a0b0762020-09-02 16:37:01 +0200673 ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data,
Michal Vasko69730152020-10-09 16:30:07 +0200674 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200675 } else {
Michal Vasko34e334d2021-01-25 16:12:31 +0100676 ret = lys_create_module(ctx, in, format, need_implemented, lysp_load_module_check, &check_data, features, unres,
Michal Vasko69730152020-10-09 16:30:07 +0200677 (struct lys_module **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200678
679 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200680 ly_in_free(in, 1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200681 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100682
683 *result = mod;
684
685 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200686
Radek Krejci9ed7a192018-10-31 16:23:51 +0100687cleanup:
688 free(filepath);
689 return ret;
690}
691
Radek Krejcid33273d2018-10-25 14:55:52 +0200692LY_ERR
Michal Vasko18a86e52021-04-16 11:50:13 +0200693lys_load_module(struct ly_ctx *ctx, const char *name, const char *revision, ly_bool need_implemented,
Michal Vasko34e334d2021-01-25 16:12:31 +0100694 const char **features, struct lys_glob_unres *unres, struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200695{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100696 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200697 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200698
Radek Krejci9ed7a192018-10-31 16:23:51 +0100699 void (*module_data_free)(void *module_data, void *user_data) = NULL;
700 struct lysp_load_module_check_data check_data = {0};
Michal Vasko0550b762020-11-24 18:04:08 +0100701 struct lys_module *ctx_latest = NULL, *m;
Michal Vasko63f3d842020-07-08 10:10:14 +0200702 struct ly_in *in;
Michal Vasko0550b762020-11-24 18:04:08 +0100703 LY_ERR ret;
Michal Vasko34e334d2021-01-25 16:12:31 +0100704 ly_bool implement;
Radek Krejci086c7132018-10-26 15:29:04 +0200705
Michal Vasko405cc9e2020-12-01 12:01:27 +0100706 assert(mod && unres);
Radek Krejci0af46292019-01-11 16:02:31 +0100707
Michal Vasko25d6ad02020-10-22 12:20:22 +0200708 if (ctx->flags & LY_CTX_ALL_IMPLEMENTED) {
Radek Krejcia53d7c92020-08-21 11:30:56 +0200709 implement = 1;
Michal Vasko34e334d2021-01-25 16:12:31 +0100710 } else {
711 implement = need_implemented;
Radek Krejcia53d7c92020-08-21 11:30:56 +0200712 }
713
Michal Vasko0550b762020-11-24 18:04:08 +0100714 /*
715 * try to get the module from the context
716 */
Radek Krejci0af46292019-01-11 16:02:31 +0100717 if (!*mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100718 if (revision) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200719 /* get the specific revision */
Michal Vasko22df3f02020-08-24 13:29:22 +0200720 *mod = (struct lys_module *)ly_ctx_get_module(ctx, name, revision);
Radek Krejci0af46292019-01-11 16:02:31 +0100721 } else {
Michal Vasko0550b762020-11-24 18:04:08 +0100722 if (implement) {
723 /* prefer the implemented module instead of the latest one */
724 *mod = (struct lys_module *)ly_ctx_get_module_implemented(ctx, name);
725 }
726 if (!*mod) {
727 /* get the requested module of the latest revision in the context */
728 *mod = (struct lys_module *)ly_ctx_get_module_latest(ctx, name);
729 if (*mod && ((*mod)->latest_revision == 1)) {
730 /* let us now search with callback and searchpaths to check if there is newer revision outside the context */
731 ctx_latest = *mod;
732 *mod = NULL;
733 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200734 }
Radek Krejci0af46292019-01-11 16:02:31 +0100735 }
Radek Krejci086c7132018-10-26 15:29:04 +0200736 }
737
Michal Vasko0550b762020-11-24 18:04:08 +0100738 /* check collision with other implemented revision */
739 if (implement) {
740 m = ly_ctx_get_module_implemented(ctx, name);
741 if (m && (!*mod || (*mod && (m != *mod)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100742 LOGVAL(ctx, LYVE_REFERENCE, "Module \"%s\" is already present in other implemented revision.", name);
Michal Vasko0550b762020-11-24 18:04:08 +0100743 *mod = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200744 return LY_EDENIED;
745 }
Michal Vasko0550b762020-11-24 18:04:08 +0100746 }
Radek Krejci086c7132018-10-26 15:29:04 +0200747
Michal Vasko0550b762020-11-24 18:04:08 +0100748 /*
749 * no suitable module in the context, try to load it
750 */
751 if (!*mod) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100752 /* module not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200753 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
754search_clb:
755 if (ctx->imp_clb) {
756 if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
Michal Vasko69730152020-10-09 16:30:07 +0200757 &format, &module_data, &module_data_free) == LY_SUCCESS) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200758 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
Radek Krejci9ed7a192018-10-31 16:23:51 +0100759 check_data.name = name;
760 check_data.revision = revision;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100761 lys_create_module(ctx, in, format, implement, lysp_load_module_check, &check_data, features, unres, mod);
Michal Vasko63f3d842020-07-08 10:10:14 +0200762 ly_in_free(in, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100763 if (module_data_free) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200764 module_data_free((void *)module_data, ctx->imp_clb_data);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100765 }
Radek Krejci086c7132018-10-26 15:29:04 +0200766 }
767 }
768 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
769 goto search_file;
770 }
771 } else {
772search_file:
773 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
774 /* module was not received from the callback or there is no callback set */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100775 lys_module_localfile(ctx, name, revision, features, implement, NULL, NULL, ctx_latest ? 0 : 1, unres,
776 (void **)mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200777 }
Michal Vasko0550b762020-11-24 18:04:08 +0100778 if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejci086c7132018-10-26 15:29:04 +0200779 goto search_clb;
780 }
781 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100782
Radek Krejcib3289d62019-09-18 12:21:39 +0200783 /* update the latest_revision flag - here we have selected the latest available schema,
784 * consider that even the callback provides correct latest revision */
Michal Vasko0550b762020-11-24 18:04:08 +0100785 if (!*mod && ctx_latest) {
Michal Vasko003c44a2021-02-24 17:13:11 +0100786 LOGVRB("Newer revision than \"%s@%s\" not found, using this as the latest revision.", ctx_latest->name,
Michal Vasko0550b762020-11-24 18:04:08 +0100787 ctx_latest->revision);
788 ctx_latest->latest_revision = 2;
789 *mod = ctx_latest;
790 } else if (*mod && !revision && ((*mod)->latest_revision == 1)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100791 (*mod)->latest_revision = 2;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100792 }
Radek Krejci086c7132018-10-26 15:29:04 +0200793
Michal Vasko0550b762020-11-24 18:04:08 +0100794 if (!*mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100795 LOGVAL(ctx, LYVE_REFERENCE, "%s \"%s\" module failed.", implement ? "Loading" : "Importing", name);
Michal Vasko0550b762020-11-24 18:04:08 +0100796 return LY_EVALID;
797 }
798 } else {
799 /* we have module from the current context, circular check */
800 if ((*mod)->parsed->parsing) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100801 LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
Radek Krejci086c7132018-10-26 15:29:04 +0200802 *mod = NULL;
803 return LY_EVALID;
804 }
805 }
Radek Krejci086c7132018-10-26 15:29:04 +0200806
Michal Vasko0550b762020-11-24 18:04:08 +0100807 /*
808 * module found, make sure it is implemented if should be
809 */
Michal Vasko962b6cd2020-12-08 10:07:49 +0100810 if (implement) {
Radek Krejci2415f882021-01-20 16:27:09 +0100811 if (!(*mod)->implemented) {
812 /* implement */
813 ret = lys_set_implemented_r(*mod, features, unres);
814 if (ret) {
815 *mod = NULL;
816 return ret;
817 }
818 } else if (features) {
Michal Vasko962b6cd2020-12-08 10:07:49 +0100819 /* set features if different */
820 ret = lys_set_features((*mod)->parsed, features);
821 if (!ret) {
822 /* context need to be recompiled so that feature changes are properly applied */
823 unres->recompile = 1;
824 } else if (ret != LY_EEXIST) {
825 /* error */
826 return ret;
827 } /* else no feature changes */
Michal Vaskoc5d64862020-11-24 18:04:45 +0100828 }
Radek Krejci086c7132018-10-26 15:29:04 +0200829 }
Radek Krejci086c7132018-10-26 15:29:04 +0200830
831 return LY_SUCCESS;
832}
833
834LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200835lysp_check_stringchar(struct lys_parser_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +0200836{
837 if (!is_yangutf8char(c)) {
838 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
839 return LY_EVALID;
840 }
841 return LY_SUCCESS;
842}
843
844LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200845lysp_check_identifierchar(struct lys_parser_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +0200846{
Michal Vasko69730152020-10-09 16:30:07 +0200847 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +0200848 if (!is_yangidentstartchar(c)) {
aPiecekc89b2242021-05-14 14:19:11 +0200849 if ((c < UCHAR_MAX) && isprint(c)) {
Michal Vasko7c769042021-03-25 12:20:49 +0100850 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
851 } else {
852 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character 0x%04x.", c);
853 }
David Sedlák4a650532019-07-10 11:55:18 +0200854 return LY_EVALID;
855 }
856 if (prefix) {
857 if (first) {
858 (*prefix) = 0;
859 } else {
860 (*prefix) = 2;
861 }
862 }
Michal Vasko69730152020-10-09 16:30:07 +0200863 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +0200864 (*prefix) = 1;
865 } else if (!is_yangidentchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200866 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200867 return LY_EVALID;
868 }
869
870 return LY_SUCCESS;
871}
872
Radek Krejci771928a2021-01-19 13:42:36 +0100873/**
874 * @brief Try to find the parsed submodule in main module for the given include record.
875 *
876 * @param[in] pctx main parser context
877 * @param[in] inc The include record with missing parsed submodule. According to include info try to find
878 * the corresponding parsed submodule in main module's includes.
879 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
880 * @return LY_ENOT - the parsed module was not found.
881 * @return LY_EVALID - YANG rule violation
882 */
883static LY_ERR
884lysp_get_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200885{
Radek Krejci771928a2021-01-19 13:42:36 +0100886 LY_ARRAY_COUNT_TYPE i;
887 struct lysp_module *main_pmod = pctx->parsed_mod->mod->parsed;
Michal Vasko69730152020-10-09 16:30:07 +0200888
Radek Krejci771928a2021-01-19 13:42:36 +0100889 LY_ARRAY_FOR(main_pmod->includes, i) {
890 if (strcmp(main_pmod->includes[i].name, inc->name)) {
891 continue;
892 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200893
Radek Krejci771928a2021-01-19 13:42:36 +0100894 if (inc->rev[0] && strncmp(inc->rev, main_pmod->includes[i].rev, LY_REV_SIZE)) {
895 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
896 "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
897 ((struct lysp_submodule *)pctx->parsed_mod)->name, inc->rev,
898 main_pmod->includes[i].name, main_pmod->includes[i].rev, main_pmod->mod->name);
899 return LY_EVALID;
900 }
901
902 inc->submodule = main_pmod->includes[i].submodule;
903 return inc->submodule ? LY_SUCCESS : LY_ENOT;
904 }
905
906 if (main_pmod->version == LYS_VERSION_1_1) {
907 LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
908 "YANG 1.1 requires all submodules to be included from main module. "
909 "But submodule \"%s\" includes submodule \"%s\" which is not included by main module \"%s\".",
910 ((struct lysp_submodule *)pctx->parsed_mod)->name, inc->name, main_pmod->mod->name);
911 return LY_EVALID;
912 } else {
913 return LY_ENOT;
914 }
915}
916
917/**
918 * @brief Make the copy of the given include record into the main module.
919 *
920 * YANG 1.0 does not require the main module to include all the submodules. Therefore, parsing submodules can cause
921 * reallocating and extending the includes array in the main module by the submodules included only in submodules.
922 *
923 * @param[in] pctx main parser context
924 * @param[in] inc Include record to copy into main module taken from @p pctx.
925 * @return LY_ERR value.
926 */
927static LY_ERR
928lysp_inject_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
929{
930 LY_ARRAY_COUNT_TYPE i;
931 struct lysp_include *inc_new, *inc_tofill = NULL;
932 struct lysp_module *main_pmod = pctx->parsed_mod->mod->parsed;
933
934 /* first, try to find the corresponding record with missing parsed submodule */
935 LY_ARRAY_FOR(main_pmod->includes, i) {
936 if (strcmp(main_pmod->includes[i].name, inc->name)) {
937 continue;
938 }
939 inc_tofill = &main_pmod->includes[i];
940 break;
941 }
942
943 if (inc_tofill) {
944 inc_tofill->submodule = inc->submodule;
945 } else {
946 LY_ARRAY_NEW_RET(PARSER_CTX(pctx), main_pmod->includes, inc_new, LY_EMEM);
947
948 inc_new->submodule = inc->submodule;
949 DUP_STRING_RET(PARSER_CTX(pctx), inc->name, inc_new->name);
950 DUP_STRING_RET(PARSER_CTX(pctx), inc->dsc, inc_new->dsc);
951 DUP_STRING_RET(PARSER_CTX(pctx), inc->ref, inc_new->ref);
952 /* TODO duplicate extensions */
953 memcpy(inc_new->rev, inc->rev, LY_REV_SIZE);
954 inc_new->injected = 1;
955 }
956 return LY_SUCCESS;
957}
958
959LY_ERR
960lysp_load_submodules(struct lys_parser_ctx *pctx, struct lysp_module *pmod)
961{
962 LY_ARRAY_COUNT_TYPE u;
963 struct ly_ctx *ctx = PARSER_CTX(pctx);
964
965 LY_ARRAY_FOR(pmod->includes, u) {
966 LY_ERR ret = LY_SUCCESS;
967 struct lysp_submodule *submod = NULL;
968 struct lysp_include *inc = &pmod->includes[u];
969
970 if (inc->submodule) {
971 continue;
972 }
973
974 if (pmod->is_submod) {
975 /* try to find the submodule in the main module or its submodules */
976 ret = lysp_get_submodule(pctx, inc);
977 LY_CHECK_RET(ret && ret != LY_ENOT, ret);
978 LY_CHECK_RET(ret == LY_SUCCESS, LY_SUCCESS); /* submodule found in linked with the inc */
979 }
980
981 /* submodule not present in the main module, get the input data and parse it */
982 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcidf549132021-01-21 10:32:32 +0100983search_clb:
Radek Krejci771928a2021-01-19 13:42:36 +0100984 if (ctx->imp_clb) {
985 const char *submodule_data = NULL;
986 LYS_INFORMAT format = LYS_IN_UNKNOWN;
987 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
988 struct lysp_load_module_check_data check_data = {0};
989 struct ly_in *in;
990
991 if (ctx->imp_clb(pctx->parsed_mod->mod->name, NULL, inc->name,
992 inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
993 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
994 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
995 check_data.name = inc->name;
996 check_data.revision = inc->rev[0] ? inc->rev : NULL;
997 check_data.submoduleof = pctx->parsed_mod->mod->name;
998 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, &submod);
999
1000 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1001 * submodule's include into main module, where it is missing */
1002 inc = &pmod->includes[u];
1003
1004 ly_in_free(in, 0);
1005 if (submodule_data_free) {
1006 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
1007 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001008 }
1009 }
Radek Krejci771928a2021-01-19 13:42:36 +01001010 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1011 goto search_file;
1012 }
1013 } else {
Radek Krejcidf549132021-01-21 10:32:32 +01001014search_file:
Radek Krejci771928a2021-01-19 13:42:36 +01001015 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
1016 /* submodule was not received from the callback or there is no callback set */
1017 lys_module_localfile(ctx, inc->name,
1018 inc->rev[0] ? inc->rev : NULL, NULL, 0, pctx,
1019 pctx->parsed_mod->mod->name, 1, NULL, (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001020
Radek Krejci771928a2021-01-19 13:42:36 +01001021 /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1022 * submodule's include into main module, where it is missing */
1023 inc = &pmod->includes[u];
1024 }
1025 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1026 goto search_clb;
1027 }
1028 }
1029 if (submod) {
1030 if (!inc->rev[0] && (submod->latest_revision == 1)) {
1031 /* update the latest_revision flag - here we have selected the latest available schema,
1032 * consider that even the callback provides correct latest revision */
1033 submod->latest_revision = 2;
1034 }
1035
1036 inc->submodule = submod;
1037 if (ret == LY_ENOT) {
1038 /* the submodule include is not present in YANG 1.0 main module - add it there */
1039 LY_CHECK_RET(lysp_inject_submodule(pctx, &pmod->includes[u]));
1040 }
1041 }
1042 if (!inc->submodule) {
1043 LOGVAL(ctx, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name,
1044 pctx->parsed_mod->is_submod ? ((struct lysp_submodule *)pctx->parsed_mod)->name : pctx->parsed_mod->mod->name);
1045 return LY_EVALID;
1046 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001047 }
1048
1049 return LY_SUCCESS;
1050}
1051
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001052API const struct lysc_when *
1053lysc_has_when(const struct lysc_node *node)
1054{
Radek Krejci9a3823e2021-01-27 20:26:46 +01001055 struct lysc_when **when;
1056
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001057 if (!node) {
1058 return NULL;
1059 }
1060
1061 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001062 when = lysc_node_when(node);
1063 if (when) {
1064 return when[0];
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01001065 }
1066 node = node->parent;
1067 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1068
1069 return NULL;
1070}
1071
Radek Krejci0935f412019-08-20 16:15:18 +02001072API const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001073lys_nodetype2str(uint16_t nodetype)
1074{
Michal Vaskod989ba02020-08-24 10:59:24 +02001075 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001076 case LYS_CONTAINER:
1077 return "container";
1078 case LYS_CHOICE:
1079 return "choice";
1080 case LYS_LEAF:
1081 return "leaf";
1082 case LYS_LEAFLIST:
1083 return "leaf-list";
1084 case LYS_LIST:
1085 return "list";
1086 case LYS_ANYXML:
1087 return "anyxml";
1088 case LYS_ANYDATA:
1089 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001090 case LYS_CASE:
1091 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001092 case LYS_RPC:
1093 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001094 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001095 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001096 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001097 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001098 case LYS_USES:
1099 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001100 default:
1101 return "unknown";
1102 }
1103}
1104
Radek Krejci39b7fc22021-02-26 23:29:18 +01001105API enum ly_stmt
1106lys_nodetype2stmt(uint16_t nodetype)
1107{
1108 switch (nodetype) {
1109 case LYS_CONTAINER:
1110 return LY_STMT_CONTAINER;
1111 case LYS_CHOICE:
1112 return LY_STMT_CHOICE;
1113 case LYS_LEAF:
1114 return LY_STMT_LEAF;
1115 case LYS_LEAFLIST:
1116 return LY_STMT_LEAF_LIST;
1117 case LYS_LIST:
1118 return LY_STMT_LIST;
1119 case LYS_ANYXML:
1120 return LY_STMT_ANYXML;
1121 case LYS_ANYDATA:
1122 return LY_STMT_ANYDATA;
1123 case LYS_CASE:
1124 return LY_STMT_CASE;
1125 case LYS_RPC:
1126 return LY_STMT_RPC;
1127 case LYS_ACTION:
1128 return LY_STMT_ACTION;
1129 case LYS_NOTIF:
1130 return LY_STMT_NOTIFICATION;
1131 case LYS_USES:
1132 return LY_STMT_USES;
1133 case LYS_INPUT:
1134 return LY_STMT_INPUT;
1135 case LYS_OUTPUT:
1136 return LY_STMT_OUTPUT;
1137 default:
1138 return LY_STMT_NONE;
1139 }
1140}
1141
Radek Krejci693262f2019-04-29 15:23:20 +02001142const char *
1143lys_datatype2str(LY_DATA_TYPE basetype)
1144{
Michal Vaskod989ba02020-08-24 10:59:24 +02001145 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001146 case LY_TYPE_BINARY:
1147 return "binary";
1148 case LY_TYPE_UINT8:
1149 return "uint8";
1150 case LY_TYPE_UINT16:
1151 return "uint16";
1152 case LY_TYPE_UINT32:
1153 return "uint32";
1154 case LY_TYPE_UINT64:
1155 return "uint64";
1156 case LY_TYPE_STRING:
1157 return "string";
1158 case LY_TYPE_BITS:
1159 return "bits";
1160 case LY_TYPE_BOOL:
1161 return "boolean";
1162 case LY_TYPE_DEC64:
1163 return "decimal64";
1164 case LY_TYPE_EMPTY:
1165 return "empty";
1166 case LY_TYPE_ENUM:
1167 return "enumeration";
1168 case LY_TYPE_IDENT:
1169 return "identityref";
1170 case LY_TYPE_INST:
1171 return "instance-identifier";
1172 case LY_TYPE_LEAFREF:
1173 return "leafref";
1174 case LY_TYPE_UNION:
1175 return "union";
1176 case LY_TYPE_INT8:
1177 return "int8";
1178 case LY_TYPE_INT16:
1179 return "int16";
1180 case LY_TYPE_INT32:
1181 return "int32";
1182 case LY_TYPE_INT64:
1183 return "int64";
1184 default:
1185 return "unknown";
1186 }
1187}
1188
Radek Krejci056d0a82018-12-06 16:57:25 +01001189API const struct lysp_tpdf *
1190lysp_node_typedefs(const struct lysp_node *node)
1191{
Radek Krejci0fb28562018-12-13 15:17:37 +01001192 switch (node->nodetype) {
1193 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001194 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001195 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001196 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001197 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001198 return ((struct lysp_node_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001199 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001200 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001201 return ((struct lysp_node_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001202 case LYS_INPUT:
1203 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001204 return ((struct lysp_node_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001205 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001206 return ((struct lysp_node_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001207 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001208 return NULL;
1209 }
1210}
1211
Radek Krejci2a9fc652021-01-22 17:44:34 +01001212API const struct lysp_node_grp *
Radek Krejci53ea6152018-12-13 15:21:15 +01001213lysp_node_groupings(const struct lysp_node *node)
1214{
1215 switch (node->nodetype) {
1216 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001217 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001218 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001219 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001220 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001221 return ((struct lysp_node_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001222 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001223 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001224 return ((struct lysp_node_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001225 case LYS_INPUT:
1226 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001227 return ((struct lysp_node_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001228 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001229 return ((struct lysp_node_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001230 default:
1231 return NULL;
1232 }
1233}
1234
Radek Krejci2a9fc652021-01-22 17:44:34 +01001235struct lysp_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001236lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001237{
1238 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001239
Radek Krejcibbe09a92018-11-08 09:36:54 +01001240 switch (node->nodetype) {
1241 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001242 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001243 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001244 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001245 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001246 return &((struct lysp_node_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001247 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001248 return &((struct lysp_node_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001249 default:
1250 return NULL;
1251 }
1252}
1253
Radek Krejci2a9fc652021-01-22 17:44:34 +01001254API const struct lysp_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001255lysp_node_actions(const struct lysp_node *node)
1256{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001257 struct lysp_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001258
Michal Vasko22df3f02020-08-24 13:29:22 +02001259 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001260 if (actions) {
1261 return *actions;
1262 } else {
1263 return NULL;
1264 }
1265}
1266
Radek Krejci2a9fc652021-01-22 17:44:34 +01001267struct lysp_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001268lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001269{
1270 assert(node);
1271 switch (node->nodetype) {
1272 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001273 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001274 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001275 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001276 case LYS_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001277 return &((struct lysp_node_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001278 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001279 return &((struct lysp_node_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001280 default:
1281 return NULL;
1282 }
1283}
1284
Radek Krejci2a9fc652021-01-22 17:44:34 +01001285API const struct lysp_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001286lysp_node_notifs(const struct lysp_node *node)
1287{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001288 struct lysp_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001289
Michal Vasko22df3f02020-08-24 13:29:22 +02001290 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001291 if (notifs) {
1292 return *notifs;
1293 } else {
1294 return NULL;
1295 }
1296}
1297
Radek Krejcibbe09a92018-11-08 09:36:54 +01001298struct lysp_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001299lysp_node_child_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001300{
1301 assert(node);
1302 switch (node->nodetype) {
1303 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001304 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001305 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001306 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001307 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001308 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001309 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001310 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001311 case LYS_GROUPING:
Radek Krejci01180ac2021-01-27 08:48:22 +01001312 return &((struct lysp_node_grp *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001313 case LYS_AUGMENT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001314 return &((struct lysp_node_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001315 case LYS_INPUT:
1316 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001317 return &((struct lysp_node_action_inout *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001318 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001319 return &((struct lysp_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001320 default:
1321 return NULL;
1322 }
1323}
1324
Radek Krejci056d0a82018-12-06 16:57:25 +01001325API const struct lysp_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001326lysp_node_child(const struct lysp_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01001327{
Michal Vasko544e58a2021-01-28 14:33:41 +01001328 struct lysp_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001329
1330 if (!node) {
1331 return NULL;
1332 }
1333
Michal Vasko544e58a2021-01-28 14:33:41 +01001334 child = lysp_node_child_p((struct lysp_node *)node);
1335 if (child) {
1336 return *child;
Radek Krejci056d0a82018-12-06 16:57:25 +01001337 } else {
1338 return NULL;
1339 }
1340}
1341
Radek Krejci9a3823e2021-01-27 20:26:46 +01001342struct lysp_restr **
1343lysp_node_musts_p(const struct lysp_node *node)
1344{
1345 if (!node) {
1346 return NULL;
1347 }
1348
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001349 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001350 case LYS_CONTAINER:
1351 return &((struct lysp_node_container *)node)->musts;
1352 case LYS_LEAF:
1353 return &((struct lysp_node_leaf *)node)->musts;
1354 case LYS_LEAFLIST:
1355 return &((struct lysp_node_leaflist *)node)->musts;
1356 case LYS_LIST:
1357 return &((struct lysp_node_list *)node)->musts;
1358 case LYS_ANYXML:
1359 case LYS_ANYDATA:
1360 return &((struct lysp_node_anydata *)node)->musts;
1361 case LYS_NOTIF:
1362 return &((struct lysp_node_notif *)node)->musts;
1363 case LYS_INPUT:
1364 case LYS_OUTPUT:
1365 return &((struct lysp_node_action_inout *)node)->musts;
1366 default:
1367 return NULL;
1368 }
1369}
1370
1371struct lysp_restr *
1372lysp_node_musts(const struct lysp_node *node)
1373{
1374 struct lysp_restr **musts;
1375
1376 musts = lysp_node_musts_p(node);
1377 if (musts) {
1378 return *musts;
1379 } else {
1380 return NULL;
1381 }
1382}
1383
1384struct lysp_when **
1385lysp_node_when_p(const struct lysp_node *node)
1386{
1387 if (!node) {
1388 return NULL;
1389 }
1390
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001391 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001392 case LYS_CONTAINER:
1393 return &((struct lysp_node_container *)node)->when;
1394 case LYS_CHOICE:
1395 return &((struct lysp_node_choice *)node)->when;
1396 case LYS_LEAF:
1397 return &((struct lysp_node_leaf *)node)->when;
1398 case LYS_LEAFLIST:
1399 return &((struct lysp_node_leaflist *)node)->when;
1400 case LYS_LIST:
1401 return &((struct lysp_node_list *)node)->when;
1402 case LYS_ANYXML:
1403 case LYS_ANYDATA:
1404 return &((struct lysp_node_anydata *)node)->when;
1405 case LYS_CASE:
1406 return &((struct lysp_node_case *)node)->when;
1407 case LYS_USES:
1408 return &((struct lysp_node_uses *)node)->when;
1409 case LYS_AUGMENT:
1410 return &((struct lysp_node_augment *)node)->when;
1411 default:
1412 return NULL;
1413 }
1414}
1415
1416struct lysp_when *
1417lysp_node_when(const struct lysp_node *node)
1418{
1419 struct lysp_when **when;
1420
1421 when = lysp_node_when_p(node);
1422 if (when) {
1423 return *when;
1424 } else {
1425 return NULL;
1426 }
1427}
1428
Radek Krejci2a9fc652021-01-22 17:44:34 +01001429struct lysc_node_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001430lysc_node_actions_p(struct lysc_node *node)
1431{
1432 assert(node);
1433 switch (node->nodetype) {
1434 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001435 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001436 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001437 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001438 default:
1439 return NULL;
1440 }
1441}
1442
Radek Krejci2a9fc652021-01-22 17:44:34 +01001443API const struct lysc_node_action *
Radek Krejci056d0a82018-12-06 16:57:25 +01001444lysc_node_actions(const struct lysc_node *node)
1445{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001446 struct lysc_node_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001447
Michal Vasko22df3f02020-08-24 13:29:22 +02001448 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001449 if (actions) {
1450 return *actions;
1451 } else {
1452 return NULL;
1453 }
1454}
1455
Radek Krejci2a9fc652021-01-22 17:44:34 +01001456struct lysc_node_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001457lysc_node_notifs_p(struct lysc_node *node)
1458{
1459 assert(node);
1460 switch (node->nodetype) {
1461 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001462 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001463 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001464 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001465 default:
1466 return NULL;
1467 }
1468}
1469
Radek Krejci2a9fc652021-01-22 17:44:34 +01001470API const struct lysc_node_notif *
Radek Krejci056d0a82018-12-06 16:57:25 +01001471lysc_node_notifs(const struct lysc_node *node)
1472{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001473 struct lysc_node_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001474
Michal Vasko22df3f02020-08-24 13:29:22 +02001475 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001476 if (notifs) {
1477 return *notifs;
1478 } else {
1479 return NULL;
1480 }
1481}
1482
Radek Krejcibbe09a92018-11-08 09:36:54 +01001483struct lysc_node **
Michal Vasko544e58a2021-01-28 14:33:41 +01001484lysc_node_child_p(const struct lysc_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001485{
Michal Vasko544e58a2021-01-28 14:33:41 +01001486 assert(node && !(node->nodetype & (LYS_RPC | LYS_ACTION)));
1487
Radek Krejcibbe09a92018-11-08 09:36:54 +01001488 switch (node->nodetype) {
1489 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001490 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001491 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001492 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001493 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001494 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001495 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001496 return &((struct lysc_node_list *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001497 case LYS_INPUT:
1498 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001499 return &((struct lysc_node_action_inout *)node)->child;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001500 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001501 return &((struct lysc_node_notif *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001502 default:
1503 return NULL;
1504 }
1505}
1506
Radek Krejci056d0a82018-12-06 16:57:25 +01001507API const struct lysc_node *
Michal Vasko544e58a2021-01-28 14:33:41 +01001508lysc_node_child(const struct lysc_node *node)
Radek Krejcia3045382018-11-22 14:30:31 +01001509{
Michal Vasko544e58a2021-01-28 14:33:41 +01001510 struct lysc_node **child;
Radek Krejcie7b95092019-05-15 11:03:07 +02001511
1512 if (!node) {
1513 return NULL;
1514 }
1515
Michal Vasko544e58a2021-01-28 14:33:41 +01001516 if (node->nodetype & (LYS_RPC | LYS_ACTION)) {
1517 return &((struct lysc_node_action *)node)->input.node;
Radek Krejcibe154442021-01-21 11:06:36 +01001518 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01001519 child = lysc_node_child_p(node);
1520 if (child) {
1521 return *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001522 }
Michal Vasko2a668712020-10-21 11:48:09 +02001523 }
Michal Vasko544e58a2021-01-28 14:33:41 +01001524
1525 return NULL;
Michal Vasko2a668712020-10-21 11:48:09 +02001526}
1527
Radek Krejci9a3823e2021-01-27 20:26:46 +01001528struct lysc_must **
1529lysc_node_musts_p(const struct lysc_node *node)
1530{
1531 if (!node) {
1532 return NULL;
1533 }
1534
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001535 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001536 case LYS_CONTAINER:
1537 return &((struct lysc_node_container *)node)->musts;
1538 case LYS_LEAF:
1539 return &((struct lysc_node_leaf *)node)->musts;
1540 case LYS_LEAFLIST:
1541 return &((struct lysc_node_leaflist *)node)->musts;
1542 case LYS_LIST:
1543 return &((struct lysc_node_list *)node)->musts;
1544 case LYS_ANYXML:
1545 case LYS_ANYDATA:
1546 return &((struct lysc_node_anydata *)node)->musts;
1547 case LYS_NOTIF:
1548 return &((struct lysc_node_notif *)node)->musts;
1549 case LYS_INPUT:
1550 case LYS_OUTPUT:
1551 return &((struct lysc_node_action_inout *)node)->musts;
1552 default:
1553 return NULL;
1554 }
1555}
1556
1557API struct lysc_must *
1558lysc_node_musts(const struct lysc_node *node)
1559{
1560 struct lysc_must **must_p;
1561
1562 must_p = lysc_node_musts_p(node);
1563 if (must_p) {
1564 return *must_p;
1565 } else {
1566 return NULL;
1567 }
1568}
1569
1570struct lysc_when ***
1571lysc_node_when_p(const struct lysc_node *node)
1572{
1573 if (!node) {
1574 return NULL;
1575 }
1576
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001577 switch (node->nodetype) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001578 case LYS_CONTAINER:
1579 return &((struct lysc_node_container *)node)->when;
1580 case LYS_CHOICE:
1581 return &((struct lysc_node_choice *)node)->when;
1582 case LYS_LEAF:
1583 return &((struct lysc_node_leaf *)node)->when;
1584 case LYS_LEAFLIST:
1585 return &((struct lysc_node_leaflist *)node)->when;
1586 case LYS_LIST:
1587 return &((struct lysc_node_list *)node)->when;
1588 case LYS_ANYXML:
1589 case LYS_ANYDATA:
1590 return &((struct lysc_node_anydata *)node)->when;
1591 case LYS_CASE:
1592 return &((struct lysc_node_case *)node)->when;
1593 case LYS_NOTIF:
1594 return &((struct lysc_node_notif *)node)->when;
1595 case LYS_RPC:
1596 case LYS_ACTION:
1597 return &((struct lysc_node_action *)node)->when;
1598 default:
1599 return NULL;
1600 }
1601}
1602
1603API struct lysc_when **
1604lysc_node_when(const struct lysc_node *node)
1605{
1606 struct lysc_when ***when_p;
1607
1608 when_p = lysc_node_when_p(node);
1609 if (when_p) {
1610 return *when_p;
1611 } else {
1612 return NULL;
1613 }
1614}
1615
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001616struct lys_module *
1617lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1618{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001619 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001620 if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) {
Michal Vasko69730152020-10-09 16:30:07 +02001621 return (struct lys_module *)ctx->list.objs[u];
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001622 }
1623 }
1624 return NULL;
1625}
1626
Radek Krejcid6b76452019-09-03 17:03:03 +02001627enum ly_stmt
Radek Krejcid54412f2020-12-17 20:25:35 +01001628lysp_match_kw(struct ly_in *in, uint64_t *indent)
David Sedlákc10e7902018-12-17 02:17:59 +01001629{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001630/**
Radek Krejcid54412f2020-12-17 20:25:35 +01001631 * @brief Move the input by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001632 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001633 *
1634 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001635 */
Radek Krejcid54412f2020-12-17 20:25:35 +01001636#define MOVE_IN(COUNT) \
1637 ly_in_skip(in, COUNT); \
1638 if (indent) { \
1639 (*indent)+=COUNT; \
1640 }
1641#define IF_KW(STR, LEN, STMT) \
1642 if (!strncmp(in->current, STR, LEN)) { \
1643 MOVE_IN(LEN); \
1644 (*kw)=STMT; \
1645 }
1646#define IF_KW_PREFIX(STR, LEN) \
1647 if (!strncmp(in->current, STR, LEN)) { \
1648 MOVE_IN(LEN);
1649#define IF_KW_PREFIX_END \
1650 }
David Sedlák572e7ab2019-06-04 16:01:58 +02001651
Michal Vasko63f3d842020-07-08 10:10:14 +02001652 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001653 enum ly_stmt result = LY_STMT_NONE;
1654 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001655 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001656 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001657 case 'a':
Radek Krejcid54412f2020-12-17 20:25:35 +01001658 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001659 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1660 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1661 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001662 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001663 IF_KW("data", 4, LY_STMT_ANYDATA)
1664 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001665 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001666 break;
1667 case 'b':
Radek Krejcid54412f2020-12-17 20:25:35 +01001668 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001669 IF_KW("ase", 3, LY_STMT_BASE)
1670 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1671 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001672 break;
1673 case 'c':
Radek Krejcid54412f2020-12-17 20:25:35 +01001674 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001675 IF_KW("ase", 3, LY_STMT_CASE)
1676 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001677 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001678 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001679 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001680 IF_KW("ct", 2, LY_STMT_CONTACT)
1681 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001682 IF_KW_PREFIX_END
1683 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001684 break;
1685 case 'd':
Radek Krejcid54412f2020-12-17 20:25:35 +01001686 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001687 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001688 IF_KW("fault", 5, LY_STMT_DEFAULT)
1689 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001690 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001691 IF_KW("e", 1, LY_STMT_DEVIATE)
1692 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001693 IF_KW_PREFIX_END
1694 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001695 break;
1696 case 'e':
Radek Krejcid54412f2020-12-17 20:25:35 +01001697 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001698 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001699 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001700 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1701 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001702 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001703 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001704 break;
1705 case 'f':
Radek Krejcid54412f2020-12-17 20:25:35 +01001706 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001707 IF_KW("eature", 6, LY_STMT_FEATURE)
1708 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001709 break;
1710 case 'g':
Radek Krejcid54412f2020-12-17 20:25:35 +01001711 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001712 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001713 break;
1714 case 'i':
Radek Krejcid54412f2020-12-17 20:25:35 +01001715 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001716 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1717 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1718 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001719 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001720 IF_KW("clude", 5, LY_STMT_INCLUDE)
1721 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001722 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001723 break;
1724 case 'k':
Radek Krejcid54412f2020-12-17 20:25:35 +01001725 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001726 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001727 break;
1728 case 'l':
Radek Krejcid54412f2020-12-17 20:25:35 +01001729 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001730 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001731 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1732 else IF_KW("af", 2, LY_STMT_LEAF)
1733 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001734 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001735 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001736 break;
1737 case 'm':
Radek Krejcid54412f2020-12-17 20:25:35 +01001738 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001739 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001740 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1741 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001742 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001743 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1744 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001745 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001746 IF_KW("ule", 3, LY_STMT_MODULE)
1747 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001748 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001749 break;
1750 case 'n':
Radek Krejcid54412f2020-12-17 20:25:35 +01001751 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001752 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1753 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001754 break;
1755 case 'o':
Radek Krejcid54412f2020-12-17 20:25:35 +01001756 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001757 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001758 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1759 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001760 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001761 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001762 break;
1763 case 'p':
Radek Krejcid54412f2020-12-17 20:25:35 +01001764 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001765 IF_KW("ath", 3, LY_STMT_PATH)
1766 else IF_KW("attern", 6, LY_STMT_PATTERN)
1767 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001768 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001769 IF_KW("fix", 3, LY_STMT_PREFIX)
1770 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001771 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001772 break;
1773 case 'r':
Radek Krejcid54412f2020-12-17 20:25:35 +01001774 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001775 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001776 else IF_KW_PREFIX("e", 1)
1777 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001778 IF_KW("erence", 6, LY_STMT_REFERENCE)
1779 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001780 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001781 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1782 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1783 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001784 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001785 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001786 break;
1787 case 's':
Radek Krejcid54412f2020-12-17 20:25:35 +01001788 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001789 IF_KW("tatus", 5, LY_STMT_STATUS)
1790 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001791 break;
1792 case 't':
Radek Krejcid54412f2020-12-17 20:25:35 +01001793 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001794 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1795 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001796 break;
1797 case 'u':
Radek Krejcid54412f2020-12-17 20:25:35 +01001798 MOVE_IN(1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001799 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001800 IF_KW("que", 3, LY_STMT_UNIQUE)
1801 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001802 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001803 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001804 break;
1805 case 'v':
Radek Krejcid54412f2020-12-17 20:25:35 +01001806 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001807 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001808 break;
1809 case 'w':
Radek Krejcid54412f2020-12-17 20:25:35 +01001810 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001811 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001812 break;
1813 case 'y':
Radek Krejcid54412f2020-12-17 20:25:35 +01001814 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001815 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
1816 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001817 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001818 default:
Radek Krejcid54412f2020-12-17 20:25:35 +01001819 /* if indent is not NULL we are matching keyword from YANG data */
1820 if (indent) {
Michal Vasko63f3d842020-07-08 10:10:14 +02001821 if (in->current[0] == ';') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001822 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001823 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02001824 } else if (in->current[0] == '{') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001825 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001826 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001827 } else if (in->current[0] == '}') {
Radek Krejcid54412f2020-12-17 20:25:35 +01001828 MOVE_IN(1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001829 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001830 }
1831 }
David Sedlák23a59a62018-10-26 13:08:02 +02001832 break;
1833 }
1834
Michal Vasko63f3d842020-07-08 10:10:14 +02001835 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02001836 /* the keyword is not terminated */
1837 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001838 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02001839 }
1840
David Sedlák1bccdfa2019-06-17 15:55:27 +02001841#undef IF_KW
1842#undef IF_KW_PREFIX
1843#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01001844#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02001845 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01001846
David Sedlák1bccdfa2019-06-17 15:55:27 +02001847 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02001848}
David Sedlákecf5eb82019-06-03 14:12:44 +02001849
Radek Krejci85ac8312021-03-03 20:21:33 +01001850LY_ERR
1851lysp_ext_find_definition(const struct ly_ctx *ctx, const struct lysp_ext_instance *ext, const struct lys_module **ext_mod,
1852 struct lysp_ext **ext_def)
1853{
Radek Krejci85ac8312021-03-03 20:21:33 +01001854 const char *tmp, *name, *prefix;
1855 size_t pref_len, name_len;
1856 LY_ARRAY_COUNT_TYPE v;
1857 const struct lys_module *mod = NULL;
1858
Radek Krejcicbb62422021-03-15 09:29:21 +01001859 assert(ext_def);
1860
Radek Krejci85ac8312021-03-03 20:21:33 +01001861 *ext_def = NULL;
1862 if (ext_mod) {
1863 *ext_mod = NULL;
1864 }
1865
Radek Krejci677abea2021-03-05 14:24:53 +01001866 /* parse the prefix, the nodeid was previously already parsed and checked */
Radek Krejci85ac8312021-03-03 20:21:33 +01001867 tmp = ext->name;
Radek Krejci677abea2021-03-05 14:24:53 +01001868 ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len);
Radek Krejci85ac8312021-03-03 20:21:33 +01001869
1870 /* get module where the extension definition should be placed */
1871 mod = ly_resolve_prefix(ctx, prefix, pref_len, ext->format, ext->prefix_data);
1872 if (!mod) {
Radek Krejci422afb12021-03-04 16:38:16 +01001873 LOGVAL(ctx, LYVE_REFERENCE, "Invalid prefix \"%.*s\" used for extension instance identifier.", (int)pref_len, prefix);
Radek Krejci85ac8312021-03-03 20:21:33 +01001874 return LY_EVALID;
1875 } else if (!mod->parsed->extensions) {
1876 LOGVAL(ctx, LYVE_REFERENCE, "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
1877 ext->name, mod->name);
1878 return LY_EVALID;
1879 }
1880
1881 /* find the parsed extension definition there */
1882 LY_ARRAY_FOR(mod->parsed->extensions, v) {
1883 if (!strcmp(name, mod->parsed->extensions[v].name)) {
1884 *ext_def = &mod->parsed->extensions[v];
1885 break;
1886 }
1887 }
1888
Radek Krejcicbb62422021-03-15 09:29:21 +01001889 if (!(*ext_def)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001890 LOGVAL(ctx, LYVE_REFERENCE, "Extension definition of extension instance \"%s\" not found.", ext->name);
1891 return LY_EVALID;
1892 }
1893
1894 if (ext_mod) {
1895 *ext_mod = mod;
1896 }
1897 return LY_SUCCESS;
1898}
1899
1900LY_ERR
1901lysp_ext_instance_resolve_argument(struct ly_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysp_ext *ext_def)
1902{
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001903 if (!ext_def->argname || ext_p->argument) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001904 /* nothing to do */
1905 return LY_SUCCESS;
1906 }
1907
Radek Krejci8df109d2021-04-23 12:19:08 +02001908 if (ext_p->format == LY_VALUE_XML) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001909 /* Schema was parsed from YIN and an argument is expected, ... */
1910 struct lysp_stmt *stmt = NULL;
1911
1912 if (ext_def->flags & LYS_YINELEM_TRUE) {
1913 /* ... argument was the first XML child element */
1914 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {}
1915 if (stmt) {
1916 const char *arg, *ext, *name_arg, *name_ext, *prefix_arg, *prefix_ext;
1917 size_t name_arg_len, name_ext_len, prefix_arg_len, prefix_ext_len;
1918
1919 stmt = ext_p->child;
1920
1921 arg = stmt->stmt;
1922 ly_parse_nodeid(&arg, &prefix_arg, &prefix_arg_len, &name_arg, &name_arg_len);
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001923 if (ly_strncmp(ext_def->argname, name_arg, name_arg_len)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001924 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" expects argument element \"%s\" as its first XML child, "
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001925 "but \"%.*s\" element found.", ext_p->name, ext_def->argname, (int)name_arg_len, name_arg);
Radek Krejci85ac8312021-03-03 20:21:33 +01001926 return LY_EVALID;
1927 }
1928
1929 /* check namespace - all the extension instances must be qualified and argument element is expected in the same
1930 * namespace. Do not check just prefixes, there can be different prefixes pointing to the same namespace */
1931 ext = ext_p->name; /* include prefix */
1932 ly_parse_nodeid(&ext, &prefix_ext, &prefix_ext_len, &name_ext, &name_ext_len);
1933
1934 if (ly_resolve_prefix(ctx, prefix_ext, prefix_ext_len, ext_p->format, ext_p->prefix_data) !=
1935 ly_resolve_prefix(ctx, prefix_arg, prefix_arg_len, stmt->format, stmt->prefix_data)) {
1936 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" element and its argument element \"%s\" are "
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001937 "expected in the same namespace, but they differ.", ext_p->name, ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01001938 return LY_EVALID;
1939 }
1940 }
1941 } else {
1942 /* ... argument was one of the XML attributes which are represented as child stmt
1943 * with LYS_YIN_ATTR flag */
1944 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001945 if (!strcmp(stmt->stmt, ext_def->argname)) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001946 /* this is the extension's argument */
1947 break;
1948 }
1949 }
1950 }
1951
1952 if (stmt) {
1953 LY_CHECK_RET(lydict_insert(ctx, stmt->arg, 0, &ext_p->argument));
1954 stmt->flags |= LYS_YIN_ARGUMENT;
1955 }
1956 }
1957
1958 if (!ext_p->argument) {
1959 /* missing extension's argument */
1960 LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" misses argument %s\"%s\".",
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001961 ext_p->name, (ext_def->flags & LYS_YINELEM_TRUE) ? "element " : "", ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01001962 return LY_EVALID;
1963 }
1964
1965 return LY_SUCCESS;
1966}
1967
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001968LY_ARRAY_COUNT_TYPE
Radek Krejcifc596f92021-02-26 22:40:26 +01001969lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, enum ly_stmt substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001970{
1971 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1972
Michal Vaskod989ba02020-08-24 10:59:24 +02001973 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejciab430862021-03-02 20:13:40 +01001974 if (ext[index].parent_stmt == substmt) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001975 return index;
1976 }
1977 }
1978
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001979 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001980}
1981
Michal Vasko62ed12d2020-05-21 10:08:25 +02001982const struct lysc_node *
Michal Vasko72244882021-01-12 15:21:05 +01001983lysc_data_node(const struct lysc_node *schema)
Michal Vasko62ed12d2020-05-21 10:08:25 +02001984{
1985 const struct lysc_node *parent;
1986
Michal Vasko72244882021-01-12 15:21:05 +01001987 parent = schema;
Radek Krejcidf549132021-01-21 10:32:32 +01001988 while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC |
1989 LYS_ACTION | LYS_NOTIF))) {
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001990 parent = parent->parent;
Michal Vasko72244882021-01-12 15:21:05 +01001991 }
Michal Vasko62ed12d2020-05-21 10:08:25 +02001992
1993 return parent;
1994}