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