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