blob: bedf795798c9335174f80cb3aa13c3301e8f096e [file] [log] [blame]
Radek Krejci86d106e2018-10-18 09:53:19 +02001/**
2 * @file tree_schema_helpers.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejcie7b95092019-05-15 11:03:07 +02004 * @brief Parsing and validation helper functions for schema trees
Radek Krejci86d106e2018-10-18 09:53:19 +02005 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Krejci535ea9f2020-05-29 16:01:05 +020014
15#define _GNU_SOURCE
Radek Krejci86d106e2018-10-18 09:53:19 +020016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stdint.h>
Radek Krejci9ed7a192018-10-31 16:23:51 +010020#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020022#include <time.h>
23
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
25#include "config.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
Radek Krejci95710c92019-02-11 15:49:55 +010038lys_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *context_node,
Radek Krejci6eeb58f2019-02-22 16:29:37 +010039 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 */
Michal Vasko294b76d2019-12-16 10:37:54 +010095 ret = lys_set_implemented_internal((struct lys_module*)mod, 2);
96 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,
174 uint16_t flags1, void *mod1, const char *name1,
175 uint16_t flags2, void *mod2, const char *name2)
176{
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 Krejci4f28eda2018-11-12 11:46:16 +0100356 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 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) {
371 *module = lysp_module_find_prefix(start_module, id, str - id);
372 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100373 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100374 } else {
375 *module = start_module;
376 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100377
378 /* check for built-in types */
379 *type = lysp_type_str2builtin(name, strlen(name));
380 if (*type) {
381 *tpdf = NULL;
382 return LY_SUCCESS;
383 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100384 }
385 LY_CHECK_RET(!(*module), LY_ENOTFOUND);
386
387 if (start_node && *module == start_module) {
388 /* search typedefs in parent's nodes */
389 *node = start_node;
390 while (*node) {
391 *tpdf = lysp_type_match(name, *node);
392 if (*tpdf) {
393 /* match */
394 return LY_SUCCESS;
395 }
396 *node = (*node)->parent;
397 }
398 }
399
400 /* search in top-level typedefs */
401 if ((*module)->typedefs) {
402 LY_ARRAY_FOR((*module)->typedefs, u) {
403 if (!strcmp(name, (*module)->typedefs[u].name)) {
404 /* match */
405 *tpdf = &(*module)->typedefs[u];
406 return LY_SUCCESS;
407 }
408 }
409 }
410
411 /* search in submodules' typedefs */
412 LY_ARRAY_FOR((*module)->includes, u) {
413 typedefs = (*module)->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100414 LY_ARRAY_FOR(typedefs, v) {
415 if (!strcmp(name, typedefs[v].name)) {
416 /* match */
417 *tpdf = &typedefs[v];
418 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100419 }
420 }
421 }
422
423 return LY_ENOTFOUND;
424}
425
David Sedlák6544c182019-07-12 13:17:33 +0200426LY_ERR
David Sedlák07869a52019-07-12 14:28:19 +0200427lysp_check_enum_name(struct lys_parser_ctx *ctx, const char *name, size_t name_len)
David Sedlák6544c182019-07-12 13:17:33 +0200428{
429 if (!name_len) {
430 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
431 return LY_EVALID;
432 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
433 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
434 name_len, name);
435 return LY_EVALID;
436 } else {
437 for (size_t u = 0; u < name_len; ++u) {
438 if (iscntrl(name[u])) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100439 LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
David Sedlák6544c182019-07-12 13:17:33 +0200440 name_len, name, u + 1);
441 break;
442 }
443 }
444 }
445
446 return LY_SUCCESS;
447}
448
Michal Vaskob36053d2020-03-26 15:49:30 +0100449/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100450 * @brief Check name of a new type to avoid name collisions.
451 *
452 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
453 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
454 * @param[in] tpdf Typedef definition to check.
455 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
456 * typedefs are checked, caller is supposed to free the table.
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 * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise.
460 */
461static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200462lysp_check_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
Radek Krejcibbe09a92018-11-08 09:36:54 +0100463 struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped)
464{
465 struct lysp_node *parent;
466 uint32_t hash;
467 size_t name_len;
468 const char *name;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200469 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100470 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100471
472 assert(ctx);
473 assert(tpdf);
474
475 name = tpdf->name;
476 name_len = strlen(name);
477
Radek Krejci4f28eda2018-11-12 11:46:16 +0100478 if (lysp_type_str2builtin(name, name_len)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100479 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 +0100480 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100481 }
482
483 /* check locally scoped typedefs (avoid name shadowing) */
484 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100485 typedefs = lysp_node_typedefs(node);
486 LY_ARRAY_FOR(typedefs, u) {
487 if (&typedefs[u] == tpdf) {
488 break;
489 }
490 if (!strcmp(name, typedefs[u].name)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100491 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with sibling type.", name);
Radek Krejci0fb28562018-12-13 15:17:37 +0100492 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100493 }
494 }
495 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200496 for (parent = node->parent; parent; parent = parent->parent) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100497 if (lysp_type_match(name, parent)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100498 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 +0100499 return LY_EEXIST;
500 }
501 }
502 }
503
504 /* check collision with the top-level typedefs */
505 hash = dict_hash(name, name_len);
506 if (node) {
507 lyht_insert(tpdfs_scoped, &name, hash, NULL);
508 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100509 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 +0100510 return LY_EEXIST;
511 }
512 } else {
513 if (lyht_insert(tpdfs_global, &name, hash, NULL)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100514 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 +0100515 return LY_EEXIST;
516 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100517 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
518 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
519 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100520 }
521
522 return LY_SUCCESS;
523}
524
525static int
526lysp_id_cmp(void *val1, void *val2, int UNUSED(mod), void *UNUSED(cb_data))
527{
528 return !strcmp(val1, val2);
529}
530
531LY_ERR
David Sedlákd2ebe572019-07-22 12:53:14 +0200532lysp_parse_finalize_reallocated(struct lys_parser_ctx *ctx, struct lysp_grp *groupings, struct lysp_augment *augments,
533 struct lysp_action *actions, struct lysp_notif *notifs)
534{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200535 LY_ARRAY_COUNT_TYPE u, v;
David Sedlákd2ebe572019-07-22 12:53:14 +0200536 struct lysp_node *child;
537
538 /* finalize parent pointers to the reallocated items */
539
540 /* gropings */
541 LY_ARRAY_FOR(groupings, u) {
542 LY_LIST_FOR(groupings[u].data, child) {
543 child->parent = (struct lysp_node*)&groupings[u];
544 }
545 LY_ARRAY_FOR(groupings[u].actions, v) {
546 groupings[u].actions[v].parent = (struct lysp_node*)&groupings[u];
547 }
548 LY_ARRAY_FOR(groupings[u].notifs, v) {
549 groupings[u].notifs[v].parent = (struct lysp_node*)&groupings[u];
550 }
551 LY_ARRAY_FOR(groupings[u].groupings, v) {
552 groupings[u].groupings[v].parent = (struct lysp_node*)&groupings[u];
553 }
554 if (groupings[u].typedefs) {
555 ly_set_add(&ctx->tpdfs_nodes, &groupings[u], 0);
556 }
557 }
558
559 /* augments */
560 LY_ARRAY_FOR(augments, u) {
561 LY_LIST_FOR(augments[u].child, child) {
562 child->parent = (struct lysp_node*)&augments[u];
563 }
564 LY_ARRAY_FOR(augments[u].actions, v) {
565 augments[u].actions[v].parent = (struct lysp_node*)&augments[u];
566 }
567 LY_ARRAY_FOR(augments[u].notifs, v) {
568 augments[u].notifs[v].parent = (struct lysp_node*)&augments[u];
569 }
570 }
571
572 /* actions */
573 LY_ARRAY_FOR(actions, u) {
574 if (actions[u].input.parent) {
575 actions[u].input.parent = (struct lysp_node*)&actions[u];
576 LY_LIST_FOR(actions[u].input.data, child) {
577 child->parent = (struct lysp_node*)&actions[u].input;
578 }
579 LY_ARRAY_FOR(actions[u].input.groupings, v) {
580 actions[u].input.groupings[v].parent = (struct lysp_node*)&actions[u].input;
581 }
582 if (actions[u].input.typedefs) {
583 ly_set_add(&ctx->tpdfs_nodes, &actions[u].input, 0);
584 }
585 }
586 if (actions[u].output.parent) {
587 actions[u].output.parent = (struct lysp_node*)&actions[u];
588 LY_LIST_FOR(actions[u].output.data, child) {
589 child->parent = (struct lysp_node*)&actions[u].output;
590 }
591 LY_ARRAY_FOR(actions[u].output.groupings, v) {
592 actions[u].output.groupings[v].parent = (struct lysp_node*)&actions[u].output;
593 }
594 if (actions[u].output.typedefs) {
595 ly_set_add(&ctx->tpdfs_nodes, &actions[u].output, 0);
596 }
597 }
598 LY_ARRAY_FOR(actions[u].groupings, v) {
599 actions[u].groupings[v].parent = (struct lysp_node*)&actions[u];
600 }
601 if (actions[u].typedefs) {
602 ly_set_add(&ctx->tpdfs_nodes, &actions[u], 0);
603 }
604 }
605
606 /* notifications */
607 LY_ARRAY_FOR(notifs, u) {
608 LY_LIST_FOR(notifs[u].data, child) {
609 child->parent = (struct lysp_node*)&notifs[u];
610 }
611 LY_ARRAY_FOR(notifs[u].groupings, v) {
612 notifs[u].groupings[v].parent = (struct lysp_node*)&notifs[u];
613 }
614 if (notifs[u].typedefs) {
615 ly_set_add(&ctx->tpdfs_nodes, &notifs[u], 0);
616 }
617 }
618
619 return LY_SUCCESS;
620}
621
622LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200623lysp_check_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100624{
625 struct hash_table *ids_global;
626 struct hash_table *ids_scoped;
Radek Krejci0fb28562018-12-13 15:17:37 +0100627 const struct lysp_tpdf *typedefs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200628 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200629 uint32_t i;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100630 LY_ERR ret = LY_EVALID;
631
632 /* check name collisions - typedefs and groupings */
633 ids_global = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1);
634 ids_scoped = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200635 LY_ARRAY_FOR(mod->typedefs, v) {
636 if (lysp_check_typedef(ctx, NULL, &mod->typedefs[v], ids_global, ids_scoped)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100637 goto cleanup;
638 }
639 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200640 LY_ARRAY_FOR(mod->includes, v) {
641 LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
642 if (lysp_check_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global, ids_scoped)) {
Radek Krejci3b1f9292018-11-08 10:58:35 +0100643 goto cleanup;
644 }
645 }
646 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200647 for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
648 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
649 LY_ARRAY_FOR(typedefs, u) {
650 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 +0100651 goto cleanup;
652 }
653 }
654 }
655 ret = LY_SUCCESS;
656cleanup:
657 lyht_free(ids_global);
658 lyht_free(ids_scoped);
659 ly_set_erase(&ctx->tpdfs_nodes, NULL);
660
661 return ret;
662}
663
Radek Krejci9ed7a192018-10-31 16:23:51 +0100664struct lysp_load_module_check_data {
665 const char *name;
666 const char *revision;
667 const char *path;
668 const char* submoduleof;
669};
670
671static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100672lysp_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 +0100673{
674 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100675 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200676 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100677 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100678 struct lysp_revision *revs;
679
680 name = mod ? mod->mod->name : submod->name;
681 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200682 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100683
684 if (info->name) {
685 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100686 if (strcmp(info->name, name)) {
687 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100688 return LY_EINVAL;
689 }
690 }
691 if (info->revision) {
692 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100693 if (!revs || strcmp(info->revision, revs[0].date)) {
694 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Radek Krejcib07b5c92019-04-08 10:56:37 +0200695 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100696 return LY_EINVAL;
697 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200698 } else if (!latest_revision) {
699 /* do not log, we just need to drop the schema and use the latest revision from the context */
700 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100701 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100702 if (submod) {
703 assert(info->submoduleof);
704
Radek Krejci9ed7a192018-10-31 16:23:51 +0100705 /* check that the submodule belongs-to our module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100706 if (strcmp(info->submoduleof, submod->belongsto)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100707 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 +0100708 submod->name, info->submoduleof, submod->belongsto);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100709 return LY_EVALID;
710 }
711 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100712 if (submod->parsing) {
713 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100714 return LY_EVALID;
715 }
716 }
717 if (info->path) {
718 /* check that name and revision match filename */
719 filename = strrchr(info->path, '/');
720 if (!filename) {
721 filename = info->path;
722 } else {
723 filename++;
724 }
725 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100726 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100727 rev = strchr(filename, '@');
728 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100729 if (strncmp(filename, name, len) ||
Radek Krejci9ed7a192018-10-31 16:23:51 +0100730 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100731 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100732 }
733 /* revision */
734 if (rev) {
735 len = dot - ++rev;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100736 if (!revs || len != 10 || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100737 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100738 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100739 }
740 }
741 }
742 return LY_SUCCESS;
743}
744
745LY_ERR
fredgancd485b82019-10-18 15:00:17 +0800746lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, int implement,
Radek Krejci78f06822019-10-30 12:54:05 +0100747 struct lys_parser_ctx *main_ctx, const char *main_name, int required, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100748{
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200749 struct ly_in *in;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100750 char *filepath = NULL;
751 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100752 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100753 LY_ERR ret = LY_SUCCESS;
754 struct lysp_load_module_check_data check_data = {0};
755
756 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, revision,
757 &filepath, &format));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200758 if (!filepath) {
759 if (required) {
760 LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
761 revision ? revision : "");
762 }
763 return LY_ENOTFOUND;
764 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100765
766 LOGVRB("Loading schema from \"%s\" file.", filepath);
767
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200768 /* get the (sub)module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200769 LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
770 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100771 check_data.name = name;
772 check_data.revision = revision;
773 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800774 check_data.submoduleof = main_name;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200775 if (main_ctx) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200776 ret = lys_parse_mem_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data,
777 (struct lysp_submodule **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200778 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200779 ret = lys_parse_mem_module(ctx, in, format, implement, lysp_load_module_check, &check_data,
780 (struct lys_module **)&mod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200781
782 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200783 LY_CHECK_ERR_GOTO(ret, ly_in_free(in, 1), cleanup);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100784
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100785 if (main_ctx) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200786 lys_parser_fill_filepath(ctx, in, &((struct lysp_submodule *)mod)->filepath);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100787 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200788 lys_parser_fill_filepath(ctx, in, &((struct lys_module *)mod)->filepath);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100789 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200790 ly_in_free(in, 1);
791
792 if (mod && implement) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200793 lys_compile((struct lys_module **)&mod, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100794 }
795
796 *result = mod;
797
798 /* success */
799cleanup:
800 free(filepath);
801 return ret;
802}
803
Radek Krejcid33273d2018-10-25 14:55:52 +0200804LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +0200805lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, int implement, int require_parsed,
806 struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200807{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100808 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200809 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100810 void (*module_data_free)(void *module_data, void *user_data) = NULL;
811 struct lysp_load_module_check_data check_data = {0};
Radek Krejcib3289d62019-09-18 12:21:39 +0200812 struct lys_module *m = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200813 struct ly_in *in;
Radek Krejci086c7132018-10-26 15:29:04 +0200814
Radek Krejci0af46292019-01-11 16:02:31 +0100815 assert(mod);
816
817 if (!*mod) {
818 /* try to get the module from the context */
819 if (revision) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200820 /* get the specific revision */
Radek Krejci0af46292019-01-11 16:02:31 +0100821 *mod = (struct lys_module*)ly_ctx_get_module(ctx, name, revision);
Radek Krejcied5acc52019-04-25 15:57:04 +0200822 } else if (implement) {
823 /* prefer the implemented module instead of the latest one */
824 *mod = (struct lys_module*)ly_ctx_get_module_implemented(ctx, name);
825 if (!*mod) {
826 /* there is no implemented module in the context, try to get the latest revision module */
827 goto latest_in_the_context;
828 }
Radek Krejci0af46292019-01-11 16:02:31 +0100829 } else {
Radek Krejcied5acc52019-04-25 15:57:04 +0200830 /* get the requested module of the latest revision in the context */
831latest_in_the_context:
Radek Krejci0af46292019-01-11 16:02:31 +0100832 *mod = (struct lys_module*)ly_ctx_get_module_latest(ctx, name);
Radek Krejcib3289d62019-09-18 12:21:39 +0200833 if (*mod && (*mod)->latest_revision == 1) {
834 /* let us now search with callback and searchpaths to check if there is newer revision outside the context */
835 m = *mod;
836 *mod = NULL;
837 }
Radek Krejci0af46292019-01-11 16:02:31 +0100838 }
Radek Krejci086c7132018-10-26 15:29:04 +0200839 }
840
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100841 if (!(*mod) || (require_parsed && !(*mod)->parsed)) {
842 (*mod) = NULL;
843
Radek Krejci086c7132018-10-26 15:29:04 +0200844 /* check collision with other implemented revision */
845 if (implement && ly_ctx_get_module_implemented(ctx, name)) {
846 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
847 "Module \"%s\" is already present in other implemented revision.", name);
848 return LY_EDENIED;
849 }
850
Radek Krejci9ed7a192018-10-31 16:23:51 +0100851 /* module not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200852 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
853search_clb:
854 if (ctx->imp_clb) {
855 if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100856 &format, &module_data, &module_data_free) == LY_SUCCESS) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200857 LY_CHECK_RET(ly_in_new_memory(module_data, &in));
Radek Krejci9ed7a192018-10-31 16:23:51 +0100858 check_data.name = name;
859 check_data.revision = revision;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200860 lys_parse_mem_module(ctx, in, format, implement, lysp_load_module_check, &check_data, mod);
Michal Vasko63f3d842020-07-08 10:10:14 +0200861 ly_in_free(in, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100862 if (module_data_free) {
863 module_data_free((void*)module_data, ctx->imp_clb_data);
864 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200865 if (*mod && implement) {
866 lys_compile(mod, 0);
Radek Krejci096235c2019-01-11 11:12:19 +0100867 }
Radek Krejci086c7132018-10-26 15:29:04 +0200868 }
869 }
870 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
871 goto search_file;
872 }
873 } else {
874search_file:
875 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
876 /* module was not received from the callback or there is no callback set */
Radek Krejci78f06822019-10-30 12:54:05 +0100877 lys_module_localfile(ctx, name, revision, implement, NULL, NULL, m ? 0 : 1, (void **)mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200878 }
879 if (!(*mod) && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
880 goto search_clb;
881 }
882 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100883
Radek Krejcib3289d62019-09-18 12:21:39 +0200884 /* update the latest_revision flag - here we have selected the latest available schema,
885 * consider that even the callback provides correct latest revision */
886 if (!(*mod) && m) {
Radek Krejci78f06822019-10-30 12:54:05 +0100887 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 +0200888 m->latest_revision = 2;
889 *mod = m;
890 } else if ((*mod) && !revision && ((*mod)->latest_revision == 1)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100891 (*mod)->latest_revision = 2;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100892 }
Radek Krejci086c7132018-10-26 15:29:04 +0200893 } else {
894 /* we have module from the current context */
Radek Krejci0af46292019-01-11 16:02:31 +0100895 if (implement) {
896 m = ly_ctx_get_module_implemented(ctx, name);
897 if (m && m != *mod) {
898 /* check collision with other implemented revision */
899 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
900 "Module \"%s\" is already present in other implemented revision.", name);
901 *mod = NULL;
902 return LY_EDENIED;
903 }
Radek Krejci086c7132018-10-26 15:29:04 +0200904 }
905
906 /* circular check */
Radek Krejcif8f882a2018-10-31 14:51:15 +0100907 if ((*mod)->parsed && (*mod)->parsed->parsing) {
Radek Krejci086c7132018-10-26 15:29:04 +0200908 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
909 *mod = NULL;
910 return LY_EVALID;
911 }
912 }
913 if (!(*mod)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100914 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "%s \"%s\" module failed.", implement ? "Loading" : "Importing", name);
Radek Krejci086c7132018-10-26 15:29:04 +0200915 return LY_EVALID;
916 }
917
918 if (implement) {
919 /* mark the module implemented, check for collision was already done */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100920 (*mod)->implemented = 1;
Radek Krejci086c7132018-10-26 15:29:04 +0200921 }
Radek Krejci086c7132018-10-26 15:29:04 +0200922
923 return LY_SUCCESS;
924}
925
926LY_ERR
David Sedlák4a650532019-07-10 11:55:18 +0200927lysp_check_stringchar(struct lys_parser_ctx *ctx, unsigned int c)
928{
929 if (!is_yangutf8char(c)) {
930 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
931 return LY_EVALID;
932 }
933 return LY_SUCCESS;
934}
935
936LY_ERR
937lysp_check_identifierchar(struct lys_parser_ctx *ctx, unsigned int c, int first, int *prefix)
938{
939 if (first || (prefix && (*prefix) == 1)) {
940 if (!is_yangidentstartchar(c)) {
941 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c'.", c);
942 return LY_EVALID;
943 }
944 if (prefix) {
945 if (first) {
946 (*prefix) = 0;
947 } else {
948 (*prefix) = 2;
949 }
950 }
951 } else if (c == ':' && prefix && (*prefix) == 0) {
952 (*prefix) = 1;
953 } else if (!is_yangidentchar(c)) {
954 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c'.", c);
955 return LY_EVALID;
956 }
957
958 return LY_SUCCESS;
959}
960
961LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100962lysp_load_submodule(struct lys_parser_ctx *pctx, struct lysp_module *mod, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200963{
Michal Vaskob36053d2020-03-26 15:49:30 +0100964 struct ly_ctx *ctx = (struct ly_ctx *)(PARSER_CTX(pctx));
Radek Krejci3eb299d2019-04-08 15:07:44 +0200965 struct lysp_submodule *submod = NULL;
Radek Krejcid33273d2018-10-25 14:55:52 +0200966 const char *submodule_data = NULL;
967 LYS_INFORMAT format = LYS_IN_UNKNOWN;
968 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100969 struct lysp_load_module_check_data check_data = {0};
Michal Vasko63f3d842020-07-08 10:10:14 +0200970 struct ly_in *in;
Radek Krejcid33273d2018-10-25 14:55:52 +0200971
Radek Krejcibbe09a92018-11-08 09:36:54 +0100972 /* submodule not present in the context, get the input data and parse it */
Michal Vaskob36053d2020-03-26 15:49:30 +0100973 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200974search_clb:
Michal Vaskob36053d2020-03-26 15:49:30 +0100975 if (ctx->imp_clb) {
976 if (ctx->imp_clb(mod->mod->name, NULL, inc->name, inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
Radek Krejcibbe09a92018-11-08 09:36:54 +0100977 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200978 LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
Radek Krejcibbe09a92018-11-08 09:36:54 +0100979 check_data.name = inc->name;
980 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100981 check_data.submoduleof = mod->mod->name;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200982 lys_parse_mem_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, &submod);
Michal Vasko63f3d842020-07-08 10:10:14 +0200983 ly_in_free(in, 0);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100984 if (submodule_data_free) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100985 submodule_data_free((void*)submodule_data, ctx->imp_clb_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200986 }
987 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200988 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100989 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100990 goto search_file;
Radek Krejcid33273d2018-10-25 14:55:52 +0200991 }
Radek Krejci2d31ea72018-10-25 15:46:42 +0200992 } else {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100993search_file:
Michal Vaskob36053d2020-03-26 15:49:30 +0100994 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100995 /* submodule was not received from the callback or there is no callback set */
Michal Vaskob36053d2020-03-26 15:49:30 +0100996 lys_module_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, 0, pctx, mod->mod->name, 1, (void**)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100997 }
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_clb;
1000 }
1001 }
1002 if (submod) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001003 if (!inc->rev[0] && (submod->latest_revision == 1)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01001004 /* update the latest_revision flag - here we have selected the latest available schema,
1005 * consider that even the callback provides correct latest revision */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001006 submod->latest_revision = 2;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001007 }
1008
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001009 inc->submodule = submod;
Radek Krejcid33273d2018-10-25 14:55:52 +02001010 }
1011 if (!inc->submodule) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001012 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001013 inc->name, mod->mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +02001014 return LY_EVALID;
1015 }
1016
1017 return LY_SUCCESS;
1018}
1019
Radek Krejci01342af2019-01-03 15:18:08 +01001020#define FIND_MODULE(TYPE, MOD) \
Radek Krejcice8c1592018-10-29 15:35:51 +01001021 TYPE *imp; \
Radek Krejci7f9b6512019-09-18 13:11:09 +02001022 if (!ly_strncmp((MOD)->mod->prefix, prefix, len)) { \
Radek Krejcice8c1592018-10-29 15:35:51 +01001023 /* it is the prefix of the module itself */ \
Radek Krejci0af46292019-01-11 16:02:31 +01001024 m = ly_ctx_get_module((MOD)->mod->ctx, (MOD)->mod->name, (MOD)->mod->revision); \
Radek Krejcice8c1592018-10-29 15:35:51 +01001025 } \
1026 /* search in imports */ \
Radek Krejcibbe09a92018-11-08 09:36:54 +01001027 if (!m) { \
1028 LY_ARRAY_FOR((MOD)->imports, TYPE, imp) { \
Radek Krejci7f9b6512019-09-18 13:11:09 +02001029 if (!ly_strncmp(imp->prefix, prefix, len)) { \
Radek Krejcibbe09a92018-11-08 09:36:54 +01001030 m = imp->module; \
1031 break; \
1032 } \
Radek Krejcice8c1592018-10-29 15:35:51 +01001033 } \
Radek Krejcibbe09a92018-11-08 09:36:54 +01001034 }
Radek Krejcice8c1592018-10-29 15:35:51 +01001035
Radek Krejcibbe09a92018-11-08 09:36:54 +01001036struct lysc_module *
Radek Krejcia3045382018-11-22 14:30:31 +01001037lysc_module_find_prefix(const struct lysc_module *mod, const char *prefix, size_t len)
Radek Krejci151a5b72018-10-19 14:21:44 +02001038{
Radek Krejcibbe09a92018-11-08 09:36:54 +01001039 const struct lys_module *m = NULL;
1040
Radek Krejci01342af2019-01-03 15:18:08 +01001041 FIND_MODULE(struct lysc_import, mod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001042 return m ? m->compiled : NULL;
Radek Krejcice8c1592018-10-29 15:35:51 +01001043}
Radek Krejci151a5b72018-10-19 14:21:44 +02001044
Radek Krejcibbe09a92018-11-08 09:36:54 +01001045struct lysp_module *
Radek Krejcia3045382018-11-22 14:30:31 +01001046lysp_module_find_prefix(const struct lysp_module *mod, const char *prefix, size_t len)
Radek Krejcice8c1592018-10-29 15:35:51 +01001047{
Radek Krejcibbe09a92018-11-08 09:36:54 +01001048 const struct lys_module *m = NULL;
1049
Radek Krejci01342af2019-01-03 15:18:08 +01001050 FIND_MODULE(struct lysp_import, mod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001051 return m ? m->parsed : NULL;
Radek Krejcice8c1592018-10-29 15:35:51 +01001052}
Radek Krejci151a5b72018-10-19 14:21:44 +02001053
Radek Krejcice8c1592018-10-29 15:35:51 +01001054struct lys_module *
Radek Krejcia3045382018-11-22 14:30:31 +01001055lys_module_find_prefix(const struct lys_module *mod, const char *prefix, size_t len)
Radek Krejcice8c1592018-10-29 15:35:51 +01001056{
Radek Krejcibbe09a92018-11-08 09:36:54 +01001057 const struct lys_module *m = NULL;
1058
Michal Vasko88a163b2019-12-18 12:11:44 +01001059 if (!prefix || (!strncmp(prefix, mod->prefix, len) && !mod->prefix[len])) {
1060 return (struct lys_module *)mod;
Radek Krejci73dead22019-07-11 16:46:16 +02001061 }
Radek Krejcice8c1592018-10-29 15:35:51 +01001062 if (mod->compiled) {
Radek Krejci01342af2019-01-03 15:18:08 +01001063 FIND_MODULE(struct lysc_import, mod->compiled);
Radek Krejcice8c1592018-10-29 15:35:51 +01001064 } else {
Radek Krejci01342af2019-01-03 15:18:08 +01001065 FIND_MODULE(struct lysp_import, mod->parsed);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001066 }
1067 return (struct lys_module*)m;
1068}
1069
Radek Krejcia3045382018-11-22 14:30:31 +01001070const char *
Radek Krejci693262f2019-04-29 15:23:20 +02001071lys_prefix_find_module(const struct lys_module *mod, const struct lys_module *import)
1072{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001073 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001074
1075 if (import == mod) {
1076 return mod->prefix;
1077 }
1078
1079 if (mod->parsed) {
1080 LY_ARRAY_FOR(mod->parsed->imports, u) {
1081 if (mod->parsed->imports[u].module == import) {
1082 return mod->parsed->imports[u].prefix;
1083 }
1084 }
1085 } else {
1086 /* we don't have original information about the import's prefix,
1087 * so the prefix of the import module itself is returned instead */
1088 return import->prefix;
1089 }
1090
1091 return NULL;
1092}
1093
Radek Krejci0935f412019-08-20 16:15:18 +02001094API const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001095lys_nodetype2str(uint16_t nodetype)
1096{
1097 switch(nodetype) {
1098 case LYS_CONTAINER:
1099 return "container";
1100 case LYS_CHOICE:
1101 return "choice";
1102 case LYS_LEAF:
1103 return "leaf";
1104 case LYS_LEAFLIST:
1105 return "leaf-list";
1106 case LYS_LIST:
1107 return "list";
1108 case LYS_ANYXML:
1109 return "anyxml";
1110 case LYS_ANYDATA:
1111 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001112 case LYS_CASE:
1113 return "case";
Michal Vasko1bf09392020-03-27 12:38:10 +01001114 case LYS_RPC:
1115 return "RPC";
Radek Krejcif538ce52019-03-05 10:46:14 +01001116 case LYS_ACTION:
Michal Vasko1bf09392020-03-27 12:38:10 +01001117 return "action";
Radek Krejcif538ce52019-03-05 10:46:14 +01001118 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001119 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001120 case LYS_USES:
1121 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001122 default:
1123 return "unknown";
1124 }
1125}
1126
Radek Krejci693262f2019-04-29 15:23:20 +02001127const char *
1128lys_datatype2str(LY_DATA_TYPE basetype)
1129{
1130 switch(basetype) {
1131 case LY_TYPE_BINARY:
1132 return "binary";
1133 case LY_TYPE_UINT8:
1134 return "uint8";
1135 case LY_TYPE_UINT16:
1136 return "uint16";
1137 case LY_TYPE_UINT32:
1138 return "uint32";
1139 case LY_TYPE_UINT64:
1140 return "uint64";
1141 case LY_TYPE_STRING:
1142 return "string";
1143 case LY_TYPE_BITS:
1144 return "bits";
1145 case LY_TYPE_BOOL:
1146 return "boolean";
1147 case LY_TYPE_DEC64:
1148 return "decimal64";
1149 case LY_TYPE_EMPTY:
1150 return "empty";
1151 case LY_TYPE_ENUM:
1152 return "enumeration";
1153 case LY_TYPE_IDENT:
1154 return "identityref";
1155 case LY_TYPE_INST:
1156 return "instance-identifier";
1157 case LY_TYPE_LEAFREF:
1158 return "leafref";
1159 case LY_TYPE_UNION:
1160 return "union";
1161 case LY_TYPE_INT8:
1162 return "int8";
1163 case LY_TYPE_INT16:
1164 return "int16";
1165 case LY_TYPE_INT32:
1166 return "int32";
1167 case LY_TYPE_INT64:
1168 return "int64";
1169 default:
1170 return "unknown";
1171 }
1172}
1173
Radek Krejci056d0a82018-12-06 16:57:25 +01001174API const struct lysp_tpdf *
1175lysp_node_typedefs(const struct lysp_node *node)
1176{
Radek Krejci0fb28562018-12-13 15:17:37 +01001177 switch (node->nodetype) {
1178 case LYS_CONTAINER:
1179 return ((struct lysp_node_container*)node)->typedefs;
1180 case LYS_LIST:
1181 return ((struct lysp_node_list*)node)->typedefs;
1182 case LYS_GROUPING:
1183 return ((struct lysp_grp*)node)->typedefs;
Michal Vasko1bf09392020-03-27 12:38:10 +01001184 case LYS_RPC:
Radek Krejci0fb28562018-12-13 15:17:37 +01001185 case LYS_ACTION:
1186 return ((struct lysp_action*)node)->typedefs;
1187 case LYS_INOUT:
1188 return ((struct lysp_action_inout*)node)->typedefs;
1189 case LYS_NOTIF:
1190 return ((struct lysp_notif*)node)->typedefs;
1191 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001192 return NULL;
1193 }
1194}
1195
Radek Krejci53ea6152018-12-13 15:21:15 +01001196API const struct lysp_grp *
1197lysp_node_groupings(const struct lysp_node *node)
1198{
1199 switch (node->nodetype) {
1200 case LYS_CONTAINER:
1201 return ((struct lysp_node_container*)node)->groupings;
1202 case LYS_LIST:
1203 return ((struct lysp_node_list*)node)->groupings;
1204 case LYS_GROUPING:
1205 return ((struct lysp_grp*)node)->groupings;
Michal Vasko1bf09392020-03-27 12:38:10 +01001206 case LYS_RPC:
Radek Krejci53ea6152018-12-13 15:21:15 +01001207 case LYS_ACTION:
1208 return ((struct lysp_action*)node)->groupings;
1209 case LYS_INOUT:
1210 return ((struct lysp_action_inout*)node)->groupings;
1211 case LYS_NOTIF:
1212 return ((struct lysp_notif*)node)->groupings;
1213 default:
1214 return NULL;
1215 }
1216}
1217
Radek Krejcibbe09a92018-11-08 09:36:54 +01001218struct lysp_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001219lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001220{
1221 assert(node);
1222 switch (node->nodetype) {
1223 case LYS_CONTAINER:
1224 return &((struct lysp_node_container*)node)->actions;
1225 case LYS_LIST:
1226 return &((struct lysp_node_list*)node)->actions;
1227 case LYS_GROUPING:
1228 return &((struct lysp_grp*)node)->actions;
1229 case LYS_AUGMENT:
1230 return &((struct lysp_augment*)node)->actions;
1231 default:
1232 return NULL;
1233 }
1234}
1235
Radek Krejci056d0a82018-12-06 16:57:25 +01001236API const struct lysp_action *
1237lysp_node_actions(const struct lysp_node *node)
1238{
1239 struct lysp_action **actions;
1240 actions = lysp_node_actions_p((struct lysp_node*)node);
1241 if (actions) {
1242 return *actions;
1243 } else {
1244 return NULL;
1245 }
1246}
1247
Radek Krejcibbe09a92018-11-08 09:36:54 +01001248struct lysp_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001249lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001250{
1251 assert(node);
1252 switch (node->nodetype) {
1253 case LYS_CONTAINER:
1254 return &((struct lysp_node_container*)node)->notifs;
1255 case LYS_LIST:
1256 return &((struct lysp_node_list*)node)->notifs;
1257 case LYS_GROUPING:
1258 return &((struct lysp_grp*)node)->notifs;
1259 case LYS_AUGMENT:
1260 return &((struct lysp_augment*)node)->notifs;
1261 default:
1262 return NULL;
1263 }
1264}
1265
Radek Krejci056d0a82018-12-06 16:57:25 +01001266API const struct lysp_notif *
1267lysp_node_notifs(const struct lysp_node *node)
1268{
1269 struct lysp_notif **notifs;
1270 notifs = lysp_node_notifs_p((struct lysp_node*)node);
1271 if (notifs) {
1272 return *notifs;
1273 } else {
1274 return NULL;
1275 }
1276}
1277
Radek Krejcibbe09a92018-11-08 09:36:54 +01001278struct lysp_node **
Radek Krejci056d0a82018-12-06 16:57:25 +01001279lysp_node_children_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001280{
1281 assert(node);
1282 switch (node->nodetype) {
1283 case LYS_CONTAINER:
1284 return &((struct lysp_node_container*)node)->child;
1285 case LYS_CHOICE:
1286 return &((struct lysp_node_choice*)node)->child;
1287 case LYS_LIST:
1288 return &((struct lysp_node_list*)node)->child;
1289 case LYS_CASE:
1290 return &((struct lysp_node_case*)node)->child;
1291 case LYS_GROUPING:
1292 return &((struct lysp_grp*)node)->data;
1293 case LYS_AUGMENT:
1294 return &((struct lysp_augment*)node)->child;
1295 case LYS_INOUT:
1296 return &((struct lysp_action_inout*)node)->data;
1297 case LYS_NOTIF:
1298 return &((struct lysp_notif*)node)->data;
1299 default:
1300 return NULL;
1301 }
1302}
1303
Radek Krejci056d0a82018-12-06 16:57:25 +01001304API const struct lysp_node *
1305lysp_node_children(const struct lysp_node *node)
1306{
1307 struct lysp_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001308
1309 if (!node) {
1310 return NULL;
1311 }
1312
Radek Krejci056d0a82018-12-06 16:57:25 +01001313 children = lysp_node_children_p((struct lysp_node*)node);
1314 if (children) {
1315 return *children;
1316 } else {
1317 return NULL;
1318 }
1319}
1320
1321struct lysc_action **
1322lysc_node_actions_p(struct lysc_node *node)
1323{
1324 assert(node);
1325 switch (node->nodetype) {
1326 case LYS_CONTAINER:
1327 return &((struct lysc_node_container*)node)->actions;
1328 case LYS_LIST:
1329 return &((struct lysc_node_list*)node)->actions;
1330 default:
1331 return NULL;
1332 }
1333}
1334
1335API const struct lysc_action *
1336lysc_node_actions(const struct lysc_node *node)
1337{
1338 struct lysc_action **actions;
1339 actions = lysc_node_actions_p((struct lysc_node*)node);
1340 if (actions) {
1341 return *actions;
1342 } else {
1343 return NULL;
1344 }
1345}
1346
1347struct lysc_notif **
1348lysc_node_notifs_p(struct lysc_node *node)
1349{
1350 assert(node);
1351 switch (node->nodetype) {
1352 case LYS_CONTAINER:
1353 return &((struct lysc_node_container*)node)->notifs;
1354 case LYS_LIST:
1355 return &((struct lysc_node_list*)node)->notifs;
1356 default:
1357 return NULL;
1358 }
1359}
1360
1361API const struct lysc_notif *
1362lysc_node_notifs(const struct lysc_node *node)
1363{
1364 struct lysc_notif **notifs;
1365 notifs = lysc_node_notifs_p((struct lysc_node*)node);
1366 if (notifs) {
1367 return *notifs;
1368 } else {
1369 return NULL;
1370 }
1371}
1372
Radek Krejcibbe09a92018-11-08 09:36:54 +01001373struct lysc_node **
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001374lysc_node_children_p(const struct lysc_node *node, uint16_t flags)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001375{
1376 assert(node);
1377 switch (node->nodetype) {
1378 case LYS_CONTAINER:
1379 return &((struct lysc_node_container*)node)->child;
1380 case LYS_CHOICE:
Radek Krejcia3045382018-11-22 14:30:31 +01001381 if (((struct lysc_node_choice*)node)->cases) {
Radek Krejci95710c92019-02-11 15:49:55 +01001382 return &((struct lysc_node_choice*)node)->cases->child;
Radek Krejcia3045382018-11-22 14:30:31 +01001383 } else {
1384 return NULL;
1385 }
Radek Krejci01342af2019-01-03 15:18:08 +01001386 case LYS_CASE:
1387 return &((struct lysc_node_case*)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001388 case LYS_LIST:
1389 return &((struct lysc_node_list*)node)->child;
Michal Vasko1bf09392020-03-27 12:38:10 +01001390 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001391 case LYS_ACTION:
1392 if (flags & LYS_CONFIG_R) {
1393 return &((struct lysc_action*)node)->output.data;
1394 } else {
1395 /* LYS_CONFIG_W, but also the default case */
1396 return &((struct lysc_action*)node)->input.data;
1397 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02001398 case LYS_NOTIF:
1399 return &((struct lysc_notif*)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001400 default:
1401 return NULL;
1402 }
1403}
1404
Radek Krejci056d0a82018-12-06 16:57:25 +01001405API const struct lysc_node *
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001406lysc_node_children(const struct lysc_node *node, uint16_t flags)
Radek Krejcia3045382018-11-22 14:30:31 +01001407{
Radek Krejci056d0a82018-12-06 16:57:25 +01001408 struct lysc_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001409
1410 if (!node) {
1411 return NULL;
1412 }
1413
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001414 children = lysc_node_children_p((struct lysc_node*)node, flags);
Radek Krejci056d0a82018-12-06 16:57:25 +01001415 if (children) {
1416 return *children;
1417 } else {
Radek Krejcia3045382018-11-22 14:30:31 +01001418 return NULL;
1419 }
1420}
1421
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001422struct lys_module *
1423lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1424{
1425 unsigned int u;
1426
1427 for (u = 0; u < ctx->list.count; ++u) {
1428 if (((struct lys_module*)ctx->list.objs[u])->parsed == mod) {
1429 return ((struct lys_module*)ctx->list.objs[u]);
1430 }
1431 }
1432 return NULL;
1433}
1434
Radek Krejcid6b76452019-09-03 17:03:03 +02001435enum ly_stmt
Michal Vasko63f3d842020-07-08 10:10:14 +02001436lysp_match_kw(struct lys_yang_parser_ctx *ctx, struct ly_in *in)
David Sedlákc10e7902018-12-17 02:17:59 +01001437{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001438/**
Michal Vasko63f3d842020-07-08 10:10:14 +02001439 * @brief Move the INPUT by COUNT items. Also updates the indent value in yang parser context
David Sedlák1bccdfa2019-06-17 15:55:27 +02001440 * @param[in] CTX yang parser context to update its indent value.
Michal Vasko63f3d842020-07-08 10:10:14 +02001441 * @param[in,out] IN input to move
David Sedlák1bccdfa2019-06-17 15:55:27 +02001442 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
1443 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001444#define MOVE_IN(CTX, IN, COUNT) ly_in_skip(IN, COUNT);if(CTX){(CTX)->indent+=COUNT;}
1445#define IF_KW(STR, LEN, STMT) if (!strncmp(in->current, STR, LEN)) {MOVE_IN(ctx, in, LEN);*kw=STMT;}
1446#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 +02001447#define IF_KW_PREFIX_END }
David Sedlák572e7ab2019-06-04 16:01:58 +02001448
Michal Vasko63f3d842020-07-08 10:10:14 +02001449 const char *start = in->current;
Radek Krejcid6b76452019-09-03 17:03:03 +02001450 enum ly_stmt result = LY_STMT_NONE;
1451 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001452 /* read the keyword itself */
Michal Vasko63f3d842020-07-08 10:10:14 +02001453 switch (in->current[0]) {
David Sedlák23a59a62018-10-26 13:08:02 +02001454 case 'a':
Michal Vasko63f3d842020-07-08 10:10:14 +02001455 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001456 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1457 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1458 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001459 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001460 IF_KW("data", 4, LY_STMT_ANYDATA)
1461 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001462 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001463 break;
1464 case 'b':
Michal Vasko63f3d842020-07-08 10:10:14 +02001465 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001466 IF_KW("ase", 3, LY_STMT_BASE)
1467 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1468 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001469 break;
1470 case 'c':
Michal Vasko63f3d842020-07-08 10:10:14 +02001471 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001472 IF_KW("ase", 3, LY_STMT_CASE)
1473 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001474 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001475 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001476 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001477 IF_KW("ct", 2, LY_STMT_CONTACT)
1478 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001479 IF_KW_PREFIX_END
1480 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001481 break;
1482 case 'd':
Michal Vasko63f3d842020-07-08 10:10:14 +02001483 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001484 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001485 IF_KW("fault", 5, LY_STMT_DEFAULT)
1486 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001487 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001488 IF_KW("e", 1, LY_STMT_DEVIATE)
1489 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001490 IF_KW_PREFIX_END
1491 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001492 break;
1493 case 'e':
Michal Vasko63f3d842020-07-08 10:10:14 +02001494 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001495 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001496 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001497 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1498 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001499 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001500 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001501 break;
1502 case 'f':
Michal Vasko63f3d842020-07-08 10:10:14 +02001503 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001504 IF_KW("eature", 6, LY_STMT_FEATURE)
1505 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001506 break;
1507 case 'g':
Michal Vasko63f3d842020-07-08 10:10:14 +02001508 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001509 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001510 break;
1511 case 'i':
Michal Vasko63f3d842020-07-08 10:10:14 +02001512 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001513 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1514 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1515 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001516 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001517 IF_KW("clude", 5, LY_STMT_INCLUDE)
1518 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001519 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001520 break;
1521 case 'k':
Michal Vasko63f3d842020-07-08 10:10:14 +02001522 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001523 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001524 break;
1525 case 'l':
Michal Vasko63f3d842020-07-08 10:10:14 +02001526 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001527 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001528 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1529 else IF_KW("af", 2, LY_STMT_LEAF)
1530 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001531 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001532 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001533 break;
1534 case 'm':
Michal Vasko63f3d842020-07-08 10:10:14 +02001535 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001536 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001537 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1538 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001539 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001540 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1541 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001542 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001543 IF_KW("ule", 3, LY_STMT_MODULE)
1544 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001545 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001546 break;
1547 case 'n':
Michal Vasko63f3d842020-07-08 10:10:14 +02001548 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001549 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1550 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001551 break;
1552 case 'o':
Michal Vasko63f3d842020-07-08 10:10:14 +02001553 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001554 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001555 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1556 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001557 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001558 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001559 break;
1560 case 'p':
Michal Vasko63f3d842020-07-08 10:10:14 +02001561 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001562 IF_KW("ath", 3, LY_STMT_PATH)
1563 else IF_KW("attern", 6, LY_STMT_PATTERN)
1564 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001565 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001566 IF_KW("fix", 3, LY_STMT_PREFIX)
1567 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001568 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001569 break;
1570 case 'r':
Michal Vasko63f3d842020-07-08 10:10:14 +02001571 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001572 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001573 else IF_KW_PREFIX("e", 1)
1574 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001575 IF_KW("erence", 6, LY_STMT_REFERENCE)
1576 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001577 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001578 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1579 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1580 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001581 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001582 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001583 break;
1584 case 's':
Michal Vasko63f3d842020-07-08 10:10:14 +02001585 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001586 IF_KW("tatus", 5, LY_STMT_STATUS)
1587 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001588 break;
1589 case 't':
Michal Vasko63f3d842020-07-08 10:10:14 +02001590 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001591 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1592 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001593 break;
1594 case 'u':
Michal Vasko63f3d842020-07-08 10:10:14 +02001595 MOVE_IN(ctx, in, 1);
David Sedlák1bccdfa2019-06-17 15:55:27 +02001596 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001597 IF_KW("que", 3, LY_STMT_UNIQUE)
1598 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001599 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001600 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001601 break;
1602 case 'v':
Michal Vasko63f3d842020-07-08 10:10:14 +02001603 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001604 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001605 break;
1606 case 'w':
Michal Vasko63f3d842020-07-08 10:10:14 +02001607 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001608 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001609 break;
1610 case 'y':
Michal Vasko63f3d842020-07-08 10:10:14 +02001611 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001612 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
1613 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001614 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001615 default:
David Sedlák1bccdfa2019-06-17 15:55:27 +02001616 /* if context is not NULL we are matching keyword from YANG data*/
1617 if (ctx) {
Michal Vasko63f3d842020-07-08 10:10:14 +02001618 if (in->current[0] == ';') {
1619 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001620 *kw = LY_STMT_SYNTAX_SEMICOLON;
Michal Vasko63f3d842020-07-08 10:10:14 +02001621 } else if (in->current[0] == '{') {
1622 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001623 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001624 } else if (in->current[0] == '}') {
1625 MOVE_IN(ctx, in, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001626 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001627 }
1628 }
David Sedlák23a59a62018-10-26 13:08:02 +02001629 break;
1630 }
1631
Michal Vasko63f3d842020-07-08 10:10:14 +02001632 if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
Radek Krejci6e546bf2020-05-19 16:16:19 +02001633 /* the keyword is not terminated */
1634 *kw = LY_STMT_NONE;
Michal Vasko63f3d842020-07-08 10:10:14 +02001635 in->current = start;
Radek Krejci6e546bf2020-05-19 16:16:19 +02001636 }
1637
David Sedlák1bccdfa2019-06-17 15:55:27 +02001638#undef IF_KW
1639#undef IF_KW_PREFIX
1640#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01001641#undef MOVE_IN
David Sedlák18730132019-03-15 15:51:34 +01001642
David Sedlák1bccdfa2019-06-17 15:55:27 +02001643 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02001644}
David Sedlákecf5eb82019-06-03 14:12:44 +02001645
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001646LY_ARRAY_COUNT_TYPE
1647lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, LYEXT_SUBSTMT substmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001648{
1649 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1650
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001651 for (; index < LY_ARRAY_COUNT(ext); index++) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001652 if (ext[index].insubstmt == substmt) {
1653 return index;
1654 }
1655 }
1656
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001657 return LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001658}
1659
Radek Krejcia1911222019-07-22 17:24:50 +02001660/**
1661 * @brief Schema mapping of YANG modules to prefixes in values.
1662 *
1663 * Implementation of ly_clb_get_prefix. Inverse function to lys_resolve_prefix.
1664 *
1665 * In this case the @p mod is searched in the list of imports and the import's prefix
1666 * (not the module's itself) prefix is returned.
1667 */
1668const char *
1669lys_get_prefix(const struct lys_module *mod, void *private)
1670{
1671 struct lys_module *context_mod = (struct lys_module*)private;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001672 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001673
Radek Krejci474f9b82019-07-24 11:36:37 +02001674 if (context_mod == mod) {
1675 return context_mod->prefix;
1676 }
Radek Krejcia1911222019-07-22 17:24:50 +02001677 LY_ARRAY_FOR(context_mod->compiled->imports, u) {
1678 if (context_mod->compiled->imports[u].module == mod) {
1679 /* match */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02001680 return context_mod->compiled->imports[u].prefix;
Radek Krejcia1911222019-07-22 17:24:50 +02001681 }
1682 }
1683
1684 return NULL;
1685}
1686
1687/**
1688 * @brief Schema mapping of prefix in values to YANG modules (imports).
1689 *
1690 * Implementation of ly_clb_resolve_prefix. Inverse function to lys_get_prefix().
1691 *
1692 * In this case the @p prefix is searched in the list of imports' prefixes (not the prefixes of the imported modules themselves).
1693 */
1694const struct lys_module *
Michal Vasko52927e22020-03-16 17:26:14 +01001695lys_resolve_prefix(const struct ly_ctx *UNUSED(ctx), const char *prefix, size_t prefix_len, void *private)
Radek Krejcia1911222019-07-22 17:24:50 +02001696{
1697 return lys_module_find_prefix((const struct lys_module*)private, prefix, prefix_len);
1698}
Michal Vasko62ed12d2020-05-21 10:08:25 +02001699
1700const struct lysc_node *
1701lysc_data_parent(const struct lysc_node *schema)
1702{
1703 const struct lysc_node *parent;
1704
1705 for (parent = schema->parent; parent && (parent->nodetype & (LYS_CHOICE | LYS_CASE)); parent = parent->parent);
1706
1707 return parent;
1708}
Michal Vasko00cbf532020-06-15 13:58:47 +02001709
1710int
1711lysc_is_output(const struct lysc_node *schema)
1712{
1713 const struct lysc_node *parent;
1714
1715 assert(schema);
1716
1717 for (parent = schema->parent; parent && !(parent->nodetype & (LYS_RPC | LYS_ACTION)); parent = parent->parent);
1718 if (parent && (schema->flags & LYS_CONFIG_R)) {
1719 return 1;
1720 }
1721 return 0;
1722}
Michal Vaskod975f862020-06-23 13:29:28 +02001723
1724API int
1725lysc_is_userordered(const struct lysc_node *schema)
1726{
1727 if (!schema || !(schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) || !(schema->flags & LYS_ORDBY_USER)) {
1728 return 0;
1729 }
1730
1731 return 1;
1732}