blob: bcb88213e05cc5cb4f6f4cc3fc342553296318c4 [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 Krejci0f969882020-08-21 16:56:47 +020039 const struct lys_module *context_module, int nodetype, int implement,
40 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 Krejci6eeb58f2019-02-22 16:29:37 +010047 int getnext_extra_flag = 0;
Radek Krejci05b774b2019-02-25 13:26:18 +010048 int 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 */
Radek Krejcia46012b2020-08-12 15:41:04 +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 Krejcie7b95092019-05-15 11:03:07 +0200196lysp_check_date(struct lys_parser_ctx *ctx, const char *date, int date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +0200197{
198 int i;
199 struct tm tm, tm_;
200 char *r;
201
Michal Vaskob36053d2020-03-26 15:49:30 +0100202 LY_CHECK_ARG_RET(ctx ? PARSER_CTX(ctx) : NULL, date, LY_EINVAL);
203 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 +0200204
205 /* check format */
206 for (i = 0; i < date_len; i++) {
207 if (i == 4 || i == 7) {
208 if (date[i] != '-') {
209 goto error;
210 }
211 } else if (!isdigit(date[i])) {
212 goto error;
213 }
214 }
215
216 /* check content, e.g. 2018-02-31 */
217 memset(&tm, 0, sizeof tm);
218 r = strptime(date, "%Y-%m-%d", &tm);
219 if (!r || r != &date[LY_REV_SIZE - 1]) {
220 goto error;
221 }
222 memcpy(&tm_, &tm, sizeof tm);
223 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
224 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
225 /* checking days is enough, since other errors
226 * have been checked by strptime() */
227 goto error;
228 }
229
230 return LY_SUCCESS;
231
232error:
Radek Krejcid33273d2018-10-25 14:55:52 +0200233 if (stmt) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100234 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100235 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100236 } else {
237 LOGVAL(NULL, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, date_len, date, stmt);
238 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200239 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200240 return LY_EINVAL;
241}
242
243void
244lysp_sort_revisions(struct lysp_revision *revs)
245{
246 uint8_t i, r;
247 struct lysp_revision rev;
248
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200249 for (i = 1, r = 0; revs && i < LY_ARRAY_COUNT(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200250 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200251 r = i;
252 }
253 }
254
255 if (r) {
256 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200257 memcpy(&rev, &revs[0], sizeof rev);
258 memcpy(&revs[0], &revs[r], sizeof rev);
259 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200260 }
261}
Radek Krejci151a5b72018-10-19 14:21:44 +0200262
Radek Krejcibbe09a92018-11-08 09:36:54 +0100263static const struct lysp_tpdf *
264lysp_type_match(const char *name, struct lysp_node *node)
265{
Radek Krejci0fb28562018-12-13 15:17:37 +0100266 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200267 LY_ARRAY_COUNT_TYPE u;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100268
Radek Krejci0fb28562018-12-13 15:17:37 +0100269 typedefs = lysp_node_typedefs(node);
270 LY_ARRAY_FOR(typedefs, u) {
271 if (!strcmp(name, typedefs[u].name)) {
272 /* match */
273 return &typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100274 }
275 }
276
277 return NULL;
278}
279
Radek Krejci4f28eda2018-11-12 11:46:16 +0100280static LY_DATA_TYPE
281lysp_type_str2builtin(const char *name, size_t len)
282{
283 if (len >= 4) { /* otherwise it does not match any built-in type */
284 if (name[0] == 'b') {
285 if (name[1] == 'i') {
286 if (len == 6 && !strncmp(&name[2], "nary", 4)) {
287 return LY_TYPE_BINARY;
288 } else if (len == 4 && !strncmp(&name[2], "ts", 2)) {
289 return LY_TYPE_BITS;
290 }
291 } else if (len == 7 && !strncmp(&name[1], "oolean", 6)) {
292 return LY_TYPE_BOOL;
293 }
294 } else if (name[0] == 'd') {
295 if (len == 9 && !strncmp(&name[1], "ecimal64", 8)) {
296 return LY_TYPE_DEC64;
297 }
298 } else if (name[0] == 'e') {
299 if (len == 5 && !strncmp(&name[1], "mpty", 4)) {
300 return LY_TYPE_EMPTY;
301 } else if (len == 11 && !strncmp(&name[1], "numeration", 10)) {
302 return LY_TYPE_ENUM;
303 }
304 } else if (name[0] == 'i') {
305 if (name[1] == 'n') {
306 if (len == 4 && !strncmp(&name[2], "t8", 2)) {
307 return LY_TYPE_INT8;
308 } else if (len == 5) {
309 if (!strncmp(&name[2], "t16", 3)) {
310 return LY_TYPE_INT16;
311 } else if (!strncmp(&name[2], "t32", 3)) {
312 return LY_TYPE_INT32;
313 } else if (!strncmp(&name[2], "t64", 3)) {
314 return LY_TYPE_INT64;
315 }
316 } else if (len == 19 && !strncmp(&name[2], "stance-identifier", 17)) {
317 return LY_TYPE_INST;
318 }
319 } else if (len == 11 && !strncmp(&name[1], "dentityref", 10)) {
320 return LY_TYPE_IDENT;
321 }
322 } else if (name[0] == 'l') {
323 if (len == 7 && !strncmp(&name[1], "eafref", 6)) {
324 return LY_TYPE_LEAFREF;
325 }
326 } else if (name[0] == 's') {
327 if (len == 6 && !strncmp(&name[1], "tring", 5)) {
328 return LY_TYPE_STRING;
329 }
330 } else if (name[0] == 'u') {
331 if (name[1] == 'n') {
332 if (len == 5 && !strncmp(&name[2], "ion", 3)) {
333 return LY_TYPE_UNION;
334 }
335 } else if (name[1] == 'i' && name[2] == 'n' && name[3] == 't') {
336 if (len == 5 && name[4] == '8') {
337 return LY_TYPE_UINT8;
338 } else if (len == 6) {
339 if (!strncmp(&name[4], "16", 2)) {
340 return LY_TYPE_UINT16;
341 } else if (!strncmp(&name[4], "32", 2)) {
342 return LY_TYPE_UINT32;
343 } else if (!strncmp(&name[4], "64", 2)) {
344 return LY_TYPE_UINT64;
345 }
346 }
347 }
348 }
349 }
350
351 return LY_TYPE_UNKNOWN;
352}
353
Radek Krejcibbe09a92018-11-08 09:36:54 +0100354LY_ERR
355lysp_type_find(const char *id, struct lysp_node *start_node, struct lysp_module *start_module,
Radek Krejci0f969882020-08-21 16:56:47 +0200356 LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node, struct lysp_module **module)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100357{
358 const char *str, *name;
359 struct lysp_tpdf *typedefs;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200360 struct lys_module *mod;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200361 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100362
363 assert(id);
364 assert(start_module);
365 assert(tpdf);
366 assert(node);
367 assert(module);
368
Radek Krejci4f28eda2018-11-12 11:46:16 +0100369 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100370 str = strchr(id, ':');
371 if (str) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200372 mod = lys_module_find_prefix(start_module->mod, id, str - id);
373 *module = mod ? mod->parsed : NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100374 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100375 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100376 } else {
377 *module = start_module;
378 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100379
380 /* check for built-in types */
381 *type = lysp_type_str2builtin(name, strlen(name));
382 if (*type) {
383 *tpdf = NULL;
384 return LY_SUCCESS;
385 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100386 }
387 LY_CHECK_RET(!(*module), LY_ENOTFOUND);
388
389 if (start_node && *module == start_module) {
390 /* search typedefs in parent's nodes */
391 *node = start_node;
392 while (*node) {
393 *tpdf = lysp_type_match(name, *node);
394 if (*tpdf) {
395 /* match */
396 return LY_SUCCESS;
397 }
398 *node = (*node)->parent;
399 }
400 }
401
402 /* search in top-level typedefs */
403 if ((*module)->typedefs) {
404 LY_ARRAY_FOR((*module)->typedefs, u) {
405 if (!strcmp(name, (*module)->typedefs[u].name)) {
406 /* match */
407 *tpdf = &(*module)->typedefs[u];
408 return LY_SUCCESS;
409 }
410 }
411 }
412
413 /* search in submodules' typedefs */
414 LY_ARRAY_FOR((*module)->includes, u) {
415 typedefs = (*module)->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100416 LY_ARRAY_FOR(typedefs, v) {
417 if (!strcmp(name, typedefs[v].name)) {
418 /* match */
419 *tpdf = &typedefs[v];
420 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100421 }
422 }
423 }
424
425 return LY_ENOTFOUND;
426}
427
David Sedlák6544c182019-07-12 13:17:33 +0200428LY_ERR
David Sedlák07869a52019-07-12 14:28:19 +0200429lysp_check_enum_name(struct lys_parser_ctx *ctx, const char *name, size_t name_len)
David Sedlák6544c182019-07-12 13:17:33 +0200430{
431 if (!name_len) {
432 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
433 return LY_EVALID;
434 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
435 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
436 name_len, name);
437 return LY_EVALID;
438 } else {
439 for (size_t u = 0; u < name_len; ++u) {
440 if (iscntrl(name[u])) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100441 LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
David Sedlák6544c182019-07-12 13:17:33 +0200442 name_len, name, u + 1);
443 break;
444 }
445 }
446 }
447
448 return LY_SUCCESS;
449}
450
Michal Vaskob36053d2020-03-26 15:49:30 +0100451/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100452 * @brief Check name of a new type to avoid name collisions.
453 *
454 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
455 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
456 * @param[in] tpdf Typedef definition to check.
457 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
458 * typedefs are checked, caller is supposed to free the table.
459 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
460 * typedefs are checked, caller is supposed to free the table.
461 * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise.
462 */
463static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200464lysp_check_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
Radek Krejci0f969882020-08-21 16:56:47 +0200465 struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100466{
467 struct lysp_node *parent;
468 uint32_t hash;
469 size_t name_len;
470 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200471 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100472 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100473
474 assert(ctx);
475 assert(tpdf);
476
477 name = tpdf->name;
478 name_len = strlen(name);
479
Radek Krejci4f28eda2018-11-12 11:46:16 +0100480 if (lysp_type_str2builtin(name, name_len)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100481 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 +0100482 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100483 }
484
485 /* check locally scoped typedefs (avoid name shadowing) */
486 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100487 typedefs = lysp_node_typedefs(node);
488 LY_ARRAY_FOR(typedefs, u) {
489 if (&typedefs[u] == tpdf) {
490 break;
491 }
492 if (!strcmp(name, typedefs[u].name)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100493 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with sibling type.", name);
Radek Krejci0fb28562018-12-13 15:17:37 +0100494 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100495 }
496 }
497 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200498 for (parent = node->parent; parent; parent = parent->parent) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100499 if (lysp_type_match(name, parent)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100500 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 +0100501 return LY_EEXIST;
502 }
503 }
504 }
505
506 /* check collision with the top-level typedefs */
507 hash = dict_hash(name, name_len);
508 if (node) {
509 lyht_insert(tpdfs_scoped, &name, hash, NULL);
510 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100511 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 +0100512 return LY_EEXIST;
513 }
514 } else {
515 if (lyht_insert(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100516 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 +0100517 return LY_EEXIST;
518 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100519 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
520 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
521 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100522 }
523
524 return LY_SUCCESS;
525}
526
527static int
528lysp_id_cmp(void *val1, void *val2, int UNUSED(mod), void *UNUSED(cb_data))
529{
530 return !strcmp(val1, val2);
531}
532
533LY_ERR
David Sedlákd2ebe572019-07-22 12:53:14 +0200534lysp_parse_finalize_reallocated(struct lys_parser_ctx *ctx, struct lysp_grp *groupings, struct lysp_augment *augments,
Radek Krejci0f969882020-08-21 16:56:47 +0200535 struct lysp_action *actions, struct lysp_notif *notifs)
David Sedlákd2ebe572019-07-22 12:53:14 +0200536{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200537 LY_ARRAY_COUNT_TYPE u, v;
David Sedlákd2ebe572019-07-22 12:53:14 +0200538 struct lysp_node *child;
539
540 /* finalize parent pointers to the reallocated items */
541
542 /* gropings */
543 LY_ARRAY_FOR(groupings, u) {
544 LY_LIST_FOR(groupings[u].data, child) {
545 child->parent = (struct lysp_node*)&groupings[u];
546 }
547 LY_ARRAY_FOR(groupings[u].actions, v) {
548 groupings[u].actions[v].parent = (struct lysp_node*)&groupings[u];
549 }
550 LY_ARRAY_FOR(groupings[u].notifs, v) {
551 groupings[u].notifs[v].parent = (struct lysp_node*)&groupings[u];
552 }
553 LY_ARRAY_FOR(groupings[u].groupings, v) {
554 groupings[u].groupings[v].parent = (struct lysp_node*)&groupings[u];
555 }
556 if (groupings[u].typedefs) {
557 ly_set_add(&ctx->tpdfs_nodes, &groupings[u], 0);
558 }
559 }
560
561 /* augments */
562 LY_ARRAY_FOR(augments, u) {
563 LY_LIST_FOR(augments[u].child, child) {
564 child->parent = (struct lysp_node*)&augments[u];
565 }
566 LY_ARRAY_FOR(augments[u].actions, v) {
567 augments[u].actions[v].parent = (struct lysp_node*)&augments[u];
568 }
569 LY_ARRAY_FOR(augments[u].notifs, v) {
570 augments[u].notifs[v].parent = (struct lysp_node*)&augments[u];
571 }
572 }
573
574 /* actions */
575 LY_ARRAY_FOR(actions, u) {
576 if (actions[u].input.parent) {
577 actions[u].input.parent = (struct lysp_node*)&actions[u];
578 LY_LIST_FOR(actions[u].input.data, child) {
579 child->parent = (struct lysp_node*)&actions[u].input;
580 }
581 LY_ARRAY_FOR(actions[u].input.groupings, v) {
582 actions[u].input.groupings[v].parent = (struct lysp_node*)&actions[u].input;
583 }
584 if (actions[u].input.typedefs) {
585 ly_set_add(&ctx->tpdfs_nodes, &actions[u].input, 0);
586 }
587 }
588 if (actions[u].output.parent) {
589 actions[u].output.parent = (struct lysp_node*)&actions[u];
590 LY_LIST_FOR(actions[u].output.data, child) {
591 child->parent = (struct lysp_node*)&actions[u].output;
592 }
593 LY_ARRAY_FOR(actions[u].output.groupings, v) {
594 actions[u].output.groupings[v].parent = (struct lysp_node*)&actions[u].output;
595 }
596 if (actions[u].output.typedefs) {
597 ly_set_add(&ctx->tpdfs_nodes, &actions[u].output, 0);
598 }
599 }
600 LY_ARRAY_FOR(actions[u].groupings, v) {
601 actions[u].groupings[v].parent = (struct lysp_node*)&actions[u];
602 }
603 if (actions[u].typedefs) {
604 ly_set_add(&ctx->tpdfs_nodes, &actions[u], 0);
605 }
606 }
607
608 /* notifications */
609 LY_ARRAY_FOR(notifs, u) {
610 LY_LIST_FOR(notifs[u].data, child) {
611 child->parent = (struct lysp_node*)&notifs[u];
612 }
613 LY_ARRAY_FOR(notifs[u].groupings, v) {
614 notifs[u].groupings[v].parent = (struct lysp_node*)&notifs[u];
615 }
616 if (notifs[u].typedefs) {
617 ly_set_add(&ctx->tpdfs_nodes, &notifs[u], 0);
618 }
619 }
620
621 return LY_SUCCESS;
622}
623
624LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200625lysp_check_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100626{
627 struct hash_table *ids_global;
628 struct hash_table *ids_scoped;
Radek Krejci0fb28562018-12-13 15:17:37 +0100629 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200630 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200631 uint32_t i;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100632 LY_ERR ret = LY_EVALID;
633
634 /* check name collisions - typedefs and groupings */
635 ids_global = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1);
636 ids_scoped = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200637 LY_ARRAY_FOR(mod->typedefs, v) {
638 if (lysp_check_typedef(ctx, NULL, &mod->typedefs[v], ids_global, ids_scoped)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100639 goto cleanup;
640 }
641 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200642 LY_ARRAY_FOR(mod->includes, v) {
643 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
644 if (lysp_check_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global, ids_scoped)) {
Radek Krejci3b1f9292018-11-08 10:58:35 +0100645 goto cleanup;
646 }
647 }
648 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200649 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
650 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
651 LY_ARRAY_FOR(typedefs, u) {
652 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 +0100653 goto cleanup;
654 }
655 }
656 }
657 ret = LY_SUCCESS;
658cleanup:
659 lyht_free(ids_global);
660 lyht_free(ids_scoped);
661 ly_set_erase(&ctx->tpdfs_nodes, NULL);
662
663 return ret;
664}
665
Radek Krejci9ed7a192018-10-31 16:23:51 +0100666struct lysp_load_module_check_data {
667 const char *name;
668 const char *revision;
669 const char *path;
670 const char* submoduleof;
671};
672
673static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100674lysp_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 +0100675{
676 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100677 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200678 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100679 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100680 struct lysp_revision *revs;
681
682 name = mod ? mod->mod->name : submod->name;
683 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200684 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100685
686 if (info->name) {
687 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100688 if (strcmp(info->name, name)) {
689 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100690 return LY_EINVAL;
691 }
692 }
693 if (info->revision) {
694 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100695 if (!revs || strcmp(info->revision, revs[0].date)) {
696 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Radek Krejcib07b5c92019-04-08 10:56:37 +0200697 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100698 return LY_EINVAL;
699 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200700 } else if (!latest_revision) {
701 /* do not log, we just need to drop the schema and use the latest revision from the context */
702 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100703 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100704 if (submod) {
705 assert(info->submoduleof);
706
Radek Krejci9ed7a192018-10-31 16:23:51 +0100707 /* check that the submodule belongs-to our module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100708 if (strcmp(info->submoduleof, submod->belongsto)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100709 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 +0100710 submod->name, info->submoduleof, submod->belongsto);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100711 return LY_EVALID;
712 }
713 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100714 if (submod->parsing) {
715 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100716 return LY_EVALID;
717 }
718 }
719 if (info->path) {
720 /* check that name and revision match filename */
721 filename = strrchr(info->path, '/');
722 if (!filename) {
723 filename = info->path;
724 } else {
725 filename++;
726 }
727 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100728 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100729 rev = strchr(filename, '@');
730 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100731 if (strncmp(filename, name, len) ||
Radek Krejci9ed7a192018-10-31 16:23:51 +0100732 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100733 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100734 }
735 /* revision */
736 if (rev) {
737 len = dot - ++rev;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100738 if (!revs || len != 10 || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100739 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100740 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100741 }
742 }
743 }
744 return LY_SUCCESS;
745}
746
747LY_ERR
fredgancd485b82019-10-18 15:00:17 +0800748lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, int implement,
Radek Krejci0f969882020-08-21 16:56:47 +0200749 struct lys_parser_ctx *main_ctx, const char *main_name, int required, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100750{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200751 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100752 char *filepath = NULL;
753 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100754 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100755 LY_ERR ret = LY_SUCCESS;
756 struct lysp_load_module_check_data check_data = {0};
757
758 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, revision,
759 &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200760 if (!filepath) {
761 if (required) {
762 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
763 revision ? revision : "");
764 }
765 return LY_ENOTFOUND;
766 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100767
768 LOGVRB("Loading schema from \"%s\" file.", filepath);
769
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200770 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200771 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
772 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100773 check_data.name = name;
774 check_data.revision = revision;
775 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800776 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200777 if (main_ctx) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200778 ret = lys_parse_mem_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data,
Radek Krejci0f969882020-08-21 16:56:47 +0200779 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200780 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200781 ret = lys_parse_mem_module(ctx, in, format, implement, lysp_load_module_check, &check_data,
Radek Krejci0f969882020-08-21 16:56:47 +0200782 (struct lys_module **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200783
784 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200785 LY_CHECK_ERR_GOTO(ret, ly_in_free(in, 1), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100786
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100787 if (main_ctx) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200788 lys_parser_fill_filepath(ctx, in, &((struct lysp_submodule *)mod)->filepath);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100789 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200790 lys_parser_fill_filepath(ctx, in, &((struct lys_module *)mod)->filepath);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100791 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200792 ly_in_free(in, 1);
793
794 if (mod && implement) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200795 lys_compile((struct lys_module **)&mod, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100796 }
797
798 *result = mod;
799
800 /* success */
801cleanup:
802 free(filepath);
803 return ret;
804}
805
Radek Krejcid33273d2018-10-25 14:55:52 +0200806LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +0200807lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, int implement, int require_parsed,
Radek Krejci0f969882020-08-21 16:56:47 +0200808 struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200809{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100810 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200811 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100812 void (*module_data_free)(void *module_data, void *user_data) = NULL;
813 struct lysp_load_module_check_data check_data = {0};
Radek Krejcib3289d62019-09-18 12:21:39 +0200814 struct lys_module *m = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200815 struct ly_in *in;
Radek Krejci086c7132018-10-26 15:29:04 +0200816
Radek Krejci0af46292019-01-11 16:02:31 +0100817 assert(mod);
818
Radek Krejcia53d7c92020-08-21 11:30:56 +0200819 if (ctx->flags & LY_CTX_ALLIMPLEMENTED) {
820 implement = 1;
821 }
822
Radek Krejci0af46292019-01-11 16:02:31 +0100823 if (!*mod) {
824 /* try to get the module from the context */
825 if (revision) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200826 /* get the specific revision */
Radek Krejci0af46292019-01-11 16:02:31 +0100827 *mod = (struct lys_module*)ly_ctx_get_module(ctx, name, revision);
Radek Krejcied5acc52019-04-25 15:57:04 +0200828 } else if (implement) {
829 /* prefer the implemented module instead of the latest one */
830 *mod = (struct lys_module*)ly_ctx_get_module_implemented(ctx, name);
831 if (!*mod) {
832 /* there is no implemented module in the context, try to get the latest revision module */
833 goto latest_in_the_context;
834 }
Radek Krejci0af46292019-01-11 16:02:31 +0100835 } else {
Radek Krejcied5acc52019-04-25 15:57:04 +0200836 /* get the requested module of the latest revision in the context */
837latest_in_the_context:
Radek Krejci0af46292019-01-11 16:02:31 +0100838 *mod = (struct lys_module*)ly_ctx_get_module_latest(ctx, name);
Radek Krejcib3289d62019-09-18 12:21:39 +0200839 if (*mod && (*mod)->latest_revision == 1) {
840 /* let us now search with callback and searchpaths to check if there is newer revision outside the context */
841 m = *mod;
842 *mod = NULL;
843 }
Radek Krejci0af46292019-01-11 16:02:31 +0100844 }
Radek Krejci086c7132018-10-26 15:29:04 +0200845 }
846
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100847 if (!(*mod) || (require_parsed && !(*mod)->parsed)) {
848 (*mod) = NULL;
849
Radek Krejci086c7132018-10-26 15:29:04 +0200850 /* check collision with other implemented revision */
851 if (implement && ly_ctx_get_module_implemented(ctx, name)) {
852 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
853 "Module \"%s\" is already present in other implemented revision.", name);
854 return LY_EDENIED;
855 }
856
Radek Krejci9ed7a192018-10-31 16:23:51 +0100857 /* module not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200858 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
859search_clb:
860 if (ctx->imp_clb) {
861 if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100862 &format, &module_data, &module_data_free) == LY_SUCCESS) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200863 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
Radek Krejci9ed7a192018-10-31 16:23:51 +0100864 check_data.name = name;
865 check_data.revision = revision;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200866 lys_parse_mem_module(ctx, in, format, implement, lysp_load_module_check, &check_data, mod);
Michal Vasko63f3d842020-07-08 10:10:14 +0200867 ly_in_free(in, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100868 if (module_data_free) {
869 module_data_free((void*)module_data, ctx->imp_clb_data);
870 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200871 if (*mod && implement) {
872 lys_compile(mod, 0);
Radek Krejci096235c2019-01-11 11:12:19 +0100873 }
Radek Krejci086c7132018-10-26 15:29:04 +0200874 }
875 }
876 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
877 goto search_file;
878 }
879 } else {
880search_file:
881 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
882 /* module was not received from the callback or there is no callback set */
Radek Krejci78f06822019-10-30 12:54:05 +0100883 lys_module_localfile(ctx, name, revision, implement, NULL, NULL, m ? 0 : 1, (void **)mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200884 }
885 if (!(*mod) && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
886 goto search_clb;
887 }
888 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100889
Radek Krejcib3289d62019-09-18 12:21:39 +0200890 /* update the latest_revision flag - here we have selected the latest available schema,
891 * consider that even the callback provides correct latest revision */
892 if (!(*mod) && m) {
Radek Krejci78f06822019-10-30 12:54:05 +0100893 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 +0200894 m->latest_revision = 2;
895 *mod = m;
896 } else if ((*mod) && !revision && ((*mod)->latest_revision == 1)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100897 (*mod)->latest_revision = 2;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100898 }
Radek Krejci086c7132018-10-26 15:29:04 +0200899 } else {
900 /* we have module from the current context */
Radek Krejci0af46292019-01-11 16:02:31 +0100901 if (implement) {
902 m = ly_ctx_get_module_implemented(ctx, name);
903 if (m && m != *mod) {
904 /* check collision with other implemented revision */
905 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
906 "Module \"%s\" is already present in other implemented revision.", name);
907 *mod = NULL;
908 return LY_EDENIED;
909 }
Radek Krejci086c7132018-10-26 15:29:04 +0200910 }
911
912 /* circular check */
Radek Krejcif8f882a2018-10-31 14:51:15 +0100913 if ((*mod)->parsed && (*mod)->parsed->parsing) {
Radek Krejci086c7132018-10-26 15:29:04 +0200914 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
915 *mod = NULL;
916 return LY_EVALID;
917 }
918 }
919 if (!(*mod)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100920 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "%s \"%s\" module failed.", implement ? "Loading" : "Importing", name);
Radek Krejci086c7132018-10-26 15:29:04 +0200921 return LY_EVALID;
922 }
923
924 if (implement) {
925 /* mark the module implemented, check for collision was already done */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100926 (*mod)->implemented = 1;
Radek Krejci086c7132018-10-26 15:29:04 +0200927 }
Radek Krejci086c7132018-10-26 15:29:04 +0200928
929 return LY_SUCCESS;
930}
931
932LY_ERR
David Sedlák4a650532019-07-10 11:55:18 +0200933lysp_check_stringchar(struct lys_parser_ctx *ctx, unsigned int c)
934{
935 if (!is_yangutf8char(c)) {
936 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
937 return LY_EVALID;
938 }
939 return LY_SUCCESS;
940}
941
942LY_ERR
943lysp_check_identifierchar(struct lys_parser_ctx *ctx, unsigned int c, int first, int *prefix)
944{
945 if (first || (prefix && (*prefix) == 1)) {
946 if (!is_yangidentstartchar(c)) {
947 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c'.", c);
948 return LY_EVALID;
949 }
950 if (prefix) {
951 if (first) {
952 (*prefix) = 0;
953 } else {
954 (*prefix) = 2;
955 }
956 }
957 } else if (c == ':' && prefix && (*prefix) == 0) {
958 (*prefix) = 1;
959 } else if (!is_yangidentchar(c)) {
960 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c'.", c);
961 return LY_EVALID;
962 }
963
964 return LY_SUCCESS;
965}
966
967LY_ERR
Michal Vasko7c8439f2020-08-05 13:25:19 +0200968lysp_load_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200969{
Michal Vaskob36053d2020-03-26 15:49:30 +0100970 struct ly_ctx *ctx = (struct ly_ctx *)(PARSER_CTX(pctx));
Radek Krejci3eb299d2019-04-08 15:07:44 +0200971 struct lysp_submodule *submod = NULL;
Radek Krejcid33273d2018-10-25 14:55:52 +0200972 const char *submodule_data = NULL;
973 LYS_INFORMAT format = LYS_IN_UNKNOWN;
974 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100975 struct lysp_load_module_check_data check_data = {0};
Michal Vasko63f3d842020-07-08 10:10:14 +0200976 struct ly_in *in;
Radek Krejcid33273d2018-10-25 14:55:52 +0200977
Radek Krejcibbe09a92018-11-08 09:36:54 +0100978 /* submodule not present in the context, get the input data and parse it */
Michal Vaskob36053d2020-03-26 15:49:30 +0100979 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200980search_clb:
Michal Vaskob36053d2020-03-26 15:49:30 +0100981 if (ctx->imp_clb) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200982 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 +0100983 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200984 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
Radek Krejcibbe09a92018-11-08 09:36:54 +0100985 check_data.name = inc->name;
986 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200987 check_data.submoduleof = pctx->main_mod->name;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200988 lys_parse_mem_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, &submod);
Michal Vasko63f3d842020-07-08 10:10:14 +0200989 ly_in_free(in, 0);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100990 if (submodule_data_free) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100991 submodule_data_free((void*)submodule_data, ctx->imp_clb_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200992 }
993 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200994 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100995 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100996 goto search_file;
Radek Krejcid33273d2018-10-25 14:55:52 +0200997 }
Radek Krejci2d31ea72018-10-25 15:46:42 +0200998 } else {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100999search_file:
Michal Vaskob36053d2020-03-26 15:49:30 +01001000 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001001 /* submodule was not received from the callback or there is no callback set */
Michal Vasko7c8439f2020-08-05 13:25:19 +02001002 lys_module_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, 0, pctx, pctx->main_mod->name, 1,
Radek Krejci0f969882020-08-21 16:56:47 +02001003 (void**)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001004 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001005 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01001006 goto search_clb;
1007 }
1008 }
1009 if (submod) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001010 if (!inc->rev[0] && (submod->latest_revision == 1)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01001011 /* update the latest_revision flag - here we have selected the latest available schema,
1012 * consider that even the callback provides correct latest revision */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001013 submod->latest_revision = 2;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001014 }
1015
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001016 inc->submodule = submod;
Radek Krejcid33273d2018-10-25 14:55:52 +02001017 }
1018 if (!inc->submodule) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001019 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.",
Michal Vasko7c8439f2020-08-05 13:25:19 +02001020 inc->name, pctx->main_mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +02001021 return LY_EVALID;
1022 }
1023
1024 return LY_SUCCESS;
1025}
1026
Radek Krejcice8c1592018-10-29 15:35:51 +01001027struct lys_module *
Radek Krejcia3045382018-11-22 14:30:31 +01001028lys_module_find_prefix(const struct lys_module *mod, const char *prefix, size_t len)
Radek Krejcice8c1592018-10-29 15:35:51 +01001029{
Michal Vasko7c8439f2020-08-05 13:25:19 +02001030 struct lys_module *m = NULL;
1031 LY_ARRAY_COUNT_TYPE u;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001032
Radek Krejci873ab432020-08-13 13:47:27 +02001033 if (!len || !ly_strncmp(mod->prefix, prefix, len)) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001034 /* it is the prefix of the module itself */
1035 m = (struct lys_module *)mod;
Radek Krejci73dead22019-07-11 16:46:16 +02001036 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001037
1038 /* search in imports */
1039 if (!m && mod->parsed) {
1040 LY_ARRAY_FOR(mod->parsed->imports, u) {
1041 if (!ly_strncmp(mod->parsed->imports[u].prefix, prefix, len)) {
1042 m = mod->parsed->imports[u].module;
1043 break;
1044 }
1045 }
Radek Krejcibbe09a92018-11-08 09:36:54 +01001046 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001047
1048 return m;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001049}
1050
Radek Krejcia3045382018-11-22 14:30:31 +01001051const char *
Radek Krejci693262f2019-04-29 15:23:20 +02001052lys_prefix_find_module(const struct lys_module *mod, const struct lys_module *import)
1053{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001054 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001055
1056 if (import == mod) {
1057 return mod->prefix;
1058 }
1059
1060 if (mod->parsed) {
1061 LY_ARRAY_FOR(mod->parsed->imports, u) {
1062 if (mod->parsed->imports[u].module == import) {
1063 return mod->parsed->imports[u].prefix;
1064 }
1065 }
1066 } else {
1067 /* we don't have original information about the import's prefix,
1068 * so the prefix of the import module itself is returned instead */
1069 return import->prefix;
1070 }
1071
1072 return NULL;
1073}
1074
Radek Krejci0935f412019-08-20 16:15:18 +02001075API const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001076lys_nodetype2str(uint16_t nodetype)
1077{
1078 switch(nodetype) {
1079 case LYS_CONTAINER:
1080 return "container";
1081 case LYS_CHOICE:
1082 return "choice";
1083 case LYS_LEAF:
1084 return "leaf";
1085 case LYS_LEAFLIST:
1086 return "leaf-list";
1087 case LYS_LIST:
1088 return "list";
1089 case LYS_ANYXML:
1090 return "anyxml";
1091 case LYS_ANYDATA:
1092 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001093 case LYS_CASE:
1094 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001095 case LYS_RPC:
1096 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001097 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001098 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001099 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001100 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001101 case LYS_USES:
1102 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001103 default:
1104 return "unknown";
1105 }
1106}
1107
Radek Krejci693262f2019-04-29 15:23:20 +02001108const char *
1109lys_datatype2str(LY_DATA_TYPE basetype)
1110{
1111 switch(basetype) {
1112 case LY_TYPE_BINARY:
1113 return "binary";
1114 case LY_TYPE_UINT8:
1115 return "uint8";
1116 case LY_TYPE_UINT16:
1117 return "uint16";
1118 case LY_TYPE_UINT32:
1119 return "uint32";
1120 case LY_TYPE_UINT64:
1121 return "uint64";
1122 case LY_TYPE_STRING:
1123 return "string";
1124 case LY_TYPE_BITS:
1125 return "bits";
1126 case LY_TYPE_BOOL:
1127 return "boolean";
1128 case LY_TYPE_DEC64:
1129 return "decimal64";
1130 case LY_TYPE_EMPTY:
1131 return "empty";
1132 case LY_TYPE_ENUM:
1133 return "enumeration";
1134 case LY_TYPE_IDENT:
1135 return "identityref";
1136 case LY_TYPE_INST:
1137 return "instance-identifier";
1138 case LY_TYPE_LEAFREF:
1139 return "leafref";
1140 case LY_TYPE_UNION:
1141 return "union";
1142 case LY_TYPE_INT8:
1143 return "int8";
1144 case LY_TYPE_INT16:
1145 return "int16";
1146 case LY_TYPE_INT32:
1147 return "int32";
1148 case LY_TYPE_INT64:
1149 return "int64";
1150 default:
1151 return "unknown";
1152 }
1153}
1154
Radek Krejci056d0a82018-12-06 16:57:25 +01001155API const struct lysp_tpdf *
1156lysp_node_typedefs(const struct lysp_node *node)
1157{
Radek Krejci0fb28562018-12-13 15:17:37 +01001158 switch (node->nodetype) {
1159 case LYS_CONTAINER:
1160 return ((struct lysp_node_container*)node)->typedefs;
1161 case LYS_LIST:
1162 return ((struct lysp_node_list*)node)->typedefs;
1163 case LYS_GROUPING:
1164 return ((struct lysp_grp*)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001165 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001166 case LYS_ACTION:
1167 return ((struct lysp_action*)node)->typedefs;
1168 case LYS_INOUT:
1169 return ((struct lysp_action_inout*)node)->typedefs;
1170 case LYS_NOTIF:
1171 return ((struct lysp_notif*)node)->typedefs;
1172 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001173 return NULL;
1174 }
1175}
1176
Radek Krejci53ea6152018-12-13 15:21:15 +01001177API const struct lysp_grp *
1178lysp_node_groupings(const struct lysp_node *node)
1179{
1180 switch (node->nodetype) {
1181 case LYS_CONTAINER:
1182 return ((struct lysp_node_container*)node)->groupings;
1183 case LYS_LIST:
1184 return ((struct lysp_node_list*)node)->groupings;
1185 case LYS_GROUPING:
1186 return ((struct lysp_grp*)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001187 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001188 case LYS_ACTION:
1189 return ((struct lysp_action*)node)->groupings;
1190 case LYS_INOUT:
1191 return ((struct lysp_action_inout*)node)->groupings;
1192 case LYS_NOTIF:
1193 return ((struct lysp_notif*)node)->groupings;
1194 default:
1195 return NULL;
1196 }
1197}
1198
Radek Krejcibbe09a92018-11-08 09:36:54 +01001199struct lysp_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001200lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001201{
1202 assert(node);
1203 switch (node->nodetype) {
1204 case LYS_CONTAINER:
1205 return &((struct lysp_node_container*)node)->actions;
1206 case LYS_LIST:
1207 return &((struct lysp_node_list*)node)->actions;
1208 case LYS_GROUPING:
1209 return &((struct lysp_grp*)node)->actions;
1210 case LYS_AUGMENT:
1211 return &((struct lysp_augment*)node)->actions;
1212 default:
1213 return NULL;
1214 }
1215}
1216
Radek Krejci056d0a82018-12-06 16:57:25 +01001217API const struct lysp_action *
1218lysp_node_actions(const struct lysp_node *node)
1219{
1220 struct lysp_action **actions;
1221 actions = lysp_node_actions_p((struct lysp_node*)node);
1222 if (actions) {
1223 return *actions;
1224 } else {
1225 return NULL;
1226 }
1227}
1228
Radek Krejcibbe09a92018-11-08 09:36:54 +01001229struct lysp_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001230lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001231{
1232 assert(node);
1233 switch (node->nodetype) {
1234 case LYS_CONTAINER:
1235 return &((struct lysp_node_container*)node)->notifs;
1236 case LYS_LIST:
1237 return &((struct lysp_node_list*)node)->notifs;
1238 case LYS_GROUPING:
1239 return &((struct lysp_grp*)node)->notifs;
1240 case LYS_AUGMENT:
1241 return &((struct lysp_augment*)node)->notifs;
1242 default:
1243 return NULL;
1244 }
1245}
1246
Radek Krejci056d0a82018-12-06 16:57:25 +01001247API const struct lysp_notif *
1248lysp_node_notifs(const struct lysp_node *node)
1249{
1250 struct lysp_notif **notifs;
1251 notifs = lysp_node_notifs_p((struct lysp_node*)node);
1252 if (notifs) {
1253 return *notifs;
1254 } else {
1255 return NULL;
1256 }
1257}
1258
Radek Krejcibbe09a92018-11-08 09:36:54 +01001259struct lysp_node **
Radek Krejci056d0a82018-12-06 16:57:25 +01001260lysp_node_children_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001261{
1262 assert(node);
1263 switch (node->nodetype) {
1264 case LYS_CONTAINER:
1265 return &((struct lysp_node_container*)node)->child;
1266 case LYS_CHOICE:
1267 return &((struct lysp_node_choice*)node)->child;
1268 case LYS_LIST:
1269 return &((struct lysp_node_list*)node)->child;
1270 case LYS_CASE:
1271 return &((struct lysp_node_case*)node)->child;
1272 case LYS_GROUPING:
1273 return &((struct lysp_grp*)node)->data;
1274 case LYS_AUGMENT:
1275 return &((struct lysp_augment*)node)->child;
1276 case LYS_INOUT:
1277 return &((struct lysp_action_inout*)node)->data;
1278 case LYS_NOTIF:
1279 return &((struct lysp_notif*)node)->data;
1280 default:
1281 return NULL;
1282 }
1283}
1284
Radek Krejci056d0a82018-12-06 16:57:25 +01001285API const struct lysp_node *
1286lysp_node_children(const struct lysp_node *node)
1287{
1288 struct lysp_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001289
1290 if (!node) {
1291 return NULL;
1292 }
1293
Radek Krejci056d0a82018-12-06 16:57:25 +01001294 children = lysp_node_children_p((struct lysp_node*)node);
1295 if (children) {
1296 return *children;
1297 } else {
1298 return NULL;
1299 }
1300}
1301
1302struct lysc_action **
1303lysc_node_actions_p(struct lysc_node *node)
1304{
1305 assert(node);
1306 switch (node->nodetype) {
1307 case LYS_CONTAINER:
1308 return &((struct lysc_node_container*)node)->actions;
1309 case LYS_LIST:
1310 return &((struct lysc_node_list*)node)->actions;
1311 default:
1312 return NULL;
1313 }
1314}
1315
1316API const struct lysc_action *
1317lysc_node_actions(const struct lysc_node *node)
1318{
1319 struct lysc_action **actions;
1320 actions = lysc_node_actions_p((struct lysc_node*)node);
1321 if (actions) {
1322 return *actions;
1323 } else {
1324 return NULL;
1325 }
1326}
1327
1328struct lysc_notif **
1329lysc_node_notifs_p(struct lysc_node *node)
1330{
1331 assert(node);
1332 switch (node->nodetype) {
1333 case LYS_CONTAINER:
1334 return &((struct lysc_node_container*)node)->notifs;
1335 case LYS_LIST:
1336 return &((struct lysc_node_list*)node)->notifs;
1337 default:
1338 return NULL;
1339 }
1340}
1341
1342API const struct lysc_notif *
1343lysc_node_notifs(const struct lysc_node *node)
1344{
1345 struct lysc_notif **notifs;
1346 notifs = lysc_node_notifs_p((struct lysc_node*)node);
1347 if (notifs) {
1348 return *notifs;
1349 } else {
1350 return NULL;
1351 }
1352}
1353
Radek Krejcibbe09a92018-11-08 09:36:54 +01001354struct lysc_node **
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001355lysc_node_children_p(const struct lysc_node *node, uint16_t flags)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001356{
1357 assert(node);
1358 switch (node->nodetype) {
1359 case LYS_CONTAINER:
1360 return &((struct lysc_node_container*)node)->child;
1361 case LYS_CHOICE:
Radek Krejcia3045382018-11-22 14:30:31 +01001362 if (((struct lysc_node_choice*)node)->cases) {
Radek Krejci95710c92019-02-11 15:49:55 +01001363 return &((struct lysc_node_choice*)node)->cases->child;
Radek Krejcia3045382018-11-22 14:30:31 +01001364 } else {
1365 return NULL;
1366 }
Radek Krejci01342af2019-01-03 15:18:08 +01001367 case LYS_CASE:
1368 return &((struct lysc_node_case*)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001369 case LYS_LIST:
1370 return &((struct lysc_node_list*)node)->child;
Michal Vasko1bf09392020-03-27 12:38:10 +01001371 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001372 case LYS_ACTION:
1373 if (flags & LYS_CONFIG_R) {
1374 return &((struct lysc_action*)node)->output.data;
1375 } else {
1376 /* LYS_CONFIG_W, but also the default case */
1377 return &((struct lysc_action*)node)->input.data;
1378 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02001379 case LYS_NOTIF:
1380 return &((struct lysc_notif*)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001381 default:
1382 return NULL;
1383 }
1384}
1385
Radek Krejci056d0a82018-12-06 16:57:25 +01001386API const struct lysc_node *
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001387lysc_node_children(const struct lysc_node *node, uint16_t flags)
Radek Krejcia3045382018-11-22 14:30:31 +01001388{
Radek Krejci056d0a82018-12-06 16:57:25 +01001389 struct lysc_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001390
1391 if (!node) {
1392 return NULL;
1393 }
1394
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001395 children = lysc_node_children_p((struct lysc_node*)node, flags);
Radek Krejci056d0a82018-12-06 16:57:25 +01001396 if (children) {
1397 return *children;
1398 } else {
Radek Krejcia3045382018-11-22 14:30:31 +01001399 return NULL;
1400 }
1401}
1402
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001403struct lys_module *
1404lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1405{
1406 unsigned int u;
1407
1408 for (u = 0; u < ctx->list.count; ++u) {
1409 if (((struct lys_module*)ctx->list.objs[u])->parsed == mod) {
1410 return ((struct lys_module*)ctx->list.objs[u]);
1411 }
1412 }
1413 return NULL;
1414}
1415
Radek Krejcid6b76452019-09-03 17:03:03 +02001416enum ly_stmt
Michal Vasko63f3d842020-07-08 10:10:14 +02001417lysp_match_kw(struct lys_yang_parser_ctx *ctx, struct ly_in *in)
David Sedlákc10e7902018-12-17 02:17:59 +01001418{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001419/**
Michal Vasko63f3d842020-07-08 10:10:14 +02001420 * @brief Move the INPUT by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001421 * @param[in] CTX yang parser context to update its indent value.
Michal Vasko63f3d842020-07-08 10:10:14 +02001422 * @param[in,out] IN input to move
David Sedlák1bccdfa2019-06-17 15:55:27 +02001423 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
Michal Vasko64246d82020-08-19 12:35:00 +02001424 *
1425 * *INDENT-OFF*
David Sedlák1bccdfa2019-06-17 15:55:27 +02001426 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001427#define MOVE_IN(CTX, IN, COUNT) ly_in_skip(IN, COUNT);if(CTX){(CTX)->indent+=COUNT;}
1428#define IF_KW(STR, LEN, STMT) if (!strncmp(in->current, STR, LEN)) {MOVE_IN(ctx, in, LEN);*kw=STMT;}
1429#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 +02001430#define IF_KW_PREFIX_END }
David Sedlák572e7ab2019-06-04 16:01:58 +02001431
Michal Vasko63f3d842020-07-08 10:10:14 +02001432 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001433 enum ly_stmt result = LY_STMT_NONE;
1434 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001435 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001436 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001437 case 'a':
Michal Vasko63f3d842020-07-08 10:10:14 +02001438 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001439 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1440 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1441 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001442 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001443 IF_KW("data", 4, LY_STMT_ANYDATA)
1444 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001445 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001446 break;
1447 case 'b':
Michal Vasko63f3d842020-07-08 10:10:14 +02001448 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001449 IF_KW("ase", 3, LY_STMT_BASE)
1450 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1451 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001452 break;
1453 case 'c':
Michal Vasko63f3d842020-07-08 10:10:14 +02001454 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001455 IF_KW("ase", 3, LY_STMT_CASE)
1456 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001457 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001458 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001459 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001460 IF_KW("ct", 2, LY_STMT_CONTACT)
1461 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001462 IF_KW_PREFIX_END
1463 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001464 break;
1465 case 'd':
Michal Vasko63f3d842020-07-08 10:10:14 +02001466 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001467 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001468 IF_KW("fault", 5, LY_STMT_DEFAULT)
1469 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001470 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001471 IF_KW("e", 1, LY_STMT_DEVIATE)
1472 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001473 IF_KW_PREFIX_END
1474 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001475 break;
1476 case 'e':
Michal Vasko63f3d842020-07-08 10:10:14 +02001477 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001478 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001479 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001480 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1481 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001482 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001483 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001484 break;
1485 case 'f':
Michal Vasko63f3d842020-07-08 10:10:14 +02001486 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001487 IF_KW("eature", 6, LY_STMT_FEATURE)
1488 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001489 break;
1490 case 'g':
Michal Vasko63f3d842020-07-08 10:10:14 +02001491 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001492 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001493 break;
1494 case 'i':
Michal Vasko63f3d842020-07-08 10:10:14 +02001495 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001496 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1497 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1498 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001499 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001500 IF_KW("clude", 5, LY_STMT_INCLUDE)
1501 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001502 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001503 break;
1504 case 'k':
Michal Vasko63f3d842020-07-08 10:10:14 +02001505 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001506 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001507 break;
1508 case 'l':
Michal Vasko63f3d842020-07-08 10:10:14 +02001509 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001510 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001511 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1512 else IF_KW("af", 2, LY_STMT_LEAF)
1513 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001514 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001515 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001516 break;
1517 case 'm':
Michal Vasko63f3d842020-07-08 10:10:14 +02001518 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001519 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001520 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1521 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001522 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001523 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1524 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001525 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001526 IF_KW("ule", 3, LY_STMT_MODULE)
1527 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001528 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001529 break;
1530 case 'n':
Michal Vasko63f3d842020-07-08 10:10:14 +02001531 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001532 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1533 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001534 break;
1535 case 'o':
Michal Vasko63f3d842020-07-08 10:10:14 +02001536 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001537 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001538 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1539 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001540 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001541 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001542 break;
1543 case 'p':
Michal Vasko63f3d842020-07-08 10:10:14 +02001544 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001545 IF_KW("ath", 3, LY_STMT_PATH)
1546 else IF_KW("attern", 6, LY_STMT_PATTERN)
1547 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001548 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001549 IF_KW("fix", 3, LY_STMT_PREFIX)
1550 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001551 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001552 break;
1553 case 'r':
Michal Vasko63f3d842020-07-08 10:10:14 +02001554 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001555 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001556 else IF_KW_PREFIX("e", 1)
1557 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001558 IF_KW("erence", 6, LY_STMT_REFERENCE)
1559 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001560 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001561 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1562 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1563 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001564 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001565 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001566 break;
1567 case 's':
Michal Vasko63f3d842020-07-08 10:10:14 +02001568 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001569 IF_KW("tatus", 5, LY_STMT_STATUS)
1570 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001571 break;
1572 case 't':
Michal Vasko63f3d842020-07-08 10:10:14 +02001573 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001574 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1575 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001576 break;
1577 case 'u':
Michal Vasko63f3d842020-07-08 10:10:14 +02001578 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001579 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001580 IF_KW("que", 3, LY_STMT_UNIQUE)
1581 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001582 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001583 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001584 break;
1585 case 'v':
Michal Vasko63f3d842020-07-08 10:10:14 +02001586 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001587 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001588 break;
1589 case 'w':
Michal Vasko63f3d842020-07-08 10:10:14 +02001590 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001591 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001592 break;
1593 case 'y':
Michal Vasko63f3d842020-07-08 10:10:14 +02001594 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001595 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
1596 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001597 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001598 default:
David Sedlák1bccdfa2019-06-17 15:55:27 +02001599 /* if context is not NULL we are matching keyword from YANG data*/
1600 if (ctx) {
Michal Vasko63f3d842020-07-08 10:10:14 +02001601 if (in->current[0] == ';') {
1602 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001603 *kw = LY_STMT_SYNTAX_SEMICOLON;
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_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001607 } else if (in->current[0] == '}') {
1608 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001609 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001610 }
1611 }
David Sedlák23a59a62018-10-26 13:08:02 +02001612 break;
1613 }
1614
Michal Vasko63f3d842020-07-08 10:10:14 +02001615 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02001616 /* the keyword is not terminated */
1617 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001618 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02001619 }
1620
David Sedlák1bccdfa2019-06-17 15:55:27 +02001621#undef IF_KW
1622#undef IF_KW_PREFIX
1623#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01001624#undef MOVE_IN
Michal Vasko64246d82020-08-19 12:35:00 +02001625 /* *INDENT-ON* */
David Sedlák18730132019-03-15 15:51:34 +01001626
David Sedlák1bccdfa2019-06-17 15:55:27 +02001627 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02001628}
David Sedlákecf5eb82019-06-03 14:12:44 +02001629
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001630LY_ARRAY_COUNT_TYPE
1631lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, LYEXT_SUBSTMT substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001632{
1633 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1634
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001635 for (; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001636 if (ext[index].insubstmt == substmt) {
1637 return index;
1638 }
1639 }
1640
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001641 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001642}
1643
Michal Vasko62ed12d2020-05-21 10:08:25 +02001644const struct lysc_node *
1645lysc_data_parent(const struct lysc_node *schema)
1646{
1647 const struct lysc_node *parent;
1648
Radek Krejci1e008d22020-08-17 11:37:37 +02001649 for (parent = schema->parent; parent && (parent->nodetype & (LYS_CHOICE | LYS_CASE)); parent = parent->parent) {}
Michal Vasko62ed12d2020-05-21 10:08:25 +02001650
1651 return parent;
1652}
Michal Vasko00cbf532020-06-15 13:58:47 +02001653
1654int
1655lysc_is_output(const struct lysc_node *schema)
1656{
1657 const struct lysc_node *parent;
1658
1659 assert(schema);
1660
Radek Krejci1e008d22020-08-17 11:37:37 +02001661 for (parent = schema->parent; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION)); parent = parent->parent) {}
Michal Vasko00cbf532020-06-15 13:58:47 +02001662 if (parent && (schema->flags & LYS_CONFIG_R)) {
1663 return 1;
1664 }
1665 return 0;
1666}
Michal Vaskod975f862020-06-23 13:29:28 +02001667
1668API int
1669lysc_is_userordered(const struct lysc_node *schema)
1670{
1671 if (!schema || !(schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) || !(schema->flags & LYS_ORDBY_USER)) {
1672 return 0;
1673 }
1674
1675 return 1;
1676}
Michal Vasko2fd91b92020-07-17 16:39:02 +02001677
1678API LY_ERR
1679lysc_set_private(const struct lysc_node *node, void *priv, void **prev)
1680{
1681 struct lysc_action *act;
1682 struct lysc_notif *notif;
1683
1684 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
1685
1686 switch (node->nodetype) {
1687 case LYS_CONTAINER:
1688 case LYS_CHOICE:
1689 case LYS_CASE:
1690 case LYS_LEAF:
1691 case LYS_LEAFLIST:
1692 case LYS_LIST:
1693 case LYS_ANYXML:
1694 case LYS_ANYDATA:
1695 if (prev) {
1696 *prev = node->priv;
1697 }
1698 ((struct lysc_node *)node)->priv = priv;
1699 break;
1700 case LYS_RPC:
1701 case LYS_ACTION:
1702 act = (struct lysc_action *)node;
1703 if (prev) {
1704 *prev = act->priv;
1705 }
1706 act->priv = priv;
1707 break;
1708 case LYS_NOTIF:
1709 notif = (struct lysc_notif *)node;
1710 if (prev) {
1711 *prev = notif->priv;
1712 }
1713 notif->priv = priv;
1714 break;
1715 default:
1716 return LY_EINVAL;
1717 }
1718
1719 return LY_SUCCESS;
1720}