blob: 0211da0e3b268ec79d2adf57e33840fcd8938fd1 [file] [log] [blame]
Radek Krejci86d106e2018-10-18 09:53:19 +02001/**
2 * @file tree_schema_helpers.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejcie7b95092019-05-15 11:03:07 +02004 * @brief Parsing and validation helper functions for schema trees
Radek Krejci86d106e2018-10-18 09:53:19 +02005 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Krejci535ea9f2020-05-29 16:01:05 +020014
15#define _GNU_SOURCE
Radek Krejci86d106e2018-10-18 09:53:19 +020016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020018#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010019#include <stddef.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <stdint.h>
Radek Krejci9ed7a192018-10-31 16:23:51 +010021#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020023#include <time.h>
24
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020026#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010029#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020030#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010031#include "log.h"
Michal Vasko69730152020-10-09 16:30:07 +020032#include "parser_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020033#include "set.h"
34#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020036#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020037#include "tree_schema_internal.h"
38
Radek Krejci85747952019-06-07 16:43:43 +020039LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +020040lysp_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 +020041{
42 struct lysp_import *i;
43
Michal Vasko69730152020-10-09 16:30:07 +020044 if (module_prefix && (&module_prefix != value) && !strcmp(module_prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010045 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value);
Radek Krejci86d106e2018-10-18 09:53:19 +020046 return LY_EEXIST;
47 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +010048 LY_ARRAY_FOR(imports, struct lysp_import, i) {
Michal Vasko69730152020-10-09 16:30:07 +020049 if (i->prefix && (&i->prefix != value) && !strcmp(i->prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +010050 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +010051 return LY_EEXIST;
Radek Krejci86d106e2018-10-18 09:53:19 +020052 }
53 }
54 return LY_SUCCESS;
55}
56
57LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020058lysp_check_date(struct lys_parser_ctx *ctx, const char *date, uint8_t date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +020059{
Radek Krejci86d106e2018-10-18 09:53:19 +020060 struct tm tm, tm_;
61 char *r;
62
Michal Vaskob36053d2020-03-26 15:49:30 +010063 LY_CHECK_ARG_RET(ctx ? PARSER_CTX(ctx) : NULL, date, LY_EINVAL);
64 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 +020065
66 /* check format */
Radek Krejci1deb5be2020-08-26 16:43:36 +020067 for (uint8_t i = 0; i < date_len; i++) {
Michal Vasko69730152020-10-09 16:30:07 +020068 if ((i == 4) || (i == 7)) {
Radek Krejci86d106e2018-10-18 09:53:19 +020069 if (date[i] != '-') {
70 goto error;
71 }
72 } else if (!isdigit(date[i])) {
73 goto error;
74 }
75 }
76
77 /* check content, e.g. 2018-02-31 */
78 memset(&tm, 0, sizeof tm);
79 r = strptime(date, "%Y-%m-%d", &tm);
Michal Vasko69730152020-10-09 16:30:07 +020080 if (!r || (r != &date[LY_REV_SIZE - 1])) {
Radek Krejci86d106e2018-10-18 09:53:19 +020081 goto error;
82 }
83 memcpy(&tm_, &tm, sizeof tm);
84 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
85 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
86 /* checking days is enough, since other errors
87 * have been checked by strptime() */
88 goto error;
89 }
90
91 return LY_SUCCESS;
92
93error:
Radek Krejcid33273d2018-10-25 14:55:52 +020094 if (stmt) {
Radek Krejcibbe09a92018-11-08 09:36:54 +010095 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +010096 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt);
Radek Krejcibbe09a92018-11-08 09:36:54 +010097 } else {
98 LOGVAL(NULL, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, date_len, date, stmt);
99 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200100 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200101 return LY_EINVAL;
102}
103
104void
105lysp_sort_revisions(struct lysp_revision *revs)
106{
Radek Krejci857189e2020-09-01 13:26:36 +0200107 LY_ARRAY_COUNT_TYPE i, r;
Radek Krejci86d106e2018-10-18 09:53:19 +0200108 struct lysp_revision rev;
109
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200110 for (i = 1, r = 0; revs && i < LY_ARRAY_COUNT(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200111 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200112 r = i;
113 }
114 }
115
116 if (r) {
117 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200118 memcpy(&rev, &revs[0], sizeof rev);
119 memcpy(&revs[0], &revs[r], sizeof rev);
120 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200121 }
122}
Radek Krejci151a5b72018-10-19 14:21:44 +0200123
Radek Krejcibbe09a92018-11-08 09:36:54 +0100124static const struct lysp_tpdf *
125lysp_type_match(const char *name, struct lysp_node *node)
126{
Radek Krejci0fb28562018-12-13 15:17:37 +0100127 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200128 LY_ARRAY_COUNT_TYPE u;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100129
Radek Krejci0fb28562018-12-13 15:17:37 +0100130 typedefs = lysp_node_typedefs(node);
131 LY_ARRAY_FOR(typedefs, u) {
132 if (!strcmp(name, typedefs[u].name)) {
133 /* match */
134 return &typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100135 }
136 }
137
138 return NULL;
139}
140
Radek Krejci4f28eda2018-11-12 11:46:16 +0100141static LY_DATA_TYPE
142lysp_type_str2builtin(const char *name, size_t len)
143{
144 if (len >= 4) { /* otherwise it does not match any built-in type */
145 if (name[0] == 'b') {
146 if (name[1] == 'i') {
Michal Vasko69730152020-10-09 16:30:07 +0200147 if ((len == 6) && !strncmp(&name[2], "nary", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100148 return LY_TYPE_BINARY;
Michal Vasko69730152020-10-09 16:30:07 +0200149 } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100150 return LY_TYPE_BITS;
151 }
Michal Vasko69730152020-10-09 16:30:07 +0200152 } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100153 return LY_TYPE_BOOL;
154 }
155 } else if (name[0] == 'd') {
Michal Vasko69730152020-10-09 16:30:07 +0200156 if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100157 return LY_TYPE_DEC64;
158 }
159 } else if (name[0] == 'e') {
Michal Vasko69730152020-10-09 16:30:07 +0200160 if ((len == 5) && !strncmp(&name[1], "mpty", 4)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100161 return LY_TYPE_EMPTY;
Michal Vasko69730152020-10-09 16:30:07 +0200162 } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100163 return LY_TYPE_ENUM;
164 }
165 } else if (name[0] == 'i') {
166 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200167 if ((len == 4) && !strncmp(&name[2], "t8", 2)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100168 return LY_TYPE_INT8;
169 } else if (len == 5) {
170 if (!strncmp(&name[2], "t16", 3)) {
171 return LY_TYPE_INT16;
172 } else if (!strncmp(&name[2], "t32", 3)) {
173 return LY_TYPE_INT32;
174 } else if (!strncmp(&name[2], "t64", 3)) {
175 return LY_TYPE_INT64;
176 }
Michal Vasko69730152020-10-09 16:30:07 +0200177 } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100178 return LY_TYPE_INST;
179 }
Michal Vasko69730152020-10-09 16:30:07 +0200180 } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100181 return LY_TYPE_IDENT;
182 }
183 } else if (name[0] == 'l') {
Michal Vasko69730152020-10-09 16:30:07 +0200184 if ((len == 7) && !strncmp(&name[1], "eafref", 6)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100185 return LY_TYPE_LEAFREF;
186 }
187 } else if (name[0] == 's') {
Michal Vasko69730152020-10-09 16:30:07 +0200188 if ((len == 6) && !strncmp(&name[1], "tring", 5)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100189 return LY_TYPE_STRING;
190 }
191 } else if (name[0] == 'u') {
192 if (name[1] == 'n') {
Michal Vasko69730152020-10-09 16:30:07 +0200193 if ((len == 5) && !strncmp(&name[2], "ion", 3)) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100194 return LY_TYPE_UNION;
195 }
Michal Vasko69730152020-10-09 16:30:07 +0200196 } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) {
197 if ((len == 5) && (name[4] == '8')) {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100198 return LY_TYPE_UINT8;
199 } else if (len == 6) {
200 if (!strncmp(&name[4], "16", 2)) {
201 return LY_TYPE_UINT16;
202 } else if (!strncmp(&name[4], "32", 2)) {
203 return LY_TYPE_UINT32;
204 } else if (!strncmp(&name[4], "64", 2)) {
205 return LY_TYPE_UINT64;
206 }
207 }
208 }
209 }
210 }
211
212 return LY_TYPE_UNKNOWN;
213}
214
Radek Krejcibbe09a92018-11-08 09:36:54 +0100215LY_ERR
216lysp_type_find(const char *id, struct lysp_node *start_node, struct lysp_module *start_module,
Radek Krejci0f969882020-08-21 16:56:47 +0200217 LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node, struct lysp_module **module)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100218{
219 const char *str, *name;
220 struct lysp_tpdf *typedefs;
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100221 const struct lys_module *mod;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200222 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100223
224 assert(id);
225 assert(start_module);
226 assert(tpdf);
227 assert(node);
228 assert(module);
229
Radek Krejci4f28eda2018-11-12 11:46:16 +0100230 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100231 str = strchr(id, ':');
232 if (str) {
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100233 mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_PREF_SCHEMA, (void *)start_module);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200234 *module = mod ? mod->parsed : NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100235 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100236 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100237 } else {
238 *module = start_module;
239 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100240
241 /* check for built-in types */
242 *type = lysp_type_str2builtin(name, strlen(name));
243 if (*type) {
244 *tpdf = NULL;
245 return LY_SUCCESS;
246 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100247 }
248 LY_CHECK_RET(!(*module), LY_ENOTFOUND);
249
Michal Vasko69730152020-10-09 16:30:07 +0200250 if (start_node && (*module == start_module)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100251 /* search typedefs in parent's nodes */
252 *node = start_node;
253 while (*node) {
254 *tpdf = lysp_type_match(name, *node);
255 if (*tpdf) {
256 /* match */
257 return LY_SUCCESS;
258 }
259 *node = (*node)->parent;
260 }
261 }
262
263 /* search in top-level typedefs */
264 if ((*module)->typedefs) {
265 LY_ARRAY_FOR((*module)->typedefs, u) {
266 if (!strcmp(name, (*module)->typedefs[u].name)) {
267 /* match */
268 *tpdf = &(*module)->typedefs[u];
269 return LY_SUCCESS;
270 }
271 }
272 }
273
274 /* search in submodules' typedefs */
275 LY_ARRAY_FOR((*module)->includes, u) {
276 typedefs = (*module)->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100277 LY_ARRAY_FOR(typedefs, v) {
278 if (!strcmp(name, typedefs[v].name)) {
279 /* match */
280 *tpdf = &typedefs[v];
281 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100282 }
283 }
284 }
285
286 return LY_ENOTFOUND;
287}
288
David Sedlák6544c182019-07-12 13:17:33 +0200289LY_ERR
David Sedlák07869a52019-07-12 14:28:19 +0200290lysp_check_enum_name(struct lys_parser_ctx *ctx, const char *name, size_t name_len)
David Sedlák6544c182019-07-12 13:17:33 +0200291{
292 if (!name_len) {
293 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
294 return LY_EVALID;
295 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
296 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
Michal Vasko69730152020-10-09 16:30:07 +0200297 name_len, name);
David Sedlák6544c182019-07-12 13:17:33 +0200298 return LY_EVALID;
299 } else {
300 for (size_t u = 0; u < name_len; ++u) {
301 if (iscntrl(name[u])) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100302 LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
Michal Vasko69730152020-10-09 16:30:07 +0200303 name_len, name, u + 1);
David Sedlák6544c182019-07-12 13:17:33 +0200304 break;
305 }
306 }
307 }
308
309 return LY_SUCCESS;
310}
311
Michal Vaskob36053d2020-03-26 15:49:30 +0100312/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100313 * @brief Check name of a new type to avoid name collisions.
314 *
315 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
316 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
317 * @param[in] tpdf Typedef definition to check.
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 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
321 * typedefs are checked, caller is supposed to free the table.
322 * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise.
323 */
324static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100325lysp_check_dup_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
Radek Krejci0f969882020-08-21 16:56:47 +0200326 struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100327{
328 struct lysp_node *parent;
329 uint32_t hash;
330 size_t name_len;
331 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200332 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100333 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100334
335 assert(ctx);
336 assert(tpdf);
337
338 name = tpdf->name;
339 name_len = strlen(name);
340
Radek Krejci4f28eda2018-11-12 11:46:16 +0100341 if (lysp_type_str2builtin(name, name_len)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100342 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 +0100343 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100344 }
345
346 /* check locally scoped typedefs (avoid name shadowing) */
347 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100348 typedefs = lysp_node_typedefs(node);
349 LY_ARRAY_FOR(typedefs, u) {
350 if (&typedefs[u] == tpdf) {
351 break;
352 }
353 if (!strcmp(name, typedefs[u].name)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100354 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with sibling type.", name);
Radek Krejci0fb28562018-12-13 15:17:37 +0100355 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100356 }
357 }
358 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200359 for (parent = node->parent; parent; parent = parent->parent) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100360 if (lysp_type_match(name, parent)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100361 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 +0100362 return LY_EEXIST;
363 }
364 }
365 }
366
367 /* check collision with the top-level typedefs */
368 hash = dict_hash(name, name_len);
369 if (node) {
370 lyht_insert(tpdfs_scoped, &name, hash, NULL);
371 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100372 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 +0100373 return LY_EEXIST;
374 }
375 } else {
376 if (lyht_insert(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100377 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 +0100378 return LY_EEXIST;
379 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100380 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
381 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
382 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100383 }
384
385 return LY_SUCCESS;
386}
387
Radek Krejci857189e2020-09-01 13:26:36 +0200388/**
389 * @brief Compare identifiers.
390 * Implementation of ::values_equal_cb.
391 */
392static ly_bool
393lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Radek Krejcibbe09a92018-11-08 09:36:54 +0100394{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200395 return strcmp(val1, val2) == 0 ? 1 : 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100396}
397
398LY_ERR
David Sedlákd2ebe572019-07-22 12:53:14 +0200399lysp_parse_finalize_reallocated(struct lys_parser_ctx *ctx, struct lysp_grp *groupings, struct lysp_augment *augments,
Radek Krejci0f969882020-08-21 16:56:47 +0200400 struct lysp_action *actions, struct lysp_notif *notifs)
David Sedlákd2ebe572019-07-22 12:53:14 +0200401{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200402 LY_ARRAY_COUNT_TYPE u, v;
David Sedlákd2ebe572019-07-22 12:53:14 +0200403 struct lysp_node *child;
404
405 /* finalize parent pointers to the reallocated items */
406
407 /* gropings */
408 LY_ARRAY_FOR(groupings, u) {
409 LY_LIST_FOR(groupings[u].data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200410 child->parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200411 }
412 LY_ARRAY_FOR(groupings[u].actions, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200413 groupings[u].actions[v].parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200414 }
415 LY_ARRAY_FOR(groupings[u].notifs, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200416 groupings[u].notifs[v].parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200417 }
418 LY_ARRAY_FOR(groupings[u].groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200419 groupings[u].groupings[v].parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200420 }
421 if (groupings[u].typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200422 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &groupings[u], 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200423 }
424 }
425
426 /* augments */
427 LY_ARRAY_FOR(augments, u) {
428 LY_LIST_FOR(augments[u].child, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200429 child->parent = (struct lysp_node *)&augments[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200430 }
431 LY_ARRAY_FOR(augments[u].actions, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200432 augments[u].actions[v].parent = (struct lysp_node *)&augments[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200433 }
434 LY_ARRAY_FOR(augments[u].notifs, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200435 augments[u].notifs[v].parent = (struct lysp_node *)&augments[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200436 }
437 }
438
439 /* actions */
440 LY_ARRAY_FOR(actions, u) {
441 if (actions[u].input.parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200442 actions[u].input.parent = (struct lysp_node *)&actions[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200443 LY_LIST_FOR(actions[u].input.data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200444 child->parent = (struct lysp_node *)&actions[u].input;
David Sedlákd2ebe572019-07-22 12:53:14 +0200445 }
446 LY_ARRAY_FOR(actions[u].input.groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200447 actions[u].input.groupings[v].parent = (struct lysp_node *)&actions[u].input;
David Sedlákd2ebe572019-07-22 12:53:14 +0200448 }
449 if (actions[u].input.typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200450 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &actions[u].input, 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200451 }
452 }
453 if (actions[u].output.parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200454 actions[u].output.parent = (struct lysp_node *)&actions[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200455 LY_LIST_FOR(actions[u].output.data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200456 child->parent = (struct lysp_node *)&actions[u].output;
David Sedlákd2ebe572019-07-22 12:53:14 +0200457 }
458 LY_ARRAY_FOR(actions[u].output.groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200459 actions[u].output.groupings[v].parent = (struct lysp_node *)&actions[u].output;
David Sedlákd2ebe572019-07-22 12:53:14 +0200460 }
461 if (actions[u].output.typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200462 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &actions[u].output, 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200463 }
464 }
465 LY_ARRAY_FOR(actions[u].groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200466 actions[u].groupings[v].parent = (struct lysp_node *)&actions[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200467 }
468 if (actions[u].typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200469 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &actions[u], 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200470 }
471 }
472
473 /* notifications */
474 LY_ARRAY_FOR(notifs, u) {
475 LY_LIST_FOR(notifs[u].data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200476 child->parent = (struct lysp_node *)&notifs[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200477 }
478 LY_ARRAY_FOR(notifs[u].groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200479 notifs[u].groupings[v].parent = (struct lysp_node *)&notifs[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200480 }
481 if (notifs[u].typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200482 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &notifs[u], 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200483 }
484 }
485
486 return LY_SUCCESS;
487}
488
489LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100490lysp_check_dup_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100491{
492 struct hash_table *ids_global;
493 struct hash_table *ids_scoped;
Radek Krejci0fb28562018-12-13 15:17:37 +0100494 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200495 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200496 uint32_t i;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100497 LY_ERR ret = LY_EVALID;
498
499 /* check name collisions - typedefs and groupings */
Michal Vasko22df3f02020-08-24 13:29:22 +0200500 ids_global = lyht_new(8, sizeof(char *), lysp_id_cmp, NULL, 1);
501 ids_scoped = lyht_new(8, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200502 LY_ARRAY_FOR(mod->typedefs, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100503 if (lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global, ids_scoped)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100504 goto cleanup;
505 }
506 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200507 LY_ARRAY_FOR(mod->includes, v) {
508 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100509 if (lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global, ids_scoped)) {
Radek Krejci3b1f9292018-11-08 10:58:35 +0100510 goto cleanup;
511 }
512 }
513 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200514 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
515 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
516 LY_ARRAY_FOR(typedefs, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100517 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 +0100518 goto cleanup;
519 }
520 }
521 }
522 ret = LY_SUCCESS;
523cleanup:
524 lyht_free(ids_global);
525 lyht_free(ids_scoped);
526 ly_set_erase(&ctx->tpdfs_nodes, NULL);
527
528 return ret;
529}
530
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100531static ly_bool
532ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
533{
534 void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
535
536 return ptr1 == ptr2 ? 1 : 0;
537}
538
539LY_ERR
540lysp_check_dup_features(struct lys_parser_ctx *ctx, struct lysp_module *mod)
541{
542 LY_ARRAY_COUNT_TYPE u;
543 struct hash_table *ht;
544 struct lysp_feature *f;
545 uint32_t hash;
546 LY_ERR ret = LY_SUCCESS, r;
547
548 ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
549 LY_CHECK_RET(!ht, LY_EMEM);
550
551 /* add all module features into a hash table */
552 LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
553 hash = dict_hash(f->name, strlen(f->name));
554 r = lyht_insert(ht, &f->name, hash, NULL);
555 if (r == LY_EEXIST) {
556 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
557 ret = LY_EVALID;
558 goto cleanup;
559 } else if (r) {
560 ret = r;
561 goto cleanup;
562 }
563 }
564
565 /* add all submodule features into a hash table */
566 LY_ARRAY_FOR(mod->includes, u) {
567 LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
568 hash = dict_hash(f->name, strlen(f->name));
569 r = lyht_insert(ht, &f->name, hash, NULL);
570 if (r == LY_EEXIST) {
571 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
572 ret = LY_EVALID;
573 goto cleanup;
574 } else if (r) {
575 ret = r;
576 goto cleanup;
577 }
578 }
579 }
580
581cleanup:
582 lyht_free(ht);
583 return ret;
584}
585
586LY_ERR
587lysp_check_dup_identities(struct lys_parser_ctx *ctx, struct lysp_module *mod)
588{
589 LY_ARRAY_COUNT_TYPE u;
590 struct hash_table *ht;
591 struct lysp_ident *i;
592 uint32_t hash;
593 LY_ERR ret = LY_SUCCESS, r;
594
595 ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
596 LY_CHECK_RET(!ht, LY_EMEM);
597
598 /* add all module identities into a hash table */
599 LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
600 hash = dict_hash(i->name, strlen(i->name));
601 r = lyht_insert(ht, &i->name, hash, NULL);
602 if (r == LY_EEXIST) {
603 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
604 ret = LY_EVALID;
605 goto cleanup;
606 } else if (r) {
607 ret = r;
608 goto cleanup;
609 }
610 }
611
612 /* add all submodule identities into a hash table */
613 LY_ARRAY_FOR(mod->includes, u) {
614 LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
615 hash = dict_hash(i->name, strlen(i->name));
616 r = lyht_insert(ht, &i->name, hash, NULL);
617 if (r == LY_EEXIST) {
618 LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
619 ret = LY_EVALID;
620 goto cleanup;
621 } else if (r) {
622 ret = r;
623 goto cleanup;
624 }
625 }
626 }
627
628cleanup:
629 lyht_free(ht);
630 return ret;
631}
632
Radek Krejci9ed7a192018-10-31 16:23:51 +0100633struct lysp_load_module_check_data {
634 const char *name;
635 const char *revision;
636 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200637 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100638};
639
640static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100641lysp_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 +0100642{
643 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100644 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200645 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100646 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100647 struct lysp_revision *revs;
648
649 name = mod ? mod->mod->name : submod->name;
650 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200651 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100652
653 if (info->name) {
654 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100655 if (strcmp(info->name, name)) {
656 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100657 return LY_EINVAL;
658 }
659 }
660 if (info->revision) {
661 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100662 if (!revs || strcmp(info->revision, revs[0].date)) {
663 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Michal Vasko69730152020-10-09 16:30:07 +0200664 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100665 return LY_EINVAL;
666 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200667 } else if (!latest_revision) {
668 /* do not log, we just need to drop the schema and use the latest revision from the context */
669 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100670 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100671 if (submod) {
672 assert(info->submoduleof);
673
Radek Krejci9ed7a192018-10-31 16:23:51 +0100674 /* check that the submodule belongs-to our module */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200675 if (strcmp(info->submoduleof, submod->mod->name)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100676 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 +0200677 submod->name, info->submoduleof, submod->mod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100678 return LY_EVALID;
679 }
680 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100681 if (submod->parsing) {
682 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100683 return LY_EVALID;
684 }
685 }
686 if (info->path) {
687 /* check that name and revision match filename */
688 filename = strrchr(info->path, '/');
689 if (!filename) {
690 filename = info->path;
691 } else {
692 filename++;
693 }
694 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100695 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100696 rev = strchr(filename, '@');
697 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100698 if (strncmp(filename, name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +0200699 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100700 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100701 }
702 /* revision */
703 if (rev) {
704 len = dot - ++rev;
Michal Vasko69730152020-10-09 16:30:07 +0200705 if (!revs || (len != 10) || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100706 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +0200707 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100708 }
709 }
710 }
711 return LY_SUCCESS;
712}
713
714LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100715lys_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 +0200716 struct lys_parser_ctx *main_ctx, const char *main_name, ly_bool required, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100717{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200718 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100719 char *filepath = NULL;
720 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100721 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100722 LY_ERR ret = LY_SUCCESS;
723 struct lysp_load_module_check_data check_data = {0};
724
725 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 +0200726 &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200727 if (!filepath) {
728 if (required) {
729 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
Michal Vasko69730152020-10-09 16:30:07 +0200730 revision ? revision : "");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200731 }
732 return LY_ENOTFOUND;
733 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100734
735 LOGVRB("Loading schema from \"%s\" file.", filepath);
736
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200737 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200738 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +0200739 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100740 check_data.name = name;
741 check_data.revision = revision;
742 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800743 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200744 if (main_ctx) {
Michal Vasko7a0b0762020-09-02 16:37:01 +0200745 ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data,
Michal Vasko69730152020-10-09 16:30:07 +0200746 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200747 } else {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100748 ret = lys_create_module(ctx, in, format, implement, lysp_load_module_check, &check_data, features,
Michal Vasko69730152020-10-09 16:30:07 +0200749 (struct lys_module **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200750
751 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200752 ly_in_free(in, 1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200753 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100754
755 *result = mod;
756
757 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200758
Radek Krejci9ed7a192018-10-31 16:23:51 +0100759cleanup:
760 free(filepath);
761 return ret;
762}
763
Radek Krejcid33273d2018-10-25 14:55:52 +0200764LY_ERR
Michal Vasko0550b762020-11-24 18:04:08 +0100765lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, ly_bool implement, const char **features,
766 struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200767{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100768 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200769 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200770
Radek Krejci9ed7a192018-10-31 16:23:51 +0100771 void (*module_data_free)(void *module_data, void *user_data) = NULL;
772 struct lysp_load_module_check_data check_data = {0};
Michal Vasko0550b762020-11-24 18:04:08 +0100773 struct lys_module *ctx_latest = NULL, *m;
Michal Vasko63f3d842020-07-08 10:10:14 +0200774 struct ly_in *in;
Michal Vasko0550b762020-11-24 18:04:08 +0100775 LY_ERR ret;
Radek Krejci086c7132018-10-26 15:29:04 +0200776
Radek Krejci0af46292019-01-11 16:02:31 +0100777 assert(mod);
778
Michal Vasko25d6ad02020-10-22 12:20:22 +0200779 if (ctx->flags & LY_CTX_ALL_IMPLEMENTED) {
Radek Krejcia53d7c92020-08-21 11:30:56 +0200780 implement = 1;
781 }
782
Michal Vasko0550b762020-11-24 18:04:08 +0100783 /*
784 * try to get the module from the context
785 */
Radek Krejci0af46292019-01-11 16:02:31 +0100786 if (!*mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100787 if (revision) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200788 /* get the specific revision */
Michal Vasko22df3f02020-08-24 13:29:22 +0200789 *mod = (struct lys_module *)ly_ctx_get_module(ctx, name, revision);
Radek Krejci0af46292019-01-11 16:02:31 +0100790 } else {
Michal Vasko0550b762020-11-24 18:04:08 +0100791 if (implement) {
792 /* prefer the implemented module instead of the latest one */
793 *mod = (struct lys_module *)ly_ctx_get_module_implemented(ctx, name);
794 }
795 if (!*mod) {
796 /* get the requested module of the latest revision in the context */
797 *mod = (struct lys_module *)ly_ctx_get_module_latest(ctx, name);
798 if (*mod && ((*mod)->latest_revision == 1)) {
799 /* let us now search with callback and searchpaths to check if there is newer revision outside the context */
800 ctx_latest = *mod;
801 *mod = NULL;
802 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200803 }
Radek Krejci0af46292019-01-11 16:02:31 +0100804 }
Radek Krejci086c7132018-10-26 15:29:04 +0200805 }
806
Michal Vasko0550b762020-11-24 18:04:08 +0100807 /* check collision with other implemented revision */
808 if (implement) {
809 m = ly_ctx_get_module_implemented(ctx, name);
810 if (m && (!*mod || (*mod && (m != *mod)))) {
Radek Krejci086c7132018-10-26 15:29:04 +0200811 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200812 "Module \"%s\" is already present in other implemented revision.", name);
Michal Vasko0550b762020-11-24 18:04:08 +0100813 *mod = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200814 return LY_EDENIED;
815 }
Michal Vasko0550b762020-11-24 18:04:08 +0100816 }
Radek Krejci086c7132018-10-26 15:29:04 +0200817
Michal Vasko0550b762020-11-24 18:04:08 +0100818 /*
819 * no suitable module in the context, try to load it
820 */
821 if (!*mod) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100822 /* module not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200823 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
824search_clb:
825 if (ctx->imp_clb) {
826 if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
Michal Vasko69730152020-10-09 16:30:07 +0200827 &format, &module_data, &module_data_free) == LY_SUCCESS) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200828 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
Radek Krejci9ed7a192018-10-31 16:23:51 +0100829 check_data.name = name;
830 check_data.revision = revision;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100831 lys_create_module(ctx, in, format, implement, lysp_load_module_check, &check_data, features, mod);
Michal Vasko63f3d842020-07-08 10:10:14 +0200832 ly_in_free(in, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100833 if (module_data_free) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200834 module_data_free((void *)module_data, ctx->imp_clb_data);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100835 }
Radek Krejci086c7132018-10-26 15:29:04 +0200836 }
837 }
838 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
839 goto search_file;
840 }
841 } else {
842search_file:
843 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
844 /* module was not received from the callback or there is no callback set */
Michal Vasko0550b762020-11-24 18:04:08 +0100845 lys_module_localfile(ctx, name, revision, features, implement, NULL, NULL, ctx_latest ? 0 : 1, (void **)mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200846 }
Michal Vasko0550b762020-11-24 18:04:08 +0100847 if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejci086c7132018-10-26 15:29:04 +0200848 goto search_clb;
849 }
850 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100851
Radek Krejcib3289d62019-09-18 12:21:39 +0200852 /* update the latest_revision flag - here we have selected the latest available schema,
853 * consider that even the callback provides correct latest revision */
Michal Vasko0550b762020-11-24 18:04:08 +0100854 if (!*mod && ctx_latest) {
855 LOGVRB("Newer revision than %s-%s not found, using this as the latest revision.", ctx_latest->name,
856 ctx_latest->revision);
857 ctx_latest->latest_revision = 2;
858 *mod = ctx_latest;
859 } else if (*mod && !revision && ((*mod)->latest_revision == 1)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100860 (*mod)->latest_revision = 2;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100861 }
Radek Krejci086c7132018-10-26 15:29:04 +0200862
Michal Vasko0550b762020-11-24 18:04:08 +0100863 if (!*mod) {
864 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "%s \"%s\" module failed.",
865 implement ? "Loading" : "Importing", name);
866 return LY_EVALID;
867 }
868 } else {
869 /* we have module from the current context, circular check */
870 if ((*mod)->parsed->parsing) {
Radek Krejci086c7132018-10-26 15:29:04 +0200871 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
872 *mod = NULL;
873 return LY_EVALID;
874 }
875 }
Radek Krejci086c7132018-10-26 15:29:04 +0200876
Michal Vasko0550b762020-11-24 18:04:08 +0100877 /*
878 * module found, make sure it is implemented if should be
879 */
Radek Krejci086c7132018-10-26 15:29:04 +0200880 if (implement) {
881 /* mark the module implemented, check for collision was already done */
Michal Vaskoc5d64862020-11-24 18:04:45 +0100882 ret = lys_set_implemented(*mod, features);
883 if (ret) {
884 *mod = NULL;
885 return ret;
886 }
Radek Krejci086c7132018-10-26 15:29:04 +0200887 }
Radek Krejci086c7132018-10-26 15:29:04 +0200888
889 return LY_SUCCESS;
890}
891
892LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200893lysp_check_stringchar(struct lys_parser_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +0200894{
895 if (!is_yangutf8char(c)) {
896 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
897 return LY_EVALID;
898 }
899 return LY_SUCCESS;
900}
901
902LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200903lysp_check_identifierchar(struct lys_parser_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +0200904{
Michal Vasko69730152020-10-09 16:30:07 +0200905 if (first || (prefix && ((*prefix) == 1))) {
David Sedlák4a650532019-07-10 11:55:18 +0200906 if (!is_yangidentstartchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200907 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200908 return LY_EVALID;
909 }
910 if (prefix) {
911 if (first) {
912 (*prefix) = 0;
913 } else {
914 (*prefix) = 2;
915 }
916 }
Michal Vasko69730152020-10-09 16:30:07 +0200917 } else if ((c == ':') && prefix && ((*prefix) == 0)) {
David Sedlák4a650532019-07-10 11:55:18 +0200918 (*prefix) = 1;
919 } else if (!is_yangidentchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200920 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200921 return LY_EVALID;
922 }
923
924 return LY_SUCCESS;
925}
926
927LY_ERR
Michal Vasko7c8439f2020-08-05 13:25:19 +0200928lysp_load_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200929{
Michal Vaskob36053d2020-03-26 15:49:30 +0100930 struct ly_ctx *ctx = (struct ly_ctx *)(PARSER_CTX(pctx));
Radek Krejci3eb299d2019-04-08 15:07:44 +0200931 struct lysp_submodule *submod = NULL;
Radek Krejcid33273d2018-10-25 14:55:52 +0200932 const char *submodule_data = NULL;
933 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko69730152020-10-09 16:30:07 +0200934
Radek Krejcid33273d2018-10-25 14:55:52 +0200935 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100936 struct lysp_load_module_check_data check_data = {0};
Michal Vasko63f3d842020-07-08 10:10:14 +0200937 struct ly_in *in;
Radek Krejcid33273d2018-10-25 14:55:52 +0200938
Radek Krejcibbe09a92018-11-08 09:36:54 +0100939 /* submodule not present in the context, get the input data and parse it */
Michal Vaskob36053d2020-03-26 15:49:30 +0100940 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200941search_clb:
Michal Vaskob36053d2020-03-26 15:49:30 +0100942 if (ctx->imp_clb) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200943 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 +0200944 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200945 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
Radek Krejcibbe09a92018-11-08 09:36:54 +0100946 check_data.name = inc->name;
947 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200948 check_data.submoduleof = pctx->parsed_mod->mod->name;
Michal Vasko7a0b0762020-09-02 16:37:01 +0200949 lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, &submod);
Michal Vasko63f3d842020-07-08 10:10:14 +0200950 ly_in_free(in, 0);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100951 if (submodule_data_free) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200952 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200953 }
954 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200955 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100956 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100957 goto search_file;
Radek Krejcid33273d2018-10-25 14:55:52 +0200958 }
Radek Krejci2d31ea72018-10-25 15:46:42 +0200959 } else {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100960search_file:
Michal Vaskob36053d2020-03-26 15:49:30 +0100961 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100962 /* submodule was not received from the callback or there is no callback set */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100963 lys_module_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, NULL, 0, pctx,
964 pctx->parsed_mod->mod->name, 1, (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100965 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100966 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100967 goto search_clb;
968 }
969 }
970 if (submod) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100971 if (!inc->rev[0] && (submod->latest_revision == 1)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100972 /* update the latest_revision flag - here we have selected the latest available schema,
973 * consider that even the callback provides correct latest revision */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100974 submod->latest_revision = 2;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100975 }
976
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100977 inc->submodule = submod;
Radek Krejcid33273d2018-10-25 14:55:52 +0200978 }
979 if (!inc->submodule) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100980 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.",
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200981 inc->name, pctx->parsed_mod->mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200982 return LY_EVALID;
983 }
984
985 return LY_SUCCESS;
986}
987
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100988API const struct lysc_when *
989lysc_has_when(const struct lysc_node *node)
990{
991 if (!node) {
992 return NULL;
993 }
994
995 do {
996 if (node->when) {
997 return *node->when;
998 }
999 node = node->parent;
1000 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1001
1002 return NULL;
1003}
1004
Radek Krejci0935f412019-08-20 16:15:18 +02001005API const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001006lys_nodetype2str(uint16_t nodetype)
1007{
Michal Vaskod989ba02020-08-24 10:59:24 +02001008 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001009 case LYS_CONTAINER:
1010 return "container";
1011 case LYS_CHOICE:
1012 return "choice";
1013 case LYS_LEAF:
1014 return "leaf";
1015 case LYS_LEAFLIST:
1016 return "leaf-list";
1017 case LYS_LIST:
1018 return "list";
1019 case LYS_ANYXML:
1020 return "anyxml";
1021 case LYS_ANYDATA:
1022 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001023 case LYS_CASE:
1024 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001025 case LYS_RPC:
1026 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001027 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001028 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001029 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001030 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001031 case LYS_USES:
1032 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001033 default:
1034 return "unknown";
1035 }
1036}
1037
Radek Krejci693262f2019-04-29 15:23:20 +02001038const char *
1039lys_datatype2str(LY_DATA_TYPE basetype)
1040{
Michal Vaskod989ba02020-08-24 10:59:24 +02001041 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001042 case LY_TYPE_BINARY:
1043 return "binary";
1044 case LY_TYPE_UINT8:
1045 return "uint8";
1046 case LY_TYPE_UINT16:
1047 return "uint16";
1048 case LY_TYPE_UINT32:
1049 return "uint32";
1050 case LY_TYPE_UINT64:
1051 return "uint64";
1052 case LY_TYPE_STRING:
1053 return "string";
1054 case LY_TYPE_BITS:
1055 return "bits";
1056 case LY_TYPE_BOOL:
1057 return "boolean";
1058 case LY_TYPE_DEC64:
1059 return "decimal64";
1060 case LY_TYPE_EMPTY:
1061 return "empty";
1062 case LY_TYPE_ENUM:
1063 return "enumeration";
1064 case LY_TYPE_IDENT:
1065 return "identityref";
1066 case LY_TYPE_INST:
1067 return "instance-identifier";
1068 case LY_TYPE_LEAFREF:
1069 return "leafref";
1070 case LY_TYPE_UNION:
1071 return "union";
1072 case LY_TYPE_INT8:
1073 return "int8";
1074 case LY_TYPE_INT16:
1075 return "int16";
1076 case LY_TYPE_INT32:
1077 return "int32";
1078 case LY_TYPE_INT64:
1079 return "int64";
1080 default:
1081 return "unknown";
1082 }
1083}
1084
Radek Krejci056d0a82018-12-06 16:57:25 +01001085API const struct lysp_tpdf *
1086lysp_node_typedefs(const struct lysp_node *node)
1087{
Radek Krejci0fb28562018-12-13 15:17:37 +01001088 switch (node->nodetype) {
1089 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001090 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001091 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001092 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001093 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001094 return ((struct lysp_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001095 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001096 case LYS_ACTION:
Michal Vasko22df3f02020-08-24 13:29:22 +02001097 return ((struct lysp_action *)node)->typedefs;
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)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001101 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001102 return ((struct lysp_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001103 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001104 return NULL;
1105 }
1106}
1107
Radek Krejci53ea6152018-12-13 15:21:15 +01001108API const struct lysp_grp *
1109lysp_node_groupings(const struct lysp_node *node)
1110{
1111 switch (node->nodetype) {
1112 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001113 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001114 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001115 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001116 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001117 return ((struct lysp_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001118 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001119 case LYS_ACTION:
Michal Vasko22df3f02020-08-24 13:29:22 +02001120 return ((struct lysp_action *)node)->groupings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001121 case LYS_INPUT:
1122 case LYS_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001123 return ((struct lysp_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001124 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001125 return ((struct lysp_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001126 default:
1127 return NULL;
1128 }
1129}
1130
Radek Krejcibbe09a92018-11-08 09:36:54 +01001131struct lysp_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001132lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001133{
1134 assert(node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001135
Radek Krejcibbe09a92018-11-08 09:36:54 +01001136 switch (node->nodetype) {
1137 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001138 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001139 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001140 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001141 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001142 return &((struct lysp_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001143 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001144 return &((struct lysp_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001145 default:
1146 return NULL;
1147 }
1148}
1149
Radek Krejci056d0a82018-12-06 16:57:25 +01001150API const struct lysp_action *
1151lysp_node_actions(const struct lysp_node *node)
1152{
1153 struct lysp_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001154
Michal Vasko22df3f02020-08-24 13:29:22 +02001155 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001156 if (actions) {
1157 return *actions;
1158 } else {
1159 return NULL;
1160 }
1161}
1162
Radek Krejcibbe09a92018-11-08 09:36:54 +01001163struct lysp_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001164lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001165{
1166 assert(node);
1167 switch (node->nodetype) {
1168 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001169 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001170 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001171 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001172 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001173 return &((struct lysp_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001174 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001175 return &((struct lysp_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001176 default:
1177 return NULL;
1178 }
1179}
1180
Radek Krejci056d0a82018-12-06 16:57:25 +01001181API const struct lysp_notif *
1182lysp_node_notifs(const struct lysp_node *node)
1183{
1184 struct lysp_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001185
Michal Vasko22df3f02020-08-24 13:29:22 +02001186 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001187 if (notifs) {
1188 return *notifs;
1189 } else {
1190 return NULL;
1191 }
1192}
1193
Radek Krejcibbe09a92018-11-08 09:36:54 +01001194struct lysp_node **
Radek Krejci056d0a82018-12-06 16:57:25 +01001195lysp_node_children_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001196{
1197 assert(node);
1198 switch (node->nodetype) {
1199 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001200 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001201 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001202 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001203 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001204 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001205 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001206 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001207 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001208 return &((struct lysp_grp *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001209 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001210 return &((struct lysp_augment *)node)->child;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001211 case LYS_INPUT:
1212 case LYS_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001213 return &((struct lysp_action_inout *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001214 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001215 return &((struct lysp_notif *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001216 default:
1217 return NULL;
1218 }
1219}
1220
Radek Krejci056d0a82018-12-06 16:57:25 +01001221API const struct lysp_node *
1222lysp_node_children(const struct lysp_node *node)
1223{
1224 struct lysp_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001225
1226 if (!node) {
1227 return NULL;
1228 }
1229
Michal Vasko22df3f02020-08-24 13:29:22 +02001230 children = lysp_node_children_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001231 if (children) {
1232 return *children;
1233 } else {
1234 return NULL;
1235 }
1236}
1237
1238struct lysc_action **
1239lysc_node_actions_p(struct lysc_node *node)
1240{
1241 assert(node);
1242 switch (node->nodetype) {
1243 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001244 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001245 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001246 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001247 default:
1248 return NULL;
1249 }
1250}
1251
1252API const struct lysc_action *
1253lysc_node_actions(const struct lysc_node *node)
1254{
1255 struct lysc_action **actions;
Michal Vasko69730152020-10-09 16:30:07 +02001256
Michal Vasko22df3f02020-08-24 13:29:22 +02001257 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001258 if (actions) {
1259 return *actions;
1260 } else {
1261 return NULL;
1262 }
1263}
1264
1265struct lysc_notif **
1266lysc_node_notifs_p(struct lysc_node *node)
1267{
1268 assert(node);
1269 switch (node->nodetype) {
1270 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001271 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001272 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001273 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001274 default:
1275 return NULL;
1276 }
1277}
1278
1279API const struct lysc_notif *
1280lysc_node_notifs(const struct lysc_node *node)
1281{
1282 struct lysc_notif **notifs;
Michal Vasko69730152020-10-09 16:30:07 +02001283
Michal Vasko22df3f02020-08-24 13:29:22 +02001284 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001285 if (notifs) {
1286 return *notifs;
1287 } else {
1288 return NULL;
1289 }
1290}
1291
Radek Krejcibbe09a92018-11-08 09:36:54 +01001292struct lysc_node **
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001293lysc_node_children_p(const struct lysc_node *node, uint16_t flags)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001294{
1295 assert(node);
1296 switch (node->nodetype) {
1297 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001298 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001299 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001300 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001301 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001302 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001303 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001304 return &((struct lysc_node_list *)node)->child;
Michal Vasko1bf09392020-03-27 12:38:10 +01001305 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001306 case LYS_ACTION:
1307 if (flags & LYS_CONFIG_R) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001308 return &((struct lysc_action *)node)->output.data;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001309 } else {
1310 /* LYS_CONFIG_W, but also the default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02001311 return &((struct lysc_action *)node)->input.data;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001312 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02001313 case LYS_INPUT:
1314 case LYS_OUTPUT:
1315 return &((struct lysc_action_inout *)node)->data;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001316 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001317 return &((struct lysc_notif *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001318 default:
1319 return NULL;
1320 }
1321}
1322
Radek Krejci056d0a82018-12-06 16:57:25 +01001323API const struct lysc_node *
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001324lysc_node_children(const struct lysc_node *node, uint16_t flags)
Radek Krejcia3045382018-11-22 14:30:31 +01001325{
Radek Krejci056d0a82018-12-06 16:57:25 +01001326 struct lysc_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001327
1328 if (!node) {
1329 return NULL;
1330 }
1331
Michal Vasko22df3f02020-08-24 13:29:22 +02001332 children = lysc_node_children_p((struct lysc_node *)node, flags);
Radek Krejci056d0a82018-12-06 16:57:25 +01001333 if (children) {
1334 return *children;
1335 } else {
Radek Krejcia3045382018-11-22 14:30:31 +01001336 return NULL;
1337 }
1338}
1339
Michal Vasko2a668712020-10-21 11:48:09 +02001340API const struct lysc_node *
Michal Vasko208a04a2020-10-21 15:17:12 +02001341lysc_node_children_full(const struct lysc_node *node, uint16_t flags)
Michal Vasko2a668712020-10-21 11:48:09 +02001342{
1343 switch (node->nodetype) {
1344 case LYS_CONTAINER:
1345 return ((struct lysc_node_container *)node)->child;
1346 case LYS_CHOICE:
1347 return (struct lysc_node *)((struct lysc_node_choice *)node)->cases;
1348 case LYS_CASE:
1349 return ((struct lysc_node_case *)node)->child;
1350 case LYS_LIST:
1351 return ((struct lysc_node_list *)node)->child;
1352 case LYS_RPC:
1353 case LYS_ACTION:
1354 if (flags & LYS_CONFIG_R) {
1355 return (struct lysc_node *)&((struct lysc_action *)node)->output;
1356 } else {
1357 /* LYS_CONFIG_W, but also the default case */
1358 return (struct lysc_node *)&((struct lysc_action *)node)->input;
1359 }
1360 case LYS_INPUT:
1361 case LYS_OUTPUT:
1362 return ((struct lysc_action_inout *)node)->data;
1363 case LYS_NOTIF:
1364 return ((struct lysc_notif *)node)->data;
1365 default:
1366 return NULL;
1367 }
1368}
1369
1370API const struct lysc_node *
Michal Vasko208a04a2020-10-21 15:17:12 +02001371lysc_node_parent_full(const struct lysc_node *node)
Michal Vasko2a668712020-10-21 11:48:09 +02001372{
1373 if (!node) {
1374 return NULL;
1375 } else if (node->nodetype == LYS_INPUT) {
1376 return (struct lysc_node *)(((char *)node) - offsetof(struct lysc_action, input));
1377 } else if (node->nodetype == LYS_OUTPUT) {
1378 return (struct lysc_node *)(((char *)node) - offsetof(struct lysc_action, output));
1379 } else if (node->parent && (node->parent->nodetype & (LYS_RPC | LYS_ACTION))) {
1380 if (node->flags & LYS_CONFIG_W) {
1381 return (struct lysc_node *)&((struct lysc_action *)node->parent)->input;
1382 } else {
1383 return (struct lysc_node *)&((struct lysc_action *)node->parent)->output;
1384 }
1385 } else {
1386 return node->parent;
1387 }
1388}
1389
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001390struct lys_module *
1391lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1392{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001393 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001394 if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) {
Michal Vasko69730152020-10-09 16:30:07 +02001395 return (struct lys_module *)ctx->list.objs[u];
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001396 }
1397 }
1398 return NULL;
1399}
1400
Radek Krejcid6b76452019-09-03 17:03:03 +02001401enum ly_stmt
Michal Vasko63f3d842020-07-08 10:10:14 +02001402lysp_match_kw(struct lys_yang_parser_ctx *ctx, struct ly_in *in)
David Sedlákc10e7902018-12-17 02:17:59 +01001403{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001404/**
Michal Vasko63f3d842020-07-08 10:10:14 +02001405 * @brief Move the INPUT by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001406 * @param[in] CTX yang parser context to update its indent value.
Michal Vasko63f3d842020-07-08 10:10:14 +02001407 * @param[in,out] IN input to move
David Sedlák1bccdfa2019-06-17 15:55:27 +02001408 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001409 *
1410 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001411 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001412#define MOVE_IN(CTX, IN, COUNT) ly_in_skip(IN, COUNT);if(CTX){(CTX)->indent+=COUNT;}
1413#define IF_KW(STR, LEN, STMT) if (!strncmp(in->current, STR, LEN)) {MOVE_IN(ctx, in, LEN);*kw=STMT;}
1414#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 +02001415#define IF_KW_PREFIX_END }
David Sedlák572e7ab2019-06-04 16:01:58 +02001416
Michal Vasko63f3d842020-07-08 10:10:14 +02001417 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001418 enum ly_stmt result = LY_STMT_NONE;
1419 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001420 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001421 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001422 case 'a':
Michal Vasko63f3d842020-07-08 10:10:14 +02001423 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001424 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1425 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1426 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001427 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001428 IF_KW("data", 4, LY_STMT_ANYDATA)
1429 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001430 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001431 break;
1432 case 'b':
Michal Vasko63f3d842020-07-08 10:10:14 +02001433 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001434 IF_KW("ase", 3, LY_STMT_BASE)
1435 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1436 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001437 break;
1438 case 'c':
Michal Vasko63f3d842020-07-08 10:10:14 +02001439 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001440 IF_KW("ase", 3, LY_STMT_CASE)
1441 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001442 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001443 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001444 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001445 IF_KW("ct", 2, LY_STMT_CONTACT)
1446 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001447 IF_KW_PREFIX_END
1448 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001449 break;
1450 case 'd':
Michal Vasko63f3d842020-07-08 10:10:14 +02001451 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001452 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001453 IF_KW("fault", 5, LY_STMT_DEFAULT)
1454 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001455 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001456 IF_KW("e", 1, LY_STMT_DEVIATE)
1457 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001458 IF_KW_PREFIX_END
1459 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001460 break;
1461 case 'e':
Michal Vasko63f3d842020-07-08 10:10:14 +02001462 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001463 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001464 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001465 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1466 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001467 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001468 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001469 break;
1470 case 'f':
Michal Vasko63f3d842020-07-08 10:10:14 +02001471 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001472 IF_KW("eature", 6, LY_STMT_FEATURE)
1473 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001474 break;
1475 case 'g':
Michal Vasko63f3d842020-07-08 10:10:14 +02001476 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001477 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001478 break;
1479 case 'i':
Michal Vasko63f3d842020-07-08 10:10:14 +02001480 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001481 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1482 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1483 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001484 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001485 IF_KW("clude", 5, LY_STMT_INCLUDE)
1486 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001487 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001488 break;
1489 case 'k':
Michal Vasko63f3d842020-07-08 10:10:14 +02001490 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001491 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001492 break;
1493 case 'l':
Michal Vasko63f3d842020-07-08 10:10:14 +02001494 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001495 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001496 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1497 else IF_KW("af", 2, LY_STMT_LEAF)
1498 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001499 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001500 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001501 break;
1502 case 'm':
Michal Vasko63f3d842020-07-08 10:10:14 +02001503 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001504 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001505 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1506 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001507 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001508 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1509 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001510 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001511 IF_KW("ule", 3, LY_STMT_MODULE)
1512 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
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 'n':
Michal Vasko63f3d842020-07-08 10:10:14 +02001516 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001517 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1518 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001519 break;
1520 case 'o':
Michal Vasko63f3d842020-07-08 10:10:14 +02001521 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001522 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001523 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1524 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001525 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001526 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001527 break;
1528 case 'p':
Michal Vasko63f3d842020-07-08 10:10:14 +02001529 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001530 IF_KW("ath", 3, LY_STMT_PATH)
1531 else IF_KW("attern", 6, LY_STMT_PATTERN)
1532 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001533 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001534 IF_KW("fix", 3, LY_STMT_PREFIX)
1535 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001536 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001537 break;
1538 case 'r':
Michal Vasko63f3d842020-07-08 10:10:14 +02001539 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001540 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001541 else IF_KW_PREFIX("e", 1)
1542 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001543 IF_KW("erence", 6, LY_STMT_REFERENCE)
1544 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001545 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001546 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1547 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1548 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001549 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001550 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001551 break;
1552 case 's':
Michal Vasko63f3d842020-07-08 10:10:14 +02001553 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001554 IF_KW("tatus", 5, LY_STMT_STATUS)
1555 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001556 break;
1557 case 't':
Michal Vasko63f3d842020-07-08 10:10:14 +02001558 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001559 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1560 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001561 break;
1562 case 'u':
Michal Vasko63f3d842020-07-08 10:10:14 +02001563 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001564 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001565 IF_KW("que", 3, LY_STMT_UNIQUE)
1566 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001567 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001568 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001569 break;
1570 case 'v':
Michal Vasko63f3d842020-07-08 10:10:14 +02001571 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001572 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001573 break;
1574 case 'w':
Michal Vasko63f3d842020-07-08 10:10:14 +02001575 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001576 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001577 break;
1578 case 'y':
Michal Vasko63f3d842020-07-08 10:10:14 +02001579 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001580 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
1581 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001582 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001583 default:
David Sedlák1bccdfa2019-06-17 15:55:27 +02001584 /* if context is not NULL we are matching keyword from YANG data*/
1585 if (ctx) {
Michal Vasko63f3d842020-07-08 10:10:14 +02001586 if (in->current[0] == ';') {
1587 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001588 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02001589 } else if (in->current[0] == '{') {
1590 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001591 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001592 } else if (in->current[0] == '}') {
1593 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001594 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001595 }
1596 }
David Sedlák23a59a62018-10-26 13:08:02 +02001597 break;
1598 }
1599
Michal Vasko63f3d842020-07-08 10:10:14 +02001600 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02001601 /* the keyword is not terminated */
1602 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001603 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02001604 }
1605
David Sedlák1bccdfa2019-06-17 15:55:27 +02001606#undef IF_KW
1607#undef IF_KW_PREFIX
1608#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01001609#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02001610 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01001611
David Sedlák1bccdfa2019-06-17 15:55:27 +02001612 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02001613}
David Sedlákecf5eb82019-06-03 14:12:44 +02001614
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001615LY_ARRAY_COUNT_TYPE
1616lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, LYEXT_SUBSTMT substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001617{
1618 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1619
Michal Vaskod989ba02020-08-24 10:59:24 +02001620 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001621 if (ext[index].insubstmt == substmt) {
1622 return index;
1623 }
1624 }
1625
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001626 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001627}
1628
Michal Vasko62ed12d2020-05-21 10:08:25 +02001629const struct lysc_node *
1630lysc_data_parent(const struct lysc_node *schema)
1631{
1632 const struct lysc_node *parent;
1633
Radek Krejci1e008d22020-08-17 11:37:37 +02001634 for (parent = schema->parent; parent && (parent->nodetype & (LYS_CHOICE | LYS_CASE)); parent = parent->parent) {}
Michal Vasko62ed12d2020-05-21 10:08:25 +02001635
1636 return parent;
1637}
Michal Vasko00cbf532020-06-15 13:58:47 +02001638
Radek Krejci857189e2020-09-01 13:26:36 +02001639ly_bool
Michal Vasko00cbf532020-06-15 13:58:47 +02001640lysc_is_output(const struct lysc_node *schema)
1641{
1642 const struct lysc_node *parent;
1643
1644 assert(schema);
1645
Radek Krejci1e008d22020-08-17 11:37:37 +02001646 for (parent = schema->parent; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION)); parent = parent->parent) {}
Michal Vasko00cbf532020-06-15 13:58:47 +02001647 if (parent && (schema->flags & LYS_CONFIG_R)) {
1648 return 1;
1649 }
1650 return 0;
1651}