Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2 | * @file tree_schema_common.c |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 5 | * @brief Parsing and validation common functions for schema trees |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 6 | * |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015 - 2022 CESNET, z.s.p.o. |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | |
| 16 | #define _GNU_SOURCE |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 17 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 18 | #include <assert.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 19 | #include <ctype.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 20 | #include <stddef.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | #include <stdint.h> |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 22 | #include <stdlib.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 23 | #include <string.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 24 | #include <time.h> |
| 25 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 26 | #include "common.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 27 | #include "compat.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 28 | #include "context.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 29 | #include "dict.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 30 | #include "hash_table.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 31 | #include "in.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 32 | #include "in_internal.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 33 | #include "log.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 34 | #include "parser_schema.h" |
Michal Vasko | 962b6cd | 2020-12-08 10:07:49 +0100 | [diff] [blame] | 35 | #include "schema_compile.h" |
Michal Vasko | 79135ae | 2020-12-16 10:08:35 +0100 | [diff] [blame] | 36 | #include "schema_features.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 37 | #include "set.h" |
| 38 | #include "tree.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 39 | #include "tree_edit.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 40 | #include "tree_schema.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 41 | #include "tree_schema_internal.h" |
| 42 | |
Radek Krejci | 8574795 | 2019-06-07 16:43:43 +0200 | [diff] [blame] | 43 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 44 | lysp_check_prefix(struct lys_parser_ctx *ctx, struct lysp_import *imports, const char *module_prefix, const char **value) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 45 | { |
| 46 | struct lysp_import *i; |
| 47 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 48 | if (module_prefix && (&module_prefix != value) && !strcmp(module_prefix, *value)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 49 | LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 50 | return LY_EEXIST; |
| 51 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 52 | LY_ARRAY_FOR(imports, struct lysp_import, i) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 53 | if (i->prefix && (&i->prefix != value) && !strcmp(i->prefix, *value)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 54 | LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 55 | return LY_EEXIST; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | return LY_SUCCESS; |
| 59 | } |
| 60 | |
| 61 | LY_ERR |
Juraj Vijtiuk | 74dad9e | 2021-05-26 12:42:14 +0200 | [diff] [blame] | 62 | lysp_check_date(struct lys_parser_ctx *ctx, const char *date, size_t date_len, const char *stmt) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 63 | { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 64 | struct tm tm, tm_; |
| 65 | char *r; |
| 66 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 67 | LY_CHECK_ARG_RET(ctx ? PARSER_CTX(ctx) : NULL, date, LY_EINVAL); |
| 68 | LY_CHECK_ERR_RET(date_len != LY_REV_SIZE - 1, LOGARG(ctx ? PARSER_CTX(ctx) : NULL, date_len), LY_EINVAL); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 69 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 70 | /* check format: YYYY-MM-DD */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 71 | for (uint8_t i = 0; i < date_len; i++) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 72 | if ((i == 4) || (i == 7)) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 73 | if (date[i] != '-') { |
| 74 | goto error; |
| 75 | } |
| 76 | } else if (!isdigit(date[i])) { |
| 77 | goto error; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /* check content, e.g. 2018-02-31 */ |
| 82 | memset(&tm, 0, sizeof tm); |
| 83 | r = strptime(date, "%Y-%m-%d", &tm); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 84 | if (!r || (r != &date[LY_REV_SIZE - 1])) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 85 | goto error; |
| 86 | } |
| 87 | memcpy(&tm_, &tm, sizeof tm); |
| 88 | mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */ |
| 89 | if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */ |
| 90 | /* checking days is enough, since other errors |
| 91 | * have been checked by strptime() */ |
| 92 | goto error; |
| 93 | } |
| 94 | |
| 95 | return LY_SUCCESS; |
| 96 | |
| 97 | error: |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 98 | if (stmt) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 99 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 100 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 101 | return LY_EINVAL; |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | lysp_sort_revisions(struct lysp_revision *revs) |
| 106 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 107 | LY_ARRAY_COUNT_TYPE i, r; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 108 | struct lysp_revision rev; |
| 109 | |
Radek Krejci | c7d13e3 | 2020-12-09 12:32:24 +0100 | [diff] [blame] | 110 | for (i = 1, r = 0; i < LY_ARRAY_COUNT(revs); i++) { |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 111 | if (strcmp(revs[i].date, revs[r].date) > 0) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 112 | r = i; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if (r) { |
| 117 | /* the newest revision is not on position 0, switch them */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 118 | memcpy(&rev, &revs[0], sizeof rev); |
| 119 | memcpy(&revs[0], &revs[r], sizeof rev); |
| 120 | memcpy(&revs[r], &rev, sizeof rev); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 121 | } |
| 122 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 123 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 124 | static const struct lysp_tpdf * |
| 125 | lysp_type_match(const char *name, struct lysp_node *node) |
| 126 | { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 127 | const struct lysp_tpdf *typedefs; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 128 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 129 | |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 130 | typedefs = lysp_node_typedefs(node); |
| 131 | LY_ARRAY_FOR(typedefs, u) { |
| 132 | if (!strcmp(name, typedefs[u].name)) { |
| 133 | /* match */ |
| 134 | return &typedefs[u]; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | return NULL; |
| 139 | } |
| 140 | |
aPiecek | 63e080d | 2021-06-29 13:53:28 +0200 | [diff] [blame] | 141 | static const struct lysp_node_grp * |
| 142 | lysp_grouping_match(const char *name, struct lysp_node *node) |
| 143 | { |
| 144 | const struct lysp_node_grp *groupings, *grp_iter; |
| 145 | |
| 146 | groupings = lysp_node_groupings(node); |
| 147 | LY_LIST_FOR(groupings, grp_iter) { |
| 148 | if (!strcmp(name, grp_iter->name)) { |
| 149 | /* match */ |
| 150 | return grp_iter; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return NULL; |
| 155 | } |
| 156 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 157 | static LY_DATA_TYPE |
| 158 | lysp_type_str2builtin(const char *name, size_t len) |
| 159 | { |
| 160 | if (len >= 4) { /* otherwise it does not match any built-in type */ |
| 161 | if (name[0] == 'b') { |
| 162 | if (name[1] == 'i') { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 163 | if ((len == 6) && !strncmp(&name[2], "nary", 4)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 164 | return LY_TYPE_BINARY; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 165 | } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 166 | return LY_TYPE_BITS; |
| 167 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 168 | } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 169 | return LY_TYPE_BOOL; |
| 170 | } |
| 171 | } else if (name[0] == 'd') { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 172 | if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 173 | return LY_TYPE_DEC64; |
| 174 | } |
| 175 | } else if (name[0] == 'e') { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 176 | if ((len == 5) && !strncmp(&name[1], "mpty", 4)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 177 | return LY_TYPE_EMPTY; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 178 | } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 179 | return LY_TYPE_ENUM; |
| 180 | } |
| 181 | } else if (name[0] == 'i') { |
| 182 | if (name[1] == 'n') { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 183 | if ((len == 4) && !strncmp(&name[2], "t8", 2)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 184 | return LY_TYPE_INT8; |
| 185 | } else if (len == 5) { |
| 186 | if (!strncmp(&name[2], "t16", 3)) { |
| 187 | return LY_TYPE_INT16; |
| 188 | } else if (!strncmp(&name[2], "t32", 3)) { |
| 189 | return LY_TYPE_INT32; |
| 190 | } else if (!strncmp(&name[2], "t64", 3)) { |
| 191 | return LY_TYPE_INT64; |
| 192 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 193 | } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 194 | return LY_TYPE_INST; |
| 195 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 196 | } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 197 | return LY_TYPE_IDENT; |
| 198 | } |
| 199 | } else if (name[0] == 'l') { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 200 | if ((len == 7) && !strncmp(&name[1], "eafref", 6)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 201 | return LY_TYPE_LEAFREF; |
| 202 | } |
| 203 | } else if (name[0] == 's') { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 204 | if ((len == 6) && !strncmp(&name[1], "tring", 5)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 205 | return LY_TYPE_STRING; |
| 206 | } |
| 207 | } else if (name[0] == 'u') { |
| 208 | if (name[1] == 'n') { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 209 | if ((len == 5) && !strncmp(&name[2], "ion", 3)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 210 | return LY_TYPE_UNION; |
| 211 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 212 | } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) { |
| 213 | if ((len == 5) && (name[4] == '8')) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 214 | return LY_TYPE_UINT8; |
| 215 | } else if (len == 6) { |
| 216 | if (!strncmp(&name[4], "16", 2)) { |
| 217 | return LY_TYPE_UINT16; |
| 218 | } else if (!strncmp(&name[4], "32", 2)) { |
| 219 | return LY_TYPE_UINT32; |
| 220 | } else if (!strncmp(&name[4], "64", 2)) { |
| 221 | return LY_TYPE_UINT64; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | return LY_TYPE_UNKNOWN; |
| 229 | } |
| 230 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 231 | LY_ERR |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 232 | lysp_type_find(const char *id, struct lysp_node *start_node, const struct lysp_module *start_module, |
| 233 | LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 234 | { |
| 235 | const char *str, *name; |
| 236 | struct lysp_tpdf *typedefs; |
Michal Vasko | b2d55bf | 2020-11-02 15:42:43 +0100 | [diff] [blame] | 237 | const struct lys_module *mod; |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 238 | const struct lysp_module *local_module; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 239 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 240 | |
| 241 | assert(id); |
| 242 | assert(start_module); |
| 243 | assert(tpdf); |
| 244 | assert(node); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 245 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 246 | *node = NULL; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 247 | str = strchr(id, ':'); |
| 248 | if (str) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 249 | mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_VALUE_SCHEMA, (void *)start_module); |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 250 | local_module = mod ? mod->parsed : NULL; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 251 | name = str + 1; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 252 | *type = LY_TYPE_UNKNOWN; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 253 | } else { |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 254 | local_module = start_module; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 255 | name = id; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 256 | |
| 257 | /* check for built-in types */ |
| 258 | *type = lysp_type_str2builtin(name, strlen(name)); |
| 259 | if (*type) { |
| 260 | *tpdf = NULL; |
| 261 | return LY_SUCCESS; |
| 262 | } |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 263 | } |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 264 | LY_CHECK_RET(!local_module, LY_ENOTFOUND); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 265 | |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 266 | if (start_node && (local_module == start_module)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 267 | /* search typedefs in parent's nodes */ |
| 268 | *node = start_node; |
| 269 | while (*node) { |
| 270 | *tpdf = lysp_type_match(name, *node); |
| 271 | if (*tpdf) { |
| 272 | /* match */ |
| 273 | return LY_SUCCESS; |
| 274 | } |
| 275 | *node = (*node)->parent; |
| 276 | } |
| 277 | } |
| 278 | |
Michal Vasko | 915e544 | 2021-06-08 14:59:21 +0200 | [diff] [blame] | 279 | /* go to main module if in submodule */ |
| 280 | local_module = local_module->mod->parsed; |
| 281 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 282 | /* search in top-level typedefs */ |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 283 | if (local_module->typedefs) { |
| 284 | LY_ARRAY_FOR(local_module->typedefs, u) { |
| 285 | if (!strcmp(name, local_module->typedefs[u].name)) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 286 | /* match */ |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 287 | *tpdf = &local_module->typedefs[u]; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 288 | return LY_SUCCESS; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
Michal Vasko | 915e544 | 2021-06-08 14:59:21 +0200 | [diff] [blame] | 293 | /* search in all submodules' typedefs */ |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 294 | LY_ARRAY_FOR(local_module->includes, u) { |
| 295 | typedefs = local_module->includes[u].submodule->typedefs; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 296 | LY_ARRAY_FOR(typedefs, v) { |
| 297 | if (!strcmp(name, typedefs[v].name)) { |
| 298 | /* match */ |
| 299 | *tpdf = &typedefs[v]; |
| 300 | return LY_SUCCESS; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | return LY_ENOTFOUND; |
| 306 | } |
| 307 | |
David Sedlák | 6544c18 | 2019-07-12 13:17:33 +0200 | [diff] [blame] | 308 | LY_ERR |
David Sedlák | 07869a5 | 2019-07-12 14:28:19 +0200 | [diff] [blame] | 309 | lysp_check_enum_name(struct lys_parser_ctx *ctx, const char *name, size_t name_len) |
David Sedlák | 6544c18 | 2019-07-12 13:17:33 +0200 | [diff] [blame] | 310 | { |
| 311 | if (!name_len) { |
| 312 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length."); |
| 313 | return LY_EVALID; |
| 314 | } else if (isspace(name[0]) || isspace(name[name_len - 1])) { |
| 315 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 316 | (int)name_len, name); |
David Sedlák | 6544c18 | 2019-07-12 13:17:33 +0200 | [diff] [blame] | 317 | return LY_EVALID; |
| 318 | } else { |
| 319 | for (size_t u = 0; u < name_len; ++u) { |
| 320 | if (iscntrl(name[u])) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 321 | LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 322 | (int)name_len, name, u + 1); |
David Sedlák | 6544c18 | 2019-07-12 13:17:33 +0200 | [diff] [blame] | 323 | break; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | return LY_SUCCESS; |
| 329 | } |
| 330 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 331 | /** |
aPiecek | dc12b9f | 2021-06-25 10:55:47 +0200 | [diff] [blame] | 332 | * @brief Insert @p name to hash table and if @p name has already |
| 333 | * been added, then log an error. |
| 334 | * |
| 335 | * This function is used to detect duplicate names. |
| 336 | * |
| 337 | * @param[in,out] ctx Context to log the error. |
| 338 | * @param[in,out] ht Hash table with top-level names. |
| 339 | * @param[in] name Inserted top-level identifier. |
| 340 | * @param[in] statement The name of the statement type from which |
| 341 | * @p name originated (eg typedef, feature, ...). |
| 342 | * @param[in] err_detail Optional error specification. |
| 343 | * @return LY_ERR, but LY_EEXIST is mapped to LY_EVALID. |
| 344 | */ |
| 345 | static LY_ERR |
| 346 | lysp_check_dup_ht_insert(struct lys_parser_ctx *ctx, struct hash_table *ht, |
| 347 | const char *name, const char *statement, const char *err_detail) |
| 348 | { |
| 349 | LY_ERR ret; |
| 350 | uint32_t hash; |
| 351 | |
| 352 | hash = dict_hash(name, strlen(name)); |
| 353 | ret = lyht_insert(ht, &name, hash, NULL); |
| 354 | if (ret == LY_EEXIST) { |
| 355 | if (err_detail) { |
| 356 | LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT2, name, statement, err_detail); |
| 357 | } else { |
| 358 | LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, name, statement); |
| 359 | } |
| 360 | ret = LY_EVALID; |
| 361 | } |
| 362 | |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | /** |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 367 | * @brief Check name of a new type to avoid name collisions. |
| 368 | * |
| 369 | * @param[in] ctx Parser context, module where the type is being defined is taken from here. |
| 370 | * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef. |
| 371 | * @param[in] tpdf Typedef definition to check. |
| 372 | * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's |
aPiecek | c7594b7 | 2021-06-29 09:00:03 +0200 | [diff] [blame] | 373 | * typedefs are checked, caller is supposed to free the table. |
| 374 | * @return LY_EVALID in case of collision, LY_SUCCESS otherwise. |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 375 | */ |
| 376 | static LY_ERR |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 377 | lysp_check_dup_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf, |
aPiecek | e1fbd95 | 2021-06-29 08:12:55 +0200 | [diff] [blame] | 378 | struct hash_table *tpdfs_global) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 379 | { |
| 380 | struct lysp_node *parent; |
| 381 | uint32_t hash; |
| 382 | size_t name_len; |
| 383 | const char *name; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 384 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 385 | const struct lysp_tpdf *typedefs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 386 | |
| 387 | assert(ctx); |
| 388 | assert(tpdf); |
| 389 | |
| 390 | name = tpdf->name; |
| 391 | name_len = strlen(name); |
| 392 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 393 | if (lysp_type_str2builtin(name, name_len)) { |
aPiecek | c7594b7 | 2021-06-29 09:00:03 +0200 | [diff] [blame] | 394 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, |
| 395 | "Duplicate identifier \"%s\" of typedef statement - name collision with a built-in type.", name); |
| 396 | return LY_EVALID; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* check locally scoped typedefs (avoid name shadowing) */ |
| 400 | if (node) { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 401 | typedefs = lysp_node_typedefs(node); |
| 402 | LY_ARRAY_FOR(typedefs, u) { |
| 403 | if (&typedefs[u] == tpdf) { |
| 404 | break; |
| 405 | } |
| 406 | if (!strcmp(name, typedefs[u].name)) { |
aPiecek | c7594b7 | 2021-06-29 09:00:03 +0200 | [diff] [blame] | 407 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, |
| 408 | "Duplicate identifier \"%s\" of typedef statement - name collision with sibling type.", name); |
| 409 | return LY_EVALID; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | /* search typedefs in parent's nodes */ |
Radek Krejci | 87e78ca | 2019-05-02 09:51:29 +0200 | [diff] [blame] | 413 | for (parent = node->parent; parent; parent = parent->parent) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 414 | if (lysp_type_match(name, parent)) { |
aPiecek | c7594b7 | 2021-06-29 09:00:03 +0200 | [diff] [blame] | 415 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, |
| 416 | "Duplicate identifier \"%s\" of typedef statement - name collision with another scoped type.", name); |
| 417 | return LY_EVALID; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /* check collision with the top-level typedefs */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 423 | if (node) { |
aPiecek | c7594b7 | 2021-06-29 09:00:03 +0200 | [diff] [blame] | 424 | hash = dict_hash(name, name_len); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 425 | if (!lyht_find(tpdfs_global, &name, hash, NULL)) { |
aPiecek | c7594b7 | 2021-06-29 09:00:03 +0200 | [diff] [blame] | 426 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, |
| 427 | "Duplicate identifier \"%s\" of typedef statement - scoped type collide with a top-level type.", name); |
| 428 | return LY_EVALID; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 429 | } |
| 430 | } else { |
aPiecek | c7594b7 | 2021-06-29 09:00:03 +0200 | [diff] [blame] | 431 | LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, tpdfs_global, name, "typedef", |
| 432 | "name collision with another top-level type")); |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 433 | /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the |
| 434 | * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision |
| 435 | * is detected in the first branch few lines above */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | return LY_SUCCESS; |
| 439 | } |
| 440 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 441 | /** |
| 442 | * @brief Compare identifiers. |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 443 | * Implementation of ::lyht_value_equal_cb. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 444 | */ |
| 445 | static ly_bool |
| 446 | lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data)) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 447 | { |
Michal Vasko | 11ac39a | 2021-07-23 12:46:56 +0200 | [diff] [blame] | 448 | char *id1, *id2; |
| 449 | |
| 450 | id1 = *(char **)val1; |
| 451 | id2 = *(char **)val2; |
| 452 | |
| 453 | return strcmp(id1, id2) == 0 ? 1 : 0; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | LY_ERR |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 457 | lysp_check_dup_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 458 | { |
| 459 | struct hash_table *ids_global; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 460 | const struct lysp_tpdf *typedefs; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 461 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 462 | uint32_t i; |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 463 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 464 | |
| 465 | /* check name collisions - typedefs and groupings */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 466 | ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1); |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 467 | LY_ARRAY_FOR(mod->typedefs, v) { |
aPiecek | e1fbd95 | 2021-06-29 08:12:55 +0200 | [diff] [blame] | 468 | ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 469 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 470 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 471 | LY_ARRAY_FOR(mod->includes, v) { |
| 472 | LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) { |
aPiecek | e1fbd95 | 2021-06-29 08:12:55 +0200 | [diff] [blame] | 473 | ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 474 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame] | 475 | } |
| 476 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 477 | for (i = 0; i < ctx->tpdfs_nodes.count; ++i) { |
| 478 | typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]); |
| 479 | LY_ARRAY_FOR(typedefs, u) { |
aPiecek | e1fbd95 | 2021-06-29 08:12:55 +0200 | [diff] [blame] | 480 | ret = lysp_check_dup_typedef(ctx, (struct lysp_node *)ctx->tpdfs_nodes.objs[i], &typedefs[u], ids_global); |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 481 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 482 | } |
| 483 | } |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 484 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 485 | cleanup: |
| 486 | lyht_free(ids_global); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 487 | return ret; |
| 488 | } |
| 489 | |
aPiecek | 63e080d | 2021-06-29 13:53:28 +0200 | [diff] [blame] | 490 | /** |
| 491 | * @brief Check name of a new grouping to avoid name collisions. |
| 492 | * |
| 493 | * @param[in] ctx Parser context, module where the grouping is being defined is taken from here. |
| 494 | * @param[in] node Schema node where the grouping is being defined, NULL in case of a top-level grouping. |
| 495 | * @param[in] grp Grouping definition to check. |
| 496 | * @param[in,out] grps_global Initialized hash table to store temporary data between calls. When the module's |
| 497 | * groupings are checked, caller is supposed to free the table. |
| 498 | * @return LY_EVALID in case of collision, LY_SUCCESS otherwise. |
| 499 | */ |
| 500 | static LY_ERR |
| 501 | lysp_check_dup_grouping(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_node_grp *grp, |
| 502 | struct hash_table *grps_global) |
| 503 | { |
| 504 | struct lysp_node *parent; |
| 505 | uint32_t hash; |
| 506 | size_t name_len; |
| 507 | const char *name; |
| 508 | const struct lysp_node_grp *groupings, *grp_iter; |
| 509 | |
| 510 | assert(ctx); |
| 511 | assert(grp); |
| 512 | |
| 513 | name = grp->name; |
| 514 | name_len = strlen(name); |
| 515 | |
| 516 | /* check locally scoped groupings (avoid name shadowing) */ |
| 517 | if (node) { |
| 518 | groupings = lysp_node_groupings(node); |
| 519 | LY_LIST_FOR(groupings, grp_iter) { |
| 520 | if (grp_iter == grp) { |
| 521 | break; |
| 522 | } |
| 523 | if (!strcmp(name, grp_iter->name)) { |
| 524 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, |
| 525 | "Duplicate identifier \"%s\" of grouping statement - name collision with sibling grouping.", name); |
| 526 | return LY_EVALID; |
| 527 | } |
| 528 | } |
| 529 | /* search grouping in parent's nodes */ |
| 530 | for (parent = node->parent; parent; parent = parent->parent) { |
| 531 | if (lysp_grouping_match(name, parent)) { |
| 532 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, |
| 533 | "Duplicate identifier \"%s\" of grouping statement - name collision with another scoped grouping.", name); |
| 534 | return LY_EVALID; |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | /* check collision with the top-level groupings */ |
| 540 | if (node) { |
| 541 | hash = dict_hash(name, name_len); |
| 542 | if (!lyht_find(grps_global, &name, hash, NULL)) { |
| 543 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, |
| 544 | "Duplicate identifier \"%s\" of grouping statement - scoped grouping collide with a top-level grouping.", name); |
| 545 | return LY_EVALID; |
| 546 | } |
| 547 | } else { |
| 548 | LY_CHECK_RET(lysp_check_dup_ht_insert(ctx, grps_global, name, "grouping", |
| 549 | "name collision with another top-level grouping")); |
| 550 | } |
| 551 | |
| 552 | return LY_SUCCESS; |
| 553 | } |
| 554 | |
| 555 | LY_ERR |
| 556 | lysp_check_dup_groupings(struct lys_parser_ctx *ctx, struct lysp_module *mod) |
| 557 | { |
| 558 | struct hash_table *ids_global; |
| 559 | const struct lysp_node_grp *groupings, *grp_iter; |
| 560 | LY_ARRAY_COUNT_TYPE u; |
| 561 | uint32_t i; |
| 562 | LY_ERR ret = LY_SUCCESS; |
| 563 | |
| 564 | ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1); |
| 565 | LY_LIST_FOR(mod->groupings, grp_iter) { |
| 566 | ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global); |
| 567 | LY_CHECK_GOTO(ret, cleanup); |
| 568 | } |
| 569 | LY_ARRAY_FOR(mod->includes, u) { |
| 570 | LY_LIST_FOR(mod->includes[u].submodule->groupings, grp_iter) { |
| 571 | ret = lysp_check_dup_grouping(ctx, NULL, grp_iter, ids_global); |
| 572 | LY_CHECK_GOTO(ret, cleanup); |
| 573 | } |
| 574 | } |
| 575 | for (i = 0; i < ctx->grps_nodes.count; ++i) { |
| 576 | groupings = lysp_node_groupings((struct lysp_node *)ctx->grps_nodes.objs[i]); |
| 577 | LY_LIST_FOR(groupings, grp_iter) { |
| 578 | ret = lysp_check_dup_grouping(ctx, (struct lysp_node *)ctx->grps_nodes.objs[i], grp_iter, ids_global); |
| 579 | LY_CHECK_GOTO(ret, cleanup); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | cleanup: |
| 584 | lyht_free(ids_global); |
| 585 | return ret; |
| 586 | } |
| 587 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 588 | static ly_bool |
| 589 | ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data)) |
| 590 | { |
| 591 | void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p); |
| 592 | |
| 593 | return ptr1 == ptr2 ? 1 : 0; |
| 594 | } |
| 595 | |
| 596 | LY_ERR |
| 597 | lysp_check_dup_features(struct lys_parser_ctx *ctx, struct lysp_module *mod) |
| 598 | { |
| 599 | LY_ARRAY_COUNT_TYPE u; |
| 600 | struct hash_table *ht; |
| 601 | struct lysp_feature *f; |
aPiecek | dc12b9f | 2021-06-25 10:55:47 +0200 | [diff] [blame] | 602 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 603 | |
aPiecek | f620307 | 2021-06-25 10:58:26 +0200 | [diff] [blame] | 604 | ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 605 | LY_CHECK_RET(!ht, LY_EMEM); |
| 606 | |
| 607 | /* add all module features into a hash table */ |
| 608 | LY_ARRAY_FOR(mod->features, struct lysp_feature, f) { |
aPiecek | ea147b3 | 2021-06-29 07:52:47 +0200 | [diff] [blame] | 609 | ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature", |
| 610 | "name collision with another top-level feature"); |
aPiecek | dc12b9f | 2021-06-25 10:55:47 +0200 | [diff] [blame] | 611 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /* add all submodule features into a hash table */ |
| 615 | LY_ARRAY_FOR(mod->includes, u) { |
| 616 | LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) { |
aPiecek | ea147b3 | 2021-06-29 07:52:47 +0200 | [diff] [blame] | 617 | ret = lysp_check_dup_ht_insert(ctx, ht, f->name, "feature", |
| 618 | "name collision with another top-level feature"); |
aPiecek | dc12b9f | 2021-06-25 10:55:47 +0200 | [diff] [blame] | 619 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
| 623 | cleanup: |
| 624 | lyht_free(ht); |
| 625 | return ret; |
| 626 | } |
| 627 | |
| 628 | LY_ERR |
| 629 | lysp_check_dup_identities(struct lys_parser_ctx *ctx, struct lysp_module *mod) |
| 630 | { |
| 631 | LY_ARRAY_COUNT_TYPE u; |
| 632 | struct hash_table *ht; |
| 633 | struct lysp_ident *i; |
aPiecek | dc12b9f | 2021-06-25 10:55:47 +0200 | [diff] [blame] | 634 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 635 | |
aPiecek | f620307 | 2021-06-25 10:58:26 +0200 | [diff] [blame] | 636 | ht = lyht_new(LYHT_MIN_SIZE, sizeof(void *), ly_ptrequal_cb, NULL, 1); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 637 | LY_CHECK_RET(!ht, LY_EMEM); |
| 638 | |
| 639 | /* add all module identities into a hash table */ |
| 640 | LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) { |
aPiecek | ea147b3 | 2021-06-29 07:52:47 +0200 | [diff] [blame] | 641 | ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity", |
| 642 | "name collision with another top-level identity"); |
aPiecek | dc12b9f | 2021-06-25 10:55:47 +0200 | [diff] [blame] | 643 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | /* add all submodule identities into a hash table */ |
| 647 | LY_ARRAY_FOR(mod->includes, u) { |
| 648 | LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) { |
aPiecek | ea147b3 | 2021-06-29 07:52:47 +0200 | [diff] [blame] | 649 | ret = lysp_check_dup_ht_insert(ctx, ht, i->name, "identity", |
| 650 | "name collision with another top-level identity"); |
aPiecek | dc12b9f | 2021-06-25 10:55:47 +0200 | [diff] [blame] | 651 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | |
| 655 | cleanup: |
| 656 | lyht_free(ht); |
| 657 | return ret; |
| 658 | } |
| 659 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 660 | struct lysp_load_module_check_data { |
| 661 | const char *name; |
| 662 | const char *revision; |
| 663 | const char *path; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 664 | const char *submoduleof; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 665 | }; |
| 666 | |
| 667 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 668 | lysp_load_module_check(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data) |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 669 | { |
| 670 | struct lysp_load_module_check_data *info = data; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 671 | const char *filename, *dot, *rev, *name; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 672 | uint8_t latest_revision; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 673 | size_t len; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 674 | struct lysp_revision *revs; |
| 675 | |
| 676 | name = mod ? mod->mod->name : submod->name; |
| 677 | revs = mod ? mod->revs : submod->revs; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 678 | latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 679 | |
| 680 | if (info->name) { |
| 681 | /* check name of the parsed model */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 682 | if (strcmp(info->name, name)) { |
| 683 | LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 684 | return LY_EINVAL; |
| 685 | } |
| 686 | } |
| 687 | if (info->revision) { |
| 688 | /* check revision of the parsed model */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 689 | if (!revs || strcmp(info->revision, revs[0].date)) { |
| 690 | LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 691 | revs ? revs[0].date : "none", info->revision); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 692 | return LY_EINVAL; |
| 693 | } |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 694 | } else if (!latest_revision) { |
| 695 | /* do not log, we just need to drop the schema and use the latest revision from the context */ |
| 696 | return LY_EEXIST; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 697 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 698 | if (submod) { |
| 699 | assert(info->submoduleof); |
| 700 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 701 | /* check that the submodule belongs-to our module */ |
Michal Vasko | c3781c3 | 2020-10-06 14:04:08 +0200 | [diff] [blame] | 702 | if (strcmp(info->submoduleof, submod->mod->name)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 703 | LOGVAL(ctx, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 704 | submod->name, info->submoduleof, submod->mod->name); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 705 | return LY_EVALID; |
| 706 | } |
| 707 | /* check circular dependency */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 708 | if (submod->parsing) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 709 | LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 710 | return LY_EVALID; |
| 711 | } |
| 712 | } |
| 713 | if (info->path) { |
| 714 | /* check that name and revision match filename */ |
| 715 | filename = strrchr(info->path, '/'); |
| 716 | if (!filename) { |
| 717 | filename = info->path; |
| 718 | } else { |
| 719 | filename++; |
| 720 | } |
| 721 | /* name */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 722 | len = strlen(name); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 723 | rev = strchr(filename, '@'); |
| 724 | dot = strrchr(info->path, '.'); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 725 | if (strncmp(filename, name, len) || |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 726 | ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 727 | LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 728 | } |
| 729 | /* revision */ |
| 730 | if (rev) { |
| 731 | len = dot - ++rev; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 732 | if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 733 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 734 | revs ? revs[0].date : "none"); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | } |
| 738 | return LY_SUCCESS; |
| 739 | } |
| 740 | |
Michal Vasko | e7a1daf | 2021-04-19 18:04:27 +0200 | [diff] [blame] | 741 | /** |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 742 | * @brief Parse a (sub)module from a local file and add into the context. |
Michal Vasko | e7a1daf | 2021-04-19 18:04:27 +0200 | [diff] [blame] | 743 | * |
| 744 | * This function does not check the presence of the (sub)module in context, it should be done before calling this function. |
| 745 | * |
Michal Vasko | e7a1daf | 2021-04-19 18:04:27 +0200 | [diff] [blame] | 746 | * @param[in] ctx libyang context where to work. |
| 747 | * @param[in] name Name of the (sub)module to load. |
| 748 | * @param[in] revision Optional revision of the (sub)module to load, if NULL the newest revision is being loaded. |
Michal Vasko | e7a1daf | 2021-04-19 18:04:27 +0200 | [diff] [blame] | 749 | * @param[in] main_ctx Parser context of the main module in case of loading submodule. |
| 750 | * @param[in] main_name Main module name in case of loading submodule. |
| 751 | * @param[in] required Module is required so error (even if the input file not found) are important. If 0, there is some |
| 752 | * backup and it is actually ok if the input data are not found. However, parser reports errors even in this case. |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 753 | * @param[in,out] new_mods Set of all the new mods added to the context. Includes this module and all of its imports. |
Michal Vasko | e7a1daf | 2021-04-19 18:04:27 +0200 | [diff] [blame] | 754 | * @param[out] result Parsed YANG schema tree of the requested module (struct lys_module*) or submodule (struct lysp_submodule*). |
| 755 | * If it is a module, it is already in the context! |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 756 | * @return LY_SUCCESS on success. |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 757 | * @return LY_ERR on error. |
Michal Vasko | e7a1daf | 2021-04-19 18:04:27 +0200 | [diff] [blame] | 758 | */ |
| 759 | static LY_ERR |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 760 | lys_parse_localfile(struct ly_ctx *ctx, const char *name, const char *revision, struct lys_parser_ctx *main_ctx, |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 761 | const char *main_name, ly_bool required, struct ly_set *new_mods, void **result) |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 762 | { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 763 | struct ly_in *in; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 764 | char *filepath = NULL; |
| 765 | LYS_INFORMAT format; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 766 | void *mod = NULL; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 767 | LY_ERR ret = LY_SUCCESS; |
| 768 | struct lysp_load_module_check_data check_data = {0}; |
| 769 | |
Michal Vasko | 87f1cf0 | 2021-06-08 14:02:47 +0200 | [diff] [blame] | 770 | LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, |
| 771 | revision, &filepath, &format)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 772 | if (!filepath) { |
| 773 | if (required) { |
| 774 | LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 775 | revision ? revision : ""); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 776 | } |
| 777 | return LY_ENOTFOUND; |
| 778 | } |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 779 | |
| 780 | LOGVRB("Loading schema from \"%s\" file.", filepath); |
| 781 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 782 | /* get the (sub)module */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 783 | LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 784 | LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 785 | check_data.name = name; |
| 786 | check_data.revision = revision; |
| 787 | check_data.path = filepath; |
fredgan | cd485b8 | 2019-10-18 15:00:17 +0800 | [diff] [blame] | 788 | check_data.submoduleof = main_name; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 789 | if (main_ctx) { |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 790 | ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data, new_mods, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 791 | (struct lysp_submodule **)&mod); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 792 | } else { |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 793 | ret = lys_parse_in(ctx, in, format, lysp_load_module_check, &check_data, new_mods, (struct lys_module **)&mod); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 794 | |
| 795 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 796 | ly_in_free(in, 1); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 797 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 798 | |
| 799 | *result = mod; |
| 800 | |
| 801 | /* success */ |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 802 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 803 | cleanup: |
| 804 | free(filepath); |
| 805 | return ret; |
| 806 | } |
| 807 | |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 808 | /** |
| 809 | * @brief Load module from searchdirs or from callback. |
| 810 | * |
| 811 | * @param[in] ctx libyang context where to work. |
| 812 | * @param[in] name Name of module to load. |
| 813 | * @param[in] revision Revision of module to load. |
Michal Vasko | bdac23f | 2022-01-12 14:02:35 +0100 | [diff] [blame] | 814 | * @param[in] mod_latest Module with the latest revision found in context, otherwise set to NULL. |
| 815 | * @param[in,out] new_mods Set of all the new mods added to the context. Includes this module and all of its imports. |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 816 | * @param[out] mod Loaded module. |
| 817 | * @return LY_SUCCESS on success. |
| 818 | * @return LY_ERR on error. |
| 819 | */ |
| 820 | static LY_ERR |
| 821 | lys_parse_load_from_clb_or_file(struct ly_ctx *ctx, const char *name, const char *revision, |
| 822 | struct lys_module *mod_latest, struct ly_set *new_mods, struct lys_module **mod) |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 823 | { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 824 | const char *module_data = NULL; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 825 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 826 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 827 | void (*module_data_free)(void *module_data, void *user_data) = NULL; |
| 828 | struct lysp_load_module_check_data check_data = {0}; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 829 | struct ly_in *in; |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 830 | |
Michal Vasko | bdac23f | 2022-01-12 14:02:35 +0100 | [diff] [blame] | 831 | *mod = NULL; |
| 832 | |
| 833 | if (mod_latest && (!ctx->imp_clb || (mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB)) && |
| 834 | ((ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) || (mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) { |
| 835 | /* we are not able to find a newer revision */ |
| 836 | return LY_SUCCESS; |
| 837 | } |
| 838 | |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 839 | if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 840 | search_clb: |
Michal Vasko | bdac23f | 2022-01-12 14:02:35 +0100 | [diff] [blame] | 841 | /* check there is a callback and should be called */ |
| 842 | if (ctx->imp_clb && (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_IMPCLB))) { |
| 843 | if (!ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data, &format, &module_data, &module_data_free)) { |
| 844 | LY_CHECK_RET(ly_in_new_memory(module_data, &in)); |
| 845 | check_data.name = name; |
| 846 | check_data.revision = revision; |
| 847 | lys_parse_in(ctx, in, format, lysp_load_module_check, &check_data, new_mods, mod); |
| 848 | ly_in_free(in, 0); |
| 849 | if (module_data_free) { |
| 850 | module_data_free((void *)module_data, ctx->imp_clb_data); |
| 851 | } |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 852 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 853 | } |
Michal Vasko | bdac23f | 2022-01-12 14:02:35 +0100 | [diff] [blame] | 854 | if (*mod && !revision) { |
| 855 | /* we got the latest revision module from the callback */ |
| 856 | (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB; |
| 857 | } else if (!*mod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 858 | goto search_file; |
| 859 | } |
| 860 | } else { |
| 861 | search_file: |
Michal Vasko | bdac23f | 2022-01-12 14:02:35 +0100 | [diff] [blame] | 862 | /* check we can use searchdirs and that we should */ |
| 863 | if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS) && |
| 864 | (!mod_latest || !(mod_latest->latest_revision & LYS_MOD_LATEST_SEARCHDIRS))) { |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 865 | lys_parse_localfile(ctx, name, revision, NULL, NULL, mod_latest ? 0 : 1, new_mods, (void **)mod); |
| 866 | } |
Michal Vasko | bdac23f | 2022-01-12 14:02:35 +0100 | [diff] [blame] | 867 | if (*mod && !revision) { |
| 868 | /* we got the latest revision module in the searchdirs */ |
| 869 | (*mod)->latest_revision |= LYS_MOD_LATEST_IMPCLB; |
| 870 | } else if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 871 | goto search_clb; |
| 872 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 873 | } |
| 874 | |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 875 | return LY_SUCCESS; |
| 876 | } |
| 877 | |
| 878 | /** |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 879 | * @brief Get module without revision according to priorities. |
| 880 | * |
| 881 | * 1. Search for the module with LYS_MOD_IMPORTED_REV. |
| 882 | * 2. Search for the implemented module. |
| 883 | * 3. Search for the latest module in the context. |
| 884 | * |
| 885 | * @param[in] ctx libyang context where module is searched. |
| 886 | * @param[in] name Name of the searched module. |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 887 | * @return Found module from context or NULL. |
| 888 | */ |
| 889 | static struct lys_module * |
Michal Vasko | bdac23f | 2022-01-12 14:02:35 +0100 | [diff] [blame] | 890 | lys_get_module_without_revision(struct ly_ctx *ctx, const char *name) |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 891 | { |
| 892 | struct lys_module *mod, *mod_impl; |
| 893 | uint32_t index; |
| 894 | |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 895 | /* Try to find module with LYS_MOD_IMPORTED_REV flag. */ |
| 896 | index = 0; |
| 897 | while ((mod = ly_ctx_get_module_iter(ctx, &index))) { |
| 898 | if (!strcmp(mod->name, name) && (mod->latest_revision & LYS_MOD_IMPORTED_REV)) { |
| 899 | break; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | /* Try to find the implemented module. */ |
| 904 | mod_impl = ly_ctx_get_module_implemented(ctx, name); |
| 905 | if (mod && mod_impl) { |
Michal Vasko | bdac23f | 2022-01-12 14:02:35 +0100 | [diff] [blame] | 906 | LOGVRB("Implemented module \"%s@%s\" is not used for import, revision \"%s\" is imported instead.", |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 907 | mod_impl->name, mod_impl->revision, mod->revision); |
| 908 | return mod; |
| 909 | } else if (mod_impl) { |
| 910 | return mod_impl; |
| 911 | } |
| 912 | |
| 913 | /* Try to find the latest module in the current context. */ |
| 914 | mod = ly_ctx_get_module_latest(ctx, name); |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 915 | |
| 916 | return mod; |
| 917 | } |
| 918 | |
| 919 | /** |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 920 | * @brief Check if a circular dependency exists between modules. |
| 921 | * |
| 922 | * @param[in] ctx libyang context for log an error. |
| 923 | * @param[in,out] mod Examined module which is set to NULL |
| 924 | * if the circular dependency is detected. |
| 925 | * @return LY_SUCCESS if no circular dependecy is detected, |
| 926 | * otherwise LY_EVALID. |
| 927 | */ |
| 928 | static LY_ERR |
| 929 | lys_check_circular_dependency(struct ly_ctx *ctx, struct lys_module **mod) |
| 930 | { |
| 931 | if ((*mod) && (*mod)->parsed->parsing) { |
| 932 | LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", (*mod)->name); |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 933 | *mod = NULL; |
| 934 | return LY_EVALID; |
Michal Vasko | 0550b76 | 2020-11-24 18:04:08 +0100 | [diff] [blame] | 935 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 936 | |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 937 | return LY_SUCCESS; |
| 938 | } |
| 939 | |
| 940 | LY_ERR |
| 941 | lys_parse_load(struct ly_ctx *ctx, const char *name, const char *revision, struct ly_set *new_mods, |
| 942 | struct lys_module **mod) |
| 943 | { |
| 944 | struct lys_module *mod_latest = NULL; |
| 945 | |
| 946 | assert(mod && new_mods); |
| 947 | |
Michal Vasko | 0550b76 | 2020-11-24 18:04:08 +0100 | [diff] [blame] | 948 | /* |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 949 | * Try to get the module from the context. |
Michal Vasko | 0550b76 | 2020-11-24 18:04:08 +0100 | [diff] [blame] | 950 | */ |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 951 | if (revision) { |
| 952 | /* Get the specific revision. */ |
| 953 | *mod = ly_ctx_get_module(ctx, name, revision); |
| 954 | } else { |
Michal Vasko | bdac23f | 2022-01-12 14:02:35 +0100 | [diff] [blame] | 955 | /* Get the requested module in a suitable revision in the context. */ |
| 956 | *mod = lys_get_module_without_revision(ctx, name); |
| 957 | if (*mod && !(*mod)->implemented && !((*mod)->latest_revision & LYS_MOD_IMPORTED_REV)) { |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 958 | /* Let us now search with callback and searchpaths to check |
| 959 | * if there is newer revision outside the context. |
| 960 | */ |
| 961 | mod_latest = *mod; |
| 962 | *mod = NULL; |
| 963 | } |
| 964 | } |
| 965 | |
Michal Vasko | 0550b76 | 2020-11-24 18:04:08 +0100 | [diff] [blame] | 966 | if (!*mod) { |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 967 | /* No suitable module in the context, try to load it. */ |
Michal Vasko | bdac23f | 2022-01-12 14:02:35 +0100 | [diff] [blame] | 968 | LY_CHECK_RET(lys_parse_load_from_clb_or_file(ctx, name, revision, mod_latest, new_mods, mod)); |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 969 | if (!*mod && !mod_latest) { |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 970 | LOGVAL(ctx, LYVE_REFERENCE, "Loading \"%s\" module failed.", name); |
Michal Vasko | 0550b76 | 2020-11-24 18:04:08 +0100 | [diff] [blame] | 971 | return LY_EVALID; |
| 972 | } |
aPiecek | 4725aea | 2021-07-28 10:18:33 +0200 | [diff] [blame] | 973 | |
| 974 | /* Update the latest_revision flag - here we have selected the latest available schema, |
| 975 | * consider that even the callback provides correct latest revision. |
| 976 | */ |
| 977 | if (!*mod) { |
| 978 | LOGVRB("Newer revision than \"%s@%s\" not found, using this as the latest revision.", |
| 979 | mod_latest->name, mod_latest->revision); |
| 980 | assert(mod_latest->latest_revision & LYS_MOD_LATEST_REV); |
| 981 | mod_latest->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS; |
| 982 | *mod = mod_latest; |
| 983 | } else if (*mod && !revision && ((*mod)->latest_revision & LYS_MOD_LATEST_REV)) { |
| 984 | (*mod)->latest_revision |= LYS_MOD_LATEST_SEARCHDIRS; |
| 985 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 986 | } |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 987 | |
aPiecek | 9f8c7e7 | 2021-07-28 12:01:56 +0200 | [diff] [blame] | 988 | /* Checking the circular dependence of imported modules. */ |
| 989 | LY_CHECK_RET(lys_check_circular_dependency(ctx, mod)); |
| 990 | |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 991 | return LY_SUCCESS; |
| 992 | } |
| 993 | |
| 994 | LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 995 | lysp_check_stringchar(struct lys_parser_ctx *ctx, uint32_t c) |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 996 | { |
| 997 | if (!is_yangutf8char(c)) { |
| 998 | LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c); |
| 999 | return LY_EVALID; |
| 1000 | } |
| 1001 | return LY_SUCCESS; |
| 1002 | } |
| 1003 | |
| 1004 | LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1005 | lysp_check_identifierchar(struct lys_parser_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix) |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 1006 | { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1007 | if (first || (prefix && ((*prefix) == 1))) { |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 1008 | if (!is_yangidentstartchar(c)) { |
aPiecek | c89b224 | 2021-05-14 14:19:11 +0200 | [diff] [blame] | 1009 | if ((c < UCHAR_MAX) && isprint(c)) { |
Michal Vasko | 7c76904 | 2021-03-25 12:20:49 +0100 | [diff] [blame] | 1010 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c); |
| 1011 | } else { |
| 1012 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character 0x%04x.", c); |
| 1013 | } |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 1014 | return LY_EVALID; |
| 1015 | } |
| 1016 | if (prefix) { |
| 1017 | if (first) { |
| 1018 | (*prefix) = 0; |
| 1019 | } else { |
| 1020 | (*prefix) = 2; |
| 1021 | } |
| 1022 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1023 | } else if ((c == ':') && prefix && ((*prefix) == 0)) { |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 1024 | (*prefix) = 1; |
| 1025 | } else if (!is_yangidentchar(c)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1026 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c); |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 1027 | return LY_EVALID; |
| 1028 | } |
| 1029 | |
| 1030 | return LY_SUCCESS; |
| 1031 | } |
| 1032 | |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1033 | /** |
| 1034 | * @brief Try to find the parsed submodule in main module for the given include record. |
| 1035 | * |
| 1036 | * @param[in] pctx main parser context |
| 1037 | * @param[in] inc The include record with missing parsed submodule. According to include info try to find |
| 1038 | * the corresponding parsed submodule in main module's includes. |
| 1039 | * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record |
| 1040 | * @return LY_ENOT - the parsed module was not found. |
| 1041 | * @return LY_EVALID - YANG rule violation |
| 1042 | */ |
| 1043 | static LY_ERR |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1044 | lysp_main_pmod_get_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc) |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1045 | { |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1046 | LY_ARRAY_COUNT_TYPE i; |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1047 | struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1048 | |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1049 | LY_ARRAY_FOR(main_pmod->includes, i) { |
| 1050 | if (strcmp(main_pmod->includes[i].name, inc->name)) { |
| 1051 | continue; |
| 1052 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1053 | |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1054 | if (inc->rev[0] && strncmp(inc->rev, main_pmod->includes[i].rev, LY_REV_SIZE)) { |
| 1055 | LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE, |
| 1056 | "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.", |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1057 | ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev, |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1058 | main_pmod->includes[i].name, main_pmod->includes[i].rev, main_pmod->mod->name); |
| 1059 | return LY_EVALID; |
| 1060 | } |
| 1061 | |
| 1062 | inc->submodule = main_pmod->includes[i].submodule; |
| 1063 | return inc->submodule ? LY_SUCCESS : LY_ENOT; |
| 1064 | } |
| 1065 | |
| 1066 | if (main_pmod->version == LYS_VERSION_1_1) { |
| 1067 | LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE, |
| 1068 | "YANG 1.1 requires all submodules to be included from main module. " |
| 1069 | "But submodule \"%s\" includes submodule \"%s\" which is not included by main module \"%s\".", |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1070 | ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->name, main_pmod->mod->name); |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1071 | return LY_EVALID; |
| 1072 | } else { |
| 1073 | return LY_ENOT; |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | /** |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1078 | * @brief Try to find the parsed submodule in currenlty parsed modules for the given include record. |
| 1079 | * |
| 1080 | * @param[in] pctx main parser context |
| 1081 | * @param[in] inc The include record with missing parsed submodule. |
| 1082 | * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record |
| 1083 | * @return LY_ENOT - the parsed module was not found. |
| 1084 | * @return LY_EVALID - YANG rule violation |
| 1085 | */ |
| 1086 | static LY_ERR |
| 1087 | lysp_parsed_mods_get_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc) |
| 1088 | { |
| 1089 | uint32_t i; |
| 1090 | struct lysp_submodule *submod; |
| 1091 | |
| 1092 | for (i = 0; i < pctx->parsed_mods->count - 1; ++i) { |
| 1093 | submod = pctx->parsed_mods->objs[i]; |
| 1094 | if (!submod->is_submod) { |
| 1095 | continue; |
| 1096 | } |
| 1097 | |
| 1098 | if (strcmp(submod->name, inc->name)) { |
| 1099 | continue; |
| 1100 | } |
| 1101 | |
| 1102 | if (inc->rev[0] && submod->revs && strncmp(inc->rev, submod->revs[0].date, LY_REV_SIZE)) { |
| 1103 | LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE, |
| 1104 | "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.", |
| 1105 | ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name, inc->rev, |
| 1106 | submod->name, submod->revs[0].date, PARSER_CUR_PMOD(pctx)->mod->name); |
| 1107 | return LY_EVALID; |
| 1108 | } |
| 1109 | |
| 1110 | inc->submodule = submod; |
| 1111 | return LY_SUCCESS; |
| 1112 | } |
| 1113 | |
| 1114 | return LY_ENOT; |
| 1115 | } |
| 1116 | |
| 1117 | /** |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1118 | * @brief Make the copy of the given include record into the main module. |
| 1119 | * |
| 1120 | * YANG 1.0 does not require the main module to include all the submodules. Therefore, parsing submodules can cause |
| 1121 | * reallocating and extending the includes array in the main module by the submodules included only in submodules. |
| 1122 | * |
| 1123 | * @param[in] pctx main parser context |
| 1124 | * @param[in] inc Include record to copy into main module taken from @p pctx. |
| 1125 | * @return LY_ERR value. |
| 1126 | */ |
| 1127 | static LY_ERR |
| 1128 | lysp_inject_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc) |
| 1129 | { |
| 1130 | LY_ARRAY_COUNT_TYPE i; |
| 1131 | struct lysp_include *inc_new, *inc_tofill = NULL; |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1132 | struct lysp_module *main_pmod = PARSER_CUR_PMOD(pctx)->mod->parsed; |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1133 | |
| 1134 | /* first, try to find the corresponding record with missing parsed submodule */ |
| 1135 | LY_ARRAY_FOR(main_pmod->includes, i) { |
| 1136 | if (strcmp(main_pmod->includes[i].name, inc->name)) { |
| 1137 | continue; |
| 1138 | } |
| 1139 | inc_tofill = &main_pmod->includes[i]; |
| 1140 | break; |
| 1141 | } |
| 1142 | |
| 1143 | if (inc_tofill) { |
| 1144 | inc_tofill->submodule = inc->submodule; |
| 1145 | } else { |
| 1146 | LY_ARRAY_NEW_RET(PARSER_CTX(pctx), main_pmod->includes, inc_new, LY_EMEM); |
| 1147 | |
| 1148 | inc_new->submodule = inc->submodule; |
| 1149 | DUP_STRING_RET(PARSER_CTX(pctx), inc->name, inc_new->name); |
| 1150 | DUP_STRING_RET(PARSER_CTX(pctx), inc->dsc, inc_new->dsc); |
| 1151 | DUP_STRING_RET(PARSER_CTX(pctx), inc->ref, inc_new->ref); |
| 1152 | /* TODO duplicate extensions */ |
| 1153 | memcpy(inc_new->rev, inc->rev, LY_REV_SIZE); |
| 1154 | inc_new->injected = 1; |
| 1155 | } |
| 1156 | return LY_SUCCESS; |
| 1157 | } |
| 1158 | |
| 1159 | LY_ERR |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 1160 | lysp_load_submodules(struct lys_parser_ctx *pctx, struct lysp_module *pmod, struct ly_set *new_mods) |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1161 | { |
| 1162 | LY_ARRAY_COUNT_TYPE u; |
| 1163 | struct ly_ctx *ctx = PARSER_CTX(pctx); |
| 1164 | |
| 1165 | LY_ARRAY_FOR(pmod->includes, u) { |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1166 | LY_ERR ret = LY_SUCCESS, r; |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1167 | struct lysp_submodule *submod = NULL; |
| 1168 | struct lysp_include *inc = &pmod->includes[u]; |
| 1169 | |
| 1170 | if (inc->submodule) { |
| 1171 | continue; |
| 1172 | } |
| 1173 | |
| 1174 | if (pmod->is_submod) { |
| 1175 | /* try to find the submodule in the main module or its submodules */ |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1176 | ret = lysp_main_pmod_get_submodule(pctx, inc); |
| 1177 | LY_CHECK_RET(ret != LY_ENOT, ret); |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1178 | } |
| 1179 | |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1180 | /* try to use currently parsed submodule */ |
| 1181 | r = lysp_parsed_mods_get_submodule(pctx, inc); |
| 1182 | LY_CHECK_RET(r != LY_ENOT, r); |
| 1183 | |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1184 | /* submodule not present in the main module, get the input data and parse it */ |
| 1185 | if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 1186 | search_clb: |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1187 | if (ctx->imp_clb) { |
| 1188 | const char *submodule_data = NULL; |
| 1189 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 1190 | |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1191 | void (*submodule_data_free)(void *module_data, void *user_data) = NULL; |
| 1192 | struct lysp_load_module_check_data check_data = {0}; |
| 1193 | struct ly_in *in; |
| 1194 | |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1195 | if (ctx->imp_clb(PARSER_CUR_PMOD(pctx)->mod->name, NULL, inc->name, |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1196 | inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data, |
| 1197 | &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) { |
| 1198 | LY_CHECK_RET(ly_in_new_memory(submodule_data, &in)); |
| 1199 | check_data.name = inc->name; |
| 1200 | check_data.revision = inc->rev[0] ? inc->rev : NULL; |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1201 | check_data.submoduleof = PARSER_CUR_PMOD(pctx)->mod->name; |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 1202 | lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, new_mods, &submod); |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1203 | |
| 1204 | /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting |
| 1205 | * submodule's include into main module, where it is missing */ |
| 1206 | inc = &pmod->includes[u]; |
| 1207 | |
| 1208 | ly_in_free(in, 0); |
| 1209 | if (submodule_data_free) { |
| 1210 | submodule_data_free((void *)submodule_data, ctx->imp_clb_data); |
| 1211 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1212 | } |
| 1213 | } |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1214 | if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 1215 | goto search_file; |
| 1216 | } |
| 1217 | } else { |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 1218 | search_file: |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1219 | if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) { |
| 1220 | /* submodule was not received from the callback or there is no callback set */ |
Michal Vasko | dd6d5a3 | 2022-07-14 13:54:58 +0200 | [diff] [blame] | 1221 | lys_parse_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, pctx->main_ctx, |
| 1222 | PARSER_CUR_PMOD(pctx->main_ctx)->mod->name, 1, new_mods, (void **)&submod); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1223 | |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1224 | /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting |
| 1225 | * submodule's include into main module, where it is missing */ |
| 1226 | inc = &pmod->includes[u]; |
| 1227 | } |
| 1228 | if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) { |
| 1229 | goto search_clb; |
| 1230 | } |
| 1231 | } |
| 1232 | if (submod) { |
| 1233 | if (!inc->rev[0] && (submod->latest_revision == 1)) { |
| 1234 | /* update the latest_revision flag - here we have selected the latest available schema, |
| 1235 | * consider that even the callback provides correct latest revision */ |
| 1236 | submod->latest_revision = 2; |
| 1237 | } |
| 1238 | |
| 1239 | inc->submodule = submod; |
| 1240 | if (ret == LY_ENOT) { |
| 1241 | /* the submodule include is not present in YANG 1.0 main module - add it there */ |
| 1242 | LY_CHECK_RET(lysp_inject_submodule(pctx, &pmod->includes[u])); |
| 1243 | } |
| 1244 | } |
| 1245 | if (!inc->submodule) { |
| 1246 | LOGVAL(ctx, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name, |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1247 | PARSER_CUR_PMOD(pctx)->is_submod ? ((struct lysp_submodule *)PARSER_CUR_PMOD(pctx))->name : |
| 1248 | PARSER_CUR_PMOD(pctx)->mod->name); |
Radek Krejci | 771928a | 2021-01-19 13:42:36 +0100 | [diff] [blame] | 1249 | return LY_EVALID; |
| 1250 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | return LY_SUCCESS; |
| 1254 | } |
| 1255 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1256 | LIBYANG_API_DEF const struct lysc_when * |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 1257 | lysc_has_when(const struct lysc_node *node) |
| 1258 | { |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1259 | struct lysc_when **when; |
| 1260 | |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 1261 | if (!node) { |
| 1262 | return NULL; |
| 1263 | } |
| 1264 | |
| 1265 | do { |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1266 | when = lysc_node_when(node); |
| 1267 | if (when) { |
| 1268 | return when[0]; |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 1269 | } |
| 1270 | node = node->parent; |
| 1271 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 1272 | |
| 1273 | return NULL; |
| 1274 | } |
| 1275 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1276 | LIBYANG_API_DEF const struct lys_module * |
Michal Vasko | ef53c81 | 2021-10-13 10:21:03 +0200 | [diff] [blame] | 1277 | lysc_owner_module(const struct lysc_node *node) |
| 1278 | { |
| 1279 | if (!node) { |
| 1280 | return NULL; |
| 1281 | } |
| 1282 | |
| 1283 | for ( ; node->parent; node = node->parent) {} |
| 1284 | return node->module; |
| 1285 | } |
| 1286 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1287 | LIBYANG_API_DEF const char * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1288 | lys_nodetype2str(uint16_t nodetype) |
| 1289 | { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1290 | switch (nodetype) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1291 | case LYS_CONTAINER: |
| 1292 | return "container"; |
| 1293 | case LYS_CHOICE: |
| 1294 | return "choice"; |
| 1295 | case LYS_LEAF: |
| 1296 | return "leaf"; |
| 1297 | case LYS_LEAFLIST: |
| 1298 | return "leaf-list"; |
| 1299 | case LYS_LIST: |
| 1300 | return "list"; |
| 1301 | case LYS_ANYXML: |
| 1302 | return "anyxml"; |
| 1303 | case LYS_ANYDATA: |
| 1304 | return "anydata"; |
Radek Krejci | f12a1f0 | 2019-02-11 16:42:08 +0100 | [diff] [blame] | 1305 | case LYS_CASE: |
| 1306 | return "case"; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1307 | case LYS_RPC: |
| 1308 | return "RPC"; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1309 | case LYS_ACTION: |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1310 | return "action"; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1311 | case LYS_NOTIF: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 1312 | return "notification"; |
Radek Krejci | fc81ea8 | 2019-04-18 13:27:22 +0200 | [diff] [blame] | 1313 | case LYS_USES: |
| 1314 | return "uses"; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1315 | default: |
| 1316 | return "unknown"; |
| 1317 | } |
| 1318 | } |
| 1319 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1320 | LIBYANG_API_DEF enum ly_stmt |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 1321 | lys_nodetype2stmt(uint16_t nodetype) |
| 1322 | { |
| 1323 | switch (nodetype) { |
| 1324 | case LYS_CONTAINER: |
| 1325 | return LY_STMT_CONTAINER; |
| 1326 | case LYS_CHOICE: |
| 1327 | return LY_STMT_CHOICE; |
| 1328 | case LYS_LEAF: |
| 1329 | return LY_STMT_LEAF; |
| 1330 | case LYS_LEAFLIST: |
| 1331 | return LY_STMT_LEAF_LIST; |
| 1332 | case LYS_LIST: |
| 1333 | return LY_STMT_LIST; |
| 1334 | case LYS_ANYXML: |
| 1335 | return LY_STMT_ANYXML; |
| 1336 | case LYS_ANYDATA: |
| 1337 | return LY_STMT_ANYDATA; |
| 1338 | case LYS_CASE: |
| 1339 | return LY_STMT_CASE; |
| 1340 | case LYS_RPC: |
| 1341 | return LY_STMT_RPC; |
| 1342 | case LYS_ACTION: |
| 1343 | return LY_STMT_ACTION; |
| 1344 | case LYS_NOTIF: |
| 1345 | return LY_STMT_NOTIFICATION; |
| 1346 | case LYS_USES: |
| 1347 | return LY_STMT_USES; |
| 1348 | case LYS_INPUT: |
| 1349 | return LY_STMT_INPUT; |
| 1350 | case LYS_OUTPUT: |
| 1351 | return LY_STMT_OUTPUT; |
| 1352 | default: |
| 1353 | return LY_STMT_NONE; |
| 1354 | } |
| 1355 | } |
| 1356 | |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1357 | const char * |
| 1358 | lys_datatype2str(LY_DATA_TYPE basetype) |
| 1359 | { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1360 | switch (basetype) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 1361 | case LY_TYPE_BINARY: |
| 1362 | return "binary"; |
| 1363 | case LY_TYPE_UINT8: |
| 1364 | return "uint8"; |
| 1365 | case LY_TYPE_UINT16: |
| 1366 | return "uint16"; |
| 1367 | case LY_TYPE_UINT32: |
| 1368 | return "uint32"; |
| 1369 | case LY_TYPE_UINT64: |
| 1370 | return "uint64"; |
| 1371 | case LY_TYPE_STRING: |
| 1372 | return "string"; |
| 1373 | case LY_TYPE_BITS: |
| 1374 | return "bits"; |
| 1375 | case LY_TYPE_BOOL: |
| 1376 | return "boolean"; |
| 1377 | case LY_TYPE_DEC64: |
| 1378 | return "decimal64"; |
| 1379 | case LY_TYPE_EMPTY: |
| 1380 | return "empty"; |
| 1381 | case LY_TYPE_ENUM: |
| 1382 | return "enumeration"; |
| 1383 | case LY_TYPE_IDENT: |
| 1384 | return "identityref"; |
| 1385 | case LY_TYPE_INST: |
| 1386 | return "instance-identifier"; |
| 1387 | case LY_TYPE_LEAFREF: |
| 1388 | return "leafref"; |
| 1389 | case LY_TYPE_UNION: |
| 1390 | return "union"; |
| 1391 | case LY_TYPE_INT8: |
| 1392 | return "int8"; |
| 1393 | case LY_TYPE_INT16: |
| 1394 | return "int16"; |
| 1395 | case LY_TYPE_INT32: |
| 1396 | return "int32"; |
| 1397 | case LY_TYPE_INT64: |
| 1398 | return "int64"; |
| 1399 | default: |
| 1400 | return "unknown"; |
| 1401 | } |
| 1402 | } |
| 1403 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1404 | LIBYANG_API_DEF const struct lysp_tpdf * |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1405 | lysp_node_typedefs(const struct lysp_node *node) |
| 1406 | { |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1407 | switch (node->nodetype) { |
| 1408 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1409 | return ((struct lysp_node_container *)node)->typedefs; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1410 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1411 | return ((struct lysp_node_list *)node)->typedefs; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1412 | case LYS_GROUPING: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1413 | return ((struct lysp_node_grp *)node)->typedefs; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1414 | case LYS_RPC: |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1415 | case LYS_ACTION: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1416 | return ((struct lysp_node_action *)node)->typedefs; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1417 | case LYS_INPUT: |
| 1418 | case LYS_OUTPUT: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1419 | return ((struct lysp_node_action_inout *)node)->typedefs; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1420 | case LYS_NOTIF: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1421 | return ((struct lysp_node_notif *)node)->typedefs; |
Radek Krejci | 0fb2856 | 2018-12-13 15:17:37 +0100 | [diff] [blame] | 1422 | default: |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1423 | return NULL; |
| 1424 | } |
| 1425 | } |
| 1426 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1427 | LIBYANG_API_DEF const struct lysp_node_grp * |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1428 | lysp_node_groupings(const struct lysp_node *node) |
| 1429 | { |
| 1430 | switch (node->nodetype) { |
| 1431 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1432 | return ((struct lysp_node_container *)node)->groupings; |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1433 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1434 | return ((struct lysp_node_list *)node)->groupings; |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1435 | case LYS_GROUPING: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1436 | return ((struct lysp_node_grp *)node)->groupings; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1437 | case LYS_RPC: |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1438 | case LYS_ACTION: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1439 | return ((struct lysp_node_action *)node)->groupings; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1440 | case LYS_INPUT: |
| 1441 | case LYS_OUTPUT: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1442 | return ((struct lysp_node_action_inout *)node)->groupings; |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1443 | case LYS_NOTIF: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1444 | return ((struct lysp_node_notif *)node)->groupings; |
Radek Krejci | 53ea615 | 2018-12-13 15:21:15 +0100 | [diff] [blame] | 1445 | default: |
| 1446 | return NULL; |
| 1447 | } |
| 1448 | } |
| 1449 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1450 | struct lysp_node_action ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1451 | lysp_node_actions_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1452 | { |
| 1453 | assert(node); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1454 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1455 | switch (node->nodetype) { |
| 1456 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1457 | return &((struct lysp_node_container *)node)->actions; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1458 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1459 | return &((struct lysp_node_list *)node)->actions; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1460 | case LYS_GROUPING: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1461 | return &((struct lysp_node_grp *)node)->actions; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1462 | case LYS_AUGMENT: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1463 | return &((struct lysp_node_augment *)node)->actions; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1464 | default: |
| 1465 | return NULL; |
| 1466 | } |
| 1467 | } |
| 1468 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1469 | LIBYANG_API_DEF const struct lysp_node_action * |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1470 | lysp_node_actions(const struct lysp_node *node) |
| 1471 | { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1472 | struct lysp_node_action **actions; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1473 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1474 | actions = lysp_node_actions_p((struct lysp_node *)node); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1475 | if (actions) { |
| 1476 | return *actions; |
| 1477 | } else { |
| 1478 | return NULL; |
| 1479 | } |
| 1480 | } |
| 1481 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1482 | struct lysp_node_notif ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1483 | lysp_node_notifs_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1484 | { |
| 1485 | assert(node); |
| 1486 | switch (node->nodetype) { |
| 1487 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1488 | return &((struct lysp_node_container *)node)->notifs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1489 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1490 | return &((struct lysp_node_list *)node)->notifs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1491 | case LYS_GROUPING: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1492 | return &((struct lysp_node_grp *)node)->notifs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1493 | case LYS_AUGMENT: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1494 | return &((struct lysp_node_augment *)node)->notifs; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1495 | default: |
| 1496 | return NULL; |
| 1497 | } |
| 1498 | } |
| 1499 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1500 | LIBYANG_API_DEF const struct lysp_node_notif * |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1501 | lysp_node_notifs(const struct lysp_node *node) |
| 1502 | { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1503 | struct lysp_node_notif **notifs; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1504 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1505 | notifs = lysp_node_notifs_p((struct lysp_node *)node); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1506 | if (notifs) { |
| 1507 | return *notifs; |
| 1508 | } else { |
| 1509 | return NULL; |
| 1510 | } |
| 1511 | } |
| 1512 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1513 | struct lysp_node ** |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1514 | lysp_node_child_p(struct lysp_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1515 | { |
| 1516 | assert(node); |
| 1517 | switch (node->nodetype) { |
| 1518 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1519 | return &((struct lysp_node_container *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1520 | case LYS_CHOICE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1521 | return &((struct lysp_node_choice *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1522 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1523 | return &((struct lysp_node_list *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1524 | case LYS_CASE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1525 | return &((struct lysp_node_case *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1526 | case LYS_GROUPING: |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1527 | return &((struct lysp_node_grp *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1528 | case LYS_AUGMENT: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1529 | return &((struct lysp_node_augment *)node)->child; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1530 | case LYS_INPUT: |
| 1531 | case LYS_OUTPUT: |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1532 | return &((struct lysp_node_action_inout *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1533 | case LYS_NOTIF: |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1534 | return &((struct lysp_node_notif *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1535 | default: |
| 1536 | return NULL; |
| 1537 | } |
| 1538 | } |
| 1539 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1540 | LIBYANG_API_DEF const struct lysp_node * |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1541 | lysp_node_child(const struct lysp_node *node) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1542 | { |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1543 | struct lysp_node **child; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1544 | |
| 1545 | if (!node) { |
| 1546 | return NULL; |
| 1547 | } |
| 1548 | |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1549 | child = lysp_node_child_p((struct lysp_node *)node); |
| 1550 | if (child) { |
| 1551 | return *child; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1552 | } else { |
| 1553 | return NULL; |
| 1554 | } |
| 1555 | } |
| 1556 | |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1557 | struct lysp_restr ** |
| 1558 | lysp_node_musts_p(const struct lysp_node *node) |
| 1559 | { |
| 1560 | if (!node) { |
| 1561 | return NULL; |
| 1562 | } |
| 1563 | |
Radek Krejci | 2d5f6df | 2021-01-28 14:00:13 +0100 | [diff] [blame] | 1564 | switch (node->nodetype) { |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1565 | case LYS_CONTAINER: |
| 1566 | return &((struct lysp_node_container *)node)->musts; |
| 1567 | case LYS_LEAF: |
| 1568 | return &((struct lysp_node_leaf *)node)->musts; |
| 1569 | case LYS_LEAFLIST: |
| 1570 | return &((struct lysp_node_leaflist *)node)->musts; |
| 1571 | case LYS_LIST: |
| 1572 | return &((struct lysp_node_list *)node)->musts; |
| 1573 | case LYS_ANYXML: |
| 1574 | case LYS_ANYDATA: |
| 1575 | return &((struct lysp_node_anydata *)node)->musts; |
| 1576 | case LYS_NOTIF: |
| 1577 | return &((struct lysp_node_notif *)node)->musts; |
| 1578 | case LYS_INPUT: |
| 1579 | case LYS_OUTPUT: |
| 1580 | return &((struct lysp_node_action_inout *)node)->musts; |
| 1581 | default: |
| 1582 | return NULL; |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | struct lysp_restr * |
| 1587 | lysp_node_musts(const struct lysp_node *node) |
| 1588 | { |
| 1589 | struct lysp_restr **musts; |
| 1590 | |
| 1591 | musts = lysp_node_musts_p(node); |
| 1592 | if (musts) { |
| 1593 | return *musts; |
| 1594 | } else { |
| 1595 | return NULL; |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | struct lysp_when ** |
| 1600 | lysp_node_when_p(const struct lysp_node *node) |
| 1601 | { |
| 1602 | if (!node) { |
| 1603 | return NULL; |
| 1604 | } |
| 1605 | |
Radek Krejci | 2d5f6df | 2021-01-28 14:00:13 +0100 | [diff] [blame] | 1606 | switch (node->nodetype) { |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1607 | case LYS_CONTAINER: |
| 1608 | return &((struct lysp_node_container *)node)->when; |
| 1609 | case LYS_CHOICE: |
| 1610 | return &((struct lysp_node_choice *)node)->when; |
| 1611 | case LYS_LEAF: |
| 1612 | return &((struct lysp_node_leaf *)node)->when; |
| 1613 | case LYS_LEAFLIST: |
| 1614 | return &((struct lysp_node_leaflist *)node)->when; |
| 1615 | case LYS_LIST: |
| 1616 | return &((struct lysp_node_list *)node)->when; |
| 1617 | case LYS_ANYXML: |
| 1618 | case LYS_ANYDATA: |
| 1619 | return &((struct lysp_node_anydata *)node)->when; |
| 1620 | case LYS_CASE: |
| 1621 | return &((struct lysp_node_case *)node)->when; |
| 1622 | case LYS_USES: |
| 1623 | return &((struct lysp_node_uses *)node)->when; |
| 1624 | case LYS_AUGMENT: |
| 1625 | return &((struct lysp_node_augment *)node)->when; |
| 1626 | default: |
| 1627 | return NULL; |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | struct lysp_when * |
| 1632 | lysp_node_when(const struct lysp_node *node) |
| 1633 | { |
| 1634 | struct lysp_when **when; |
| 1635 | |
| 1636 | when = lysp_node_when_p(node); |
| 1637 | if (when) { |
| 1638 | return *when; |
| 1639 | } else { |
| 1640 | return NULL; |
| 1641 | } |
| 1642 | } |
| 1643 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1644 | struct lysc_node_action ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1645 | lysc_node_actions_p(struct lysc_node *node) |
| 1646 | { |
| 1647 | assert(node); |
| 1648 | switch (node->nodetype) { |
| 1649 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1650 | return &((struct lysc_node_container *)node)->actions; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1651 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1652 | return &((struct lysc_node_list *)node)->actions; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1653 | default: |
| 1654 | return NULL; |
| 1655 | } |
| 1656 | } |
| 1657 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1658 | LIBYANG_API_DEF const struct lysc_node_action * |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1659 | lysc_node_actions(const struct lysc_node *node) |
| 1660 | { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1661 | struct lysc_node_action **actions; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1662 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1663 | actions = lysc_node_actions_p((struct lysc_node *)node); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1664 | if (actions) { |
| 1665 | return *actions; |
| 1666 | } else { |
| 1667 | return NULL; |
| 1668 | } |
| 1669 | } |
| 1670 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1671 | struct lysc_node_notif ** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1672 | lysc_node_notifs_p(struct lysc_node *node) |
| 1673 | { |
| 1674 | assert(node); |
| 1675 | switch (node->nodetype) { |
| 1676 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1677 | return &((struct lysc_node_container *)node)->notifs; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1678 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1679 | return &((struct lysc_node_list *)node)->notifs; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1680 | default: |
| 1681 | return NULL; |
| 1682 | } |
| 1683 | } |
| 1684 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1685 | LIBYANG_API_DEF const struct lysc_node_notif * |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1686 | lysc_node_notifs(const struct lysc_node *node) |
| 1687 | { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1688 | struct lysc_node_notif **notifs; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1689 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1690 | notifs = lysc_node_notifs_p((struct lysc_node *)node); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1691 | if (notifs) { |
| 1692 | return *notifs; |
| 1693 | } else { |
| 1694 | return NULL; |
| 1695 | } |
| 1696 | } |
| 1697 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1698 | struct lysc_node ** |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1699 | lysc_node_child_p(const struct lysc_node *node) |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1700 | { |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1701 | assert(node && !(node->nodetype & (LYS_RPC | LYS_ACTION))); |
| 1702 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1703 | switch (node->nodetype) { |
| 1704 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1705 | return &((struct lysc_node_container *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1706 | case LYS_CHOICE: |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 1707 | return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 1708 | case LYS_CASE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1709 | return &((struct lysc_node_case *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1710 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1711 | return &((struct lysc_node_list *)node)->child; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1712 | case LYS_INPUT: |
| 1713 | case LYS_OUTPUT: |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1714 | return &((struct lysc_node_action_inout *)node)->child; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 1715 | case LYS_NOTIF: |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1716 | return &((struct lysc_node_notif *)node)->child; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1717 | default: |
| 1718 | return NULL; |
| 1719 | } |
| 1720 | } |
| 1721 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1722 | LIBYANG_API_DEF const struct lysc_node * |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1723 | lysc_node_child(const struct lysc_node *node) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1724 | { |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1725 | struct lysc_node **child; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1726 | |
| 1727 | if (!node) { |
| 1728 | return NULL; |
| 1729 | } |
| 1730 | |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1731 | if (node->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 1732 | return &((struct lysc_node_action *)node)->input.node; |
Radek Krejci | be15444 | 2021-01-21 11:06:36 +0100 | [diff] [blame] | 1733 | } else { |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1734 | child = lysc_node_child_p(node); |
| 1735 | if (child) { |
| 1736 | return *child; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1737 | } |
Michal Vasko | 2a66871 | 2020-10-21 11:48:09 +0200 | [diff] [blame] | 1738 | } |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1739 | |
| 1740 | return NULL; |
Michal Vasko | 2a66871 | 2020-10-21 11:48:09 +0200 | [diff] [blame] | 1741 | } |
| 1742 | |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1743 | struct lysc_must ** |
| 1744 | lysc_node_musts_p(const struct lysc_node *node) |
| 1745 | { |
| 1746 | if (!node) { |
| 1747 | return NULL; |
| 1748 | } |
| 1749 | |
Radek Krejci | 2d5f6df | 2021-01-28 14:00:13 +0100 | [diff] [blame] | 1750 | switch (node->nodetype) { |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1751 | case LYS_CONTAINER: |
| 1752 | return &((struct lysc_node_container *)node)->musts; |
| 1753 | case LYS_LEAF: |
| 1754 | return &((struct lysc_node_leaf *)node)->musts; |
| 1755 | case LYS_LEAFLIST: |
| 1756 | return &((struct lysc_node_leaflist *)node)->musts; |
| 1757 | case LYS_LIST: |
| 1758 | return &((struct lysc_node_list *)node)->musts; |
| 1759 | case LYS_ANYXML: |
| 1760 | case LYS_ANYDATA: |
| 1761 | return &((struct lysc_node_anydata *)node)->musts; |
| 1762 | case LYS_NOTIF: |
| 1763 | return &((struct lysc_node_notif *)node)->musts; |
| 1764 | case LYS_INPUT: |
| 1765 | case LYS_OUTPUT: |
| 1766 | return &((struct lysc_node_action_inout *)node)->musts; |
| 1767 | default: |
| 1768 | return NULL; |
| 1769 | } |
| 1770 | } |
| 1771 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1772 | LIBYANG_API_DEF struct lysc_must * |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1773 | lysc_node_musts(const struct lysc_node *node) |
| 1774 | { |
| 1775 | struct lysc_must **must_p; |
| 1776 | |
| 1777 | must_p = lysc_node_musts_p(node); |
| 1778 | if (must_p) { |
| 1779 | return *must_p; |
| 1780 | } else { |
| 1781 | return NULL; |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | struct lysc_when *** |
| 1786 | lysc_node_when_p(const struct lysc_node *node) |
| 1787 | { |
| 1788 | if (!node) { |
| 1789 | return NULL; |
| 1790 | } |
| 1791 | |
Radek Krejci | 2d5f6df | 2021-01-28 14:00:13 +0100 | [diff] [blame] | 1792 | switch (node->nodetype) { |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1793 | case LYS_CONTAINER: |
| 1794 | return &((struct lysc_node_container *)node)->when; |
| 1795 | case LYS_CHOICE: |
| 1796 | return &((struct lysc_node_choice *)node)->when; |
| 1797 | case LYS_LEAF: |
| 1798 | return &((struct lysc_node_leaf *)node)->when; |
| 1799 | case LYS_LEAFLIST: |
| 1800 | return &((struct lysc_node_leaflist *)node)->when; |
| 1801 | case LYS_LIST: |
| 1802 | return &((struct lysc_node_list *)node)->when; |
| 1803 | case LYS_ANYXML: |
| 1804 | case LYS_ANYDATA: |
| 1805 | return &((struct lysc_node_anydata *)node)->when; |
| 1806 | case LYS_CASE: |
| 1807 | return &((struct lysc_node_case *)node)->when; |
| 1808 | case LYS_NOTIF: |
| 1809 | return &((struct lysc_node_notif *)node)->when; |
| 1810 | case LYS_RPC: |
| 1811 | case LYS_ACTION: |
| 1812 | return &((struct lysc_node_action *)node)->when; |
| 1813 | default: |
| 1814 | return NULL; |
| 1815 | } |
| 1816 | } |
| 1817 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1818 | LIBYANG_API_DEF struct lysc_when ** |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1819 | lysc_node_when(const struct lysc_node *node) |
| 1820 | { |
| 1821 | struct lysc_when ***when_p; |
| 1822 | |
| 1823 | when_p = lysc_node_when_p(node); |
| 1824 | if (when_p) { |
| 1825 | return *when_p; |
| 1826 | } else { |
| 1827 | return NULL; |
| 1828 | } |
| 1829 | } |
| 1830 | |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1831 | struct lys_module * |
| 1832 | lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod) |
| 1833 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1834 | for (uint32_t u = 0; u < ctx->list.count; ++u) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1835 | if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1836 | return (struct lys_module *)ctx->list.objs[u]; |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1837 | } |
| 1838 | } |
| 1839 | return NULL; |
| 1840 | } |
| 1841 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1842 | enum ly_stmt |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1843 | lysp_match_kw(struct ly_in *in, uint64_t *indent) |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 1844 | { |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1845 | /** |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1846 | * @brief Move the input by COUNT items. Also updates the indent value in yang parser context |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1847 | * @param[in] COUNT number of items for which the DATA pointer is supposed to move on. |
Michal Vasko | 64246d8 | 2020-08-19 12:35:00 +0200 | [diff] [blame] | 1848 | * |
| 1849 | * *INDENT-OFF* |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1850 | */ |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1851 | #define MOVE_IN(COUNT) \ |
| 1852 | ly_in_skip(in, COUNT); \ |
| 1853 | if (indent) { \ |
| 1854 | (*indent)+=COUNT; \ |
| 1855 | } |
| 1856 | #define IF_KW(STR, LEN, STMT) \ |
| 1857 | if (!strncmp(in->current, STR, LEN)) { \ |
| 1858 | MOVE_IN(LEN); \ |
| 1859 | (*kw)=STMT; \ |
| 1860 | } |
| 1861 | #define IF_KW_PREFIX(STR, LEN) \ |
| 1862 | if (!strncmp(in->current, STR, LEN)) { \ |
| 1863 | MOVE_IN(LEN); |
| 1864 | #define IF_KW_PREFIX_END \ |
| 1865 | } |
David Sedlák | 572e7ab | 2019-06-04 16:01:58 +0200 | [diff] [blame] | 1866 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1867 | const char *start = in->current; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1868 | enum ly_stmt result = LY_STMT_NONE; |
| 1869 | enum ly_stmt *kw = &result; |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1870 | /* read the keyword itself */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1871 | switch (in->current[0]) { |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1872 | case 'a': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1873 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1874 | IF_KW("rgument", 7, LY_STMT_ARGUMENT) |
| 1875 | else IF_KW("ugment", 6, LY_STMT_AUGMENT) |
| 1876 | else IF_KW("ction", 5, LY_STMT_ACTION) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1877 | else IF_KW_PREFIX("ny", 2) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1878 | IF_KW("data", 4, LY_STMT_ANYDATA) |
| 1879 | else IF_KW("xml", 3, LY_STMT_ANYXML) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1880 | IF_KW_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1881 | break; |
| 1882 | case 'b': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1883 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1884 | IF_KW("ase", 3, LY_STMT_BASE) |
| 1885 | else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO) |
| 1886 | else IF_KW("it", 2, LY_STMT_BIT) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1887 | break; |
| 1888 | case 'c': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1889 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1890 | IF_KW("ase", 3, LY_STMT_CASE) |
| 1891 | else IF_KW("hoice", 5, LY_STMT_CHOICE) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1892 | else IF_KW_PREFIX("on", 2) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1893 | IF_KW("fig", 3, LY_STMT_CONFIG) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1894 | else IF_KW_PREFIX("ta", 2) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1895 | IF_KW("ct", 2, LY_STMT_CONTACT) |
| 1896 | else IF_KW("iner", 4, LY_STMT_CONTAINER) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1897 | IF_KW_PREFIX_END |
| 1898 | IF_KW_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1899 | break; |
| 1900 | case 'd': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1901 | MOVE_IN(1); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1902 | IF_KW_PREFIX("e", 1) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1903 | IF_KW("fault", 5, LY_STMT_DEFAULT) |
| 1904 | else IF_KW("scription", 9, LY_STMT_DESCRIPTION) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1905 | else IF_KW_PREFIX("viat", 4) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1906 | IF_KW("e", 1, LY_STMT_DEVIATE) |
| 1907 | else IF_KW("ion", 3, LY_STMT_DEVIATION) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1908 | IF_KW_PREFIX_END |
| 1909 | IF_KW_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1910 | break; |
| 1911 | case 'e': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1912 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1913 | IF_KW("num", 3, LY_STMT_ENUM) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1914 | else IF_KW_PREFIX("rror-", 5) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1915 | IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG) |
| 1916 | else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1917 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1918 | else IF_KW("xtension", 8, LY_STMT_EXTENSION) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1919 | break; |
| 1920 | case 'f': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1921 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1922 | IF_KW("eature", 6, LY_STMT_FEATURE) |
| 1923 | else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1924 | break; |
| 1925 | case 'g': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1926 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1927 | IF_KW("rouping", 7, LY_STMT_GROUPING) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1928 | break; |
| 1929 | case 'i': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1930 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1931 | IF_KW("dentity", 7, LY_STMT_IDENTITY) |
| 1932 | else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE) |
| 1933 | else IF_KW("mport", 5, LY_STMT_IMPORT) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1934 | else IF_KW_PREFIX("n", 1) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1935 | IF_KW("clude", 5, LY_STMT_INCLUDE) |
| 1936 | else IF_KW("put", 3, LY_STMT_INPUT) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1937 | IF_KW_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1938 | break; |
| 1939 | case 'k': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1940 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1941 | IF_KW("ey", 2, LY_STMT_KEY) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1942 | break; |
| 1943 | case 'l': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1944 | MOVE_IN(1); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1945 | IF_KW_PREFIX("e", 1) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1946 | IF_KW("af-list", 7, LY_STMT_LEAF_LIST) |
| 1947 | else IF_KW("af", 2, LY_STMT_LEAF) |
| 1948 | else IF_KW("ngth", 4, LY_STMT_LENGTH) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1949 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1950 | else IF_KW("ist", 3, LY_STMT_LIST) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1951 | break; |
| 1952 | case 'm': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1953 | MOVE_IN(1); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1954 | IF_KW_PREFIX("a", 1) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1955 | IF_KW("ndatory", 7, LY_STMT_MANDATORY) |
| 1956 | else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1957 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1958 | else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS) |
| 1959 | else IF_KW("ust", 3, LY_STMT_MUST) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1960 | else IF_KW_PREFIX("od", 2) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1961 | IF_KW("ule", 3, LY_STMT_MODULE) |
| 1962 | else IF_KW("ifier", 5, LY_STMT_MODIFIER) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1963 | IF_KW_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1964 | break; |
| 1965 | case 'n': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1966 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1967 | IF_KW("amespace", 8, LY_STMT_NAMESPACE) |
| 1968 | else IF_KW("otification", 11, LY_STMT_NOTIFICATION) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1969 | break; |
| 1970 | case 'o': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1971 | MOVE_IN(1); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1972 | IF_KW_PREFIX("r", 1) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1973 | IF_KW("dered-by", 8, LY_STMT_ORDERED_BY) |
| 1974 | else IF_KW("ganization", 10, LY_STMT_ORGANIZATION) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1975 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1976 | else IF_KW("utput", 5, LY_STMT_OUTPUT) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1977 | break; |
| 1978 | case 'p': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1979 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1980 | IF_KW("ath", 3, LY_STMT_PATH) |
| 1981 | else IF_KW("attern", 6, LY_STMT_PATTERN) |
| 1982 | else IF_KW("osition", 7, LY_STMT_POSITION) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1983 | else IF_KW_PREFIX("re", 2) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1984 | IF_KW("fix", 3, LY_STMT_PREFIX) |
| 1985 | else IF_KW("sence", 5, LY_STMT_PRESENCE) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1986 | IF_KW_PREFIX_END |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 1987 | break; |
| 1988 | case 'r': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1989 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1990 | IF_KW("ange", 4, LY_STMT_RANGE) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1991 | else IF_KW_PREFIX("e", 1) |
| 1992 | IF_KW_PREFIX("f", 1) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1993 | IF_KW("erence", 6, LY_STMT_REFERENCE) |
| 1994 | else IF_KW("ine", 3, LY_STMT_REFINE) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1995 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1996 | else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE) |
| 1997 | else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE) |
| 1998 | else IF_KW("vision", 6, LY_STMT_REVISION) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 1999 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2000 | else IF_KW("pc", 2, LY_STMT_RPC) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 2001 | break; |
| 2002 | case 's': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2003 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2004 | IF_KW("tatus", 5, LY_STMT_STATUS) |
| 2005 | else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 2006 | break; |
| 2007 | case 't': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2008 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2009 | IF_KW("ypedef", 6, LY_STMT_TYPEDEF) |
| 2010 | else IF_KW("ype", 3, LY_STMT_TYPE) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 2011 | break; |
| 2012 | case 'u': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2013 | MOVE_IN(1); |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 2014 | IF_KW_PREFIX("ni", 2) |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2015 | IF_KW("que", 3, LY_STMT_UNIQUE) |
| 2016 | else IF_KW("ts", 2, LY_STMT_UNITS) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 2017 | IF_KW_PREFIX_END |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2018 | else IF_KW("ses", 3, LY_STMT_USES) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 2019 | break; |
| 2020 | case 'v': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2021 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2022 | IF_KW("alue", 4, LY_STMT_VALUE) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 2023 | break; |
| 2024 | case 'w': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2025 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2026 | IF_KW("hen", 3, LY_STMT_WHEN) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 2027 | break; |
| 2028 | case 'y': |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2029 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2030 | IF_KW("ang-version", 11, LY_STMT_YANG_VERSION) |
| 2031 | else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT) |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 2032 | break; |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 2033 | default: |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2034 | /* if indent is not NULL we are matching keyword from YANG data */ |
| 2035 | if (indent) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 2036 | if (in->current[0] == ';') { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2037 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2038 | *kw = LY_STMT_SYNTAX_SEMICOLON; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 2039 | } else if (in->current[0] == '{') { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2040 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2041 | *kw = LY_STMT_SYNTAX_LEFT_BRACE; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 2042 | } else if (in->current[0] == '}') { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 2043 | MOVE_IN(1); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2044 | *kw = LY_STMT_SYNTAX_RIGHT_BRACE; |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 2045 | } |
| 2046 | } |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 2047 | break; |
| 2048 | } |
| 2049 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 2050 | if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) { |
Radek Krejci | 6e546bf | 2020-05-19 16:16:19 +0200 | [diff] [blame] | 2051 | /* the keyword is not terminated */ |
| 2052 | *kw = LY_STMT_NONE; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 2053 | in->current = start; |
Radek Krejci | 6e546bf | 2020-05-19 16:16:19 +0200 | [diff] [blame] | 2054 | } |
| 2055 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 2056 | #undef IF_KW |
| 2057 | #undef IF_KW_PREFIX |
| 2058 | #undef IF_KW_PREFIX_END |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 2059 | #undef MOVE_IN |
Michal Vasko | 64246d8 | 2020-08-19 12:35:00 +0200 | [diff] [blame] | 2060 | /* *INDENT-ON* */ |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 2061 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 2062 | return result; |
David Sedlák | 23a59a6 | 2018-10-26 13:08:02 +0200 | [diff] [blame] | 2063 | } |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 2064 | |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2065 | LY_ERR |
| 2066 | lysp_ext_find_definition(const struct ly_ctx *ctx, const struct lysp_ext_instance *ext, const struct lys_module **ext_mod, |
| 2067 | struct lysp_ext **ext_def) |
| 2068 | { |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2069 | const char *tmp, *name, *prefix; |
| 2070 | size_t pref_len, name_len; |
Michal Vasko | e168dbd | 2022-06-07 13:53:52 +0200 | [diff] [blame] | 2071 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2072 | const struct lys_module *mod = NULL; |
Michal Vasko | e168dbd | 2022-06-07 13:53:52 +0200 | [diff] [blame] | 2073 | const struct lysp_submodule *submod; |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2074 | |
Radek Krejci | cbb6242 | 2021-03-15 09:29:21 +0100 | [diff] [blame] | 2075 | assert(ext_def); |
| 2076 | |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2077 | *ext_def = NULL; |
| 2078 | if (ext_mod) { |
| 2079 | *ext_mod = NULL; |
| 2080 | } |
| 2081 | |
Radek Krejci | 677abea | 2021-03-05 14:24:53 +0100 | [diff] [blame] | 2082 | /* parse the prefix, the nodeid was previously already parsed and checked */ |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2083 | tmp = ext->name; |
Radek Krejci | 677abea | 2021-03-05 14:24:53 +0100 | [diff] [blame] | 2084 | ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len); |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2085 | |
| 2086 | /* get module where the extension definition should be placed */ |
| 2087 | mod = ly_resolve_prefix(ctx, prefix, pref_len, ext->format, ext->prefix_data); |
| 2088 | if (!mod) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 2089 | LOGVAL(ctx, LYVE_REFERENCE, "Invalid prefix \"%.*s\" used for extension instance identifier.", (int)pref_len, prefix); |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2090 | return LY_EVALID; |
| 2091 | } else if (!mod->parsed->extensions) { |
| 2092 | LOGVAL(ctx, LYVE_REFERENCE, "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 2093 | ext->name, mod->name); |
| 2094 | return LY_EVALID; |
| 2095 | } |
| 2096 | |
| 2097 | /* find the parsed extension definition there */ |
| 2098 | LY_ARRAY_FOR(mod->parsed->extensions, v) { |
| 2099 | if (!strcmp(name, mod->parsed->extensions[v].name)) { |
| 2100 | *ext_def = &mod->parsed->extensions[v]; |
| 2101 | break; |
| 2102 | } |
| 2103 | } |
Michal Vasko | e168dbd | 2022-06-07 13:53:52 +0200 | [diff] [blame] | 2104 | if (!*ext_def) { |
| 2105 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
| 2106 | submod = mod->parsed->includes[u].submodule; |
| 2107 | LY_ARRAY_FOR(submod->extensions, v) { |
| 2108 | if (!strcmp(name, submod->extensions[v].name)) { |
| 2109 | *ext_def = &submod->extensions[v]; |
| 2110 | break; |
| 2111 | } |
| 2112 | } |
| 2113 | } |
| 2114 | } |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2115 | |
Michal Vasko | e168dbd | 2022-06-07 13:53:52 +0200 | [diff] [blame] | 2116 | if (!*ext_def) { |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2117 | LOGVAL(ctx, LYVE_REFERENCE, "Extension definition of extension instance \"%s\" not found.", ext->name); |
| 2118 | return LY_EVALID; |
| 2119 | } |
| 2120 | |
| 2121 | if (ext_mod) { |
| 2122 | *ext_mod = mod; |
| 2123 | } |
| 2124 | return LY_SUCCESS; |
| 2125 | } |
| 2126 | |
| 2127 | LY_ERR |
| 2128 | lysp_ext_instance_resolve_argument(struct ly_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysp_ext *ext_def) |
| 2129 | { |
Radek Krejci | 9f87b0c | 2021-03-05 14:45:26 +0100 | [diff] [blame] | 2130 | if (!ext_def->argname || ext_p->argument) { |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2131 | /* nothing to do */ |
| 2132 | return LY_SUCCESS; |
| 2133 | } |
| 2134 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 2135 | if (ext_p->format == LY_VALUE_XML) { |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2136 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 2137 | struct lysp_stmt *stmt = NULL; |
| 2138 | |
| 2139 | if (ext_def->flags & LYS_YINELEM_TRUE) { |
| 2140 | /* ... argument was the first XML child element */ |
| 2141 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {} |
| 2142 | if (stmt) { |
| 2143 | const char *arg, *ext, *name_arg, *name_ext, *prefix_arg, *prefix_ext; |
| 2144 | size_t name_arg_len, name_ext_len, prefix_arg_len, prefix_ext_len; |
| 2145 | |
| 2146 | stmt = ext_p->child; |
| 2147 | |
| 2148 | arg = stmt->stmt; |
| 2149 | ly_parse_nodeid(&arg, &prefix_arg, &prefix_arg_len, &name_arg, &name_arg_len); |
Radek Krejci | 9f87b0c | 2021-03-05 14:45:26 +0100 | [diff] [blame] | 2150 | if (ly_strncmp(ext_def->argname, name_arg, name_arg_len)) { |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2151 | LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" expects argument element \"%s\" as its first XML child, " |
Radek Krejci | 9f87b0c | 2021-03-05 14:45:26 +0100 | [diff] [blame] | 2152 | "but \"%.*s\" element found.", ext_p->name, ext_def->argname, (int)name_arg_len, name_arg); |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2153 | return LY_EVALID; |
| 2154 | } |
| 2155 | |
| 2156 | /* check namespace - all the extension instances must be qualified and argument element is expected in the same |
| 2157 | * namespace. Do not check just prefixes, there can be different prefixes pointing to the same namespace */ |
| 2158 | ext = ext_p->name; /* include prefix */ |
| 2159 | ly_parse_nodeid(&ext, &prefix_ext, &prefix_ext_len, &name_ext, &name_ext_len); |
| 2160 | |
| 2161 | if (ly_resolve_prefix(ctx, prefix_ext, prefix_ext_len, ext_p->format, ext_p->prefix_data) != |
| 2162 | ly_resolve_prefix(ctx, prefix_arg, prefix_arg_len, stmt->format, stmt->prefix_data)) { |
| 2163 | LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" element and its argument element \"%s\" are " |
Radek Krejci | 9f87b0c | 2021-03-05 14:45:26 +0100 | [diff] [blame] | 2164 | "expected in the same namespace, but they differ.", ext_p->name, ext_def->argname); |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2165 | return LY_EVALID; |
| 2166 | } |
| 2167 | } |
| 2168 | } else { |
| 2169 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 2170 | * with LYS_YIN_ATTR flag */ |
| 2171 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
Radek Krejci | 9f87b0c | 2021-03-05 14:45:26 +0100 | [diff] [blame] | 2172 | if (!strcmp(stmt->stmt, ext_def->argname)) { |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2173 | /* this is the extension's argument */ |
| 2174 | break; |
| 2175 | } |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | if (stmt) { |
| 2180 | LY_CHECK_RET(lydict_insert(ctx, stmt->arg, 0, &ext_p->argument)); |
| 2181 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | if (!ext_p->argument) { |
| 2186 | /* missing extension's argument */ |
| 2187 | LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" misses argument %s\"%s\".", |
Radek Krejci | 9f87b0c | 2021-03-05 14:45:26 +0100 | [diff] [blame] | 2188 | ext_p->name, (ext_def->flags & LYS_YINELEM_TRUE) ? "element " : "", ext_def->argname); |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 2189 | return LY_EVALID; |
| 2190 | } |
| 2191 | |
| 2192 | return LY_SUCCESS; |
| 2193 | } |
| 2194 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2195 | LY_ARRAY_COUNT_TYPE |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 2196 | lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, enum ly_stmt substmt) |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2197 | { |
| 2198 | LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL); |
| 2199 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2200 | for ( ; index < LY_ARRAY_COUNT(ext); index++) { |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 2201 | if (ext[index].parent_stmt == substmt) { |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2202 | return index; |
| 2203 | } |
| 2204 | } |
| 2205 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2206 | return LY_ARRAY_COUNT(ext); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2207 | } |
| 2208 | |
Michal Vasko | f8ded14 | 2022-02-17 10:48:01 +0100 | [diff] [blame] | 2209 | LIBYANG_API_DEF const struct lysc_node * |
Michal Vasko | 7224488 | 2021-01-12 15:21:05 +0100 | [diff] [blame] | 2210 | lysc_data_node(const struct lysc_node *schema) |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 2211 | { |
| 2212 | const struct lysc_node *parent; |
| 2213 | |
Michal Vasko | 7224488 | 2021-01-12 15:21:05 +0100 | [diff] [blame] | 2214 | parent = schema; |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 2215 | while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | |
| 2216 | LYS_ACTION | LYS_NOTIF))) { |
Radek Krejci | 7d95fbb | 2021-01-26 17:33:13 +0100 | [diff] [blame] | 2217 | parent = parent->parent; |
Michal Vasko | 7224488 | 2021-01-12 15:21:05 +0100 | [diff] [blame] | 2218 | } |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 2219 | |
| 2220 | return parent; |
| 2221 | } |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 2222 | |
| 2223 | ly_bool |
| 2224 | lys_has_recompiled(const struct lys_module *mod) |
| 2225 | { |
| 2226 | LY_ARRAY_COUNT_TYPE u; |
| 2227 | |
| 2228 | if (LYSP_HAS_RECOMPILED(mod->parsed)) { |
| 2229 | return 1; |
| 2230 | } |
| 2231 | |
| 2232 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
| 2233 | if (LYSP_HAS_RECOMPILED(mod->parsed->includes[u].submodule)) { |
| 2234 | return 1; |
| 2235 | } |
| 2236 | } |
| 2237 | |
| 2238 | return 0; |
| 2239 | } |
| 2240 | |
| 2241 | ly_bool |
| 2242 | lys_has_compiled(const struct lys_module *mod) |
| 2243 | { |
| 2244 | LY_ARRAY_COUNT_TYPE u; |
| 2245 | |
| 2246 | if (LYSP_HAS_COMPILED(mod->parsed)) { |
| 2247 | return 1; |
| 2248 | } |
| 2249 | |
| 2250 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
| 2251 | if (LYSP_HAS_COMPILED(mod->parsed->includes[u].submodule)) { |
| 2252 | return 1; |
| 2253 | } |
| 2254 | } |
| 2255 | |
| 2256 | return 0; |
| 2257 | } |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 2258 | |
| 2259 | ly_bool |
Michal Vasko | 87cfdba | 2022-02-22 14:13:45 +0100 | [diff] [blame] | 2260 | lys_has_dep_mods(const struct lys_module *mod) |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 2261 | { |
| 2262 | LY_ARRAY_COUNT_TYPE u; |
| 2263 | |
Michal Vasko | 87cfdba | 2022-02-22 14:13:45 +0100 | [diff] [blame] | 2264 | /* features */ |
| 2265 | if (mod->parsed->features) { |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 2266 | return 1; |
| 2267 | } |
| 2268 | |
Michal Vasko | 87cfdba | 2022-02-22 14:13:45 +0100 | [diff] [blame] | 2269 | /* groupings */ |
| 2270 | if (mod->parsed->groupings) { |
| 2271 | return 1; |
| 2272 | } |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 2273 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
| 2274 | if (mod->parsed->includes[u].submodule->groupings) { |
| 2275 | return 1; |
| 2276 | } |
| 2277 | } |
| 2278 | |
Michal Vasko | 87cfdba | 2022-02-22 14:13:45 +0100 | [diff] [blame] | 2279 | /* augments (adding nodes with leafrefs) */ |
| 2280 | if (mod->parsed->augments) { |
| 2281 | return 1; |
| 2282 | } |
| 2283 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
| 2284 | if (mod->parsed->includes[u].submodule->augments) { |
| 2285 | return 1; |
| 2286 | } |
| 2287 | } |
| 2288 | |
Michal Vasko | 7ee5be2 | 2021-06-16 17:03:34 +0200 | [diff] [blame] | 2289 | return 0; |
| 2290 | } |