blob: 3ce3f3bef07036523f68e9a9e01067e45014acb3 [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 Krejcie7b95092019-05-15 11:03:07 +020019#include <stdint.h>
Radek Krejci9ed7a192018-10-31 16:23:51 +010020#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020022#include <time.h>
23
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020025#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "hash_table.h"
28#include "log.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020029#include "in_internal.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020030#include "parser_internal.h"
Michal Vasko69730152020-10-09 16:30:07 +020031#include "parser_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020032#include "set.h"
33#include "tree.h"
34#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020035#include "tree_schema_internal.h"
36
Radek Krejci85747952019-06-07 16:43:43 +020037LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +020038lysp_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 +020039{
40 struct lysp_import *i;
41
Michal Vasko69730152020-10-09 16:30:07 +020042 if (module_prefix && (&module_prefix != value) && !strcmp(module_prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010043 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value);
Radek Krejci86d106e2018-10-18 09:53:19 +020044 return LY_EEXIST;
45 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +010046 LY_ARRAY_FOR(imports, struct lysp_import, i) {
Michal Vasko69730152020-10-09 16:30:07 +020047 if (i->prefix && (&i->prefix != value) && !strcmp(i->prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010048 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +010049 return LY_EEXIST;
Radek Krejci86d106e2018-10-18 09:53:19 +020050 }
51 }
52 return LY_SUCCESS;
53}
54
55LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020056lysp_check_date(struct lys_parser_ctx *ctx, const char *date, uint8_t date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +020057{
Radek Krejci86d106e2018-10-18 09:53:19 +020058 struct tm tm, tm_;
59 char *r;
60
Michal Vaskob36053d2020-03-26 15:49:30 +010061 LY_CHECK_ARG_RET(ctx ? PARSER_CTX(ctx) : NULL, date, LY_EINVAL);
62 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 +020063
64 /* check format */
Radek Krejci1deb5be2020-08-26 16:43:36 +020065 for (uint8_t i = 0; i < date_len; i++) {
Michal Vasko69730152020-10-09 16:30:07 +020066 if ((i == 4) || (i == 7)) {
Radek Krejci86d106e2018-10-18 09:53:19 +020067 if (date[i] != '-') {
68 goto error;
69 }
70 } else if (!isdigit(date[i])) {
71 goto error;
72 }
73 }
74
75 /* check content, e.g. 2018-02-31 */
76 memset(&tm, 0, sizeof tm);
77 r = strptime(date, "%Y-%m-%d", &tm);
Michal Vasko69730152020-10-09 16:30:07 +020078 if (!r || (r != &date[LY_REV_SIZE - 1])) {
Radek Krejci86d106e2018-10-18 09:53:19 +020079 goto error;
80 }
81 memcpy(&tm_, &tm, sizeof tm);
82 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
83 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
84 /* checking days is enough, since other errors
85 * have been checked by strptime() */
86 goto error;
87 }
88
89 return LY_SUCCESS;
90
91error:
Radek Krejcid33273d2018-10-25 14:55:52 +020092 if (stmt) {
Radek Krejcibbe09a92018-11-08 09:36:54 +010093 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +010094 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt);
Radek Krejcibbe09a92018-11-08 09:36:54 +010095 } else {
96 LOGVAL(NULL, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, date_len, date, stmt);
97 }
Radek Krejcid33273d2018-10-25 14:55:52 +020098 }
Radek Krejci86d106e2018-10-18 09:53:19 +020099 return LY_EINVAL;
100}
101
102void
103lysp_sort_revisions(struct lysp_revision *revs)
104{
Radek Krejci857189e2020-09-01 13:26:36 +0200105 LY_ARRAY_COUNT_TYPE i, r;
Radek Krejci86d106e2018-10-18 09:53:19 +0200106 struct lysp_revision rev;
107
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200108 for (i = 1, r = 0; revs && i < LY_ARRAY_COUNT(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200109 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200110 r = i;
111 }
112 }
113
114 if (r) {
115 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200116 memcpy(&rev, &revs[0], sizeof rev);
117 memcpy(&revs[0], &revs[r], sizeof rev);
118 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200119 }
120}
Radek Krejci151a5b72018-10-19 14:21:44 +0200121
Radek Krejcibbe09a92018-11-08 09:36:54 +0100122static const struct lysp_tpdf *
123lysp_type_match(const char *name, struct lysp_node *node)
124{
Radek Krejci0fb28562018-12-13 15:17:37 +0100125 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200126 LY_ARRAY_COUNT_TYPE u;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100127
Radek Krejci0fb28562018-12-13 15:17:37 +0100128 typedefs = lysp_node_typedefs(node);
129 LY_ARRAY_FOR(typedefs, u) {
130 if (!strcmp(name, typedefs[u].name)) {
131 /* match */
132 return &typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100133 }
134 }
135
136 return NULL;
137}
138
Radek Krejci4f28eda2018-11-12 11:46:16 +0100139static LY_DATA_TYPE
140lysp_type_str2builtin(const char *name, size_t len)
141{
142 if (len >= 4) { /* otherwise it does not match any built-in type */
143 if (name[0] == 'b') {
144 if (name[1] == 'i') {
Michal Vasko69730152020-10-09 16:30:07 +0200145 if ((len == 6) && !strncmp(&name[2], "nary", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100146 return LY_TYPE_BINARY;
Michal Vasko69730152020-10-09 16:30:07 +0200147 } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100148 return LY_TYPE_BITS;
149 }
Michal Vasko69730152020-10-09 16:30:07 +0200150 } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100151 return LY_TYPE_BOOL;
152 }
153 } else if (name[0] == 'd') {
Michal Vasko69730152020-10-09 16:30:07 +0200154 if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100155 return LY_TYPE_DEC64;
156 }
157 } else if (name[0] == 'e') {
Michal Vasko69730152020-10-09 16:30:07 +0200158 if ((len == 5) && !strncmp(&name[1], "mpty", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100159 return LY_TYPE_EMPTY;
Michal Vasko69730152020-10-09 16:30:07 +0200160 } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100161 return LY_TYPE_ENUM;
162 }
163 } else if (name[0] == 'i') {
164 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200165 if ((len == 4) && !strncmp(&name[2], "t8", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100166 return LY_TYPE_INT8;
167 } else if (len == 5) {
168 if (!strncmp(&name[2], "t16", 3)) {
169 return LY_TYPE_INT16;
170 } else if (!strncmp(&name[2], "t32", 3)) {
171 return LY_TYPE_INT32;
172 } else if (!strncmp(&name[2], "t64", 3)) {
173 return LY_TYPE_INT64;
174 }
Michal Vasko69730152020-10-09 16:30:07 +0200175 } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100176 return LY_TYPE_INST;
177 }
Michal Vasko69730152020-10-09 16:30:07 +0200178 } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100179 return LY_TYPE_IDENT;
180 }
181 } else if (name[0] == 'l') {
Michal Vasko69730152020-10-09 16:30:07 +0200182 if ((len == 7) && !strncmp(&name[1], "eafref", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100183 return LY_TYPE_LEAFREF;
184 }
185 } else if (name[0] == 's') {
Michal Vasko69730152020-10-09 16:30:07 +0200186 if ((len == 6) && !strncmp(&name[1], "tring", 5)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100187 return LY_TYPE_STRING;
188 }
189 } else if (name[0] == 'u') {
190 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200191 if ((len == 5) && !strncmp(&name[2], "ion", 3)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100192 return LY_TYPE_UNION;
193 }
Michal Vasko69730152020-10-09 16:30:07 +0200194 } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) {
195 if ((len == 5) && (name[4] == '8')) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100196 return LY_TYPE_UINT8;
197 } else if (len == 6) {
198 if (!strncmp(&name[4], "16", 2)) {
199 return LY_TYPE_UINT16;
200 } else if (!strncmp(&name[4], "32", 2)) {
201 return LY_TYPE_UINT32;
202 } else if (!strncmp(&name[4], "64", 2)) {
203 return LY_TYPE_UINT64;
204 }
205 }
206 }
207 }
208 }
209
210 return LY_TYPE_UNKNOWN;
211}
212
Radek Krejcibbe09a92018-11-08 09:36:54 +0100213LY_ERR
214lysp_type_find(const char *id, struct lysp_node *start_node, struct lysp_module *start_module,
Radek Krejci0f969882020-08-21 16:56:47 +0200215 LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node, struct lysp_module **module)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100216{
217 const char *str, *name;
218 struct lysp_tpdf *typedefs;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200219 struct lys_module *mod;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200220 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100221
222 assert(id);
223 assert(start_module);
224 assert(tpdf);
225 assert(node);
226 assert(module);
227
Radek Krejci4f28eda2018-11-12 11:46:16 +0100228 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100229 str = strchr(id, ':');
230 if (str) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200231 mod = lysp_module_find_prefix(start_module, id, str - id);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200232 *module = mod ? mod->parsed : NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100233 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100234 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100235 } else {
236 *module = start_module;
237 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100238
239 /* check for built-in types */
240 *type = lysp_type_str2builtin(name, strlen(name));
241 if (*type) {
242 *tpdf = NULL;
243 return LY_SUCCESS;
244 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100245 }
246 LY_CHECK_RET(!(*module), LY_ENOTFOUND);
247
Michal Vasko69730152020-10-09 16:30:07 +0200248 if (start_node && (*module == start_module)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100249 /* search typedefs in parent's nodes */
250 *node = start_node;
251 while (*node) {
252 *tpdf = lysp_type_match(name, *node);
253 if (*tpdf) {
254 /* match */
255 return LY_SUCCESS;
256 }
257 *node = (*node)->parent;
258 }
259 }
260
261 /* search in top-level typedefs */
262 if ((*module)->typedefs) {
263 LY_ARRAY_FOR((*module)->typedefs, u) {
264 if (!strcmp(name, (*module)->typedefs[u].name)) {
265 /* match */
266 *tpdf = &(*module)->typedefs[u];
267 return LY_SUCCESS;
268 }
269 }
270 }
271
272 /* search in submodules' typedefs */
273 LY_ARRAY_FOR((*module)->includes, u) {
274 typedefs = (*module)->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100275 LY_ARRAY_FOR(typedefs, v) {
276 if (!strcmp(name, typedefs[v].name)) {
277 /* match */
278 *tpdf = &typedefs[v];
279 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100280 }
281 }
282 }
283
284 return LY_ENOTFOUND;
285}
286
David Sedlák6544c182019-07-12 13:17:33 +0200287LY_ERR
David Sedlák07869a52019-07-12 14:28:19 +0200288lysp_check_enum_name(struct lys_parser_ctx *ctx, const char *name, size_t name_len)
David Sedlák6544c182019-07-12 13:17:33 +0200289{
290 if (!name_len) {
291 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
292 return LY_EVALID;
293 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
294 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
Michal Vasko69730152020-10-09 16:30:07 +0200295 name_len, name);
David Sedlák6544c182019-07-12 13:17:33 +0200296 return LY_EVALID;
297 } else {
298 for (size_t u = 0; u < name_len; ++u) {
299 if (iscntrl(name[u])) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100300 LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
Michal Vasko69730152020-10-09 16:30:07 +0200301 name_len, name, u + 1);
David Sedlák6544c182019-07-12 13:17:33 +0200302 break;
303 }
304 }
305 }
306
307 return LY_SUCCESS;
308}
309
Michal Vaskob36053d2020-03-26 15:49:30 +0100310/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100311 * @brief Check name of a new type to avoid name collisions.
312 *
313 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
314 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
315 * @param[in] tpdf Typedef definition to check.
316 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
317 * typedefs are checked, caller is supposed to free the table.
318 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
319 * typedefs are checked, caller is supposed to free the table.
320 * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise.
321 */
322static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100323lysp_check_dup_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
Radek Krejci0f969882020-08-21 16:56:47 +0200324 struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100325{
326 struct lysp_node *parent;
327 uint32_t hash;
328 size_t name_len;
329 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200330 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100331 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100332
333 assert(ctx);
334 assert(tpdf);
335
336 name = tpdf->name;
337 name_len = strlen(name);
338
Radek Krejci4f28eda2018-11-12 11:46:16 +0100339 if (lysp_type_str2builtin(name, name_len)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100340 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with a built-in type.", name);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100341 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100342 }
343
344 /* check locally scoped typedefs (avoid name shadowing) */
345 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100346 typedefs = lysp_node_typedefs(node);
347 LY_ARRAY_FOR(typedefs, u) {
348 if (&typedefs[u] == tpdf) {
349 break;
350 }
351 if (!strcmp(name, typedefs[u].name)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100352 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with sibling type.", name);
Radek Krejci0fb28562018-12-13 15:17:37 +0100353 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100354 }
355 }
356 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200357 for (parent = node->parent; parent; parent = parent->parent) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100358 if (lysp_type_match(name, parent)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100359 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with another scoped type.", name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100360 return LY_EEXIST;
361 }
362 }
363 }
364
365 /* check collision with the top-level typedefs */
366 hash = dict_hash(name, name_len);
367 if (node) {
368 lyht_insert(tpdfs_scoped, &name, hash, NULL);
369 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100370 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - scoped type collide with a top-level type.", name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100371 return LY_EEXIST;
372 }
373 } else {
374 if (lyht_insert(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100375 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with another top-level type.", name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100376 return LY_EEXIST;
377 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100378 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
379 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
380 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100381 }
382
383 return LY_SUCCESS;
384}
385
Radek Krejci857189e2020-09-01 13:26:36 +0200386/**
387 * @brief Compare identifiers.
388 * Implementation of ::values_equal_cb.
389 */
390static ly_bool
391lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Radek Krejcibbe09a92018-11-08 09:36:54 +0100392{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200393 return strcmp(val1, val2) == 0 ? 1 : 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100394}
395
396LY_ERR
David Sedlákd2ebe572019-07-22 12:53:14 +0200397lysp_parse_finalize_reallocated(struct lys_parser_ctx *ctx, struct lysp_grp *groupings, struct lysp_augment *augments,
Radek Krejci0f969882020-08-21 16:56:47 +0200398 struct lysp_action *actions, struct lysp_notif *notifs)
David Sedlákd2ebe572019-07-22 12:53:14 +0200399{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200400 LY_ARRAY_COUNT_TYPE u, v;
David Sedlákd2ebe572019-07-22 12:53:14 +0200401 struct lysp_node *child;
402
403 /* finalize parent pointers to the reallocated items */
404
405 /* gropings */
406 LY_ARRAY_FOR(groupings, u) {
407 LY_LIST_FOR(groupings[u].data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200408 child->parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200409 }
410 LY_ARRAY_FOR(groupings[u].actions, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200411 groupings[u].actions[v].parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200412 }
413 LY_ARRAY_FOR(groupings[u].notifs, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200414 groupings[u].notifs[v].parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200415 }
416 LY_ARRAY_FOR(groupings[u].groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200417 groupings[u].groupings[v].parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200418 }
419 if (groupings[u].typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200420 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &groupings[u], 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200421 }
422 }
423
424 /* augments */
425 LY_ARRAY_FOR(augments, u) {
426 LY_LIST_FOR(augments[u].child, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200427 child->parent = (struct lysp_node *)&augments[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200428 }
429 LY_ARRAY_FOR(augments[u].actions, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200430 augments[u].actions[v].parent = (struct lysp_node *)&augments[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200431 }
432 LY_ARRAY_FOR(augments[u].notifs, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200433 augments[u].notifs[v].parent = (struct lysp_node *)&augments[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200434 }
435 }
436
437 /* actions */
438 LY_ARRAY_FOR(actions, u) {
439 if (actions[u].input.parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200440 actions[u].input.parent = (struct lysp_node *)&actions[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200441 LY_LIST_FOR(actions[u].input.data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200442 child->parent = (struct lysp_node *)&actions[u].input;
David Sedlákd2ebe572019-07-22 12:53:14 +0200443 }
444 LY_ARRAY_FOR(actions[u].input.groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200445 actions[u].input.groupings[v].parent = (struct lysp_node *)&actions[u].input;
David Sedlákd2ebe572019-07-22 12:53:14 +0200446 }
447 if (actions[u].input.typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200448 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &actions[u].input, 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200449 }
450 }
451 if (actions[u].output.parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200452 actions[u].output.parent = (struct lysp_node *)&actions[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200453 LY_LIST_FOR(actions[u].output.data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200454 child->parent = (struct lysp_node *)&actions[u].output;
David Sedlákd2ebe572019-07-22 12:53:14 +0200455 }
456 LY_ARRAY_FOR(actions[u].output.groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200457 actions[u].output.groupings[v].parent = (struct lysp_node *)&actions[u].output;
David Sedlákd2ebe572019-07-22 12:53:14 +0200458 }
459 if (actions[u].output.typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200460 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &actions[u].output, 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200461 }
462 }
463 LY_ARRAY_FOR(actions[u].groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200464 actions[u].groupings[v].parent = (struct lysp_node *)&actions[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200465 }
466 if (actions[u].typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200467 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &actions[u], 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200468 }
469 }
470
471 /* notifications */
472 LY_ARRAY_FOR(notifs, u) {
473 LY_LIST_FOR(notifs[u].data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200474 child->parent = (struct lysp_node *)&notifs[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200475 }
476 LY_ARRAY_FOR(notifs[u].groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200477 notifs[u].groupings[v].parent = (struct lysp_node *)&notifs[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200478 }
479 if (notifs[u].typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200480 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &notifs[u], 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200481 }
482 }
483
484 return LY_SUCCESS;
485}
486
487LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100488lysp_check_dup_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100489{
490 struct hash_table *ids_global;
491 struct hash_table *ids_scoped;
Radek Krejci0fb28562018-12-13 15:17:37 +0100492 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200493 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200494 uint32_t i;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100495 LY_ERR ret = LY_EVALID;
496
497 /* check name collisions - typedefs and groupings */
Michal Vasko22df3f02020-08-24 13:29:22 +0200498 ids_global = lyht_new(8, sizeof(char *), lysp_id_cmp, NULL, 1);
499 ids_scoped = lyht_new(8, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200500 LY_ARRAY_FOR(mod->typedefs, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100501 if (lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global, ids_scoped)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100502 goto cleanup;
503 }
504 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200505 LY_ARRAY_FOR(mod->includes, v) {
506 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100507 if (lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global, ids_scoped)) {
Radek Krejci3b1f9292018-11-08 10:58:35 +0100508 goto cleanup;
509 }
510 }
511 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200512 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
513 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
514 LY_ARRAY_FOR(typedefs, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100515 if (lysp_check_dup_typedef(ctx, (struct lysp_node *)ctx->tpdfs_nodes.objs[i], &typedefs[u], ids_global, ids_scoped)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100516 goto cleanup;
517 }
518 }
519 }
520 ret = LY_SUCCESS;
521cleanup:
522 lyht_free(ids_global);
523 lyht_free(ids_scoped);
524 ly_set_erase(&ctx->tpdfs_nodes, NULL);
525
526 return ret;
527}
528
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100529static ly_bool
530ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
531{
532 void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
533
534 return ptr1 == ptr2 ? 1 : 0;
535}
536
537LY_ERR
538lysp_check_dup_features(struct lys_parser_ctx *ctx, struct lysp_module *mod)
539{
540 LY_ARRAY_COUNT_TYPE u;
541 struct hash_table *ht;
542 struct lysp_feature *f;
543 uint32_t hash;
544 LY_ERR ret = LY_SUCCESS, r;
545
546 ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
547 LY_CHECK_RET(!ht, LY_EMEM);
548
549 /* add all module features into a hash table */
550 LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
551 hash = dict_hash(f->name, strlen(f->name));
552 r = lyht_insert(ht, &f->name, hash, NULL);
553 if (r == LY_EEXIST) {
554 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
555 ret = LY_EVALID;
556 goto cleanup;
557 } else if (r) {
558 ret = r;
559 goto cleanup;
560 }
561 }
562
563 /* add all submodule features into a hash table */
564 LY_ARRAY_FOR(mod->includes, u) {
565 LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
566 hash = dict_hash(f->name, strlen(f->name));
567 r = lyht_insert(ht, &f->name, hash, NULL);
568 if (r == LY_EEXIST) {
569 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
570 ret = LY_EVALID;
571 goto cleanup;
572 } else if (r) {
573 ret = r;
574 goto cleanup;
575 }
576 }
577 }
578
579cleanup:
580 lyht_free(ht);
581 return ret;
582}
583
584LY_ERR
585lysp_check_dup_identities(struct lys_parser_ctx *ctx, struct lysp_module *mod)
586{
587 LY_ARRAY_COUNT_TYPE u;
588 struct hash_table *ht;
589 struct lysp_ident *i;
590 uint32_t hash;
591 LY_ERR ret = LY_SUCCESS, r;
592
593 ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
594 LY_CHECK_RET(!ht, LY_EMEM);
595
596 /* add all module identities into a hash table */
597 LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
598 hash = dict_hash(i->name, strlen(i->name));
599 r = lyht_insert(ht, &i->name, hash, NULL);
600 if (r == LY_EEXIST) {
601 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
602 ret = LY_EVALID;
603 goto cleanup;
604 } else if (r) {
605 ret = r;
606 goto cleanup;
607 }
608 }
609
610 /* add all submodule identities into a hash table */
611 LY_ARRAY_FOR(mod->includes, u) {
612 LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
613 hash = dict_hash(i->name, strlen(i->name));
614 r = lyht_insert(ht, &i->name, hash, NULL);
615 if (r == LY_EEXIST) {
616 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
617 ret = LY_EVALID;
618 goto cleanup;
619 } else if (r) {
620 ret = r;
621 goto cleanup;
622 }
623 }
624 }
625
626cleanup:
627 lyht_free(ht);
628 return ret;
629}
630
Radek Krejci9ed7a192018-10-31 16:23:51 +0100631struct lysp_load_module_check_data {
632 const char *name;
633 const char *revision;
634 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200635 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100636};
637
638static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100639lysp_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 +0100640{
641 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100642 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200643 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100644 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100645 struct lysp_revision *revs;
646
647 name = mod ? mod->mod->name : submod->name;
648 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200649 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100650
651 if (info->name) {
652 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100653 if (strcmp(info->name, name)) {
654 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100655 return LY_EINVAL;
656 }
657 }
658 if (info->revision) {
659 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100660 if (!revs || strcmp(info->revision, revs[0].date)) {
661 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200662 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100663 return LY_EINVAL;
664 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200665 } else if (!latest_revision) {
666 /* do not log, we just need to drop the schema and use the latest revision from the context */
667 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100668 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100669 if (submod) {
670 assert(info->submoduleof);
671
Radek Krejci9ed7a192018-10-31 16:23:51 +0100672 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200673 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100674 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 +0200675 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100676 return LY_EVALID;
677 }
678 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100679 if (submod->parsing) {
680 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100681 return LY_EVALID;
682 }
683 }
684 if (info->path) {
685 /* check that name and revision match filename */
686 filename = strrchr(info->path, '/');
687 if (!filename) {
688 filename = info->path;
689 } else {
690 filename++;
691 }
692 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100693 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100694 rev = strchr(filename, '@');
695 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100696 if (strncmp(filename, name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +0200697 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100698 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100699 }
700 /* revision */
701 if (rev) {
702 len = dot - ++rev;
Michal Vasko69730152020-10-09 16:30:07 +0200703 if (!revs || (len != 10) || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100704 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +0200705 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100706 }
707 }
708 }
709 return LY_SUCCESS;
710}
711
712LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100713lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, const char **features, ly_bool implement,
Radek Krejci857189e2020-09-01 13:26:36 +0200714 struct lys_parser_ctx *main_ctx, const char *main_name, ly_bool required, 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 Vasko7b1ad1a2020-11-02 15:41:27 +0100746 ret = lys_create_module(ctx, in, format, implement, lysp_load_module_check, &check_data, features,
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
Radek Krejci857189e2020-09-01 13:26:36 +0200763lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, ly_bool implement, ly_bool require_parsed,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100764 const char **features, 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};
Radek Krejcib3289d62019-09-18 12:21:39 +0200771 struct lys_module *m = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200772 struct ly_in *in;
Radek Krejci086c7132018-10-26 15:29:04 +0200773
Radek Krejci0af46292019-01-11 16:02:31 +0100774 assert(mod);
775
Michal Vasko25d6ad02020-10-22 12:20:22 +0200776 if (ctx->flags & LY_CTX_ALL_IMPLEMENTED) {
Radek Krejcia53d7c92020-08-21 11:30:56 +0200777 implement = 1;
778 }
779
Radek Krejci0af46292019-01-11 16:02:31 +0100780 if (!*mod) {
781 /* try to get the module from the context */
782 if (revision) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200783 /* get the specific revision */
Michal Vasko22df3f02020-08-24 13:29:22 +0200784 *mod = (struct lys_module *)ly_ctx_get_module(ctx, name, revision);
Radek Krejcied5acc52019-04-25 15:57:04 +0200785 } else if (implement) {
786 /* prefer the implemented module instead of the latest one */
Michal Vasko22df3f02020-08-24 13:29:22 +0200787 *mod = (struct lys_module *)ly_ctx_get_module_implemented(ctx, name);
Radek Krejcied5acc52019-04-25 15:57:04 +0200788 if (!*mod) {
789 /* there is no implemented module in the context, try to get the latest revision module */
790 goto latest_in_the_context;
791 }
Radek Krejci0af46292019-01-11 16:02:31 +0100792 } else {
Radek Krejcied5acc52019-04-25 15:57:04 +0200793 /* get the requested module of the latest revision in the context */
794latest_in_the_context:
Michal Vasko22df3f02020-08-24 13:29:22 +0200795 *mod = (struct lys_module *)ly_ctx_get_module_latest(ctx, name);
Michal Vasko69730152020-10-09 16:30:07 +0200796 if (*mod && ((*mod)->latest_revision == 1)) {
Radek Krejcib3289d62019-09-18 12:21:39 +0200797 /* let us now search with callback and searchpaths to check if there is newer revision outside the context */
798 m = *mod;
799 *mod = NULL;
800 }
Radek Krejci0af46292019-01-11 16:02:31 +0100801 }
Radek Krejci086c7132018-10-26 15:29:04 +0200802 }
803
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100804 if (!(*mod) || (require_parsed && !(*mod)->parsed)) {
805 (*mod) = NULL;
806
Radek Krejci086c7132018-10-26 15:29:04 +0200807 /* check collision with other implemented revision */
808 if (implement && ly_ctx_get_module_implemented(ctx, name)) {
809 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);
Radek Krejci086c7132018-10-26 15:29:04 +0200811 return LY_EDENIED;
812 }
813
Radek Krejci9ed7a192018-10-31 16:23:51 +0100814 /* module not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200815 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
816search_clb:
817 if (ctx->imp_clb) {
818 if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
Michal Vasko69730152020-10-09 16:30:07 +0200819 &format, &module_data, &module_data_free) == LY_SUCCESS) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200820 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
Radek Krejci9ed7a192018-10-31 16:23:51 +0100821 check_data.name = name;
822 check_data.revision = revision;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100823 lys_create_module(ctx, in, format, implement, lysp_load_module_check, &check_data, features, mod);
Michal Vasko63f3d842020-07-08 10:10:14 +0200824 ly_in_free(in, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100825 if (module_data_free) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200826 module_data_free((void *)module_data, ctx->imp_clb_data);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100827 }
Radek Krejci086c7132018-10-26 15:29:04 +0200828 }
829 }
830 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
831 goto search_file;
832 }
833 } else {
834search_file:
835 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
836 /* module was not received from the callback or there is no callback set */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100837 lys_module_localfile(ctx, name, revision, features, implement, NULL, NULL, m ? 0 : 1, (void **)mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200838 }
839 if (!(*mod) && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
840 goto search_clb;
841 }
842 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100843
Radek Krejcib3289d62019-09-18 12:21:39 +0200844 /* update the latest_revision flag - here we have selected the latest available schema,
845 * consider that even the callback provides correct latest revision */
846 if (!(*mod) && m) {
Radek Krejci78f06822019-10-30 12:54:05 +0100847 LOGVRB("Newer revision than %s-%s not found, using this as the latest revision.", m->name, m->revision);
Radek Krejcib3289d62019-09-18 12:21:39 +0200848 m->latest_revision = 2;
849 *mod = m;
850 } else if ((*mod) && !revision && ((*mod)->latest_revision == 1)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100851 (*mod)->latest_revision = 2;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100852 }
Radek Krejci086c7132018-10-26 15:29:04 +0200853 } else {
854 /* we have module from the current context */
Radek Krejci0af46292019-01-11 16:02:31 +0100855 if (implement) {
856 m = ly_ctx_get_module_implemented(ctx, name);
Michal Vasko69730152020-10-09 16:30:07 +0200857 if (m && (m != *mod)) {
Radek Krejci0af46292019-01-11 16:02:31 +0100858 /* check collision with other implemented revision */
859 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200860 "Module \"%s\" is already present in other implemented revision.", name);
Radek Krejci0af46292019-01-11 16:02:31 +0100861 *mod = NULL;
862 return LY_EDENIED;
863 }
Radek Krejci086c7132018-10-26 15:29:04 +0200864 }
865
866 /* circular check */
Radek Krejcif8f882a2018-10-31 14:51:15 +0100867 if ((*mod)->parsed && (*mod)->parsed->parsing) {
Radek Krejci086c7132018-10-26 15:29:04 +0200868 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
869 *mod = NULL;
870 return LY_EVALID;
871 }
872 }
873 if (!(*mod)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100874 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "%s \"%s\" module failed.", implement ? "Loading" : "Importing", name);
Radek Krejci086c7132018-10-26 15:29:04 +0200875 return LY_EVALID;
876 }
877
878 if (implement) {
879 /* mark the module implemented, check for collision was already done */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100880 (*mod)->implemented = 1;
Radek Krejci086c7132018-10-26 15:29:04 +0200881 }
Radek Krejci086c7132018-10-26 15:29:04 +0200882
883 return LY_SUCCESS;
884}
885
886LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200887lysp_check_stringchar(struct lys_parser_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +0200888{
889 if (!is_yangutf8char(c)) {
890 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
891 return LY_EVALID;
892 }
893 return LY_SUCCESS;
894}
895
896LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200897lysp_check_identifierchar(struct lys_parser_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +0200898{
Michal Vasko69730152020-10-09 16:30:07 +0200899 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +0200900 if (!is_yangidentstartchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200901 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200902 return LY_EVALID;
903 }
904 if (prefix) {
905 if (first) {
906 (*prefix) = 0;
907 } else {
908 (*prefix) = 2;
909 }
910 }
Michal Vasko69730152020-10-09 16:30:07 +0200911 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +0200912 (*prefix) = 1;
913 } else if (!is_yangidentchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200914 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200915 return LY_EVALID;
916 }
917
918 return LY_SUCCESS;
919}
920
921LY_ERR
Michal Vasko7c8439f2020-08-05 13:25:19 +0200922lysp_load_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200923{
Michal Vaskob36053d2020-03-26 15:49:30 +0100924 struct ly_ctx *ctx = (struct ly_ctx *)(PARSER_CTX(pctx));
Radek Krejci3eb299d2019-04-08 15:07:44 +0200925 struct lysp_submodule *submod = NULL;
Radek Krejcid33273d2018-10-25 14:55:52 +0200926 const char *submodule_data = NULL;
927 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200928
Radek Krejcid33273d2018-10-25 14:55:52 +0200929 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100930 struct lysp_load_module_check_data check_data = {0};
Michal Vasko63f3d842020-07-08 10:10:14 +0200931 struct ly_in *in;
Radek Krejcid33273d2018-10-25 14:55:52 +0200932
Radek Krejcibbe09a92018-11-08 09:36:54 +0100933 /* submodule not present in the context, get the input data and parse it */
Michal Vaskob36053d2020-03-26 15:49:30 +0100934 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200935search_clb:
Michal Vaskob36053d2020-03-26 15:49:30 +0100936 if (ctx->imp_clb) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200937 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 +0200938 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200939 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
Radek Krejcibbe09a92018-11-08 09:36:54 +0100940 check_data.name = inc->name;
941 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200942 check_data.submoduleof = pctx->parsed_mod->mod->name;
Michal Vasko7a0b0762020-09-02 16:37:01 +0200943 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, &submod);
Michal Vasko63f3d842020-07-08 10:10:14 +0200944 ly_in_free(in, 0);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100945 if (submodule_data_free) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200946 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200947 }
948 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200949 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100950 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100951 goto search_file;
Radek Krejcid33273d2018-10-25 14:55:52 +0200952 }
Radek Krejci2d31ea72018-10-25 15:46:42 +0200953 } else {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100954search_file:
Michal Vaskob36053d2020-03-26 15:49:30 +0100955 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100956 /* submodule was not received from the callback or there is no callback set */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100957 lys_module_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, NULL, 0, pctx,
958 pctx->parsed_mod->mod->name, 1, (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100959 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100960 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100961 goto search_clb;
962 }
963 }
964 if (submod) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100965 if (!inc->rev[0] && (submod->latest_revision == 1)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100966 /* update the latest_revision flag - here we have selected the latest available schema,
967 * consider that even the callback provides correct latest revision */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100968 submod->latest_revision = 2;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100969 }
970
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100971 inc->submodule = submod;
Radek Krejcid33273d2018-10-25 14:55:52 +0200972 }
973 if (!inc->submodule) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100974 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.",
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200975 inc->name, pctx->parsed_mod->mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200976 return LY_EVALID;
977 }
978
979 return LY_SUCCESS;
980}
981
Radek Krejcice8c1592018-10-29 15:35:51 +0100982struct lys_module *
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200983lysp_module_find_prefix(const struct lysp_module *prefix_mod, const char *prefix, size_t len)
Radek Krejcice8c1592018-10-29 15:35:51 +0100984{
Michal Vasko7c8439f2020-08-05 13:25:19 +0200985 struct lys_module *m = NULL;
986 LY_ARRAY_COUNT_TYPE u;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200987 const char *local_prefix;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100988
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200989 local_prefix = prefix_mod->is_submod ? ((struct lysp_submodule *)prefix_mod)->prefix : prefix_mod->mod->prefix;
990 if (!len || !ly_strncmp(local_prefix, prefix, len)) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200991 /* it is the prefix of the module itself */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200992 m = prefix_mod->mod;
Radek Krejci73dead22019-07-11 16:46:16 +0200993 }
Michal Vasko7c8439f2020-08-05 13:25:19 +0200994
995 /* search in imports */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200996 if (!m) {
997 LY_ARRAY_FOR(prefix_mod->imports, u) {
998 if (!ly_strncmp(prefix_mod->imports[u].prefix, prefix, len)) {
999 m = prefix_mod->imports[u].module;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001000 break;
1001 }
1002 }
Radek Krejcibbe09a92018-11-08 09:36:54 +01001003 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001004
1005 return m;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001006}
1007
Radek Krejci0935f412019-08-20 16:15:18 +02001008API const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001009lys_nodetype2str(uint16_t nodetype)
1010{
Michal Vaskod989ba02020-08-24 10:59:24 +02001011 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001012 case LYS_CONTAINER:
1013 return "container";
1014 case LYS_CHOICE:
1015 return "choice";
1016 case LYS_LEAF:
1017 return "leaf";
1018 case LYS_LEAFLIST:
1019 return "leaf-list";
1020 case LYS_LIST:
1021 return "list";
1022 case LYS_ANYXML:
1023 return "anyxml";
1024 case LYS_ANYDATA:
1025 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001026 case LYS_CASE:
1027 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001028 case LYS_RPC:
1029 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001030 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001031 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001032 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001033 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001034 case LYS_USES:
1035 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001036 default:
1037 return "unknown";
1038 }
1039}
1040
Radek Krejci693262f2019-04-29 15:23:20 +02001041const char *
1042lys_datatype2str(LY_DATA_TYPE basetype)
1043{
Michal Vaskod989ba02020-08-24 10:59:24 +02001044 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001045 case LY_TYPE_BINARY:
1046 return "binary";
1047 case LY_TYPE_UINT8:
1048 return "uint8";
1049 case LY_TYPE_UINT16:
1050 return "uint16";
1051 case LY_TYPE_UINT32:
1052 return "uint32";
1053 case LY_TYPE_UINT64:
1054 return "uint64";
1055 case LY_TYPE_STRING:
1056 return "string";
1057 case LY_TYPE_BITS:
1058 return "bits";
1059 case LY_TYPE_BOOL:
1060 return "boolean";
1061 case LY_TYPE_DEC64:
1062 return "decimal64";
1063 case LY_TYPE_EMPTY:
1064 return "empty";
1065 case LY_TYPE_ENUM:
1066 return "enumeration";
1067 case LY_TYPE_IDENT:
1068 return "identityref";
1069 case LY_TYPE_INST:
1070 return "instance-identifier";
1071 case LY_TYPE_LEAFREF:
1072 return "leafref";
1073 case LY_TYPE_UNION:
1074 return "union";
1075 case LY_TYPE_INT8:
1076 return "int8";
1077 case LY_TYPE_INT16:
1078 return "int16";
1079 case LY_TYPE_INT32:
1080 return "int32";
1081 case LY_TYPE_INT64:
1082 return "int64";
1083 default:
1084 return "unknown";
1085 }
1086}
1087
Radek Krejci056d0a82018-12-06 16:57:25 +01001088API const struct lysp_tpdf *
1089lysp_node_typedefs(const struct lysp_node *node)
1090{
Radek Krejci0fb28562018-12-13 15:17:37 +01001091 switch (node->nodetype) {
1092 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001093 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001094 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001095 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001096 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001097 return ((struct lysp_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001098 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001099 case LYS_ACTION:
Michal Vasko22df3f02020-08-24 13:29:22 +02001100 return ((struct lysp_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001101 case LYS_INPUT:
1102 case LYS_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001103 return ((struct lysp_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001104 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001105 return ((struct lysp_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001106 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001107 return NULL;
1108 }
1109}
1110
Radek Krejci53ea6152018-12-13 15:21:15 +01001111API const struct lysp_grp *
1112lysp_node_groupings(const struct lysp_node *node)
1113{
1114 switch (node->nodetype) {
1115 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001116 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001117 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001118 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001119 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001120 return ((struct lysp_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001121 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001122 case LYS_ACTION:
Michal Vasko22df3f02020-08-24 13:29:22 +02001123 return ((struct lysp_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001124 case LYS_INPUT:
1125 case LYS_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001126 return ((struct lysp_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001127 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001128 return ((struct lysp_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001129 default:
1130 return NULL;
1131 }
1132}
1133
Radek Krejcibbe09a92018-11-08 09:36:54 +01001134struct lysp_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001135lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001136{
1137 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001138
Radek Krejcibbe09a92018-11-08 09:36:54 +01001139 switch (node->nodetype) {
1140 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001141 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001142 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001143 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001144 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001145 return &((struct lysp_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001146 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001147 return &((struct lysp_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001148 default:
1149 return NULL;
1150 }
1151}
1152
Radek Krejci056d0a82018-12-06 16:57:25 +01001153API const struct lysp_action *
1154lysp_node_actions(const struct lysp_node *node)
1155{
1156 struct lysp_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001157
Michal Vasko22df3f02020-08-24 13:29:22 +02001158 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001159 if (actions) {
1160 return *actions;
1161 } else {
1162 return NULL;
1163 }
1164}
1165
Radek Krejcibbe09a92018-11-08 09:36:54 +01001166struct lysp_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001167lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001168{
1169 assert(node);
1170 switch (node->nodetype) {
1171 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001172 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001173 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001174 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001175 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001176 return &((struct lysp_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001177 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001178 return &((struct lysp_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001179 default:
1180 return NULL;
1181 }
1182}
1183
Radek Krejci056d0a82018-12-06 16:57:25 +01001184API const struct lysp_notif *
1185lysp_node_notifs(const struct lysp_node *node)
1186{
1187 struct lysp_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001188
Michal Vasko22df3f02020-08-24 13:29:22 +02001189 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001190 if (notifs) {
1191 return *notifs;
1192 } else {
1193 return NULL;
1194 }
1195}
1196
Radek Krejcibbe09a92018-11-08 09:36:54 +01001197struct lysp_node **
Radek Krejci056d0a82018-12-06 16:57:25 +01001198lysp_node_children_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001199{
1200 assert(node);
1201 switch (node->nodetype) {
1202 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001203 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001204 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001205 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001206 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001207 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001208 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001209 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001210 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001211 return &((struct lysp_grp *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001212 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001213 return &((struct lysp_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001214 case LYS_INPUT:
1215 case LYS_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001216 return &((struct lysp_action_inout *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001217 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001218 return &((struct lysp_notif *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001219 default:
1220 return NULL;
1221 }
1222}
1223
Radek Krejci056d0a82018-12-06 16:57:25 +01001224API const struct lysp_node *
1225lysp_node_children(const struct lysp_node *node)
1226{
1227 struct lysp_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001228
1229 if (!node) {
1230 return NULL;
1231 }
1232
Michal Vasko22df3f02020-08-24 13:29:22 +02001233 children = lysp_node_children_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001234 if (children) {
1235 return *children;
1236 } else {
1237 return NULL;
1238 }
1239}
1240
1241struct lysc_action **
1242lysc_node_actions_p(struct lysc_node *node)
1243{
1244 assert(node);
1245 switch (node->nodetype) {
1246 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001247 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001248 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001249 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001250 default:
1251 return NULL;
1252 }
1253}
1254
1255API const struct lysc_action *
1256lysc_node_actions(const struct lysc_node *node)
1257{
1258 struct lysc_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001259
Michal Vasko22df3f02020-08-24 13:29:22 +02001260 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001261 if (actions) {
1262 return *actions;
1263 } else {
1264 return NULL;
1265 }
1266}
1267
1268struct lysc_notif **
1269lysc_node_notifs_p(struct lysc_node *node)
1270{
1271 assert(node);
1272 switch (node->nodetype) {
1273 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001274 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001275 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001276 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001277 default:
1278 return NULL;
1279 }
1280}
1281
1282API const struct lysc_notif *
1283lysc_node_notifs(const struct lysc_node *node)
1284{
1285 struct lysc_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001286
Michal Vasko22df3f02020-08-24 13:29:22 +02001287 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001288 if (notifs) {
1289 return *notifs;
1290 } else {
1291 return NULL;
1292 }
1293}
1294
Radek Krejcibbe09a92018-11-08 09:36:54 +01001295struct lysc_node **
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001296lysc_node_children_p(const struct lysc_node *node, uint16_t flags)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001297{
1298 assert(node);
1299 switch (node->nodetype) {
1300 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001301 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001302 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001303 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001304 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001305 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001306 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001307 return &((struct lysc_node_list *)node)->child;
Michal Vasko1bf09392020-03-27 12:38:10 +01001308 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001309 case LYS_ACTION:
1310 if (flags & LYS_CONFIG_R) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001311 return &((struct lysc_action *)node)->output.data;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001312 } else {
1313 /* LYS_CONFIG_W, but also the default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02001314 return &((struct lysc_action *)node)->input.data;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001315 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02001316 case LYS_INPUT:
1317 case LYS_OUTPUT:
1318 return &((struct lysc_action_inout *)node)->data;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001319 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001320 return &((struct lysc_notif *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001321 default:
1322 return NULL;
1323 }
1324}
1325
Radek Krejci056d0a82018-12-06 16:57:25 +01001326API const struct lysc_node *
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001327lysc_node_children(const struct lysc_node *node, uint16_t flags)
Radek Krejcia3045382018-11-22 14:30:31 +01001328{
Radek Krejci056d0a82018-12-06 16:57:25 +01001329 struct lysc_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001330
1331 if (!node) {
1332 return NULL;
1333 }
1334
Michal Vasko22df3f02020-08-24 13:29:22 +02001335 children = lysc_node_children_p((struct lysc_node *)node, flags);
Radek Krejci056d0a82018-12-06 16:57:25 +01001336 if (children) {
1337 return *children;
1338 } else {
Radek Krejcia3045382018-11-22 14:30:31 +01001339 return NULL;
1340 }
1341}
1342
Michal Vasko2a668712020-10-21 11:48:09 +02001343API const struct lysc_node *
Michal Vasko208a04a2020-10-21 15:17:12 +02001344lysc_node_children_full(const struct lysc_node *node, uint16_t flags)
Michal Vasko2a668712020-10-21 11:48:09 +02001345{
1346 switch (node->nodetype) {
1347 case LYS_CONTAINER:
1348 return ((struct lysc_node_container *)node)->child;
1349 case LYS_CHOICE:
1350 return (struct lysc_node *)((struct lysc_node_choice *)node)->cases;
1351 case LYS_CASE:
1352 return ((struct lysc_node_case *)node)->child;
1353 case LYS_LIST:
1354 return ((struct lysc_node_list *)node)->child;
1355 case LYS_RPC:
1356 case LYS_ACTION:
1357 if (flags & LYS_CONFIG_R) {
1358 return (struct lysc_node *)&((struct lysc_action *)node)->output;
1359 } else {
1360 /* LYS_CONFIG_W, but also the default case */
1361 return (struct lysc_node *)&((struct lysc_action *)node)->input;
1362 }
1363 case LYS_INPUT:
1364 case LYS_OUTPUT:
1365 return ((struct lysc_action_inout *)node)->data;
1366 case LYS_NOTIF:
1367 return ((struct lysc_notif *)node)->data;
1368 default:
1369 return NULL;
1370 }
1371}
1372
1373API const struct lysc_node *
Michal Vasko208a04a2020-10-21 15:17:12 +02001374lysc_node_parent_full(const struct lysc_node *node)
Michal Vasko2a668712020-10-21 11:48:09 +02001375{
1376 if (!node) {
1377 return NULL;
1378 } else if (node->nodetype == LYS_INPUT) {
1379 return (struct lysc_node *)(((char *)node) - offsetof(struct lysc_action, input));
1380 } else if (node->nodetype == LYS_OUTPUT) {
1381 return (struct lysc_node *)(((char *)node) - offsetof(struct lysc_action, output));
1382 } else if (node->parent && (node->parent->nodetype & (LYS_RPC | LYS_ACTION))) {
1383 if (node->flags & LYS_CONFIG_W) {
1384 return (struct lysc_node *)&((struct lysc_action *)node->parent)->input;
1385 } else {
1386 return (struct lysc_node *)&((struct lysc_action *)node->parent)->output;
1387 }
1388 } else {
1389 return node->parent;
1390 }
1391}
1392
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001393struct lys_module *
1394lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1395{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001396 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001397 if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) {
Michal Vasko69730152020-10-09 16:30:07 +02001398 return (struct lys_module *)ctx->list.objs[u];
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001399 }
1400 }
1401 return NULL;
1402}
1403
Radek Krejcid6b76452019-09-03 17:03:03 +02001404enum ly_stmt
Michal Vasko63f3d842020-07-08 10:10:14 +02001405lysp_match_kw(struct lys_yang_parser_ctx *ctx, struct ly_in *in)
David Sedlákc10e7902018-12-17 02:17:59 +01001406{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001407/**
Michal Vasko63f3d842020-07-08 10:10:14 +02001408 * @brief Move the INPUT by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001409 * @param[in] CTX yang parser context to update its indent value.
Michal Vasko63f3d842020-07-08 10:10:14 +02001410 * @param[in,out] IN input to move
David Sedlák1bccdfa2019-06-17 15:55:27 +02001411 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001412 *
1413 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001414 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001415#define MOVE_IN(CTX, IN, COUNT) ly_in_skip(IN, COUNT);if(CTX){(CTX)->indent+=COUNT;}
1416#define IF_KW(STR, LEN, STMT) if (!strncmp(in->current, STR, LEN)) {MOVE_IN(ctx, in, LEN);*kw=STMT;}
1417#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 +02001418#define IF_KW_PREFIX_END }
David Sedlák572e7ab2019-06-04 16:01:58 +02001419
Michal Vasko63f3d842020-07-08 10:10:14 +02001420 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001421 enum ly_stmt result = LY_STMT_NONE;
1422 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001423 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001424 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001425 case 'a':
Michal Vasko63f3d842020-07-08 10:10:14 +02001426 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001427 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1428 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1429 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001430 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001431 IF_KW("data", 4, LY_STMT_ANYDATA)
1432 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001433 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001434 break;
1435 case 'b':
Michal Vasko63f3d842020-07-08 10:10:14 +02001436 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001437 IF_KW("ase", 3, LY_STMT_BASE)
1438 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1439 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001440 break;
1441 case 'c':
Michal Vasko63f3d842020-07-08 10:10:14 +02001442 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001443 IF_KW("ase", 3, LY_STMT_CASE)
1444 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001445 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001446 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001447 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001448 IF_KW("ct", 2, LY_STMT_CONTACT)
1449 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001450 IF_KW_PREFIX_END
1451 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001452 break;
1453 case 'd':
Michal Vasko63f3d842020-07-08 10:10:14 +02001454 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001455 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001456 IF_KW("fault", 5, LY_STMT_DEFAULT)
1457 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001458 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001459 IF_KW("e", 1, LY_STMT_DEVIATE)
1460 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001461 IF_KW_PREFIX_END
1462 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001463 break;
1464 case 'e':
Michal Vasko63f3d842020-07-08 10:10:14 +02001465 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001466 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001467 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001468 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1469 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001470 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001471 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001472 break;
1473 case 'f':
Michal Vasko63f3d842020-07-08 10:10:14 +02001474 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001475 IF_KW("eature", 6, LY_STMT_FEATURE)
1476 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001477 break;
1478 case 'g':
Michal Vasko63f3d842020-07-08 10:10:14 +02001479 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001480 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001481 break;
1482 case 'i':
Michal Vasko63f3d842020-07-08 10:10:14 +02001483 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001484 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1485 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1486 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001487 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001488 IF_KW("clude", 5, LY_STMT_INCLUDE)
1489 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001490 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001491 break;
1492 case 'k':
Michal Vasko63f3d842020-07-08 10:10:14 +02001493 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001494 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001495 break;
1496 case 'l':
Michal Vasko63f3d842020-07-08 10:10:14 +02001497 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001498 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001499 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1500 else IF_KW("af", 2, LY_STMT_LEAF)
1501 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001502 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001503 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001504 break;
1505 case 'm':
Michal Vasko63f3d842020-07-08 10:10:14 +02001506 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001507 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001508 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1509 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001510 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001511 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1512 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001513 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001514 IF_KW("ule", 3, LY_STMT_MODULE)
1515 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001516 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001517 break;
1518 case 'n':
Michal Vasko63f3d842020-07-08 10:10:14 +02001519 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001520 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1521 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001522 break;
1523 case 'o':
Michal Vasko63f3d842020-07-08 10:10:14 +02001524 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001525 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001526 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1527 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001528 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001529 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001530 break;
1531 case 'p':
Michal Vasko63f3d842020-07-08 10:10:14 +02001532 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001533 IF_KW("ath", 3, LY_STMT_PATH)
1534 else IF_KW("attern", 6, LY_STMT_PATTERN)
1535 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001536 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001537 IF_KW("fix", 3, LY_STMT_PREFIX)
1538 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001539 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001540 break;
1541 case 'r':
Michal Vasko63f3d842020-07-08 10:10:14 +02001542 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001543 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001544 else IF_KW_PREFIX("e", 1)
1545 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001546 IF_KW("erence", 6, LY_STMT_REFERENCE)
1547 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001548 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001549 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1550 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1551 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001552 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001553 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001554 break;
1555 case 's':
Michal Vasko63f3d842020-07-08 10:10:14 +02001556 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001557 IF_KW("tatus", 5, LY_STMT_STATUS)
1558 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001559 break;
1560 case 't':
Michal Vasko63f3d842020-07-08 10:10:14 +02001561 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001562 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1563 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001564 break;
1565 case 'u':
Michal Vasko63f3d842020-07-08 10:10:14 +02001566 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001567 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001568 IF_KW("que", 3, LY_STMT_UNIQUE)
1569 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001570 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001571 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001572 break;
1573 case 'v':
Michal Vasko63f3d842020-07-08 10:10:14 +02001574 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001575 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001576 break;
1577 case 'w':
Michal Vasko63f3d842020-07-08 10:10:14 +02001578 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001579 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001580 break;
1581 case 'y':
Michal Vasko63f3d842020-07-08 10:10:14 +02001582 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001583 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
1584 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001585 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001586 default:
David Sedlák1bccdfa2019-06-17 15:55:27 +02001587 /* if context is not NULL we are matching keyword from YANG data*/
1588 if (ctx) {
Michal Vasko63f3d842020-07-08 10:10:14 +02001589 if (in->current[0] == ';') {
1590 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001591 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02001592 } else if (in->current[0] == '{') {
1593 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001594 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001595 } else if (in->current[0] == '}') {
1596 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001597 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001598 }
1599 }
David Sedlák23a59a62018-10-26 13:08:02 +02001600 break;
1601 }
1602
Michal Vasko63f3d842020-07-08 10:10:14 +02001603 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02001604 /* the keyword is not terminated */
1605 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001606 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02001607 }
1608
David Sedlák1bccdfa2019-06-17 15:55:27 +02001609#undef IF_KW
1610#undef IF_KW_PREFIX
1611#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01001612#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02001613 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01001614
David Sedlák1bccdfa2019-06-17 15:55:27 +02001615 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02001616}
David Sedlákecf5eb82019-06-03 14:12:44 +02001617
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001618LY_ARRAY_COUNT_TYPE
1619lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, LYEXT_SUBSTMT substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001620{
1621 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1622
Michal Vaskod989ba02020-08-24 10:59:24 +02001623 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001624 if (ext[index].insubstmt == substmt) {
1625 return index;
1626 }
1627 }
1628
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001629 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001630}
1631
Michal Vasko62ed12d2020-05-21 10:08:25 +02001632const struct lysc_node *
1633lysc_data_parent(const struct lysc_node *schema)
1634{
1635 const struct lysc_node *parent;
1636
Radek Krejci1e008d22020-08-17 11:37:37 +02001637 for (parent = schema->parent; parent && (parent->nodetype & (LYS_CHOICE | LYS_CASE)); parent = parent->parent) {}
Michal Vasko62ed12d2020-05-21 10:08:25 +02001638
1639 return parent;
1640}
Michal Vasko00cbf532020-06-15 13:58:47 +02001641
Radek Krejci857189e2020-09-01 13:26:36 +02001642ly_bool
Michal Vasko00cbf532020-06-15 13:58:47 +02001643lysc_is_output(const struct lysc_node *schema)
1644{
1645 const struct lysc_node *parent;
1646
1647 assert(schema);
1648
Radek Krejci1e008d22020-08-17 11:37:37 +02001649 for (parent = schema->parent; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION)); parent = parent->parent) {}
Michal Vasko00cbf532020-06-15 13:58:47 +02001650 if (parent && (schema->flags & LYS_CONFIG_R)) {
1651 return 1;
1652 }
1653 return 0;
1654}
Michal Vaskod975f862020-06-23 13:29:28 +02001655
Radek Krejci857189e2020-09-01 13:26:36 +02001656API ly_bool
Michal Vaskod975f862020-06-23 13:29:28 +02001657lysc_is_userordered(const struct lysc_node *schema)
1658{
1659 if (!schema || !(schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) || !(schema->flags & LYS_ORDBY_USER)) {
1660 return 0;
1661 }
1662
1663 return 1;
1664}