blob: a5f6ef556ac67f4a1ece83cf48f10c3782eddc38 [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
Radek Krejcie7b95092019-05-15 11:03:07 +0200323lysp_check_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
Radek Krejcie7b95092019-05-15 11:03:07 +0200488lysp_check_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) {
501 if (lysp_check_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) {
507 if (lysp_check_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) {
515 if (lysp_check_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
Radek Krejci9ed7a192018-10-31 16:23:51 +0100529struct lysp_load_module_check_data {
530 const char *name;
531 const char *revision;
532 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200533 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100534};
535
536static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100537lysp_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 +0100538{
539 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100540 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200541 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100542 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100543 struct lysp_revision *revs;
544
545 name = mod ? mod->mod->name : submod->name;
546 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200547 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100548
549 if (info->name) {
550 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100551 if (strcmp(info->name, name)) {
552 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100553 return LY_EINVAL;
554 }
555 }
556 if (info->revision) {
557 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100558 if (!revs || strcmp(info->revision, revs[0].date)) {
559 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200560 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100561 return LY_EINVAL;
562 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200563 } else if (!latest_revision) {
564 /* do not log, we just need to drop the schema and use the latest revision from the context */
565 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100566 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100567 if (submod) {
568 assert(info->submoduleof);
569
Radek Krejci9ed7a192018-10-31 16:23:51 +0100570 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200571 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100572 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 +0200573 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100574 return LY_EVALID;
575 }
576 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100577 if (submod->parsing) {
578 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100579 return LY_EVALID;
580 }
581 }
582 if (info->path) {
583 /* check that name and revision match filename */
584 filename = strrchr(info->path, '/');
585 if (!filename) {
586 filename = info->path;
587 } else {
588 filename++;
589 }
590 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100591 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100592 rev = strchr(filename, '@');
593 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100594 if (strncmp(filename, name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +0200595 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100596 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100597 }
598 /* revision */
599 if (rev) {
600 len = dot - ++rev;
Michal Vasko69730152020-10-09 16:30:07 +0200601 if (!revs || (len != 10) || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100602 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +0200603 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100604 }
605 }
606 }
607 return LY_SUCCESS;
608}
609
610LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200611lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, ly_bool implement,
612 struct lys_parser_ctx *main_ctx, const char *main_name, ly_bool required, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100613{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200614 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100615 char *filepath = NULL;
616 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100617 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100618 LY_ERR ret = LY_SUCCESS;
619 struct lysp_load_module_check_data check_data = {0};
620
621 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 +0200622 &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200623 if (!filepath) {
624 if (required) {
625 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200626 revision ? revision : "");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200627 }
628 return LY_ENOTFOUND;
629 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100630
631 LOGVRB("Loading schema from \"%s\" file.", filepath);
632
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200633 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200634 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +0200635 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100636 check_data.name = name;
637 check_data.revision = revision;
638 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800639 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200640 if (main_ctx) {
Michal Vasko7a0b0762020-09-02 16:37:01 +0200641 ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data,
Michal Vasko69730152020-10-09 16:30:07 +0200642 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200643 } else {
Michal Vasko7a0b0762020-09-02 16:37:01 +0200644 ret = lys_create_module(ctx, in, format, implement, lysp_load_module_check, &check_data,
Michal Vasko69730152020-10-09 16:30:07 +0200645 (struct lys_module **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200646
647 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200648 ly_in_free(in, 1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200649 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100650
651 *result = mod;
652
653 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200654
Radek Krejci9ed7a192018-10-31 16:23:51 +0100655cleanup:
656 free(filepath);
657 return ret;
658}
659
Radek Krejcid33273d2018-10-25 14:55:52 +0200660LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200661lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, ly_bool implement, ly_bool require_parsed,
Radek Krejci0f969882020-08-21 16:56:47 +0200662 struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200663{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100664 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200665 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200666
Radek Krejci9ed7a192018-10-31 16:23:51 +0100667 void (*module_data_free)(void *module_data, void *user_data) = NULL;
668 struct lysp_load_module_check_data check_data = {0};
Radek Krejcib3289d62019-09-18 12:21:39 +0200669 struct lys_module *m = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200670 struct ly_in *in;
Radek Krejci086c7132018-10-26 15:29:04 +0200671
Radek Krejci0af46292019-01-11 16:02:31 +0100672 assert(mod);
673
Michal Vasko25d6ad02020-10-22 12:20:22 +0200674 if (ctx->flags & LY_CTX_ALL_IMPLEMENTED) {
Radek Krejcia53d7c92020-08-21 11:30:56 +0200675 implement = 1;
676 }
677
Radek Krejci0af46292019-01-11 16:02:31 +0100678 if (!*mod) {
679 /* try to get the module from the context */
680 if (revision) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200681 /* get the specific revision */
Michal Vasko22df3f02020-08-24 13:29:22 +0200682 *mod = (struct lys_module *)ly_ctx_get_module(ctx, name, revision);
Radek Krejcied5acc52019-04-25 15:57:04 +0200683 } else if (implement) {
684 /* prefer the implemented module instead of the latest one */
Michal Vasko22df3f02020-08-24 13:29:22 +0200685 *mod = (struct lys_module *)ly_ctx_get_module_implemented(ctx, name);
Radek Krejcied5acc52019-04-25 15:57:04 +0200686 if (!*mod) {
687 /* there is no implemented module in the context, try to get the latest revision module */
688 goto latest_in_the_context;
689 }
Radek Krejci0af46292019-01-11 16:02:31 +0100690 } else {
Radek Krejcied5acc52019-04-25 15:57:04 +0200691 /* get the requested module of the latest revision in the context */
692latest_in_the_context:
Michal Vasko22df3f02020-08-24 13:29:22 +0200693 *mod = (struct lys_module *)ly_ctx_get_module_latest(ctx, name);
Michal Vasko69730152020-10-09 16:30:07 +0200694 if (*mod && ((*mod)->latest_revision == 1)) {
Radek Krejcib3289d62019-09-18 12:21:39 +0200695 /* let us now search with callback and searchpaths to check if there is newer revision outside the context */
696 m = *mod;
697 *mod = NULL;
698 }
Radek Krejci0af46292019-01-11 16:02:31 +0100699 }
Radek Krejci086c7132018-10-26 15:29:04 +0200700 }
701
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100702 if (!(*mod) || (require_parsed && !(*mod)->parsed)) {
703 (*mod) = NULL;
704
Radek Krejci086c7132018-10-26 15:29:04 +0200705 /* check collision with other implemented revision */
706 if (implement && ly_ctx_get_module_implemented(ctx, name)) {
707 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200708 "Module \"%s\" is already present in other implemented revision.", name);
Radek Krejci086c7132018-10-26 15:29:04 +0200709 return LY_EDENIED;
710 }
711
Radek Krejci9ed7a192018-10-31 16:23:51 +0100712 /* module not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200713 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
714search_clb:
715 if (ctx->imp_clb) {
716 if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
Michal Vasko69730152020-10-09 16:30:07 +0200717 &format, &module_data, &module_data_free) == LY_SUCCESS) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200718 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
Radek Krejci9ed7a192018-10-31 16:23:51 +0100719 check_data.name = name;
720 check_data.revision = revision;
Michal Vasko7a0b0762020-09-02 16:37:01 +0200721 lys_create_module(ctx, in, format, implement, lysp_load_module_check, &check_data, mod);
Michal Vasko63f3d842020-07-08 10:10:14 +0200722 ly_in_free(in, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100723 if (module_data_free) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200724 module_data_free((void *)module_data, ctx->imp_clb_data);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100725 }
Radek Krejci086c7132018-10-26 15:29:04 +0200726 }
727 }
728 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
729 goto search_file;
730 }
731 } else {
732search_file:
733 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
734 /* module was not received from the callback or there is no callback set */
Radek Krejci78f06822019-10-30 12:54:05 +0100735 lys_module_localfile(ctx, name, revision, implement, NULL, NULL, m ? 0 : 1, (void **)mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200736 }
737 if (!(*mod) && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
738 goto search_clb;
739 }
740 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100741
Radek Krejcib3289d62019-09-18 12:21:39 +0200742 /* update the latest_revision flag - here we have selected the latest available schema,
743 * consider that even the callback provides correct latest revision */
744 if (!(*mod) && m) {
Radek Krejci78f06822019-10-30 12:54:05 +0100745 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 +0200746 m->latest_revision = 2;
747 *mod = m;
748 } else if ((*mod) && !revision && ((*mod)->latest_revision == 1)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100749 (*mod)->latest_revision = 2;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100750 }
Radek Krejci086c7132018-10-26 15:29:04 +0200751 } else {
752 /* we have module from the current context */
Radek Krejci0af46292019-01-11 16:02:31 +0100753 if (implement) {
754 m = ly_ctx_get_module_implemented(ctx, name);
Michal Vasko69730152020-10-09 16:30:07 +0200755 if (m && (m != *mod)) {
Radek Krejci0af46292019-01-11 16:02:31 +0100756 /* check collision with other implemented revision */
757 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200758 "Module \"%s\" is already present in other implemented revision.", name);
Radek Krejci0af46292019-01-11 16:02:31 +0100759 *mod = NULL;
760 return LY_EDENIED;
761 }
Radek Krejci086c7132018-10-26 15:29:04 +0200762 }
763
764 /* circular check */
Radek Krejcif8f882a2018-10-31 14:51:15 +0100765 if ((*mod)->parsed && (*mod)->parsed->parsing) {
Radek Krejci086c7132018-10-26 15:29:04 +0200766 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
767 *mod = NULL;
768 return LY_EVALID;
769 }
770 }
771 if (!(*mod)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100772 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "%s \"%s\" module failed.", implement ? "Loading" : "Importing", name);
Radek Krejci086c7132018-10-26 15:29:04 +0200773 return LY_EVALID;
774 }
775
776 if (implement) {
777 /* mark the module implemented, check for collision was already done */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100778 (*mod)->implemented = 1;
Radek Krejci086c7132018-10-26 15:29:04 +0200779 }
Radek Krejci086c7132018-10-26 15:29:04 +0200780
781 return LY_SUCCESS;
782}
783
784LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200785lysp_check_stringchar(struct lys_parser_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +0200786{
787 if (!is_yangutf8char(c)) {
788 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
789 return LY_EVALID;
790 }
791 return LY_SUCCESS;
792}
793
794LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200795lysp_check_identifierchar(struct lys_parser_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +0200796{
Michal Vasko69730152020-10-09 16:30:07 +0200797 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +0200798 if (!is_yangidentstartchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200799 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200800 return LY_EVALID;
801 }
802 if (prefix) {
803 if (first) {
804 (*prefix) = 0;
805 } else {
806 (*prefix) = 2;
807 }
808 }
Michal Vasko69730152020-10-09 16:30:07 +0200809 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +0200810 (*prefix) = 1;
811 } else if (!is_yangidentchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200812 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200813 return LY_EVALID;
814 }
815
816 return LY_SUCCESS;
817}
818
819LY_ERR
Michal Vasko7c8439f2020-08-05 13:25:19 +0200820lysp_load_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200821{
Michal Vaskob36053d2020-03-26 15:49:30 +0100822 struct ly_ctx *ctx = (struct ly_ctx *)(PARSER_CTX(pctx));
Radek Krejci3eb299d2019-04-08 15:07:44 +0200823 struct lysp_submodule *submod = NULL;
Radek Krejcid33273d2018-10-25 14:55:52 +0200824 const char *submodule_data = NULL;
825 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200826
Radek Krejcid33273d2018-10-25 14:55:52 +0200827 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100828 struct lysp_load_module_check_data check_data = {0};
Michal Vasko63f3d842020-07-08 10:10:14 +0200829 struct ly_in *in;
Radek Krejcid33273d2018-10-25 14:55:52 +0200830
Radek Krejcibbe09a92018-11-08 09:36:54 +0100831 /* submodule not present in the context, get the input data and parse it */
Michal Vaskob36053d2020-03-26 15:49:30 +0100832 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200833search_clb:
Michal Vaskob36053d2020-03-26 15:49:30 +0100834 if (ctx->imp_clb) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200835 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 +0200836 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200837 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
Radek Krejcibbe09a92018-11-08 09:36:54 +0100838 check_data.name = inc->name;
839 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200840 check_data.submoduleof = pctx->parsed_mod->mod->name;
Michal Vasko7a0b0762020-09-02 16:37:01 +0200841 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, &submod);
Michal Vasko63f3d842020-07-08 10:10:14 +0200842 ly_in_free(in, 0);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100843 if (submodule_data_free) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200844 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200845 }
846 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200847 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100848 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100849 goto search_file;
Radek Krejcid33273d2018-10-25 14:55:52 +0200850 }
Radek Krejci2d31ea72018-10-25 15:46:42 +0200851 } else {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100852search_file:
Michal Vaskob36053d2020-03-26 15:49:30 +0100853 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100854 /* submodule was not received from the callback or there is no callback set */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200855 lys_module_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, 0, pctx, pctx->parsed_mod->mod->name, 1,
Michal Vasko22df3f02020-08-24 13:29:22 +0200856 (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100857 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100858 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100859 goto search_clb;
860 }
861 }
862 if (submod) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100863 if (!inc->rev[0] && (submod->latest_revision == 1)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100864 /* update the latest_revision flag - here we have selected the latest available schema,
865 * consider that even the callback provides correct latest revision */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100866 submod->latest_revision = 2;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100867 }
868
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100869 inc->submodule = submod;
Radek Krejcid33273d2018-10-25 14:55:52 +0200870 }
871 if (!inc->submodule) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100872 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.",
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200873 inc->name, pctx->parsed_mod->mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200874 return LY_EVALID;
875 }
876
877 return LY_SUCCESS;
878}
879
Radek Krejcice8c1592018-10-29 15:35:51 +0100880struct lys_module *
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200881lysp_module_find_prefix(const struct lysp_module *prefix_mod, const char *prefix, size_t len)
Radek Krejcice8c1592018-10-29 15:35:51 +0100882{
Michal Vasko7c8439f2020-08-05 13:25:19 +0200883 struct lys_module *m = NULL;
884 LY_ARRAY_COUNT_TYPE u;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200885 const char *local_prefix;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100886
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200887 local_prefix = prefix_mod->is_submod ? ((struct lysp_submodule *)prefix_mod)->prefix : prefix_mod->mod->prefix;
888 if (!len || !ly_strncmp(local_prefix, prefix, len)) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200889 /* it is the prefix of the module itself */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200890 m = prefix_mod->mod;
Radek Krejci73dead22019-07-11 16:46:16 +0200891 }
Michal Vasko7c8439f2020-08-05 13:25:19 +0200892
893 /* search in imports */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200894 if (!m) {
895 LY_ARRAY_FOR(prefix_mod->imports, u) {
896 if (!ly_strncmp(prefix_mod->imports[u].prefix, prefix, len)) {
897 m = prefix_mod->imports[u].module;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200898 break;
899 }
900 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100901 }
Michal Vasko7c8439f2020-08-05 13:25:19 +0200902
903 return m;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100904}
905
Radek Krejci0935f412019-08-20 16:15:18 +0200906API const char *
Radek Krejcia3045382018-11-22 14:30:31 +0100907lys_nodetype2str(uint16_t nodetype)
908{
Michal Vaskod989ba02020-08-24 10:59:24 +0200909 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +0100910 case LYS_CONTAINER:
911 return "container";
912 case LYS_CHOICE:
913 return "choice";
914 case LYS_LEAF:
915 return "leaf";
916 case LYS_LEAFLIST:
917 return "leaf-list";
918 case LYS_LIST:
919 return "list";
920 case LYS_ANYXML:
921 return "anyxml";
922 case LYS_ANYDATA:
923 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +0100924 case LYS_CASE:
925 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +0100926 case LYS_RPC:
927 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +0100928 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +0100929 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +0100930 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +0100931 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +0200932 case LYS_USES:
933 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +0100934 default:
935 return "unknown";
936 }
937}
938
Radek Krejci693262f2019-04-29 15:23:20 +0200939const char *
940lys_datatype2str(LY_DATA_TYPE basetype)
941{
Michal Vaskod989ba02020-08-24 10:59:24 +0200942 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +0200943 case LY_TYPE_BINARY:
944 return "binary";
945 case LY_TYPE_UINT8:
946 return "uint8";
947 case LY_TYPE_UINT16:
948 return "uint16";
949 case LY_TYPE_UINT32:
950 return "uint32";
951 case LY_TYPE_UINT64:
952 return "uint64";
953 case LY_TYPE_STRING:
954 return "string";
955 case LY_TYPE_BITS:
956 return "bits";
957 case LY_TYPE_BOOL:
958 return "boolean";
959 case LY_TYPE_DEC64:
960 return "decimal64";
961 case LY_TYPE_EMPTY:
962 return "empty";
963 case LY_TYPE_ENUM:
964 return "enumeration";
965 case LY_TYPE_IDENT:
966 return "identityref";
967 case LY_TYPE_INST:
968 return "instance-identifier";
969 case LY_TYPE_LEAFREF:
970 return "leafref";
971 case LY_TYPE_UNION:
972 return "union";
973 case LY_TYPE_INT8:
974 return "int8";
975 case LY_TYPE_INT16:
976 return "int16";
977 case LY_TYPE_INT32:
978 return "int32";
979 case LY_TYPE_INT64:
980 return "int64";
981 default:
982 return "unknown";
983 }
984}
985
Radek Krejci056d0a82018-12-06 16:57:25 +0100986API const struct lysp_tpdf *
987lysp_node_typedefs(const struct lysp_node *node)
988{
Radek Krejci0fb28562018-12-13 15:17:37 +0100989 switch (node->nodetype) {
990 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200991 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +0100992 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200993 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +0100994 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +0200995 return ((struct lysp_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +0100996 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +0100997 case LYS_ACTION:
Michal Vasko22df3f02020-08-24 13:29:22 +0200998 return ((struct lysp_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200999 case LYS_INPUT:
1000 case LYS_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001001 return ((struct lysp_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001002 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001003 return ((struct lysp_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001004 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001005 return NULL;
1006 }
1007}
1008
Radek Krejci53ea6152018-12-13 15:21:15 +01001009API const struct lysp_grp *
1010lysp_node_groupings(const struct lysp_node *node)
1011{
1012 switch (node->nodetype) {
1013 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001014 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001015 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001016 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001017 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001018 return ((struct lysp_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001019 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001020 case LYS_ACTION:
Michal Vasko22df3f02020-08-24 13:29:22 +02001021 return ((struct lysp_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001022 case LYS_INPUT:
1023 case LYS_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001024 return ((struct lysp_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001025 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001026 return ((struct lysp_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001027 default:
1028 return NULL;
1029 }
1030}
1031
Radek Krejcibbe09a92018-11-08 09:36:54 +01001032struct lysp_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001033lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001034{
1035 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001036
Radek Krejcibbe09a92018-11-08 09:36:54 +01001037 switch (node->nodetype) {
1038 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001039 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001040 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001041 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001042 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001043 return &((struct lysp_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001044 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001045 return &((struct lysp_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001046 default:
1047 return NULL;
1048 }
1049}
1050
Radek Krejci056d0a82018-12-06 16:57:25 +01001051API const struct lysp_action *
1052lysp_node_actions(const struct lysp_node *node)
1053{
1054 struct lysp_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001055
Michal Vasko22df3f02020-08-24 13:29:22 +02001056 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001057 if (actions) {
1058 return *actions;
1059 } else {
1060 return NULL;
1061 }
1062}
1063
Radek Krejcibbe09a92018-11-08 09:36:54 +01001064struct lysp_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001065lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001066{
1067 assert(node);
1068 switch (node->nodetype) {
1069 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001070 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001071 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001072 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001073 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001074 return &((struct lysp_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001075 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001076 return &((struct lysp_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001077 default:
1078 return NULL;
1079 }
1080}
1081
Radek Krejci056d0a82018-12-06 16:57:25 +01001082API const struct lysp_notif *
1083lysp_node_notifs(const struct lysp_node *node)
1084{
1085 struct lysp_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001086
Michal Vasko22df3f02020-08-24 13:29:22 +02001087 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001088 if (notifs) {
1089 return *notifs;
1090 } else {
1091 return NULL;
1092 }
1093}
1094
Radek Krejcibbe09a92018-11-08 09:36:54 +01001095struct lysp_node **
Radek Krejci056d0a82018-12-06 16:57:25 +01001096lysp_node_children_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001097{
1098 assert(node);
1099 switch (node->nodetype) {
1100 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001101 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001102 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001103 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001104 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001105 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001106 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001107 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001108 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001109 return &((struct lysp_grp *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001110 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001111 return &((struct lysp_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001112 case LYS_INPUT:
1113 case LYS_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001114 return &((struct lysp_action_inout *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001115 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001116 return &((struct lysp_notif *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001117 default:
1118 return NULL;
1119 }
1120}
1121
Radek Krejci056d0a82018-12-06 16:57:25 +01001122API const struct lysp_node *
1123lysp_node_children(const struct lysp_node *node)
1124{
1125 struct lysp_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001126
1127 if (!node) {
1128 return NULL;
1129 }
1130
Michal Vasko22df3f02020-08-24 13:29:22 +02001131 children = lysp_node_children_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001132 if (children) {
1133 return *children;
1134 } else {
1135 return NULL;
1136 }
1137}
1138
1139struct lysc_action **
1140lysc_node_actions_p(struct lysc_node *node)
1141{
1142 assert(node);
1143 switch (node->nodetype) {
1144 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001145 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001146 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001147 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001148 default:
1149 return NULL;
1150 }
1151}
1152
1153API const struct lysc_action *
1154lysc_node_actions(const struct lysc_node *node)
1155{
1156 struct lysc_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001157
Michal Vasko22df3f02020-08-24 13:29:22 +02001158 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001159 if (actions) {
1160 return *actions;
1161 } else {
1162 return NULL;
1163 }
1164}
1165
1166struct lysc_notif **
1167lysc_node_notifs_p(struct lysc_node *node)
1168{
1169 assert(node);
1170 switch (node->nodetype) {
1171 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001172 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001173 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001174 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001175 default:
1176 return NULL;
1177 }
1178}
1179
1180API const struct lysc_notif *
1181lysc_node_notifs(const struct lysc_node *node)
1182{
1183 struct lysc_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001184
Michal Vasko22df3f02020-08-24 13:29:22 +02001185 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001186 if (notifs) {
1187 return *notifs;
1188 } else {
1189 return NULL;
1190 }
1191}
1192
Radek Krejcibbe09a92018-11-08 09:36:54 +01001193struct lysc_node **
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001194lysc_node_children_p(const struct lysc_node *node, uint16_t flags)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001195{
1196 assert(node);
1197 switch (node->nodetype) {
1198 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001199 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001200 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001201 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001202 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001203 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001204 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001205 return &((struct lysc_node_list *)node)->child;
Michal Vasko1bf09392020-03-27 12:38:10 +01001206 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001207 case LYS_ACTION:
1208 if (flags & LYS_CONFIG_R) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001209 return &((struct lysc_action *)node)->output.data;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001210 } else {
1211 /* LYS_CONFIG_W, but also the default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02001212 return &((struct lysc_action *)node)->input.data;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001213 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02001214 case LYS_INPUT:
1215 case LYS_OUTPUT:
1216 return &((struct lysc_action_inout *)node)->data;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001217 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001218 return &((struct lysc_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 lysc_node *
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001225lysc_node_children(const struct lysc_node *node, uint16_t flags)
Radek Krejcia3045382018-11-22 14:30:31 +01001226{
Radek Krejci056d0a82018-12-06 16:57:25 +01001227 struct lysc_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 = lysc_node_children_p((struct lysc_node *)node, flags);
Radek Krejci056d0a82018-12-06 16:57:25 +01001234 if (children) {
1235 return *children;
1236 } else {
Radek Krejcia3045382018-11-22 14:30:31 +01001237 return NULL;
1238 }
1239}
1240
Michal Vasko2a668712020-10-21 11:48:09 +02001241API const struct lysc_node *
Michal Vasko208a04a2020-10-21 15:17:12 +02001242lysc_node_children_full(const struct lysc_node *node, uint16_t flags)
Michal Vasko2a668712020-10-21 11:48:09 +02001243{
1244 switch (node->nodetype) {
1245 case LYS_CONTAINER:
1246 return ((struct lysc_node_container *)node)->child;
1247 case LYS_CHOICE:
1248 return (struct lysc_node *)((struct lysc_node_choice *)node)->cases;
1249 case LYS_CASE:
1250 return ((struct lysc_node_case *)node)->child;
1251 case LYS_LIST:
1252 return ((struct lysc_node_list *)node)->child;
1253 case LYS_RPC:
1254 case LYS_ACTION:
1255 if (flags & LYS_CONFIG_R) {
1256 return (struct lysc_node *)&((struct lysc_action *)node)->output;
1257 } else {
1258 /* LYS_CONFIG_W, but also the default case */
1259 return (struct lysc_node *)&((struct lysc_action *)node)->input;
1260 }
1261 case LYS_INPUT:
1262 case LYS_OUTPUT:
1263 return ((struct lysc_action_inout *)node)->data;
1264 case LYS_NOTIF:
1265 return ((struct lysc_notif *)node)->data;
1266 default:
1267 return NULL;
1268 }
1269}
1270
1271API const struct lysc_node *
Michal Vasko208a04a2020-10-21 15:17:12 +02001272lysc_node_parent_full(const struct lysc_node *node)
Michal Vasko2a668712020-10-21 11:48:09 +02001273{
1274 if (!node) {
1275 return NULL;
1276 } else if (node->nodetype == LYS_INPUT) {
1277 return (struct lysc_node *)(((char *)node) - offsetof(struct lysc_action, input));
1278 } else if (node->nodetype == LYS_OUTPUT) {
1279 return (struct lysc_node *)(((char *)node) - offsetof(struct lysc_action, output));
1280 } else if (node->parent && (node->parent->nodetype & (LYS_RPC | LYS_ACTION))) {
1281 if (node->flags & LYS_CONFIG_W) {
1282 return (struct lysc_node *)&((struct lysc_action *)node->parent)->input;
1283 } else {
1284 return (struct lysc_node *)&((struct lysc_action *)node->parent)->output;
1285 }
1286 } else {
1287 return node->parent;
1288 }
1289}
1290
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001291struct lys_module *
1292lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1293{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001294 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001295 if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) {
Michal Vasko69730152020-10-09 16:30:07 +02001296 return (struct lys_module *)ctx->list.objs[u];
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001297 }
1298 }
1299 return NULL;
1300}
1301
Radek Krejcid6b76452019-09-03 17:03:03 +02001302enum ly_stmt
Michal Vasko63f3d842020-07-08 10:10:14 +02001303lysp_match_kw(struct lys_yang_parser_ctx *ctx, struct ly_in *in)
David Sedlákc10e7902018-12-17 02:17:59 +01001304{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001305/**
Michal Vasko63f3d842020-07-08 10:10:14 +02001306 * @brief Move the INPUT by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001307 * @param[in] CTX yang parser context to update its indent value.
Michal Vasko63f3d842020-07-08 10:10:14 +02001308 * @param[in,out] IN input to move
David Sedlák1bccdfa2019-06-17 15:55:27 +02001309 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001310 *
1311 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001312 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001313#define MOVE_IN(CTX, IN, COUNT) ly_in_skip(IN, COUNT);if(CTX){(CTX)->indent+=COUNT;}
1314#define IF_KW(STR, LEN, STMT) if (!strncmp(in->current, STR, LEN)) {MOVE_IN(ctx, in, LEN);*kw=STMT;}
1315#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 +02001316#define IF_KW_PREFIX_END }
David Sedlák572e7ab2019-06-04 16:01:58 +02001317
Michal Vasko63f3d842020-07-08 10:10:14 +02001318 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001319 enum ly_stmt result = LY_STMT_NONE;
1320 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001321 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001322 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001323 case 'a':
Michal Vasko63f3d842020-07-08 10:10:14 +02001324 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001325 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1326 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1327 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001328 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001329 IF_KW("data", 4, LY_STMT_ANYDATA)
1330 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001331 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001332 break;
1333 case 'b':
Michal Vasko63f3d842020-07-08 10:10:14 +02001334 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001335 IF_KW("ase", 3, LY_STMT_BASE)
1336 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1337 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001338 break;
1339 case 'c':
Michal Vasko63f3d842020-07-08 10:10:14 +02001340 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001341 IF_KW("ase", 3, LY_STMT_CASE)
1342 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001343 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001344 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001345 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001346 IF_KW("ct", 2, LY_STMT_CONTACT)
1347 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001348 IF_KW_PREFIX_END
1349 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001350 break;
1351 case 'd':
Michal Vasko63f3d842020-07-08 10:10:14 +02001352 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001353 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001354 IF_KW("fault", 5, LY_STMT_DEFAULT)
1355 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001356 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001357 IF_KW("e", 1, LY_STMT_DEVIATE)
1358 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001359 IF_KW_PREFIX_END
1360 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001361 break;
1362 case 'e':
Michal Vasko63f3d842020-07-08 10:10:14 +02001363 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001364 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001365 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001366 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1367 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001368 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001369 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001370 break;
1371 case 'f':
Michal Vasko63f3d842020-07-08 10:10:14 +02001372 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001373 IF_KW("eature", 6, LY_STMT_FEATURE)
1374 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001375 break;
1376 case 'g':
Michal Vasko63f3d842020-07-08 10:10:14 +02001377 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001378 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001379 break;
1380 case 'i':
Michal Vasko63f3d842020-07-08 10:10:14 +02001381 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001382 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1383 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1384 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001385 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001386 IF_KW("clude", 5, LY_STMT_INCLUDE)
1387 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001388 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001389 break;
1390 case 'k':
Michal Vasko63f3d842020-07-08 10:10:14 +02001391 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001392 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001393 break;
1394 case 'l':
Michal Vasko63f3d842020-07-08 10:10:14 +02001395 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001396 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001397 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1398 else IF_KW("af", 2, LY_STMT_LEAF)
1399 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001400 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001401 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001402 break;
1403 case 'm':
Michal Vasko63f3d842020-07-08 10:10:14 +02001404 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001405 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001406 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1407 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001408 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001409 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1410 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001411 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001412 IF_KW("ule", 3, LY_STMT_MODULE)
1413 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001414 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001415 break;
1416 case 'n':
Michal Vasko63f3d842020-07-08 10:10:14 +02001417 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001418 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1419 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001420 break;
1421 case 'o':
Michal Vasko63f3d842020-07-08 10:10:14 +02001422 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001423 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001424 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1425 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001426 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001427 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001428 break;
1429 case 'p':
Michal Vasko63f3d842020-07-08 10:10:14 +02001430 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001431 IF_KW("ath", 3, LY_STMT_PATH)
1432 else IF_KW("attern", 6, LY_STMT_PATTERN)
1433 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001434 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001435 IF_KW("fix", 3, LY_STMT_PREFIX)
1436 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001437 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001438 break;
1439 case 'r':
Michal Vasko63f3d842020-07-08 10:10:14 +02001440 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001441 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001442 else IF_KW_PREFIX("e", 1)
1443 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001444 IF_KW("erence", 6, LY_STMT_REFERENCE)
1445 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001446 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001447 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1448 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1449 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001450 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001451 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001452 break;
1453 case 's':
Michal Vasko63f3d842020-07-08 10:10:14 +02001454 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001455 IF_KW("tatus", 5, LY_STMT_STATUS)
1456 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001457 break;
1458 case 't':
Michal Vasko63f3d842020-07-08 10:10:14 +02001459 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001460 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1461 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001462 break;
1463 case 'u':
Michal Vasko63f3d842020-07-08 10:10:14 +02001464 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001465 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001466 IF_KW("que", 3, LY_STMT_UNIQUE)
1467 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001468 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001469 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001470 break;
1471 case 'v':
Michal Vasko63f3d842020-07-08 10:10:14 +02001472 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001473 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001474 break;
1475 case 'w':
Michal Vasko63f3d842020-07-08 10:10:14 +02001476 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001477 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001478 break;
1479 case 'y':
Michal Vasko63f3d842020-07-08 10:10:14 +02001480 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001481 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
1482 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001483 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001484 default:
David Sedlák1bccdfa2019-06-17 15:55:27 +02001485 /* if context is not NULL we are matching keyword from YANG data*/
1486 if (ctx) {
Michal Vasko63f3d842020-07-08 10:10:14 +02001487 if (in->current[0] == ';') {
1488 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001489 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02001490 } else if (in->current[0] == '{') {
1491 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001492 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001493 } else if (in->current[0] == '}') {
1494 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001495 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001496 }
1497 }
David Sedlák23a59a62018-10-26 13:08:02 +02001498 break;
1499 }
1500
Michal Vasko63f3d842020-07-08 10:10:14 +02001501 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02001502 /* the keyword is not terminated */
1503 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001504 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02001505 }
1506
David Sedlák1bccdfa2019-06-17 15:55:27 +02001507#undef IF_KW
1508#undef IF_KW_PREFIX
1509#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01001510#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02001511 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01001512
David Sedlák1bccdfa2019-06-17 15:55:27 +02001513 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02001514}
David Sedlákecf5eb82019-06-03 14:12:44 +02001515
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001516LY_ARRAY_COUNT_TYPE
1517lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, LYEXT_SUBSTMT substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001518{
1519 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1520
Michal Vaskod989ba02020-08-24 10:59:24 +02001521 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001522 if (ext[index].insubstmt == substmt) {
1523 return index;
1524 }
1525 }
1526
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001527 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001528}
1529
Michal Vasko62ed12d2020-05-21 10:08:25 +02001530const struct lysc_node *
1531lysc_data_parent(const struct lysc_node *schema)
1532{
1533 const struct lysc_node *parent;
1534
Radek Krejci1e008d22020-08-17 11:37:37 +02001535 for (parent = schema->parent; parent && (parent->nodetype & (LYS_CHOICE | LYS_CASE)); parent = parent->parent) {}
Michal Vasko62ed12d2020-05-21 10:08:25 +02001536
1537 return parent;
1538}
Michal Vasko00cbf532020-06-15 13:58:47 +02001539
Radek Krejci857189e2020-09-01 13:26:36 +02001540ly_bool
Michal Vasko00cbf532020-06-15 13:58:47 +02001541lysc_is_output(const struct lysc_node *schema)
1542{
1543 const struct lysc_node *parent;
1544
1545 assert(schema);
1546
Radek Krejci1e008d22020-08-17 11:37:37 +02001547 for (parent = schema->parent; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION)); parent = parent->parent) {}
Michal Vasko00cbf532020-06-15 13:58:47 +02001548 if (parent && (schema->flags & LYS_CONFIG_R)) {
1549 return 1;
1550 }
1551 return 0;
1552}
Michal Vaskod975f862020-06-23 13:29:28 +02001553
Radek Krejci857189e2020-09-01 13:26:36 +02001554API ly_bool
Michal Vaskod975f862020-06-23 13:29:28 +02001555lysc_is_userordered(const struct lysc_node *schema)
1556{
1557 if (!schema || !(schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) || !(schema->flags & LYS_ORDBY_USER)) {
1558 return 0;
1559 }
1560
1561 return 1;
1562}