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