xref: /openbmc/linux/scripts/kconfig/symbol.c (revision c252147de68cf58ba601278481e473dab432cee4)
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 
229*c252147dSSam Ravnborg /*
230*c252147dSSam Ravnborg  * Find the default symbol for a choice.
231*c252147dSSam Ravnborg  * First try the default values for the choice symbol
232*c252147dSSam Ravnborg  * Next locate the first visible choice value
233*c252147dSSam Ravnborg  * Return NULL if none was found
234*c252147dSSam Ravnborg  */
235*c252147dSSam 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 
257*c252147dSSam Ravnborg 	/* failed to locate any defaults */
258*c252147dSSam Ravnborg 	return NULL;
259*c252147dSSam Ravnborg }
260*c252147dSSam Ravnborg 
261*c252147dSSam Ravnborg static struct symbol *sym_calc_choice(struct symbol *sym)
262*c252147dSSam Ravnborg {
263*c252147dSSam Ravnborg 	struct symbol *def_sym;
264*c252147dSSam Ravnborg 	struct property *prop;
265*c252147dSSam Ravnborg 	struct expr *e;
266*c252147dSSam Ravnborg 
267*c252147dSSam Ravnborg 	/* first calculate all choice values' visibilities */
268*c252147dSSam Ravnborg 	prop = sym_get_choice_prop(sym);
269*c252147dSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, def_sym)
270*c252147dSSam Ravnborg 		sym_calc_visibility(def_sym);
271*c252147dSSam Ravnborg 
272*c252147dSSam Ravnborg 	/* is the user choice visible? */
273*c252147dSSam Ravnborg 	def_sym = sym->def[S_DEF_USER].val;
274*c252147dSSam Ravnborg 	if (def_sym && def_sym->visible != no)
275*c252147dSSam Ravnborg 		return def_sym;
276*c252147dSSam Ravnborg 
277*c252147dSSam Ravnborg 	def_sym = sym_choice_default(sym);
278*c252147dSSam Ravnborg 
279*c252147dSSam Ravnborg 	if (def_sym == NULL)
2801da177e4SLinus Torvalds 		/* no choice? reset tristate value */
2811da177e4SLinus Torvalds 		sym->curr.tri = no;
282*c252147dSSam Ravnborg 
283*c252147dSSam 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 
6641da177e4SLinus Torvalds const char *sym_get_string_value(struct symbol *sym)
6651da177e4SLinus Torvalds {
6661da177e4SLinus Torvalds 	tristate val;
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	switch (sym->type) {
6691da177e4SLinus Torvalds 	case S_BOOLEAN:
6701da177e4SLinus Torvalds 	case S_TRISTATE:
6711da177e4SLinus Torvalds 		val = sym_get_tristate_value(sym);
6721da177e4SLinus Torvalds 		switch (val) {
6731da177e4SLinus Torvalds 		case no:
6741da177e4SLinus Torvalds 			return "n";
6751da177e4SLinus Torvalds 		case mod:
6761da177e4SLinus Torvalds 			return "m";
6771da177e4SLinus Torvalds 		case yes:
6781da177e4SLinus Torvalds 			return "y";
6791da177e4SLinus Torvalds 		}
6801da177e4SLinus Torvalds 		break;
6811da177e4SLinus Torvalds 	default:
6821da177e4SLinus Torvalds 		;
6831da177e4SLinus Torvalds 	}
6841da177e4SLinus Torvalds 	return (const char *)sym->curr.val;
6851da177e4SLinus Torvalds }
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds bool sym_is_changable(struct symbol *sym)
6881da177e4SLinus Torvalds {
6891da177e4SLinus Torvalds 	return sym->visible > sym->rev_dep.tri;
6901da177e4SLinus Torvalds }
6911da177e4SLinus Torvalds 
692e66f25d7SAndi Kleen static unsigned strhash(const char *s)
693e66f25d7SAndi Kleen {
694e66f25d7SAndi Kleen 	/* fnv32 hash */
695e66f25d7SAndi Kleen 	unsigned hash = 2166136261U;
696e66f25d7SAndi Kleen 	for (; *s; s++)
697e66f25d7SAndi Kleen 		hash = (hash ^ *s) * 0x01000193;
698e66f25d7SAndi Kleen 	return hash;
699e66f25d7SAndi Kleen }
700e66f25d7SAndi Kleen 
7015a1aa8a1SRoman Zippel struct symbol *sym_lookup(const char *name, int flags)
7021da177e4SLinus Torvalds {
7031da177e4SLinus Torvalds 	struct symbol *symbol;
7041da177e4SLinus Torvalds 	char *new_name;
705e66f25d7SAndi Kleen 	int hash;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	if (name) {
7081da177e4SLinus Torvalds 		if (name[0] && !name[1]) {
7091da177e4SLinus Torvalds 			switch (name[0]) {
7101da177e4SLinus Torvalds 			case 'y': return &symbol_yes;
7111da177e4SLinus Torvalds 			case 'm': return &symbol_mod;
7121da177e4SLinus Torvalds 			case 'n': return &symbol_no;
7131da177e4SLinus Torvalds 			}
7141da177e4SLinus Torvalds 		}
715e66f25d7SAndi Kleen 		hash = strhash(name) % SYMBOL_HASHSIZE;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 		for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
718e66f25d7SAndi Kleen 			if (symbol->name &&
719e66f25d7SAndi Kleen 			    !strcmp(symbol->name, name) &&
7205a1aa8a1SRoman Zippel 			    (flags ? symbol->flags & flags
7215a1aa8a1SRoman Zippel 				   : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
7221da177e4SLinus Torvalds 				return symbol;
7231da177e4SLinus Torvalds 		}
7241da177e4SLinus Torvalds 		new_name = strdup(name);
7251da177e4SLinus Torvalds 	} else {
7261da177e4SLinus Torvalds 		new_name = NULL;
727e66f25d7SAndi Kleen 		hash = 0;
7281da177e4SLinus Torvalds 	}
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	symbol = malloc(sizeof(*symbol));
7311da177e4SLinus Torvalds 	memset(symbol, 0, sizeof(*symbol));
7321da177e4SLinus Torvalds 	symbol->name = new_name;
7331da177e4SLinus Torvalds 	symbol->type = S_UNKNOWN;
7345a1aa8a1SRoman Zippel 	symbol->flags |= flags;
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	symbol->next = symbol_hash[hash];
7371da177e4SLinus Torvalds 	symbol_hash[hash] = symbol;
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	return symbol;
7401da177e4SLinus Torvalds }
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds struct symbol *sym_find(const char *name)
7431da177e4SLinus Torvalds {
7441da177e4SLinus Torvalds 	struct symbol *symbol = NULL;
7451da177e4SLinus Torvalds 	int hash = 0;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	if (!name)
7481da177e4SLinus Torvalds 		return NULL;
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	if (name[0] && !name[1]) {
7511da177e4SLinus Torvalds 		switch (name[0]) {
7521da177e4SLinus Torvalds 		case 'y': return &symbol_yes;
7531da177e4SLinus Torvalds 		case 'm': return &symbol_mod;
7541da177e4SLinus Torvalds 		case 'n': return &symbol_no;
7551da177e4SLinus Torvalds 		}
7561da177e4SLinus Torvalds 	}
757e66f25d7SAndi Kleen 	hash = strhash(name) % SYMBOL_HASHSIZE;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
760e66f25d7SAndi Kleen 		if (symbol->name &&
761e66f25d7SAndi Kleen 		    !strcmp(symbol->name, name) &&
7621da177e4SLinus Torvalds 		    !(symbol->flags & SYMBOL_CONST))
7631da177e4SLinus Torvalds 				break;
7641da177e4SLinus Torvalds 	}
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 	return symbol;
7671da177e4SLinus Torvalds }
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds struct symbol **sym_re_search(const char *pattern)
7701da177e4SLinus Torvalds {
7711da177e4SLinus Torvalds 	struct symbol *sym, **sym_arr = NULL;
7721da177e4SLinus Torvalds 	int i, cnt, size;
7731da177e4SLinus Torvalds 	regex_t re;
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	cnt = size = 0;
7761da177e4SLinus Torvalds 	/* Skip if empty */
7771da177e4SLinus Torvalds 	if (strlen(pattern) == 0)
7781da177e4SLinus Torvalds 		return NULL;
7791da177e4SLinus Torvalds 	if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
7801da177e4SLinus Torvalds 		return NULL;
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
7831da177e4SLinus Torvalds 		if (sym->flags & SYMBOL_CONST || !sym->name)
7841da177e4SLinus Torvalds 			continue;
7851da177e4SLinus Torvalds 		if (regexec(&re, sym->name, 0, NULL, 0))
7861da177e4SLinus Torvalds 			continue;
7871da177e4SLinus Torvalds 		if (cnt + 1 >= size) {
7881da177e4SLinus Torvalds 			void *tmp = sym_arr;
7891da177e4SLinus Torvalds 			size += 16;
7901da177e4SLinus Torvalds 			sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
7911da177e4SLinus Torvalds 			if (!sym_arr) {
7921da177e4SLinus Torvalds 				free(tmp);
7931da177e4SLinus Torvalds 				return NULL;
7941da177e4SLinus Torvalds 			}
7951da177e4SLinus Torvalds 		}
796da6df879SLi Zefan 		sym_calc_value(sym);
7971da177e4SLinus Torvalds 		sym_arr[cnt++] = sym;
7981da177e4SLinus Torvalds 	}
7991da177e4SLinus Torvalds 	if (sym_arr)
8001da177e4SLinus Torvalds 		sym_arr[cnt] = NULL;
8011da177e4SLinus Torvalds 	regfree(&re);
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	return sym_arr;
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
806d595cea6SRoman Zippel /*
807d595cea6SRoman Zippel  * When we check for recursive dependencies we use a stack to save
808d595cea6SRoman Zippel  * current state so we can print out relevant info to user.
809d595cea6SRoman Zippel  * The entries are located on the call stack so no need to free memory.
810d595cea6SRoman Zippel  * Note inser() remove() must always match to properly clear the stack.
811d595cea6SRoman Zippel  */
812d595cea6SRoman Zippel static struct dep_stack {
813d595cea6SRoman Zippel 	struct dep_stack *prev, *next;
814d595cea6SRoman Zippel 	struct symbol *sym;
815d595cea6SRoman Zippel 	struct property *prop;
816d595cea6SRoman Zippel 	struct expr *expr;
817d595cea6SRoman Zippel } *check_top;
818d595cea6SRoman Zippel 
819d595cea6SRoman Zippel static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
820d595cea6SRoman Zippel {
821d595cea6SRoman Zippel 	memset(stack, 0, sizeof(*stack));
822d595cea6SRoman Zippel 	if (check_top)
823d595cea6SRoman Zippel 		check_top->next = stack;
824d595cea6SRoman Zippel 	stack->prev = check_top;
825d595cea6SRoman Zippel 	stack->sym = sym;
826d595cea6SRoman Zippel 	check_top = stack;
827d595cea6SRoman Zippel }
828d595cea6SRoman Zippel 
829d595cea6SRoman Zippel static void dep_stack_remove(void)
830d595cea6SRoman Zippel {
831d595cea6SRoman Zippel 	check_top = check_top->prev;
832d595cea6SRoman Zippel 	if (check_top)
833d595cea6SRoman Zippel 		check_top->next = NULL;
834d595cea6SRoman Zippel }
835d595cea6SRoman Zippel 
836d595cea6SRoman Zippel /*
837d595cea6SRoman Zippel  * Called when we have detected a recursive dependency.
838d595cea6SRoman Zippel  * check_top point to the top of the stact so we use
839d595cea6SRoman Zippel  * the ->prev pointer to locate the bottom of the stack.
840d595cea6SRoman Zippel  */
841d595cea6SRoman Zippel static void sym_check_print_recursive(struct symbol *last_sym)
842d595cea6SRoman Zippel {
843d595cea6SRoman Zippel 	struct dep_stack *stack;
844d595cea6SRoman Zippel 	struct symbol *sym, *next_sym;
845d595cea6SRoman Zippel 	struct menu *menu = NULL;
846d595cea6SRoman Zippel 	struct property *prop;
847d595cea6SRoman Zippel 	struct dep_stack cv_stack;
848d595cea6SRoman Zippel 
849d595cea6SRoman Zippel 	if (sym_is_choice_value(last_sym)) {
850d595cea6SRoman Zippel 		dep_stack_insert(&cv_stack, last_sym);
851d595cea6SRoman Zippel 		last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
852d595cea6SRoman Zippel 	}
853d595cea6SRoman Zippel 
854d595cea6SRoman Zippel 	for (stack = check_top; stack != NULL; stack = stack->prev)
855d595cea6SRoman Zippel 		if (stack->sym == last_sym)
856d595cea6SRoman Zippel 			break;
857d595cea6SRoman Zippel 	if (!stack) {
858d595cea6SRoman Zippel 		fprintf(stderr, "unexpected recursive dependency error\n");
859d595cea6SRoman Zippel 		return;
860d595cea6SRoman Zippel 	}
861d595cea6SRoman Zippel 
862d595cea6SRoman Zippel 	for (; stack; stack = stack->next) {
863d595cea6SRoman Zippel 		sym = stack->sym;
864d595cea6SRoman Zippel 		next_sym = stack->next ? stack->next->sym : last_sym;
865d595cea6SRoman Zippel 		prop = stack->prop;
866d595cea6SRoman Zippel 
867d595cea6SRoman Zippel 		/* for choice values find the menu entry (used below) */
868d595cea6SRoman Zippel 		if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
869d595cea6SRoman Zippel 			for (prop = sym->prop; prop; prop = prop->next) {
870d595cea6SRoman Zippel 				menu = prop->menu;
871d595cea6SRoman Zippel 				if (prop->menu)
872d595cea6SRoman Zippel 					break;
873d595cea6SRoman Zippel 			}
874d595cea6SRoman Zippel 		}
875d595cea6SRoman Zippel 		if (stack->sym == last_sym)
876d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
877d595cea6SRoman Zippel 				prop->file->name, prop->lineno);
878d595cea6SRoman Zippel 		if (stack->expr) {
879d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
880d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
881d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
882d595cea6SRoman Zippel 				prop_get_type_name(prop->type),
883d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
884d595cea6SRoman Zippel 		} else if (stack->prop) {
885d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
886d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
887d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
888d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
889d595cea6SRoman Zippel 		} else if (sym_is_choice(sym)) {
890d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
891d595cea6SRoman Zippel 				menu->file->name, menu->lineno,
892d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
893d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
894d595cea6SRoman Zippel 		} else if (sym_is_choice_value(sym)) {
895d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
896d595cea6SRoman Zippel 				menu->file->name, menu->lineno,
897d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
898d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
899d595cea6SRoman Zippel 		} else {
900d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
901d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
902d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
903d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
904d595cea6SRoman Zippel 		}
905d595cea6SRoman Zippel 	}
906d595cea6SRoman Zippel 
907d595cea6SRoman Zippel 	if (check_top == &cv_stack)
908d595cea6SRoman Zippel 		dep_stack_remove();
909d595cea6SRoman Zippel }
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds static struct symbol *sym_check_expr_deps(struct expr *e)
9121da177e4SLinus Torvalds {
9131da177e4SLinus Torvalds 	struct symbol *sym;
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	if (!e)
9161da177e4SLinus Torvalds 		return NULL;
9171da177e4SLinus Torvalds 	switch (e->type) {
9181da177e4SLinus Torvalds 	case E_OR:
9191da177e4SLinus Torvalds 	case E_AND:
9201da177e4SLinus Torvalds 		sym = sym_check_expr_deps(e->left.expr);
9211da177e4SLinus Torvalds 		if (sym)
9221da177e4SLinus Torvalds 			return sym;
9231da177e4SLinus Torvalds 		return sym_check_expr_deps(e->right.expr);
9241da177e4SLinus Torvalds 	case E_NOT:
9251da177e4SLinus Torvalds 		return sym_check_expr_deps(e->left.expr);
9261da177e4SLinus Torvalds 	case E_EQUAL:
9271da177e4SLinus Torvalds 	case E_UNEQUAL:
9281da177e4SLinus Torvalds 		sym = sym_check_deps(e->left.sym);
9291da177e4SLinus Torvalds 		if (sym)
9301da177e4SLinus Torvalds 			return sym;
9311da177e4SLinus Torvalds 		return sym_check_deps(e->right.sym);
9321da177e4SLinus Torvalds 	case E_SYMBOL:
9331da177e4SLinus Torvalds 		return sym_check_deps(e->left.sym);
9341da177e4SLinus Torvalds 	default:
9351da177e4SLinus Torvalds 		break;
9361da177e4SLinus Torvalds 	}
9371da177e4SLinus Torvalds 	printf("Oops! How to check %d?\n", e->type);
9381da177e4SLinus Torvalds 	return NULL;
9391da177e4SLinus Torvalds }
9401da177e4SLinus Torvalds 
9415447d34bSSam Ravnborg /* return NULL when dependencies are OK */
94248981178SRoman Zippel static struct symbol *sym_check_sym_deps(struct symbol *sym)
94348981178SRoman Zippel {
94448981178SRoman Zippel 	struct symbol *sym2;
94548981178SRoman Zippel 	struct property *prop;
946d595cea6SRoman Zippel 	struct dep_stack stack;
947d595cea6SRoman Zippel 
948d595cea6SRoman Zippel 	dep_stack_insert(&stack, sym);
94948981178SRoman Zippel 
95048981178SRoman Zippel 	sym2 = sym_check_expr_deps(sym->rev_dep.expr);
95148981178SRoman Zippel 	if (sym2)
952d595cea6SRoman Zippel 		goto out;
95348981178SRoman Zippel 
95448981178SRoman Zippel 	for (prop = sym->prop; prop; prop = prop->next) {
95548981178SRoman Zippel 		if (prop->type == P_CHOICE || prop->type == P_SELECT)
95648981178SRoman Zippel 			continue;
957d595cea6SRoman Zippel 		stack.prop = prop;
95848981178SRoman Zippel 		sym2 = sym_check_expr_deps(prop->visible.expr);
95948981178SRoman Zippel 		if (sym2)
96048981178SRoman Zippel 			break;
96148981178SRoman Zippel 		if (prop->type != P_DEFAULT || sym_is_choice(sym))
96248981178SRoman Zippel 			continue;
963d595cea6SRoman Zippel 		stack.expr = prop->expr;
96448981178SRoman Zippel 		sym2 = sym_check_expr_deps(prop->expr);
96548981178SRoman Zippel 		if (sym2)
96648981178SRoman Zippel 			break;
967d595cea6SRoman Zippel 		stack.expr = NULL;
96848981178SRoman Zippel 	}
96948981178SRoman Zippel 
970d595cea6SRoman Zippel out:
971d595cea6SRoman Zippel 	dep_stack_remove();
972d595cea6SRoman Zippel 
97348981178SRoman Zippel 	return sym2;
97448981178SRoman Zippel }
97548981178SRoman Zippel 
97648981178SRoman Zippel static struct symbol *sym_check_choice_deps(struct symbol *choice)
97748981178SRoman Zippel {
97848981178SRoman Zippel 	struct symbol *sym, *sym2;
97948981178SRoman Zippel 	struct property *prop;
98048981178SRoman Zippel 	struct expr *e;
981d595cea6SRoman Zippel 	struct dep_stack stack;
982d595cea6SRoman Zippel 
983d595cea6SRoman Zippel 	dep_stack_insert(&stack, choice);
98448981178SRoman Zippel 
98548981178SRoman Zippel 	prop = sym_get_choice_prop(choice);
98648981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym)
98748981178SRoman Zippel 		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
98848981178SRoman Zippel 
98948981178SRoman Zippel 	choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
99048981178SRoman Zippel 	sym2 = sym_check_sym_deps(choice);
99148981178SRoman Zippel 	choice->flags &= ~SYMBOL_CHECK;
99248981178SRoman Zippel 	if (sym2)
99348981178SRoman Zippel 		goto out;
99448981178SRoman Zippel 
99548981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym) {
99648981178SRoman Zippel 		sym2 = sym_check_sym_deps(sym);
997d595cea6SRoman Zippel 		if (sym2)
99848981178SRoman Zippel 			break;
99948981178SRoman Zippel 	}
100048981178SRoman Zippel out:
100148981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym)
100248981178SRoman Zippel 		sym->flags &= ~SYMBOL_CHECK;
100348981178SRoman Zippel 
100448981178SRoman Zippel 	if (sym2 && sym_is_choice_value(sym2) &&
100548981178SRoman Zippel 	    prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
100648981178SRoman Zippel 		sym2 = choice;
100748981178SRoman Zippel 
1008d595cea6SRoman Zippel 	dep_stack_remove();
1009d595cea6SRoman Zippel 
101048981178SRoman Zippel 	return sym2;
101148981178SRoman Zippel }
101248981178SRoman Zippel 
10131da177e4SLinus Torvalds struct symbol *sym_check_deps(struct symbol *sym)
10141da177e4SLinus Torvalds {
10151da177e4SLinus Torvalds 	struct symbol *sym2;
10161da177e4SLinus Torvalds 	struct property *prop;
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	if (sym->flags & SYMBOL_CHECK) {
1019d595cea6SRoman Zippel 		sym_check_print_recursive(sym);
10201da177e4SLinus Torvalds 		return sym;
10211da177e4SLinus Torvalds 	}
10223f04e7ddSDavid Gibson 	if (sym->flags & SYMBOL_CHECKED)
10233f04e7ddSDavid Gibson 		return NULL;
10241da177e4SLinus Torvalds 
102548981178SRoman Zippel 	if (sym_is_choice_value(sym)) {
1026d595cea6SRoman Zippel 		struct dep_stack stack;
1027d595cea6SRoman Zippel 
102848981178SRoman Zippel 		/* for choice groups start the check with main choice symbol */
1029d595cea6SRoman Zippel 		dep_stack_insert(&stack, sym);
103048981178SRoman Zippel 		prop = sym_get_choice_prop(sym);
103148981178SRoman Zippel 		sym2 = sym_check_deps(prop_get_symbol(prop));
1032d595cea6SRoman Zippel 		dep_stack_remove();
103348981178SRoman Zippel 	} else if (sym_is_choice(sym)) {
103448981178SRoman Zippel 		sym2 = sym_check_choice_deps(sym);
103548981178SRoman Zippel 	} else {
10361da177e4SLinus Torvalds 		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
103748981178SRoman Zippel 		sym2 = sym_check_sym_deps(sym);
10381da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_CHECK;
103948981178SRoman Zippel 	}
104048981178SRoman Zippel 
1041d595cea6SRoman Zippel 	if (sym2 && sym2 == sym)
104248981178SRoman Zippel 		sym2 = NULL;
104348981178SRoman Zippel 
10441da177e4SLinus Torvalds 	return sym2;
10451da177e4SLinus Torvalds }
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds struct property *prop_alloc(enum prop_type type, struct symbol *sym)
10481da177e4SLinus Torvalds {
10491da177e4SLinus Torvalds 	struct property *prop;
10501da177e4SLinus Torvalds 	struct property **propp;
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 	prop = malloc(sizeof(*prop));
10531da177e4SLinus Torvalds 	memset(prop, 0, sizeof(*prop));
10541da177e4SLinus Torvalds 	prop->type = type;
10551da177e4SLinus Torvalds 	prop->sym = sym;
10561da177e4SLinus Torvalds 	prop->file = current_file;
10571da177e4SLinus Torvalds 	prop->lineno = zconf_lineno();
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	/* append property to the prop list of symbol */
10601da177e4SLinus Torvalds 	if (sym) {
10611da177e4SLinus Torvalds 		for (propp = &sym->prop; *propp; propp = &(*propp)->next)
10621da177e4SLinus Torvalds 			;
10631da177e4SLinus Torvalds 		*propp = prop;
10641da177e4SLinus Torvalds 	}
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 	return prop;
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds struct symbol *prop_get_symbol(struct property *prop)
10701da177e4SLinus Torvalds {
10711da177e4SLinus Torvalds 	if (prop->expr && (prop->expr->type == E_SYMBOL ||
10727a962923SRoman Zippel 			   prop->expr->type == E_LIST))
10731da177e4SLinus Torvalds 		return prop->expr->left.sym;
10741da177e4SLinus Torvalds 	return NULL;
10751da177e4SLinus Torvalds }
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds const char *prop_get_type_name(enum prop_type type)
10781da177e4SLinus Torvalds {
10791da177e4SLinus Torvalds 	switch (type) {
10801da177e4SLinus Torvalds 	case P_PROMPT:
10811da177e4SLinus Torvalds 		return "prompt";
108293449082SRoman Zippel 	case P_ENV:
108393449082SRoman Zippel 		return "env";
10841da177e4SLinus Torvalds 	case P_COMMENT:
10851da177e4SLinus Torvalds 		return "comment";
10861da177e4SLinus Torvalds 	case P_MENU:
10871da177e4SLinus Torvalds 		return "menu";
10881da177e4SLinus Torvalds 	case P_DEFAULT:
10891da177e4SLinus Torvalds 		return "default";
10901da177e4SLinus Torvalds 	case P_CHOICE:
10911da177e4SLinus Torvalds 		return "choice";
10921da177e4SLinus Torvalds 	case P_SELECT:
10931da177e4SLinus Torvalds 		return "select";
10941da177e4SLinus Torvalds 	case P_RANGE:
10951da177e4SLinus Torvalds 		return "range";
109659e89e3dSSam Ravnborg 	case P_SYMBOL:
109759e89e3dSSam Ravnborg 		return "symbol";
10981da177e4SLinus Torvalds 	case P_UNKNOWN:
10991da177e4SLinus Torvalds 		break;
11001da177e4SLinus Torvalds 	}
11011da177e4SLinus Torvalds 	return "unknown";
11021da177e4SLinus Torvalds }
110393449082SRoman Zippel 
11044356f489STrevor Keith static void prop_add_env(const char *env)
110593449082SRoman Zippel {
110693449082SRoman Zippel 	struct symbol *sym, *sym2;
110793449082SRoman Zippel 	struct property *prop;
110893449082SRoman Zippel 	char *p;
110993449082SRoman Zippel 
111093449082SRoman Zippel 	sym = current_entry->sym;
111193449082SRoman Zippel 	sym->flags |= SYMBOL_AUTO;
111293449082SRoman Zippel 	for_all_properties(sym, prop, P_ENV) {
111393449082SRoman Zippel 		sym2 = prop_get_symbol(prop);
111493449082SRoman Zippel 		if (strcmp(sym2->name, env))
111593449082SRoman Zippel 			menu_warn(current_entry, "redefining environment symbol from %s",
111693449082SRoman Zippel 				  sym2->name);
111793449082SRoman Zippel 		return;
111893449082SRoman Zippel 	}
111993449082SRoman Zippel 
112093449082SRoman Zippel 	prop = prop_alloc(P_ENV, sym);
11215a1aa8a1SRoman Zippel 	prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
112293449082SRoman Zippel 
112393449082SRoman Zippel 	sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
112493449082SRoman Zippel 	sym_env_list->right.sym = sym;
112593449082SRoman Zippel 
112693449082SRoman Zippel 	p = getenv(env);
112793449082SRoman Zippel 	if (p)
112893449082SRoman Zippel 		sym_add_default(sym, p);
112993449082SRoman Zippel 	else
113093449082SRoman Zippel 		menu_warn(current_entry, "environment variable %s undefined", env);
113193449082SRoman Zippel }
1132