xref: /openbmc/linux/scripts/kconfig/symbol.c (revision 0c1822e6991a10da6dc391f0a2e2cf5fb2e31238)
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 #define LKC_DIRECT_LINK
131da177e4SLinus Torvalds #include "lkc.h"
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds struct symbol symbol_yes = {
161da177e4SLinus Torvalds 	.name = "y",
171da177e4SLinus Torvalds 	.curr = { "y", yes },
18c0e150acSRoman Zippel 	.flags = SYMBOL_CONST|SYMBOL_VALID,
191da177e4SLinus Torvalds }, symbol_mod = {
201da177e4SLinus Torvalds 	.name = "m",
211da177e4SLinus Torvalds 	.curr = { "m", mod },
22c0e150acSRoman Zippel 	.flags = SYMBOL_CONST|SYMBOL_VALID,
231da177e4SLinus Torvalds }, symbol_no = {
241da177e4SLinus Torvalds 	.name = "n",
251da177e4SLinus Torvalds 	.curr = { "n", no },
26c0e150acSRoman Zippel 	.flags = SYMBOL_CONST|SYMBOL_VALID,
271da177e4SLinus Torvalds }, symbol_empty = {
281da177e4SLinus Torvalds 	.name = "",
291da177e4SLinus Torvalds 	.curr = { "", no },
301da177e4SLinus Torvalds 	.flags = SYMBOL_VALID,
311da177e4SLinus Torvalds };
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds int sym_change_count;
341da177e4SLinus Torvalds struct symbol *modules_sym;
351da177e4SLinus Torvalds tristate modules_val;
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds void sym_add_default(struct symbol *sym, const char *def)
381da177e4SLinus Torvalds {
391da177e4SLinus Torvalds 	struct property *prop = prop_alloc(P_DEFAULT, sym);
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds 	prop->expr = expr_alloc_symbol(sym_lookup(def, 1));
421da177e4SLinus Torvalds }
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds void sym_init(void)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	struct symbol *sym;
471da177e4SLinus Torvalds 	struct utsname uts;
481da177e4SLinus Torvalds 	char *p;
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("ARCH", 0);
581da177e4SLinus Torvalds 	sym->type = S_STRING;
591da177e4SLinus Torvalds 	sym->flags |= SYMBOL_AUTO;
601da177e4SLinus Torvalds 	p = getenv("ARCH");
611da177e4SLinus Torvalds 	if (p)
621da177e4SLinus Torvalds 		sym_add_default(sym, p);
631da177e4SLinus Torvalds 
642244cbd8SSam Ravnborg 	sym = sym_lookup("KERNELVERSION", 0);
651da177e4SLinus Torvalds 	sym->type = S_STRING;
661da177e4SLinus Torvalds 	sym->flags |= SYMBOL_AUTO;
672244cbd8SSam Ravnborg 	p = getenv("KERNELVERSION");
681da177e4SLinus Torvalds 	if (p)
691da177e4SLinus Torvalds 		sym_add_default(sym, p);
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	sym = sym_lookup("UNAME_RELEASE", 0);
721da177e4SLinus Torvalds 	sym->type = S_STRING;
731da177e4SLinus Torvalds 	sym->flags |= SYMBOL_AUTO;
741da177e4SLinus Torvalds 	sym_add_default(sym, uts.release);
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds enum symbol_type sym_get_type(struct symbol *sym)
781da177e4SLinus Torvalds {
791da177e4SLinus Torvalds 	enum symbol_type type = sym->type;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	if (type == S_TRISTATE) {
821da177e4SLinus Torvalds 		if (sym_is_choice_value(sym) && sym->visible == yes)
831da177e4SLinus Torvalds 			type = S_BOOLEAN;
841da177e4SLinus Torvalds 		else if (modules_val == no)
851da177e4SLinus Torvalds 			type = S_BOOLEAN;
861da177e4SLinus Torvalds 	}
871da177e4SLinus Torvalds 	return type;
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds const char *sym_type_name(enum symbol_type type)
911da177e4SLinus Torvalds {
921da177e4SLinus Torvalds 	switch (type) {
931da177e4SLinus Torvalds 	case S_BOOLEAN:
941da177e4SLinus Torvalds 		return "boolean";
951da177e4SLinus Torvalds 	case S_TRISTATE:
961da177e4SLinus Torvalds 		return "tristate";
971da177e4SLinus Torvalds 	case S_INT:
981da177e4SLinus Torvalds 		return "integer";
991da177e4SLinus Torvalds 	case S_HEX:
1001da177e4SLinus Torvalds 		return "hex";
1011da177e4SLinus Torvalds 	case S_STRING:
1021da177e4SLinus Torvalds 		return "string";
1031da177e4SLinus Torvalds 	case S_UNKNOWN:
1041da177e4SLinus Torvalds 		return "unknown";
1051da177e4SLinus Torvalds 	case S_OTHER:
1061da177e4SLinus Torvalds 		break;
1071da177e4SLinus Torvalds 	}
1081da177e4SLinus Torvalds 	return "???";
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds struct property *sym_get_choice_prop(struct symbol *sym)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	struct property *prop;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	for_all_choices(sym, prop)
1161da177e4SLinus Torvalds 		return prop;
1171da177e4SLinus Torvalds 	return NULL;
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds struct property *sym_get_default_prop(struct symbol *sym)
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds 	struct property *prop;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	for_all_defaults(sym, prop) {
1251da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
1261da177e4SLinus Torvalds 		if (prop->visible.tri != no)
1271da177e4SLinus Torvalds 			return prop;
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds 	return NULL;
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds struct property *sym_get_range_prop(struct symbol *sym)
1331da177e4SLinus Torvalds {
1341da177e4SLinus Torvalds 	struct property *prop;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	for_all_properties(sym, prop, P_RANGE) {
1371da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
1381da177e4SLinus Torvalds 		if (prop->visible.tri != no)
1391da177e4SLinus Torvalds 			return prop;
1401da177e4SLinus Torvalds 	}
1411da177e4SLinus Torvalds 	return NULL;
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
1444cf3cbe2SRoman Zippel static int sym_get_range_val(struct symbol *sym, int base)
1454cf3cbe2SRoman Zippel {
1464cf3cbe2SRoman Zippel 	sym_calc_value(sym);
1474cf3cbe2SRoman Zippel 	switch (sym->type) {
1484cf3cbe2SRoman Zippel 	case S_INT:
1494cf3cbe2SRoman Zippel 		base = 10;
1504cf3cbe2SRoman Zippel 		break;
1514cf3cbe2SRoman Zippel 	case S_HEX:
1524cf3cbe2SRoman Zippel 		base = 16;
1534cf3cbe2SRoman Zippel 		break;
1544cf3cbe2SRoman Zippel 	default:
1554cf3cbe2SRoman Zippel 		break;
1564cf3cbe2SRoman Zippel 	}
1574cf3cbe2SRoman Zippel 	return strtol(sym->curr.val, NULL, base);
1584cf3cbe2SRoman Zippel }
1594cf3cbe2SRoman Zippel 
1604cf3cbe2SRoman Zippel static void sym_validate_range(struct symbol *sym)
1614cf3cbe2SRoman Zippel {
1624cf3cbe2SRoman Zippel 	struct property *prop;
1634cf3cbe2SRoman Zippel 	int base, val, val2;
1644cf3cbe2SRoman Zippel 	char str[64];
1654cf3cbe2SRoman Zippel 
1664cf3cbe2SRoman Zippel 	switch (sym->type) {
1674cf3cbe2SRoman Zippel 	case S_INT:
1684cf3cbe2SRoman Zippel 		base = 10;
1694cf3cbe2SRoman Zippel 		break;
1704cf3cbe2SRoman Zippel 	case S_HEX:
1714cf3cbe2SRoman Zippel 		base = 16;
1724cf3cbe2SRoman Zippel 		break;
1734cf3cbe2SRoman Zippel 	default:
1744cf3cbe2SRoman Zippel 		return;
1754cf3cbe2SRoman Zippel 	}
1764cf3cbe2SRoman Zippel 	prop = sym_get_range_prop(sym);
1774cf3cbe2SRoman Zippel 	if (!prop)
1784cf3cbe2SRoman Zippel 		return;
1794cf3cbe2SRoman Zippel 	val = strtol(sym->curr.val, NULL, base);
1804cf3cbe2SRoman Zippel 	val2 = sym_get_range_val(prop->expr->left.sym, base);
1814cf3cbe2SRoman Zippel 	if (val >= val2) {
1824cf3cbe2SRoman Zippel 		val2 = sym_get_range_val(prop->expr->right.sym, base);
1834cf3cbe2SRoman Zippel 		if (val <= val2)
1844cf3cbe2SRoman Zippel 			return;
1854cf3cbe2SRoman Zippel 	}
1864cf3cbe2SRoman Zippel 	if (sym->type == S_INT)
1874cf3cbe2SRoman Zippel 		sprintf(str, "%d", val2);
1884cf3cbe2SRoman Zippel 	else
1894cf3cbe2SRoman Zippel 		sprintf(str, "0x%x", val2);
1904cf3cbe2SRoman Zippel 	sym->curr.val = strdup(str);
1914cf3cbe2SRoman Zippel }
1924cf3cbe2SRoman Zippel 
1931da177e4SLinus Torvalds static void sym_calc_visibility(struct symbol *sym)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds 	struct property *prop;
1961da177e4SLinus Torvalds 	tristate tri;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	/* any prompt visible? */
1991da177e4SLinus Torvalds 	tri = no;
2001da177e4SLinus Torvalds 	for_all_prompts(sym, prop) {
2011da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
2021da177e4SLinus Torvalds 		tri = E_OR(tri, prop->visible.tri);
2031da177e4SLinus Torvalds 	}
2041da177e4SLinus Torvalds 	if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
2051da177e4SLinus Torvalds 		tri = yes;
2061da177e4SLinus Torvalds 	if (sym->visible != tri) {
2071da177e4SLinus Torvalds 		sym->visible = tri;
2081da177e4SLinus Torvalds 		sym_set_changed(sym);
2091da177e4SLinus Torvalds 	}
2101da177e4SLinus Torvalds 	if (sym_is_choice_value(sym))
2111da177e4SLinus Torvalds 		return;
2121da177e4SLinus Torvalds 	tri = no;
2131da177e4SLinus Torvalds 	if (sym->rev_dep.expr)
2141da177e4SLinus Torvalds 		tri = expr_calc_value(sym->rev_dep.expr);
2151da177e4SLinus Torvalds 	if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
2161da177e4SLinus Torvalds 		tri = yes;
2171da177e4SLinus Torvalds 	if (sym->rev_dep.tri != tri) {
2181da177e4SLinus Torvalds 		sym->rev_dep.tri = tri;
2191da177e4SLinus Torvalds 		sym_set_changed(sym);
2201da177e4SLinus Torvalds 	}
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds static struct symbol *sym_calc_choice(struct symbol *sym)
2241da177e4SLinus Torvalds {
2251da177e4SLinus Torvalds 	struct symbol *def_sym;
2261da177e4SLinus Torvalds 	struct property *prop;
2271da177e4SLinus Torvalds 	struct expr *e;
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	/* is the user choice visible? */
230*0c1822e6SRoman Zippel 	def_sym = sym->def[S_DEF_USER].val;
2311da177e4SLinus Torvalds 	if (def_sym) {
2321da177e4SLinus Torvalds 		sym_calc_visibility(def_sym);
2331da177e4SLinus Torvalds 		if (def_sym->visible != no)
2341da177e4SLinus Torvalds 			return def_sym;
2351da177e4SLinus Torvalds 	}
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	/* any of the defaults visible? */
2381da177e4SLinus Torvalds 	for_all_defaults(sym, prop) {
2391da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
2401da177e4SLinus Torvalds 		if (prop->visible.tri == no)
2411da177e4SLinus Torvalds 			continue;
2421da177e4SLinus Torvalds 		def_sym = prop_get_symbol(prop);
2431da177e4SLinus Torvalds 		sym_calc_visibility(def_sym);
2441da177e4SLinus Torvalds 		if (def_sym->visible != no)
2451da177e4SLinus Torvalds 			return def_sym;
2461da177e4SLinus Torvalds 	}
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	/* just get the first visible value */
2491da177e4SLinus Torvalds 	prop = sym_get_choice_prop(sym);
2501da177e4SLinus Torvalds 	for (e = prop->expr; e; e = e->left.expr) {
2511da177e4SLinus Torvalds 		def_sym = e->right.sym;
2521da177e4SLinus Torvalds 		sym_calc_visibility(def_sym);
2531da177e4SLinus Torvalds 		if (def_sym->visible != no)
2541da177e4SLinus Torvalds 			return def_sym;
2551da177e4SLinus Torvalds 	}
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	/* no choice? reset tristate value */
2581da177e4SLinus Torvalds 	sym->curr.tri = no;
2591da177e4SLinus Torvalds 	return NULL;
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds void sym_calc_value(struct symbol *sym)
2631da177e4SLinus Torvalds {
2641da177e4SLinus Torvalds 	struct symbol_value newval, oldval;
2651da177e4SLinus Torvalds 	struct property *prop;
2661da177e4SLinus Torvalds 	struct expr *e;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	if (!sym)
2691da177e4SLinus Torvalds 		return;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	if (sym->flags & SYMBOL_VALID)
2721da177e4SLinus Torvalds 		return;
2731da177e4SLinus Torvalds 	sym->flags |= SYMBOL_VALID;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	oldval = sym->curr;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	switch (sym->type) {
2781da177e4SLinus Torvalds 	case S_INT:
2791da177e4SLinus Torvalds 	case S_HEX:
2801da177e4SLinus Torvalds 	case S_STRING:
2811da177e4SLinus Torvalds 		newval = symbol_empty.curr;
2821da177e4SLinus Torvalds 		break;
2831da177e4SLinus Torvalds 	case S_BOOLEAN:
2841da177e4SLinus Torvalds 	case S_TRISTATE:
2851da177e4SLinus Torvalds 		newval = symbol_no.curr;
2861da177e4SLinus Torvalds 		break;
2871da177e4SLinus Torvalds 	default:
2881da177e4SLinus Torvalds 		sym->curr.val = sym->name;
2891da177e4SLinus Torvalds 		sym->curr.tri = no;
2901da177e4SLinus Torvalds 		return;
2911da177e4SLinus Torvalds 	}
2921da177e4SLinus Torvalds 	if (!sym_is_choice_value(sym))
2931da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_WRITE;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	sym_calc_visibility(sym);
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	/* set default if recursively called */
2981da177e4SLinus Torvalds 	sym->curr = newval;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	switch (sym_get_type(sym)) {
3011da177e4SLinus Torvalds 	case S_BOOLEAN:
3021da177e4SLinus Torvalds 	case S_TRISTATE:
3031da177e4SLinus Torvalds 		if (sym_is_choice_value(sym) && sym->visible == yes) {
3041da177e4SLinus Torvalds 			prop = sym_get_choice_prop(sym);
3051da177e4SLinus Torvalds 			newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
3061da177e4SLinus Torvalds 		} else if (E_OR(sym->visible, sym->rev_dep.tri) != no) {
3071da177e4SLinus Torvalds 			sym->flags |= SYMBOL_WRITE;
3081da177e4SLinus Torvalds 			if (sym_has_value(sym))
309*0c1822e6SRoman Zippel 				newval.tri = sym->def[S_DEF_USER].tri;
3101da177e4SLinus Torvalds 			else if (!sym_is_choice(sym)) {
3111da177e4SLinus Torvalds 				prop = sym_get_default_prop(sym);
3121da177e4SLinus Torvalds 				if (prop)
3131da177e4SLinus Torvalds 					newval.tri = expr_calc_value(prop->expr);
3141da177e4SLinus Torvalds 			}
3151da177e4SLinus Torvalds 			newval.tri = E_OR(E_AND(newval.tri, sym->visible), sym->rev_dep.tri);
3161da177e4SLinus Torvalds 		} else if (!sym_is_choice(sym)) {
3171da177e4SLinus Torvalds 			prop = sym_get_default_prop(sym);
3181da177e4SLinus Torvalds 			if (prop) {
3191da177e4SLinus Torvalds 				sym->flags |= SYMBOL_WRITE;
3201da177e4SLinus Torvalds 				newval.tri = expr_calc_value(prop->expr);
3211da177e4SLinus Torvalds 			}
3221da177e4SLinus Torvalds 		}
3231da177e4SLinus Torvalds 		if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
3241da177e4SLinus Torvalds 			newval.tri = yes;
3251da177e4SLinus Torvalds 		break;
3261da177e4SLinus Torvalds 	case S_STRING:
3271da177e4SLinus Torvalds 	case S_HEX:
3281da177e4SLinus Torvalds 	case S_INT:
3291da177e4SLinus Torvalds 		if (sym->visible != no) {
3301da177e4SLinus Torvalds 			sym->flags |= SYMBOL_WRITE;
3311da177e4SLinus Torvalds 			if (sym_has_value(sym)) {
332*0c1822e6SRoman Zippel 				newval.val = sym->def[S_DEF_USER].val;
3331da177e4SLinus Torvalds 				break;
3341da177e4SLinus Torvalds 			}
3351da177e4SLinus Torvalds 		}
3361da177e4SLinus Torvalds 		prop = sym_get_default_prop(sym);
3371da177e4SLinus Torvalds 		if (prop) {
3381da177e4SLinus Torvalds 			struct symbol *ds = prop_get_symbol(prop);
3391da177e4SLinus Torvalds 			if (ds) {
3401da177e4SLinus Torvalds 				sym->flags |= SYMBOL_WRITE;
3411da177e4SLinus Torvalds 				sym_calc_value(ds);
3421da177e4SLinus Torvalds 				newval.val = ds->curr.val;
3431da177e4SLinus Torvalds 			}
3441da177e4SLinus Torvalds 		}
3451da177e4SLinus Torvalds 		break;
3461da177e4SLinus Torvalds 	default:
3471da177e4SLinus Torvalds 		;
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	sym->curr = newval;
3511da177e4SLinus Torvalds 	if (sym_is_choice(sym) && newval.tri == yes)
3521da177e4SLinus Torvalds 		sym->curr.val = sym_calc_choice(sym);
3534cf3cbe2SRoman Zippel 	sym_validate_range(sym);
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	if (memcmp(&oldval, &sym->curr, sizeof(oldval)))
3561da177e4SLinus Torvalds 		sym_set_changed(sym);
3571da177e4SLinus Torvalds 	if (modules_sym == sym)
3581da177e4SLinus Torvalds 		modules_val = modules_sym->curr.tri;
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	if (sym_is_choice(sym)) {
3611da177e4SLinus Torvalds 		int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
3621da177e4SLinus Torvalds 		prop = sym_get_choice_prop(sym);
3631da177e4SLinus Torvalds 		for (e = prop->expr; e; e = e->left.expr) {
3641da177e4SLinus Torvalds 			e->right.sym->flags |= flags;
3651da177e4SLinus Torvalds 			if (flags & SYMBOL_CHANGED)
3661da177e4SLinus Torvalds 				sym_set_changed(e->right.sym);
3671da177e4SLinus Torvalds 		}
3681da177e4SLinus Torvalds 	}
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds void sym_clear_all_valid(void)
3721da177e4SLinus Torvalds {
3731da177e4SLinus Torvalds 	struct symbol *sym;
3741da177e4SLinus Torvalds 	int i;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	for_all_symbols(i, sym)
3771da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_VALID;
3781da177e4SLinus Torvalds 	sym_change_count++;
3791da177e4SLinus Torvalds 	if (modules_sym)
3801da177e4SLinus Torvalds 		sym_calc_value(modules_sym);
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds void sym_set_changed(struct symbol *sym)
3841da177e4SLinus Torvalds {
3851da177e4SLinus Torvalds 	struct property *prop;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	sym->flags |= SYMBOL_CHANGED;
3881da177e4SLinus Torvalds 	for (prop = sym->prop; prop; prop = prop->next) {
3891da177e4SLinus Torvalds 		if (prop->menu)
3901da177e4SLinus Torvalds 			prop->menu->flags |= MENU_CHANGED;
3911da177e4SLinus Torvalds 	}
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds void sym_set_all_changed(void)
3951da177e4SLinus Torvalds {
3961da177e4SLinus Torvalds 	struct symbol *sym;
3971da177e4SLinus Torvalds 	int i;
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	for_all_symbols(i, sym)
4001da177e4SLinus Torvalds 		sym_set_changed(sym);
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds bool sym_tristate_within_range(struct symbol *sym, tristate val)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	int type = sym_get_type(sym);
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	if (sym->visible == no)
4081da177e4SLinus Torvalds 		return false;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	if (type != S_BOOLEAN && type != S_TRISTATE)
4111da177e4SLinus Torvalds 		return false;
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	if (type == S_BOOLEAN && val == mod)
4141da177e4SLinus Torvalds 		return false;
4151da177e4SLinus Torvalds 	if (sym->visible <= sym->rev_dep.tri)
4161da177e4SLinus Torvalds 		return false;
4171da177e4SLinus Torvalds 	if (sym_is_choice_value(sym) && sym->visible == yes)
4181da177e4SLinus Torvalds 		return val == yes;
4191da177e4SLinus Torvalds 	return val >= sym->rev_dep.tri && val <= sym->visible;
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds bool sym_set_tristate_value(struct symbol *sym, tristate val)
4231da177e4SLinus Torvalds {
4241da177e4SLinus Torvalds 	tristate oldval = sym_get_tristate_value(sym);
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	if (oldval != val && !sym_tristate_within_range(sym, val))
4271da177e4SLinus Torvalds 		return false;
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	if (sym->flags & SYMBOL_NEW) {
4301da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_NEW;
4311da177e4SLinus Torvalds 		sym_set_changed(sym);
4321da177e4SLinus Torvalds 	}
4333f23ca2bSRoman Zippel 	/*
4343f23ca2bSRoman Zippel 	 * setting a choice value also resets the new flag of the choice
4353f23ca2bSRoman Zippel 	 * symbol and all other choice values.
4363f23ca2bSRoman Zippel 	 */
4371da177e4SLinus Torvalds 	if (sym_is_choice_value(sym) && val == yes) {
4381da177e4SLinus Torvalds 		struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
4393f23ca2bSRoman Zippel 		struct property *prop;
4403f23ca2bSRoman Zippel 		struct expr *e;
4411da177e4SLinus Torvalds 
442*0c1822e6SRoman Zippel 		cs->def[S_DEF_USER].val = sym;
4431da177e4SLinus Torvalds 		cs->flags &= ~SYMBOL_NEW;
4443f23ca2bSRoman Zippel 		prop = sym_get_choice_prop(cs);
4453f23ca2bSRoman Zippel 		for (e = prop->expr; e; e = e->left.expr) {
4463f23ca2bSRoman Zippel 			if (e->right.sym->visible != no)
4473f23ca2bSRoman Zippel 				e->right.sym->flags &= ~SYMBOL_NEW;
4483f23ca2bSRoman Zippel 		}
4491da177e4SLinus Torvalds 	}
4501da177e4SLinus Torvalds 
451*0c1822e6SRoman Zippel 	sym->def[S_DEF_USER].tri = val;
4521da177e4SLinus Torvalds 	if (oldval != val) {
4531da177e4SLinus Torvalds 		sym_clear_all_valid();
4541da177e4SLinus Torvalds 		if (sym == modules_sym)
4551da177e4SLinus Torvalds 			sym_set_all_changed();
4561da177e4SLinus Torvalds 	}
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	return true;
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds tristate sym_toggle_tristate_value(struct symbol *sym)
4621da177e4SLinus Torvalds {
4631da177e4SLinus Torvalds 	tristate oldval, newval;
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	oldval = newval = sym_get_tristate_value(sym);
4661da177e4SLinus Torvalds 	do {
4671da177e4SLinus Torvalds 		switch (newval) {
4681da177e4SLinus Torvalds 		case no:
4691da177e4SLinus Torvalds 			newval = mod;
4701da177e4SLinus Torvalds 			break;
4711da177e4SLinus Torvalds 		case mod:
4721da177e4SLinus Torvalds 			newval = yes;
4731da177e4SLinus Torvalds 			break;
4741da177e4SLinus Torvalds 		case yes:
4751da177e4SLinus Torvalds 			newval = no;
4761da177e4SLinus Torvalds 			break;
4771da177e4SLinus Torvalds 		}
4781da177e4SLinus Torvalds 		if (sym_set_tristate_value(sym, newval))
4791da177e4SLinus Torvalds 			break;
4801da177e4SLinus Torvalds 	} while (oldval != newval);
4811da177e4SLinus Torvalds 	return newval;
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds bool sym_string_valid(struct symbol *sym, const char *str)
4851da177e4SLinus Torvalds {
4861da177e4SLinus Torvalds 	signed char ch;
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	switch (sym->type) {
4891da177e4SLinus Torvalds 	case S_STRING:
4901da177e4SLinus Torvalds 		return true;
4911da177e4SLinus Torvalds 	case S_INT:
4921da177e4SLinus Torvalds 		ch = *str++;
4931da177e4SLinus Torvalds 		if (ch == '-')
4941da177e4SLinus Torvalds 			ch = *str++;
4951da177e4SLinus Torvalds 		if (!isdigit(ch))
4961da177e4SLinus Torvalds 			return false;
4971da177e4SLinus Torvalds 		if (ch == '0' && *str != 0)
4981da177e4SLinus Torvalds 			return false;
4991da177e4SLinus Torvalds 		while ((ch = *str++)) {
5001da177e4SLinus Torvalds 			if (!isdigit(ch))
5011da177e4SLinus Torvalds 				return false;
5021da177e4SLinus Torvalds 		}
5031da177e4SLinus Torvalds 		return true;
5041da177e4SLinus Torvalds 	case S_HEX:
5051da177e4SLinus Torvalds 		if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
5061da177e4SLinus Torvalds 			str += 2;
5071da177e4SLinus Torvalds 		ch = *str++;
5081da177e4SLinus Torvalds 		do {
5091da177e4SLinus Torvalds 			if (!isxdigit(ch))
5101da177e4SLinus Torvalds 				return false;
5111da177e4SLinus Torvalds 		} while ((ch = *str++));
5121da177e4SLinus Torvalds 		return true;
5131da177e4SLinus Torvalds 	case S_BOOLEAN:
5141da177e4SLinus Torvalds 	case S_TRISTATE:
5151da177e4SLinus Torvalds 		switch (str[0]) {
5161da177e4SLinus Torvalds 		case 'y': case 'Y':
5171da177e4SLinus Torvalds 		case 'm': case 'M':
5181da177e4SLinus Torvalds 		case 'n': case 'N':
5191da177e4SLinus Torvalds 			return true;
5201da177e4SLinus Torvalds 		}
5211da177e4SLinus Torvalds 		return false;
5221da177e4SLinus Torvalds 	default:
5231da177e4SLinus Torvalds 		return false;
5241da177e4SLinus Torvalds 	}
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds bool sym_string_within_range(struct symbol *sym, const char *str)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 	struct property *prop;
5301da177e4SLinus Torvalds 	int val;
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	switch (sym->type) {
5331da177e4SLinus Torvalds 	case S_STRING:
5341da177e4SLinus Torvalds 		return sym_string_valid(sym, str);
5351da177e4SLinus Torvalds 	case S_INT:
5361da177e4SLinus Torvalds 		if (!sym_string_valid(sym, str))
5371da177e4SLinus Torvalds 			return false;
5381da177e4SLinus Torvalds 		prop = sym_get_range_prop(sym);
5391da177e4SLinus Torvalds 		if (!prop)
5401da177e4SLinus Torvalds 			return true;
5411da177e4SLinus Torvalds 		val = strtol(str, NULL, 10);
5424cf3cbe2SRoman Zippel 		return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
5434cf3cbe2SRoman Zippel 		       val <= sym_get_range_val(prop->expr->right.sym, 10);
5441da177e4SLinus Torvalds 	case S_HEX:
5451da177e4SLinus Torvalds 		if (!sym_string_valid(sym, str))
5461da177e4SLinus Torvalds 			return false;
5471da177e4SLinus Torvalds 		prop = sym_get_range_prop(sym);
5481da177e4SLinus Torvalds 		if (!prop)
5491da177e4SLinus Torvalds 			return true;
5501da177e4SLinus Torvalds 		val = strtol(str, NULL, 16);
5514cf3cbe2SRoman Zippel 		return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
5524cf3cbe2SRoman Zippel 		       val <= sym_get_range_val(prop->expr->right.sym, 16);
5531da177e4SLinus Torvalds 	case S_BOOLEAN:
5541da177e4SLinus Torvalds 	case S_TRISTATE:
5551da177e4SLinus Torvalds 		switch (str[0]) {
5561da177e4SLinus Torvalds 		case 'y': case 'Y':
5571da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, yes);
5581da177e4SLinus Torvalds 		case 'm': case 'M':
5591da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, mod);
5601da177e4SLinus Torvalds 		case 'n': case 'N':
5611da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, no);
5621da177e4SLinus Torvalds 		}
5631da177e4SLinus Torvalds 		return false;
5641da177e4SLinus Torvalds 	default:
5651da177e4SLinus Torvalds 		return false;
5661da177e4SLinus Torvalds 	}
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds bool sym_set_string_value(struct symbol *sym, const char *newval)
5701da177e4SLinus Torvalds {
5711da177e4SLinus Torvalds 	const char *oldval;
5721da177e4SLinus Torvalds 	char *val;
5731da177e4SLinus Torvalds 	int size;
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	switch (sym->type) {
5761da177e4SLinus Torvalds 	case S_BOOLEAN:
5771da177e4SLinus Torvalds 	case S_TRISTATE:
5781da177e4SLinus Torvalds 		switch (newval[0]) {
5791da177e4SLinus Torvalds 		case 'y': case 'Y':
5801da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, yes);
5811da177e4SLinus Torvalds 		case 'm': case 'M':
5821da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, mod);
5831da177e4SLinus Torvalds 		case 'n': case 'N':
5841da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, no);
5851da177e4SLinus Torvalds 		}
5861da177e4SLinus Torvalds 		return false;
5871da177e4SLinus Torvalds 	default:
5881da177e4SLinus Torvalds 		;
5891da177e4SLinus Torvalds 	}
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	if (!sym_string_within_range(sym, newval))
5921da177e4SLinus Torvalds 		return false;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	if (sym->flags & SYMBOL_NEW) {
5951da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_NEW;
5961da177e4SLinus Torvalds 		sym_set_changed(sym);
5971da177e4SLinus Torvalds 	}
5981da177e4SLinus Torvalds 
599*0c1822e6SRoman Zippel 	oldval = sym->def[S_DEF_USER].val;
6001da177e4SLinus Torvalds 	size = strlen(newval) + 1;
6011da177e4SLinus Torvalds 	if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
6021da177e4SLinus Torvalds 		size += 2;
603*0c1822e6SRoman Zippel 		sym->def[S_DEF_USER].val = val = malloc(size);
6041da177e4SLinus Torvalds 		*val++ = '0';
6051da177e4SLinus Torvalds 		*val++ = 'x';
6061da177e4SLinus Torvalds 	} else if (!oldval || strcmp(oldval, newval))
607*0c1822e6SRoman Zippel 		sym->def[S_DEF_USER].val = val = malloc(size);
6081da177e4SLinus Torvalds 	else
6091da177e4SLinus Torvalds 		return true;
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	strcpy(val, newval);
6121da177e4SLinus Torvalds 	free((void *)oldval);
6131da177e4SLinus Torvalds 	sym_clear_all_valid();
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 	return true;
6161da177e4SLinus Torvalds }
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds const char *sym_get_string_value(struct symbol *sym)
6191da177e4SLinus Torvalds {
6201da177e4SLinus Torvalds 	tristate val;
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	switch (sym->type) {
6231da177e4SLinus Torvalds 	case S_BOOLEAN:
6241da177e4SLinus Torvalds 	case S_TRISTATE:
6251da177e4SLinus Torvalds 		val = sym_get_tristate_value(sym);
6261da177e4SLinus Torvalds 		switch (val) {
6271da177e4SLinus Torvalds 		case no:
6281da177e4SLinus Torvalds 			return "n";
6291da177e4SLinus Torvalds 		case mod:
6301da177e4SLinus Torvalds 			return "m";
6311da177e4SLinus Torvalds 		case yes:
6321da177e4SLinus Torvalds 			return "y";
6331da177e4SLinus Torvalds 		}
6341da177e4SLinus Torvalds 		break;
6351da177e4SLinus Torvalds 	default:
6361da177e4SLinus Torvalds 		;
6371da177e4SLinus Torvalds 	}
6381da177e4SLinus Torvalds 	return (const char *)sym->curr.val;
6391da177e4SLinus Torvalds }
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds bool sym_is_changable(struct symbol *sym)
6421da177e4SLinus Torvalds {
6431da177e4SLinus Torvalds 	return sym->visible > sym->rev_dep.tri;
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds struct symbol *sym_lookup(const char *name, int isconst)
6471da177e4SLinus Torvalds {
6481da177e4SLinus Torvalds 	struct symbol *symbol;
6491da177e4SLinus Torvalds 	const char *ptr;
6501da177e4SLinus Torvalds 	char *new_name;
6511da177e4SLinus Torvalds 	int hash = 0;
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	if (name) {
6541da177e4SLinus Torvalds 		if (name[0] && !name[1]) {
6551da177e4SLinus Torvalds 			switch (name[0]) {
6561da177e4SLinus Torvalds 			case 'y': return &symbol_yes;
6571da177e4SLinus Torvalds 			case 'm': return &symbol_mod;
6581da177e4SLinus Torvalds 			case 'n': return &symbol_no;
6591da177e4SLinus Torvalds 			}
6601da177e4SLinus Torvalds 		}
6611da177e4SLinus Torvalds 		for (ptr = name; *ptr; ptr++)
6621da177e4SLinus Torvalds 			hash += *ptr;
6631da177e4SLinus Torvalds 		hash &= 0xff;
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 		for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
6661da177e4SLinus Torvalds 			if (!strcmp(symbol->name, name)) {
6671da177e4SLinus Torvalds 				if ((isconst && symbol->flags & SYMBOL_CONST) ||
6681da177e4SLinus Torvalds 				    (!isconst && !(symbol->flags & SYMBOL_CONST)))
6691da177e4SLinus Torvalds 					return symbol;
6701da177e4SLinus Torvalds 			}
6711da177e4SLinus Torvalds 		}
6721da177e4SLinus Torvalds 		new_name = strdup(name);
6731da177e4SLinus Torvalds 	} else {
6741da177e4SLinus Torvalds 		new_name = NULL;
6751da177e4SLinus Torvalds 		hash = 256;
6761da177e4SLinus Torvalds 	}
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	symbol = malloc(sizeof(*symbol));
6791da177e4SLinus Torvalds 	memset(symbol, 0, sizeof(*symbol));
6801da177e4SLinus Torvalds 	symbol->name = new_name;
6811da177e4SLinus Torvalds 	symbol->type = S_UNKNOWN;
6821da177e4SLinus Torvalds 	symbol->flags = SYMBOL_NEW;
6831da177e4SLinus Torvalds 	if (isconst)
6841da177e4SLinus Torvalds 		symbol->flags |= SYMBOL_CONST;
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	symbol->next = symbol_hash[hash];
6871da177e4SLinus Torvalds 	symbol_hash[hash] = symbol;
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	return symbol;
6901da177e4SLinus Torvalds }
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds struct symbol *sym_find(const char *name)
6931da177e4SLinus Torvalds {
6941da177e4SLinus Torvalds 	struct symbol *symbol = NULL;
6951da177e4SLinus Torvalds 	const char *ptr;
6961da177e4SLinus Torvalds 	int hash = 0;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	if (!name)
6991da177e4SLinus Torvalds 		return NULL;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	if (name[0] && !name[1]) {
7021da177e4SLinus Torvalds 		switch (name[0]) {
7031da177e4SLinus Torvalds 		case 'y': return &symbol_yes;
7041da177e4SLinus Torvalds 		case 'm': return &symbol_mod;
7051da177e4SLinus Torvalds 		case 'n': return &symbol_no;
7061da177e4SLinus Torvalds 		}
7071da177e4SLinus Torvalds 	}
7081da177e4SLinus Torvalds 	for (ptr = name; *ptr; ptr++)
7091da177e4SLinus Torvalds 		hash += *ptr;
7101da177e4SLinus Torvalds 	hash &= 0xff;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
7131da177e4SLinus Torvalds 		if (!strcmp(symbol->name, name) &&
7141da177e4SLinus Torvalds 		    !(symbol->flags & SYMBOL_CONST))
7151da177e4SLinus Torvalds 				break;
7161da177e4SLinus Torvalds 	}
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	return symbol;
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds struct symbol **sym_re_search(const char *pattern)
7221da177e4SLinus Torvalds {
7231da177e4SLinus Torvalds 	struct symbol *sym, **sym_arr = NULL;
7241da177e4SLinus Torvalds 	int i, cnt, size;
7251da177e4SLinus Torvalds 	regex_t re;
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	cnt = size = 0;
7281da177e4SLinus Torvalds 	/* Skip if empty */
7291da177e4SLinus Torvalds 	if (strlen(pattern) == 0)
7301da177e4SLinus Torvalds 		return NULL;
7311da177e4SLinus Torvalds 	if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
7321da177e4SLinus Torvalds 		return NULL;
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
7351da177e4SLinus Torvalds 		if (sym->flags & SYMBOL_CONST || !sym->name)
7361da177e4SLinus Torvalds 			continue;
7371da177e4SLinus Torvalds 		if (regexec(&re, sym->name, 0, NULL, 0))
7381da177e4SLinus Torvalds 			continue;
7391da177e4SLinus Torvalds 		if (cnt + 1 >= size) {
7401da177e4SLinus Torvalds 			void *tmp = sym_arr;
7411da177e4SLinus Torvalds 			size += 16;
7421da177e4SLinus Torvalds 			sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
7431da177e4SLinus Torvalds 			if (!sym_arr) {
7441da177e4SLinus Torvalds 				free(tmp);
7451da177e4SLinus Torvalds 				return NULL;
7461da177e4SLinus Torvalds 			}
7471da177e4SLinus Torvalds 		}
7481da177e4SLinus Torvalds 		sym_arr[cnt++] = sym;
7491da177e4SLinus Torvalds 	}
7501da177e4SLinus Torvalds 	if (sym_arr)
7511da177e4SLinus Torvalds 		sym_arr[cnt] = NULL;
7521da177e4SLinus Torvalds 	regfree(&re);
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	return sym_arr;
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds struct symbol *sym_check_deps(struct symbol *sym);
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds static struct symbol *sym_check_expr_deps(struct expr *e)
7611da177e4SLinus Torvalds {
7621da177e4SLinus Torvalds 	struct symbol *sym;
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	if (!e)
7651da177e4SLinus Torvalds 		return NULL;
7661da177e4SLinus Torvalds 	switch (e->type) {
7671da177e4SLinus Torvalds 	case E_OR:
7681da177e4SLinus Torvalds 	case E_AND:
7691da177e4SLinus Torvalds 		sym = sym_check_expr_deps(e->left.expr);
7701da177e4SLinus Torvalds 		if (sym)
7711da177e4SLinus Torvalds 			return sym;
7721da177e4SLinus Torvalds 		return sym_check_expr_deps(e->right.expr);
7731da177e4SLinus Torvalds 	case E_NOT:
7741da177e4SLinus Torvalds 		return sym_check_expr_deps(e->left.expr);
7751da177e4SLinus Torvalds 	case E_EQUAL:
7761da177e4SLinus Torvalds 	case E_UNEQUAL:
7771da177e4SLinus Torvalds 		sym = sym_check_deps(e->left.sym);
7781da177e4SLinus Torvalds 		if (sym)
7791da177e4SLinus Torvalds 			return sym;
7801da177e4SLinus Torvalds 		return sym_check_deps(e->right.sym);
7811da177e4SLinus Torvalds 	case E_SYMBOL:
7821da177e4SLinus Torvalds 		return sym_check_deps(e->left.sym);
7831da177e4SLinus Torvalds 	default:
7841da177e4SLinus Torvalds 		break;
7851da177e4SLinus Torvalds 	}
7861da177e4SLinus Torvalds 	printf("Oops! How to check %d?\n", e->type);
7871da177e4SLinus Torvalds 	return NULL;
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds struct symbol *sym_check_deps(struct symbol *sym)
7911da177e4SLinus Torvalds {
7921da177e4SLinus Torvalds 	struct symbol *sym2;
7931da177e4SLinus Torvalds 	struct property *prop;
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	if (sym->flags & SYMBOL_CHECK) {
7961da177e4SLinus Torvalds 		printf("Warning! Found recursive dependency: %s", sym->name);
7971da177e4SLinus Torvalds 		return sym;
7981da177e4SLinus Torvalds 	}
7993f04e7ddSDavid Gibson 	if (sym->flags & SYMBOL_CHECKED)
8003f04e7ddSDavid Gibson 		return NULL;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
8031da177e4SLinus Torvalds 	sym2 = sym_check_expr_deps(sym->rev_dep.expr);
8041da177e4SLinus Torvalds 	if (sym2)
8051da177e4SLinus Torvalds 		goto out;
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 	for (prop = sym->prop; prop; prop = prop->next) {
8081da177e4SLinus Torvalds 		if (prop->type == P_CHOICE || prop->type == P_SELECT)
8091da177e4SLinus Torvalds 			continue;
8101da177e4SLinus Torvalds 		sym2 = sym_check_expr_deps(prop->visible.expr);
8111da177e4SLinus Torvalds 		if (sym2)
8121da177e4SLinus Torvalds 			goto out;
8131da177e4SLinus Torvalds 		if (prop->type != P_DEFAULT || sym_is_choice(sym))
8141da177e4SLinus Torvalds 			continue;
8151da177e4SLinus Torvalds 		sym2 = sym_check_expr_deps(prop->expr);
8161da177e4SLinus Torvalds 		if (sym2)
8171da177e4SLinus Torvalds 			goto out;
8181da177e4SLinus Torvalds 	}
8191da177e4SLinus Torvalds out:
8203f04e7ddSDavid Gibson 	if (sym2) {
8211da177e4SLinus Torvalds 		printf(" %s", sym->name);
8223f04e7ddSDavid Gibson 		if (sym2 == sym) {
8233f04e7ddSDavid Gibson 			printf("\n");
8243f04e7ddSDavid Gibson 			sym2 = NULL;
8253f04e7ddSDavid Gibson 		}
8263f04e7ddSDavid Gibson 	}
8271da177e4SLinus Torvalds 	sym->flags &= ~SYMBOL_CHECK;
8281da177e4SLinus Torvalds 	return sym2;
8291da177e4SLinus Torvalds }
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds struct property *prop_alloc(enum prop_type type, struct symbol *sym)
8321da177e4SLinus Torvalds {
8331da177e4SLinus Torvalds 	struct property *prop;
8341da177e4SLinus Torvalds 	struct property **propp;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	prop = malloc(sizeof(*prop));
8371da177e4SLinus Torvalds 	memset(prop, 0, sizeof(*prop));
8381da177e4SLinus Torvalds 	prop->type = type;
8391da177e4SLinus Torvalds 	prop->sym = sym;
8401da177e4SLinus Torvalds 	prop->file = current_file;
8411da177e4SLinus Torvalds 	prop->lineno = zconf_lineno();
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	/* append property to the prop list of symbol */
8441da177e4SLinus Torvalds 	if (sym) {
8451da177e4SLinus Torvalds 		for (propp = &sym->prop; *propp; propp = &(*propp)->next)
8461da177e4SLinus Torvalds 			;
8471da177e4SLinus Torvalds 		*propp = prop;
8481da177e4SLinus Torvalds 	}
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	return prop;
8511da177e4SLinus Torvalds }
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds struct symbol *prop_get_symbol(struct property *prop)
8541da177e4SLinus Torvalds {
8551da177e4SLinus Torvalds 	if (prop->expr && (prop->expr->type == E_SYMBOL ||
8561da177e4SLinus Torvalds 			   prop->expr->type == E_CHOICE))
8571da177e4SLinus Torvalds 		return prop->expr->left.sym;
8581da177e4SLinus Torvalds 	return NULL;
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds const char *prop_get_type_name(enum prop_type type)
8621da177e4SLinus Torvalds {
8631da177e4SLinus Torvalds 	switch (type) {
8641da177e4SLinus Torvalds 	case P_PROMPT:
8651da177e4SLinus Torvalds 		return "prompt";
8661da177e4SLinus Torvalds 	case P_COMMENT:
8671da177e4SLinus Torvalds 		return "comment";
8681da177e4SLinus Torvalds 	case P_MENU:
8691da177e4SLinus Torvalds 		return "menu";
8701da177e4SLinus Torvalds 	case P_DEFAULT:
8711da177e4SLinus Torvalds 		return "default";
8721da177e4SLinus Torvalds 	case P_CHOICE:
8731da177e4SLinus Torvalds 		return "choice";
8741da177e4SLinus Torvalds 	case P_SELECT:
8751da177e4SLinus Torvalds 		return "select";
8761da177e4SLinus Torvalds 	case P_RANGE:
8771da177e4SLinus Torvalds 		return "range";
8781da177e4SLinus Torvalds 	case P_UNKNOWN:
8791da177e4SLinus Torvalds 		break;
8801da177e4SLinus Torvalds 	}
8811da177e4SLinus Torvalds 	return "unknown";
8821da177e4SLinus Torvalds }
883