blob: 3f6c7e69a544c6ee0da7a1aac3c116326d2ee05d [file] [log] [blame]
Radek Krejci86d106e2018-10-18 09:53:19 +02001/**
2 * @file tree_schema_helpers.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Parsing and validation helper functions
5 *
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
16#include <ctype.h>
Radek Krejci9ed7a192018-10-31 16:23:51 +010017#include <dirent.h>
18#include <errno.h>
19#include <fcntl.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020020#include <limits.h>
Radek Krejci9ed7a192018-10-31 16:23:51 +010021#include <stdlib.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24#include <unistd.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020025#include <time.h>
26
27#include "libyang.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020028#include "tree_schema_internal.h"
29
Radek Krejcia3045382018-11-22 14:30:31 +010030/**
31 * @brief Parse an identifier.
32 *
33 * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l'))
34 * identifier = (ALPHA / "_")
35 * *(ALPHA / DIGIT / "_" / "-" / ".")
36 *
37 * @param[in,out] id Identifier to parse. When returned, it points to the first character which is not part of the identifier.
38 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid starting character.
39 */
40static LY_ERR
41lys_parse_id(const char **id)
42{
43 assert(id && *id);
44
Radek Krejcia9026eb2018-12-12 16:04:47 +010045 if (!is_yangidentstartchar(**id)) {
Radek Krejcia3045382018-11-22 14:30:31 +010046 return LY_EINVAL;
47 }
48 ++(*id);
49
Radek Krejcia9026eb2018-12-12 16:04:47 +010050 while (is_yangidentchar(**id)) {
Radek Krejcia3045382018-11-22 14:30:31 +010051 ++(*id);
52 }
53 return LY_SUCCESS;
54}
55
56LY_ERR
57lys_parse_nodeid(const char **id, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
58{
59 assert(id && *id);
60 assert(prefix && prefix_len);
61 assert(name && name_len);
62
63 *prefix = *id;
64 *prefix_len = 0;
65 *name = NULL;
66 *name_len = 0;
67
68 LY_CHECK_RET(lys_parse_id(id));
69 if (**id == ':') {
70 /* there is prefix */
71 *prefix_len = *id - *prefix;
72 ++(*id);
73 *name = *id;
74
75 LY_CHECK_RET(lys_parse_id(id));
76 *name_len = *id - *name;
77 } else {
78 /* there is no prefix, so what we have as prefix now is actually the name */
79 *name = *prefix;
80 *name_len = *id - *name;
81 *prefix = NULL;
82 }
83
84 return LY_SUCCESS;
85}
86
Radek Krejci86d106e2018-10-18 09:53:19 +020087LY_ERR
Radek Krejci9bb94eb2018-12-04 16:48:35 +010088lys_resolve_descendant_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *context_node,
89 int nodetype, const struct lysc_node **target)
90{
91 LY_ERR ret = LY_EVALID;
92 const char *name, *prefix, *id;
93 const struct lysc_node *context;
94 size_t name_len, prefix_len;
95 const struct lys_module *mod;
96
97 assert(nodeid);
98 assert(context_node);
99 assert(target);
100 *target = NULL;
101
102 id = nodeid;
103 context = context_node;
104 while (*id && (ret = lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
105 if (prefix) {
106 mod = lys_module_find_prefix(context_node->module, prefix, prefix_len);
107 if (!mod) {
108 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
109 "Invalid descendant-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
110 id - nodeid, nodeid, prefix_len, prefix, context_node->module->compiled->name);
111 return LY_ENOTFOUND;
112 }
113 } else {
114 mod = context_node->module;
115 }
Radek Krejcia9026eb2018-12-12 16:04:47 +0100116 context = lys_child(context, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100117 if (!context) {
118 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
119 "Invalid descendant-schema-nodeid value \"%.*s\" - target node not found.", id - nodeid, nodeid);
120 return LY_ENOTFOUND;
121 }
122 if (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len)) {
123 break;
124 }
125 if (id && *id != '/') {
126 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
127 "Invalid descendant-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci665421c2018-12-05 08:03:39 +0100128 id - nodeid + 1, nodeid);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100129 return LY_EVALID;
130 }
131 ++id;
132 }
133
134 if (ret == LY_SUCCESS) {
135 *target = context;
136 if (nodetype && !(context->nodetype & nodetype)) {
137 return LY_EDENIED;
138 }
139 }
140
141 return ret;
142}
143
144LY_ERR
Radek Krejci86d106e2018-10-18 09:53:19 +0200145lysp_check_prefix(struct ly_parser_ctx *ctx, struct lysp_module *module, const char **value)
146{
147 struct lysp_import *i;
148
149 if (module->prefix && &module->prefix != value && !strcmp(module->prefix, *value)) {
150 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE,
151 "Prefix \"%s\" already used as module prefix.", *value);
152 return LY_EEXIST;
153 }
154 if (module->imports) {
155 LY_ARRAY_FOR(module->imports, struct lysp_import, i) {
156 if (i->prefix && &i->prefix != value && !strcmp(i->prefix, *value)) {
157 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE,
158 "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name);
159 return LY_EEXIST;
160 }
161 }
162 }
163 return LY_SUCCESS;
164}
165
166LY_ERR
Radek Krejci4f28eda2018-11-12 11:46:16 +0100167lysc_check_status(struct lysc_ctx *ctx,
168 uint16_t flags1, void *mod1, const char *name1,
169 uint16_t flags2, void *mod2, const char *name2)
170{
171 uint16_t flg1, flg2;
172
173 flg1 = (flags1 & LYS_STATUS_MASK) ? (flags1 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
174 flg2 = (flags2 & LYS_STATUS_MASK) ? (flags2 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
175
176 if ((flg1 < flg2) && (mod1 == mod2)) {
177 if (ctx) {
178 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
179 "A %s definition \"%s\" is not allowed to reference %s definition \"%s\".",
180 flg1 == LYS_STATUS_CURR ? "current" : "deprecated", name1,
181 flg2 == LYS_STATUS_OBSLT ? "obsolete" : "deprecated", name2);
182 }
183 return LY_EVALID;
184 }
185
186 return LY_SUCCESS;
187}
188
189LY_ERR
Radek Krejcibbe09a92018-11-08 09:36:54 +0100190lysp_check_date(struct ly_parser_ctx *ctx, const char *date, int date_len, const char *stmt)
Radek Krejci86d106e2018-10-18 09:53:19 +0200191{
192 int i;
193 struct tm tm, tm_;
194 char *r;
195
Radek Krejcibbe09a92018-11-08 09:36:54 +0100196 LY_CHECK_ARG_RET(ctx ? ctx->ctx : NULL, date, LY_EINVAL);
197 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 +0200198
199 /* check format */
200 for (i = 0; i < date_len; i++) {
201 if (i == 4 || i == 7) {
202 if (date[i] != '-') {
203 goto error;
204 }
205 } else if (!isdigit(date[i])) {
206 goto error;
207 }
208 }
209
210 /* check content, e.g. 2018-02-31 */
211 memset(&tm, 0, sizeof tm);
212 r = strptime(date, "%Y-%m-%d", &tm);
213 if (!r || r != &date[LY_REV_SIZE - 1]) {
214 goto error;
215 }
216 memcpy(&tm_, &tm, sizeof tm);
217 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
218 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
219 /* checking days is enough, since other errors
220 * have been checked by strptime() */
221 goto error;
222 }
223
224 return LY_SUCCESS;
225
226error:
Radek Krejcid33273d2018-10-25 14:55:52 +0200227 if (stmt) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100228 if (ctx) {
229 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LY_VCODE_INVAL, date_len, date, stmt);
230 } else {
231 LOGVAL(NULL, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, date_len, date, stmt);
232 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200233 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200234 return LY_EINVAL;
235}
236
237void
238lysp_sort_revisions(struct lysp_revision *revs)
239{
240 uint8_t i, r;
241 struct lysp_revision rev;
242
243 for (i = 1, r = 0; revs && i < LY_ARRAY_SIZE(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +0200244 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +0200245 r = i;
246 }
247 }
248
249 if (r) {
250 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200251 memcpy(&rev, &revs[0], sizeof rev);
252 memcpy(&revs[0], &revs[r], sizeof rev);
253 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200254 }
255}
Radek Krejci151a5b72018-10-19 14:21:44 +0200256
Radek Krejcibbe09a92018-11-08 09:36:54 +0100257static const struct lysp_tpdf *
258lysp_type_match(const char *name, struct lysp_node *node)
259{
260 struct lysp_tpdf **typedefs;
261 unsigned int u;
262
Radek Krejci056d0a82018-12-06 16:57:25 +0100263 typedefs = lysp_node_typedefs_p(node);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100264 if (typedefs && *typedefs) {
265 LY_ARRAY_FOR(*typedefs, u) {
266 if (!strcmp(name, (*typedefs)[u].name)) {
267 /* match */
268 return &(*typedefs)[u];
269 }
270 }
271 }
272
273 return NULL;
274}
275
Radek Krejci4f28eda2018-11-12 11:46:16 +0100276static LY_DATA_TYPE
277lysp_type_str2builtin(const char *name, size_t len)
278{
279 if (len >= 4) { /* otherwise it does not match any built-in type */
280 if (name[0] == 'b') {
281 if (name[1] == 'i') {
282 if (len == 6 && !strncmp(&name[2], "nary", 4)) {
283 return LY_TYPE_BINARY;
284 } else if (len == 4 && !strncmp(&name[2], "ts", 2)) {
285 return LY_TYPE_BITS;
286 }
287 } else if (len == 7 && !strncmp(&name[1], "oolean", 6)) {
288 return LY_TYPE_BOOL;
289 }
290 } else if (name[0] == 'd') {
291 if (len == 9 && !strncmp(&name[1], "ecimal64", 8)) {
292 return LY_TYPE_DEC64;
293 }
294 } else if (name[0] == 'e') {
295 if (len == 5 && !strncmp(&name[1], "mpty", 4)) {
296 return LY_TYPE_EMPTY;
297 } else if (len == 11 && !strncmp(&name[1], "numeration", 10)) {
298 return LY_TYPE_ENUM;
299 }
300 } else if (name[0] == 'i') {
301 if (name[1] == 'n') {
302 if (len == 4 && !strncmp(&name[2], "t8", 2)) {
303 return LY_TYPE_INT8;
304 } else if (len == 5) {
305 if (!strncmp(&name[2], "t16", 3)) {
306 return LY_TYPE_INT16;
307 } else if (!strncmp(&name[2], "t32", 3)) {
308 return LY_TYPE_INT32;
309 } else if (!strncmp(&name[2], "t64", 3)) {
310 return LY_TYPE_INT64;
311 }
312 } else if (len == 19 && !strncmp(&name[2], "stance-identifier", 17)) {
313 return LY_TYPE_INST;
314 }
315 } else if (len == 11 && !strncmp(&name[1], "dentityref", 10)) {
316 return LY_TYPE_IDENT;
317 }
318 } else if (name[0] == 'l') {
319 if (len == 7 && !strncmp(&name[1], "eafref", 6)) {
320 return LY_TYPE_LEAFREF;
321 }
322 } else if (name[0] == 's') {
323 if (len == 6 && !strncmp(&name[1], "tring", 5)) {
324 return LY_TYPE_STRING;
325 }
326 } else if (name[0] == 'u') {
327 if (name[1] == 'n') {
328 if (len == 5 && !strncmp(&name[2], "ion", 3)) {
329 return LY_TYPE_UNION;
330 }
331 } else if (name[1] == 'i' && name[2] == 'n' && name[3] == 't') {
332 if (len == 5 && name[4] == '8') {
333 return LY_TYPE_UINT8;
334 } else if (len == 6) {
335 if (!strncmp(&name[4], "16", 2)) {
336 return LY_TYPE_UINT16;
337 } else if (!strncmp(&name[4], "32", 2)) {
338 return LY_TYPE_UINT32;
339 } else if (!strncmp(&name[4], "64", 2)) {
340 return LY_TYPE_UINT64;
341 }
342 }
343 }
344 }
345 }
346
347 return LY_TYPE_UNKNOWN;
348}
349
Radek Krejcibbe09a92018-11-08 09:36:54 +0100350LY_ERR
351lysp_type_find(const char *id, struct lysp_node *start_node, struct lysp_module *start_module,
Radek Krejci4f28eda2018-11-12 11:46:16 +0100352 LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node, struct lysp_module **module)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100353{
354 const char *str, *name;
355 struct lysp_tpdf *typedefs;
356 unsigned int u, v;
357
358 assert(id);
359 assert(start_module);
360 assert(tpdf);
361 assert(node);
362 assert(module);
363
Radek Krejci4f28eda2018-11-12 11:46:16 +0100364 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100365 str = strchr(id, ':');
366 if (str) {
367 *module = lysp_module_find_prefix(start_module, id, str - id);
368 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100369 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100370 } else {
371 *module = start_module;
372 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100373
374 /* check for built-in types */
375 *type = lysp_type_str2builtin(name, strlen(name));
376 if (*type) {
377 *tpdf = NULL;
378 return LY_SUCCESS;
379 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100380 }
381 LY_CHECK_RET(!(*module), LY_ENOTFOUND);
382
383 if (start_node && *module == start_module) {
384 /* search typedefs in parent's nodes */
385 *node = start_node;
386 while (*node) {
387 *tpdf = lysp_type_match(name, *node);
388 if (*tpdf) {
389 /* match */
390 return LY_SUCCESS;
391 }
392 *node = (*node)->parent;
393 }
394 }
395
396 /* search in top-level typedefs */
397 if ((*module)->typedefs) {
398 LY_ARRAY_FOR((*module)->typedefs, u) {
399 if (!strcmp(name, (*module)->typedefs[u].name)) {
400 /* match */
401 *tpdf = &(*module)->typedefs[u];
402 return LY_SUCCESS;
403 }
404 }
405 }
406
407 /* search in submodules' typedefs */
408 LY_ARRAY_FOR((*module)->includes, u) {
409 typedefs = (*module)->includes[u].submodule->typedefs;
410 if (typedefs) {
411 LY_ARRAY_FOR(typedefs, v) {
412 if (!strcmp(name, typedefs[v].name)) {
413 /* match */
414 *tpdf = &typedefs[v];
415 return LY_SUCCESS;
416 }
417 }
418 }
419 }
420
421 return LY_ENOTFOUND;
422}
423
424/*
425 * @brief Check name of a new type to avoid name collisions.
426 *
427 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
428 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
429 * @param[in] tpdf Typedef definition to check.
430 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
431 * typedefs are checked, caller is supposed to free the table.
432 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
433 * typedefs are checked, caller is supposed to free the table.
434 * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise.
435 */
436static LY_ERR
437lysp_check_typedef(struct ly_parser_ctx *ctx, struct lysp_node *node, struct lysp_tpdf *tpdf,
438 struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped)
439{
440 struct lysp_node *parent;
441 uint32_t hash;
442 size_t name_len;
443 const char *name;
444 unsigned int u;
445 struct lysp_tpdf **typedefs;
446
447 assert(ctx);
448 assert(tpdf);
449
450 name = tpdf->name;
451 name_len = strlen(name);
452
Radek Krejci4f28eda2018-11-12 11:46:16 +0100453 if (lysp_type_str2builtin(name, name_len)) {
454 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
455 "Invalid name \"%s\" of typedef - name collision with a built-in type.", name);
456 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100457 }
458
459 /* check locally scoped typedefs (avoid name shadowing) */
460 if (node) {
Radek Krejci056d0a82018-12-06 16:57:25 +0100461 typedefs = lysp_node_typedefs_p(node);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100462 if (typedefs && *typedefs) {
463 LY_ARRAY_FOR(*typedefs, u) {
464 if (typedefs[u] == tpdf) {
465 break;
466 }
467 if (!strcmp(name, (*typedefs)[u].name)) {
468 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
469 "Invalid name \"%s\" of typedef - name collision with sibling type.", name);
470 return LY_EEXIST;
471 }
472 }
473 }
474 /* search typedefs in parent's nodes */
475 for (parent = node->parent; parent; parent = node->parent) {
476 if (lysp_type_match(name, parent)) {
477 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
478 "Invalid name \"%s\" of typedef - name collision with another scoped type.", name);
479 return LY_EEXIST;
480 }
481 }
482 }
483
484 /* check collision with the top-level typedefs */
485 hash = dict_hash(name, name_len);
486 if (node) {
487 lyht_insert(tpdfs_scoped, &name, hash, NULL);
488 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
489 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
490 "Invalid name \"%s\" of typedef - scoped type collide with a top-level type.", name);
491 return LY_EEXIST;
492 }
493 } else {
494 if (lyht_insert(tpdfs_global, &name, hash, NULL)) {
495 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
496 "Invalid name \"%s\" of typedef - name collision with another top-level type.", name);
497 return LY_EEXIST;
498 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100499 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
500 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
501 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100502 }
503
504 return LY_SUCCESS;
505}
506
507static int
508lysp_id_cmp(void *val1, void *val2, int UNUSED(mod), void *UNUSED(cb_data))
509{
510 return !strcmp(val1, val2);
511}
512
513LY_ERR
514lysp_check_typedefs(struct ly_parser_ctx *ctx)
515{
516 struct hash_table *ids_global;
517 struct hash_table *ids_scoped;
518 struct lysp_tpdf **typedefs;
519 unsigned int i, u;
520 LY_ERR ret = LY_EVALID;
521
522 /* check name collisions - typedefs and groupings */
523 ids_global = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1);
524 ids_scoped = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1);
525 LY_ARRAY_FOR(ctx->mod->typedefs, i) {
526 if (lysp_check_typedef(ctx, NULL, &ctx->mod->typedefs[i], ids_global, ids_scoped)) {
527 goto cleanup;
528 }
529 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100530 LY_ARRAY_FOR(ctx->mod->includes, i) {
531 LY_ARRAY_FOR(ctx->mod->includes[i].submodule->typedefs, u) {
532 if (lysp_check_typedef(ctx, NULL, &ctx->mod->includes[i].submodule->typedefs[u], ids_global, ids_scoped)) {
533 goto cleanup;
534 }
535 }
536 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100537 for (u = 0; u < ctx->tpdfs_nodes.count; ++u) {
Radek Krejci056d0a82018-12-06 16:57:25 +0100538 typedefs = lysp_node_typedefs_p((struct lysp_node *)ctx->tpdfs_nodes.objs[u]);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100539 LY_ARRAY_FOR(*typedefs, i) {
540 if (lysp_check_typedef(ctx, (struct lysp_node *)ctx->tpdfs_nodes.objs[u], &(*typedefs)[i], ids_global, ids_scoped)) {
541 goto cleanup;
542 }
543 }
544 }
545 ret = LY_SUCCESS;
546cleanup:
547 lyht_free(ids_global);
548 lyht_free(ids_scoped);
549 ly_set_erase(&ctx->tpdfs_nodes, NULL);
550
551 return ret;
552}
553
Radek Krejci086c7132018-10-26 15:29:04 +0200554void
555lys_module_implement(struct lys_module *mod)
556{
557 assert(mod);
558 if (mod->parsed) {
559 mod->parsed->implemented = 1;
560 }
561 if (mod->compiled) {
562 mod->compiled->implemented = 1;
563 }
564}
565
Radek Krejci9ed7a192018-10-31 16:23:51 +0100566struct lysp_load_module_check_data {
567 const char *name;
568 const char *revision;
569 const char *path;
570 const char* submoduleof;
571};
572
573static LY_ERR
574lysp_load_module_check(struct ly_ctx *ctx, struct lysp_module *mod, void *data)
575{
576 struct lysp_load_module_check_data *info = data;
577 const char *filename, *dot, *rev;
578 size_t len;
579
580 if (info->name) {
581 /* check name of the parsed model */
582 if (strcmp(info->name, mod->name)) {
583 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", mod->name, info->name);
584 return LY_EINVAL;
585 }
586 }
587 if (info->revision) {
588 /* check revision of the parsed model */
589 if (!mod->revs || strcmp(info->revision, mod->revs[0].date)) {
590 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", mod->name,
591 mod->revs[0].date, info->revision);
592 return LY_EINVAL;
593 }
594 }
595 if (info->submoduleof) {
596 /* check that we have really a submodule */
597 if (!mod->submodule) {
598 /* submodule is not a submodule */
599 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Included \"%s\" schema from \"%s\" is actually not a submodule.",
600 mod->name, info->submoduleof);
601 return LY_EVALID;
602 }
603 /* check that the submodule belongs-to our module */
604 if (strcmp(info->submoduleof, mod->belongsto)) {
605 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
606 mod->name, info->submoduleof, mod->belongsto);
607 return LY_EVALID;
608 }
609 /* check circular dependency */
610 if (mod->parsing) {
611 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", mod->name);
612 return LY_EVALID;
613 }
614 }
615 if (info->path) {
616 /* check that name and revision match filename */
617 filename = strrchr(info->path, '/');
618 if (!filename) {
619 filename = info->path;
620 } else {
621 filename++;
622 }
623 /* name */
624 len = strlen(mod->name);
625 rev = strchr(filename, '@');
626 dot = strrchr(info->path, '.');
627 if (strncmp(filename, mod->name, len) ||
628 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
629 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
630 }
631 /* revision */
632 if (rev) {
633 len = dot - ++rev;
634 if (!mod->revs || len != 10 || strncmp(mod->revs[0].date, rev, len)) {
635 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
636 mod->revs ? mod->revs[0].date : "none");
637 }
638 }
639 }
640 return LY_SUCCESS;
641}
642
643LY_ERR
Radek Krejci3b1f9292018-11-08 10:58:35 +0100644lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, int implement, struct ly_parser_ctx *main_ctx,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100645 struct lys_module **result)
646{
647 int fd;
648 char *filepath = NULL;
649 LYS_INFORMAT format;
650 struct lys_module *mod = NULL;
651 LY_ERR ret = LY_SUCCESS;
652 struct lysp_load_module_check_data check_data = {0};
653
654 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, revision,
655 &filepath, &format));
656 LY_CHECK_ERR_RET(!filepath, LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.",
657 name, revision ? "@" : "", revision ? revision : ""), LY_ENOTFOUND);
658
659
660 LOGVRB("Loading schema from \"%s\" file.", filepath);
661
662 /* open the file */
663 fd = open(filepath, O_RDONLY);
664 LY_CHECK_ERR_GOTO(fd < 0, LOGERR(ctx, LY_ESYS, "Unable to open data model file \"%s\" (%s).",
665 filepath, strerror(errno)); ret = LY_ESYS, cleanup);
666
667 check_data.name = name;
668 check_data.revision = revision;
669 check_data.path = filepath;
Radek Krejci3b1f9292018-11-08 10:58:35 +0100670 mod = lys_parse_fd_(ctx, fd, format, implement, main_ctx,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100671 lysp_load_module_check, &check_data);
672 close(fd);
673 LY_CHECK_ERR_GOTO(!mod, ly_errcode(ctx), cleanup);
674
675 if (!mod->parsed->filepath) {
676 char rpath[PATH_MAX];
677 if (realpath(filepath, rpath) != NULL) {
678 mod->parsed->filepath = lydict_insert(ctx, rpath, 0);
679 } else {
680 mod->parsed->filepath = lydict_insert(ctx, filepath, 0);
681 }
682 }
683
684 *result = mod;
685
686 /* success */
687cleanup:
688 free(filepath);
689 return ret;
690}
691
Radek Krejcid33273d2018-10-25 14:55:52 +0200692LY_ERR
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100693lysp_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 +0200694{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100695 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200696 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100697 void (*module_data_free)(void *module_data, void *user_data) = NULL;
698 struct lysp_load_module_check_data check_data = {0};
Radek Krejci086c7132018-10-26 15:29:04 +0200699
700 /* try to get the module from the context */
701 if (revision) {
702 *mod = (struct lys_module*)ly_ctx_get_module(ctx, name, revision);
703 } else {
704 *mod = (struct lys_module*)ly_ctx_get_module_latest(ctx, name);
705 }
706
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100707 if (!(*mod) || (require_parsed && !(*mod)->parsed)) {
708 (*mod) = NULL;
709
Radek Krejci086c7132018-10-26 15:29:04 +0200710 /* check collision with other implemented revision */
711 if (implement && ly_ctx_get_module_implemented(ctx, name)) {
712 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
713 "Module \"%s\" is already present in other implemented revision.", name);
714 return LY_EDENIED;
715 }
716
Radek Krejci9ed7a192018-10-31 16:23:51 +0100717 /* module not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200718 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
719search_clb:
720 if (ctx->imp_clb) {
721 if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100722 &format, &module_data, &module_data_free) == LY_SUCCESS) {
723 check_data.name = name;
724 check_data.revision = revision;
Radek Krejci3b1f9292018-11-08 10:58:35 +0100725 *mod = lys_parse_mem_(ctx, module_data, format, implement, NULL,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100726 lysp_load_module_check, &check_data);
727 if (module_data_free) {
728 module_data_free((void*)module_data, ctx->imp_clb_data);
729 }
Radek Krejci086c7132018-10-26 15:29:04 +0200730 }
731 }
732 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
733 goto search_file;
734 }
735 } else {
736search_file:
737 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
738 /* module was not received from the callback or there is no callback set */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100739 lys_module_localfile(ctx, name, revision, implement, NULL, mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200740 }
741 if (!(*mod) && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
742 goto search_clb;
743 }
744 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100745
Radek Krejcid14e9692018-11-01 11:00:37 +0100746 if ((*mod) && !revision && ((*mod)->parsed->latest_revision == 1)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100747 /* update the latest_revision flag - here we have selected the latest available schema,
748 * consider that even the callback provides correct latest revision */
749 (*mod)->parsed->latest_revision = 2;
750 }
Radek Krejci086c7132018-10-26 15:29:04 +0200751 } else {
752 /* we have module from the current context */
753 if (implement && (ly_ctx_get_module_implemented(ctx, name) != *mod)) {
754 /* check collision with other implemented revision */
755 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
756 "Module \"%s\" is already present in other implemented revision.", name);
757 *mod = NULL;
758 return LY_EDENIED;
759 }
760
761 /* circular check */
Radek Krejcif8f882a2018-10-31 14:51:15 +0100762 if ((*mod)->parsed && (*mod)->parsed->parsing) {
Radek Krejci086c7132018-10-26 15:29:04 +0200763 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
764 *mod = NULL;
765 return LY_EVALID;
766 }
767 }
768 if (!(*mod)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100769 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "%s \"%s\" module failed.", implement ? "Loading" : "Importing", name);
Radek Krejci086c7132018-10-26 15:29:04 +0200770 return LY_EVALID;
771 }
772
773 if (implement) {
774 /* mark the module implemented, check for collision was already done */
775 lys_module_implement(*mod);
776 }
Radek Krejci086c7132018-10-26 15:29:04 +0200777
778 return LY_SUCCESS;
779}
780
781LY_ERR
Radek Krejci3b1f9292018-11-08 10:58:35 +0100782lysp_load_submodule(struct ly_parser_ctx *ctx, struct lysp_module *mod, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200783{
784 struct lys_module *submod;
785 const char *submodule_data = NULL;
786 LYS_INFORMAT format = LYS_IN_UNKNOWN;
787 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100788 struct lysp_load_module_check_data check_data = {0};
Radek Krejcid33273d2018-10-25 14:55:52 +0200789
Radek Krejcibbe09a92018-11-08 09:36:54 +0100790 /* submodule not present in the context, get the input data and parse it */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100791 if (!(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200792search_clb:
Radek Krejci3b1f9292018-11-08 10:58:35 +0100793 if (ctx->ctx->imp_clb) {
794 if (ctx->ctx->imp_clb(mod->name, NULL, inc->name, inc->rev[0] ? inc->rev : NULL, ctx->ctx->imp_clb_data,
Radek Krejcibbe09a92018-11-08 09:36:54 +0100795 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
796 check_data.name = inc->name;
797 check_data.revision = inc->rev[0] ? inc->rev : NULL;
798 check_data.submoduleof = mod->name;
Radek Krejci3b1f9292018-11-08 10:58:35 +0100799 submod = lys_parse_mem_(ctx->ctx, submodule_data, format, mod->implemented, ctx,
Radek Krejcibbe09a92018-11-08 09:36:54 +0100800 lysp_load_module_check, &check_data);
801 if (submodule_data_free) {
Radek Krejci3b1f9292018-11-08 10:58:35 +0100802 submodule_data_free((void*)submodule_data, ctx->ctx->imp_clb_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200803 }
804 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200805 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100806 if (!submod && !(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100807 goto search_file;
Radek Krejcid33273d2018-10-25 14:55:52 +0200808 }
Radek Krejci2d31ea72018-10-25 15:46:42 +0200809 } else {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100810search_file:
Radek Krejci3b1f9292018-11-08 10:58:35 +0100811 if (!(ctx->ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100812 /* module was not received from the callback or there is no callback set */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100813 lys_module_localfile(ctx->ctx, inc->name, inc->rev[0] ? inc->rev : NULL, mod->implemented, ctx, &submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100814 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100815 if (!submod && (ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100816 goto search_clb;
817 }
818 }
819 if (submod) {
820 if (!inc->rev[0] && (submod->parsed->latest_revision == 1)) {
821 /* update the latest_revision flag - here we have selected the latest available schema,
822 * consider that even the callback provides correct latest revision */
823 submod->parsed->latest_revision = 2;
824 }
825
826 inc->submodule = submod->parsed;
827 free(submod);
Radek Krejcid33273d2018-10-25 14:55:52 +0200828 }
829 if (!inc->submodule) {
Radek Krejci3b1f9292018-11-08 10:58:35 +0100830 LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name, mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200831 return LY_EVALID;
832 }
833
834 return LY_SUCCESS;
835}
836
Radek Krejcibbe09a92018-11-08 09:36:54 +0100837#define FIND_MODULE(TYPE, MOD, ID) \
Radek Krejcice8c1592018-10-29 15:35:51 +0100838 TYPE *imp; \
839 if (!strncmp((MOD)->prefix, prefix, len) && (MOD)->prefix[len] == '\0') { \
840 /* it is the prefix of the module itself */ \
Radek Krejcibbe09a92018-11-08 09:36:54 +0100841 m = ly_ctx_get_module((MOD)->ctx, (MOD)->name, ((struct lysc_module*)(MOD))->revision); \
Radek Krejcice8c1592018-10-29 15:35:51 +0100842 } \
843 /* search in imports */ \
Radek Krejcibbe09a92018-11-08 09:36:54 +0100844 if (!m) { \
845 LY_ARRAY_FOR((MOD)->imports, TYPE, imp) { \
846 if (!strncmp(imp->prefix, prefix, len) && (MOD)->prefix[len] == '\0') { \
847 m = imp->module; \
848 break; \
849 } \
Radek Krejcice8c1592018-10-29 15:35:51 +0100850 } \
Radek Krejcibbe09a92018-11-08 09:36:54 +0100851 }
Radek Krejcice8c1592018-10-29 15:35:51 +0100852
Radek Krejcibbe09a92018-11-08 09:36:54 +0100853struct lysc_module *
Radek Krejcia3045382018-11-22 14:30:31 +0100854lysc_module_find_prefix(const struct lysc_module *mod, const char *prefix, size_t len)
Radek Krejci151a5b72018-10-19 14:21:44 +0200855{
Radek Krejcibbe09a92018-11-08 09:36:54 +0100856 const struct lys_module *m = NULL;
857
858 FIND_MODULE(struct lysc_import, mod, 1);
859 return m ? m->compiled : NULL;
Radek Krejcice8c1592018-10-29 15:35:51 +0100860}
Radek Krejci151a5b72018-10-19 14:21:44 +0200861
Radek Krejcibbe09a92018-11-08 09:36:54 +0100862struct lysp_module *
Radek Krejcia3045382018-11-22 14:30:31 +0100863lysp_module_find_prefix(const struct lysp_module *mod, const char *prefix, size_t len)
Radek Krejcice8c1592018-10-29 15:35:51 +0100864{
Radek Krejcibbe09a92018-11-08 09:36:54 +0100865 const struct lys_module *m = NULL;
866
867 FIND_MODULE(struct lysp_import, mod, 1);
868 return m ? m->parsed : NULL;
Radek Krejcice8c1592018-10-29 15:35:51 +0100869}
Radek Krejci151a5b72018-10-19 14:21:44 +0200870
Radek Krejcice8c1592018-10-29 15:35:51 +0100871struct lys_module *
Radek Krejcia3045382018-11-22 14:30:31 +0100872lys_module_find_prefix(const struct lys_module *mod, const char *prefix, size_t len)
Radek Krejcice8c1592018-10-29 15:35:51 +0100873{
Radek Krejcibbe09a92018-11-08 09:36:54 +0100874 const struct lys_module *m = NULL;
875
Radek Krejcice8c1592018-10-29 15:35:51 +0100876 if (mod->compiled) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100877 FIND_MODULE(struct lysc_import, mod->compiled, 1);
Radek Krejcice8c1592018-10-29 15:35:51 +0100878 } else {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100879 FIND_MODULE(struct lysp_import, mod->parsed, 2);
880 }
881 return (struct lys_module*)m;
882}
883
Radek Krejcia3045382018-11-22 14:30:31 +0100884const char *
885lys_nodetype2str(uint16_t nodetype)
886{
887 switch(nodetype) {
888 case LYS_CONTAINER:
889 return "container";
890 case LYS_CHOICE:
891 return "choice";
892 case LYS_LEAF:
893 return "leaf";
894 case LYS_LEAFLIST:
895 return "leaf-list";
896 case LYS_LIST:
897 return "list";
898 case LYS_ANYXML:
899 return "anyxml";
900 case LYS_ANYDATA:
901 return "anydata";
902 default:
903 return "unknown";
904 }
905}
906
Radek Krejcibbe09a92018-11-08 09:36:54 +0100907struct lysp_tpdf **
Radek Krejci056d0a82018-12-06 16:57:25 +0100908lysp_node_typedefs_p(const struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100909{
910 switch (node->nodetype) {
911 case LYS_CONTAINER:
912 return &((struct lysp_node_container*)node)->typedefs;
913 case LYS_LIST:
914 return &((struct lysp_node_list*)node)->typedefs;
915 case LYS_GROUPING:
916 return &((struct lysp_grp*)node)->typedefs;
917 case LYS_ACTION:
918 return &((struct lysp_action*)node)->typedefs;
919 case LYS_INOUT:
920 return &((struct lysp_action_inout*)node)->typedefs;
921 case LYS_NOTIF:
922 return &((struct lysp_notif*)node)->typedefs;
923 default:
924 return NULL;
Radek Krejci151a5b72018-10-19 14:21:44 +0200925 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200926}
Radek Krejcibbe09a92018-11-08 09:36:54 +0100927
Radek Krejci056d0a82018-12-06 16:57:25 +0100928API const struct lysp_tpdf *
929lysp_node_typedefs(const struct lysp_node *node)
930{
931 struct lysp_tpdf **tpdfs;
932 tpdfs = lysp_node_typedefs_p(node);
933 if (tpdfs) {
934 return *tpdfs;
935 } else {
936 return NULL;
937 }
938}
939
Radek Krejcibbe09a92018-11-08 09:36:54 +0100940struct lysp_action **
Radek Krejci056d0a82018-12-06 16:57:25 +0100941lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100942{
943 assert(node);
944 switch (node->nodetype) {
945 case LYS_CONTAINER:
946 return &((struct lysp_node_container*)node)->actions;
947 case LYS_LIST:
948 return &((struct lysp_node_list*)node)->actions;
949 case LYS_GROUPING:
950 return &((struct lysp_grp*)node)->actions;
951 case LYS_AUGMENT:
952 return &((struct lysp_augment*)node)->actions;
953 default:
954 return NULL;
955 }
956}
957
Radek Krejci056d0a82018-12-06 16:57:25 +0100958API const struct lysp_action *
959lysp_node_actions(const struct lysp_node *node)
960{
961 struct lysp_action **actions;
962 actions = lysp_node_actions_p((struct lysp_node*)node);
963 if (actions) {
964 return *actions;
965 } else {
966 return NULL;
967 }
968}
969
Radek Krejcibbe09a92018-11-08 09:36:54 +0100970struct lysp_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +0100971lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100972{
973 assert(node);
974 switch (node->nodetype) {
975 case LYS_CONTAINER:
976 return &((struct lysp_node_container*)node)->notifs;
977 case LYS_LIST:
978 return &((struct lysp_node_list*)node)->notifs;
979 case LYS_GROUPING:
980 return &((struct lysp_grp*)node)->notifs;
981 case LYS_AUGMENT:
982 return &((struct lysp_augment*)node)->notifs;
983 default:
984 return NULL;
985 }
986}
987
Radek Krejci056d0a82018-12-06 16:57:25 +0100988API const struct lysp_notif *
989lysp_node_notifs(const struct lysp_node *node)
990{
991 struct lysp_notif **notifs;
992 notifs = lysp_node_notifs_p((struct lysp_node*)node);
993 if (notifs) {
994 return *notifs;
995 } else {
996 return NULL;
997 }
998}
999
Radek Krejcibbe09a92018-11-08 09:36:54 +01001000struct lysp_node **
Radek Krejci056d0a82018-12-06 16:57:25 +01001001lysp_node_children_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001002{
1003 assert(node);
1004 switch (node->nodetype) {
1005 case LYS_CONTAINER:
1006 return &((struct lysp_node_container*)node)->child;
1007 case LYS_CHOICE:
1008 return &((struct lysp_node_choice*)node)->child;
1009 case LYS_LIST:
1010 return &((struct lysp_node_list*)node)->child;
1011 case LYS_CASE:
1012 return &((struct lysp_node_case*)node)->child;
1013 case LYS_GROUPING:
1014 return &((struct lysp_grp*)node)->data;
1015 case LYS_AUGMENT:
1016 return &((struct lysp_augment*)node)->child;
1017 case LYS_INOUT:
1018 return &((struct lysp_action_inout*)node)->data;
1019 case LYS_NOTIF:
1020 return &((struct lysp_notif*)node)->data;
1021 default:
1022 return NULL;
1023 }
1024}
1025
Radek Krejci056d0a82018-12-06 16:57:25 +01001026API const struct lysp_node *
1027lysp_node_children(const struct lysp_node *node)
1028{
1029 struct lysp_node **children;
1030 children = lysp_node_children_p((struct lysp_node*)node);
1031 if (children) {
1032 return *children;
1033 } else {
1034 return NULL;
1035 }
1036}
1037
1038struct lysc_action **
1039lysc_node_actions_p(struct lysc_node *node)
1040{
1041 assert(node);
1042 switch (node->nodetype) {
1043 case LYS_CONTAINER:
1044 return &((struct lysc_node_container*)node)->actions;
1045 case LYS_LIST:
1046 return &((struct lysc_node_list*)node)->actions;
1047 default:
1048 return NULL;
1049 }
1050}
1051
1052API const struct lysc_action *
1053lysc_node_actions(const struct lysc_node *node)
1054{
1055 struct lysc_action **actions;
1056 actions = lysc_node_actions_p((struct lysc_node*)node);
1057 if (actions) {
1058 return *actions;
1059 } else {
1060 return NULL;
1061 }
1062}
1063
1064struct lysc_notif **
1065lysc_node_notifs_p(struct lysc_node *node)
1066{
1067 assert(node);
1068 switch (node->nodetype) {
1069 case LYS_CONTAINER:
1070 return &((struct lysc_node_container*)node)->notifs;
1071 case LYS_LIST:
1072 return &((struct lysc_node_list*)node)->notifs;
1073 default:
1074 return NULL;
1075 }
1076}
1077
1078API const struct lysc_notif *
1079lysc_node_notifs(const struct lysc_node *node)
1080{
1081 struct lysc_notif **notifs;
1082 notifs = lysc_node_notifs_p((struct lysc_node*)node);
1083 if (notifs) {
1084 return *notifs;
1085 } else {
1086 return NULL;
1087 }
1088}
1089
Radek Krejcibbe09a92018-11-08 09:36:54 +01001090struct lysc_node **
Radek Krejci056d0a82018-12-06 16:57:25 +01001091lysc_node_children_p(const struct lysc_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001092{
1093 assert(node);
1094 switch (node->nodetype) {
1095 case LYS_CONTAINER:
1096 return &((struct lysc_node_container*)node)->child;
1097 case LYS_CHOICE:
Radek Krejcia3045382018-11-22 14:30:31 +01001098 if (((struct lysc_node_choice*)node)->cases) {
1099 return &((struct lysc_node_choice*)node)->cases[0].child;
1100 } else {
1101 return NULL;
1102 }
Radek Krejcibbe09a92018-11-08 09:36:54 +01001103 case LYS_LIST:
1104 return &((struct lysc_node_list*)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001105/* TODO
1106 case LYS_INOUT:
1107 return &((struct lysc_action_inout*)node)->child;
1108 case LYS_NOTIF:
1109 return &((struct lysc_notif*)node)->child;
1110*/
1111 default:
1112 return NULL;
1113 }
1114}
1115
Radek Krejci056d0a82018-12-06 16:57:25 +01001116API const struct lysc_node *
1117lysc_node_children(const struct lysc_node *node)
Radek Krejcia3045382018-11-22 14:30:31 +01001118{
Radek Krejci056d0a82018-12-06 16:57:25 +01001119 struct lysc_node **children;
1120 children = lysc_node_children_p((struct lysc_node*)node);
1121 if (children) {
1122 return *children;
1123 } else {
Radek Krejcia3045382018-11-22 14:30:31 +01001124 return NULL;
1125 }
1126}
1127
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001128struct lys_module *
1129lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1130{
1131 unsigned int u;
1132
1133 for (u = 0; u < ctx->list.count; ++u) {
1134 if (((struct lys_module*)ctx->list.objs[u])->parsed == mod) {
1135 return ((struct lys_module*)ctx->list.objs[u]);
1136 }
1137 }
1138 return NULL;
1139}
1140