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