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