blob: 2118af77451473a5fe92769d7afc5275b6f53a59 [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
David Sedlák6544c182019-07-12 13:17:33 +0200427LY_ERR
428lysp_check_enum_name(struct lys_parser_ctx *ctx, char *name, size_t name_len)
429{
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
Radek Krejcie7b95092019-05-15 11:03:07 +0200538lysp_check_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100539{
540 struct hash_table *ids_global;
541 struct hash_table *ids_scoped;
Radek Krejci0fb28562018-12-13 15:17:37 +0100542 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100543 unsigned int i, u;
544 LY_ERR ret = LY_EVALID;
545
546 /* check name collisions - typedefs and groupings */
547 ids_global = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1);
548 ids_scoped = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100549 LY_ARRAY_FOR(mod->typedefs, i) {
550 if (lysp_check_typedef(ctx, NULL, &mod->typedefs[i], ids_global, ids_scoped)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100551 goto cleanup;
552 }
553 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100554 LY_ARRAY_FOR(mod->includes, i) {
555 LY_ARRAY_FOR(mod->includes[i].submodule->typedefs, u) {
556 if (lysp_check_typedef(ctx, NULL, &mod->includes[i].submodule->typedefs[u], ids_global, ids_scoped)) {
Radek Krejci3b1f9292018-11-08 10:58:35 +0100557 goto cleanup;
558 }
559 }
560 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100561 for (u = 0; u < ctx->tpdfs_nodes.count; ++u) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100562 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[u]);
563 LY_ARRAY_FOR(typedefs, i) {
564 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 +0100565 goto cleanup;
566 }
567 }
568 }
569 ret = LY_SUCCESS;
570cleanup:
571 lyht_free(ids_global);
572 lyht_free(ids_scoped);
573 ly_set_erase(&ctx->tpdfs_nodes, NULL);
574
575 return ret;
576}
577
Radek Krejci9ed7a192018-10-31 16:23:51 +0100578struct lysp_load_module_check_data {
579 const char *name;
580 const char *revision;
581 const char *path;
582 const char* submoduleof;
583};
584
585static LY_ERR
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100586lysp_load_module_check(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100587{
588 struct lysp_load_module_check_data *info = data;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100589 const char *filename, *dot, *rev, *name;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100590 size_t len;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100591 struct lysp_revision *revs;
592
593 name = mod ? mod->mod->name : submod->name;
594 revs = mod ? mod->revs : submod->revs;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100595
596 if (info->name) {
597 /* check name of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100598 if (strcmp(info->name, name)) {
599 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100600 return LY_EINVAL;
601 }
602 }
603 if (info->revision) {
604 /* check revision of the parsed model */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100605 if (!revs || strcmp(info->revision, revs[0].date)) {
606 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
Radek Krejcib07b5c92019-04-08 10:56:37 +0200607 revs ? revs[0].date : "none", info->revision);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100608 return LY_EINVAL;
609 }
610 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100611 if (submod) {
612 assert(info->submoduleof);
613
Radek Krejci9ed7a192018-10-31 16:23:51 +0100614 /* check that the submodule belongs-to our module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100615 if (strcmp(info->submoduleof, submod->belongsto)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100616 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 +0100617 submod->name, info->submoduleof, submod->belongsto);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100618 return LY_EVALID;
619 }
620 /* check circular dependency */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100621 if (submod->parsing) {
622 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100623 return LY_EVALID;
624 }
625 }
626 if (info->path) {
627 /* check that name and revision match filename */
628 filename = strrchr(info->path, '/');
629 if (!filename) {
630 filename = info->path;
631 } else {
632 filename++;
633 }
634 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100635 len = strlen(name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100636 rev = strchr(filename, '@');
637 dot = strrchr(info->path, '.');
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100638 if (strncmp(filename, name, len) ||
Radek Krejci9ed7a192018-10-31 16:23:51 +0100639 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100640 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100641 }
642 /* revision */
643 if (rev) {
644 len = dot - ++rev;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100645 if (!revs || len != 10 || strncmp(revs[0].date, rev, len)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100646 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100647 revs ? revs[0].date : "none");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100648 }
649 }
650 }
651 return LY_SUCCESS;
652}
653
654LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200655lys_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 +0100656 void **result)
Radek Krejci9ed7a192018-10-31 16:23:51 +0100657{
658 int fd;
659 char *filepath = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100660 const char **fp;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100661 LYS_INFORMAT format;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100662 void *mod = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100663 LY_ERR ret = LY_SUCCESS;
664 struct lysp_load_module_check_data check_data = {0};
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100665 char rpath[PATH_MAX];
Radek Krejci9ed7a192018-10-31 16:23:51 +0100666
667 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, revision,
668 &filepath, &format));
669 LY_CHECK_ERR_RET(!filepath, LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.",
670 name, revision ? "@" : "", revision ? revision : ""), LY_ENOTFOUND);
671
672
673 LOGVRB("Loading schema from \"%s\" file.", filepath);
674
675 /* open the file */
676 fd = open(filepath, O_RDONLY);
677 LY_CHECK_ERR_GOTO(fd < 0, LOGERR(ctx, LY_ESYS, "Unable to open data model file \"%s\" (%s).",
678 filepath, strerror(errno)); ret = LY_ESYS, cleanup);
679
680 check_data.name = name;
681 check_data.revision = revision;
682 check_data.path = filepath;
Radek Krejci3b1f9292018-11-08 10:58:35 +0100683 mod = lys_parse_fd_(ctx, fd, format, implement, main_ctx,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100684 lysp_load_module_check, &check_data);
685 close(fd);
686 LY_CHECK_ERR_GOTO(!mod, ly_errcode(ctx), cleanup);
687
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100688 if (main_ctx) {
689 fp = &((struct lysp_submodule*)mod)->filepath;
690 } else {
691 fp = &((struct lys_module*)mod)->filepath;
692 }
693 if (!(*fp)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100694 if (realpath(filepath, rpath) != NULL) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100695 (*fp) = lydict_insert(ctx, rpath, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100696 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100697 (*fp) = lydict_insert(ctx, filepath, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100698 }
699 }
700
701 *result = mod;
702
703 /* success */
704cleanup:
705 free(filepath);
706 return ret;
707}
708
Radek Krejcid33273d2018-10-25 14:55:52 +0200709LY_ERR
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100710lysp_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 +0200711{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100712 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200713 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100714 void (*module_data_free)(void *module_data, void *user_data) = NULL;
715 struct lysp_load_module_check_data check_data = {0};
Radek Krejci0af46292019-01-11 16:02:31 +0100716 struct lys_module *m;
Radek Krejci086c7132018-10-26 15:29:04 +0200717
Radek Krejci0af46292019-01-11 16:02:31 +0100718 assert(mod);
719
720 if (!*mod) {
721 /* try to get the module from the context */
722 if (revision) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200723 /* get the specific revision */
Radek Krejci0af46292019-01-11 16:02:31 +0100724 *mod = (struct lys_module*)ly_ctx_get_module(ctx, name, revision);
Radek Krejcied5acc52019-04-25 15:57:04 +0200725 } else if (implement) {
726 /* prefer the implemented module instead of the latest one */
727 *mod = (struct lys_module*)ly_ctx_get_module_implemented(ctx, name);
728 if (!*mod) {
729 /* there is no implemented module in the context, try to get the latest revision module */
730 goto latest_in_the_context;
731 }
Radek Krejci0af46292019-01-11 16:02:31 +0100732 } else {
Radek Krejcied5acc52019-04-25 15:57:04 +0200733 /* get the requested module of the latest revision in the context */
734latest_in_the_context:
Radek Krejci0af46292019-01-11 16:02:31 +0100735 *mod = (struct lys_module*)ly_ctx_get_module_latest(ctx, name);
736 }
Radek Krejci086c7132018-10-26 15:29:04 +0200737 }
738
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100739 if (!(*mod) || (require_parsed && !(*mod)->parsed)) {
740 (*mod) = NULL;
741
Radek Krejci086c7132018-10-26 15:29:04 +0200742 /* check collision with other implemented revision */
743 if (implement && ly_ctx_get_module_implemented(ctx, name)) {
744 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
745 "Module \"%s\" is already present in other implemented revision.", name);
746 return LY_EDENIED;
747 }
748
Radek Krejci9ed7a192018-10-31 16:23:51 +0100749 /* module not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200750 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
751search_clb:
752 if (ctx->imp_clb) {
753 if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100754 &format, &module_data, &module_data_free) == LY_SUCCESS) {
755 check_data.name = name;
756 check_data.revision = revision;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100757 *mod = lys_parse_mem_module(ctx, module_data, format, implement,
758 lysp_load_module_check, &check_data);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100759 if (module_data_free) {
760 module_data_free((void*)module_data, ctx->imp_clb_data);
761 }
Radek Krejci096235c2019-01-11 11:12:19 +0100762 if (*mod && implement && lys_compile(*mod, 0)) {
763 ly_set_rm(&ctx->list, *mod, NULL);
764 lys_module_free(*mod, NULL);
765 *mod = NULL;
766 }
Radek Krejci086c7132018-10-26 15:29:04 +0200767 }
768 }
769 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
770 goto search_file;
771 }
772 } else {
773search_file:
774 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
775 /* module was not received from the callback or there is no callback set */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100776 lys_module_localfile(ctx, name, revision, implement, NULL, (void **)mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200777 }
778 if (!(*mod) && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
779 goto search_clb;
780 }
781 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100782
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100783 if ((*mod) && !revision && ((*mod)->latest_revision == 1)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100784 /* update the latest_revision flag - here we have selected the latest available schema,
785 * consider that even the callback provides correct latest revision */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100786 (*mod)->latest_revision = 2;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100787 }
Radek Krejci086c7132018-10-26 15:29:04 +0200788 } else {
789 /* we have module from the current context */
Radek Krejci0af46292019-01-11 16:02:31 +0100790 if (implement) {
791 m = ly_ctx_get_module_implemented(ctx, name);
792 if (m && m != *mod) {
793 /* check collision with other implemented revision */
794 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
795 "Module \"%s\" is already present in other implemented revision.", name);
796 *mod = NULL;
797 return LY_EDENIED;
798 }
Radek Krejci086c7132018-10-26 15:29:04 +0200799 }
800
801 /* circular check */
Radek Krejcif8f882a2018-10-31 14:51:15 +0100802 if ((*mod)->parsed && (*mod)->parsed->parsing) {
Radek Krejci086c7132018-10-26 15:29:04 +0200803 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
804 *mod = NULL;
805 return LY_EVALID;
806 }
807 }
808 if (!(*mod)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100809 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "%s \"%s\" module failed.", implement ? "Loading" : "Importing", name);
Radek Krejci086c7132018-10-26 15:29:04 +0200810 return LY_EVALID;
811 }
812
813 if (implement) {
814 /* mark the module implemented, check for collision was already done */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100815 (*mod)->implemented = 1;
Radek Krejci086c7132018-10-26 15:29:04 +0200816 }
Radek Krejci086c7132018-10-26 15:29:04 +0200817
818 return LY_SUCCESS;
819}
820
821LY_ERR
David Sedlák4a650532019-07-10 11:55:18 +0200822lysp_check_stringchar(struct lys_parser_ctx *ctx, unsigned int c)
823{
824 if (!is_yangutf8char(c)) {
825 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
826 return LY_EVALID;
827 }
828 return LY_SUCCESS;
829}
830
831LY_ERR
832lysp_check_identifierchar(struct lys_parser_ctx *ctx, unsigned int c, int first, int *prefix)
833{
834 if (first || (prefix && (*prefix) == 1)) {
835 if (!is_yangidentstartchar(c)) {
836 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c'.", c);
837 return LY_EVALID;
838 }
839 if (prefix) {
840 if (first) {
841 (*prefix) = 0;
842 } else {
843 (*prefix) = 2;
844 }
845 }
846 } else if (c == ':' && prefix && (*prefix) == 0) {
847 (*prefix) = 1;
848 } else if (!is_yangidentchar(c)) {
849 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c'.", c);
850 return LY_EVALID;
851 }
852
853 return LY_SUCCESS;
854}
855
856LY_ERR
Radek Krejcie7b95092019-05-15 11:03:07 +0200857lysp_load_submodule(struct lys_parser_ctx *ctx, struct lysp_module *mod, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200858{
Radek Krejci3eb299d2019-04-08 15:07:44 +0200859 struct lysp_submodule *submod = NULL;
Radek Krejcid33273d2018-10-25 14:55:52 +0200860 const char *submodule_data = NULL;
861 LYS_INFORMAT format = LYS_IN_UNKNOWN;
862 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100863 struct lysp_load_module_check_data check_data = {0};
Radek Krejcid33273d2018-10-25 14:55:52 +0200864
Radek Krejcibbe09a92018-11-08 09:36:54 +0100865 /* submodule not present in the context, get the input data and parse it */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100866 if (!(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200867search_clb:
Radek Krejci3b1f9292018-11-08 10:58:35 +0100868 if (ctx->ctx->imp_clb) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100869 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 +0100870 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
871 check_data.name = inc->name;
872 check_data.revision = inc->rev[0] ? inc->rev : NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100873 check_data.submoduleof = mod->mod->name;
874 submod = lys_parse_mem_submodule(ctx->ctx, submodule_data, format, ctx,
875 lysp_load_module_check, &check_data);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100876 if (submodule_data_free) {
Radek Krejci3b1f9292018-11-08 10:58:35 +0100877 submodule_data_free((void*)submodule_data, ctx->ctx->imp_clb_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200878 }
879 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200880 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100881 if (!submod && !(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100882 goto search_file;
Radek Krejcid33273d2018-10-25 14:55:52 +0200883 }
Radek Krejci2d31ea72018-10-25 15:46:42 +0200884 } else {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100885search_file:
Radek Krejci3b1f9292018-11-08 10:58:35 +0100886 if (!(ctx->ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100887 /* submodule was not received from the callback or there is no callback set */
888 lys_module_localfile(ctx->ctx, inc->name, inc->rev[0] ? inc->rev : NULL, 0, ctx, (void**)&submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100889 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100890 if (!submod && (ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100891 goto search_clb;
892 }
893 }
894 if (submod) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100895 if (!inc->rev[0] && (submod->latest_revision == 1)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100896 /* update the latest_revision flag - here we have selected the latest available schema,
897 * consider that even the callback provides correct latest revision */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100898 submod->latest_revision = 2;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100899 }
900
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100901 inc->submodule = submod;
Radek Krejcid33273d2018-10-25 14:55:52 +0200902 }
903 if (!inc->submodule) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100904 LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.",
905 inc->name, mod->mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200906 return LY_EVALID;
907 }
908
909 return LY_SUCCESS;
910}
911
Radek Krejci01342af2019-01-03 15:18:08 +0100912#define FIND_MODULE(TYPE, MOD) \
Radek Krejcice8c1592018-10-29 15:35:51 +0100913 TYPE *imp; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100914 if (!strncmp((MOD)->mod->prefix, prefix, len) && (MOD)->mod->prefix[len] == '\0') { \
Radek Krejcice8c1592018-10-29 15:35:51 +0100915 /* it is the prefix of the module itself */ \
Radek Krejci0af46292019-01-11 16:02:31 +0100916 m = ly_ctx_get_module((MOD)->mod->ctx, (MOD)->mod->name, (MOD)->mod->revision); \
Radek Krejcice8c1592018-10-29 15:35:51 +0100917 } \
918 /* search in imports */ \
Radek Krejcibbe09a92018-11-08 09:36:54 +0100919 if (!m) { \
920 LY_ARRAY_FOR((MOD)->imports, TYPE, imp) { \
Radek Krejci01342af2019-01-03 15:18:08 +0100921 if (!strncmp(imp->prefix, prefix, len) && imp->prefix[len] == '\0') { \
Radek Krejcibbe09a92018-11-08 09:36:54 +0100922 m = imp->module; \
923 break; \
924 } \
Radek Krejcice8c1592018-10-29 15:35:51 +0100925 } \
Radek Krejcibbe09a92018-11-08 09:36:54 +0100926 }
Radek Krejcice8c1592018-10-29 15:35:51 +0100927
Radek Krejcibbe09a92018-11-08 09:36:54 +0100928struct lysc_module *
Radek Krejcia3045382018-11-22 14:30:31 +0100929lysc_module_find_prefix(const struct lysc_module *mod, const char *prefix, size_t len)
Radek Krejci86d106e2018-10-18 09:53:19 +0200930{
Radek Krejcibbe09a92018-11-08 09:36:54 +0100931 const struct lys_module *m = NULL;
932
Radek Krejci01342af2019-01-03 15:18:08 +0100933 FIND_MODULE(struct lysc_import, mod);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100934 return m ? m->compiled : NULL;
Radek Krejcice8c1592018-10-29 15:35:51 +0100935}
Radek Krejci86d106e2018-10-18 09:53:19 +0200936
Radek Krejcibbe09a92018-11-08 09:36:54 +0100937struct lysp_module *
Radek Krejcia3045382018-11-22 14:30:31 +0100938lysp_module_find_prefix(const struct lysp_module *mod, const char *prefix, size_t len)
Radek Krejcice8c1592018-10-29 15:35:51 +0100939{
Radek Krejcibbe09a92018-11-08 09:36:54 +0100940 const struct lys_module *m = NULL;
941
Radek Krejci01342af2019-01-03 15:18:08 +0100942 FIND_MODULE(struct lysp_import, mod);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100943 return m ? m->parsed : NULL;
Radek Krejcice8c1592018-10-29 15:35:51 +0100944}
Radek Krejci86d106e2018-10-18 09:53:19 +0200945
Radek Krejcice8c1592018-10-29 15:35:51 +0100946struct lys_module *
Radek Krejcia3045382018-11-22 14:30:31 +0100947lys_module_find_prefix(const struct lys_module *mod, const char *prefix, size_t len)
Radek Krejcice8c1592018-10-29 15:35:51 +0100948{
Radek Krejcibbe09a92018-11-08 09:36:54 +0100949 const struct lys_module *m = NULL;
950
Radek Krejcice8c1592018-10-29 15:35:51 +0100951 if (mod->compiled) {
Radek Krejci01342af2019-01-03 15:18:08 +0100952 FIND_MODULE(struct lysc_import, mod->compiled);
Radek Krejcice8c1592018-10-29 15:35:51 +0100953 } else {
Radek Krejci01342af2019-01-03 15:18:08 +0100954 FIND_MODULE(struct lysp_import, mod->parsed);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100955 }
956 return (struct lys_module*)m;
957}
958
Radek Krejcia3045382018-11-22 14:30:31 +0100959const char *
Radek Krejci693262f2019-04-29 15:23:20 +0200960lys_prefix_find_module(const struct lys_module *mod, const struct lys_module *import)
961{
962 unsigned int u;
963
964 if (import == mod) {
965 return mod->prefix;
966 }
967
968 if (mod->parsed) {
969 LY_ARRAY_FOR(mod->parsed->imports, u) {
970 if (mod->parsed->imports[u].module == import) {
971 return mod->parsed->imports[u].prefix;
972 }
973 }
974 } else {
975 /* we don't have original information about the import's prefix,
976 * so the prefix of the import module itself is returned instead */
977 return import->prefix;
978 }
979
980 return NULL;
981}
982
983const char *
Radek Krejcia3045382018-11-22 14:30:31 +0100984lys_nodetype2str(uint16_t nodetype)
985{
986 switch(nodetype) {
987 case LYS_CONTAINER:
988 return "container";
989 case LYS_CHOICE:
990 return "choice";
991 case LYS_LEAF:
992 return "leaf";
993 case LYS_LEAFLIST:
994 return "leaf-list";
995 case LYS_LIST:
996 return "list";
997 case LYS_ANYXML:
998 return "anyxml";
999 case LYS_ANYDATA:
1000 return "anydata";
Radek Krejcif12a1f02019-02-11 16:42:08 +01001001 case LYS_CASE:
1002 return "case";
Radek Krejcif538ce52019-03-05 10:46:14 +01001003 case LYS_ACTION:
1004 return "RPC/action";
1005 case LYS_NOTIF:
1006 return "Notification";
Radek Krejcifc81ea82019-04-18 13:27:22 +02001007 case LYS_USES:
1008 return "uses";
Radek Krejcia3045382018-11-22 14:30:31 +01001009 default:
1010 return "unknown";
1011 }
1012}
1013
Radek Krejci693262f2019-04-29 15:23:20 +02001014const char *
1015lys_datatype2str(LY_DATA_TYPE basetype)
1016{
1017 switch(basetype) {
1018 case LY_TYPE_BINARY:
1019 return "binary";
1020 case LY_TYPE_UINT8:
1021 return "uint8";
1022 case LY_TYPE_UINT16:
1023 return "uint16";
1024 case LY_TYPE_UINT32:
1025 return "uint32";
1026 case LY_TYPE_UINT64:
1027 return "uint64";
1028 case LY_TYPE_STRING:
1029 return "string";
1030 case LY_TYPE_BITS:
1031 return "bits";
1032 case LY_TYPE_BOOL:
1033 return "boolean";
1034 case LY_TYPE_DEC64:
1035 return "decimal64";
1036 case LY_TYPE_EMPTY:
1037 return "empty";
1038 case LY_TYPE_ENUM:
1039 return "enumeration";
1040 case LY_TYPE_IDENT:
1041 return "identityref";
1042 case LY_TYPE_INST:
1043 return "instance-identifier";
1044 case LY_TYPE_LEAFREF:
1045 return "leafref";
1046 case LY_TYPE_UNION:
1047 return "union";
1048 case LY_TYPE_INT8:
1049 return "int8";
1050 case LY_TYPE_INT16:
1051 return "int16";
1052 case LY_TYPE_INT32:
1053 return "int32";
1054 case LY_TYPE_INT64:
1055 return "int64";
Radek Krejcibbe09a92018-11-08 09:36:54 +01001056 default:
1057 return "unknown";
1058 }
1059}
1060
Radek Krejci056d0a82018-12-06 16:57:25 +01001061API const struct lysp_tpdf *
1062lysp_node_typedefs(const struct lysp_node *node)
1063{
Radek Krejci0fb28562018-12-13 15:17:37 +01001064 switch (node->nodetype) {
1065 case LYS_CONTAINER:
1066 return ((struct lysp_node_container*)node)->typedefs;
1067 case LYS_LIST:
1068 return ((struct lysp_node_list*)node)->typedefs;
1069 case LYS_GROUPING:
1070 return ((struct lysp_grp*)node)->typedefs;
1071 case LYS_ACTION:
1072 return ((struct lysp_action*)node)->typedefs;
1073 case LYS_INOUT:
1074 return ((struct lysp_action_inout*)node)->typedefs;
1075 case LYS_NOTIF:
1076 return ((struct lysp_notif*)node)->typedefs;
1077 default:
Radek Krejci056d0a82018-12-06 16:57:25 +01001078 return NULL;
1079 }
1080}
1081
Radek Krejci53ea6152018-12-13 15:21:15 +01001082API const struct lysp_grp *
1083lysp_node_groupings(const struct lysp_node *node)
1084{
1085 switch (node->nodetype) {
1086 case LYS_CONTAINER:
1087 return ((struct lysp_node_container*)node)->groupings;
1088 case LYS_LIST:
1089 return ((struct lysp_node_list*)node)->groupings;
1090 case LYS_GROUPING:
1091 return ((struct lysp_grp*)node)->groupings;
1092 case LYS_ACTION:
1093 return ((struct lysp_action*)node)->groupings;
1094 case LYS_INOUT:
1095 return ((struct lysp_action_inout*)node)->groupings;
1096 case LYS_NOTIF:
1097 return ((struct lysp_notif*)node)->groupings;
1098 default:
1099 return NULL;
1100 }
1101}
1102
Radek Krejcibbe09a92018-11-08 09:36:54 +01001103struct lysp_action **
Radek Krejci056d0a82018-12-06 16:57:25 +01001104lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001105{
1106 assert(node);
1107 switch (node->nodetype) {
1108 case LYS_CONTAINER:
1109 return &((struct lysp_node_container*)node)->actions;
1110 case LYS_LIST:
1111 return &((struct lysp_node_list*)node)->actions;
1112 case LYS_GROUPING:
1113 return &((struct lysp_grp*)node)->actions;
1114 case LYS_AUGMENT:
1115 return &((struct lysp_augment*)node)->actions;
1116 default:
1117 return NULL;
1118 }
1119}
1120
Radek Krejci056d0a82018-12-06 16:57:25 +01001121API const struct lysp_action *
1122lysp_node_actions(const struct lysp_node *node)
1123{
1124 struct lysp_action **actions;
1125 actions = lysp_node_actions_p((struct lysp_node*)node);
1126 if (actions) {
1127 return *actions;
1128 } else {
1129 return NULL;
1130 }
1131}
1132
Radek Krejcibbe09a92018-11-08 09:36:54 +01001133struct lysp_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +01001134lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001135{
1136 assert(node);
1137 switch (node->nodetype) {
1138 case LYS_CONTAINER:
1139 return &((struct lysp_node_container*)node)->notifs;
1140 case LYS_LIST:
1141 return &((struct lysp_node_list*)node)->notifs;
1142 case LYS_GROUPING:
1143 return &((struct lysp_grp*)node)->notifs;
1144 case LYS_AUGMENT:
1145 return &((struct lysp_augment*)node)->notifs;
1146 default:
1147 return NULL;
1148 }
1149}
1150
Radek Krejci056d0a82018-12-06 16:57:25 +01001151API const struct lysp_notif *
1152lysp_node_notifs(const struct lysp_node *node)
1153{
1154 struct lysp_notif **notifs;
1155 notifs = lysp_node_notifs_p((struct lysp_node*)node);
1156 if (notifs) {
1157 return *notifs;
1158 } else {
1159 return NULL;
1160 }
1161}
1162
Radek Krejcibbe09a92018-11-08 09:36:54 +01001163struct lysp_node **
Radek Krejci056d0a82018-12-06 16:57:25 +01001164lysp_node_children_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001165{
1166 assert(node);
1167 switch (node->nodetype) {
1168 case LYS_CONTAINER:
1169 return &((struct lysp_node_container*)node)->child;
1170 case LYS_CHOICE:
1171 return &((struct lysp_node_choice*)node)->child;
1172 case LYS_LIST:
1173 return &((struct lysp_node_list*)node)->child;
1174 case LYS_CASE:
1175 return &((struct lysp_node_case*)node)->child;
1176 case LYS_GROUPING:
1177 return &((struct lysp_grp*)node)->data;
1178 case LYS_AUGMENT:
1179 return &((struct lysp_augment*)node)->child;
1180 case LYS_INOUT:
1181 return &((struct lysp_action_inout*)node)->data;
1182 case LYS_NOTIF:
1183 return &((struct lysp_notif*)node)->data;
1184 default:
1185 return NULL;
1186 }
1187}
1188
Radek Krejci056d0a82018-12-06 16:57:25 +01001189API const struct lysp_node *
1190lysp_node_children(const struct lysp_node *node)
1191{
1192 struct lysp_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001193
1194 if (!node) {
1195 return NULL;
1196 }
1197
Radek Krejci056d0a82018-12-06 16:57:25 +01001198 children = lysp_node_children_p((struct lysp_node*)node);
1199 if (children) {
1200 return *children;
1201 } else {
1202 return NULL;
1203 }
1204}
1205
1206struct lysc_action **
1207lysc_node_actions_p(struct lysc_node *node)
1208{
1209 assert(node);
1210 switch (node->nodetype) {
1211 case LYS_CONTAINER:
1212 return &((struct lysc_node_container*)node)->actions;
1213 case LYS_LIST:
1214 return &((struct lysc_node_list*)node)->actions;
1215 default:
1216 return NULL;
1217 }
1218}
1219
1220API const struct lysc_action *
1221lysc_node_actions(const struct lysc_node *node)
1222{
1223 struct lysc_action **actions;
1224 actions = lysc_node_actions_p((struct lysc_node*)node);
1225 if (actions) {
1226 return *actions;
1227 } else {
1228 return NULL;
1229 }
1230}
1231
1232struct lysc_notif **
1233lysc_node_notifs_p(struct lysc_node *node)
1234{
1235 assert(node);
1236 switch (node->nodetype) {
1237 case LYS_CONTAINER:
1238 return &((struct lysc_node_container*)node)->notifs;
1239 case LYS_LIST:
1240 return &((struct lysc_node_list*)node)->notifs;
1241 default:
1242 return NULL;
1243 }
1244}
1245
1246API const struct lysc_notif *
1247lysc_node_notifs(const struct lysc_node *node)
1248{
1249 struct lysc_notif **notifs;
1250 notifs = lysc_node_notifs_p((struct lysc_node*)node);
1251 if (notifs) {
1252 return *notifs;
1253 } else {
1254 return NULL;
1255 }
1256}
1257
Radek Krejcibbe09a92018-11-08 09:36:54 +01001258struct lysc_node **
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001259lysc_node_children_p(const struct lysc_node *node, uint16_t flags)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001260{
1261 assert(node);
1262 switch (node->nodetype) {
1263 case LYS_CONTAINER:
1264 return &((struct lysc_node_container*)node)->child;
1265 case LYS_CHOICE:
Radek Krejcia3045382018-11-22 14:30:31 +01001266 if (((struct lysc_node_choice*)node)->cases) {
Radek Krejci95710c92019-02-11 15:49:55 +01001267 return &((struct lysc_node_choice*)node)->cases->child;
Radek Krejcia3045382018-11-22 14:30:31 +01001268 } else {
1269 return NULL;
1270 }
Radek Krejci01342af2019-01-03 15:18:08 +01001271 case LYS_CASE:
1272 return &((struct lysc_node_case*)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001273 case LYS_LIST:
1274 return &((struct lysc_node_list*)node)->child;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001275 case LYS_ACTION:
1276 if (flags & LYS_CONFIG_R) {
1277 return &((struct lysc_action*)node)->output.data;
1278 } else {
1279 /* LYS_CONFIG_W, but also the default case */
1280 return &((struct lysc_action*)node)->input.data;
1281 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02001282 case LYS_NOTIF:
1283 return &((struct lysc_notif*)node)->data;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001284 default:
1285 return NULL;
1286 }
1287}
1288
Radek Krejci056d0a82018-12-06 16:57:25 +01001289API const struct lysc_node *
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001290lysc_node_children(const struct lysc_node *node, uint16_t flags)
Radek Krejcia3045382018-11-22 14:30:31 +01001291{
Radek Krejci056d0a82018-12-06 16:57:25 +01001292 struct lysc_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +02001293
1294 if (!node) {
1295 return NULL;
1296 }
1297
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001298 children = lysc_node_children_p((struct lysc_node*)node, flags);
Radek Krejci056d0a82018-12-06 16:57:25 +01001299 if (children) {
1300 return *children;
1301 } else {
Radek Krejcia3045382018-11-22 14:30:31 +01001302 return NULL;
1303 }
1304}
1305
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001306struct lys_module *
1307lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1308{
1309 unsigned int u;
1310
1311 for (u = 0; u < ctx->list.count; ++u) {
1312 if (((struct lys_module*)ctx->list.objs[u])->parsed == mod) {
1313 return ((struct lys_module*)ctx->list.objs[u]);
1314 }
1315 }
1316 return NULL;
1317}
1318
David Sedlákc10e7902018-12-17 02:17:59 +01001319enum yang_keyword
David Sedlák5f8f0332019-06-18 16:34:30 +02001320lysp_match_kw(struct lys_parser_ctx *ctx, const char **data)
David Sedlákc10e7902018-12-17 02:17:59 +01001321{
David Sedlák1bccdfa2019-06-17 15:55:27 +02001322/**
1323 * @brief Move the DATA pointer by COUNT items. Also updates the indent value in yang parser context
1324 * @param[in] CTX yang parser context to update its indent value.
1325 * @param[in,out] DATA pointer to move
1326 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
1327 */
1328#define MOVE_IN(CTX, DATA, COUNT) (*(DATA))+=COUNT;if(CTX){(CTX)->indent+=COUNT;}
1329#define IF_KW(STR, LEN, STMT) if (!strncmp(*(data), STR, LEN)) {MOVE_IN(ctx, data, LEN);*kw=STMT;}
1330#define IF_KW_PREFIX(STR, LEN) if (!strncmp(*(data), STR, LEN)) {MOVE_IN(ctx, data, LEN);
1331#define IF_KW_PREFIX_END }
David Sedlák572e7ab2019-06-04 16:01:58 +02001332
David Sedlák1bccdfa2019-06-17 15:55:27 +02001333 enum yang_keyword result = YANG_NONE;
1334 enum yang_keyword *kw = &result;
1335 /* read the keyword itself */
1336 switch (**data) {
David Sedlák23a59a62018-10-26 13:08:02 +02001337 case 'a':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001338 MOVE_IN(ctx, data, 1);
1339 IF_KW("rgument", 7, YANG_ARGUMENT)
1340 else IF_KW("ugment", 6, YANG_AUGMENT)
1341 else IF_KW("ction", 5, YANG_ACTION)
1342 else IF_KW_PREFIX("ny", 2)
1343 IF_KW("data", 4, YANG_ANYDATA)
1344 else IF_KW("xml", 3, YANG_ANYXML)
1345 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001346 break;
1347 case 'b':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001348 MOVE_IN(ctx, data, 1);
1349 IF_KW("ase", 3, YANG_BASE)
1350 else IF_KW("elongs-to", 9, YANG_BELONGS_TO)
1351 else IF_KW("it", 2, YANG_BIT)
David Sedlák23a59a62018-10-26 13:08:02 +02001352 break;
1353 case 'c':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001354 MOVE_IN(ctx, data, 1);
1355 IF_KW("ase", 3, YANG_CASE)
1356 else IF_KW("hoice", 5, YANG_CHOICE)
1357 else IF_KW_PREFIX("on", 2)
1358 IF_KW("fig", 3, YANG_CONFIG)
1359 else IF_KW_PREFIX("ta", 2)
1360 IF_KW("ct", 2, YANG_CONTACT)
1361 else IF_KW("iner", 4, YANG_CONTAINER)
1362 IF_KW_PREFIX_END
1363 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001364 break;
1365 case 'd':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001366 MOVE_IN(ctx, data, 1);
1367 IF_KW_PREFIX("e", 1)
1368 IF_KW("fault", 5, YANG_DEFAULT)
1369 else IF_KW("scription", 9, YANG_DESCRIPTION)
1370 else IF_KW_PREFIX("viat", 4)
1371 IF_KW("e", 1, YANG_DEVIATE)
1372 else IF_KW("ion", 3, YANG_DEVIATION)
1373 IF_KW_PREFIX_END
1374 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001375 break;
1376 case 'e':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001377 MOVE_IN(ctx, data, 1);
1378 IF_KW("num", 3, YANG_ENUM)
1379 else IF_KW_PREFIX("rror-", 5)
1380 IF_KW("app-tag", 7, YANG_ERROR_APP_TAG)
1381 else IF_KW("message", 7, YANG_ERROR_MESSAGE)
1382 IF_KW_PREFIX_END
1383 else IF_KW("xtension", 8, YANG_EXTENSION)
David Sedlák23a59a62018-10-26 13:08:02 +02001384 break;
1385 case 'f':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001386 MOVE_IN(ctx, data, 1);
1387 IF_KW("eature", 6, YANG_FEATURE)
1388 else IF_KW("raction-digits", 14, YANG_FRACTION_DIGITS)
David Sedlák23a59a62018-10-26 13:08:02 +02001389 break;
1390 case 'g':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001391 MOVE_IN(ctx, data, 1);
1392 IF_KW("rouping", 7, YANG_GROUPING)
David Sedlák23a59a62018-10-26 13:08:02 +02001393 break;
1394 case 'i':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001395 MOVE_IN(ctx, data, 1);
1396 IF_KW("dentity", 7, YANG_IDENTITY)
1397 else IF_KW("f-feature", 9, YANG_IF_FEATURE)
1398 else IF_KW("mport", 5, YANG_IMPORT)
1399 else IF_KW_PREFIX("n", 1)
1400 IF_KW("clude", 5, YANG_INCLUDE)
1401 else IF_KW("put", 3, YANG_INPUT)
1402 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001403 break;
1404 case 'k':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001405 MOVE_IN(ctx, data, 1);
1406 IF_KW("ey", 2, YANG_KEY)
David Sedlák23a59a62018-10-26 13:08:02 +02001407 break;
1408 case 'l':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001409 MOVE_IN(ctx, data, 1);
1410 IF_KW_PREFIX("e", 1)
1411 IF_KW("af-list", 7, YANG_LEAF_LIST)
1412 else IF_KW("af", 2, YANG_LEAF)
1413 else IF_KW("ngth", 4, YANG_LENGTH)
1414 IF_KW_PREFIX_END
1415 else IF_KW("ist", 3, YANG_LIST)
David Sedlák23a59a62018-10-26 13:08:02 +02001416 break;
1417 case 'm':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001418 MOVE_IN(ctx, data, 1);
1419 IF_KW_PREFIX("a", 1)
1420 IF_KW("ndatory", 7, YANG_MANDATORY)
1421 else IF_KW("x-elements", 10, YANG_MAX_ELEMENTS)
1422 IF_KW_PREFIX_END
1423 else IF_KW("in-elements", 11, YANG_MIN_ELEMENTS)
1424 else IF_KW("ust", 3, YANG_MUST)
1425 else IF_KW_PREFIX("od", 2)
1426 IF_KW("ule", 3, YANG_MODULE)
1427 else IF_KW("ifier", 5, YANG_MODIFIER)
1428 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001429 break;
1430 case 'n':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001431 MOVE_IN(ctx, data, 1);
1432 IF_KW("amespace", 8, YANG_NAMESPACE)
1433 else IF_KW("otification", 11, YANG_NOTIFICATION)
David Sedlák23a59a62018-10-26 13:08:02 +02001434 break;
1435 case 'o':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001436 MOVE_IN(ctx, data, 1);
1437 IF_KW_PREFIX("r", 1)
1438 IF_KW("dered-by", 8, YANG_ORDERED_BY)
1439 else IF_KW("ganization", 10, YANG_ORGANIZATION)
1440 IF_KW_PREFIX_END
1441 else IF_KW("utput", 5, YANG_OUTPUT)
David Sedlák23a59a62018-10-26 13:08:02 +02001442 break;
1443 case 'p':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001444 MOVE_IN(ctx, data, 1);
1445 IF_KW("ath", 3, YANG_PATH)
1446 else IF_KW("attern", 6, YANG_PATTERN)
1447 else IF_KW("osition", 7, YANG_POSITION)
1448 else IF_KW_PREFIX("re", 2)
1449 IF_KW("fix", 3, YANG_PREFIX)
1450 else IF_KW("sence", 5, YANG_PRESENCE)
1451 IF_KW_PREFIX_END
David Sedlák23a59a62018-10-26 13:08:02 +02001452 break;
1453 case 'r':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001454 MOVE_IN(ctx, data, 1);
1455 IF_KW("ange", 4, YANG_RANGE)
1456 else IF_KW_PREFIX("e", 1)
1457 IF_KW_PREFIX("f", 1)
1458 IF_KW("erence", 6, YANG_REFERENCE)
1459 else IF_KW("ine", 3, YANG_REFINE)
1460 IF_KW_PREFIX_END
1461 else IF_KW("quire-instance", 14, YANG_REQUIRE_INSTANCE)
1462 else IF_KW("vision-date", 11, YANG_REVISION_DATE)
1463 else IF_KW("vision", 6, YANG_REVISION)
1464 IF_KW_PREFIX_END
1465 else IF_KW("pc", 2, YANG_RPC)
David Sedlák23a59a62018-10-26 13:08:02 +02001466 break;
1467 case 's':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001468 MOVE_IN(ctx, data, 1);
1469 IF_KW("tatus", 5, YANG_STATUS)
1470 else IF_KW("ubmodule", 8, YANG_SUBMODULE)
David Sedlák23a59a62018-10-26 13:08:02 +02001471 break;
1472 case 't':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001473 MOVE_IN(ctx, data, 1);
1474 IF_KW("ypedef", 6, YANG_TYPEDEF)
1475 else IF_KW("ype", 3, YANG_TYPE)
David Sedlák23a59a62018-10-26 13:08:02 +02001476 break;
1477 case 'u':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001478 MOVE_IN(ctx, data, 1);
1479 IF_KW_PREFIX("ni", 2)
1480 IF_KW("que", 3, YANG_UNIQUE)
1481 else IF_KW("ts", 2, YANG_UNITS)
1482 IF_KW_PREFIX_END
1483 else IF_KW("ses", 3, YANG_USES)
David Sedlák23a59a62018-10-26 13:08:02 +02001484 break;
1485 case 'v':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001486 MOVE_IN(ctx, data, 1);
1487 IF_KW("alue", 4, YANG_VALUE)
David Sedlák23a59a62018-10-26 13:08:02 +02001488 break;
1489 case 'w':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001490 MOVE_IN(ctx, data, 1);
1491 IF_KW("hen", 3, YANG_WHEN)
David Sedlák23a59a62018-10-26 13:08:02 +02001492 break;
1493 case 'y':
David Sedlák1bccdfa2019-06-17 15:55:27 +02001494 MOVE_IN(ctx, data, 1);
1495 IF_KW("ang-version", 11, YANG_YANG_VERSION)
1496 else IF_KW("in-element", 10, YANG_YIN_ELEMENT)
David Sedlák23a59a62018-10-26 13:08:02 +02001497 break;
David Sedlák23a59a62018-10-26 13:08:02 +02001498 default:
David Sedlák1bccdfa2019-06-17 15:55:27 +02001499 /* if context is not NULL we are matching keyword from YANG data*/
1500 if (ctx) {
1501 if (**data == ';') {
1502 MOVE_IN(ctx, data, 1);
1503 *kw = YANG_SEMICOLON;
1504 } else if (**data == '{') {
1505 MOVE_IN(ctx, data, 1);
1506 *kw = YANG_LEFT_BRACE;
1507 } else if (**data == '}') {
1508 MOVE_IN(ctx, data, 1);
1509 *kw = YANG_RIGHT_BRACE;
1510 }
1511 }
David Sedlák23a59a62018-10-26 13:08:02 +02001512 break;
1513 }
1514
David Sedlák1bccdfa2019-06-17 15:55:27 +02001515#undef IF_KW
1516#undef IF_KW_PREFIX
1517#undef IF_KW_PREFIX_END
David Sedlák18730132019-03-15 15:51:34 +01001518#undef MOVE_IN
David Sedlák18730132019-03-15 15:51:34 +01001519
David Sedlák1bccdfa2019-06-17 15:55:27 +02001520 return result;
David Sedlák23a59a62018-10-26 13:08:02 +02001521}
David Sedlákecf5eb82019-06-03 14:12:44 +02001522
Radek Krejcid3ca0632019-04-16 16:54:54 +02001523unsigned int
1524lysp_ext_instance_iter(struct lysp_ext_instance *ext, unsigned int index, LYEXT_SUBSTMT substmt)
1525{
1526 LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1527
1528 for (; index < LY_ARRAY_SIZE(ext); index++) {
1529 if (ext[index].insubstmt == substmt) {
1530 return index;
1531 }
1532 }
1533
1534 return LY_ARRAY_SIZE(ext);
1535}
1536