blob: ddc2d3a801a6b1412220d78bd52f5f90fd9f6db8 [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 */
Radek Krejci77a8bcd2019-09-11 11:20:02 +020095 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci9bb94eb2018-12-04 16:48:35 +010096 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +010097 if (context_node && context_node->nodetype == LYS_ACTION) {
98 /* move through input/output manually */
Radek Krejci7f9b6512019-09-18 13:11:09 +020099 if (!ly_strncmp("input", name, name_len)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100100 (*result_flag) |= LYSC_OPT_RPC_INPUT;
Radek Krejci7f9b6512019-09-18 13:11:09 +0200101 } else if (!ly_strncmp("output", name, name_len)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100102 (*result_flag) |= LYSC_OPT_RPC_OUTPUT;
103 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
Radek Krejci05b774b2019-02-25 13:26:18 +0100104 } else {
105 goto getnext;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100106 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100107 current_nodetype = LYS_INOUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100108 } else {
Radek Krejci05b774b2019-02-25 13:26:18 +0100109getnext:
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100110 context_node = lys_child(context_node, mod, name, name_len, 0,
111 getnext_extra_flag | LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
112 if (!context_node) {
113 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
114 "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.", nodeid_type, id - nodeid, nodeid);
115 return LY_ENOTFOUND;
116 }
117 getnext_extra_flag = 0;
Radek Krejci05b774b2019-02-25 13:26:18 +0100118 current_nodetype = context_node->nodetype;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100119
Radek Krejci05b774b2019-02-25 13:26:18 +0100120 if (current_nodetype == LYS_NOTIF) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100121 (*result_flag) |= LYSC_OPT_NOTIFICATION;
122 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100123 }
Radek Krejci01342af2019-01-03 15:18:08 +0100124 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100125 break;
126 }
Radek Krejci01342af2019-01-03 15:18:08 +0100127 if (*id != '/') {
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100128 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci95710c92019-02-11 15:49:55 +0100129 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
130 nodeid_type, id - nodeid + 1, nodeid);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100131 return LY_EVALID;
132 }
133 ++id;
134 }
135
136 if (ret == LY_SUCCESS) {
Radek Krejci7af64242019-02-18 13:07:53 +0100137 *target = context_node;
Radek Krejci05b774b2019-02-25 13:26:18 +0100138 if (nodetype & LYS_INOUT) {
139 /* instead of input/output nodes, the RPC/action node is actually returned */
140 }
141 if (nodetype && !(current_nodetype & nodetype)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100142 return LY_EDENIED;
143 }
Radek Krejci95710c92019-02-11 15:49:55 +0100144 } else {
145 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
146 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci3641f562019-02-13 15:38:40 +0100147 nodeid_type, nodeid_len ? nodeid_len : strlen(nodeid), nodeid);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100148 }
149
150 return ret;
151}
152
153LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200154lysp_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 +0200155{
156 struct lysp_import *i;
157
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100158 if (module_prefix && &module_prefix != value && !strcmp(module_prefix, *value)) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200159 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE,
160 "Prefix \"%s\" already used as module prefix.", *value);
161 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)) {
165 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.",
166 *value, i->name);
167 return LY_EEXIST;
Radek Krejci86d106e2018-10-18 09:53:19 +0200168 }
169 }
170 return LY_SUCCESS;
171}
172
173LY_ERR
Radek Krejci4f28eda2018-11-12 11:46:16 +0100174lysc_check_status(struct lysc_ctx *ctx,
175 uint16_t flags1, void *mod1, const char *name1,
176 uint16_t flags2, void *mod2, const char *name2)
177{
178 uint16_t flg1, flg2;
179
180 flg1 = (flags1 & LYS_STATUS_MASK) ? (flags1 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
181 flg2 = (flags2 & LYS_STATUS_MASK) ? (flags2 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
182
183 if ((flg1 < flg2) && (mod1 == mod2)) {
184 if (ctx) {
185 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
186 "A %s definition \"%s\" is not allowed to reference %s definition \"%s\".",
187 flg1 == LYS_STATUS_CURR ? "current" : "deprecated", name1,
188 flg2 == LYS_STATUS_OBSLT ? "obsolete" : "deprecated", name2);
189 }
190 return LY_EVALID;
191 }
192
193 return LY_SUCCESS;
194}
195
196LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200197lysp_check_date(struct lys_parser_ctx *ctx, const char *date, int date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +0200198{
199 int i;
200 struct tm tm, tm_;
201 char *r;
202
Radek Krejcibbe09a92018-11-08 09:36:54 +0100203 LY_CHECK_ARG_RET(ctx ? ctx->ctx : NULL, date, LY_EINVAL);
204 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 +0200205
206 /* check format */
207 for (i = 0; i < date_len; i++) {
208 if (i == 4 || i == 7) {
209 if (date[i] != '-') {
210 goto error;
211 }
212 } else if (!isdigit(date[i])) {
213 goto error;
214 }
215 }
216
217 /* check content, e.g. 2018-02-31 */
218 memset(&tm, 0, sizeof tm);
219 r = strptime(date, "%Y-%m-%d", &tm);
220 if (!r || r != &date[LY_REV_SIZE - 1]) {
221 goto error;
222 }
223 memcpy(&tm_, &tm, sizeof tm);
224 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
225 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
226 /* checking days is enough, since other errors
227 * have been checked by strptime() */
228 goto error;
229 }
230
231 return LY_SUCCESS;
232
233error:
Radek Krejcid33273d2018-10-25 14:55:52 +0200234 if (stmt) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100235 if (ctx) {
236 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LY_VCODE_INVAL, date_len, date, stmt);
237 } else {
238 LOGVAL(NULL, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, date_len, date, stmt);
239 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200240 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200241 return LY_EINVAL;
242}
243
244void
245lysp_sort_revisions(struct lysp_revision *revs)
246{
247 uint8_t i, r;
248 struct lysp_revision rev;
249
250 for (i = 1, r = 0; revs && i < LY_ARRAY_SIZE(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200251 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200252 r = i;
253 }
254 }
255
256 if (r) {
257 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200258 memcpy(&rev, &revs[0], sizeof rev);
259 memcpy(&revs[0], &revs[r], sizeof rev);
260 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200261 }
262}
Radek Krejci151a5b72018-10-19 14:21:44 +0200263
Radek Krejcibbe09a92018-11-08 09:36:54 +0100264static const struct lysp_tpdf *
265lysp_type_match(const char *name, struct lysp_node *node)
266{
Radek Krejci0fb28562018-12-13 15:17:37 +0100267 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100268 unsigned int u;
269
Radek Krejci0fb28562018-12-13 15:17:37 +0100270 typedefs = lysp_node_typedefs(node);
271 LY_ARRAY_FOR(typedefs, u) {
272 if (!strcmp(name, typedefs[u].name)) {
273 /* match */
274 return &typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100275 }
276 }
277
278 return NULL;
279}
280
Radek Krejci4f28eda2018-11-12 11:46:16 +0100281static LY_DATA_TYPE
282lysp_type_str2builtin(const char *name, size_t len)
283{
284 if (len >= 4) { /* otherwise it does not match any built-in type */
285 if (name[0] == 'b') {
286 if (name[1] == 'i') {
287 if (len == 6 && !strncmp(&name[2], "nary", 4)) {
288 return LY_TYPE_BINARY;
289 } else if (len == 4 && !strncmp(&name[2], "ts", 2)) {
290 return LY_TYPE_BITS;
291 }
292 } else if (len == 7 && !strncmp(&name[1], "oolean", 6)) {
293 return LY_TYPE_BOOL;
294 }
295 } else if (name[0] == 'd') {
296 if (len == 9 && !strncmp(&name[1], "ecimal64", 8)) {
297 return LY_TYPE_DEC64;
298 }
299 } else if (name[0] == 'e') {
300 if (len == 5 && !strncmp(&name[1], "mpty", 4)) {
301 return LY_TYPE_EMPTY;
302 } else if (len == 11 && !strncmp(&name[1], "numeration", 10)) {
303 return LY_TYPE_ENUM;
304 }
305 } else if (name[0] == 'i') {
306 if (name[1] == 'n') {
307 if (len == 4 && !strncmp(&name[2], "t8", 2)) {
308 return LY_TYPE_INT8;
309 } else if (len == 5) {
310 if (!strncmp(&name[2], "t16", 3)) {
311 return LY_TYPE_INT16;
312 } else if (!strncmp(&name[2], "t32", 3)) {
313 return LY_TYPE_INT32;
314 } else if (!strncmp(&name[2], "t64", 3)) {
315 return LY_TYPE_INT64;
316 }
317 } else if (len == 19 && !strncmp(&name[2], "stance-identifier", 17)) {
318 return LY_TYPE_INST;
319 }
320 } else if (len == 11 && !strncmp(&name[1], "dentityref", 10)) {
321 return LY_TYPE_IDENT;
322 }
323 } else if (name[0] == 'l') {
324 if (len == 7 && !strncmp(&name[1], "eafref", 6)) {
325 return LY_TYPE_LEAFREF;
326 }
327 } else if (name[0] == 's') {
328 if (len == 6 && !strncmp(&name[1], "tring", 5)) {
329 return LY_TYPE_STRING;
330 }
331 } else if (name[0] == 'u') {
332 if (name[1] == 'n') {
333 if (len == 5 && !strncmp(&name[2], "ion", 3)) {
334 return LY_TYPE_UNION;
335 }
336 } else if (name[1] == 'i' && name[2] == 'n' && name[3] == 't') {
337 if (len == 5 && name[4] == '8') {
338 return LY_TYPE_UINT8;
339 } else if (len == 6) {
340 if (!strncmp(&name[4], "16", 2)) {
341 return LY_TYPE_UINT16;
342 } else if (!strncmp(&name[4], "32", 2)) {
343 return LY_TYPE_UINT32;
344 } else if (!strncmp(&name[4], "64", 2)) {
345 return LY_TYPE_UINT64;
346 }
347 }
348 }
349 }
350 }
351
352 return LY_TYPE_UNKNOWN;
353}
354
Radek Krejcibbe09a92018-11-08 09:36:54 +0100355LY_ERR
356lysp_type_find(const char *id, struct lysp_node *start_node, struct lysp_module *start_module,
Radek Krejci4f28eda2018-11-12 11:46:16 +0100357 LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node, struct lysp_module **module)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100358{
359 const char *str, *name;
360 struct lysp_tpdf *typedefs;
361 unsigned int u, v;
362
363 assert(id);
364 assert(start_module);
365 assert(tpdf);
366 assert(node);
367 assert(module);
368
Radek Krejci4f28eda2018-11-12 11:46:16 +0100369 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100370 str = strchr(id, ':');
371 if (str) {
372 *module = lysp_module_find_prefix(start_module, id, str - id);
373 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100374 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100375 } else {
376 *module = start_module;
377 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100378
379 /* check for built-in types */
380 *type = lysp_type_str2builtin(name, strlen(name));
381 if (*type) {
382 *tpdf = NULL;
383 return LY_SUCCESS;
384 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100385 }
386 LY_CHECK_RET(!(*module), LY_ENOTFOUND);
387
388 if (start_node && *module == start_module) {
389 /* search typedefs in parent's nodes */
390 *node = start_node;
391 while (*node) {
392 *tpdf = lysp_type_match(name, *node);
393 if (*tpdf) {
394 /* match */
395 return LY_SUCCESS;
396 }
397 *node = (*node)->parent;
398 }
399 }
400
401 /* search in top-level typedefs */
402 if ((*module)->typedefs) {
403 LY_ARRAY_FOR((*module)->typedefs, u) {
404 if (!strcmp(name, (*module)->typedefs[u].name)) {
405 /* match */
406 *tpdf = &(*module)->typedefs[u];
407 return LY_SUCCESS;
408 }
409 }
410 }
411
412 /* search in submodules' typedefs */
413 LY_ARRAY_FOR((*module)->includes, u) {
414 typedefs = (*module)->includes[u].submodule->typedefs;
Radek Krejci76b3e962018-12-14 17:01:25 +0100415 LY_ARRAY_FOR(typedefs, v) {
416 if (!strcmp(name, typedefs[v].name)) {
417 /* match */
418 *tpdf = &typedefs[v];
419 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100420 }
421 }
422 }
423
424 return LY_ENOTFOUND;
425}
426
David Sedlák6544c182019-07-12 13:17:33 +0200427LY_ERR
David Sedlák07869a52019-07-12 14:28:19 +0200428lysp_check_enum_name(struct lys_parser_ctx *ctx, const char *name, size_t name_len)
David Sedlák6544c182019-07-12 13:17:33 +0200429{
430 if (!name_len) {
431 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
432 return LY_EVALID;
433 } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
434 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
435 name_len, name);
436 return LY_EVALID;
437 } else {
438 for (size_t u = 0; u < name_len; ++u) {
439 if (iscntrl(name[u])) {
440 LOGWRN(ctx->ctx, "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
441 name_len, name, u + 1);
442 break;
443 }
444 }
445 }
446
447 return LY_SUCCESS;
448}
449
Radek Krejcibbe09a92018-11-08 09:36:54 +0100450/*
451 * @brief Check name of a new type to avoid name collisions.
452 *
453 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
454 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
455 * @param[in] tpdf Typedef definition to check.
456 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
457 * typedefs are checked, caller is supposed to free the table.
458 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
459 * typedefs are checked, caller is supposed to free the table.
460 * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise.
461 */
462static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200463lysp_check_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
Radek Krejcibbe09a92018-11-08 09:36:54 +0100464 struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped)
465{
466 struct lysp_node *parent;
467 uint32_t hash;
468 size_t name_len;
469 const char *name;
470 unsigned int u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100471 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100472
473 assert(ctx);
474 assert(tpdf);
475
476 name = tpdf->name;
477 name_len = strlen(name);
478
Radek Krejci4f28eda2018-11-12 11:46:16 +0100479 if (lysp_type_str2builtin(name, name_len)) {
480 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
481 "Invalid name \"%s\" of typedef - name collision with a built-in type.", name);
482 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100483 }
484
485 /* check locally scoped typedefs (avoid name shadowing) */
486 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100487 typedefs = lysp_node_typedefs(node);
488 LY_ARRAY_FOR(typedefs, u) {
489 if (&typedefs[u] == tpdf) {
490 break;
491 }
492 if (!strcmp(name, typedefs[u].name)) {
493 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
494 "Invalid name \"%s\" of typedef - name collision with sibling type.", name);
495 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100496 }
497 }
498 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200499 for (parent = node->parent; parent; parent = parent->parent) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100500 if (lysp_type_match(name, parent)) {
501 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
502 "Invalid name \"%s\" of typedef - name collision with another scoped type.", name);
503 return LY_EEXIST;
504 }
505 }
506 }
507
508 /* check collision with the top-level typedefs */
509 hash = dict_hash(name, name_len);
510 if (node) {
511 lyht_insert(tpdfs_scoped, &name, hash, NULL);
512 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
513 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
514 "Invalid name \"%s\" of typedef - scoped type collide with a top-level type.", name);
515 return LY_EEXIST;
516 }
517 } else {
518 if (lyht_insert(tpdfs_global, &name, hash, NULL)) {
519 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
520 "Invalid name \"%s\" of typedef - name collision with another top-level type.", name);
521 return LY_EEXIST;
522 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100523 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
524 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
525 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100526 }
527
528 return LY_SUCCESS;
529}
530
531static int
532lysp_id_cmp(void *val1, void *val2, int UNUSED(mod), void *UNUSED(cb_data))
533{
534 return !strcmp(val1, val2);
535}
536
537LY_ERR
David Sedlákd2ebe572019-07-22 12:53:14 +0200538lysp_parse_finalize_reallocated(struct lys_parser_ctx *ctx, struct lysp_grp *groupings, struct lysp_augment *augments,
539 struct lysp_action *actions, struct lysp_notif *notifs)
540{
541 unsigned int u, v;
542 struct lysp_node *child;
543
544 /* finalize parent pointers to the reallocated items */
545
546 /* gropings */
547 LY_ARRAY_FOR(groupings, u) {
548 LY_LIST_FOR(groupings[u].data, child) {
549 child->parent = (struct lysp_node*)&groupings[u];
550 }
551 LY_ARRAY_FOR(groupings[u].actions, v) {
552 groupings[u].actions[v].parent = (struct lysp_node*)&groupings[u];
553 }
554 LY_ARRAY_FOR(groupings[u].notifs, v) {
555 groupings[u].notifs[v].parent = (struct lysp_node*)&groupings[u];
556 }
557 LY_ARRAY_FOR(groupings[u].groupings, v) {
558 groupings[u].groupings[v].parent = (struct lysp_node*)&groupings[u];
559 }
560 if (groupings[u].typedefs) {
561 ly_set_add(&ctx->tpdfs_nodes, &groupings[u], 0);
562 }
563 }
564
565 /* augments */
566 LY_ARRAY_FOR(augments, u) {
567 LY_LIST_FOR(augments[u].child, child) {
568 child->parent = (struct lysp_node*)&augments[u];
569 }
570 LY_ARRAY_FOR(augments[u].actions, v) {
571 augments[u].actions[v].parent = (struct lysp_node*)&augments[u];
572 }
573 LY_ARRAY_FOR(augments[u].notifs, v) {
574 augments[u].notifs[v].parent = (struct lysp_node*)&augments[u];
575 }
576 }
577
578 /* actions */
579 LY_ARRAY_FOR(actions, u) {
580 if (actions[u].input.parent) {
581 actions[u].input.parent = (struct lysp_node*)&actions[u];
582 LY_LIST_FOR(actions[u].input.data, child) {
583 child->parent = (struct lysp_node*)&actions[u].input;
584 }
585 LY_ARRAY_FOR(actions[u].input.groupings, v) {
586 actions[u].input.groupings[v].parent = (struct lysp_node*)&actions[u].input;
587 }
588 if (actions[u].input.typedefs) {
589 ly_set_add(&ctx->tpdfs_nodes, &actions[u].input, 0);
590 }
591 }
592 if (actions[u].output.parent) {
593 actions[u].output.parent = (struct lysp_node*)&actions[u];
594 LY_LIST_FOR(actions[u].output.data, child) {
595 child->parent = (struct lysp_node*)&actions[u].output;
596 }
597 LY_ARRAY_FOR(actions[u].output.groupings, v) {
598 actions[u].output.groupings[v].parent = (struct lysp_node*)&actions[u].output;
599 }
600 if (actions[u].output.typedefs) {
601 ly_set_add(&ctx->tpdfs_nodes, &actions[u].output, 0);
602 }
603 }
604 LY_ARRAY_FOR(actions[u].groupings, v) {
605 actions[u].groupings[v].parent = (struct lysp_node*)&actions[u];
606 }
607 if (actions[u].typedefs) {
608 ly_set_add(&ctx->tpdfs_nodes, &actions[u], 0);
609 }
610 }
611
612 /* notifications */
613 LY_ARRAY_FOR(notifs, u) {
614 LY_LIST_FOR(notifs[u].data, child) {
615 child->parent = (struct lysp_node*)&notifs[u];
616 }
617 LY_ARRAY_FOR(notifs[u].groupings, v) {
618 notifs[u].groupings[v].parent = (struct lysp_node*)&notifs[u];
619 }
620 if (notifs[u].typedefs) {
621 ly_set_add(&ctx->tpdfs_nodes, &notifs[u], 0);
622 }
623 }
624
625 return LY_SUCCESS;
626}
627
628LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200629lysp_check_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100630{
631 struct hash_table *ids_global;
632 struct hash_table *ids_scoped;
Radek Krejci0fb28562018-12-13 15:17:37 +0100633 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100634 unsigned int i, u;
635 LY_ERR ret = LY_EVALID;
636
637 /* check name collisions - typedefs and groupings */
638 ids_global = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1);
639 ids_scoped = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100640 LY_ARRAY_FOR(mod->typedefs, i) {
641 if (lysp_check_typedef(ctx, NULL, &mod->typedefs[i], ids_global, ids_scoped)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100642 goto cleanup;
643 }
644 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100645 LY_ARRAY_FOR(mod->includes, i) {
646 LY_ARRAY_FOR(mod->includes[i].submodule->typedefs, u) {
647 if (lysp_check_typedef(ctx, NULL, &mod->includes[i].submodule->typedefs[u], ids_global, ids_scoped)) {
Radek Krejci3b1f9292018-11-08 10:58:35 +0100648 goto cleanup;
649 }
650 }
651 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100652 for (u = 0; u < ctx->tpdfs_nodes.count; ++u) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100653 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[u]);
654 LY_ARRAY_FOR(typedefs, i) {
655 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 +0100656 goto cleanup;
657 }
658 }
659 }
660 ret = LY_SUCCESS;
661cleanup:
662 lyht_free(ids_global);
663 lyht_free(ids_scoped);
664 ly_set_erase(&ctx->tpdfs_nodes, NULL);
665
666 return ret;
667}
668
Radek Krejci9ed7a192018-10-31 16:23:51 +0100669struct lysp_load_module_check_data {
670 const char *name;
671 const char *revision;
672 const char *path;
673 const char* submoduleof;
674};
675
676static LY_ERR
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100677lysp_load_module_check(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100678{
679 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100680 const char *filename, *dot, *rev, *name;
Radek Krejcib3289d62019-09-18 12:21:39 +0200681 uint8_t latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100682 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100683 struct lysp_revision *revs;
684
685 name = mod ? mod->mod->name : submod->name;
686 revs = mod ? mod->revs : submod->revs;
Radek Krejcib3289d62019-09-18 12:21:39 +0200687 latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100688
689 if (info->name) {
690 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100691 if (strcmp(info->name, name)) {
692 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100693 return LY_EINVAL;
694 }
695 }
696 if (info->revision) {
697 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100698 if (!revs || strcmp(info->revision, revs[0].date)) {
699 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Radek Krejcib07b5c92019-04-08 10:56:37 +0200700 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100701 return LY_EINVAL;
702 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200703 } else if (!latest_revision) {
704 /* do not log, we just need to drop the schema and use the latest revision from the context */
705 return LY_EEXIST;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100706 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100707 if (submod) {
708 assert(info->submoduleof);
709
Radek Krejci9ed7a192018-10-31 16:23:51 +0100710 /* check that the submodule belongs-to our module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100711 if (strcmp(info->submoduleof, submod->belongsto)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100712 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100713 submod->name, info->submoduleof, submod->belongsto);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100714 return LY_EVALID;
715 }
716 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100717 if (submod->parsing) {
718 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100719 return LY_EVALID;
720 }
721 }
722 if (info->path) {
723 /* check that name and revision match filename */
724 filename = strrchr(info->path, '/');
725 if (!filename) {
726 filename = info->path;
727 } else {
728 filename++;
729 }
730 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100731 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100732 rev = strchr(filename, '@');
733 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100734 if (strncmp(filename, name, len) ||
Radek Krejci9ed7a192018-10-31 16:23:51 +0100735 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100736 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100737 }
738 /* revision */
739 if (rev) {
740 len = dot - ++rev;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100741 if (!revs || len != 10 || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100742 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100743 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100744 }
745 }
746 }
747 return LY_SUCCESS;
748}
749
750LY_ERR
fredgancd485b82019-10-18 15:00:17 +0800751lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, int implement,
Radek Krejci78f06822019-10-30 12:54:05 +0100752 struct lys_parser_ctx *main_ctx, const char *main_name, int required, void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100753{
754 int fd;
755 char *filepath = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100756 const char **fp;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100757 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100758 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100759 LY_ERR ret = LY_SUCCESS;
760 struct lysp_load_module_check_data check_data = {0};
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100761 char rpath[PATH_MAX];
Radek Krejci9ed7a192018-10-31 16:23:51 +0100762
763 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, revision,
764 &filepath, &format));
Radek Krejci78f06822019-10-30 12:54:05 +0100765 LY_CHECK_ERR_RET(!filepath, if (required) {LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.",
766 name, revision ? "@" : "", revision ? revision : "");}, LY_ENOTFOUND);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100767
768 LOGVRB("Loading schema from \"%s\" file.", filepath);
769
770 /* open the file */
771 fd = open(filepath, O_RDONLY);
772 LY_CHECK_ERR_GOTO(fd < 0, LOGERR(ctx, LY_ESYS, "Unable to open data model file \"%s\" (%s).",
773 filepath, strerror(errno)); ret = LY_ESYS, cleanup);
774
775 check_data.name = name;
776 check_data.revision = revision;
777 check_data.path = filepath;
fredgancd485b82019-10-18 15:00:17 +0800778 check_data.submoduleof = main_name;
Radek Krejci3b1f9292018-11-08 10:58:35 +0100779 mod = lys_parse_fd_(ctx, fd, format, implement, main_ctx,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100780 lysp_load_module_check, &check_data);
781 close(fd);
782 LY_CHECK_ERR_GOTO(!mod, ly_errcode(ctx), cleanup);
783
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100784 if (main_ctx) {
785 fp = &((struct lysp_submodule*)mod)->filepath;
786 } else {
787 fp = &((struct lys_module*)mod)->filepath;
788 }
789 if (!(*fp)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100790 if (realpath(filepath, rpath) != NULL) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100791 (*fp) = lydict_insert(ctx, rpath, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100792 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100793 (*fp) = lydict_insert(ctx, filepath, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100794 }
795 }
796
797 *result = mod;
798
799 /* success */
800cleanup:
801 free(filepath);
802 return ret;
803}
804
Radek Krejcid33273d2018-10-25 14:55:52 +0200805LY_ERR
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100806lysp_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 +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;
Radek Krejci086c7132018-10-26 15:29:04 +0200813
Radek Krejci0af46292019-01-11 16:02:31 +0100814 assert(mod);
815
816 if (!*mod) {
817 /* try to get the module from the context */
818 if (revision) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200819 /* get the specific revision */
Radek Krejci0af46292019-01-11 16:02:31 +0100820 *mod = (struct lys_module*)ly_ctx_get_module(ctx, name, revision);
Radek Krejcied5acc52019-04-25 15:57:04 +0200821 } else if (implement) {
822 /* prefer the implemented module instead of the latest one */
823 *mod = (struct lys_module*)ly_ctx_get_module_implemented(ctx, name);
824 if (!*mod) {
825 /* there is no implemented module in the context, try to get the latest revision module */
826 goto latest_in_the_context;
827 }
Radek Krejci0af46292019-01-11 16:02:31 +0100828 } else {
Radek Krejcied5acc52019-04-25 15:57:04 +0200829 /* get the requested module of the latest revision in the context */
830latest_in_the_context:
Radek Krejci0af46292019-01-11 16:02:31 +0100831 *mod = (struct lys_module*)ly_ctx_get_module_latest(ctx, name);
Radek Krejcib3289d62019-09-18 12:21:39 +0200832 if (*mod && (*mod)->latest_revision == 1) {
833 /* let us now search with callback and searchpaths to check if there is newer revision outside the context */
834 m = *mod;
835 *mod = NULL;
836 }
Radek Krejci0af46292019-01-11 16:02:31 +0100837 }
Radek Krejci086c7132018-10-26 15:29:04 +0200838 }
839
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100840 if (!(*mod) || (require_parsed && !(*mod)->parsed)) {
841 (*mod) = NULL;
842
Radek Krejci086c7132018-10-26 15:29:04 +0200843 /* check collision with other implemented revision */
844 if (implement && ly_ctx_get_module_implemented(ctx, name)) {
845 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
846 "Module \"%s\" is already present in other implemented revision.", name);
847 return LY_EDENIED;
848 }
849
Radek Krejci9ed7a192018-10-31 16:23:51 +0100850 /* module not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200851 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
852search_clb:
853 if (ctx->imp_clb) {
854 if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100855 &format, &module_data, &module_data_free) == LY_SUCCESS) {
856 check_data.name = name;
857 check_data.revision = revision;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100858 *mod = lys_parse_mem_module(ctx, module_data, format, implement,
859 lysp_load_module_check, &check_data);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100860 if (module_data_free) {
861 module_data_free((void*)module_data, ctx->imp_clb_data);
862 }
Radek Krejci096235c2019-01-11 11:12:19 +0100863 if (*mod && implement && lys_compile(*mod, 0)) {
864 ly_set_rm(&ctx->list, *mod, NULL);
865 lys_module_free(*mod, NULL);
866 *mod = NULL;
867 }
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
Radek Krejcie7b95092019-05-15 11:03:07 +0200962lysp_load_submodule(struct lys_parser_ctx *ctx, struct lysp_module *mod, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200963{
Radek Krejci3eb299d2019-04-08 15:07:44 +0200964 struct lysp_submodule *submod = NULL;
Radek Krejcid33273d2018-10-25 14:55:52 +0200965 const char *submodule_data = NULL;
966 LYS_INFORMAT format = LYS_IN_UNKNOWN;
967 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100968 struct lysp_load_module_check_data check_data = {0};
Radek Krejcid33273d2018-10-25 14:55:52 +0200969
Radek Krejcibbe09a92018-11-08 09:36:54 +0100970 /* submodule not present in the context, get the input data and parse it */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100971 if (!(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200972search_clb:
Radek Krejci3b1f9292018-11-08 10:58:35 +0100973 if (ctx->ctx->imp_clb) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100974 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 +0100975 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
976 check_data.name = inc->name;
977 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100978 check_data.submoduleof = mod->mod->name;
979 submod = lys_parse_mem_submodule(ctx->ctx, submodule_data, format, ctx,
980 lysp_load_module_check, &check_data);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100981 if (submodule_data_free) {
Radek Krejci3b1f9292018-11-08 10:58:35 +0100982 submodule_data_free((void*)submodule_data, ctx->ctx->imp_clb_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200983 }
984 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200985 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100986 if (!submod && !(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100987 goto search_file;
Radek Krejcid33273d2018-10-25 14:55:52 +0200988 }
Radek Krejci2d31ea72018-10-25 15:46:42 +0200989 } else {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100990search_file:
Radek Krejci3b1f9292018-11-08 10:58:35 +0100991 if (!(ctx->ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100992 /* submodule was not received from the callback or there is no callback set */
Radek Krejci78f06822019-10-30 12:54:05 +0100993 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 +0100994 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100995 if (!submod && (ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100996 goto search_clb;
997 }
998 }
999 if (submod) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001000 if (!inc->rev[0] && (submod->latest_revision == 1)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +01001001 /* update the latest_revision flag - here we have selected the latest available schema,
1002 * consider that even the callback provides correct latest revision */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001003 submod->latest_revision = 2;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001004 }
1005
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001006 inc->submodule = submod;
Radek Krejcid33273d2018-10-25 14:55:52 +02001007 }
1008 if (!inc->submodule) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001009 LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.",
1010 inc->name, mod->mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +02001011 return LY_EVALID;
1012 }
1013
1014 return LY_SUCCESS;
1015}
1016
Radek Krejci01342af2019-01-03 15:18:08 +01001017#define FIND_MODULE(TYPE, MOD) \
Radek Krejcice8c1592018-10-29 15:35:51 +01001018 TYPE *imp; \
Radek Krejci7f9b6512019-09-18 13:11:09 +02001019 if (!ly_strncmp((MOD)->mod->prefix, prefix, len)) { \
Radek Krejcice8c1592018-10-29 15:35:51 +01001020 /* it is the prefix of the module itself */ \
Radek Krejci0af46292019-01-11 16:02:31 +01001021 m = ly_ctx_get_module((MOD)->mod->ctx, (MOD)->mod->name, (MOD)->mod->revision); \
Radek Krejcice8c1592018-10-29 15:35:51 +01001022 } \
1023 /* search in imports */ \
Radek Krejcibbe09a92018-11-08 09:36:54 +01001024 if (!m) { \
1025 LY_ARRAY_FOR((MOD)->imports, TYPE, imp) { \
Radek Krejci7f9b6512019-09-18 13:11:09 +02001026 if (!ly_strncmp(imp->prefix, prefix, len)) { \
Radek Krejcibbe09a92018-11-08 09:36:54 +01001027 m = imp->module; \
1028 break; \
1029 } \
Radek Krejcice8c1592018-10-29 15:35:51 +01001030 } \
Radek Krejcibbe09a92018-11-08 09:36:54 +01001031 }
Radek Krejcice8c1592018-10-29 15:35:51 +01001032
Radek Krejcibbe09a92018-11-08 09:36:54 +01001033struct lysc_module *
Radek Krejcia3045382018-11-22 14:30:31 +01001034lysc_module_find_prefix(const struct lysc_module *mod, const char *prefix, size_t len)
Radek Krejci151a5b72018-10-19 14:21:44 +02001035{
Radek Krejcibbe09a92018-11-08 09:36:54 +01001036 const struct lys_module *m = NULL;
1037
Radek Krejci01342af2019-01-03 15:18:08 +01001038 FIND_MODULE(struct lysc_import, mod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001039 return m ? m->compiled : NULL;
Radek Krejcice8c1592018-10-29 15:35:51 +01001040}
Radek Krejci151a5b72018-10-19 14:21:44 +02001041
Radek Krejcibbe09a92018-11-08 09:36:54 +01001042struct lysp_module *
Radek Krejcia3045382018-11-22 14:30:31 +01001043lysp_module_find_prefix(const struct lysp_module *mod, const char *prefix, size_t len)
Radek Krejcice8c1592018-10-29 15:35:51 +01001044{
Radek Krejcibbe09a92018-11-08 09:36:54 +01001045 const struct lys_module *m = NULL;
1046
Radek Krejci01342af2019-01-03 15:18:08 +01001047 FIND_MODULE(struct lysp_import, mod);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001048 return m ? m->parsed : NULL;
Radek Krejcice8c1592018-10-29 15:35:51 +01001049}
Radek Krejci151a5b72018-10-19 14:21:44 +02001050
Radek Krejcice8c1592018-10-29 15:35:51 +01001051struct lys_module *
Radek Krejcia3045382018-11-22 14:30:31 +01001052lys_module_find_prefix(const struct lys_module *mod, const char *prefix, size_t len)
Radek Krejcice8c1592018-10-29 15:35:51 +01001053{
Radek Krejcibbe09a92018-11-08 09:36:54 +01001054 const struct lys_module *m = NULL;
1055
Radek Krejci73dead22019-07-11 16:46:16 +02001056 if (!prefix) {
1057 return (struct lys_module*)mod;
1058 }
Radek Krejcice8c1592018-10-29 15:35:51 +01001059 if (mod->compiled) {
Radek Krejci01342af2019-01-03 15:18:08 +01001060 FIND_MODULE(struct lysc_import, mod->compiled);
Radek Krejcice8c1592018-10-29 15:35:51 +01001061 } else {
Radek Krejci01342af2019-01-03 15:18:08 +01001062 FIND_MODULE(struct lysp_import, mod->parsed);
Radek Krejcibbe09a92018-11-08 09:36:54 +01001063 }
1064 return (struct lys_module*)m;
1065}
1066
Radek Krejcia3045382018-11-22 14:30:31 +01001067const char *
Radek Krejci693262f2019-04-29 15:23:20 +02001068lys_prefix_find_module(const struct lys_module *mod, const struct lys_module *import)
1069{
1070 unsigned int u;
1071
1072 if (import == mod) {
1073 return mod->prefix;
1074 }
1075
1076 if (mod->parsed) {
1077 LY_ARRAY_FOR(mod->parsed->imports, u) {
1078 if (mod->parsed->imports[u].module == import) {
1079 return mod->parsed->imports[u].prefix;
1080 }
1081 }
1082 } else {
1083 /* we don't have original information about the import's prefix,
1084 * so the prefix of the import module itself is returned instead */
1085 return import->prefix;
1086 }
1087
1088 return NULL;
1089}
1090
Radek Krejci0935f412019-08-20 16:15:18 +02001091API const char *
Radek Krejcia3045382018-11-22 14:30:31 +01001092lys_nodetype2str(uint16_t nodetype)
1093{
1094 switch(nodetype) {
1095 case LYS_CONTAINER:
1096 return "container";
1097 case LYS_CHOICE:
1098 return "choice";
1099 case LYS_LEAF:
1100 return "leaf";
1101 case LYS_LEAFLIST:
1102 return "leaf-list";
1103 case LYS_LIST:
1104 return "list";
1105 case LYS_ANYXML:
1106 return "anyxml";
1107 case LYS_ANYDATA:
1108 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001109 case LYS_CASE:
1110 return "case";
Radek Krejcif538ce52019-03-05 10:46:14 +01001111 case LYS_ACTION:
1112 return "RPC/action";
1113 case LYS_NOTIF:
1114 return "Notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001115 case LYS_USES:
1116 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001117 default:
1118 return "unknown";
1119 }
1120}
1121
Radek Krejci693262f2019-04-29 15:23:20 +02001122const char *
1123lys_datatype2str(LY_DATA_TYPE basetype)
1124{
1125 switch(basetype) {
1126 case LY_TYPE_BINARY:
1127 return "binary";
1128 case LY_TYPE_UINT8:
1129 return "uint8";
1130 case LY_TYPE_UINT16:
1131 return "uint16";
1132 case LY_TYPE_UINT32:
1133 return "uint32";
1134 case LY_TYPE_UINT64:
1135 return "uint64";
1136 case LY_TYPE_STRING:
1137 return "string";
1138 case LY_TYPE_BITS:
1139 return "bits";
1140 case LY_TYPE_BOOL:
1141 return "boolean";
1142 case LY_TYPE_DEC64:
1143 return "decimal64";
1144 case LY_TYPE_EMPTY:
1145 return "empty";
1146 case LY_TYPE_ENUM:
1147 return "enumeration";
1148 case LY_TYPE_IDENT:
1149 return "identityref";
1150 case LY_TYPE_INST:
1151 return "instance-identifier";
1152 case LY_TYPE_LEAFREF:
1153 return "leafref";
1154 case LY_TYPE_UNION:
1155 return "union";
1156 case LY_TYPE_INT8:
1157 return "int8";
1158 case LY_TYPE_INT16:
1159 return "int16";
1160 case LY_TYPE_INT32:
1161 return "int32";
1162 case LY_TYPE_INT64:
1163 return "int64";
1164 default:
1165 return "unknown";
1166 }
1167}
1168
Radek Krejci056d0a82018-12-06 16:57:25 +01001169API const struct lysp_tpdf *
1170lysp_node_typedefs(const struct lysp_node *node)
1171{
Radek Krejci0fb28562018-12-13 15:17:37 +01001172 switch (node->nodetype) {
1173 case LYS_CONTAINER:
1174 return ((struct lysp_node_container*)node)->typedefs;
1175 case LYS_LIST:
1176 return ((struct lysp_node_list*)node)->typedefs;
1177 case LYS_GROUPING:
1178 return ((struct lysp_grp*)node)->typedefs;
1179 case LYS_ACTION:
1180 return ((struct lysp_action*)node)->typedefs;
1181 case LYS_INOUT:
1182 return ((struct lysp_action_inout*)node)->typedefs;
1183 case LYS_NOTIF:
1184 return ((struct lysp_notif*)node)->typedefs;
1185 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001186 return NULL;
1187 }
1188}
1189
Radek Krejci53ea6152018-12-13 15:21:15 +01001190API const struct lysp_grp *
1191lysp_node_groupings(const struct lysp_node *node)
1192{
1193 switch (node->nodetype) {
1194 case LYS_CONTAINER:
1195 return ((struct lysp_node_container*)node)->groupings;
1196 case LYS_LIST:
1197 return ((struct lysp_node_list*)node)->groupings;
1198 case LYS_GROUPING:
1199 return ((struct lysp_grp*)node)->groupings;
1200 case LYS_ACTION:
1201 return ((struct lysp_action*)node)->groupings;
1202 case LYS_INOUT:
1203 return ((struct lysp_action_inout*)node)->groupings;
1204 case LYS_NOTIF:
1205 return ((struct lysp_notif*)node)->groupings;
1206 default:
1207 return NULL;
1208 }
1209}
1210
Radek Krejcibbe09a92018-11-08 09:36:54 +01001211struct lysp_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001212lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001213{
1214 assert(node);
1215 switch (node->nodetype) {
1216 case LYS_CONTAINER:
1217 return &((struct lysp_node_container*)node)->actions;
1218 case LYS_LIST:
1219 return &((struct lysp_node_list*)node)->actions;
1220 case LYS_GROUPING:
1221 return &((struct lysp_grp*)node)->actions;
1222 case LYS_AUGMENT:
1223 return &((struct lysp_augment*)node)->actions;
1224 default:
1225 return NULL;
1226 }
1227}
1228
Radek Krejci056d0a82018-12-06 16:57:25 +01001229API const struct lysp_action *
1230lysp_node_actions(const struct lysp_node *node)
1231{
1232 struct lysp_action **actions;
1233 actions = lysp_node_actions_p((struct lysp_node*)node);
1234 if (actions) {
1235 return *actions;
1236 } else {
1237 return NULL;
1238 }
1239}
1240
Radek Krejcibbe09a92018-11-08 09:36:54 +01001241struct lysp_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001242lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001243{
1244 assert(node);
1245 switch (node->nodetype) {
1246 case LYS_CONTAINER:
1247 return &((struct lysp_node_container*)node)->notifs;
1248 case LYS_LIST:
1249 return &((struct lysp_node_list*)node)->notifs;
1250 case LYS_GROUPING:
1251 return &((struct lysp_grp*)node)->notifs;
1252 case LYS_AUGMENT:
1253 return &((struct lysp_augment*)node)->notifs;
1254 default:
1255 return NULL;
1256 }
1257}
1258
Radek Krejci056d0a82018-12-06 16:57:25 +01001259API const struct lysp_notif *
1260lysp_node_notifs(const struct lysp_node *node)
1261{
1262 struct lysp_notif **notifs;
1263 notifs = lysp_node_notifs_p((struct lysp_node*)node);
1264 if (notifs) {
1265 return *notifs;
1266 } else {
1267 return NULL;
1268 }
1269}
1270
Radek Krejcibbe09a92018-11-08 09:36:54 +01001271struct lysp_node **
Radek Krejci056d0a82018-12-06 16:57:25 +01001272lysp_node_children_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001273{
1274 assert(node);
1275 switch (node->nodetype) {
1276 case LYS_CONTAINER:
1277 return &((struct lysp_node_container*)node)->child;
1278 case LYS_CHOICE:
1279 return &((struct lysp_node_choice*)node)->child;
1280 case LYS_LIST:
1281 return &((struct lysp_node_list*)node)->child;
1282 case LYS_CASE:
1283 return &((struct lysp_node_case*)node)->child;
1284 case LYS_GROUPING:
1285 return &((struct lysp_grp*)node)->data;
1286 case LYS_AUGMENT:
1287 return &((struct lysp_augment*)node)->child;
1288 case LYS_INOUT:
1289 return &((struct lysp_action_inout*)node)->data;
1290 case LYS_NOTIF:
1291 return &((struct lysp_notif*)node)->data;
1292 default:
1293 return NULL;
1294 }
1295}
1296
Radek Krejci056d0a82018-12-06 16:57:25 +01001297API const struct lysp_node *
1298lysp_node_children(const struct lysp_node *node)
1299{
1300 struct lysp_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001301
1302 if (!node) {
1303 return NULL;
1304 }
1305
Radek Krejci056d0a82018-12-06 16:57:25 +01001306 children = lysp_node_children_p((struct lysp_node*)node);
1307 if (children) {
1308 return *children;
1309 } else {
1310 return NULL;
1311 }
1312}
1313
1314struct lysc_action **
1315lysc_node_actions_p(struct lysc_node *node)
1316{
1317 assert(node);
1318 switch (node->nodetype) {
1319 case LYS_CONTAINER:
1320 return &((struct lysc_node_container*)node)->actions;
1321 case LYS_LIST:
1322 return &((struct lysc_node_list*)node)->actions;
1323 default:
1324 return NULL;
1325 }
1326}
1327
1328API const struct lysc_action *
1329lysc_node_actions(const struct lysc_node *node)
1330{
1331 struct lysc_action **actions;
1332 actions = lysc_node_actions_p((struct lysc_node*)node);
1333 if (actions) {
1334 return *actions;
1335 } else {
1336 return NULL;
1337 }
1338}
1339
1340struct lysc_notif **
1341lysc_node_notifs_p(struct lysc_node *node)
1342{
1343 assert(node);
1344 switch (node->nodetype) {
1345 case LYS_CONTAINER:
1346 return &((struct lysc_node_container*)node)->notifs;
1347 case LYS_LIST:
1348 return &((struct lysc_node_list*)node)->notifs;
1349 default:
1350 return NULL;
1351 }
1352}
1353
1354API const struct lysc_notif *
1355lysc_node_notifs(const struct lysc_node *node)
1356{
1357 struct lysc_notif **notifs;
1358 notifs = lysc_node_notifs_p((struct lysc_node*)node);
1359 if (notifs) {
1360 return *notifs;
1361 } else {
1362 return NULL;
1363 }
1364}
1365
Radek Krejcibbe09a92018-11-08 09:36:54 +01001366struct lysc_node **
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001367lysc_node_children_p(const struct lysc_node *node, uint16_t flags)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001368{
1369 assert(node);
1370 switch (node->nodetype) {
1371 case LYS_CONTAINER:
1372 return &((struct lysc_node_container*)node)->child;
1373 case LYS_CHOICE:
Radek Krejcia3045382018-11-22 14:30:31 +01001374 if (((struct lysc_node_choice*)node)->cases) {
Radek Krejci95710c92019-02-11 15:49:55 +01001375 return &((struct lysc_node_choice*)node)->cases->child;
Radek Krejcia3045382018-11-22 14:30:31 +01001376 } else {
1377 return NULL;
1378 }
Radek Krejci01342af2019-01-03 15:18:08 +01001379 case LYS_CASE:
1380 return &((struct lysc_node_case*)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001381 case LYS_LIST:
1382 return &((struct lysc_node_list*)node)->child;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001383 case LYS_ACTION:
1384 if (flags & LYS_CONFIG_R) {
1385 return &((struct lysc_action*)node)->output.data;
1386 } else {
1387 /* LYS_CONFIG_W, but also the default case */
1388 return &((struct lysc_action*)node)->input.data;
1389 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02001390 case LYS_NOTIF:
1391 return &((struct lysc_notif*)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001392 default:
1393 return NULL;
1394 }
1395}
1396
Radek Krejci056d0a82018-12-06 16:57:25 +01001397API const struct lysc_node *
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001398lysc_node_children(const struct lysc_node *node, uint16_t flags)
Radek Krejcia3045382018-11-22 14:30:31 +01001399{
Radek Krejci056d0a82018-12-06 16:57:25 +01001400 struct lysc_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001401
1402 if (!node) {
1403 return NULL;
1404 }
1405
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001406 children = lysc_node_children_p((struct lysc_node*)node, flags);
Radek Krejci056d0a82018-12-06 16:57:25 +01001407 if (children) {
1408 return *children;
1409 } else {
Radek Krejcia3045382018-11-22 14:30:31 +01001410 return NULL;
1411 }
1412}
1413
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001414struct lys_module *
1415lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1416{
1417 unsigned int u;
1418
1419 for (u = 0; u < ctx->list.count; ++u) {
1420 if (((struct lys_module*)ctx->list.objs[u])->parsed == mod) {
1421 return ((struct lys_module*)ctx->list.objs[u]);
1422 }
1423 }
1424 return NULL;
1425}
1426
Radek Krejcid6b76452019-09-03 17:03:03 +02001427enum ly_stmt
David Sedlák5f8f0332019-06-18 16:34:30 +02001428lysp_match_kw(struct lys_parser_ctx *ctx, const char **data)
David Sedlákc10e7902018-12-17 02:17:59 +01001429{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001430/**
1431 * @brief Move the DATA pointer by COUNT items. Also updates the indent value in yang parser context
1432 * @param[in] CTX yang parser context to update its indent value.
1433 * @param[in,out] DATA pointer to move
1434 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
1435 */
1436#define MOVE_IN(CTX, DATA, COUNT) (*(DATA))+=COUNT;if(CTX){(CTX)->indent+=COUNT;}
1437#define IF_KW(STR, LEN, STMT) if (!strncmp(*(data), STR, LEN)) {MOVE_IN(ctx, data, LEN);*kw=STMT;}
1438#define IF_KW_PREFIX(STR, LEN) if (!strncmp(*(data), STR, LEN)) {MOVE_IN(ctx, data, LEN);
1439#define IF_KW_PREFIX_END }
David Sedlák572e7ab2019-06-04 16:01:58 +02001440
Radek Krejcid6b76452019-09-03 17:03:03 +02001441 enum ly_stmt result = LY_STMT_NONE;
1442 enum ly_stmt *kw = &result;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001443 /* read the keyword itself */
1444 switch (**data) {
David Sedlák23a59a62018-10-26 13:08:02 +02001445 case 'a':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001446 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001447 IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1448 else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1449 else IF_KW("ction", 5, LY_STMT_ACTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001450 else IF_KW_PREFIX("ny", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001451 IF_KW("data", 4, LY_STMT_ANYDATA)
1452 else IF_KW("xml", 3, LY_STMT_ANYXML)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001453 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001454 break;
1455 case 'b':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001456 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001457 IF_KW("ase", 3, LY_STMT_BASE)
1458 else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1459 else IF_KW("it", 2, LY_STMT_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001460 break;
1461 case 'c':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001462 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001463 IF_KW("ase", 3, LY_STMT_CASE)
1464 else IF_KW("hoice", 5, LY_STMT_CHOICE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001465 else IF_KW_PREFIX("on", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001466 IF_KW("fig", 3, LY_STMT_CONFIG)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001467 else IF_KW_PREFIX("ta", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001468 IF_KW("ct", 2, LY_STMT_CONTACT)
1469 else IF_KW("iner", 4, LY_STMT_CONTAINER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001470 IF_KW_PREFIX_END
1471 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001472 break;
1473 case 'd':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001474 MOVE_IN(ctx, data, 1);
1475 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001476 IF_KW("fault", 5, LY_STMT_DEFAULT)
1477 else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001478 else IF_KW_PREFIX("viat", 4)
Radek Krejcid6b76452019-09-03 17:03:03 +02001479 IF_KW("e", 1, LY_STMT_DEVIATE)
1480 else IF_KW("ion", 3, LY_STMT_DEVIATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001481 IF_KW_PREFIX_END
1482 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001483 break;
1484 case 'e':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001485 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001486 IF_KW("num", 3, LY_STMT_ENUM)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001487 else IF_KW_PREFIX("rror-", 5)
Radek Krejcid6b76452019-09-03 17:03:03 +02001488 IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1489 else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001490 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001491 else IF_KW("xtension", 8, LY_STMT_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001492 break;
1493 case 'f':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001494 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001495 IF_KW("eature", 6, LY_STMT_FEATURE)
1496 else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001497 break;
1498 case 'g':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001499 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001500 IF_KW("rouping", 7, LY_STMT_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001501 break;
1502 case 'i':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001503 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001504 IF_KW("dentity", 7, LY_STMT_IDENTITY)
1505 else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1506 else IF_KW("mport", 5, LY_STMT_IMPORT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001507 else IF_KW_PREFIX("n", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001508 IF_KW("clude", 5, LY_STMT_INCLUDE)
1509 else IF_KW("put", 3, LY_STMT_INPUT)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001510 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001511 break;
1512 case 'k':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001513 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001514 IF_KW("ey", 2, LY_STMT_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001515 break;
1516 case 'l':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001517 MOVE_IN(ctx, data, 1);
1518 IF_KW_PREFIX("e", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001519 IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1520 else IF_KW("af", 2, LY_STMT_LEAF)
1521 else IF_KW("ngth", 4, LY_STMT_LENGTH)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001522 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001523 else IF_KW("ist", 3, LY_STMT_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001524 break;
1525 case 'm':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001526 MOVE_IN(ctx, data, 1);
1527 IF_KW_PREFIX("a", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001528 IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1529 else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001530 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001531 else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1532 else IF_KW("ust", 3, LY_STMT_MUST)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001533 else IF_KW_PREFIX("od", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001534 IF_KW("ule", 3, LY_STMT_MODULE)
1535 else IF_KW("ifier", 5, LY_STMT_MODIFIER)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001536 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001537 break;
1538 case 'n':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001539 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001540 IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1541 else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001542 break;
1543 case 'o':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001544 MOVE_IN(ctx, data, 1);
1545 IF_KW_PREFIX("r", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001546 IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1547 else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001548 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001549 else IF_KW("utput", 5, LY_STMT_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001550 break;
1551 case 'p':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001552 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001553 IF_KW("ath", 3, LY_STMT_PATH)
1554 else IF_KW("attern", 6, LY_STMT_PATTERN)
1555 else IF_KW("osition", 7, LY_STMT_POSITION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001556 else IF_KW_PREFIX("re", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001557 IF_KW("fix", 3, LY_STMT_PREFIX)
1558 else IF_KW("sence", 5, LY_STMT_PRESENCE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001559 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001560 break;
1561 case 'r':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001562 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001563 IF_KW("ange", 4, LY_STMT_RANGE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001564 else IF_KW_PREFIX("e", 1)
1565 IF_KW_PREFIX("f", 1)
Radek Krejcid6b76452019-09-03 17:03:03 +02001566 IF_KW("erence", 6, LY_STMT_REFERENCE)
1567 else IF_KW("ine", 3, LY_STMT_REFINE)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001568 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001569 else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1570 else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1571 else IF_KW("vision", 6, LY_STMT_REVISION)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001572 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001573 else IF_KW("pc", 2, LY_STMT_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001574 break;
1575 case 's':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001576 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001577 IF_KW("tatus", 5, LY_STMT_STATUS)
1578 else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001579 break;
1580 case 't':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001581 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001582 IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1583 else IF_KW("ype", 3, LY_STMT_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001584 break;
1585 case 'u':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001586 MOVE_IN(ctx, data, 1);
1587 IF_KW_PREFIX("ni", 2)
Radek Krejcid6b76452019-09-03 17:03:03 +02001588 IF_KW("que", 3, LY_STMT_UNIQUE)
1589 else IF_KW("ts", 2, LY_STMT_UNITS)
David Sedlák1bccdfa2019-06-17 15:55:27 +02001590 IF_KW_PREFIX_END
Radek Krejcid6b76452019-09-03 17:03:03 +02001591 else IF_KW("ses", 3, LY_STMT_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001592 break;
1593 case 'v':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001594 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001595 IF_KW("alue", 4, LY_STMT_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001596 break;
1597 case 'w':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001598 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001599 IF_KW("hen", 3, LY_STMT_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001600 break;
1601 case 'y':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001602 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001603 IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
1604 else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001605 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001606 default:
David Sedlák1bccdfa2019-06-17 15:55:27 +02001607 /* if context is not NULL we are matching keyword from YANG data*/
1608 if (ctx) {
1609 if (**data == ';') {
1610 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001611 *kw = LY_STMT_SYNTAX_SEMICOLON;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001612 } else if (**data == '{') {
1613 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001614 *kw = LY_STMT_SYNTAX_LEFT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001615 } else if (**data == '}') {
1616 MOVE_IN(ctx, data, 1);
Radek Krejcid6b76452019-09-03 17:03:03 +02001617 *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
David Sedlák1bccdfa2019-06-17 15:55:27 +02001618 }
1619 }
David Sedlák23a59a62018-10-26 13:08:02 +02001620 break;
1621 }
1622
David Sedlák1bccdfa2019-06-17 15:55:27 +02001623#undef IF_KW
1624#undef IF_KW_PREFIX
1625#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01001626#undef MOVE_IN
David Sedlák18730132019-03-15 15:51:34 +01001627
David Sedlák1bccdfa2019-06-17 15:55:27 +02001628 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02001629}
David Sedlákecf5eb82019-06-03 14:12:44 +02001630
Radek Krejcid3ca0632019-04-16 16:54:54 +02001631unsigned int
1632lysp_ext_instance_iter(struct lysp_ext_instance *ext, unsigned int index, LYEXT_SUBSTMT substmt)
1633{
1634 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1635
1636 for (; index < LY_ARRAY_SIZE(ext); index++) {
1637 if (ext[index].insubstmt == substmt) {
1638 return index;
1639 }
1640 }
1641
1642 return LY_ARRAY_SIZE(ext);
1643}
1644
Radek Krejcia1911222019-07-22 17:24:50 +02001645/**
1646 * @brief Schema mapping of YANG modules to prefixes in values.
1647 *
1648 * Implementation of ly_clb_get_prefix. Inverse function to lys_resolve_prefix.
1649 *
1650 * In this case the @p mod is searched in the list of imports and the import's prefix
1651 * (not the module's itself) prefix is returned.
1652 */
1653const char *
1654lys_get_prefix(const struct lys_module *mod, void *private)
1655{
1656 struct lys_module *context_mod = (struct lys_module*)private;
1657 unsigned int u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001658
Radek Krejci474f9b82019-07-24 11:36:37 +02001659 if (context_mod == mod) {
1660 return context_mod->prefix;
1661 }
Radek Krejcia1911222019-07-22 17:24:50 +02001662 LY_ARRAY_FOR(context_mod->compiled->imports, u) {
1663 if (context_mod->compiled->imports[u].module == mod) {
1664 /* match */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02001665 return context_mod->compiled->imports[u].prefix;
Radek Krejcia1911222019-07-22 17:24:50 +02001666 }
1667 }
1668
1669 return NULL;
1670}
1671
1672/**
1673 * @brief Schema mapping of prefix in values to YANG modules (imports).
1674 *
1675 * Implementation of ly_clb_resolve_prefix. Inverse function to lys_get_prefix().
1676 *
1677 * In this case the @p prefix is searched in the list of imports' prefixes (not the prefixes of the imported modules themselves).
1678 */
1679const struct lys_module *
1680lys_resolve_prefix(struct ly_ctx *UNUSED(ctx), const char *prefix, size_t prefix_len, void *private)
1681{
1682 return lys_module_find_prefix((const struct lys_module*)private, prefix, prefix_len);
1683}