xref: /openbmc/linux/scripts/kconfig/symbol.c (revision 7cf3d73b4360e91b14326632ab1aeda4cb26308d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
31da177e4SLinus Torvalds  * Released under the terms of the GNU GPL v2.0.
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <ctype.h>
71da177e4SLinus Torvalds #include <stdlib.h>
81da177e4SLinus Torvalds #include <string.h>
91da177e4SLinus Torvalds #include <regex.h>
101da177e4SLinus Torvalds #include <sys/utsname.h>
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #define LKC_DIRECT_LINK
131da177e4SLinus Torvalds #include "lkc.h"
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds struct symbol symbol_yes = {
161da177e4SLinus Torvalds 	.name = "y",
171da177e4SLinus Torvalds 	.curr = { "y", yes },
18c0e150acSRoman Zippel 	.flags = SYMBOL_CONST|SYMBOL_VALID,
191da177e4SLinus Torvalds }, symbol_mod = {
201da177e4SLinus Torvalds 	.name = "m",
211da177e4SLinus Torvalds 	.curr = { "m", mod },
22c0e150acSRoman Zippel 	.flags = SYMBOL_CONST|SYMBOL_VALID,
231da177e4SLinus Torvalds }, symbol_no = {
241da177e4SLinus Torvalds 	.name = "n",
251da177e4SLinus Torvalds 	.curr = { "n", no },
26c0e150acSRoman Zippel 	.flags = SYMBOL_CONST|SYMBOL_VALID,
271da177e4SLinus Torvalds }, symbol_empty = {
281da177e4SLinus Torvalds 	.name = "",
291da177e4SLinus Torvalds 	.curr = { "", no },
301da177e4SLinus Torvalds 	.flags = SYMBOL_VALID,
311da177e4SLinus Torvalds };
321da177e4SLinus Torvalds 
33face4374SRoman Zippel struct symbol *sym_defconfig_list;
341da177e4SLinus Torvalds struct symbol *modules_sym;
351da177e4SLinus Torvalds tristate modules_val;
361da177e4SLinus Torvalds 
3793449082SRoman Zippel struct expr *sym_env_list;
3893449082SRoman Zippel 
394356f489STrevor Keith static void sym_add_default(struct symbol *sym, const char *def)
401da177e4SLinus Torvalds {
411da177e4SLinus Torvalds 	struct property *prop = prop_alloc(P_DEFAULT, sym);
421da177e4SLinus Torvalds 
435a1aa8a1SRoman Zippel 	prop->expr = expr_alloc_symbol(sym_lookup(def, SYMBOL_CONST));
441da177e4SLinus Torvalds }
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds void sym_init(void)
471da177e4SLinus Torvalds {
481da177e4SLinus Torvalds 	struct symbol *sym;
491da177e4SLinus Torvalds 	struct utsname uts;
501da177e4SLinus Torvalds 	static bool inited = false;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 	if (inited)
531da177e4SLinus Torvalds 		return;
541da177e4SLinus Torvalds 	inited = true;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	uname(&uts);
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 	sym = sym_lookup("UNAME_RELEASE", 0);
591da177e4SLinus Torvalds 	sym->type = S_STRING;
601da177e4SLinus Torvalds 	sym->flags |= SYMBOL_AUTO;
611da177e4SLinus Torvalds 	sym_add_default(sym, uts.release);
621da177e4SLinus Torvalds }
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds enum symbol_type sym_get_type(struct symbol *sym)
651da177e4SLinus Torvalds {
661da177e4SLinus Torvalds 	enum symbol_type type = sym->type;
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds 	if (type == S_TRISTATE) {
691da177e4SLinus Torvalds 		if (sym_is_choice_value(sym) && sym->visible == yes)
701da177e4SLinus Torvalds 			type = S_BOOLEAN;
711da177e4SLinus Torvalds 		else if (modules_val == no)
721da177e4SLinus Torvalds 			type = S_BOOLEAN;
731da177e4SLinus Torvalds 	}
741da177e4SLinus Torvalds 	return type;
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds const char *sym_type_name(enum symbol_type type)
781da177e4SLinus Torvalds {
791da177e4SLinus Torvalds 	switch (type) {
801da177e4SLinus Torvalds 	case S_BOOLEAN:
811da177e4SLinus Torvalds 		return "boolean";
821da177e4SLinus Torvalds 	case S_TRISTATE:
831da177e4SLinus Torvalds 		return "tristate";
841da177e4SLinus Torvalds 	case S_INT:
851da177e4SLinus Torvalds 		return "integer";
861da177e4SLinus Torvalds 	case S_HEX:
871da177e4SLinus Torvalds 		return "hex";
881da177e4SLinus Torvalds 	case S_STRING:
891da177e4SLinus Torvalds 		return "string";
901da177e4SLinus Torvalds 	case S_UNKNOWN:
911da177e4SLinus Torvalds 		return "unknown";
921da177e4SLinus Torvalds 	case S_OTHER:
931da177e4SLinus Torvalds 		break;
941da177e4SLinus Torvalds 	}
951da177e4SLinus Torvalds 	return "???";
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds struct property *sym_get_choice_prop(struct symbol *sym)
991da177e4SLinus Torvalds {
1001da177e4SLinus Torvalds 	struct property *prop;
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 	for_all_choices(sym, prop)
1031da177e4SLinus Torvalds 		return prop;
1041da177e4SLinus Torvalds 	return NULL;
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
10793449082SRoman Zippel struct property *sym_get_env_prop(struct symbol *sym)
10893449082SRoman Zippel {
10993449082SRoman Zippel 	struct property *prop;
11093449082SRoman Zippel 
11193449082SRoman Zippel 	for_all_properties(sym, prop, P_ENV)
11293449082SRoman Zippel 		return prop;
11393449082SRoman Zippel 	return NULL;
11493449082SRoman Zippel }
11593449082SRoman Zippel 
1161da177e4SLinus Torvalds struct property *sym_get_default_prop(struct symbol *sym)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	struct property *prop;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	for_all_defaults(sym, prop) {
1211da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
1221da177e4SLinus Torvalds 		if (prop->visible.tri != no)
1231da177e4SLinus Torvalds 			return prop;
1241da177e4SLinus Torvalds 	}
1251da177e4SLinus Torvalds 	return NULL;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
1284356f489STrevor Keith static struct property *sym_get_range_prop(struct symbol *sym)
1291da177e4SLinus Torvalds {
1301da177e4SLinus Torvalds 	struct property *prop;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	for_all_properties(sym, prop, P_RANGE) {
1331da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
1341da177e4SLinus Torvalds 		if (prop->visible.tri != no)
1351da177e4SLinus Torvalds 			return prop;
1361da177e4SLinus Torvalds 	}
1371da177e4SLinus Torvalds 	return NULL;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1404cf3cbe2SRoman Zippel static int sym_get_range_val(struct symbol *sym, int base)
1414cf3cbe2SRoman Zippel {
1424cf3cbe2SRoman Zippel 	sym_calc_value(sym);
1434cf3cbe2SRoman Zippel 	switch (sym->type) {
1444cf3cbe2SRoman Zippel 	case S_INT:
1454cf3cbe2SRoman Zippel 		base = 10;
1464cf3cbe2SRoman Zippel 		break;
1474cf3cbe2SRoman Zippel 	case S_HEX:
1484cf3cbe2SRoman Zippel 		base = 16;
1494cf3cbe2SRoman Zippel 		break;
1504cf3cbe2SRoman Zippel 	default:
1514cf3cbe2SRoman Zippel 		break;
1524cf3cbe2SRoman Zippel 	}
1534cf3cbe2SRoman Zippel 	return strtol(sym->curr.val, NULL, base);
1544cf3cbe2SRoman Zippel }
1554cf3cbe2SRoman Zippel 
1564cf3cbe2SRoman Zippel static void sym_validate_range(struct symbol *sym)
1574cf3cbe2SRoman Zippel {
1584cf3cbe2SRoman Zippel 	struct property *prop;
1594cf3cbe2SRoman Zippel 	int base, val, val2;
1604cf3cbe2SRoman Zippel 	char str[64];
1614cf3cbe2SRoman Zippel 
1624cf3cbe2SRoman Zippel 	switch (sym->type) {
1634cf3cbe2SRoman Zippel 	case S_INT:
1644cf3cbe2SRoman Zippel 		base = 10;
1654cf3cbe2SRoman Zippel 		break;
1664cf3cbe2SRoman Zippel 	case S_HEX:
1674cf3cbe2SRoman Zippel 		base = 16;
1684cf3cbe2SRoman Zippel 		break;
1694cf3cbe2SRoman Zippel 	default:
1704cf3cbe2SRoman Zippel 		return;
1714cf3cbe2SRoman Zippel 	}
1724cf3cbe2SRoman Zippel 	prop = sym_get_range_prop(sym);
1734cf3cbe2SRoman Zippel 	if (!prop)
1744cf3cbe2SRoman Zippel 		return;
1754cf3cbe2SRoman Zippel 	val = strtol(sym->curr.val, NULL, base);
1764cf3cbe2SRoman Zippel 	val2 = sym_get_range_val(prop->expr->left.sym, base);
1774cf3cbe2SRoman Zippel 	if (val >= val2) {
1784cf3cbe2SRoman Zippel 		val2 = sym_get_range_val(prop->expr->right.sym, base);
1794cf3cbe2SRoman Zippel 		if (val <= val2)
1804cf3cbe2SRoman Zippel 			return;
1814cf3cbe2SRoman Zippel 	}
1824cf3cbe2SRoman Zippel 	if (sym->type == S_INT)
1834cf3cbe2SRoman Zippel 		sprintf(str, "%d", val2);
1844cf3cbe2SRoman Zippel 	else
1854cf3cbe2SRoman Zippel 		sprintf(str, "0x%x", val2);
1864cf3cbe2SRoman Zippel 	sym->curr.val = strdup(str);
1874cf3cbe2SRoman Zippel }
1884cf3cbe2SRoman Zippel 
1891da177e4SLinus Torvalds static void sym_calc_visibility(struct symbol *sym)
1901da177e4SLinus Torvalds {
1911da177e4SLinus Torvalds 	struct property *prop;
1921da177e4SLinus Torvalds 	tristate tri;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	/* any prompt visible? */
1951da177e4SLinus Torvalds 	tri = no;
1961da177e4SLinus Torvalds 	for_all_prompts(sym, prop) {
1971da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
198d6ee3576SSam Ravnborg 		tri = EXPR_OR(tri, prop->visible.tri);
1991da177e4SLinus Torvalds 	}
2001da177e4SLinus Torvalds 	if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
2011da177e4SLinus Torvalds 		tri = yes;
2021da177e4SLinus Torvalds 	if (sym->visible != tri) {
2031da177e4SLinus Torvalds 		sym->visible = tri;
2041da177e4SLinus Torvalds 		sym_set_changed(sym);
2051da177e4SLinus Torvalds 	}
2061da177e4SLinus Torvalds 	if (sym_is_choice_value(sym))
2071da177e4SLinus Torvalds 		return;
208246cf9c2SCatalin Marinas 	/* defaulting to "yes" if no explicit "depends on" are given */
209246cf9c2SCatalin Marinas 	tri = yes;
210246cf9c2SCatalin Marinas 	if (sym->dir_dep.expr)
211246cf9c2SCatalin Marinas 		tri = expr_calc_value(sym->dir_dep.expr);
212246cf9c2SCatalin Marinas 	if (tri == mod)
213246cf9c2SCatalin Marinas 		tri = yes;
214246cf9c2SCatalin Marinas 	if (sym->dir_dep.tri != tri) {
215246cf9c2SCatalin Marinas 		sym->dir_dep.tri = tri;
216246cf9c2SCatalin Marinas 		sym_set_changed(sym);
217246cf9c2SCatalin Marinas 	}
2181da177e4SLinus Torvalds 	tri = no;
2191da177e4SLinus Torvalds 	if (sym->rev_dep.expr)
2201da177e4SLinus Torvalds 		tri = expr_calc_value(sym->rev_dep.expr);
2211da177e4SLinus Torvalds 	if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
2221da177e4SLinus Torvalds 		tri = yes;
2231da177e4SLinus Torvalds 	if (sym->rev_dep.tri != tri) {
2241da177e4SLinus Torvalds 		sym->rev_dep.tri = tri;
2251da177e4SLinus Torvalds 		sym_set_changed(sym);
2261da177e4SLinus Torvalds 	}
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
229c252147dSSam Ravnborg /*
230c252147dSSam Ravnborg  * Find the default symbol for a choice.
231c252147dSSam Ravnborg  * First try the default values for the choice symbol
232c252147dSSam Ravnborg  * Next locate the first visible choice value
233c252147dSSam Ravnborg  * Return NULL if none was found
234c252147dSSam Ravnborg  */
235c252147dSSam Ravnborg struct symbol *sym_choice_default(struct symbol *sym)
2361da177e4SLinus Torvalds {
2371da177e4SLinus Torvalds 	struct symbol *def_sym;
2381da177e4SLinus Torvalds 	struct property *prop;
2391da177e4SLinus Torvalds 	struct expr *e;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	/* any of the defaults visible? */
2421da177e4SLinus Torvalds 	for_all_defaults(sym, prop) {
2431da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
2441da177e4SLinus Torvalds 		if (prop->visible.tri == no)
2451da177e4SLinus Torvalds 			continue;
2461da177e4SLinus Torvalds 		def_sym = prop_get_symbol(prop);
2471da177e4SLinus Torvalds 		if (def_sym->visible != no)
2481da177e4SLinus Torvalds 			return def_sym;
2491da177e4SLinus Torvalds 	}
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	/* just get the first visible value */
2521da177e4SLinus Torvalds 	prop = sym_get_choice_prop(sym);
2530a28c47bSJan Beulich 	expr_list_for_each_sym(prop->expr, e, def_sym)
2541da177e4SLinus Torvalds 		if (def_sym->visible != no)
2551da177e4SLinus Torvalds 			return def_sym;
2561da177e4SLinus Torvalds 
257c252147dSSam Ravnborg 	/* failed to locate any defaults */
258c252147dSSam Ravnborg 	return NULL;
259c252147dSSam Ravnborg }
260c252147dSSam Ravnborg 
261c252147dSSam Ravnborg static struct symbol *sym_calc_choice(struct symbol *sym)
262c252147dSSam Ravnborg {
263c252147dSSam Ravnborg 	struct symbol *def_sym;
264c252147dSSam Ravnborg 	struct property *prop;
265c252147dSSam Ravnborg 	struct expr *e;
266c252147dSSam Ravnborg 
267c252147dSSam Ravnborg 	/* first calculate all choice values' visibilities */
268c252147dSSam Ravnborg 	prop = sym_get_choice_prop(sym);
269c252147dSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, def_sym)
270c252147dSSam Ravnborg 		sym_calc_visibility(def_sym);
271c252147dSSam Ravnborg 
272c252147dSSam Ravnborg 	/* is the user choice visible? */
273c252147dSSam Ravnborg 	def_sym = sym->def[S_DEF_USER].val;
274c252147dSSam Ravnborg 	if (def_sym && def_sym->visible != no)
275c252147dSSam Ravnborg 		return def_sym;
276c252147dSSam Ravnborg 
277c252147dSSam Ravnborg 	def_sym = sym_choice_default(sym);
278c252147dSSam Ravnborg 
279c252147dSSam Ravnborg 	if (def_sym == NULL)
2801da177e4SLinus Torvalds 		/* no choice? reset tristate value */
2811da177e4SLinus Torvalds 		sym->curr.tri = no;
282c252147dSSam Ravnborg 
283c252147dSSam Ravnborg 	return def_sym;
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds void sym_calc_value(struct symbol *sym)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	struct symbol_value newval, oldval;
2891da177e4SLinus Torvalds 	struct property *prop;
2901da177e4SLinus Torvalds 	struct expr *e;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	if (!sym)
2931da177e4SLinus Torvalds 		return;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	if (sym->flags & SYMBOL_VALID)
2961da177e4SLinus Torvalds 		return;
2971da177e4SLinus Torvalds 	sym->flags |= SYMBOL_VALID;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	oldval = sym->curr;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	switch (sym->type) {
3021da177e4SLinus Torvalds 	case S_INT:
3031da177e4SLinus Torvalds 	case S_HEX:
3041da177e4SLinus Torvalds 	case S_STRING:
3051da177e4SLinus Torvalds 		newval = symbol_empty.curr;
3061da177e4SLinus Torvalds 		break;
3071da177e4SLinus Torvalds 	case S_BOOLEAN:
3081da177e4SLinus Torvalds 	case S_TRISTATE:
3091da177e4SLinus Torvalds 		newval = symbol_no.curr;
3101da177e4SLinus Torvalds 		break;
3111da177e4SLinus Torvalds 	default:
3121da177e4SLinus Torvalds 		sym->curr.val = sym->name;
3131da177e4SLinus Torvalds 		sym->curr.tri = no;
3141da177e4SLinus Torvalds 		return;
3151da177e4SLinus Torvalds 	}
3161da177e4SLinus Torvalds 	if (!sym_is_choice_value(sym))
3171da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_WRITE;
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	sym_calc_visibility(sym);
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	/* set default if recursively called */
3221da177e4SLinus Torvalds 	sym->curr = newval;
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	switch (sym_get_type(sym)) {
3251da177e4SLinus Torvalds 	case S_BOOLEAN:
3261da177e4SLinus Torvalds 	case S_TRISTATE:
3271da177e4SLinus Torvalds 		if (sym_is_choice_value(sym) && sym->visible == yes) {
3281da177e4SLinus Torvalds 			prop = sym_get_choice_prop(sym);
3291da177e4SLinus Torvalds 			newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
330587c9061SRoman Zippel 		} else {
331587c9061SRoman Zippel 			if (sym->visible != no) {
332587c9061SRoman Zippel 				/* if the symbol is visible use the user value
333587c9061SRoman Zippel 				 * if available, otherwise try the default value
334587c9061SRoman Zippel 				 */
3351da177e4SLinus Torvalds 				sym->flags |= SYMBOL_WRITE;
336587c9061SRoman Zippel 				if (sym_has_value(sym)) {
337587c9061SRoman Zippel 					newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
338587c9061SRoman Zippel 							      sym->visible);
339587c9061SRoman Zippel 					goto calc_newval;
3401da177e4SLinus Torvalds 				}
341587c9061SRoman Zippel 			}
342587c9061SRoman Zippel 			if (sym->rev_dep.tri != no)
343587c9061SRoman Zippel 				sym->flags |= SYMBOL_WRITE;
344587c9061SRoman Zippel 			if (!sym_is_choice(sym)) {
3451da177e4SLinus Torvalds 				prop = sym_get_default_prop(sym);
3461da177e4SLinus Torvalds 				if (prop) {
3471da177e4SLinus Torvalds 					sym->flags |= SYMBOL_WRITE;
348587c9061SRoman Zippel 					newval.tri = EXPR_AND(expr_calc_value(prop->expr),
349587c9061SRoman Zippel 							      prop->visible.tri);
3501da177e4SLinus Torvalds 				}
3511da177e4SLinus Torvalds 			}
352587c9061SRoman Zippel 		calc_newval:
353246cf9c2SCatalin Marinas 			if (sym->dir_dep.tri == no && sym->rev_dep.tri != no) {
354246cf9c2SCatalin Marinas 				fprintf(stderr, "warning: (");
355246cf9c2SCatalin Marinas 				expr_fprint(sym->rev_dep.expr, stderr);
356246cf9c2SCatalin Marinas 				fprintf(stderr, ") selects %s which has unmet direct dependencies (",
357246cf9c2SCatalin Marinas 					sym->name);
358246cf9c2SCatalin Marinas 				expr_fprint(sym->dir_dep.expr, stderr);
359246cf9c2SCatalin Marinas 				fprintf(stderr, ")\n");
360246cf9c2SCatalin Marinas 			}
361587c9061SRoman Zippel 			newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
362587c9061SRoman Zippel 		}
3631da177e4SLinus Torvalds 		if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
3641da177e4SLinus Torvalds 			newval.tri = yes;
3651da177e4SLinus Torvalds 		break;
3661da177e4SLinus Torvalds 	case S_STRING:
3671da177e4SLinus Torvalds 	case S_HEX:
3681da177e4SLinus Torvalds 	case S_INT:
3691da177e4SLinus Torvalds 		if (sym->visible != no) {
3701da177e4SLinus Torvalds 			sym->flags |= SYMBOL_WRITE;
3711da177e4SLinus Torvalds 			if (sym_has_value(sym)) {
3720c1822e6SRoman Zippel 				newval.val = sym->def[S_DEF_USER].val;
3731da177e4SLinus Torvalds 				break;
3741da177e4SLinus Torvalds 			}
3751da177e4SLinus Torvalds 		}
3761da177e4SLinus Torvalds 		prop = sym_get_default_prop(sym);
3771da177e4SLinus Torvalds 		if (prop) {
3781da177e4SLinus Torvalds 			struct symbol *ds = prop_get_symbol(prop);
3791da177e4SLinus Torvalds 			if (ds) {
3801da177e4SLinus Torvalds 				sym->flags |= SYMBOL_WRITE;
3811da177e4SLinus Torvalds 				sym_calc_value(ds);
3821da177e4SLinus Torvalds 				newval.val = ds->curr.val;
3831da177e4SLinus Torvalds 			}
3841da177e4SLinus Torvalds 		}
3851da177e4SLinus Torvalds 		break;
3861da177e4SLinus Torvalds 	default:
3871da177e4SLinus Torvalds 		;
3881da177e4SLinus Torvalds 	}
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	sym->curr = newval;
3911da177e4SLinus Torvalds 	if (sym_is_choice(sym) && newval.tri == yes)
3921da177e4SLinus Torvalds 		sym->curr.val = sym_calc_choice(sym);
3934cf3cbe2SRoman Zippel 	sym_validate_range(sym);
3941da177e4SLinus Torvalds 
395face4374SRoman Zippel 	if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
3961da177e4SLinus Torvalds 		sym_set_changed(sym);
397face4374SRoman Zippel 		if (modules_sym == sym) {
398face4374SRoman Zippel 			sym_set_all_changed();
3991da177e4SLinus Torvalds 			modules_val = modules_sym->curr.tri;
400face4374SRoman Zippel 		}
401face4374SRoman Zippel 	}
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	if (sym_is_choice(sym)) {
4047a962923SRoman Zippel 		struct symbol *choice_sym;
4057a962923SRoman Zippel 
4061da177e4SLinus Torvalds 		prop = sym_get_choice_prop(sym);
4077a962923SRoman Zippel 		expr_list_for_each_sym(prop->expr, e, choice_sym) {
4080a28c47bSJan Beulich 			if ((sym->flags & SYMBOL_WRITE) &&
4090a28c47bSJan Beulich 			    choice_sym->visible != no)
4100a28c47bSJan Beulich 				choice_sym->flags |= SYMBOL_WRITE;
4110a28c47bSJan Beulich 			if (sym->flags & SYMBOL_CHANGED)
4127a962923SRoman Zippel 				sym_set_changed(choice_sym);
4131da177e4SLinus Torvalds 		}
4141da177e4SLinus Torvalds 	}
4155a1aa8a1SRoman Zippel 
4165a1aa8a1SRoman Zippel 	if (sym->flags & SYMBOL_AUTO)
4175a1aa8a1SRoman Zippel 		sym->flags &= ~SYMBOL_WRITE;
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds void sym_clear_all_valid(void)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	struct symbol *sym;
4231da177e4SLinus Torvalds 	int i;
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	for_all_symbols(i, sym)
4261da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_VALID;
427bfc10001SKarsten Wiese 	sym_add_change_count(1);
4281da177e4SLinus Torvalds 	if (modules_sym)
4291da177e4SLinus Torvalds 		sym_calc_value(modules_sym);
4301da177e4SLinus Torvalds }
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds void sym_set_changed(struct symbol *sym)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds 	struct property *prop;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	sym->flags |= SYMBOL_CHANGED;
4371da177e4SLinus Torvalds 	for (prop = sym->prop; prop; prop = prop->next) {
4381da177e4SLinus Torvalds 		if (prop->menu)
4391da177e4SLinus Torvalds 			prop->menu->flags |= MENU_CHANGED;
4401da177e4SLinus Torvalds 	}
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds void sym_set_all_changed(void)
4441da177e4SLinus Torvalds {
4451da177e4SLinus Torvalds 	struct symbol *sym;
4461da177e4SLinus Torvalds 	int i;
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	for_all_symbols(i, sym)
4491da177e4SLinus Torvalds 		sym_set_changed(sym);
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds bool sym_tristate_within_range(struct symbol *sym, tristate val)
4531da177e4SLinus Torvalds {
4541da177e4SLinus Torvalds 	int type = sym_get_type(sym);
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	if (sym->visible == no)
4571da177e4SLinus Torvalds 		return false;
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	if (type != S_BOOLEAN && type != S_TRISTATE)
4601da177e4SLinus Torvalds 		return false;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	if (type == S_BOOLEAN && val == mod)
4631da177e4SLinus Torvalds 		return false;
4641da177e4SLinus Torvalds 	if (sym->visible <= sym->rev_dep.tri)
4651da177e4SLinus Torvalds 		return false;
4661da177e4SLinus Torvalds 	if (sym_is_choice_value(sym) && sym->visible == yes)
4671da177e4SLinus Torvalds 		return val == yes;
4681da177e4SLinus Torvalds 	return val >= sym->rev_dep.tri && val <= sym->visible;
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds bool sym_set_tristate_value(struct symbol *sym, tristate val)
4721da177e4SLinus Torvalds {
4731da177e4SLinus Torvalds 	tristate oldval = sym_get_tristate_value(sym);
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	if (oldval != val && !sym_tristate_within_range(sym, val))
4761da177e4SLinus Torvalds 		return false;
4771da177e4SLinus Torvalds 
478669bfad9SRoman Zippel 	if (!(sym->flags & SYMBOL_DEF_USER)) {
479669bfad9SRoman Zippel 		sym->flags |= SYMBOL_DEF_USER;
4801da177e4SLinus Torvalds 		sym_set_changed(sym);
4811da177e4SLinus Torvalds 	}
4823f23ca2bSRoman Zippel 	/*
4833f23ca2bSRoman Zippel 	 * setting a choice value also resets the new flag of the choice
4843f23ca2bSRoman Zippel 	 * symbol and all other choice values.
4853f23ca2bSRoman Zippel 	 */
4861da177e4SLinus Torvalds 	if (sym_is_choice_value(sym) && val == yes) {
4871da177e4SLinus Torvalds 		struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
4883f23ca2bSRoman Zippel 		struct property *prop;
4893f23ca2bSRoman Zippel 		struct expr *e;
4901da177e4SLinus Torvalds 
4910c1822e6SRoman Zippel 		cs->def[S_DEF_USER].val = sym;
492669bfad9SRoman Zippel 		cs->flags |= SYMBOL_DEF_USER;
4933f23ca2bSRoman Zippel 		prop = sym_get_choice_prop(cs);
4943f23ca2bSRoman Zippel 		for (e = prop->expr; e; e = e->left.expr) {
4953f23ca2bSRoman Zippel 			if (e->right.sym->visible != no)
496669bfad9SRoman Zippel 				e->right.sym->flags |= SYMBOL_DEF_USER;
4973f23ca2bSRoman Zippel 		}
4981da177e4SLinus Torvalds 	}
4991da177e4SLinus Torvalds 
5000c1822e6SRoman Zippel 	sym->def[S_DEF_USER].tri = val;
501face4374SRoman Zippel 	if (oldval != val)
5021da177e4SLinus Torvalds 		sym_clear_all_valid();
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	return true;
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds tristate sym_toggle_tristate_value(struct symbol *sym)
5081da177e4SLinus Torvalds {
5091da177e4SLinus Torvalds 	tristate oldval, newval;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	oldval = newval = sym_get_tristate_value(sym);
5121da177e4SLinus Torvalds 	do {
5131da177e4SLinus Torvalds 		switch (newval) {
5141da177e4SLinus Torvalds 		case no:
5151da177e4SLinus Torvalds 			newval = mod;
5161da177e4SLinus Torvalds 			break;
5171da177e4SLinus Torvalds 		case mod:
5181da177e4SLinus Torvalds 			newval = yes;
5191da177e4SLinus Torvalds 			break;
5201da177e4SLinus Torvalds 		case yes:
5211da177e4SLinus Torvalds 			newval = no;
5221da177e4SLinus Torvalds 			break;
5231da177e4SLinus Torvalds 		}
5241da177e4SLinus Torvalds 		if (sym_set_tristate_value(sym, newval))
5251da177e4SLinus Torvalds 			break;
5261da177e4SLinus Torvalds 	} while (oldval != newval);
5271da177e4SLinus Torvalds 	return newval;
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds bool sym_string_valid(struct symbol *sym, const char *str)
5311da177e4SLinus Torvalds {
5321da177e4SLinus Torvalds 	signed char ch;
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	switch (sym->type) {
5351da177e4SLinus Torvalds 	case S_STRING:
5361da177e4SLinus Torvalds 		return true;
5371da177e4SLinus Torvalds 	case S_INT:
5381da177e4SLinus Torvalds 		ch = *str++;
5391da177e4SLinus Torvalds 		if (ch == '-')
5401da177e4SLinus Torvalds 			ch = *str++;
5411da177e4SLinus Torvalds 		if (!isdigit(ch))
5421da177e4SLinus Torvalds 			return false;
5431da177e4SLinus Torvalds 		if (ch == '0' && *str != 0)
5441da177e4SLinus Torvalds 			return false;
5451da177e4SLinus Torvalds 		while ((ch = *str++)) {
5461da177e4SLinus Torvalds 			if (!isdigit(ch))
5471da177e4SLinus Torvalds 				return false;
5481da177e4SLinus Torvalds 		}
5491da177e4SLinus Torvalds 		return true;
5501da177e4SLinus Torvalds 	case S_HEX:
5511da177e4SLinus Torvalds 		if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
5521da177e4SLinus Torvalds 			str += 2;
5531da177e4SLinus Torvalds 		ch = *str++;
5541da177e4SLinus Torvalds 		do {
5551da177e4SLinus Torvalds 			if (!isxdigit(ch))
5561da177e4SLinus Torvalds 				return false;
5571da177e4SLinus Torvalds 		} while ((ch = *str++));
5581da177e4SLinus Torvalds 		return true;
5591da177e4SLinus Torvalds 	case S_BOOLEAN:
5601da177e4SLinus Torvalds 	case S_TRISTATE:
5611da177e4SLinus Torvalds 		switch (str[0]) {
5621da177e4SLinus Torvalds 		case 'y': case 'Y':
5631da177e4SLinus Torvalds 		case 'm': case 'M':
5641da177e4SLinus Torvalds 		case 'n': case 'N':
5651da177e4SLinus Torvalds 			return true;
5661da177e4SLinus Torvalds 		}
5671da177e4SLinus Torvalds 		return false;
5681da177e4SLinus Torvalds 	default:
5691da177e4SLinus Torvalds 		return false;
5701da177e4SLinus Torvalds 	}
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds bool sym_string_within_range(struct symbol *sym, const char *str)
5741da177e4SLinus Torvalds {
5751da177e4SLinus Torvalds 	struct property *prop;
5761da177e4SLinus Torvalds 	int val;
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	switch (sym->type) {
5791da177e4SLinus Torvalds 	case S_STRING:
5801da177e4SLinus Torvalds 		return sym_string_valid(sym, str);
5811da177e4SLinus Torvalds 	case S_INT:
5821da177e4SLinus Torvalds 		if (!sym_string_valid(sym, str))
5831da177e4SLinus Torvalds 			return false;
5841da177e4SLinus Torvalds 		prop = sym_get_range_prop(sym);
5851da177e4SLinus Torvalds 		if (!prop)
5861da177e4SLinus Torvalds 			return true;
5871da177e4SLinus Torvalds 		val = strtol(str, NULL, 10);
5884cf3cbe2SRoman Zippel 		return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
5894cf3cbe2SRoman Zippel 		       val <= sym_get_range_val(prop->expr->right.sym, 10);
5901da177e4SLinus Torvalds 	case S_HEX:
5911da177e4SLinus Torvalds 		if (!sym_string_valid(sym, str))
5921da177e4SLinus Torvalds 			return false;
5931da177e4SLinus Torvalds 		prop = sym_get_range_prop(sym);
5941da177e4SLinus Torvalds 		if (!prop)
5951da177e4SLinus Torvalds 			return true;
5961da177e4SLinus Torvalds 		val = strtol(str, NULL, 16);
5974cf3cbe2SRoman Zippel 		return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
5984cf3cbe2SRoman Zippel 		       val <= sym_get_range_val(prop->expr->right.sym, 16);
5991da177e4SLinus Torvalds 	case S_BOOLEAN:
6001da177e4SLinus Torvalds 	case S_TRISTATE:
6011da177e4SLinus Torvalds 		switch (str[0]) {
6021da177e4SLinus Torvalds 		case 'y': case 'Y':
6031da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, yes);
6041da177e4SLinus Torvalds 		case 'm': case 'M':
6051da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, mod);
6061da177e4SLinus Torvalds 		case 'n': case 'N':
6071da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, no);
6081da177e4SLinus Torvalds 		}
6091da177e4SLinus Torvalds 		return false;
6101da177e4SLinus Torvalds 	default:
6111da177e4SLinus Torvalds 		return false;
6121da177e4SLinus Torvalds 	}
6131da177e4SLinus Torvalds }
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds bool sym_set_string_value(struct symbol *sym, const char *newval)
6161da177e4SLinus Torvalds {
6171da177e4SLinus Torvalds 	const char *oldval;
6181da177e4SLinus Torvalds 	char *val;
6191da177e4SLinus Torvalds 	int size;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	switch (sym->type) {
6221da177e4SLinus Torvalds 	case S_BOOLEAN:
6231da177e4SLinus Torvalds 	case S_TRISTATE:
6241da177e4SLinus Torvalds 		switch (newval[0]) {
6251da177e4SLinus Torvalds 		case 'y': case 'Y':
6261da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, yes);
6271da177e4SLinus Torvalds 		case 'm': case 'M':
6281da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, mod);
6291da177e4SLinus Torvalds 		case 'n': case 'N':
6301da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, no);
6311da177e4SLinus Torvalds 		}
6321da177e4SLinus Torvalds 		return false;
6331da177e4SLinus Torvalds 	default:
6341da177e4SLinus Torvalds 		;
6351da177e4SLinus Torvalds 	}
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	if (!sym_string_within_range(sym, newval))
6381da177e4SLinus Torvalds 		return false;
6391da177e4SLinus Torvalds 
640669bfad9SRoman Zippel 	if (!(sym->flags & SYMBOL_DEF_USER)) {
641669bfad9SRoman Zippel 		sym->flags |= SYMBOL_DEF_USER;
6421da177e4SLinus Torvalds 		sym_set_changed(sym);
6431da177e4SLinus Torvalds 	}
6441da177e4SLinus Torvalds 
6450c1822e6SRoman Zippel 	oldval = sym->def[S_DEF_USER].val;
6461da177e4SLinus Torvalds 	size = strlen(newval) + 1;
6471da177e4SLinus Torvalds 	if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
6481da177e4SLinus Torvalds 		size += 2;
6490c1822e6SRoman Zippel 		sym->def[S_DEF_USER].val = val = malloc(size);
6501da177e4SLinus Torvalds 		*val++ = '0';
6511da177e4SLinus Torvalds 		*val++ = 'x';
6521da177e4SLinus Torvalds 	} else if (!oldval || strcmp(oldval, newval))
6530c1822e6SRoman Zippel 		sym->def[S_DEF_USER].val = val = malloc(size);
6541da177e4SLinus Torvalds 	else
6551da177e4SLinus Torvalds 		return true;
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	strcpy(val, newval);
6581da177e4SLinus Torvalds 	free((void *)oldval);
6591da177e4SLinus Torvalds 	sym_clear_all_valid();
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	return true;
6621da177e4SLinus Torvalds }
6631da177e4SLinus Torvalds 
664*7cf3d73bSSam Ravnborg /*
665*7cf3d73bSSam Ravnborg  * Find the default value associated to a symbol.
666*7cf3d73bSSam Ravnborg  * For tristate symbol handle the modules=n case
667*7cf3d73bSSam Ravnborg  * in which case "m" becomes "y".
668*7cf3d73bSSam Ravnborg  * If the symbol does not have any default then fallback
669*7cf3d73bSSam Ravnborg  * to the fixed default values.
670*7cf3d73bSSam Ravnborg  */
671*7cf3d73bSSam Ravnborg const char *sym_get_string_default(struct symbol *sym)
672*7cf3d73bSSam Ravnborg {
673*7cf3d73bSSam Ravnborg 	struct property *prop;
674*7cf3d73bSSam Ravnborg 	struct symbol *ds;
675*7cf3d73bSSam Ravnborg 	const char *str;
676*7cf3d73bSSam Ravnborg 	tristate val;
677*7cf3d73bSSam Ravnborg 
678*7cf3d73bSSam Ravnborg 	sym_calc_visibility(sym);
679*7cf3d73bSSam Ravnborg 	sym_calc_value(modules_sym);
680*7cf3d73bSSam Ravnborg 	val = symbol_no.curr.tri;
681*7cf3d73bSSam Ravnborg 	str = symbol_empty.curr.val;
682*7cf3d73bSSam Ravnborg 
683*7cf3d73bSSam Ravnborg 	/* If symbol has a default value look it up */
684*7cf3d73bSSam Ravnborg 	prop = sym_get_default_prop(sym);
685*7cf3d73bSSam Ravnborg 	if (prop != NULL) {
686*7cf3d73bSSam Ravnborg 		switch (sym->type) {
687*7cf3d73bSSam Ravnborg 		case S_BOOLEAN:
688*7cf3d73bSSam Ravnborg 		case S_TRISTATE:
689*7cf3d73bSSam Ravnborg 			/* The visibility imay limit the value from yes => mod */
690*7cf3d73bSSam Ravnborg 			val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
691*7cf3d73bSSam Ravnborg 			break;
692*7cf3d73bSSam Ravnborg 		default:
693*7cf3d73bSSam Ravnborg 			/*
694*7cf3d73bSSam Ravnborg 			 * The following fails to handle the situation
695*7cf3d73bSSam Ravnborg 			 * where a default value is further limited by
696*7cf3d73bSSam Ravnborg 			 * the valid range.
697*7cf3d73bSSam Ravnborg 			 */
698*7cf3d73bSSam Ravnborg 			ds = prop_get_symbol(prop);
699*7cf3d73bSSam Ravnborg 			if (ds != NULL) {
700*7cf3d73bSSam Ravnborg 				sym_calc_value(ds);
701*7cf3d73bSSam Ravnborg 				str = (const char *)ds->curr.val;
702*7cf3d73bSSam Ravnborg 			}
703*7cf3d73bSSam Ravnborg 		}
704*7cf3d73bSSam Ravnborg 	}
705*7cf3d73bSSam Ravnborg 
706*7cf3d73bSSam Ravnborg 	/* Handle select statements */
707*7cf3d73bSSam Ravnborg 	val = EXPR_OR(val, sym->rev_dep.tri);
708*7cf3d73bSSam Ravnborg 
709*7cf3d73bSSam Ravnborg 	/* transpose mod to yes if modules are not enabled */
710*7cf3d73bSSam Ravnborg 	if (val == mod)
711*7cf3d73bSSam Ravnborg 		if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
712*7cf3d73bSSam Ravnborg 			val = yes;
713*7cf3d73bSSam Ravnborg 
714*7cf3d73bSSam Ravnborg 	/* transpose mod to yes if type is bool */
715*7cf3d73bSSam Ravnborg 	if (sym->type == S_BOOLEAN && val == mod)
716*7cf3d73bSSam Ravnborg 		val = yes;
717*7cf3d73bSSam Ravnborg 
718*7cf3d73bSSam Ravnborg 	switch (sym->type) {
719*7cf3d73bSSam Ravnborg 	case S_BOOLEAN:
720*7cf3d73bSSam Ravnborg 	case S_TRISTATE:
721*7cf3d73bSSam Ravnborg 		switch (val) {
722*7cf3d73bSSam Ravnborg 		case no: return "n";
723*7cf3d73bSSam Ravnborg 		case mod: return "m";
724*7cf3d73bSSam Ravnborg 		case yes: return "y";
725*7cf3d73bSSam Ravnborg 		}
726*7cf3d73bSSam Ravnborg 	case S_INT:
727*7cf3d73bSSam Ravnborg 	case S_HEX:
728*7cf3d73bSSam Ravnborg 		return str;
729*7cf3d73bSSam Ravnborg 	case S_STRING:
730*7cf3d73bSSam Ravnborg 		return str;
731*7cf3d73bSSam Ravnborg 	case S_OTHER:
732*7cf3d73bSSam Ravnborg 	case S_UNKNOWN:
733*7cf3d73bSSam Ravnborg 		break;
734*7cf3d73bSSam Ravnborg 	}
735*7cf3d73bSSam Ravnborg 	return "";
736*7cf3d73bSSam Ravnborg }
737*7cf3d73bSSam Ravnborg 
7381da177e4SLinus Torvalds const char *sym_get_string_value(struct symbol *sym)
7391da177e4SLinus Torvalds {
7401da177e4SLinus Torvalds 	tristate val;
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	switch (sym->type) {
7431da177e4SLinus Torvalds 	case S_BOOLEAN:
7441da177e4SLinus Torvalds 	case S_TRISTATE:
7451da177e4SLinus Torvalds 		val = sym_get_tristate_value(sym);
7461da177e4SLinus Torvalds 		switch (val) {
7471da177e4SLinus Torvalds 		case no:
7481da177e4SLinus Torvalds 			return "n";
7491da177e4SLinus Torvalds 		case mod:
7501da177e4SLinus Torvalds 			return "m";
7511da177e4SLinus Torvalds 		case yes:
7521da177e4SLinus Torvalds 			return "y";
7531da177e4SLinus Torvalds 		}
7541da177e4SLinus Torvalds 		break;
7551da177e4SLinus Torvalds 	default:
7561da177e4SLinus Torvalds 		;
7571da177e4SLinus Torvalds 	}
7581da177e4SLinus Torvalds 	return (const char *)sym->curr.val;
7591da177e4SLinus Torvalds }
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds bool sym_is_changable(struct symbol *sym)
7621da177e4SLinus Torvalds {
7631da177e4SLinus Torvalds 	return sym->visible > sym->rev_dep.tri;
7641da177e4SLinus Torvalds }
7651da177e4SLinus Torvalds 
766e66f25d7SAndi Kleen static unsigned strhash(const char *s)
767e66f25d7SAndi Kleen {
768e66f25d7SAndi Kleen 	/* fnv32 hash */
769e66f25d7SAndi Kleen 	unsigned hash = 2166136261U;
770e66f25d7SAndi Kleen 	for (; *s; s++)
771e66f25d7SAndi Kleen 		hash = (hash ^ *s) * 0x01000193;
772e66f25d7SAndi Kleen 	return hash;
773e66f25d7SAndi Kleen }
774e66f25d7SAndi Kleen 
7755a1aa8a1SRoman Zippel struct symbol *sym_lookup(const char *name, int flags)
7761da177e4SLinus Torvalds {
7771da177e4SLinus Torvalds 	struct symbol *symbol;
7781da177e4SLinus Torvalds 	char *new_name;
779e66f25d7SAndi Kleen 	int hash;
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	if (name) {
7821da177e4SLinus Torvalds 		if (name[0] && !name[1]) {
7831da177e4SLinus Torvalds 			switch (name[0]) {
7841da177e4SLinus Torvalds 			case 'y': return &symbol_yes;
7851da177e4SLinus Torvalds 			case 'm': return &symbol_mod;
7861da177e4SLinus Torvalds 			case 'n': return &symbol_no;
7871da177e4SLinus Torvalds 			}
7881da177e4SLinus Torvalds 		}
789e66f25d7SAndi Kleen 		hash = strhash(name) % SYMBOL_HASHSIZE;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 		for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
792e66f25d7SAndi Kleen 			if (symbol->name &&
793e66f25d7SAndi Kleen 			    !strcmp(symbol->name, name) &&
7945a1aa8a1SRoman Zippel 			    (flags ? symbol->flags & flags
7955a1aa8a1SRoman Zippel 				   : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
7961da177e4SLinus Torvalds 				return symbol;
7971da177e4SLinus Torvalds 		}
7981da177e4SLinus Torvalds 		new_name = strdup(name);
7991da177e4SLinus Torvalds 	} else {
8001da177e4SLinus Torvalds 		new_name = NULL;
801e66f25d7SAndi Kleen 		hash = 0;
8021da177e4SLinus Torvalds 	}
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	symbol = malloc(sizeof(*symbol));
8051da177e4SLinus Torvalds 	memset(symbol, 0, sizeof(*symbol));
8061da177e4SLinus Torvalds 	symbol->name = new_name;
8071da177e4SLinus Torvalds 	symbol->type = S_UNKNOWN;
8085a1aa8a1SRoman Zippel 	symbol->flags |= flags;
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	symbol->next = symbol_hash[hash];
8111da177e4SLinus Torvalds 	symbol_hash[hash] = symbol;
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	return symbol;
8141da177e4SLinus Torvalds }
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds struct symbol *sym_find(const char *name)
8171da177e4SLinus Torvalds {
8181da177e4SLinus Torvalds 	struct symbol *symbol = NULL;
8191da177e4SLinus Torvalds 	int hash = 0;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	if (!name)
8221da177e4SLinus Torvalds 		return NULL;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	if (name[0] && !name[1]) {
8251da177e4SLinus Torvalds 		switch (name[0]) {
8261da177e4SLinus Torvalds 		case 'y': return &symbol_yes;
8271da177e4SLinus Torvalds 		case 'm': return &symbol_mod;
8281da177e4SLinus Torvalds 		case 'n': return &symbol_no;
8291da177e4SLinus Torvalds 		}
8301da177e4SLinus Torvalds 	}
831e66f25d7SAndi Kleen 	hash = strhash(name) % SYMBOL_HASHSIZE;
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
834e66f25d7SAndi Kleen 		if (symbol->name &&
835e66f25d7SAndi Kleen 		    !strcmp(symbol->name, name) &&
8361da177e4SLinus Torvalds 		    !(symbol->flags & SYMBOL_CONST))
8371da177e4SLinus Torvalds 				break;
8381da177e4SLinus Torvalds 	}
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	return symbol;
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds struct symbol **sym_re_search(const char *pattern)
8441da177e4SLinus Torvalds {
8451da177e4SLinus Torvalds 	struct symbol *sym, **sym_arr = NULL;
8461da177e4SLinus Torvalds 	int i, cnt, size;
8471da177e4SLinus Torvalds 	regex_t re;
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	cnt = size = 0;
8501da177e4SLinus Torvalds 	/* Skip if empty */
8511da177e4SLinus Torvalds 	if (strlen(pattern) == 0)
8521da177e4SLinus Torvalds 		return NULL;
8531da177e4SLinus Torvalds 	if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
8541da177e4SLinus Torvalds 		return NULL;
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
8571da177e4SLinus Torvalds 		if (sym->flags & SYMBOL_CONST || !sym->name)
8581da177e4SLinus Torvalds 			continue;
8591da177e4SLinus Torvalds 		if (regexec(&re, sym->name, 0, NULL, 0))
8601da177e4SLinus Torvalds 			continue;
8611da177e4SLinus Torvalds 		if (cnt + 1 >= size) {
8621da177e4SLinus Torvalds 			void *tmp = sym_arr;
8631da177e4SLinus Torvalds 			size += 16;
8641da177e4SLinus Torvalds 			sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
8651da177e4SLinus Torvalds 			if (!sym_arr) {
8661da177e4SLinus Torvalds 				free(tmp);
8671da177e4SLinus Torvalds 				return NULL;
8681da177e4SLinus Torvalds 			}
8691da177e4SLinus Torvalds 		}
870da6df879SLi Zefan 		sym_calc_value(sym);
8711da177e4SLinus Torvalds 		sym_arr[cnt++] = sym;
8721da177e4SLinus Torvalds 	}
8731da177e4SLinus Torvalds 	if (sym_arr)
8741da177e4SLinus Torvalds 		sym_arr[cnt] = NULL;
8751da177e4SLinus Torvalds 	regfree(&re);
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	return sym_arr;
8781da177e4SLinus Torvalds }
8791da177e4SLinus Torvalds 
880d595cea6SRoman Zippel /*
881d595cea6SRoman Zippel  * When we check for recursive dependencies we use a stack to save
882d595cea6SRoman Zippel  * current state so we can print out relevant info to user.
883d595cea6SRoman Zippel  * The entries are located on the call stack so no need to free memory.
884d595cea6SRoman Zippel  * Note inser() remove() must always match to properly clear the stack.
885d595cea6SRoman Zippel  */
886d595cea6SRoman Zippel static struct dep_stack {
887d595cea6SRoman Zippel 	struct dep_stack *prev, *next;
888d595cea6SRoman Zippel 	struct symbol *sym;
889d595cea6SRoman Zippel 	struct property *prop;
890d595cea6SRoman Zippel 	struct expr *expr;
891d595cea6SRoman Zippel } *check_top;
892d595cea6SRoman Zippel 
893d595cea6SRoman Zippel static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
894d595cea6SRoman Zippel {
895d595cea6SRoman Zippel 	memset(stack, 0, sizeof(*stack));
896d595cea6SRoman Zippel 	if (check_top)
897d595cea6SRoman Zippel 		check_top->next = stack;
898d595cea6SRoman Zippel 	stack->prev = check_top;
899d595cea6SRoman Zippel 	stack->sym = sym;
900d595cea6SRoman Zippel 	check_top = stack;
901d595cea6SRoman Zippel }
902d595cea6SRoman Zippel 
903d595cea6SRoman Zippel static void dep_stack_remove(void)
904d595cea6SRoman Zippel {
905d595cea6SRoman Zippel 	check_top = check_top->prev;
906d595cea6SRoman Zippel 	if (check_top)
907d595cea6SRoman Zippel 		check_top->next = NULL;
908d595cea6SRoman Zippel }
909d595cea6SRoman Zippel 
910d595cea6SRoman Zippel /*
911d595cea6SRoman Zippel  * Called when we have detected a recursive dependency.
912d595cea6SRoman Zippel  * check_top point to the top of the stact so we use
913d595cea6SRoman Zippel  * the ->prev pointer to locate the bottom of the stack.
914d595cea6SRoman Zippel  */
915d595cea6SRoman Zippel static void sym_check_print_recursive(struct symbol *last_sym)
916d595cea6SRoman Zippel {
917d595cea6SRoman Zippel 	struct dep_stack *stack;
918d595cea6SRoman Zippel 	struct symbol *sym, *next_sym;
919d595cea6SRoman Zippel 	struct menu *menu = NULL;
920d595cea6SRoman Zippel 	struct property *prop;
921d595cea6SRoman Zippel 	struct dep_stack cv_stack;
922d595cea6SRoman Zippel 
923d595cea6SRoman Zippel 	if (sym_is_choice_value(last_sym)) {
924d595cea6SRoman Zippel 		dep_stack_insert(&cv_stack, last_sym);
925d595cea6SRoman Zippel 		last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
926d595cea6SRoman Zippel 	}
927d595cea6SRoman Zippel 
928d595cea6SRoman Zippel 	for (stack = check_top; stack != NULL; stack = stack->prev)
929d595cea6SRoman Zippel 		if (stack->sym == last_sym)
930d595cea6SRoman Zippel 			break;
931d595cea6SRoman Zippel 	if (!stack) {
932d595cea6SRoman Zippel 		fprintf(stderr, "unexpected recursive dependency error\n");
933d595cea6SRoman Zippel 		return;
934d595cea6SRoman Zippel 	}
935d595cea6SRoman Zippel 
936d595cea6SRoman Zippel 	for (; stack; stack = stack->next) {
937d595cea6SRoman Zippel 		sym = stack->sym;
938d595cea6SRoman Zippel 		next_sym = stack->next ? stack->next->sym : last_sym;
939d595cea6SRoman Zippel 		prop = stack->prop;
940d595cea6SRoman Zippel 
941d595cea6SRoman Zippel 		/* for choice values find the menu entry (used below) */
942d595cea6SRoman Zippel 		if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
943d595cea6SRoman Zippel 			for (prop = sym->prop; prop; prop = prop->next) {
944d595cea6SRoman Zippel 				menu = prop->menu;
945d595cea6SRoman Zippel 				if (prop->menu)
946d595cea6SRoman Zippel 					break;
947d595cea6SRoman Zippel 			}
948d595cea6SRoman Zippel 		}
949d595cea6SRoman Zippel 		if (stack->sym == last_sym)
950d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
951d595cea6SRoman Zippel 				prop->file->name, prop->lineno);
952d595cea6SRoman Zippel 		if (stack->expr) {
953d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
954d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
955d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
956d595cea6SRoman Zippel 				prop_get_type_name(prop->type),
957d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
958d595cea6SRoman Zippel 		} else if (stack->prop) {
959d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
960d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
961d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
962d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
963d595cea6SRoman Zippel 		} else if (sym_is_choice(sym)) {
964d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
965d595cea6SRoman Zippel 				menu->file->name, menu->lineno,
966d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
967d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
968d595cea6SRoman Zippel 		} else if (sym_is_choice_value(sym)) {
969d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
970d595cea6SRoman Zippel 				menu->file->name, menu->lineno,
971d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
972d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
973d595cea6SRoman Zippel 		} else {
974d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
975d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
976d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
977d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
978d595cea6SRoman Zippel 		}
979d595cea6SRoman Zippel 	}
980d595cea6SRoman Zippel 
981d595cea6SRoman Zippel 	if (check_top == &cv_stack)
982d595cea6SRoman Zippel 		dep_stack_remove();
983d595cea6SRoman Zippel }
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds static struct symbol *sym_check_expr_deps(struct expr *e)
9861da177e4SLinus Torvalds {
9871da177e4SLinus Torvalds 	struct symbol *sym;
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	if (!e)
9901da177e4SLinus Torvalds 		return NULL;
9911da177e4SLinus Torvalds 	switch (e->type) {
9921da177e4SLinus Torvalds 	case E_OR:
9931da177e4SLinus Torvalds 	case E_AND:
9941da177e4SLinus Torvalds 		sym = sym_check_expr_deps(e->left.expr);
9951da177e4SLinus Torvalds 		if (sym)
9961da177e4SLinus Torvalds 			return sym;
9971da177e4SLinus Torvalds 		return sym_check_expr_deps(e->right.expr);
9981da177e4SLinus Torvalds 	case E_NOT:
9991da177e4SLinus Torvalds 		return sym_check_expr_deps(e->left.expr);
10001da177e4SLinus Torvalds 	case E_EQUAL:
10011da177e4SLinus Torvalds 	case E_UNEQUAL:
10021da177e4SLinus Torvalds 		sym = sym_check_deps(e->left.sym);
10031da177e4SLinus Torvalds 		if (sym)
10041da177e4SLinus Torvalds 			return sym;
10051da177e4SLinus Torvalds 		return sym_check_deps(e->right.sym);
10061da177e4SLinus Torvalds 	case E_SYMBOL:
10071da177e4SLinus Torvalds 		return sym_check_deps(e->left.sym);
10081da177e4SLinus Torvalds 	default:
10091da177e4SLinus Torvalds 		break;
10101da177e4SLinus Torvalds 	}
10111da177e4SLinus Torvalds 	printf("Oops! How to check %d?\n", e->type);
10121da177e4SLinus Torvalds 	return NULL;
10131da177e4SLinus Torvalds }
10141da177e4SLinus Torvalds 
10155447d34bSSam Ravnborg /* return NULL when dependencies are OK */
101648981178SRoman Zippel static struct symbol *sym_check_sym_deps(struct symbol *sym)
101748981178SRoman Zippel {
101848981178SRoman Zippel 	struct symbol *sym2;
101948981178SRoman Zippel 	struct property *prop;
1020d595cea6SRoman Zippel 	struct dep_stack stack;
1021d595cea6SRoman Zippel 
1022d595cea6SRoman Zippel 	dep_stack_insert(&stack, sym);
102348981178SRoman Zippel 
102448981178SRoman Zippel 	sym2 = sym_check_expr_deps(sym->rev_dep.expr);
102548981178SRoman Zippel 	if (sym2)
1026d595cea6SRoman Zippel 		goto out;
102748981178SRoman Zippel 
102848981178SRoman Zippel 	for (prop = sym->prop; prop; prop = prop->next) {
102948981178SRoman Zippel 		if (prop->type == P_CHOICE || prop->type == P_SELECT)
103048981178SRoman Zippel 			continue;
1031d595cea6SRoman Zippel 		stack.prop = prop;
103248981178SRoman Zippel 		sym2 = sym_check_expr_deps(prop->visible.expr);
103348981178SRoman Zippel 		if (sym2)
103448981178SRoman Zippel 			break;
103548981178SRoman Zippel 		if (prop->type != P_DEFAULT || sym_is_choice(sym))
103648981178SRoman Zippel 			continue;
1037d595cea6SRoman Zippel 		stack.expr = prop->expr;
103848981178SRoman Zippel 		sym2 = sym_check_expr_deps(prop->expr);
103948981178SRoman Zippel 		if (sym2)
104048981178SRoman Zippel 			break;
1041d595cea6SRoman Zippel 		stack.expr = NULL;
104248981178SRoman Zippel 	}
104348981178SRoman Zippel 
1044d595cea6SRoman Zippel out:
1045d595cea6SRoman Zippel 	dep_stack_remove();
1046d595cea6SRoman Zippel 
104748981178SRoman Zippel 	return sym2;
104848981178SRoman Zippel }
104948981178SRoman Zippel 
105048981178SRoman Zippel static struct symbol *sym_check_choice_deps(struct symbol *choice)
105148981178SRoman Zippel {
105248981178SRoman Zippel 	struct symbol *sym, *sym2;
105348981178SRoman Zippel 	struct property *prop;
105448981178SRoman Zippel 	struct expr *e;
1055d595cea6SRoman Zippel 	struct dep_stack stack;
1056d595cea6SRoman Zippel 
1057d595cea6SRoman Zippel 	dep_stack_insert(&stack, choice);
105848981178SRoman Zippel 
105948981178SRoman Zippel 	prop = sym_get_choice_prop(choice);
106048981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym)
106148981178SRoman Zippel 		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
106248981178SRoman Zippel 
106348981178SRoman Zippel 	choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
106448981178SRoman Zippel 	sym2 = sym_check_sym_deps(choice);
106548981178SRoman Zippel 	choice->flags &= ~SYMBOL_CHECK;
106648981178SRoman Zippel 	if (sym2)
106748981178SRoman Zippel 		goto out;
106848981178SRoman Zippel 
106948981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym) {
107048981178SRoman Zippel 		sym2 = sym_check_sym_deps(sym);
1071d595cea6SRoman Zippel 		if (sym2)
107248981178SRoman Zippel 			break;
107348981178SRoman Zippel 	}
107448981178SRoman Zippel out:
107548981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym)
107648981178SRoman Zippel 		sym->flags &= ~SYMBOL_CHECK;
107748981178SRoman Zippel 
107848981178SRoman Zippel 	if (sym2 && sym_is_choice_value(sym2) &&
107948981178SRoman Zippel 	    prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
108048981178SRoman Zippel 		sym2 = choice;
108148981178SRoman Zippel 
1082d595cea6SRoman Zippel 	dep_stack_remove();
1083d595cea6SRoman Zippel 
108448981178SRoman Zippel 	return sym2;
108548981178SRoman Zippel }
108648981178SRoman Zippel 
10871da177e4SLinus Torvalds struct symbol *sym_check_deps(struct symbol *sym)
10881da177e4SLinus Torvalds {
10891da177e4SLinus Torvalds 	struct symbol *sym2;
10901da177e4SLinus Torvalds 	struct property *prop;
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	if (sym->flags & SYMBOL_CHECK) {
1093d595cea6SRoman Zippel 		sym_check_print_recursive(sym);
10941da177e4SLinus Torvalds 		return sym;
10951da177e4SLinus Torvalds 	}
10963f04e7ddSDavid Gibson 	if (sym->flags & SYMBOL_CHECKED)
10973f04e7ddSDavid Gibson 		return NULL;
10981da177e4SLinus Torvalds 
109948981178SRoman Zippel 	if (sym_is_choice_value(sym)) {
1100d595cea6SRoman Zippel 		struct dep_stack stack;
1101d595cea6SRoman Zippel 
110248981178SRoman Zippel 		/* for choice groups start the check with main choice symbol */
1103d595cea6SRoman Zippel 		dep_stack_insert(&stack, sym);
110448981178SRoman Zippel 		prop = sym_get_choice_prop(sym);
110548981178SRoman Zippel 		sym2 = sym_check_deps(prop_get_symbol(prop));
1106d595cea6SRoman Zippel 		dep_stack_remove();
110748981178SRoman Zippel 	} else if (sym_is_choice(sym)) {
110848981178SRoman Zippel 		sym2 = sym_check_choice_deps(sym);
110948981178SRoman Zippel 	} else {
11101da177e4SLinus Torvalds 		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
111148981178SRoman Zippel 		sym2 = sym_check_sym_deps(sym);
11121da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_CHECK;
111348981178SRoman Zippel 	}
111448981178SRoman Zippel 
1115d595cea6SRoman Zippel 	if (sym2 && sym2 == sym)
111648981178SRoman Zippel 		sym2 = NULL;
111748981178SRoman Zippel 
11181da177e4SLinus Torvalds 	return sym2;
11191da177e4SLinus Torvalds }
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds struct property *prop_alloc(enum prop_type type, struct symbol *sym)
11221da177e4SLinus Torvalds {
11231da177e4SLinus Torvalds 	struct property *prop;
11241da177e4SLinus Torvalds 	struct property **propp;
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 	prop = malloc(sizeof(*prop));
11271da177e4SLinus Torvalds 	memset(prop, 0, sizeof(*prop));
11281da177e4SLinus Torvalds 	prop->type = type;
11291da177e4SLinus Torvalds 	prop->sym = sym;
11301da177e4SLinus Torvalds 	prop->file = current_file;
11311da177e4SLinus Torvalds 	prop->lineno = zconf_lineno();
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 	/* append property to the prop list of symbol */
11341da177e4SLinus Torvalds 	if (sym) {
11351da177e4SLinus Torvalds 		for (propp = &sym->prop; *propp; propp = &(*propp)->next)
11361da177e4SLinus Torvalds 			;
11371da177e4SLinus Torvalds 		*propp = prop;
11381da177e4SLinus Torvalds 	}
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	return prop;
11411da177e4SLinus Torvalds }
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds struct symbol *prop_get_symbol(struct property *prop)
11441da177e4SLinus Torvalds {
11451da177e4SLinus Torvalds 	if (prop->expr && (prop->expr->type == E_SYMBOL ||
11467a962923SRoman Zippel 			   prop->expr->type == E_LIST))
11471da177e4SLinus Torvalds 		return prop->expr->left.sym;
11481da177e4SLinus Torvalds 	return NULL;
11491da177e4SLinus Torvalds }
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds const char *prop_get_type_name(enum prop_type type)
11521da177e4SLinus Torvalds {
11531da177e4SLinus Torvalds 	switch (type) {
11541da177e4SLinus Torvalds 	case P_PROMPT:
11551da177e4SLinus Torvalds 		return "prompt";
115693449082SRoman Zippel 	case P_ENV:
115793449082SRoman Zippel 		return "env";
11581da177e4SLinus Torvalds 	case P_COMMENT:
11591da177e4SLinus Torvalds 		return "comment";
11601da177e4SLinus Torvalds 	case P_MENU:
11611da177e4SLinus Torvalds 		return "menu";
11621da177e4SLinus Torvalds 	case P_DEFAULT:
11631da177e4SLinus Torvalds 		return "default";
11641da177e4SLinus Torvalds 	case P_CHOICE:
11651da177e4SLinus Torvalds 		return "choice";
11661da177e4SLinus Torvalds 	case P_SELECT:
11671da177e4SLinus Torvalds 		return "select";
11681da177e4SLinus Torvalds 	case P_RANGE:
11691da177e4SLinus Torvalds 		return "range";
117059e89e3dSSam Ravnborg 	case P_SYMBOL:
117159e89e3dSSam Ravnborg 		return "symbol";
11721da177e4SLinus Torvalds 	case P_UNKNOWN:
11731da177e4SLinus Torvalds 		break;
11741da177e4SLinus Torvalds 	}
11751da177e4SLinus Torvalds 	return "unknown";
11761da177e4SLinus Torvalds }
117793449082SRoman Zippel 
11784356f489STrevor Keith static void prop_add_env(const char *env)
117993449082SRoman Zippel {
118093449082SRoman Zippel 	struct symbol *sym, *sym2;
118193449082SRoman Zippel 	struct property *prop;
118293449082SRoman Zippel 	char *p;
118393449082SRoman Zippel 
118493449082SRoman Zippel 	sym = current_entry->sym;
118593449082SRoman Zippel 	sym->flags |= SYMBOL_AUTO;
118693449082SRoman Zippel 	for_all_properties(sym, prop, P_ENV) {
118793449082SRoman Zippel 		sym2 = prop_get_symbol(prop);
118893449082SRoman Zippel 		if (strcmp(sym2->name, env))
118993449082SRoman Zippel 			menu_warn(current_entry, "redefining environment symbol from %s",
119093449082SRoman Zippel 				  sym2->name);
119193449082SRoman Zippel 		return;
119293449082SRoman Zippel 	}
119393449082SRoman Zippel 
119493449082SRoman Zippel 	prop = prop_alloc(P_ENV, sym);
11955a1aa8a1SRoman Zippel 	prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
119693449082SRoman Zippel 
119793449082SRoman Zippel 	sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
119893449082SRoman Zippel 	sym_env_list->right.sym = sym;
119993449082SRoman Zippel 
120093449082SRoman Zippel 	p = getenv(env);
120193449082SRoman Zippel 	if (p)
120293449082SRoman Zippel 		sym_add_default(sym, p);
120393449082SRoman Zippel 	else
120493449082SRoman Zippel 		menu_warn(current_entry, "environment variable %s undefined", env);
120593449082SRoman Zippel }
1206