xref: /openbmc/linux/scripts/kconfig/symbol.c (revision d595cea62403db4e65b98a8bb96ff2b5205c7b82)
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 
2291da177e4SLinus Torvalds static struct symbol *sym_calc_choice(struct symbol *sym)
2301da177e4SLinus Torvalds {
2311da177e4SLinus Torvalds 	struct symbol *def_sym;
2321da177e4SLinus Torvalds 	struct property *prop;
2331da177e4SLinus Torvalds 	struct expr *e;
2341da177e4SLinus Torvalds 
2350a28c47bSJan Beulich 	/* first calculate all choice values' visibilities */
2360a28c47bSJan Beulich 	prop = sym_get_choice_prop(sym);
2370a28c47bSJan Beulich 	expr_list_for_each_sym(prop->expr, e, def_sym)
2380a28c47bSJan Beulich 		sym_calc_visibility(def_sym);
2390a28c47bSJan Beulich 
2401da177e4SLinus Torvalds 	/* is the user choice visible? */
2410c1822e6SRoman Zippel 	def_sym = sym->def[S_DEF_USER].val;
2420a28c47bSJan Beulich 	if (def_sym && def_sym->visible != no)
2431da177e4SLinus Torvalds 		return def_sym;
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	/* any of the defaults visible? */
2461da177e4SLinus Torvalds 	for_all_defaults(sym, prop) {
2471da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
2481da177e4SLinus Torvalds 		if (prop->visible.tri == no)
2491da177e4SLinus Torvalds 			continue;
2501da177e4SLinus Torvalds 		def_sym = prop_get_symbol(prop);
2511da177e4SLinus Torvalds 		if (def_sym->visible != no)
2521da177e4SLinus Torvalds 			return def_sym;
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	/* just get the first visible value */
2561da177e4SLinus Torvalds 	prop = sym_get_choice_prop(sym);
2570a28c47bSJan Beulich 	expr_list_for_each_sym(prop->expr, e, def_sym)
2581da177e4SLinus Torvalds 		if (def_sym->visible != no)
2591da177e4SLinus Torvalds 			return def_sym;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	/* no choice? reset tristate value */
2621da177e4SLinus Torvalds 	sym->curr.tri = no;
2631da177e4SLinus Torvalds 	return NULL;
2641da177e4SLinus Torvalds }
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds void sym_calc_value(struct symbol *sym)
2671da177e4SLinus Torvalds {
2681da177e4SLinus Torvalds 	struct symbol_value newval, oldval;
2691da177e4SLinus Torvalds 	struct property *prop;
2701da177e4SLinus Torvalds 	struct expr *e;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	if (!sym)
2731da177e4SLinus Torvalds 		return;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	if (sym->flags & SYMBOL_VALID)
2761da177e4SLinus Torvalds 		return;
2771da177e4SLinus Torvalds 	sym->flags |= SYMBOL_VALID;
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	oldval = sym->curr;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	switch (sym->type) {
2821da177e4SLinus Torvalds 	case S_INT:
2831da177e4SLinus Torvalds 	case S_HEX:
2841da177e4SLinus Torvalds 	case S_STRING:
2851da177e4SLinus Torvalds 		newval = symbol_empty.curr;
2861da177e4SLinus Torvalds 		break;
2871da177e4SLinus Torvalds 	case S_BOOLEAN:
2881da177e4SLinus Torvalds 	case S_TRISTATE:
2891da177e4SLinus Torvalds 		newval = symbol_no.curr;
2901da177e4SLinus Torvalds 		break;
2911da177e4SLinus Torvalds 	default:
2921da177e4SLinus Torvalds 		sym->curr.val = sym->name;
2931da177e4SLinus Torvalds 		sym->curr.tri = no;
2941da177e4SLinus Torvalds 		return;
2951da177e4SLinus Torvalds 	}
2961da177e4SLinus Torvalds 	if (!sym_is_choice_value(sym))
2971da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_WRITE;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	sym_calc_visibility(sym);
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	/* set default if recursively called */
3021da177e4SLinus Torvalds 	sym->curr = newval;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	switch (sym_get_type(sym)) {
3051da177e4SLinus Torvalds 	case S_BOOLEAN:
3061da177e4SLinus Torvalds 	case S_TRISTATE:
3071da177e4SLinus Torvalds 		if (sym_is_choice_value(sym) && sym->visible == yes) {
3081da177e4SLinus Torvalds 			prop = sym_get_choice_prop(sym);
3091da177e4SLinus Torvalds 			newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
310587c9061SRoman Zippel 		} else {
311587c9061SRoman Zippel 			if (sym->visible != no) {
312587c9061SRoman Zippel 				/* if the symbol is visible use the user value
313587c9061SRoman Zippel 				 * if available, otherwise try the default value
314587c9061SRoman Zippel 				 */
3151da177e4SLinus Torvalds 				sym->flags |= SYMBOL_WRITE;
316587c9061SRoman Zippel 				if (sym_has_value(sym)) {
317587c9061SRoman Zippel 					newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
318587c9061SRoman Zippel 							      sym->visible);
319587c9061SRoman Zippel 					goto calc_newval;
3201da177e4SLinus Torvalds 				}
321587c9061SRoman Zippel 			}
322587c9061SRoman Zippel 			if (sym->rev_dep.tri != no)
323587c9061SRoman Zippel 				sym->flags |= SYMBOL_WRITE;
324587c9061SRoman Zippel 			if (!sym_is_choice(sym)) {
3251da177e4SLinus Torvalds 				prop = sym_get_default_prop(sym);
3261da177e4SLinus Torvalds 				if (prop) {
3271da177e4SLinus Torvalds 					sym->flags |= SYMBOL_WRITE;
328587c9061SRoman Zippel 					newval.tri = EXPR_AND(expr_calc_value(prop->expr),
329587c9061SRoman Zippel 							      prop->visible.tri);
3301da177e4SLinus Torvalds 				}
3311da177e4SLinus Torvalds 			}
332587c9061SRoman Zippel 		calc_newval:
333246cf9c2SCatalin Marinas 			if (sym->dir_dep.tri == no && sym->rev_dep.tri != no) {
334246cf9c2SCatalin Marinas 				fprintf(stderr, "warning: (");
335246cf9c2SCatalin Marinas 				expr_fprint(sym->rev_dep.expr, stderr);
336246cf9c2SCatalin Marinas 				fprintf(stderr, ") selects %s which has unmet direct dependencies (",
337246cf9c2SCatalin Marinas 					sym->name);
338246cf9c2SCatalin Marinas 				expr_fprint(sym->dir_dep.expr, stderr);
339246cf9c2SCatalin Marinas 				fprintf(stderr, ")\n");
340246cf9c2SCatalin Marinas 			}
341587c9061SRoman Zippel 			newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
342587c9061SRoman Zippel 		}
3431da177e4SLinus Torvalds 		if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
3441da177e4SLinus Torvalds 			newval.tri = yes;
3451da177e4SLinus Torvalds 		break;
3461da177e4SLinus Torvalds 	case S_STRING:
3471da177e4SLinus Torvalds 	case S_HEX:
3481da177e4SLinus Torvalds 	case S_INT:
3491da177e4SLinus Torvalds 		if (sym->visible != no) {
3501da177e4SLinus Torvalds 			sym->flags |= SYMBOL_WRITE;
3511da177e4SLinus Torvalds 			if (sym_has_value(sym)) {
3520c1822e6SRoman Zippel 				newval.val = sym->def[S_DEF_USER].val;
3531da177e4SLinus Torvalds 				break;
3541da177e4SLinus Torvalds 			}
3551da177e4SLinus Torvalds 		}
3561da177e4SLinus Torvalds 		prop = sym_get_default_prop(sym);
3571da177e4SLinus Torvalds 		if (prop) {
3581da177e4SLinus Torvalds 			struct symbol *ds = prop_get_symbol(prop);
3591da177e4SLinus Torvalds 			if (ds) {
3601da177e4SLinus Torvalds 				sym->flags |= SYMBOL_WRITE;
3611da177e4SLinus Torvalds 				sym_calc_value(ds);
3621da177e4SLinus Torvalds 				newval.val = ds->curr.val;
3631da177e4SLinus Torvalds 			}
3641da177e4SLinus Torvalds 		}
3651da177e4SLinus Torvalds 		break;
3661da177e4SLinus Torvalds 	default:
3671da177e4SLinus Torvalds 		;
3681da177e4SLinus Torvalds 	}
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	sym->curr = newval;
3711da177e4SLinus Torvalds 	if (sym_is_choice(sym) && newval.tri == yes)
3721da177e4SLinus Torvalds 		sym->curr.val = sym_calc_choice(sym);
3734cf3cbe2SRoman Zippel 	sym_validate_range(sym);
3741da177e4SLinus Torvalds 
375face4374SRoman Zippel 	if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
3761da177e4SLinus Torvalds 		sym_set_changed(sym);
377face4374SRoman Zippel 		if (modules_sym == sym) {
378face4374SRoman Zippel 			sym_set_all_changed();
3791da177e4SLinus Torvalds 			modules_val = modules_sym->curr.tri;
380face4374SRoman Zippel 		}
381face4374SRoman Zippel 	}
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	if (sym_is_choice(sym)) {
3847a962923SRoman Zippel 		struct symbol *choice_sym;
3857a962923SRoman Zippel 
3861da177e4SLinus Torvalds 		prop = sym_get_choice_prop(sym);
3877a962923SRoman Zippel 		expr_list_for_each_sym(prop->expr, e, choice_sym) {
3880a28c47bSJan Beulich 			if ((sym->flags & SYMBOL_WRITE) &&
3890a28c47bSJan Beulich 			    choice_sym->visible != no)
3900a28c47bSJan Beulich 				choice_sym->flags |= SYMBOL_WRITE;
3910a28c47bSJan Beulich 			if (sym->flags & SYMBOL_CHANGED)
3927a962923SRoman Zippel 				sym_set_changed(choice_sym);
3931da177e4SLinus Torvalds 		}
3941da177e4SLinus Torvalds 	}
3955a1aa8a1SRoman Zippel 
3965a1aa8a1SRoman Zippel 	if (sym->flags & SYMBOL_AUTO)
3975a1aa8a1SRoman Zippel 		sym->flags &= ~SYMBOL_WRITE;
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds void sym_clear_all_valid(void)
4011da177e4SLinus Torvalds {
4021da177e4SLinus Torvalds 	struct symbol *sym;
4031da177e4SLinus Torvalds 	int i;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	for_all_symbols(i, sym)
4061da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_VALID;
407bfc10001SKarsten Wiese 	sym_add_change_count(1);
4081da177e4SLinus Torvalds 	if (modules_sym)
4091da177e4SLinus Torvalds 		sym_calc_value(modules_sym);
4101da177e4SLinus Torvalds }
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds void sym_set_changed(struct symbol *sym)
4131da177e4SLinus Torvalds {
4141da177e4SLinus Torvalds 	struct property *prop;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	sym->flags |= SYMBOL_CHANGED;
4171da177e4SLinus Torvalds 	for (prop = sym->prop; prop; prop = prop->next) {
4181da177e4SLinus Torvalds 		if (prop->menu)
4191da177e4SLinus Torvalds 			prop->menu->flags |= MENU_CHANGED;
4201da177e4SLinus Torvalds 	}
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds void sym_set_all_changed(void)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	struct symbol *sym;
4261da177e4SLinus Torvalds 	int i;
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	for_all_symbols(i, sym)
4291da177e4SLinus Torvalds 		sym_set_changed(sym);
4301da177e4SLinus Torvalds }
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds bool sym_tristate_within_range(struct symbol *sym, tristate val)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds 	int type = sym_get_type(sym);
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	if (sym->visible == no)
4371da177e4SLinus Torvalds 		return false;
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	if (type != S_BOOLEAN && type != S_TRISTATE)
4401da177e4SLinus Torvalds 		return false;
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	if (type == S_BOOLEAN && val == mod)
4431da177e4SLinus Torvalds 		return false;
4441da177e4SLinus Torvalds 	if (sym->visible <= sym->rev_dep.tri)
4451da177e4SLinus Torvalds 		return false;
4461da177e4SLinus Torvalds 	if (sym_is_choice_value(sym) && sym->visible == yes)
4471da177e4SLinus Torvalds 		return val == yes;
4481da177e4SLinus Torvalds 	return val >= sym->rev_dep.tri && val <= sym->visible;
4491da177e4SLinus Torvalds }
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds bool sym_set_tristate_value(struct symbol *sym, tristate val)
4521da177e4SLinus Torvalds {
4531da177e4SLinus Torvalds 	tristate oldval = sym_get_tristate_value(sym);
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	if (oldval != val && !sym_tristate_within_range(sym, val))
4561da177e4SLinus Torvalds 		return false;
4571da177e4SLinus Torvalds 
458669bfad9SRoman Zippel 	if (!(sym->flags & SYMBOL_DEF_USER)) {
459669bfad9SRoman Zippel 		sym->flags |= SYMBOL_DEF_USER;
4601da177e4SLinus Torvalds 		sym_set_changed(sym);
4611da177e4SLinus Torvalds 	}
4623f23ca2bSRoman Zippel 	/*
4633f23ca2bSRoman Zippel 	 * setting a choice value also resets the new flag of the choice
4643f23ca2bSRoman Zippel 	 * symbol and all other choice values.
4653f23ca2bSRoman Zippel 	 */
4661da177e4SLinus Torvalds 	if (sym_is_choice_value(sym) && val == yes) {
4671da177e4SLinus Torvalds 		struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
4683f23ca2bSRoman Zippel 		struct property *prop;
4693f23ca2bSRoman Zippel 		struct expr *e;
4701da177e4SLinus Torvalds 
4710c1822e6SRoman Zippel 		cs->def[S_DEF_USER].val = sym;
472669bfad9SRoman Zippel 		cs->flags |= SYMBOL_DEF_USER;
4733f23ca2bSRoman Zippel 		prop = sym_get_choice_prop(cs);
4743f23ca2bSRoman Zippel 		for (e = prop->expr; e; e = e->left.expr) {
4753f23ca2bSRoman Zippel 			if (e->right.sym->visible != no)
476669bfad9SRoman Zippel 				e->right.sym->flags |= SYMBOL_DEF_USER;
4773f23ca2bSRoman Zippel 		}
4781da177e4SLinus Torvalds 	}
4791da177e4SLinus Torvalds 
4800c1822e6SRoman Zippel 	sym->def[S_DEF_USER].tri = val;
481face4374SRoman Zippel 	if (oldval != val)
4821da177e4SLinus Torvalds 		sym_clear_all_valid();
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	return true;
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds tristate sym_toggle_tristate_value(struct symbol *sym)
4881da177e4SLinus Torvalds {
4891da177e4SLinus Torvalds 	tristate oldval, newval;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	oldval = newval = sym_get_tristate_value(sym);
4921da177e4SLinus Torvalds 	do {
4931da177e4SLinus Torvalds 		switch (newval) {
4941da177e4SLinus Torvalds 		case no:
4951da177e4SLinus Torvalds 			newval = mod;
4961da177e4SLinus Torvalds 			break;
4971da177e4SLinus Torvalds 		case mod:
4981da177e4SLinus Torvalds 			newval = yes;
4991da177e4SLinus Torvalds 			break;
5001da177e4SLinus Torvalds 		case yes:
5011da177e4SLinus Torvalds 			newval = no;
5021da177e4SLinus Torvalds 			break;
5031da177e4SLinus Torvalds 		}
5041da177e4SLinus Torvalds 		if (sym_set_tristate_value(sym, newval))
5051da177e4SLinus Torvalds 			break;
5061da177e4SLinus Torvalds 	} while (oldval != newval);
5071da177e4SLinus Torvalds 	return newval;
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds bool sym_string_valid(struct symbol *sym, const char *str)
5111da177e4SLinus Torvalds {
5121da177e4SLinus Torvalds 	signed char ch;
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	switch (sym->type) {
5151da177e4SLinus Torvalds 	case S_STRING:
5161da177e4SLinus Torvalds 		return true;
5171da177e4SLinus Torvalds 	case S_INT:
5181da177e4SLinus Torvalds 		ch = *str++;
5191da177e4SLinus Torvalds 		if (ch == '-')
5201da177e4SLinus Torvalds 			ch = *str++;
5211da177e4SLinus Torvalds 		if (!isdigit(ch))
5221da177e4SLinus Torvalds 			return false;
5231da177e4SLinus Torvalds 		if (ch == '0' && *str != 0)
5241da177e4SLinus Torvalds 			return false;
5251da177e4SLinus Torvalds 		while ((ch = *str++)) {
5261da177e4SLinus Torvalds 			if (!isdigit(ch))
5271da177e4SLinus Torvalds 				return false;
5281da177e4SLinus Torvalds 		}
5291da177e4SLinus Torvalds 		return true;
5301da177e4SLinus Torvalds 	case S_HEX:
5311da177e4SLinus Torvalds 		if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
5321da177e4SLinus Torvalds 			str += 2;
5331da177e4SLinus Torvalds 		ch = *str++;
5341da177e4SLinus Torvalds 		do {
5351da177e4SLinus Torvalds 			if (!isxdigit(ch))
5361da177e4SLinus Torvalds 				return false;
5371da177e4SLinus Torvalds 		} while ((ch = *str++));
5381da177e4SLinus Torvalds 		return true;
5391da177e4SLinus Torvalds 	case S_BOOLEAN:
5401da177e4SLinus Torvalds 	case S_TRISTATE:
5411da177e4SLinus Torvalds 		switch (str[0]) {
5421da177e4SLinus Torvalds 		case 'y': case 'Y':
5431da177e4SLinus Torvalds 		case 'm': case 'M':
5441da177e4SLinus Torvalds 		case 'n': case 'N':
5451da177e4SLinus Torvalds 			return true;
5461da177e4SLinus Torvalds 		}
5471da177e4SLinus Torvalds 		return false;
5481da177e4SLinus Torvalds 	default:
5491da177e4SLinus Torvalds 		return false;
5501da177e4SLinus Torvalds 	}
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds bool sym_string_within_range(struct symbol *sym, const char *str)
5541da177e4SLinus Torvalds {
5551da177e4SLinus Torvalds 	struct property *prop;
5561da177e4SLinus Torvalds 	int val;
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	switch (sym->type) {
5591da177e4SLinus Torvalds 	case S_STRING:
5601da177e4SLinus Torvalds 		return sym_string_valid(sym, str);
5611da177e4SLinus Torvalds 	case S_INT:
5621da177e4SLinus Torvalds 		if (!sym_string_valid(sym, str))
5631da177e4SLinus Torvalds 			return false;
5641da177e4SLinus Torvalds 		prop = sym_get_range_prop(sym);
5651da177e4SLinus Torvalds 		if (!prop)
5661da177e4SLinus Torvalds 			return true;
5671da177e4SLinus Torvalds 		val = strtol(str, NULL, 10);
5684cf3cbe2SRoman Zippel 		return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
5694cf3cbe2SRoman Zippel 		       val <= sym_get_range_val(prop->expr->right.sym, 10);
5701da177e4SLinus Torvalds 	case S_HEX:
5711da177e4SLinus Torvalds 		if (!sym_string_valid(sym, str))
5721da177e4SLinus Torvalds 			return false;
5731da177e4SLinus Torvalds 		prop = sym_get_range_prop(sym);
5741da177e4SLinus Torvalds 		if (!prop)
5751da177e4SLinus Torvalds 			return true;
5761da177e4SLinus Torvalds 		val = strtol(str, NULL, 16);
5774cf3cbe2SRoman Zippel 		return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
5784cf3cbe2SRoman Zippel 		       val <= sym_get_range_val(prop->expr->right.sym, 16);
5791da177e4SLinus Torvalds 	case S_BOOLEAN:
5801da177e4SLinus Torvalds 	case S_TRISTATE:
5811da177e4SLinus Torvalds 		switch (str[0]) {
5821da177e4SLinus Torvalds 		case 'y': case 'Y':
5831da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, yes);
5841da177e4SLinus Torvalds 		case 'm': case 'M':
5851da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, mod);
5861da177e4SLinus Torvalds 		case 'n': case 'N':
5871da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, no);
5881da177e4SLinus Torvalds 		}
5891da177e4SLinus Torvalds 		return false;
5901da177e4SLinus Torvalds 	default:
5911da177e4SLinus Torvalds 		return false;
5921da177e4SLinus Torvalds 	}
5931da177e4SLinus Torvalds }
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds bool sym_set_string_value(struct symbol *sym, const char *newval)
5961da177e4SLinus Torvalds {
5971da177e4SLinus Torvalds 	const char *oldval;
5981da177e4SLinus Torvalds 	char *val;
5991da177e4SLinus Torvalds 	int size;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	switch (sym->type) {
6021da177e4SLinus Torvalds 	case S_BOOLEAN:
6031da177e4SLinus Torvalds 	case S_TRISTATE:
6041da177e4SLinus Torvalds 		switch (newval[0]) {
6051da177e4SLinus Torvalds 		case 'y': case 'Y':
6061da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, yes);
6071da177e4SLinus Torvalds 		case 'm': case 'M':
6081da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, mod);
6091da177e4SLinus Torvalds 		case 'n': case 'N':
6101da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, no);
6111da177e4SLinus Torvalds 		}
6121da177e4SLinus Torvalds 		return false;
6131da177e4SLinus Torvalds 	default:
6141da177e4SLinus Torvalds 		;
6151da177e4SLinus Torvalds 	}
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	if (!sym_string_within_range(sym, newval))
6181da177e4SLinus Torvalds 		return false;
6191da177e4SLinus Torvalds 
620669bfad9SRoman Zippel 	if (!(sym->flags & SYMBOL_DEF_USER)) {
621669bfad9SRoman Zippel 		sym->flags |= SYMBOL_DEF_USER;
6221da177e4SLinus Torvalds 		sym_set_changed(sym);
6231da177e4SLinus Torvalds 	}
6241da177e4SLinus Torvalds 
6250c1822e6SRoman Zippel 	oldval = sym->def[S_DEF_USER].val;
6261da177e4SLinus Torvalds 	size = strlen(newval) + 1;
6271da177e4SLinus Torvalds 	if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
6281da177e4SLinus Torvalds 		size += 2;
6290c1822e6SRoman Zippel 		sym->def[S_DEF_USER].val = val = malloc(size);
6301da177e4SLinus Torvalds 		*val++ = '0';
6311da177e4SLinus Torvalds 		*val++ = 'x';
6321da177e4SLinus Torvalds 	} else if (!oldval || strcmp(oldval, newval))
6330c1822e6SRoman Zippel 		sym->def[S_DEF_USER].val = val = malloc(size);
6341da177e4SLinus Torvalds 	else
6351da177e4SLinus Torvalds 		return true;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	strcpy(val, newval);
6381da177e4SLinus Torvalds 	free((void *)oldval);
6391da177e4SLinus Torvalds 	sym_clear_all_valid();
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	return true;
6421da177e4SLinus Torvalds }
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds const char *sym_get_string_value(struct symbol *sym)
6451da177e4SLinus Torvalds {
6461da177e4SLinus Torvalds 	tristate val;
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	switch (sym->type) {
6491da177e4SLinus Torvalds 	case S_BOOLEAN:
6501da177e4SLinus Torvalds 	case S_TRISTATE:
6511da177e4SLinus Torvalds 		val = sym_get_tristate_value(sym);
6521da177e4SLinus Torvalds 		switch (val) {
6531da177e4SLinus Torvalds 		case no:
6541da177e4SLinus Torvalds 			return "n";
6551da177e4SLinus Torvalds 		case mod:
6561da177e4SLinus Torvalds 			return "m";
6571da177e4SLinus Torvalds 		case yes:
6581da177e4SLinus Torvalds 			return "y";
6591da177e4SLinus Torvalds 		}
6601da177e4SLinus Torvalds 		break;
6611da177e4SLinus Torvalds 	default:
6621da177e4SLinus Torvalds 		;
6631da177e4SLinus Torvalds 	}
6641da177e4SLinus Torvalds 	return (const char *)sym->curr.val;
6651da177e4SLinus Torvalds }
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds bool sym_is_changable(struct symbol *sym)
6681da177e4SLinus Torvalds {
6691da177e4SLinus Torvalds 	return sym->visible > sym->rev_dep.tri;
6701da177e4SLinus Torvalds }
6711da177e4SLinus Torvalds 
672e66f25d7SAndi Kleen static unsigned strhash(const char *s)
673e66f25d7SAndi Kleen {
674e66f25d7SAndi Kleen 	/* fnv32 hash */
675e66f25d7SAndi Kleen 	unsigned hash = 2166136261U;
676e66f25d7SAndi Kleen 	for (; *s; s++)
677e66f25d7SAndi Kleen 		hash = (hash ^ *s) * 0x01000193;
678e66f25d7SAndi Kleen 	return hash;
679e66f25d7SAndi Kleen }
680e66f25d7SAndi Kleen 
6815a1aa8a1SRoman Zippel struct symbol *sym_lookup(const char *name, int flags)
6821da177e4SLinus Torvalds {
6831da177e4SLinus Torvalds 	struct symbol *symbol;
6841da177e4SLinus Torvalds 	char *new_name;
685e66f25d7SAndi Kleen 	int hash;
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	if (name) {
6881da177e4SLinus Torvalds 		if (name[0] && !name[1]) {
6891da177e4SLinus Torvalds 			switch (name[0]) {
6901da177e4SLinus Torvalds 			case 'y': return &symbol_yes;
6911da177e4SLinus Torvalds 			case 'm': return &symbol_mod;
6921da177e4SLinus Torvalds 			case 'n': return &symbol_no;
6931da177e4SLinus Torvalds 			}
6941da177e4SLinus Torvalds 		}
695e66f25d7SAndi Kleen 		hash = strhash(name) % SYMBOL_HASHSIZE;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 		for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
698e66f25d7SAndi Kleen 			if (symbol->name &&
699e66f25d7SAndi Kleen 			    !strcmp(symbol->name, name) &&
7005a1aa8a1SRoman Zippel 			    (flags ? symbol->flags & flags
7015a1aa8a1SRoman Zippel 				   : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
7021da177e4SLinus Torvalds 				return symbol;
7031da177e4SLinus Torvalds 		}
7041da177e4SLinus Torvalds 		new_name = strdup(name);
7051da177e4SLinus Torvalds 	} else {
7061da177e4SLinus Torvalds 		new_name = NULL;
707e66f25d7SAndi Kleen 		hash = 0;
7081da177e4SLinus Torvalds 	}
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	symbol = malloc(sizeof(*symbol));
7111da177e4SLinus Torvalds 	memset(symbol, 0, sizeof(*symbol));
7121da177e4SLinus Torvalds 	symbol->name = new_name;
7131da177e4SLinus Torvalds 	symbol->type = S_UNKNOWN;
7145a1aa8a1SRoman Zippel 	symbol->flags |= flags;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	symbol->next = symbol_hash[hash];
7171da177e4SLinus Torvalds 	symbol_hash[hash] = symbol;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 	return symbol;
7201da177e4SLinus Torvalds }
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds struct symbol *sym_find(const char *name)
7231da177e4SLinus Torvalds {
7241da177e4SLinus Torvalds 	struct symbol *symbol = NULL;
7251da177e4SLinus Torvalds 	int hash = 0;
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	if (!name)
7281da177e4SLinus Torvalds 		return NULL;
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	if (name[0] && !name[1]) {
7311da177e4SLinus Torvalds 		switch (name[0]) {
7321da177e4SLinus Torvalds 		case 'y': return &symbol_yes;
7331da177e4SLinus Torvalds 		case 'm': return &symbol_mod;
7341da177e4SLinus Torvalds 		case 'n': return &symbol_no;
7351da177e4SLinus Torvalds 		}
7361da177e4SLinus Torvalds 	}
737e66f25d7SAndi Kleen 	hash = strhash(name) % SYMBOL_HASHSIZE;
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
740e66f25d7SAndi Kleen 		if (symbol->name &&
741e66f25d7SAndi Kleen 		    !strcmp(symbol->name, name) &&
7421da177e4SLinus Torvalds 		    !(symbol->flags & SYMBOL_CONST))
7431da177e4SLinus Torvalds 				break;
7441da177e4SLinus Torvalds 	}
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	return symbol;
7471da177e4SLinus Torvalds }
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds struct symbol **sym_re_search(const char *pattern)
7501da177e4SLinus Torvalds {
7511da177e4SLinus Torvalds 	struct symbol *sym, **sym_arr = NULL;
7521da177e4SLinus Torvalds 	int i, cnt, size;
7531da177e4SLinus Torvalds 	regex_t re;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	cnt = size = 0;
7561da177e4SLinus Torvalds 	/* Skip if empty */
7571da177e4SLinus Torvalds 	if (strlen(pattern) == 0)
7581da177e4SLinus Torvalds 		return NULL;
7591da177e4SLinus Torvalds 	if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
7601da177e4SLinus Torvalds 		return NULL;
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
7631da177e4SLinus Torvalds 		if (sym->flags & SYMBOL_CONST || !sym->name)
7641da177e4SLinus Torvalds 			continue;
7651da177e4SLinus Torvalds 		if (regexec(&re, sym->name, 0, NULL, 0))
7661da177e4SLinus Torvalds 			continue;
7671da177e4SLinus Torvalds 		if (cnt + 1 >= size) {
7681da177e4SLinus Torvalds 			void *tmp = sym_arr;
7691da177e4SLinus Torvalds 			size += 16;
7701da177e4SLinus Torvalds 			sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
7711da177e4SLinus Torvalds 			if (!sym_arr) {
7721da177e4SLinus Torvalds 				free(tmp);
7731da177e4SLinus Torvalds 				return NULL;
7741da177e4SLinus Torvalds 			}
7751da177e4SLinus Torvalds 		}
776da6df879SLi Zefan 		sym_calc_value(sym);
7771da177e4SLinus Torvalds 		sym_arr[cnt++] = sym;
7781da177e4SLinus Torvalds 	}
7791da177e4SLinus Torvalds 	if (sym_arr)
7801da177e4SLinus Torvalds 		sym_arr[cnt] = NULL;
7811da177e4SLinus Torvalds 	regfree(&re);
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	return sym_arr;
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
786*d595cea6SRoman Zippel /*
787*d595cea6SRoman Zippel  * When we check for recursive dependencies we use a stack to save
788*d595cea6SRoman Zippel  * current state so we can print out relevant info to user.
789*d595cea6SRoman Zippel  * The entries are located on the call stack so no need to free memory.
790*d595cea6SRoman Zippel  * Note inser() remove() must always match to properly clear the stack.
791*d595cea6SRoman Zippel  */
792*d595cea6SRoman Zippel static struct dep_stack {
793*d595cea6SRoman Zippel 	struct dep_stack *prev, *next;
794*d595cea6SRoman Zippel 	struct symbol *sym;
795*d595cea6SRoman Zippel 	struct property *prop;
796*d595cea6SRoman Zippel 	struct expr *expr;
797*d595cea6SRoman Zippel } *check_top;
798*d595cea6SRoman Zippel 
799*d595cea6SRoman Zippel static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
800*d595cea6SRoman Zippel {
801*d595cea6SRoman Zippel 	memset(stack, 0, sizeof(*stack));
802*d595cea6SRoman Zippel 	if (check_top)
803*d595cea6SRoman Zippel 		check_top->next = stack;
804*d595cea6SRoman Zippel 	stack->prev = check_top;
805*d595cea6SRoman Zippel 	stack->sym = sym;
806*d595cea6SRoman Zippel 	check_top = stack;
807*d595cea6SRoman Zippel }
808*d595cea6SRoman Zippel 
809*d595cea6SRoman Zippel static void dep_stack_remove(void)
810*d595cea6SRoman Zippel {
811*d595cea6SRoman Zippel 	check_top = check_top->prev;
812*d595cea6SRoman Zippel 	if (check_top)
813*d595cea6SRoman Zippel 		check_top->next = NULL;
814*d595cea6SRoman Zippel }
815*d595cea6SRoman Zippel 
816*d595cea6SRoman Zippel /*
817*d595cea6SRoman Zippel  * Called when we have detected a recursive dependency.
818*d595cea6SRoman Zippel  * check_top point to the top of the stact so we use
819*d595cea6SRoman Zippel  * the ->prev pointer to locate the bottom of the stack.
820*d595cea6SRoman Zippel  */
821*d595cea6SRoman Zippel static void sym_check_print_recursive(struct symbol *last_sym)
822*d595cea6SRoman Zippel {
823*d595cea6SRoman Zippel 	struct dep_stack *stack;
824*d595cea6SRoman Zippel 	struct symbol *sym, *next_sym;
825*d595cea6SRoman Zippel 	struct menu *menu = NULL;
826*d595cea6SRoman Zippel 	struct property *prop;
827*d595cea6SRoman Zippel 	struct dep_stack cv_stack;
828*d595cea6SRoman Zippel 
829*d595cea6SRoman Zippel 	if (sym_is_choice_value(last_sym)) {
830*d595cea6SRoman Zippel 		dep_stack_insert(&cv_stack, last_sym);
831*d595cea6SRoman Zippel 		last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
832*d595cea6SRoman Zippel 	}
833*d595cea6SRoman Zippel 
834*d595cea6SRoman Zippel 	for (stack = check_top; stack != NULL; stack = stack->prev)
835*d595cea6SRoman Zippel 		if (stack->sym == last_sym)
836*d595cea6SRoman Zippel 			break;
837*d595cea6SRoman Zippel 	if (!stack) {
838*d595cea6SRoman Zippel 		fprintf(stderr, "unexpected recursive dependency error\n");
839*d595cea6SRoman Zippel 		return;
840*d595cea6SRoman Zippel 	}
841*d595cea6SRoman Zippel 
842*d595cea6SRoman Zippel 	for (; stack; stack = stack->next) {
843*d595cea6SRoman Zippel 		sym = stack->sym;
844*d595cea6SRoman Zippel 		next_sym = stack->next ? stack->next->sym : last_sym;
845*d595cea6SRoman Zippel 		prop = stack->prop;
846*d595cea6SRoman Zippel 
847*d595cea6SRoman Zippel 		/* for choice values find the menu entry (used below) */
848*d595cea6SRoman Zippel 		if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
849*d595cea6SRoman Zippel 			for (prop = sym->prop; prop; prop = prop->next) {
850*d595cea6SRoman Zippel 				menu = prop->menu;
851*d595cea6SRoman Zippel 				if (prop->menu)
852*d595cea6SRoman Zippel 					break;
853*d595cea6SRoman Zippel 			}
854*d595cea6SRoman Zippel 		}
855*d595cea6SRoman Zippel 		if (stack->sym == last_sym)
856*d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
857*d595cea6SRoman Zippel 				prop->file->name, prop->lineno);
858*d595cea6SRoman Zippel 		if (stack->expr) {
859*d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
860*d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
861*d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
862*d595cea6SRoman Zippel 				prop_get_type_name(prop->type),
863*d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
864*d595cea6SRoman Zippel 		} else if (stack->prop) {
865*d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
866*d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
867*d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
868*d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
869*d595cea6SRoman Zippel 		} else if (sym_is_choice(sym)) {
870*d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
871*d595cea6SRoman Zippel 				menu->file->name, menu->lineno,
872*d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
873*d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
874*d595cea6SRoman Zippel 		} else if (sym_is_choice_value(sym)) {
875*d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
876*d595cea6SRoman Zippel 				menu->file->name, menu->lineno,
877*d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
878*d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
879*d595cea6SRoman Zippel 		} else {
880*d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
881*d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
882*d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
883*d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
884*d595cea6SRoman Zippel 		}
885*d595cea6SRoman Zippel 	}
886*d595cea6SRoman Zippel 
887*d595cea6SRoman Zippel 	if (check_top == &cv_stack)
888*d595cea6SRoman Zippel 		dep_stack_remove();
889*d595cea6SRoman Zippel }
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds static struct symbol *sym_check_expr_deps(struct expr *e)
8921da177e4SLinus Torvalds {
8931da177e4SLinus Torvalds 	struct symbol *sym;
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	if (!e)
8961da177e4SLinus Torvalds 		return NULL;
8971da177e4SLinus Torvalds 	switch (e->type) {
8981da177e4SLinus Torvalds 	case E_OR:
8991da177e4SLinus Torvalds 	case E_AND:
9001da177e4SLinus Torvalds 		sym = sym_check_expr_deps(e->left.expr);
9011da177e4SLinus Torvalds 		if (sym)
9021da177e4SLinus Torvalds 			return sym;
9031da177e4SLinus Torvalds 		return sym_check_expr_deps(e->right.expr);
9041da177e4SLinus Torvalds 	case E_NOT:
9051da177e4SLinus Torvalds 		return sym_check_expr_deps(e->left.expr);
9061da177e4SLinus Torvalds 	case E_EQUAL:
9071da177e4SLinus Torvalds 	case E_UNEQUAL:
9081da177e4SLinus Torvalds 		sym = sym_check_deps(e->left.sym);
9091da177e4SLinus Torvalds 		if (sym)
9101da177e4SLinus Torvalds 			return sym;
9111da177e4SLinus Torvalds 		return sym_check_deps(e->right.sym);
9121da177e4SLinus Torvalds 	case E_SYMBOL:
9131da177e4SLinus Torvalds 		return sym_check_deps(e->left.sym);
9141da177e4SLinus Torvalds 	default:
9151da177e4SLinus Torvalds 		break;
9161da177e4SLinus Torvalds 	}
9171da177e4SLinus Torvalds 	printf("Oops! How to check %d?\n", e->type);
9181da177e4SLinus Torvalds 	return NULL;
9191da177e4SLinus Torvalds }
9201da177e4SLinus Torvalds 
9215447d34bSSam Ravnborg /* return NULL when dependencies are OK */
92248981178SRoman Zippel static struct symbol *sym_check_sym_deps(struct symbol *sym)
92348981178SRoman Zippel {
92448981178SRoman Zippel 	struct symbol *sym2;
92548981178SRoman Zippel 	struct property *prop;
926*d595cea6SRoman Zippel 	struct dep_stack stack;
927*d595cea6SRoman Zippel 
928*d595cea6SRoman Zippel 	dep_stack_insert(&stack, sym);
92948981178SRoman Zippel 
93048981178SRoman Zippel 	sym2 = sym_check_expr_deps(sym->rev_dep.expr);
93148981178SRoman Zippel 	if (sym2)
932*d595cea6SRoman Zippel 		goto out;
93348981178SRoman Zippel 
93448981178SRoman Zippel 	for (prop = sym->prop; prop; prop = prop->next) {
93548981178SRoman Zippel 		if (prop->type == P_CHOICE || prop->type == P_SELECT)
93648981178SRoman Zippel 			continue;
937*d595cea6SRoman Zippel 		stack.prop = prop;
93848981178SRoman Zippel 		sym2 = sym_check_expr_deps(prop->visible.expr);
93948981178SRoman Zippel 		if (sym2)
94048981178SRoman Zippel 			break;
94148981178SRoman Zippel 		if (prop->type != P_DEFAULT || sym_is_choice(sym))
94248981178SRoman Zippel 			continue;
943*d595cea6SRoman Zippel 		stack.expr = prop->expr;
94448981178SRoman Zippel 		sym2 = sym_check_expr_deps(prop->expr);
94548981178SRoman Zippel 		if (sym2)
94648981178SRoman Zippel 			break;
947*d595cea6SRoman Zippel 		stack.expr = NULL;
94848981178SRoman Zippel 	}
94948981178SRoman Zippel 
950*d595cea6SRoman Zippel out:
951*d595cea6SRoman Zippel 	dep_stack_remove();
952*d595cea6SRoman Zippel 
95348981178SRoman Zippel 	return sym2;
95448981178SRoman Zippel }
95548981178SRoman Zippel 
95648981178SRoman Zippel static struct symbol *sym_check_choice_deps(struct symbol *choice)
95748981178SRoman Zippel {
95848981178SRoman Zippel 	struct symbol *sym, *sym2;
95948981178SRoman Zippel 	struct property *prop;
96048981178SRoman Zippel 	struct expr *e;
961*d595cea6SRoman Zippel 	struct dep_stack stack;
962*d595cea6SRoman Zippel 
963*d595cea6SRoman Zippel 	dep_stack_insert(&stack, choice);
96448981178SRoman Zippel 
96548981178SRoman Zippel 	prop = sym_get_choice_prop(choice);
96648981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym)
96748981178SRoman Zippel 		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
96848981178SRoman Zippel 
96948981178SRoman Zippel 	choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
97048981178SRoman Zippel 	sym2 = sym_check_sym_deps(choice);
97148981178SRoman Zippel 	choice->flags &= ~SYMBOL_CHECK;
97248981178SRoman Zippel 	if (sym2)
97348981178SRoman Zippel 		goto out;
97448981178SRoman Zippel 
97548981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym) {
97648981178SRoman Zippel 		sym2 = sym_check_sym_deps(sym);
977*d595cea6SRoman Zippel 		if (sym2)
97848981178SRoman Zippel 			break;
97948981178SRoman Zippel 	}
98048981178SRoman Zippel out:
98148981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym)
98248981178SRoman Zippel 		sym->flags &= ~SYMBOL_CHECK;
98348981178SRoman Zippel 
98448981178SRoman Zippel 	if (sym2 && sym_is_choice_value(sym2) &&
98548981178SRoman Zippel 	    prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
98648981178SRoman Zippel 		sym2 = choice;
98748981178SRoman Zippel 
988*d595cea6SRoman Zippel 	dep_stack_remove();
989*d595cea6SRoman Zippel 
99048981178SRoman Zippel 	return sym2;
99148981178SRoman Zippel }
99248981178SRoman Zippel 
9931da177e4SLinus Torvalds struct symbol *sym_check_deps(struct symbol *sym)
9941da177e4SLinus Torvalds {
9951da177e4SLinus Torvalds 	struct symbol *sym2;
9961da177e4SLinus Torvalds 	struct property *prop;
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 	if (sym->flags & SYMBOL_CHECK) {
999*d595cea6SRoman Zippel 		sym_check_print_recursive(sym);
10001da177e4SLinus Torvalds 		return sym;
10011da177e4SLinus Torvalds 	}
10023f04e7ddSDavid Gibson 	if (sym->flags & SYMBOL_CHECKED)
10033f04e7ddSDavid Gibson 		return NULL;
10041da177e4SLinus Torvalds 
100548981178SRoman Zippel 	if (sym_is_choice_value(sym)) {
1006*d595cea6SRoman Zippel 		struct dep_stack stack;
1007*d595cea6SRoman Zippel 
100848981178SRoman Zippel 		/* for choice groups start the check with main choice symbol */
1009*d595cea6SRoman Zippel 		dep_stack_insert(&stack, sym);
101048981178SRoman Zippel 		prop = sym_get_choice_prop(sym);
101148981178SRoman Zippel 		sym2 = sym_check_deps(prop_get_symbol(prop));
1012*d595cea6SRoman Zippel 		dep_stack_remove();
101348981178SRoman Zippel 	} else if (sym_is_choice(sym)) {
101448981178SRoman Zippel 		sym2 = sym_check_choice_deps(sym);
101548981178SRoman Zippel 	} else {
10161da177e4SLinus Torvalds 		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
101748981178SRoman Zippel 		sym2 = sym_check_sym_deps(sym);
10181da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_CHECK;
101948981178SRoman Zippel 	}
102048981178SRoman Zippel 
1021*d595cea6SRoman Zippel 	if (sym2 && sym2 == sym)
102248981178SRoman Zippel 		sym2 = NULL;
102348981178SRoman Zippel 
10241da177e4SLinus Torvalds 	return sym2;
10251da177e4SLinus Torvalds }
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds struct property *prop_alloc(enum prop_type type, struct symbol *sym)
10281da177e4SLinus Torvalds {
10291da177e4SLinus Torvalds 	struct property *prop;
10301da177e4SLinus Torvalds 	struct property **propp;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	prop = malloc(sizeof(*prop));
10331da177e4SLinus Torvalds 	memset(prop, 0, sizeof(*prop));
10341da177e4SLinus Torvalds 	prop->type = type;
10351da177e4SLinus Torvalds 	prop->sym = sym;
10361da177e4SLinus Torvalds 	prop->file = current_file;
10371da177e4SLinus Torvalds 	prop->lineno = zconf_lineno();
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	/* append property to the prop list of symbol */
10401da177e4SLinus Torvalds 	if (sym) {
10411da177e4SLinus Torvalds 		for (propp = &sym->prop; *propp; propp = &(*propp)->next)
10421da177e4SLinus Torvalds 			;
10431da177e4SLinus Torvalds 		*propp = prop;
10441da177e4SLinus Torvalds 	}
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 	return prop;
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds struct symbol *prop_get_symbol(struct property *prop)
10501da177e4SLinus Torvalds {
10511da177e4SLinus Torvalds 	if (prop->expr && (prop->expr->type == E_SYMBOL ||
10527a962923SRoman Zippel 			   prop->expr->type == E_LIST))
10531da177e4SLinus Torvalds 		return prop->expr->left.sym;
10541da177e4SLinus Torvalds 	return NULL;
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds const char *prop_get_type_name(enum prop_type type)
10581da177e4SLinus Torvalds {
10591da177e4SLinus Torvalds 	switch (type) {
10601da177e4SLinus Torvalds 	case P_PROMPT:
10611da177e4SLinus Torvalds 		return "prompt";
106293449082SRoman Zippel 	case P_ENV:
106393449082SRoman Zippel 		return "env";
10641da177e4SLinus Torvalds 	case P_COMMENT:
10651da177e4SLinus Torvalds 		return "comment";
10661da177e4SLinus Torvalds 	case P_MENU:
10671da177e4SLinus Torvalds 		return "menu";
10681da177e4SLinus Torvalds 	case P_DEFAULT:
10691da177e4SLinus Torvalds 		return "default";
10701da177e4SLinus Torvalds 	case P_CHOICE:
10711da177e4SLinus Torvalds 		return "choice";
10721da177e4SLinus Torvalds 	case P_SELECT:
10731da177e4SLinus Torvalds 		return "select";
10741da177e4SLinus Torvalds 	case P_RANGE:
10751da177e4SLinus Torvalds 		return "range";
107659e89e3dSSam Ravnborg 	case P_SYMBOL:
107759e89e3dSSam Ravnborg 		return "symbol";
10781da177e4SLinus Torvalds 	case P_UNKNOWN:
10791da177e4SLinus Torvalds 		break;
10801da177e4SLinus Torvalds 	}
10811da177e4SLinus Torvalds 	return "unknown";
10821da177e4SLinus Torvalds }
108393449082SRoman Zippel 
10844356f489STrevor Keith static void prop_add_env(const char *env)
108593449082SRoman Zippel {
108693449082SRoman Zippel 	struct symbol *sym, *sym2;
108793449082SRoman Zippel 	struct property *prop;
108893449082SRoman Zippel 	char *p;
108993449082SRoman Zippel 
109093449082SRoman Zippel 	sym = current_entry->sym;
109193449082SRoman Zippel 	sym->flags |= SYMBOL_AUTO;
109293449082SRoman Zippel 	for_all_properties(sym, prop, P_ENV) {
109393449082SRoman Zippel 		sym2 = prop_get_symbol(prop);
109493449082SRoman Zippel 		if (strcmp(sym2->name, env))
109593449082SRoman Zippel 			menu_warn(current_entry, "redefining environment symbol from %s",
109693449082SRoman Zippel 				  sym2->name);
109793449082SRoman Zippel 		return;
109893449082SRoman Zippel 	}
109993449082SRoman Zippel 
110093449082SRoman Zippel 	prop = prop_alloc(P_ENV, sym);
11015a1aa8a1SRoman Zippel 	prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
110293449082SRoman Zippel 
110393449082SRoman Zippel 	sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
110493449082SRoman Zippel 	sym_env_list->right.sym = sym;
110593449082SRoman Zippel 
110693449082SRoman Zippel 	p = getenv(env);
110793449082SRoman Zippel 	if (p)
110893449082SRoman Zippel 		sym_add_default(sym, p);
110993449082SRoman Zippel 	else
111093449082SRoman Zippel 		menu_warn(current_entry, "environment variable %s undefined", env);
111193449082SRoman Zippel }
1112