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