blob: 82bc9eaf4bfc09640930cad72817fab1c3c2b6fc [file] [log] [blame]
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001/**
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 Vasko7b1ad1a2020-11-02 15:41:27 +010021#include <stdint.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010025
26#include "common.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010027#include "log.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010028#include "set.h"
29#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010030#include "tree_edit.h"
Radek Krejci47fab892020-11-05 17:02:41 +010031#include "tree_schema.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010032#include "tree_schema_internal.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010033
Radek Krejcif13b87b2020-12-01 22:02:17 +010034#define IFF_RECORDS_IN_BYTE 4
35#define IFF_RECORD_BITS 2
36#define IFF_RECORD_MASK 0x3
37
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010038uint8_t
39lysc_iff_getop(uint8_t *list, size_t pos)
40{
41 uint8_t *item;
Radek Krejcif13b87b2020-12-01 22:02:17 +010042 uint8_t mask = IFF_RECORD_MASK, result;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010043
Radek Krejcif13b87b2020-12-01 22:02:17 +010044 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 Vasko7b1ad1a2020-11-02 15:41:27 +010047}
48
49static LY_ERR
50lysc_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
87API LY_ERR
88lysc_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
100API struct lysp_feature *
101lysp_feature_next(const struct lysp_feature *last, const struct lysp_module *pmod, uint32_t *idx)
102{
103 struct lysp_feature *features;
104
105 if (!*idx) {
106 /* module features */
107 features = pmod->features;
Radek Krejcic7d13e32020-12-09 12:32:24 +0100108 } else if ((*idx - 1) < LY_ARRAY_COUNT(pmod->includes)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100109 /* submodule features */
110 features = pmod->includes[*idx - 1].submodule->features;
111 } else {
112 /* no more features */
113 return NULL;
114 }
115
116 /* get the next feature */
117 if (features && (!last || (&features[LY_ARRAY_COUNT(features) - 1] != last))) {
118 return !last ? &features[0] : (struct lysp_feature *)last + 1;
119 }
120
121 /* no more features in current (sub)module */
122 ++(*idx);
123 return lysp_feature_next(NULL, pmod, idx);
124}
125
126/**
127 * @brief Find a feature of the given name and referenced in the given module.
128 *
129 * @param[in] pmod Module where the feature was referenced (used to resolve prefix of the feature).
130 * @param[in] name Name of the feature including possible prefix.
131 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
132 * @param[in] prefixed Whether the feature name can be prefixed.
133 * @return Pointer to the feature structure if found, NULL otherwise.
134 */
135static struct lysp_feature *
136lysp_feature_find(const struct lysp_module *pmod, const char *name, size_t len, ly_bool prefixed)
137{
138 const struct lys_module *mod;
139 const char *ptr;
140 struct lysp_feature *f = NULL;
141 uint32_t idx = 0;
142
143 assert(pmod);
144
145 if (prefixed && (ptr = ly_strnchr(name, ':', len))) {
146 /* we have a prefixed feature */
Radek Krejci8df109d2021-04-23 12:19:08 +0200147 mod = ly_resolve_prefix(pmod->mod->ctx, name, ptr - name, LY_VALUE_SCHEMA, (void *)pmod);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100148 LY_CHECK_RET(!mod, NULL);
149
150 pmod = mod->parsed;
151 len = len - (ptr - name) - 1;
152 name = ptr + 1;
153 }
154
Michal Vaskoecd94572020-12-03 14:15:58 +0100155 /* feature without prefix, look in main module and all submodules */
156 if (pmod->is_submod) {
157 pmod = pmod->mod->parsed;
158 }
159
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100160 /* we have the correct module, get the feature */
161 while ((f = lysp_feature_next(f, pmod, &idx))) {
162 if (!ly_strncmp(f->name, name, len)) {
163 return f;
164 }
165 }
166
167 return NULL;
168}
169
170API LY_ERR
171lys_feature_value(const struct lys_module *module, const char *feature)
172{
173 const struct lysp_feature *f;
174
175 LY_CHECK_ARG_RET(NULL, module, module->parsed, feature, LY_EINVAL);
176
177 /* search for the specified feature */
178 f = lysp_feature_find(module->parsed, feature, strlen(feature), 0);
179 LY_CHECK_RET(!f, LY_ENOTFOUND);
180
181 /* feature disabled */
182 if (!(f->flags & LYS_FENABLED)) {
183 return LY_ENOT;
184 }
185
186 /* feature enabled */
187 return LY_SUCCESS;
188}
189
190/**
191 * @brief Stack for processing if-feature expressions.
192 */
193struct iff_stack {
194 size_t size; /**< number of items in the stack */
195 size_t index; /**< first empty item */
196 uint8_t *stack; /**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
197};
Radek Krejcif13b87b2020-12-01 22:02:17 +0100198#define IFF_STACK_SIZE_STEP 4
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100199
200/**
201 * @brief Add @ref ifftokens into the stack.
202 * @param[in] stack The if-feature stack to use.
203 * @param[in] value One of the @ref ifftokens to store in the stack.
204 * @return LY_EMEM in case of memory allocation error
205 * @return LY_ESUCCESS if the value successfully stored.
206 */
207static LY_ERR
208iff_stack_push(struct iff_stack *stack, uint8_t value)
209{
210 if (stack->index == stack->size) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100211 stack->size += IFF_STACK_SIZE_STEP;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100212 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
213 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
214 }
215 stack->stack[stack->index++] = value;
216 return LY_SUCCESS;
217}
218
219/**
220 * @brief Get (and remove) the last item form the stack.
221 * @param[in] stack The if-feature stack to use.
222 * @return The value from the top of the stack.
223 */
224static uint8_t
225iff_stack_pop(struct iff_stack *stack)
226{
227 assert(stack && stack->index);
228
229 stack->index--;
230 return stack->stack[stack->index];
231}
232
233/**
234 * @brief Clean up the stack.
235 * @param[in] stack The if-feature stack to use.
236 */
237static void
238iff_stack_clean(struct iff_stack *stack)
239{
240 stack->size = 0;
241 free(stack->stack);
242}
243
244/**
245 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
246 * (libyang format of the if-feature expression).
247 * @param[in,out] list The 2bits array to modify.
248 * @param[in] op The operand (@ref ifftokens) to store.
249 * @param[in] pos Position (0-based) where to store the given @p op.
250 */
251static void
252iff_setop(uint8_t *list, uint8_t op, size_t pos)
253{
254 uint8_t *item;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100255 uint8_t mask = IFF_RECORD_MASK;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100256
Radek Krejcif13b87b2020-12-01 22:02:17 +0100257 assert(op <= IFF_RECORD_MASK); /* max 2 bits */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100258
Radek Krejcif13b87b2020-12-01 22:02:17 +0100259 item = &list[pos / IFF_RECORDS_IN_BYTE];
260 mask = mask << IFF_RECORD_BITS * (pos % IFF_RECORDS_IN_BYTE);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100261 *item = (*item) & ~mask;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100262 *item = (*item) | (op << IFF_RECORD_BITS * (pos % IFF_RECORDS_IN_BYTE));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100263}
264
265#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
266#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
267
268static LY_ERR
269lys_compile_iffeature(const struct ly_ctx *ctx, struct lysp_qname *qname, struct lysc_iffeature *iff)
270{
271 LY_ERR rc = LY_SUCCESS;
272 const char *c = qname->str;
273 int64_t i, j;
274 int8_t op_len, last_not = 0, checkversion = 0;
275 LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1;
276 uint8_t op;
277 struct iff_stack stack = {0, 0, NULL};
278 struct lysp_feature *f;
279
280 assert(c);
281
282 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
283 for (i = j = 0; c[i]; i++) {
284 if (c[i] == '(') {
285 j++;
286 checkversion = 1;
287 continue;
288 } else if (c[i] == ')') {
289 j--;
290 continue;
291 } else if (isspace(c[i])) {
292 checkversion = 1;
293 continue;
294 }
295
Radek Krejcif13b87b2020-12-01 22:02:17 +0100296 if (!strncmp(&c[i], "not", op_len = ly_strlen_const("not")) ||
297 !strncmp(&c[i], "and", op_len = ly_strlen_const("and")) ||
298 !strncmp(&c[i], "or", op_len = ly_strlen_const("or"))) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100299 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') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100302 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - unexpected end of expression.", qname->str);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100303 return LY_EVALID;
304 } else if (!isspace(c[i + op_len])) {
305 /* feature name starting with the not/and/or */
306 last_not = 0;
307 f_size++;
308 } else if (c[i] == 'n') { /* not operation */
309 if (last_not) {
310 /* double not */
311 expr_size = expr_size - 2;
312 last_not = 0;
313 } else {
314 last_not = 1;
315 }
316 } else { /* and, or */
317 if (f_exp != f_size) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100318 LOGVAL(ctx, LYVE_SYNTAX_YANG,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100319 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.",
320 qname->str, op_len, &c[i]);
321 return LY_EVALID;
322 }
323 f_exp++;
324
325 /* not a not operation */
326 last_not = 0;
327 }
328 i += op_len;
329 } else {
330 f_size++;
331 last_not = 0;
332 }
333 expr_size++;
334
335 while (!isspace(c[i])) {
336 if (!c[i] || (c[i] == ')') || (c[i] == '(')) {
337 i--;
338 break;
339 }
340 i++;
341 }
342 }
343 if (j) {
344 /* not matching count of ( and ) */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100345 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", qname->str);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100346 return LY_EVALID;
347 }
348 if (f_exp != f_size) {
349 /* features do not match the needed arguments for the logical operations */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100350 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - number of features in expression does not match "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100351 "the required number of operands for the operations.", qname->str);
352 return LY_EVALID;
353 }
354
355 if (checkversion || (expr_size > 1)) {
356 /* check that we have 1.1 module */
357 if (qname->mod->version != LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100358 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", qname->str);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100359 return LY_EVALID;
360 }
361 }
362
363 /* allocate the memory */
364 LY_ARRAY_CREATE_RET(ctx, iff->features, f_size, LY_EMEM);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100365 iff->expr = calloc((j = (expr_size / IFF_RECORDS_IN_BYTE) + ((expr_size % IFF_RECORDS_IN_BYTE) ? 1 : 0)), sizeof *iff->expr);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100366 stack.stack = malloc(expr_size * sizeof *stack.stack);
367 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx); rc = LY_EMEM, error);
368
369 stack.size = expr_size;
370 f_size--; expr_size--; /* used as indexes from now */
371
372 for (i--; i >= 0; i--) {
373 if (c[i] == ')') {
374 /* push it on stack */
375 iff_stack_push(&stack, LYS_IFF_RP);
376 continue;
377 } else if (c[i] == '(') {
378 /* pop from the stack into result all operators until ) */
379 while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
380 iff_setop(iff->expr, op, expr_size--);
381 }
382 continue;
383 } else if (isspace(c[i])) {
384 continue;
385 }
386
387 /* end of operator or operand -> find beginning and get what is it */
388 j = i + 1;
389 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
390 i--;
391 }
392 i++; /* go back by one step */
393
Radek Krejcif13b87b2020-12-01 22:02:17 +0100394 if (!strncmp(&c[i], "not", ly_strlen_const("not")) && isspace(c[i + ly_strlen_const("not")])) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100395 if (stack.index && (stack.stack[stack.index - 1] == LYS_IFF_NOT)) {
396 /* double not */
397 iff_stack_pop(&stack);
398 } else {
399 /* not has the highest priority, so do not pop from the stack
400 * as in case of AND and OR */
401 iff_stack_push(&stack, LYS_IFF_NOT);
402 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100403 } else if (!strncmp(&c[i], "and", ly_strlen_const("and")) && isspace(c[i + ly_strlen_const("and")])) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100404 /* as for OR - pop from the stack all operators with the same or higher
405 * priority and store them to the result, then push the AND to the stack */
406 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
407 op = iff_stack_pop(&stack);
408 iff_setop(iff->expr, op, expr_size--);
409 }
410 iff_stack_push(&stack, LYS_IFF_AND);
411 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
412 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
413 op = iff_stack_pop(&stack);
414 iff_setop(iff->expr, op, expr_size--);
415 }
416 iff_stack_push(&stack, LYS_IFF_OR);
417 } else {
418 /* feature name, length is j - i */
419
420 /* add it to the expression */
421 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
422
423 /* now get the link to the feature definition */
424 f = lysp_feature_find(qname->mod, &c[i], j - i, 1);
425 if (!f) {
Radek Krejci422afb12021-03-04 16:38:16 +0100426 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".",
427 qname->str, (int)(j - i), &c[i]);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100428 rc = LY_EVALID;
429 goto error;
430 }
431 iff->features[f_size] = f;
432 LY_ARRAY_INCREMENT(iff->features);
433 f_size--;
434 }
435 }
436 while (stack.index) {
437 op = iff_stack_pop(&stack);
438 iff_setop(iff->expr, op, expr_size--);
439 }
440
441 if (++expr_size || ++f_size) {
442 /* not all expected operators and operands found */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100443 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - processing error.", qname->str);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100444 rc = LY_EINT;
445 } else {
446 rc = LY_SUCCESS;
447 }
448
449error:
450 /* cleanup */
451 iff_stack_clean(&stack);
452
453 return rc;
454}
455
456LY_ERR
457lys_eval_iffeatures(const struct ly_ctx *ctx, struct lysp_qname *iffeatures, ly_bool *enabled)
458{
459 LY_ERR ret;
460 struct lysc_iffeature iff = {0};
461
462 if (!iffeatures) {
463 *enabled = 1;
464 return LY_SUCCESS;
465 }
466
467 LY_CHECK_RET(lys_compile_iffeature(ctx, iffeatures, &iff));
468
469 ret = lysc_iffeature_value(&iff);
470 lysc_iffeature_free((struct ly_ctx *)ctx, &iff);
471 if (ret == LY_ENOT) {
472 *enabled = 0;
473 } else if (ret) {
474 return ret;
475 } else {
476 *enabled = 1;
477 }
478
479 return LY_SUCCESS;
480}
481
Michal Vasko08c8b272020-11-24 18:11:30 +0100482/**
483 * @brief Check whether all enabled features have their if-features satisfied.
484 * Enabled features with false if-features are disabled with a warning.
485 *
486 * @param[in] pmod Parsed module features to check.
487 * @return LY_ERR value.
488 */
489static LY_ERR
490lys_check_features(struct lysp_module *pmod)
491{
492 LY_ERR r;
493 uint32_t i = 0;
494 struct lysp_feature *f = NULL;
495
496 while ((f = lysp_feature_next(f, pmod, &i))) {
497 if (!(f->flags & LYS_FENABLED) || !f->iffeatures) {
498 /* disabled feature or no if-features to check */
499 continue;
500 }
501
502 assert(f->iffeatures_c);
503 r = lysc_iffeature_value(f->iffeatures_c);
504 if (r == LY_ENOT) {
505 LOGWRN(pmod->mod->ctx, "Feature \"%s\" cannot be enabled because its \"if-feature\" is not satisfied.",
506 f->name);
507
508 /* disable feature and re-evaluate all the feature if-features again */
509 f->flags &= ~LYS_FENABLED;
510 return lys_check_features(pmod);
511 } else if (r) {
512 return r;
513 } /* else if-feature satisfied */
514 }
515
516 return LY_SUCCESS;
517}
518
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100519LY_ERR
Michal Vasko08c8b272020-11-24 18:11:30 +0100520lys_set_features(struct lysp_module *pmod, const char **features)
521{
522 uint32_t i = 0, j;
523 struct lysp_feature *f = 0;
524 ly_bool change = 0;
525
526 if (!features) {
Radek Krejci2415f882021-01-20 16:27:09 +0100527 /* do not touch the features */
528
529 } else if (!features[0]) {
Michal Vasko08c8b272020-11-24 18:11:30 +0100530 /* disable all the features */
531 while ((f = lysp_feature_next(f, pmod, &i))) {
532 if (f->flags & LYS_FENABLED) {
533 f->flags &= ~LYS_FENABLED;
534 change = 1;
535 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100536 }
Michal Vasko08c8b272020-11-24 18:11:30 +0100537 } else if (!strcmp(features[0], "*")) {
538 /* enable all the features */
539 while ((f = lysp_feature_next(f, pmod, &i))) {
540 if (!(f->flags & LYS_FENABLED)) {
541 f->flags |= LYS_FENABLED;
542 change = 1;
543 }
544 }
545 } else {
546 /* enable specific features, disable the rest */
547 while ((f = lysp_feature_next(f, pmod, &i))) {
548 for (j = 0; features[j]; ++j) {
549 if (!strcmp(f->name, features[j])) {
550 break;
551 }
552 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100553
Michal Vasko08c8b272020-11-24 18:11:30 +0100554 if (features[j] && !(f->flags & LYS_FENABLED)) {
555 /* enable */
556 f->flags |= LYS_FENABLED;
557 change = 1;
558 } else if (!features[j] && (f->flags & LYS_FENABLED)) {
559 /* disable */
560 f->flags &= ~LYS_FENABLED;
561 change = 1;
562 }
563 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100564 }
565
Michal Vasko08c8b272020-11-24 18:11:30 +0100566 if (!change) {
567 /* features already set correctly */
568 return LY_EEXIST;
569 }
570
571 /* check final features if-feature state */
572 return lys_check_features(pmod);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100573}
574
575/**
576 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
577 *
578 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
579 *
580 * @param[in] ctx Compile context for logging.
581 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
582 * being currently processed).
583 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
584 * @return LY_SUCCESS if everything is ok.
585 * @return LY_EVALID if the feature references indirectly itself.
586 */
587static LY_ERR
588lys_compile_feature_circular_check(const struct ly_ctx *ctx, struct lysp_feature *feature, struct lysp_feature **depfeatures)
589{
590 LY_ERR ret = LY_SUCCESS;
591 LY_ARRAY_COUNT_TYPE u, v;
592 struct ly_set recursion = {0};
593 struct lysp_feature *drv;
594
595 if (!depfeatures) {
596 return LY_SUCCESS;
597 }
598
599 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
600 if (feature == depfeatures[u]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100601 LOGVAL(ctx, LYVE_REFERENCE, "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100602 ret = LY_EVALID;
603 goto cleanup;
604 }
605 ret = ly_set_add(&recursion, depfeatures[u], 0, NULL);
606 LY_CHECK_GOTO(ret, cleanup);
607 }
608
609 for (v = 0; v < recursion.count; ++v) {
610 drv = recursion.objs[v];
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100611 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
612 if (feature == drv->depfeatures[u]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100613 LOGVAL(ctx, LYVE_REFERENCE, "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100614 ret = LY_EVALID;
615 goto cleanup;
616 }
617 ly_set_add(&recursion, drv->depfeatures[u], 0, NULL);
618 LY_CHECK_GOTO(ret, cleanup);
619 }
620 }
621
622cleanup:
623 ly_set_erase(&recursion, NULL);
624 return ret;
625}
626
627LY_ERR
628lys_compile_feature_iffeatures(struct lysp_module *pmod)
629{
630 LY_ARRAY_COUNT_TYPE u, v;
631 struct lysp_feature *f = NULL, **df;
632 uint32_t idx = 0;
633
634 while ((f = lysp_feature_next(f, pmod, &idx))) {
635 if (!f->iffeatures) {
636 continue;
637 }
638
639 /* compile if-features */
640 LY_ARRAY_CREATE_RET(pmod->mod->ctx, f->iffeatures_c, LY_ARRAY_COUNT(f->iffeatures), LY_EMEM);
641 LY_ARRAY_FOR(f->iffeatures, u) {
642 LY_ARRAY_INCREMENT(f->iffeatures_c);
643 LY_CHECK_RET(lys_compile_iffeature(pmod->mod->ctx, &(f->iffeatures)[u], &(f->iffeatures_c)[u]));
644 }
645 LY_ARRAY_FOR(f->iffeatures_c, u) {
646 LY_ARRAY_FOR(f->iffeatures_c[u].features, v) {
647 /* check for circular dependency - direct reference first,... */
648 if (f == f->iffeatures_c[u].features[v]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100649 LOGVAL(pmod->mod->ctx, LYVE_REFERENCE, "Feature \"%s\" is referenced from itself.", f->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100650 return LY_EVALID;
651 }
652 /* ... and indirect circular reference */
653 LY_CHECK_RET(lys_compile_feature_circular_check(pmod->mod->ctx, f->iffeatures_c[u].features[v], f->depfeatures));
654
655 /* add itself into the dependants list */
656 LY_ARRAY_NEW_RET(pmod->mod->ctx, f->iffeatures_c[u].features[v]->depfeatures, df, LY_EMEM);
657 *df = f;
658 }
659 }
660 }
661
662 return LY_SUCCESS;
663}