xref: /openbmc/linux/scripts/kconfig/symbol.c (revision 76a540958af5390a94b7f68c46cb7f2aed34ccf1)
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 
33face4374SRoman Zippel struct symbol *sym_defconfig_list;
341da177e4SLinus Torvalds struct symbol *modules_sym;
351da177e4SLinus Torvalds tristate modules_val;
361da177e4SLinus Torvalds 
3793449082SRoman Zippel struct expr *sym_env_list;
3893449082SRoman Zippel 
394356f489STrevor Keith static void sym_add_default(struct symbol *sym, const char *def)
401da177e4SLinus Torvalds {
411da177e4SLinus Torvalds 	struct property *prop = prop_alloc(P_DEFAULT, sym);
421da177e4SLinus Torvalds 
435a1aa8a1SRoman Zippel 	prop->expr = expr_alloc_symbol(sym_lookup(def, SYMBOL_CONST));
441da177e4SLinus Torvalds }
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds void sym_init(void)
471da177e4SLinus Torvalds {
481da177e4SLinus Torvalds 	struct symbol *sym;
491da177e4SLinus Torvalds 	struct utsname uts;
501da177e4SLinus Torvalds 	static bool inited = false;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 	if (inited)
531da177e4SLinus Torvalds 		return;
541da177e4SLinus Torvalds 	inited = true;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	uname(&uts);
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 	sym = sym_lookup("UNAME_RELEASE", 0);
591da177e4SLinus Torvalds 	sym->type = S_STRING;
601da177e4SLinus Torvalds 	sym->flags |= SYMBOL_AUTO;
611da177e4SLinus Torvalds 	sym_add_default(sym, uts.release);
621da177e4SLinus Torvalds }
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds enum symbol_type sym_get_type(struct symbol *sym)
651da177e4SLinus Torvalds {
661da177e4SLinus Torvalds 	enum symbol_type type = sym->type;
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds 	if (type == S_TRISTATE) {
691da177e4SLinus Torvalds 		if (sym_is_choice_value(sym) && sym->visible == yes)
701da177e4SLinus Torvalds 			type = S_BOOLEAN;
711da177e4SLinus Torvalds 		else if (modules_val == no)
721da177e4SLinus Torvalds 			type = S_BOOLEAN;
731da177e4SLinus Torvalds 	}
741da177e4SLinus Torvalds 	return type;
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds const char *sym_type_name(enum symbol_type type)
781da177e4SLinus Torvalds {
791da177e4SLinus Torvalds 	switch (type) {
801da177e4SLinus Torvalds 	case S_BOOLEAN:
811da177e4SLinus Torvalds 		return "boolean";
821da177e4SLinus Torvalds 	case S_TRISTATE:
831da177e4SLinus Torvalds 		return "tristate";
841da177e4SLinus Torvalds 	case S_INT:
851da177e4SLinus Torvalds 		return "integer";
861da177e4SLinus Torvalds 	case S_HEX:
871da177e4SLinus Torvalds 		return "hex";
881da177e4SLinus Torvalds 	case S_STRING:
891da177e4SLinus Torvalds 		return "string";
901da177e4SLinus Torvalds 	case S_UNKNOWN:
911da177e4SLinus Torvalds 		return "unknown";
921da177e4SLinus Torvalds 	case S_OTHER:
931da177e4SLinus Torvalds 		break;
941da177e4SLinus Torvalds 	}
951da177e4SLinus Torvalds 	return "???";
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds struct property *sym_get_choice_prop(struct symbol *sym)
991da177e4SLinus Torvalds {
1001da177e4SLinus Torvalds 	struct property *prop;
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 	for_all_choices(sym, prop)
1031da177e4SLinus Torvalds 		return prop;
1041da177e4SLinus Torvalds 	return NULL;
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
10793449082SRoman Zippel struct property *sym_get_env_prop(struct symbol *sym)
10893449082SRoman Zippel {
10993449082SRoman Zippel 	struct property *prop;
11093449082SRoman Zippel 
11193449082SRoman Zippel 	for_all_properties(sym, prop, P_ENV)
11293449082SRoman Zippel 		return prop;
11393449082SRoman Zippel 	return NULL;
11493449082SRoman Zippel }
11593449082SRoman Zippel 
1161da177e4SLinus Torvalds struct property *sym_get_default_prop(struct symbol *sym)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	struct property *prop;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	for_all_defaults(sym, prop) {
1211da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
1221da177e4SLinus Torvalds 		if (prop->visible.tri != no)
1231da177e4SLinus Torvalds 			return prop;
1241da177e4SLinus Torvalds 	}
1251da177e4SLinus Torvalds 	return NULL;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
1284356f489STrevor Keith static struct property *sym_get_range_prop(struct symbol *sym)
1291da177e4SLinus Torvalds {
1301da177e4SLinus Torvalds 	struct property *prop;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	for_all_properties(sym, prop, P_RANGE) {
1331da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
1341da177e4SLinus Torvalds 		if (prop->visible.tri != no)
1351da177e4SLinus Torvalds 			return prop;
1361da177e4SLinus Torvalds 	}
1371da177e4SLinus Torvalds 	return NULL;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1404cf3cbe2SRoman Zippel static int sym_get_range_val(struct symbol *sym, int base)
1414cf3cbe2SRoman Zippel {
1424cf3cbe2SRoman Zippel 	sym_calc_value(sym);
1434cf3cbe2SRoman Zippel 	switch (sym->type) {
1444cf3cbe2SRoman Zippel 	case S_INT:
1454cf3cbe2SRoman Zippel 		base = 10;
1464cf3cbe2SRoman Zippel 		break;
1474cf3cbe2SRoman Zippel 	case S_HEX:
1484cf3cbe2SRoman Zippel 		base = 16;
1494cf3cbe2SRoman Zippel 		break;
1504cf3cbe2SRoman Zippel 	default:
1514cf3cbe2SRoman Zippel 		break;
1524cf3cbe2SRoman Zippel 	}
1534cf3cbe2SRoman Zippel 	return strtol(sym->curr.val, NULL, base);
1544cf3cbe2SRoman Zippel }
1554cf3cbe2SRoman Zippel 
1564cf3cbe2SRoman Zippel static void sym_validate_range(struct symbol *sym)
1574cf3cbe2SRoman Zippel {
1584cf3cbe2SRoman Zippel 	struct property *prop;
1594cf3cbe2SRoman Zippel 	int base, val, val2;
1604cf3cbe2SRoman Zippel 	char str[64];
1614cf3cbe2SRoman Zippel 
1624cf3cbe2SRoman Zippel 	switch (sym->type) {
1634cf3cbe2SRoman Zippel 	case S_INT:
1644cf3cbe2SRoman Zippel 		base = 10;
1654cf3cbe2SRoman Zippel 		break;
1664cf3cbe2SRoman Zippel 	case S_HEX:
1674cf3cbe2SRoman Zippel 		base = 16;
1684cf3cbe2SRoman Zippel 		break;
1694cf3cbe2SRoman Zippel 	default:
1704cf3cbe2SRoman Zippel 		return;
1714cf3cbe2SRoman Zippel 	}
1724cf3cbe2SRoman Zippel 	prop = sym_get_range_prop(sym);
1734cf3cbe2SRoman Zippel 	if (!prop)
1744cf3cbe2SRoman Zippel 		return;
1754cf3cbe2SRoman Zippel 	val = strtol(sym->curr.val, NULL, base);
1764cf3cbe2SRoman Zippel 	val2 = sym_get_range_val(prop->expr->left.sym, base);
1774cf3cbe2SRoman Zippel 	if (val >= val2) {
1784cf3cbe2SRoman Zippel 		val2 = sym_get_range_val(prop->expr->right.sym, base);
1794cf3cbe2SRoman Zippel 		if (val <= val2)
1804cf3cbe2SRoman Zippel 			return;
1814cf3cbe2SRoman Zippel 	}
1824cf3cbe2SRoman Zippel 	if (sym->type == S_INT)
1834cf3cbe2SRoman Zippel 		sprintf(str, "%d", val2);
1844cf3cbe2SRoman Zippel 	else
1854cf3cbe2SRoman Zippel 		sprintf(str, "0x%x", val2);
1864cf3cbe2SRoman Zippel 	sym->curr.val = strdup(str);
1874cf3cbe2SRoman Zippel }
1884cf3cbe2SRoman Zippel 
1891da177e4SLinus Torvalds static void sym_calc_visibility(struct symbol *sym)
1901da177e4SLinus Torvalds {
1911da177e4SLinus Torvalds 	struct property *prop;
1921da177e4SLinus Torvalds 	tristate tri;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	/* any prompt visible? */
1951da177e4SLinus Torvalds 	tri = no;
1961da177e4SLinus Torvalds 	for_all_prompts(sym, prop) {
1971da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
198d6ee3576SSam Ravnborg 		tri = EXPR_OR(tri, prop->visible.tri);
1991da177e4SLinus Torvalds 	}
2001da177e4SLinus Torvalds 	if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
2011da177e4SLinus Torvalds 		tri = yes;
2021da177e4SLinus Torvalds 	if (sym->visible != tri) {
2031da177e4SLinus Torvalds 		sym->visible = tri;
2041da177e4SLinus Torvalds 		sym_set_changed(sym);
2051da177e4SLinus Torvalds 	}
2061da177e4SLinus Torvalds 	if (sym_is_choice_value(sym))
2071da177e4SLinus Torvalds 		return;
208246cf9c2SCatalin Marinas 	/* defaulting to "yes" if no explicit "depends on" are given */
209246cf9c2SCatalin Marinas 	tri = yes;
210246cf9c2SCatalin Marinas 	if (sym->dir_dep.expr)
211246cf9c2SCatalin Marinas 		tri = expr_calc_value(sym->dir_dep.expr);
212246cf9c2SCatalin Marinas 	if (tri == mod)
213246cf9c2SCatalin Marinas 		tri = yes;
214246cf9c2SCatalin Marinas 	if (sym->dir_dep.tri != tri) {
215246cf9c2SCatalin Marinas 		sym->dir_dep.tri = tri;
216246cf9c2SCatalin Marinas 		sym_set_changed(sym);
217246cf9c2SCatalin Marinas 	}
2181da177e4SLinus Torvalds 	tri = no;
2191da177e4SLinus Torvalds 	if (sym->rev_dep.expr)
2201da177e4SLinus Torvalds 		tri = expr_calc_value(sym->rev_dep.expr);
2211da177e4SLinus Torvalds 	if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
2221da177e4SLinus Torvalds 		tri = yes;
2231da177e4SLinus Torvalds 	if (sym->rev_dep.tri != tri) {
2241da177e4SLinus Torvalds 		sym->rev_dep.tri = tri;
2251da177e4SLinus Torvalds 		sym_set_changed(sym);
2261da177e4SLinus Torvalds 	}
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
229c252147dSSam Ravnborg /*
230c252147dSSam Ravnborg  * Find the default symbol for a choice.
231c252147dSSam Ravnborg  * First try the default values for the choice symbol
232c252147dSSam Ravnborg  * Next locate the first visible choice value
233c252147dSSam Ravnborg  * Return NULL if none was found
234c252147dSSam Ravnborg  */
235c252147dSSam Ravnborg struct symbol *sym_choice_default(struct symbol *sym)
2361da177e4SLinus Torvalds {
2371da177e4SLinus Torvalds 	struct symbol *def_sym;
2381da177e4SLinus Torvalds 	struct property *prop;
2391da177e4SLinus Torvalds 	struct expr *e;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	/* any of the defaults visible? */
2421da177e4SLinus Torvalds 	for_all_defaults(sym, prop) {
2431da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
2441da177e4SLinus Torvalds 		if (prop->visible.tri == no)
2451da177e4SLinus Torvalds 			continue;
2461da177e4SLinus Torvalds 		def_sym = prop_get_symbol(prop);
2471da177e4SLinus Torvalds 		if (def_sym->visible != no)
2481da177e4SLinus Torvalds 			return def_sym;
2491da177e4SLinus Torvalds 	}
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	/* just get the first visible value */
2521da177e4SLinus Torvalds 	prop = sym_get_choice_prop(sym);
2530a28c47bSJan Beulich 	expr_list_for_each_sym(prop->expr, e, def_sym)
2541da177e4SLinus Torvalds 		if (def_sym->visible != no)
2551da177e4SLinus Torvalds 			return def_sym;
2561da177e4SLinus Torvalds 
257c252147dSSam Ravnborg 	/* failed to locate any defaults */
258c252147dSSam Ravnborg 	return NULL;
259c252147dSSam Ravnborg }
260c252147dSSam Ravnborg 
261c252147dSSam Ravnborg static struct symbol *sym_calc_choice(struct symbol *sym)
262c252147dSSam Ravnborg {
263c252147dSSam Ravnborg 	struct symbol *def_sym;
264c252147dSSam Ravnborg 	struct property *prop;
265c252147dSSam Ravnborg 	struct expr *e;
266c252147dSSam Ravnborg 
267c252147dSSam Ravnborg 	/* first calculate all choice values' visibilities */
268c252147dSSam Ravnborg 	prop = sym_get_choice_prop(sym);
269c252147dSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, def_sym)
270c252147dSSam Ravnborg 		sym_calc_visibility(def_sym);
271c252147dSSam Ravnborg 
272c252147dSSam Ravnborg 	/* is the user choice visible? */
273c252147dSSam Ravnborg 	def_sym = sym->def[S_DEF_USER].val;
274c252147dSSam Ravnborg 	if (def_sym && def_sym->visible != no)
275c252147dSSam Ravnborg 		return def_sym;
276c252147dSSam Ravnborg 
277c252147dSSam Ravnborg 	def_sym = sym_choice_default(sym);
278c252147dSSam Ravnborg 
279c252147dSSam Ravnborg 	if (def_sym == NULL)
2801da177e4SLinus Torvalds 		/* no choice? reset tristate value */
2811da177e4SLinus Torvalds 		sym->curr.tri = no;
282c252147dSSam Ravnborg 
283c252147dSSam Ravnborg 	return def_sym;
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds void sym_calc_value(struct symbol *sym)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	struct symbol_value newval, oldval;
2891da177e4SLinus Torvalds 	struct property *prop;
2901da177e4SLinus Torvalds 	struct expr *e;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	if (!sym)
2931da177e4SLinus Torvalds 		return;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	if (sym->flags & SYMBOL_VALID)
2961da177e4SLinus Torvalds 		return;
2971da177e4SLinus Torvalds 	sym->flags |= SYMBOL_VALID;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	oldval = sym->curr;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	switch (sym->type) {
3021da177e4SLinus Torvalds 	case S_INT:
3031da177e4SLinus Torvalds 	case S_HEX:
3041da177e4SLinus Torvalds 	case S_STRING:
3051da177e4SLinus Torvalds 		newval = symbol_empty.curr;
3061da177e4SLinus Torvalds 		break;
3071da177e4SLinus Torvalds 	case S_BOOLEAN:
3081da177e4SLinus Torvalds 	case S_TRISTATE:
3091da177e4SLinus Torvalds 		newval = symbol_no.curr;
3101da177e4SLinus Torvalds 		break;
3111da177e4SLinus Torvalds 	default:
3121da177e4SLinus Torvalds 		sym->curr.val = sym->name;
3131da177e4SLinus Torvalds 		sym->curr.tri = no;
3141da177e4SLinus Torvalds 		return;
3151da177e4SLinus Torvalds 	}
3161da177e4SLinus Torvalds 	if (!sym_is_choice_value(sym))
3171da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_WRITE;
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	sym_calc_visibility(sym);
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	/* set default if recursively called */
3221da177e4SLinus Torvalds 	sym->curr = newval;
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	switch (sym_get_type(sym)) {
3251da177e4SLinus Torvalds 	case S_BOOLEAN:
3261da177e4SLinus Torvalds 	case S_TRISTATE:
3271da177e4SLinus Torvalds 		if (sym_is_choice_value(sym) && sym->visible == yes) {
3281da177e4SLinus Torvalds 			prop = sym_get_choice_prop(sym);
3291da177e4SLinus Torvalds 			newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
330587c9061SRoman Zippel 		} else {
331587c9061SRoman Zippel 			if (sym->visible != no) {
332587c9061SRoman Zippel 				/* if the symbol is visible use the user value
333587c9061SRoman Zippel 				 * if available, otherwise try the default value
334587c9061SRoman Zippel 				 */
3351da177e4SLinus Torvalds 				sym->flags |= SYMBOL_WRITE;
336587c9061SRoman Zippel 				if (sym_has_value(sym)) {
337587c9061SRoman Zippel 					newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
338587c9061SRoman Zippel 							      sym->visible);
339587c9061SRoman Zippel 					goto calc_newval;
3401da177e4SLinus Torvalds 				}
341587c9061SRoman Zippel 			}
342587c9061SRoman Zippel 			if (sym->rev_dep.tri != no)
343587c9061SRoman Zippel 				sym->flags |= SYMBOL_WRITE;
344587c9061SRoman Zippel 			if (!sym_is_choice(sym)) {
3451da177e4SLinus Torvalds 				prop = sym_get_default_prop(sym);
3461da177e4SLinus Torvalds 				if (prop) {
3471da177e4SLinus Torvalds 					sym->flags |= SYMBOL_WRITE;
348587c9061SRoman Zippel 					newval.tri = EXPR_AND(expr_calc_value(prop->expr),
349587c9061SRoman Zippel 							      prop->visible.tri);
3501da177e4SLinus Torvalds 				}
3511da177e4SLinus Torvalds 			}
352587c9061SRoman Zippel 		calc_newval:
353246cf9c2SCatalin Marinas 			if (sym->dir_dep.tri == no && sym->rev_dep.tri != no) {
354246cf9c2SCatalin Marinas 				fprintf(stderr, "warning: (");
355246cf9c2SCatalin Marinas 				expr_fprint(sym->rev_dep.expr, stderr);
356246cf9c2SCatalin Marinas 				fprintf(stderr, ") selects %s which has unmet direct dependencies (",
357246cf9c2SCatalin Marinas 					sym->name);
358246cf9c2SCatalin Marinas 				expr_fprint(sym->dir_dep.expr, stderr);
359246cf9c2SCatalin Marinas 				fprintf(stderr, ")\n");
360246cf9c2SCatalin Marinas 			}
361587c9061SRoman Zippel 			newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
362587c9061SRoman Zippel 		}
3631da177e4SLinus Torvalds 		if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
3641da177e4SLinus Torvalds 			newval.tri = yes;
3651da177e4SLinus Torvalds 		break;
3661da177e4SLinus Torvalds 	case S_STRING:
3671da177e4SLinus Torvalds 	case S_HEX:
3681da177e4SLinus Torvalds 	case S_INT:
3691da177e4SLinus Torvalds 		if (sym->visible != no) {
3701da177e4SLinus Torvalds 			sym->flags |= SYMBOL_WRITE;
3711da177e4SLinus Torvalds 			if (sym_has_value(sym)) {
3720c1822e6SRoman Zippel 				newval.val = sym->def[S_DEF_USER].val;
3731da177e4SLinus Torvalds 				break;
3741da177e4SLinus Torvalds 			}
3751da177e4SLinus Torvalds 		}
3761da177e4SLinus Torvalds 		prop = sym_get_default_prop(sym);
3771da177e4SLinus Torvalds 		if (prop) {
3781da177e4SLinus Torvalds 			struct symbol *ds = prop_get_symbol(prop);
3791da177e4SLinus Torvalds 			if (ds) {
3801da177e4SLinus Torvalds 				sym->flags |= SYMBOL_WRITE;
3811da177e4SLinus Torvalds 				sym_calc_value(ds);
3821da177e4SLinus Torvalds 				newval.val = ds->curr.val;
3831da177e4SLinus Torvalds 			}
3841da177e4SLinus Torvalds 		}
3851da177e4SLinus Torvalds 		break;
3861da177e4SLinus Torvalds 	default:
3871da177e4SLinus Torvalds 		;
3881da177e4SLinus Torvalds 	}
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	sym->curr = newval;
3911da177e4SLinus Torvalds 	if (sym_is_choice(sym) && newval.tri == yes)
3921da177e4SLinus Torvalds 		sym->curr.val = sym_calc_choice(sym);
3934cf3cbe2SRoman Zippel 	sym_validate_range(sym);
3941da177e4SLinus Torvalds 
395face4374SRoman Zippel 	if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
3961da177e4SLinus Torvalds 		sym_set_changed(sym);
397face4374SRoman Zippel 		if (modules_sym == sym) {
398face4374SRoman Zippel 			sym_set_all_changed();
3991da177e4SLinus Torvalds 			modules_val = modules_sym->curr.tri;
400face4374SRoman Zippel 		}
401face4374SRoman Zippel 	}
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	if (sym_is_choice(sym)) {
4047a962923SRoman Zippel 		struct symbol *choice_sym;
4057a962923SRoman Zippel 
4061da177e4SLinus Torvalds 		prop = sym_get_choice_prop(sym);
4077a962923SRoman Zippel 		expr_list_for_each_sym(prop->expr, e, choice_sym) {
4080a28c47bSJan Beulich 			if ((sym->flags & SYMBOL_WRITE) &&
4090a28c47bSJan Beulich 			    choice_sym->visible != no)
4100a28c47bSJan Beulich 				choice_sym->flags |= SYMBOL_WRITE;
4110a28c47bSJan Beulich 			if (sym->flags & SYMBOL_CHANGED)
4127a962923SRoman Zippel 				sym_set_changed(choice_sym);
4131da177e4SLinus Torvalds 		}
4141da177e4SLinus Torvalds 	}
4155a1aa8a1SRoman Zippel 
4165a1aa8a1SRoman Zippel 	if (sym->flags & SYMBOL_AUTO)
4175a1aa8a1SRoman Zippel 		sym->flags &= ~SYMBOL_WRITE;
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds void sym_clear_all_valid(void)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	struct symbol *sym;
4231da177e4SLinus Torvalds 	int i;
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	for_all_symbols(i, sym)
4261da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_VALID;
427bfc10001SKarsten Wiese 	sym_add_change_count(1);
4281da177e4SLinus Torvalds 	if (modules_sym)
4291da177e4SLinus Torvalds 		sym_calc_value(modules_sym);
4301da177e4SLinus Torvalds }
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds void sym_set_changed(struct symbol *sym)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds 	struct property *prop;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	sym->flags |= SYMBOL_CHANGED;
4371da177e4SLinus Torvalds 	for (prop = sym->prop; prop; prop = prop->next) {
4381da177e4SLinus Torvalds 		if (prop->menu)
4391da177e4SLinus Torvalds 			prop->menu->flags |= MENU_CHANGED;
4401da177e4SLinus Torvalds 	}
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds void sym_set_all_changed(void)
4441da177e4SLinus Torvalds {
4451da177e4SLinus Torvalds 	struct symbol *sym;
4461da177e4SLinus Torvalds 	int i;
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	for_all_symbols(i, sym)
4491da177e4SLinus Torvalds 		sym_set_changed(sym);
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds bool sym_tristate_within_range(struct symbol *sym, tristate val)
4531da177e4SLinus Torvalds {
4541da177e4SLinus Torvalds 	int type = sym_get_type(sym);
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	if (sym->visible == no)
4571da177e4SLinus Torvalds 		return false;
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	if (type != S_BOOLEAN && type != S_TRISTATE)
4601da177e4SLinus Torvalds 		return false;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	if (type == S_BOOLEAN && val == mod)
4631da177e4SLinus Torvalds 		return false;
4641da177e4SLinus Torvalds 	if (sym->visible <= sym->rev_dep.tri)
4651da177e4SLinus Torvalds 		return false;
4661da177e4SLinus Torvalds 	if (sym_is_choice_value(sym) && sym->visible == yes)
4671da177e4SLinus Torvalds 		return val == yes;
4681da177e4SLinus Torvalds 	return val >= sym->rev_dep.tri && val <= sym->visible;
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds bool sym_set_tristate_value(struct symbol *sym, tristate val)
4721da177e4SLinus Torvalds {
4731da177e4SLinus Torvalds 	tristate oldval = sym_get_tristate_value(sym);
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	if (oldval != val && !sym_tristate_within_range(sym, val))
4761da177e4SLinus Torvalds 		return false;
4771da177e4SLinus Torvalds 
478669bfad9SRoman Zippel 	if (!(sym->flags & SYMBOL_DEF_USER)) {
479669bfad9SRoman Zippel 		sym->flags |= SYMBOL_DEF_USER;
4801da177e4SLinus Torvalds 		sym_set_changed(sym);
4811da177e4SLinus Torvalds 	}
4823f23ca2bSRoman Zippel 	/*
4833f23ca2bSRoman Zippel 	 * setting a choice value also resets the new flag of the choice
4843f23ca2bSRoman Zippel 	 * symbol and all other choice values.
4853f23ca2bSRoman Zippel 	 */
4861da177e4SLinus Torvalds 	if (sym_is_choice_value(sym) && val == yes) {
4871da177e4SLinus Torvalds 		struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
4883f23ca2bSRoman Zippel 		struct property *prop;
4893f23ca2bSRoman Zippel 		struct expr *e;
4901da177e4SLinus Torvalds 
4910c1822e6SRoman Zippel 		cs->def[S_DEF_USER].val = sym;
492669bfad9SRoman Zippel 		cs->flags |= SYMBOL_DEF_USER;
4933f23ca2bSRoman Zippel 		prop = sym_get_choice_prop(cs);
4943f23ca2bSRoman Zippel 		for (e = prop->expr; e; e = e->left.expr) {
4953f23ca2bSRoman Zippel 			if (e->right.sym->visible != no)
496669bfad9SRoman Zippel 				e->right.sym->flags |= SYMBOL_DEF_USER;
4973f23ca2bSRoman Zippel 		}
4981da177e4SLinus Torvalds 	}
4991da177e4SLinus Torvalds 
5000c1822e6SRoman Zippel 	sym->def[S_DEF_USER].tri = val;
501face4374SRoman Zippel 	if (oldval != val)
5021da177e4SLinus Torvalds 		sym_clear_all_valid();
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	return true;
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds tristate sym_toggle_tristate_value(struct symbol *sym)
5081da177e4SLinus Torvalds {
5091da177e4SLinus Torvalds 	tristate oldval, newval;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	oldval = newval = sym_get_tristate_value(sym);
5121da177e4SLinus Torvalds 	do {
5131da177e4SLinus Torvalds 		switch (newval) {
5141da177e4SLinus Torvalds 		case no:
5151da177e4SLinus Torvalds 			newval = mod;
5161da177e4SLinus Torvalds 			break;
5171da177e4SLinus Torvalds 		case mod:
5181da177e4SLinus Torvalds 			newval = yes;
5191da177e4SLinus Torvalds 			break;
5201da177e4SLinus Torvalds 		case yes:
5211da177e4SLinus Torvalds 			newval = no;
5221da177e4SLinus Torvalds 			break;
5231da177e4SLinus Torvalds 		}
5241da177e4SLinus Torvalds 		if (sym_set_tristate_value(sym, newval))
5251da177e4SLinus Torvalds 			break;
5261da177e4SLinus Torvalds 	} while (oldval != newval);
5271da177e4SLinus Torvalds 	return newval;
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds bool sym_string_valid(struct symbol *sym, const char *str)
5311da177e4SLinus Torvalds {
5321da177e4SLinus Torvalds 	signed char ch;
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	switch (sym->type) {
5351da177e4SLinus Torvalds 	case S_STRING:
5361da177e4SLinus Torvalds 		return true;
5371da177e4SLinus Torvalds 	case S_INT:
5381da177e4SLinus Torvalds 		ch = *str++;
5391da177e4SLinus Torvalds 		if (ch == '-')
5401da177e4SLinus Torvalds 			ch = *str++;
5411da177e4SLinus Torvalds 		if (!isdigit(ch))
5421da177e4SLinus Torvalds 			return false;
5431da177e4SLinus Torvalds 		if (ch == '0' && *str != 0)
5441da177e4SLinus Torvalds 			return false;
5451da177e4SLinus Torvalds 		while ((ch = *str++)) {
5461da177e4SLinus Torvalds 			if (!isdigit(ch))
5471da177e4SLinus Torvalds 				return false;
5481da177e4SLinus Torvalds 		}
5491da177e4SLinus Torvalds 		return true;
5501da177e4SLinus Torvalds 	case S_HEX:
5511da177e4SLinus Torvalds 		if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
5521da177e4SLinus Torvalds 			str += 2;
5531da177e4SLinus Torvalds 		ch = *str++;
5541da177e4SLinus Torvalds 		do {
5551da177e4SLinus Torvalds 			if (!isxdigit(ch))
5561da177e4SLinus Torvalds 				return false;
5571da177e4SLinus Torvalds 		} while ((ch = *str++));
5581da177e4SLinus Torvalds 		return true;
5591da177e4SLinus Torvalds 	case S_BOOLEAN:
5601da177e4SLinus Torvalds 	case S_TRISTATE:
5611da177e4SLinus Torvalds 		switch (str[0]) {
5621da177e4SLinus Torvalds 		case 'y': case 'Y':
5631da177e4SLinus Torvalds 		case 'm': case 'M':
5641da177e4SLinus Torvalds 		case 'n': case 'N':
5651da177e4SLinus Torvalds 			return true;
5661da177e4SLinus Torvalds 		}
5671da177e4SLinus Torvalds 		return false;
5681da177e4SLinus Torvalds 	default:
5691da177e4SLinus Torvalds 		return false;
5701da177e4SLinus Torvalds 	}
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds bool sym_string_within_range(struct symbol *sym, const char *str)
5741da177e4SLinus Torvalds {
5751da177e4SLinus Torvalds 	struct property *prop;
5761da177e4SLinus Torvalds 	int val;
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	switch (sym->type) {
5791da177e4SLinus Torvalds 	case S_STRING:
5801da177e4SLinus Torvalds 		return sym_string_valid(sym, str);
5811da177e4SLinus Torvalds 	case S_INT:
5821da177e4SLinus Torvalds 		if (!sym_string_valid(sym, str))
5831da177e4SLinus Torvalds 			return false;
5841da177e4SLinus Torvalds 		prop = sym_get_range_prop(sym);
5851da177e4SLinus Torvalds 		if (!prop)
5861da177e4SLinus Torvalds 			return true;
5871da177e4SLinus Torvalds 		val = strtol(str, NULL, 10);
5884cf3cbe2SRoman Zippel 		return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
5894cf3cbe2SRoman Zippel 		       val <= sym_get_range_val(prop->expr->right.sym, 10);
5901da177e4SLinus Torvalds 	case S_HEX:
5911da177e4SLinus Torvalds 		if (!sym_string_valid(sym, str))
5921da177e4SLinus Torvalds 			return false;
5931da177e4SLinus Torvalds 		prop = sym_get_range_prop(sym);
5941da177e4SLinus Torvalds 		if (!prop)
5951da177e4SLinus Torvalds 			return true;
5961da177e4SLinus Torvalds 		val = strtol(str, NULL, 16);
5974cf3cbe2SRoman Zippel 		return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
5984cf3cbe2SRoman Zippel 		       val <= sym_get_range_val(prop->expr->right.sym, 16);
5991da177e4SLinus Torvalds 	case S_BOOLEAN:
6001da177e4SLinus Torvalds 	case S_TRISTATE:
6011da177e4SLinus Torvalds 		switch (str[0]) {
6021da177e4SLinus Torvalds 		case 'y': case 'Y':
6031da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, yes);
6041da177e4SLinus Torvalds 		case 'm': case 'M':
6051da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, mod);
6061da177e4SLinus Torvalds 		case 'n': case 'N':
6071da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, no);
6081da177e4SLinus Torvalds 		}
6091da177e4SLinus Torvalds 		return false;
6101da177e4SLinus Torvalds 	default:
6111da177e4SLinus Torvalds 		return false;
6121da177e4SLinus Torvalds 	}
6131da177e4SLinus Torvalds }
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds bool sym_set_string_value(struct symbol *sym, const char *newval)
6161da177e4SLinus Torvalds {
6171da177e4SLinus Torvalds 	const char *oldval;
6181da177e4SLinus Torvalds 	char *val;
6191da177e4SLinus Torvalds 	int size;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	switch (sym->type) {
6221da177e4SLinus Torvalds 	case S_BOOLEAN:
6231da177e4SLinus Torvalds 	case S_TRISTATE:
6241da177e4SLinus Torvalds 		switch (newval[0]) {
6251da177e4SLinus Torvalds 		case 'y': case 'Y':
6261da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, yes);
6271da177e4SLinus Torvalds 		case 'm': case 'M':
6281da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, mod);
6291da177e4SLinus Torvalds 		case 'n': case 'N':
6301da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, no);
6311da177e4SLinus Torvalds 		}
6321da177e4SLinus Torvalds 		return false;
6331da177e4SLinus Torvalds 	default:
6341da177e4SLinus Torvalds 		;
6351da177e4SLinus Torvalds 	}
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	if (!sym_string_within_range(sym, newval))
6381da177e4SLinus Torvalds 		return false;
6391da177e4SLinus Torvalds 
640669bfad9SRoman Zippel 	if (!(sym->flags & SYMBOL_DEF_USER)) {
641669bfad9SRoman Zippel 		sym->flags |= SYMBOL_DEF_USER;
6421da177e4SLinus Torvalds 		sym_set_changed(sym);
6431da177e4SLinus Torvalds 	}
6441da177e4SLinus Torvalds 
6450c1822e6SRoman Zippel 	oldval = sym->def[S_DEF_USER].val;
6461da177e4SLinus Torvalds 	size = strlen(newval) + 1;
6471da177e4SLinus Torvalds 	if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
6481da177e4SLinus Torvalds 		size += 2;
6490c1822e6SRoman Zippel 		sym->def[S_DEF_USER].val = val = malloc(size);
6501da177e4SLinus Torvalds 		*val++ = '0';
6511da177e4SLinus Torvalds 		*val++ = 'x';
6521da177e4SLinus Torvalds 	} else if (!oldval || strcmp(oldval, newval))
6530c1822e6SRoman Zippel 		sym->def[S_DEF_USER].val = val = malloc(size);
6541da177e4SLinus Torvalds 	else
6551da177e4SLinus Torvalds 		return true;
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	strcpy(val, newval);
6581da177e4SLinus Torvalds 	free((void *)oldval);
6591da177e4SLinus Torvalds 	sym_clear_all_valid();
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	return true;
6621da177e4SLinus Torvalds }
6631da177e4SLinus Torvalds 
6647cf3d73bSSam Ravnborg /*
6657cf3d73bSSam Ravnborg  * Find the default value associated to a symbol.
6667cf3d73bSSam Ravnborg  * For tristate symbol handle the modules=n case
6677cf3d73bSSam Ravnborg  * in which case "m" becomes "y".
6687cf3d73bSSam Ravnborg  * If the symbol does not have any default then fallback
6697cf3d73bSSam Ravnborg  * to the fixed default values.
6707cf3d73bSSam Ravnborg  */
6717cf3d73bSSam Ravnborg const char *sym_get_string_default(struct symbol *sym)
6727cf3d73bSSam Ravnborg {
6737cf3d73bSSam Ravnborg 	struct property *prop;
6747cf3d73bSSam Ravnborg 	struct symbol *ds;
6757cf3d73bSSam Ravnborg 	const char *str;
6767cf3d73bSSam Ravnborg 	tristate val;
6777cf3d73bSSam Ravnborg 
6787cf3d73bSSam Ravnborg 	sym_calc_visibility(sym);
6797cf3d73bSSam Ravnborg 	sym_calc_value(modules_sym);
6807cf3d73bSSam Ravnborg 	val = symbol_no.curr.tri;
6817cf3d73bSSam Ravnborg 	str = symbol_empty.curr.val;
6827cf3d73bSSam Ravnborg 
6837cf3d73bSSam Ravnborg 	/* If symbol has a default value look it up */
6847cf3d73bSSam Ravnborg 	prop = sym_get_default_prop(sym);
6857cf3d73bSSam Ravnborg 	if (prop != NULL) {
6867cf3d73bSSam Ravnborg 		switch (sym->type) {
6877cf3d73bSSam Ravnborg 		case S_BOOLEAN:
6887cf3d73bSSam Ravnborg 		case S_TRISTATE:
6897cf3d73bSSam Ravnborg 			/* The visibility imay limit the value from yes => mod */
6907cf3d73bSSam Ravnborg 			val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
6917cf3d73bSSam Ravnborg 			break;
6927cf3d73bSSam Ravnborg 		default:
6937cf3d73bSSam Ravnborg 			/*
6947cf3d73bSSam Ravnborg 			 * The following fails to handle the situation
6957cf3d73bSSam Ravnborg 			 * where a default value is further limited by
6967cf3d73bSSam Ravnborg 			 * the valid range.
6977cf3d73bSSam Ravnborg 			 */
6987cf3d73bSSam Ravnborg 			ds = prop_get_symbol(prop);
6997cf3d73bSSam Ravnborg 			if (ds != NULL) {
7007cf3d73bSSam Ravnborg 				sym_calc_value(ds);
7017cf3d73bSSam Ravnborg 				str = (const char *)ds->curr.val;
7027cf3d73bSSam Ravnborg 			}
7037cf3d73bSSam Ravnborg 		}
7047cf3d73bSSam Ravnborg 	}
7057cf3d73bSSam Ravnborg 
7067cf3d73bSSam Ravnborg 	/* Handle select statements */
7077cf3d73bSSam Ravnborg 	val = EXPR_OR(val, sym->rev_dep.tri);
7087cf3d73bSSam Ravnborg 
7097cf3d73bSSam Ravnborg 	/* transpose mod to yes if modules are not enabled */
7107cf3d73bSSam Ravnborg 	if (val == mod)
7117cf3d73bSSam Ravnborg 		if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
7127cf3d73bSSam Ravnborg 			val = yes;
7137cf3d73bSSam Ravnborg 
7147cf3d73bSSam Ravnborg 	/* transpose mod to yes if type is bool */
7157cf3d73bSSam Ravnborg 	if (sym->type == S_BOOLEAN && val == mod)
7167cf3d73bSSam Ravnborg 		val = yes;
7177cf3d73bSSam Ravnborg 
7187cf3d73bSSam Ravnborg 	switch (sym->type) {
7197cf3d73bSSam Ravnborg 	case S_BOOLEAN:
7207cf3d73bSSam Ravnborg 	case S_TRISTATE:
7217cf3d73bSSam Ravnborg 		switch (val) {
7227cf3d73bSSam Ravnborg 		case no: return "n";
7237cf3d73bSSam Ravnborg 		case mod: return "m";
7247cf3d73bSSam Ravnborg 		case yes: return "y";
7257cf3d73bSSam Ravnborg 		}
7267cf3d73bSSam Ravnborg 	case S_INT:
7277cf3d73bSSam Ravnborg 	case S_HEX:
7287cf3d73bSSam Ravnborg 		return str;
7297cf3d73bSSam Ravnborg 	case S_STRING:
7307cf3d73bSSam Ravnborg 		return str;
7317cf3d73bSSam Ravnborg 	case S_OTHER:
7327cf3d73bSSam Ravnborg 	case S_UNKNOWN:
7337cf3d73bSSam Ravnborg 		break;
7347cf3d73bSSam Ravnborg 	}
7357cf3d73bSSam Ravnborg 	return "";
7367cf3d73bSSam Ravnborg }
7377cf3d73bSSam Ravnborg 
7381da177e4SLinus Torvalds const char *sym_get_string_value(struct symbol *sym)
7391da177e4SLinus Torvalds {
7401da177e4SLinus Torvalds 	tristate val;
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	switch (sym->type) {
7431da177e4SLinus Torvalds 	case S_BOOLEAN:
7441da177e4SLinus Torvalds 	case S_TRISTATE:
7451da177e4SLinus Torvalds 		val = sym_get_tristate_value(sym);
7461da177e4SLinus Torvalds 		switch (val) {
7471da177e4SLinus Torvalds 		case no:
7481da177e4SLinus Torvalds 			return "n";
7491da177e4SLinus Torvalds 		case mod:
7501da177e4SLinus Torvalds 			return "m";
7511da177e4SLinus Torvalds 		case yes:
7521da177e4SLinus Torvalds 			return "y";
7531da177e4SLinus Torvalds 		}
7541da177e4SLinus Torvalds 		break;
7551da177e4SLinus Torvalds 	default:
7561da177e4SLinus Torvalds 		;
7571da177e4SLinus Torvalds 	}
7581da177e4SLinus Torvalds 	return (const char *)sym->curr.val;
7591da177e4SLinus Torvalds }
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds bool sym_is_changable(struct symbol *sym)
7621da177e4SLinus Torvalds {
7631da177e4SLinus Torvalds 	return sym->visible > sym->rev_dep.tri;
7641da177e4SLinus Torvalds }
7651da177e4SLinus Torvalds 
766e66f25d7SAndi Kleen static unsigned strhash(const char *s)
767e66f25d7SAndi Kleen {
768e66f25d7SAndi Kleen 	/* fnv32 hash */
769e66f25d7SAndi Kleen 	unsigned hash = 2166136261U;
770e66f25d7SAndi Kleen 	for (; *s; s++)
771e66f25d7SAndi Kleen 		hash = (hash ^ *s) * 0x01000193;
772e66f25d7SAndi Kleen 	return hash;
773e66f25d7SAndi Kleen }
774e66f25d7SAndi Kleen 
7755a1aa8a1SRoman Zippel struct symbol *sym_lookup(const char *name, int flags)
7761da177e4SLinus Torvalds {
7771da177e4SLinus Torvalds 	struct symbol *symbol;
7781da177e4SLinus Torvalds 	char *new_name;
779e66f25d7SAndi Kleen 	int hash;
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	if (name) {
7821da177e4SLinus Torvalds 		if (name[0] && !name[1]) {
7831da177e4SLinus Torvalds 			switch (name[0]) {
7841da177e4SLinus Torvalds 			case 'y': return &symbol_yes;
7851da177e4SLinus Torvalds 			case 'm': return &symbol_mod;
7861da177e4SLinus Torvalds 			case 'n': return &symbol_no;
7871da177e4SLinus Torvalds 			}
7881da177e4SLinus Torvalds 		}
789e66f25d7SAndi Kleen 		hash = strhash(name) % SYMBOL_HASHSIZE;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 		for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
792e66f25d7SAndi Kleen 			if (symbol->name &&
793e66f25d7SAndi Kleen 			    !strcmp(symbol->name, name) &&
7945a1aa8a1SRoman Zippel 			    (flags ? symbol->flags & flags
7955a1aa8a1SRoman Zippel 				   : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
7961da177e4SLinus Torvalds 				return symbol;
7971da177e4SLinus Torvalds 		}
7981da177e4SLinus Torvalds 		new_name = strdup(name);
7991da177e4SLinus Torvalds 	} else {
8001da177e4SLinus Torvalds 		new_name = NULL;
801e66f25d7SAndi Kleen 		hash = 0;
8021da177e4SLinus Torvalds 	}
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	symbol = malloc(sizeof(*symbol));
8051da177e4SLinus Torvalds 	memset(symbol, 0, sizeof(*symbol));
8061da177e4SLinus Torvalds 	symbol->name = new_name;
8071da177e4SLinus Torvalds 	symbol->type = S_UNKNOWN;
8085a1aa8a1SRoman Zippel 	symbol->flags |= flags;
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	symbol->next = symbol_hash[hash];
8111da177e4SLinus Torvalds 	symbol_hash[hash] = symbol;
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	return symbol;
8141da177e4SLinus Torvalds }
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds struct symbol *sym_find(const char *name)
8171da177e4SLinus Torvalds {
8181da177e4SLinus Torvalds 	struct symbol *symbol = NULL;
8191da177e4SLinus Torvalds 	int hash = 0;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	if (!name)
8221da177e4SLinus Torvalds 		return NULL;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	if (name[0] && !name[1]) {
8251da177e4SLinus Torvalds 		switch (name[0]) {
8261da177e4SLinus Torvalds 		case 'y': return &symbol_yes;
8271da177e4SLinus Torvalds 		case 'm': return &symbol_mod;
8281da177e4SLinus Torvalds 		case 'n': return &symbol_no;
8291da177e4SLinus Torvalds 		}
8301da177e4SLinus Torvalds 	}
831e66f25d7SAndi Kleen 	hash = strhash(name) % SYMBOL_HASHSIZE;
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
834e66f25d7SAndi Kleen 		if (symbol->name &&
835e66f25d7SAndi Kleen 		    !strcmp(symbol->name, name) &&
8361da177e4SLinus Torvalds 		    !(symbol->flags & SYMBOL_CONST))
8371da177e4SLinus Torvalds 				break;
8381da177e4SLinus Torvalds 	}
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	return symbol;
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
843*76a54095SArnaud Lacombe /*
844*76a54095SArnaud Lacombe  * Expand symbol's names embedded in the string given in argument. Symbols'
845*76a54095SArnaud Lacombe  * name to be expanded shall be prefixed by a '$'. Unknown symbol expands to
846*76a54095SArnaud Lacombe  * the empty string.
847*76a54095SArnaud Lacombe  */
848*76a54095SArnaud Lacombe const char *sym_expand_string_value(const char *in)
849*76a54095SArnaud Lacombe {
850*76a54095SArnaud Lacombe 	const char *src;
851*76a54095SArnaud Lacombe 	char *res;
852*76a54095SArnaud Lacombe 	size_t reslen;
853*76a54095SArnaud Lacombe 
854*76a54095SArnaud Lacombe 	reslen = strlen(in) + 1;
855*76a54095SArnaud Lacombe 	res = malloc(reslen);
856*76a54095SArnaud Lacombe 	res[0] = '\0';
857*76a54095SArnaud Lacombe 
858*76a54095SArnaud Lacombe 	while ((src = strchr(in, '$'))) {
859*76a54095SArnaud Lacombe 		char *p, name[SYMBOL_MAXLENGTH];
860*76a54095SArnaud Lacombe 		const char *symval = "";
861*76a54095SArnaud Lacombe 		struct symbol *sym;
862*76a54095SArnaud Lacombe 		size_t newlen;
863*76a54095SArnaud Lacombe 
864*76a54095SArnaud Lacombe 		strncat(res, in, src - in);
865*76a54095SArnaud Lacombe 		src++;
866*76a54095SArnaud Lacombe 
867*76a54095SArnaud Lacombe 		p = name;
868*76a54095SArnaud Lacombe 		while (isalnum(*src) || *src == '_')
869*76a54095SArnaud Lacombe 			*p++ = *src++;
870*76a54095SArnaud Lacombe 		*p = '\0';
871*76a54095SArnaud Lacombe 
872*76a54095SArnaud Lacombe 		sym = sym_find(name);
873*76a54095SArnaud Lacombe 		if (sym != NULL) {
874*76a54095SArnaud Lacombe 			sym_calc_value(sym);
875*76a54095SArnaud Lacombe 			symval = sym_get_string_value(sym);
876*76a54095SArnaud Lacombe 		}
877*76a54095SArnaud Lacombe 
878*76a54095SArnaud Lacombe 		newlen = strlen(res) + strlen(symval) + strlen(src);
879*76a54095SArnaud Lacombe 		if (newlen > reslen) {
880*76a54095SArnaud Lacombe 			reslen = newlen;
881*76a54095SArnaud Lacombe 			realloc(res, reslen);
882*76a54095SArnaud Lacombe 		}
883*76a54095SArnaud Lacombe 
884*76a54095SArnaud Lacombe 		strcat(res, symval);
885*76a54095SArnaud Lacombe 		in = src;
886*76a54095SArnaud Lacombe 	}
887*76a54095SArnaud Lacombe 	strcat(res, in);
888*76a54095SArnaud Lacombe 
889*76a54095SArnaud Lacombe 	return res;
890*76a54095SArnaud Lacombe }
891*76a54095SArnaud Lacombe 
8921da177e4SLinus Torvalds struct symbol **sym_re_search(const char *pattern)
8931da177e4SLinus Torvalds {
8941da177e4SLinus Torvalds 	struct symbol *sym, **sym_arr = NULL;
8951da177e4SLinus Torvalds 	int i, cnt, size;
8961da177e4SLinus Torvalds 	regex_t re;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	cnt = size = 0;
8991da177e4SLinus Torvalds 	/* Skip if empty */
9001da177e4SLinus Torvalds 	if (strlen(pattern) == 0)
9011da177e4SLinus Torvalds 		return NULL;
9021da177e4SLinus Torvalds 	if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
9031da177e4SLinus Torvalds 		return NULL;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
9061da177e4SLinus Torvalds 		if (sym->flags & SYMBOL_CONST || !sym->name)
9071da177e4SLinus Torvalds 			continue;
9081da177e4SLinus Torvalds 		if (regexec(&re, sym->name, 0, NULL, 0))
9091da177e4SLinus Torvalds 			continue;
9101da177e4SLinus Torvalds 		if (cnt + 1 >= size) {
9111da177e4SLinus Torvalds 			void *tmp = sym_arr;
9121da177e4SLinus Torvalds 			size += 16;
9131da177e4SLinus Torvalds 			sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
9141da177e4SLinus Torvalds 			if (!sym_arr) {
9151da177e4SLinus Torvalds 				free(tmp);
9161da177e4SLinus Torvalds 				return NULL;
9171da177e4SLinus Torvalds 			}
9181da177e4SLinus Torvalds 		}
919da6df879SLi Zefan 		sym_calc_value(sym);
9201da177e4SLinus Torvalds 		sym_arr[cnt++] = sym;
9211da177e4SLinus Torvalds 	}
9221da177e4SLinus Torvalds 	if (sym_arr)
9231da177e4SLinus Torvalds 		sym_arr[cnt] = NULL;
9241da177e4SLinus Torvalds 	regfree(&re);
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	return sym_arr;
9271da177e4SLinus Torvalds }
9281da177e4SLinus Torvalds 
929d595cea6SRoman Zippel /*
930d595cea6SRoman Zippel  * When we check for recursive dependencies we use a stack to save
931d595cea6SRoman Zippel  * current state so we can print out relevant info to user.
932d595cea6SRoman Zippel  * The entries are located on the call stack so no need to free memory.
933d595cea6SRoman Zippel  * Note inser() remove() must always match to properly clear the stack.
934d595cea6SRoman Zippel  */
935d595cea6SRoman Zippel static struct dep_stack {
936d595cea6SRoman Zippel 	struct dep_stack *prev, *next;
937d595cea6SRoman Zippel 	struct symbol *sym;
938d595cea6SRoman Zippel 	struct property *prop;
939d595cea6SRoman Zippel 	struct expr *expr;
940d595cea6SRoman Zippel } *check_top;
941d595cea6SRoman Zippel 
942d595cea6SRoman Zippel static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
943d595cea6SRoman Zippel {
944d595cea6SRoman Zippel 	memset(stack, 0, sizeof(*stack));
945d595cea6SRoman Zippel 	if (check_top)
946d595cea6SRoman Zippel 		check_top->next = stack;
947d595cea6SRoman Zippel 	stack->prev = check_top;
948d595cea6SRoman Zippel 	stack->sym = sym;
949d595cea6SRoman Zippel 	check_top = stack;
950d595cea6SRoman Zippel }
951d595cea6SRoman Zippel 
952d595cea6SRoman Zippel static void dep_stack_remove(void)
953d595cea6SRoman Zippel {
954d595cea6SRoman Zippel 	check_top = check_top->prev;
955d595cea6SRoman Zippel 	if (check_top)
956d595cea6SRoman Zippel 		check_top->next = NULL;
957d595cea6SRoman Zippel }
958d595cea6SRoman Zippel 
959d595cea6SRoman Zippel /*
960d595cea6SRoman Zippel  * Called when we have detected a recursive dependency.
961d595cea6SRoman Zippel  * check_top point to the top of the stact so we use
962d595cea6SRoman Zippel  * the ->prev pointer to locate the bottom of the stack.
963d595cea6SRoman Zippel  */
964d595cea6SRoman Zippel static void sym_check_print_recursive(struct symbol *last_sym)
965d595cea6SRoman Zippel {
966d595cea6SRoman Zippel 	struct dep_stack *stack;
967d595cea6SRoman Zippel 	struct symbol *sym, *next_sym;
968d595cea6SRoman Zippel 	struct menu *menu = NULL;
969d595cea6SRoman Zippel 	struct property *prop;
970d595cea6SRoman Zippel 	struct dep_stack cv_stack;
971d595cea6SRoman Zippel 
972d595cea6SRoman Zippel 	if (sym_is_choice_value(last_sym)) {
973d595cea6SRoman Zippel 		dep_stack_insert(&cv_stack, last_sym);
974d595cea6SRoman Zippel 		last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
975d595cea6SRoman Zippel 	}
976d595cea6SRoman Zippel 
977d595cea6SRoman Zippel 	for (stack = check_top; stack != NULL; stack = stack->prev)
978d595cea6SRoman Zippel 		if (stack->sym == last_sym)
979d595cea6SRoman Zippel 			break;
980d595cea6SRoman Zippel 	if (!stack) {
981d595cea6SRoman Zippel 		fprintf(stderr, "unexpected recursive dependency error\n");
982d595cea6SRoman Zippel 		return;
983d595cea6SRoman Zippel 	}
984d595cea6SRoman Zippel 
985d595cea6SRoman Zippel 	for (; stack; stack = stack->next) {
986d595cea6SRoman Zippel 		sym = stack->sym;
987d595cea6SRoman Zippel 		next_sym = stack->next ? stack->next->sym : last_sym;
988d595cea6SRoman Zippel 		prop = stack->prop;
9893643f849SSam Ravnborg 		if (prop == NULL)
9903643f849SSam Ravnborg 			prop = stack->sym->prop;
991d595cea6SRoman Zippel 
992d595cea6SRoman Zippel 		/* for choice values find the menu entry (used below) */
993d595cea6SRoman Zippel 		if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
994d595cea6SRoman Zippel 			for (prop = sym->prop; prop; prop = prop->next) {
995d595cea6SRoman Zippel 				menu = prop->menu;
996d595cea6SRoman Zippel 				if (prop->menu)
997d595cea6SRoman Zippel 					break;
998d595cea6SRoman Zippel 			}
999d595cea6SRoman Zippel 		}
1000d595cea6SRoman Zippel 		if (stack->sym == last_sym)
1001d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
1002d595cea6SRoman Zippel 				prop->file->name, prop->lineno);
1003d595cea6SRoman Zippel 		if (stack->expr) {
1004d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
1005d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
1006d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
1007d595cea6SRoman Zippel 				prop_get_type_name(prop->type),
1008d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
1009d595cea6SRoman Zippel 		} else if (stack->prop) {
1010d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
1011d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
1012d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
1013d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
1014d595cea6SRoman Zippel 		} else if (sym_is_choice(sym)) {
1015d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
1016d595cea6SRoman Zippel 				menu->file->name, menu->lineno,
1017d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
1018d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
1019d595cea6SRoman Zippel 		} else if (sym_is_choice_value(sym)) {
1020d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
1021d595cea6SRoman Zippel 				menu->file->name, menu->lineno,
1022d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
1023d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
1024d595cea6SRoman Zippel 		} else {
1025d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
1026d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
1027d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
1028d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
1029d595cea6SRoman Zippel 		}
1030d595cea6SRoman Zippel 	}
1031d595cea6SRoman Zippel 
1032d595cea6SRoman Zippel 	if (check_top == &cv_stack)
1033d595cea6SRoman Zippel 		dep_stack_remove();
1034d595cea6SRoman Zippel }
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds static struct symbol *sym_check_expr_deps(struct expr *e)
10371da177e4SLinus Torvalds {
10381da177e4SLinus Torvalds 	struct symbol *sym;
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	if (!e)
10411da177e4SLinus Torvalds 		return NULL;
10421da177e4SLinus Torvalds 	switch (e->type) {
10431da177e4SLinus Torvalds 	case E_OR:
10441da177e4SLinus Torvalds 	case E_AND:
10451da177e4SLinus Torvalds 		sym = sym_check_expr_deps(e->left.expr);
10461da177e4SLinus Torvalds 		if (sym)
10471da177e4SLinus Torvalds 			return sym;
10481da177e4SLinus Torvalds 		return sym_check_expr_deps(e->right.expr);
10491da177e4SLinus Torvalds 	case E_NOT:
10501da177e4SLinus Torvalds 		return sym_check_expr_deps(e->left.expr);
10511da177e4SLinus Torvalds 	case E_EQUAL:
10521da177e4SLinus Torvalds 	case E_UNEQUAL:
10531da177e4SLinus Torvalds 		sym = sym_check_deps(e->left.sym);
10541da177e4SLinus Torvalds 		if (sym)
10551da177e4SLinus Torvalds 			return sym;
10561da177e4SLinus Torvalds 		return sym_check_deps(e->right.sym);
10571da177e4SLinus Torvalds 	case E_SYMBOL:
10581da177e4SLinus Torvalds 		return sym_check_deps(e->left.sym);
10591da177e4SLinus Torvalds 	default:
10601da177e4SLinus Torvalds 		break;
10611da177e4SLinus Torvalds 	}
10621da177e4SLinus Torvalds 	printf("Oops! How to check %d?\n", e->type);
10631da177e4SLinus Torvalds 	return NULL;
10641da177e4SLinus Torvalds }
10651da177e4SLinus Torvalds 
10665447d34bSSam Ravnborg /* return NULL when dependencies are OK */
106748981178SRoman Zippel static struct symbol *sym_check_sym_deps(struct symbol *sym)
106848981178SRoman Zippel {
106948981178SRoman Zippel 	struct symbol *sym2;
107048981178SRoman Zippel 	struct property *prop;
1071d595cea6SRoman Zippel 	struct dep_stack stack;
1072d595cea6SRoman Zippel 
1073d595cea6SRoman Zippel 	dep_stack_insert(&stack, sym);
107448981178SRoman Zippel 
107548981178SRoman Zippel 	sym2 = sym_check_expr_deps(sym->rev_dep.expr);
107648981178SRoman Zippel 	if (sym2)
1077d595cea6SRoman Zippel 		goto out;
107848981178SRoman Zippel 
107948981178SRoman Zippel 	for (prop = sym->prop; prop; prop = prop->next) {
108048981178SRoman Zippel 		if (prop->type == P_CHOICE || prop->type == P_SELECT)
108148981178SRoman Zippel 			continue;
1082d595cea6SRoman Zippel 		stack.prop = prop;
108348981178SRoman Zippel 		sym2 = sym_check_expr_deps(prop->visible.expr);
108448981178SRoman Zippel 		if (sym2)
108548981178SRoman Zippel 			break;
108648981178SRoman Zippel 		if (prop->type != P_DEFAULT || sym_is_choice(sym))
108748981178SRoman Zippel 			continue;
1088d595cea6SRoman Zippel 		stack.expr = prop->expr;
108948981178SRoman Zippel 		sym2 = sym_check_expr_deps(prop->expr);
109048981178SRoman Zippel 		if (sym2)
109148981178SRoman Zippel 			break;
1092d595cea6SRoman Zippel 		stack.expr = NULL;
109348981178SRoman Zippel 	}
109448981178SRoman Zippel 
1095d595cea6SRoman Zippel out:
1096d595cea6SRoman Zippel 	dep_stack_remove();
1097d595cea6SRoman Zippel 
109848981178SRoman Zippel 	return sym2;
109948981178SRoman Zippel }
110048981178SRoman Zippel 
110148981178SRoman Zippel static struct symbol *sym_check_choice_deps(struct symbol *choice)
110248981178SRoman Zippel {
110348981178SRoman Zippel 	struct symbol *sym, *sym2;
110448981178SRoman Zippel 	struct property *prop;
110548981178SRoman Zippel 	struct expr *e;
1106d595cea6SRoman Zippel 	struct dep_stack stack;
1107d595cea6SRoman Zippel 
1108d595cea6SRoman Zippel 	dep_stack_insert(&stack, choice);
110948981178SRoman Zippel 
111048981178SRoman Zippel 	prop = sym_get_choice_prop(choice);
111148981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym)
111248981178SRoman Zippel 		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
111348981178SRoman Zippel 
111448981178SRoman Zippel 	choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
111548981178SRoman Zippel 	sym2 = sym_check_sym_deps(choice);
111648981178SRoman Zippel 	choice->flags &= ~SYMBOL_CHECK;
111748981178SRoman Zippel 	if (sym2)
111848981178SRoman Zippel 		goto out;
111948981178SRoman Zippel 
112048981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym) {
112148981178SRoman Zippel 		sym2 = sym_check_sym_deps(sym);
1122d595cea6SRoman Zippel 		if (sym2)
112348981178SRoman Zippel 			break;
112448981178SRoman Zippel 	}
112548981178SRoman Zippel out:
112648981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym)
112748981178SRoman Zippel 		sym->flags &= ~SYMBOL_CHECK;
112848981178SRoman Zippel 
112948981178SRoman Zippel 	if (sym2 && sym_is_choice_value(sym2) &&
113048981178SRoman Zippel 	    prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
113148981178SRoman Zippel 		sym2 = choice;
113248981178SRoman Zippel 
1133d595cea6SRoman Zippel 	dep_stack_remove();
1134d595cea6SRoman Zippel 
113548981178SRoman Zippel 	return sym2;
113648981178SRoman Zippel }
113748981178SRoman Zippel 
11381da177e4SLinus Torvalds struct symbol *sym_check_deps(struct symbol *sym)
11391da177e4SLinus Torvalds {
11401da177e4SLinus Torvalds 	struct symbol *sym2;
11411da177e4SLinus Torvalds 	struct property *prop;
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	if (sym->flags & SYMBOL_CHECK) {
1144d595cea6SRoman Zippel 		sym_check_print_recursive(sym);
11451da177e4SLinus Torvalds 		return sym;
11461da177e4SLinus Torvalds 	}
11473f04e7ddSDavid Gibson 	if (sym->flags & SYMBOL_CHECKED)
11483f04e7ddSDavid Gibson 		return NULL;
11491da177e4SLinus Torvalds 
115048981178SRoman Zippel 	if (sym_is_choice_value(sym)) {
1151d595cea6SRoman Zippel 		struct dep_stack stack;
1152d595cea6SRoman Zippel 
115348981178SRoman Zippel 		/* for choice groups start the check with main choice symbol */
1154d595cea6SRoman Zippel 		dep_stack_insert(&stack, sym);
115548981178SRoman Zippel 		prop = sym_get_choice_prop(sym);
115648981178SRoman Zippel 		sym2 = sym_check_deps(prop_get_symbol(prop));
1157d595cea6SRoman Zippel 		dep_stack_remove();
115848981178SRoman Zippel 	} else if (sym_is_choice(sym)) {
115948981178SRoman Zippel 		sym2 = sym_check_choice_deps(sym);
116048981178SRoman Zippel 	} else {
11611da177e4SLinus Torvalds 		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
116248981178SRoman Zippel 		sym2 = sym_check_sym_deps(sym);
11631da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_CHECK;
116448981178SRoman Zippel 	}
116548981178SRoman Zippel 
1166d595cea6SRoman Zippel 	if (sym2 && sym2 == sym)
116748981178SRoman Zippel 		sym2 = NULL;
116848981178SRoman Zippel 
11691da177e4SLinus Torvalds 	return sym2;
11701da177e4SLinus Torvalds }
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds struct property *prop_alloc(enum prop_type type, struct symbol *sym)
11731da177e4SLinus Torvalds {
11741da177e4SLinus Torvalds 	struct property *prop;
11751da177e4SLinus Torvalds 	struct property **propp;
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 	prop = malloc(sizeof(*prop));
11781da177e4SLinus Torvalds 	memset(prop, 0, sizeof(*prop));
11791da177e4SLinus Torvalds 	prop->type = type;
11801da177e4SLinus Torvalds 	prop->sym = sym;
11811da177e4SLinus Torvalds 	prop->file = current_file;
11821da177e4SLinus Torvalds 	prop->lineno = zconf_lineno();
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 	/* append property to the prop list of symbol */
11851da177e4SLinus Torvalds 	if (sym) {
11861da177e4SLinus Torvalds 		for (propp = &sym->prop; *propp; propp = &(*propp)->next)
11871da177e4SLinus Torvalds 			;
11881da177e4SLinus Torvalds 		*propp = prop;
11891da177e4SLinus Torvalds 	}
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 	return prop;
11921da177e4SLinus Torvalds }
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds struct symbol *prop_get_symbol(struct property *prop)
11951da177e4SLinus Torvalds {
11961da177e4SLinus Torvalds 	if (prop->expr && (prop->expr->type == E_SYMBOL ||
11977a962923SRoman Zippel 			   prop->expr->type == E_LIST))
11981da177e4SLinus Torvalds 		return prop->expr->left.sym;
11991da177e4SLinus Torvalds 	return NULL;
12001da177e4SLinus Torvalds }
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds const char *prop_get_type_name(enum prop_type type)
12031da177e4SLinus Torvalds {
12041da177e4SLinus Torvalds 	switch (type) {
12051da177e4SLinus Torvalds 	case P_PROMPT:
12061da177e4SLinus Torvalds 		return "prompt";
120793449082SRoman Zippel 	case P_ENV:
120893449082SRoman Zippel 		return "env";
12091da177e4SLinus Torvalds 	case P_COMMENT:
12101da177e4SLinus Torvalds 		return "comment";
12111da177e4SLinus Torvalds 	case P_MENU:
12121da177e4SLinus Torvalds 		return "menu";
12131da177e4SLinus Torvalds 	case P_DEFAULT:
12141da177e4SLinus Torvalds 		return "default";
12151da177e4SLinus Torvalds 	case P_CHOICE:
12161da177e4SLinus Torvalds 		return "choice";
12171da177e4SLinus Torvalds 	case P_SELECT:
12181da177e4SLinus Torvalds 		return "select";
12191da177e4SLinus Torvalds 	case P_RANGE:
12201da177e4SLinus Torvalds 		return "range";
122159e89e3dSSam Ravnborg 	case P_SYMBOL:
122259e89e3dSSam Ravnborg 		return "symbol";
12231da177e4SLinus Torvalds 	case P_UNKNOWN:
12241da177e4SLinus Torvalds 		break;
12251da177e4SLinus Torvalds 	}
12261da177e4SLinus Torvalds 	return "unknown";
12271da177e4SLinus Torvalds }
122893449082SRoman Zippel 
12294356f489STrevor Keith static void prop_add_env(const char *env)
123093449082SRoman Zippel {
123193449082SRoman Zippel 	struct symbol *sym, *sym2;
123293449082SRoman Zippel 	struct property *prop;
123393449082SRoman Zippel 	char *p;
123493449082SRoman Zippel 
123593449082SRoman Zippel 	sym = current_entry->sym;
123693449082SRoman Zippel 	sym->flags |= SYMBOL_AUTO;
123793449082SRoman Zippel 	for_all_properties(sym, prop, P_ENV) {
123893449082SRoman Zippel 		sym2 = prop_get_symbol(prop);
123993449082SRoman Zippel 		if (strcmp(sym2->name, env))
124093449082SRoman Zippel 			menu_warn(current_entry, "redefining environment symbol from %s",
124193449082SRoman Zippel 				  sym2->name);
124293449082SRoman Zippel 		return;
124393449082SRoman Zippel 	}
124493449082SRoman Zippel 
124593449082SRoman Zippel 	prop = prop_alloc(P_ENV, sym);
12465a1aa8a1SRoman Zippel 	prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
124793449082SRoman Zippel 
124893449082SRoman Zippel 	sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
124993449082SRoman Zippel 	sym_env_list->right.sym = sym;
125093449082SRoman Zippel 
125193449082SRoman Zippel 	p = getenv(env);
125293449082SRoman Zippel 	if (p)
125393449082SRoman Zippel 		sym_add_default(sym, p);
125493449082SRoman Zippel 	else
125593449082SRoman Zippel 		menu_warn(current_entry, "environment variable %s undefined", env);
125693449082SRoman Zippel }
1257