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