blob: 42743f029b250f2fe9a0047ca15637ad33a50a18 [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 Vaskob2d55bf2020-11-02 15:42:43 +0100219 const 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 Vaskob2d55bf2020-11-02 15:42:43 +0100231 mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_PREF_SCHEMA, (void *)start_module);
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 Krejci0935f412019-08-20 16:15:18 +0200982API const char *
Radek Krejcia3045382018-11-22 14:30:31 +0100983lys_nodetype2str(uint16_t nodetype)
984{
Michal Vaskod989ba02020-08-24 10:59:24 +0200985 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +0100986 case LYS_CONTAINER:
987 return "container";
988 case LYS_CHOICE:
989 return "choice";
990 case LYS_LEAF:
991 return "leaf";
992 case LYS_LEAFLIST:
993 return "leaf-list";
994 case LYS_LIST:
995 return "list";
996 case LYS_ANYXML:
997 return "anyxml";
998 case LYS_ANYDATA:
999 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001000 case LYS_CASE:
1001 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001002 case LYS_RPC:
1003 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001004 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001005 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001006 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001007 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001008 case LYS_USES:
1009 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001010 default:
1011 return "unknown";
1012 }
1013}
1014
Radek Krejci693262f2019-04-29 15:23:20 +02001015const char *
1016lys_datatype2str(LY_DATA_TYPE basetype)
1017{
Michal Vaskod989ba02020-08-24 10:59:24 +02001018 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001019 case LY_TYPE_BINARY:
1020 return "binary";
1021 case LY_TYPE_UINT8:
1022 return "uint8";
1023 case LY_TYPE_UINT16:
1024 return "uint16";
1025 case LY_TYPE_UINT32:
1026 return "uint32";
1027 case LY_TYPE_UINT64:
1028 return "uint64";
1029 case LY_TYPE_STRING:
1030 return "string";
1031 case LY_TYPE_BITS:
1032 return "bits";
1033 case LY_TYPE_BOOL:
1034 return "boolean";
1035 case LY_TYPE_DEC64:
1036 return "decimal64";
1037 case LY_TYPE_EMPTY:
1038 return "empty";
1039 case LY_TYPE_ENUM:
1040 return "enumeration";
1041 case LY_TYPE_IDENT:
1042 return "identityref";
1043 case LY_TYPE_INST:
1044 return "instance-identifier";
1045 case LY_TYPE_LEAFREF:
1046 return "leafref";
1047 case LY_TYPE_UNION:
1048 return "union";
1049 case LY_TYPE_INT8:
1050 return "int8";
1051 case LY_TYPE_INT16:
1052 return "int16";
1053 case LY_TYPE_INT32:
1054 return "int32";
1055 case LY_TYPE_INT64:
1056 return "int64";
1057 default:
1058 return "unknown";
1059 }
1060}
1061
Radek Krejci056d0a82018-12-06 16:57:25 +01001062API const struct lysp_tpdf *
1063lysp_node_typedefs(const struct lysp_node *node)
1064{
Radek Krejci0fb28562018-12-13 15:17:37 +01001065 switch (node->nodetype) {
1066 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001067 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001068 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001069 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001070 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001071 return ((struct lysp_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001072 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001073 case LYS_ACTION:
Michal Vasko22df3f02020-08-24 13:29:22 +02001074 return ((struct lysp_action *)node)->typedefs;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001075 case LYS_INPUT:
1076 case LYS_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001077 return ((struct lysp_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001078 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001079 return ((struct lysp_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001080 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001081 return NULL;
1082 }
1083}
1084
Radek Krejci53ea6152018-12-13 15:21:15 +01001085API const struct lysp_grp *
1086lysp_node_groupings(const struct lysp_node *node)
1087{
1088 switch (node->nodetype) {
1089 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001090 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001091 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001092 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001093 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001094 return ((struct lysp_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001095 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001096 case LYS_ACTION:
Michal Vasko22df3f02020-08-24 13:29:22 +02001097 return ((struct lysp_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001098 case LYS_INPUT:
1099 case LYS_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001100 return ((struct lysp_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001101 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001102 return ((struct lysp_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001103 default:
1104 return NULL;
1105 }
1106}
1107
Radek Krejcibbe09a92018-11-08 09:36:54 +01001108struct lysp_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001109lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001110{
1111 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001112
Radek Krejcibbe09a92018-11-08 09:36:54 +01001113 switch (node->nodetype) {
1114 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001115 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001116 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001117 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001118 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001119 return &((struct lysp_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001120 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001121 return &((struct lysp_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001122 default:
1123 return NULL;
1124 }
1125}
1126
Radek Krejci056d0a82018-12-06 16:57:25 +01001127API const struct lysp_action *
1128lysp_node_actions(const struct lysp_node *node)
1129{
1130 struct lysp_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001131
Michal Vasko22df3f02020-08-24 13:29:22 +02001132 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001133 if (actions) {
1134 return *actions;
1135 } else {
1136 return NULL;
1137 }
1138}
1139
Radek Krejcibbe09a92018-11-08 09:36:54 +01001140struct lysp_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001141lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001142{
1143 assert(node);
1144 switch (node->nodetype) {
1145 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001146 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001147 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001148 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001149 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001150 return &((struct lysp_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001151 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001152 return &((struct lysp_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001153 default:
1154 return NULL;
1155 }
1156}
1157
Radek Krejci056d0a82018-12-06 16:57:25 +01001158API const struct lysp_notif *
1159lysp_node_notifs(const struct lysp_node *node)
1160{
1161 struct lysp_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001162
Michal Vasko22df3f02020-08-24 13:29:22 +02001163 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001164 if (notifs) {
1165 return *notifs;
1166 } else {
1167 return NULL;
1168 }
1169}
1170
Radek Krejcibbe09a92018-11-08 09:36:54 +01001171struct lysp_node **
Radek Krejci056d0a82018-12-06 16:57:25 +01001172lysp_node_children_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001173{
1174 assert(node);
1175 switch (node->nodetype) {
1176 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001177 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001178 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001179 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001180 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001181 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001182 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001183 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001184 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001185 return &((struct lysp_grp *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001186 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001187 return &((struct lysp_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001188 case LYS_INPUT:
1189 case LYS_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001190 return &((struct lysp_action_inout *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001191 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001192 return &((struct lysp_notif *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001193 default:
1194 return NULL;
1195 }
1196}
1197
Radek Krejci056d0a82018-12-06 16:57:25 +01001198API const struct lysp_node *
1199lysp_node_children(const struct lysp_node *node)
1200{
1201 struct lysp_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001202
1203 if (!node) {
1204 return NULL;
1205 }
1206
Michal Vasko22df3f02020-08-24 13:29:22 +02001207 children = lysp_node_children_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001208 if (children) {
1209 return *children;
1210 } else {
1211 return NULL;
1212 }
1213}
1214
1215struct lysc_action **
1216lysc_node_actions_p(struct lysc_node *node)
1217{
1218 assert(node);
1219 switch (node->nodetype) {
1220 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001221 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001222 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001223 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001224 default:
1225 return NULL;
1226 }
1227}
1228
1229API const struct lysc_action *
1230lysc_node_actions(const struct lysc_node *node)
1231{
1232 struct lysc_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001233
Michal Vasko22df3f02020-08-24 13:29:22 +02001234 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001235 if (actions) {
1236 return *actions;
1237 } else {
1238 return NULL;
1239 }
1240}
1241
1242struct lysc_notif **
1243lysc_node_notifs_p(struct lysc_node *node)
1244{
1245 assert(node);
1246 switch (node->nodetype) {
1247 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001248 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001249 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001250 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001251 default:
1252 return NULL;
1253 }
1254}
1255
1256API const struct lysc_notif *
1257lysc_node_notifs(const struct lysc_node *node)
1258{
1259 struct lysc_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001260
Michal Vasko22df3f02020-08-24 13:29:22 +02001261 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001262 if (notifs) {
1263 return *notifs;
1264 } else {
1265 return NULL;
1266 }
1267}
1268
Radek Krejcibbe09a92018-11-08 09:36:54 +01001269struct lysc_node **
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001270lysc_node_children_p(const struct lysc_node *node, uint16_t flags)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001271{
1272 assert(node);
1273 switch (node->nodetype) {
1274 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001275 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001276 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001277 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001278 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001279 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001280 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001281 return &((struct lysc_node_list *)node)->child;
Michal Vasko1bf09392020-03-27 12:38:10 +01001282 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001283 case LYS_ACTION:
1284 if (flags & LYS_CONFIG_R) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001285 return &((struct lysc_action *)node)->output.data;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001286 } else {
1287 /* LYS_CONFIG_W, but also the default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02001288 return &((struct lysc_action *)node)->input.data;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001289 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02001290 case LYS_INPUT:
1291 case LYS_OUTPUT:
1292 return &((struct lysc_action_inout *)node)->data;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001293 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001294 return &((struct lysc_notif *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001295 default:
1296 return NULL;
1297 }
1298}
1299
Radek Krejci056d0a82018-12-06 16:57:25 +01001300API const struct lysc_node *
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001301lysc_node_children(const struct lysc_node *node, uint16_t flags)
Radek Krejcia3045382018-11-22 14:30:31 +01001302{
Radek Krejci056d0a82018-12-06 16:57:25 +01001303 struct lysc_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001304
1305 if (!node) {
1306 return NULL;
1307 }
1308
Michal Vasko22df3f02020-08-24 13:29:22 +02001309 children = lysc_node_children_p((struct lysc_node *)node, flags);
Radek Krejci056d0a82018-12-06 16:57:25 +01001310 if (children) {
1311 return *children;
1312 } else {
Radek Krejcia3045382018-11-22 14:30:31 +01001313 return NULL;
1314 }
1315}
1316
Michal Vasko2a668712020-10-21 11:48:09 +02001317API const struct lysc_node *
Michal Vasko208a04a2020-10-21 15:17:12 +02001318lysc_node_children_full(const struct lysc_node *node, uint16_t flags)
Michal Vasko2a668712020-10-21 11:48:09 +02001319{
1320 switch (node->nodetype) {
1321 case LYS_CONTAINER:
1322 return ((struct lysc_node_container *)node)->child;
1323 case LYS_CHOICE:
1324 return (struct lysc_node *)((struct lysc_node_choice *)node)->cases;
1325 case LYS_CASE:
1326 return ((struct lysc_node_case *)node)->child;
1327 case LYS_LIST:
1328 return ((struct lysc_node_list *)node)->child;
1329 case LYS_RPC:
1330 case LYS_ACTION:
1331 if (flags & LYS_CONFIG_R) {
1332 return (struct lysc_node *)&((struct lysc_action *)node)->output;
1333 } else {
1334 /* LYS_CONFIG_W, but also the default case */
1335 return (struct lysc_node *)&((struct lysc_action *)node)->input;
1336 }
1337 case LYS_INPUT:
1338 case LYS_OUTPUT:
1339 return ((struct lysc_action_inout *)node)->data;
1340 case LYS_NOTIF:
1341 return ((struct lysc_notif *)node)->data;
1342 default:
1343 return NULL;
1344 }
1345}
1346
1347API const struct lysc_node *
Michal Vasko208a04a2020-10-21 15:17:12 +02001348lysc_node_parent_full(const struct lysc_node *node)
Michal Vasko2a668712020-10-21 11:48:09 +02001349{
1350 if (!node) {
1351 return NULL;
1352 } else if (node->nodetype == LYS_INPUT) {
1353 return (struct lysc_node *)(((char *)node) - offsetof(struct lysc_action, input));
1354 } else if (node->nodetype == LYS_OUTPUT) {
1355 return (struct lysc_node *)(((char *)node) - offsetof(struct lysc_action, output));
1356 } else if (node->parent && (node->parent->nodetype & (LYS_RPC | LYS_ACTION))) {
1357 if (node->flags & LYS_CONFIG_W) {
1358 return (struct lysc_node *)&((struct lysc_action *)node->parent)->input;
1359 } else {
1360 return (struct lysc_node *)&((struct lysc_action *)node->parent)->output;
1361 }
1362 } else {
1363 return node->parent;
1364 }
1365}
1366
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001367struct lys_module *
1368lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1369{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001370 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001371 if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) {
Michal Vasko69730152020-10-09 16:30:07 +02001372 return (struct lys_module *)ctx->list.objs[u];
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001373 }
1374 }
1375 return NULL;
1376}
1377
Radek Krejcid6b76452019-09-03 17:03:03 +02001378enum ly_stmt
Michal Vasko63f3d842020-07-08 10:10:14 +02001379lysp_match_kw(struct lys_yang_parser_ctx *ctx, struct ly_in *in)
David Sedlákc10e7902018-12-17 02:17:59 +01001380{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001381/**
Michal Vasko63f3d842020-07-08 10:10:14 +02001382 * @brief Move the INPUT by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001383 * @param[in] CTX yang parser context to update its indent value.
Michal Vasko63f3d842020-07-08 10:10:14 +02001384 * @param[in,out] IN input to move
David Sedlák1bccdfa2019-06-17 15:55:27 +02001385 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001386 *
1387 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001388 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001389#define MOVE_IN(CTX, IN, COUNT) ly_in_skip(IN, COUNT);if(CTX){(CTX)->indent+=COUNT;}
1390#define IF_KW(STR, LEN, STMT) if (!strncmp(in->current, STR, LEN)) {MOVE_IN(ctx, in, LEN);*kw=STMT;}
1391#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 +02001392#define IF_KW_PREFIX_END }
David Sedlák572e7ab2019-06-04 16:01:58 +02001393
Michal Vasko63f3d842020-07-08 10:10:14 +02001394 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001395 enum ly_stmt result = LY_STMT_NONE;
1396 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001397 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001398 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001399 case 'a':
Michal Vasko63f3d842020-07-08 10:10:14 +02001400 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001401 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1402 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1403 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001404 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001405 IF_KW("data", 4, LY_STMT_ANYDATA)
1406 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001407 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001408 break;
1409 case 'b':
Michal Vasko63f3d842020-07-08 10:10:14 +02001410 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001411 IF_KW("ase", 3, LY_STMT_BASE)
1412 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1413 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001414 break;
1415 case 'c':
Michal Vasko63f3d842020-07-08 10:10:14 +02001416 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001417 IF_KW("ase", 3, LY_STMT_CASE)
1418 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001419 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001420 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001421 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001422 IF_KW("ct", 2, LY_STMT_CONTACT)
1423 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001424 IF_KW_PREFIX_END
1425 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001426 break;
1427 case 'd':
Michal Vasko63f3d842020-07-08 10:10:14 +02001428 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001429 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001430 IF_KW("fault", 5, LY_STMT_DEFAULT)
1431 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001432 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001433 IF_KW("e", 1, LY_STMT_DEVIATE)
1434 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001435 IF_KW_PREFIX_END
1436 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001437 break;
1438 case 'e':
Michal Vasko63f3d842020-07-08 10:10:14 +02001439 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001440 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001441 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001442 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1443 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001444 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001445 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001446 break;
1447 case 'f':
Michal Vasko63f3d842020-07-08 10:10:14 +02001448 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001449 IF_KW("eature", 6, LY_STMT_FEATURE)
1450 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001451 break;
1452 case 'g':
Michal Vasko63f3d842020-07-08 10:10:14 +02001453 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001454 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001455 break;
1456 case 'i':
Michal Vasko63f3d842020-07-08 10:10:14 +02001457 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001458 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1459 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1460 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001461 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001462 IF_KW("clude", 5, LY_STMT_INCLUDE)
1463 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001464 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001465 break;
1466 case 'k':
Michal Vasko63f3d842020-07-08 10:10:14 +02001467 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001468 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001469 break;
1470 case 'l':
Michal Vasko63f3d842020-07-08 10:10:14 +02001471 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001472 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001473 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1474 else IF_KW("af", 2, LY_STMT_LEAF)
1475 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001476 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001477 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001478 break;
1479 case 'm':
Michal Vasko63f3d842020-07-08 10:10:14 +02001480 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001481 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001482 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1483 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001484 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001485 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1486 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001487 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001488 IF_KW("ule", 3, LY_STMT_MODULE)
1489 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
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 'n':
Michal Vasko63f3d842020-07-08 10:10:14 +02001493 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001494 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1495 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001496 break;
1497 case 'o':
Michal Vasko63f3d842020-07-08 10:10:14 +02001498 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001499 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001500 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1501 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001502 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001503 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001504 break;
1505 case 'p':
Michal Vasko63f3d842020-07-08 10:10:14 +02001506 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001507 IF_KW("ath", 3, LY_STMT_PATH)
1508 else IF_KW("attern", 6, LY_STMT_PATTERN)
1509 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001510 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001511 IF_KW("fix", 3, LY_STMT_PREFIX)
1512 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001513 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001514 break;
1515 case 'r':
Michal Vasko63f3d842020-07-08 10:10:14 +02001516 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001517 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001518 else IF_KW_PREFIX("e", 1)
1519 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001520 IF_KW("erence", 6, LY_STMT_REFERENCE)
1521 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001522 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001523 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1524 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1525 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001526 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001527 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001528 break;
1529 case 's':
Michal Vasko63f3d842020-07-08 10:10:14 +02001530 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001531 IF_KW("tatus", 5, LY_STMT_STATUS)
1532 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001533 break;
1534 case 't':
Michal Vasko63f3d842020-07-08 10:10:14 +02001535 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001536 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1537 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001538 break;
1539 case 'u':
Michal Vasko63f3d842020-07-08 10:10:14 +02001540 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001541 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001542 IF_KW("que", 3, LY_STMT_UNIQUE)
1543 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001544 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001545 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001546 break;
1547 case 'v':
Michal Vasko63f3d842020-07-08 10:10:14 +02001548 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001549 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001550 break;
1551 case 'w':
Michal Vasko63f3d842020-07-08 10:10:14 +02001552 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001553 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001554 break;
1555 case 'y':
Michal Vasko63f3d842020-07-08 10:10:14 +02001556 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001557 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
1558 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001559 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001560 default:
David Sedlák1bccdfa2019-06-17 15:55:27 +02001561 /* if context is not NULL we are matching keyword from YANG data*/
1562 if (ctx) {
Michal Vasko63f3d842020-07-08 10:10:14 +02001563 if (in->current[0] == ';') {
1564 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001565 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02001566 } else if (in->current[0] == '{') {
1567 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001568 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001569 } else if (in->current[0] == '}') {
1570 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001571 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001572 }
1573 }
David Sedlák23a59a62018-10-26 13:08:02 +02001574 break;
1575 }
1576
Michal Vasko63f3d842020-07-08 10:10:14 +02001577 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02001578 /* the keyword is not terminated */
1579 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001580 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02001581 }
1582
David Sedlák1bccdfa2019-06-17 15:55:27 +02001583#undef IF_KW
1584#undef IF_KW_PREFIX
1585#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01001586#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02001587 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01001588
David Sedlák1bccdfa2019-06-17 15:55:27 +02001589 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02001590}
David Sedlákecf5eb82019-06-03 14:12:44 +02001591
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001592LY_ARRAY_COUNT_TYPE
1593lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, LYEXT_SUBSTMT substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001594{
1595 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1596
Michal Vaskod989ba02020-08-24 10:59:24 +02001597 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001598 if (ext[index].insubstmt == substmt) {
1599 return index;
1600 }
1601 }
1602
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001603 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001604}
1605
Michal Vasko62ed12d2020-05-21 10:08:25 +02001606const struct lysc_node *
1607lysc_data_parent(const struct lysc_node *schema)
1608{
1609 const struct lysc_node *parent;
1610
Radek Krejci1e008d22020-08-17 11:37:37 +02001611 for (parent = schema->parent; parent && (parent->nodetype & (LYS_CHOICE | LYS_CASE)); parent = parent->parent) {}
Michal Vasko62ed12d2020-05-21 10:08:25 +02001612
1613 return parent;
1614}
Michal Vasko00cbf532020-06-15 13:58:47 +02001615
Radek Krejci857189e2020-09-01 13:26:36 +02001616ly_bool
Michal Vasko00cbf532020-06-15 13:58:47 +02001617lysc_is_output(const struct lysc_node *schema)
1618{
1619 const struct lysc_node *parent;
1620
1621 assert(schema);
1622
Radek Krejci1e008d22020-08-17 11:37:37 +02001623 for (parent = schema->parent; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION)); parent = parent->parent) {}
Michal Vasko00cbf532020-06-15 13:58:47 +02001624 if (parent && (schema->flags & LYS_CONFIG_R)) {
1625 return 1;
1626 }
1627 return 0;
1628}
Michal Vaskod975f862020-06-23 13:29:28 +02001629
Radek Krejci857189e2020-09-01 13:26:36 +02001630API ly_bool
Michal Vaskod975f862020-06-23 13:29:28 +02001631lysc_is_userordered(const struct lysc_node *schema)
1632{
1633 if (!schema || !(schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) || !(schema->flags & LYS_ORDBY_USER)) {
1634 return 0;
1635 }
1636
1637 return 1;
1638}