xref: /openbmc/u-boot/scripts/kconfig/symbol.c (revision e91610da7c8a9fe42f3e5a75f06c3d1a0cb5f815)
10a9064fbSMasahiro Yamada /*
20a9064fbSMasahiro Yamada  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
30a9064fbSMasahiro Yamada  * Released under the terms of the GNU GPL v2.0.
40a9064fbSMasahiro Yamada  */
50a9064fbSMasahiro Yamada 
60a9064fbSMasahiro Yamada #include <ctype.h>
70a9064fbSMasahiro Yamada #include <stdlib.h>
80a9064fbSMasahiro Yamada #include <string.h>
90a9064fbSMasahiro Yamada #include <regex.h>
100a9064fbSMasahiro Yamada #include <sys/utsname.h>
110a9064fbSMasahiro Yamada 
120a9064fbSMasahiro Yamada #include "lkc.h"
130a9064fbSMasahiro Yamada 
140a9064fbSMasahiro Yamada struct symbol symbol_yes = {
150a9064fbSMasahiro Yamada 	.name = "y",
160a9064fbSMasahiro Yamada 	.curr = { "y", yes },
170a9064fbSMasahiro Yamada 	.flags = SYMBOL_CONST|SYMBOL_VALID,
180a9064fbSMasahiro Yamada }, symbol_mod = {
190a9064fbSMasahiro Yamada 	.name = "m",
200a9064fbSMasahiro Yamada 	.curr = { "m", mod },
210a9064fbSMasahiro Yamada 	.flags = SYMBOL_CONST|SYMBOL_VALID,
220a9064fbSMasahiro Yamada }, symbol_no = {
230a9064fbSMasahiro Yamada 	.name = "n",
240a9064fbSMasahiro Yamada 	.curr = { "n", no },
250a9064fbSMasahiro Yamada 	.flags = SYMBOL_CONST|SYMBOL_VALID,
260a9064fbSMasahiro Yamada }, symbol_empty = {
270a9064fbSMasahiro Yamada 	.name = "",
280a9064fbSMasahiro Yamada 	.curr = { "", no },
290a9064fbSMasahiro Yamada 	.flags = SYMBOL_VALID,
300a9064fbSMasahiro Yamada };
310a9064fbSMasahiro Yamada 
320a9064fbSMasahiro Yamada struct symbol *sym_defconfig_list;
330a9064fbSMasahiro Yamada struct symbol *modules_sym;
340a9064fbSMasahiro Yamada tristate modules_val;
350a9064fbSMasahiro Yamada 
360a9064fbSMasahiro Yamada struct expr *sym_env_list;
370a9064fbSMasahiro Yamada 
sym_add_default(struct symbol * sym,const char * def)380a9064fbSMasahiro Yamada static void sym_add_default(struct symbol *sym, const char *def)
390a9064fbSMasahiro Yamada {
400a9064fbSMasahiro Yamada 	struct property *prop = prop_alloc(P_DEFAULT, sym);
410a9064fbSMasahiro Yamada 
420a9064fbSMasahiro Yamada 	prop->expr = expr_alloc_symbol(sym_lookup(def, SYMBOL_CONST));
430a9064fbSMasahiro Yamada }
440a9064fbSMasahiro Yamada 
sym_init(void)450a9064fbSMasahiro Yamada void sym_init(void)
460a9064fbSMasahiro Yamada {
470a9064fbSMasahiro Yamada 	struct symbol *sym;
480a9064fbSMasahiro Yamada 	struct utsname uts;
490a9064fbSMasahiro Yamada 	static bool inited = false;
500a9064fbSMasahiro Yamada 
510a9064fbSMasahiro Yamada 	if (inited)
520a9064fbSMasahiro Yamada 		return;
530a9064fbSMasahiro Yamada 	inited = true;
540a9064fbSMasahiro Yamada 
550a9064fbSMasahiro Yamada 	uname(&uts);
560a9064fbSMasahiro Yamada 
570a9064fbSMasahiro Yamada 	sym = sym_lookup("UNAME_RELEASE", 0);
580a9064fbSMasahiro Yamada 	sym->type = S_STRING;
590a9064fbSMasahiro Yamada 	sym->flags |= SYMBOL_AUTO;
600a9064fbSMasahiro Yamada 	sym_add_default(sym, uts.release);
610a9064fbSMasahiro Yamada }
620a9064fbSMasahiro Yamada 
sym_get_type(struct symbol * sym)630a9064fbSMasahiro Yamada enum symbol_type sym_get_type(struct symbol *sym)
640a9064fbSMasahiro Yamada {
650a9064fbSMasahiro Yamada 	enum symbol_type type = sym->type;
660a9064fbSMasahiro Yamada 
670a9064fbSMasahiro Yamada 	if (type == S_TRISTATE) {
680a9064fbSMasahiro Yamada 		if (sym_is_choice_value(sym) && sym->visible == yes)
690a9064fbSMasahiro Yamada 			type = S_BOOLEAN;
700a9064fbSMasahiro Yamada 		else if (modules_val == no)
710a9064fbSMasahiro Yamada 			type = S_BOOLEAN;
720a9064fbSMasahiro Yamada 	}
730a9064fbSMasahiro Yamada 	return type;
740a9064fbSMasahiro Yamada }
750a9064fbSMasahiro Yamada 
sym_type_name(enum symbol_type type)760a9064fbSMasahiro Yamada const char *sym_type_name(enum symbol_type type)
770a9064fbSMasahiro Yamada {
780a9064fbSMasahiro Yamada 	switch (type) {
790a9064fbSMasahiro Yamada 	case S_BOOLEAN:
80*e91610daSEugeniu Rosca 		return "bool";
810a9064fbSMasahiro Yamada 	case S_TRISTATE:
820a9064fbSMasahiro Yamada 		return "tristate";
830a9064fbSMasahiro Yamada 	case S_INT:
840a9064fbSMasahiro Yamada 		return "integer";
850a9064fbSMasahiro Yamada 	case S_HEX:
860a9064fbSMasahiro Yamada 		return "hex";
870a9064fbSMasahiro Yamada 	case S_STRING:
880a9064fbSMasahiro Yamada 		return "string";
890a9064fbSMasahiro Yamada 	case S_UNKNOWN:
900a9064fbSMasahiro Yamada 		return "unknown";
910a9064fbSMasahiro Yamada 	case S_OTHER:
920a9064fbSMasahiro Yamada 		break;
930a9064fbSMasahiro Yamada 	}
940a9064fbSMasahiro Yamada 	return "???";
950a9064fbSMasahiro Yamada }
960a9064fbSMasahiro Yamada 
sym_get_choice_prop(struct symbol * sym)970a9064fbSMasahiro Yamada struct property *sym_get_choice_prop(struct symbol *sym)
980a9064fbSMasahiro Yamada {
990a9064fbSMasahiro Yamada 	struct property *prop;
1000a9064fbSMasahiro Yamada 
1010a9064fbSMasahiro Yamada 	for_all_choices(sym, prop)
1020a9064fbSMasahiro Yamada 		return prop;
1030a9064fbSMasahiro Yamada 	return NULL;
1040a9064fbSMasahiro Yamada }
1050a9064fbSMasahiro Yamada 
sym_get_env_prop(struct symbol * sym)1060a9064fbSMasahiro Yamada struct property *sym_get_env_prop(struct symbol *sym)
1070a9064fbSMasahiro Yamada {
1080a9064fbSMasahiro Yamada 	struct property *prop;
1090a9064fbSMasahiro Yamada 
1100a9064fbSMasahiro Yamada 	for_all_properties(sym, prop, P_ENV)
1110a9064fbSMasahiro Yamada 		return prop;
1120a9064fbSMasahiro Yamada 	return NULL;
1130a9064fbSMasahiro Yamada }
1140a9064fbSMasahiro Yamada 
sym_get_default_prop(struct symbol * sym)1159b5f0b1dSMasahiro Yamada static struct property *sym_get_default_prop(struct symbol *sym)
1160a9064fbSMasahiro Yamada {
1170a9064fbSMasahiro Yamada 	struct property *prop;
1180a9064fbSMasahiro Yamada 
1190a9064fbSMasahiro Yamada 	for_all_defaults(sym, prop) {
1200a9064fbSMasahiro Yamada 		prop->visible.tri = expr_calc_value(prop->visible.expr);
1210a9064fbSMasahiro Yamada 		if (prop->visible.tri != no)
1220a9064fbSMasahiro Yamada 			return prop;
1230a9064fbSMasahiro Yamada 	}
1240a9064fbSMasahiro Yamada 	return NULL;
1250a9064fbSMasahiro Yamada }
1260a9064fbSMasahiro Yamada 
sym_get_range_prop(struct symbol * sym)1270a9064fbSMasahiro Yamada static struct property *sym_get_range_prop(struct symbol *sym)
1280a9064fbSMasahiro Yamada {
1290a9064fbSMasahiro Yamada 	struct property *prop;
1300a9064fbSMasahiro Yamada 
1310a9064fbSMasahiro Yamada 	for_all_properties(sym, prop, P_RANGE) {
1320a9064fbSMasahiro Yamada 		prop->visible.tri = expr_calc_value(prop->visible.expr);
1330a9064fbSMasahiro Yamada 		if (prop->visible.tri != no)
1340a9064fbSMasahiro Yamada 			return prop;
1350a9064fbSMasahiro Yamada 	}
1360a9064fbSMasahiro Yamada 	return NULL;
1370a9064fbSMasahiro Yamada }
1380a9064fbSMasahiro Yamada 
sym_get_range_val(struct symbol * sym,int base)1390a9064fbSMasahiro Yamada static long long sym_get_range_val(struct symbol *sym, int base)
1400a9064fbSMasahiro Yamada {
1410a9064fbSMasahiro Yamada 	sym_calc_value(sym);
1420a9064fbSMasahiro Yamada 	switch (sym->type) {
1430a9064fbSMasahiro Yamada 	case S_INT:
1440a9064fbSMasahiro Yamada 		base = 10;
1450a9064fbSMasahiro Yamada 		break;
1460a9064fbSMasahiro Yamada 	case S_HEX:
1470a9064fbSMasahiro Yamada 		base = 16;
1480a9064fbSMasahiro Yamada 		break;
1490a9064fbSMasahiro Yamada 	default:
1500a9064fbSMasahiro Yamada 		break;
1510a9064fbSMasahiro Yamada 	}
1520a9064fbSMasahiro Yamada 	return strtoll(sym->curr.val, NULL, base);
1530a9064fbSMasahiro Yamada }
1540a9064fbSMasahiro Yamada 
sym_validate_range(struct symbol * sym)1550a9064fbSMasahiro Yamada static void sym_validate_range(struct symbol *sym)
1560a9064fbSMasahiro Yamada {
1570a9064fbSMasahiro Yamada 	struct property *prop;
1580a9064fbSMasahiro Yamada 	int base;
1590a9064fbSMasahiro Yamada 	long long val, val2;
1600a9064fbSMasahiro Yamada 	char str[64];
1610a9064fbSMasahiro Yamada 
1620a9064fbSMasahiro Yamada 	switch (sym->type) {
1630a9064fbSMasahiro Yamada 	case S_INT:
1640a9064fbSMasahiro Yamada 		base = 10;
1650a9064fbSMasahiro Yamada 		break;
1660a9064fbSMasahiro Yamada 	case S_HEX:
1670a9064fbSMasahiro Yamada 		base = 16;
1680a9064fbSMasahiro Yamada 		break;
1690a9064fbSMasahiro Yamada 	default:
1700a9064fbSMasahiro Yamada 		return;
1710a9064fbSMasahiro Yamada 	}
1720a9064fbSMasahiro Yamada 	prop = sym_get_range_prop(sym);
1730a9064fbSMasahiro Yamada 	if (!prop)
1740a9064fbSMasahiro Yamada 		return;
1750a9064fbSMasahiro Yamada 	val = strtoll(sym->curr.val, NULL, base);
1760a9064fbSMasahiro Yamada 	val2 = sym_get_range_val(prop->expr->left.sym, base);
1770a9064fbSMasahiro Yamada 	if (val >= val2) {
1780a9064fbSMasahiro Yamada 		val2 = sym_get_range_val(prop->expr->right.sym, base);
1790a9064fbSMasahiro Yamada 		if (val <= val2)
1800a9064fbSMasahiro Yamada 			return;
1810a9064fbSMasahiro Yamada 	}
1820a9064fbSMasahiro Yamada 	if (sym->type == S_INT)
1830a9064fbSMasahiro Yamada 		sprintf(str, "%lld", val2);
1840a9064fbSMasahiro Yamada 	else
1850a9064fbSMasahiro Yamada 		sprintf(str, "0x%llx", val2);
186*e91610daSEugeniu Rosca 	sym->curr.val = xstrdup(str);
1870a9064fbSMasahiro Yamada }
1880a9064fbSMasahiro Yamada 
sym_set_changed(struct symbol * sym)1899b5f0b1dSMasahiro Yamada static void sym_set_changed(struct symbol *sym)
1909b5f0b1dSMasahiro Yamada {
1919b5f0b1dSMasahiro Yamada 	struct property *prop;
1929b5f0b1dSMasahiro Yamada 
1939b5f0b1dSMasahiro Yamada 	sym->flags |= SYMBOL_CHANGED;
1949b5f0b1dSMasahiro Yamada 	for (prop = sym->prop; prop; prop = prop->next) {
1959b5f0b1dSMasahiro Yamada 		if (prop->menu)
1969b5f0b1dSMasahiro Yamada 			prop->menu->flags |= MENU_CHANGED;
1979b5f0b1dSMasahiro Yamada 	}
1989b5f0b1dSMasahiro Yamada }
1999b5f0b1dSMasahiro Yamada 
sym_set_all_changed(void)2009b5f0b1dSMasahiro Yamada static void sym_set_all_changed(void)
2019b5f0b1dSMasahiro Yamada {
2029b5f0b1dSMasahiro Yamada 	struct symbol *sym;
2039b5f0b1dSMasahiro Yamada 	int i;
2049b5f0b1dSMasahiro Yamada 
2059b5f0b1dSMasahiro Yamada 	for_all_symbols(i, sym)
2069b5f0b1dSMasahiro Yamada 		sym_set_changed(sym);
2079b5f0b1dSMasahiro Yamada }
2089b5f0b1dSMasahiro Yamada 
sym_calc_visibility(struct symbol * sym)2090a9064fbSMasahiro Yamada static void sym_calc_visibility(struct symbol *sym)
2100a9064fbSMasahiro Yamada {
2110a9064fbSMasahiro Yamada 	struct property *prop;
212bf7ab1e7SMasahiro Yamada 	struct symbol *choice_sym = NULL;
2130a9064fbSMasahiro Yamada 	tristate tri;
2140a9064fbSMasahiro Yamada 
2150a9064fbSMasahiro Yamada 	/* any prompt visible? */
2160a9064fbSMasahiro Yamada 	tri = no;
217bf7ab1e7SMasahiro Yamada 
218bf7ab1e7SMasahiro Yamada 	if (sym_is_choice_value(sym))
219bf7ab1e7SMasahiro Yamada 		choice_sym = prop_get_symbol(sym_get_choice_prop(sym));
220bf7ab1e7SMasahiro Yamada 
2210a9064fbSMasahiro Yamada 	for_all_prompts(sym, prop) {
2220a9064fbSMasahiro Yamada 		prop->visible.tri = expr_calc_value(prop->visible.expr);
223bf7ab1e7SMasahiro Yamada 		/*
224bf7ab1e7SMasahiro Yamada 		 * Tristate choice_values with visibility 'mod' are
225bf7ab1e7SMasahiro Yamada 		 * not visible if the corresponding choice's value is
226bf7ab1e7SMasahiro Yamada 		 * 'yes'.
227bf7ab1e7SMasahiro Yamada 		 */
228bf7ab1e7SMasahiro Yamada 		if (choice_sym && sym->type == S_TRISTATE &&
229bf7ab1e7SMasahiro Yamada 		    prop->visible.tri == mod && choice_sym->curr.tri == yes)
230bf7ab1e7SMasahiro Yamada 			prop->visible.tri = no;
231bf7ab1e7SMasahiro Yamada 
2320a9064fbSMasahiro Yamada 		tri = EXPR_OR(tri, prop->visible.tri);
2330a9064fbSMasahiro Yamada 	}
2340a9064fbSMasahiro Yamada 	if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
2350a9064fbSMasahiro Yamada 		tri = yes;
2360a9064fbSMasahiro Yamada 	if (sym->visible != tri) {
2370a9064fbSMasahiro Yamada 		sym->visible = tri;
2380a9064fbSMasahiro Yamada 		sym_set_changed(sym);
2390a9064fbSMasahiro Yamada 	}
2400a9064fbSMasahiro Yamada 	if (sym_is_choice_value(sym))
2410a9064fbSMasahiro Yamada 		return;
2420a9064fbSMasahiro Yamada 	/* defaulting to "yes" if no explicit "depends on" are given */
2430a9064fbSMasahiro Yamada 	tri = yes;
2440a9064fbSMasahiro Yamada 	if (sym->dir_dep.expr)
2450a9064fbSMasahiro Yamada 		tri = expr_calc_value(sym->dir_dep.expr);
246*e91610daSEugeniu Rosca 	if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
2470a9064fbSMasahiro Yamada 		tri = yes;
2480a9064fbSMasahiro Yamada 	if (sym->dir_dep.tri != tri) {
2490a9064fbSMasahiro Yamada 		sym->dir_dep.tri = tri;
2500a9064fbSMasahiro Yamada 		sym_set_changed(sym);
2510a9064fbSMasahiro Yamada 	}
2520a9064fbSMasahiro Yamada 	tri = no;
2530a9064fbSMasahiro Yamada 	if (sym->rev_dep.expr)
2540a9064fbSMasahiro Yamada 		tri = expr_calc_value(sym->rev_dep.expr);
2550a9064fbSMasahiro Yamada 	if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
2560a9064fbSMasahiro Yamada 		tri = yes;
2570a9064fbSMasahiro Yamada 	if (sym->rev_dep.tri != tri) {
2580a9064fbSMasahiro Yamada 		sym->rev_dep.tri = tri;
2590a9064fbSMasahiro Yamada 		sym_set_changed(sym);
2600a9064fbSMasahiro Yamada 	}
261bf7ab1e7SMasahiro Yamada 	tri = no;
262bf7ab1e7SMasahiro Yamada 	if (sym->implied.expr && sym->dir_dep.tri != no)
263bf7ab1e7SMasahiro Yamada 		tri = expr_calc_value(sym->implied.expr);
264bf7ab1e7SMasahiro Yamada 	if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
265bf7ab1e7SMasahiro Yamada 		tri = yes;
266bf7ab1e7SMasahiro Yamada 	if (sym->implied.tri != tri) {
267bf7ab1e7SMasahiro Yamada 		sym->implied.tri = tri;
268bf7ab1e7SMasahiro Yamada 		sym_set_changed(sym);
269bf7ab1e7SMasahiro Yamada 	}
2700a9064fbSMasahiro Yamada }
2710a9064fbSMasahiro Yamada 
2720a9064fbSMasahiro Yamada /*
2730a9064fbSMasahiro Yamada  * Find the default symbol for a choice.
2740a9064fbSMasahiro Yamada  * First try the default values for the choice symbol
2750a9064fbSMasahiro Yamada  * Next locate the first visible choice value
2760a9064fbSMasahiro Yamada  * Return NULL if none was found
2770a9064fbSMasahiro Yamada  */
sym_choice_default(struct symbol * sym)2780a9064fbSMasahiro Yamada struct symbol *sym_choice_default(struct symbol *sym)
2790a9064fbSMasahiro Yamada {
2800a9064fbSMasahiro Yamada 	struct symbol *def_sym;
2810a9064fbSMasahiro Yamada 	struct property *prop;
2820a9064fbSMasahiro Yamada 	struct expr *e;
2830a9064fbSMasahiro Yamada 
2840a9064fbSMasahiro Yamada 	/* any of the defaults visible? */
2850a9064fbSMasahiro Yamada 	for_all_defaults(sym, prop) {
2860a9064fbSMasahiro Yamada 		prop->visible.tri = expr_calc_value(prop->visible.expr);
2870a9064fbSMasahiro Yamada 		if (prop->visible.tri == no)
2880a9064fbSMasahiro Yamada 			continue;
2890a9064fbSMasahiro Yamada 		def_sym = prop_get_symbol(prop);
2900a9064fbSMasahiro Yamada 		if (def_sym->visible != no)
2910a9064fbSMasahiro Yamada 			return def_sym;
2920a9064fbSMasahiro Yamada 	}
2930a9064fbSMasahiro Yamada 
2940a9064fbSMasahiro Yamada 	/* just get the first visible value */
2950a9064fbSMasahiro Yamada 	prop = sym_get_choice_prop(sym);
2960a9064fbSMasahiro Yamada 	expr_list_for_each_sym(prop->expr, e, def_sym)
2970a9064fbSMasahiro Yamada 		if (def_sym->visible != no)
2980a9064fbSMasahiro Yamada 			return def_sym;
2990a9064fbSMasahiro Yamada 
3000a9064fbSMasahiro Yamada 	/* failed to locate any defaults */
3010a9064fbSMasahiro Yamada 	return NULL;
3020a9064fbSMasahiro Yamada }
3030a9064fbSMasahiro Yamada 
sym_calc_choice(struct symbol * sym)3040a9064fbSMasahiro Yamada static struct symbol *sym_calc_choice(struct symbol *sym)
3050a9064fbSMasahiro Yamada {
3060a9064fbSMasahiro Yamada 	struct symbol *def_sym;
3070a9064fbSMasahiro Yamada 	struct property *prop;
3080a9064fbSMasahiro Yamada 	struct expr *e;
3090a9064fbSMasahiro Yamada 	int flags;
3100a9064fbSMasahiro Yamada 
3110a9064fbSMasahiro Yamada 	/* first calculate all choice values' visibilities */
3120a9064fbSMasahiro Yamada 	flags = sym->flags;
3130a9064fbSMasahiro Yamada 	prop = sym_get_choice_prop(sym);
3140a9064fbSMasahiro Yamada 	expr_list_for_each_sym(prop->expr, e, def_sym) {
3150a9064fbSMasahiro Yamada 		sym_calc_visibility(def_sym);
3160a9064fbSMasahiro Yamada 		if (def_sym->visible != no)
3170a9064fbSMasahiro Yamada 			flags &= def_sym->flags;
3180a9064fbSMasahiro Yamada 	}
3190a9064fbSMasahiro Yamada 
3200a9064fbSMasahiro Yamada 	sym->flags &= flags | ~SYMBOL_DEF_USER;
3210a9064fbSMasahiro Yamada 
3220a9064fbSMasahiro Yamada 	/* is the user choice visible? */
3230a9064fbSMasahiro Yamada 	def_sym = sym->def[S_DEF_USER].val;
3240a9064fbSMasahiro Yamada 	if (def_sym && def_sym->visible != no)
3250a9064fbSMasahiro Yamada 		return def_sym;
3260a9064fbSMasahiro Yamada 
3270a9064fbSMasahiro Yamada 	def_sym = sym_choice_default(sym);
3280a9064fbSMasahiro Yamada 
3290a9064fbSMasahiro Yamada 	if (def_sym == NULL)
3300a9064fbSMasahiro Yamada 		/* no choice? reset tristate value */
3310a9064fbSMasahiro Yamada 		sym->curr.tri = no;
3320a9064fbSMasahiro Yamada 
3330a9064fbSMasahiro Yamada 	return def_sym;
3340a9064fbSMasahiro Yamada }
3350a9064fbSMasahiro Yamada 
sym_warn_unmet_dep(struct symbol * sym)336*e91610daSEugeniu Rosca static void sym_warn_unmet_dep(struct symbol *sym)
337*e91610daSEugeniu Rosca {
338*e91610daSEugeniu Rosca 	struct gstr gs = str_new();
339*e91610daSEugeniu Rosca 
340*e91610daSEugeniu Rosca 	str_printf(&gs,
341*e91610daSEugeniu Rosca 		   "\nWARNING: unmet direct dependencies detected for %s\n",
342*e91610daSEugeniu Rosca 		   sym->name);
343*e91610daSEugeniu Rosca 	str_printf(&gs,
344*e91610daSEugeniu Rosca 		   "  Depends on [%c]: ",
345*e91610daSEugeniu Rosca 		   sym->dir_dep.tri == mod ? 'm' : 'n');
346*e91610daSEugeniu Rosca 	expr_gstr_print(sym->dir_dep.expr, &gs);
347*e91610daSEugeniu Rosca 	str_printf(&gs, "\n");
348*e91610daSEugeniu Rosca 
349*e91610daSEugeniu Rosca 	expr_gstr_print_revdep(sym->rev_dep.expr, &gs, yes,
350*e91610daSEugeniu Rosca 			       "  Selected by [y]:\n");
351*e91610daSEugeniu Rosca 	expr_gstr_print_revdep(sym->rev_dep.expr, &gs, mod,
352*e91610daSEugeniu Rosca 			       "  Selected by [m]:\n");
353*e91610daSEugeniu Rosca 
354*e91610daSEugeniu Rosca 	fputs(str_get(&gs), stderr);
355*e91610daSEugeniu Rosca }
356*e91610daSEugeniu Rosca 
sym_calc_value(struct symbol * sym)3570a9064fbSMasahiro Yamada void sym_calc_value(struct symbol *sym)
3580a9064fbSMasahiro Yamada {
3590a9064fbSMasahiro Yamada 	struct symbol_value newval, oldval;
3600a9064fbSMasahiro Yamada 	struct property *prop;
3610a9064fbSMasahiro Yamada 	struct expr *e;
3620a9064fbSMasahiro Yamada 
3630a9064fbSMasahiro Yamada 	if (!sym)
3640a9064fbSMasahiro Yamada 		return;
3650a9064fbSMasahiro Yamada 
3660a9064fbSMasahiro Yamada 	if (sym->flags & SYMBOL_VALID)
3670a9064fbSMasahiro Yamada 		return;
3680a9064fbSMasahiro Yamada 
3690a9064fbSMasahiro Yamada 	if (sym_is_choice_value(sym) &&
3700a9064fbSMasahiro Yamada 	    sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES) {
3710a9064fbSMasahiro Yamada 		sym->flags &= ~SYMBOL_NEED_SET_CHOICE_VALUES;
3720a9064fbSMasahiro Yamada 		prop = sym_get_choice_prop(sym);
3730a9064fbSMasahiro Yamada 		sym_calc_value(prop_get_symbol(prop));
3740a9064fbSMasahiro Yamada 	}
3750a9064fbSMasahiro Yamada 
3760a9064fbSMasahiro Yamada 	sym->flags |= SYMBOL_VALID;
3770a9064fbSMasahiro Yamada 
3780a9064fbSMasahiro Yamada 	oldval = sym->curr;
3790a9064fbSMasahiro Yamada 
3800a9064fbSMasahiro Yamada 	switch (sym->type) {
3810a9064fbSMasahiro Yamada 	case S_INT:
3820a9064fbSMasahiro Yamada 	case S_HEX:
3830a9064fbSMasahiro Yamada 	case S_STRING:
3840a9064fbSMasahiro Yamada 		newval = symbol_empty.curr;
3850a9064fbSMasahiro Yamada 		break;
3860a9064fbSMasahiro Yamada 	case S_BOOLEAN:
3870a9064fbSMasahiro Yamada 	case S_TRISTATE:
3880a9064fbSMasahiro Yamada 		newval = symbol_no.curr;
3890a9064fbSMasahiro Yamada 		break;
3900a9064fbSMasahiro Yamada 	default:
3910a9064fbSMasahiro Yamada 		sym->curr.val = sym->name;
3920a9064fbSMasahiro Yamada 		sym->curr.tri = no;
3930a9064fbSMasahiro Yamada 		return;
3940a9064fbSMasahiro Yamada 	}
3950a9064fbSMasahiro Yamada 	sym->flags &= ~SYMBOL_WRITE;
3960a9064fbSMasahiro Yamada 
3970a9064fbSMasahiro Yamada 	sym_calc_visibility(sym);
3980a9064fbSMasahiro Yamada 
399*e91610daSEugeniu Rosca 	if (sym->visible != no)
400*e91610daSEugeniu Rosca 		sym->flags |= SYMBOL_WRITE;
401*e91610daSEugeniu Rosca 
4020a9064fbSMasahiro Yamada 	/* set default if recursively called */
4030a9064fbSMasahiro Yamada 	sym->curr = newval;
4040a9064fbSMasahiro Yamada 
4050a9064fbSMasahiro Yamada 	switch (sym_get_type(sym)) {
4060a9064fbSMasahiro Yamada 	case S_BOOLEAN:
4070a9064fbSMasahiro Yamada 	case S_TRISTATE:
4080a9064fbSMasahiro Yamada 		if (sym_is_choice_value(sym) && sym->visible == yes) {
4090a9064fbSMasahiro Yamada 			prop = sym_get_choice_prop(sym);
4100a9064fbSMasahiro Yamada 			newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
4110a9064fbSMasahiro Yamada 		} else {
4120a9064fbSMasahiro Yamada 			if (sym->visible != no) {
4130a9064fbSMasahiro Yamada 				/* if the symbol is visible use the user value
4140a9064fbSMasahiro Yamada 				 * if available, otherwise try the default value
4150a9064fbSMasahiro Yamada 				 */
4160a9064fbSMasahiro Yamada 				if (sym_has_value(sym)) {
4170a9064fbSMasahiro Yamada 					newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
4180a9064fbSMasahiro Yamada 							      sym->visible);
4190a9064fbSMasahiro Yamada 					goto calc_newval;
4200a9064fbSMasahiro Yamada 				}
4210a9064fbSMasahiro Yamada 			}
4220a9064fbSMasahiro Yamada 			if (sym->rev_dep.tri != no)
4230a9064fbSMasahiro Yamada 				sym->flags |= SYMBOL_WRITE;
4240a9064fbSMasahiro Yamada 			if (!sym_is_choice(sym)) {
4250a9064fbSMasahiro Yamada 				prop = sym_get_default_prop(sym);
4260a9064fbSMasahiro Yamada 				if (prop) {
4270a9064fbSMasahiro Yamada 					newval.tri = EXPR_AND(expr_calc_value(prop->expr),
4280a9064fbSMasahiro Yamada 							      prop->visible.tri);
429*e91610daSEugeniu Rosca 					if (newval.tri != no)
430*e91610daSEugeniu Rosca 						sym->flags |= SYMBOL_WRITE;
4310a9064fbSMasahiro Yamada 				}
432bf7ab1e7SMasahiro Yamada 				if (sym->implied.tri != no) {
433bf7ab1e7SMasahiro Yamada 					sym->flags |= SYMBOL_WRITE;
434bf7ab1e7SMasahiro Yamada 					newval.tri = EXPR_OR(newval.tri, sym->implied.tri);
435bf7ab1e7SMasahiro Yamada 				}
4360a9064fbSMasahiro Yamada 			}
4370a9064fbSMasahiro Yamada 		calc_newval:
438*e91610daSEugeniu Rosca 			if (sym->dir_dep.tri < sym->rev_dep.tri)
439*e91610daSEugeniu Rosca 				sym_warn_unmet_dep(sym);
4400a9064fbSMasahiro Yamada 			newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
4410a9064fbSMasahiro Yamada 		}
442bf7ab1e7SMasahiro Yamada 		if (newval.tri == mod &&
443bf7ab1e7SMasahiro Yamada 		    (sym_get_type(sym) == S_BOOLEAN || sym->implied.tri == yes))
4440a9064fbSMasahiro Yamada 			newval.tri = yes;
4450a9064fbSMasahiro Yamada 		break;
4460a9064fbSMasahiro Yamada 	case S_STRING:
4470a9064fbSMasahiro Yamada 	case S_HEX:
4480a9064fbSMasahiro Yamada 	case S_INT:
449*e91610daSEugeniu Rosca 		if (sym->visible != no && sym_has_value(sym)) {
4500a9064fbSMasahiro Yamada 			newval.val = sym->def[S_DEF_USER].val;
4510a9064fbSMasahiro Yamada 			break;
4520a9064fbSMasahiro Yamada 		}
4530a9064fbSMasahiro Yamada 		prop = sym_get_default_prop(sym);
4540a9064fbSMasahiro Yamada 		if (prop) {
4550a9064fbSMasahiro Yamada 			struct symbol *ds = prop_get_symbol(prop);
4560a9064fbSMasahiro Yamada 			if (ds) {
4570a9064fbSMasahiro Yamada 				sym->flags |= SYMBOL_WRITE;
4580a9064fbSMasahiro Yamada 				sym_calc_value(ds);
4590a9064fbSMasahiro Yamada 				newval.val = ds->curr.val;
4600a9064fbSMasahiro Yamada 			}
4610a9064fbSMasahiro Yamada 		}
4620a9064fbSMasahiro Yamada 		break;
4630a9064fbSMasahiro Yamada 	default:
4640a9064fbSMasahiro Yamada 		;
4650a9064fbSMasahiro Yamada 	}
4660a9064fbSMasahiro Yamada 
4670a9064fbSMasahiro Yamada 	sym->curr = newval;
4680a9064fbSMasahiro Yamada 	if (sym_is_choice(sym) && newval.tri == yes)
4690a9064fbSMasahiro Yamada 		sym->curr.val = sym_calc_choice(sym);
4700a9064fbSMasahiro Yamada 	sym_validate_range(sym);
4710a9064fbSMasahiro Yamada 
4720a9064fbSMasahiro Yamada 	if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
4730a9064fbSMasahiro Yamada 		sym_set_changed(sym);
4740a9064fbSMasahiro Yamada 		if (modules_sym == sym) {
4750a9064fbSMasahiro Yamada 			sym_set_all_changed();
4760a9064fbSMasahiro Yamada 			modules_val = modules_sym->curr.tri;
4770a9064fbSMasahiro Yamada 		}
4780a9064fbSMasahiro Yamada 	}
4790a9064fbSMasahiro Yamada 
4800a9064fbSMasahiro Yamada 	if (sym_is_choice(sym)) {
4810a9064fbSMasahiro Yamada 		struct symbol *choice_sym;
4820a9064fbSMasahiro Yamada 
4830a9064fbSMasahiro Yamada 		prop = sym_get_choice_prop(sym);
4840a9064fbSMasahiro Yamada 		expr_list_for_each_sym(prop->expr, e, choice_sym) {
4850a9064fbSMasahiro Yamada 			if ((sym->flags & SYMBOL_WRITE) &&
4860a9064fbSMasahiro Yamada 			    choice_sym->visible != no)
4870a9064fbSMasahiro Yamada 				choice_sym->flags |= SYMBOL_WRITE;
4880a9064fbSMasahiro Yamada 			if (sym->flags & SYMBOL_CHANGED)
4890a9064fbSMasahiro Yamada 				sym_set_changed(choice_sym);
4900a9064fbSMasahiro Yamada 		}
4910a9064fbSMasahiro Yamada 	}
4920a9064fbSMasahiro Yamada 
4930a9064fbSMasahiro Yamada 	if (sym->flags & SYMBOL_AUTO)
4940a9064fbSMasahiro Yamada 		sym->flags &= ~SYMBOL_WRITE;
4950a9064fbSMasahiro Yamada 
4960a9064fbSMasahiro Yamada 	if (sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES)
4970a9064fbSMasahiro Yamada 		set_all_choice_values(sym);
4980a9064fbSMasahiro Yamada }
4990a9064fbSMasahiro Yamada 
sym_clear_all_valid(void)5000a9064fbSMasahiro Yamada void sym_clear_all_valid(void)
5010a9064fbSMasahiro Yamada {
5020a9064fbSMasahiro Yamada 	struct symbol *sym;
5030a9064fbSMasahiro Yamada 	int i;
5040a9064fbSMasahiro Yamada 
5050a9064fbSMasahiro Yamada 	for_all_symbols(i, sym)
5060a9064fbSMasahiro Yamada 		sym->flags &= ~SYMBOL_VALID;
5070a9064fbSMasahiro Yamada 	sym_add_change_count(1);
5080a9064fbSMasahiro Yamada 	sym_calc_value(modules_sym);
5090a9064fbSMasahiro Yamada }
5100a9064fbSMasahiro Yamada 
sym_tristate_within_range(struct symbol * sym,tristate val)5110a9064fbSMasahiro Yamada bool sym_tristate_within_range(struct symbol *sym, tristate val)
5120a9064fbSMasahiro Yamada {
5130a9064fbSMasahiro Yamada 	int type = sym_get_type(sym);
5140a9064fbSMasahiro Yamada 
5150a9064fbSMasahiro Yamada 	if (sym->visible == no)
5160a9064fbSMasahiro Yamada 		return false;
5170a9064fbSMasahiro Yamada 
5180a9064fbSMasahiro Yamada 	if (type != S_BOOLEAN && type != S_TRISTATE)
5190a9064fbSMasahiro Yamada 		return false;
5200a9064fbSMasahiro Yamada 
5210a9064fbSMasahiro Yamada 	if (type == S_BOOLEAN && val == mod)
5220a9064fbSMasahiro Yamada 		return false;
5230a9064fbSMasahiro Yamada 	if (sym->visible <= sym->rev_dep.tri)
5240a9064fbSMasahiro Yamada 		return false;
525bf7ab1e7SMasahiro Yamada 	if (sym->implied.tri == yes && val == mod)
526bf7ab1e7SMasahiro Yamada 		return false;
5270a9064fbSMasahiro Yamada 	if (sym_is_choice_value(sym) && sym->visible == yes)
5280a9064fbSMasahiro Yamada 		return val == yes;
5290a9064fbSMasahiro Yamada 	return val >= sym->rev_dep.tri && val <= sym->visible;
5300a9064fbSMasahiro Yamada }
5310a9064fbSMasahiro Yamada 
sym_set_tristate_value(struct symbol * sym,tristate val)5320a9064fbSMasahiro Yamada bool sym_set_tristate_value(struct symbol *sym, tristate val)
5330a9064fbSMasahiro Yamada {
5340a9064fbSMasahiro Yamada 	tristate oldval = sym_get_tristate_value(sym);
5350a9064fbSMasahiro Yamada 
5360a9064fbSMasahiro Yamada 	if (oldval != val && !sym_tristate_within_range(sym, val))
5370a9064fbSMasahiro Yamada 		return false;
5380a9064fbSMasahiro Yamada 
5390a9064fbSMasahiro Yamada 	if (!(sym->flags & SYMBOL_DEF_USER)) {
5400a9064fbSMasahiro Yamada 		sym->flags |= SYMBOL_DEF_USER;
5410a9064fbSMasahiro Yamada 		sym_set_changed(sym);
5420a9064fbSMasahiro Yamada 	}
5430a9064fbSMasahiro Yamada 	/*
5440a9064fbSMasahiro Yamada 	 * setting a choice value also resets the new flag of the choice
5450a9064fbSMasahiro Yamada 	 * symbol and all other choice values.
5460a9064fbSMasahiro Yamada 	 */
5470a9064fbSMasahiro Yamada 	if (sym_is_choice_value(sym) && val == yes) {
5480a9064fbSMasahiro Yamada 		struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
5490a9064fbSMasahiro Yamada 		struct property *prop;
5500a9064fbSMasahiro Yamada 		struct expr *e;
5510a9064fbSMasahiro Yamada 
5520a9064fbSMasahiro Yamada 		cs->def[S_DEF_USER].val = sym;
5530a9064fbSMasahiro Yamada 		cs->flags |= SYMBOL_DEF_USER;
5540a9064fbSMasahiro Yamada 		prop = sym_get_choice_prop(cs);
5550a9064fbSMasahiro Yamada 		for (e = prop->expr; e; e = e->left.expr) {
5560a9064fbSMasahiro Yamada 			if (e->right.sym->visible != no)
5570a9064fbSMasahiro Yamada 				e->right.sym->flags |= SYMBOL_DEF_USER;
5580a9064fbSMasahiro Yamada 		}
5590a9064fbSMasahiro Yamada 	}
5600a9064fbSMasahiro Yamada 
5610a9064fbSMasahiro Yamada 	sym->def[S_DEF_USER].tri = val;
5620a9064fbSMasahiro Yamada 	if (oldval != val)
5630a9064fbSMasahiro Yamada 		sym_clear_all_valid();
5640a9064fbSMasahiro Yamada 
5650a9064fbSMasahiro Yamada 	return true;
5660a9064fbSMasahiro Yamada }
5670a9064fbSMasahiro Yamada 
sym_toggle_tristate_value(struct symbol * sym)5680a9064fbSMasahiro Yamada tristate sym_toggle_tristate_value(struct symbol *sym)
5690a9064fbSMasahiro Yamada {
5700a9064fbSMasahiro Yamada 	tristate oldval, newval;
5710a9064fbSMasahiro Yamada 
5720a9064fbSMasahiro Yamada 	oldval = newval = sym_get_tristate_value(sym);
5730a9064fbSMasahiro Yamada 	do {
5740a9064fbSMasahiro Yamada 		switch (newval) {
5750a9064fbSMasahiro Yamada 		case no:
5760a9064fbSMasahiro Yamada 			newval = mod;
5770a9064fbSMasahiro Yamada 			break;
5780a9064fbSMasahiro Yamada 		case mod:
5790a9064fbSMasahiro Yamada 			newval = yes;
5800a9064fbSMasahiro Yamada 			break;
5810a9064fbSMasahiro Yamada 		case yes:
5820a9064fbSMasahiro Yamada 			newval = no;
5830a9064fbSMasahiro Yamada 			break;
5840a9064fbSMasahiro Yamada 		}
5850a9064fbSMasahiro Yamada 		if (sym_set_tristate_value(sym, newval))
5860a9064fbSMasahiro Yamada 			break;
5870a9064fbSMasahiro Yamada 	} while (oldval != newval);
5880a9064fbSMasahiro Yamada 	return newval;
5890a9064fbSMasahiro Yamada }
5900a9064fbSMasahiro Yamada 
sym_string_valid(struct symbol * sym,const char * str)5910a9064fbSMasahiro Yamada bool sym_string_valid(struct symbol *sym, const char *str)
5920a9064fbSMasahiro Yamada {
5930a9064fbSMasahiro Yamada 	signed char ch;
5940a9064fbSMasahiro Yamada 
5950a9064fbSMasahiro Yamada 	switch (sym->type) {
5960a9064fbSMasahiro Yamada 	case S_STRING:
5970a9064fbSMasahiro Yamada 		return true;
5980a9064fbSMasahiro Yamada 	case S_INT:
5990a9064fbSMasahiro Yamada 		ch = *str++;
6000a9064fbSMasahiro Yamada 		if (ch == '-')
6010a9064fbSMasahiro Yamada 			ch = *str++;
6020a9064fbSMasahiro Yamada 		if (!isdigit(ch))
6030a9064fbSMasahiro Yamada 			return false;
6040a9064fbSMasahiro Yamada 		if (ch == '0' && *str != 0)
6050a9064fbSMasahiro Yamada 			return false;
6060a9064fbSMasahiro Yamada 		while ((ch = *str++)) {
6070a9064fbSMasahiro Yamada 			if (!isdigit(ch))
6080a9064fbSMasahiro Yamada 				return false;
6090a9064fbSMasahiro Yamada 		}
6100a9064fbSMasahiro Yamada 		return true;
6110a9064fbSMasahiro Yamada 	case S_HEX:
6120a9064fbSMasahiro Yamada 		if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
6130a9064fbSMasahiro Yamada 			str += 2;
6140a9064fbSMasahiro Yamada 		ch = *str++;
6150a9064fbSMasahiro Yamada 		do {
6160a9064fbSMasahiro Yamada 			if (!isxdigit(ch))
6170a9064fbSMasahiro Yamada 				return false;
6180a9064fbSMasahiro Yamada 		} while ((ch = *str++));
6190a9064fbSMasahiro Yamada 		return true;
6200a9064fbSMasahiro Yamada 	case S_BOOLEAN:
6210a9064fbSMasahiro Yamada 	case S_TRISTATE:
6220a9064fbSMasahiro Yamada 		switch (str[0]) {
6230a9064fbSMasahiro Yamada 		case 'y': case 'Y':
6240a9064fbSMasahiro Yamada 		case 'm': case 'M':
6250a9064fbSMasahiro Yamada 		case 'n': case 'N':
6260a9064fbSMasahiro Yamada 			return true;
6270a9064fbSMasahiro Yamada 		}
6280a9064fbSMasahiro Yamada 		return false;
6290a9064fbSMasahiro Yamada 	default:
6300a9064fbSMasahiro Yamada 		return false;
6310a9064fbSMasahiro Yamada 	}
6320a9064fbSMasahiro Yamada }
6330a9064fbSMasahiro Yamada 
sym_string_within_range(struct symbol * sym,const char * str)6340a9064fbSMasahiro Yamada bool sym_string_within_range(struct symbol *sym, const char *str)
6350a9064fbSMasahiro Yamada {
6360a9064fbSMasahiro Yamada 	struct property *prop;
6370a9064fbSMasahiro Yamada 	long long val;
6380a9064fbSMasahiro Yamada 
6390a9064fbSMasahiro Yamada 	switch (sym->type) {
6400a9064fbSMasahiro Yamada 	case S_STRING:
6410a9064fbSMasahiro Yamada 		return sym_string_valid(sym, str);
6420a9064fbSMasahiro Yamada 	case S_INT:
6430a9064fbSMasahiro Yamada 		if (!sym_string_valid(sym, str))
6440a9064fbSMasahiro Yamada 			return false;
6450a9064fbSMasahiro Yamada 		prop = sym_get_range_prop(sym);
6460a9064fbSMasahiro Yamada 		if (!prop)
6470a9064fbSMasahiro Yamada 			return true;
6480a9064fbSMasahiro Yamada 		val = strtoll(str, NULL, 10);
6490a9064fbSMasahiro Yamada 		return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
6500a9064fbSMasahiro Yamada 		       val <= sym_get_range_val(prop->expr->right.sym, 10);
6510a9064fbSMasahiro Yamada 	case S_HEX:
6520a9064fbSMasahiro Yamada 		if (!sym_string_valid(sym, str))
6530a9064fbSMasahiro Yamada 			return false;
6540a9064fbSMasahiro Yamada 		prop = sym_get_range_prop(sym);
6550a9064fbSMasahiro Yamada 		if (!prop)
6560a9064fbSMasahiro Yamada 			return true;
6570a9064fbSMasahiro Yamada 		val = strtoll(str, NULL, 16);
6580a9064fbSMasahiro Yamada 		return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
6590a9064fbSMasahiro Yamada 		       val <= sym_get_range_val(prop->expr->right.sym, 16);
6600a9064fbSMasahiro Yamada 	case S_BOOLEAN:
6610a9064fbSMasahiro Yamada 	case S_TRISTATE:
6620a9064fbSMasahiro Yamada 		switch (str[0]) {
6630a9064fbSMasahiro Yamada 		case 'y': case 'Y':
6640a9064fbSMasahiro Yamada 			return sym_tristate_within_range(sym, yes);
6650a9064fbSMasahiro Yamada 		case 'm': case 'M':
6660a9064fbSMasahiro Yamada 			return sym_tristate_within_range(sym, mod);
6670a9064fbSMasahiro Yamada 		case 'n': case 'N':
6680a9064fbSMasahiro Yamada 			return sym_tristate_within_range(sym, no);
6690a9064fbSMasahiro Yamada 		}
6700a9064fbSMasahiro Yamada 		return false;
6710a9064fbSMasahiro Yamada 	default:
6720a9064fbSMasahiro Yamada 		return false;
6730a9064fbSMasahiro Yamada 	}
6740a9064fbSMasahiro Yamada }
6750a9064fbSMasahiro Yamada 
sym_set_string_value(struct symbol * sym,const char * newval)6760a9064fbSMasahiro Yamada bool sym_set_string_value(struct symbol *sym, const char *newval)
6770a9064fbSMasahiro Yamada {
6780a9064fbSMasahiro Yamada 	const char *oldval;
6790a9064fbSMasahiro Yamada 	char *val;
6800a9064fbSMasahiro Yamada 	int size;
6810a9064fbSMasahiro Yamada 
6820a9064fbSMasahiro Yamada 	switch (sym->type) {
6830a9064fbSMasahiro Yamada 	case S_BOOLEAN:
6840a9064fbSMasahiro Yamada 	case S_TRISTATE:
6850a9064fbSMasahiro Yamada 		switch (newval[0]) {
6860a9064fbSMasahiro Yamada 		case 'y': case 'Y':
6870a9064fbSMasahiro Yamada 			return sym_set_tristate_value(sym, yes);
6880a9064fbSMasahiro Yamada 		case 'm': case 'M':
6890a9064fbSMasahiro Yamada 			return sym_set_tristate_value(sym, mod);
6900a9064fbSMasahiro Yamada 		case 'n': case 'N':
6910a9064fbSMasahiro Yamada 			return sym_set_tristate_value(sym, no);
6920a9064fbSMasahiro Yamada 		}
6930a9064fbSMasahiro Yamada 		return false;
6940a9064fbSMasahiro Yamada 	default:
6950a9064fbSMasahiro Yamada 		;
6960a9064fbSMasahiro Yamada 	}
6970a9064fbSMasahiro Yamada 
6980a9064fbSMasahiro Yamada 	if (!sym_string_within_range(sym, newval))
6990a9064fbSMasahiro Yamada 		return false;
7000a9064fbSMasahiro Yamada 
7010a9064fbSMasahiro Yamada 	if (!(sym->flags & SYMBOL_DEF_USER)) {
7020a9064fbSMasahiro Yamada 		sym->flags |= SYMBOL_DEF_USER;
7030a9064fbSMasahiro Yamada 		sym_set_changed(sym);
7040a9064fbSMasahiro Yamada 	}
7050a9064fbSMasahiro Yamada 
7060a9064fbSMasahiro Yamada 	oldval = sym->def[S_DEF_USER].val;
7070a9064fbSMasahiro Yamada 	size = strlen(newval) + 1;
7080a9064fbSMasahiro Yamada 	if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
7090a9064fbSMasahiro Yamada 		size += 2;
7100a9064fbSMasahiro Yamada 		sym->def[S_DEF_USER].val = val = xmalloc(size);
7110a9064fbSMasahiro Yamada 		*val++ = '0';
7120a9064fbSMasahiro Yamada 		*val++ = 'x';
7130a9064fbSMasahiro Yamada 	} else if (!oldval || strcmp(oldval, newval))
7140a9064fbSMasahiro Yamada 		sym->def[S_DEF_USER].val = val = xmalloc(size);
7150a9064fbSMasahiro Yamada 	else
7160a9064fbSMasahiro Yamada 		return true;
7170a9064fbSMasahiro Yamada 
7180a9064fbSMasahiro Yamada 	strcpy(val, newval);
7190a9064fbSMasahiro Yamada 	free((void *)oldval);
7200a9064fbSMasahiro Yamada 	sym_clear_all_valid();
7210a9064fbSMasahiro Yamada 
7220a9064fbSMasahiro Yamada 	return true;
7230a9064fbSMasahiro Yamada }
7240a9064fbSMasahiro Yamada 
7250a9064fbSMasahiro Yamada /*
7260a9064fbSMasahiro Yamada  * Find the default value associated to a symbol.
7270a9064fbSMasahiro Yamada  * For tristate symbol handle the modules=n case
7280a9064fbSMasahiro Yamada  * in which case "m" becomes "y".
7290a9064fbSMasahiro Yamada  * If the symbol does not have any default then fallback
7300a9064fbSMasahiro Yamada  * to the fixed default values.
7310a9064fbSMasahiro Yamada  */
sym_get_string_default(struct symbol * sym)7320a9064fbSMasahiro Yamada const char *sym_get_string_default(struct symbol *sym)
7330a9064fbSMasahiro Yamada {
7340a9064fbSMasahiro Yamada 	struct property *prop;
7350a9064fbSMasahiro Yamada 	struct symbol *ds;
7360a9064fbSMasahiro Yamada 	const char *str;
7370a9064fbSMasahiro Yamada 	tristate val;
7380a9064fbSMasahiro Yamada 
7390a9064fbSMasahiro Yamada 	sym_calc_visibility(sym);
7400a9064fbSMasahiro Yamada 	sym_calc_value(modules_sym);
7410a9064fbSMasahiro Yamada 	val = symbol_no.curr.tri;
7420a9064fbSMasahiro Yamada 	str = symbol_empty.curr.val;
7430a9064fbSMasahiro Yamada 
7440a9064fbSMasahiro Yamada 	/* If symbol has a default value look it up */
7450a9064fbSMasahiro Yamada 	prop = sym_get_default_prop(sym);
7460a9064fbSMasahiro Yamada 	if (prop != NULL) {
7470a9064fbSMasahiro Yamada 		switch (sym->type) {
7480a9064fbSMasahiro Yamada 		case S_BOOLEAN:
7490a9064fbSMasahiro Yamada 		case S_TRISTATE:
7500a9064fbSMasahiro Yamada 			/* The visibility may limit the value from yes => mod */
7510a9064fbSMasahiro Yamada 			val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
7520a9064fbSMasahiro Yamada 			break;
7530a9064fbSMasahiro Yamada 		default:
7540a9064fbSMasahiro Yamada 			/*
7550a9064fbSMasahiro Yamada 			 * The following fails to handle the situation
7560a9064fbSMasahiro Yamada 			 * where a default value is further limited by
7570a9064fbSMasahiro Yamada 			 * the valid range.
7580a9064fbSMasahiro Yamada 			 */
7590a9064fbSMasahiro Yamada 			ds = prop_get_symbol(prop);
7600a9064fbSMasahiro Yamada 			if (ds != NULL) {
7610a9064fbSMasahiro Yamada 				sym_calc_value(ds);
7620a9064fbSMasahiro Yamada 				str = (const char *)ds->curr.val;
7630a9064fbSMasahiro Yamada 			}
7640a9064fbSMasahiro Yamada 		}
7650a9064fbSMasahiro Yamada 	}
7660a9064fbSMasahiro Yamada 
7670a9064fbSMasahiro Yamada 	/* Handle select statements */
7680a9064fbSMasahiro Yamada 	val = EXPR_OR(val, sym->rev_dep.tri);
7690a9064fbSMasahiro Yamada 
7700a9064fbSMasahiro Yamada 	/* transpose mod to yes if modules are not enabled */
7710a9064fbSMasahiro Yamada 	if (val == mod)
7720a9064fbSMasahiro Yamada 		if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
7730a9064fbSMasahiro Yamada 			val = yes;
7740a9064fbSMasahiro Yamada 
7750a9064fbSMasahiro Yamada 	/* transpose mod to yes if type is bool */
7760a9064fbSMasahiro Yamada 	if (sym->type == S_BOOLEAN && val == mod)
7770a9064fbSMasahiro Yamada 		val = yes;
7780a9064fbSMasahiro Yamada 
779bf7ab1e7SMasahiro Yamada 	/* adjust the default value if this symbol is implied by another */
780bf7ab1e7SMasahiro Yamada 	if (val < sym->implied.tri)
781bf7ab1e7SMasahiro Yamada 		val = sym->implied.tri;
782bf7ab1e7SMasahiro Yamada 
7830a9064fbSMasahiro Yamada 	switch (sym->type) {
7840a9064fbSMasahiro Yamada 	case S_BOOLEAN:
7850a9064fbSMasahiro Yamada 	case S_TRISTATE:
7860a9064fbSMasahiro Yamada 		switch (val) {
7870a9064fbSMasahiro Yamada 		case no: return "n";
7880a9064fbSMasahiro Yamada 		case mod: return "m";
7890a9064fbSMasahiro Yamada 		case yes: return "y";
7900a9064fbSMasahiro Yamada 		}
7910a9064fbSMasahiro Yamada 	case S_INT:
7920a9064fbSMasahiro Yamada 	case S_HEX:
7930a9064fbSMasahiro Yamada 		return str;
7940a9064fbSMasahiro Yamada 	case S_STRING:
7950a9064fbSMasahiro Yamada 		return str;
7960a9064fbSMasahiro Yamada 	case S_OTHER:
7970a9064fbSMasahiro Yamada 	case S_UNKNOWN:
7980a9064fbSMasahiro Yamada 		break;
7990a9064fbSMasahiro Yamada 	}
8000a9064fbSMasahiro Yamada 	return "";
8010a9064fbSMasahiro Yamada }
8020a9064fbSMasahiro Yamada 
sym_get_string_value(struct symbol * sym)8030a9064fbSMasahiro Yamada const char *sym_get_string_value(struct symbol *sym)
8040a9064fbSMasahiro Yamada {
8050a9064fbSMasahiro Yamada 	tristate val;
8060a9064fbSMasahiro Yamada 
8070a9064fbSMasahiro Yamada 	switch (sym->type) {
8080a9064fbSMasahiro Yamada 	case S_BOOLEAN:
8090a9064fbSMasahiro Yamada 	case S_TRISTATE:
8100a9064fbSMasahiro Yamada 		val = sym_get_tristate_value(sym);
8110a9064fbSMasahiro Yamada 		switch (val) {
8120a9064fbSMasahiro Yamada 		case no:
8130a9064fbSMasahiro Yamada 			return "n";
8140a9064fbSMasahiro Yamada 		case mod:
8150a9064fbSMasahiro Yamada 			sym_calc_value(modules_sym);
8160a9064fbSMasahiro Yamada 			return (modules_sym->curr.tri == no) ? "n" : "m";
8170a9064fbSMasahiro Yamada 		case yes:
8180a9064fbSMasahiro Yamada 			return "y";
8190a9064fbSMasahiro Yamada 		}
8200a9064fbSMasahiro Yamada 		break;
8210a9064fbSMasahiro Yamada 	default:
8220a9064fbSMasahiro Yamada 		;
8230a9064fbSMasahiro Yamada 	}
8240a9064fbSMasahiro Yamada 	return (const char *)sym->curr.val;
8250a9064fbSMasahiro Yamada }
8260a9064fbSMasahiro Yamada 
sym_is_changable(struct symbol * sym)8270a9064fbSMasahiro Yamada bool sym_is_changable(struct symbol *sym)
8280a9064fbSMasahiro Yamada {
8290a9064fbSMasahiro Yamada 	return sym->visible > sym->rev_dep.tri;
8300a9064fbSMasahiro Yamada }
8310a9064fbSMasahiro Yamada 
strhash(const char * s)8320a9064fbSMasahiro Yamada static unsigned strhash(const char *s)
8330a9064fbSMasahiro Yamada {
8340a9064fbSMasahiro Yamada 	/* fnv32 hash */
8350a9064fbSMasahiro Yamada 	unsigned hash = 2166136261U;
8360a9064fbSMasahiro Yamada 	for (; *s; s++)
8370a9064fbSMasahiro Yamada 		hash = (hash ^ *s) * 0x01000193;
8380a9064fbSMasahiro Yamada 	return hash;
8390a9064fbSMasahiro Yamada }
8400a9064fbSMasahiro Yamada 
sym_lookup(const char * name,int flags)8410a9064fbSMasahiro Yamada struct symbol *sym_lookup(const char *name, int flags)
8420a9064fbSMasahiro Yamada {
8430a9064fbSMasahiro Yamada 	struct symbol *symbol;
8440a9064fbSMasahiro Yamada 	char *new_name;
8450a9064fbSMasahiro Yamada 	int hash;
8460a9064fbSMasahiro Yamada 
8470a9064fbSMasahiro Yamada 	if (name) {
8480a9064fbSMasahiro Yamada 		if (name[0] && !name[1]) {
8490a9064fbSMasahiro Yamada 			switch (name[0]) {
8500a9064fbSMasahiro Yamada 			case 'y': return &symbol_yes;
8510a9064fbSMasahiro Yamada 			case 'm': return &symbol_mod;
8520a9064fbSMasahiro Yamada 			case 'n': return &symbol_no;
8530a9064fbSMasahiro Yamada 			}
8540a9064fbSMasahiro Yamada 		}
8550a9064fbSMasahiro Yamada 		hash = strhash(name) % SYMBOL_HASHSIZE;
8560a9064fbSMasahiro Yamada 
8570a9064fbSMasahiro Yamada 		for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
8580a9064fbSMasahiro Yamada 			if (symbol->name &&
8590a9064fbSMasahiro Yamada 			    !strcmp(symbol->name, name) &&
8600a9064fbSMasahiro Yamada 			    (flags ? symbol->flags & flags
8610a9064fbSMasahiro Yamada 				   : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
8620a9064fbSMasahiro Yamada 				return symbol;
8630a9064fbSMasahiro Yamada 		}
864*e91610daSEugeniu Rosca 		new_name = xstrdup(name);
8650a9064fbSMasahiro Yamada 	} else {
8660a9064fbSMasahiro Yamada 		new_name = NULL;
8670a9064fbSMasahiro Yamada 		hash = 0;
8680a9064fbSMasahiro Yamada 	}
8690a9064fbSMasahiro Yamada 
8700a9064fbSMasahiro Yamada 	symbol = xmalloc(sizeof(*symbol));
8710a9064fbSMasahiro Yamada 	memset(symbol, 0, sizeof(*symbol));
8720a9064fbSMasahiro Yamada 	symbol->name = new_name;
8730a9064fbSMasahiro Yamada 	symbol->type = S_UNKNOWN;
8740a9064fbSMasahiro Yamada 	symbol->flags |= flags;
8750a9064fbSMasahiro Yamada 
8760a9064fbSMasahiro Yamada 	symbol->next = symbol_hash[hash];
8770a9064fbSMasahiro Yamada 	symbol_hash[hash] = symbol;
8780a9064fbSMasahiro Yamada 
8790a9064fbSMasahiro Yamada 	return symbol;
8800a9064fbSMasahiro Yamada }
8810a9064fbSMasahiro Yamada 
sym_find(const char * name)8820a9064fbSMasahiro Yamada struct symbol *sym_find(const char *name)
8830a9064fbSMasahiro Yamada {
8840a9064fbSMasahiro Yamada 	struct symbol *symbol = NULL;
8850a9064fbSMasahiro Yamada 	int hash = 0;
8860a9064fbSMasahiro Yamada 
8870a9064fbSMasahiro Yamada 	if (!name)
8880a9064fbSMasahiro Yamada 		return NULL;
8890a9064fbSMasahiro Yamada 
8900a9064fbSMasahiro Yamada 	if (name[0] && !name[1]) {
8910a9064fbSMasahiro Yamada 		switch (name[0]) {
8920a9064fbSMasahiro Yamada 		case 'y': return &symbol_yes;
8930a9064fbSMasahiro Yamada 		case 'm': return &symbol_mod;
8940a9064fbSMasahiro Yamada 		case 'n': return &symbol_no;
8950a9064fbSMasahiro Yamada 		}
8960a9064fbSMasahiro Yamada 	}
8970a9064fbSMasahiro Yamada 	hash = strhash(name) % SYMBOL_HASHSIZE;
8980a9064fbSMasahiro Yamada 
8990a9064fbSMasahiro Yamada 	for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
9000a9064fbSMasahiro Yamada 		if (symbol->name &&
9010a9064fbSMasahiro Yamada 		    !strcmp(symbol->name, name) &&
9020a9064fbSMasahiro Yamada 		    !(symbol->flags & SYMBOL_CONST))
9030a9064fbSMasahiro Yamada 				break;
9040a9064fbSMasahiro Yamada 	}
9050a9064fbSMasahiro Yamada 
9060a9064fbSMasahiro Yamada 	return symbol;
9070a9064fbSMasahiro Yamada }
9080a9064fbSMasahiro Yamada 
9090a9064fbSMasahiro Yamada /*
9100a9064fbSMasahiro Yamada  * Expand symbol's names embedded in the string given in argument. Symbols'
9110a9064fbSMasahiro Yamada  * name to be expanded shall be prefixed by a '$'. Unknown symbol expands to
9120a9064fbSMasahiro Yamada  * the empty string.
9130a9064fbSMasahiro Yamada  */
sym_expand_string_value(const char * in)914*e91610daSEugeniu Rosca char *sym_expand_string_value(const char *in)
9150a9064fbSMasahiro Yamada {
9160a9064fbSMasahiro Yamada 	const char *src;
9170a9064fbSMasahiro Yamada 	char *res;
9180a9064fbSMasahiro Yamada 	size_t reslen;
9190a9064fbSMasahiro Yamada 
920*e91610daSEugeniu Rosca 	/*
921*e91610daSEugeniu Rosca 	 * Note: 'in' might come from a token that's about to be
922*e91610daSEugeniu Rosca 	 * freed, so make sure to always allocate a new string
923*e91610daSEugeniu Rosca 	 */
9240a9064fbSMasahiro Yamada 	reslen = strlen(in) + 1;
9250a9064fbSMasahiro Yamada 	res = xmalloc(reslen);
9260a9064fbSMasahiro Yamada 	res[0] = '\0';
9270a9064fbSMasahiro Yamada 
9280a9064fbSMasahiro Yamada 	while ((src = strchr(in, '$'))) {
9290a9064fbSMasahiro Yamada 		char *p, name[SYMBOL_MAXLENGTH];
9300a9064fbSMasahiro Yamada 		const char *symval = "";
9310a9064fbSMasahiro Yamada 		struct symbol *sym;
9320a9064fbSMasahiro Yamada 		size_t newlen;
9330a9064fbSMasahiro Yamada 
9340a9064fbSMasahiro Yamada 		strncat(res, in, src - in);
9350a9064fbSMasahiro Yamada 		src++;
9360a9064fbSMasahiro Yamada 
9370a9064fbSMasahiro Yamada 		p = name;
9380a9064fbSMasahiro Yamada 		while (isalnum(*src) || *src == '_')
9390a9064fbSMasahiro Yamada 			*p++ = *src++;
9400a9064fbSMasahiro Yamada 		*p = '\0';
9410a9064fbSMasahiro Yamada 
9420a9064fbSMasahiro Yamada 		sym = sym_find(name);
9430a9064fbSMasahiro Yamada 		if (sym != NULL) {
9440a9064fbSMasahiro Yamada 			sym_calc_value(sym);
9450a9064fbSMasahiro Yamada 			symval = sym_get_string_value(sym);
9460a9064fbSMasahiro Yamada 		}
9470a9064fbSMasahiro Yamada 
9480a9064fbSMasahiro Yamada 		newlen = strlen(res) + strlen(symval) + strlen(src) + 1;
9490a9064fbSMasahiro Yamada 		if (newlen > reslen) {
9500a9064fbSMasahiro Yamada 			reslen = newlen;
951*e91610daSEugeniu Rosca 			res = xrealloc(res, reslen);
9520a9064fbSMasahiro Yamada 		}
9530a9064fbSMasahiro Yamada 
9540a9064fbSMasahiro Yamada 		strcat(res, symval);
9550a9064fbSMasahiro Yamada 		in = src;
9560a9064fbSMasahiro Yamada 	}
9570a9064fbSMasahiro Yamada 	strcat(res, in);
9580a9064fbSMasahiro Yamada 
9590a9064fbSMasahiro Yamada 	return res;
9600a9064fbSMasahiro Yamada }
9610a9064fbSMasahiro Yamada 
sym_escape_string_value(const char * in)962bf7ab1e7SMasahiro Yamada const char *sym_escape_string_value(const char *in)
963bf7ab1e7SMasahiro Yamada {
964bf7ab1e7SMasahiro Yamada 	const char *p;
965bf7ab1e7SMasahiro Yamada 	size_t reslen;
966bf7ab1e7SMasahiro Yamada 	char *res;
967bf7ab1e7SMasahiro Yamada 	size_t l;
968bf7ab1e7SMasahiro Yamada 
969bf7ab1e7SMasahiro Yamada 	reslen = strlen(in) + strlen("\"\"") + 1;
970bf7ab1e7SMasahiro Yamada 
971bf7ab1e7SMasahiro Yamada 	p = in;
972bf7ab1e7SMasahiro Yamada 	for (;;) {
973bf7ab1e7SMasahiro Yamada 		l = strcspn(p, "\"\\");
974bf7ab1e7SMasahiro Yamada 		p += l;
975bf7ab1e7SMasahiro Yamada 
976bf7ab1e7SMasahiro Yamada 		if (p[0] == '\0')
977bf7ab1e7SMasahiro Yamada 			break;
978bf7ab1e7SMasahiro Yamada 
979bf7ab1e7SMasahiro Yamada 		reslen++;
980bf7ab1e7SMasahiro Yamada 		p++;
981bf7ab1e7SMasahiro Yamada 	}
982bf7ab1e7SMasahiro Yamada 
983bf7ab1e7SMasahiro Yamada 	res = xmalloc(reslen);
984bf7ab1e7SMasahiro Yamada 	res[0] = '\0';
985bf7ab1e7SMasahiro Yamada 
986bf7ab1e7SMasahiro Yamada 	strcat(res, "\"");
987bf7ab1e7SMasahiro Yamada 
988bf7ab1e7SMasahiro Yamada 	p = in;
989bf7ab1e7SMasahiro Yamada 	for (;;) {
990bf7ab1e7SMasahiro Yamada 		l = strcspn(p, "\"\\");
991bf7ab1e7SMasahiro Yamada 		strncat(res, p, l);
992bf7ab1e7SMasahiro Yamada 		p += l;
993bf7ab1e7SMasahiro Yamada 
994bf7ab1e7SMasahiro Yamada 		if (p[0] == '\0')
995bf7ab1e7SMasahiro Yamada 			break;
996bf7ab1e7SMasahiro Yamada 
997bf7ab1e7SMasahiro Yamada 		strcat(res, "\\");
998bf7ab1e7SMasahiro Yamada 		strncat(res, p++, 1);
999bf7ab1e7SMasahiro Yamada 	}
1000bf7ab1e7SMasahiro Yamada 
1001bf7ab1e7SMasahiro Yamada 	strcat(res, "\"");
1002bf7ab1e7SMasahiro Yamada 	return res;
1003bf7ab1e7SMasahiro Yamada }
1004bf7ab1e7SMasahiro Yamada 
10050a9064fbSMasahiro Yamada struct sym_match {
10060a9064fbSMasahiro Yamada 	struct symbol	*sym;
10070a9064fbSMasahiro Yamada 	off_t		so, eo;
10080a9064fbSMasahiro Yamada };
10090a9064fbSMasahiro Yamada 
10100a9064fbSMasahiro Yamada /* Compare matched symbols as thus:
10110a9064fbSMasahiro Yamada  * - first, symbols that match exactly
10120a9064fbSMasahiro Yamada  * - then, alphabetical sort
10130a9064fbSMasahiro Yamada  */
sym_rel_comp(const void * sym1,const void * sym2)10140a9064fbSMasahiro Yamada static int sym_rel_comp(const void *sym1, const void *sym2)
10150a9064fbSMasahiro Yamada {
10160a9064fbSMasahiro Yamada 	const struct sym_match *s1 = sym1;
10170a9064fbSMasahiro Yamada 	const struct sym_match *s2 = sym2;
10180a9064fbSMasahiro Yamada 	int exact1, exact2;
10190a9064fbSMasahiro Yamada 
10200a9064fbSMasahiro Yamada 	/* Exact match:
10210a9064fbSMasahiro Yamada 	 * - if matched length on symbol s1 is the length of that symbol,
10220a9064fbSMasahiro Yamada 	 *   then this symbol should come first;
10230a9064fbSMasahiro Yamada 	 * - if matched length on symbol s2 is the length of that symbol,
10240a9064fbSMasahiro Yamada 	 *   then this symbol should come first.
10250a9064fbSMasahiro Yamada 	 * Note: since the search can be a regexp, both symbols may match
10260a9064fbSMasahiro Yamada 	 * exactly; if this is the case, we can't decide which comes first,
10270a9064fbSMasahiro Yamada 	 * and we fallback to sorting alphabetically.
10280a9064fbSMasahiro Yamada 	 */
10290a9064fbSMasahiro Yamada 	exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
10300a9064fbSMasahiro Yamada 	exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
10310a9064fbSMasahiro Yamada 	if (exact1 && !exact2)
10320a9064fbSMasahiro Yamada 		return -1;
10330a9064fbSMasahiro Yamada 	if (!exact1 && exact2)
10340a9064fbSMasahiro Yamada 		return 1;
10350a9064fbSMasahiro Yamada 
10360a9064fbSMasahiro Yamada 	/* As a fallback, sort symbols alphabetically */
10370a9064fbSMasahiro Yamada 	return strcmp(s1->sym->name, s2->sym->name);
10380a9064fbSMasahiro Yamada }
10390a9064fbSMasahiro Yamada 
sym_re_search(const char * pattern)10400a9064fbSMasahiro Yamada struct symbol **sym_re_search(const char *pattern)
10410a9064fbSMasahiro Yamada {
10420a9064fbSMasahiro Yamada 	struct symbol *sym, **sym_arr = NULL;
10430a9064fbSMasahiro Yamada 	struct sym_match *sym_match_arr = NULL;
10440a9064fbSMasahiro Yamada 	int i, cnt, size;
10450a9064fbSMasahiro Yamada 	regex_t re;
10460a9064fbSMasahiro Yamada 	regmatch_t match[1];
10470a9064fbSMasahiro Yamada 
10480a9064fbSMasahiro Yamada 	cnt = size = 0;
10490a9064fbSMasahiro Yamada 	/* Skip if empty */
10500a9064fbSMasahiro Yamada 	if (strlen(pattern) == 0)
10510a9064fbSMasahiro Yamada 		return NULL;
10520a9064fbSMasahiro Yamada 	if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
10530a9064fbSMasahiro Yamada 		return NULL;
10540a9064fbSMasahiro Yamada 
10550a9064fbSMasahiro Yamada 	for_all_symbols(i, sym) {
10560a9064fbSMasahiro Yamada 		if (sym->flags & SYMBOL_CONST || !sym->name)
10570a9064fbSMasahiro Yamada 			continue;
10580a9064fbSMasahiro Yamada 		if (regexec(&re, sym->name, 1, match, 0))
10590a9064fbSMasahiro Yamada 			continue;
10600a9064fbSMasahiro Yamada 		if (cnt >= size) {
10610a9064fbSMasahiro Yamada 			void *tmp;
10620a9064fbSMasahiro Yamada 			size += 16;
10630a9064fbSMasahiro Yamada 			tmp = realloc(sym_match_arr, size * sizeof(struct sym_match));
10640a9064fbSMasahiro Yamada 			if (!tmp)
10650a9064fbSMasahiro Yamada 				goto sym_re_search_free;
10660a9064fbSMasahiro Yamada 			sym_match_arr = tmp;
10670a9064fbSMasahiro Yamada 		}
10680a9064fbSMasahiro Yamada 		sym_calc_value(sym);
10690a9064fbSMasahiro Yamada 		/* As regexec returned 0, we know we have a match, so
10700a9064fbSMasahiro Yamada 		 * we can use match[0].rm_[se]o without further checks
10710a9064fbSMasahiro Yamada 		 */
10720a9064fbSMasahiro Yamada 		sym_match_arr[cnt].so = match[0].rm_so;
10730a9064fbSMasahiro Yamada 		sym_match_arr[cnt].eo = match[0].rm_eo;
10740a9064fbSMasahiro Yamada 		sym_match_arr[cnt++].sym = sym;
10750a9064fbSMasahiro Yamada 	}
10760a9064fbSMasahiro Yamada 	if (sym_match_arr) {
10770a9064fbSMasahiro Yamada 		qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp);
10780931ed3cSHeinrich Schuchardt 		sym_arr = malloc((cnt+1) * sizeof(struct symbol *));
10790a9064fbSMasahiro Yamada 		if (!sym_arr)
10800a9064fbSMasahiro Yamada 			goto sym_re_search_free;
10810a9064fbSMasahiro Yamada 		for (i = 0; i < cnt; i++)
10820a9064fbSMasahiro Yamada 			sym_arr[i] = sym_match_arr[i].sym;
10830a9064fbSMasahiro Yamada 		sym_arr[cnt] = NULL;
10840a9064fbSMasahiro Yamada 	}
10850a9064fbSMasahiro Yamada sym_re_search_free:
10860a9064fbSMasahiro Yamada 	/* sym_match_arr can be NULL if no match, but free(NULL) is OK */
10870a9064fbSMasahiro Yamada 	free(sym_match_arr);
10880a9064fbSMasahiro Yamada 	regfree(&re);
10890a9064fbSMasahiro Yamada 
10900a9064fbSMasahiro Yamada 	return sym_arr;
10910a9064fbSMasahiro Yamada }
10920a9064fbSMasahiro Yamada 
10930a9064fbSMasahiro Yamada /*
10940a9064fbSMasahiro Yamada  * When we check for recursive dependencies we use a stack to save
10950a9064fbSMasahiro Yamada  * current state so we can print out relevant info to user.
10960a9064fbSMasahiro Yamada  * The entries are located on the call stack so no need to free memory.
10970a9064fbSMasahiro Yamada  * Note insert() remove() must always match to properly clear the stack.
10980a9064fbSMasahiro Yamada  */
10990a9064fbSMasahiro Yamada static struct dep_stack {
11000a9064fbSMasahiro Yamada 	struct dep_stack *prev, *next;
11010a9064fbSMasahiro Yamada 	struct symbol *sym;
11020a9064fbSMasahiro Yamada 	struct property *prop;
11030a9064fbSMasahiro Yamada 	struct expr *expr;
11040a9064fbSMasahiro Yamada } *check_top;
11050a9064fbSMasahiro Yamada 
dep_stack_insert(struct dep_stack * stack,struct symbol * sym)11060a9064fbSMasahiro Yamada static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
11070a9064fbSMasahiro Yamada {
11080a9064fbSMasahiro Yamada 	memset(stack, 0, sizeof(*stack));
11090a9064fbSMasahiro Yamada 	if (check_top)
11100a9064fbSMasahiro Yamada 		check_top->next = stack;
11110a9064fbSMasahiro Yamada 	stack->prev = check_top;
11120a9064fbSMasahiro Yamada 	stack->sym = sym;
11130a9064fbSMasahiro Yamada 	check_top = stack;
11140a9064fbSMasahiro Yamada }
11150a9064fbSMasahiro Yamada 
dep_stack_remove(void)11160a9064fbSMasahiro Yamada static void dep_stack_remove(void)
11170a9064fbSMasahiro Yamada {
11180a9064fbSMasahiro Yamada 	check_top = check_top->prev;
11190a9064fbSMasahiro Yamada 	if (check_top)
11200a9064fbSMasahiro Yamada 		check_top->next = NULL;
11210a9064fbSMasahiro Yamada }
11220a9064fbSMasahiro Yamada 
11230a9064fbSMasahiro Yamada /*
11240a9064fbSMasahiro Yamada  * Called when we have detected a recursive dependency.
11250a9064fbSMasahiro Yamada  * check_top point to the top of the stact so we use
11260a9064fbSMasahiro Yamada  * the ->prev pointer to locate the bottom of the stack.
11270a9064fbSMasahiro Yamada  */
sym_check_print_recursive(struct symbol * last_sym)11280a9064fbSMasahiro Yamada static void sym_check_print_recursive(struct symbol *last_sym)
11290a9064fbSMasahiro Yamada {
11300a9064fbSMasahiro Yamada 	struct dep_stack *stack;
11310a9064fbSMasahiro Yamada 	struct symbol *sym, *next_sym;
11320a9064fbSMasahiro Yamada 	struct menu *menu = NULL;
11330a9064fbSMasahiro Yamada 	struct property *prop;
11340a9064fbSMasahiro Yamada 	struct dep_stack cv_stack;
11350a9064fbSMasahiro Yamada 
11360a9064fbSMasahiro Yamada 	if (sym_is_choice_value(last_sym)) {
11370a9064fbSMasahiro Yamada 		dep_stack_insert(&cv_stack, last_sym);
11380a9064fbSMasahiro Yamada 		last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
11390a9064fbSMasahiro Yamada 	}
11400a9064fbSMasahiro Yamada 
11410a9064fbSMasahiro Yamada 	for (stack = check_top; stack != NULL; stack = stack->prev)
11420a9064fbSMasahiro Yamada 		if (stack->sym == last_sym)
11430a9064fbSMasahiro Yamada 			break;
11440a9064fbSMasahiro Yamada 	if (!stack) {
11450a9064fbSMasahiro Yamada 		fprintf(stderr, "unexpected recursive dependency error\n");
11460a9064fbSMasahiro Yamada 		return;
11470a9064fbSMasahiro Yamada 	}
11480a9064fbSMasahiro Yamada 
11490a9064fbSMasahiro Yamada 	for (; stack; stack = stack->next) {
11500a9064fbSMasahiro Yamada 		sym = stack->sym;
11510a9064fbSMasahiro Yamada 		next_sym = stack->next ? stack->next->sym : last_sym;
11520a9064fbSMasahiro Yamada 		prop = stack->prop;
11530a9064fbSMasahiro Yamada 		if (prop == NULL)
11540a9064fbSMasahiro Yamada 			prop = stack->sym->prop;
11550a9064fbSMasahiro Yamada 
11560a9064fbSMasahiro Yamada 		/* for choice values find the menu entry (used below) */
11570a9064fbSMasahiro Yamada 		if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
11580a9064fbSMasahiro Yamada 			for (prop = sym->prop; prop; prop = prop->next) {
11590a9064fbSMasahiro Yamada 				menu = prop->menu;
11600a9064fbSMasahiro Yamada 				if (prop->menu)
11610a9064fbSMasahiro Yamada 					break;
11620a9064fbSMasahiro Yamada 			}
11630a9064fbSMasahiro Yamada 		}
11640a9064fbSMasahiro Yamada 		if (stack->sym == last_sym)
11650a9064fbSMasahiro Yamada 			fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
11660a9064fbSMasahiro Yamada 				prop->file->name, prop->lineno);
1167*e91610daSEugeniu Rosca 
11680a9064fbSMasahiro Yamada 		if (stack->expr) {
11690a9064fbSMasahiro Yamada 			fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
11700a9064fbSMasahiro Yamada 				prop->file->name, prop->lineno,
11710a9064fbSMasahiro Yamada 				sym->name ? sym->name : "<choice>",
11720a9064fbSMasahiro Yamada 				prop_get_type_name(prop->type),
11730a9064fbSMasahiro Yamada 				next_sym->name ? next_sym->name : "<choice>");
11740a9064fbSMasahiro Yamada 		} else if (stack->prop) {
11750a9064fbSMasahiro Yamada 			fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
11760a9064fbSMasahiro Yamada 				prop->file->name, prop->lineno,
11770a9064fbSMasahiro Yamada 				sym->name ? sym->name : "<choice>",
11780a9064fbSMasahiro Yamada 				next_sym->name ? next_sym->name : "<choice>");
11790a9064fbSMasahiro Yamada 		} else if (sym_is_choice(sym)) {
11800a9064fbSMasahiro Yamada 			fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
11810a9064fbSMasahiro Yamada 				menu->file->name, menu->lineno,
11820a9064fbSMasahiro Yamada 				sym->name ? sym->name : "<choice>",
11830a9064fbSMasahiro Yamada 				next_sym->name ? next_sym->name : "<choice>");
11840a9064fbSMasahiro Yamada 		} else if (sym_is_choice_value(sym)) {
11850a9064fbSMasahiro Yamada 			fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
11860a9064fbSMasahiro Yamada 				menu->file->name, menu->lineno,
11870a9064fbSMasahiro Yamada 				sym->name ? sym->name : "<choice>",
11880a9064fbSMasahiro Yamada 				next_sym->name ? next_sym->name : "<choice>");
11890a9064fbSMasahiro Yamada 		} else {
11900a9064fbSMasahiro Yamada 			fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
11910a9064fbSMasahiro Yamada 				prop->file->name, prop->lineno,
11920a9064fbSMasahiro Yamada 				sym->name ? sym->name : "<choice>",
11930a9064fbSMasahiro Yamada 				next_sym->name ? next_sym->name : "<choice>");
11940a9064fbSMasahiro Yamada 		}
11950a9064fbSMasahiro Yamada 	}
11960a9064fbSMasahiro Yamada 
1197*e91610daSEugeniu Rosca 	fprintf(stderr,
1198*e91610daSEugeniu Rosca 		"For a resolution refer to Documentation/kbuild/kconfig-language.txt\n"
1199*e91610daSEugeniu Rosca 		"subsection \"Kconfig recursive dependency limitations\"\n"
1200*e91610daSEugeniu Rosca 		"\n");
1201*e91610daSEugeniu Rosca 
12020a9064fbSMasahiro Yamada 	if (check_top == &cv_stack)
12030a9064fbSMasahiro Yamada 		dep_stack_remove();
12040a9064fbSMasahiro Yamada }
12050a9064fbSMasahiro Yamada 
sym_check_expr_deps(struct expr * e)12060a9064fbSMasahiro Yamada static struct symbol *sym_check_expr_deps(struct expr *e)
12070a9064fbSMasahiro Yamada {
12080a9064fbSMasahiro Yamada 	struct symbol *sym;
12090a9064fbSMasahiro Yamada 
12100a9064fbSMasahiro Yamada 	if (!e)
12110a9064fbSMasahiro Yamada 		return NULL;
12120a9064fbSMasahiro Yamada 	switch (e->type) {
12130a9064fbSMasahiro Yamada 	case E_OR:
12140a9064fbSMasahiro Yamada 	case E_AND:
12150a9064fbSMasahiro Yamada 		sym = sym_check_expr_deps(e->left.expr);
12160a9064fbSMasahiro Yamada 		if (sym)
12170a9064fbSMasahiro Yamada 			return sym;
12180a9064fbSMasahiro Yamada 		return sym_check_expr_deps(e->right.expr);
12190a9064fbSMasahiro Yamada 	case E_NOT:
12200a9064fbSMasahiro Yamada 		return sym_check_expr_deps(e->left.expr);
12210a9064fbSMasahiro Yamada 	case E_EQUAL:
1222bf7ab1e7SMasahiro Yamada 	case E_GEQ:
1223bf7ab1e7SMasahiro Yamada 	case E_GTH:
1224bf7ab1e7SMasahiro Yamada 	case E_LEQ:
1225bf7ab1e7SMasahiro Yamada 	case E_LTH:
12260a9064fbSMasahiro Yamada 	case E_UNEQUAL:
12270a9064fbSMasahiro Yamada 		sym = sym_check_deps(e->left.sym);
12280a9064fbSMasahiro Yamada 		if (sym)
12290a9064fbSMasahiro Yamada 			return sym;
12300a9064fbSMasahiro Yamada 		return sym_check_deps(e->right.sym);
12310a9064fbSMasahiro Yamada 	case E_SYMBOL:
12320a9064fbSMasahiro Yamada 		return sym_check_deps(e->left.sym);
12330a9064fbSMasahiro Yamada 	default:
12340a9064fbSMasahiro Yamada 		break;
12350a9064fbSMasahiro Yamada 	}
1236*e91610daSEugeniu Rosca 	fprintf(stderr, "Oops! How to check %d?\n", e->type);
12370a9064fbSMasahiro Yamada 	return NULL;
12380a9064fbSMasahiro Yamada }
12390a9064fbSMasahiro Yamada 
12400a9064fbSMasahiro Yamada /* return NULL when dependencies are OK */
sym_check_sym_deps(struct symbol * sym)12410a9064fbSMasahiro Yamada static struct symbol *sym_check_sym_deps(struct symbol *sym)
12420a9064fbSMasahiro Yamada {
12430a9064fbSMasahiro Yamada 	struct symbol *sym2;
12440a9064fbSMasahiro Yamada 	struct property *prop;
12450a9064fbSMasahiro Yamada 	struct dep_stack stack;
12460a9064fbSMasahiro Yamada 
12470a9064fbSMasahiro Yamada 	dep_stack_insert(&stack, sym);
12480a9064fbSMasahiro Yamada 
12490a9064fbSMasahiro Yamada 	sym2 = sym_check_expr_deps(sym->rev_dep.expr);
12500a9064fbSMasahiro Yamada 	if (sym2)
12510a9064fbSMasahiro Yamada 		goto out;
12520a9064fbSMasahiro Yamada 
12530a9064fbSMasahiro Yamada 	for (prop = sym->prop; prop; prop = prop->next) {
12540a9064fbSMasahiro Yamada 		if (prop->type == P_CHOICE || prop->type == P_SELECT)
12550a9064fbSMasahiro Yamada 			continue;
12560a9064fbSMasahiro Yamada 		stack.prop = prop;
12570a9064fbSMasahiro Yamada 		sym2 = sym_check_expr_deps(prop->visible.expr);
12580a9064fbSMasahiro Yamada 		if (sym2)
12590a9064fbSMasahiro Yamada 			break;
12600a9064fbSMasahiro Yamada 		if (prop->type != P_DEFAULT || sym_is_choice(sym))
12610a9064fbSMasahiro Yamada 			continue;
12620a9064fbSMasahiro Yamada 		stack.expr = prop->expr;
12630a9064fbSMasahiro Yamada 		sym2 = sym_check_expr_deps(prop->expr);
12640a9064fbSMasahiro Yamada 		if (sym2)
12650a9064fbSMasahiro Yamada 			break;
12660a9064fbSMasahiro Yamada 		stack.expr = NULL;
12670a9064fbSMasahiro Yamada 	}
12680a9064fbSMasahiro Yamada 
12690a9064fbSMasahiro Yamada out:
12700a9064fbSMasahiro Yamada 	dep_stack_remove();
12710a9064fbSMasahiro Yamada 
12720a9064fbSMasahiro Yamada 	return sym2;
12730a9064fbSMasahiro Yamada }
12740a9064fbSMasahiro Yamada 
sym_check_choice_deps(struct symbol * choice)12750a9064fbSMasahiro Yamada static struct symbol *sym_check_choice_deps(struct symbol *choice)
12760a9064fbSMasahiro Yamada {
12770a9064fbSMasahiro Yamada 	struct symbol *sym, *sym2;
12780a9064fbSMasahiro Yamada 	struct property *prop;
12790a9064fbSMasahiro Yamada 	struct expr *e;
12800a9064fbSMasahiro Yamada 	struct dep_stack stack;
12810a9064fbSMasahiro Yamada 
12820a9064fbSMasahiro Yamada 	dep_stack_insert(&stack, choice);
12830a9064fbSMasahiro Yamada 
12840a9064fbSMasahiro Yamada 	prop = sym_get_choice_prop(choice);
12850a9064fbSMasahiro Yamada 	expr_list_for_each_sym(prop->expr, e, sym)
12860a9064fbSMasahiro Yamada 		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
12870a9064fbSMasahiro Yamada 
12880a9064fbSMasahiro Yamada 	choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
12890a9064fbSMasahiro Yamada 	sym2 = sym_check_sym_deps(choice);
12900a9064fbSMasahiro Yamada 	choice->flags &= ~SYMBOL_CHECK;
12910a9064fbSMasahiro Yamada 	if (sym2)
12920a9064fbSMasahiro Yamada 		goto out;
12930a9064fbSMasahiro Yamada 
12940a9064fbSMasahiro Yamada 	expr_list_for_each_sym(prop->expr, e, sym) {
12950a9064fbSMasahiro Yamada 		sym2 = sym_check_sym_deps(sym);
12960a9064fbSMasahiro Yamada 		if (sym2)
12970a9064fbSMasahiro Yamada 			break;
12980a9064fbSMasahiro Yamada 	}
12990a9064fbSMasahiro Yamada out:
13000a9064fbSMasahiro Yamada 	expr_list_for_each_sym(prop->expr, e, sym)
13010a9064fbSMasahiro Yamada 		sym->flags &= ~SYMBOL_CHECK;
13020a9064fbSMasahiro Yamada 
13030a9064fbSMasahiro Yamada 	if (sym2 && sym_is_choice_value(sym2) &&
13040a9064fbSMasahiro Yamada 	    prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
13050a9064fbSMasahiro Yamada 		sym2 = choice;
13060a9064fbSMasahiro Yamada 
13070a9064fbSMasahiro Yamada 	dep_stack_remove();
13080a9064fbSMasahiro Yamada 
13090a9064fbSMasahiro Yamada 	return sym2;
13100a9064fbSMasahiro Yamada }
13110a9064fbSMasahiro Yamada 
sym_check_deps(struct symbol * sym)13120a9064fbSMasahiro Yamada struct symbol *sym_check_deps(struct symbol *sym)
13130a9064fbSMasahiro Yamada {
13140a9064fbSMasahiro Yamada 	struct symbol *sym2;
13150a9064fbSMasahiro Yamada 	struct property *prop;
13160a9064fbSMasahiro Yamada 
13170a9064fbSMasahiro Yamada 	if (sym->flags & SYMBOL_CHECK) {
13180a9064fbSMasahiro Yamada 		sym_check_print_recursive(sym);
13190a9064fbSMasahiro Yamada 		return sym;
13200a9064fbSMasahiro Yamada 	}
13210a9064fbSMasahiro Yamada 	if (sym->flags & SYMBOL_CHECKED)
13220a9064fbSMasahiro Yamada 		return NULL;
13230a9064fbSMasahiro Yamada 
13240a9064fbSMasahiro Yamada 	if (sym_is_choice_value(sym)) {
13250a9064fbSMasahiro Yamada 		struct dep_stack stack;
13260a9064fbSMasahiro Yamada 
13270a9064fbSMasahiro Yamada 		/* for choice groups start the check with main choice symbol */
13280a9064fbSMasahiro Yamada 		dep_stack_insert(&stack, sym);
13290a9064fbSMasahiro Yamada 		prop = sym_get_choice_prop(sym);
13300a9064fbSMasahiro Yamada 		sym2 = sym_check_deps(prop_get_symbol(prop));
13310a9064fbSMasahiro Yamada 		dep_stack_remove();
13320a9064fbSMasahiro Yamada 	} else if (sym_is_choice(sym)) {
13330a9064fbSMasahiro Yamada 		sym2 = sym_check_choice_deps(sym);
13340a9064fbSMasahiro Yamada 	} else {
13350a9064fbSMasahiro Yamada 		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
13360a9064fbSMasahiro Yamada 		sym2 = sym_check_sym_deps(sym);
13370a9064fbSMasahiro Yamada 		sym->flags &= ~SYMBOL_CHECK;
13380a9064fbSMasahiro Yamada 	}
13390a9064fbSMasahiro Yamada 
13400a9064fbSMasahiro Yamada 	if (sym2 && sym2 == sym)
13410a9064fbSMasahiro Yamada 		sym2 = NULL;
13420a9064fbSMasahiro Yamada 
13430a9064fbSMasahiro Yamada 	return sym2;
13440a9064fbSMasahiro Yamada }
13450a9064fbSMasahiro Yamada 
prop_alloc(enum prop_type type,struct symbol * sym)13460a9064fbSMasahiro Yamada struct property *prop_alloc(enum prop_type type, struct symbol *sym)
13470a9064fbSMasahiro Yamada {
13480a9064fbSMasahiro Yamada 	struct property *prop;
13490a9064fbSMasahiro Yamada 	struct property **propp;
13500a9064fbSMasahiro Yamada 
13510a9064fbSMasahiro Yamada 	prop = xmalloc(sizeof(*prop));
13520a9064fbSMasahiro Yamada 	memset(prop, 0, sizeof(*prop));
13530a9064fbSMasahiro Yamada 	prop->type = type;
13540a9064fbSMasahiro Yamada 	prop->sym = sym;
13550a9064fbSMasahiro Yamada 	prop->file = current_file;
13560a9064fbSMasahiro Yamada 	prop->lineno = zconf_lineno();
13570a9064fbSMasahiro Yamada 
13580a9064fbSMasahiro Yamada 	/* append property to the prop list of symbol */
13590a9064fbSMasahiro Yamada 	if (sym) {
13600a9064fbSMasahiro Yamada 		for (propp = &sym->prop; *propp; propp = &(*propp)->next)
13610a9064fbSMasahiro Yamada 			;
13620a9064fbSMasahiro Yamada 		*propp = prop;
13630a9064fbSMasahiro Yamada 	}
13640a9064fbSMasahiro Yamada 
13650a9064fbSMasahiro Yamada 	return prop;
13660a9064fbSMasahiro Yamada }
13670a9064fbSMasahiro Yamada 
prop_get_symbol(struct property * prop)13680a9064fbSMasahiro Yamada struct symbol *prop_get_symbol(struct property *prop)
13690a9064fbSMasahiro Yamada {
13700a9064fbSMasahiro Yamada 	if (prop->expr && (prop->expr->type == E_SYMBOL ||
13710a9064fbSMasahiro Yamada 			   prop->expr->type == E_LIST))
13720a9064fbSMasahiro Yamada 		return prop->expr->left.sym;
13730a9064fbSMasahiro Yamada 	return NULL;
13740a9064fbSMasahiro Yamada }
13750a9064fbSMasahiro Yamada 
prop_get_type_name(enum prop_type type)13760a9064fbSMasahiro Yamada const char *prop_get_type_name(enum prop_type type)
13770a9064fbSMasahiro Yamada {
13780a9064fbSMasahiro Yamada 	switch (type) {
13790a9064fbSMasahiro Yamada 	case P_PROMPT:
13800a9064fbSMasahiro Yamada 		return "prompt";
13810a9064fbSMasahiro Yamada 	case P_ENV:
13820a9064fbSMasahiro Yamada 		return "env";
13830a9064fbSMasahiro Yamada 	case P_COMMENT:
13840a9064fbSMasahiro Yamada 		return "comment";
13850a9064fbSMasahiro Yamada 	case P_MENU:
13860a9064fbSMasahiro Yamada 		return "menu";
13870a9064fbSMasahiro Yamada 	case P_DEFAULT:
13880a9064fbSMasahiro Yamada 		return "default";
13890a9064fbSMasahiro Yamada 	case P_CHOICE:
13900a9064fbSMasahiro Yamada 		return "choice";
13910a9064fbSMasahiro Yamada 	case P_SELECT:
13920a9064fbSMasahiro Yamada 		return "select";
1393bf7ab1e7SMasahiro Yamada 	case P_IMPLY:
1394bf7ab1e7SMasahiro Yamada 		return "imply";
13950a9064fbSMasahiro Yamada 	case P_RANGE:
13960a9064fbSMasahiro Yamada 		return "range";
13970a9064fbSMasahiro Yamada 	case P_SYMBOL:
13980a9064fbSMasahiro Yamada 		return "symbol";
13990a9064fbSMasahiro Yamada 	case P_UNKNOWN:
14000a9064fbSMasahiro Yamada 		break;
14010a9064fbSMasahiro Yamada 	}
14020a9064fbSMasahiro Yamada 	return "unknown";
14030a9064fbSMasahiro Yamada }
14040a9064fbSMasahiro Yamada 
prop_add_env(const char * env)14050a9064fbSMasahiro Yamada static void prop_add_env(const char *env)
14060a9064fbSMasahiro Yamada {
14070a9064fbSMasahiro Yamada 	struct symbol *sym, *sym2;
14080a9064fbSMasahiro Yamada 	struct property *prop;
14090a9064fbSMasahiro Yamada 	char *p;
14100a9064fbSMasahiro Yamada 
14110a9064fbSMasahiro Yamada 	sym = current_entry->sym;
14120a9064fbSMasahiro Yamada 	sym->flags |= SYMBOL_AUTO;
14130a9064fbSMasahiro Yamada 	for_all_properties(sym, prop, P_ENV) {
14140a9064fbSMasahiro Yamada 		sym2 = prop_get_symbol(prop);
14150a9064fbSMasahiro Yamada 		if (strcmp(sym2->name, env))
14160a9064fbSMasahiro Yamada 			menu_warn(current_entry, "redefining environment symbol from %s",
14170a9064fbSMasahiro Yamada 				  sym2->name);
14180a9064fbSMasahiro Yamada 		return;
14190a9064fbSMasahiro Yamada 	}
14200a9064fbSMasahiro Yamada 
14210a9064fbSMasahiro Yamada 	prop = prop_alloc(P_ENV, sym);
14220a9064fbSMasahiro Yamada 	prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
14230a9064fbSMasahiro Yamada 
14240a9064fbSMasahiro Yamada 	sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
14250a9064fbSMasahiro Yamada 	sym_env_list->right.sym = sym;
14260a9064fbSMasahiro Yamada 
14270a9064fbSMasahiro Yamada 	p = getenv(env);
14280a9064fbSMasahiro Yamada 	if (p)
14290a9064fbSMasahiro Yamada 		sym_add_default(sym, p);
14300a9064fbSMasahiro Yamada 	else
14310a9064fbSMasahiro Yamada 		menu_warn(current_entry, "environment variable %s undefined", env);
14320a9064fbSMasahiro Yamada }
1433