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