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