blob: 5fa24ba2e49ede7cc3c86f7d2faed3f0825764a6 [file] [log] [blame]
Radek Krejci86d106e2018-10-18 09:53:19 +02001/**
2 * @file tree_schema_helpers.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejcie7b95092019-05-15 11:03:07 +02004 * @brief Parsing and validation helper functions for schema trees
Radek Krejci86d106e2018-10-18 09:53:19 +02005 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Krejci535ea9f2020-05-29 16:01:05 +020014
15#define _GNU_SOURCE
Radek Krejci86d106e2018-10-18 09:53:19 +020016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020018#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010019#include <stddef.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <stdint.h>
Radek Krejci9ed7a192018-10-31 16:23:51 +010021#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020023#include <time.h>
24
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020026#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010029#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020030#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010031#include "log.h"
Michal Vasko69730152020-10-09 16:30:07 +020032#include "parser_schema.h"
Michal Vasko962b6cd2020-12-08 10:07:49 +010033#include "schema_features.h"
34#include "schema_compile.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020035#include "set.h"
36#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010037#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020038#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020039#include "tree_schema_internal.h"
40
Radek Krejci85747952019-06-07 16:43:43 +020041LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +020042lysp_check_prefix(struct lys_parser_ctx *ctx, struct lysp_import *imports, const char *module_prefix, const char **value)
Radek Krejci86d106e2018-10-18 09:53:19 +020043{
44 struct lysp_import *i;
45
Michal Vasko69730152020-10-09 16:30:07 +020046 if (module_prefix && (&module_prefix != value) && !strcmp(module_prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010047 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value);
Radek Krejci86d106e2018-10-18 09:53:19 +020048 return LY_EEXIST;
49 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +010050 LY_ARRAY_FOR(imports, struct lysp_import, i) {
Michal Vasko69730152020-10-09 16:30:07 +020051 if (i->prefix && (&i->prefix != value) && !strcmp(i->prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010052 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +010053 return LY_EEXIST;
Radek Krejci86d106e2018-10-18 09:53:19 +020054 }
55 }
56 return LY_SUCCESS;
57}
58
59LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020060lysp_check_date(struct lys_parser_ctx *ctx, const char *date, uint8_t date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +020061{
Radek Krejci86d106e2018-10-18 09:53:19 +020062 struct tm tm, tm_;
63 char *r;
64
Michal Vaskob36053d2020-03-26 15:49:30 +010065 LY_CHECK_ARG_RET(ctx ? PARSER_CTX(ctx) : NULL, date, LY_EINVAL);
66 LY_CHECK_ERR_RET(date_len != LY_REV_SIZE - 1, LOGARG(ctx ? PARSER_CTX(ctx) : NULL, date_len), LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +020067
Radek Krejcif13b87b2020-12-01 22:02:17 +010068 /* check format: YYYY-MM-DD */
Radek Krejci1deb5be2020-08-26 16:43:36 +020069 for (uint8_t i = 0; i < date_len; i++) {
Michal Vasko69730152020-10-09 16:30:07 +020070 if ((i == 4) || (i == 7)) {
Radek Krejci86d106e2018-10-18 09:53:19 +020071 if (date[i] != '-') {
72 goto error;
73 }
74 } else if (!isdigit(date[i])) {
75 goto error;
76 }
77 }
78
79 /* check content, e.g. 2018-02-31 */
80 memset(&tm, 0, sizeof tm);
81 r = strptime(date, "%Y-%m-%d", &tm);
Michal Vasko69730152020-10-09 16:30:07 +020082 if (!r || (r != &date[LY_REV_SIZE - 1])) {
Radek Krejci86d106e2018-10-18 09:53:19 +020083 goto error;
84 }
85 memcpy(&tm_, &tm, sizeof tm);
86 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
87 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
88 /* checking days is enough, since other errors
89 * have been checked by strptime() */
90 goto error;
91 }
92
93 return LY_SUCCESS;
94
95error:
Radek Krejcid33273d2018-10-25 14:55:52 +020096 if (stmt) {
Radek Krejcibbe09a92018-11-08 09:36:54 +010097 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +010098 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt);
Radek Krejcibbe09a92018-11-08 09:36:54 +010099 } else {
100 LOGVAL(NULL, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, date_len, date, stmt);
101 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200102 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200103 return LY_EINVAL;
104}
105
106void
107lysp_sort_revisions(struct lysp_revision *revs)
108{
Radek Krejci857189e2020-09-01 13:26:36 +0200109 LY_ARRAY_COUNT_TYPE i, r;
Radek Krejci86d106e2018-10-18 09:53:19 +0200110 struct lysp_revision rev;
111
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200112 for (i = 1, r = 0; revs && i < LY_ARRAY_COUNT(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200113 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200114 r = i;
115 }
116 }
117
118 if (r) {
119 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200120 memcpy(&rev, &revs[0], sizeof rev);
121 memcpy(&revs[0], &revs[r], sizeof rev);
122 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200123 }
124}
Radek Krejci151a5b72018-10-19 14:21:44 +0200125
Radek Krejcibbe09a92018-11-08 09:36:54 +0100126static const struct lysp_tpdf *
127lysp_type_match(const char *name, struct lysp_node *node)
128{
Radek Krejci0fb28562018-12-13 15:17:37 +0100129 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200130 LY_ARRAY_COUNT_TYPE u;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100131
Radek Krejci0fb28562018-12-13 15:17:37 +0100132 typedefs = lysp_node_typedefs(node);
133 LY_ARRAY_FOR(typedefs, u) {
134 if (!strcmp(name, typedefs[u].name)) {
135 /* match */
136 return &typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100137 }
138 }
139
140 return NULL;
141}
142
Radek Krejci4f28eda2018-11-12 11:46:16 +0100143static LY_DATA_TYPE
144lysp_type_str2builtin(const char *name, size_t len)
145{
146 if (len >= 4) { /* otherwise it does not match any built-in type */
147 if (name[0] == 'b') {
148 if (name[1] == 'i') {
Michal Vasko69730152020-10-09 16:30:07 +0200149 if ((len == 6) && !strncmp(&name[2], "nary", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100150 return LY_TYPE_BINARY;
Michal Vasko69730152020-10-09 16:30:07 +0200151 } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100152 return LY_TYPE_BITS;
153 }
Michal Vasko69730152020-10-09 16:30:07 +0200154 } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100155 return LY_TYPE_BOOL;
156 }
157 } else if (name[0] == 'd') {
Michal Vasko69730152020-10-09 16:30:07 +0200158 if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100159 return LY_TYPE_DEC64;
160 }
161 } else if (name[0] == 'e') {
Michal Vasko69730152020-10-09 16:30:07 +0200162 if ((len == 5) && !strncmp(&name[1], "mpty", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100163 return LY_TYPE_EMPTY;
Michal Vasko69730152020-10-09 16:30:07 +0200164 } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100165 return LY_TYPE_ENUM;
166 }
167 } else if (name[0] == 'i') {
168 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200169 if ((len == 4) && !strncmp(&name[2], "t8", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100170 return LY_TYPE_INT8;
171 } else if (len == 5) {
172 if (!strncmp(&name[2], "t16", 3)) {
173 return LY_TYPE_INT16;
174 } else if (!strncmp(&name[2], "t32", 3)) {
175 return LY_TYPE_INT32;
176 } else if (!strncmp(&name[2], "t64", 3)) {
177 return LY_TYPE_INT64;
178 }
Michal Vasko69730152020-10-09 16:30:07 +0200179 } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100180 return LY_TYPE_INST;
181 }
Michal Vasko69730152020-10-09 16:30:07 +0200182 } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100183 return LY_TYPE_IDENT;
184 }
185 } else if (name[0] == 'l') {
Michal Vasko69730152020-10-09 16:30:07 +0200186 if ((len == 7) && !strncmp(&name[1], "eafref", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100187 return LY_TYPE_LEAFREF;
188 }
189 } else if (name[0] == 's') {
Michal Vasko69730152020-10-09 16:30:07 +0200190 if ((len == 6) && !strncmp(&name[1], "tring", 5)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100191 return LY_TYPE_STRING;
192 }
193 } else if (name[0] == 'u') {
194 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200195 if ((len == 5) && !strncmp(&name[2], "ion", 3)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100196 return LY_TYPE_UNION;
197 }
Michal Vasko69730152020-10-09 16:30:07 +0200198 } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) {
199 if ((len == 5) && (name[4] == '8')) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100200 return LY_TYPE_UINT8;
201 } else if (len == 6) {
202 if (!strncmp(&name[4], "16", 2)) {
203 return LY_TYPE_UINT16;
204 } else if (!strncmp(&name[4], "32", 2)) {
205 return LY_TYPE_UINT32;
206 } else if (!strncmp(&name[4], "64", 2)) {
207 return LY_TYPE_UINT64;
208 }
209 }
210 }
211 }
212 }
213
214 return LY_TYPE_UNKNOWN;
215}
216
Radek Krejcibbe09a92018-11-08 09:36:54 +0100217LY_ERR
218lysp_type_find(const char *id, struct lysp_node *start_node, struct lysp_module *start_module,
Radek Krejci0f969882020-08-21 16:56:47 +0200219 LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node, struct lysp_module **module)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100220{
221 const char *str, *name;
222 struct lysp_tpdf *typedefs;
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100223 const struct lys_module *mod;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200224 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100225
226 assert(id);
227 assert(start_module);
228 assert(tpdf);
229 assert(node);
230 assert(module);
231
Radek Krejci4f28eda2018-11-12 11:46:16 +0100232 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100233 str = strchr(id, ':');
234 if (str) {
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100235 mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_PREF_SCHEMA, (void *)start_module);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200236 *module = mod ? mod->parsed : NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100237 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100238 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100239 } else {
240 *module = start_module;
241 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100242
243 /* check for built-in types */
244 *type = lysp_type_str2builtin(name, strlen(name));
245 if (*type) {
246 *tpdf = NULL;
247 return LY_SUCCESS;
248 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100249 }
250 LY_CHECK_RET(!(*module), LY_ENOTFOUND);
251
Michal Vasko69730152020-10-09 16:30:07 +0200252 if (start_node && (*module == start_module)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100253 /* search typedefs in parent's nodes */
254 *node = start_node;
255 while (*node) {
256 *tpdf = lysp_type_match(name, *node);
257 if (*tpdf) {
258 /* match */
259 return LY_SUCCESS;
260 }
261 *node = (*node)->parent;
262 }
263 }
264
265 /* search in top-level typedefs */
266 if ((*module)->typedefs) {
267 LY_ARRAY_FOR((*module)->typedefs, u) {
268 if (!strcmp(name, (*module)->typedefs[u].name)) {
269 /* match */
270 *tpdf = &(*module)->typedefs[u];
271 return LY_SUCCESS;
272 }
273 }
274 }
275
276 /* search in submodules' typedefs */
277 LY_ARRAY_FOR((*module)->includes, u) {
278 typedefs = (*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\").",
Michal Vasko69730152020-10-09 16:30:07 +0200299 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).",
Michal Vasko69730152020-10-09 16:30:07 +0200305 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.
392 * Implementation of ::values_equal_cb.
393 */
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
David Sedlákd2ebe572019-07-22 12:53:14 +0200401lysp_parse_finalize_reallocated(struct lys_parser_ctx *ctx, struct lysp_grp *groupings, struct lysp_augment *augments,
Radek Krejci0f969882020-08-21 16:56:47 +0200402 struct lysp_action *actions, struct lysp_notif *notifs)
David Sedlákd2ebe572019-07-22 12:53:14 +0200403{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200404 LY_ARRAY_COUNT_TYPE u, v;
David Sedlákd2ebe572019-07-22 12:53:14 +0200405 struct lysp_node *child;
406
407 /* finalize parent pointers to the reallocated items */
408
409 /* gropings */
410 LY_ARRAY_FOR(groupings, u) {
411 LY_LIST_FOR(groupings[u].data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200412 child->parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200413 }
414 LY_ARRAY_FOR(groupings[u].actions, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200415 groupings[u].actions[v].parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200416 }
417 LY_ARRAY_FOR(groupings[u].notifs, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200418 groupings[u].notifs[v].parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200419 }
420 LY_ARRAY_FOR(groupings[u].groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200421 groupings[u].groupings[v].parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200422 }
423 if (groupings[u].typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200424 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &groupings[u], 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200425 }
426 }
427
428 /* augments */
429 LY_ARRAY_FOR(augments, u) {
430 LY_LIST_FOR(augments[u].child, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200431 child->parent = (struct lysp_node *)&augments[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200432 }
433 LY_ARRAY_FOR(augments[u].actions, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200434 augments[u].actions[v].parent = (struct lysp_node *)&augments[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200435 }
436 LY_ARRAY_FOR(augments[u].notifs, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200437 augments[u].notifs[v].parent = (struct lysp_node *)&augments[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200438 }
439 }
440
441 /* actions */
442 LY_ARRAY_FOR(actions, u) {
443 if (actions[u].input.parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200444 actions[u].input.parent = (struct lysp_node *)&actions[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200445 LY_LIST_FOR(actions[u].input.data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200446 child->parent = (struct lysp_node *)&actions[u].input;
David Sedlákd2ebe572019-07-22 12:53:14 +0200447 }
448 LY_ARRAY_FOR(actions[u].input.groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200449 actions[u].input.groupings[v].parent = (struct lysp_node *)&actions[u].input;
David Sedlákd2ebe572019-07-22 12:53:14 +0200450 }
451 if (actions[u].input.typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200452 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &actions[u].input, 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200453 }
454 }
455 if (actions[u].output.parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200456 actions[u].output.parent = (struct lysp_node *)&actions[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200457 LY_LIST_FOR(actions[u].output.data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200458 child->parent = (struct lysp_node *)&actions[u].output;
David Sedlákd2ebe572019-07-22 12:53:14 +0200459 }
460 LY_ARRAY_FOR(actions[u].output.groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200461 actions[u].output.groupings[v].parent = (struct lysp_node *)&actions[u].output;
David Sedlákd2ebe572019-07-22 12:53:14 +0200462 }
463 if (actions[u].output.typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200464 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &actions[u].output, 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200465 }
466 }
467 LY_ARRAY_FOR(actions[u].groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200468 actions[u].groupings[v].parent = (struct lysp_node *)&actions[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200469 }
470 if (actions[u].typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200471 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &actions[u], 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200472 }
473 }
474
475 /* notifications */
476 LY_ARRAY_FOR(notifs, u) {
477 LY_LIST_FOR(notifs[u].data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200478 child->parent = (struct lysp_node *)&notifs[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200479 }
480 LY_ARRAY_FOR(notifs[u].groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200481 notifs[u].groupings[v].parent = (struct lysp_node *)&notifs[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200482 }
483 if (notifs[u].typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200484 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &notifs[u], 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200485 }
486 }
487
488 return LY_SUCCESS;
489}
490
491LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100492lysp_check_dup_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100493{
494 struct hash_table *ids_global;
495 struct hash_table *ids_scoped;
Radek Krejci0fb28562018-12-13 15:17:37 +0100496 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200497 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200498 uint32_t i;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100499 LY_ERR ret = LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100500
501 /* check name collisions - typedefs and groupings */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100502 ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
503 ids_scoped = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200504 LY_ARRAY_FOR(mod->typedefs, v) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100505 ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global, ids_scoped);
506 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100507 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200508 LY_ARRAY_FOR(mod->includes, v) {
509 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100510 ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global, ids_scoped);
511 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci3b1f9292018-11-08 10:58:35 +0100512 }
513 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200514 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
515 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
516 LY_ARRAY_FOR(typedefs, u) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100517 ret = lysp_check_dup_typedef(ctx, (struct lysp_node *)ctx->tpdfs_nodes.objs[i], &typedefs[u], ids_global, ids_scoped);
518 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100519 }
520 }
Michal Vasko405cc9e2020-12-01 12:01:27 +0100521
Radek Krejcibbe09a92018-11-08 09:36:54 +0100522cleanup:
523 lyht_free(ids_global);
524 lyht_free(ids_scoped);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100525 return ret;
526}
527
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100528static ly_bool
529ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
530{
531 void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
532
533 return ptr1 == ptr2 ? 1 : 0;
534}
535
536LY_ERR
537lysp_check_dup_features(struct lys_parser_ctx *ctx, struct lysp_module *mod)
538{
539 LY_ARRAY_COUNT_TYPE u;
540 struct hash_table *ht;
541 struct lysp_feature *f;
542 uint32_t hash;
543 LY_ERR ret = LY_SUCCESS, r;
544
545 ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
546 LY_CHECK_RET(!ht, LY_EMEM);
547
548 /* add all module features into a hash table */
549 LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
550 hash = dict_hash(f->name, strlen(f->name));
551 r = lyht_insert(ht, &f->name, hash, NULL);
552 if (r == LY_EEXIST) {
553 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
554 ret = LY_EVALID;
555 goto cleanup;
556 } else if (r) {
557 ret = r;
558 goto cleanup;
559 }
560 }
561
562 /* add all submodule features into a hash table */
563 LY_ARRAY_FOR(mod->includes, u) {
564 LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
565 hash = dict_hash(f->name, strlen(f->name));
566 r = lyht_insert(ht, &f->name, hash, NULL);
567 if (r == LY_EEXIST) {
568 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
569 ret = LY_EVALID;
570 goto cleanup;
571 } else if (r) {
572 ret = r;
573 goto cleanup;
574 }
575 }
576 }
577
578cleanup:
579 lyht_free(ht);
580 return ret;
581}
582
583LY_ERR
584lysp_check_dup_identities(struct lys_parser_ctx *ctx, struct lysp_module *mod)
585{
586 LY_ARRAY_COUNT_TYPE u;
587 struct hash_table *ht;
588 struct lysp_ident *i;
589 uint32_t hash;
590 LY_ERR ret = LY_SUCCESS, r;
591
592 ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
593 LY_CHECK_RET(!ht, LY_EMEM);
594
595 /* add all module identities into a hash table */
596 LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
597 hash = dict_hash(i->name, strlen(i->name));
598 r = lyht_insert(ht, &i->name, hash, NULL);
599 if (r == LY_EEXIST) {
600 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
601 ret = LY_EVALID;
602 goto cleanup;
603 } else if (r) {
604 ret = r;
605 goto cleanup;
606 }
607 }
608
609 /* add all submodule identities into a hash table */
610 LY_ARRAY_FOR(mod->includes, u) {
611 LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
612 hash = dict_hash(i->name, strlen(i->name));
613 r = lyht_insert(ht, &i->name, hash, NULL);
614 if (r == LY_EEXIST) {
615 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
616 ret = LY_EVALID;
617 goto cleanup;
618 } else if (r) {
619 ret = r;
620 goto cleanup;
621 }
622 }
623 }
624
625cleanup:
626 lyht_free(ht);
627 return ret;
628}
629
Radek Krejci9ed7a192018-10-31 16:23:51 +0100630struct lysp_load_module_check_data {
631 const char *name;
632 const char *revision;
633 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200634 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100635};
636
637static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100638lysp_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 +0100639{
640 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100641 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200642 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100643 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100644 struct lysp_revision *revs;
645
646 name = mod ? mod->mod->name : submod->name;
647 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200648 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100649
650 if (info->name) {
651 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100652 if (strcmp(info->name, name)) {
653 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100654 return LY_EINVAL;
655 }
656 }
657 if (info->revision) {
658 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100659 if (!revs || strcmp(info->revision, revs[0].date)) {
660 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200661 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100662 return LY_EINVAL;
663 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200664 } else if (!latest_revision) {
665 /* do not log, we just need to drop the schema and use the latest revision from the context */
666 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100667 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100668 if (submod) {
669 assert(info->submoduleof);
670
Radek Krejci9ed7a192018-10-31 16:23:51 +0100671 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200672 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100673 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200674 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100675 return LY_EVALID;
676 }
677 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100678 if (submod->parsing) {
679 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100680 return LY_EVALID;
681 }
682 }
683 if (info->path) {
684 /* check that name and revision match filename */
685 filename = strrchr(info->path, '/');
686 if (!filename) {
687 filename = info->path;
688 } else {
689 filename++;
690 }
691 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100692 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100693 rev = strchr(filename, '@');
694 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100695 if (strncmp(filename, name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +0200696 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100697 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100698 }
699 /* revision */
700 if (rev) {
701 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100702 if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100703 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +0200704 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100705 }
706 }
707 }
708 return LY_SUCCESS;
709}
710
711LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100712lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, const char **features, ly_bool implement,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100713 struct lys_parser_ctx *main_ctx, const char *main_name, ly_bool required, struct lys_glob_unres *unres,
714 void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100715{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200716 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100717 char *filepath = NULL;
718 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100719 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100720 LY_ERR ret = LY_SUCCESS;
721 struct lysp_load_module_check_data check_data = {0};
722
723 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 +0200724 &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200725 if (!filepath) {
726 if (required) {
727 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200728 revision ? revision : "");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200729 }
730 return LY_ENOTFOUND;
731 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100732
733 LOGVRB("Loading schema from \"%s\" file.", filepath);
734
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200735 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200736 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +0200737 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100738 check_data.name = name;
739 check_data.revision = revision;
740 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800741 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200742 if (main_ctx) {
Michal Vasko7a0b0762020-09-02 16:37:01 +0200743 ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data,
Michal Vasko69730152020-10-09 16:30:07 +0200744 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200745 } else {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100746 ret = lys_create_module(ctx, in, format, implement, lysp_load_module_check, &check_data, features, unres,
Michal Vasko69730152020-10-09 16:30:07 +0200747 (struct lys_module **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200748
749 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200750 ly_in_free(in, 1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200751 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100752
753 *result = mod;
754
755 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200756
Radek Krejci9ed7a192018-10-31 16:23:51 +0100757cleanup:
758 free(filepath);
759 return ret;
760}
761
Radek Krejcid33273d2018-10-25 14:55:52 +0200762LY_ERR
Michal Vasko0550b762020-11-24 18:04:08 +0100763lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, ly_bool implement, const char **features,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100764 struct lys_glob_unres *unres, struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200765{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100766 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200767 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200768
Radek Krejci9ed7a192018-10-31 16:23:51 +0100769 void (*module_data_free)(void *module_data, void *user_data) = NULL;
770 struct lysp_load_module_check_data check_data = {0};
Michal Vasko0550b762020-11-24 18:04:08 +0100771 struct lys_module *ctx_latest = NULL, *m;
Michal Vasko63f3d842020-07-08 10:10:14 +0200772 struct ly_in *in;
Michal Vasko0550b762020-11-24 18:04:08 +0100773 LY_ERR ret;
Radek Krejci086c7132018-10-26 15:29:04 +0200774
Michal Vasko405cc9e2020-12-01 12:01:27 +0100775 assert(mod && unres);
Radek Krejci0af46292019-01-11 16:02:31 +0100776
Michal Vasko25d6ad02020-10-22 12:20:22 +0200777 if (ctx->flags & LY_CTX_ALL_IMPLEMENTED) {
Radek Krejcia53d7c92020-08-21 11:30:56 +0200778 implement = 1;
779 }
780
Michal Vasko0550b762020-11-24 18:04:08 +0100781 /*
782 * try to get the module from the context
783 */
Radek Krejci0af46292019-01-11 16:02:31 +0100784 if (!*mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100785 if (revision) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200786 /* get the specific revision */
Michal Vasko22df3f02020-08-24 13:29:22 +0200787 *mod = (struct lys_module *)ly_ctx_get_module(ctx, name, revision);
Radek Krejci0af46292019-01-11 16:02:31 +0100788 } else {
Michal Vasko0550b762020-11-24 18:04:08 +0100789 if (implement) {
790 /* prefer the implemented module instead of the latest one */
791 *mod = (struct lys_module *)ly_ctx_get_module_implemented(ctx, name);
792 }
793 if (!*mod) {
794 /* get the requested module of the latest revision in the context */
795 *mod = (struct lys_module *)ly_ctx_get_module_latest(ctx, name);
796 if (*mod && ((*mod)->latest_revision == 1)) {
797 /* let us now search with callback and searchpaths to check if there is newer revision outside the context */
798 ctx_latest = *mod;
799 *mod = NULL;
800 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200801 }
Radek Krejci0af46292019-01-11 16:02:31 +0100802 }
Radek Krejci086c7132018-10-26 15:29:04 +0200803 }
804
Michal Vasko0550b762020-11-24 18:04:08 +0100805 /* check collision with other implemented revision */
806 if (implement) {
807 m = ly_ctx_get_module_implemented(ctx, name);
808 if (m && (!*mod || (*mod && (m != *mod)))) {
Radek Krejci086c7132018-10-26 15:29:04 +0200809 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200810 "Module \"%s\" is already present in other implemented revision.", name);
Michal Vasko0550b762020-11-24 18:04:08 +0100811 *mod = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200812 return LY_EDENIED;
813 }
Michal Vasko0550b762020-11-24 18:04:08 +0100814 }
Radek Krejci086c7132018-10-26 15:29:04 +0200815
Michal Vasko0550b762020-11-24 18:04:08 +0100816 /*
817 * no suitable module in the context, try to load it
818 */
819 if (!*mod) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100820 /* module not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200821 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
822search_clb:
823 if (ctx->imp_clb) {
824 if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
Michal Vasko69730152020-10-09 16:30:07 +0200825 &format, &module_data, &module_data_free) == LY_SUCCESS) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200826 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
Radek Krejci9ed7a192018-10-31 16:23:51 +0100827 check_data.name = name;
828 check_data.revision = revision;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100829 lys_create_module(ctx, in, format, implement, lysp_load_module_check, &check_data, features, unres, mod);
Michal Vasko63f3d842020-07-08 10:10:14 +0200830 ly_in_free(in, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100831 if (module_data_free) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200832 module_data_free((void *)module_data, ctx->imp_clb_data);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100833 }
Radek Krejci086c7132018-10-26 15:29:04 +0200834 }
835 }
836 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
837 goto search_file;
838 }
839 } else {
840search_file:
841 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
842 /* module was not received from the callback or there is no callback set */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100843 lys_module_localfile(ctx, name, revision, features, implement, NULL, NULL, ctx_latest ? 0 : 1, unres,
844 (void **)mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200845 }
Michal Vasko0550b762020-11-24 18:04:08 +0100846 if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejci086c7132018-10-26 15:29:04 +0200847 goto search_clb;
848 }
849 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100850
Radek Krejcib3289d62019-09-18 12:21:39 +0200851 /* update the latest_revision flag - here we have selected the latest available schema,
852 * consider that even the callback provides correct latest revision */
Michal Vasko0550b762020-11-24 18:04:08 +0100853 if (!*mod && ctx_latest) {
854 LOGVRB("Newer revision than %s-%s not found, using this as the latest revision.", ctx_latest->name,
855 ctx_latest->revision);
856 ctx_latest->latest_revision = 2;
857 *mod = ctx_latest;
858 } else if (*mod && !revision && ((*mod)->latest_revision == 1)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100859 (*mod)->latest_revision = 2;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100860 }
Radek Krejci086c7132018-10-26 15:29:04 +0200861
Michal Vasko0550b762020-11-24 18:04:08 +0100862 if (!*mod) {
863 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "%s \"%s\" module failed.",
864 implement ? "Loading" : "Importing", name);
865 return LY_EVALID;
866 }
867 } else {
868 /* we have module from the current context, circular check */
869 if ((*mod)->parsed->parsing) {
Radek Krejci086c7132018-10-26 15:29:04 +0200870 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
871 *mod = NULL;
872 return LY_EVALID;
873 }
874 }
Radek Krejci086c7132018-10-26 15:29:04 +0200875
Michal Vasko0550b762020-11-24 18:04:08 +0100876 /*
877 * module found, make sure it is implemented if should be
878 */
Michal Vasko962b6cd2020-12-08 10:07:49 +0100879 if (implement) {
880 if ((*mod)->implemented) {
881 /* set features if different */
882 ret = lys_set_features((*mod)->parsed, features);
883 if (!ret) {
884 /* context need to be recompiled so that feature changes are properly applied */
885 unres->recompile = 1;
886 } else if (ret != LY_EEXIST) {
887 /* error */
888 return ret;
889 } /* else no feature changes */
890 } else {
891 /* implement */
892 ret = lys_set_implemented_r(*mod, features, unres);
893 if (ret) {
894 *mod = NULL;
895 return ret;
896 }
Michal Vaskoc5d64862020-11-24 18:04:45 +0100897 }
Radek Krejci086c7132018-10-26 15:29:04 +0200898 }
Radek Krejci086c7132018-10-26 15:29:04 +0200899
900 return LY_SUCCESS;
901}
902
903LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200904lysp_check_stringchar(struct lys_parser_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +0200905{
906 if (!is_yangutf8char(c)) {
907 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
908 return LY_EVALID;
909 }
910 return LY_SUCCESS;
911}
912
913LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200914lysp_check_identifierchar(struct lys_parser_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +0200915{
Michal Vasko69730152020-10-09 16:30:07 +0200916 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +0200917 if (!is_yangidentstartchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200918 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200919 return LY_EVALID;
920 }
921 if (prefix) {
922 if (first) {
923 (*prefix) = 0;
924 } else {
925 (*prefix) = 2;
926 }
927 }
Michal Vasko69730152020-10-09 16:30:07 +0200928 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +0200929 (*prefix) = 1;
930 } else if (!is_yangidentchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200931 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200932 return LY_EVALID;
933 }
934
935 return LY_SUCCESS;
936}
937
938LY_ERR
Michal Vasko7c8439f2020-08-05 13:25:19 +0200939lysp_load_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200940{
Michal Vaskob36053d2020-03-26 15:49:30 +0100941 struct ly_ctx *ctx = (struct ly_ctx *)(PARSER_CTX(pctx));
Radek Krejci3eb299d2019-04-08 15:07:44 +0200942 struct lysp_submodule *submod = NULL;
Radek Krejcid33273d2018-10-25 14:55:52 +0200943 const char *submodule_data = NULL;
944 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200945
Radek Krejcid33273d2018-10-25 14:55:52 +0200946 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100947 struct lysp_load_module_check_data check_data = {0};
Michal Vasko63f3d842020-07-08 10:10:14 +0200948 struct ly_in *in;
Radek Krejcid33273d2018-10-25 14:55:52 +0200949
Radek Krejcibbe09a92018-11-08 09:36:54 +0100950 /* submodule not present in the context, get the input data and parse it */
Michal Vaskob36053d2020-03-26 15:49:30 +0100951 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200952search_clb:
Michal Vaskob36053d2020-03-26 15:49:30 +0100953 if (ctx->imp_clb) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200954 if (ctx->imp_clb(pctx->parsed_mod->mod->name, NULL, inc->name, inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
Michal Vasko69730152020-10-09 16:30:07 +0200955 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200956 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
Radek Krejcibbe09a92018-11-08 09:36:54 +0100957 check_data.name = inc->name;
958 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200959 check_data.submoduleof = pctx->parsed_mod->mod->name;
Michal Vasko7a0b0762020-09-02 16:37:01 +0200960 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, &submod);
Michal Vasko63f3d842020-07-08 10:10:14 +0200961 ly_in_free(in, 0);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100962 if (submodule_data_free) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200963 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200964 }
965 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200966 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100967 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100968 goto search_file;
Radek Krejcid33273d2018-10-25 14:55:52 +0200969 }
Radek Krejci2d31ea72018-10-25 15:46:42 +0200970 } else {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100971search_file:
Michal Vaskob36053d2020-03-26 15:49:30 +0100972 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100973 /* submodule was not received from the callback or there is no callback set */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100974 lys_module_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, NULL, 0, pctx,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100975 pctx->parsed_mod->mod->name, 1, NULL, (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100976 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100977 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100978 goto search_clb;
979 }
980 }
981 if (submod) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100982 if (!inc->rev[0] && (submod->latest_revision == 1)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100983 /* update the latest_revision flag - here we have selected the latest available schema,
984 * consider that even the callback provides correct latest revision */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100985 submod->latest_revision = 2;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100986 }
987
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100988 inc->submodule = submod;
Radek Krejcid33273d2018-10-25 14:55:52 +0200989 }
990 if (!inc->submodule) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100991 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.",
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200992 inc->name, pctx->parsed_mod->mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200993 return LY_EVALID;
994 }
995
996 return LY_SUCCESS;
997}
998
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100999API const struct lysc_when *
1000lysc_has_when(const struct lysc_node *node)
1001{
1002 if (!node) {
1003 return NULL;
1004 }
1005
1006 do {
1007 if (node->when) {
1008 return *node->when;
1009 }
1010 node = node->parent;
1011 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1012
1013 return NULL;
1014}
1015
Radek Krejci0935f412019-08-20 16:15:18 +02001016API const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001017lys_nodetype2str(uint16_t nodetype)
1018{
Michal Vaskod989ba02020-08-24 10:59:24 +02001019 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001020 case LYS_CONTAINER:
1021 return "container";
1022 case LYS_CHOICE:
1023 return "choice";
1024 case LYS_LEAF:
1025 return "leaf";
1026 case LYS_LEAFLIST:
1027 return "leaf-list";
1028 case LYS_LIST:
1029 return "list";
1030 case LYS_ANYXML:
1031 return "anyxml";
1032 case LYS_ANYDATA:
1033 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001034 case LYS_CASE:
1035 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001036 case LYS_RPC:
1037 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001038 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001039 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001040 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001041 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001042 case LYS_USES:
1043 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001044 default:
1045 return "unknown";
1046 }
1047}
1048
Radek Krejci693262f2019-04-29 15:23:20 +02001049const char *
1050lys_datatype2str(LY_DATA_TYPE basetype)
1051{
Michal Vaskod989ba02020-08-24 10:59:24 +02001052 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001053 case LY_TYPE_BINARY:
1054 return "binary";
1055 case LY_TYPE_UINT8:
1056 return "uint8";
1057 case LY_TYPE_UINT16:
1058 return "uint16";
1059 case LY_TYPE_UINT32:
1060 return "uint32";
1061 case LY_TYPE_UINT64:
1062 return "uint64";
1063 case LY_TYPE_STRING:
1064 return "string";
1065 case LY_TYPE_BITS:
1066 return "bits";
1067 case LY_TYPE_BOOL:
1068 return "boolean";
1069 case LY_TYPE_DEC64:
1070 return "decimal64";
1071 case LY_TYPE_EMPTY:
1072 return "empty";
1073 case LY_TYPE_ENUM:
1074 return "enumeration";
1075 case LY_TYPE_IDENT:
1076 return "identityref";
1077 case LY_TYPE_INST:
1078 return "instance-identifier";
1079 case LY_TYPE_LEAFREF:
1080 return "leafref";
1081 case LY_TYPE_UNION:
1082 return "union";
1083 case LY_TYPE_INT8:
1084 return "int8";
1085 case LY_TYPE_INT16:
1086 return "int16";
1087 case LY_TYPE_INT32:
1088 return "int32";
1089 case LY_TYPE_INT64:
1090 return "int64";
1091 default:
1092 return "unknown";
1093 }
1094}
1095
Radek Krejci056d0a82018-12-06 16:57:25 +01001096API const struct lysp_tpdf *
1097lysp_node_typedefs(const struct lysp_node *node)
1098{
Radek Krejci0fb28562018-12-13 15:17:37 +01001099 switch (node->nodetype) {
1100 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001101 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001102 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001103 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001104 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001105 return ((struct lysp_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001106 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001107 case LYS_ACTION:
Michal Vasko22df3f02020-08-24 13:29:22 +02001108 return ((struct lysp_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001109 case LYS_INPUT:
1110 case LYS_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001111 return ((struct lysp_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001112 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001113 return ((struct lysp_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001114 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001115 return NULL;
1116 }
1117}
1118
Radek Krejci53ea6152018-12-13 15:21:15 +01001119API const struct lysp_grp *
1120lysp_node_groupings(const struct lysp_node *node)
1121{
1122 switch (node->nodetype) {
1123 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001124 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001125 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001126 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001127 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001128 return ((struct lysp_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001129 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001130 case LYS_ACTION:
Michal Vasko22df3f02020-08-24 13:29:22 +02001131 return ((struct lysp_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001132 case LYS_INPUT:
1133 case LYS_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001134 return ((struct lysp_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001135 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001136 return ((struct lysp_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001137 default:
1138 return NULL;
1139 }
1140}
1141
Radek Krejcibbe09a92018-11-08 09:36:54 +01001142struct lysp_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001143lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001144{
1145 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001146
Radek Krejcibbe09a92018-11-08 09:36:54 +01001147 switch (node->nodetype) {
1148 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001149 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001150 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001151 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001152 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001153 return &((struct lysp_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001154 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001155 return &((struct lysp_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001156 default:
1157 return NULL;
1158 }
1159}
1160
Radek Krejci056d0a82018-12-06 16:57:25 +01001161API const struct lysp_action *
1162lysp_node_actions(const struct lysp_node *node)
1163{
1164 struct lysp_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001165
Michal Vasko22df3f02020-08-24 13:29:22 +02001166 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001167 if (actions) {
1168 return *actions;
1169 } else {
1170 return NULL;
1171 }
1172}
1173
Radek Krejcibbe09a92018-11-08 09:36:54 +01001174struct lysp_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001175lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001176{
1177 assert(node);
1178 switch (node->nodetype) {
1179 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001180 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001181 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001182 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001183 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001184 return &((struct lysp_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001185 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001186 return &((struct lysp_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001187 default:
1188 return NULL;
1189 }
1190}
1191
Radek Krejci056d0a82018-12-06 16:57:25 +01001192API const struct lysp_notif *
1193lysp_node_notifs(const struct lysp_node *node)
1194{
1195 struct lysp_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001196
Michal Vasko22df3f02020-08-24 13:29:22 +02001197 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001198 if (notifs) {
1199 return *notifs;
1200 } else {
1201 return NULL;
1202 }
1203}
1204
Radek Krejcibbe09a92018-11-08 09:36:54 +01001205struct lysp_node **
Radek Krejci056d0a82018-12-06 16:57:25 +01001206lysp_node_children_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001207{
1208 assert(node);
1209 switch (node->nodetype) {
1210 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001211 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001212 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001213 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001214 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001215 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001216 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001217 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001218 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001219 return &((struct lysp_grp *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001220 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001221 return &((struct lysp_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001222 case LYS_INPUT:
1223 case LYS_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001224 return &((struct lysp_action_inout *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001225 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001226 return &((struct lysp_notif *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001227 default:
1228 return NULL;
1229 }
1230}
1231
Radek Krejci056d0a82018-12-06 16:57:25 +01001232API const struct lysp_node *
1233lysp_node_children(const struct lysp_node *node)
1234{
1235 struct lysp_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001236
1237 if (!node) {
1238 return NULL;
1239 }
1240
Michal Vasko22df3f02020-08-24 13:29:22 +02001241 children = lysp_node_children_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001242 if (children) {
1243 return *children;
1244 } else {
1245 return NULL;
1246 }
1247}
1248
1249struct lysc_action **
1250lysc_node_actions_p(struct lysc_node *node)
1251{
1252 assert(node);
1253 switch (node->nodetype) {
1254 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001255 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001256 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001257 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001258 default:
1259 return NULL;
1260 }
1261}
1262
1263API const struct lysc_action *
1264lysc_node_actions(const struct lysc_node *node)
1265{
1266 struct lysc_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001267
Michal Vasko22df3f02020-08-24 13:29:22 +02001268 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001269 if (actions) {
1270 return *actions;
1271 } else {
1272 return NULL;
1273 }
1274}
1275
1276struct lysc_notif **
1277lysc_node_notifs_p(struct lysc_node *node)
1278{
1279 assert(node);
1280 switch (node->nodetype) {
1281 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001282 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001283 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001284 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001285 default:
1286 return NULL;
1287 }
1288}
1289
1290API const struct lysc_notif *
1291lysc_node_notifs(const struct lysc_node *node)
1292{
1293 struct lysc_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001294
Michal Vasko22df3f02020-08-24 13:29:22 +02001295 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001296 if (notifs) {
1297 return *notifs;
1298 } else {
1299 return NULL;
1300 }
1301}
1302
Radek Krejcibbe09a92018-11-08 09:36:54 +01001303struct lysc_node **
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001304lysc_node_children_p(const struct lysc_node *node, uint16_t flags)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001305{
1306 assert(node);
1307 switch (node->nodetype) {
1308 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001309 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001310 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001311 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001312 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001313 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001314 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001315 return &((struct lysc_node_list *)node)->child;
Michal Vasko1bf09392020-03-27 12:38:10 +01001316 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001317 case LYS_ACTION:
1318 if (flags & LYS_CONFIG_R) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001319 return &((struct lysc_action *)node)->output.data;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001320 } else {
1321 /* LYS_CONFIG_W, but also the default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02001322 return &((struct lysc_action *)node)->input.data;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001323 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02001324 case LYS_INPUT:
1325 case LYS_OUTPUT:
1326 return &((struct lysc_action_inout *)node)->data;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001327 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001328 return &((struct lysc_notif *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001329 default:
1330 return NULL;
1331 }
1332}
1333
Radek Krejci056d0a82018-12-06 16:57:25 +01001334API const struct lysc_node *
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001335lysc_node_children(const struct lysc_node *node, uint16_t flags)
Radek Krejcia3045382018-11-22 14:30:31 +01001336{
Radek Krejci056d0a82018-12-06 16:57:25 +01001337 struct lysc_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001338
1339 if (!node) {
1340 return NULL;
1341 }
1342
Michal Vasko22df3f02020-08-24 13:29:22 +02001343 children = lysc_node_children_p((struct lysc_node *)node, flags);
Radek Krejci056d0a82018-12-06 16:57:25 +01001344 if (children) {
1345 return *children;
1346 } else {
Radek Krejcia3045382018-11-22 14:30:31 +01001347 return NULL;
1348 }
1349}
1350
Michal Vasko2a668712020-10-21 11:48:09 +02001351API const struct lysc_node *
Michal Vasko208a04a2020-10-21 15:17:12 +02001352lysc_node_children_full(const struct lysc_node *node, uint16_t flags)
Michal Vasko2a668712020-10-21 11:48:09 +02001353{
1354 switch (node->nodetype) {
1355 case LYS_CONTAINER:
1356 return ((struct lysc_node_container *)node)->child;
1357 case LYS_CHOICE:
1358 return (struct lysc_node *)((struct lysc_node_choice *)node)->cases;
1359 case LYS_CASE:
1360 return ((struct lysc_node_case *)node)->child;
1361 case LYS_LIST:
1362 return ((struct lysc_node_list *)node)->child;
1363 case LYS_RPC:
1364 case LYS_ACTION:
1365 if (flags & LYS_CONFIG_R) {
1366 return (struct lysc_node *)&((struct lysc_action *)node)->output;
1367 } else {
1368 /* LYS_CONFIG_W, but also the default case */
1369 return (struct lysc_node *)&((struct lysc_action *)node)->input;
1370 }
1371 case LYS_INPUT:
1372 case LYS_OUTPUT:
1373 return ((struct lysc_action_inout *)node)->data;
1374 case LYS_NOTIF:
1375 return ((struct lysc_notif *)node)->data;
1376 default:
1377 return NULL;
1378 }
1379}
1380
1381API const struct lysc_node *
Michal Vasko208a04a2020-10-21 15:17:12 +02001382lysc_node_parent_full(const struct lysc_node *node)
Michal Vasko2a668712020-10-21 11:48:09 +02001383{
1384 if (!node) {
1385 return NULL;
1386 } else if (node->nodetype == LYS_INPUT) {
1387 return (struct lysc_node *)(((char *)node) - offsetof(struct lysc_action, input));
1388 } else if (node->nodetype == LYS_OUTPUT) {
1389 return (struct lysc_node *)(((char *)node) - offsetof(struct lysc_action, output));
1390 } else if (node->parent && (node->parent->nodetype & (LYS_RPC | LYS_ACTION))) {
1391 if (node->flags & LYS_CONFIG_W) {
1392 return (struct lysc_node *)&((struct lysc_action *)node->parent)->input;
1393 } else {
1394 return (struct lysc_node *)&((struct lysc_action *)node->parent)->output;
1395 }
1396 } else {
1397 return node->parent;
1398 }
1399}
1400
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001401struct lys_module *
1402lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1403{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001404 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001405 if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) {
Michal Vasko69730152020-10-09 16:30:07 +02001406 return (struct lys_module *)ctx->list.objs[u];
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001407 }
1408 }
1409 return NULL;
1410}
1411
Radek Krejcid6b76452019-09-03 17:03:03 +02001412enum ly_stmt
Michal Vasko63f3d842020-07-08 10:10:14 +02001413lysp_match_kw(struct lys_yang_parser_ctx *ctx, struct ly_in *in)
David Sedlákc10e7902018-12-17 02:17:59 +01001414{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001415/**
Michal Vasko63f3d842020-07-08 10:10:14 +02001416 * @brief Move the INPUT by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001417 * @param[in] CTX yang parser context to update its indent value.
Michal Vasko63f3d842020-07-08 10:10:14 +02001418 * @param[in,out] IN input to move
David Sedlák1bccdfa2019-06-17 15:55:27 +02001419 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001420 *
1421 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001422 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001423#define MOVE_IN(CTX, IN, COUNT) ly_in_skip(IN, COUNT);if(CTX){(CTX)->indent+=COUNT;}
1424#define IF_KW(STR, LEN, STMT) if (!strncmp(in->current, STR, LEN)) {MOVE_IN(ctx, in, LEN);*kw=STMT;}
1425#define IF_KW_PREFIX(STR, LEN) if (!strncmp(in->current, STR, LEN)) {MOVE_IN(ctx, in, LEN);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001426#define IF_KW_PREFIX_END }
David Sedlák572e7ab2019-06-04 16:01:58 +02001427
Michal Vasko63f3d842020-07-08 10:10:14 +02001428 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001429 enum ly_stmt result = LY_STMT_NONE;
1430 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001431 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001432 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001433 case 'a':
Michal Vasko63f3d842020-07-08 10:10:14 +02001434 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001435 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1436 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1437 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001438 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001439 IF_KW("data", 4, LY_STMT_ANYDATA)
1440 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001441 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001442 break;
1443 case 'b':
Michal Vasko63f3d842020-07-08 10:10:14 +02001444 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001445 IF_KW("ase", 3, LY_STMT_BASE)
1446 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1447 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001448 break;
1449 case 'c':
Michal Vasko63f3d842020-07-08 10:10:14 +02001450 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001451 IF_KW("ase", 3, LY_STMT_CASE)
1452 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001453 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001454 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001455 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001456 IF_KW("ct", 2, LY_STMT_CONTACT)
1457 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001458 IF_KW_PREFIX_END
1459 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001460 break;
1461 case 'd':
Michal Vasko63f3d842020-07-08 10:10:14 +02001462 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001463 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001464 IF_KW("fault", 5, LY_STMT_DEFAULT)
1465 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001466 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001467 IF_KW("e", 1, LY_STMT_DEVIATE)
1468 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001469 IF_KW_PREFIX_END
1470 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001471 break;
1472 case 'e':
Michal Vasko63f3d842020-07-08 10:10:14 +02001473 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001474 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001475 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001476 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1477 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001478 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001479 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001480 break;
1481 case 'f':
Michal Vasko63f3d842020-07-08 10:10:14 +02001482 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001483 IF_KW("eature", 6, LY_STMT_FEATURE)
1484 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001485 break;
1486 case 'g':
Michal Vasko63f3d842020-07-08 10:10:14 +02001487 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001488 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001489 break;
1490 case 'i':
Michal Vasko63f3d842020-07-08 10:10:14 +02001491 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001492 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1493 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1494 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001495 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001496 IF_KW("clude", 5, LY_STMT_INCLUDE)
1497 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001498 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001499 break;
1500 case 'k':
Michal Vasko63f3d842020-07-08 10:10:14 +02001501 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001502 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001503 break;
1504 case 'l':
Michal Vasko63f3d842020-07-08 10:10:14 +02001505 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001506 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001507 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1508 else IF_KW("af", 2, LY_STMT_LEAF)
1509 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001510 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001511 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001512 break;
1513 case 'm':
Michal Vasko63f3d842020-07-08 10:10:14 +02001514 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001515 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001516 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1517 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001518 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001519 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1520 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001521 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001522 IF_KW("ule", 3, LY_STMT_MODULE)
1523 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001524 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001525 break;
1526 case 'n':
Michal Vasko63f3d842020-07-08 10:10:14 +02001527 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001528 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1529 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001530 break;
1531 case 'o':
Michal Vasko63f3d842020-07-08 10:10:14 +02001532 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001533 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001534 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1535 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001536 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001537 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001538 break;
1539 case 'p':
Michal Vasko63f3d842020-07-08 10:10:14 +02001540 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001541 IF_KW("ath", 3, LY_STMT_PATH)
1542 else IF_KW("attern", 6, LY_STMT_PATTERN)
1543 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001544 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001545 IF_KW("fix", 3, LY_STMT_PREFIX)
1546 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001547 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001548 break;
1549 case 'r':
Michal Vasko63f3d842020-07-08 10:10:14 +02001550 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001551 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001552 else IF_KW_PREFIX("e", 1)
1553 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001554 IF_KW("erence", 6, LY_STMT_REFERENCE)
1555 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001556 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001557 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1558 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1559 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001560 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001561 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001562 break;
1563 case 's':
Michal Vasko63f3d842020-07-08 10:10:14 +02001564 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001565 IF_KW("tatus", 5, LY_STMT_STATUS)
1566 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001567 break;
1568 case 't':
Michal Vasko63f3d842020-07-08 10:10:14 +02001569 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001570 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1571 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001572 break;
1573 case 'u':
Michal Vasko63f3d842020-07-08 10:10:14 +02001574 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001575 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001576 IF_KW("que", 3, LY_STMT_UNIQUE)
1577 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001578 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001579 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001580 break;
1581 case 'v':
Michal Vasko63f3d842020-07-08 10:10:14 +02001582 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001583 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001584 break;
1585 case 'w':
Michal Vasko63f3d842020-07-08 10:10:14 +02001586 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001587 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001588 break;
1589 case 'y':
Michal Vasko63f3d842020-07-08 10:10:14 +02001590 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001591 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
1592 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001593 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001594 default:
David Sedlák1bccdfa2019-06-17 15:55:27 +02001595 /* if context is not NULL we are matching keyword from YANG data*/
1596 if (ctx) {
Michal Vasko63f3d842020-07-08 10:10:14 +02001597 if (in->current[0] == ';') {
1598 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001599 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02001600 } else if (in->current[0] == '{') {
1601 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001602 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001603 } else if (in->current[0] == '}') {
1604 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001605 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001606 }
1607 }
David Sedlák23a59a62018-10-26 13:08:02 +02001608 break;
1609 }
1610
Michal Vasko63f3d842020-07-08 10:10:14 +02001611 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02001612 /* the keyword is not terminated */
1613 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001614 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02001615 }
1616
David Sedlák1bccdfa2019-06-17 15:55:27 +02001617#undef IF_KW
1618#undef IF_KW_PREFIX
1619#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01001620#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02001621 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01001622
David Sedlák1bccdfa2019-06-17 15:55:27 +02001623 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02001624}
David Sedlákecf5eb82019-06-03 14:12:44 +02001625
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001626LY_ARRAY_COUNT_TYPE
1627lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, LYEXT_SUBSTMT substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001628{
1629 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1630
Michal Vaskod989ba02020-08-24 10:59:24 +02001631 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001632 if (ext[index].insubstmt == substmt) {
1633 return index;
1634 }
1635 }
1636
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001637 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001638}
1639
Michal Vasko62ed12d2020-05-21 10:08:25 +02001640const struct lysc_node *
1641lysc_data_parent(const struct lysc_node *schema)
1642{
1643 const struct lysc_node *parent;
1644
Radek Krejci1e008d22020-08-17 11:37:37 +02001645 for (parent = schema->parent; parent && (parent->nodetype & (LYS_CHOICE | LYS_CASE)); parent = parent->parent) {}
Michal Vasko62ed12d2020-05-21 10:08:25 +02001646
1647 return parent;
1648}
Michal Vasko00cbf532020-06-15 13:58:47 +02001649
Radek Krejci857189e2020-09-01 13:26:36 +02001650ly_bool
Michal Vasko00cbf532020-06-15 13:58:47 +02001651lysc_is_output(const struct lysc_node *schema)
1652{
1653 const struct lysc_node *parent;
1654
1655 assert(schema);
1656
Radek Krejci1e008d22020-08-17 11:37:37 +02001657 for (parent = schema->parent; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION)); parent = parent->parent) {}
Michal Vasko00cbf532020-06-15 13:58:47 +02001658 if (parent && (schema->flags & LYS_CONFIG_R)) {
1659 return 1;
1660 }
1661 return 0;
1662}