blob: c367f9658a4d68c5516de735ba66307375bab164 [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"
29#include "extensions.h"
30#include "hash_table.h"
31#include "log.h"
32#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
37LY_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 */
95 ly_ctx_module_implement_internal(ctx->ctx, (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 */
99 if (!strncmp("input", name, name_len)) {
100 (*result_flag) |= LYSC_OPT_RPC_INPUT;
Radek Krejci05b774b2019-02-25 13:26:18 +0100101 } else if (!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
Radek Krejci86d106e2018-10-18 09:53:19 +0200153LY_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)
Radek Krejci151a5b72018-10-19 14:21:44 +0200266{
Radek Krejci0fb28562018-12-13 15:17:37 +0100267 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100268 unsigned int u;
Radek Krejci151a5b72018-10-19 14:21:44 +0200269
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 Krejci151a5b72018-10-19 14:21:44 +0200275 }
276 }
277
278 return NULL;
279}
David Sedlák23a59a62018-10-26 13:08:02 +0200280
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
427/*
428 * @brief Check name of a new type to avoid name collisions.
429 *
430 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
431 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
432 * @param[in] tpdf Typedef definition to check.
433 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
434 * typedefs are checked, caller is supposed to free the table.
435 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
436 * typedefs are checked, caller is supposed to free the table.
437 * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise.
438 */
439static LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200440lysp_check_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
Radek Krejcibbe09a92018-11-08 09:36:54 +0100441 struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped)
442{
443 struct lysp_node *parent;
444 uint32_t hash;
445 size_t name_len;
446 const char *name;
447 unsigned int u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100448 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100449
450 assert(ctx);
451 assert(tpdf);
452
453 name = tpdf->name;
454 name_len = strlen(name);
455
Radek Krejci4f28eda2018-11-12 11:46:16 +0100456 if (lysp_type_str2builtin(name, name_len)) {
457 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
458 "Invalid name \"%s\" of typedef - name collision with a built-in type.", name);
459 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100460 }
461
462 /* check locally scoped typedefs (avoid name shadowing) */
463 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100464 typedefs = lysp_node_typedefs(node);
465 LY_ARRAY_FOR(typedefs, u) {
466 if (&typedefs[u] == tpdf) {
467 break;
468 }
469 if (!strcmp(name, typedefs[u].name)) {
470 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
471 "Invalid name \"%s\" of typedef - name collision with sibling type.", name);
472 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100473 }
474 }
475 /* search typedefs in parent's nodes */
Radek Krejci87e78ca2019-05-02 09:51:29 +0200476 for (parent = node->parent; parent; parent = parent->parent) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100477 if (lysp_type_match(name, parent)) {
478 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
479 "Invalid name \"%s\" of typedef - name collision with another scoped type.", name);
480 return LY_EEXIST;
481 }
482 }
483 }
484
485 /* check collision with the top-level typedefs */
486 hash = dict_hash(name, name_len);
487 if (node) {
488 lyht_insert(tpdfs_scoped, &name, hash, NULL);
489 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
490 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
491 "Invalid name \"%s\" of typedef - scoped type collide with a top-level type.", name);
492 return LY_EEXIST;
493 }
494 } else {
495 if (lyht_insert(tpdfs_global, &name, hash, NULL)) {
496 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
497 "Invalid name \"%s\" of typedef - name collision with another top-level type.", name);
498 return LY_EEXIST;
499 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100500 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
501 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
502 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100503 }
504
505 return LY_SUCCESS;
506}
507
508static int
509lysp_id_cmp(void *val1, void *val2, int UNUSED(mod), void *UNUSED(cb_data))
510{
511 return !strcmp(val1, val2);
512}
513
514LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200515lysp_check_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100516{
517 struct hash_table *ids_global;
518 struct hash_table *ids_scoped;
Radek Krejci0fb28562018-12-13 15:17:37 +0100519 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100520 unsigned int i, u;
521 LY_ERR ret = LY_EVALID;
522
523 /* check name collisions - typedefs and groupings */
524 ids_global = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1);
525 ids_scoped = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100526 LY_ARRAY_FOR(mod->typedefs, i) {
527 if (lysp_check_typedef(ctx, NULL, &mod->typedefs[i], ids_global, ids_scoped)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100528 goto cleanup;
529 }
530 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100531 LY_ARRAY_FOR(mod->includes, i) {
532 LY_ARRAY_FOR(mod->includes[i].submodule->typedefs, u) {
533 if (lysp_check_typedef(ctx, NULL, &mod->includes[i].submodule->typedefs[u], ids_global, ids_scoped)) {
Radek Krejci3b1f9292018-11-08 10:58:35 +0100534 goto cleanup;
535 }
536 }
537 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100538 for (u = 0; u < ctx->tpdfs_nodes.count; ++u) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100539 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[u]);
540 LY_ARRAY_FOR(typedefs, i) {
541 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 +0100542 goto cleanup;
543 }
544 }
545 }
546 ret = LY_SUCCESS;
547cleanup:
548 lyht_free(ids_global);
549 lyht_free(ids_scoped);
550 ly_set_erase(&ctx->tpdfs_nodes, NULL);
551
552 return ret;
553}
554
Radek Krejci9ed7a192018-10-31 16:23:51 +0100555struct lysp_load_module_check_data {
556 const char *name;
557 const char *revision;
558 const char *path;
559 const char* submoduleof;
560};
561
562static LY_ERR
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100563lysp_load_module_check(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100564{
565 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100566 const char *filename, *dot, *rev, *name;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100567 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100568 struct lysp_revision *revs;
569
570 name = mod ? mod->mod->name : submod->name;
571 revs = mod ? mod->revs : submod->revs;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100572
573 if (info->name) {
574 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100575 if (strcmp(info->name, name)) {
576 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100577 return LY_EINVAL;
578 }
579 }
580 if (info->revision) {
581 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100582 if (!revs || strcmp(info->revision, revs[0].date)) {
583 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Radek Krejcib07b5c92019-04-08 10:56:37 +0200584 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100585 return LY_EINVAL;
586 }
587 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100588 if (submod) {
589 assert(info->submoduleof);
590
Radek Krejci9ed7a192018-10-31 16:23:51 +0100591 /* check that the submodule belongs-to our module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100592 if (strcmp(info->submoduleof, submod->belongsto)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100593 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 +0100594 submod->name, info->submoduleof, submod->belongsto);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100595 return LY_EVALID;
596 }
597 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100598 if (submod->parsing) {
599 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100600 return LY_EVALID;
601 }
602 }
603 if (info->path) {
604 /* check that name and revision match filename */
605 filename = strrchr(info->path, '/');
606 if (!filename) {
607 filename = info->path;
608 } else {
609 filename++;
610 }
611 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100612 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100613 rev = strchr(filename, '@');
614 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100615 if (strncmp(filename, name, len) ||
Radek Krejci9ed7a192018-10-31 16:23:51 +0100616 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100617 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100618 }
619 /* revision */
620 if (rev) {
621 len = dot - ++rev;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100622 if (!revs || len != 10 || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100623 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100624 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100625 }
626 }
627 }
628 return LY_SUCCESS;
629}
630
631LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200632lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, int implement, struct lys_parser_ctx *main_ctx,
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100633 void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100634{
635 int fd;
636 char *filepath = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100637 const char **fp;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100638 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100639 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100640 LY_ERR ret = LY_SUCCESS;
641 struct lysp_load_module_check_data check_data = {0};
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100642 char rpath[PATH_MAX];
Radek Krejci9ed7a192018-10-31 16:23:51 +0100643
644 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, revision,
645 &filepath, &format));
646 LY_CHECK_ERR_RET(!filepath, LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.",
647 name, revision ? "@" : "", revision ? revision : ""), LY_ENOTFOUND);
648
649
650 LOGVRB("Loading schema from \"%s\" file.", filepath);
651
652 /* open the file */
653 fd = open(filepath, O_RDONLY);
654 LY_CHECK_ERR_GOTO(fd < 0, LOGERR(ctx, LY_ESYS, "Unable to open data model file \"%s\" (%s).",
655 filepath, strerror(errno)); ret = LY_ESYS, cleanup);
656
657 check_data.name = name;
658 check_data.revision = revision;
659 check_data.path = filepath;
Radek Krejci3b1f9292018-11-08 10:58:35 +0100660 mod = lys_parse_fd_(ctx, fd, format, implement, main_ctx,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100661 lysp_load_module_check, &check_data);
662 close(fd);
663 LY_CHECK_ERR_GOTO(!mod, ly_errcode(ctx), cleanup);
664
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100665 if (main_ctx) {
666 fp = &((struct lysp_submodule*)mod)->filepath;
667 } else {
668 fp = &((struct lys_module*)mod)->filepath;
669 }
670 if (!(*fp)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100671 if (realpath(filepath, rpath) != NULL) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100672 (*fp) = lydict_insert(ctx, rpath, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100673 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100674 (*fp) = lydict_insert(ctx, filepath, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100675 }
676 }
677
678 *result = mod;
679
680 /* success */
681cleanup:
682 free(filepath);
683 return ret;
684}
685
Radek Krejcid33273d2018-10-25 14:55:52 +0200686LY_ERR
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100687lysp_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 +0200688{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100689 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200690 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100691 void (*module_data_free)(void *module_data, void *user_data) = NULL;
692 struct lysp_load_module_check_data check_data = {0};
Radek Krejci0af46292019-01-11 16:02:31 +0100693 struct lys_module *m;
Radek Krejci086c7132018-10-26 15:29:04 +0200694
Radek Krejci0af46292019-01-11 16:02:31 +0100695 assert(mod);
696
697 if (!*mod) {
698 /* try to get the module from the context */
699 if (revision) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200700 /* get the specific revision */
Radek Krejci0af46292019-01-11 16:02:31 +0100701 *mod = (struct lys_module*)ly_ctx_get_module(ctx, name, revision);
Radek Krejcied5acc52019-04-25 15:57:04 +0200702 } else if (implement) {
703 /* prefer the implemented module instead of the latest one */
704 *mod = (struct lys_module*)ly_ctx_get_module_implemented(ctx, name);
705 if (!*mod) {
706 /* there is no implemented module in the context, try to get the latest revision module */
707 goto latest_in_the_context;
708 }
Radek Krejci0af46292019-01-11 16:02:31 +0100709 } else {
Radek Krejcied5acc52019-04-25 15:57:04 +0200710 /* get the requested module of the latest revision in the context */
711latest_in_the_context:
Radek Krejci0af46292019-01-11 16:02:31 +0100712 *mod = (struct lys_module*)ly_ctx_get_module_latest(ctx, name);
713 }
Radek Krejci086c7132018-10-26 15:29:04 +0200714 }
715
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100716 if (!(*mod) || (require_parsed && !(*mod)->parsed)) {
717 (*mod) = NULL;
718
Radek Krejci086c7132018-10-26 15:29:04 +0200719 /* check collision with other implemented revision */
720 if (implement && ly_ctx_get_module_implemented(ctx, name)) {
721 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
722 "Module \"%s\" is already present in other implemented revision.", name);
723 return LY_EDENIED;
724 }
725
Radek Krejci9ed7a192018-10-31 16:23:51 +0100726 /* module not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200727 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
728search_clb:
729 if (ctx->imp_clb) {
730 if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100731 &format, &module_data, &module_data_free) == LY_SUCCESS) {
732 check_data.name = name;
733 check_data.revision = revision;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100734 *mod = lys_parse_mem_module(ctx, module_data, format, implement,
735 lysp_load_module_check, &check_data);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100736 if (module_data_free) {
737 module_data_free((void*)module_data, ctx->imp_clb_data);
738 }
Radek Krejci096235c2019-01-11 11:12:19 +0100739 if (*mod && implement && lys_compile(*mod, 0)) {
740 ly_set_rm(&ctx->list, *mod, NULL);
741 lys_module_free(*mod, NULL);
742 *mod = NULL;
743 }
Radek Krejci086c7132018-10-26 15:29:04 +0200744 }
745 }
746 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
747 goto search_file;
748 }
749 } else {
750search_file:
751 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
752 /* module was not received from the callback or there is no callback set */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100753 lys_module_localfile(ctx, name, revision, implement, NULL, (void **)mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200754 }
755 if (!(*mod) && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
756 goto search_clb;
757 }
758 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100759
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100760 if ((*mod) && !revision && ((*mod)->latest_revision == 1)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100761 /* update the latest_revision flag - here we have selected the latest available schema,
762 * consider that even the callback provides correct latest revision */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100763 (*mod)->latest_revision = 2;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100764 }
Radek Krejci086c7132018-10-26 15:29:04 +0200765 } else {
766 /* we have module from the current context */
Radek Krejci0af46292019-01-11 16:02:31 +0100767 if (implement) {
768 m = ly_ctx_get_module_implemented(ctx, name);
769 if (m && m != *mod) {
770 /* check collision with other implemented revision */
771 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
772 "Module \"%s\" is already present in other implemented revision.", name);
773 *mod = NULL;
774 return LY_EDENIED;
775 }
Radek Krejci086c7132018-10-26 15:29:04 +0200776 }
777
778 /* circular check */
Radek Krejcif8f882a2018-10-31 14:51:15 +0100779 if ((*mod)->parsed && (*mod)->parsed->parsing) {
Radek Krejci086c7132018-10-26 15:29:04 +0200780 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
781 *mod = NULL;
782 return LY_EVALID;
783 }
784 }
785 if (!(*mod)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100786 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "%s \"%s\" module failed.", implement ? "Loading" : "Importing", name);
Radek Krejci086c7132018-10-26 15:29:04 +0200787 return LY_EVALID;
788 }
789
790 if (implement) {
791 /* mark the module implemented, check for collision was already done */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100792 (*mod)->implemented = 1;
Radek Krejci086c7132018-10-26 15:29:04 +0200793 }
Radek Krejci086c7132018-10-26 15:29:04 +0200794
795 return LY_SUCCESS;
796}
797
798LY_ERR
David Sedlák4a650532019-07-10 11:55:18 +0200799lysp_check_stringchar(struct lys_parser_ctx *ctx, unsigned int c)
800{
801 if (!is_yangutf8char(c)) {
802 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
803 return LY_EVALID;
804 }
805 return LY_SUCCESS;
806}
807
808LY_ERR
809lysp_check_identifierchar(struct lys_parser_ctx *ctx, unsigned int c, int first, int *prefix)
810{
811 if (first || (prefix && (*prefix) == 1)) {
812 if (!is_yangidentstartchar(c)) {
813 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c'.", c);
814 return LY_EVALID;
815 }
816 if (prefix) {
817 if (first) {
818 (*prefix) = 0;
819 } else {
820 (*prefix) = 2;
821 }
822 }
823 } else if (c == ':' && prefix && (*prefix) == 0) {
824 (*prefix) = 1;
825 } else if (!is_yangidentchar(c)) {
826 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c'.", c);
827 return LY_EVALID;
828 }
829
830 return LY_SUCCESS;
831}
832
833LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200834lysp_load_submodule(struct lys_parser_ctx *ctx, struct lysp_module *mod, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200835{
Radek Krejci3eb299d2019-04-08 15:07:44 +0200836 struct lysp_submodule *submod = NULL;
Radek Krejcid33273d2018-10-25 14:55:52 +0200837 const char *submodule_data = NULL;
838 LYS_INFORMAT format = LYS_IN_UNKNOWN;
839 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100840 struct lysp_load_module_check_data check_data = {0};
Radek Krejcid33273d2018-10-25 14:55:52 +0200841
Radek Krejcibbe09a92018-11-08 09:36:54 +0100842 /* submodule not present in the context, get the input data and parse it */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100843 if (!(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200844search_clb:
Radek Krejci3b1f9292018-11-08 10:58:35 +0100845 if (ctx->ctx->imp_clb) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100846 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 +0100847 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
848 check_data.name = inc->name;
849 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100850 check_data.submoduleof = mod->mod->name;
851 submod = lys_parse_mem_submodule(ctx->ctx, submodule_data, format, ctx,
852 lysp_load_module_check, &check_data);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100853 if (submodule_data_free) {
Radek Krejci3b1f9292018-11-08 10:58:35 +0100854 submodule_data_free((void*)submodule_data, ctx->ctx->imp_clb_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200855 }
856 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200857 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100858 if (!submod && !(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100859 goto search_file;
Radek Krejcid33273d2018-10-25 14:55:52 +0200860 }
Radek Krejci2d31ea72018-10-25 15:46:42 +0200861 } else {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100862search_file:
Radek Krejci3b1f9292018-11-08 10:58:35 +0100863 if (!(ctx->ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100864 /* submodule was not received from the callback or there is no callback set */
865 lys_module_localfile(ctx->ctx, inc->name, inc->rev[0] ? inc->rev : NULL, 0, ctx, (void**)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100866 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100867 if (!submod && (ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100868 goto search_clb;
869 }
870 }
871 if (submod) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100872 if (!inc->rev[0] && (submod->latest_revision == 1)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100873 /* update the latest_revision flag - here we have selected the latest available schema,
874 * consider that even the callback provides correct latest revision */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100875 submod->latest_revision = 2;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100876 }
877
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100878 inc->submodule = submod;
Radek Krejcid33273d2018-10-25 14:55:52 +0200879 }
880 if (!inc->submodule) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100881 LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.",
882 inc->name, mod->mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200883 return LY_EVALID;
884 }
885
886 return LY_SUCCESS;
887}
888
Radek Krejci01342af2019-01-03 15:18:08 +0100889#define FIND_MODULE(TYPE, MOD) \
Radek Krejcice8c1592018-10-29 15:35:51 +0100890 TYPE *imp; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100891 if (!strncmp((MOD)->mod->prefix, prefix, len) && (MOD)->mod->prefix[len] == '\0') { \
Radek Krejcice8c1592018-10-29 15:35:51 +0100892 /* it is the prefix of the module itself */ \
Radek Krejci0af46292019-01-11 16:02:31 +0100893 m = ly_ctx_get_module((MOD)->mod->ctx, (MOD)->mod->name, (MOD)->mod->revision); \
Radek Krejcice8c1592018-10-29 15:35:51 +0100894 } \
895 /* search in imports */ \
Radek Krejcibbe09a92018-11-08 09:36:54 +0100896 if (!m) { \
897 LY_ARRAY_FOR((MOD)->imports, TYPE, imp) { \
Radek Krejci01342af2019-01-03 15:18:08 +0100898 if (!strncmp(imp->prefix, prefix, len) && imp->prefix[len] == '\0') { \
Radek Krejcibbe09a92018-11-08 09:36:54 +0100899 m = imp->module; \
900 break; \
901 } \
Radek Krejcice8c1592018-10-29 15:35:51 +0100902 } \
Radek Krejcibbe09a92018-11-08 09:36:54 +0100903 }
Radek Krejcice8c1592018-10-29 15:35:51 +0100904
Radek Krejcibbe09a92018-11-08 09:36:54 +0100905struct lysc_module *
Radek Krejcia3045382018-11-22 14:30:31 +0100906lysc_module_find_prefix(const struct lysc_module *mod, const char *prefix, size_t len)
Radek Krejci86d106e2018-10-18 09:53:19 +0200907{
Radek Krejcibbe09a92018-11-08 09:36:54 +0100908 const struct lys_module *m = NULL;
909
Radek Krejci01342af2019-01-03 15:18:08 +0100910 FIND_MODULE(struct lysc_import, mod);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100911 return m ? m->compiled : NULL;
Radek Krejcice8c1592018-10-29 15:35:51 +0100912}
Radek Krejci86d106e2018-10-18 09:53:19 +0200913
Radek Krejcibbe09a92018-11-08 09:36:54 +0100914struct lysp_module *
Radek Krejcia3045382018-11-22 14:30:31 +0100915lysp_module_find_prefix(const struct lysp_module *mod, const char *prefix, size_t len)
Radek Krejcice8c1592018-10-29 15:35:51 +0100916{
Radek Krejcibbe09a92018-11-08 09:36:54 +0100917 const struct lys_module *m = NULL;
918
Radek Krejci01342af2019-01-03 15:18:08 +0100919 FIND_MODULE(struct lysp_import, mod);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100920 return m ? m->parsed : NULL;
Radek Krejcice8c1592018-10-29 15:35:51 +0100921}
Radek Krejci86d106e2018-10-18 09:53:19 +0200922
Radek Krejcice8c1592018-10-29 15:35:51 +0100923struct lys_module *
Radek Krejcia3045382018-11-22 14:30:31 +0100924lys_module_find_prefix(const struct lys_module *mod, const char *prefix, size_t len)
Radek Krejcice8c1592018-10-29 15:35:51 +0100925{
Radek Krejcibbe09a92018-11-08 09:36:54 +0100926 const struct lys_module *m = NULL;
927
Radek Krejcice8c1592018-10-29 15:35:51 +0100928 if (mod->compiled) {
Radek Krejci01342af2019-01-03 15:18:08 +0100929 FIND_MODULE(struct lysc_import, mod->compiled);
Radek Krejcice8c1592018-10-29 15:35:51 +0100930 } else {
Radek Krejci01342af2019-01-03 15:18:08 +0100931 FIND_MODULE(struct lysp_import, mod->parsed);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100932 }
933 return (struct lys_module*)m;
934}
935
Radek Krejcia3045382018-11-22 14:30:31 +0100936const char *
Radek Krejci693262f2019-04-29 15:23:20 +0200937lys_prefix_find_module(const struct lys_module *mod, const struct lys_module *import)
938{
939 unsigned int u;
940
941 if (import == mod) {
942 return mod->prefix;
943 }
944
945 if (mod->parsed) {
946 LY_ARRAY_FOR(mod->parsed->imports, u) {
947 if (mod->parsed->imports[u].module == import) {
948 return mod->parsed->imports[u].prefix;
949 }
950 }
951 } else {
952 /* we don't have original information about the import's prefix,
953 * so the prefix of the import module itself is returned instead */
954 return import->prefix;
955 }
956
957 return NULL;
958}
959
960const char *
Radek Krejcia3045382018-11-22 14:30:31 +0100961lys_nodetype2str(uint16_t nodetype)
962{
963 switch(nodetype) {
964 case LYS_CONTAINER:
965 return "container";
966 case LYS_CHOICE:
967 return "choice";
968 case LYS_LEAF:
969 return "leaf";
970 case LYS_LEAFLIST:
971 return "leaf-list";
972 case LYS_LIST:
973 return "list";
974 case LYS_ANYXML:
975 return "anyxml";
976 case LYS_ANYDATA:
977 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +0100978 case LYS_CASE:
979 return "case";
Radek Krejcif538ce52019-03-05 10:46:14 +0100980 case LYS_ACTION:
981 return "RPC/action";
982 case LYS_NOTIF:
983 return "Notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +0200984 case LYS_USES:
985 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +0100986 default:
987 return "unknown";
988 }
989}
990
Radek Krejci693262f2019-04-29 15:23:20 +0200991const char *
992lys_datatype2str(LY_DATA_TYPE basetype)
993{
994 switch(basetype) {
995 case LY_TYPE_BINARY:
996 return "binary";
997 case LY_TYPE_UINT8:
998 return "uint8";
999 case LY_TYPE_UINT16:
1000 return "uint16";
1001 case LY_TYPE_UINT32:
1002 return "uint32";
1003 case LY_TYPE_UINT64:
1004 return "uint64";
1005 case LY_TYPE_STRING:
1006 return "string";
1007 case LY_TYPE_BITS:
1008 return "bits";
1009 case LY_TYPE_BOOL:
1010 return "boolean";
1011 case LY_TYPE_DEC64:
1012 return "decimal64";
1013 case LY_TYPE_EMPTY:
1014 return "empty";
1015 case LY_TYPE_ENUM:
1016 return "enumeration";
1017 case LY_TYPE_IDENT:
1018 return "identityref";
1019 case LY_TYPE_INST:
1020 return "instance-identifier";
1021 case LY_TYPE_LEAFREF:
1022 return "leafref";
1023 case LY_TYPE_UNION:
1024 return "union";
1025 case LY_TYPE_INT8:
1026 return "int8";
1027 case LY_TYPE_INT16:
1028 return "int16";
1029 case LY_TYPE_INT32:
1030 return "int32";
1031 case LY_TYPE_INT64:
1032 return "int64";
Radek Krejcibbe09a92018-11-08 09:36:54 +01001033 default:
1034 return "unknown";
1035 }
1036}
1037
Radek Krejci056d0a82018-12-06 16:57:25 +01001038API const struct lysp_tpdf *
1039lysp_node_typedefs(const struct lysp_node *node)
1040{
Radek Krejci0fb28562018-12-13 15:17:37 +01001041 switch (node->nodetype) {
1042 case LYS_CONTAINER:
1043 return ((struct lysp_node_container*)node)->typedefs;
1044 case LYS_LIST:
1045 return ((struct lysp_node_list*)node)->typedefs;
1046 case LYS_GROUPING:
1047 return ((struct lysp_grp*)node)->typedefs;
1048 case LYS_ACTION:
1049 return ((struct lysp_action*)node)->typedefs;
1050 case LYS_INOUT:
1051 return ((struct lysp_action_inout*)node)->typedefs;
1052 case LYS_NOTIF:
1053 return ((struct lysp_notif*)node)->typedefs;
1054 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001055 return NULL;
1056 }
1057}
1058
Radek Krejci53ea6152018-12-13 15:21:15 +01001059API const struct lysp_grp *
1060lysp_node_groupings(const struct lysp_node *node)
1061{
1062 switch (node->nodetype) {
1063 case LYS_CONTAINER:
1064 return ((struct lysp_node_container*)node)->groupings;
1065 case LYS_LIST:
1066 return ((struct lysp_node_list*)node)->groupings;
1067 case LYS_GROUPING:
1068 return ((struct lysp_grp*)node)->groupings;
1069 case LYS_ACTION:
1070 return ((struct lysp_action*)node)->groupings;
1071 case LYS_INOUT:
1072 return ((struct lysp_action_inout*)node)->groupings;
1073 case LYS_NOTIF:
1074 return ((struct lysp_notif*)node)->groupings;
1075 default:
1076 return NULL;
1077 }
1078}
1079
Radek Krejcibbe09a92018-11-08 09:36:54 +01001080struct lysp_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001081lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001082{
1083 assert(node);
1084 switch (node->nodetype) {
1085 case LYS_CONTAINER:
1086 return &((struct lysp_node_container*)node)->actions;
1087 case LYS_LIST:
1088 return &((struct lysp_node_list*)node)->actions;
1089 case LYS_GROUPING:
1090 return &((struct lysp_grp*)node)->actions;
1091 case LYS_AUGMENT:
1092 return &((struct lysp_augment*)node)->actions;
1093 default:
1094 return NULL;
1095 }
1096}
1097
Radek Krejci056d0a82018-12-06 16:57:25 +01001098API const struct lysp_action *
1099lysp_node_actions(const struct lysp_node *node)
1100{
1101 struct lysp_action **actions;
1102 actions = lysp_node_actions_p((struct lysp_node*)node);
1103 if (actions) {
1104 return *actions;
1105 } else {
1106 return NULL;
1107 }
1108}
1109
Radek Krejcibbe09a92018-11-08 09:36:54 +01001110struct lysp_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001111lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001112{
1113 assert(node);
1114 switch (node->nodetype) {
1115 case LYS_CONTAINER:
1116 return &((struct lysp_node_container*)node)->notifs;
1117 case LYS_LIST:
1118 return &((struct lysp_node_list*)node)->notifs;
1119 case LYS_GROUPING:
1120 return &((struct lysp_grp*)node)->notifs;
1121 case LYS_AUGMENT:
1122 return &((struct lysp_augment*)node)->notifs;
1123 default:
1124 return NULL;
1125 }
1126}
1127
Radek Krejci056d0a82018-12-06 16:57:25 +01001128API const struct lysp_notif *
1129lysp_node_notifs(const struct lysp_node *node)
1130{
1131 struct lysp_notif **notifs;
1132 notifs = lysp_node_notifs_p((struct lysp_node*)node);
1133 if (notifs) {
1134 return *notifs;
1135 } else {
1136 return NULL;
1137 }
1138}
1139
Radek Krejcibbe09a92018-11-08 09:36:54 +01001140struct lysp_node **
Radek Krejci056d0a82018-12-06 16:57:25 +01001141lysp_node_children_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001142{
1143 assert(node);
1144 switch (node->nodetype) {
1145 case LYS_CONTAINER:
1146 return &((struct lysp_node_container*)node)->child;
1147 case LYS_CHOICE:
1148 return &((struct lysp_node_choice*)node)->child;
1149 case LYS_LIST:
1150 return &((struct lysp_node_list*)node)->child;
1151 case LYS_CASE:
1152 return &((struct lysp_node_case*)node)->child;
1153 case LYS_GROUPING:
1154 return &((struct lysp_grp*)node)->data;
1155 case LYS_AUGMENT:
1156 return &((struct lysp_augment*)node)->child;
1157 case LYS_INOUT:
1158 return &((struct lysp_action_inout*)node)->data;
1159 case LYS_NOTIF:
1160 return &((struct lysp_notif*)node)->data;
1161 default:
1162 return NULL;
1163 }
1164}
1165
Radek Krejci056d0a82018-12-06 16:57:25 +01001166API const struct lysp_node *
1167lysp_node_children(const struct lysp_node *node)
1168{
1169 struct lysp_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001170
1171 if (!node) {
1172 return NULL;
1173 }
1174
Radek Krejci056d0a82018-12-06 16:57:25 +01001175 children = lysp_node_children_p((struct lysp_node*)node);
1176 if (children) {
1177 return *children;
1178 } else {
1179 return NULL;
1180 }
1181}
1182
1183struct lysc_action **
1184lysc_node_actions_p(struct lysc_node *node)
1185{
1186 assert(node);
1187 switch (node->nodetype) {
1188 case LYS_CONTAINER:
1189 return &((struct lysc_node_container*)node)->actions;
1190 case LYS_LIST:
1191 return &((struct lysc_node_list*)node)->actions;
1192 default:
1193 return NULL;
1194 }
1195}
1196
1197API const struct lysc_action *
1198lysc_node_actions(const struct lysc_node *node)
1199{
1200 struct lysc_action **actions;
1201 actions = lysc_node_actions_p((struct lysc_node*)node);
1202 if (actions) {
1203 return *actions;
1204 } else {
1205 return NULL;
1206 }
1207}
1208
1209struct lysc_notif **
1210lysc_node_notifs_p(struct lysc_node *node)
1211{
1212 assert(node);
1213 switch (node->nodetype) {
1214 case LYS_CONTAINER:
1215 return &((struct lysc_node_container*)node)->notifs;
1216 case LYS_LIST:
1217 return &((struct lysc_node_list*)node)->notifs;
1218 default:
1219 return NULL;
1220 }
1221}
1222
1223API const struct lysc_notif *
1224lysc_node_notifs(const struct lysc_node *node)
1225{
1226 struct lysc_notif **notifs;
1227 notifs = lysc_node_notifs_p((struct lysc_node*)node);
1228 if (notifs) {
1229 return *notifs;
1230 } else {
1231 return NULL;
1232 }
1233}
1234
Radek Krejcibbe09a92018-11-08 09:36:54 +01001235struct lysc_node **
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001236lysc_node_children_p(const struct lysc_node *node, uint16_t flags)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001237{
1238 assert(node);
1239 switch (node->nodetype) {
1240 case LYS_CONTAINER:
1241 return &((struct lysc_node_container*)node)->child;
1242 case LYS_CHOICE:
Radek Krejcia3045382018-11-22 14:30:31 +01001243 if (((struct lysc_node_choice*)node)->cases) {
Radek Krejci95710c92019-02-11 15:49:55 +01001244 return &((struct lysc_node_choice*)node)->cases->child;
Radek Krejcia3045382018-11-22 14:30:31 +01001245 } else {
1246 return NULL;
1247 }
Radek Krejci01342af2019-01-03 15:18:08 +01001248 case LYS_CASE:
1249 return &((struct lysc_node_case*)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001250 case LYS_LIST:
1251 return &((struct lysc_node_list*)node)->child;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001252 case LYS_ACTION:
1253 if (flags & LYS_CONFIG_R) {
1254 return &((struct lysc_action*)node)->output.data;
1255 } else {
1256 /* LYS_CONFIG_W, but also the default case */
1257 return &((struct lysc_action*)node)->input.data;
1258 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02001259 case LYS_NOTIF:
1260 return &((struct lysc_notif*)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001261 default:
1262 return NULL;
1263 }
1264}
1265
Radek Krejci056d0a82018-12-06 16:57:25 +01001266API const struct lysc_node *
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001267lysc_node_children(const struct lysc_node *node, uint16_t flags)
Radek Krejcia3045382018-11-22 14:30:31 +01001268{
Radek Krejci056d0a82018-12-06 16:57:25 +01001269 struct lysc_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001270
1271 if (!node) {
1272 return NULL;
1273 }
1274
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001275 children = lysc_node_children_p((struct lysc_node*)node, flags);
Radek Krejci056d0a82018-12-06 16:57:25 +01001276 if (children) {
1277 return *children;
1278 } else {
Radek Krejcia3045382018-11-22 14:30:31 +01001279 return NULL;
1280 }
1281}
1282
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001283struct lys_module *
1284lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1285{
1286 unsigned int u;
1287
1288 for (u = 0; u < ctx->list.count; ++u) {
1289 if (((struct lys_module*)ctx->list.objs[u])->parsed == mod) {
1290 return ((struct lys_module*)ctx->list.objs[u]);
1291 }
1292 }
1293 return NULL;
1294}
1295
David Sedlákc10e7902018-12-17 02:17:59 +01001296enum yang_keyword
David Sedlák5f8f0332019-06-18 16:34:30 +02001297lysp_match_kw(struct lys_parser_ctx *ctx, const char **data)
David Sedlákc10e7902018-12-17 02:17:59 +01001298{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001299/**
1300 * @brief Move the DATA pointer by COUNT items. Also updates the indent value in yang parser context
1301 * @param[in] CTX yang parser context to update its indent value.
1302 * @param[in,out] DATA pointer to move
1303 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
1304 */
1305#define MOVE_IN(CTX, DATA, COUNT) (*(DATA))+=COUNT;if(CTX){(CTX)->indent+=COUNT;}
1306#define IF_KW(STR, LEN, STMT) if (!strncmp(*(data), STR, LEN)) {MOVE_IN(ctx, data, LEN);*kw=STMT;}
1307#define IF_KW_PREFIX(STR, LEN) if (!strncmp(*(data), STR, LEN)) {MOVE_IN(ctx, data, LEN);
1308#define IF_KW_PREFIX_END }
David Sedlák572e7ab2019-06-04 16:01:58 +02001309
David Sedlák1bccdfa2019-06-17 15:55:27 +02001310 enum yang_keyword result = YANG_NONE;
1311 enum yang_keyword *kw = &result;
1312 /* read the keyword itself */
1313 switch (**data) {
David Sedlák23a59a62018-10-26 13:08:02 +02001314 case 'a':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001315 MOVE_IN(ctx, data, 1);
1316 IF_KW("rgument", 7, YANG_ARGUMENT)
1317 else IF_KW("ugment", 6, YANG_AUGMENT)
1318 else IF_KW("ction", 5, YANG_ACTION)
1319 else IF_KW_PREFIX("ny", 2)
1320 IF_KW("data", 4, YANG_ANYDATA)
1321 else IF_KW("xml", 3, YANG_ANYXML)
1322 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001323 break;
1324 case 'b':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001325 MOVE_IN(ctx, data, 1);
1326 IF_KW("ase", 3, YANG_BASE)
1327 else IF_KW("elongs-to", 9, YANG_BELONGS_TO)
1328 else IF_KW("it", 2, YANG_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001329 break;
1330 case 'c':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001331 MOVE_IN(ctx, data, 1);
1332 IF_KW("ase", 3, YANG_CASE)
1333 else IF_KW("hoice", 5, YANG_CHOICE)
1334 else IF_KW_PREFIX("on", 2)
1335 IF_KW("fig", 3, YANG_CONFIG)
1336 else IF_KW_PREFIX("ta", 2)
1337 IF_KW("ct", 2, YANG_CONTACT)
1338 else IF_KW("iner", 4, YANG_CONTAINER)
1339 IF_KW_PREFIX_END
1340 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001341 break;
1342 case 'd':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001343 MOVE_IN(ctx, data, 1);
1344 IF_KW_PREFIX("e", 1)
1345 IF_KW("fault", 5, YANG_DEFAULT)
1346 else IF_KW("scription", 9, YANG_DESCRIPTION)
1347 else IF_KW_PREFIX("viat", 4)
1348 IF_KW("e", 1, YANG_DEVIATE)
1349 else IF_KW("ion", 3, YANG_DEVIATION)
1350 IF_KW_PREFIX_END
1351 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001352 break;
1353 case 'e':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001354 MOVE_IN(ctx, data, 1);
1355 IF_KW("num", 3, YANG_ENUM)
1356 else IF_KW_PREFIX("rror-", 5)
1357 IF_KW("app-tag", 7, YANG_ERROR_APP_TAG)
1358 else IF_KW("message", 7, YANG_ERROR_MESSAGE)
1359 IF_KW_PREFIX_END
1360 else IF_KW("xtension", 8, YANG_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001361 break;
1362 case 'f':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001363 MOVE_IN(ctx, data, 1);
1364 IF_KW("eature", 6, YANG_FEATURE)
1365 else IF_KW("raction-digits", 14, YANG_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001366 break;
1367 case 'g':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001368 MOVE_IN(ctx, data, 1);
1369 IF_KW("rouping", 7, YANG_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001370 break;
1371 case 'i':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001372 MOVE_IN(ctx, data, 1);
1373 IF_KW("dentity", 7, YANG_IDENTITY)
1374 else IF_KW("f-feature", 9, YANG_IF_FEATURE)
1375 else IF_KW("mport", 5, YANG_IMPORT)
1376 else IF_KW_PREFIX("n", 1)
1377 IF_KW("clude", 5, YANG_INCLUDE)
1378 else IF_KW("put", 3, YANG_INPUT)
1379 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001380 break;
1381 case 'k':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001382 MOVE_IN(ctx, data, 1);
1383 IF_KW("ey", 2, YANG_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001384 break;
1385 case 'l':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001386 MOVE_IN(ctx, data, 1);
1387 IF_KW_PREFIX("e", 1)
1388 IF_KW("af-list", 7, YANG_LEAF_LIST)
1389 else IF_KW("af", 2, YANG_LEAF)
1390 else IF_KW("ngth", 4, YANG_LENGTH)
1391 IF_KW_PREFIX_END
1392 else IF_KW("ist", 3, YANG_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001393 break;
1394 case 'm':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001395 MOVE_IN(ctx, data, 1);
1396 IF_KW_PREFIX("a", 1)
1397 IF_KW("ndatory", 7, YANG_MANDATORY)
1398 else IF_KW("x-elements", 10, YANG_MAX_ELEMENTS)
1399 IF_KW_PREFIX_END
1400 else IF_KW("in-elements", 11, YANG_MIN_ELEMENTS)
1401 else IF_KW("ust", 3, YANG_MUST)
1402 else IF_KW_PREFIX("od", 2)
1403 IF_KW("ule", 3, YANG_MODULE)
1404 else IF_KW("ifier", 5, YANG_MODIFIER)
1405 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001406 break;
1407 case 'n':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001408 MOVE_IN(ctx, data, 1);
1409 IF_KW("amespace", 8, YANG_NAMESPACE)
1410 else IF_KW("otification", 11, YANG_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001411 break;
1412 case 'o':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001413 MOVE_IN(ctx, data, 1);
1414 IF_KW_PREFIX("r", 1)
1415 IF_KW("dered-by", 8, YANG_ORDERED_BY)
1416 else IF_KW("ganization", 10, YANG_ORGANIZATION)
1417 IF_KW_PREFIX_END
1418 else IF_KW("utput", 5, YANG_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001419 break;
1420 case 'p':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001421 MOVE_IN(ctx, data, 1);
1422 IF_KW("ath", 3, YANG_PATH)
1423 else IF_KW("attern", 6, YANG_PATTERN)
1424 else IF_KW("osition", 7, YANG_POSITION)
1425 else IF_KW_PREFIX("re", 2)
1426 IF_KW("fix", 3, YANG_PREFIX)
1427 else IF_KW("sence", 5, YANG_PRESENCE)
1428 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001429 break;
1430 case 'r':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001431 MOVE_IN(ctx, data, 1);
1432 IF_KW("ange", 4, YANG_RANGE)
1433 else IF_KW_PREFIX("e", 1)
1434 IF_KW_PREFIX("f", 1)
1435 IF_KW("erence", 6, YANG_REFERENCE)
1436 else IF_KW("ine", 3, YANG_REFINE)
1437 IF_KW_PREFIX_END
1438 else IF_KW("quire-instance", 14, YANG_REQUIRE_INSTANCE)
1439 else IF_KW("vision-date", 11, YANG_REVISION_DATE)
1440 else IF_KW("vision", 6, YANG_REVISION)
1441 IF_KW_PREFIX_END
1442 else IF_KW("pc", 2, YANG_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001443 break;
1444 case 's':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001445 MOVE_IN(ctx, data, 1);
1446 IF_KW("tatus", 5, YANG_STATUS)
1447 else IF_KW("ubmodule", 8, YANG_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001448 break;
1449 case 't':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001450 MOVE_IN(ctx, data, 1);
1451 IF_KW("ypedef", 6, YANG_TYPEDEF)
1452 else IF_KW("ype", 3, YANG_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001453 break;
1454 case 'u':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001455 MOVE_IN(ctx, data, 1);
1456 IF_KW_PREFIX("ni", 2)
1457 IF_KW("que", 3, YANG_UNIQUE)
1458 else IF_KW("ts", 2, YANG_UNITS)
1459 IF_KW_PREFIX_END
1460 else IF_KW("ses", 3, YANG_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001461 break;
1462 case 'v':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001463 MOVE_IN(ctx, data, 1);
1464 IF_KW("alue", 4, YANG_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001465 break;
1466 case 'w':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001467 MOVE_IN(ctx, data, 1);
1468 IF_KW("hen", 3, YANG_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001469 break;
1470 case 'y':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001471 MOVE_IN(ctx, data, 1);
1472 IF_KW("ang-version", 11, YANG_YANG_VERSION)
1473 else IF_KW("in-element", 10, YANG_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001474 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001475 default:
David Sedlák1bccdfa2019-06-17 15:55:27 +02001476 /* if context is not NULL we are matching keyword from YANG data*/
1477 if (ctx) {
1478 if (**data == ';') {
1479 MOVE_IN(ctx, data, 1);
1480 *kw = YANG_SEMICOLON;
1481 } else if (**data == '{') {
1482 MOVE_IN(ctx, data, 1);
1483 *kw = YANG_LEFT_BRACE;
1484 } else if (**data == '}') {
1485 MOVE_IN(ctx, data, 1);
1486 *kw = YANG_RIGHT_BRACE;
1487 }
1488 }
David Sedlák23a59a62018-10-26 13:08:02 +02001489 break;
1490 }
1491
David Sedlák1bccdfa2019-06-17 15:55:27 +02001492#undef IF_KW
1493#undef IF_KW_PREFIX
1494#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01001495#undef MOVE_IN
David Sedlák18730132019-03-15 15:51:34 +01001496
David Sedlák1bccdfa2019-06-17 15:55:27 +02001497 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02001498}
David Sedlákecf5eb82019-06-03 14:12:44 +02001499
Radek Krejcid3ca0632019-04-16 16:54:54 +02001500unsigned int
1501lysp_ext_instance_iter(struct lysp_ext_instance *ext, unsigned int index, LYEXT_SUBSTMT substmt)
1502{
1503 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1504
1505 for (; index < LY_ARRAY_SIZE(ext); index++) {
1506 if (ext[index].insubstmt == substmt) {
1507 return index;
1508 }
1509 }
1510
1511 return LY_ARRAY_SIZE(ext);
1512}
1513