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