Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file schema_features.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Schema feature handling |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2020 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 | */ |
| 14 | |
| 15 | #define _GNU_SOURCE |
| 16 | |
| 17 | #include "schema_features.h" |
| 18 | |
| 19 | #include <assert.h> |
| 20 | #include <ctype.h> |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 25 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 26 | #include "log.h" |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame] | 27 | #include "ly_common.h" |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 28 | #include "set.h" |
| 29 | #include "tree.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 30 | #include "tree_edit.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 31 | #include "tree_schema.h" |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 32 | #include "tree_schema_internal.h" |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 33 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 34 | #define IFF_RECORDS_IN_BYTE 4 |
| 35 | #define IFF_RECORD_BITS 2 |
| 36 | #define IFF_RECORD_MASK 0x3 |
| 37 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 38 | uint8_t |
| 39 | lysc_iff_getop(uint8_t *list, size_t pos) |
| 40 | { |
| 41 | uint8_t *item; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 42 | uint8_t mask = IFF_RECORD_MASK, result; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 43 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 44 | item = &list[pos / IFF_RECORDS_IN_BYTE]; |
| 45 | result = (*item) & (mask << IFF_RECORD_BITS * (pos % IFF_RECORDS_IN_BYTE)); |
| 46 | return result >> IFF_RECORD_BITS * (pos % IFF_RECORDS_IN_BYTE); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static LY_ERR |
| 50 | lysc_iffeature_value_(const struct lysc_iffeature *iff, size_t *index_e, size_t *index_f) |
| 51 | { |
| 52 | uint8_t op; |
| 53 | LY_ERR a, b; |
| 54 | |
| 55 | op = lysc_iff_getop(iff->expr, *index_e); |
| 56 | (*index_e)++; |
| 57 | |
| 58 | switch (op) { |
| 59 | case LYS_IFF_F: |
| 60 | /* resolve feature */ |
| 61 | return (iff->features[(*index_f)++]->flags & LYS_FENABLED) ? LY_SUCCESS : LY_ENOT; |
| 62 | case LYS_IFF_NOT: |
| 63 | /* invert result */ |
| 64 | return lysc_iffeature_value_(iff, index_e, index_f) == LY_SUCCESS ? LY_ENOT : LY_SUCCESS; |
| 65 | case LYS_IFF_AND: |
| 66 | case LYS_IFF_OR: |
| 67 | a = lysc_iffeature_value_(iff, index_e, index_f); |
| 68 | b = lysc_iffeature_value_(iff, index_e, index_f); |
| 69 | if (op == LYS_IFF_AND) { |
| 70 | if ((a == LY_SUCCESS) && (b == LY_SUCCESS)) { |
| 71 | return LY_SUCCESS; |
| 72 | } else { |
| 73 | return LY_ENOT; |
| 74 | } |
| 75 | } else { /* LYS_IFF_OR */ |
| 76 | if ((a == LY_SUCCESS) || (b == LY_SUCCESS)) { |
| 77 | return LY_SUCCESS; |
| 78 | } else { |
| 79 | return LY_ENOT; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return LY_ENOT; |
| 85 | } |
| 86 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 87 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 88 | lysc_iffeature_value(const struct lysc_iffeature *iff) |
| 89 | { |
| 90 | size_t index_e = 0, index_f = 0; |
| 91 | |
| 92 | LY_CHECK_ARG_RET(NULL, iff, LY_EINVAL); |
| 93 | |
| 94 | if (iff->expr) { |
| 95 | return lysc_iffeature_value_(iff, &index_e, &index_f); |
| 96 | } |
| 97 | return LY_ENOT; |
| 98 | } |
| 99 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 100 | LIBYANG_API_DEF LY_ERR |
aPiecek | f4a0a19 | 2021-08-03 15:14:17 +0200 | [diff] [blame] | 101 | lys_identity_iffeature_value(const struct lysc_ident *ident) |
| 102 | { |
aPiecek | f3e2db8 | 2021-08-05 10:18:43 +0200 | [diff] [blame] | 103 | LY_ARRAY_COUNT_TYPE u, v; |
aPiecek | f4a0a19 | 2021-08-03 15:14:17 +0200 | [diff] [blame] | 104 | ly_bool enabled; |
aPiecek | f3e2db8 | 2021-08-05 10:18:43 +0200 | [diff] [blame] | 105 | const struct lysp_ident *idents_p, *found_ident = NULL; |
| 106 | struct lysp_include *includes; |
aPiecek | f4a0a19 | 2021-08-03 15:14:17 +0200 | [diff] [blame] | 107 | |
Michal Vasko | cba757c | 2024-09-10 14:46:07 +0200 | [diff] [blame^] | 108 | LY_CHECK_ARG_RET(NULL, ident, ident->module->parsed, LY_EINVAL); |
aPiecek | f4a0a19 | 2021-08-03 15:14:17 +0200 | [diff] [blame] | 109 | |
aPiecek | f3e2db8 | 2021-08-05 10:18:43 +0200 | [diff] [blame] | 110 | /* Search parsed identity in the module. */ |
aPiecek | f4a0a19 | 2021-08-03 15:14:17 +0200 | [diff] [blame] | 111 | idents_p = ident->module->parsed->identities; |
| 112 | LY_ARRAY_FOR(idents_p, u) { |
| 113 | if (idents_p[u].name == ident->name) { |
aPiecek | f3e2db8 | 2021-08-05 10:18:43 +0200 | [diff] [blame] | 114 | found_ident = &idents_p[u]; |
aPiecek | f4a0a19 | 2021-08-03 15:14:17 +0200 | [diff] [blame] | 115 | break; |
| 116 | } |
| 117 | } |
aPiecek | f4a0a19 | 2021-08-03 15:14:17 +0200 | [diff] [blame] | 118 | |
aPiecek | f3e2db8 | 2021-08-05 10:18:43 +0200 | [diff] [blame] | 119 | if (!found_ident) { |
| 120 | /* It is not in the module, so it must be in some submodule. */ |
| 121 | includes = ident->module->parsed->includes; |
| 122 | LY_ARRAY_FOR(includes, u) { |
| 123 | idents_p = includes[u].submodule->identities; |
| 124 | LY_ARRAY_FOR(idents_p, v) { |
| 125 | if (idents_p[v].name == ident->name) { |
| 126 | found_ident = &idents_p[v]; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | assert(found_ident); |
| 134 | |
| 135 | /* Evaluate its if-feature. */ |
| 136 | LY_CHECK_RET(lys_eval_iffeatures(ident->module->ctx, found_ident->iffeatures, &enabled)); |
aPiecek | f4a0a19 | 2021-08-03 15:14:17 +0200 | [diff] [blame] | 137 | if (!enabled) { |
| 138 | return LY_ENOT; |
| 139 | } |
| 140 | |
| 141 | return LY_SUCCESS; |
| 142 | } |
| 143 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 144 | LIBYANG_API_DEF struct lysp_feature * |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 145 | lysp_feature_next(const struct lysp_feature *last, const struct lysp_module *pmod, uint32_t *idx) |
| 146 | { |
| 147 | struct lysp_feature *features; |
| 148 | |
Michal Vasko | cba757c | 2024-09-10 14:46:07 +0200 | [diff] [blame^] | 149 | LY_CHECK_ARG_RET(NULL, pmod, NULL); |
| 150 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 151 | if (!*idx) { |
| 152 | /* module features */ |
| 153 | features = pmod->features; |
Radek Krejci | c7d13e3 | 2020-12-09 12:32:24 +0100 | [diff] [blame] | 154 | } else if ((*idx - 1) < LY_ARRAY_COUNT(pmod->includes)) { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 155 | /* submodule features */ |
| 156 | features = pmod->includes[*idx - 1].submodule->features; |
| 157 | } else { |
| 158 | /* no more features */ |
| 159 | return NULL; |
| 160 | } |
| 161 | |
| 162 | /* get the next feature */ |
| 163 | if (features && (!last || (&features[LY_ARRAY_COUNT(features) - 1] != last))) { |
| 164 | return !last ? &features[0] : (struct lysp_feature *)last + 1; |
| 165 | } |
| 166 | |
| 167 | /* no more features in current (sub)module */ |
| 168 | ++(*idx); |
| 169 | return lysp_feature_next(NULL, pmod, idx); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @brief Find a feature of the given name and referenced in the given module. |
| 174 | * |
| 175 | * @param[in] pmod Module where the feature was referenced (used to resolve prefix of the feature). |
| 176 | * @param[in] name Name of the feature including possible prefix. |
| 177 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 178 | * @param[in] prefixed Whether the feature name can be prefixed. |
| 179 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 180 | */ |
| 181 | static struct lysp_feature * |
| 182 | lysp_feature_find(const struct lysp_module *pmod, const char *name, size_t len, ly_bool prefixed) |
| 183 | { |
| 184 | const struct lys_module *mod; |
| 185 | const char *ptr; |
| 186 | struct lysp_feature *f = NULL; |
| 187 | uint32_t idx = 0; |
| 188 | |
| 189 | assert(pmod); |
| 190 | |
| 191 | if (prefixed && (ptr = ly_strnchr(name, ':', len))) { |
| 192 | /* we have a prefixed feature */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 193 | mod = ly_resolve_prefix(pmod->mod->ctx, name, ptr - name, LY_VALUE_SCHEMA, (void *)pmod); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 194 | LY_CHECK_RET(!mod, NULL); |
| 195 | |
| 196 | pmod = mod->parsed; |
| 197 | len = len - (ptr - name) - 1; |
| 198 | name = ptr + 1; |
| 199 | } |
| 200 | |
Michal Vasko | ecd9457 | 2020-12-03 14:15:58 +0100 | [diff] [blame] | 201 | /* feature without prefix, look in main module and all submodules */ |
| 202 | if (pmod->is_submod) { |
| 203 | pmod = pmod->mod->parsed; |
| 204 | } |
| 205 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 206 | /* we have the correct module, get the feature */ |
| 207 | while ((f = lysp_feature_next(f, pmod, &idx))) { |
| 208 | if (!ly_strncmp(f->name, name, len)) { |
| 209 | return f; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return NULL; |
| 214 | } |
| 215 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 216 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 217 | lys_feature_value(const struct lys_module *module, const char *feature) |
| 218 | { |
| 219 | const struct lysp_feature *f; |
| 220 | |
| 221 | LY_CHECK_ARG_RET(NULL, module, module->parsed, feature, LY_EINVAL); |
| 222 | |
| 223 | /* search for the specified feature */ |
| 224 | f = lysp_feature_find(module->parsed, feature, strlen(feature), 0); |
| 225 | LY_CHECK_RET(!f, LY_ENOTFOUND); |
| 226 | |
| 227 | /* feature disabled */ |
| 228 | if (!(f->flags & LYS_FENABLED)) { |
| 229 | return LY_ENOT; |
| 230 | } |
| 231 | |
| 232 | /* feature enabled */ |
| 233 | return LY_SUCCESS; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * @brief Stack for processing if-feature expressions. |
| 238 | */ |
| 239 | struct iff_stack { |
| 240 | size_t size; /**< number of items in the stack */ |
| 241 | size_t index; /**< first empty item */ |
| 242 | uint8_t *stack; /**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */ |
| 243 | }; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 244 | #define IFF_STACK_SIZE_STEP 4 |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 245 | |
| 246 | /** |
| 247 | * @brief Add @ref ifftokens into the stack. |
| 248 | * @param[in] stack The if-feature stack to use. |
| 249 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 250 | * @return LY_EMEM in case of memory allocation error |
| 251 | * @return LY_ESUCCESS if the value successfully stored. |
| 252 | */ |
| 253 | static LY_ERR |
| 254 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 255 | { |
| 256 | if (stack->index == stack->size) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 257 | stack->size += IFF_STACK_SIZE_STEP; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 258 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 259 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 260 | } |
| 261 | stack->stack[stack->index++] = value; |
| 262 | return LY_SUCCESS; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * @brief Get (and remove) the last item form the stack. |
| 267 | * @param[in] stack The if-feature stack to use. |
| 268 | * @return The value from the top of the stack. |
| 269 | */ |
| 270 | static uint8_t |
| 271 | iff_stack_pop(struct iff_stack *stack) |
| 272 | { |
| 273 | assert(stack && stack->index); |
| 274 | |
| 275 | stack->index--; |
| 276 | return stack->stack[stack->index]; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * @brief Clean up the stack. |
| 281 | * @param[in] stack The if-feature stack to use. |
| 282 | */ |
| 283 | static void |
| 284 | iff_stack_clean(struct iff_stack *stack) |
| 285 | { |
| 286 | stack->size = 0; |
| 287 | free(stack->stack); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 292 | * (libyang format of the if-feature expression). |
| 293 | * @param[in,out] list The 2bits array to modify. |
| 294 | * @param[in] op The operand (@ref ifftokens) to store. |
| 295 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 296 | */ |
| 297 | static void |
| 298 | iff_setop(uint8_t *list, uint8_t op, size_t pos) |
| 299 | { |
| 300 | uint8_t *item; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 301 | uint8_t mask = IFF_RECORD_MASK; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 302 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 303 | assert(op <= IFF_RECORD_MASK); /* max 2 bits */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 304 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 305 | item = &list[pos / IFF_RECORDS_IN_BYTE]; |
| 306 | mask = mask << IFF_RECORD_BITS * (pos % IFF_RECORDS_IN_BYTE); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 307 | *item = (*item) & ~mask; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 308 | *item = (*item) | (op << IFF_RECORD_BITS * (pos % IFF_RECORDS_IN_BYTE)); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 312 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
| 313 | |
| 314 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 315 | lys_compile_iffeature(const struct ly_ctx *ctx, const struct lysp_qname *qname, struct lysc_iffeature *iff) |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 316 | { |
| 317 | LY_ERR rc = LY_SUCCESS; |
| 318 | const char *c = qname->str; |
| 319 | int64_t i, j; |
| 320 | int8_t op_len, last_not = 0, checkversion = 0; |
| 321 | LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1; |
| 322 | uint8_t op; |
| 323 | struct iff_stack stack = {0, 0, NULL}; |
| 324 | struct lysp_feature *f; |
| 325 | |
| 326 | assert(c); |
| 327 | |
| 328 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 329 | for (i = j = 0; c[i]; i++) { |
| 330 | if (c[i] == '(') { |
| 331 | j++; |
| 332 | checkversion = 1; |
| 333 | continue; |
| 334 | } else if (c[i] == ')') { |
| 335 | j--; |
| 336 | continue; |
| 337 | } else if (isspace(c[i])) { |
| 338 | checkversion = 1; |
| 339 | continue; |
| 340 | } |
| 341 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 342 | if (!strncmp(&c[i], "not", op_len = ly_strlen_const("not")) || |
| 343 | !strncmp(&c[i], "and", op_len = ly_strlen_const("and")) || |
| 344 | !strncmp(&c[i], "or", op_len = ly_strlen_const("or"))) { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 345 | uint64_t spaces; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 346 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 347 | for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++) {} |
| 348 | if (c[i + op_len + spaces] == '\0') { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 349 | LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - unexpected end of expression.", qname->str); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 350 | return LY_EVALID; |
| 351 | } else if (!isspace(c[i + op_len])) { |
| 352 | /* feature name starting with the not/and/or */ |
| 353 | last_not = 0; |
| 354 | f_size++; |
| 355 | } else if (c[i] == 'n') { /* not operation */ |
| 356 | if (last_not) { |
| 357 | /* double not */ |
| 358 | expr_size = expr_size - 2; |
| 359 | last_not = 0; |
| 360 | } else { |
| 361 | last_not = 1; |
| 362 | } |
| 363 | } else { /* and, or */ |
| 364 | if (f_exp != f_size) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 365 | LOGVAL(ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 366 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", |
| 367 | qname->str, op_len, &c[i]); |
| 368 | return LY_EVALID; |
| 369 | } |
| 370 | f_exp++; |
| 371 | |
| 372 | /* not a not operation */ |
| 373 | last_not = 0; |
| 374 | } |
| 375 | i += op_len; |
| 376 | } else { |
| 377 | f_size++; |
| 378 | last_not = 0; |
| 379 | } |
| 380 | expr_size++; |
| 381 | |
| 382 | while (!isspace(c[i])) { |
| 383 | if (!c[i] || (c[i] == ')') || (c[i] == '(')) { |
| 384 | i--; |
| 385 | break; |
| 386 | } |
| 387 | i++; |
| 388 | } |
| 389 | } |
| 390 | if (j) { |
| 391 | /* not matching count of ( and ) */ |
Michal Vasko | 6a4ef77 | 2022-02-08 15:07:44 +0100 | [diff] [blame] | 392 | LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", |
| 393 | qname->str); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 394 | return LY_EVALID; |
| 395 | } |
| 396 | if (f_exp != f_size) { |
| 397 | /* features do not match the needed arguments for the logical operations */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 398 | LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 399 | "the required number of operands for the operations.", qname->str); |
| 400 | return LY_EVALID; |
| 401 | } |
| 402 | |
| 403 | if (checkversion || (expr_size > 1)) { |
| 404 | /* check that we have 1.1 module */ |
| 405 | if (qname->mod->version != LYS_VERSION_1_1) { |
Michal Vasko | 6a4ef77 | 2022-02-08 15:07:44 +0100 | [diff] [blame] | 406 | LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", |
| 407 | qname->str); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 408 | return LY_EVALID; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /* allocate the memory */ |
| 413 | LY_ARRAY_CREATE_RET(ctx, iff->features, f_size, LY_EMEM); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 414 | iff->expr = calloc((j = (expr_size / IFF_RECORDS_IN_BYTE) + ((expr_size % IFF_RECORDS_IN_BYTE) ? 1 : 0)), sizeof *iff->expr); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 415 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
Michal Vasko | 47dc105 | 2023-02-01 12:09:36 +0100 | [diff] [blame] | 416 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 417 | |
| 418 | stack.size = expr_size; |
| 419 | f_size--; expr_size--; /* used as indexes from now */ |
| 420 | |
| 421 | for (i--; i >= 0; i--) { |
| 422 | if (c[i] == ')') { |
| 423 | /* push it on stack */ |
| 424 | iff_stack_push(&stack, LYS_IFF_RP); |
| 425 | continue; |
| 426 | } else if (c[i] == '(') { |
| 427 | /* pop from the stack into result all operators until ) */ |
| 428 | while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 429 | iff_setop(iff->expr, op, expr_size--); |
| 430 | } |
| 431 | continue; |
| 432 | } else if (isspace(c[i])) { |
| 433 | continue; |
| 434 | } |
| 435 | |
| 436 | /* end of operator or operand -> find beginning and get what is it */ |
| 437 | j = i + 1; |
| 438 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 439 | i--; |
| 440 | } |
| 441 | i++; /* go back by one step */ |
| 442 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 443 | if (!strncmp(&c[i], "not", ly_strlen_const("not")) && isspace(c[i + ly_strlen_const("not")])) { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 444 | if (stack.index && (stack.stack[stack.index - 1] == LYS_IFF_NOT)) { |
| 445 | /* double not */ |
| 446 | iff_stack_pop(&stack); |
| 447 | } else { |
| 448 | /* not has the highest priority, so do not pop from the stack |
| 449 | * as in case of AND and OR */ |
| 450 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 451 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 452 | } else if (!strncmp(&c[i], "and", ly_strlen_const("and")) && isspace(c[i + ly_strlen_const("and")])) { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 453 | /* as for OR - pop from the stack all operators with the same or higher |
| 454 | * priority and store them to the result, then push the AND to the stack */ |
| 455 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 456 | op = iff_stack_pop(&stack); |
| 457 | iff_setop(iff->expr, op, expr_size--); |
| 458 | } |
| 459 | iff_stack_push(&stack, LYS_IFF_AND); |
| 460 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 461 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 462 | op = iff_stack_pop(&stack); |
| 463 | iff_setop(iff->expr, op, expr_size--); |
| 464 | } |
| 465 | iff_stack_push(&stack, LYS_IFF_OR); |
| 466 | } else { |
| 467 | /* feature name, length is j - i */ |
| 468 | |
| 469 | /* add it to the expression */ |
| 470 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 471 | |
| 472 | /* now get the link to the feature definition */ |
| 473 | f = lysp_feature_find(qname->mod, &c[i], j - i, 1); |
| 474 | if (!f) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 475 | LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", |
| 476 | qname->str, (int)(j - i), &c[i]); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 477 | rc = LY_EVALID; |
Michal Vasko | 47dc105 | 2023-02-01 12:09:36 +0100 | [diff] [blame] | 478 | goto cleanup; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 479 | } |
| 480 | iff->features[f_size] = f; |
| 481 | LY_ARRAY_INCREMENT(iff->features); |
| 482 | f_size--; |
| 483 | } |
| 484 | } |
| 485 | while (stack.index) { |
| 486 | op = iff_stack_pop(&stack); |
| 487 | iff_setop(iff->expr, op, expr_size--); |
| 488 | } |
| 489 | |
| 490 | if (++expr_size || ++f_size) { |
| 491 | /* not all expected operators and operands found */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 492 | LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - processing error.", qname->str); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 493 | rc = LY_EINT; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 494 | } |
| 495 | |
Michal Vasko | 47dc105 | 2023-02-01 12:09:36 +0100 | [diff] [blame] | 496 | cleanup: |
| 497 | if (rc) { |
| 498 | LY_ARRAY_FREE(iff->features); |
| 499 | iff->features = NULL; |
| 500 | free(iff->expr); |
| 501 | iff->expr = NULL; |
| 502 | } |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 503 | iff_stack_clean(&stack); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 504 | return rc; |
| 505 | } |
| 506 | |
| 507 | LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 508 | lys_eval_iffeatures(const struct ly_ctx *ctx, const struct lysp_qname *iffeatures, ly_bool *enabled) |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 509 | { |
| 510 | LY_ERR ret; |
Michal Vasko | c3f5573 | 2021-07-29 10:03:17 +0200 | [diff] [blame] | 511 | LY_ARRAY_COUNT_TYPE u; |
| 512 | struct lysc_iffeature iff; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 513 | struct lysf_ctx fctx = {.ctx = (struct ly_ctx *)ctx}; |
Michal Vasko | c3f5573 | 2021-07-29 10:03:17 +0200 | [diff] [blame] | 514 | |
| 515 | /* enabled by default */ |
| 516 | *enabled = 1; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 517 | |
| 518 | if (!iffeatures) { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 519 | return LY_SUCCESS; |
| 520 | } |
| 521 | |
Michal Vasko | c3f5573 | 2021-07-29 10:03:17 +0200 | [diff] [blame] | 522 | /* evaluate all if-feature conditions or until an unsatisfied one is found */ |
| 523 | LY_ARRAY_FOR(iffeatures, u) { |
| 524 | memset(&iff, 0, sizeof iff); |
| 525 | LY_CHECK_RET(lys_compile_iffeature(ctx, &iffeatures[u], &iff)); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 526 | |
Michal Vasko | c3f5573 | 2021-07-29 10:03:17 +0200 | [diff] [blame] | 527 | ret = lysc_iffeature_value(&iff); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 528 | lysc_iffeature_free(&fctx, &iff); |
Michal Vasko | c3f5573 | 2021-07-29 10:03:17 +0200 | [diff] [blame] | 529 | if (ret == LY_ENOT) { |
| 530 | *enabled = 0; |
| 531 | break; |
| 532 | } else if (ret) { |
| 533 | return ret; |
| 534 | } |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | return LY_SUCCESS; |
| 538 | } |
| 539 | |
Michal Vasko | 6a4ef77 | 2022-02-08 15:07:44 +0100 | [diff] [blame] | 540 | LY_ERR |
| 541 | lys_check_features(const struct lysp_module *pmod) |
Michal Vasko | 08c8b27 | 2020-11-24 18:11:30 +0100 | [diff] [blame] | 542 | { |
| 543 | LY_ERR r; |
| 544 | uint32_t i = 0; |
| 545 | struct lysp_feature *f = NULL; |
| 546 | |
| 547 | while ((f = lysp_feature_next(f, pmod, &i))) { |
| 548 | if (!(f->flags & LYS_FENABLED) || !f->iffeatures) { |
| 549 | /* disabled feature or no if-features to check */ |
| 550 | continue; |
| 551 | } |
| 552 | |
| 553 | assert(f->iffeatures_c); |
| 554 | r = lysc_iffeature_value(f->iffeatures_c); |
| 555 | if (r == LY_ENOT) { |
Michal Vasko | 6a4ef77 | 2022-02-08 15:07:44 +0100 | [diff] [blame] | 556 | LOGERR(pmod->mod->ctx, LY_EDENIED, "Feature \"%s\" cannot be enabled because its \"if-feature\" is not satisfied.", |
Michal Vasko | 08c8b27 | 2020-11-24 18:11:30 +0100 | [diff] [blame] | 557 | f->name); |
Michal Vasko | 6a4ef77 | 2022-02-08 15:07:44 +0100 | [diff] [blame] | 558 | return LY_EDENIED; |
Michal Vasko | 08c8b27 | 2020-11-24 18:11:30 +0100 | [diff] [blame] | 559 | } else if (r) { |
| 560 | return r; |
| 561 | } /* else if-feature satisfied */ |
| 562 | } |
| 563 | |
| 564 | return LY_SUCCESS; |
| 565 | } |
| 566 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 567 | LY_ERR |
Michal Vasko | 08c8b27 | 2020-11-24 18:11:30 +0100 | [diff] [blame] | 568 | lys_set_features(struct lysp_module *pmod, const char **features) |
| 569 | { |
| 570 | uint32_t i = 0, j; |
| 571 | struct lysp_feature *f = 0; |
| 572 | ly_bool change = 0; |
| 573 | |
| 574 | if (!features) { |
Radek Krejci | 2415f88 | 2021-01-20 16:27:09 +0100 | [diff] [blame] | 575 | /* do not touch the features */ |
| 576 | |
| 577 | } else if (!features[0]) { |
Michal Vasko | 08c8b27 | 2020-11-24 18:11:30 +0100 | [diff] [blame] | 578 | /* disable all the features */ |
| 579 | while ((f = lysp_feature_next(f, pmod, &i))) { |
| 580 | if (f->flags & LYS_FENABLED) { |
| 581 | f->flags &= ~LYS_FENABLED; |
| 582 | change = 1; |
| 583 | } |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 584 | } |
Michal Vasko | 08c8b27 | 2020-11-24 18:11:30 +0100 | [diff] [blame] | 585 | } else if (!strcmp(features[0], "*")) { |
| 586 | /* enable all the features */ |
| 587 | while ((f = lysp_feature_next(f, pmod, &i))) { |
| 588 | if (!(f->flags & LYS_FENABLED)) { |
| 589 | f->flags |= LYS_FENABLED; |
| 590 | change = 1; |
| 591 | } |
| 592 | } |
| 593 | } else { |
Michal Vasko | c8997c5 | 2021-08-23 15:38:34 +0200 | [diff] [blame] | 594 | /* check that all the features exist */ |
| 595 | for (j = 0; features[j]; ++j) { |
| 596 | if (!lysp_feature_find(pmod, features[j], strlen(features[j]), 0)) { |
| 597 | LOGERR(pmod->mod->ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", features[j], pmod->mod->name); |
| 598 | return LY_EINVAL; |
| 599 | } |
| 600 | } |
| 601 | |
Michal Vasko | 08c8b27 | 2020-11-24 18:11:30 +0100 | [diff] [blame] | 602 | /* enable specific features, disable the rest */ |
| 603 | while ((f = lysp_feature_next(f, pmod, &i))) { |
| 604 | for (j = 0; features[j]; ++j) { |
| 605 | if (!strcmp(f->name, features[j])) { |
| 606 | break; |
| 607 | } |
| 608 | } |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 609 | |
Michal Vasko | 08c8b27 | 2020-11-24 18:11:30 +0100 | [diff] [blame] | 610 | if (features[j] && !(f->flags & LYS_FENABLED)) { |
| 611 | /* enable */ |
| 612 | f->flags |= LYS_FENABLED; |
| 613 | change = 1; |
| 614 | } else if (!features[j] && (f->flags & LYS_FENABLED)) { |
| 615 | /* disable */ |
| 616 | f->flags &= ~LYS_FENABLED; |
| 617 | change = 1; |
| 618 | } |
| 619 | } |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 620 | } |
| 621 | |
Michal Vasko | 08c8b27 | 2020-11-24 18:11:30 +0100 | [diff] [blame] | 622 | if (!change) { |
| 623 | /* features already set correctly */ |
| 624 | return LY_EEXIST; |
| 625 | } |
| 626 | |
Michal Vasko | 6a4ef77 | 2022-02-08 15:07:44 +0100 | [diff] [blame] | 627 | return LY_SUCCESS; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | /** |
| 631 | * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement). |
| 632 | * |
| 633 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 634 | * |
| 635 | * @param[in] ctx Compile context for logging. |
| 636 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 637 | * being currently processed). |
| 638 | * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature) |
| 639 | * @return LY_SUCCESS if everything is ok. |
| 640 | * @return LY_EVALID if the feature references indirectly itself. |
| 641 | */ |
| 642 | static LY_ERR |
| 643 | lys_compile_feature_circular_check(const struct ly_ctx *ctx, struct lysp_feature *feature, struct lysp_feature **depfeatures) |
| 644 | { |
| 645 | LY_ERR ret = LY_SUCCESS; |
| 646 | LY_ARRAY_COUNT_TYPE u, v; |
| 647 | struct ly_set recursion = {0}; |
| 648 | struct lysp_feature *drv; |
| 649 | |
| 650 | if (!depfeatures) { |
| 651 | return LY_SUCCESS; |
| 652 | } |
| 653 | |
| 654 | for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) { |
| 655 | if (feature == depfeatures[u]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 656 | LOGVAL(ctx, LYVE_REFERENCE, "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 657 | ret = LY_EVALID; |
| 658 | goto cleanup; |
| 659 | } |
| 660 | ret = ly_set_add(&recursion, depfeatures[u], 0, NULL); |
| 661 | LY_CHECK_GOTO(ret, cleanup); |
| 662 | } |
| 663 | |
| 664 | for (v = 0; v < recursion.count; ++v) { |
| 665 | drv = recursion.objs[v]; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 666 | for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) { |
| 667 | if (feature == drv->depfeatures[u]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 668 | LOGVAL(ctx, LYVE_REFERENCE, "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 669 | ret = LY_EVALID; |
| 670 | goto cleanup; |
| 671 | } |
| 672 | ly_set_add(&recursion, drv->depfeatures[u], 0, NULL); |
| 673 | LY_CHECK_GOTO(ret, cleanup); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | cleanup: |
| 678 | ly_set_erase(&recursion, NULL); |
| 679 | return ret; |
| 680 | } |
| 681 | |
| 682 | LY_ERR |
| 683 | lys_compile_feature_iffeatures(struct lysp_module *pmod) |
| 684 | { |
| 685 | LY_ARRAY_COUNT_TYPE u, v; |
| 686 | struct lysp_feature *f = NULL, **df; |
| 687 | uint32_t idx = 0; |
| 688 | |
| 689 | while ((f = lysp_feature_next(f, pmod, &idx))) { |
| 690 | if (!f->iffeatures) { |
| 691 | continue; |
| 692 | } |
| 693 | |
| 694 | /* compile if-features */ |
| 695 | LY_ARRAY_CREATE_RET(pmod->mod->ctx, f->iffeatures_c, LY_ARRAY_COUNT(f->iffeatures), LY_EMEM); |
| 696 | LY_ARRAY_FOR(f->iffeatures, u) { |
| 697 | LY_ARRAY_INCREMENT(f->iffeatures_c); |
| 698 | LY_CHECK_RET(lys_compile_iffeature(pmod->mod->ctx, &(f->iffeatures)[u], &(f->iffeatures_c)[u])); |
| 699 | } |
| 700 | LY_ARRAY_FOR(f->iffeatures_c, u) { |
| 701 | LY_ARRAY_FOR(f->iffeatures_c[u].features, v) { |
| 702 | /* check for circular dependency - direct reference first,... */ |
| 703 | if (f == f->iffeatures_c[u].features[v]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 704 | LOGVAL(pmod->mod->ctx, LYVE_REFERENCE, "Feature \"%s\" is referenced from itself.", f->name); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 705 | return LY_EVALID; |
| 706 | } |
| 707 | /* ... and indirect circular reference */ |
| 708 | LY_CHECK_RET(lys_compile_feature_circular_check(pmod->mod->ctx, f->iffeatures_c[u].features[v], f->depfeatures)); |
| 709 | |
| 710 | /* add itself into the dependants list */ |
| 711 | LY_ARRAY_NEW_RET(pmod->mod->ctx, f->iffeatures_c[u].features[v]->depfeatures, df, LY_EMEM); |
| 712 | *df = f; |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | return LY_SUCCESS; |
| 718 | } |