build: scripts/config - update to kconfig-v6.6.16
[openwrt/openwrt.git] / scripts / config / symbol.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
4 */
5
6 #include <sys/types.h>
7 #include <ctype.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <regex.h>
11
12 #include "lkc.h"
13
14 struct symbol symbol_yes = {
15 .name = "y",
16 .curr = { "y", yes },
17 .flags = SYMBOL_CONST|SYMBOL_VALID,
18 };
19
20 struct symbol symbol_mod = {
21 .name = "m",
22 .curr = { "m", mod },
23 .flags = SYMBOL_CONST|SYMBOL_VALID,
24 };
25
26 struct symbol symbol_no = {
27 .name = "n",
28 .curr = { "n", no },
29 .flags = SYMBOL_CONST|SYMBOL_VALID,
30 };
31
32 static struct symbol symbol_empty = {
33 .name = "",
34 .curr = { "", no },
35 .flags = SYMBOL_VALID,
36 };
37
38 struct symbol *modules_sym;
39 static tristate modules_val;
40 int recursive_is_error;
41
42 enum symbol_type sym_get_type(struct symbol *sym)
43 {
44 enum symbol_type type = sym->type;
45
46 if (type == S_TRISTATE) {
47 if (sym_is_choice_value(sym) && sym->visible == yes)
48 type = S_BOOLEAN;
49 else if (modules_val == no)
50 type = S_BOOLEAN;
51 }
52 return type;
53 }
54
55 const char *sym_type_name(enum symbol_type type)
56 {
57 switch (type) {
58 case S_BOOLEAN:
59 return "bool";
60 case S_TRISTATE:
61 return "tristate";
62 case S_INT:
63 return "integer";
64 case S_HEX:
65 return "hex";
66 case S_STRING:
67 return "string";
68 case S_UNKNOWN:
69 return "unknown";
70 }
71 return "???";
72 }
73
74 struct property *sym_get_choice_prop(struct symbol *sym)
75 {
76 struct property *prop;
77
78 for_all_choices(sym, prop)
79 return prop;
80 return NULL;
81 }
82
83 static struct property *sym_get_default_prop(struct symbol *sym)
84 {
85 struct property *prop;
86
87 for_all_defaults(sym, prop) {
88 prop->visible.tri = expr_calc_value(prop->visible.expr);
89 if (prop->visible.tri != no)
90 return prop;
91 }
92 return NULL;
93 }
94
95 struct property *sym_get_range_prop(struct symbol *sym)
96 {
97 struct property *prop;
98
99 for_all_properties(sym, prop, P_RANGE) {
100 prop->visible.tri = expr_calc_value(prop->visible.expr);
101 if (prop->visible.tri != no)
102 return prop;
103 }
104 return NULL;
105 }
106
107 static long long sym_get_range_val(struct symbol *sym, int base)
108 {
109 sym_calc_value(sym);
110 switch (sym->type) {
111 case S_INT:
112 base = 10;
113 break;
114 case S_HEX:
115 base = 16;
116 break;
117 default:
118 break;
119 }
120 return strtoll(sym->curr.val, NULL, base);
121 }
122
123 static void sym_validate_range(struct symbol *sym)
124 {
125 struct property *prop;
126 struct symbol *range_sym;
127 int base;
128 long long val, val2;
129
130 switch (sym->type) {
131 case S_INT:
132 base = 10;
133 break;
134 case S_HEX:
135 base = 16;
136 break;
137 default:
138 return;
139 }
140 prop = sym_get_range_prop(sym);
141 if (!prop)
142 return;
143 val = strtoll(sym->curr.val, NULL, base);
144 range_sym = prop->expr->left.sym;
145 val2 = sym_get_range_val(range_sym, base);
146 if (val >= val2) {
147 range_sym = prop->expr->right.sym;
148 val2 = sym_get_range_val(range_sym, base);
149 if (val <= val2)
150 return;
151 }
152 sym->curr.val = range_sym->curr.val;
153 }
154
155 static void sym_set_changed(struct symbol *sym)
156 {
157 struct property *prop;
158
159 sym->flags |= SYMBOL_CHANGED;
160 for (prop = sym->prop; prop; prop = prop->next) {
161 if (prop->menu)
162 prop->menu->flags |= MENU_CHANGED;
163 }
164 }
165
166 static void sym_set_all_changed(void)
167 {
168 struct symbol *sym;
169 int i;
170
171 for_all_symbols(i, sym)
172 sym_set_changed(sym);
173 }
174
175 static void sym_calc_visibility(struct symbol *sym)
176 {
177 struct property *prop;
178 struct symbol *choice_sym = NULL;
179 tristate tri;
180
181 /* any prompt visible? */
182 tri = no;
183
184 if (sym_is_choice_value(sym))
185 choice_sym = prop_get_symbol(sym_get_choice_prop(sym));
186
187 for_all_prompts(sym, prop) {
188 prop->visible.tri = expr_calc_value(prop->visible.expr);
189 /*
190 * Tristate choice_values with visibility 'mod' are
191 * not visible if the corresponding choice's value is
192 * 'yes'.
193 */
194 if (choice_sym && sym->type == S_TRISTATE &&
195 prop->visible.tri == mod && choice_sym->curr.tri == yes)
196 prop->visible.tri = no;
197
198 tri = EXPR_OR(tri, prop->visible.tri);
199 }
200 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
201 tri = yes;
202 if (sym->visible != tri) {
203 sym->visible = tri;
204 sym_set_changed(sym);
205 }
206 if (sym_is_choice_value(sym))
207 return;
208 /* defaulting to "yes" if no explicit "depends on" are given */
209 tri = yes;
210 if (sym->dir_dep.expr)
211 tri = expr_calc_value(sym->dir_dep.expr);
212 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
213 tri = yes;
214 if (sym->dir_dep.tri != tri) {
215 sym->dir_dep.tri = tri;
216 sym_set_changed(sym);
217 }
218 tri = no;
219 if (sym->rev_dep.expr)
220 tri = expr_calc_value(sym->rev_dep.expr);
221 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
222 tri = yes;
223 if (sym->rev_dep.tri != tri) {
224 sym->rev_dep.tri = tri;
225 sym_set_changed(sym);
226 }
227 tri = no;
228 if (sym->implied.expr)
229 tri = expr_calc_value(sym->implied.expr);
230 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
231 tri = yes;
232 if (sym->implied.tri != tri) {
233 sym->implied.tri = tri;
234 sym_set_changed(sym);
235 }
236 }
237
238 /*
239 * Find the default symbol for a choice.
240 * First try the default values for the choice symbol
241 * Next locate the first visible choice value
242 * Return NULL if none was found
243 */
244 struct symbol *sym_choice_default(struct symbol *sym)
245 {
246 struct symbol *def_sym;
247 struct property *prop;
248 struct expr *e;
249
250 /* any of the defaults visible? */
251 for_all_defaults(sym, prop) {
252 prop->visible.tri = expr_calc_value(prop->visible.expr);
253 if (prop->visible.tri == no)
254 continue;
255 def_sym = prop_get_symbol(prop);
256 if (def_sym->visible != no)
257 return def_sym;
258 }
259
260 /* just get the first visible value */
261 prop = sym_get_choice_prop(sym);
262 expr_list_for_each_sym(prop->expr, e, def_sym)
263 if (def_sym->visible != no)
264 return def_sym;
265
266 /* failed to locate any defaults */
267 return NULL;
268 }
269
270 static struct symbol *sym_calc_choice(struct symbol *sym)
271 {
272 struct symbol *def_sym;
273 struct property *prop;
274 struct expr *e;
275 int flags;
276
277 /* first calculate all choice values' visibilities */
278 flags = sym->flags;
279 prop = sym_get_choice_prop(sym);
280 expr_list_for_each_sym(prop->expr, e, def_sym) {
281 sym_calc_visibility(def_sym);
282 if (def_sym->visible != no)
283 flags &= def_sym->flags;
284 }
285
286 sym->flags &= flags | ~SYMBOL_DEF_USER;
287
288 /* is the user choice visible? */
289 def_sym = sym->def[S_DEF_USER].val;
290 if (def_sym && def_sym->visible != no)
291 return def_sym;
292
293 def_sym = sym_choice_default(sym);
294
295 if (def_sym == NULL)
296 /* no choice? reset tristate value */
297 sym->curr.tri = no;
298
299 return def_sym;
300 }
301
302 void sym_calc_value(struct symbol *sym)
303 {
304 struct symbol_value newval, oldval;
305 struct property *prop;
306 struct expr *e;
307
308 if (!sym)
309 return;
310
311 if (sym->flags & SYMBOL_VALID)
312 return;
313
314 if (sym_is_choice_value(sym) &&
315 sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES) {
316 sym->flags &= ~SYMBOL_NEED_SET_CHOICE_VALUES;
317 prop = sym_get_choice_prop(sym);
318 sym_calc_value(prop_get_symbol(prop));
319 }
320
321 sym->flags |= SYMBOL_VALID;
322
323 oldval = sym->curr;
324
325 switch (sym->type) {
326 case S_INT:
327 case S_HEX:
328 case S_STRING:
329 newval = symbol_empty.curr;
330 break;
331 case S_BOOLEAN:
332 case S_TRISTATE:
333 newval = symbol_no.curr;
334 break;
335 default:
336 sym->curr.val = sym->name;
337 sym->curr.tri = no;
338 return;
339 }
340 sym->flags &= ~SYMBOL_WRITE;
341
342 sym_calc_visibility(sym);
343
344 if (sym->visible != no)
345 sym->flags |= SYMBOL_WRITE;
346
347 /* set default if recursively called */
348 sym->curr = newval;
349
350 switch (sym_get_type(sym)) {
351 case S_BOOLEAN:
352 case S_TRISTATE:
353 if (sym_is_choice_value(sym) && sym->visible == yes) {
354 prop = sym_get_choice_prop(sym);
355 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
356 } else {
357 if (sym->visible != no) {
358 /* if the symbol is visible use the user value
359 * if available, otherwise try the default value
360 */
361 if (sym_has_value(sym)) {
362 newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
363 sym->visible);
364 goto calc_newval;
365 }
366 }
367 if (sym->rev_dep.tri != no)
368 sym->flags |= SYMBOL_WRITE;
369 if (!sym_is_choice(sym)) {
370 prop = sym_get_default_prop(sym);
371 if (prop) {
372 sym->flags |= SYMBOL_WRITE;
373 newval.tri = EXPR_AND(expr_calc_value(prop->expr),
374 prop->visible.tri);
375 }
376 if (sym->implied.tri != no) {
377 sym->flags |= SYMBOL_WRITE;
378 newval.tri = EXPR_OR(newval.tri, sym->implied.tri);
379 newval.tri = EXPR_AND(newval.tri,
380 sym->dir_dep.tri);
381 }
382 }
383 calc_newval:
384 if (sym->dir_dep.tri == no && sym->rev_dep.tri != no)
385 newval.tri = no;
386 else
387 newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
388 }
389 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
390 newval.tri = yes;
391 break;
392 case S_STRING:
393 case S_HEX:
394 case S_INT:
395 if (sym->visible != no && sym_has_value(sym)) {
396 newval.val = sym->def[S_DEF_USER].val;
397 break;
398 }
399 prop = sym_get_default_prop(sym);
400 if (prop) {
401 struct symbol *ds = prop_get_symbol(prop);
402 if (ds) {
403 sym->flags |= SYMBOL_WRITE;
404 sym_calc_value(ds);
405 newval.val = ds->curr.val;
406 }
407 }
408 break;
409 default:
410 ;
411 }
412
413 sym->curr = newval;
414 if (sym_is_choice(sym) && newval.tri == yes)
415 sym->curr.val = sym_calc_choice(sym);
416 sym_validate_range(sym);
417
418 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
419 sym_set_changed(sym);
420 if (modules_sym == sym) {
421 sym_set_all_changed();
422 modules_val = modules_sym->curr.tri;
423 }
424 }
425
426 if (sym_is_choice(sym)) {
427 struct symbol *choice_sym;
428
429 prop = sym_get_choice_prop(sym);
430 expr_list_for_each_sym(prop->expr, e, choice_sym) {
431 if ((sym->flags & SYMBOL_WRITE) &&
432 choice_sym->visible != no)
433 choice_sym->flags |= SYMBOL_WRITE;
434 if (sym->flags & SYMBOL_CHANGED)
435 sym_set_changed(choice_sym);
436 }
437 }
438
439 if (sym->flags & SYMBOL_NO_WRITE)
440 sym->flags &= ~SYMBOL_WRITE;
441
442 if (sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES)
443 set_all_choice_values(sym);
444 }
445
446 void sym_clear_all_valid(void)
447 {
448 struct symbol *sym;
449 int i;
450
451 for_all_symbols(i, sym)
452 sym->flags &= ~SYMBOL_VALID;
453 conf_set_changed(true);
454 sym_calc_value(modules_sym);
455 }
456
457 bool sym_tristate_within_range(struct symbol *sym, tristate val)
458 {
459 int type = sym_get_type(sym);
460
461 if (sym->visible == no)
462 return false;
463
464 if (type != S_BOOLEAN && type != S_TRISTATE)
465 return false;
466
467 if (type == S_BOOLEAN && val == mod)
468 return false;
469 if (sym->visible <= sym->rev_dep.tri)
470 return false;
471 if (sym_is_choice_value(sym) && sym->visible == yes)
472 return val == yes;
473 return val >= sym->rev_dep.tri && val <= sym->visible;
474 }
475
476 bool sym_set_tristate_value(struct symbol *sym, tristate val)
477 {
478 tristate oldval = sym_get_tristate_value(sym);
479
480 if (oldval != val && !sym_tristate_within_range(sym, val))
481 return false;
482
483 if (!(sym->flags & SYMBOL_DEF_USER)) {
484 sym->flags |= SYMBOL_DEF_USER;
485 sym_set_changed(sym);
486 }
487 /*
488 * setting a choice value also resets the new flag of the choice
489 * symbol and all other choice values.
490 */
491 if (sym_is_choice_value(sym) && val == yes) {
492 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
493 struct property *prop;
494 struct expr *e;
495
496 cs->def[S_DEF_USER].val = sym;
497 cs->flags |= SYMBOL_DEF_USER;
498 prop = sym_get_choice_prop(cs);
499 for (e = prop->expr; e; e = e->left.expr) {
500 if (e->right.sym->visible != no)
501 e->right.sym->flags |= SYMBOL_DEF_USER;
502 }
503 }
504
505 sym->def[S_DEF_USER].tri = val;
506 if (oldval != val)
507 sym_clear_all_valid();
508
509 return true;
510 }
511
512 tristate sym_toggle_tristate_value(struct symbol *sym)
513 {
514 tristate oldval, newval;
515
516 oldval = newval = sym_get_tristate_value(sym);
517 do {
518 switch (newval) {
519 case no:
520 newval = mod;
521 break;
522 case mod:
523 newval = yes;
524 break;
525 case yes:
526 newval = no;
527 break;
528 }
529 if (sym_set_tristate_value(sym, newval))
530 break;
531 } while (oldval != newval);
532 return newval;
533 }
534
535 bool sym_string_valid(struct symbol *sym, const char *str)
536 {
537 signed char ch;
538
539 switch (sym->type) {
540 case S_STRING:
541 return true;
542 case S_INT:
543 ch = *str++;
544 if (ch == '-')
545 ch = *str++;
546 if (!isdigit(ch))
547 return false;
548 if (ch == '0' && *str != 0)
549 return false;
550 while ((ch = *str++)) {
551 if (!isdigit(ch))
552 return false;
553 }
554 return true;
555 case S_HEX:
556 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
557 str += 2;
558 ch = *str++;
559 do {
560 if (!isxdigit(ch))
561 return false;
562 } while ((ch = *str++));
563 return true;
564 case S_BOOLEAN:
565 case S_TRISTATE:
566 switch (str[0]) {
567 case 'y': case 'Y':
568 case 'm': case 'M':
569 case 'n': case 'N':
570 return true;
571 }
572 return false;
573 default:
574 return false;
575 }
576 }
577
578 bool sym_string_within_range(struct symbol *sym, const char *str)
579 {
580 struct property *prop;
581 long long val;
582
583 switch (sym->type) {
584 case S_STRING:
585 return sym_string_valid(sym, str);
586 case S_INT:
587 if (!sym_string_valid(sym, str))
588 return false;
589 prop = sym_get_range_prop(sym);
590 if (!prop)
591 return true;
592 val = strtoll(str, NULL, 10);
593 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
594 val <= sym_get_range_val(prop->expr->right.sym, 10);
595 case S_HEX:
596 if (!sym_string_valid(sym, str))
597 return false;
598 prop = sym_get_range_prop(sym);
599 if (!prop)
600 return true;
601 val = strtoll(str, NULL, 16);
602 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
603 val <= sym_get_range_val(prop->expr->right.sym, 16);
604 case S_BOOLEAN:
605 case S_TRISTATE:
606 switch (str[0]) {
607 case 'y': case 'Y':
608 return sym_tristate_within_range(sym, yes);
609 case 'm': case 'M':
610 return sym_tristate_within_range(sym, mod);
611 case 'n': case 'N':
612 return sym_tristate_within_range(sym, no);
613 }
614 return false;
615 default:
616 return false;
617 }
618 }
619
620 bool sym_set_string_value(struct symbol *sym, const char *newval)
621 {
622 const char *oldval;
623 char *val;
624 int size;
625
626 switch (sym->type) {
627 case S_BOOLEAN:
628 case S_TRISTATE:
629 switch (newval[0]) {
630 case 'y': case 'Y':
631 return sym_set_tristate_value(sym, yes);
632 case 'm': case 'M':
633 return sym_set_tristate_value(sym, mod);
634 case 'n': case 'N':
635 return sym_set_tristate_value(sym, no);
636 }
637 return false;
638 default:
639 ;
640 }
641
642 if (!sym_string_within_range(sym, newval))
643 return false;
644
645 if (!(sym->flags & SYMBOL_DEF_USER)) {
646 sym->flags |= SYMBOL_DEF_USER;
647 sym_set_changed(sym);
648 }
649
650 oldval = sym->def[S_DEF_USER].val;
651 size = strlen(newval) + 1;
652 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
653 size += 2;
654 sym->def[S_DEF_USER].val = val = xmalloc(size);
655 *val++ = '0';
656 *val++ = 'x';
657 } else if (!oldval || strcmp(oldval, newval))
658 sym->def[S_DEF_USER].val = val = xmalloc(size);
659 else
660 return true;
661
662 strcpy(val, newval);
663 free((void *)oldval);
664 sym_clear_all_valid();
665
666 return true;
667 }
668
669 /*
670 * Find the default value associated to a symbol.
671 * For tristate symbol handle the modules=n case
672 * in which case "m" becomes "y".
673 * If the symbol does not have any default then fallback
674 * to the fixed default values.
675 */
676 const char *sym_get_string_default(struct symbol *sym)
677 {
678 struct property *prop;
679 struct symbol *ds;
680 const char *str;
681 tristate val;
682
683 sym_calc_visibility(sym);
684 sym_calc_value(modules_sym);
685 val = symbol_no.curr.tri;
686 str = symbol_empty.curr.val;
687
688 /* If symbol has a default value look it up */
689 prop = sym_get_default_prop(sym);
690 if (prop != NULL) {
691 switch (sym->type) {
692 case S_BOOLEAN:
693 case S_TRISTATE:
694 /* The visibility may limit the value from yes => mod */
695 val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
696 break;
697 default:
698 /*
699 * The following fails to handle the situation
700 * where a default value is further limited by
701 * the valid range.
702 */
703 ds = prop_get_symbol(prop);
704 if (ds != NULL) {
705 sym_calc_value(ds);
706 str = (const char *)ds->curr.val;
707 }
708 }
709 }
710
711 /* Handle select statements */
712 val = EXPR_OR(val, sym->rev_dep.tri);
713
714 /* transpose mod to yes if modules are not enabled */
715 if (val == mod)
716 if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
717 val = yes;
718
719 /* transpose mod to yes if type is bool */
720 if (sym->type == S_BOOLEAN && val == mod)
721 val = yes;
722
723 /* adjust the default value if this symbol is implied by another */
724 if (val < sym->implied.tri)
725 val = sym->implied.tri;
726
727 switch (sym->type) {
728 case S_BOOLEAN:
729 case S_TRISTATE:
730 switch (val) {
731 case no: return "n";
732 case mod: return "m";
733 case yes: return "y";
734 }
735 case S_INT:
736 case S_HEX:
737 return str;
738 case S_STRING:
739 return str;
740 case S_UNKNOWN:
741 break;
742 }
743 return "";
744 }
745
746 const char *sym_get_string_value(struct symbol *sym)
747 {
748 tristate val;
749
750 switch (sym->type) {
751 case S_BOOLEAN:
752 case S_TRISTATE:
753 val = sym_get_tristate_value(sym);
754 switch (val) {
755 case no:
756 return "n";
757 case mod:
758 sym_calc_value(modules_sym);
759 return (modules_sym->curr.tri == no) ? "n" : "m";
760 case yes:
761 return "y";
762 }
763 break;
764 default:
765 ;
766 }
767 return (const char *)sym->curr.val;
768 }
769
770 bool sym_is_changeable(struct symbol *sym)
771 {
772 return sym->visible > sym->rev_dep.tri;
773 }
774
775 static unsigned strhash(const char *s)
776 {
777 /* fnv32 hash */
778 unsigned hash = 2166136261U;
779 for (; *s; s++)
780 hash = (hash ^ *s) * 0x01000193;
781 return hash;
782 }
783
784 struct symbol *sym_lookup(const char *name, int flags)
785 {
786 struct symbol *symbol;
787 char *new_name;
788 int hash;
789
790 if (name) {
791 if (name[0] && !name[1]) {
792 switch (name[0]) {
793 case 'y': return &symbol_yes;
794 case 'm': return &symbol_mod;
795 case 'n': return &symbol_no;
796 }
797 }
798 hash = strhash(name) % SYMBOL_HASHSIZE;
799
800 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
801 if (symbol->name &&
802 !strcmp(symbol->name, name) &&
803 (flags ? symbol->flags & flags
804 : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
805 return symbol;
806 }
807 new_name = xstrdup(name);
808 } else {
809 new_name = NULL;
810 hash = 0;
811 }
812
813 symbol = xmalloc(sizeof(*symbol));
814 memset(symbol, 0, sizeof(*symbol));
815 symbol->name = new_name;
816 symbol->type = S_UNKNOWN;
817 symbol->flags = flags;
818
819 symbol->next = symbol_hash[hash];
820 symbol_hash[hash] = symbol;
821
822 return symbol;
823 }
824
825 struct symbol *sym_find(const char *name)
826 {
827 struct symbol *symbol = NULL;
828 int hash = 0;
829
830 if (!name)
831 return NULL;
832
833 if (name[0] && !name[1]) {
834 switch (name[0]) {
835 case 'y': return &symbol_yes;
836 case 'm': return &symbol_mod;
837 case 'n': return &symbol_no;
838 }
839 }
840 hash = strhash(name) % SYMBOL_HASHSIZE;
841
842 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
843 if (symbol->name &&
844 !strcmp(symbol->name, name) &&
845 !(symbol->flags & SYMBOL_CONST))
846 break;
847 }
848
849 return symbol;
850 }
851
852 struct sym_match {
853 struct symbol *sym;
854 off_t so, eo;
855 };
856
857 /* Compare matched symbols as thus:
858 * - first, symbols that match exactly
859 * - then, alphabetical sort
860 */
861 static int sym_rel_comp(const void *sym1, const void *sym2)
862 {
863 const struct sym_match *s1 = sym1;
864 const struct sym_match *s2 = sym2;
865 int exact1, exact2;
866
867 /* Exact match:
868 * - if matched length on symbol s1 is the length of that symbol,
869 * then this symbol should come first;
870 * - if matched length on symbol s2 is the length of that symbol,
871 * then this symbol should come first.
872 * Note: since the search can be a regexp, both symbols may match
873 * exactly; if this is the case, we can't decide which comes first,
874 * and we fallback to sorting alphabetically.
875 */
876 exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
877 exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
878 if (exact1 && !exact2)
879 return -1;
880 if (!exact1 && exact2)
881 return 1;
882
883 /* As a fallback, sort symbols alphabetically */
884 return strcmp(s1->sym->name, s2->sym->name);
885 }
886
887 struct symbol **sym_re_search(const char *pattern)
888 {
889 struct symbol *sym, **sym_arr = NULL;
890 struct sym_match *sym_match_arr = NULL;
891 int i, cnt, size;
892 regex_t re;
893 regmatch_t match[1];
894
895 cnt = size = 0;
896 /* Skip if empty */
897 if (strlen(pattern) == 0)
898 return NULL;
899 if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
900 return NULL;
901
902 for_all_symbols(i, sym) {
903 if (sym->flags & SYMBOL_CONST || !sym->name)
904 continue;
905 if (regexec(&re, sym->name, 1, match, 0))
906 continue;
907 if (cnt >= size) {
908 void *tmp;
909 size += 16;
910 tmp = realloc(sym_match_arr, size * sizeof(struct sym_match));
911 if (!tmp)
912 goto sym_re_search_free;
913 sym_match_arr = tmp;
914 }
915 sym_calc_value(sym);
916 /* As regexec returned 0, we know we have a match, so
917 * we can use match[0].rm_[se]o without further checks
918 */
919 sym_match_arr[cnt].so = match[0].rm_so;
920 sym_match_arr[cnt].eo = match[0].rm_eo;
921 sym_match_arr[cnt++].sym = sym;
922 }
923 if (sym_match_arr) {
924 qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp);
925 sym_arr = malloc((cnt+1) * sizeof(struct symbol *));
926 if (!sym_arr)
927 goto sym_re_search_free;
928 for (i = 0; i < cnt; i++)
929 sym_arr[i] = sym_match_arr[i].sym;
930 sym_arr[cnt] = NULL;
931 }
932 sym_re_search_free:
933 /* sym_match_arr can be NULL if no match, but free(NULL) is OK */
934 free(sym_match_arr);
935 regfree(&re);
936
937 return sym_arr;
938 }
939
940 /*
941 * When we check for recursive dependencies we use a stack to save
942 * current state so we can print out relevant info to user.
943 * The entries are located on the call stack so no need to free memory.
944 * Note insert() remove() must always match to properly clear the stack.
945 */
946 static struct dep_stack {
947 struct dep_stack *prev, *next;
948 struct symbol *sym;
949 struct property *prop;
950 struct expr **expr;
951 } *check_top;
952
953 static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
954 {
955 memset(stack, 0, sizeof(*stack));
956 if (check_top)
957 check_top->next = stack;
958 stack->prev = check_top;
959 stack->sym = sym;
960 check_top = stack;
961 }
962
963 static void dep_stack_remove(void)
964 {
965 check_top = check_top->prev;
966 if (check_top)
967 check_top->next = NULL;
968 }
969
970 /*
971 * Called when we have detected a recursive dependency.
972 * check_top point to the top of the stact so we use
973 * the ->prev pointer to locate the bottom of the stack.
974 */
975 static void sym_check_print_recursive(struct symbol *last_sym)
976 {
977 struct dep_stack *stack;
978 struct symbol *sym, *next_sym;
979 struct menu *menu = NULL;
980 struct property *prop;
981 struct dep_stack cv_stack;
982
983 if (sym_is_choice_value(last_sym)) {
984 dep_stack_insert(&cv_stack, last_sym);
985 last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
986 }
987
988 for (stack = check_top; stack != NULL; stack = stack->prev)
989 if (stack->sym == last_sym)
990 break;
991 if (!stack) {
992 fprintf(stderr, "unexpected recursive dependency error\n");
993 return;
994 }
995
996 for (; stack; stack = stack->next) {
997 sym = stack->sym;
998 next_sym = stack->next ? stack->next->sym : last_sym;
999 prop = stack->prop;
1000 if (prop == NULL)
1001 prop = stack->sym->prop;
1002
1003 /* for choice values find the menu entry (used below) */
1004 if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
1005 for (prop = sym->prop; prop; prop = prop->next) {
1006 menu = prop->menu;
1007 if (prop->menu)
1008 break;
1009 }
1010 }
1011 if (stack->sym == last_sym)
1012 fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
1013 prop->file->name, prop->lineno);
1014
1015 if (sym_is_choice(sym)) {
1016 fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
1017 menu->file->name, menu->lineno,
1018 sym->name ? sym->name : "<choice>",
1019 next_sym->name ? next_sym->name : "<choice>");
1020 } else if (sym_is_choice_value(sym)) {
1021 fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
1022 menu->file->name, menu->lineno,
1023 sym->name ? sym->name : "<choice>",
1024 next_sym->name ? next_sym->name : "<choice>");
1025 } else if (stack->expr == &sym->dir_dep.expr) {
1026 fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
1027 prop->file->name, prop->lineno,
1028 sym->name ? sym->name : "<choice>",
1029 next_sym->name ? next_sym->name : "<choice>");
1030 } else if (stack->expr == &sym->rev_dep.expr) {
1031 fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
1032 prop->file->name, prop->lineno,
1033 sym->name ? sym->name : "<choice>",
1034 next_sym->name ? next_sym->name : "<choice>");
1035 } else if (stack->expr == &sym->implied.expr) {
1036 fprintf(stderr, "%s:%d:\tsymbol %s is implied by %s\n",
1037 prop->file->name, prop->lineno,
1038 sym->name ? sym->name : "<choice>",
1039 next_sym->name ? next_sym->name : "<choice>");
1040 } else if (stack->expr) {
1041 fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
1042 prop->file->name, prop->lineno,
1043 sym->name ? sym->name : "<choice>",
1044 prop_get_type_name(prop->type),
1045 next_sym->name ? next_sym->name : "<choice>");
1046 } else {
1047 fprintf(stderr, "%s:%d:\tsymbol %s %s is visible depending on %s\n",
1048 prop->file->name, prop->lineno,
1049 sym->name ? sym->name : "<choice>",
1050 prop_get_type_name(prop->type),
1051 next_sym->name ? next_sym->name : "<choice>");
1052 }
1053 }
1054
1055 fprintf(stderr,
1056 "For a resolution refer to Documentation/kbuild/kconfig-language.rst\n"
1057 "subsection \"Kconfig recursive dependency limitations\"\n"
1058 "\n");
1059
1060 if (check_top == &cv_stack)
1061 dep_stack_remove();
1062 }
1063
1064 static struct symbol *sym_check_expr_deps(struct expr *e)
1065 {
1066 struct symbol *sym;
1067
1068 if (!e)
1069 return NULL;
1070 switch (e->type) {
1071 case E_OR:
1072 case E_AND:
1073 sym = sym_check_expr_deps(e->left.expr);
1074 if (sym)
1075 return sym;
1076 return sym_check_expr_deps(e->right.expr);
1077 case E_NOT:
1078 return sym_check_expr_deps(e->left.expr);
1079 case E_EQUAL:
1080 case E_GEQ:
1081 case E_GTH:
1082 case E_LEQ:
1083 case E_LTH:
1084 case E_UNEQUAL:
1085 sym = sym_check_deps(e->left.sym);
1086 if (sym)
1087 return sym;
1088 return sym_check_deps(e->right.sym);
1089 case E_SYMBOL:
1090 return sym_check_deps(e->left.sym);
1091 default:
1092 break;
1093 }
1094 fprintf(stderr, "Oops! How to check %d?\n", e->type);
1095 return NULL;
1096 }
1097
1098 /* return NULL when dependencies are OK */
1099 static struct symbol *sym_check_sym_deps(struct symbol *sym)
1100 {
1101 struct symbol *sym2;
1102 struct property *prop;
1103 struct dep_stack stack;
1104
1105 dep_stack_insert(&stack, sym);
1106
1107 stack.expr = &sym->dir_dep.expr;
1108 sym2 = sym_check_expr_deps(sym->dir_dep.expr);
1109 if (sym2)
1110 goto out;
1111
1112 stack.expr = &sym->rev_dep.expr;
1113 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
1114 if (sym2)
1115 goto out;
1116
1117 stack.expr = &sym->implied.expr;
1118 sym2 = sym_check_expr_deps(sym->implied.expr);
1119 if (sym2)
1120 goto out;
1121
1122 stack.expr = NULL;
1123
1124 for (prop = sym->prop; prop; prop = prop->next) {
1125 if (prop->type == P_CHOICE || prop->type == P_SELECT ||
1126 prop->type == P_IMPLY)
1127 continue;
1128 stack.prop = prop;
1129 sym2 = sym_check_expr_deps(prop->visible.expr);
1130 if (sym2)
1131 break;
1132 if (prop->type != P_DEFAULT || sym_is_choice(sym))
1133 continue;
1134 stack.expr = &prop->expr;
1135 sym2 = sym_check_expr_deps(prop->expr);
1136 if (sym2)
1137 break;
1138 stack.expr = NULL;
1139 }
1140
1141 out:
1142 dep_stack_remove();
1143
1144 return sym2;
1145 }
1146
1147 static struct symbol *sym_check_choice_deps(struct symbol *choice)
1148 {
1149 struct symbol *sym, *sym2;
1150 struct property *prop;
1151 struct expr *e;
1152 struct dep_stack stack;
1153
1154 dep_stack_insert(&stack, choice);
1155
1156 prop = sym_get_choice_prop(choice);
1157 expr_list_for_each_sym(prop->expr, e, sym)
1158 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1159
1160 choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1161 sym2 = sym_check_sym_deps(choice);
1162 choice->flags &= ~SYMBOL_CHECK;
1163 if (sym2)
1164 goto out;
1165
1166 expr_list_for_each_sym(prop->expr, e, sym) {
1167 sym2 = sym_check_sym_deps(sym);
1168 if (sym2)
1169 break;
1170 }
1171 out:
1172 expr_list_for_each_sym(prop->expr, e, sym)
1173 sym->flags &= ~SYMBOL_CHECK;
1174
1175 if (sym2 && sym_is_choice_value(sym2) &&
1176 prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
1177 sym2 = choice;
1178
1179 dep_stack_remove();
1180
1181 return sym2;
1182 }
1183
1184 struct symbol *sym_check_deps(struct symbol *sym)
1185 {
1186 struct symbol *sym2;
1187 struct property *prop;
1188
1189 if (sym->flags & SYMBOL_CHECK) {
1190 sym_check_print_recursive(sym);
1191 return sym;
1192 }
1193 if (sym->flags & SYMBOL_CHECKED)
1194 return NULL;
1195
1196 if (sym_is_choice_value(sym)) {
1197 struct dep_stack stack;
1198
1199 /* for choice groups start the check with main choice symbol */
1200 dep_stack_insert(&stack, sym);
1201 prop = sym_get_choice_prop(sym);
1202 sym2 = sym_check_deps(prop_get_symbol(prop));
1203 dep_stack_remove();
1204 } else if (sym_is_choice(sym)) {
1205 sym2 = sym_check_choice_deps(sym);
1206 } else {
1207 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1208 sym2 = sym_check_sym_deps(sym);
1209 sym->flags &= ~SYMBOL_CHECK;
1210 }
1211
1212 if (!recursive_is_error && sym2 && sym2 == sym)
1213 sym2 = NULL;
1214
1215 return sym2;
1216 }
1217
1218 struct symbol *prop_get_symbol(struct property *prop)
1219 {
1220 if (prop->expr && (prop->expr->type == E_SYMBOL ||
1221 prop->expr->type == E_LIST))
1222 return prop->expr->left.sym;
1223 return NULL;
1224 }
1225
1226 const char *prop_get_type_name(enum prop_type type)
1227 {
1228 switch (type) {
1229 case P_PROMPT:
1230 return "prompt";
1231 case P_COMMENT:
1232 return "comment";
1233 case P_MENU:
1234 return "menu";
1235 case P_DEFAULT:
1236 return "default";
1237 case P_CHOICE:
1238 return "choice";
1239 case P_SELECT:
1240 return "select";
1241 case P_IMPLY:
1242 return "imply";
1243 case P_RANGE:
1244 return "range";
1245 case P_SYMBOL:
1246 return "symbol";
1247 case P_RESET:
1248 return "reset";
1249 case P_UNKNOWN:
1250 break;
1251 }
1252 return "unknown";
1253 }