xref: /openbmc/linux/scripts/kconfig/symbol.c (revision f8f69dc0b4e070d4a4d39889a85424913cc922d5)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
31da177e4SLinus Torvalds  * Released under the terms of the GNU GPL v2.0.
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <ctype.h>
71da177e4SLinus Torvalds #include <stdlib.h>
81da177e4SLinus Torvalds #include <string.h>
91da177e4SLinus Torvalds #include <regex.h>
101da177e4SLinus Torvalds #include <sys/utsname.h>
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include "lkc.h"
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds struct symbol symbol_yes = {
151da177e4SLinus Torvalds 	.name = "y",
161da177e4SLinus Torvalds 	.curr = { "y", yes },
17c0e150acSRoman Zippel 	.flags = SYMBOL_CONST|SYMBOL_VALID,
181da177e4SLinus Torvalds }, symbol_mod = {
191da177e4SLinus Torvalds 	.name = "m",
201da177e4SLinus Torvalds 	.curr = { "m", mod },
21c0e150acSRoman Zippel 	.flags = SYMBOL_CONST|SYMBOL_VALID,
221da177e4SLinus Torvalds }, symbol_no = {
231da177e4SLinus Torvalds 	.name = "n",
241da177e4SLinus Torvalds 	.curr = { "n", no },
25c0e150acSRoman Zippel 	.flags = SYMBOL_CONST|SYMBOL_VALID,
261da177e4SLinus Torvalds }, symbol_empty = {
271da177e4SLinus Torvalds 	.name = "",
281da177e4SLinus Torvalds 	.curr = { "", no },
291da177e4SLinus Torvalds 	.flags = SYMBOL_VALID,
301da177e4SLinus Torvalds };
311da177e4SLinus Torvalds 
32face4374SRoman Zippel struct symbol *sym_defconfig_list;
331da177e4SLinus Torvalds struct symbol *modules_sym;
341da177e4SLinus Torvalds tristate modules_val;
351da177e4SLinus Torvalds 
3693449082SRoman Zippel struct expr *sym_env_list;
3793449082SRoman Zippel 
384356f489STrevor Keith static void sym_add_default(struct symbol *sym, const char *def)
391da177e4SLinus Torvalds {
401da177e4SLinus Torvalds 	struct property *prop = prop_alloc(P_DEFAULT, sym);
411da177e4SLinus Torvalds 
425a1aa8a1SRoman Zippel 	prop->expr = expr_alloc_symbol(sym_lookup(def, SYMBOL_CONST));
431da177e4SLinus Torvalds }
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds void sym_init(void)
461da177e4SLinus Torvalds {
471da177e4SLinus Torvalds 	struct symbol *sym;
481da177e4SLinus Torvalds 	struct utsname uts;
491da177e4SLinus Torvalds 	static bool inited = false;
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 	if (inited)
521da177e4SLinus Torvalds 		return;
531da177e4SLinus Torvalds 	inited = true;
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	uname(&uts);
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds 	sym = sym_lookup("UNAME_RELEASE", 0);
581da177e4SLinus Torvalds 	sym->type = S_STRING;
591da177e4SLinus Torvalds 	sym->flags |= SYMBOL_AUTO;
601da177e4SLinus Torvalds 	sym_add_default(sym, uts.release);
611da177e4SLinus Torvalds }
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds enum symbol_type sym_get_type(struct symbol *sym)
641da177e4SLinus Torvalds {
651da177e4SLinus Torvalds 	enum symbol_type type = sym->type;
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 	if (type == S_TRISTATE) {
681da177e4SLinus Torvalds 		if (sym_is_choice_value(sym) && sym->visible == yes)
691da177e4SLinus Torvalds 			type = S_BOOLEAN;
701da177e4SLinus Torvalds 		else if (modules_val == no)
711da177e4SLinus Torvalds 			type = S_BOOLEAN;
721da177e4SLinus Torvalds 	}
731da177e4SLinus Torvalds 	return type;
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds const char *sym_type_name(enum symbol_type type)
771da177e4SLinus Torvalds {
781da177e4SLinus Torvalds 	switch (type) {
791da177e4SLinus Torvalds 	case S_BOOLEAN:
80b92d804aSMasahiro Yamada 		return "bool";
811da177e4SLinus Torvalds 	case S_TRISTATE:
821da177e4SLinus Torvalds 		return "tristate";
831da177e4SLinus Torvalds 	case S_INT:
841da177e4SLinus Torvalds 		return "integer";
851da177e4SLinus Torvalds 	case S_HEX:
861da177e4SLinus Torvalds 		return "hex";
871da177e4SLinus Torvalds 	case S_STRING:
881da177e4SLinus Torvalds 		return "string";
891da177e4SLinus Torvalds 	case S_UNKNOWN:
901da177e4SLinus Torvalds 		return "unknown";
911da177e4SLinus Torvalds 	case S_OTHER:
921da177e4SLinus Torvalds 		break;
931da177e4SLinus Torvalds 	}
941da177e4SLinus Torvalds 	return "???";
951da177e4SLinus Torvalds }
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds struct property *sym_get_choice_prop(struct symbol *sym)
981da177e4SLinus Torvalds {
991da177e4SLinus Torvalds 	struct property *prop;
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	for_all_choices(sym, prop)
1021da177e4SLinus Torvalds 		return prop;
1031da177e4SLinus Torvalds 	return NULL;
1041da177e4SLinus Torvalds }
1051da177e4SLinus Torvalds 
10693449082SRoman Zippel struct property *sym_get_env_prop(struct symbol *sym)
10793449082SRoman Zippel {
10893449082SRoman Zippel 	struct property *prop;
10993449082SRoman Zippel 
11093449082SRoman Zippel 	for_all_properties(sym, prop, P_ENV)
11193449082SRoman Zippel 		return prop;
11293449082SRoman Zippel 	return NULL;
11393449082SRoman Zippel }
11493449082SRoman Zippel 
115ad8d40cdSMichal Marek static struct property *sym_get_default_prop(struct symbol *sym)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	struct property *prop;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 	for_all_defaults(sym, prop) {
1201da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
1211da177e4SLinus Torvalds 		if (prop->visible.tri != no)
1221da177e4SLinus Torvalds 			return prop;
1231da177e4SLinus Torvalds 	}
1241da177e4SLinus Torvalds 	return NULL;
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
1274356f489STrevor Keith static struct property *sym_get_range_prop(struct symbol *sym)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	struct property *prop;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	for_all_properties(sym, prop, P_RANGE) {
1321da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
1331da177e4SLinus Torvalds 		if (prop->visible.tri != no)
1341da177e4SLinus Torvalds 			return prop;
1351da177e4SLinus Torvalds 	}
1361da177e4SLinus Torvalds 	return NULL;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
139129784abSKees Cook static long long sym_get_range_val(struct symbol *sym, int base)
1404cf3cbe2SRoman Zippel {
1414cf3cbe2SRoman Zippel 	sym_calc_value(sym);
1424cf3cbe2SRoman Zippel 	switch (sym->type) {
1434cf3cbe2SRoman Zippel 	case S_INT:
1444cf3cbe2SRoman Zippel 		base = 10;
1454cf3cbe2SRoman Zippel 		break;
1464cf3cbe2SRoman Zippel 	case S_HEX:
1474cf3cbe2SRoman Zippel 		base = 16;
1484cf3cbe2SRoman Zippel 		break;
1494cf3cbe2SRoman Zippel 	default:
1504cf3cbe2SRoman Zippel 		break;
1514cf3cbe2SRoman Zippel 	}
152129784abSKees Cook 	return strtoll(sym->curr.val, NULL, base);
1534cf3cbe2SRoman Zippel }
1544cf3cbe2SRoman Zippel 
1554cf3cbe2SRoman Zippel static void sym_validate_range(struct symbol *sym)
1564cf3cbe2SRoman Zippel {
1574cf3cbe2SRoman Zippel 	struct property *prop;
158129784abSKees Cook 	int base;
159129784abSKees Cook 	long long val, val2;
1604cf3cbe2SRoman Zippel 	char str[64];
1614cf3cbe2SRoman Zippel 
1624cf3cbe2SRoman Zippel 	switch (sym->type) {
1634cf3cbe2SRoman Zippel 	case S_INT:
1644cf3cbe2SRoman Zippel 		base = 10;
1654cf3cbe2SRoman Zippel 		break;
1664cf3cbe2SRoman Zippel 	case S_HEX:
1674cf3cbe2SRoman Zippel 		base = 16;
1684cf3cbe2SRoman Zippel 		break;
1694cf3cbe2SRoman Zippel 	default:
1704cf3cbe2SRoman Zippel 		return;
1714cf3cbe2SRoman Zippel 	}
1724cf3cbe2SRoman Zippel 	prop = sym_get_range_prop(sym);
1734cf3cbe2SRoman Zippel 	if (!prop)
1744cf3cbe2SRoman Zippel 		return;
175129784abSKees Cook 	val = strtoll(sym->curr.val, NULL, base);
1764cf3cbe2SRoman Zippel 	val2 = sym_get_range_val(prop->expr->left.sym, base);
1774cf3cbe2SRoman Zippel 	if (val >= val2) {
1784cf3cbe2SRoman Zippel 		val2 = sym_get_range_val(prop->expr->right.sym, base);
1794cf3cbe2SRoman Zippel 		if (val <= val2)
1804cf3cbe2SRoman Zippel 			return;
1814cf3cbe2SRoman Zippel 	}
1824cf3cbe2SRoman Zippel 	if (sym->type == S_INT)
183129784abSKees Cook 		sprintf(str, "%lld", val2);
1844cf3cbe2SRoman Zippel 	else
185129784abSKees Cook 		sprintf(str, "0x%llx", val2);
186cd81fc82SMasahiro Yamada 	sym->curr.val = xstrdup(str);
1874cf3cbe2SRoman Zippel }
1884cf3cbe2SRoman Zippel 
189ad8d40cdSMichal Marek static void sym_set_changed(struct symbol *sym)
190ad8d40cdSMichal Marek {
191ad8d40cdSMichal Marek 	struct property *prop;
192ad8d40cdSMichal Marek 
193ad8d40cdSMichal Marek 	sym->flags |= SYMBOL_CHANGED;
194ad8d40cdSMichal Marek 	for (prop = sym->prop; prop; prop = prop->next) {
195ad8d40cdSMichal Marek 		if (prop->menu)
196ad8d40cdSMichal Marek 			prop->menu->flags |= MENU_CHANGED;
197ad8d40cdSMichal Marek 	}
198ad8d40cdSMichal Marek }
199ad8d40cdSMichal Marek 
200ad8d40cdSMichal Marek static void sym_set_all_changed(void)
201ad8d40cdSMichal Marek {
202ad8d40cdSMichal Marek 	struct symbol *sym;
203ad8d40cdSMichal Marek 	int i;
204ad8d40cdSMichal Marek 
205ad8d40cdSMichal Marek 	for_all_symbols(i, sym)
206ad8d40cdSMichal Marek 		sym_set_changed(sym);
207ad8d40cdSMichal Marek }
208ad8d40cdSMichal Marek 
2091da177e4SLinus Torvalds static void sym_calc_visibility(struct symbol *sym)
2101da177e4SLinus Torvalds {
2111da177e4SLinus Torvalds 	struct property *prop;
212fa64e5f6SDirk Gouders 	struct symbol *choice_sym = NULL;
2131da177e4SLinus Torvalds 	tristate tri;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	/* any prompt visible? */
2161da177e4SLinus Torvalds 	tri = no;
217fa64e5f6SDirk Gouders 
218fa64e5f6SDirk Gouders 	if (sym_is_choice_value(sym))
219fa64e5f6SDirk Gouders 		choice_sym = prop_get_symbol(sym_get_choice_prop(sym));
220fa64e5f6SDirk Gouders 
2211da177e4SLinus Torvalds 	for_all_prompts(sym, prop) {
2221da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
223fa64e5f6SDirk Gouders 		/*
224fa64e5f6SDirk Gouders 		 * Tristate choice_values with visibility 'mod' are
225fa64e5f6SDirk Gouders 		 * not visible if the corresponding choice's value is
226fa64e5f6SDirk Gouders 		 * 'yes'.
227fa64e5f6SDirk Gouders 		 */
228fa64e5f6SDirk Gouders 		if (choice_sym && sym->type == S_TRISTATE &&
229fa64e5f6SDirk Gouders 		    prop->visible.tri == mod && choice_sym->curr.tri == yes)
230fa64e5f6SDirk Gouders 			prop->visible.tri = no;
231fa64e5f6SDirk Gouders 
232d6ee3576SSam Ravnborg 		tri = EXPR_OR(tri, prop->visible.tri);
2331da177e4SLinus Torvalds 	}
2341da177e4SLinus Torvalds 	if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
2351da177e4SLinus Torvalds 		tri = yes;
2361da177e4SLinus Torvalds 	if (sym->visible != tri) {
2371da177e4SLinus Torvalds 		sym->visible = tri;
2381da177e4SLinus Torvalds 		sym_set_changed(sym);
2391da177e4SLinus Torvalds 	}
2401da177e4SLinus Torvalds 	if (sym_is_choice_value(sym))
2411da177e4SLinus Torvalds 		return;
242246cf9c2SCatalin Marinas 	/* defaulting to "yes" if no explicit "depends on" are given */
243246cf9c2SCatalin Marinas 	tri = yes;
244246cf9c2SCatalin Marinas 	if (sym->dir_dep.expr)
245246cf9c2SCatalin Marinas 		tri = expr_calc_value(sym->dir_dep.expr);
246f622f827SMasahiro Yamada 	if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
247246cf9c2SCatalin Marinas 		tri = yes;
248246cf9c2SCatalin Marinas 	if (sym->dir_dep.tri != tri) {
249246cf9c2SCatalin Marinas 		sym->dir_dep.tri = tri;
250246cf9c2SCatalin Marinas 		sym_set_changed(sym);
251246cf9c2SCatalin Marinas 	}
2521da177e4SLinus Torvalds 	tri = no;
2531da177e4SLinus Torvalds 	if (sym->rev_dep.expr)
2541da177e4SLinus Torvalds 		tri = expr_calc_value(sym->rev_dep.expr);
2551da177e4SLinus Torvalds 	if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
2561da177e4SLinus Torvalds 		tri = yes;
2571da177e4SLinus Torvalds 	if (sym->rev_dep.tri != tri) {
2581da177e4SLinus Torvalds 		sym->rev_dep.tri = tri;
2591da177e4SLinus Torvalds 		sym_set_changed(sym);
2601da177e4SLinus Torvalds 	}
261237e3ad0SNicolas Pitre 	tri = no;
262237e3ad0SNicolas Pitre 	if (sym->implied.expr && sym->dir_dep.tri != no)
263237e3ad0SNicolas Pitre 		tri = expr_calc_value(sym->implied.expr);
264237e3ad0SNicolas Pitre 	if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
265237e3ad0SNicolas Pitre 		tri = yes;
266237e3ad0SNicolas Pitre 	if (sym->implied.tri != tri) {
267237e3ad0SNicolas Pitre 		sym->implied.tri = tri;
268237e3ad0SNicolas Pitre 		sym_set_changed(sym);
269237e3ad0SNicolas Pitre 	}
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
272c252147dSSam Ravnborg /*
273c252147dSSam Ravnborg  * Find the default symbol for a choice.
274c252147dSSam Ravnborg  * First try the default values for the choice symbol
275c252147dSSam Ravnborg  * Next locate the first visible choice value
276c252147dSSam Ravnborg  * Return NULL if none was found
277c252147dSSam Ravnborg  */
278c252147dSSam Ravnborg struct symbol *sym_choice_default(struct symbol *sym)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	struct symbol *def_sym;
2811da177e4SLinus Torvalds 	struct property *prop;
2821da177e4SLinus Torvalds 	struct expr *e;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	/* any of the defaults visible? */
2851da177e4SLinus Torvalds 	for_all_defaults(sym, prop) {
2861da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
2871da177e4SLinus Torvalds 		if (prop->visible.tri == no)
2881da177e4SLinus Torvalds 			continue;
2891da177e4SLinus Torvalds 		def_sym = prop_get_symbol(prop);
2901da177e4SLinus Torvalds 		if (def_sym->visible != no)
2911da177e4SLinus Torvalds 			return def_sym;
2921da177e4SLinus Torvalds 	}
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	/* just get the first visible value */
2951da177e4SLinus Torvalds 	prop = sym_get_choice_prop(sym);
2960a28c47bSJan Beulich 	expr_list_for_each_sym(prop->expr, e, def_sym)
2971da177e4SLinus Torvalds 		if (def_sym->visible != no)
2981da177e4SLinus Torvalds 			return def_sym;
2991da177e4SLinus Torvalds 
300c252147dSSam Ravnborg 	/* failed to locate any defaults */
301c252147dSSam Ravnborg 	return NULL;
302c252147dSSam Ravnborg }
303c252147dSSam Ravnborg 
304c252147dSSam Ravnborg static struct symbol *sym_calc_choice(struct symbol *sym)
305c252147dSSam Ravnborg {
306c252147dSSam Ravnborg 	struct symbol *def_sym;
307c252147dSSam Ravnborg 	struct property *prop;
308c252147dSSam Ravnborg 	struct expr *e;
3095d09598dSArnaud Lacombe 	int flags;
310c252147dSSam Ravnborg 
311c252147dSSam Ravnborg 	/* first calculate all choice values' visibilities */
3125d09598dSArnaud Lacombe 	flags = sym->flags;
313c252147dSSam Ravnborg 	prop = sym_get_choice_prop(sym);
3145d09598dSArnaud Lacombe 	expr_list_for_each_sym(prop->expr, e, def_sym) {
315c252147dSSam Ravnborg 		sym_calc_visibility(def_sym);
3165d09598dSArnaud Lacombe 		if (def_sym->visible != no)
3175d09598dSArnaud Lacombe 			flags &= def_sym->flags;
3185d09598dSArnaud Lacombe 	}
3195d09598dSArnaud Lacombe 
3205d09598dSArnaud Lacombe 	sym->flags &= flags | ~SYMBOL_DEF_USER;
321c252147dSSam Ravnborg 
322c252147dSSam Ravnborg 	/* is the user choice visible? */
323c252147dSSam Ravnborg 	def_sym = sym->def[S_DEF_USER].val;
324c252147dSSam Ravnborg 	if (def_sym && def_sym->visible != no)
325c252147dSSam Ravnborg 		return def_sym;
326c252147dSSam Ravnborg 
327c252147dSSam Ravnborg 	def_sym = sym_choice_default(sym);
328c252147dSSam Ravnborg 
329c252147dSSam Ravnborg 	if (def_sym == NULL)
3301da177e4SLinus Torvalds 		/* no choice? reset tristate value */
3311da177e4SLinus Torvalds 		sym->curr.tri = no;
332c252147dSSam Ravnborg 
333c252147dSSam Ravnborg 	return def_sym;
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
336*f8f69dc0SMasahiro Yamada static void sym_warn_unmet_dep(struct symbol *sym)
337*f8f69dc0SMasahiro Yamada {
338*f8f69dc0SMasahiro Yamada 	struct gstr gs = str_new();
339*f8f69dc0SMasahiro Yamada 
340*f8f69dc0SMasahiro Yamada 	str_printf(&gs,
341*f8f69dc0SMasahiro Yamada 		   "\nWARNING: unmet direct dependencies detected for %s\n",
342*f8f69dc0SMasahiro Yamada 		   sym->name);
343*f8f69dc0SMasahiro Yamada 	str_printf(&gs,
344*f8f69dc0SMasahiro Yamada 		   "  Depends on [%c]: ",
345*f8f69dc0SMasahiro Yamada 		   sym->dir_dep.tri == mod ? 'm' : 'n');
346*f8f69dc0SMasahiro Yamada 	expr_gstr_print(sym->dir_dep.expr, &gs);
347*f8f69dc0SMasahiro Yamada 	str_printf(&gs, "\n");
348*f8f69dc0SMasahiro Yamada 
349*f8f69dc0SMasahiro Yamada 	expr_gstr_print_revdep(sym->rev_dep.expr, &gs, yes,
350*f8f69dc0SMasahiro Yamada 			       "  Selected by [y]:\n");
351*f8f69dc0SMasahiro Yamada 	expr_gstr_print_revdep(sym->rev_dep.expr, &gs, mod,
352*f8f69dc0SMasahiro Yamada 			       "  Selected by [m]:\n");
353*f8f69dc0SMasahiro Yamada 
354*f8f69dc0SMasahiro Yamada 	fputs(str_get(&gs), stderr);
355*f8f69dc0SMasahiro Yamada }
356*f8f69dc0SMasahiro Yamada 
3571da177e4SLinus Torvalds void sym_calc_value(struct symbol *sym)
3581da177e4SLinus Torvalds {
3591da177e4SLinus Torvalds 	struct symbol_value newval, oldval;
3601da177e4SLinus Torvalds 	struct property *prop;
3611da177e4SLinus Torvalds 	struct expr *e;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	if (!sym)
3641da177e4SLinus Torvalds 		return;
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	if (sym->flags & SYMBOL_VALID)
3671da177e4SLinus Torvalds 		return;
368fbe98bb9SArve Hjønnevåg 
369fbe98bb9SArve Hjønnevåg 	if (sym_is_choice_value(sym) &&
370fbe98bb9SArve Hjønnevåg 	    sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES) {
371fbe98bb9SArve Hjønnevåg 		sym->flags &= ~SYMBOL_NEED_SET_CHOICE_VALUES;
372fbe98bb9SArve Hjønnevåg 		prop = sym_get_choice_prop(sym);
373fbe98bb9SArve Hjønnevåg 		sym_calc_value(prop_get_symbol(prop));
374fbe98bb9SArve Hjønnevåg 	}
375fbe98bb9SArve Hjønnevåg 
3761da177e4SLinus Torvalds 	sym->flags |= SYMBOL_VALID;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	oldval = sym->curr;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	switch (sym->type) {
3811da177e4SLinus Torvalds 	case S_INT:
3821da177e4SLinus Torvalds 	case S_HEX:
3831da177e4SLinus Torvalds 	case S_STRING:
3841da177e4SLinus Torvalds 		newval = symbol_empty.curr;
3851da177e4SLinus Torvalds 		break;
3861da177e4SLinus Torvalds 	case S_BOOLEAN:
3871da177e4SLinus Torvalds 	case S_TRISTATE:
3881da177e4SLinus Torvalds 		newval = symbol_no.curr;
3891da177e4SLinus Torvalds 		break;
3901da177e4SLinus Torvalds 	default:
3911da177e4SLinus Torvalds 		sym->curr.val = sym->name;
3921da177e4SLinus Torvalds 		sym->curr.tri = no;
3931da177e4SLinus Torvalds 		return;
3941da177e4SLinus Torvalds 	}
3951da177e4SLinus Torvalds 	sym->flags &= ~SYMBOL_WRITE;
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	sym_calc_visibility(sym);
3981da177e4SLinus Torvalds 
399cb67ab2cSMasahiro Yamada 	if (sym->visible != no)
400cb67ab2cSMasahiro Yamada 		sym->flags |= SYMBOL_WRITE;
401cb67ab2cSMasahiro Yamada 
4021da177e4SLinus Torvalds 	/* set default if recursively called */
4031da177e4SLinus Torvalds 	sym->curr = newval;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	switch (sym_get_type(sym)) {
4061da177e4SLinus Torvalds 	case S_BOOLEAN:
4071da177e4SLinus Torvalds 	case S_TRISTATE:
4081da177e4SLinus Torvalds 		if (sym_is_choice_value(sym) && sym->visible == yes) {
4091da177e4SLinus Torvalds 			prop = sym_get_choice_prop(sym);
4101da177e4SLinus Torvalds 			newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
411587c9061SRoman Zippel 		} else {
412587c9061SRoman Zippel 			if (sym->visible != no) {
413587c9061SRoman Zippel 				/* if the symbol is visible use the user value
414587c9061SRoman Zippel 				 * if available, otherwise try the default value
415587c9061SRoman Zippel 				 */
416587c9061SRoman Zippel 				if (sym_has_value(sym)) {
417587c9061SRoman Zippel 					newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
418587c9061SRoman Zippel 							      sym->visible);
419587c9061SRoman Zippel 					goto calc_newval;
4201da177e4SLinus Torvalds 				}
421587c9061SRoman Zippel 			}
422587c9061SRoman Zippel 			if (sym->rev_dep.tri != no)
423587c9061SRoman Zippel 				sym->flags |= SYMBOL_WRITE;
424587c9061SRoman Zippel 			if (!sym_is_choice(sym)) {
4251da177e4SLinus Torvalds 				prop = sym_get_default_prop(sym);
4261da177e4SLinus Torvalds 				if (prop) {
427587c9061SRoman Zippel 					newval.tri = EXPR_AND(expr_calc_value(prop->expr),
428587c9061SRoman Zippel 							      prop->visible.tri);
429f467c564SUlf Magnusson 					if (newval.tri != no)
430f467c564SUlf Magnusson 						sym->flags |= SYMBOL_WRITE;
4311da177e4SLinus Torvalds 				}
432237e3ad0SNicolas Pitre 				if (sym->implied.tri != no) {
433237e3ad0SNicolas Pitre 					sym->flags |= SYMBOL_WRITE;
434237e3ad0SNicolas Pitre 					newval.tri = EXPR_OR(newval.tri, sym->implied.tri);
435237e3ad0SNicolas Pitre 				}
4361da177e4SLinus Torvalds 			}
437587c9061SRoman Zippel 		calc_newval:
438*f8f69dc0SMasahiro Yamada 			if (sym->dir_dep.tri < sym->rev_dep.tri)
439*f8f69dc0SMasahiro Yamada 				sym_warn_unmet_dep(sym);
440587c9061SRoman Zippel 			newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
441587c9061SRoman Zippel 		}
442237e3ad0SNicolas Pitre 		if (newval.tri == mod &&
443237e3ad0SNicolas Pitre 		    (sym_get_type(sym) == S_BOOLEAN || sym->implied.tri == yes))
4441da177e4SLinus Torvalds 			newval.tri = yes;
4451da177e4SLinus Torvalds 		break;
4461da177e4SLinus Torvalds 	case S_STRING:
4471da177e4SLinus Torvalds 	case S_HEX:
4481da177e4SLinus Torvalds 	case S_INT:
449cb67ab2cSMasahiro Yamada 		if (sym->visible != no && sym_has_value(sym)) {
4500c1822e6SRoman Zippel 			newval.val = sym->def[S_DEF_USER].val;
4511da177e4SLinus Torvalds 			break;
4521da177e4SLinus Torvalds 		}
4531da177e4SLinus Torvalds 		prop = sym_get_default_prop(sym);
4541da177e4SLinus Torvalds 		if (prop) {
4551da177e4SLinus Torvalds 			struct symbol *ds = prop_get_symbol(prop);
4561da177e4SLinus Torvalds 			if (ds) {
4571da177e4SLinus Torvalds 				sym->flags |= SYMBOL_WRITE;
4581da177e4SLinus Torvalds 				sym_calc_value(ds);
4591da177e4SLinus Torvalds 				newval.val = ds->curr.val;
4601da177e4SLinus Torvalds 			}
4611da177e4SLinus Torvalds 		}
4621da177e4SLinus Torvalds 		break;
4631da177e4SLinus Torvalds 	default:
4641da177e4SLinus Torvalds 		;
4651da177e4SLinus Torvalds 	}
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	sym->curr = newval;
4681da177e4SLinus Torvalds 	if (sym_is_choice(sym) && newval.tri == yes)
4691da177e4SLinus Torvalds 		sym->curr.val = sym_calc_choice(sym);
4704cf3cbe2SRoman Zippel 	sym_validate_range(sym);
4711da177e4SLinus Torvalds 
472face4374SRoman Zippel 	if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
4731da177e4SLinus Torvalds 		sym_set_changed(sym);
474face4374SRoman Zippel 		if (modules_sym == sym) {
475face4374SRoman Zippel 			sym_set_all_changed();
4761da177e4SLinus Torvalds 			modules_val = modules_sym->curr.tri;
477face4374SRoman Zippel 		}
478face4374SRoman Zippel 	}
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	if (sym_is_choice(sym)) {
4817a962923SRoman Zippel 		struct symbol *choice_sym;
4827a962923SRoman Zippel 
4831da177e4SLinus Torvalds 		prop = sym_get_choice_prop(sym);
4847a962923SRoman Zippel 		expr_list_for_each_sym(prop->expr, e, choice_sym) {
4850a28c47bSJan Beulich 			if ((sym->flags & SYMBOL_WRITE) &&
4860a28c47bSJan Beulich 			    choice_sym->visible != no)
4870a28c47bSJan Beulich 				choice_sym->flags |= SYMBOL_WRITE;
4880a28c47bSJan Beulich 			if (sym->flags & SYMBOL_CHANGED)
4897a962923SRoman Zippel 				sym_set_changed(choice_sym);
4901da177e4SLinus Torvalds 		}
4911da177e4SLinus Torvalds 	}
4925a1aa8a1SRoman Zippel 
4935a1aa8a1SRoman Zippel 	if (sym->flags & SYMBOL_AUTO)
4945a1aa8a1SRoman Zippel 		sym->flags &= ~SYMBOL_WRITE;
495fbe98bb9SArve Hjønnevåg 
496fbe98bb9SArve Hjønnevåg 	if (sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES)
497fbe98bb9SArve Hjønnevåg 		set_all_choice_values(sym);
4981da177e4SLinus Torvalds }
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds void sym_clear_all_valid(void)
5011da177e4SLinus Torvalds {
5021da177e4SLinus Torvalds 	struct symbol *sym;
5031da177e4SLinus Torvalds 	int i;
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	for_all_symbols(i, sym)
5061da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_VALID;
507bfc10001SKarsten Wiese 	sym_add_change_count(1);
5081da177e4SLinus Torvalds 	sym_calc_value(modules_sym);
5091da177e4SLinus Torvalds }
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds bool sym_tristate_within_range(struct symbol *sym, tristate val)
5121da177e4SLinus Torvalds {
5131da177e4SLinus Torvalds 	int type = sym_get_type(sym);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	if (sym->visible == no)
5161da177e4SLinus Torvalds 		return false;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	if (type != S_BOOLEAN && type != S_TRISTATE)
5191da177e4SLinus Torvalds 		return false;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	if (type == S_BOOLEAN && val == mod)
5221da177e4SLinus Torvalds 		return false;
5231da177e4SLinus Torvalds 	if (sym->visible <= sym->rev_dep.tri)
5241da177e4SLinus Torvalds 		return false;
525237e3ad0SNicolas Pitre 	if (sym->implied.tri == yes && val == mod)
526237e3ad0SNicolas Pitre 		return false;
5271da177e4SLinus Torvalds 	if (sym_is_choice_value(sym) && sym->visible == yes)
5281da177e4SLinus Torvalds 		return val == yes;
5291da177e4SLinus Torvalds 	return val >= sym->rev_dep.tri && val <= sym->visible;
5301da177e4SLinus Torvalds }
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds bool sym_set_tristate_value(struct symbol *sym, tristate val)
5331da177e4SLinus Torvalds {
5341da177e4SLinus Torvalds 	tristate oldval = sym_get_tristate_value(sym);
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	if (oldval != val && !sym_tristate_within_range(sym, val))
5371da177e4SLinus Torvalds 		return false;
5381da177e4SLinus Torvalds 
539669bfad9SRoman Zippel 	if (!(sym->flags & SYMBOL_DEF_USER)) {
540669bfad9SRoman Zippel 		sym->flags |= SYMBOL_DEF_USER;
5411da177e4SLinus Torvalds 		sym_set_changed(sym);
5421da177e4SLinus Torvalds 	}
5433f23ca2bSRoman Zippel 	/*
5443f23ca2bSRoman Zippel 	 * setting a choice value also resets the new flag of the choice
5453f23ca2bSRoman Zippel 	 * symbol and all other choice values.
5463f23ca2bSRoman Zippel 	 */
5471da177e4SLinus Torvalds 	if (sym_is_choice_value(sym) && val == yes) {
5481da177e4SLinus Torvalds 		struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
5493f23ca2bSRoman Zippel 		struct property *prop;
5503f23ca2bSRoman Zippel 		struct expr *e;
5511da177e4SLinus Torvalds 
5520c1822e6SRoman Zippel 		cs->def[S_DEF_USER].val = sym;
553669bfad9SRoman Zippel 		cs->flags |= SYMBOL_DEF_USER;
5543f23ca2bSRoman Zippel 		prop = sym_get_choice_prop(cs);
5553f23ca2bSRoman Zippel 		for (e = prop->expr; e; e = e->left.expr) {
5563f23ca2bSRoman Zippel 			if (e->right.sym->visible != no)
557669bfad9SRoman Zippel 				e->right.sym->flags |= SYMBOL_DEF_USER;
5583f23ca2bSRoman Zippel 		}
5591da177e4SLinus Torvalds 	}
5601da177e4SLinus Torvalds 
5610c1822e6SRoman Zippel 	sym->def[S_DEF_USER].tri = val;
562face4374SRoman Zippel 	if (oldval != val)
5631da177e4SLinus Torvalds 		sym_clear_all_valid();
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	return true;
5661da177e4SLinus Torvalds }
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds tristate sym_toggle_tristate_value(struct symbol *sym)
5691da177e4SLinus Torvalds {
5701da177e4SLinus Torvalds 	tristate oldval, newval;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	oldval = newval = sym_get_tristate_value(sym);
5731da177e4SLinus Torvalds 	do {
5741da177e4SLinus Torvalds 		switch (newval) {
5751da177e4SLinus Torvalds 		case no:
5761da177e4SLinus Torvalds 			newval = mod;
5771da177e4SLinus Torvalds 			break;
5781da177e4SLinus Torvalds 		case mod:
5791da177e4SLinus Torvalds 			newval = yes;
5801da177e4SLinus Torvalds 			break;
5811da177e4SLinus Torvalds 		case yes:
5821da177e4SLinus Torvalds 			newval = no;
5831da177e4SLinus Torvalds 			break;
5841da177e4SLinus Torvalds 		}
5851da177e4SLinus Torvalds 		if (sym_set_tristate_value(sym, newval))
5861da177e4SLinus Torvalds 			break;
5871da177e4SLinus Torvalds 	} while (oldval != newval);
5881da177e4SLinus Torvalds 	return newval;
5891da177e4SLinus Torvalds }
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds bool sym_string_valid(struct symbol *sym, const char *str)
5921da177e4SLinus Torvalds {
5931da177e4SLinus Torvalds 	signed char ch;
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	switch (sym->type) {
5961da177e4SLinus Torvalds 	case S_STRING:
5971da177e4SLinus Torvalds 		return true;
5981da177e4SLinus Torvalds 	case S_INT:
5991da177e4SLinus Torvalds 		ch = *str++;
6001da177e4SLinus Torvalds 		if (ch == '-')
6011da177e4SLinus Torvalds 			ch = *str++;
6021da177e4SLinus Torvalds 		if (!isdigit(ch))
6031da177e4SLinus Torvalds 			return false;
6041da177e4SLinus Torvalds 		if (ch == '0' && *str != 0)
6051da177e4SLinus Torvalds 			return false;
6061da177e4SLinus Torvalds 		while ((ch = *str++)) {
6071da177e4SLinus Torvalds 			if (!isdigit(ch))
6081da177e4SLinus Torvalds 				return false;
6091da177e4SLinus Torvalds 		}
6101da177e4SLinus Torvalds 		return true;
6111da177e4SLinus Torvalds 	case S_HEX:
6121da177e4SLinus Torvalds 		if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
6131da177e4SLinus Torvalds 			str += 2;
6141da177e4SLinus Torvalds 		ch = *str++;
6151da177e4SLinus Torvalds 		do {
6161da177e4SLinus Torvalds 			if (!isxdigit(ch))
6171da177e4SLinus Torvalds 				return false;
6181da177e4SLinus Torvalds 		} while ((ch = *str++));
6191da177e4SLinus Torvalds 		return true;
6201da177e4SLinus Torvalds 	case S_BOOLEAN:
6211da177e4SLinus Torvalds 	case S_TRISTATE:
6221da177e4SLinus Torvalds 		switch (str[0]) {
6231da177e4SLinus Torvalds 		case 'y': case 'Y':
6241da177e4SLinus Torvalds 		case 'm': case 'M':
6251da177e4SLinus Torvalds 		case 'n': case 'N':
6261da177e4SLinus Torvalds 			return true;
6271da177e4SLinus Torvalds 		}
6281da177e4SLinus Torvalds 		return false;
6291da177e4SLinus Torvalds 	default:
6301da177e4SLinus Torvalds 		return false;
6311da177e4SLinus Torvalds 	}
6321da177e4SLinus Torvalds }
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds bool sym_string_within_range(struct symbol *sym, const char *str)
6351da177e4SLinus Torvalds {
6361da177e4SLinus Torvalds 	struct property *prop;
637129784abSKees Cook 	long long val;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	switch (sym->type) {
6401da177e4SLinus Torvalds 	case S_STRING:
6411da177e4SLinus Torvalds 		return sym_string_valid(sym, str);
6421da177e4SLinus Torvalds 	case S_INT:
6431da177e4SLinus Torvalds 		if (!sym_string_valid(sym, str))
6441da177e4SLinus Torvalds 			return false;
6451da177e4SLinus Torvalds 		prop = sym_get_range_prop(sym);
6461da177e4SLinus Torvalds 		if (!prop)
6471da177e4SLinus Torvalds 			return true;
648129784abSKees Cook 		val = strtoll(str, NULL, 10);
6494cf3cbe2SRoman Zippel 		return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
6504cf3cbe2SRoman Zippel 		       val <= sym_get_range_val(prop->expr->right.sym, 10);
6511da177e4SLinus Torvalds 	case S_HEX:
6521da177e4SLinus Torvalds 		if (!sym_string_valid(sym, str))
6531da177e4SLinus Torvalds 			return false;
6541da177e4SLinus Torvalds 		prop = sym_get_range_prop(sym);
6551da177e4SLinus Torvalds 		if (!prop)
6561da177e4SLinus Torvalds 			return true;
657129784abSKees Cook 		val = strtoll(str, NULL, 16);
6584cf3cbe2SRoman Zippel 		return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
6594cf3cbe2SRoman Zippel 		       val <= sym_get_range_val(prop->expr->right.sym, 16);
6601da177e4SLinus Torvalds 	case S_BOOLEAN:
6611da177e4SLinus Torvalds 	case S_TRISTATE:
6621da177e4SLinus Torvalds 		switch (str[0]) {
6631da177e4SLinus Torvalds 		case 'y': case 'Y':
6641da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, yes);
6651da177e4SLinus Torvalds 		case 'm': case 'M':
6661da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, mod);
6671da177e4SLinus Torvalds 		case 'n': case 'N':
6681da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, no);
6691da177e4SLinus Torvalds 		}
6701da177e4SLinus Torvalds 		return false;
6711da177e4SLinus Torvalds 	default:
6721da177e4SLinus Torvalds 		return false;
6731da177e4SLinus Torvalds 	}
6741da177e4SLinus Torvalds }
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds bool sym_set_string_value(struct symbol *sym, const char *newval)
6771da177e4SLinus Torvalds {
6781da177e4SLinus Torvalds 	const char *oldval;
6791da177e4SLinus Torvalds 	char *val;
6801da177e4SLinus Torvalds 	int size;
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	switch (sym->type) {
6831da177e4SLinus Torvalds 	case S_BOOLEAN:
6841da177e4SLinus Torvalds 	case S_TRISTATE:
6851da177e4SLinus Torvalds 		switch (newval[0]) {
6861da177e4SLinus Torvalds 		case 'y': case 'Y':
6871da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, yes);
6881da177e4SLinus Torvalds 		case 'm': case 'M':
6891da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, mod);
6901da177e4SLinus Torvalds 		case 'n': case 'N':
6911da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, no);
6921da177e4SLinus Torvalds 		}
6931da177e4SLinus Torvalds 		return false;
6941da177e4SLinus Torvalds 	default:
6951da177e4SLinus Torvalds 		;
6961da177e4SLinus Torvalds 	}
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	if (!sym_string_within_range(sym, newval))
6991da177e4SLinus Torvalds 		return false;
7001da177e4SLinus Torvalds 
701669bfad9SRoman Zippel 	if (!(sym->flags & SYMBOL_DEF_USER)) {
702669bfad9SRoman Zippel 		sym->flags |= SYMBOL_DEF_USER;
7031da177e4SLinus Torvalds 		sym_set_changed(sym);
7041da177e4SLinus Torvalds 	}
7051da177e4SLinus Torvalds 
7060c1822e6SRoman Zippel 	oldval = sym->def[S_DEF_USER].val;
7071da177e4SLinus Torvalds 	size = strlen(newval) + 1;
7081da177e4SLinus Torvalds 	if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
7091da177e4SLinus Torvalds 		size += 2;
710177acf78SAlan Cox 		sym->def[S_DEF_USER].val = val = xmalloc(size);
7111da177e4SLinus Torvalds 		*val++ = '0';
7121da177e4SLinus Torvalds 		*val++ = 'x';
7131da177e4SLinus Torvalds 	} else if (!oldval || strcmp(oldval, newval))
714177acf78SAlan Cox 		sym->def[S_DEF_USER].val = val = xmalloc(size);
7151da177e4SLinus Torvalds 	else
7161da177e4SLinus Torvalds 		return true;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	strcpy(val, newval);
7191da177e4SLinus Torvalds 	free((void *)oldval);
7201da177e4SLinus Torvalds 	sym_clear_all_valid();
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	return true;
7231da177e4SLinus Torvalds }
7241da177e4SLinus Torvalds 
7257cf3d73bSSam Ravnborg /*
7267cf3d73bSSam Ravnborg  * Find the default value associated to a symbol.
7277cf3d73bSSam Ravnborg  * For tristate symbol handle the modules=n case
7287cf3d73bSSam Ravnborg  * in which case "m" becomes "y".
7297cf3d73bSSam Ravnborg  * If the symbol does not have any default then fallback
7307cf3d73bSSam Ravnborg  * to the fixed default values.
7317cf3d73bSSam Ravnborg  */
7327cf3d73bSSam Ravnborg const char *sym_get_string_default(struct symbol *sym)
7337cf3d73bSSam Ravnborg {
7347cf3d73bSSam Ravnborg 	struct property *prop;
7357cf3d73bSSam Ravnborg 	struct symbol *ds;
7367cf3d73bSSam Ravnborg 	const char *str;
7377cf3d73bSSam Ravnborg 	tristate val;
7387cf3d73bSSam Ravnborg 
7397cf3d73bSSam Ravnborg 	sym_calc_visibility(sym);
7407cf3d73bSSam Ravnborg 	sym_calc_value(modules_sym);
7417cf3d73bSSam Ravnborg 	val = symbol_no.curr.tri;
7427cf3d73bSSam Ravnborg 	str = symbol_empty.curr.val;
7437cf3d73bSSam Ravnborg 
7447cf3d73bSSam Ravnborg 	/* If symbol has a default value look it up */
7457cf3d73bSSam Ravnborg 	prop = sym_get_default_prop(sym);
7467cf3d73bSSam Ravnborg 	if (prop != NULL) {
7477cf3d73bSSam Ravnborg 		switch (sym->type) {
7487cf3d73bSSam Ravnborg 		case S_BOOLEAN:
7497cf3d73bSSam Ravnborg 		case S_TRISTATE:
750579fb8e7SArnaud Lacombe 			/* The visibility may limit the value from yes => mod */
7517cf3d73bSSam Ravnborg 			val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
7527cf3d73bSSam Ravnborg 			break;
7537cf3d73bSSam Ravnborg 		default:
7547cf3d73bSSam Ravnborg 			/*
7557cf3d73bSSam Ravnborg 			 * The following fails to handle the situation
7567cf3d73bSSam Ravnborg 			 * where a default value is further limited by
7577cf3d73bSSam Ravnborg 			 * the valid range.
7587cf3d73bSSam Ravnborg 			 */
7597cf3d73bSSam Ravnborg 			ds = prop_get_symbol(prop);
7607cf3d73bSSam Ravnborg 			if (ds != NULL) {
7617cf3d73bSSam Ravnborg 				sym_calc_value(ds);
7627cf3d73bSSam Ravnborg 				str = (const char *)ds->curr.val;
7637cf3d73bSSam Ravnborg 			}
7647cf3d73bSSam Ravnborg 		}
7657cf3d73bSSam Ravnborg 	}
7667cf3d73bSSam Ravnborg 
7677cf3d73bSSam Ravnborg 	/* Handle select statements */
7687cf3d73bSSam Ravnborg 	val = EXPR_OR(val, sym->rev_dep.tri);
7697cf3d73bSSam Ravnborg 
7707cf3d73bSSam Ravnborg 	/* transpose mod to yes if modules are not enabled */
7717cf3d73bSSam Ravnborg 	if (val == mod)
7727cf3d73bSSam Ravnborg 		if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
7737cf3d73bSSam Ravnborg 			val = yes;
7747cf3d73bSSam Ravnborg 
7757cf3d73bSSam Ravnborg 	/* transpose mod to yes if type is bool */
7767cf3d73bSSam Ravnborg 	if (sym->type == S_BOOLEAN && val == mod)
7777cf3d73bSSam Ravnborg 		val = yes;
7787cf3d73bSSam Ravnborg 
779237e3ad0SNicolas Pitre 	/* adjust the default value if this symbol is implied by another */
780237e3ad0SNicolas Pitre 	if (val < sym->implied.tri)
781237e3ad0SNicolas Pitre 		val = sym->implied.tri;
782237e3ad0SNicolas Pitre 
7837cf3d73bSSam Ravnborg 	switch (sym->type) {
7847cf3d73bSSam Ravnborg 	case S_BOOLEAN:
7857cf3d73bSSam Ravnborg 	case S_TRISTATE:
7867cf3d73bSSam Ravnborg 		switch (val) {
7877cf3d73bSSam Ravnborg 		case no: return "n";
7887cf3d73bSSam Ravnborg 		case mod: return "m";
7897cf3d73bSSam Ravnborg 		case yes: return "y";
7907cf3d73bSSam Ravnborg 		}
7917cf3d73bSSam Ravnborg 	case S_INT:
7927cf3d73bSSam Ravnborg 	case S_HEX:
7937cf3d73bSSam Ravnborg 		return str;
7947cf3d73bSSam Ravnborg 	case S_STRING:
7957cf3d73bSSam Ravnborg 		return str;
7967cf3d73bSSam Ravnborg 	case S_OTHER:
7977cf3d73bSSam Ravnborg 	case S_UNKNOWN:
7987cf3d73bSSam Ravnborg 		break;
7997cf3d73bSSam Ravnborg 	}
8007cf3d73bSSam Ravnborg 	return "";
8017cf3d73bSSam Ravnborg }
8027cf3d73bSSam Ravnborg 
8031da177e4SLinus Torvalds const char *sym_get_string_value(struct symbol *sym)
8041da177e4SLinus Torvalds {
8051da177e4SLinus Torvalds 	tristate val;
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 	switch (sym->type) {
8081da177e4SLinus Torvalds 	case S_BOOLEAN:
8091da177e4SLinus Torvalds 	case S_TRISTATE:
8101da177e4SLinus Torvalds 		val = sym_get_tristate_value(sym);
8111da177e4SLinus Torvalds 		switch (val) {
8121da177e4SLinus Torvalds 		case no:
8131da177e4SLinus Torvalds 			return "n";
8141da177e4SLinus Torvalds 		case mod:
815e54e692bSArnaud Lacombe 			sym_calc_value(modules_sym);
816e54e692bSArnaud Lacombe 			return (modules_sym->curr.tri == no) ? "n" : "m";
8171da177e4SLinus Torvalds 		case yes:
8181da177e4SLinus Torvalds 			return "y";
8191da177e4SLinus Torvalds 		}
8201da177e4SLinus Torvalds 		break;
8211da177e4SLinus Torvalds 	default:
8221da177e4SLinus Torvalds 		;
8231da177e4SLinus Torvalds 	}
8241da177e4SLinus Torvalds 	return (const char *)sym->curr.val;
8251da177e4SLinus Torvalds }
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds bool sym_is_changable(struct symbol *sym)
8281da177e4SLinus Torvalds {
8291da177e4SLinus Torvalds 	return sym->visible > sym->rev_dep.tri;
8301da177e4SLinus Torvalds }
8311da177e4SLinus Torvalds 
832e66f25d7SAndi Kleen static unsigned strhash(const char *s)
833e66f25d7SAndi Kleen {
834e66f25d7SAndi Kleen 	/* fnv32 hash */
835e66f25d7SAndi Kleen 	unsigned hash = 2166136261U;
836e66f25d7SAndi Kleen 	for (; *s; s++)
837e66f25d7SAndi Kleen 		hash = (hash ^ *s) * 0x01000193;
838e66f25d7SAndi Kleen 	return hash;
839e66f25d7SAndi Kleen }
840e66f25d7SAndi Kleen 
8415a1aa8a1SRoman Zippel struct symbol *sym_lookup(const char *name, int flags)
8421da177e4SLinus Torvalds {
8431da177e4SLinus Torvalds 	struct symbol *symbol;
8441da177e4SLinus Torvalds 	char *new_name;
845e66f25d7SAndi Kleen 	int hash;
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	if (name) {
8481da177e4SLinus Torvalds 		if (name[0] && !name[1]) {
8491da177e4SLinus Torvalds 			switch (name[0]) {
8501da177e4SLinus Torvalds 			case 'y': return &symbol_yes;
8511da177e4SLinus Torvalds 			case 'm': return &symbol_mod;
8521da177e4SLinus Torvalds 			case 'n': return &symbol_no;
8531da177e4SLinus Torvalds 			}
8541da177e4SLinus Torvalds 		}
855e66f25d7SAndi Kleen 		hash = strhash(name) % SYMBOL_HASHSIZE;
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 		for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
858e66f25d7SAndi Kleen 			if (symbol->name &&
859e66f25d7SAndi Kleen 			    !strcmp(symbol->name, name) &&
8605a1aa8a1SRoman Zippel 			    (flags ? symbol->flags & flags
8615a1aa8a1SRoman Zippel 				   : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
8621da177e4SLinus Torvalds 				return symbol;
8631da177e4SLinus Torvalds 		}
864cd81fc82SMasahiro Yamada 		new_name = xstrdup(name);
8651da177e4SLinus Torvalds 	} else {
8661da177e4SLinus Torvalds 		new_name = NULL;
867e66f25d7SAndi Kleen 		hash = 0;
8681da177e4SLinus Torvalds 	}
8691da177e4SLinus Torvalds 
870177acf78SAlan Cox 	symbol = xmalloc(sizeof(*symbol));
8711da177e4SLinus Torvalds 	memset(symbol, 0, sizeof(*symbol));
8721da177e4SLinus Torvalds 	symbol->name = new_name;
8731da177e4SLinus Torvalds 	symbol->type = S_UNKNOWN;
8745a1aa8a1SRoman Zippel 	symbol->flags |= flags;
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	symbol->next = symbol_hash[hash];
8771da177e4SLinus Torvalds 	symbol_hash[hash] = symbol;
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	return symbol;
8801da177e4SLinus Torvalds }
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds struct symbol *sym_find(const char *name)
8831da177e4SLinus Torvalds {
8841da177e4SLinus Torvalds 	struct symbol *symbol = NULL;
8851da177e4SLinus Torvalds 	int hash = 0;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	if (!name)
8881da177e4SLinus Torvalds 		return NULL;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	if (name[0] && !name[1]) {
8911da177e4SLinus Torvalds 		switch (name[0]) {
8921da177e4SLinus Torvalds 		case 'y': return &symbol_yes;
8931da177e4SLinus Torvalds 		case 'm': return &symbol_mod;
8941da177e4SLinus Torvalds 		case 'n': return &symbol_no;
8951da177e4SLinus Torvalds 		}
8961da177e4SLinus Torvalds 	}
897e66f25d7SAndi Kleen 	hash = strhash(name) % SYMBOL_HASHSIZE;
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 	for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
900e66f25d7SAndi Kleen 		if (symbol->name &&
901e66f25d7SAndi Kleen 		    !strcmp(symbol->name, name) &&
9021da177e4SLinus Torvalds 		    !(symbol->flags & SYMBOL_CONST))
9031da177e4SLinus Torvalds 				break;
9041da177e4SLinus Torvalds 	}
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	return symbol;
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds 
90976a54095SArnaud Lacombe /*
91076a54095SArnaud Lacombe  * Expand symbol's names embedded in the string given in argument. Symbols'
91176a54095SArnaud Lacombe  * name to be expanded shall be prefixed by a '$'. Unknown symbol expands to
91276a54095SArnaud Lacombe  * the empty string.
91376a54095SArnaud Lacombe  */
914523ca58bSMasahiro Yamada char *sym_expand_string_value(const char *in)
91576a54095SArnaud Lacombe {
91676a54095SArnaud Lacombe 	const char *src;
91776a54095SArnaud Lacombe 	char *res;
91876a54095SArnaud Lacombe 	size_t reslen;
91976a54095SArnaud Lacombe 
92024161a67SUlf Magnusson 	/*
92124161a67SUlf Magnusson 	 * Note: 'in' might come from a token that's about to be
92224161a67SUlf Magnusson 	 * freed, so make sure to always allocate a new string
92324161a67SUlf Magnusson 	 */
92476a54095SArnaud Lacombe 	reslen = strlen(in) + 1;
925177acf78SAlan Cox 	res = xmalloc(reslen);
92676a54095SArnaud Lacombe 	res[0] = '\0';
92776a54095SArnaud Lacombe 
92876a54095SArnaud Lacombe 	while ((src = strchr(in, '$'))) {
92976a54095SArnaud Lacombe 		char *p, name[SYMBOL_MAXLENGTH];
93076a54095SArnaud Lacombe 		const char *symval = "";
93176a54095SArnaud Lacombe 		struct symbol *sym;
93276a54095SArnaud Lacombe 		size_t newlen;
93376a54095SArnaud Lacombe 
93476a54095SArnaud Lacombe 		strncat(res, in, src - in);
93576a54095SArnaud Lacombe 		src++;
93676a54095SArnaud Lacombe 
93776a54095SArnaud Lacombe 		p = name;
93876a54095SArnaud Lacombe 		while (isalnum(*src) || *src == '_')
93976a54095SArnaud Lacombe 			*p++ = *src++;
94076a54095SArnaud Lacombe 		*p = '\0';
94176a54095SArnaud Lacombe 
94276a54095SArnaud Lacombe 		sym = sym_find(name);
94376a54095SArnaud Lacombe 		if (sym != NULL) {
94476a54095SArnaud Lacombe 			sym_calc_value(sym);
94576a54095SArnaud Lacombe 			symval = sym_get_string_value(sym);
94676a54095SArnaud Lacombe 		}
94776a54095SArnaud Lacombe 
948020e773fSAndy Whitcroft 		newlen = strlen(res) + strlen(symval) + strlen(src) + 1;
94976a54095SArnaud Lacombe 		if (newlen > reslen) {
95076a54095SArnaud Lacombe 			reslen = newlen;
951d717f24dSMasahiro Yamada 			res = xrealloc(res, reslen);
95276a54095SArnaud Lacombe 		}
95376a54095SArnaud Lacombe 
95476a54095SArnaud Lacombe 		strcat(res, symval);
95576a54095SArnaud Lacombe 		in = src;
95676a54095SArnaud Lacombe 	}
95776a54095SArnaud Lacombe 	strcat(res, in);
95876a54095SArnaud Lacombe 
95976a54095SArnaud Lacombe 	return res;
96076a54095SArnaud Lacombe }
96176a54095SArnaud Lacombe 
962e54e692bSArnaud Lacombe const char *sym_escape_string_value(const char *in)
963e54e692bSArnaud Lacombe {
964e54e692bSArnaud Lacombe 	const char *p;
965e54e692bSArnaud Lacombe 	size_t reslen;
966e54e692bSArnaud Lacombe 	char *res;
967e54e692bSArnaud Lacombe 	size_t l;
968e54e692bSArnaud Lacombe 
969e54e692bSArnaud Lacombe 	reslen = strlen(in) + strlen("\"\"") + 1;
970e54e692bSArnaud Lacombe 
971e54e692bSArnaud Lacombe 	p = in;
972e54e692bSArnaud Lacombe 	for (;;) {
973e54e692bSArnaud Lacombe 		l = strcspn(p, "\"\\");
974e54e692bSArnaud Lacombe 		p += l;
975e54e692bSArnaud Lacombe 
976e54e692bSArnaud Lacombe 		if (p[0] == '\0')
977e54e692bSArnaud Lacombe 			break;
978e54e692bSArnaud Lacombe 
979e54e692bSArnaud Lacombe 		reslen++;
980e54e692bSArnaud Lacombe 		p++;
981e54e692bSArnaud Lacombe 	}
982e54e692bSArnaud Lacombe 
983177acf78SAlan Cox 	res = xmalloc(reslen);
984e54e692bSArnaud Lacombe 	res[0] = '\0';
985e54e692bSArnaud Lacombe 
986e54e692bSArnaud Lacombe 	strcat(res, "\"");
987e54e692bSArnaud Lacombe 
988e54e692bSArnaud Lacombe 	p = in;
989e54e692bSArnaud Lacombe 	for (;;) {
990e54e692bSArnaud Lacombe 		l = strcspn(p, "\"\\");
991e54e692bSArnaud Lacombe 		strncat(res, p, l);
992e54e692bSArnaud Lacombe 		p += l;
993e54e692bSArnaud Lacombe 
994e54e692bSArnaud Lacombe 		if (p[0] == '\0')
995e54e692bSArnaud Lacombe 			break;
996e54e692bSArnaud Lacombe 
997e54e692bSArnaud Lacombe 		strcat(res, "\\");
998e54e692bSArnaud Lacombe 		strncat(res, p++, 1);
999e54e692bSArnaud Lacombe 	}
1000e54e692bSArnaud Lacombe 
1001e54e692bSArnaud Lacombe 	strcat(res, "\"");
1002e54e692bSArnaud Lacombe 	return res;
1003e54e692bSArnaud Lacombe }
1004e54e692bSArnaud Lacombe 
1005193b40aeSYann E. MORIN struct sym_match {
1006193b40aeSYann E. MORIN 	struct symbol	*sym;
1007193b40aeSYann E. MORIN 	off_t		so, eo;
1008193b40aeSYann E. MORIN };
1009193b40aeSYann E. MORIN 
1010193b40aeSYann E. MORIN /* Compare matched symbols as thus:
1011193b40aeSYann E. MORIN  * - first, symbols that match exactly
1012193b40aeSYann E. MORIN  * - then, alphabetical sort
1013193b40aeSYann E. MORIN  */
1014193b40aeSYann E. MORIN static int sym_rel_comp(const void *sym1, const void *sym2)
1015193b40aeSYann E. MORIN {
1016508382a0SYann E. MORIN 	const struct sym_match *s1 = sym1;
1017508382a0SYann E. MORIN 	const struct sym_match *s2 = sym2;
101826e933e3SYann E. MORIN 	int exact1, exact2;
1019193b40aeSYann E. MORIN 
1020193b40aeSYann E. MORIN 	/* Exact match:
1021193b40aeSYann E. MORIN 	 * - if matched length on symbol s1 is the length of that symbol,
1022193b40aeSYann E. MORIN 	 *   then this symbol should come first;
1023193b40aeSYann E. MORIN 	 * - if matched length on symbol s2 is the length of that symbol,
1024193b40aeSYann E. MORIN 	 *   then this symbol should come first.
1025193b40aeSYann E. MORIN 	 * Note: since the search can be a regexp, both symbols may match
1026193b40aeSYann E. MORIN 	 * exactly; if this is the case, we can't decide which comes first,
1027193b40aeSYann E. MORIN 	 * and we fallback to sorting alphabetically.
1028193b40aeSYann E. MORIN 	 */
102926e933e3SYann E. MORIN 	exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
103026e933e3SYann E. MORIN 	exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
103126e933e3SYann E. MORIN 	if (exact1 && !exact2)
1032193b40aeSYann E. MORIN 		return -1;
103326e933e3SYann E. MORIN 	if (!exact1 && exact2)
1034193b40aeSYann E. MORIN 		return 1;
1035193b40aeSYann E. MORIN 
1036193b40aeSYann E. MORIN 	/* As a fallback, sort symbols alphabetically */
1037193b40aeSYann E. MORIN 	return strcmp(s1->sym->name, s2->sym->name);
1038193b40aeSYann E. MORIN }
1039193b40aeSYann E. MORIN 
10401da177e4SLinus Torvalds struct symbol **sym_re_search(const char *pattern)
10411da177e4SLinus Torvalds {
10421da177e4SLinus Torvalds 	struct symbol *sym, **sym_arr = NULL;
1043508382a0SYann E. MORIN 	struct sym_match *sym_match_arr = NULL;
10441da177e4SLinus Torvalds 	int i, cnt, size;
10451da177e4SLinus Torvalds 	regex_t re;
1046193b40aeSYann E. MORIN 	regmatch_t match[1];
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 	cnt = size = 0;
10491da177e4SLinus Torvalds 	/* Skip if empty */
10501da177e4SLinus Torvalds 	if (strlen(pattern) == 0)
10511da177e4SLinus Torvalds 		return NULL;
1052193b40aeSYann E. MORIN 	if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
10531da177e4SLinus Torvalds 		return NULL;
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
10561da177e4SLinus Torvalds 		if (sym->flags & SYMBOL_CONST || !sym->name)
10571da177e4SLinus Torvalds 			continue;
1058193b40aeSYann E. MORIN 		if (regexec(&re, sym->name, 1, match, 0))
10591da177e4SLinus Torvalds 			continue;
10601407f97aSYann E. MORIN 		if (cnt >= size) {
1061193b40aeSYann E. MORIN 			void *tmp;
10621da177e4SLinus Torvalds 			size += 16;
1063508382a0SYann E. MORIN 			tmp = realloc(sym_match_arr, size * sizeof(struct sym_match));
1064803b3519SYann E. MORIN 			if (!tmp)
1065193b40aeSYann E. MORIN 				goto sym_re_search_free;
1066193b40aeSYann E. MORIN 			sym_match_arr = tmp;
10671da177e4SLinus Torvalds 		}
1068da6df879SLi Zefan 		sym_calc_value(sym);
1069803b3519SYann E. MORIN 		/* As regexec returned 0, we know we have a match, so
1070193b40aeSYann E. MORIN 		 * we can use match[0].rm_[se]o without further checks
1071193b40aeSYann E. MORIN 		 */
1072508382a0SYann E. MORIN 		sym_match_arr[cnt].so = match[0].rm_so;
1073508382a0SYann E. MORIN 		sym_match_arr[cnt].eo = match[0].rm_eo;
1074508382a0SYann E. MORIN 		sym_match_arr[cnt++].sym = sym;
10751da177e4SLinus Torvalds 	}
1076193b40aeSYann E. MORIN 	if (sym_match_arr) {
1077508382a0SYann E. MORIN 		qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp);
107888127daeSHeinrich Schuchardt 		sym_arr = malloc((cnt+1) * sizeof(struct symbol *));
1079193b40aeSYann E. MORIN 		if (!sym_arr)
1080193b40aeSYann E. MORIN 			goto sym_re_search_free;
1081193b40aeSYann E. MORIN 		for (i = 0; i < cnt; i++)
1082508382a0SYann E. MORIN 			sym_arr[i] = sym_match_arr[i].sym;
10831da177e4SLinus Torvalds 		sym_arr[cnt] = NULL;
1084193b40aeSYann E. MORIN 	}
1085193b40aeSYann E. MORIN sym_re_search_free:
1086508382a0SYann E. MORIN 	/* sym_match_arr can be NULL if no match, but free(NULL) is OK */
1087193b40aeSYann E. MORIN 	free(sym_match_arr);
10881da177e4SLinus Torvalds 	regfree(&re);
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	return sym_arr;
10911da177e4SLinus Torvalds }
10921da177e4SLinus Torvalds 
1093d595cea6SRoman Zippel /*
1094d595cea6SRoman Zippel  * When we check for recursive dependencies we use a stack to save
1095d595cea6SRoman Zippel  * current state so we can print out relevant info to user.
1096d595cea6SRoman Zippel  * The entries are located on the call stack so no need to free memory.
10978d9dfe82SMartin Walch  * Note insert() remove() must always match to properly clear the stack.
1098d595cea6SRoman Zippel  */
1099d595cea6SRoman Zippel static struct dep_stack {
1100d595cea6SRoman Zippel 	struct dep_stack *prev, *next;
1101d595cea6SRoman Zippel 	struct symbol *sym;
1102d595cea6SRoman Zippel 	struct property *prop;
1103d595cea6SRoman Zippel 	struct expr *expr;
1104d595cea6SRoman Zippel } *check_top;
1105d595cea6SRoman Zippel 
1106d595cea6SRoman Zippel static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
1107d595cea6SRoman Zippel {
1108d595cea6SRoman Zippel 	memset(stack, 0, sizeof(*stack));
1109d595cea6SRoman Zippel 	if (check_top)
1110d595cea6SRoman Zippel 		check_top->next = stack;
1111d595cea6SRoman Zippel 	stack->prev = check_top;
1112d595cea6SRoman Zippel 	stack->sym = sym;
1113d595cea6SRoman Zippel 	check_top = stack;
1114d595cea6SRoman Zippel }
1115d595cea6SRoman Zippel 
1116d595cea6SRoman Zippel static void dep_stack_remove(void)
1117d595cea6SRoman Zippel {
1118d595cea6SRoman Zippel 	check_top = check_top->prev;
1119d595cea6SRoman Zippel 	if (check_top)
1120d595cea6SRoman Zippel 		check_top->next = NULL;
1121d595cea6SRoman Zippel }
1122d595cea6SRoman Zippel 
1123d595cea6SRoman Zippel /*
1124d595cea6SRoman Zippel  * Called when we have detected a recursive dependency.
1125d595cea6SRoman Zippel  * check_top point to the top of the stact so we use
1126d595cea6SRoman Zippel  * the ->prev pointer to locate the bottom of the stack.
1127d595cea6SRoman Zippel  */
1128d595cea6SRoman Zippel static void sym_check_print_recursive(struct symbol *last_sym)
1129d595cea6SRoman Zippel {
1130d595cea6SRoman Zippel 	struct dep_stack *stack;
1131d595cea6SRoman Zippel 	struct symbol *sym, *next_sym;
1132d595cea6SRoman Zippel 	struct menu *menu = NULL;
1133d595cea6SRoman Zippel 	struct property *prop;
1134d595cea6SRoman Zippel 	struct dep_stack cv_stack;
1135d595cea6SRoman Zippel 
1136d595cea6SRoman Zippel 	if (sym_is_choice_value(last_sym)) {
1137d595cea6SRoman Zippel 		dep_stack_insert(&cv_stack, last_sym);
1138d595cea6SRoman Zippel 		last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
1139d595cea6SRoman Zippel 	}
1140d595cea6SRoman Zippel 
1141d595cea6SRoman Zippel 	for (stack = check_top; stack != NULL; stack = stack->prev)
1142d595cea6SRoman Zippel 		if (stack->sym == last_sym)
1143d595cea6SRoman Zippel 			break;
1144d595cea6SRoman Zippel 	if (!stack) {
1145d595cea6SRoman Zippel 		fprintf(stderr, "unexpected recursive dependency error\n");
1146d595cea6SRoman Zippel 		return;
1147d595cea6SRoman Zippel 	}
1148d595cea6SRoman Zippel 
1149d595cea6SRoman Zippel 	for (; stack; stack = stack->next) {
1150d595cea6SRoman Zippel 		sym = stack->sym;
1151d595cea6SRoman Zippel 		next_sym = stack->next ? stack->next->sym : last_sym;
1152d595cea6SRoman Zippel 		prop = stack->prop;
11533643f849SSam Ravnborg 		if (prop == NULL)
11543643f849SSam Ravnborg 			prop = stack->sym->prop;
1155d595cea6SRoman Zippel 
1156d595cea6SRoman Zippel 		/* for choice values find the menu entry (used below) */
1157d595cea6SRoman Zippel 		if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
1158d595cea6SRoman Zippel 			for (prop = sym->prop; prop; prop = prop->next) {
1159d595cea6SRoman Zippel 				menu = prop->menu;
1160d595cea6SRoman Zippel 				if (prop->menu)
1161d595cea6SRoman Zippel 					break;
1162d595cea6SRoman Zippel 			}
1163d595cea6SRoman Zippel 		}
1164d595cea6SRoman Zippel 		if (stack->sym == last_sym)
1165d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
1166d595cea6SRoman Zippel 				prop->file->name, prop->lineno);
1167e3b03bf2SMasahiro Yamada 
1168d595cea6SRoman Zippel 		if (stack->expr) {
1169d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
1170d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
1171d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
1172d595cea6SRoman Zippel 				prop_get_type_name(prop->type),
1173d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
1174d595cea6SRoman Zippel 		} else if (stack->prop) {
1175d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
1176d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
1177d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
1178d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
1179d595cea6SRoman Zippel 		} else if (sym_is_choice(sym)) {
1180d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
1181d595cea6SRoman Zippel 				menu->file->name, menu->lineno,
1182d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
1183d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
1184d595cea6SRoman Zippel 		} else if (sym_is_choice_value(sym)) {
1185d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
1186d595cea6SRoman Zippel 				menu->file->name, menu->lineno,
1187d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
1188d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
1189d595cea6SRoman Zippel 		} else {
1190d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
1191d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
1192d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
1193d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
1194d595cea6SRoman Zippel 		}
1195d595cea6SRoman Zippel 	}
1196d595cea6SRoman Zippel 
1197e3b03bf2SMasahiro Yamada 	fprintf(stderr,
1198e3b03bf2SMasahiro Yamada 		"For a resolution refer to Documentation/kbuild/kconfig-language.txt\n"
1199e3b03bf2SMasahiro Yamada 		"subsection \"Kconfig recursive dependency limitations\"\n"
1200e3b03bf2SMasahiro Yamada 		"\n");
1201e3b03bf2SMasahiro Yamada 
1202d595cea6SRoman Zippel 	if (check_top == &cv_stack)
1203d595cea6SRoman Zippel 		dep_stack_remove();
1204d595cea6SRoman Zippel }
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds static struct symbol *sym_check_expr_deps(struct expr *e)
12071da177e4SLinus Torvalds {
12081da177e4SLinus Torvalds 	struct symbol *sym;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	if (!e)
12111da177e4SLinus Torvalds 		return NULL;
12121da177e4SLinus Torvalds 	switch (e->type) {
12131da177e4SLinus Torvalds 	case E_OR:
12141da177e4SLinus Torvalds 	case E_AND:
12151da177e4SLinus Torvalds 		sym = sym_check_expr_deps(e->left.expr);
12161da177e4SLinus Torvalds 		if (sym)
12171da177e4SLinus Torvalds 			return sym;
12181da177e4SLinus Torvalds 		return sym_check_expr_deps(e->right.expr);
12191da177e4SLinus Torvalds 	case E_NOT:
12201da177e4SLinus Torvalds 		return sym_check_expr_deps(e->left.expr);
12211da177e4SLinus Torvalds 	case E_EQUAL:
122231847b67SJan Beulich 	case E_GEQ:
122331847b67SJan Beulich 	case E_GTH:
122431847b67SJan Beulich 	case E_LEQ:
122531847b67SJan Beulich 	case E_LTH:
12261da177e4SLinus Torvalds 	case E_UNEQUAL:
12271da177e4SLinus Torvalds 		sym = sym_check_deps(e->left.sym);
12281da177e4SLinus Torvalds 		if (sym)
12291da177e4SLinus Torvalds 			return sym;
12301da177e4SLinus Torvalds 		return sym_check_deps(e->right.sym);
12311da177e4SLinus Torvalds 	case E_SYMBOL:
12321da177e4SLinus Torvalds 		return sym_check_deps(e->left.sym);
12331da177e4SLinus Torvalds 	default:
12341da177e4SLinus Torvalds 		break;
12351da177e4SLinus Torvalds 	}
12369e3e10c7SMasahiro Yamada 	fprintf(stderr, "Oops! How to check %d?\n", e->type);
12371da177e4SLinus Torvalds 	return NULL;
12381da177e4SLinus Torvalds }
12391da177e4SLinus Torvalds 
12405447d34bSSam Ravnborg /* return NULL when dependencies are OK */
124148981178SRoman Zippel static struct symbol *sym_check_sym_deps(struct symbol *sym)
124248981178SRoman Zippel {
124348981178SRoman Zippel 	struct symbol *sym2;
124448981178SRoman Zippel 	struct property *prop;
1245d595cea6SRoman Zippel 	struct dep_stack stack;
1246d595cea6SRoman Zippel 
1247d595cea6SRoman Zippel 	dep_stack_insert(&stack, sym);
124848981178SRoman Zippel 
124948981178SRoman Zippel 	sym2 = sym_check_expr_deps(sym->rev_dep.expr);
125048981178SRoman Zippel 	if (sym2)
1251d595cea6SRoman Zippel 		goto out;
125248981178SRoman Zippel 
125348981178SRoman Zippel 	for (prop = sym->prop; prop; prop = prop->next) {
125448981178SRoman Zippel 		if (prop->type == P_CHOICE || prop->type == P_SELECT)
125548981178SRoman Zippel 			continue;
1256d595cea6SRoman Zippel 		stack.prop = prop;
125748981178SRoman Zippel 		sym2 = sym_check_expr_deps(prop->visible.expr);
125848981178SRoman Zippel 		if (sym2)
125948981178SRoman Zippel 			break;
126048981178SRoman Zippel 		if (prop->type != P_DEFAULT || sym_is_choice(sym))
126148981178SRoman Zippel 			continue;
1262d595cea6SRoman Zippel 		stack.expr = prop->expr;
126348981178SRoman Zippel 		sym2 = sym_check_expr_deps(prop->expr);
126448981178SRoman Zippel 		if (sym2)
126548981178SRoman Zippel 			break;
1266d595cea6SRoman Zippel 		stack.expr = NULL;
126748981178SRoman Zippel 	}
126848981178SRoman Zippel 
1269d595cea6SRoman Zippel out:
1270d595cea6SRoman Zippel 	dep_stack_remove();
1271d595cea6SRoman Zippel 
127248981178SRoman Zippel 	return sym2;
127348981178SRoman Zippel }
127448981178SRoman Zippel 
127548981178SRoman Zippel static struct symbol *sym_check_choice_deps(struct symbol *choice)
127648981178SRoman Zippel {
127748981178SRoman Zippel 	struct symbol *sym, *sym2;
127848981178SRoman Zippel 	struct property *prop;
127948981178SRoman Zippel 	struct expr *e;
1280d595cea6SRoman Zippel 	struct dep_stack stack;
1281d595cea6SRoman Zippel 
1282d595cea6SRoman Zippel 	dep_stack_insert(&stack, choice);
128348981178SRoman Zippel 
128448981178SRoman Zippel 	prop = sym_get_choice_prop(choice);
128548981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym)
128648981178SRoman Zippel 		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
128748981178SRoman Zippel 
128848981178SRoman Zippel 	choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
128948981178SRoman Zippel 	sym2 = sym_check_sym_deps(choice);
129048981178SRoman Zippel 	choice->flags &= ~SYMBOL_CHECK;
129148981178SRoman Zippel 	if (sym2)
129248981178SRoman Zippel 		goto out;
129348981178SRoman Zippel 
129448981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym) {
129548981178SRoman Zippel 		sym2 = sym_check_sym_deps(sym);
1296d595cea6SRoman Zippel 		if (sym2)
129748981178SRoman Zippel 			break;
129848981178SRoman Zippel 	}
129948981178SRoman Zippel out:
130048981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym)
130148981178SRoman Zippel 		sym->flags &= ~SYMBOL_CHECK;
130248981178SRoman Zippel 
130348981178SRoman Zippel 	if (sym2 && sym_is_choice_value(sym2) &&
130448981178SRoman Zippel 	    prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
130548981178SRoman Zippel 		sym2 = choice;
130648981178SRoman Zippel 
1307d595cea6SRoman Zippel 	dep_stack_remove();
1308d595cea6SRoman Zippel 
130948981178SRoman Zippel 	return sym2;
131048981178SRoman Zippel }
131148981178SRoman Zippel 
13121da177e4SLinus Torvalds struct symbol *sym_check_deps(struct symbol *sym)
13131da177e4SLinus Torvalds {
13141da177e4SLinus Torvalds 	struct symbol *sym2;
13151da177e4SLinus Torvalds 	struct property *prop;
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 	if (sym->flags & SYMBOL_CHECK) {
1318d595cea6SRoman Zippel 		sym_check_print_recursive(sym);
13191da177e4SLinus Torvalds 		return sym;
13201da177e4SLinus Torvalds 	}
13213f04e7ddSDavid Gibson 	if (sym->flags & SYMBOL_CHECKED)
13223f04e7ddSDavid Gibson 		return NULL;
13231da177e4SLinus Torvalds 
132448981178SRoman Zippel 	if (sym_is_choice_value(sym)) {
1325d595cea6SRoman Zippel 		struct dep_stack stack;
1326d595cea6SRoman Zippel 
132748981178SRoman Zippel 		/* for choice groups start the check with main choice symbol */
1328d595cea6SRoman Zippel 		dep_stack_insert(&stack, sym);
132948981178SRoman Zippel 		prop = sym_get_choice_prop(sym);
133048981178SRoman Zippel 		sym2 = sym_check_deps(prop_get_symbol(prop));
1331d595cea6SRoman Zippel 		dep_stack_remove();
133248981178SRoman Zippel 	} else if (sym_is_choice(sym)) {
133348981178SRoman Zippel 		sym2 = sym_check_choice_deps(sym);
133448981178SRoman Zippel 	} else {
13351da177e4SLinus Torvalds 		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
133648981178SRoman Zippel 		sym2 = sym_check_sym_deps(sym);
13371da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_CHECK;
133848981178SRoman Zippel 	}
133948981178SRoman Zippel 
1340d595cea6SRoman Zippel 	if (sym2 && sym2 == sym)
134148981178SRoman Zippel 		sym2 = NULL;
134248981178SRoman Zippel 
13431da177e4SLinus Torvalds 	return sym2;
13441da177e4SLinus Torvalds }
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds struct property *prop_alloc(enum prop_type type, struct symbol *sym)
13471da177e4SLinus Torvalds {
13481da177e4SLinus Torvalds 	struct property *prop;
13491da177e4SLinus Torvalds 	struct property **propp;
13501da177e4SLinus Torvalds 
1351177acf78SAlan Cox 	prop = xmalloc(sizeof(*prop));
13521da177e4SLinus Torvalds 	memset(prop, 0, sizeof(*prop));
13531da177e4SLinus Torvalds 	prop->type = type;
13541da177e4SLinus Torvalds 	prop->sym = sym;
13551da177e4SLinus Torvalds 	prop->file = current_file;
13561da177e4SLinus Torvalds 	prop->lineno = zconf_lineno();
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 	/* append property to the prop list of symbol */
13591da177e4SLinus Torvalds 	if (sym) {
13601da177e4SLinus Torvalds 		for (propp = &sym->prop; *propp; propp = &(*propp)->next)
13611da177e4SLinus Torvalds 			;
13621da177e4SLinus Torvalds 		*propp = prop;
13631da177e4SLinus Torvalds 	}
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 	return prop;
13661da177e4SLinus Torvalds }
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds struct symbol *prop_get_symbol(struct property *prop)
13691da177e4SLinus Torvalds {
13701da177e4SLinus Torvalds 	if (prop->expr && (prop->expr->type == E_SYMBOL ||
13717a962923SRoman Zippel 			   prop->expr->type == E_LIST))
13721da177e4SLinus Torvalds 		return prop->expr->left.sym;
13731da177e4SLinus Torvalds 	return NULL;
13741da177e4SLinus Torvalds }
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds const char *prop_get_type_name(enum prop_type type)
13771da177e4SLinus Torvalds {
13781da177e4SLinus Torvalds 	switch (type) {
13791da177e4SLinus Torvalds 	case P_PROMPT:
13801da177e4SLinus Torvalds 		return "prompt";
138193449082SRoman Zippel 	case P_ENV:
138293449082SRoman Zippel 		return "env";
13831da177e4SLinus Torvalds 	case P_COMMENT:
13841da177e4SLinus Torvalds 		return "comment";
13851da177e4SLinus Torvalds 	case P_MENU:
13861da177e4SLinus Torvalds 		return "menu";
13871da177e4SLinus Torvalds 	case P_DEFAULT:
13881da177e4SLinus Torvalds 		return "default";
13891da177e4SLinus Torvalds 	case P_CHOICE:
13901da177e4SLinus Torvalds 		return "choice";
13911da177e4SLinus Torvalds 	case P_SELECT:
13921da177e4SLinus Torvalds 		return "select";
1393237e3ad0SNicolas Pitre 	case P_IMPLY:
1394237e3ad0SNicolas Pitre 		return "imply";
13951da177e4SLinus Torvalds 	case P_RANGE:
13961da177e4SLinus Torvalds 		return "range";
139759e89e3dSSam Ravnborg 	case P_SYMBOL:
139859e89e3dSSam Ravnborg 		return "symbol";
13991da177e4SLinus Torvalds 	case P_UNKNOWN:
14001da177e4SLinus Torvalds 		break;
14011da177e4SLinus Torvalds 	}
14021da177e4SLinus Torvalds 	return "unknown";
14031da177e4SLinus Torvalds }
140493449082SRoman Zippel 
14054356f489STrevor Keith static void prop_add_env(const char *env)
140693449082SRoman Zippel {
140793449082SRoman Zippel 	struct symbol *sym, *sym2;
140893449082SRoman Zippel 	struct property *prop;
140993449082SRoman Zippel 	char *p;
141093449082SRoman Zippel 
141193449082SRoman Zippel 	sym = current_entry->sym;
141293449082SRoman Zippel 	sym->flags |= SYMBOL_AUTO;
141393449082SRoman Zippel 	for_all_properties(sym, prop, P_ENV) {
141493449082SRoman Zippel 		sym2 = prop_get_symbol(prop);
141593449082SRoman Zippel 		if (strcmp(sym2->name, env))
141693449082SRoman Zippel 			menu_warn(current_entry, "redefining environment symbol from %s",
141793449082SRoman Zippel 				  sym2->name);
141893449082SRoman Zippel 		return;
141993449082SRoman Zippel 	}
142093449082SRoman Zippel 
142193449082SRoman Zippel 	prop = prop_alloc(P_ENV, sym);
14225a1aa8a1SRoman Zippel 	prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
142393449082SRoman Zippel 
142493449082SRoman Zippel 	sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
142593449082SRoman Zippel 	sym_env_list->right.sym = sym;
142693449082SRoman Zippel 
142793449082SRoman Zippel 	p = getenv(env);
142893449082SRoman Zippel 	if (p)
142993449082SRoman Zippel 		sym_add_default(sym, p);
143093449082SRoman Zippel 	else
143193449082SRoman Zippel 		menu_warn(current_entry, "environment variable %s undefined", env);
143293449082SRoman Zippel }
1433