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