xref: /openbmc/linux/scripts/kconfig/symbol.c (revision 1407f97aeda5720d6327d69f6058537c0fd469e3)
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:
801da177e4SLinus Torvalds 		return "boolean";
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 
1151da177e4SLinus Torvalds 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 
139b57caaaeSKees Cook static 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 	}
1524cf3cbe2SRoman Zippel 	return strtol(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;
158b57caaaeSKees Cook 	long base, val, val2;
1594cf3cbe2SRoman Zippel 	char str[64];
1604cf3cbe2SRoman Zippel 
1614cf3cbe2SRoman Zippel 	switch (sym->type) {
1624cf3cbe2SRoman Zippel 	case S_INT:
1634cf3cbe2SRoman Zippel 		base = 10;
1644cf3cbe2SRoman Zippel 		break;
1654cf3cbe2SRoman Zippel 	case S_HEX:
1664cf3cbe2SRoman Zippel 		base = 16;
1674cf3cbe2SRoman Zippel 		break;
1684cf3cbe2SRoman Zippel 	default:
1694cf3cbe2SRoman Zippel 		return;
1704cf3cbe2SRoman Zippel 	}
1714cf3cbe2SRoman Zippel 	prop = sym_get_range_prop(sym);
1724cf3cbe2SRoman Zippel 	if (!prop)
1734cf3cbe2SRoman Zippel 		return;
1744cf3cbe2SRoman Zippel 	val = strtol(sym->curr.val, NULL, base);
1754cf3cbe2SRoman Zippel 	val2 = sym_get_range_val(prop->expr->left.sym, base);
1764cf3cbe2SRoman Zippel 	if (val >= val2) {
1774cf3cbe2SRoman Zippel 		val2 = sym_get_range_val(prop->expr->right.sym, base);
1784cf3cbe2SRoman Zippel 		if (val <= val2)
1794cf3cbe2SRoman Zippel 			return;
1804cf3cbe2SRoman Zippel 	}
1814cf3cbe2SRoman Zippel 	if (sym->type == S_INT)
182b57caaaeSKees Cook 		sprintf(str, "%ld", val2);
1834cf3cbe2SRoman Zippel 	else
184b57caaaeSKees Cook 		sprintf(str, "0x%lx", val2);
1854cf3cbe2SRoman Zippel 	sym->curr.val = strdup(str);
1864cf3cbe2SRoman Zippel }
1874cf3cbe2SRoman Zippel 
1881da177e4SLinus Torvalds static void sym_calc_visibility(struct symbol *sym)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	struct property *prop;
1911da177e4SLinus Torvalds 	tristate tri;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	/* any prompt visible? */
1941da177e4SLinus Torvalds 	tri = no;
1951da177e4SLinus Torvalds 	for_all_prompts(sym, prop) {
1961da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
197d6ee3576SSam Ravnborg 		tri = EXPR_OR(tri, prop->visible.tri);
1981da177e4SLinus Torvalds 	}
1991da177e4SLinus Torvalds 	if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
2001da177e4SLinus Torvalds 		tri = yes;
2011da177e4SLinus Torvalds 	if (sym->visible != tri) {
2021da177e4SLinus Torvalds 		sym->visible = tri;
2031da177e4SLinus Torvalds 		sym_set_changed(sym);
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds 	if (sym_is_choice_value(sym))
2061da177e4SLinus Torvalds 		return;
207246cf9c2SCatalin Marinas 	/* defaulting to "yes" if no explicit "depends on" are given */
208246cf9c2SCatalin Marinas 	tri = yes;
209246cf9c2SCatalin Marinas 	if (sym->dir_dep.expr)
210246cf9c2SCatalin Marinas 		tri = expr_calc_value(sym->dir_dep.expr);
211246cf9c2SCatalin Marinas 	if (tri == mod)
212246cf9c2SCatalin Marinas 		tri = yes;
213246cf9c2SCatalin Marinas 	if (sym->dir_dep.tri != tri) {
214246cf9c2SCatalin Marinas 		sym->dir_dep.tri = tri;
215246cf9c2SCatalin Marinas 		sym_set_changed(sym);
216246cf9c2SCatalin Marinas 	}
2171da177e4SLinus Torvalds 	tri = no;
2181da177e4SLinus Torvalds 	if (sym->rev_dep.expr)
2191da177e4SLinus Torvalds 		tri = expr_calc_value(sym->rev_dep.expr);
2201da177e4SLinus Torvalds 	if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
2211da177e4SLinus Torvalds 		tri = yes;
2221da177e4SLinus Torvalds 	if (sym->rev_dep.tri != tri) {
2231da177e4SLinus Torvalds 		sym->rev_dep.tri = tri;
2241da177e4SLinus Torvalds 		sym_set_changed(sym);
2251da177e4SLinus Torvalds 	}
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
228c252147dSSam Ravnborg /*
229c252147dSSam Ravnborg  * Find the default symbol for a choice.
230c252147dSSam Ravnborg  * First try the default values for the choice symbol
231c252147dSSam Ravnborg  * Next locate the first visible choice value
232c252147dSSam Ravnborg  * Return NULL if none was found
233c252147dSSam Ravnborg  */
234c252147dSSam Ravnborg struct symbol *sym_choice_default(struct symbol *sym)
2351da177e4SLinus Torvalds {
2361da177e4SLinus Torvalds 	struct symbol *def_sym;
2371da177e4SLinus Torvalds 	struct property *prop;
2381da177e4SLinus Torvalds 	struct expr *e;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	/* any of the defaults visible? */
2411da177e4SLinus Torvalds 	for_all_defaults(sym, prop) {
2421da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
2431da177e4SLinus Torvalds 		if (prop->visible.tri == no)
2441da177e4SLinus Torvalds 			continue;
2451da177e4SLinus Torvalds 		def_sym = prop_get_symbol(prop);
2461da177e4SLinus Torvalds 		if (def_sym->visible != no)
2471da177e4SLinus Torvalds 			return def_sym;
2481da177e4SLinus Torvalds 	}
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	/* just get the first visible value */
2511da177e4SLinus Torvalds 	prop = sym_get_choice_prop(sym);
2520a28c47bSJan Beulich 	expr_list_for_each_sym(prop->expr, e, def_sym)
2531da177e4SLinus Torvalds 		if (def_sym->visible != no)
2541da177e4SLinus Torvalds 			return def_sym;
2551da177e4SLinus Torvalds 
256c252147dSSam Ravnborg 	/* failed to locate any defaults */
257c252147dSSam Ravnborg 	return NULL;
258c252147dSSam Ravnborg }
259c252147dSSam Ravnborg 
260c252147dSSam Ravnborg static struct symbol *sym_calc_choice(struct symbol *sym)
261c252147dSSam Ravnborg {
262c252147dSSam Ravnborg 	struct symbol *def_sym;
263c252147dSSam Ravnborg 	struct property *prop;
264c252147dSSam Ravnborg 	struct expr *e;
2655d09598dSArnaud Lacombe 	int flags;
266c252147dSSam Ravnborg 
267c252147dSSam Ravnborg 	/* first calculate all choice values' visibilities */
2685d09598dSArnaud Lacombe 	flags = sym->flags;
269c252147dSSam Ravnborg 	prop = sym_get_choice_prop(sym);
2705d09598dSArnaud Lacombe 	expr_list_for_each_sym(prop->expr, e, def_sym) {
271c252147dSSam Ravnborg 		sym_calc_visibility(def_sym);
2725d09598dSArnaud Lacombe 		if (def_sym->visible != no)
2735d09598dSArnaud Lacombe 			flags &= def_sym->flags;
2745d09598dSArnaud Lacombe 	}
2755d09598dSArnaud Lacombe 
2765d09598dSArnaud Lacombe 	sym->flags &= flags | ~SYMBOL_DEF_USER;
277c252147dSSam Ravnborg 
278c252147dSSam Ravnborg 	/* is the user choice visible? */
279c252147dSSam Ravnborg 	def_sym = sym->def[S_DEF_USER].val;
280c252147dSSam Ravnborg 	if (def_sym && def_sym->visible != no)
281c252147dSSam Ravnborg 		return def_sym;
282c252147dSSam Ravnborg 
283c252147dSSam Ravnborg 	def_sym = sym_choice_default(sym);
284c252147dSSam Ravnborg 
285c252147dSSam Ravnborg 	if (def_sym == NULL)
2861da177e4SLinus Torvalds 		/* no choice? reset tristate value */
2871da177e4SLinus Torvalds 		sym->curr.tri = no;
288c252147dSSam Ravnborg 
289c252147dSSam Ravnborg 	return def_sym;
2901da177e4SLinus Torvalds }
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds void sym_calc_value(struct symbol *sym)
2931da177e4SLinus Torvalds {
2941da177e4SLinus Torvalds 	struct symbol_value newval, oldval;
2951da177e4SLinus Torvalds 	struct property *prop;
2961da177e4SLinus Torvalds 	struct expr *e;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	if (!sym)
2991da177e4SLinus Torvalds 		return;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	if (sym->flags & SYMBOL_VALID)
3021da177e4SLinus Torvalds 		return;
303fbe98bb9SArve Hjønnevåg 
304fbe98bb9SArve Hjønnevåg 	if (sym_is_choice_value(sym) &&
305fbe98bb9SArve Hjønnevåg 	    sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES) {
306fbe98bb9SArve Hjønnevåg 		sym->flags &= ~SYMBOL_NEED_SET_CHOICE_VALUES;
307fbe98bb9SArve Hjønnevåg 		prop = sym_get_choice_prop(sym);
308fbe98bb9SArve Hjønnevåg 		sym_calc_value(prop_get_symbol(prop));
309fbe98bb9SArve Hjønnevåg 	}
310fbe98bb9SArve Hjønnevåg 
3111da177e4SLinus Torvalds 	sym->flags |= SYMBOL_VALID;
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	oldval = sym->curr;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	switch (sym->type) {
3161da177e4SLinus Torvalds 	case S_INT:
3171da177e4SLinus Torvalds 	case S_HEX:
3181da177e4SLinus Torvalds 	case S_STRING:
3191da177e4SLinus Torvalds 		newval = symbol_empty.curr;
3201da177e4SLinus Torvalds 		break;
3211da177e4SLinus Torvalds 	case S_BOOLEAN:
3221da177e4SLinus Torvalds 	case S_TRISTATE:
3231da177e4SLinus Torvalds 		newval = symbol_no.curr;
3241da177e4SLinus Torvalds 		break;
3251da177e4SLinus Torvalds 	default:
3261da177e4SLinus Torvalds 		sym->curr.val = sym->name;
3271da177e4SLinus Torvalds 		sym->curr.tri = no;
3281da177e4SLinus Torvalds 		return;
3291da177e4SLinus Torvalds 	}
3301da177e4SLinus Torvalds 	if (!sym_is_choice_value(sym))
3311da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_WRITE;
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	sym_calc_visibility(sym);
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	/* set default if recursively called */
3361da177e4SLinus Torvalds 	sym->curr = newval;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	switch (sym_get_type(sym)) {
3391da177e4SLinus Torvalds 	case S_BOOLEAN:
3401da177e4SLinus Torvalds 	case S_TRISTATE:
3411da177e4SLinus Torvalds 		if (sym_is_choice_value(sym) && sym->visible == yes) {
3421da177e4SLinus Torvalds 			prop = sym_get_choice_prop(sym);
3431da177e4SLinus Torvalds 			newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
344587c9061SRoman Zippel 		} else {
345587c9061SRoman Zippel 			if (sym->visible != no) {
346587c9061SRoman Zippel 				/* if the symbol is visible use the user value
347587c9061SRoman Zippel 				 * if available, otherwise try the default value
348587c9061SRoman Zippel 				 */
3491da177e4SLinus Torvalds 				sym->flags |= SYMBOL_WRITE;
350587c9061SRoman Zippel 				if (sym_has_value(sym)) {
351587c9061SRoman Zippel 					newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
352587c9061SRoman Zippel 							      sym->visible);
353587c9061SRoman Zippel 					goto calc_newval;
3541da177e4SLinus Torvalds 				}
355587c9061SRoman Zippel 			}
356587c9061SRoman Zippel 			if (sym->rev_dep.tri != no)
357587c9061SRoman Zippel 				sym->flags |= SYMBOL_WRITE;
358587c9061SRoman Zippel 			if (!sym_is_choice(sym)) {
3591da177e4SLinus Torvalds 				prop = sym_get_default_prop(sym);
3601da177e4SLinus Torvalds 				if (prop) {
3611da177e4SLinus Torvalds 					sym->flags |= SYMBOL_WRITE;
362587c9061SRoman Zippel 					newval.tri = EXPR_AND(expr_calc_value(prop->expr),
363587c9061SRoman Zippel 							      prop->visible.tri);
3641da177e4SLinus Torvalds 				}
3651da177e4SLinus Torvalds 			}
366587c9061SRoman Zippel 		calc_newval:
367246cf9c2SCatalin Marinas 			if (sym->dir_dep.tri == no && sym->rev_dep.tri != no) {
3681137c56bSArnaud Lacombe 				struct expr *e;
3691137c56bSArnaud Lacombe 				e = expr_simplify_unmet_dep(sym->rev_dep.expr,
3701137c56bSArnaud Lacombe 				    sym->dir_dep.expr);
371246cf9c2SCatalin Marinas 				fprintf(stderr, "warning: (");
3721137c56bSArnaud Lacombe 				expr_fprint(e, stderr);
373246cf9c2SCatalin Marinas 				fprintf(stderr, ") selects %s which has unmet direct dependencies (",
374246cf9c2SCatalin Marinas 					sym->name);
375246cf9c2SCatalin Marinas 				expr_fprint(sym->dir_dep.expr, stderr);
376246cf9c2SCatalin Marinas 				fprintf(stderr, ")\n");
3771137c56bSArnaud Lacombe 				expr_free(e);
378246cf9c2SCatalin Marinas 			}
379587c9061SRoman Zippel 			newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
380587c9061SRoman Zippel 		}
3811da177e4SLinus Torvalds 		if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
3821da177e4SLinus Torvalds 			newval.tri = yes;
3831da177e4SLinus Torvalds 		break;
3841da177e4SLinus Torvalds 	case S_STRING:
3851da177e4SLinus Torvalds 	case S_HEX:
3861da177e4SLinus Torvalds 	case S_INT:
3871da177e4SLinus Torvalds 		if (sym->visible != no) {
3881da177e4SLinus Torvalds 			sym->flags |= SYMBOL_WRITE;
3891da177e4SLinus Torvalds 			if (sym_has_value(sym)) {
3900c1822e6SRoman Zippel 				newval.val = sym->def[S_DEF_USER].val;
3911da177e4SLinus Torvalds 				break;
3921da177e4SLinus Torvalds 			}
3931da177e4SLinus Torvalds 		}
3941da177e4SLinus Torvalds 		prop = sym_get_default_prop(sym);
3951da177e4SLinus Torvalds 		if (prop) {
3961da177e4SLinus Torvalds 			struct symbol *ds = prop_get_symbol(prop);
3971da177e4SLinus Torvalds 			if (ds) {
3981da177e4SLinus Torvalds 				sym->flags |= SYMBOL_WRITE;
3991da177e4SLinus Torvalds 				sym_calc_value(ds);
4001da177e4SLinus Torvalds 				newval.val = ds->curr.val;
4011da177e4SLinus Torvalds 			}
4021da177e4SLinus Torvalds 		}
4031da177e4SLinus Torvalds 		break;
4041da177e4SLinus Torvalds 	default:
4051da177e4SLinus Torvalds 		;
4061da177e4SLinus Torvalds 	}
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	sym->curr = newval;
4091da177e4SLinus Torvalds 	if (sym_is_choice(sym) && newval.tri == yes)
4101da177e4SLinus Torvalds 		sym->curr.val = sym_calc_choice(sym);
4114cf3cbe2SRoman Zippel 	sym_validate_range(sym);
4121da177e4SLinus Torvalds 
413face4374SRoman Zippel 	if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
4141da177e4SLinus Torvalds 		sym_set_changed(sym);
415face4374SRoman Zippel 		if (modules_sym == sym) {
416face4374SRoman Zippel 			sym_set_all_changed();
4171da177e4SLinus Torvalds 			modules_val = modules_sym->curr.tri;
418face4374SRoman Zippel 		}
419face4374SRoman Zippel 	}
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	if (sym_is_choice(sym)) {
4227a962923SRoman Zippel 		struct symbol *choice_sym;
4237a962923SRoman Zippel 
4241da177e4SLinus Torvalds 		prop = sym_get_choice_prop(sym);
4257a962923SRoman Zippel 		expr_list_for_each_sym(prop->expr, e, choice_sym) {
4260a28c47bSJan Beulich 			if ((sym->flags & SYMBOL_WRITE) &&
4270a28c47bSJan Beulich 			    choice_sym->visible != no)
4280a28c47bSJan Beulich 				choice_sym->flags |= SYMBOL_WRITE;
4290a28c47bSJan Beulich 			if (sym->flags & SYMBOL_CHANGED)
4307a962923SRoman Zippel 				sym_set_changed(choice_sym);
4311da177e4SLinus Torvalds 		}
4321da177e4SLinus Torvalds 	}
4335a1aa8a1SRoman Zippel 
4345a1aa8a1SRoman Zippel 	if (sym->flags & SYMBOL_AUTO)
4355a1aa8a1SRoman Zippel 		sym->flags &= ~SYMBOL_WRITE;
436fbe98bb9SArve Hjønnevåg 
437fbe98bb9SArve Hjønnevåg 	if (sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES)
438fbe98bb9SArve Hjønnevåg 		set_all_choice_values(sym);
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds void sym_clear_all_valid(void)
4421da177e4SLinus Torvalds {
4431da177e4SLinus Torvalds 	struct symbol *sym;
4441da177e4SLinus Torvalds 	int i;
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	for_all_symbols(i, sym)
4471da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_VALID;
448bfc10001SKarsten Wiese 	sym_add_change_count(1);
4491da177e4SLinus Torvalds 	if (modules_sym)
4501da177e4SLinus Torvalds 		sym_calc_value(modules_sym);
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds void sym_set_changed(struct symbol *sym)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds 	struct property *prop;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	sym->flags |= SYMBOL_CHANGED;
4581da177e4SLinus Torvalds 	for (prop = sym->prop; prop; prop = prop->next) {
4591da177e4SLinus Torvalds 		if (prop->menu)
4601da177e4SLinus Torvalds 			prop->menu->flags |= MENU_CHANGED;
4611da177e4SLinus Torvalds 	}
4621da177e4SLinus Torvalds }
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds void sym_set_all_changed(void)
4651da177e4SLinus Torvalds {
4661da177e4SLinus Torvalds 	struct symbol *sym;
4671da177e4SLinus Torvalds 	int i;
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	for_all_symbols(i, sym)
4701da177e4SLinus Torvalds 		sym_set_changed(sym);
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds bool sym_tristate_within_range(struct symbol *sym, tristate val)
4741da177e4SLinus Torvalds {
4751da177e4SLinus Torvalds 	int type = sym_get_type(sym);
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	if (sym->visible == no)
4781da177e4SLinus Torvalds 		return false;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	if (type != S_BOOLEAN && type != S_TRISTATE)
4811da177e4SLinus Torvalds 		return false;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	if (type == S_BOOLEAN && val == mod)
4841da177e4SLinus Torvalds 		return false;
4851da177e4SLinus Torvalds 	if (sym->visible <= sym->rev_dep.tri)
4861da177e4SLinus Torvalds 		return false;
4871da177e4SLinus Torvalds 	if (sym_is_choice_value(sym) && sym->visible == yes)
4881da177e4SLinus Torvalds 		return val == yes;
4891da177e4SLinus Torvalds 	return val >= sym->rev_dep.tri && val <= sym->visible;
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds bool sym_set_tristate_value(struct symbol *sym, tristate val)
4931da177e4SLinus Torvalds {
4941da177e4SLinus Torvalds 	tristate oldval = sym_get_tristate_value(sym);
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	if (oldval != val && !sym_tristate_within_range(sym, val))
4971da177e4SLinus Torvalds 		return false;
4981da177e4SLinus Torvalds 
499669bfad9SRoman Zippel 	if (!(sym->flags & SYMBOL_DEF_USER)) {
500669bfad9SRoman Zippel 		sym->flags |= SYMBOL_DEF_USER;
5011da177e4SLinus Torvalds 		sym_set_changed(sym);
5021da177e4SLinus Torvalds 	}
5033f23ca2bSRoman Zippel 	/*
5043f23ca2bSRoman Zippel 	 * setting a choice value also resets the new flag of the choice
5053f23ca2bSRoman Zippel 	 * symbol and all other choice values.
5063f23ca2bSRoman Zippel 	 */
5071da177e4SLinus Torvalds 	if (sym_is_choice_value(sym) && val == yes) {
5081da177e4SLinus Torvalds 		struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
5093f23ca2bSRoman Zippel 		struct property *prop;
5103f23ca2bSRoman Zippel 		struct expr *e;
5111da177e4SLinus Torvalds 
5120c1822e6SRoman Zippel 		cs->def[S_DEF_USER].val = sym;
513669bfad9SRoman Zippel 		cs->flags |= SYMBOL_DEF_USER;
5143f23ca2bSRoman Zippel 		prop = sym_get_choice_prop(cs);
5153f23ca2bSRoman Zippel 		for (e = prop->expr; e; e = e->left.expr) {
5163f23ca2bSRoman Zippel 			if (e->right.sym->visible != no)
517669bfad9SRoman Zippel 				e->right.sym->flags |= SYMBOL_DEF_USER;
5183f23ca2bSRoman Zippel 		}
5191da177e4SLinus Torvalds 	}
5201da177e4SLinus Torvalds 
5210c1822e6SRoman Zippel 	sym->def[S_DEF_USER].tri = val;
522face4374SRoman Zippel 	if (oldval != val)
5231da177e4SLinus Torvalds 		sym_clear_all_valid();
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	return true;
5261da177e4SLinus Torvalds }
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds tristate sym_toggle_tristate_value(struct symbol *sym)
5291da177e4SLinus Torvalds {
5301da177e4SLinus Torvalds 	tristate oldval, newval;
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	oldval = newval = sym_get_tristate_value(sym);
5331da177e4SLinus Torvalds 	do {
5341da177e4SLinus Torvalds 		switch (newval) {
5351da177e4SLinus Torvalds 		case no:
5361da177e4SLinus Torvalds 			newval = mod;
5371da177e4SLinus Torvalds 			break;
5381da177e4SLinus Torvalds 		case mod:
5391da177e4SLinus Torvalds 			newval = yes;
5401da177e4SLinus Torvalds 			break;
5411da177e4SLinus Torvalds 		case yes:
5421da177e4SLinus Torvalds 			newval = no;
5431da177e4SLinus Torvalds 			break;
5441da177e4SLinus Torvalds 		}
5451da177e4SLinus Torvalds 		if (sym_set_tristate_value(sym, newval))
5461da177e4SLinus Torvalds 			break;
5471da177e4SLinus Torvalds 	} while (oldval != newval);
5481da177e4SLinus Torvalds 	return newval;
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds bool sym_string_valid(struct symbol *sym, const char *str)
5521da177e4SLinus Torvalds {
5531da177e4SLinus Torvalds 	signed char ch;
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	switch (sym->type) {
5561da177e4SLinus Torvalds 	case S_STRING:
5571da177e4SLinus Torvalds 		return true;
5581da177e4SLinus Torvalds 	case S_INT:
5591da177e4SLinus Torvalds 		ch = *str++;
5601da177e4SLinus Torvalds 		if (ch == '-')
5611da177e4SLinus Torvalds 			ch = *str++;
5621da177e4SLinus Torvalds 		if (!isdigit(ch))
5631da177e4SLinus Torvalds 			return false;
5641da177e4SLinus Torvalds 		if (ch == '0' && *str != 0)
5651da177e4SLinus Torvalds 			return false;
5661da177e4SLinus Torvalds 		while ((ch = *str++)) {
5671da177e4SLinus Torvalds 			if (!isdigit(ch))
5681da177e4SLinus Torvalds 				return false;
5691da177e4SLinus Torvalds 		}
5701da177e4SLinus Torvalds 		return true;
5711da177e4SLinus Torvalds 	case S_HEX:
5721da177e4SLinus Torvalds 		if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
5731da177e4SLinus Torvalds 			str += 2;
5741da177e4SLinus Torvalds 		ch = *str++;
5751da177e4SLinus Torvalds 		do {
5761da177e4SLinus Torvalds 			if (!isxdigit(ch))
5771da177e4SLinus Torvalds 				return false;
5781da177e4SLinus Torvalds 		} while ((ch = *str++));
5791da177e4SLinus Torvalds 		return true;
5801da177e4SLinus Torvalds 	case S_BOOLEAN:
5811da177e4SLinus Torvalds 	case S_TRISTATE:
5821da177e4SLinus Torvalds 		switch (str[0]) {
5831da177e4SLinus Torvalds 		case 'y': case 'Y':
5841da177e4SLinus Torvalds 		case 'm': case 'M':
5851da177e4SLinus Torvalds 		case 'n': case 'N':
5861da177e4SLinus Torvalds 			return true;
5871da177e4SLinus Torvalds 		}
5881da177e4SLinus Torvalds 		return false;
5891da177e4SLinus Torvalds 	default:
5901da177e4SLinus Torvalds 		return false;
5911da177e4SLinus Torvalds 	}
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds bool sym_string_within_range(struct symbol *sym, const char *str)
5951da177e4SLinus Torvalds {
5961da177e4SLinus Torvalds 	struct property *prop;
597b57caaaeSKees Cook 	long val;
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	switch (sym->type) {
6001da177e4SLinus Torvalds 	case S_STRING:
6011da177e4SLinus Torvalds 		return sym_string_valid(sym, str);
6021da177e4SLinus Torvalds 	case S_INT:
6031da177e4SLinus Torvalds 		if (!sym_string_valid(sym, str))
6041da177e4SLinus Torvalds 			return false;
6051da177e4SLinus Torvalds 		prop = sym_get_range_prop(sym);
6061da177e4SLinus Torvalds 		if (!prop)
6071da177e4SLinus Torvalds 			return true;
6081da177e4SLinus Torvalds 		val = strtol(str, NULL, 10);
6094cf3cbe2SRoman Zippel 		return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
6104cf3cbe2SRoman Zippel 		       val <= sym_get_range_val(prop->expr->right.sym, 10);
6111da177e4SLinus Torvalds 	case S_HEX:
6121da177e4SLinus Torvalds 		if (!sym_string_valid(sym, str))
6131da177e4SLinus Torvalds 			return false;
6141da177e4SLinus Torvalds 		prop = sym_get_range_prop(sym);
6151da177e4SLinus Torvalds 		if (!prop)
6161da177e4SLinus Torvalds 			return true;
6171da177e4SLinus Torvalds 		val = strtol(str, NULL, 16);
6184cf3cbe2SRoman Zippel 		return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
6194cf3cbe2SRoman Zippel 		       val <= sym_get_range_val(prop->expr->right.sym, 16);
6201da177e4SLinus Torvalds 	case S_BOOLEAN:
6211da177e4SLinus Torvalds 	case S_TRISTATE:
6221da177e4SLinus Torvalds 		switch (str[0]) {
6231da177e4SLinus Torvalds 		case 'y': case 'Y':
6241da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, yes);
6251da177e4SLinus Torvalds 		case 'm': case 'M':
6261da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, mod);
6271da177e4SLinus Torvalds 		case 'n': case 'N':
6281da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, no);
6291da177e4SLinus Torvalds 		}
6301da177e4SLinus Torvalds 		return false;
6311da177e4SLinus Torvalds 	default:
6321da177e4SLinus Torvalds 		return false;
6331da177e4SLinus Torvalds 	}
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds bool sym_set_string_value(struct symbol *sym, const char *newval)
6371da177e4SLinus Torvalds {
6381da177e4SLinus Torvalds 	const char *oldval;
6391da177e4SLinus Torvalds 	char *val;
6401da177e4SLinus Torvalds 	int size;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	switch (sym->type) {
6431da177e4SLinus Torvalds 	case S_BOOLEAN:
6441da177e4SLinus Torvalds 	case S_TRISTATE:
6451da177e4SLinus Torvalds 		switch (newval[0]) {
6461da177e4SLinus Torvalds 		case 'y': case 'Y':
6471da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, yes);
6481da177e4SLinus Torvalds 		case 'm': case 'M':
6491da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, mod);
6501da177e4SLinus Torvalds 		case 'n': case 'N':
6511da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, no);
6521da177e4SLinus Torvalds 		}
6531da177e4SLinus Torvalds 		return false;
6541da177e4SLinus Torvalds 	default:
6551da177e4SLinus Torvalds 		;
6561da177e4SLinus Torvalds 	}
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	if (!sym_string_within_range(sym, newval))
6591da177e4SLinus Torvalds 		return false;
6601da177e4SLinus Torvalds 
661669bfad9SRoman Zippel 	if (!(sym->flags & SYMBOL_DEF_USER)) {
662669bfad9SRoman Zippel 		sym->flags |= SYMBOL_DEF_USER;
6631da177e4SLinus Torvalds 		sym_set_changed(sym);
6641da177e4SLinus Torvalds 	}
6651da177e4SLinus Torvalds 
6660c1822e6SRoman Zippel 	oldval = sym->def[S_DEF_USER].val;
6671da177e4SLinus Torvalds 	size = strlen(newval) + 1;
6681da177e4SLinus Torvalds 	if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
6691da177e4SLinus Torvalds 		size += 2;
670177acf78SAlan Cox 		sym->def[S_DEF_USER].val = val = xmalloc(size);
6711da177e4SLinus Torvalds 		*val++ = '0';
6721da177e4SLinus Torvalds 		*val++ = 'x';
6731da177e4SLinus Torvalds 	} else if (!oldval || strcmp(oldval, newval))
674177acf78SAlan Cox 		sym->def[S_DEF_USER].val = val = xmalloc(size);
6751da177e4SLinus Torvalds 	else
6761da177e4SLinus Torvalds 		return true;
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	strcpy(val, newval);
6791da177e4SLinus Torvalds 	free((void *)oldval);
6801da177e4SLinus Torvalds 	sym_clear_all_valid();
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	return true;
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
6857cf3d73bSSam Ravnborg /*
6867cf3d73bSSam Ravnborg  * Find the default value associated to a symbol.
6877cf3d73bSSam Ravnborg  * For tristate symbol handle the modules=n case
6887cf3d73bSSam Ravnborg  * in which case "m" becomes "y".
6897cf3d73bSSam Ravnborg  * If the symbol does not have any default then fallback
6907cf3d73bSSam Ravnborg  * to the fixed default values.
6917cf3d73bSSam Ravnborg  */
6927cf3d73bSSam Ravnborg const char *sym_get_string_default(struct symbol *sym)
6937cf3d73bSSam Ravnborg {
6947cf3d73bSSam Ravnborg 	struct property *prop;
6957cf3d73bSSam Ravnborg 	struct symbol *ds;
6967cf3d73bSSam Ravnborg 	const char *str;
6977cf3d73bSSam Ravnborg 	tristate val;
6987cf3d73bSSam Ravnborg 
6997cf3d73bSSam Ravnborg 	sym_calc_visibility(sym);
7007cf3d73bSSam Ravnborg 	sym_calc_value(modules_sym);
7017cf3d73bSSam Ravnborg 	val = symbol_no.curr.tri;
7027cf3d73bSSam Ravnborg 	str = symbol_empty.curr.val;
7037cf3d73bSSam Ravnborg 
7047cf3d73bSSam Ravnborg 	/* If symbol has a default value look it up */
7057cf3d73bSSam Ravnborg 	prop = sym_get_default_prop(sym);
7067cf3d73bSSam Ravnborg 	if (prop != NULL) {
7077cf3d73bSSam Ravnborg 		switch (sym->type) {
7087cf3d73bSSam Ravnborg 		case S_BOOLEAN:
7097cf3d73bSSam Ravnborg 		case S_TRISTATE:
710579fb8e7SArnaud Lacombe 			/* The visibility may limit the value from yes => mod */
7117cf3d73bSSam Ravnborg 			val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
7127cf3d73bSSam Ravnborg 			break;
7137cf3d73bSSam Ravnborg 		default:
7147cf3d73bSSam Ravnborg 			/*
7157cf3d73bSSam Ravnborg 			 * The following fails to handle the situation
7167cf3d73bSSam Ravnborg 			 * where a default value is further limited by
7177cf3d73bSSam Ravnborg 			 * the valid range.
7187cf3d73bSSam Ravnborg 			 */
7197cf3d73bSSam Ravnborg 			ds = prop_get_symbol(prop);
7207cf3d73bSSam Ravnborg 			if (ds != NULL) {
7217cf3d73bSSam Ravnborg 				sym_calc_value(ds);
7227cf3d73bSSam Ravnborg 				str = (const char *)ds->curr.val;
7237cf3d73bSSam Ravnborg 			}
7247cf3d73bSSam Ravnborg 		}
7257cf3d73bSSam Ravnborg 	}
7267cf3d73bSSam Ravnborg 
7277cf3d73bSSam Ravnborg 	/* Handle select statements */
7287cf3d73bSSam Ravnborg 	val = EXPR_OR(val, sym->rev_dep.tri);
7297cf3d73bSSam Ravnborg 
7307cf3d73bSSam Ravnborg 	/* transpose mod to yes if modules are not enabled */
7317cf3d73bSSam Ravnborg 	if (val == mod)
7327cf3d73bSSam Ravnborg 		if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
7337cf3d73bSSam Ravnborg 			val = yes;
7347cf3d73bSSam Ravnborg 
7357cf3d73bSSam Ravnborg 	/* transpose mod to yes if type is bool */
7367cf3d73bSSam Ravnborg 	if (sym->type == S_BOOLEAN && val == mod)
7377cf3d73bSSam Ravnborg 		val = yes;
7387cf3d73bSSam Ravnborg 
7397cf3d73bSSam Ravnborg 	switch (sym->type) {
7407cf3d73bSSam Ravnborg 	case S_BOOLEAN:
7417cf3d73bSSam Ravnborg 	case S_TRISTATE:
7427cf3d73bSSam Ravnborg 		switch (val) {
7437cf3d73bSSam Ravnborg 		case no: return "n";
7447cf3d73bSSam Ravnborg 		case mod: return "m";
7457cf3d73bSSam Ravnborg 		case yes: return "y";
7467cf3d73bSSam Ravnborg 		}
7477cf3d73bSSam Ravnborg 	case S_INT:
7487cf3d73bSSam Ravnborg 	case S_HEX:
7497cf3d73bSSam Ravnborg 		return str;
7507cf3d73bSSam Ravnborg 	case S_STRING:
7517cf3d73bSSam Ravnborg 		return str;
7527cf3d73bSSam Ravnborg 	case S_OTHER:
7537cf3d73bSSam Ravnborg 	case S_UNKNOWN:
7547cf3d73bSSam Ravnborg 		break;
7557cf3d73bSSam Ravnborg 	}
7567cf3d73bSSam Ravnborg 	return "";
7577cf3d73bSSam Ravnborg }
7587cf3d73bSSam Ravnborg 
7591da177e4SLinus Torvalds const char *sym_get_string_value(struct symbol *sym)
7601da177e4SLinus Torvalds {
7611da177e4SLinus Torvalds 	tristate val;
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	switch (sym->type) {
7641da177e4SLinus Torvalds 	case S_BOOLEAN:
7651da177e4SLinus Torvalds 	case S_TRISTATE:
7661da177e4SLinus Torvalds 		val = sym_get_tristate_value(sym);
7671da177e4SLinus Torvalds 		switch (val) {
7681da177e4SLinus Torvalds 		case no:
7691da177e4SLinus Torvalds 			return "n";
7701da177e4SLinus Torvalds 		case mod:
771e54e692bSArnaud Lacombe 			sym_calc_value(modules_sym);
772e54e692bSArnaud Lacombe 			return (modules_sym->curr.tri == no) ? "n" : "m";
7731da177e4SLinus Torvalds 		case yes:
7741da177e4SLinus Torvalds 			return "y";
7751da177e4SLinus Torvalds 		}
7761da177e4SLinus Torvalds 		break;
7771da177e4SLinus Torvalds 	default:
7781da177e4SLinus Torvalds 		;
7791da177e4SLinus Torvalds 	}
7801da177e4SLinus Torvalds 	return (const char *)sym->curr.val;
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds bool sym_is_changable(struct symbol *sym)
7841da177e4SLinus Torvalds {
7851da177e4SLinus Torvalds 	return sym->visible > sym->rev_dep.tri;
7861da177e4SLinus Torvalds }
7871da177e4SLinus Torvalds 
788e66f25d7SAndi Kleen static unsigned strhash(const char *s)
789e66f25d7SAndi Kleen {
790e66f25d7SAndi Kleen 	/* fnv32 hash */
791e66f25d7SAndi Kleen 	unsigned hash = 2166136261U;
792e66f25d7SAndi Kleen 	for (; *s; s++)
793e66f25d7SAndi Kleen 		hash = (hash ^ *s) * 0x01000193;
794e66f25d7SAndi Kleen 	return hash;
795e66f25d7SAndi Kleen }
796e66f25d7SAndi Kleen 
7975a1aa8a1SRoman Zippel struct symbol *sym_lookup(const char *name, int flags)
7981da177e4SLinus Torvalds {
7991da177e4SLinus Torvalds 	struct symbol *symbol;
8001da177e4SLinus Torvalds 	char *new_name;
801e66f25d7SAndi Kleen 	int hash;
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	if (name) {
8041da177e4SLinus Torvalds 		if (name[0] && !name[1]) {
8051da177e4SLinus Torvalds 			switch (name[0]) {
8061da177e4SLinus Torvalds 			case 'y': return &symbol_yes;
8071da177e4SLinus Torvalds 			case 'm': return &symbol_mod;
8081da177e4SLinus Torvalds 			case 'n': return &symbol_no;
8091da177e4SLinus Torvalds 			}
8101da177e4SLinus Torvalds 		}
811e66f25d7SAndi Kleen 		hash = strhash(name) % SYMBOL_HASHSIZE;
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 		for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
814e66f25d7SAndi Kleen 			if (symbol->name &&
815e66f25d7SAndi Kleen 			    !strcmp(symbol->name, name) &&
8165a1aa8a1SRoman Zippel 			    (flags ? symbol->flags & flags
8175a1aa8a1SRoman Zippel 				   : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
8181da177e4SLinus Torvalds 				return symbol;
8191da177e4SLinus Torvalds 		}
8201da177e4SLinus Torvalds 		new_name = strdup(name);
8211da177e4SLinus Torvalds 	} else {
8221da177e4SLinus Torvalds 		new_name = NULL;
823e66f25d7SAndi Kleen 		hash = 0;
8241da177e4SLinus Torvalds 	}
8251da177e4SLinus Torvalds 
826177acf78SAlan Cox 	symbol = xmalloc(sizeof(*symbol));
8271da177e4SLinus Torvalds 	memset(symbol, 0, sizeof(*symbol));
8281da177e4SLinus Torvalds 	symbol->name = new_name;
8291da177e4SLinus Torvalds 	symbol->type = S_UNKNOWN;
8305a1aa8a1SRoman Zippel 	symbol->flags |= flags;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	symbol->next = symbol_hash[hash];
8331da177e4SLinus Torvalds 	symbol_hash[hash] = symbol;
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	return symbol;
8361da177e4SLinus Torvalds }
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds struct symbol *sym_find(const char *name)
8391da177e4SLinus Torvalds {
8401da177e4SLinus Torvalds 	struct symbol *symbol = NULL;
8411da177e4SLinus Torvalds 	int hash = 0;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	if (!name)
8441da177e4SLinus Torvalds 		return NULL;
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 	if (name[0] && !name[1]) {
8471da177e4SLinus Torvalds 		switch (name[0]) {
8481da177e4SLinus Torvalds 		case 'y': return &symbol_yes;
8491da177e4SLinus Torvalds 		case 'm': return &symbol_mod;
8501da177e4SLinus Torvalds 		case 'n': return &symbol_no;
8511da177e4SLinus Torvalds 		}
8521da177e4SLinus Torvalds 	}
853e66f25d7SAndi Kleen 	hash = strhash(name) % SYMBOL_HASHSIZE;
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
856e66f25d7SAndi Kleen 		if (symbol->name &&
857e66f25d7SAndi Kleen 		    !strcmp(symbol->name, name) &&
8581da177e4SLinus Torvalds 		    !(symbol->flags & SYMBOL_CONST))
8591da177e4SLinus Torvalds 				break;
8601da177e4SLinus Torvalds 	}
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	return symbol;
8631da177e4SLinus Torvalds }
8641da177e4SLinus Torvalds 
86576a54095SArnaud Lacombe /*
86676a54095SArnaud Lacombe  * Expand symbol's names embedded in the string given in argument. Symbols'
86776a54095SArnaud Lacombe  * name to be expanded shall be prefixed by a '$'. Unknown symbol expands to
86876a54095SArnaud Lacombe  * the empty string.
86976a54095SArnaud Lacombe  */
87076a54095SArnaud Lacombe const char *sym_expand_string_value(const char *in)
87176a54095SArnaud Lacombe {
87276a54095SArnaud Lacombe 	const char *src;
87376a54095SArnaud Lacombe 	char *res;
87476a54095SArnaud Lacombe 	size_t reslen;
87576a54095SArnaud Lacombe 
87676a54095SArnaud Lacombe 	reslen = strlen(in) + 1;
877177acf78SAlan Cox 	res = xmalloc(reslen);
87876a54095SArnaud Lacombe 	res[0] = '\0';
87976a54095SArnaud Lacombe 
88076a54095SArnaud Lacombe 	while ((src = strchr(in, '$'))) {
88176a54095SArnaud Lacombe 		char *p, name[SYMBOL_MAXLENGTH];
88276a54095SArnaud Lacombe 		const char *symval = "";
88376a54095SArnaud Lacombe 		struct symbol *sym;
88476a54095SArnaud Lacombe 		size_t newlen;
88576a54095SArnaud Lacombe 
88676a54095SArnaud Lacombe 		strncat(res, in, src - in);
88776a54095SArnaud Lacombe 		src++;
88876a54095SArnaud Lacombe 
88976a54095SArnaud Lacombe 		p = name;
89076a54095SArnaud Lacombe 		while (isalnum(*src) || *src == '_')
89176a54095SArnaud Lacombe 			*p++ = *src++;
89276a54095SArnaud Lacombe 		*p = '\0';
89376a54095SArnaud Lacombe 
89476a54095SArnaud Lacombe 		sym = sym_find(name);
89576a54095SArnaud Lacombe 		if (sym != NULL) {
89676a54095SArnaud Lacombe 			sym_calc_value(sym);
89776a54095SArnaud Lacombe 			symval = sym_get_string_value(sym);
89876a54095SArnaud Lacombe 		}
89976a54095SArnaud Lacombe 
900020e773fSAndy Whitcroft 		newlen = strlen(res) + strlen(symval) + strlen(src) + 1;
90176a54095SArnaud Lacombe 		if (newlen > reslen) {
90276a54095SArnaud Lacombe 			reslen = newlen;
90319c29f32SMichal Marek 			res = realloc(res, reslen);
90476a54095SArnaud Lacombe 		}
90576a54095SArnaud Lacombe 
90676a54095SArnaud Lacombe 		strcat(res, symval);
90776a54095SArnaud Lacombe 		in = src;
90876a54095SArnaud Lacombe 	}
90976a54095SArnaud Lacombe 	strcat(res, in);
91076a54095SArnaud Lacombe 
91176a54095SArnaud Lacombe 	return res;
91276a54095SArnaud Lacombe }
91376a54095SArnaud Lacombe 
914e54e692bSArnaud Lacombe const char *sym_escape_string_value(const char *in)
915e54e692bSArnaud Lacombe {
916e54e692bSArnaud Lacombe 	const char *p;
917e54e692bSArnaud Lacombe 	size_t reslen;
918e54e692bSArnaud Lacombe 	char *res;
919e54e692bSArnaud Lacombe 	size_t l;
920e54e692bSArnaud Lacombe 
921e54e692bSArnaud Lacombe 	reslen = strlen(in) + strlen("\"\"") + 1;
922e54e692bSArnaud Lacombe 
923e54e692bSArnaud Lacombe 	p = in;
924e54e692bSArnaud Lacombe 	for (;;) {
925e54e692bSArnaud Lacombe 		l = strcspn(p, "\"\\");
926e54e692bSArnaud Lacombe 		p += l;
927e54e692bSArnaud Lacombe 
928e54e692bSArnaud Lacombe 		if (p[0] == '\0')
929e54e692bSArnaud Lacombe 			break;
930e54e692bSArnaud Lacombe 
931e54e692bSArnaud Lacombe 		reslen++;
932e54e692bSArnaud Lacombe 		p++;
933e54e692bSArnaud Lacombe 	}
934e54e692bSArnaud Lacombe 
935177acf78SAlan Cox 	res = xmalloc(reslen);
936e54e692bSArnaud Lacombe 	res[0] = '\0';
937e54e692bSArnaud Lacombe 
938e54e692bSArnaud Lacombe 	strcat(res, "\"");
939e54e692bSArnaud Lacombe 
940e54e692bSArnaud Lacombe 	p = in;
941e54e692bSArnaud Lacombe 	for (;;) {
942e54e692bSArnaud Lacombe 		l = strcspn(p, "\"\\");
943e54e692bSArnaud Lacombe 		strncat(res, p, l);
944e54e692bSArnaud Lacombe 		p += l;
945e54e692bSArnaud Lacombe 
946e54e692bSArnaud Lacombe 		if (p[0] == '\0')
947e54e692bSArnaud Lacombe 			break;
948e54e692bSArnaud Lacombe 
949e54e692bSArnaud Lacombe 		strcat(res, "\\");
950e54e692bSArnaud Lacombe 		strncat(res, p++, 1);
951e54e692bSArnaud Lacombe 	}
952e54e692bSArnaud Lacombe 
953e54e692bSArnaud Lacombe 	strcat(res, "\"");
954e54e692bSArnaud Lacombe 	return res;
955e54e692bSArnaud Lacombe }
956e54e692bSArnaud Lacombe 
957193b40aeSYann E. MORIN struct sym_match {
958193b40aeSYann E. MORIN 	struct symbol	*sym;
959193b40aeSYann E. MORIN 	off_t		so, eo;
960193b40aeSYann E. MORIN };
961193b40aeSYann E. MORIN 
962193b40aeSYann E. MORIN /* Compare matched symbols as thus:
963193b40aeSYann E. MORIN  * - first, symbols that match exactly
964193b40aeSYann E. MORIN  * - then, alphabetical sort
965193b40aeSYann E. MORIN  */
966193b40aeSYann E. MORIN static int sym_rel_comp(const void *sym1, const void *sym2)
967193b40aeSYann E. MORIN {
968193b40aeSYann E. MORIN 	struct sym_match *s1 = *(struct sym_match **)sym1;
969193b40aeSYann E. MORIN 	struct sym_match *s2 = *(struct sym_match **)sym2;
97026e933e3SYann E. MORIN 	int exact1, exact2;
971193b40aeSYann E. MORIN 
972193b40aeSYann E. MORIN 	/* Exact match:
973193b40aeSYann E. MORIN 	 * - if matched length on symbol s1 is the length of that symbol,
974193b40aeSYann E. MORIN 	 *   then this symbol should come first;
975193b40aeSYann E. MORIN 	 * - if matched length on symbol s2 is the length of that symbol,
976193b40aeSYann E. MORIN 	 *   then this symbol should come first.
977193b40aeSYann E. MORIN 	 * Note: since the search can be a regexp, both symbols may match
978193b40aeSYann E. MORIN 	 * exactly; if this is the case, we can't decide which comes first,
979193b40aeSYann E. MORIN 	 * and we fallback to sorting alphabetically.
980193b40aeSYann E. MORIN 	 */
98126e933e3SYann E. MORIN 	exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
98226e933e3SYann E. MORIN 	exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
98326e933e3SYann E. MORIN 	if (exact1 && !exact2)
984193b40aeSYann E. MORIN 		return -1;
98526e933e3SYann E. MORIN 	if (!exact1 && exact2)
986193b40aeSYann E. MORIN 		return 1;
987193b40aeSYann E. MORIN 
988193b40aeSYann E. MORIN 	/* As a fallback, sort symbols alphabetically */
989193b40aeSYann E. MORIN 	return strcmp(s1->sym->name, s2->sym->name);
990193b40aeSYann E. MORIN }
991193b40aeSYann E. MORIN 
9921da177e4SLinus Torvalds struct symbol **sym_re_search(const char *pattern)
9931da177e4SLinus Torvalds {
9941da177e4SLinus Torvalds 	struct symbol *sym, **sym_arr = NULL;
995193b40aeSYann E. MORIN 	struct sym_match **sym_match_arr = NULL;
9961da177e4SLinus Torvalds 	int i, cnt, size;
9971da177e4SLinus Torvalds 	regex_t re;
998193b40aeSYann E. MORIN 	regmatch_t match[1];
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 	cnt = size = 0;
10011da177e4SLinus Torvalds 	/* Skip if empty */
10021da177e4SLinus Torvalds 	if (strlen(pattern) == 0)
10031da177e4SLinus Torvalds 		return NULL;
1004193b40aeSYann E. MORIN 	if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
10051da177e4SLinus Torvalds 		return NULL;
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
1008193b40aeSYann E. MORIN 		struct sym_match *tmp_sym_match;
10091da177e4SLinus Torvalds 		if (sym->flags & SYMBOL_CONST || !sym->name)
10101da177e4SLinus Torvalds 			continue;
1011193b40aeSYann E. MORIN 		if (regexec(&re, sym->name, 1, match, 0))
10121da177e4SLinus Torvalds 			continue;
1013*1407f97aSYann E. MORIN 		if (cnt >= size) {
1014193b40aeSYann E. MORIN 			void *tmp;
10151da177e4SLinus Torvalds 			size += 16;
1016193b40aeSYann E. MORIN 			tmp = realloc(sym_match_arr, size * sizeof(struct sym_match *));
1017803b3519SYann E. MORIN 			if (!tmp)
1018193b40aeSYann E. MORIN 				goto sym_re_search_free;
1019193b40aeSYann E. MORIN 			sym_match_arr = tmp;
10201da177e4SLinus Torvalds 		}
1021da6df879SLi Zefan 		sym_calc_value(sym);
1022193b40aeSYann E. MORIN 		tmp_sym_match = (struct sym_match*)malloc(sizeof(struct sym_match));
1023193b40aeSYann E. MORIN 		if (!tmp_sym_match)
1024193b40aeSYann E. MORIN 			goto sym_re_search_free;
1025193b40aeSYann E. MORIN 		tmp_sym_match->sym = sym;
1026803b3519SYann E. MORIN 		/* As regexec returned 0, we know we have a match, so
1027193b40aeSYann E. MORIN 		 * we can use match[0].rm_[se]o without further checks
1028193b40aeSYann E. MORIN 		 */
1029193b40aeSYann E. MORIN 		tmp_sym_match->so = match[0].rm_so;
1030193b40aeSYann E. MORIN 		tmp_sym_match->eo = match[0].rm_eo;
1031193b40aeSYann E. MORIN 		sym_match_arr[cnt++] = tmp_sym_match;
10321da177e4SLinus Torvalds 	}
1033193b40aeSYann E. MORIN 	if (sym_match_arr) {
1034193b40aeSYann E. MORIN 		qsort(sym_match_arr, cnt, sizeof(struct sym_match*), sym_rel_comp);
1035193b40aeSYann E. MORIN 		sym_arr = malloc((cnt+1) * sizeof(struct symbol));
1036193b40aeSYann E. MORIN 		if (!sym_arr)
1037193b40aeSYann E. MORIN 			goto sym_re_search_free;
1038193b40aeSYann E. MORIN 		for (i = 0; i < cnt; i++)
1039193b40aeSYann E. MORIN 			sym_arr[i] = sym_match_arr[i]->sym;
10401da177e4SLinus Torvalds 		sym_arr[cnt] = NULL;
1041193b40aeSYann E. MORIN 	}
1042193b40aeSYann E. MORIN sym_re_search_free:
1043193b40aeSYann E. MORIN 	if (sym_match_arr) {
1044193b40aeSYann E. MORIN 		for (i = 0; i < cnt; i++)
1045193b40aeSYann E. MORIN 			free(sym_match_arr[i]);
1046193b40aeSYann E. MORIN 		free(sym_match_arr);
1047193b40aeSYann E. MORIN 	}
10481da177e4SLinus Torvalds 	regfree(&re);
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	return sym_arr;
10511da177e4SLinus Torvalds }
10521da177e4SLinus Torvalds 
1053d595cea6SRoman Zippel /*
1054d595cea6SRoman Zippel  * When we check for recursive dependencies we use a stack to save
1055d595cea6SRoman Zippel  * current state so we can print out relevant info to user.
1056d595cea6SRoman Zippel  * The entries are located on the call stack so no need to free memory.
1057d595cea6SRoman Zippel  * Note inser() remove() must always match to properly clear the stack.
1058d595cea6SRoman Zippel  */
1059d595cea6SRoman Zippel static struct dep_stack {
1060d595cea6SRoman Zippel 	struct dep_stack *prev, *next;
1061d595cea6SRoman Zippel 	struct symbol *sym;
1062d595cea6SRoman Zippel 	struct property *prop;
1063d595cea6SRoman Zippel 	struct expr *expr;
1064d595cea6SRoman Zippel } *check_top;
1065d595cea6SRoman Zippel 
1066d595cea6SRoman Zippel static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
1067d595cea6SRoman Zippel {
1068d595cea6SRoman Zippel 	memset(stack, 0, sizeof(*stack));
1069d595cea6SRoman Zippel 	if (check_top)
1070d595cea6SRoman Zippel 		check_top->next = stack;
1071d595cea6SRoman Zippel 	stack->prev = check_top;
1072d595cea6SRoman Zippel 	stack->sym = sym;
1073d595cea6SRoman Zippel 	check_top = stack;
1074d595cea6SRoman Zippel }
1075d595cea6SRoman Zippel 
1076d595cea6SRoman Zippel static void dep_stack_remove(void)
1077d595cea6SRoman Zippel {
1078d595cea6SRoman Zippel 	check_top = check_top->prev;
1079d595cea6SRoman Zippel 	if (check_top)
1080d595cea6SRoman Zippel 		check_top->next = NULL;
1081d595cea6SRoman Zippel }
1082d595cea6SRoman Zippel 
1083d595cea6SRoman Zippel /*
1084d595cea6SRoman Zippel  * Called when we have detected a recursive dependency.
1085d595cea6SRoman Zippel  * check_top point to the top of the stact so we use
1086d595cea6SRoman Zippel  * the ->prev pointer to locate the bottom of the stack.
1087d595cea6SRoman Zippel  */
1088d595cea6SRoman Zippel static void sym_check_print_recursive(struct symbol *last_sym)
1089d595cea6SRoman Zippel {
1090d595cea6SRoman Zippel 	struct dep_stack *stack;
1091d595cea6SRoman Zippel 	struct symbol *sym, *next_sym;
1092d595cea6SRoman Zippel 	struct menu *menu = NULL;
1093d595cea6SRoman Zippel 	struct property *prop;
1094d595cea6SRoman Zippel 	struct dep_stack cv_stack;
1095d595cea6SRoman Zippel 
1096d595cea6SRoman Zippel 	if (sym_is_choice_value(last_sym)) {
1097d595cea6SRoman Zippel 		dep_stack_insert(&cv_stack, last_sym);
1098d595cea6SRoman Zippel 		last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
1099d595cea6SRoman Zippel 	}
1100d595cea6SRoman Zippel 
1101d595cea6SRoman Zippel 	for (stack = check_top; stack != NULL; stack = stack->prev)
1102d595cea6SRoman Zippel 		if (stack->sym == last_sym)
1103d595cea6SRoman Zippel 			break;
1104d595cea6SRoman Zippel 	if (!stack) {
1105d595cea6SRoman Zippel 		fprintf(stderr, "unexpected recursive dependency error\n");
1106d595cea6SRoman Zippel 		return;
1107d595cea6SRoman Zippel 	}
1108d595cea6SRoman Zippel 
1109d595cea6SRoman Zippel 	for (; stack; stack = stack->next) {
1110d595cea6SRoman Zippel 		sym = stack->sym;
1111d595cea6SRoman Zippel 		next_sym = stack->next ? stack->next->sym : last_sym;
1112d595cea6SRoman Zippel 		prop = stack->prop;
11133643f849SSam Ravnborg 		if (prop == NULL)
11143643f849SSam Ravnborg 			prop = stack->sym->prop;
1115d595cea6SRoman Zippel 
1116d595cea6SRoman Zippel 		/* for choice values find the menu entry (used below) */
1117d595cea6SRoman Zippel 		if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
1118d595cea6SRoman Zippel 			for (prop = sym->prop; prop; prop = prop->next) {
1119d595cea6SRoman Zippel 				menu = prop->menu;
1120d595cea6SRoman Zippel 				if (prop->menu)
1121d595cea6SRoman Zippel 					break;
1122d595cea6SRoman Zippel 			}
1123d595cea6SRoman Zippel 		}
1124d595cea6SRoman Zippel 		if (stack->sym == last_sym)
1125d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
1126d595cea6SRoman Zippel 				prop->file->name, prop->lineno);
1127d595cea6SRoman Zippel 		if (stack->expr) {
1128d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
1129d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
1130d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
1131d595cea6SRoman Zippel 				prop_get_type_name(prop->type),
1132d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
1133d595cea6SRoman Zippel 		} else if (stack->prop) {
1134d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
1135d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
1136d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
1137d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
1138d595cea6SRoman Zippel 		} else if (sym_is_choice(sym)) {
1139d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
1140d595cea6SRoman Zippel 				menu->file->name, menu->lineno,
1141d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
1142d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
1143d595cea6SRoman Zippel 		} else if (sym_is_choice_value(sym)) {
1144d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
1145d595cea6SRoman Zippel 				menu->file->name, menu->lineno,
1146d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
1147d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
1148d595cea6SRoman Zippel 		} else {
1149d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
1150d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
1151d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
1152d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
1153d595cea6SRoman Zippel 		}
1154d595cea6SRoman Zippel 	}
1155d595cea6SRoman Zippel 
1156d595cea6SRoman Zippel 	if (check_top == &cv_stack)
1157d595cea6SRoman Zippel 		dep_stack_remove();
1158d595cea6SRoman Zippel }
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds static struct symbol *sym_check_expr_deps(struct expr *e)
11611da177e4SLinus Torvalds {
11621da177e4SLinus Torvalds 	struct symbol *sym;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	if (!e)
11651da177e4SLinus Torvalds 		return NULL;
11661da177e4SLinus Torvalds 	switch (e->type) {
11671da177e4SLinus Torvalds 	case E_OR:
11681da177e4SLinus Torvalds 	case E_AND:
11691da177e4SLinus Torvalds 		sym = sym_check_expr_deps(e->left.expr);
11701da177e4SLinus Torvalds 		if (sym)
11711da177e4SLinus Torvalds 			return sym;
11721da177e4SLinus Torvalds 		return sym_check_expr_deps(e->right.expr);
11731da177e4SLinus Torvalds 	case E_NOT:
11741da177e4SLinus Torvalds 		return sym_check_expr_deps(e->left.expr);
11751da177e4SLinus Torvalds 	case E_EQUAL:
11761da177e4SLinus Torvalds 	case E_UNEQUAL:
11771da177e4SLinus Torvalds 		sym = sym_check_deps(e->left.sym);
11781da177e4SLinus Torvalds 		if (sym)
11791da177e4SLinus Torvalds 			return sym;
11801da177e4SLinus Torvalds 		return sym_check_deps(e->right.sym);
11811da177e4SLinus Torvalds 	case E_SYMBOL:
11821da177e4SLinus Torvalds 		return sym_check_deps(e->left.sym);
11831da177e4SLinus Torvalds 	default:
11841da177e4SLinus Torvalds 		break;
11851da177e4SLinus Torvalds 	}
11861da177e4SLinus Torvalds 	printf("Oops! How to check %d?\n", e->type);
11871da177e4SLinus Torvalds 	return NULL;
11881da177e4SLinus Torvalds }
11891da177e4SLinus Torvalds 
11905447d34bSSam Ravnborg /* return NULL when dependencies are OK */
119148981178SRoman Zippel static struct symbol *sym_check_sym_deps(struct symbol *sym)
119248981178SRoman Zippel {
119348981178SRoman Zippel 	struct symbol *sym2;
119448981178SRoman Zippel 	struct property *prop;
1195d595cea6SRoman Zippel 	struct dep_stack stack;
1196d595cea6SRoman Zippel 
1197d595cea6SRoman Zippel 	dep_stack_insert(&stack, sym);
119848981178SRoman Zippel 
119948981178SRoman Zippel 	sym2 = sym_check_expr_deps(sym->rev_dep.expr);
120048981178SRoman Zippel 	if (sym2)
1201d595cea6SRoman Zippel 		goto out;
120248981178SRoman Zippel 
120348981178SRoman Zippel 	for (prop = sym->prop; prop; prop = prop->next) {
120448981178SRoman Zippel 		if (prop->type == P_CHOICE || prop->type == P_SELECT)
120548981178SRoman Zippel 			continue;
1206d595cea6SRoman Zippel 		stack.prop = prop;
120748981178SRoman Zippel 		sym2 = sym_check_expr_deps(prop->visible.expr);
120848981178SRoman Zippel 		if (sym2)
120948981178SRoman Zippel 			break;
121048981178SRoman Zippel 		if (prop->type != P_DEFAULT || sym_is_choice(sym))
121148981178SRoman Zippel 			continue;
1212d595cea6SRoman Zippel 		stack.expr = prop->expr;
121348981178SRoman Zippel 		sym2 = sym_check_expr_deps(prop->expr);
121448981178SRoman Zippel 		if (sym2)
121548981178SRoman Zippel 			break;
1216d595cea6SRoman Zippel 		stack.expr = NULL;
121748981178SRoman Zippel 	}
121848981178SRoman Zippel 
1219d595cea6SRoman Zippel out:
1220d595cea6SRoman Zippel 	dep_stack_remove();
1221d595cea6SRoman Zippel 
122248981178SRoman Zippel 	return sym2;
122348981178SRoman Zippel }
122448981178SRoman Zippel 
122548981178SRoman Zippel static struct symbol *sym_check_choice_deps(struct symbol *choice)
122648981178SRoman Zippel {
122748981178SRoman Zippel 	struct symbol *sym, *sym2;
122848981178SRoman Zippel 	struct property *prop;
122948981178SRoman Zippel 	struct expr *e;
1230d595cea6SRoman Zippel 	struct dep_stack stack;
1231d595cea6SRoman Zippel 
1232d595cea6SRoman Zippel 	dep_stack_insert(&stack, choice);
123348981178SRoman Zippel 
123448981178SRoman Zippel 	prop = sym_get_choice_prop(choice);
123548981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym)
123648981178SRoman Zippel 		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
123748981178SRoman Zippel 
123848981178SRoman Zippel 	choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
123948981178SRoman Zippel 	sym2 = sym_check_sym_deps(choice);
124048981178SRoman Zippel 	choice->flags &= ~SYMBOL_CHECK;
124148981178SRoman Zippel 	if (sym2)
124248981178SRoman Zippel 		goto out;
124348981178SRoman Zippel 
124448981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym) {
124548981178SRoman Zippel 		sym2 = sym_check_sym_deps(sym);
1246d595cea6SRoman Zippel 		if (sym2)
124748981178SRoman Zippel 			break;
124848981178SRoman Zippel 	}
124948981178SRoman Zippel out:
125048981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym)
125148981178SRoman Zippel 		sym->flags &= ~SYMBOL_CHECK;
125248981178SRoman Zippel 
125348981178SRoman Zippel 	if (sym2 && sym_is_choice_value(sym2) &&
125448981178SRoman Zippel 	    prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
125548981178SRoman Zippel 		sym2 = choice;
125648981178SRoman Zippel 
1257d595cea6SRoman Zippel 	dep_stack_remove();
1258d595cea6SRoman Zippel 
125948981178SRoman Zippel 	return sym2;
126048981178SRoman Zippel }
126148981178SRoman Zippel 
12621da177e4SLinus Torvalds struct symbol *sym_check_deps(struct symbol *sym)
12631da177e4SLinus Torvalds {
12641da177e4SLinus Torvalds 	struct symbol *sym2;
12651da177e4SLinus Torvalds 	struct property *prop;
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 	if (sym->flags & SYMBOL_CHECK) {
1268d595cea6SRoman Zippel 		sym_check_print_recursive(sym);
12691da177e4SLinus Torvalds 		return sym;
12701da177e4SLinus Torvalds 	}
12713f04e7ddSDavid Gibson 	if (sym->flags & SYMBOL_CHECKED)
12723f04e7ddSDavid Gibson 		return NULL;
12731da177e4SLinus Torvalds 
127448981178SRoman Zippel 	if (sym_is_choice_value(sym)) {
1275d595cea6SRoman Zippel 		struct dep_stack stack;
1276d595cea6SRoman Zippel 
127748981178SRoman Zippel 		/* for choice groups start the check with main choice symbol */
1278d595cea6SRoman Zippel 		dep_stack_insert(&stack, sym);
127948981178SRoman Zippel 		prop = sym_get_choice_prop(sym);
128048981178SRoman Zippel 		sym2 = sym_check_deps(prop_get_symbol(prop));
1281d595cea6SRoman Zippel 		dep_stack_remove();
128248981178SRoman Zippel 	} else if (sym_is_choice(sym)) {
128348981178SRoman Zippel 		sym2 = sym_check_choice_deps(sym);
128448981178SRoman Zippel 	} else {
12851da177e4SLinus Torvalds 		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
128648981178SRoman Zippel 		sym2 = sym_check_sym_deps(sym);
12871da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_CHECK;
128848981178SRoman Zippel 	}
128948981178SRoman Zippel 
1290d595cea6SRoman Zippel 	if (sym2 && sym2 == sym)
129148981178SRoman Zippel 		sym2 = NULL;
129248981178SRoman Zippel 
12931da177e4SLinus Torvalds 	return sym2;
12941da177e4SLinus Torvalds }
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds struct property *prop_alloc(enum prop_type type, struct symbol *sym)
12971da177e4SLinus Torvalds {
12981da177e4SLinus Torvalds 	struct property *prop;
12991da177e4SLinus Torvalds 	struct property **propp;
13001da177e4SLinus Torvalds 
1301177acf78SAlan Cox 	prop = xmalloc(sizeof(*prop));
13021da177e4SLinus Torvalds 	memset(prop, 0, sizeof(*prop));
13031da177e4SLinus Torvalds 	prop->type = type;
13041da177e4SLinus Torvalds 	prop->sym = sym;
13051da177e4SLinus Torvalds 	prop->file = current_file;
13061da177e4SLinus Torvalds 	prop->lineno = zconf_lineno();
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	/* append property to the prop list of symbol */
13091da177e4SLinus Torvalds 	if (sym) {
13101da177e4SLinus Torvalds 		for (propp = &sym->prop; *propp; propp = &(*propp)->next)
13111da177e4SLinus Torvalds 			;
13121da177e4SLinus Torvalds 		*propp = prop;
13131da177e4SLinus Torvalds 	}
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 	return prop;
13161da177e4SLinus Torvalds }
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds struct symbol *prop_get_symbol(struct property *prop)
13191da177e4SLinus Torvalds {
13201da177e4SLinus Torvalds 	if (prop->expr && (prop->expr->type == E_SYMBOL ||
13217a962923SRoman Zippel 			   prop->expr->type == E_LIST))
13221da177e4SLinus Torvalds 		return prop->expr->left.sym;
13231da177e4SLinus Torvalds 	return NULL;
13241da177e4SLinus Torvalds }
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds const char *prop_get_type_name(enum prop_type type)
13271da177e4SLinus Torvalds {
13281da177e4SLinus Torvalds 	switch (type) {
13291da177e4SLinus Torvalds 	case P_PROMPT:
13301da177e4SLinus Torvalds 		return "prompt";
133193449082SRoman Zippel 	case P_ENV:
133293449082SRoman Zippel 		return "env";
13331da177e4SLinus Torvalds 	case P_COMMENT:
13341da177e4SLinus Torvalds 		return "comment";
13351da177e4SLinus Torvalds 	case P_MENU:
13361da177e4SLinus Torvalds 		return "menu";
13371da177e4SLinus Torvalds 	case P_DEFAULT:
13381da177e4SLinus Torvalds 		return "default";
13391da177e4SLinus Torvalds 	case P_CHOICE:
13401da177e4SLinus Torvalds 		return "choice";
13411da177e4SLinus Torvalds 	case P_SELECT:
13421da177e4SLinus Torvalds 		return "select";
13431da177e4SLinus Torvalds 	case P_RANGE:
13441da177e4SLinus Torvalds 		return "range";
134559e89e3dSSam Ravnborg 	case P_SYMBOL:
134659e89e3dSSam Ravnborg 		return "symbol";
13471da177e4SLinus Torvalds 	case P_UNKNOWN:
13481da177e4SLinus Torvalds 		break;
13491da177e4SLinus Torvalds 	}
13501da177e4SLinus Torvalds 	return "unknown";
13511da177e4SLinus Torvalds }
135293449082SRoman Zippel 
13534356f489STrevor Keith static void prop_add_env(const char *env)
135493449082SRoman Zippel {
135593449082SRoman Zippel 	struct symbol *sym, *sym2;
135693449082SRoman Zippel 	struct property *prop;
135793449082SRoman Zippel 	char *p;
135893449082SRoman Zippel 
135993449082SRoman Zippel 	sym = current_entry->sym;
136093449082SRoman Zippel 	sym->flags |= SYMBOL_AUTO;
136193449082SRoman Zippel 	for_all_properties(sym, prop, P_ENV) {
136293449082SRoman Zippel 		sym2 = prop_get_symbol(prop);
136393449082SRoman Zippel 		if (strcmp(sym2->name, env))
136493449082SRoman Zippel 			menu_warn(current_entry, "redefining environment symbol from %s",
136593449082SRoman Zippel 				  sym2->name);
136693449082SRoman Zippel 		return;
136793449082SRoman Zippel 	}
136893449082SRoman Zippel 
136993449082SRoman Zippel 	prop = prop_alloc(P_ENV, sym);
13705a1aa8a1SRoman Zippel 	prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
137193449082SRoman Zippel 
137293449082SRoman Zippel 	sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
137393449082SRoman Zippel 	sym_env_list->right.sym = sym;
137493449082SRoman Zippel 
137593449082SRoman Zippel 	p = getenv(env);
137693449082SRoman Zippel 	if (p)
137793449082SRoman Zippel 		sym_add_default(sym, p);
137893449082SRoman Zippel 	else
137993449082SRoman Zippel 		menu_warn(current_entry, "environment variable %s undefined", env);
138093449082SRoman Zippel }
1381