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 sym_warnings++;
380 }
381
sym_dep_errors(void)382 bool sym_dep_errors(void)
383 {
384 if (sym_warnings)
385 return getenv("KCONFIG_WERROR");
386 return false;
387 }
388
sym_calc_value(struct symbol * sym)389 void sym_calc_value(struct symbol *sym)
390 {
391 struct symbol_value newval, oldval;
392 struct property *prop;
393 struct menu *choice_menu;
394
395 if (!sym)
396 return;
397
398 if (sym->flags & SYMBOL_VALID)
399 return;
400
401 sym->flags |= SYMBOL_VALID;
402
403 oldval = sym->curr;
404
405 newval.tri = no;
406
407 switch (sym->type) {
408 case S_INT:
409 newval.val = "0";
410 break;
411 case S_HEX:
412 newval.val = "0x0";
413 break;
414 case S_STRING:
415 newval.val = "";
416 break;
417 case S_BOOLEAN:
418 case S_TRISTATE:
419 newval.val = "n";
420 break;
421 default:
422 sym->curr.val = sym->name;
423 sym->curr.tri = no;
424 return;
425 }
426 sym->flags &= ~SYMBOL_WRITE;
427
428 sym_calc_visibility(sym);
429
430 if (sym->visible != no)
431 sym->flags |= SYMBOL_WRITE;
432
433 /* set default if recursively called */
434 sym->curr = newval;
435
436 switch (sym_get_type(sym)) {
437 case S_BOOLEAN:
438 case S_TRISTATE:
439 choice_menu = sym_get_choice_menu(sym);
440
441 if (choice_menu) {
442 sym_calc_choice(choice_menu);
443 newval.tri = sym->curr.tri;
444 } else {
445 if (sym->visible != no) {
446 /* if the symbol is visible use the user value
447 * if available, otherwise try the default value
448 */
449 if (sym_has_value(sym)) {
450 newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
451 sym->visible);
452 goto calc_newval;
453 }
454 }
455 if (sym->rev_dep.tri != no)
456 sym->flags |= SYMBOL_WRITE;
457 if (!sym_is_choice(sym)) {
458 prop = sym_get_default_prop(sym);
459 if (prop) {
460 newval.tri = EXPR_AND(expr_calc_value(prop->expr),
461 prop->visible.tri);
462 if (newval.tri != no)
463 sym->flags |= SYMBOL_WRITE;
464 }
465 if (sym->implied.tri != no) {
466 sym->flags |= SYMBOL_WRITE;
467 newval.tri = EXPR_OR(newval.tri, sym->implied.tri);
468 newval.tri = EXPR_AND(newval.tri,
469 sym->dir_dep.tri);
470 }
471 }
472 calc_newval:
473 if (sym->dir_dep.tri < sym->rev_dep.tri)
474 sym_warn_unmet_dep(sym);
475 newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
476 }
477 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
478 newval.tri = yes;
479 break;
480 case S_STRING:
481 case S_HEX:
482 case S_INT:
483 if (sym->visible != no && sym_has_value(sym)) {
484 newval.val = sym->def[S_DEF_USER].val;
485 break;
486 }
487 prop = sym_get_default_prop(sym);
488 if (prop) {
489 struct symbol *ds = prop_get_symbol(prop);
490 if (ds) {
491 sym->flags |= SYMBOL_WRITE;
492 sym_calc_value(ds);
493 newval.val = ds->curr.val;
494 }
495 }
496 break;
497 default:
498 ;
499 }
500
501 sym->curr = newval;
502 sym_validate_range(sym);
503
504 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
505 sym_set_changed(sym);
506 if (modules_sym == sym) {
507 sym_set_all_changed();
508 modules_val = modules_sym->curr.tri;
509 }
510 }
511
512 if (sym_is_choice(sym))
513 sym->flags &= ~SYMBOL_WRITE;
514 }
515
sym_clear_all_valid(void)516 void sym_clear_all_valid(void)
517 {
518 struct symbol *sym;
519
520 for_all_symbols(sym)
521 sym->flags &= ~SYMBOL_VALID;
522 expr_invalidate_all();
523 conf_set_changed(true);
524 sym_calc_value(modules_sym);
525 }
526
sym_tristate_within_range(const struct symbol * sym,tristate val)527 bool sym_tristate_within_range(const struct symbol *sym, tristate val)
528 {
529 int type = sym_get_type(sym);
530
531 if (sym->visible == no)
532 return false;
533
534 if (type != S_BOOLEAN && type != S_TRISTATE)
535 return false;
536
537 if (type == S_BOOLEAN && val == mod)
538 return false;
539 if (sym->visible <= sym->rev_dep.tri)
540 return false;
541 return val >= sym->rev_dep.tri && val <= sym->visible;
542 }
543
sym_set_tristate_value(struct symbol * sym,tristate val)544 bool sym_set_tristate_value(struct symbol *sym, tristate val)
545 {
546 tristate oldval = sym_get_tristate_value(sym);
547
548 if (!sym_tristate_within_range(sym, val))
549 return false;
550
551 if (!(sym->flags & SYMBOL_DEF_USER) || sym->def[S_DEF_USER].tri != val) {
552 sym->def[S_DEF_USER].tri = val;
553 sym->flags |= SYMBOL_DEF_USER;
554 sym_set_changed(sym);
555 }
556
557 if (oldval != val)
558 sym_clear_all_valid();
559
560 return true;
561 }
562
563 /**
564 * choice_set_value - set the user input to a choice
565 *
566 * @choice: menu entry for the choice
567 * @sym: selected symbol
568 */
choice_set_value(struct menu * choice,struct symbol * sym)569 void choice_set_value(struct menu *choice, struct symbol *sym)
570 {
571 struct menu *menu;
572 bool changed = false;
573
574 menu_for_each_sub_entry(menu, choice) {
575 tristate val;
576
577 if (!menu->sym)
578 continue;
579
580 if (menu->sym->visible == no)
581 continue;
582
583 val = menu->sym == sym ? yes : no;
584
585 if (menu->sym->curr.tri != val)
586 changed = true;
587
588 menu->sym->def[S_DEF_USER].tri = val;
589 menu->sym->flags |= SYMBOL_DEF_USER;
590
591 /*
592 * Now, the user has explicitly enabled or disabled this symbol,
593 * it should be given the highest priority. We are possibly
594 * setting multiple symbols to 'n', where the first symbol is
595 * given the least prioritized 'n'. This works well when the
596 * choice block ends up with selecting 'n' symbol.
597 * (see sym_calc_choice())
598 */
599 list_move(&menu->sym->choice_link, &choice->choice_members);
600 }
601
602 if (changed)
603 sym_clear_all_valid();
604 }
605
sym_toggle_tristate_value(struct symbol * sym)606 tristate sym_toggle_tristate_value(struct symbol *sym)
607 {
608 struct menu *choice;
609 tristate oldval, newval;
610
611 choice = sym_get_choice_menu(sym);
612 if (choice) {
613 choice_set_value(choice, sym);
614 return yes;
615 }
616
617 oldval = newval = sym_get_tristate_value(sym);
618 do {
619 switch (newval) {
620 case no:
621 newval = mod;
622 break;
623 case mod:
624 newval = yes;
625 break;
626 case yes:
627 newval = no;
628 break;
629 }
630 if (sym_set_tristate_value(sym, newval))
631 break;
632 } while (oldval != newval);
633 return newval;
634 }
635
sym_string_valid(struct symbol * sym,const char * str)636 bool sym_string_valid(struct symbol *sym, const char *str)
637 {
638 signed char ch;
639
640 switch (sym->type) {
641 case S_STRING:
642 return true;
643 case S_INT:
644 ch = *str++;
645 if (ch == '-')
646 ch = *str++;
647 if (!isdigit(ch))
648 return false;
649 if (ch == '0' && *str != 0)
650 return false;
651 while ((ch = *str++)) {
652 if (!isdigit(ch))
653 return false;
654 }
655 return true;
656 case S_HEX:
657 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
658 str += 2;
659 ch = *str++;
660 do {
661 if (!isxdigit(ch))
662 return false;
663 } while ((ch = *str++));
664 return true;
665 case S_BOOLEAN:
666 case S_TRISTATE:
667 switch (str[0]) {
668 case 'y': case 'Y':
669 case 'm': case 'M':
670 case 'n': case 'N':
671 return true;
672 }
673 return false;
674 default:
675 return false;
676 }
677 }
678
sym_string_within_range(struct symbol * sym,const char * str)679 bool sym_string_within_range(struct symbol *sym, const char *str)
680 {
681 struct property *prop;
682 long long val;
683
684 switch (sym->type) {
685 case S_STRING:
686 return sym_string_valid(sym, str);
687 case S_INT:
688 if (!sym_string_valid(sym, str))
689 return false;
690 prop = sym_get_range_prop(sym);
691 if (!prop)
692 return true;
693 val = strtoll(str, NULL, 10);
694 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
695 val <= sym_get_range_val(prop->expr->right.sym, 10);
696 case S_HEX:
697 if (!sym_string_valid(sym, str))
698 return false;
699 prop = sym_get_range_prop(sym);
700 if (!prop)
701 return true;
702 val = strtoll(str, NULL, 16);
703 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
704 val <= sym_get_range_val(prop->expr->right.sym, 16);
705 case S_BOOLEAN:
706 case S_TRISTATE:
707 switch (str[0]) {
708 case 'y': case 'Y':
709 return sym_tristate_within_range(sym, yes);
710 case 'm': case 'M':
711 return sym_tristate_within_range(sym, mod);
712 case 'n': case 'N':
713 return sym_tristate_within_range(sym, no);
714 }
715 return false;
716 default:
717 return false;
718 }
719 }
720
sym_set_string_value(struct symbol * sym,const char * newval)721 bool sym_set_string_value(struct symbol *sym, const char *newval)
722 {
723 const char *oldval;
724 char *val;
725 int size;
726
727 switch (sym->type) {
728 case S_BOOLEAN:
729 case S_TRISTATE:
730 switch (newval[0]) {
731 case 'y': case 'Y':
732 return sym_set_tristate_value(sym, yes);
733 case 'm': case 'M':
734 return sym_set_tristate_value(sym, mod);
735 case 'n': case 'N':
736 return sym_set_tristate_value(sym, no);
737 }
738 return false;
739 default:
740 ;
741 }
742
743 if (!sym_string_within_range(sym, newval))
744 return false;
745
746 if (!(sym->flags & SYMBOL_DEF_USER)) {
747 sym->flags |= SYMBOL_DEF_USER;
748 sym_set_changed(sym);
749 }
750
751 oldval = sym->def[S_DEF_USER].val;
752 size = strlen(newval) + 1;
753 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
754 size += 2;
755 sym->def[S_DEF_USER].val = val = xmalloc(size);
756 *val++ = '0';
757 *val++ = 'x';
758 } else if (!oldval || strcmp(oldval, newval))
759 sym->def[S_DEF_USER].val = val = xmalloc(size);
760 else
761 return true;
762
763 strcpy(val, newval);
764 free((void *)oldval);
765 sym_clear_all_valid();
766
767 return true;
768 }
769
770 /*
771 * Find the default value associated to a symbol.
772 * For tristate symbol handle the modules=n case
773 * in which case "m" becomes "y".
774 * If the symbol does not have any default then fallback
775 * to the fixed default values.
776 */
sym_get_string_default(struct symbol * sym)777 const char *sym_get_string_default(struct symbol *sym)
778 {
779 struct property *prop;
780 struct symbol *ds;
781 const char *str = "";
782 tristate val;
783
784 sym_calc_visibility(sym);
785 sym_calc_value(modules_sym);
786 val = symbol_no.curr.tri;
787
788 /* If symbol has a default value look it up */
789 prop = sym_get_default_prop(sym);
790 if (prop != NULL) {
791 switch (sym->type) {
792 case S_BOOLEAN:
793 case S_TRISTATE:
794 /* The visibility may limit the value from yes => mod */
795 val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
796 break;
797 default:
798 /*
799 * The following fails to handle the situation
800 * where a default value is further limited by
801 * the valid range.
802 */
803 ds = prop_get_symbol(prop);
804 if (ds != NULL) {
805 sym_calc_value(ds);
806 str = (const char *)ds->curr.val;
807 }
808 }
809 }
810
811 /* Handle select statements */
812 val = EXPR_OR(val, sym->rev_dep.tri);
813
814 /* transpose mod to yes if modules are not enabled */
815 if (val == mod)
816 if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
817 val = yes;
818
819 /* transpose mod to yes if type is bool */
820 if (sym->type == S_BOOLEAN && val == mod)
821 val = yes;
822
823 /* adjust the default value if this symbol is implied by another */
824 if (val < sym->implied.tri)
825 val = sym->implied.tri;
826
827 switch (sym->type) {
828 case S_BOOLEAN:
829 case S_TRISTATE:
830 switch (val) {
831 case no: return "n";
832 case mod: return "m";
833 case yes: return "y";
834 }
835 case S_INT:
836 if (!str[0])
837 str = "0";
838 break;
839 case S_HEX:
840 if (!str[0])
841 str = "0x0";
842 break;
843 default:
844 break;
845 }
846 return str;
847 }
848
sym_get_string_value(struct symbol * sym)849 const char *sym_get_string_value(struct symbol *sym)
850 {
851 tristate val;
852
853 switch (sym->type) {
854 case S_BOOLEAN:
855 case S_TRISTATE:
856 val = sym_get_tristate_value(sym);
857 switch (val) {
858 case no:
859 return "n";
860 case mod:
861 return "m";
862 case yes:
863 return "y";
864 }
865 break;
866 default:
867 ;
868 }
869 return (const char *)sym->curr.val;
870 }
871
sym_is_changeable(const struct symbol * sym)872 bool sym_is_changeable(const struct symbol *sym)
873 {
874 return !sym_is_choice(sym) && sym->visible > sym->rev_dep.tri;
875 }
876
sym_is_choice_value(const struct symbol * sym)877 bool sym_is_choice_value(const struct symbol *sym)
878 {
879 return !list_empty(&sym->choice_link);
880 }
881
882 HASHTABLE_DEFINE(sym_hashtable, SYMBOL_HASHSIZE);
883
sym_lookup(const char * name,int flags)884 struct symbol *sym_lookup(const char *name, int flags)
885 {
886 struct symbol *symbol;
887 char *new_name;
888 int hash;
889
890 if (name) {
891 if (name[0] && !name[1]) {
892 switch (name[0]) {
893 case 'y': return &symbol_yes;
894 case 'm': return &symbol_mod;
895 case 'n': return &symbol_no;
896 }
897 }
898 hash = hash_str(name);
899
900 hash_for_each_possible(sym_hashtable, symbol, node, hash) {
901 if (symbol->name &&
902 !strcmp(symbol->name, name) &&
903 (flags ? symbol->flags & flags
904 : !(symbol->flags & SYMBOL_CONST)))
905 return symbol;
906 }
907 new_name = xstrdup(name);
908 } else {
909 new_name = NULL;
910 hash = 0;
911 }
912
913 symbol = xmalloc(sizeof(*symbol));
914 memset(symbol, 0, sizeof(*symbol));
915 symbol->name = new_name;
916 symbol->type = S_UNKNOWN;
917 symbol->flags = flags;
918 INIT_LIST_HEAD(&symbol->menus);
919 INIT_LIST_HEAD(&symbol->choice_link);
920
921 hash_add(sym_hashtable, &symbol->node, hash);
922
923 return symbol;
924 }
925
sym_find(const char * name)926 struct symbol *sym_find(const char *name)
927 {
928 struct symbol *symbol = NULL;
929 int hash = 0;
930
931 if (!name)
932 return NULL;
933
934 if (name[0] && !name[1]) {
935 switch (name[0]) {
936 case 'y': return &symbol_yes;
937 case 'm': return &symbol_mod;
938 case 'n': return &symbol_no;
939 }
940 }
941 hash = hash_str(name);
942
943 hash_for_each_possible(sym_hashtable, symbol, node, hash) {
944 if (symbol->name &&
945 !strcmp(symbol->name, name) &&
946 !(symbol->flags & SYMBOL_CONST))
947 break;
948 }
949
950 return symbol;
951 }
952
953 struct sym_match {
954 struct symbol *sym;
955 off_t so, eo;
956 };
957
958 /* Compare matched symbols as thus:
959 * - first, symbols that match exactly
960 * - then, alphabetical sort
961 */
sym_rel_comp(const void * sym1,const void * sym2)962 static int sym_rel_comp(const void *sym1, const void *sym2)
963 {
964 const struct sym_match *s1 = sym1;
965 const struct sym_match *s2 = sym2;
966 int exact1, exact2;
967
968 /* Exact match:
969 * - if matched length on symbol s1 is the length of that symbol,
970 * then this symbol should come first;
971 * - if matched length on symbol s2 is the length of that symbol,
972 * then this symbol should come first.
973 * Note: since the search can be a regexp, both symbols may match
974 * exactly; if this is the case, we can't decide which comes first,
975 * and we fallback to sorting alphabetically.
976 */
977 exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
978 exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
979 if (exact1 && !exact2)
980 return -1;
981 if (!exact1 && exact2)
982 return 1;
983
984 /* As a fallback, sort symbols alphabetically */
985 return strcmp(s1->sym->name, s2->sym->name);
986 }
987
sym_re_search(const char * pattern)988 struct symbol **sym_re_search(const char *pattern)
989 {
990 struct symbol *sym, **sym_arr = NULL;
991 struct sym_match *sym_match_arr = NULL;
992 int i, cnt, size;
993 regex_t re;
994 regmatch_t match[1];
995
996 cnt = size = 0;
997 /* Skip if empty */
998 if (strlen(pattern) == 0)
999 return NULL;
1000 if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
1001 return NULL;
1002
1003 for_all_symbols(sym) {
1004 if (sym->flags & SYMBOL_CONST || !sym->name)
1005 continue;
1006 if (regexec(&re, sym->name, 1, match, 0))
1007 continue;
1008 if (cnt >= size) {
1009 void *tmp;
1010 size += 16;
1011 tmp = realloc(sym_match_arr, size * sizeof(struct sym_match));
1012 if (!tmp)
1013 goto sym_re_search_free;
1014 sym_match_arr = tmp;
1015 }
1016 sym_calc_value(sym);
1017 /* As regexec returned 0, we know we have a match, so
1018 * we can use match[0].rm_[se]o without further checks
1019 */
1020 sym_match_arr[cnt].so = match[0].rm_so;
1021 sym_match_arr[cnt].eo = match[0].rm_eo;
1022 sym_match_arr[cnt++].sym = sym;
1023 }
1024 if (sym_match_arr) {
1025 qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp);
1026 sym_arr = malloc((cnt+1) * sizeof(struct symbol *));
1027 if (!sym_arr)
1028 goto sym_re_search_free;
1029 for (i = 0; i < cnt; i++)
1030 sym_arr[i] = sym_match_arr[i].sym;
1031 sym_arr[cnt] = NULL;
1032 }
1033 sym_re_search_free:
1034 /* sym_match_arr can be NULL if no match, but free(NULL) is OK */
1035 free(sym_match_arr);
1036 regfree(&re);
1037
1038 return sym_arr;
1039 }
1040
1041 /*
1042 * When we check for recursive dependencies we use a stack to save
1043 * current state so we can print out relevant info to user.
1044 * The entries are located on the call stack so no need to free memory.
1045 * Note insert() remove() must always match to properly clear the stack.
1046 */
1047 static struct dep_stack {
1048 struct dep_stack *prev, *next;
1049 struct symbol *sym;
1050 struct property *prop;
1051 struct expr **expr;
1052 } *check_top;
1053
dep_stack_insert(struct dep_stack * stack,struct symbol * sym)1054 static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
1055 {
1056 memset(stack, 0, sizeof(*stack));
1057 if (check_top)
1058 check_top->next = stack;
1059 stack->prev = check_top;
1060 stack->sym = sym;
1061 check_top = stack;
1062 }
1063
dep_stack_remove(void)1064 static void dep_stack_remove(void)
1065 {
1066 check_top = check_top->prev;
1067 if (check_top)
1068 check_top->next = NULL;
1069 }
1070
1071 /*
1072 * Called when we have detected a recursive dependency.
1073 * check_top point to the top of the stact so we use
1074 * the ->prev pointer to locate the bottom of the stack.
1075 */
sym_check_print_recursive(struct symbol * last_sym)1076 static void sym_check_print_recursive(struct symbol *last_sym)
1077 {
1078 struct dep_stack *stack;
1079 struct symbol *sym, *next_sym;
1080 struct menu *choice;
1081 struct dep_stack cv_stack;
1082 enum prop_type type;
1083
1084 choice = sym_get_choice_menu(last_sym);
1085 if (choice) {
1086 dep_stack_insert(&cv_stack, last_sym);
1087 last_sym = choice->sym;
1088 }
1089
1090 for (stack = check_top; stack != NULL; stack = stack->prev)
1091 if (stack->sym == last_sym)
1092 break;
1093 if (!stack) {
1094 fprintf(stderr, "unexpected recursive dependency error\n");
1095 return;
1096 }
1097
1098 for (; stack; stack = stack->next) {
1099 sym = stack->sym;
1100 next_sym = stack->next ? stack->next->sym : last_sym;
1101 type = stack->prop ? stack->prop->type : P_UNKNOWN;
1102
1103 if (stack->sym == last_sym)
1104 fprintf(stderr, "error: recursive dependency detected!\n");
1105
1106 if (sym_is_choice(next_sym)) {
1107 choice = list_first_entry(&next_sym->menus, struct menu, link);
1108
1109 fprintf(stderr, "\tsymbol %s is part of choice block at %s:%d\n",
1110 sym->name ? sym->name : "<choice>",
1111 choice->filename, choice->lineno);
1112 } else if (stack->expr == &sym->dir_dep.expr) {
1113 fprintf(stderr, "\tsymbol %s depends on %s\n",
1114 sym->name ? sym->name : "<choice>",
1115 next_sym->name);
1116 } else if (stack->expr == &sym->rev_dep.expr) {
1117 fprintf(stderr, "\tsymbol %s is selected by %s\n",
1118 sym->name, next_sym->name);
1119 } else if (stack->expr == &sym->implied.expr) {
1120 fprintf(stderr, "\tsymbol %s is implied by %s\n",
1121 sym->name, next_sym->name);
1122 } else if (stack->expr) {
1123 fprintf(stderr, "\tsymbol %s %s value contains %s\n",
1124 sym->name ? sym->name : "<choice>",
1125 prop_get_type_name(type),
1126 next_sym->name);
1127 } else {
1128 fprintf(stderr, "\tsymbol %s %s is visible depending on %s\n",
1129 sym->name ? sym->name : "<choice>",
1130 prop_get_type_name(type),
1131 next_sym->name);
1132 }
1133 }
1134
1135 fprintf(stderr,
1136 "For a resolution refer to Documentation/kbuild/kconfig-language.rst\n"
1137 "subsection \"Kconfig recursive dependency limitations\"\n"
1138 "\n");
1139
1140 if (check_top == &cv_stack)
1141 dep_stack_remove();
1142 }
1143
sym_check_expr_deps(const struct expr * e)1144 static struct symbol *sym_check_expr_deps(const struct expr *e)
1145 {
1146 struct symbol *sym;
1147
1148 if (!e)
1149 return NULL;
1150 switch (e->type) {
1151 case E_OR:
1152 case E_AND:
1153 sym = sym_check_expr_deps(e->left.expr);
1154 if (sym)
1155 return sym;
1156 return sym_check_expr_deps(e->right.expr);
1157 case E_NOT:
1158 return sym_check_expr_deps(e->left.expr);
1159 case E_EQUAL:
1160 case E_GEQ:
1161 case E_GTH:
1162 case E_LEQ:
1163 case E_LTH:
1164 case E_UNEQUAL:
1165 sym = sym_check_deps(e->left.sym);
1166 if (sym)
1167 return sym;
1168 return sym_check_deps(e->right.sym);
1169 case E_SYMBOL:
1170 return sym_check_deps(e->left.sym);
1171 default:
1172 break;
1173 }
1174 fprintf(stderr, "Oops! How to check %d?\n", e->type);
1175 return NULL;
1176 }
1177
1178 /* return NULL when dependencies are OK */
sym_check_sym_deps(struct symbol * sym)1179 static struct symbol *sym_check_sym_deps(struct symbol *sym)
1180 {
1181 struct symbol *sym2;
1182 struct property *prop;
1183 struct dep_stack stack;
1184
1185 dep_stack_insert(&stack, sym);
1186
1187 stack.expr = &sym->dir_dep.expr;
1188 sym2 = sym_check_expr_deps(sym->dir_dep.expr);
1189 if (sym2)
1190 goto out;
1191
1192 stack.expr = &sym->rev_dep.expr;
1193 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
1194 if (sym2)
1195 goto out;
1196
1197 stack.expr = &sym->implied.expr;
1198 sym2 = sym_check_expr_deps(sym->implied.expr);
1199 if (sym2)
1200 goto out;
1201
1202 stack.expr = NULL;
1203
1204 for (prop = sym->prop; prop; prop = prop->next) {
1205 if (prop->type == P_SELECT || prop->type == P_IMPLY)
1206 continue;
1207 stack.prop = prop;
1208 sym2 = sym_check_expr_deps(prop->visible.expr);
1209 if (sym2)
1210 break;
1211 if (prop->type != P_DEFAULT || sym_is_choice(sym))
1212 continue;
1213 stack.expr = &prop->expr;
1214 sym2 = sym_check_expr_deps(prop->expr);
1215 if (sym2)
1216 break;
1217 stack.expr = NULL;
1218 }
1219
1220 out:
1221 dep_stack_remove();
1222
1223 return sym2;
1224 }
1225
sym_check_choice_deps(struct symbol * choice)1226 static struct symbol *sym_check_choice_deps(struct symbol *choice)
1227 {
1228 struct menu *choice_menu, *menu;
1229 struct symbol *sym2;
1230 struct dep_stack stack;
1231
1232 dep_stack_insert(&stack, choice);
1233
1234 choice_menu = list_first_entry(&choice->menus, struct menu, link);
1235
1236 menu_for_each_sub_entry(menu, choice_menu) {
1237 if (menu->sym)
1238 menu->sym->flags |= SYMBOL_CHECK | SYMBOL_CHECKED;
1239 }
1240
1241 choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1242 sym2 = sym_check_sym_deps(choice);
1243 choice->flags &= ~SYMBOL_CHECK;
1244 if (sym2)
1245 goto out;
1246
1247 menu_for_each_sub_entry(menu, choice_menu) {
1248 if (!menu->sym)
1249 continue;
1250 sym2 = sym_check_sym_deps(menu->sym);
1251 if (sym2)
1252 break;
1253 }
1254 out:
1255 menu_for_each_sub_entry(menu, choice_menu)
1256 if (menu->sym)
1257 menu->sym->flags &= ~SYMBOL_CHECK;
1258
1259 if (sym2) {
1260 struct menu *choice_menu2;
1261
1262 choice_menu2 = sym_get_choice_menu(sym2);
1263 if (choice_menu2 == choice_menu)
1264 sym2 = choice;
1265 }
1266
1267 dep_stack_remove();
1268
1269 return sym2;
1270 }
1271
sym_check_deps(struct symbol * sym)1272 struct symbol *sym_check_deps(struct symbol *sym)
1273 {
1274 struct menu *choice;
1275 struct symbol *sym2;
1276
1277 if (sym->flags & SYMBOL_CHECK) {
1278 sym_check_print_recursive(sym);
1279 return sym;
1280 }
1281 if (sym->flags & SYMBOL_CHECKED)
1282 return NULL;
1283
1284 choice = sym_get_choice_menu(sym);
1285 if (choice) {
1286 struct dep_stack stack;
1287
1288 /* for choice groups start the check with main choice symbol */
1289 dep_stack_insert(&stack, sym);
1290 sym2 = sym_check_deps(choice->sym);
1291 dep_stack_remove();
1292 } else if (sym_is_choice(sym)) {
1293 sym2 = sym_check_choice_deps(sym);
1294 } else {
1295 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1296 sym2 = sym_check_sym_deps(sym);
1297 sym->flags &= ~SYMBOL_CHECK;
1298 }
1299
1300 return sym2;
1301 }
1302
prop_get_symbol(const struct property * prop)1303 struct symbol *prop_get_symbol(const struct property *prop)
1304 {
1305 if (prop->expr && prop->expr->type == E_SYMBOL)
1306 return prop->expr->left.sym;
1307 return NULL;
1308 }
1309
prop_get_type_name(enum prop_type type)1310 const char *prop_get_type_name(enum prop_type type)
1311 {
1312 switch (type) {
1313 case P_PROMPT:
1314 return "prompt";
1315 case P_COMMENT:
1316 return "comment";
1317 case P_MENU:
1318 return "menu";
1319 case P_DEFAULT:
1320 return "default";
1321 case P_SELECT:
1322 return "select";
1323 case P_IMPLY:
1324 return "imply";
1325 case P_RANGE:
1326 return "range";
1327 case P_UNKNOWN:
1328 break;
1329 }
1330 return "unknown";
1331 }
1332