blob: 0146f43c4e684f309e02df0360d012bf6f6bb9bf [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{
Radek Krejci0fb28562018-12-13 15:17:37 +0100260 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100261 unsigned int u;
262
Radek Krejci0fb28562018-12-13 15:17:37 +0100263 typedefs = lysp_node_typedefs(node);
264 LY_ARRAY_FOR(typedefs, u) {
265 if (!strcmp(name, typedefs[u].name)) {
266 /* match */
267 return &typedefs[u];
Radek Krejcibbe09a92018-11-08 09:36:54 +0100268 }
269 }
270
271 return NULL;
272}
273
Radek Krejci4f28eda2018-11-12 11:46:16 +0100274static LY_DATA_TYPE
275lysp_type_str2builtin(const char *name, size_t len)
276{
277 if (len >= 4) { /* otherwise it does not match any built-in type */
278 if (name[0] == 'b') {
279 if (name[1] == 'i') {
280 if (len == 6 && !strncmp(&name[2], "nary", 4)) {
281 return LY_TYPE_BINARY;
282 } else if (len == 4 && !strncmp(&name[2], "ts", 2)) {
283 return LY_TYPE_BITS;
284 }
285 } else if (len == 7 && !strncmp(&name[1], "oolean", 6)) {
286 return LY_TYPE_BOOL;
287 }
288 } else if (name[0] == 'd') {
289 if (len == 9 && !strncmp(&name[1], "ecimal64", 8)) {
290 return LY_TYPE_DEC64;
291 }
292 } else if (name[0] == 'e') {
293 if (len == 5 && !strncmp(&name[1], "mpty", 4)) {
294 return LY_TYPE_EMPTY;
295 } else if (len == 11 && !strncmp(&name[1], "numeration", 10)) {
296 return LY_TYPE_ENUM;
297 }
298 } else if (name[0] == 'i') {
299 if (name[1] == 'n') {
300 if (len == 4 && !strncmp(&name[2], "t8", 2)) {
301 return LY_TYPE_INT8;
302 } else if (len == 5) {
303 if (!strncmp(&name[2], "t16", 3)) {
304 return LY_TYPE_INT16;
305 } else if (!strncmp(&name[2], "t32", 3)) {
306 return LY_TYPE_INT32;
307 } else if (!strncmp(&name[2], "t64", 3)) {
308 return LY_TYPE_INT64;
309 }
310 } else if (len == 19 && !strncmp(&name[2], "stance-identifier", 17)) {
311 return LY_TYPE_INST;
312 }
313 } else if (len == 11 && !strncmp(&name[1], "dentityref", 10)) {
314 return LY_TYPE_IDENT;
315 }
316 } else if (name[0] == 'l') {
317 if (len == 7 && !strncmp(&name[1], "eafref", 6)) {
318 return LY_TYPE_LEAFREF;
319 }
320 } else if (name[0] == 's') {
321 if (len == 6 && !strncmp(&name[1], "tring", 5)) {
322 return LY_TYPE_STRING;
323 }
324 } else if (name[0] == 'u') {
325 if (name[1] == 'n') {
326 if (len == 5 && !strncmp(&name[2], "ion", 3)) {
327 return LY_TYPE_UNION;
328 }
329 } else if (name[1] == 'i' && name[2] == 'n' && name[3] == 't') {
330 if (len == 5 && name[4] == '8') {
331 return LY_TYPE_UINT8;
332 } else if (len == 6) {
333 if (!strncmp(&name[4], "16", 2)) {
334 return LY_TYPE_UINT16;
335 } else if (!strncmp(&name[4], "32", 2)) {
336 return LY_TYPE_UINT32;
337 } else if (!strncmp(&name[4], "64", 2)) {
338 return LY_TYPE_UINT64;
339 }
340 }
341 }
342 }
343 }
344
345 return LY_TYPE_UNKNOWN;
346}
347
Radek Krejcibbe09a92018-11-08 09:36:54 +0100348LY_ERR
349lysp_type_find(const char *id, struct lysp_node *start_node, struct lysp_module *start_module,
Radek Krejci4f28eda2018-11-12 11:46:16 +0100350 LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node, struct lysp_module **module)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100351{
352 const char *str, *name;
353 struct lysp_tpdf *typedefs;
354 unsigned int u, v;
355
356 assert(id);
357 assert(start_module);
358 assert(tpdf);
359 assert(node);
360 assert(module);
361
Radek Krejci4f28eda2018-11-12 11:46:16 +0100362 *node = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100363 str = strchr(id, ':');
364 if (str) {
365 *module = lysp_module_find_prefix(start_module, id, str - id);
366 name = str + 1;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100367 *type = LY_TYPE_UNKNOWN;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100368 } else {
369 *module = start_module;
370 name = id;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100371
372 /* check for built-in types */
373 *type = lysp_type_str2builtin(name, strlen(name));
374 if (*type) {
375 *tpdf = NULL;
376 return LY_SUCCESS;
377 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100378 }
379 LY_CHECK_RET(!(*module), LY_ENOTFOUND);
380
381 if (start_node && *module == start_module) {
382 /* search typedefs in parent's nodes */
383 *node = start_node;
384 while (*node) {
385 *tpdf = lysp_type_match(name, *node);
386 if (*tpdf) {
387 /* match */
388 return LY_SUCCESS;
389 }
390 *node = (*node)->parent;
391 }
392 }
393
394 /* search in top-level typedefs */
395 if ((*module)->typedefs) {
396 LY_ARRAY_FOR((*module)->typedefs, u) {
397 if (!strcmp(name, (*module)->typedefs[u].name)) {
398 /* match */
399 *tpdf = &(*module)->typedefs[u];
400 return LY_SUCCESS;
401 }
402 }
403 }
404
405 /* search in submodules' typedefs */
406 LY_ARRAY_FOR((*module)->includes, u) {
407 typedefs = (*module)->includes[u].submodule->typedefs;
408 if (typedefs) {
409 LY_ARRAY_FOR(typedefs, v) {
410 if (!strcmp(name, typedefs[v].name)) {
411 /* match */
412 *tpdf = &typedefs[v];
413 return LY_SUCCESS;
414 }
415 }
416 }
417 }
418
419 return LY_ENOTFOUND;
420}
421
422/*
423 * @brief Check name of a new type to avoid name collisions.
424 *
425 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
426 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
427 * @param[in] tpdf Typedef definition to check.
428 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
429 * typedefs are checked, caller is supposed to free the table.
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 * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise.
433 */
434static LY_ERR
Radek Krejci0fb28562018-12-13 15:17:37 +0100435lysp_check_typedef(struct ly_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
Radek Krejcibbe09a92018-11-08 09:36:54 +0100436 struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped)
437{
438 struct lysp_node *parent;
439 uint32_t hash;
440 size_t name_len;
441 const char *name;
442 unsigned int u;
Radek Krejci0fb28562018-12-13 15:17:37 +0100443 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100444
445 assert(ctx);
446 assert(tpdf);
447
448 name = tpdf->name;
449 name_len = strlen(name);
450
Radek Krejci4f28eda2018-11-12 11:46:16 +0100451 if (lysp_type_str2builtin(name, name_len)) {
452 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
453 "Invalid name \"%s\" of typedef - name collision with a built-in type.", name);
454 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100455 }
456
457 /* check locally scoped typedefs (avoid name shadowing) */
458 if (node) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100459 typedefs = lysp_node_typedefs(node);
460 LY_ARRAY_FOR(typedefs, u) {
461 if (&typedefs[u] == tpdf) {
462 break;
463 }
464 if (!strcmp(name, typedefs[u].name)) {
465 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
466 "Invalid name \"%s\" of typedef - name collision with sibling type.", name);
467 return LY_EEXIST;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100468 }
469 }
470 /* search typedefs in parent's nodes */
471 for (parent = node->parent; parent; parent = node->parent) {
472 if (lysp_type_match(name, parent)) {
473 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
474 "Invalid name \"%s\" of typedef - name collision with another scoped type.", name);
475 return LY_EEXIST;
476 }
477 }
478 }
479
480 /* check collision with the top-level typedefs */
481 hash = dict_hash(name, name_len);
482 if (node) {
483 lyht_insert(tpdfs_scoped, &name, hash, NULL);
484 if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
485 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
486 "Invalid name \"%s\" of typedef - scoped type collide with a top-level type.", name);
487 return LY_EEXIST;
488 }
489 } else {
490 if (lyht_insert(tpdfs_global, &name, hash, NULL)) {
491 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX_YANG,
492 "Invalid name \"%s\" of typedef - name collision with another top-level type.", name);
493 return LY_EEXIST;
494 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100495 /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
496 * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
497 * is detected in the first branch few lines above */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100498 }
499
500 return LY_SUCCESS;
501}
502
503static int
504lysp_id_cmp(void *val1, void *val2, int UNUSED(mod), void *UNUSED(cb_data))
505{
506 return !strcmp(val1, val2);
507}
508
509LY_ERR
510lysp_check_typedefs(struct ly_parser_ctx *ctx)
511{
512 struct hash_table *ids_global;
513 struct hash_table *ids_scoped;
Radek Krejci0fb28562018-12-13 15:17:37 +0100514 const struct lysp_tpdf *typedefs;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100515 unsigned int i, u;
516 LY_ERR ret = LY_EVALID;
517
518 /* check name collisions - typedefs and groupings */
519 ids_global = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1);
520 ids_scoped = lyht_new(8, sizeof(char*), lysp_id_cmp, NULL, 1);
521 LY_ARRAY_FOR(ctx->mod->typedefs, i) {
522 if (lysp_check_typedef(ctx, NULL, &ctx->mod->typedefs[i], ids_global, ids_scoped)) {
523 goto cleanup;
524 }
525 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100526 LY_ARRAY_FOR(ctx->mod->includes, i) {
527 LY_ARRAY_FOR(ctx->mod->includes[i].submodule->typedefs, u) {
528 if (lysp_check_typedef(ctx, NULL, &ctx->mod->includes[i].submodule->typedefs[u], ids_global, ids_scoped)) {
529 goto cleanup;
530 }
531 }
532 }
Radek Krejcibbe09a92018-11-08 09:36:54 +0100533 for (u = 0; u < ctx->tpdfs_nodes.count; ++u) {
Radek Krejci0fb28562018-12-13 15:17:37 +0100534 typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[u]);
535 LY_ARRAY_FOR(typedefs, i) {
536 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 +0100537 goto cleanup;
538 }
539 }
540 }
541 ret = LY_SUCCESS;
542cleanup:
543 lyht_free(ids_global);
544 lyht_free(ids_scoped);
545 ly_set_erase(&ctx->tpdfs_nodes, NULL);
546
547 return ret;
548}
549
Radek Krejci086c7132018-10-26 15:29:04 +0200550void
551lys_module_implement(struct lys_module *mod)
552{
553 assert(mod);
554 if (mod->parsed) {
555 mod->parsed->implemented = 1;
556 }
557 if (mod->compiled) {
558 mod->compiled->implemented = 1;
559 }
560}
561
Radek Krejci9ed7a192018-10-31 16:23:51 +0100562struct lysp_load_module_check_data {
563 const char *name;
564 const char *revision;
565 const char *path;
566 const char* submoduleof;
567};
568
569static LY_ERR
570lysp_load_module_check(struct ly_ctx *ctx, struct lysp_module *mod, void *data)
571{
572 struct lysp_load_module_check_data *info = data;
573 const char *filename, *dot, *rev;
574 size_t len;
575
576 if (info->name) {
577 /* check name of the parsed model */
578 if (strcmp(info->name, mod->name)) {
579 LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", mod->name, info->name);
580 return LY_EINVAL;
581 }
582 }
583 if (info->revision) {
584 /* check revision of the parsed model */
585 if (!mod->revs || strcmp(info->revision, mod->revs[0].date)) {
586 LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", mod->name,
587 mod->revs[0].date, info->revision);
588 return LY_EINVAL;
589 }
590 }
591 if (info->submoduleof) {
592 /* check that we have really a submodule */
593 if (!mod->submodule) {
594 /* submodule is not a submodule */
595 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Included \"%s\" schema from \"%s\" is actually not a submodule.",
596 mod->name, info->submoduleof);
597 return LY_EVALID;
598 }
599 /* check that the submodule belongs-to our module */
600 if (strcmp(info->submoduleof, mod->belongsto)) {
601 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
602 mod->name, info->submoduleof, mod->belongsto);
603 return LY_EVALID;
604 }
605 /* check circular dependency */
606 if (mod->parsing) {
607 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", mod->name);
608 return LY_EVALID;
609 }
610 }
611 if (info->path) {
612 /* check that name and revision match filename */
613 filename = strrchr(info->path, '/');
614 if (!filename) {
615 filename = info->path;
616 } else {
617 filename++;
618 }
619 /* name */
620 len = strlen(mod->name);
621 rev = strchr(filename, '@');
622 dot = strrchr(info->path, '.');
623 if (strncmp(filename, mod->name, len) ||
624 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
625 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
626 }
627 /* revision */
628 if (rev) {
629 len = dot - ++rev;
630 if (!mod->revs || len != 10 || strncmp(mod->revs[0].date, rev, len)) {
631 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
632 mod->revs ? mod->revs[0].date : "none");
633 }
634 }
635 }
636 return LY_SUCCESS;
637}
638
639LY_ERR
Radek Krejci3b1f9292018-11-08 10:58:35 +0100640lys_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 +0100641 struct lys_module **result)
642{
643 int fd;
644 char *filepath = NULL;
645 LYS_INFORMAT format;
646 struct lys_module *mod = NULL;
647 LY_ERR ret = LY_SUCCESS;
648 struct lysp_load_module_check_data check_data = {0};
649
650 LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, revision,
651 &filepath, &format));
652 LY_CHECK_ERR_RET(!filepath, LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.",
653 name, revision ? "@" : "", revision ? revision : ""), LY_ENOTFOUND);
654
655
656 LOGVRB("Loading schema from \"%s\" file.", filepath);
657
658 /* open the file */
659 fd = open(filepath, O_RDONLY);
660 LY_CHECK_ERR_GOTO(fd < 0, LOGERR(ctx, LY_ESYS, "Unable to open data model file \"%s\" (%s).",
661 filepath, strerror(errno)); ret = LY_ESYS, cleanup);
662
663 check_data.name = name;
664 check_data.revision = revision;
665 check_data.path = filepath;
Radek Krejci3b1f9292018-11-08 10:58:35 +0100666 mod = lys_parse_fd_(ctx, fd, format, implement, main_ctx,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100667 lysp_load_module_check, &check_data);
668 close(fd);
669 LY_CHECK_ERR_GOTO(!mod, ly_errcode(ctx), cleanup);
670
671 if (!mod->parsed->filepath) {
672 char rpath[PATH_MAX];
673 if (realpath(filepath, rpath) != NULL) {
674 mod->parsed->filepath = lydict_insert(ctx, rpath, 0);
675 } else {
676 mod->parsed->filepath = lydict_insert(ctx, filepath, 0);
677 }
678 }
679
680 *result = mod;
681
682 /* success */
683cleanup:
684 free(filepath);
685 return ret;
686}
687
Radek Krejcid33273d2018-10-25 14:55:52 +0200688LY_ERR
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100689lysp_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 +0200690{
Radek Krejci9ed7a192018-10-31 16:23:51 +0100691 const char *module_data = NULL;
Radek Krejci086c7132018-10-26 15:29:04 +0200692 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100693 void (*module_data_free)(void *module_data, void *user_data) = NULL;
694 struct lysp_load_module_check_data check_data = {0};
Radek Krejci086c7132018-10-26 15:29:04 +0200695
696 /* try to get the module from the context */
697 if (revision) {
698 *mod = (struct lys_module*)ly_ctx_get_module(ctx, name, revision);
699 } else {
700 *mod = (struct lys_module*)ly_ctx_get_module_latest(ctx, name);
701 }
702
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100703 if (!(*mod) || (require_parsed && !(*mod)->parsed)) {
704 (*mod) = NULL;
705
Radek Krejci086c7132018-10-26 15:29:04 +0200706 /* check collision with other implemented revision */
707 if (implement && ly_ctx_get_module_implemented(ctx, name)) {
708 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
709 "Module \"%s\" is already present in other implemented revision.", name);
710 return LY_EDENIED;
711 }
712
Radek Krejci9ed7a192018-10-31 16:23:51 +0100713 /* module not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200714 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
715search_clb:
716 if (ctx->imp_clb) {
717 if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100718 &format, &module_data, &module_data_free) == LY_SUCCESS) {
719 check_data.name = name;
720 check_data.revision = revision;
Radek Krejci3b1f9292018-11-08 10:58:35 +0100721 *mod = lys_parse_mem_(ctx, module_data, format, implement, NULL,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100722 lysp_load_module_check, &check_data);
723 if (module_data_free) {
724 module_data_free((void*)module_data, ctx->imp_clb_data);
725 }
Radek Krejci086c7132018-10-26 15:29:04 +0200726 }
727 }
728 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
729 goto search_file;
730 }
731 } else {
732search_file:
733 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
734 /* module was not received from the callback or there is no callback set */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100735 lys_module_localfile(ctx, name, revision, implement, NULL, mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200736 }
737 if (!(*mod) && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
738 goto search_clb;
739 }
740 }
Radek Krejci9ed7a192018-10-31 16:23:51 +0100741
Radek Krejcid14e9692018-11-01 11:00:37 +0100742 if ((*mod) && !revision && ((*mod)->parsed->latest_revision == 1)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100743 /* update the latest_revision flag - here we have selected the latest available schema,
744 * consider that even the callback provides correct latest revision */
745 (*mod)->parsed->latest_revision = 2;
746 }
Radek Krejci086c7132018-10-26 15:29:04 +0200747 } else {
748 /* we have module from the current context */
749 if (implement && (ly_ctx_get_module_implemented(ctx, name) != *mod)) {
750 /* check collision with other implemented revision */
751 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
752 "Module \"%s\" is already present in other implemented revision.", name);
753 *mod = NULL;
754 return LY_EDENIED;
755 }
756
757 /* circular check */
Radek Krejcif8f882a2018-10-31 14:51:15 +0100758 if ((*mod)->parsed && (*mod)->parsed->parsing) {
Radek Krejci086c7132018-10-26 15:29:04 +0200759 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
760 *mod = NULL;
761 return LY_EVALID;
762 }
763 }
764 if (!(*mod)) {
Radek Krejci9ed7a192018-10-31 16:23:51 +0100765 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "%s \"%s\" module failed.", implement ? "Loading" : "Importing", name);
Radek Krejci086c7132018-10-26 15:29:04 +0200766 return LY_EVALID;
767 }
768
769 if (implement) {
770 /* mark the module implemented, check for collision was already done */
771 lys_module_implement(*mod);
772 }
Radek Krejci086c7132018-10-26 15:29:04 +0200773
774 return LY_SUCCESS;
775}
776
777LY_ERR
Radek Krejci3b1f9292018-11-08 10:58:35 +0100778lysp_load_submodule(struct ly_parser_ctx *ctx, struct lysp_module *mod, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200779{
780 struct lys_module *submod;
781 const char *submodule_data = NULL;
782 LYS_INFORMAT format = LYS_IN_UNKNOWN;
783 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100784 struct lysp_load_module_check_data check_data = {0};
Radek Krejcid33273d2018-10-25 14:55:52 +0200785
Radek Krejcibbe09a92018-11-08 09:36:54 +0100786 /* submodule not present in the context, get the input data and parse it */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100787 if (!(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200788search_clb:
Radek Krejci3b1f9292018-11-08 10:58:35 +0100789 if (ctx->ctx->imp_clb) {
790 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 +0100791 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
792 check_data.name = inc->name;
793 check_data.revision = inc->rev[0] ? inc->rev : NULL;
794 check_data.submoduleof = mod->name;
Radek Krejci3b1f9292018-11-08 10:58:35 +0100795 submod = lys_parse_mem_(ctx->ctx, submodule_data, format, mod->implemented, ctx,
Radek Krejcibbe09a92018-11-08 09:36:54 +0100796 lysp_load_module_check, &check_data);
797 if (submodule_data_free) {
Radek Krejci3b1f9292018-11-08 10:58:35 +0100798 submodule_data_free((void*)submodule_data, ctx->ctx->imp_clb_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200799 }
800 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200801 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100802 if (!submod && !(ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100803 goto search_file;
Radek Krejcid33273d2018-10-25 14:55:52 +0200804 }
Radek Krejci2d31ea72018-10-25 15:46:42 +0200805 } else {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100806search_file:
Radek Krejci3b1f9292018-11-08 10:58:35 +0100807 if (!(ctx->ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100808 /* module was not received from the callback or there is no callback set */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100809 lys_module_localfile(ctx->ctx, inc->name, inc->rev[0] ? inc->rev : NULL, mod->implemented, ctx, &submod);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100810 }
Radek Krejci3b1f9292018-11-08 10:58:35 +0100811 if (!submod && (ctx->ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100812 goto search_clb;
813 }
814 }
815 if (submod) {
816 if (!inc->rev[0] && (submod->parsed->latest_revision == 1)) {
817 /* update the latest_revision flag - here we have selected the latest available schema,
818 * consider that even the callback provides correct latest revision */
819 submod->parsed->latest_revision = 2;
820 }
821
822 inc->submodule = submod->parsed;
823 free(submod);
Radek Krejcid33273d2018-10-25 14:55:52 +0200824 }
825 if (!inc->submodule) {
Radek Krejci3b1f9292018-11-08 10:58:35 +0100826 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 +0200827 return LY_EVALID;
828 }
829
830 return LY_SUCCESS;
831}
832
Radek Krejcibbe09a92018-11-08 09:36:54 +0100833#define FIND_MODULE(TYPE, MOD, ID) \
Radek Krejcice8c1592018-10-29 15:35:51 +0100834 TYPE *imp; \
835 if (!strncmp((MOD)->prefix, prefix, len) && (MOD)->prefix[len] == '\0') { \
836 /* it is the prefix of the module itself */ \
Radek Krejcibbe09a92018-11-08 09:36:54 +0100837 m = ly_ctx_get_module((MOD)->ctx, (MOD)->name, ((struct lysc_module*)(MOD))->revision); \
Radek Krejcice8c1592018-10-29 15:35:51 +0100838 } \
839 /* search in imports */ \
Radek Krejcibbe09a92018-11-08 09:36:54 +0100840 if (!m) { \
841 LY_ARRAY_FOR((MOD)->imports, TYPE, imp) { \
842 if (!strncmp(imp->prefix, prefix, len) && (MOD)->prefix[len] == '\0') { \
843 m = imp->module; \
844 break; \
845 } \
Radek Krejcice8c1592018-10-29 15:35:51 +0100846 } \
Radek Krejcibbe09a92018-11-08 09:36:54 +0100847 }
Radek Krejcice8c1592018-10-29 15:35:51 +0100848
Radek Krejcibbe09a92018-11-08 09:36:54 +0100849struct lysc_module *
Radek Krejcia3045382018-11-22 14:30:31 +0100850lysc_module_find_prefix(const struct lysc_module *mod, const char *prefix, size_t len)
Radek Krejci151a5b72018-10-19 14:21:44 +0200851{
Radek Krejcibbe09a92018-11-08 09:36:54 +0100852 const struct lys_module *m = NULL;
853
854 FIND_MODULE(struct lysc_import, mod, 1);
855 return m ? m->compiled : NULL;
Radek Krejcice8c1592018-10-29 15:35:51 +0100856}
Radek Krejci151a5b72018-10-19 14:21:44 +0200857
Radek Krejcibbe09a92018-11-08 09:36:54 +0100858struct lysp_module *
Radek Krejcia3045382018-11-22 14:30:31 +0100859lysp_module_find_prefix(const struct lysp_module *mod, const char *prefix, size_t len)
Radek Krejcice8c1592018-10-29 15:35:51 +0100860{
Radek Krejcibbe09a92018-11-08 09:36:54 +0100861 const struct lys_module *m = NULL;
862
863 FIND_MODULE(struct lysp_import, mod, 1);
864 return m ? m->parsed : NULL;
Radek Krejcice8c1592018-10-29 15:35:51 +0100865}
Radek Krejci151a5b72018-10-19 14:21:44 +0200866
Radek Krejcice8c1592018-10-29 15:35:51 +0100867struct lys_module *
Radek Krejcia3045382018-11-22 14:30:31 +0100868lys_module_find_prefix(const struct lys_module *mod, const char *prefix, size_t len)
Radek Krejcice8c1592018-10-29 15:35:51 +0100869{
Radek Krejcibbe09a92018-11-08 09:36:54 +0100870 const struct lys_module *m = NULL;
871
Radek Krejcice8c1592018-10-29 15:35:51 +0100872 if (mod->compiled) {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100873 FIND_MODULE(struct lysc_import, mod->compiled, 1);
Radek Krejcice8c1592018-10-29 15:35:51 +0100874 } else {
Radek Krejcibbe09a92018-11-08 09:36:54 +0100875 FIND_MODULE(struct lysp_import, mod->parsed, 2);
876 }
877 return (struct lys_module*)m;
878}
879
Radek Krejcia3045382018-11-22 14:30:31 +0100880const char *
881lys_nodetype2str(uint16_t nodetype)
882{
883 switch(nodetype) {
884 case LYS_CONTAINER:
885 return "container";
886 case LYS_CHOICE:
887 return "choice";
888 case LYS_LEAF:
889 return "leaf";
890 case LYS_LEAFLIST:
891 return "leaf-list";
892 case LYS_LIST:
893 return "list";
894 case LYS_ANYXML:
895 return "anyxml";
896 case LYS_ANYDATA:
897 return "anydata";
898 default:
899 return "unknown";
900 }
901}
902
Radek Krejci056d0a82018-12-06 16:57:25 +0100903API const struct lysp_tpdf *
904lysp_node_typedefs(const struct lysp_node *node)
905{
Radek Krejci0fb28562018-12-13 15:17:37 +0100906 switch (node->nodetype) {
907 case LYS_CONTAINER:
908 return ((struct lysp_node_container*)node)->typedefs;
909 case LYS_LIST:
910 return ((struct lysp_node_list*)node)->typedefs;
911 case LYS_GROUPING:
912 return ((struct lysp_grp*)node)->typedefs;
913 case LYS_ACTION:
914 return ((struct lysp_action*)node)->typedefs;
915 case LYS_INOUT:
916 return ((struct lysp_action_inout*)node)->typedefs;
917 case LYS_NOTIF:
918 return ((struct lysp_notif*)node)->typedefs;
919 default:
Radek Krejci056d0a82018-12-06 16:57:25 +0100920 return NULL;
921 }
922}
923
Radek Krejcibbe09a92018-11-08 09:36:54 +0100924struct lysp_action **
Radek Krejci056d0a82018-12-06 16:57:25 +0100925lysp_node_actions_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100926{
927 assert(node);
928 switch (node->nodetype) {
929 case LYS_CONTAINER:
930 return &((struct lysp_node_container*)node)->actions;
931 case LYS_LIST:
932 return &((struct lysp_node_list*)node)->actions;
933 case LYS_GROUPING:
934 return &((struct lysp_grp*)node)->actions;
935 case LYS_AUGMENT:
936 return &((struct lysp_augment*)node)->actions;
937 default:
938 return NULL;
939 }
940}
941
Radek Krejci056d0a82018-12-06 16:57:25 +0100942API const struct lysp_action *
943lysp_node_actions(const struct lysp_node *node)
944{
945 struct lysp_action **actions;
946 actions = lysp_node_actions_p((struct lysp_node*)node);
947 if (actions) {
948 return *actions;
949 } else {
950 return NULL;
951 }
952}
953
Radek Krejcibbe09a92018-11-08 09:36:54 +0100954struct lysp_notif **
Radek Krejci056d0a82018-12-06 16:57:25 +0100955lysp_node_notifs_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100956{
957 assert(node);
958 switch (node->nodetype) {
959 case LYS_CONTAINER:
960 return &((struct lysp_node_container*)node)->notifs;
961 case LYS_LIST:
962 return &((struct lysp_node_list*)node)->notifs;
963 case LYS_GROUPING:
964 return &((struct lysp_grp*)node)->notifs;
965 case LYS_AUGMENT:
966 return &((struct lysp_augment*)node)->notifs;
967 default:
968 return NULL;
969 }
970}
971
Radek Krejci056d0a82018-12-06 16:57:25 +0100972API const struct lysp_notif *
973lysp_node_notifs(const struct lysp_node *node)
974{
975 struct lysp_notif **notifs;
976 notifs = lysp_node_notifs_p((struct lysp_node*)node);
977 if (notifs) {
978 return *notifs;
979 } else {
980 return NULL;
981 }
982}
983
Radek Krejcibbe09a92018-11-08 09:36:54 +0100984struct lysp_node **
Radek Krejci056d0a82018-12-06 16:57:25 +0100985lysp_node_children_p(struct lysp_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +0100986{
987 assert(node);
988 switch (node->nodetype) {
989 case LYS_CONTAINER:
990 return &((struct lysp_node_container*)node)->child;
991 case LYS_CHOICE:
992 return &((struct lysp_node_choice*)node)->child;
993 case LYS_LIST:
994 return &((struct lysp_node_list*)node)->child;
995 case LYS_CASE:
996 return &((struct lysp_node_case*)node)->child;
997 case LYS_GROUPING:
998 return &((struct lysp_grp*)node)->data;
999 case LYS_AUGMENT:
1000 return &((struct lysp_augment*)node)->child;
1001 case LYS_INOUT:
1002 return &((struct lysp_action_inout*)node)->data;
1003 case LYS_NOTIF:
1004 return &((struct lysp_notif*)node)->data;
1005 default:
1006 return NULL;
1007 }
1008}
1009
Radek Krejci056d0a82018-12-06 16:57:25 +01001010API const struct lysp_node *
1011lysp_node_children(const struct lysp_node *node)
1012{
1013 struct lysp_node **children;
1014 children = lysp_node_children_p((struct lysp_node*)node);
1015 if (children) {
1016 return *children;
1017 } else {
1018 return NULL;
1019 }
1020}
1021
1022struct lysc_action **
1023lysc_node_actions_p(struct lysc_node *node)
1024{
1025 assert(node);
1026 switch (node->nodetype) {
1027 case LYS_CONTAINER:
1028 return &((struct lysc_node_container*)node)->actions;
1029 case LYS_LIST:
1030 return &((struct lysc_node_list*)node)->actions;
1031 default:
1032 return NULL;
1033 }
1034}
1035
1036API const struct lysc_action *
1037lysc_node_actions(const struct lysc_node *node)
1038{
1039 struct lysc_action **actions;
1040 actions = lysc_node_actions_p((struct lysc_node*)node);
1041 if (actions) {
1042 return *actions;
1043 } else {
1044 return NULL;
1045 }
1046}
1047
1048struct lysc_notif **
1049lysc_node_notifs_p(struct lysc_node *node)
1050{
1051 assert(node);
1052 switch (node->nodetype) {
1053 case LYS_CONTAINER:
1054 return &((struct lysc_node_container*)node)->notifs;
1055 case LYS_LIST:
1056 return &((struct lysc_node_list*)node)->notifs;
1057 default:
1058 return NULL;
1059 }
1060}
1061
1062API const struct lysc_notif *
1063lysc_node_notifs(const struct lysc_node *node)
1064{
1065 struct lysc_notif **notifs;
1066 notifs = lysc_node_notifs_p((struct lysc_node*)node);
1067 if (notifs) {
1068 return *notifs;
1069 } else {
1070 return NULL;
1071 }
1072}
1073
Radek Krejcibbe09a92018-11-08 09:36:54 +01001074struct lysc_node **
Radek Krejci056d0a82018-12-06 16:57:25 +01001075lysc_node_children_p(const struct lysc_node *node)
Radek Krejcibbe09a92018-11-08 09:36:54 +01001076{
1077 assert(node);
1078 switch (node->nodetype) {
1079 case LYS_CONTAINER:
1080 return &((struct lysc_node_container*)node)->child;
1081 case LYS_CHOICE:
Radek Krejcia3045382018-11-22 14:30:31 +01001082 if (((struct lysc_node_choice*)node)->cases) {
1083 return &((struct lysc_node_choice*)node)->cases[0].child;
1084 } else {
1085 return NULL;
1086 }
Radek Krejcibbe09a92018-11-08 09:36:54 +01001087 case LYS_LIST:
1088 return &((struct lysc_node_list*)node)->child;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001089/* TODO
1090 case LYS_INOUT:
1091 return &((struct lysc_action_inout*)node)->child;
1092 case LYS_NOTIF:
1093 return &((struct lysc_notif*)node)->child;
1094*/
1095 default:
1096 return NULL;
1097 }
1098}
1099
Radek Krejci056d0a82018-12-06 16:57:25 +01001100API const struct lysc_node *
1101lysc_node_children(const struct lysc_node *node)
Radek Krejcia3045382018-11-22 14:30:31 +01001102{
Radek Krejci056d0a82018-12-06 16:57:25 +01001103 struct lysc_node **children;
1104 children = lysc_node_children_p((struct lysc_node*)node);
1105 if (children) {
1106 return *children;
1107 } else {
Radek Krejcia3045382018-11-22 14:30:31 +01001108 return NULL;
1109 }
1110}
1111
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001112struct lys_module *
1113lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1114{
1115 unsigned int u;
1116
1117 for (u = 0; u < ctx->list.count; ++u) {
1118 if (((struct lys_module*)ctx->list.objs[u])->parsed == mod) {
1119 return ((struct lys_module*)ctx->list.objs[u]);
1120 }
1121 }
1122 return NULL;
1123}
1124