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