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