blob: 4bf97aa9810421cd9319e1df5600bd82d6ff6757 [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 Krejci9ed7a192018-10-31 16:23:51 +010014#include "common.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020015
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <assert.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020017#include <ctype.h>
Radek Krejci9ed7a192018-10-31 16:23:51 +010018#include <errno.h>
19#include <fcntl.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020020#include <limits.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <stdint.h>
Radek Krejci9ed7a192018-10-31 16:23:51 +010022#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <string.h>
Radek Krejci9ed7a192018-10-31 16:23:51 +010024#include <unistd.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020025#include <time.h>
26
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "context.h"
28#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020029#include "hash_table.h"
30#include "log.h"
Radek Krejci0935f412019-08-20 16:15:18 +020031#include "plugins_exts.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 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +010098 if (context_node && context_node->nodetype == LYS_ACTION) {
99 /* 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:
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100111 context_node = lys_child(context_node, mod, name, name_len, 0,
112 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)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200160 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE,
161 "Prefix \"%s\" already used as module prefix.", *value);
162 return LY_EEXIST;
163 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100164 LY_ARRAY_FOR(imports, struct lysp_import, i) {
165 if (i->prefix && &i->prefix != value && !strcmp(i->prefix, *value)) {
166 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.",
167 *value, i->name);
168 return LY_EEXIST;
Radek Krejci86d106e2018-10-18 09:53:19 +0200169 }
170 }
171 return LY_SUCCESS;
172}
173
174LY_ERR
Radek Krejci4f28eda2018-11-12 11:46:16 +0100175lysc_check_status(struct lysc_ctx *ctx,
176 uint16_t flags1, void *mod1, const char *name1,
177 uint16_t flags2, void *mod2, const char *name2)
178{
179 uint16_t flg1, flg2;
180
181 flg1 = (flags1 & LYS_STATUS_MASK) ? (flags1 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
182 flg2 = (flags2 & LYS_STATUS_MASK) ? (flags2 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
183
184 if ((flg1 < flg2) && (mod1 == mod2)) {
185 if (ctx) {
186 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
187 "A %s definition \"%s\" is not allowed to reference %s definition \"%s\".",
188 flg1 == LYS_STATUS_CURR ? "current" : "deprecated", name1,
189 flg2 == LYS_STATUS_OBSLT ? "obsolete" : "deprecated", name2);
190 }
191 return LY_EVALID;
192 }
193
194 return LY_SUCCESS;
195}
196
197LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200198lysp_check_date(struct lys_parser_ctx *ctx, const char *date, int date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +0200199{
200 int i;
201 struct tm tm, tm_;
202 char *r;
203
Radek Krejcibbe09a92018-11-08 09:36:54 +0100204 LY_CHECK_ARG_RET(ctx ? ctx->ctx : NULL, date, LY_EINVAL);
205 LY_CHECK_ERR_RET(date_len != LY_REV_SIZE - 1, LOGARG(ctx ? ctx->ctx : NULL, date_len), LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200206
207 /* check format */
208 for (i = 0; i < date_len; i++) {
209 if (i == 4 || i == 7) {
210 if (date[i] != '-') {
211 goto error;
212 }
213 } else if (!isdigit(date[i])) {
214 goto error;
215 }
216 }
217
218 /* check content, e.g. 2018-02-31 */
219 memset(&tm, 0, sizeof tm);
220 r = strptime(date, "%Y-%m-%d", &tm);
221 if (!r || r != &date[LY_REV_SIZE - 1]) {
222 goto error;
223 }
224 memcpy(&tm_, &tm, sizeof tm);
225 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
226 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
227 /* checking days is enough, since other errors
228 * have been checked by strptime() */
229 goto error;
230 }
231
232 return LY_SUCCESS;
233
234error:
Radek Krejcid33273d2018-10-25 14:55:52 +0200235 if (stmt) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100236 if (ctx) {
237 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LY_VCODE_INVAL, date_len, date, stmt);
238 } else {
239 LOGVAL(NULL, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, date_len, date, stmt);
240 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200241 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200242 return LY_EINVAL;
243}
244
245void
246lysp_sort_revisions(struct lysp_revision *revs)
247{
248 uint8_t i, r;
249 struct lysp_revision rev;
250
251 for (i = 1, r = 0; revs && i < LY_ARRAY_SIZE(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200252 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200253 r = i;
254 }
255 }
256
257 if (r) {
258 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200259 memcpy(&rev, &revs[0], sizeof rev);
260 memcpy(&revs[0], &revs[r], sizeof rev);
261 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200262 }
263}
Radek Krejci151a5b72018-10-19 14:21:44 +0200264
Radek Krejcibbe09a92018-11-08 09:36:54 +0100265static const struct lysp_tpdf *
266lysp_type_match(const char *name, struct lysp_node *node)
267{
Radek Krejci0fb28562018-12-13 15:17:37 +0100268 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100269 unsigned int u;
270
Radek Krejci0fb28562018-12-13 15:17:37 +0100271 typedefs = lysp_node_typedefs(node);
272 LY_ARRAY_FOR(typedefs, u) {
273 if (!strcmp(name, typedefs[u].name)) {
274 /* match */
275 return &typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100276 }
277 }
278
279 return NULL;
280}
281
Radek Krejci4f28eda2018-11-12 11:46:16 +0100282static LY_DATA_TYPE
283lysp_type_str2builtin(const char *name, size_t len)
284{
285 if (len >= 4) { /* otherwise it does not match any built-in type */
286 if (name[0] == 'b') {
287 if (name[1] == 'i') {
288 if (len == 6 && !strncmp(&name[2], "nary", 4)) {
289 return LY_TYPE_BINARY;
290 } else if (len == 4 && !strncmp(&name[2], "ts", 2)) {
291 return LY_TYPE_BITS;
292 }
293 } else if (len == 7 && !strncmp(&name[1], "oolean", 6)) {
294 return LY_TYPE_BOOL;
295 }
296 } else if (name[0] == 'd') {
297 if (len == 9 && !strncmp(&name[1], "ecimal64", 8)) {
298 return LY_TYPE_DEC64;
299 }
300 } else if (name[0] == 'e') {
301 if (len == 5 && !strncmp(&name[1], "mpty", 4)) {
302 return LY_TYPE_EMPTY;
303 } else if (len == 11 && !strncmp(&name[1], "numeration", 10)) {
304 return LY_TYPE_ENUM;
305 }
306 } else if (name[0] == 'i') {
307 if (name[1] == 'n') {
308 if (len == 4 && !strncmp(&name[2], "t8", 2)) {
309 return LY_TYPE_INT8;
310 } else if (len == 5) {
311 if (!strncmp(&name[2], "t16", 3)) {
312 return LY_TYPE_INT16;
313 } else if (!strncmp(&name[2], "t32", 3)) {
314 return LY_TYPE_INT32;
315 } else if (!strncmp(&name[2], "t64", 3)) {
316 return LY_TYPE_INT64;
317 }
318 } else if (len == 19 && !strncmp(&name[2], "stance-identifier", 17)) {
319 return LY_TYPE_INST;
320 }
321 } else if (len == 11 && !strncmp(&name[1], "dentityref", 10)) {
322 return LY_TYPE_IDENT;
323 }
324 } else if (name[0] == 'l') {
325 if (len == 7 && !strncmp(&name[1], "eafref", 6)) {
326 return LY_TYPE_LEAFREF;
327 }
328 } else if (name[0] == 's') {
329 if (len == 6 && !strncmp(&name[1], "tring", 5)) {
330 return LY_TYPE_STRING;
331 }
332 } else if (name[0] == 'u') {
333 if (name[1] == 'n') {
334 if (len == 5 && !strncmp(&name[2], "ion", 3)) {
335 return LY_TYPE_UNION;
336 }
337 } else if (name[1] == 'i' && name[2] == 'n' && name[3] == 't') {
338 if (len == 5 && name[4] == '8') {
339 return LY_TYPE_UINT8;
340 } else if (len == 6) {
341 if (!strncmp(&name[4], "16", 2)) {
342 return LY_TYPE_UINT16;
343 } else if (!strncmp(&name[4], "32", 2)) {
344 return LY_TYPE_UINT32;
345 } else if (!strncmp(&name[4], "64", 2)) {
346 return LY_TYPE_UINT64;
347 }
348 }
349 }
350 }
351 }
352
353 return LY_TYPE_UNKNOWN;
354}
355
Radek Krejcibbe09a92018-11-08 09:36:54 +0100356LY_ERR
357lysp_type_find(const char *id, struct lysp_node *start_node, struct lysp_module *start_module,
Radek Krejci4f28eda2018-11-12 11:46:16 +0100358 LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node, struct lysp_module **module)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100359{
360 const char *str, *name;
361 struct lysp_tpdf *typedefs;
362 unsigned int u, v;
363
364 assert(id);
365 assert(start_module);
366 assert(tpdf);
367 assert(node);
368 assert(module);
369
Radek Krejci4f28eda2018-11-12 11:46:16 +0100370 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100371 str = strchr(id, ':');
372 if (str) {
373 *module = lysp_module_find_prefix(start_module, id, str - id);
374 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])) {
441 LOGWRN(ctx->ctx, "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
442 name_len, name, u + 1);
443 break;
444 }
445 }
446 }
447
448 return LY_SUCCESS;
449}
450
Radek Krejcibbe09a92018-11-08 09:36:54 +0100451/*
452 * @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 Krejcibbe09a92018-11-08 09:36:54 +0100465 struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped)
466{
467 struct lysp_node *parent;
468 uint32_t hash;
469 size_t name_len;
470 const char *name;
471 unsigned int 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)) {
481 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
482 "Invalid name \"%s\" of typedef - name collision with a built-in type.", name);
483 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100484 }
485
486 /* check locally scoped typedefs (avoid name shadowing) */
487 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100488 typedefs = lysp_node_typedefs(node);
489 LY_ARRAY_FOR(typedefs, u) {
490 if (&typedefs[u] == tpdf) {
491 break;
492 }
493 if (!strcmp(name, typedefs[u].name)) {
494 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
495 "Invalid name \"%s\" of typedef - name collision with sibling type.", name);
496 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100497 }
498 }
499 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200500 for (parent = node->parent; parent; parent = parent->parent) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100501 if (lysp_type_match(name, parent)) {
502 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
503 "Invalid name \"%s\" of typedef - name collision with another scoped type.", name);
504 return LY_EEXIST;
505 }
506 }
507 }
508
509 /* check collision with the top-level typedefs */
510 hash = dict_hash(name, name_len);
511 if (node) {
512 lyht_insert(tpdfs_scoped, &name, hash, NULL);
513 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
514 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
515 "Invalid name \"%s\" of typedef - scoped type collide with a top-level type.", name);
516 return LY_EEXIST;
517 }
518 } else {
519 if (lyht_insert(tpdfs_global, &name, hash, NULL)) {
520 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
521 "Invalid name \"%s\" of typedef - name collision with another top-level type.", name);
522 return LY_EEXIST;
523 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100524 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
525 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
526 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100527 }
528
529 return LY_SUCCESS;
530}
531
532static int
533lysp_id_cmp(void *val1, void *val2, int UNUSED(mod), void *UNUSED(cb_data))
534{
535 return !strcmp(val1, val2);
536}
537
538LY_ERR
David Sedlákd2ebe572019-07-22 12:53:14 +0200539lysp_parse_finalize_reallocated(struct lys_parser_ctx *ctx, struct lysp_grp *groupings, struct lysp_augment *augments,
540 struct lysp_action *actions, struct lysp_notif *notifs)
541{
542 unsigned int u, v;
543 struct lysp_node *child;
544
545 /* finalize parent pointers to the reallocated items */
546
547 /* gropings */
548 LY_ARRAY_FOR(groupings, u) {
549 LY_LIST_FOR(groupings[u].data, child) {
550 child->parent = (struct lysp_node*)&groupings[u];
551 }
552 LY_ARRAY_FOR(groupings[u].actions, v) {
553 groupings[u].actions[v].parent = (struct lysp_node*)&groupings[u];
554 }
555 LY_ARRAY_FOR(groupings[u].notifs, v) {
556 groupings[u].notifs[v].parent = (struct lysp_node*)&groupings[u];
557 }
558 LY_ARRAY_FOR(groupings[u].groupings, v) {
559 groupings[u].groupings[v].parent = (struct lysp_node*)&groupings[u];
560 }
561 if (groupings[u].typedefs) {
562 ly_set_add(&ctx->tpdfs_nodes, &groupings[u], 0);
563 }
564 }
565
566 /* augments */
567 LY_ARRAY_FOR(augments, u) {
568 LY_LIST_FOR(augments[u].child, child) {
569 child->parent = (struct lysp_node*)&augments[u];
570 }
571 LY_ARRAY_FOR(augments[u].actions, v) {
572 augments[u].actions[v].parent = (struct lysp_node*)&augments[u];
573 }
574 LY_ARRAY_FOR(augments[u].notifs, v) {
575 augments[u].notifs[v].parent = (struct lysp_node*)&augments[u];
576 }
577 }
578
579 /* actions */
580 LY_ARRAY_FOR(actions, u) {
581 if (actions[u].input.parent) {
582 actions[u].input.parent = (struct lysp_node*)&actions[u];
583 LY_LIST_FOR(actions[u].input.data, child) {
584 child->parent = (struct lysp_node*)&actions[u].input;
585 }
586 LY_ARRAY_FOR(actions[u].input.groupings, v) {
587 actions[u].input.groupings[v].parent = (struct lysp_node*)&actions[u].input;
588 }
589 if (actions[u].input.typedefs) {
590 ly_set_add(&ctx->tpdfs_nodes, &actions[u].input, 0);
591 }
592 }
593 if (actions[u].output.parent) {
594 actions[u].output.parent = (struct lysp_node*)&actions[u];
595 LY_LIST_FOR(actions[u].output.data, child) {
596 child->parent = (struct lysp_node*)&actions[u].output;
597 }
598 LY_ARRAY_FOR(actions[u].output.groupings, v) {
599 actions[u].output.groupings[v].parent = (struct lysp_node*)&actions[u].output;
600 }
601 if (actions[u].output.typedefs) {
602 ly_set_add(&ctx->tpdfs_nodes, &actions[u].output, 0);
603 }
604 }
605 LY_ARRAY_FOR(actions[u].groupings, v) {
606 actions[u].groupings[v].parent = (struct lysp_node*)&actions[u];
607 }
608 if (actions[u].typedefs) {
609 ly_set_add(&ctx->tpdfs_nodes, &actions[u], 0);
610 }
611 }
612
613 /* notifications */
614 LY_ARRAY_FOR(notifs, u) {
615 LY_LIST_FOR(notifs[u].data, child) {
616 child->parent = (struct lysp_node*)&notifs[u];
617 }
618 LY_ARRAY_FOR(notifs[u].groupings, v) {
619 notifs[u].groupings[v].parent = (struct lysp_node*)&notifs[u];
620 }
621 if (notifs[u].typedefs) {
622 ly_set_add(&ctx->tpdfs_nodes, &notifs[u], 0);
623 }
624 }
625
626 return LY_SUCCESS;
627}
628
629LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200630lysp_check_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100631{
632 struct hash_table *ids_global;
633 struct hash_table *ids_scoped;
Radek Krejci0fb28562018-12-13 15:17:37 +0100634 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100635 unsigned int i, u;
636 LY_ERR ret = LY_EVALID;
637
638 /* check name collisions - typedefs and groupings */
639 ids_global = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1);
640 ids_scoped = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100641 LY_ARRAY_FOR(mod->typedefs, i) {
642 if (lysp_check_typedef(ctx, NULL, &mod->typedefs[i], ids_global, ids_scoped)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100643 goto cleanup;
644 }
645 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100646 LY_ARRAY_FOR(mod->includes, i) {
647 LY_ARRAY_FOR(mod->includes[i].submodule->typedefs, u) {
648 if (lysp_check_typedef(ctx, NULL, &mod->includes[i].submodule->typedefs[u], ids_global, ids_scoped)) {
Radek Krejci3b1f9292018-11-08 10:58:35 +0100649 goto cleanup;
650 }
651 }
652 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100653 for (u = 0; u < ctx->tpdfs_nodes.count; ++u) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100654 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[u]);
655 LY_ARRAY_FOR(typedefs, i) {
656 if (lysp_check_typedef(ctx, (struct lysp_node *)ctx->tpdfs_nodes.objs[u], &typedefs[i], ids_global, ids_scoped)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100657 goto cleanup;
658 }
659 }
660 }
661 ret = LY_SUCCESS;
662cleanup:
663 lyht_free(ids_global);
664 lyht_free(ids_scoped);
665 ly_set_erase(&ctx->tpdfs_nodes, NULL);
666
667 return ret;
668}
669
Radek Krejci9ed7a192018-10-31 16:23:51 +0100670struct lysp_load_module_check_data {
671 const char *name;
672 const char *revision;
673 const char *path;
674 const char* submoduleof;
675};
676
677static LY_ERR
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100678lysp_load_module_check(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100679{
680 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100681 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200682 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100683 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100684 struct lysp_revision *revs;
685
686 name = mod ? mod->mod->name : submod->name;
687 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200688 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100689
690 if (info->name) {
691 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100692 if (strcmp(info->name, name)) {
693 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100694 return LY_EINVAL;
695 }
696 }
697 if (info->revision) {
698 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100699 if (!revs || strcmp(info->revision, revs[0].date)) {
700 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Radek Krejcib07b5c92019-04-08 10:56:37 +0200701 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100702 return LY_EINVAL;
703 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200704 } else if (!latest_revision) {
705 /* do not log, we just need to drop the schema and use the latest revision from the context */
706 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100707 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100708 if (submod) {
709 assert(info->submoduleof);
710
Radek Krejci9ed7a192018-10-31 16:23:51 +0100711 /* check that the submodule belongs-to our module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100712 if (strcmp(info->submoduleof, submod->belongsto)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100713 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 +0100714 submod->name, info->submoduleof, submod->belongsto);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100715 return LY_EVALID;
716 }
717 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100718 if (submod->parsing) {
719 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100720 return LY_EVALID;
721 }
722 }
723 if (info->path) {
724 /* check that name and revision match filename */
725 filename = strrchr(info->path, '/');
726 if (!filename) {
727 filename = info->path;
728 } else {
729 filename++;
730 }
731 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100732 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100733 rev = strchr(filename, '@');
734 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100735 if (strncmp(filename, name, len) ||
Radek Krejci9ed7a192018-10-31 16:23:51 +0100736 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100737 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100738 }
739 /* revision */
740 if (rev) {
741 len = dot - ++rev;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100742 if (!revs || len != 10 || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100743 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100744 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100745 }
746 }
747 }
748 return LY_SUCCESS;
749}
750
751LY_ERR
fredgancd485b82019-10-18 15:00:17 +0800752lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, int implement,
Radek Krejci78f06822019-10-30 12:54:05 +0100753 struct lys_parser_ctx *main_ctx, const char *main_name, int required, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100754{
755 int fd;
756 char *filepath = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100757 const char **fp;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100758 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100759 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100760 LY_ERR ret = LY_SUCCESS;
761 struct lysp_load_module_check_data check_data = {0};
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100762 char rpath[PATH_MAX];
Radek Krejci9ed7a192018-10-31 16:23:51 +0100763
764 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, revision,
765 &filepath, &format));
Radek Krejci78f06822019-10-30 12:54:05 +0100766 LY_CHECK_ERR_RET(!filepath, if (required) {LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.",
767 name, revision ? "@" : "", revision ? revision : "");}, LY_ENOTFOUND);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100768
769 LOGVRB("Loading schema from \"%s\" file.", filepath);
770
771 /* open the file */
772 fd = open(filepath, O_RDONLY);
773 LY_CHECK_ERR_GOTO(fd < 0, LOGERR(ctx, LY_ESYS, "Unable to open data model file \"%s\" (%s).",
774 filepath, strerror(errno)); ret = LY_ESYS, cleanup);
775
776 check_data.name = name;
777 check_data.revision = revision;
778 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800779 check_data.submoduleof = main_name;
Radek Krejci3b1f9292018-11-08 10:58:35 +0100780 mod = lys_parse_fd_(ctx, fd, format, implement, main_ctx,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100781 lysp_load_module_check, &check_data);
782 close(fd);
783 LY_CHECK_ERR_GOTO(!mod, ly_errcode(ctx), cleanup);
784
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100785 if (main_ctx) {
786 fp = &((struct lysp_submodule*)mod)->filepath;
787 } else {
788 fp = &((struct lys_module*)mod)->filepath;
789 }
790 if (!(*fp)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100791 if (realpath(filepath, rpath) != NULL) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100792 (*fp) = lydict_insert(ctx, rpath, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100793 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100794 (*fp) = lydict_insert(ctx, filepath, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100795 }
796 }
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
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100807lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, int implement, int require_parsed, struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200808{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100809 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200810 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100811 void (*module_data_free)(void *module_data, void *user_data) = NULL;
812 struct lysp_load_module_check_data check_data = {0};
Radek Krejcib3289d62019-09-18 12:21:39 +0200813 struct lys_module *m = NULL;
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) {
857 check_data.name = name;
858 check_data.revision = revision;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100859 *mod = lys_parse_mem_module(ctx, module_data, format, implement,
860 lysp_load_module_check, &check_data);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100861 if (module_data_free) {
862 module_data_free((void*)module_data, ctx->imp_clb_data);
863 }
Radek Krejci096235c2019-01-11 11:12:19 +0100864 if (*mod && implement && lys_compile(*mod, 0)) {
865 ly_set_rm(&ctx->list, *mod, NULL);
866 lys_module_free(*mod, NULL);
867 *mod = NULL;
868 }
Radek Krejci086c7132018-10-26 15:29:04 +0200869 }
870 }
871 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
872 goto search_file;
873 }
874 } else {
875search_file:
876 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
877 /* module was not received from the callback or there is no callback set */
Radek Krejci78f06822019-10-30 12:54:05 +0100878 lys_module_localfile(ctx, name, revision, implement, NULL, NULL, m ? 0 : 1, (void **)mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200879 }
880 if (!(*mod) && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
881 goto search_clb;
882 }
883 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100884
Radek Krejcib3289d62019-09-18 12:21:39 +0200885 /* update the latest_revision flag - here we have selected the latest available schema,
886 * consider that even the callback provides correct latest revision */
887 if (!(*mod) && m) {
Radek Krejci78f06822019-10-30 12:54:05 +0100888 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 +0200889 m->latest_revision = 2;
890 *mod = m;
891 } else if ((*mod) && !revision && ((*mod)->latest_revision == 1)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100892 (*mod)->latest_revision = 2;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100893 }
Radek Krejci086c7132018-10-26 15:29:04 +0200894 } else {
895 /* we have module from the current context */
Radek Krejci0af46292019-01-11 16:02:31 +0100896 if (implement) {
897 m = ly_ctx_get_module_implemented(ctx, name);
898 if (m && m != *mod) {
899 /* check collision with other implemented revision */
900 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
901 "Module \"%s\" is already present in other implemented revision.", name);
902 *mod = NULL;
903 return LY_EDENIED;
904 }
Radek Krejci086c7132018-10-26 15:29:04 +0200905 }
906
907 /* circular check */
Radek Krejcif8f882a2018-10-31 14:51:15 +0100908 if ((*mod)->parsed && (*mod)->parsed->parsing) {
Radek Krejci086c7132018-10-26 15:29:04 +0200909 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
910 *mod = NULL;
911 return LY_EVALID;
912 }
913 }
914 if (!(*mod)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100915 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "%s \"%s\" module failed.", implement ? "Loading" : "Importing", name);
Radek Krejci086c7132018-10-26 15:29:04 +0200916 return LY_EVALID;
917 }
918
919 if (implement) {
920 /* mark the module implemented, check for collision was already done */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100921 (*mod)->implemented = 1;
Radek Krejci086c7132018-10-26 15:29:04 +0200922 }
Radek Krejci086c7132018-10-26 15:29:04 +0200923
924 return LY_SUCCESS;
925}
926
927LY_ERR
David Sedlák4a650532019-07-10 11:55:18 +0200928lysp_check_stringchar(struct lys_parser_ctx *ctx, unsigned int c)
929{
930 if (!is_yangutf8char(c)) {
931 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
932 return LY_EVALID;
933 }
934 return LY_SUCCESS;
935}
936
937LY_ERR
938lysp_check_identifierchar(struct lys_parser_ctx *ctx, unsigned int c, int first, int *prefix)
939{
940 if (first || (prefix && (*prefix) == 1)) {
941 if (!is_yangidentstartchar(c)) {
942 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c'.", c);
943 return LY_EVALID;
944 }
945 if (prefix) {
946 if (first) {
947 (*prefix) = 0;
948 } else {
949 (*prefix) = 2;
950 }
951 }
952 } else if (c == ':' && prefix && (*prefix) == 0) {
953 (*prefix) = 1;
954 } else if (!is_yangidentchar(c)) {
955 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c'.", c);
956 return LY_EVALID;
957 }
958
959 return LY_SUCCESS;
960}
961
962LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200963lysp_load_submodule(struct lys_parser_ctx *ctx, struct lysp_module *mod, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200964{
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};
Radek Krejcid33273d2018-10-25 14:55:52 +0200970
Radek Krejcibbe09a92018-11-08 09:36:54 +0100971 /* submodule not present in the context, get the input data and parse it */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100972 if (!(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200973search_clb:
Radek Krejci3b1f9292018-11-08 10:58:35 +0100974 if (ctx->ctx->imp_clb) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100975 if (ctx->ctx->imp_clb(mod->mod->name, NULL, inc->name, inc->rev[0] ? inc->rev : NULL, ctx->ctx->imp_clb_data,
Radek Krejcibbe09a92018-11-08 09:36:54 +0100976 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
977 check_data.name = inc->name;
978 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100979 check_data.submoduleof = mod->mod->name;
980 submod = lys_parse_mem_submodule(ctx->ctx, submodule_data, format, ctx,
981 lysp_load_module_check, &check_data);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100982 if (submodule_data_free) {
Radek Krejci3b1f9292018-11-08 10:58:35 +0100983 submodule_data_free((void*)submodule_data, ctx->ctx->imp_clb_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200984 }
985 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200986 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100987 if (!submod && !(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100988 goto search_file;
Radek Krejcid33273d2018-10-25 14:55:52 +0200989 }
Radek Krejci2d31ea72018-10-25 15:46:42 +0200990 } else {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100991search_file:
Radek Krejci3b1f9292018-11-08 10:58:35 +0100992 if (!(ctx->ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100993 /* submodule was not received from the callback or there is no callback set */
Radek Krejci78f06822019-10-30 12:54:05 +0100994 lys_module_localfile(ctx->ctx, inc->name, inc->rev[0] ? inc->rev : NULL, 0, ctx, mod->mod->name, 1, (void**)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100995 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100996 if (!submod && (ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100997 goto search_clb;
998 }
999 }
1000 if (submod) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001001 if (!inc->rev[0] && (submod->latest_revision == 1)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01001002 /* update the latest_revision flag - here we have selected the latest available schema,
1003 * consider that even the callback provides correct latest revision */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001004 submod->latest_revision = 2;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001005 }
1006
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001007 inc->submodule = submod;
Radek Krejcid33273d2018-10-25 14:55:52 +02001008 }
1009 if (!inc->submodule) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001010 LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.",
1011 inc->name, mod->mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +02001012 return LY_EVALID;
1013 }
1014
1015 return LY_SUCCESS;
1016}
1017
Radek Krejci01342af2019-01-03 15:18:08 +01001018#define FIND_MODULE(TYPE, MOD) \
Radek Krejcice8c1592018-10-29 15:35:51 +01001019 TYPE *imp; \
Radek Krejci7f9b6512019-09-18 13:11:09 +02001020 if (!ly_strncmp((MOD)->mod->prefix, prefix, len)) { \
Radek Krejcice8c1592018-10-29 15:35:51 +01001021 /* it is the prefix of the module itself */ \
Radek Krejci0af46292019-01-11 16:02:31 +01001022 m = ly_ctx_get_module((MOD)->mod->ctx, (MOD)->mod->name, (MOD)->mod->revision); \
Radek Krejcice8c1592018-10-29 15:35:51 +01001023 } \
1024 /* search in imports */ \
Radek Krejcibbe09a92018-11-08 09:36:54 +01001025 if (!m) { \
1026 LY_ARRAY_FOR((MOD)->imports, TYPE, imp) { \
Radek Krejci7f9b6512019-09-18 13:11:09 +02001027 if (!ly_strncmp(imp->prefix, prefix, len)) { \
Radek Krejcibbe09a92018-11-08 09:36:54 +01001028 m = imp->module; \
1029 break; \
1030 } \
Radek Krejcice8c1592018-10-29 15:35:51 +01001031 } \
Radek Krejcibbe09a92018-11-08 09:36:54 +01001032 }
Radek Krejcice8c1592018-10-29 15:35:51 +01001033
Radek Krejcibbe09a92018-11-08 09:36:54 +01001034struct lysc_module *
Radek Krejcia3045382018-11-22 14:30:31 +01001035lysc_module_find_prefix(const struct lysc_module *mod, const char *prefix, size_t len)
Radek Krejci151a5b72018-10-19 14:21:44 +02001036{
Radek Krejcibbe09a92018-11-08 09:36:54 +01001037 const struct lys_module *m = NULL;
1038
Radek Krejci01342af2019-01-03 15:18:08 +01001039 FIND_MODULE(struct lysc_import, mod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001040 return m ? m->compiled : NULL;
Radek Krejcice8c1592018-10-29 15:35:51 +01001041}
Radek Krejci151a5b72018-10-19 14:21:44 +02001042
Radek Krejcibbe09a92018-11-08 09:36:54 +01001043struct lysp_module *
Radek Krejcia3045382018-11-22 14:30:31 +01001044lysp_module_find_prefix(const struct lysp_module *mod, const char *prefix, size_t len)
Radek Krejcice8c1592018-10-29 15:35:51 +01001045{
Radek Krejcibbe09a92018-11-08 09:36:54 +01001046 const struct lys_module *m = NULL;
1047
Radek Krejci01342af2019-01-03 15:18:08 +01001048 FIND_MODULE(struct lysp_import, mod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001049 return m ? m->parsed : NULL;
Radek Krejcice8c1592018-10-29 15:35:51 +01001050}
Radek Krejci151a5b72018-10-19 14:21:44 +02001051
Radek Krejcice8c1592018-10-29 15:35:51 +01001052struct lys_module *
Radek Krejcia3045382018-11-22 14:30:31 +01001053lys_module_find_prefix(const struct lys_module *mod, const char *prefix, size_t len)
Radek Krejcice8c1592018-10-29 15:35:51 +01001054{
Radek Krejcibbe09a92018-11-08 09:36:54 +01001055 const struct lys_module *m = NULL;
1056
Michal Vasko88a163b2019-12-18 12:11:44 +01001057 if (!prefix || (!strncmp(prefix, mod->prefix, len) && !mod->prefix[len])) {
1058 return (struct lys_module *)mod;
Radek Krejci73dead22019-07-11 16:46:16 +02001059 }
Radek Krejcice8c1592018-10-29 15:35:51 +01001060 if (mod->compiled) {
Radek Krejci01342af2019-01-03 15:18:08 +01001061 FIND_MODULE(struct lysc_import, mod->compiled);
Radek Krejcice8c1592018-10-29 15:35:51 +01001062 } else {
Radek Krejci01342af2019-01-03 15:18:08 +01001063 FIND_MODULE(struct lysp_import, mod->parsed);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001064 }
1065 return (struct lys_module*)m;
1066}
1067
Radek Krejcia3045382018-11-22 14:30:31 +01001068const char *
Radek Krejci693262f2019-04-29 15:23:20 +02001069lys_prefix_find_module(const struct lys_module *mod, const struct lys_module *import)
1070{
1071 unsigned int u;
1072
1073 if (import == mod) {
1074 return mod->prefix;
1075 }
1076
1077 if (mod->parsed) {
1078 LY_ARRAY_FOR(mod->parsed->imports, u) {
1079 if (mod->parsed->imports[u].module == import) {
1080 return mod->parsed->imports[u].prefix;
1081 }
1082 }
1083 } else {
1084 /* we don't have original information about the import's prefix,
1085 * so the prefix of the import module itself is returned instead */
1086 return import->prefix;
1087 }
1088
1089 return NULL;
1090}
1091
Radek Krejci0935f412019-08-20 16:15:18 +02001092API const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001093lys_nodetype2str(uint16_t nodetype)
1094{
1095 switch(nodetype) {
1096 case LYS_CONTAINER:
1097 return "container";
1098 case LYS_CHOICE:
1099 return "choice";
1100 case LYS_LEAF:
1101 return "leaf";
1102 case LYS_LEAFLIST:
1103 return "leaf-list";
1104 case LYS_LIST:
1105 return "list";
1106 case LYS_ANYXML:
1107 return "anyxml";
1108 case LYS_ANYDATA:
1109 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001110 case LYS_CASE:
1111 return "case";
Radek Krejcif538ce52019-03-05 10:46:14 +01001112 case LYS_ACTION:
1113 return "RPC/action";
1114 case LYS_NOTIF:
Michal Vaskoa3881362020-01-21 15:57:35 +01001115 return "notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001116 case LYS_USES:
1117 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001118 default:
1119 return "unknown";
1120 }
1121}
1122
Radek Krejci693262f2019-04-29 15:23:20 +02001123const char *
1124lys_datatype2str(LY_DATA_TYPE basetype)
1125{
1126 switch(basetype) {
1127 case LY_TYPE_BINARY:
1128 return "binary";
1129 case LY_TYPE_UINT8:
1130 return "uint8";
1131 case LY_TYPE_UINT16:
1132 return "uint16";
1133 case LY_TYPE_UINT32:
1134 return "uint32";
1135 case LY_TYPE_UINT64:
1136 return "uint64";
1137 case LY_TYPE_STRING:
1138 return "string";
1139 case LY_TYPE_BITS:
1140 return "bits";
1141 case LY_TYPE_BOOL:
1142 return "boolean";
1143 case LY_TYPE_DEC64:
1144 return "decimal64";
1145 case LY_TYPE_EMPTY:
1146 return "empty";
1147 case LY_TYPE_ENUM:
1148 return "enumeration";
1149 case LY_TYPE_IDENT:
1150 return "identityref";
1151 case LY_TYPE_INST:
1152 return "instance-identifier";
1153 case LY_TYPE_LEAFREF:
1154 return "leafref";
1155 case LY_TYPE_UNION:
1156 return "union";
1157 case LY_TYPE_INT8:
1158 return "int8";
1159 case LY_TYPE_INT16:
1160 return "int16";
1161 case LY_TYPE_INT32:
1162 return "int32";
1163 case LY_TYPE_INT64:
1164 return "int64";
1165 default:
1166 return "unknown";
1167 }
1168}
1169
Radek Krejci056d0a82018-12-06 16:57:25 +01001170API const struct lysp_tpdf *
1171lysp_node_typedefs(const struct lysp_node *node)
1172{
Radek Krejci0fb28562018-12-13 15:17:37 +01001173 switch (node->nodetype) {
1174 case LYS_CONTAINER:
1175 return ((struct lysp_node_container*)node)->typedefs;
1176 case LYS_LIST:
1177 return ((struct lysp_node_list*)node)->typedefs;
1178 case LYS_GROUPING:
1179 return ((struct lysp_grp*)node)->typedefs;
1180 case LYS_ACTION:
1181 return ((struct lysp_action*)node)->typedefs;
1182 case LYS_INOUT:
1183 return ((struct lysp_action_inout*)node)->typedefs;
1184 case LYS_NOTIF:
1185 return ((struct lysp_notif*)node)->typedefs;
1186 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001187 return NULL;
1188 }
1189}
1190
Radek Krejci53ea6152018-12-13 15:21:15 +01001191API const struct lysp_grp *
1192lysp_node_groupings(const struct lysp_node *node)
1193{
1194 switch (node->nodetype) {
1195 case LYS_CONTAINER:
1196 return ((struct lysp_node_container*)node)->groupings;
1197 case LYS_LIST:
1198 return ((struct lysp_node_list*)node)->groupings;
1199 case LYS_GROUPING:
1200 return ((struct lysp_grp*)node)->groupings;
1201 case LYS_ACTION:
1202 return ((struct lysp_action*)node)->groupings;
1203 case LYS_INOUT:
1204 return ((struct lysp_action_inout*)node)->groupings;
1205 case LYS_NOTIF:
1206 return ((struct lysp_notif*)node)->groupings;
1207 default:
1208 return NULL;
1209 }
1210}
1211
Radek Krejcibbe09a92018-11-08 09:36:54 +01001212struct lysp_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001213lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001214{
1215 assert(node);
1216 switch (node->nodetype) {
1217 case LYS_CONTAINER:
1218 return &((struct lysp_node_container*)node)->actions;
1219 case LYS_LIST:
1220 return &((struct lysp_node_list*)node)->actions;
1221 case LYS_GROUPING:
1222 return &((struct lysp_grp*)node)->actions;
1223 case LYS_AUGMENT:
1224 return &((struct lysp_augment*)node)->actions;
1225 default:
1226 return NULL;
1227 }
1228}
1229
Radek Krejci056d0a82018-12-06 16:57:25 +01001230API const struct lysp_action *
1231lysp_node_actions(const struct lysp_node *node)
1232{
1233 struct lysp_action **actions;
1234 actions = lysp_node_actions_p((struct lysp_node*)node);
1235 if (actions) {
1236 return *actions;
1237 } else {
1238 return NULL;
1239 }
1240}
1241
Radek Krejcibbe09a92018-11-08 09:36:54 +01001242struct lysp_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001243lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001244{
1245 assert(node);
1246 switch (node->nodetype) {
1247 case LYS_CONTAINER:
1248 return &((struct lysp_node_container*)node)->notifs;
1249 case LYS_LIST:
1250 return &((struct lysp_node_list*)node)->notifs;
1251 case LYS_GROUPING:
1252 return &((struct lysp_grp*)node)->notifs;
1253 case LYS_AUGMENT:
1254 return &((struct lysp_augment*)node)->notifs;
1255 default:
1256 return NULL;
1257 }
1258}
1259
Radek Krejci056d0a82018-12-06 16:57:25 +01001260API const struct lysp_notif *
1261lysp_node_notifs(const struct lysp_node *node)
1262{
1263 struct lysp_notif **notifs;
1264 notifs = lysp_node_notifs_p((struct lysp_node*)node);
1265 if (notifs) {
1266 return *notifs;
1267 } else {
1268 return NULL;
1269 }
1270}
1271
Radek Krejcibbe09a92018-11-08 09:36:54 +01001272struct lysp_node **
Radek Krejci056d0a82018-12-06 16:57:25 +01001273lysp_node_children_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001274{
1275 assert(node);
1276 switch (node->nodetype) {
1277 case LYS_CONTAINER:
1278 return &((struct lysp_node_container*)node)->child;
1279 case LYS_CHOICE:
1280 return &((struct lysp_node_choice*)node)->child;
1281 case LYS_LIST:
1282 return &((struct lysp_node_list*)node)->child;
1283 case LYS_CASE:
1284 return &((struct lysp_node_case*)node)->child;
1285 case LYS_GROUPING:
1286 return &((struct lysp_grp*)node)->data;
1287 case LYS_AUGMENT:
1288 return &((struct lysp_augment*)node)->child;
1289 case LYS_INOUT:
1290 return &((struct lysp_action_inout*)node)->data;
1291 case LYS_NOTIF:
1292 return &((struct lysp_notif*)node)->data;
1293 default:
1294 return NULL;
1295 }
1296}
1297
Radek Krejci056d0a82018-12-06 16:57:25 +01001298API const struct lysp_node *
1299lysp_node_children(const struct lysp_node *node)
1300{
1301 struct lysp_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001302
1303 if (!node) {
1304 return NULL;
1305 }
1306
Radek Krejci056d0a82018-12-06 16:57:25 +01001307 children = lysp_node_children_p((struct lysp_node*)node);
1308 if (children) {
1309 return *children;
1310 } else {
1311 return NULL;
1312 }
1313}
1314
1315struct lysc_action **
1316lysc_node_actions_p(struct lysc_node *node)
1317{
1318 assert(node);
1319 switch (node->nodetype) {
1320 case LYS_CONTAINER:
1321 return &((struct lysc_node_container*)node)->actions;
1322 case LYS_LIST:
1323 return &((struct lysc_node_list*)node)->actions;
1324 default:
1325 return NULL;
1326 }
1327}
1328
1329API const struct lysc_action *
1330lysc_node_actions(const struct lysc_node *node)
1331{
1332 struct lysc_action **actions;
1333 actions = lysc_node_actions_p((struct lysc_node*)node);
1334 if (actions) {
1335 return *actions;
1336 } else {
1337 return NULL;
1338 }
1339}
1340
1341struct lysc_notif **
1342lysc_node_notifs_p(struct lysc_node *node)
1343{
1344 assert(node);
1345 switch (node->nodetype) {
1346 case LYS_CONTAINER:
1347 return &((struct lysc_node_container*)node)->notifs;
1348 case LYS_LIST:
1349 return &((struct lysc_node_list*)node)->notifs;
1350 default:
1351 return NULL;
1352 }
1353}
1354
1355API const struct lysc_notif *
1356lysc_node_notifs(const struct lysc_node *node)
1357{
1358 struct lysc_notif **notifs;
1359 notifs = lysc_node_notifs_p((struct lysc_node*)node);
1360 if (notifs) {
1361 return *notifs;
1362 } else {
1363 return NULL;
1364 }
1365}
1366
Radek Krejcibbe09a92018-11-08 09:36:54 +01001367struct lysc_node **
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001368lysc_node_children_p(const struct lysc_node *node, uint16_t flags)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001369{
1370 assert(node);
1371 switch (node->nodetype) {
1372 case LYS_CONTAINER:
1373 return &((struct lysc_node_container*)node)->child;
1374 case LYS_CHOICE:
Radek Krejcia3045382018-11-22 14:30:31 +01001375 if (((struct lysc_node_choice*)node)->cases) {
Radek Krejci95710c92019-02-11 15:49:55 +01001376 return &((struct lysc_node_choice*)node)->cases->child;
Radek Krejcia3045382018-11-22 14:30:31 +01001377 } else {
1378 return NULL;
1379 }
Radek Krejci01342af2019-01-03 15:18:08 +01001380 case LYS_CASE:
1381 return &((struct lysc_node_case*)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001382 case LYS_LIST:
1383 return &((struct lysc_node_list*)node)->child;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001384 case LYS_ACTION:
1385 if (flags & LYS_CONFIG_R) {
1386 return &((struct lysc_action*)node)->output.data;
1387 } else {
1388 /* LYS_CONFIG_W, but also the default case */
1389 return &((struct lysc_action*)node)->input.data;
1390 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02001391 case LYS_NOTIF:
1392 return &((struct lysc_notif*)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001393 default:
1394 return NULL;
1395 }
1396}
1397
Radek Krejci056d0a82018-12-06 16:57:25 +01001398API const struct lysc_node *
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001399lysc_node_children(const struct lysc_node *node, uint16_t flags)
Radek Krejcia3045382018-11-22 14:30:31 +01001400{
Radek Krejci056d0a82018-12-06 16:57:25 +01001401 struct lysc_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001402
1403 if (!node) {
1404 return NULL;
1405 }
1406
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001407 children = lysc_node_children_p((struct lysc_node*)node, flags);
Radek Krejci056d0a82018-12-06 16:57:25 +01001408 if (children) {
1409 return *children;
1410 } else {
Radek Krejcia3045382018-11-22 14:30:31 +01001411 return NULL;
1412 }
1413}
1414
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001415struct lys_module *
1416lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1417{
1418 unsigned int u;
1419
1420 for (u = 0; u < ctx->list.count; ++u) {
1421 if (((struct lys_module*)ctx->list.objs[u])->parsed == mod) {
1422 return ((struct lys_module*)ctx->list.objs[u]);
1423 }
1424 }
1425 return NULL;
1426}
1427
Radek Krejcid6b76452019-09-03 17:03:03 +02001428enum ly_stmt
David Sedlák5f8f0332019-06-18 16:34:30 +02001429lysp_match_kw(struct lys_parser_ctx *ctx, const char **data)
David Sedlákc10e7902018-12-17 02:17:59 +01001430{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001431/**
1432 * @brief Move the DATA pointer by COUNT items. Also updates the indent value in yang parser context
1433 * @param[in] CTX yang parser context to update its indent value.
1434 * @param[in,out] DATA pointer to move
1435 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
1436 */
1437#define MOVE_IN(CTX, DATA, COUNT) (*(DATA))+=COUNT;if(CTX){(CTX)->indent+=COUNT;}
1438#define IF_KW(STR, LEN, STMT) if (!strncmp(*(data), STR, LEN)) {MOVE_IN(ctx, data, LEN);*kw=STMT;}
1439#define IF_KW_PREFIX(STR, LEN) if (!strncmp(*(data), STR, LEN)) {MOVE_IN(ctx, data, LEN);
1440#define IF_KW_PREFIX_END }
David Sedlák572e7ab2019-06-04 16:01:58 +02001441
Radek Krejcid6b76452019-09-03 17:03:03 +02001442 enum ly_stmt result = LY_STMT_NONE;
1443 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001444 /* read the keyword itself */
1445 switch (**data) {
David Sedlák23a59a62018-10-26 13:08:02 +02001446 case 'a':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001447 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001448 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1449 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1450 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001451 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001452 IF_KW("data", 4, LY_STMT_ANYDATA)
1453 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001454 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001455 break;
1456 case 'b':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001457 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001458 IF_KW("ase", 3, LY_STMT_BASE)
1459 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1460 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001461 break;
1462 case 'c':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001463 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001464 IF_KW("ase", 3, LY_STMT_CASE)
1465 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001466 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001467 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001468 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001469 IF_KW("ct", 2, LY_STMT_CONTACT)
1470 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001471 IF_KW_PREFIX_END
1472 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001473 break;
1474 case 'd':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001475 MOVE_IN(ctx, data, 1);
1476 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001477 IF_KW("fault", 5, LY_STMT_DEFAULT)
1478 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001479 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001480 IF_KW("e", 1, LY_STMT_DEVIATE)
1481 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001482 IF_KW_PREFIX_END
1483 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001484 break;
1485 case 'e':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001486 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001487 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001488 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001489 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1490 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001491 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001492 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001493 break;
1494 case 'f':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001495 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001496 IF_KW("eature", 6, LY_STMT_FEATURE)
1497 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001498 break;
1499 case 'g':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001500 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001501 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001502 break;
1503 case 'i':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001504 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001505 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1506 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1507 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001508 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001509 IF_KW("clude", 5, LY_STMT_INCLUDE)
1510 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001511 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001512 break;
1513 case 'k':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001514 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001515 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001516 break;
1517 case 'l':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001518 MOVE_IN(ctx, data, 1);
1519 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001520 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1521 else IF_KW("af", 2, LY_STMT_LEAF)
1522 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001523 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001524 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001525 break;
1526 case 'm':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001527 MOVE_IN(ctx, data, 1);
1528 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001529 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1530 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001531 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001532 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1533 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001534 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001535 IF_KW("ule", 3, LY_STMT_MODULE)
1536 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001537 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001538 break;
1539 case 'n':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001540 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001541 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1542 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001543 break;
1544 case 'o':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001545 MOVE_IN(ctx, data, 1);
1546 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001547 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1548 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001549 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001550 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001551 break;
1552 case 'p':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001553 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001554 IF_KW("ath", 3, LY_STMT_PATH)
1555 else IF_KW("attern", 6, LY_STMT_PATTERN)
1556 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001557 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001558 IF_KW("fix", 3, LY_STMT_PREFIX)
1559 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001560 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001561 break;
1562 case 'r':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001563 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001564 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001565 else IF_KW_PREFIX("e", 1)
1566 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001567 IF_KW("erence", 6, LY_STMT_REFERENCE)
1568 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001569 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001570 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1571 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1572 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001573 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001574 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001575 break;
1576 case 's':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001577 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001578 IF_KW("tatus", 5, LY_STMT_STATUS)
1579 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001580 break;
1581 case 't':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001582 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001583 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1584 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001585 break;
1586 case 'u':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001587 MOVE_IN(ctx, data, 1);
1588 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001589 IF_KW("que", 3, LY_STMT_UNIQUE)
1590 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001591 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001592 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001593 break;
1594 case 'v':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001595 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001596 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001597 break;
1598 case 'w':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001599 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001600 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001601 break;
1602 case 'y':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001603 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001604 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
1605 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001606 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001607 default:
David Sedlák1bccdfa2019-06-17 15:55:27 +02001608 /* if context is not NULL we are matching keyword from YANG data*/
1609 if (ctx) {
1610 if (**data == ';') {
1611 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001612 *kw = LY_STMT_SYNTAX_SEMICOLON;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001613 } else if (**data == '{') {
1614 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001615 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001616 } else if (**data == '}') {
1617 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001618 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001619 }
1620 }
David Sedlák23a59a62018-10-26 13:08:02 +02001621 break;
1622 }
1623
David Sedlák1bccdfa2019-06-17 15:55:27 +02001624#undef IF_KW
1625#undef IF_KW_PREFIX
1626#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01001627#undef MOVE_IN
David Sedlák18730132019-03-15 15:51:34 +01001628
David Sedlák1bccdfa2019-06-17 15:55:27 +02001629 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02001630}
David Sedlákecf5eb82019-06-03 14:12:44 +02001631
Radek Krejcid3ca0632019-04-16 16:54:54 +02001632unsigned int
1633lysp_ext_instance_iter(struct lysp_ext_instance *ext, unsigned int index, LYEXT_SUBSTMT substmt)
1634{
1635 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1636
1637 for (; index < LY_ARRAY_SIZE(ext); index++) {
1638 if (ext[index].insubstmt == substmt) {
1639 return index;
1640 }
1641 }
1642
1643 return LY_ARRAY_SIZE(ext);
1644}
1645
Radek Krejcia1911222019-07-22 17:24:50 +02001646/**
1647 * @brief Schema mapping of YANG modules to prefixes in values.
1648 *
1649 * Implementation of ly_clb_get_prefix. Inverse function to lys_resolve_prefix.
1650 *
1651 * In this case the @p mod is searched in the list of imports and the import's prefix
1652 * (not the module's itself) prefix is returned.
1653 */
1654const char *
1655lys_get_prefix(const struct lys_module *mod, void *private)
1656{
1657 struct lys_module *context_mod = (struct lys_module*)private;
1658 unsigned int u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001659
Radek Krejci474f9b82019-07-24 11:36:37 +02001660 if (context_mod == mod) {
1661 return context_mod->prefix;
1662 }
Radek Krejcia1911222019-07-22 17:24:50 +02001663 LY_ARRAY_FOR(context_mod->compiled->imports, u) {
1664 if (context_mod->compiled->imports[u].module == mod) {
1665 /* match */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02001666 return context_mod->compiled->imports[u].prefix;
Radek Krejcia1911222019-07-22 17:24:50 +02001667 }
1668 }
1669
1670 return NULL;
1671}
1672
1673/**
1674 * @brief Schema mapping of prefix in values to YANG modules (imports).
1675 *
1676 * Implementation of ly_clb_resolve_prefix. Inverse function to lys_get_prefix().
1677 *
1678 * In this case the @p prefix is searched in the list of imports' prefixes (not the prefixes of the imported modules themselves).
1679 */
1680const struct lys_module *
1681lys_resolve_prefix(struct ly_ctx *UNUSED(ctx), const char *prefix, size_t prefix_len, void *private)
1682{
1683 return lys_module_find_prefix((const struct lys_module*)private, prefix, prefix_len);
1684}