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