blob: c1693591c2b4092f9f51a3a0b2d7bd4fac8ad371 [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
Michal Vaskoc5a22832020-08-20 13:21:33 +020024#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.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"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020029#include "parser.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020030#include "parser_schema.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020031#include "parser_internal.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
Michal Vaskocb18c7f2020-08-24 09:22:28 +020038lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *context_node,
Radek Krejci857189e2020-09-01 13:26:36 +020039 const struct lys_module *context_module, uint16_t nodetype, ly_bool implement,
Radek Krejci0f969882020-08-21 16:56:47 +020040 const struct lysc_node **target, uint16_t *result_flag)
Radek Krejci9bb94eb2018-12-04 16:48:35 +010041{
42 LY_ERR ret = LY_EVALID;
43 const char *name, *prefix, *id;
Radek Krejci9bb94eb2018-12-04 16:48:35 +010044 size_t name_len, prefix_len;
Radek Krejci7af64242019-02-18 13:07:53 +010045 const struct lys_module *mod;
Radek Krejci95710c92019-02-11 15:49:55 +010046 const char *nodeid_type;
Radek Krejci1deb5be2020-08-26 16:43:36 +020047 uint32_t getnext_extra_flag = 0;
48 uint16_t current_nodetype = 0;
Radek Krejci9bb94eb2018-12-04 16:48:35 +010049
50 assert(nodeid);
Radek Krejci9bb94eb2018-12-04 16:48:35 +010051 assert(target);
Radek Krejci6eeb58f2019-02-22 16:29:37 +010052 assert(result_flag);
Radek Krejci9bb94eb2018-12-04 16:48:35 +010053 *target = NULL;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010054 *result_flag = 0;
Radek Krejci9bb94eb2018-12-04 16:48:35 +010055
56 id = nodeid;
Radek Krejci95710c92019-02-11 15:49:55 +010057
58 if (context_node) {
59 /* descendant-schema-nodeid */
60 nodeid_type = "descendant";
Radek Krejci3641f562019-02-13 15:38:40 +010061
62 if (*id == '/') {
63 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
64 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
65 nodeid_len ? nodeid_len : strlen(nodeid), nodeid);
66 return LY_EVALID;
67 }
Radek Krejci95710c92019-02-11 15:49:55 +010068 } else {
69 /* absolute-schema-nodeid */
70 nodeid_type = "absolute";
Radek Krejci95710c92019-02-11 15:49:55 +010071
72 if (*id != '/') {
73 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
74 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci3641f562019-02-13 15:38:40 +010075 nodeid_len ? nodeid_len : strlen(nodeid), nodeid);
Radek Krejci95710c92019-02-11 15:49:55 +010076 return LY_EVALID;
77 }
78 ++id;
79 }
80
Radek Krejcib4a4a272019-06-10 12:44:52 +020081 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +010082 if (prefix) {
Radek Krejci95710c92019-02-11 15:49:55 +010083 mod = lys_module_find_prefix(context_module, prefix, prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +010084 if (!mod) {
85 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci95710c92019-02-11 15:49:55 +010086 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
87 nodeid_type, id - nodeid, nodeid, prefix_len, prefix, context_module->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +010088 return LY_ENOTFOUND;
89 }
90 } else {
Radek Krejci95710c92019-02-11 15:49:55 +010091 mod = context_module;
92 }
93 if (implement && !mod->implemented) {
94 /* make the module implemented */
Michal Vasko22df3f02020-08-24 13:29:22 +020095 ret = lys_set_implemented_internal((struct lys_module *)mod, ctx->ctx->module_set_id);
Michal Vasko294b76d2019-12-16 10:37:54 +010096 LY_CHECK_RET(ret);
Radek Krejci9bb94eb2018-12-04 16:48:35 +010097 }
Michal Vasko1bf09392020-03-27 12:38:10 +010098 if (context_node && (context_node->nodetype & (LYS_RPC | LYS_ACTION))) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010099 /* move through input/output manually */
Radek Krejci7f9b6512019-09-18 13:11:09 +0200100 if (!ly_strncmp("input", name, name_len)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100101 (*result_flag) |= LYSC_OPT_RPC_INPUT;
Radek Krejci7f9b6512019-09-18 13:11:09 +0200102 } else if (!ly_strncmp("output", name, name_len)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100103 (*result_flag) |= LYSC_OPT_RPC_OUTPUT;
104 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
Radek Krejci05b774b2019-02-25 13:26:18 +0100105 } else {
106 goto getnext;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100107 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100108 current_nodetype = LYS_INOUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100109 } else {
Radek Krejci05b774b2019-02-25 13:26:18 +0100110getnext:
Michal Vaskoe444f752020-02-10 12:20:06 +0100111 context_node = lys_find_child(context_node, mod, name, name_len, 0,
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100112 getnext_extra_flag | LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
113 if (!context_node) {
114 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
115 "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.", nodeid_type, id - nodeid, nodeid);
116 return LY_ENOTFOUND;
117 }
118 getnext_extra_flag = 0;
Radek Krejci05b774b2019-02-25 13:26:18 +0100119 current_nodetype = context_node->nodetype;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100120
Radek Krejci05b774b2019-02-25 13:26:18 +0100121 if (current_nodetype == LYS_NOTIF) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100122 (*result_flag) |= LYSC_OPT_NOTIFICATION;
123 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100124 }
Radek Krejci01342af2019-01-03 15:18:08 +0100125 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100126 break;
127 }
Radek Krejci01342af2019-01-03 15:18:08 +0100128 if (*id != '/') {
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100129 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci95710c92019-02-11 15:49:55 +0100130 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
131 nodeid_type, id - nodeid + 1, nodeid);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100132 return LY_EVALID;
133 }
134 ++id;
135 }
136
137 if (ret == LY_SUCCESS) {
Radek Krejci7af64242019-02-18 13:07:53 +0100138 *target = context_node;
Radek Krejci05b774b2019-02-25 13:26:18 +0100139 if (nodetype & LYS_INOUT) {
140 /* instead of input/output nodes, the RPC/action node is actually returned */
141 }
142 if (nodetype && !(current_nodetype & nodetype)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100143 return LY_EDENIED;
144 }
Radek Krejci95710c92019-02-11 15:49:55 +0100145 } else {
146 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
147 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci3641f562019-02-13 15:38:40 +0100148 nodeid_type, nodeid_len ? nodeid_len : strlen(nodeid), nodeid);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100149 }
150
151 return ret;
152}
153
154LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200155lysp_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 +0200156{
157 struct lysp_import *i;
158
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100159 if (module_prefix && &module_prefix != value && !strcmp(module_prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100160 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value);
Radek Krejci86d106e2018-10-18 09:53:19 +0200161 return LY_EEXIST;
162 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100163 LY_ARRAY_FOR(imports, struct lysp_import, i) {
164 if (i->prefix && &i->prefix != value && !strcmp(i->prefix, *value)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100165 LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100166 return LY_EEXIST;
Radek Krejci86d106e2018-10-18 09:53:19 +0200167 }
168 }
169 return LY_SUCCESS;
170}
171
172LY_ERR
Radek Krejci4f28eda2018-11-12 11:46:16 +0100173lysc_check_status(struct lysc_ctx *ctx,
Radek Krejci0f969882020-08-21 16:56:47 +0200174 uint16_t flags1, void *mod1, const char *name1,
175 uint16_t flags2, void *mod2, const char *name2)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100176{
177 uint16_t flg1, flg2;
178
179 flg1 = (flags1 & LYS_STATUS_MASK) ? (flags1 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
180 flg2 = (flags2 & LYS_STATUS_MASK) ? (flags2 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
181
182 if ((flg1 < flg2) && (mod1 == mod2)) {
183 if (ctx) {
184 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
185 "A %s definition \"%s\" is not allowed to reference %s definition \"%s\".",
186 flg1 == LYS_STATUS_CURR ? "current" : "deprecated", name1,
187 flg2 == LYS_STATUS_OBSLT ? "obsolete" : "deprecated", name2);
188 }
189 return LY_EVALID;
190 }
191
192 return LY_SUCCESS;
193}
194
195LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200196lysp_check_date(struct lys_parser_ctx *ctx, const char *date, uint8_t date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +0200197{
Radek Krejci86d106e2018-10-18 09:53:19 +0200198 struct tm tm, tm_;
199 char *r;
200
Michal Vaskob36053d2020-03-26 15:49:30 +0100201 LY_CHECK_ARG_RET(ctx ? PARSER_CTX(ctx) : NULL, date, LY_EINVAL);
202 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 +0200203
204 /* check format */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200205 for (uint8_t i = 0; i < date_len; i++) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200206 if (i == 4 || i == 7) {
207 if (date[i] != '-') {
208 goto error;
209 }
210 } else if (!isdigit(date[i])) {
211 goto error;
212 }
213 }
214
215 /* check content, e.g. 2018-02-31 */
216 memset(&tm, 0, sizeof tm);
217 r = strptime(date, "%Y-%m-%d", &tm);
218 if (!r || r != &date[LY_REV_SIZE - 1]) {
219 goto error;
220 }
221 memcpy(&tm_, &tm, sizeof tm);
222 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
223 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
224 /* checking days is enough, since other errors
225 * have been checked by strptime() */
226 goto error;
227 }
228
229 return LY_SUCCESS;
230
231error:
Radek Krejcid33273d2018-10-25 14:55:52 +0200232 if (stmt) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100233 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100234 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100235 } else {
236 LOGVAL(NULL, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, date_len, date, stmt);
237 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200238 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200239 return LY_EINVAL;
240}
241
242void
243lysp_sort_revisions(struct lysp_revision *revs)
244{
Radek Krejci857189e2020-09-01 13:26:36 +0200245 LY_ARRAY_COUNT_TYPE i, r;
Radek Krejci86d106e2018-10-18 09:53:19 +0200246 struct lysp_revision rev;
247
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200248 for (i = 1, r = 0; revs && i < LY_ARRAY_COUNT(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200249 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200250 r = i;
251 }
252 }
253
254 if (r) {
255 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200256 memcpy(&rev, &revs[0], sizeof rev);
257 memcpy(&revs[0], &revs[r], sizeof rev);
258 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200259 }
260}
Radek Krejci151a5b72018-10-19 14:21:44 +0200261
Radek Krejcibbe09a92018-11-08 09:36:54 +0100262static const struct lysp_tpdf *
263lysp_type_match(const char *name, struct lysp_node *node)
264{
Radek Krejci0fb28562018-12-13 15:17:37 +0100265 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200266 LY_ARRAY_COUNT_TYPE u;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100267
Radek Krejci0fb28562018-12-13 15:17:37 +0100268 typedefs = lysp_node_typedefs(node);
269 LY_ARRAY_FOR(typedefs, u) {
270 if (!strcmp(name, typedefs[u].name)) {
271 /* match */
272 return &typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100273 }
274 }
275
276 return NULL;
277}
278
Radek Krejci4f28eda2018-11-12 11:46:16 +0100279static LY_DATA_TYPE
280lysp_type_str2builtin(const char *name, size_t len)
281{
282 if (len >= 4) { /* otherwise it does not match any built-in type */
283 if (name[0] == 'b') {
284 if (name[1] == 'i') {
285 if (len == 6 && !strncmp(&name[2], "nary", 4)) {
286 return LY_TYPE_BINARY;
287 } else if (len == 4 && !strncmp(&name[2], "ts", 2)) {
288 return LY_TYPE_BITS;
289 }
290 } else if (len == 7 && !strncmp(&name[1], "oolean", 6)) {
291 return LY_TYPE_BOOL;
292 }
293 } else if (name[0] == 'd') {
294 if (len == 9 && !strncmp(&name[1], "ecimal64", 8)) {
295 return LY_TYPE_DEC64;
296 }
297 } else if (name[0] == 'e') {
298 if (len == 5 && !strncmp(&name[1], "mpty", 4)) {
299 return LY_TYPE_EMPTY;
300 } else if (len == 11 && !strncmp(&name[1], "numeration", 10)) {
301 return LY_TYPE_ENUM;
302 }
303 } else if (name[0] == 'i') {
304 if (name[1] == 'n') {
305 if (len == 4 && !strncmp(&name[2], "t8", 2)) {
306 return LY_TYPE_INT8;
307 } else if (len == 5) {
308 if (!strncmp(&name[2], "t16", 3)) {
309 return LY_TYPE_INT16;
310 } else if (!strncmp(&name[2], "t32", 3)) {
311 return LY_TYPE_INT32;
312 } else if (!strncmp(&name[2], "t64", 3)) {
313 return LY_TYPE_INT64;
314 }
315 } else if (len == 19 && !strncmp(&name[2], "stance-identifier", 17)) {
316 return LY_TYPE_INST;
317 }
318 } else if (len == 11 && !strncmp(&name[1], "dentityref", 10)) {
319 return LY_TYPE_IDENT;
320 }
321 } else if (name[0] == 'l') {
322 if (len == 7 && !strncmp(&name[1], "eafref", 6)) {
323 return LY_TYPE_LEAFREF;
324 }
325 } else if (name[0] == 's') {
326 if (len == 6 && !strncmp(&name[1], "tring", 5)) {
327 return LY_TYPE_STRING;
328 }
329 } else if (name[0] == 'u') {
330 if (name[1] == 'n') {
331 if (len == 5 && !strncmp(&name[2], "ion", 3)) {
332 return LY_TYPE_UNION;
333 }
334 } else if (name[1] == 'i' && name[2] == 'n' && name[3] == 't') {
335 if (len == 5 && name[4] == '8') {
336 return LY_TYPE_UINT8;
337 } else if (len == 6) {
338 if (!strncmp(&name[4], "16", 2)) {
339 return LY_TYPE_UINT16;
340 } else if (!strncmp(&name[4], "32", 2)) {
341 return LY_TYPE_UINT32;
342 } else if (!strncmp(&name[4], "64", 2)) {
343 return LY_TYPE_UINT64;
344 }
345 }
346 }
347 }
348 }
349
350 return LY_TYPE_UNKNOWN;
351}
352
Radek Krejcibbe09a92018-11-08 09:36:54 +0100353LY_ERR
354lysp_type_find(const char *id, struct lysp_node *start_node, struct lysp_module *start_module,
Radek Krejci0f969882020-08-21 16:56:47 +0200355 LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node, struct lysp_module **module)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100356{
357 const char *str, *name;
358 struct lysp_tpdf *typedefs;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200359 struct lys_module *mod;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200360 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100361
362 assert(id);
363 assert(start_module);
364 assert(tpdf);
365 assert(node);
366 assert(module);
367
Radek Krejci4f28eda2018-11-12 11:46:16 +0100368 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100369 str = strchr(id, ':');
370 if (str) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200371 mod = lys_module_find_prefix(start_module->mod, id, str - id);
372 *module = mod ? mod->parsed : NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100373 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100374 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100375 } else {
376 *module = start_module;
377 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100378
379 /* check for built-in types */
380 *type = lysp_type_str2builtin(name, strlen(name));
381 if (*type) {
382 *tpdf = NULL;
383 return LY_SUCCESS;
384 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100385 }
386 LY_CHECK_RET(!(*module), LY_ENOTFOUND);
387
388 if (start_node && *module == start_module) {
389 /* search typedefs in parent's nodes */
390 *node = start_node;
391 while (*node) {
392 *tpdf = lysp_type_match(name, *node);
393 if (*tpdf) {
394 /* match */
395 return LY_SUCCESS;
396 }
397 *node = (*node)->parent;
398 }
399 }
400
401 /* search in top-level typedefs */
402 if ((*module)->typedefs) {
403 LY_ARRAY_FOR((*module)->typedefs, u) {
404 if (!strcmp(name, (*module)->typedefs[u].name)) {
405 /* match */
406 *tpdf = &(*module)->typedefs[u];
407 return LY_SUCCESS;
408 }
409 }
410 }
411
412 /* search in submodules' typedefs */
413 LY_ARRAY_FOR((*module)->includes, u) {
414 typedefs = (*module)->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100415 LY_ARRAY_FOR(typedefs, v) {
416 if (!strcmp(name, typedefs[v].name)) {
417 /* match */
418 *tpdf = &typedefs[v];
419 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100420 }
421 }
422 }
423
424 return LY_ENOTFOUND;
425}
426
David Sedlák6544c182019-07-12 13:17:33 +0200427LY_ERR
David Sedlák07869a52019-07-12 14:28:19 +0200428lysp_check_enum_name(struct lys_parser_ctx *ctx, const char *name, size_t name_len)
David Sedlák6544c182019-07-12 13:17:33 +0200429{
430 if (!name_len) {
431 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
432 return LY_EVALID;
433 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
434 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
435 name_len, name);
436 return LY_EVALID;
437 } else {
438 for (size_t u = 0; u < name_len; ++u) {
439 if (iscntrl(name[u])) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100440 LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
David Sedlák6544c182019-07-12 13:17:33 +0200441 name_len, name, u + 1);
442 break;
443 }
444 }
445 }
446
447 return LY_SUCCESS;
448}
449
Michal Vaskob36053d2020-03-26 15:49:30 +0100450/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100451 * @brief Check name of a new type to avoid name collisions.
452 *
453 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
454 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
455 * @param[in] tpdf Typedef definition to check.
456 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
457 * typedefs are checked, caller is supposed to free the table.
458 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
459 * typedefs are checked, caller is supposed to free the table.
460 * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise.
461 */
462static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200463lysp_check_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
Radek Krejci0f969882020-08-21 16:56:47 +0200464 struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100465{
466 struct lysp_node *parent;
467 uint32_t hash;
468 size_t name_len;
469 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200470 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100471 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100472
473 assert(ctx);
474 assert(tpdf);
475
476 name = tpdf->name;
477 name_len = strlen(name);
478
Radek Krejci4f28eda2018-11-12 11:46:16 +0100479 if (lysp_type_str2builtin(name, name_len)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100480 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 +0100481 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100482 }
483
484 /* check locally scoped typedefs (avoid name shadowing) */
485 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100486 typedefs = lysp_node_typedefs(node);
487 LY_ARRAY_FOR(typedefs, u) {
488 if (&typedefs[u] == tpdf) {
489 break;
490 }
491 if (!strcmp(name, typedefs[u].name)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100492 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with sibling type.", name);
Radek Krejci0fb28562018-12-13 15:17:37 +0100493 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100494 }
495 }
496 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200497 for (parent = node->parent; parent; parent = parent->parent) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100498 if (lysp_type_match(name, parent)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100499 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 +0100500 return LY_EEXIST;
501 }
502 }
503 }
504
505 /* check collision with the top-level typedefs */
506 hash = dict_hash(name, name_len);
507 if (node) {
508 lyht_insert(tpdfs_scoped, &name, hash, NULL);
509 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100510 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 +0100511 return LY_EEXIST;
512 }
513 } else {
514 if (lyht_insert(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100515 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 +0100516 return LY_EEXIST;
517 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100518 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
519 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
520 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100521 }
522
523 return LY_SUCCESS;
524}
525
Radek Krejci857189e2020-09-01 13:26:36 +0200526/**
527 * @brief Compare identifiers.
528 * Implementation of ::values_equal_cb.
529 */
530static ly_bool
531lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Radek Krejcibbe09a92018-11-08 09:36:54 +0100532{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200533 return strcmp(val1, val2) == 0 ? 1 : 0;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100534}
535
536LY_ERR
David Sedlákd2ebe572019-07-22 12:53:14 +0200537lysp_parse_finalize_reallocated(struct lys_parser_ctx *ctx, struct lysp_grp *groupings, struct lysp_augment *augments,
Radek Krejci0f969882020-08-21 16:56:47 +0200538 struct lysp_action *actions, struct lysp_notif *notifs)
David Sedlákd2ebe572019-07-22 12:53:14 +0200539{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200540 LY_ARRAY_COUNT_TYPE u, v;
David Sedlákd2ebe572019-07-22 12:53:14 +0200541 struct lysp_node *child;
542
543 /* finalize parent pointers to the reallocated items */
544
545 /* gropings */
546 LY_ARRAY_FOR(groupings, u) {
547 LY_LIST_FOR(groupings[u].data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200548 child->parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200549 }
550 LY_ARRAY_FOR(groupings[u].actions, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200551 groupings[u].actions[v].parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200552 }
553 LY_ARRAY_FOR(groupings[u].notifs, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200554 groupings[u].notifs[v].parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200555 }
556 LY_ARRAY_FOR(groupings[u].groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200557 groupings[u].groupings[v].parent = (struct lysp_node *)&groupings[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200558 }
559 if (groupings[u].typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200560 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &groupings[u], 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200561 }
562 }
563
564 /* augments */
565 LY_ARRAY_FOR(augments, u) {
566 LY_LIST_FOR(augments[u].child, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200567 child->parent = (struct lysp_node *)&augments[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200568 }
569 LY_ARRAY_FOR(augments[u].actions, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200570 augments[u].actions[v].parent = (struct lysp_node *)&augments[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200571 }
572 LY_ARRAY_FOR(augments[u].notifs, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200573 augments[u].notifs[v].parent = (struct lysp_node *)&augments[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200574 }
575 }
576
577 /* actions */
578 LY_ARRAY_FOR(actions, u) {
579 if (actions[u].input.parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200580 actions[u].input.parent = (struct lysp_node *)&actions[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200581 LY_LIST_FOR(actions[u].input.data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200582 child->parent = (struct lysp_node *)&actions[u].input;
David Sedlákd2ebe572019-07-22 12:53:14 +0200583 }
584 LY_ARRAY_FOR(actions[u].input.groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200585 actions[u].input.groupings[v].parent = (struct lysp_node *)&actions[u].input;
David Sedlákd2ebe572019-07-22 12:53:14 +0200586 }
587 if (actions[u].input.typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200588 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &actions[u].input, 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200589 }
590 }
591 if (actions[u].output.parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200592 actions[u].output.parent = (struct lysp_node *)&actions[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200593 LY_LIST_FOR(actions[u].output.data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200594 child->parent = (struct lysp_node *)&actions[u].output;
David Sedlákd2ebe572019-07-22 12:53:14 +0200595 }
596 LY_ARRAY_FOR(actions[u].output.groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200597 actions[u].output.groupings[v].parent = (struct lysp_node *)&actions[u].output;
David Sedlákd2ebe572019-07-22 12:53:14 +0200598 }
599 if (actions[u].output.typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200600 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &actions[u].output, 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200601 }
602 }
603 LY_ARRAY_FOR(actions[u].groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200604 actions[u].groupings[v].parent = (struct lysp_node *)&actions[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200605 }
606 if (actions[u].typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200607 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &actions[u], 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200608 }
609 }
610
611 /* notifications */
612 LY_ARRAY_FOR(notifs, u) {
613 LY_LIST_FOR(notifs[u].data, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200614 child->parent = (struct lysp_node *)&notifs[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200615 }
616 LY_ARRAY_FOR(notifs[u].groupings, v) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200617 notifs[u].groupings[v].parent = (struct lysp_node *)&notifs[u];
David Sedlákd2ebe572019-07-22 12:53:14 +0200618 }
619 if (notifs[u].typedefs) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200620 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, &notifs[u], 0, NULL));
David Sedlákd2ebe572019-07-22 12:53:14 +0200621 }
622 }
623
624 return LY_SUCCESS;
625}
626
627LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200628lysp_check_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100629{
630 struct hash_table *ids_global;
631 struct hash_table *ids_scoped;
Radek Krejci0fb28562018-12-13 15:17:37 +0100632 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200633 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200634 uint32_t i;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100635 LY_ERR ret = LY_EVALID;
636
637 /* check name collisions - typedefs and groupings */
Michal Vasko22df3f02020-08-24 13:29:22 +0200638 ids_global = lyht_new(8, sizeof(char *), lysp_id_cmp, NULL, 1);
639 ids_scoped = lyht_new(8, sizeof(char *), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200640 LY_ARRAY_FOR(mod->typedefs, v) {
641 if (lysp_check_typedef(ctx, NULL, &mod->typedefs[v], ids_global, ids_scoped)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100642 goto cleanup;
643 }
644 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200645 LY_ARRAY_FOR(mod->includes, v) {
646 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
647 if (lysp_check_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global, ids_scoped)) {
Radek Krejci3b1f9292018-11-08 10:58:35 +0100648 goto cleanup;
649 }
650 }
651 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200652 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
653 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
654 LY_ARRAY_FOR(typedefs, u) {
655 if (lysp_check_typedef(ctx, (struct lysp_node *)ctx->tpdfs_nodes.objs[i], &typedefs[u], ids_global, ids_scoped)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100656 goto cleanup;
657 }
658 }
659 }
660 ret = LY_SUCCESS;
661cleanup:
662 lyht_free(ids_global);
663 lyht_free(ids_scoped);
664 ly_set_erase(&ctx->tpdfs_nodes, NULL);
665
666 return ret;
667}
668
Radek Krejci9ed7a192018-10-31 16:23:51 +0100669struct lysp_load_module_check_data {
670 const char *name;
671 const char *revision;
672 const char *path;
Michal Vasko22df3f02020-08-24 13:29:22 +0200673 const char *submoduleof;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100674};
675
676static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100677lysp_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 +0100678{
679 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100680 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200681 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100682 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100683 struct lysp_revision *revs;
684
685 name = mod ? mod->mod->name : submod->name;
686 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200687 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100688
689 if (info->name) {
690 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100691 if (strcmp(info->name, name)) {
692 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100693 return LY_EINVAL;
694 }
695 }
696 if (info->revision) {
697 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100698 if (!revs || strcmp(info->revision, revs[0].date)) {
699 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Radek Krejcib07b5c92019-04-08 10:56:37 +0200700 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100701 return LY_EINVAL;
702 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200703 } else if (!latest_revision) {
704 /* do not log, we just need to drop the schema and use the latest revision from the context */
705 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100706 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100707 if (submod) {
708 assert(info->submoduleof);
709
Radek Krejci9ed7a192018-10-31 16:23:51 +0100710 /* check that the submodule belongs-to our module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100711 if (strcmp(info->submoduleof, submod->belongsto)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100712 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100713 submod->name, info->submoduleof, submod->belongsto);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100714 return LY_EVALID;
715 }
716 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100717 if (submod->parsing) {
718 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100719 return LY_EVALID;
720 }
721 }
722 if (info->path) {
723 /* check that name and revision match filename */
724 filename = strrchr(info->path, '/');
725 if (!filename) {
726 filename = info->path;
727 } else {
728 filename++;
729 }
730 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100731 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100732 rev = strchr(filename, '@');
733 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100734 if (strncmp(filename, name, len) ||
Radek Krejci9ed7a192018-10-31 16:23:51 +0100735 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100736 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100737 }
738 /* revision */
739 if (rev) {
740 len = dot - ++rev;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100741 if (!revs || len != 10 || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100742 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100743 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100744 }
745 }
746 }
747 return LY_SUCCESS;
748}
749
750LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200751lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, ly_bool implement,
752 struct lys_parser_ctx *main_ctx, const char *main_name, ly_bool required, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100753{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200754 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100755 char *filepath = NULL;
756 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100757 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100758 LY_ERR ret = LY_SUCCESS;
759 struct lysp_load_module_check_data check_data = {0};
760
761 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, revision,
762 &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200763 if (!filepath) {
764 if (required) {
765 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
766 revision ? revision : "");
767 }
768 return LY_ENOTFOUND;
769 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100770
771 LOGVRB("Loading schema from \"%s\" file.", filepath);
772
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200773 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200774 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
775 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100776 check_data.name = name;
777 check_data.revision = revision;
778 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800779 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200780 if (main_ctx) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200781 ret = lys_parse_mem_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data,
Radek Krejci0f969882020-08-21 16:56:47 +0200782 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200783 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200784 ret = lys_parse_mem_module(ctx, in, format, implement, lysp_load_module_check, &check_data,
Radek Krejci0f969882020-08-21 16:56:47 +0200785 (struct lys_module **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200786
787 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200788 LY_CHECK_ERR_GOTO(ret, ly_in_free(in, 1), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100789
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100790 if (main_ctx) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200791 lys_parser_fill_filepath(ctx, in, &((struct lysp_submodule *)mod)->filepath);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100792 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200793 lys_parser_fill_filepath(ctx, in, &((struct lys_module *)mod)->filepath);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100794 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200795 ly_in_free(in, 1);
796
797 if (mod && implement) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200798 lys_compile((struct lys_module **)&mod, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100799 }
800
801 *result = mod;
802
803 /* success */
804cleanup:
805 free(filepath);
806 return ret;
807}
808
Radek Krejcid33273d2018-10-25 14:55:52 +0200809LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200810lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, ly_bool implement, ly_bool require_parsed,
Radek Krejci0f969882020-08-21 16:56:47 +0200811 struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200812{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100813 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200814 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100815 void (*module_data_free)(void *module_data, void *user_data) = NULL;
816 struct lysp_load_module_check_data check_data = {0};
Radek Krejcib3289d62019-09-18 12:21:39 +0200817 struct lys_module *m = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200818 struct ly_in *in;
Radek Krejci086c7132018-10-26 15:29:04 +0200819
Radek Krejci0af46292019-01-11 16:02:31 +0100820 assert(mod);
821
Radek Krejcia53d7c92020-08-21 11:30:56 +0200822 if (ctx->flags & LY_CTX_ALLIMPLEMENTED) {
823 implement = 1;
824 }
825
Radek Krejci0af46292019-01-11 16:02:31 +0100826 if (!*mod) {
827 /* try to get the module from the context */
828 if (revision) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200829 /* get the specific revision */
Michal Vasko22df3f02020-08-24 13:29:22 +0200830 *mod = (struct lys_module *)ly_ctx_get_module(ctx, name, revision);
Radek Krejcied5acc52019-04-25 15:57:04 +0200831 } else if (implement) {
832 /* prefer the implemented module instead of the latest one */
Michal Vasko22df3f02020-08-24 13:29:22 +0200833 *mod = (struct lys_module *)ly_ctx_get_module_implemented(ctx, name);
Radek Krejcied5acc52019-04-25 15:57:04 +0200834 if (!*mod) {
835 /* there is no implemented module in the context, try to get the latest revision module */
836 goto latest_in_the_context;
837 }
Radek Krejci0af46292019-01-11 16:02:31 +0100838 } else {
Radek Krejcied5acc52019-04-25 15:57:04 +0200839 /* get the requested module of the latest revision in the context */
840latest_in_the_context:
Michal Vasko22df3f02020-08-24 13:29:22 +0200841 *mod = (struct lys_module *)ly_ctx_get_module_latest(ctx, name);
Radek Krejcib3289d62019-09-18 12:21:39 +0200842 if (*mod && (*mod)->latest_revision == 1) {
843 /* let us now search with callback and searchpaths to check if there is newer revision outside the context */
844 m = *mod;
845 *mod = NULL;
846 }
Radek Krejci0af46292019-01-11 16:02:31 +0100847 }
Radek Krejci086c7132018-10-26 15:29:04 +0200848 }
849
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100850 if (!(*mod) || (require_parsed && !(*mod)->parsed)) {
851 (*mod) = NULL;
852
Radek Krejci086c7132018-10-26 15:29:04 +0200853 /* check collision with other implemented revision */
854 if (implement && ly_ctx_get_module_implemented(ctx, name)) {
855 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
856 "Module \"%s\" is already present in other implemented revision.", name);
857 return LY_EDENIED;
858 }
859
Radek Krejci9ed7a192018-10-31 16:23:51 +0100860 /* module not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200861 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
862search_clb:
863 if (ctx->imp_clb) {
864 if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100865 &format, &module_data, &module_data_free) == LY_SUCCESS) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200866 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
Radek Krejci9ed7a192018-10-31 16:23:51 +0100867 check_data.name = name;
868 check_data.revision = revision;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200869 lys_parse_mem_module(ctx, in, format, implement, lysp_load_module_check, &check_data, mod);
Michal Vasko63f3d842020-07-08 10:10:14 +0200870 ly_in_free(in, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100871 if (module_data_free) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200872 module_data_free((void *)module_data, ctx->imp_clb_data);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100873 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200874 if (*mod && implement) {
875 lys_compile(mod, 0);
Radek Krejci096235c2019-01-11 11:12:19 +0100876 }
Radek Krejci086c7132018-10-26 15:29:04 +0200877 }
878 }
879 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
880 goto search_file;
881 }
882 } else {
883search_file:
884 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
885 /* module was not received from the callback or there is no callback set */
Radek Krejci78f06822019-10-30 12:54:05 +0100886 lys_module_localfile(ctx, name, revision, implement, NULL, NULL, m ? 0 : 1, (void **)mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200887 }
888 if (!(*mod) && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
889 goto search_clb;
890 }
891 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100892
Radek Krejcib3289d62019-09-18 12:21:39 +0200893 /* update the latest_revision flag - here we have selected the latest available schema,
894 * consider that even the callback provides correct latest revision */
895 if (!(*mod) && m) {
Radek Krejci78f06822019-10-30 12:54:05 +0100896 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 +0200897 m->latest_revision = 2;
898 *mod = m;
899 } else if ((*mod) && !revision && ((*mod)->latest_revision == 1)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100900 (*mod)->latest_revision = 2;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100901 }
Radek Krejci086c7132018-10-26 15:29:04 +0200902 } else {
903 /* we have module from the current context */
Radek Krejci0af46292019-01-11 16:02:31 +0100904 if (implement) {
905 m = ly_ctx_get_module_implemented(ctx, name);
906 if (m && m != *mod) {
907 /* check collision with other implemented revision */
908 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
909 "Module \"%s\" is already present in other implemented revision.", name);
910 *mod = NULL;
911 return LY_EDENIED;
912 }
Radek Krejci086c7132018-10-26 15:29:04 +0200913 }
914
915 /* circular check */
Radek Krejcif8f882a2018-10-31 14:51:15 +0100916 if ((*mod)->parsed && (*mod)->parsed->parsing) {
Radek Krejci086c7132018-10-26 15:29:04 +0200917 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
918 *mod = NULL;
919 return LY_EVALID;
920 }
921 }
922 if (!(*mod)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100923 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "%s \"%s\" module failed.", implement ? "Loading" : "Importing", name);
Radek Krejci086c7132018-10-26 15:29:04 +0200924 return LY_EVALID;
925 }
926
927 if (implement) {
928 /* mark the module implemented, check for collision was already done */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100929 (*mod)->implemented = 1;
Radek Krejci086c7132018-10-26 15:29:04 +0200930 }
Radek Krejci086c7132018-10-26 15:29:04 +0200931
932 return LY_SUCCESS;
933}
934
935LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200936lysp_check_stringchar(struct lys_parser_ctx *ctx, uint32_t c)
David Sedlák4a650532019-07-10 11:55:18 +0200937{
938 if (!is_yangutf8char(c)) {
939 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
940 return LY_EVALID;
941 }
942 return LY_SUCCESS;
943}
944
945LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200946lysp_check_identifierchar(struct lys_parser_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
David Sedlák4a650532019-07-10 11:55:18 +0200947{
948 if (first || (prefix && (*prefix) == 1)) {
949 if (!is_yangidentstartchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200950 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200951 return LY_EVALID;
952 }
953 if (prefix) {
954 if (first) {
955 (*prefix) = 0;
956 } else {
957 (*prefix) = 2;
958 }
959 }
960 } else if (c == ':' && prefix && (*prefix) == 0) {
961 (*prefix) = 1;
962 } else if (!is_yangidentchar(c)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200963 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
David Sedlák4a650532019-07-10 11:55:18 +0200964 return LY_EVALID;
965 }
966
967 return LY_SUCCESS;
968}
969
970LY_ERR
Michal Vasko7c8439f2020-08-05 13:25:19 +0200971lysp_load_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200972{
Michal Vaskob36053d2020-03-26 15:49:30 +0100973 struct ly_ctx *ctx = (struct ly_ctx *)(PARSER_CTX(pctx));
Radek Krejci3eb299d2019-04-08 15:07:44 +0200974 struct lysp_submodule *submod = NULL;
Radek Krejcid33273d2018-10-25 14:55:52 +0200975 const char *submodule_data = NULL;
976 LYS_INFORMAT format = LYS_IN_UNKNOWN;
977 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100978 struct lysp_load_module_check_data check_data = {0};
Michal Vasko63f3d842020-07-08 10:10:14 +0200979 struct ly_in *in;
Radek Krejcid33273d2018-10-25 14:55:52 +0200980
Radek Krejcibbe09a92018-11-08 09:36:54 +0100981 /* submodule not present in the context, get the input data and parse it */
Michal Vaskob36053d2020-03-26 15:49:30 +0100982 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200983search_clb:
Michal Vaskob36053d2020-03-26 15:49:30 +0100984 if (ctx->imp_clb) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200985 if (ctx->imp_clb(pctx->main_mod->name, NULL, inc->name, inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
Radek Krejcibbe09a92018-11-08 09:36:54 +0100986 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200987 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
Radek Krejcibbe09a92018-11-08 09:36:54 +0100988 check_data.name = inc->name;
989 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200990 check_data.submoduleof = pctx->main_mod->name;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200991 lys_parse_mem_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, &submod);
Michal Vasko63f3d842020-07-08 10:10:14 +0200992 ly_in_free(in, 0);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100993 if (submodule_data_free) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200994 submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200995 }
996 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200997 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100998 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100999 goto search_file;
Radek Krejcid33273d2018-10-25 14:55:52 +02001000 }
Radek Krejci2d31ea72018-10-25 15:46:42 +02001001 } else {
Radek Krejcibbe09a92018-11-08 09:36:54 +01001002search_file:
Michal Vaskob36053d2020-03-26 15:49:30 +01001003 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001004 /* submodule was not received from the callback or there is no callback set */
Michal Vasko7c8439f2020-08-05 13:25:19 +02001005 lys_module_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, 0, pctx, pctx->main_mod->name, 1,
Michal Vasko22df3f02020-08-24 13:29:22 +02001006 (void **)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001007 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001008 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01001009 goto search_clb;
1010 }
1011 }
1012 if (submod) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001013 if (!inc->rev[0] && (submod->latest_revision == 1)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01001014 /* update the latest_revision flag - here we have selected the latest available schema,
1015 * consider that even the callback provides correct latest revision */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001016 submod->latest_revision = 2;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001017 }
1018
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001019 inc->submodule = submod;
Radek Krejcid33273d2018-10-25 14:55:52 +02001020 }
1021 if (!inc->submodule) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001022 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.",
Michal Vasko7c8439f2020-08-05 13:25:19 +02001023 inc->name, pctx->main_mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +02001024 return LY_EVALID;
1025 }
1026
1027 return LY_SUCCESS;
1028}
1029
Radek Krejcice8c1592018-10-29 15:35:51 +01001030struct lys_module *
Radek Krejcia3045382018-11-22 14:30:31 +01001031lys_module_find_prefix(const struct lys_module *mod, const char *prefix, size_t len)
Radek Krejcice8c1592018-10-29 15:35:51 +01001032{
Michal Vasko7c8439f2020-08-05 13:25:19 +02001033 struct lys_module *m = NULL;
1034 LY_ARRAY_COUNT_TYPE u;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001035
Radek Krejci873ab432020-08-13 13:47:27 +02001036 if (!len || !ly_strncmp(mod->prefix, prefix, len)) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001037 /* it is the prefix of the module itself */
1038 m = (struct lys_module *)mod;
Radek Krejci73dead22019-07-11 16:46:16 +02001039 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001040
1041 /* search in imports */
1042 if (!m && mod->parsed) {
1043 LY_ARRAY_FOR(mod->parsed->imports, u) {
1044 if (!ly_strncmp(mod->parsed->imports[u].prefix, prefix, len)) {
1045 m = mod->parsed->imports[u].module;
1046 break;
1047 }
1048 }
Radek Krejcibbe09a92018-11-08 09:36:54 +01001049 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001050
1051 return m;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001052}
1053
Radek Krejcia3045382018-11-22 14:30:31 +01001054const char *
Radek Krejci693262f2019-04-29 15:23:20 +02001055lys_prefix_find_module(const struct lys_module *mod, const struct lys_module *import)
1056{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001057 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001058
1059 if (import == mod) {
1060 return mod->prefix;
1061 }
1062
1063 if (mod->parsed) {
1064 LY_ARRAY_FOR(mod->parsed->imports, u) {
1065 if (mod->parsed->imports[u].module == import) {
1066 return mod->parsed->imports[u].prefix;
1067 }
1068 }
1069 } else {
1070 /* we don't have original information about the import's prefix,
1071 * so the prefix of the import module itself is returned instead */
1072 return import->prefix;
1073 }
1074
1075 return NULL;
1076}
1077
Radek Krejci0935f412019-08-20 16:15:18 +02001078API const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001079lys_nodetype2str(uint16_t nodetype)
1080{
Michal Vaskod989ba02020-08-24 10:59:24 +02001081 switch (nodetype) {
Radek Krejcia3045382018-11-22 14:30:31 +01001082 case LYS_CONTAINER:
1083 return "container";
1084 case LYS_CHOICE:
1085 return "choice";
1086 case LYS_LEAF:
1087 return "leaf";
1088 case LYS_LEAFLIST:
1089 return "leaf-list";
1090 case LYS_LIST:
1091 return "list";
1092 case LYS_ANYXML:
1093 return "anyxml";
1094 case LYS_ANYDATA:
1095 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001096 case LYS_CASE:
1097 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001098 case LYS_RPC:
1099 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001100 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001101 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001102 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001103 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001104 case LYS_USES:
1105 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001106 default:
1107 return "unknown";
1108 }
1109}
1110
Radek Krejci693262f2019-04-29 15:23:20 +02001111const char *
1112lys_datatype2str(LY_DATA_TYPE basetype)
1113{
Michal Vaskod989ba02020-08-24 10:59:24 +02001114 switch (basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +02001115 case LY_TYPE_BINARY:
1116 return "binary";
1117 case LY_TYPE_UINT8:
1118 return "uint8";
1119 case LY_TYPE_UINT16:
1120 return "uint16";
1121 case LY_TYPE_UINT32:
1122 return "uint32";
1123 case LY_TYPE_UINT64:
1124 return "uint64";
1125 case LY_TYPE_STRING:
1126 return "string";
1127 case LY_TYPE_BITS:
1128 return "bits";
1129 case LY_TYPE_BOOL:
1130 return "boolean";
1131 case LY_TYPE_DEC64:
1132 return "decimal64";
1133 case LY_TYPE_EMPTY:
1134 return "empty";
1135 case LY_TYPE_ENUM:
1136 return "enumeration";
1137 case LY_TYPE_IDENT:
1138 return "identityref";
1139 case LY_TYPE_INST:
1140 return "instance-identifier";
1141 case LY_TYPE_LEAFREF:
1142 return "leafref";
1143 case LY_TYPE_UNION:
1144 return "union";
1145 case LY_TYPE_INT8:
1146 return "int8";
1147 case LY_TYPE_INT16:
1148 return "int16";
1149 case LY_TYPE_INT32:
1150 return "int32";
1151 case LY_TYPE_INT64:
1152 return "int64";
1153 default:
1154 return "unknown";
1155 }
1156}
1157
Radek Krejci056d0a82018-12-06 16:57:25 +01001158API const struct lysp_tpdf *
1159lysp_node_typedefs(const struct lysp_node *node)
1160{
Radek Krejci0fb28562018-12-13 15:17:37 +01001161 switch (node->nodetype) {
1162 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001163 return ((struct lysp_node_container *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001164 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001165 return ((struct lysp_node_list *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001166 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001167 return ((struct lysp_grp *)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001168 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001169 case LYS_ACTION:
Michal Vasko22df3f02020-08-24 13:29:22 +02001170 return ((struct lysp_action *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001171 case LYS_INOUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001172 return ((struct lysp_action_inout *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001173 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001174 return ((struct lysp_notif *)node)->typedefs;
Radek Krejci0fb28562018-12-13 15:17:37 +01001175 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001176 return NULL;
1177 }
1178}
1179
Radek Krejci53ea6152018-12-13 15:21:15 +01001180API const struct lysp_grp *
1181lysp_node_groupings(const struct lysp_node *node)
1182{
1183 switch (node->nodetype) {
1184 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001185 return ((struct lysp_node_container *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001186 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001187 return ((struct lysp_node_list *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001188 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001189 return ((struct lysp_grp *)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001190 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001191 case LYS_ACTION:
Michal Vasko22df3f02020-08-24 13:29:22 +02001192 return ((struct lysp_action *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001193 case LYS_INOUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001194 return ((struct lysp_action_inout *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001195 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001196 return ((struct lysp_notif *)node)->groupings;
Radek Krejci53ea6152018-12-13 15:21:15 +01001197 default:
1198 return NULL;
1199 }
1200}
1201
Radek Krejcibbe09a92018-11-08 09:36:54 +01001202struct lysp_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001203lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001204{
1205 assert(node);
1206 switch (node->nodetype) {
1207 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001208 return &((struct lysp_node_container *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001209 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001210 return &((struct lysp_node_list *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001211 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001212 return &((struct lysp_grp *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001213 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001214 return &((struct lysp_augment *)node)->actions;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001215 default:
1216 return NULL;
1217 }
1218}
1219
Radek Krejci056d0a82018-12-06 16:57:25 +01001220API const struct lysp_action *
1221lysp_node_actions(const struct lysp_node *node)
1222{
1223 struct lysp_action **actions;
Michal Vasko22df3f02020-08-24 13:29:22 +02001224 actions = lysp_node_actions_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001225 if (actions) {
1226 return *actions;
1227 } else {
1228 return NULL;
1229 }
1230}
1231
Radek Krejcibbe09a92018-11-08 09:36:54 +01001232struct lysp_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001233lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001234{
1235 assert(node);
1236 switch (node->nodetype) {
1237 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001238 return &((struct lysp_node_container *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001239 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001240 return &((struct lysp_node_list *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001241 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001242 return &((struct lysp_grp *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001243 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001244 return &((struct lysp_augment *)node)->notifs;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001245 default:
1246 return NULL;
1247 }
1248}
1249
Radek Krejci056d0a82018-12-06 16:57:25 +01001250API const struct lysp_notif *
1251lysp_node_notifs(const struct lysp_node *node)
1252{
1253 struct lysp_notif **notifs;
Michal Vasko22df3f02020-08-24 13:29:22 +02001254 notifs = lysp_node_notifs_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001255 if (notifs) {
1256 return *notifs;
1257 } else {
1258 return NULL;
1259 }
1260}
1261
Radek Krejcibbe09a92018-11-08 09:36:54 +01001262struct lysp_node **
Radek Krejci056d0a82018-12-06 16:57:25 +01001263lysp_node_children_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001264{
1265 assert(node);
1266 switch (node->nodetype) {
1267 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001268 return &((struct lysp_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001269 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001270 return &((struct lysp_node_choice *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001271 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001272 return &((struct lysp_node_list *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001273 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001274 return &((struct lysp_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001275 case LYS_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02001276 return &((struct lysp_grp *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001277 case LYS_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001278 return &((struct lysp_augment *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001279 case LYS_INOUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02001280 return &((struct lysp_action_inout *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001281 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001282 return &((struct lysp_notif *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001283 default:
1284 return NULL;
1285 }
1286}
1287
Radek Krejci056d0a82018-12-06 16:57:25 +01001288API const struct lysp_node *
1289lysp_node_children(const struct lysp_node *node)
1290{
1291 struct lysp_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001292
1293 if (!node) {
1294 return NULL;
1295 }
1296
Michal Vasko22df3f02020-08-24 13:29:22 +02001297 children = lysp_node_children_p((struct lysp_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001298 if (children) {
1299 return *children;
1300 } else {
1301 return NULL;
1302 }
1303}
1304
1305struct lysc_action **
1306lysc_node_actions_p(struct lysc_node *node)
1307{
1308 assert(node);
1309 switch (node->nodetype) {
1310 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001311 return &((struct lysc_node_container *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001312 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001313 return &((struct lysc_node_list *)node)->actions;
Radek Krejci056d0a82018-12-06 16:57:25 +01001314 default:
1315 return NULL;
1316 }
1317}
1318
1319API const struct lysc_action *
1320lysc_node_actions(const struct lysc_node *node)
1321{
1322 struct lysc_action **actions;
Michal Vasko22df3f02020-08-24 13:29:22 +02001323 actions = lysc_node_actions_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001324 if (actions) {
1325 return *actions;
1326 } else {
1327 return NULL;
1328 }
1329}
1330
1331struct lysc_notif **
1332lysc_node_notifs_p(struct lysc_node *node)
1333{
1334 assert(node);
1335 switch (node->nodetype) {
1336 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001337 return &((struct lysc_node_container *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001338 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001339 return &((struct lysc_node_list *)node)->notifs;
Radek Krejci056d0a82018-12-06 16:57:25 +01001340 default:
1341 return NULL;
1342 }
1343}
1344
1345API const struct lysc_notif *
1346lysc_node_notifs(const struct lysc_node *node)
1347{
1348 struct lysc_notif **notifs;
Michal Vasko22df3f02020-08-24 13:29:22 +02001349 notifs = lysc_node_notifs_p((struct lysc_node *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +01001350 if (notifs) {
1351 return *notifs;
1352 } else {
1353 return NULL;
1354 }
1355}
1356
Radek Krejcibbe09a92018-11-08 09:36:54 +01001357struct lysc_node **
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001358lysc_node_children_p(const struct lysc_node *node, uint16_t flags)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001359{
1360 assert(node);
1361 switch (node->nodetype) {
1362 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02001363 return &((struct lysc_node_container *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001364 case LYS_CHOICE:
Michal Vasko20424b42020-08-31 12:29:38 +02001365 return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
Radek Krejci01342af2019-01-03 15:18:08 +01001366 case LYS_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02001367 return &((struct lysc_node_case *)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001368 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02001369 return &((struct lysc_node_list *)node)->child;
Michal Vasko1bf09392020-03-27 12:38:10 +01001370 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001371 case LYS_ACTION:
1372 if (flags & LYS_CONFIG_R) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001373 return &((struct lysc_action *)node)->output.data;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001374 } else {
1375 /* LYS_CONFIG_W, but also the default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02001376 return &((struct lysc_action *)node)->input.data;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001377 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02001378 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02001379 return &((struct lysc_notif *)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001380 default:
1381 return NULL;
1382 }
1383}
1384
Radek Krejci056d0a82018-12-06 16:57:25 +01001385API const struct lysc_node *
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001386lysc_node_children(const struct lysc_node *node, uint16_t flags)
Radek Krejcia3045382018-11-22 14:30:31 +01001387{
Radek Krejci056d0a82018-12-06 16:57:25 +01001388 struct lysc_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001389
1390 if (!node) {
1391 return NULL;
1392 }
1393
Michal Vasko22df3f02020-08-24 13:29:22 +02001394 children = lysc_node_children_p((struct lysc_node *)node, flags);
Radek Krejci056d0a82018-12-06 16:57:25 +01001395 if (children) {
1396 return *children;
1397 } else {
Radek Krejcia3045382018-11-22 14:30:31 +01001398 return NULL;
1399 }
1400}
1401
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001402struct lys_module *
1403lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1404{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001405 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001406 if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) {
1407 return ((struct lys_module *)ctx->list.objs[u]);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001408 }
1409 }
1410 return NULL;
1411}
1412
Radek Krejcid6b76452019-09-03 17:03:03 +02001413enum ly_stmt
Michal Vasko63f3d842020-07-08 10:10:14 +02001414lysp_match_kw(struct lys_yang_parser_ctx *ctx, struct ly_in *in)
David Sedlákc10e7902018-12-17 02:17:59 +01001415{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001416/**
Michal Vasko63f3d842020-07-08 10:10:14 +02001417 * @brief Move the INPUT by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001418 * @param[in] CTX yang parser context to update its indent value.
Michal Vasko63f3d842020-07-08 10:10:14 +02001419 * @param[in,out] IN input to move
David Sedlák1bccdfa2019-06-17 15:55:27 +02001420 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001421 *
1422 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001423 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001424#define MOVE_IN(CTX, IN, COUNT) ly_in_skip(IN, COUNT);if(CTX){(CTX)->indent+=COUNT;}
1425#define IF_KW(STR, LEN, STMT) if (!strncmp(in->current, STR, LEN)) {MOVE_IN(ctx, in, LEN);*kw=STMT;}
1426#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 +02001427#define IF_KW_PREFIX_END }
David Sedlák572e7ab2019-06-04 16:01:58 +02001428
Michal Vasko63f3d842020-07-08 10:10:14 +02001429 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001430 enum ly_stmt result = LY_STMT_NONE;
1431 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001432 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001433 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001434 case 'a':
Michal Vasko63f3d842020-07-08 10:10:14 +02001435 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001436 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1437 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1438 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001439 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001440 IF_KW("data", 4, LY_STMT_ANYDATA)
1441 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001442 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001443 break;
1444 case 'b':
Michal Vasko63f3d842020-07-08 10:10:14 +02001445 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001446 IF_KW("ase", 3, LY_STMT_BASE)
1447 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1448 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001449 break;
1450 case 'c':
Michal Vasko63f3d842020-07-08 10:10:14 +02001451 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001452 IF_KW("ase", 3, LY_STMT_CASE)
1453 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001454 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001455 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001456 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001457 IF_KW("ct", 2, LY_STMT_CONTACT)
1458 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001459 IF_KW_PREFIX_END
1460 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001461 break;
1462 case 'd':
Michal Vasko63f3d842020-07-08 10:10:14 +02001463 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001464 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001465 IF_KW("fault", 5, LY_STMT_DEFAULT)
1466 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001467 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001468 IF_KW("e", 1, LY_STMT_DEVIATE)
1469 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001470 IF_KW_PREFIX_END
1471 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001472 break;
1473 case 'e':
Michal Vasko63f3d842020-07-08 10:10:14 +02001474 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001475 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001476 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001477 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1478 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001479 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001480 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001481 break;
1482 case 'f':
Michal Vasko63f3d842020-07-08 10:10:14 +02001483 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001484 IF_KW("eature", 6, LY_STMT_FEATURE)
1485 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001486 break;
1487 case 'g':
Michal Vasko63f3d842020-07-08 10:10:14 +02001488 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001489 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001490 break;
1491 case 'i':
Michal Vasko63f3d842020-07-08 10:10:14 +02001492 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001493 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1494 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1495 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001496 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001497 IF_KW("clude", 5, LY_STMT_INCLUDE)
1498 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001499 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001500 break;
1501 case 'k':
Michal Vasko63f3d842020-07-08 10:10:14 +02001502 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001503 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001504 break;
1505 case 'l':
Michal Vasko63f3d842020-07-08 10:10:14 +02001506 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001507 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001508 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1509 else IF_KW("af", 2, LY_STMT_LEAF)
1510 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001511 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001512 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001513 break;
1514 case 'm':
Michal Vasko63f3d842020-07-08 10:10:14 +02001515 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001516 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001517 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1518 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001519 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001520 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1521 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001522 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001523 IF_KW("ule", 3, LY_STMT_MODULE)
1524 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001525 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001526 break;
1527 case 'n':
Michal Vasko63f3d842020-07-08 10:10:14 +02001528 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001529 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1530 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001531 break;
1532 case 'o':
Michal Vasko63f3d842020-07-08 10:10:14 +02001533 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001534 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001535 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1536 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001537 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001538 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001539 break;
1540 case 'p':
Michal Vasko63f3d842020-07-08 10:10:14 +02001541 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001542 IF_KW("ath", 3, LY_STMT_PATH)
1543 else IF_KW("attern", 6, LY_STMT_PATTERN)
1544 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001545 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001546 IF_KW("fix", 3, LY_STMT_PREFIX)
1547 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001548 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001549 break;
1550 case 'r':
Michal Vasko63f3d842020-07-08 10:10:14 +02001551 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001552 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001553 else IF_KW_PREFIX("e", 1)
1554 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001555 IF_KW("erence", 6, LY_STMT_REFERENCE)
1556 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001557 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001558 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1559 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1560 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001561 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001562 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001563 break;
1564 case 's':
Michal Vasko63f3d842020-07-08 10:10:14 +02001565 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001566 IF_KW("tatus", 5, LY_STMT_STATUS)
1567 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001568 break;
1569 case 't':
Michal Vasko63f3d842020-07-08 10:10:14 +02001570 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001571 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1572 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001573 break;
1574 case 'u':
Michal Vasko63f3d842020-07-08 10:10:14 +02001575 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001576 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001577 IF_KW("que", 3, LY_STMT_UNIQUE)
1578 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001579 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001580 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001581 break;
1582 case 'v':
Michal Vasko63f3d842020-07-08 10:10:14 +02001583 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001584 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001585 break;
1586 case 'w':
Michal Vasko63f3d842020-07-08 10:10:14 +02001587 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001588 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001589 break;
1590 case 'y':
Michal Vasko63f3d842020-07-08 10:10:14 +02001591 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001592 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
1593 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001594 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001595 default:
David Sedlák1bccdfa2019-06-17 15:55:27 +02001596 /* if context is not NULL we are matching keyword from YANG data*/
1597 if (ctx) {
Michal Vasko63f3d842020-07-08 10:10:14 +02001598 if (in->current[0] == ';') {
1599 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001600 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02001601 } else if (in->current[0] == '{') {
1602 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001603 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001604 } else if (in->current[0] == '}') {
1605 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001606 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001607 }
1608 }
David Sedlák23a59a62018-10-26 13:08:02 +02001609 break;
1610 }
1611
Michal Vasko63f3d842020-07-08 10:10:14 +02001612 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02001613 /* the keyword is not terminated */
1614 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001615 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02001616 }
1617
David Sedlák1bccdfa2019-06-17 15:55:27 +02001618#undef IF_KW
1619#undef IF_KW_PREFIX
1620#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01001621#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02001622 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01001623
David Sedlák1bccdfa2019-06-17 15:55:27 +02001624 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02001625}
David Sedlákecf5eb82019-06-03 14:12:44 +02001626
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001627LY_ARRAY_COUNT_TYPE
1628lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, LYEXT_SUBSTMT substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001629{
1630 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1631
Michal Vaskod989ba02020-08-24 10:59:24 +02001632 for ( ; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001633 if (ext[index].insubstmt == substmt) {
1634 return index;
1635 }
1636 }
1637
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001638 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001639}
1640
Michal Vasko62ed12d2020-05-21 10:08:25 +02001641const struct lysc_node *
1642lysc_data_parent(const struct lysc_node *schema)
1643{
1644 const struct lysc_node *parent;
1645
Radek Krejci1e008d22020-08-17 11:37:37 +02001646 for (parent = schema->parent; parent && (parent->nodetype & (LYS_CHOICE | LYS_CASE)); parent = parent->parent) {}
Michal Vasko62ed12d2020-05-21 10:08:25 +02001647
1648 return parent;
1649}
Michal Vasko00cbf532020-06-15 13:58:47 +02001650
Radek Krejci857189e2020-09-01 13:26:36 +02001651ly_bool
Michal Vasko00cbf532020-06-15 13:58:47 +02001652lysc_is_output(const struct lysc_node *schema)
1653{
1654 const struct lysc_node *parent;
1655
1656 assert(schema);
1657
Radek Krejci1e008d22020-08-17 11:37:37 +02001658 for (parent = schema->parent; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION)); parent = parent->parent) {}
Michal Vasko00cbf532020-06-15 13:58:47 +02001659 if (parent && (schema->flags & LYS_CONFIG_R)) {
1660 return 1;
1661 }
1662 return 0;
1663}
Michal Vaskod975f862020-06-23 13:29:28 +02001664
Radek Krejci857189e2020-09-01 13:26:36 +02001665API ly_bool
Michal Vaskod975f862020-06-23 13:29:28 +02001666lysc_is_userordered(const struct lysc_node *schema)
1667{
1668 if (!schema || !(schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) || !(schema->flags & LYS_ORDBY_USER)) {
1669 return 0;
1670 }
1671
1672 return 1;
1673}
Michal Vasko2fd91b92020-07-17 16:39:02 +02001674
1675API LY_ERR
1676lysc_set_private(const struct lysc_node *node, void *priv, void **prev)
1677{
1678 struct lysc_action *act;
1679 struct lysc_notif *notif;
1680
1681 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
1682
1683 switch (node->nodetype) {
1684 case LYS_CONTAINER:
1685 case LYS_CHOICE:
1686 case LYS_CASE:
1687 case LYS_LEAF:
1688 case LYS_LEAFLIST:
1689 case LYS_LIST:
1690 case LYS_ANYXML:
1691 case LYS_ANYDATA:
1692 if (prev) {
1693 *prev = node->priv;
1694 }
1695 ((struct lysc_node *)node)->priv = priv;
1696 break;
1697 case LYS_RPC:
1698 case LYS_ACTION:
1699 act = (struct lysc_action *)node;
1700 if (prev) {
1701 *prev = act->priv;
1702 }
1703 act->priv = priv;
1704 break;
1705 case LYS_NOTIF:
1706 notif = (struct lysc_notif *)node;
1707 if (prev) {
1708 *prev = notif->priv;
1709 }
1710 notif->priv = priv;
1711 break;
1712 default:
1713 return LY_EINVAL;
1714 }
1715
1716 return LY_SUCCESS;
1717}