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