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