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