xref: /openbmc/linux/scripts/kconfig/symbol.c (revision 558e78e3ce844c61ceffe32775dbefacf167b023)
10c874100SMasahiro Yamada // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
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 #include "lkc.h"
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds struct symbol symbol_yes = {
151da177e4SLinus Torvalds 	.name = "y",
161da177e4SLinus Torvalds 	.curr = { "y", yes },
17c0e150acSRoman Zippel 	.flags = SYMBOL_CONST|SYMBOL_VALID,
181da177e4SLinus Torvalds }, symbol_mod = {
191da177e4SLinus Torvalds 	.name = "m",
201da177e4SLinus Torvalds 	.curr = { "m", mod },
21c0e150acSRoman Zippel 	.flags = SYMBOL_CONST|SYMBOL_VALID,
221da177e4SLinus Torvalds }, symbol_no = {
231da177e4SLinus Torvalds 	.name = "n",
241da177e4SLinus Torvalds 	.curr = { "n", no },
25c0e150acSRoman Zippel 	.flags = SYMBOL_CONST|SYMBOL_VALID,
261da177e4SLinus Torvalds }, symbol_empty = {
271da177e4SLinus Torvalds 	.name = "",
281da177e4SLinus Torvalds 	.curr = { "", no },
291da177e4SLinus Torvalds 	.flags = SYMBOL_VALID,
301da177e4SLinus Torvalds };
311da177e4SLinus Torvalds 
32face4374SRoman Zippel struct symbol *sym_defconfig_list;
331da177e4SLinus Torvalds struct symbol *modules_sym;
341da177e4SLinus Torvalds tristate modules_val;
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds enum symbol_type sym_get_type(struct symbol *sym)
371da177e4SLinus Torvalds {
381da177e4SLinus Torvalds 	enum symbol_type type = sym->type;
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds 	if (type == S_TRISTATE) {
411da177e4SLinus Torvalds 		if (sym_is_choice_value(sym) && sym->visible == yes)
421da177e4SLinus Torvalds 			type = S_BOOLEAN;
431da177e4SLinus Torvalds 		else if (modules_val == no)
441da177e4SLinus Torvalds 			type = S_BOOLEAN;
451da177e4SLinus Torvalds 	}
461da177e4SLinus Torvalds 	return type;
471da177e4SLinus Torvalds }
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds const char *sym_type_name(enum symbol_type type)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds 	switch (type) {
521da177e4SLinus Torvalds 	case S_BOOLEAN:
53b92d804aSMasahiro Yamada 		return "bool";
541da177e4SLinus Torvalds 	case S_TRISTATE:
551da177e4SLinus Torvalds 		return "tristate";
561da177e4SLinus Torvalds 	case S_INT:
571da177e4SLinus Torvalds 		return "integer";
581da177e4SLinus Torvalds 	case S_HEX:
591da177e4SLinus Torvalds 		return "hex";
601da177e4SLinus Torvalds 	case S_STRING:
611da177e4SLinus Torvalds 		return "string";
621da177e4SLinus Torvalds 	case S_UNKNOWN:
631da177e4SLinus Torvalds 		return "unknown";
641da177e4SLinus Torvalds 	}
651da177e4SLinus Torvalds 	return "???";
661da177e4SLinus Torvalds }
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds struct property *sym_get_choice_prop(struct symbol *sym)
691da177e4SLinus Torvalds {
701da177e4SLinus Torvalds 	struct property *prop;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	for_all_choices(sym, prop)
731da177e4SLinus Torvalds 		return prop;
741da177e4SLinus Torvalds 	return NULL;
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
77ad8d40cdSMichal Marek static struct property *sym_get_default_prop(struct symbol *sym)
781da177e4SLinus Torvalds {
791da177e4SLinus Torvalds 	struct property *prop;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	for_all_defaults(sym, prop) {
821da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
831da177e4SLinus Torvalds 		if (prop->visible.tri != no)
841da177e4SLinus Torvalds 			return prop;
851da177e4SLinus Torvalds 	}
861da177e4SLinus Torvalds 	return NULL;
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds 
89*558e78e3SMasahiro Yamada struct property *sym_get_range_prop(struct symbol *sym)
901da177e4SLinus Torvalds {
911da177e4SLinus Torvalds 	struct property *prop;
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	for_all_properties(sym, prop, P_RANGE) {
941da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
951da177e4SLinus Torvalds 		if (prop->visible.tri != no)
961da177e4SLinus Torvalds 			return prop;
971da177e4SLinus Torvalds 	}
981da177e4SLinus Torvalds 	return NULL;
991da177e4SLinus Torvalds }
1001da177e4SLinus Torvalds 
101129784abSKees Cook static long long sym_get_range_val(struct symbol *sym, int base)
1024cf3cbe2SRoman Zippel {
1034cf3cbe2SRoman Zippel 	sym_calc_value(sym);
1044cf3cbe2SRoman Zippel 	switch (sym->type) {
1054cf3cbe2SRoman Zippel 	case S_INT:
1064cf3cbe2SRoman Zippel 		base = 10;
1074cf3cbe2SRoman Zippel 		break;
1084cf3cbe2SRoman Zippel 	case S_HEX:
1094cf3cbe2SRoman Zippel 		base = 16;
1104cf3cbe2SRoman Zippel 		break;
1114cf3cbe2SRoman Zippel 	default:
1124cf3cbe2SRoman Zippel 		break;
1134cf3cbe2SRoman Zippel 	}
114129784abSKees Cook 	return strtoll(sym->curr.val, NULL, base);
1154cf3cbe2SRoman Zippel }
1164cf3cbe2SRoman Zippel 
1174cf3cbe2SRoman Zippel static void sym_validate_range(struct symbol *sym)
1184cf3cbe2SRoman Zippel {
1194cf3cbe2SRoman Zippel 	struct property *prop;
120129784abSKees Cook 	int base;
121129784abSKees Cook 	long long val, val2;
1224cf3cbe2SRoman Zippel 	char str[64];
1234cf3cbe2SRoman Zippel 
1244cf3cbe2SRoman Zippel 	switch (sym->type) {
1254cf3cbe2SRoman Zippel 	case S_INT:
1264cf3cbe2SRoman Zippel 		base = 10;
1274cf3cbe2SRoman Zippel 		break;
1284cf3cbe2SRoman Zippel 	case S_HEX:
1294cf3cbe2SRoman Zippel 		base = 16;
1304cf3cbe2SRoman Zippel 		break;
1314cf3cbe2SRoman Zippel 	default:
1324cf3cbe2SRoman Zippel 		return;
1334cf3cbe2SRoman Zippel 	}
1344cf3cbe2SRoman Zippel 	prop = sym_get_range_prop(sym);
1354cf3cbe2SRoman Zippel 	if (!prop)
1364cf3cbe2SRoman Zippel 		return;
137129784abSKees Cook 	val = strtoll(sym->curr.val, NULL, base);
1384cf3cbe2SRoman Zippel 	val2 = sym_get_range_val(prop->expr->left.sym, base);
1394cf3cbe2SRoman Zippel 	if (val >= val2) {
1404cf3cbe2SRoman Zippel 		val2 = sym_get_range_val(prop->expr->right.sym, base);
1414cf3cbe2SRoman Zippel 		if (val <= val2)
1424cf3cbe2SRoman Zippel 			return;
1434cf3cbe2SRoman Zippel 	}
1444cf3cbe2SRoman Zippel 	if (sym->type == S_INT)
145129784abSKees Cook 		sprintf(str, "%lld", val2);
1464cf3cbe2SRoman Zippel 	else
147129784abSKees Cook 		sprintf(str, "0x%llx", val2);
148cd81fc82SMasahiro Yamada 	sym->curr.val = xstrdup(str);
1494cf3cbe2SRoman Zippel }
1504cf3cbe2SRoman Zippel 
151ad8d40cdSMichal Marek static void sym_set_changed(struct symbol *sym)
152ad8d40cdSMichal Marek {
153ad8d40cdSMichal Marek 	struct property *prop;
154ad8d40cdSMichal Marek 
155ad8d40cdSMichal Marek 	sym->flags |= SYMBOL_CHANGED;
156ad8d40cdSMichal Marek 	for (prop = sym->prop; prop; prop = prop->next) {
157ad8d40cdSMichal Marek 		if (prop->menu)
158ad8d40cdSMichal Marek 			prop->menu->flags |= MENU_CHANGED;
159ad8d40cdSMichal Marek 	}
160ad8d40cdSMichal Marek }
161ad8d40cdSMichal Marek 
162ad8d40cdSMichal Marek static void sym_set_all_changed(void)
163ad8d40cdSMichal Marek {
164ad8d40cdSMichal Marek 	struct symbol *sym;
165ad8d40cdSMichal Marek 	int i;
166ad8d40cdSMichal Marek 
167ad8d40cdSMichal Marek 	for_all_symbols(i, sym)
168ad8d40cdSMichal Marek 		sym_set_changed(sym);
169ad8d40cdSMichal Marek }
170ad8d40cdSMichal Marek 
1711da177e4SLinus Torvalds static void sym_calc_visibility(struct symbol *sym)
1721da177e4SLinus Torvalds {
1731da177e4SLinus Torvalds 	struct property *prop;
174fa64e5f6SDirk Gouders 	struct symbol *choice_sym = NULL;
1751da177e4SLinus Torvalds 	tristate tri;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	/* any prompt visible? */
1781da177e4SLinus Torvalds 	tri = no;
179fa64e5f6SDirk Gouders 
180fa64e5f6SDirk Gouders 	if (sym_is_choice_value(sym))
181fa64e5f6SDirk Gouders 		choice_sym = prop_get_symbol(sym_get_choice_prop(sym));
182fa64e5f6SDirk Gouders 
1831da177e4SLinus Torvalds 	for_all_prompts(sym, prop) {
1841da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
185fa64e5f6SDirk Gouders 		/*
186fa64e5f6SDirk Gouders 		 * Tristate choice_values with visibility 'mod' are
187fa64e5f6SDirk Gouders 		 * not visible if the corresponding choice's value is
188fa64e5f6SDirk Gouders 		 * 'yes'.
189fa64e5f6SDirk Gouders 		 */
190fa64e5f6SDirk Gouders 		if (choice_sym && sym->type == S_TRISTATE &&
191fa64e5f6SDirk Gouders 		    prop->visible.tri == mod && choice_sym->curr.tri == yes)
192fa64e5f6SDirk Gouders 			prop->visible.tri = no;
193fa64e5f6SDirk Gouders 
194d6ee3576SSam Ravnborg 		tri = EXPR_OR(tri, prop->visible.tri);
1951da177e4SLinus Torvalds 	}
1961da177e4SLinus Torvalds 	if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
1971da177e4SLinus Torvalds 		tri = yes;
1981da177e4SLinus Torvalds 	if (sym->visible != tri) {
1991da177e4SLinus Torvalds 		sym->visible = tri;
2001da177e4SLinus Torvalds 		sym_set_changed(sym);
2011da177e4SLinus Torvalds 	}
2021da177e4SLinus Torvalds 	if (sym_is_choice_value(sym))
2031da177e4SLinus Torvalds 		return;
204246cf9c2SCatalin Marinas 	/* defaulting to "yes" if no explicit "depends on" are given */
205246cf9c2SCatalin Marinas 	tri = yes;
206246cf9c2SCatalin Marinas 	if (sym->dir_dep.expr)
207246cf9c2SCatalin Marinas 		tri = expr_calc_value(sym->dir_dep.expr);
208f622f827SMasahiro Yamada 	if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
209246cf9c2SCatalin Marinas 		tri = yes;
210246cf9c2SCatalin Marinas 	if (sym->dir_dep.tri != tri) {
211246cf9c2SCatalin Marinas 		sym->dir_dep.tri = tri;
212246cf9c2SCatalin Marinas 		sym_set_changed(sym);
213246cf9c2SCatalin Marinas 	}
2141da177e4SLinus Torvalds 	tri = no;
2151da177e4SLinus Torvalds 	if (sym->rev_dep.expr)
2161da177e4SLinus Torvalds 		tri = expr_calc_value(sym->rev_dep.expr);
2171da177e4SLinus Torvalds 	if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
2181da177e4SLinus Torvalds 		tri = yes;
2191da177e4SLinus Torvalds 	if (sym->rev_dep.tri != tri) {
2201da177e4SLinus Torvalds 		sym->rev_dep.tri = tri;
2211da177e4SLinus Torvalds 		sym_set_changed(sym);
2221da177e4SLinus Torvalds 	}
223237e3ad0SNicolas Pitre 	tri = no;
224237e3ad0SNicolas Pitre 	if (sym->implied.expr && sym->dir_dep.tri != no)
225237e3ad0SNicolas Pitre 		tri = expr_calc_value(sym->implied.expr);
226237e3ad0SNicolas Pitre 	if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
227237e3ad0SNicolas Pitre 		tri = yes;
228237e3ad0SNicolas Pitre 	if (sym->implied.tri != tri) {
229237e3ad0SNicolas Pitre 		sym->implied.tri = tri;
230237e3ad0SNicolas Pitre 		sym_set_changed(sym);
231237e3ad0SNicolas Pitre 	}
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
234c252147dSSam Ravnborg /*
235c252147dSSam Ravnborg  * Find the default symbol for a choice.
236c252147dSSam Ravnborg  * First try the default values for the choice symbol
237c252147dSSam Ravnborg  * Next locate the first visible choice value
238c252147dSSam Ravnborg  * Return NULL if none was found
239c252147dSSam Ravnborg  */
240c252147dSSam Ravnborg struct symbol *sym_choice_default(struct symbol *sym)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	struct symbol *def_sym;
2431da177e4SLinus Torvalds 	struct property *prop;
2441da177e4SLinus Torvalds 	struct expr *e;
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	/* any of the defaults visible? */
2471da177e4SLinus Torvalds 	for_all_defaults(sym, prop) {
2481da177e4SLinus Torvalds 		prop->visible.tri = expr_calc_value(prop->visible.expr);
2491da177e4SLinus Torvalds 		if (prop->visible.tri == no)
2501da177e4SLinus Torvalds 			continue;
2511da177e4SLinus Torvalds 		def_sym = prop_get_symbol(prop);
2521da177e4SLinus Torvalds 		if (def_sym->visible != no)
2531da177e4SLinus Torvalds 			return def_sym;
2541da177e4SLinus Torvalds 	}
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	/* just get the first visible value */
2571da177e4SLinus Torvalds 	prop = sym_get_choice_prop(sym);
2580a28c47bSJan Beulich 	expr_list_for_each_sym(prop->expr, e, def_sym)
2591da177e4SLinus Torvalds 		if (def_sym->visible != no)
2601da177e4SLinus Torvalds 			return def_sym;
2611da177e4SLinus Torvalds 
262c252147dSSam Ravnborg 	/* failed to locate any defaults */
263c252147dSSam Ravnborg 	return NULL;
264c252147dSSam Ravnborg }
265c252147dSSam Ravnborg 
266c252147dSSam Ravnborg static struct symbol *sym_calc_choice(struct symbol *sym)
267c252147dSSam Ravnborg {
268c252147dSSam Ravnborg 	struct symbol *def_sym;
269c252147dSSam Ravnborg 	struct property *prop;
270c252147dSSam Ravnborg 	struct expr *e;
2715d09598dSArnaud Lacombe 	int flags;
272c252147dSSam Ravnborg 
273c252147dSSam Ravnborg 	/* first calculate all choice values' visibilities */
2745d09598dSArnaud Lacombe 	flags = sym->flags;
275c252147dSSam Ravnborg 	prop = sym_get_choice_prop(sym);
2765d09598dSArnaud Lacombe 	expr_list_for_each_sym(prop->expr, e, def_sym) {
277c252147dSSam Ravnborg 		sym_calc_visibility(def_sym);
2785d09598dSArnaud Lacombe 		if (def_sym->visible != no)
2795d09598dSArnaud Lacombe 			flags &= def_sym->flags;
2805d09598dSArnaud Lacombe 	}
2815d09598dSArnaud Lacombe 
2825d09598dSArnaud Lacombe 	sym->flags &= flags | ~SYMBOL_DEF_USER;
283c252147dSSam Ravnborg 
284c252147dSSam Ravnborg 	/* is the user choice visible? */
285c252147dSSam Ravnborg 	def_sym = sym->def[S_DEF_USER].val;
286c252147dSSam Ravnborg 	if (def_sym && def_sym->visible != no)
287c252147dSSam Ravnborg 		return def_sym;
288c252147dSSam Ravnborg 
289c252147dSSam Ravnborg 	def_sym = sym_choice_default(sym);
290c252147dSSam Ravnborg 
291c252147dSSam Ravnborg 	if (def_sym == NULL)
2921da177e4SLinus Torvalds 		/* no choice? reset tristate value */
2931da177e4SLinus Torvalds 		sym->curr.tri = no;
294c252147dSSam Ravnborg 
295c252147dSSam Ravnborg 	return def_sym;
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
298f8f69dc0SMasahiro Yamada static void sym_warn_unmet_dep(struct symbol *sym)
299f8f69dc0SMasahiro Yamada {
300f8f69dc0SMasahiro Yamada 	struct gstr gs = str_new();
301f8f69dc0SMasahiro Yamada 
302f8f69dc0SMasahiro Yamada 	str_printf(&gs,
303f8f69dc0SMasahiro Yamada 		   "\nWARNING: unmet direct dependencies detected for %s\n",
304f8f69dc0SMasahiro Yamada 		   sym->name);
305f8f69dc0SMasahiro Yamada 	str_printf(&gs,
306f8f69dc0SMasahiro Yamada 		   "  Depends on [%c]: ",
307f8f69dc0SMasahiro Yamada 		   sym->dir_dep.tri == mod ? 'm' : 'n');
308f8f69dc0SMasahiro Yamada 	expr_gstr_print(sym->dir_dep.expr, &gs);
309f8f69dc0SMasahiro Yamada 	str_printf(&gs, "\n");
310f8f69dc0SMasahiro Yamada 
311f8f69dc0SMasahiro Yamada 	expr_gstr_print_revdep(sym->rev_dep.expr, &gs, yes,
312f8f69dc0SMasahiro Yamada 			       "  Selected by [y]:\n");
313f8f69dc0SMasahiro Yamada 	expr_gstr_print_revdep(sym->rev_dep.expr, &gs, mod,
314f8f69dc0SMasahiro Yamada 			       "  Selected by [m]:\n");
315f8f69dc0SMasahiro Yamada 
316f8f69dc0SMasahiro Yamada 	fputs(str_get(&gs), stderr);
317f8f69dc0SMasahiro Yamada }
318f8f69dc0SMasahiro Yamada 
3191da177e4SLinus Torvalds void sym_calc_value(struct symbol *sym)
3201da177e4SLinus Torvalds {
3211da177e4SLinus Torvalds 	struct symbol_value newval, oldval;
3221da177e4SLinus Torvalds 	struct property *prop;
3231da177e4SLinus Torvalds 	struct expr *e;
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	if (!sym)
3261da177e4SLinus Torvalds 		return;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	if (sym->flags & SYMBOL_VALID)
3291da177e4SLinus Torvalds 		return;
330fbe98bb9SArve Hjønnevåg 
331fbe98bb9SArve Hjønnevåg 	if (sym_is_choice_value(sym) &&
332fbe98bb9SArve Hjønnevåg 	    sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES) {
333fbe98bb9SArve Hjønnevåg 		sym->flags &= ~SYMBOL_NEED_SET_CHOICE_VALUES;
334fbe98bb9SArve Hjønnevåg 		prop = sym_get_choice_prop(sym);
335fbe98bb9SArve Hjønnevåg 		sym_calc_value(prop_get_symbol(prop));
336fbe98bb9SArve Hjønnevåg 	}
337fbe98bb9SArve Hjønnevåg 
3381da177e4SLinus Torvalds 	sym->flags |= SYMBOL_VALID;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	oldval = sym->curr;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	switch (sym->type) {
3431da177e4SLinus Torvalds 	case S_INT:
3441da177e4SLinus Torvalds 	case S_HEX:
3451da177e4SLinus Torvalds 	case S_STRING:
3461da177e4SLinus Torvalds 		newval = symbol_empty.curr;
3471da177e4SLinus Torvalds 		break;
3481da177e4SLinus Torvalds 	case S_BOOLEAN:
3491da177e4SLinus Torvalds 	case S_TRISTATE:
3501da177e4SLinus Torvalds 		newval = symbol_no.curr;
3511da177e4SLinus Torvalds 		break;
3521da177e4SLinus Torvalds 	default:
3531da177e4SLinus Torvalds 		sym->curr.val = sym->name;
3541da177e4SLinus Torvalds 		sym->curr.tri = no;
3551da177e4SLinus Torvalds 		return;
3561da177e4SLinus Torvalds 	}
3571da177e4SLinus Torvalds 	sym->flags &= ~SYMBOL_WRITE;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	sym_calc_visibility(sym);
3601da177e4SLinus Torvalds 
361cb67ab2cSMasahiro Yamada 	if (sym->visible != no)
362cb67ab2cSMasahiro Yamada 		sym->flags |= SYMBOL_WRITE;
363cb67ab2cSMasahiro Yamada 
3641da177e4SLinus Torvalds 	/* set default if recursively called */
3651da177e4SLinus Torvalds 	sym->curr = newval;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	switch (sym_get_type(sym)) {
3681da177e4SLinus Torvalds 	case S_BOOLEAN:
3691da177e4SLinus Torvalds 	case S_TRISTATE:
3701da177e4SLinus Torvalds 		if (sym_is_choice_value(sym) && sym->visible == yes) {
3711da177e4SLinus Torvalds 			prop = sym_get_choice_prop(sym);
3721da177e4SLinus Torvalds 			newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
373587c9061SRoman Zippel 		} else {
374587c9061SRoman Zippel 			if (sym->visible != no) {
375587c9061SRoman Zippel 				/* if the symbol is visible use the user value
376587c9061SRoman Zippel 				 * if available, otherwise try the default value
377587c9061SRoman Zippel 				 */
378587c9061SRoman Zippel 				if (sym_has_value(sym)) {
379587c9061SRoman Zippel 					newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
380587c9061SRoman Zippel 							      sym->visible);
381587c9061SRoman Zippel 					goto calc_newval;
3821da177e4SLinus Torvalds 				}
383587c9061SRoman Zippel 			}
384587c9061SRoman Zippel 			if (sym->rev_dep.tri != no)
385587c9061SRoman Zippel 				sym->flags |= SYMBOL_WRITE;
386587c9061SRoman Zippel 			if (!sym_is_choice(sym)) {
3871da177e4SLinus Torvalds 				prop = sym_get_default_prop(sym);
3881da177e4SLinus Torvalds 				if (prop) {
389587c9061SRoman Zippel 					newval.tri = EXPR_AND(expr_calc_value(prop->expr),
390587c9061SRoman Zippel 							      prop->visible.tri);
391f467c564SUlf Magnusson 					if (newval.tri != no)
392f467c564SUlf Magnusson 						sym->flags |= SYMBOL_WRITE;
3931da177e4SLinus Torvalds 				}
394237e3ad0SNicolas Pitre 				if (sym->implied.tri != no) {
395237e3ad0SNicolas Pitre 					sym->flags |= SYMBOL_WRITE;
396237e3ad0SNicolas Pitre 					newval.tri = EXPR_OR(newval.tri, sym->implied.tri);
397237e3ad0SNicolas Pitre 				}
3981da177e4SLinus Torvalds 			}
399587c9061SRoman Zippel 		calc_newval:
400f8f69dc0SMasahiro Yamada 			if (sym->dir_dep.tri < sym->rev_dep.tri)
401f8f69dc0SMasahiro Yamada 				sym_warn_unmet_dep(sym);
402587c9061SRoman Zippel 			newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
403587c9061SRoman Zippel 		}
404237e3ad0SNicolas Pitre 		if (newval.tri == mod &&
405237e3ad0SNicolas Pitre 		    (sym_get_type(sym) == S_BOOLEAN || sym->implied.tri == yes))
4061da177e4SLinus Torvalds 			newval.tri = yes;
4071da177e4SLinus Torvalds 		break;
4081da177e4SLinus Torvalds 	case S_STRING:
4091da177e4SLinus Torvalds 	case S_HEX:
4101da177e4SLinus Torvalds 	case S_INT:
411cb67ab2cSMasahiro Yamada 		if (sym->visible != no && sym_has_value(sym)) {
4120c1822e6SRoman Zippel 			newval.val = sym->def[S_DEF_USER].val;
4131da177e4SLinus Torvalds 			break;
4141da177e4SLinus Torvalds 		}
4151da177e4SLinus Torvalds 		prop = sym_get_default_prop(sym);
4161da177e4SLinus Torvalds 		if (prop) {
4171da177e4SLinus Torvalds 			struct symbol *ds = prop_get_symbol(prop);
4181da177e4SLinus Torvalds 			if (ds) {
4191da177e4SLinus Torvalds 				sym->flags |= SYMBOL_WRITE;
4201da177e4SLinus Torvalds 				sym_calc_value(ds);
4211da177e4SLinus Torvalds 				newval.val = ds->curr.val;
4221da177e4SLinus Torvalds 			}
4231da177e4SLinus Torvalds 		}
4241da177e4SLinus Torvalds 		break;
4251da177e4SLinus Torvalds 	default:
4261da177e4SLinus Torvalds 		;
4271da177e4SLinus Torvalds 	}
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	sym->curr = newval;
4301da177e4SLinus Torvalds 	if (sym_is_choice(sym) && newval.tri == yes)
4311da177e4SLinus Torvalds 		sym->curr.val = sym_calc_choice(sym);
4324cf3cbe2SRoman Zippel 	sym_validate_range(sym);
4331da177e4SLinus Torvalds 
434face4374SRoman Zippel 	if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
4351da177e4SLinus Torvalds 		sym_set_changed(sym);
436face4374SRoman Zippel 		if (modules_sym == sym) {
437face4374SRoman Zippel 			sym_set_all_changed();
4381da177e4SLinus Torvalds 			modules_val = modules_sym->curr.tri;
439face4374SRoman Zippel 		}
440face4374SRoman Zippel 	}
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	if (sym_is_choice(sym)) {
4437a962923SRoman Zippel 		struct symbol *choice_sym;
4447a962923SRoman Zippel 
4451da177e4SLinus Torvalds 		prop = sym_get_choice_prop(sym);
4467a962923SRoman Zippel 		expr_list_for_each_sym(prop->expr, e, choice_sym) {
4470a28c47bSJan Beulich 			if ((sym->flags & SYMBOL_WRITE) &&
4480a28c47bSJan Beulich 			    choice_sym->visible != no)
4490a28c47bSJan Beulich 				choice_sym->flags |= SYMBOL_WRITE;
4500a28c47bSJan Beulich 			if (sym->flags & SYMBOL_CHANGED)
4517a962923SRoman Zippel 				sym_set_changed(choice_sym);
4521da177e4SLinus Torvalds 		}
4531da177e4SLinus Torvalds 	}
4545a1aa8a1SRoman Zippel 
455693359f7SDirk Gouders 	if (sym->flags & SYMBOL_NO_WRITE)
4565a1aa8a1SRoman Zippel 		sym->flags &= ~SYMBOL_WRITE;
457fbe98bb9SArve Hjønnevåg 
458fbe98bb9SArve Hjønnevåg 	if (sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES)
459fbe98bb9SArve Hjønnevåg 		set_all_choice_values(sym);
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds void sym_clear_all_valid(void)
4631da177e4SLinus Torvalds {
4641da177e4SLinus Torvalds 	struct symbol *sym;
4651da177e4SLinus Torvalds 	int i;
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	for_all_symbols(i, sym)
4681da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_VALID;
469bfc10001SKarsten Wiese 	sym_add_change_count(1);
4701da177e4SLinus Torvalds 	sym_calc_value(modules_sym);
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds bool sym_tristate_within_range(struct symbol *sym, tristate val)
4741da177e4SLinus Torvalds {
4751da177e4SLinus Torvalds 	int type = sym_get_type(sym);
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	if (sym->visible == no)
4781da177e4SLinus Torvalds 		return false;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	if (type != S_BOOLEAN && type != S_TRISTATE)
4811da177e4SLinus Torvalds 		return false;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	if (type == S_BOOLEAN && val == mod)
4841da177e4SLinus Torvalds 		return false;
4851da177e4SLinus Torvalds 	if (sym->visible <= sym->rev_dep.tri)
4861da177e4SLinus Torvalds 		return false;
487237e3ad0SNicolas Pitre 	if (sym->implied.tri == yes && val == mod)
488237e3ad0SNicolas Pitre 		return false;
4891da177e4SLinus Torvalds 	if (sym_is_choice_value(sym) && sym->visible == yes)
4901da177e4SLinus Torvalds 		return val == yes;
4911da177e4SLinus Torvalds 	return val >= sym->rev_dep.tri && val <= sym->visible;
4921da177e4SLinus Torvalds }
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds bool sym_set_tristate_value(struct symbol *sym, tristate val)
4951da177e4SLinus Torvalds {
4961da177e4SLinus Torvalds 	tristate oldval = sym_get_tristate_value(sym);
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	if (oldval != val && !sym_tristate_within_range(sym, val))
4991da177e4SLinus Torvalds 		return false;
5001da177e4SLinus Torvalds 
501669bfad9SRoman Zippel 	if (!(sym->flags & SYMBOL_DEF_USER)) {
502669bfad9SRoman Zippel 		sym->flags |= SYMBOL_DEF_USER;
5031da177e4SLinus Torvalds 		sym_set_changed(sym);
5041da177e4SLinus Torvalds 	}
5053f23ca2bSRoman Zippel 	/*
5063f23ca2bSRoman Zippel 	 * setting a choice value also resets the new flag of the choice
5073f23ca2bSRoman Zippel 	 * symbol and all other choice values.
5083f23ca2bSRoman Zippel 	 */
5091da177e4SLinus Torvalds 	if (sym_is_choice_value(sym) && val == yes) {
5101da177e4SLinus Torvalds 		struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
5113f23ca2bSRoman Zippel 		struct property *prop;
5123f23ca2bSRoman Zippel 		struct expr *e;
5131da177e4SLinus Torvalds 
5140c1822e6SRoman Zippel 		cs->def[S_DEF_USER].val = sym;
515669bfad9SRoman Zippel 		cs->flags |= SYMBOL_DEF_USER;
5163f23ca2bSRoman Zippel 		prop = sym_get_choice_prop(cs);
5173f23ca2bSRoman Zippel 		for (e = prop->expr; e; e = e->left.expr) {
5183f23ca2bSRoman Zippel 			if (e->right.sym->visible != no)
519669bfad9SRoman Zippel 				e->right.sym->flags |= SYMBOL_DEF_USER;
5203f23ca2bSRoman Zippel 		}
5211da177e4SLinus Torvalds 	}
5221da177e4SLinus Torvalds 
5230c1822e6SRoman Zippel 	sym->def[S_DEF_USER].tri = val;
524face4374SRoman Zippel 	if (oldval != val)
5251da177e4SLinus Torvalds 		sym_clear_all_valid();
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	return true;
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds tristate sym_toggle_tristate_value(struct symbol *sym)
5311da177e4SLinus Torvalds {
5321da177e4SLinus Torvalds 	tristate oldval, newval;
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	oldval = newval = sym_get_tristate_value(sym);
5351da177e4SLinus Torvalds 	do {
5361da177e4SLinus Torvalds 		switch (newval) {
5371da177e4SLinus Torvalds 		case no:
5381da177e4SLinus Torvalds 			newval = mod;
5391da177e4SLinus Torvalds 			break;
5401da177e4SLinus Torvalds 		case mod:
5411da177e4SLinus Torvalds 			newval = yes;
5421da177e4SLinus Torvalds 			break;
5431da177e4SLinus Torvalds 		case yes:
5441da177e4SLinus Torvalds 			newval = no;
5451da177e4SLinus Torvalds 			break;
5461da177e4SLinus Torvalds 		}
5471da177e4SLinus Torvalds 		if (sym_set_tristate_value(sym, newval))
5481da177e4SLinus Torvalds 			break;
5491da177e4SLinus Torvalds 	} while (oldval != newval);
5501da177e4SLinus Torvalds 	return newval;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds bool sym_string_valid(struct symbol *sym, const char *str)
5541da177e4SLinus Torvalds {
5551da177e4SLinus Torvalds 	signed char ch;
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	switch (sym->type) {
5581da177e4SLinus Torvalds 	case S_STRING:
5591da177e4SLinus Torvalds 		return true;
5601da177e4SLinus Torvalds 	case S_INT:
5611da177e4SLinus Torvalds 		ch = *str++;
5621da177e4SLinus Torvalds 		if (ch == '-')
5631da177e4SLinus Torvalds 			ch = *str++;
5641da177e4SLinus Torvalds 		if (!isdigit(ch))
5651da177e4SLinus Torvalds 			return false;
5661da177e4SLinus Torvalds 		if (ch == '0' && *str != 0)
5671da177e4SLinus Torvalds 			return false;
5681da177e4SLinus Torvalds 		while ((ch = *str++)) {
5691da177e4SLinus Torvalds 			if (!isdigit(ch))
5701da177e4SLinus Torvalds 				return false;
5711da177e4SLinus Torvalds 		}
5721da177e4SLinus Torvalds 		return true;
5731da177e4SLinus Torvalds 	case S_HEX:
5741da177e4SLinus Torvalds 		if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
5751da177e4SLinus Torvalds 			str += 2;
5761da177e4SLinus Torvalds 		ch = *str++;
5771da177e4SLinus Torvalds 		do {
5781da177e4SLinus Torvalds 			if (!isxdigit(ch))
5791da177e4SLinus Torvalds 				return false;
5801da177e4SLinus Torvalds 		} while ((ch = *str++));
5811da177e4SLinus Torvalds 		return true;
5821da177e4SLinus Torvalds 	case S_BOOLEAN:
5831da177e4SLinus Torvalds 	case S_TRISTATE:
5841da177e4SLinus Torvalds 		switch (str[0]) {
5851da177e4SLinus Torvalds 		case 'y': case 'Y':
5861da177e4SLinus Torvalds 		case 'm': case 'M':
5871da177e4SLinus Torvalds 		case 'n': case 'N':
5881da177e4SLinus Torvalds 			return true;
5891da177e4SLinus Torvalds 		}
5901da177e4SLinus Torvalds 		return false;
5911da177e4SLinus Torvalds 	default:
5921da177e4SLinus Torvalds 		return false;
5931da177e4SLinus Torvalds 	}
5941da177e4SLinus Torvalds }
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds bool sym_string_within_range(struct symbol *sym, const char *str)
5971da177e4SLinus Torvalds {
5981da177e4SLinus Torvalds 	struct property *prop;
599129784abSKees Cook 	long long val;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	switch (sym->type) {
6021da177e4SLinus Torvalds 	case S_STRING:
6031da177e4SLinus Torvalds 		return sym_string_valid(sym, str);
6041da177e4SLinus Torvalds 	case S_INT:
6051da177e4SLinus Torvalds 		if (!sym_string_valid(sym, str))
6061da177e4SLinus Torvalds 			return false;
6071da177e4SLinus Torvalds 		prop = sym_get_range_prop(sym);
6081da177e4SLinus Torvalds 		if (!prop)
6091da177e4SLinus Torvalds 			return true;
610129784abSKees Cook 		val = strtoll(str, NULL, 10);
6114cf3cbe2SRoman Zippel 		return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
6124cf3cbe2SRoman Zippel 		       val <= sym_get_range_val(prop->expr->right.sym, 10);
6131da177e4SLinus Torvalds 	case S_HEX:
6141da177e4SLinus Torvalds 		if (!sym_string_valid(sym, str))
6151da177e4SLinus Torvalds 			return false;
6161da177e4SLinus Torvalds 		prop = sym_get_range_prop(sym);
6171da177e4SLinus Torvalds 		if (!prop)
6181da177e4SLinus Torvalds 			return true;
619129784abSKees Cook 		val = strtoll(str, NULL, 16);
6204cf3cbe2SRoman Zippel 		return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
6214cf3cbe2SRoman Zippel 		       val <= sym_get_range_val(prop->expr->right.sym, 16);
6221da177e4SLinus Torvalds 	case S_BOOLEAN:
6231da177e4SLinus Torvalds 	case S_TRISTATE:
6241da177e4SLinus Torvalds 		switch (str[0]) {
6251da177e4SLinus Torvalds 		case 'y': case 'Y':
6261da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, yes);
6271da177e4SLinus Torvalds 		case 'm': case 'M':
6281da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, mod);
6291da177e4SLinus Torvalds 		case 'n': case 'N':
6301da177e4SLinus Torvalds 			return sym_tristate_within_range(sym, no);
6311da177e4SLinus Torvalds 		}
6321da177e4SLinus Torvalds 		return false;
6331da177e4SLinus Torvalds 	default:
6341da177e4SLinus Torvalds 		return false;
6351da177e4SLinus Torvalds 	}
6361da177e4SLinus Torvalds }
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds bool sym_set_string_value(struct symbol *sym, const char *newval)
6391da177e4SLinus Torvalds {
6401da177e4SLinus Torvalds 	const char *oldval;
6411da177e4SLinus Torvalds 	char *val;
6421da177e4SLinus Torvalds 	int size;
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	switch (sym->type) {
6451da177e4SLinus Torvalds 	case S_BOOLEAN:
6461da177e4SLinus Torvalds 	case S_TRISTATE:
6471da177e4SLinus Torvalds 		switch (newval[0]) {
6481da177e4SLinus Torvalds 		case 'y': case 'Y':
6491da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, yes);
6501da177e4SLinus Torvalds 		case 'm': case 'M':
6511da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, mod);
6521da177e4SLinus Torvalds 		case 'n': case 'N':
6531da177e4SLinus Torvalds 			return sym_set_tristate_value(sym, no);
6541da177e4SLinus Torvalds 		}
6551da177e4SLinus Torvalds 		return false;
6561da177e4SLinus Torvalds 	default:
6571da177e4SLinus Torvalds 		;
6581da177e4SLinus Torvalds 	}
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	if (!sym_string_within_range(sym, newval))
6611da177e4SLinus Torvalds 		return false;
6621da177e4SLinus Torvalds 
663669bfad9SRoman Zippel 	if (!(sym->flags & SYMBOL_DEF_USER)) {
664669bfad9SRoman Zippel 		sym->flags |= SYMBOL_DEF_USER;
6651da177e4SLinus Torvalds 		sym_set_changed(sym);
6661da177e4SLinus Torvalds 	}
6671da177e4SLinus Torvalds 
6680c1822e6SRoman Zippel 	oldval = sym->def[S_DEF_USER].val;
6691da177e4SLinus Torvalds 	size = strlen(newval) + 1;
6701da177e4SLinus Torvalds 	if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
6711da177e4SLinus Torvalds 		size += 2;
672177acf78SAlan Cox 		sym->def[S_DEF_USER].val = val = xmalloc(size);
6731da177e4SLinus Torvalds 		*val++ = '0';
6741da177e4SLinus Torvalds 		*val++ = 'x';
6751da177e4SLinus Torvalds 	} else if (!oldval || strcmp(oldval, newval))
676177acf78SAlan Cox 		sym->def[S_DEF_USER].val = val = xmalloc(size);
6771da177e4SLinus Torvalds 	else
6781da177e4SLinus Torvalds 		return true;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	strcpy(val, newval);
6811da177e4SLinus Torvalds 	free((void *)oldval);
6821da177e4SLinus Torvalds 	sym_clear_all_valid();
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 	return true;
6851da177e4SLinus Torvalds }
6861da177e4SLinus Torvalds 
6877cf3d73bSSam Ravnborg /*
6887cf3d73bSSam Ravnborg  * Find the default value associated to a symbol.
6897cf3d73bSSam Ravnborg  * For tristate symbol handle the modules=n case
6907cf3d73bSSam Ravnborg  * in which case "m" becomes "y".
6917cf3d73bSSam Ravnborg  * If the symbol does not have any default then fallback
6927cf3d73bSSam Ravnborg  * to the fixed default values.
6937cf3d73bSSam Ravnborg  */
6947cf3d73bSSam Ravnborg const char *sym_get_string_default(struct symbol *sym)
6957cf3d73bSSam Ravnborg {
6967cf3d73bSSam Ravnborg 	struct property *prop;
6977cf3d73bSSam Ravnborg 	struct symbol *ds;
6987cf3d73bSSam Ravnborg 	const char *str;
6997cf3d73bSSam Ravnborg 	tristate val;
7007cf3d73bSSam Ravnborg 
7017cf3d73bSSam Ravnborg 	sym_calc_visibility(sym);
7027cf3d73bSSam Ravnborg 	sym_calc_value(modules_sym);
7037cf3d73bSSam Ravnborg 	val = symbol_no.curr.tri;
7047cf3d73bSSam Ravnborg 	str = symbol_empty.curr.val;
7057cf3d73bSSam Ravnborg 
7067cf3d73bSSam Ravnborg 	/* If symbol has a default value look it up */
7077cf3d73bSSam Ravnborg 	prop = sym_get_default_prop(sym);
7087cf3d73bSSam Ravnborg 	if (prop != NULL) {
7097cf3d73bSSam Ravnborg 		switch (sym->type) {
7107cf3d73bSSam Ravnborg 		case S_BOOLEAN:
7117cf3d73bSSam Ravnborg 		case S_TRISTATE:
712579fb8e7SArnaud Lacombe 			/* The visibility may limit the value from yes => mod */
7137cf3d73bSSam Ravnborg 			val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
7147cf3d73bSSam Ravnborg 			break;
7157cf3d73bSSam Ravnborg 		default:
7167cf3d73bSSam Ravnborg 			/*
7177cf3d73bSSam Ravnborg 			 * The following fails to handle the situation
7187cf3d73bSSam Ravnborg 			 * where a default value is further limited by
7197cf3d73bSSam Ravnborg 			 * the valid range.
7207cf3d73bSSam Ravnborg 			 */
7217cf3d73bSSam Ravnborg 			ds = prop_get_symbol(prop);
7227cf3d73bSSam Ravnborg 			if (ds != NULL) {
7237cf3d73bSSam Ravnborg 				sym_calc_value(ds);
7247cf3d73bSSam Ravnborg 				str = (const char *)ds->curr.val;
7257cf3d73bSSam Ravnborg 			}
7267cf3d73bSSam Ravnborg 		}
7277cf3d73bSSam Ravnborg 	}
7287cf3d73bSSam Ravnborg 
7297cf3d73bSSam Ravnborg 	/* Handle select statements */
7307cf3d73bSSam Ravnborg 	val = EXPR_OR(val, sym->rev_dep.tri);
7317cf3d73bSSam Ravnborg 
7327cf3d73bSSam Ravnborg 	/* transpose mod to yes if modules are not enabled */
7337cf3d73bSSam Ravnborg 	if (val == mod)
7347cf3d73bSSam Ravnborg 		if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
7357cf3d73bSSam Ravnborg 			val = yes;
7367cf3d73bSSam Ravnborg 
7377cf3d73bSSam Ravnborg 	/* transpose mod to yes if type is bool */
7387cf3d73bSSam Ravnborg 	if (sym->type == S_BOOLEAN && val == mod)
7397cf3d73bSSam Ravnborg 		val = yes;
7407cf3d73bSSam Ravnborg 
741237e3ad0SNicolas Pitre 	/* adjust the default value if this symbol is implied by another */
742237e3ad0SNicolas Pitre 	if (val < sym->implied.tri)
743237e3ad0SNicolas Pitre 		val = sym->implied.tri;
744237e3ad0SNicolas Pitre 
7457cf3d73bSSam Ravnborg 	switch (sym->type) {
7467cf3d73bSSam Ravnborg 	case S_BOOLEAN:
7477cf3d73bSSam Ravnborg 	case S_TRISTATE:
7487cf3d73bSSam Ravnborg 		switch (val) {
7497cf3d73bSSam Ravnborg 		case no: return "n";
7507cf3d73bSSam Ravnborg 		case mod: return "m";
7517cf3d73bSSam Ravnborg 		case yes: return "y";
7527cf3d73bSSam Ravnborg 		}
7537cf3d73bSSam Ravnborg 	case S_INT:
7547cf3d73bSSam Ravnborg 	case S_HEX:
7557cf3d73bSSam Ravnborg 		return str;
7567cf3d73bSSam Ravnborg 	case S_STRING:
7577cf3d73bSSam Ravnborg 		return str;
7587cf3d73bSSam Ravnborg 	case S_UNKNOWN:
7597cf3d73bSSam Ravnborg 		break;
7607cf3d73bSSam Ravnborg 	}
7617cf3d73bSSam Ravnborg 	return "";
7627cf3d73bSSam Ravnborg }
7637cf3d73bSSam Ravnborg 
7641da177e4SLinus Torvalds const char *sym_get_string_value(struct symbol *sym)
7651da177e4SLinus Torvalds {
7661da177e4SLinus Torvalds 	tristate val;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	switch (sym->type) {
7691da177e4SLinus Torvalds 	case S_BOOLEAN:
7701da177e4SLinus Torvalds 	case S_TRISTATE:
7711da177e4SLinus Torvalds 		val = sym_get_tristate_value(sym);
7721da177e4SLinus Torvalds 		switch (val) {
7731da177e4SLinus Torvalds 		case no:
7741da177e4SLinus Torvalds 			return "n";
7751da177e4SLinus Torvalds 		case mod:
776e54e692bSArnaud Lacombe 			sym_calc_value(modules_sym);
777e54e692bSArnaud Lacombe 			return (modules_sym->curr.tri == no) ? "n" : "m";
7781da177e4SLinus Torvalds 		case yes:
7791da177e4SLinus Torvalds 			return "y";
7801da177e4SLinus Torvalds 		}
7811da177e4SLinus Torvalds 		break;
7821da177e4SLinus Torvalds 	default:
7831da177e4SLinus Torvalds 		;
7841da177e4SLinus Torvalds 	}
7851da177e4SLinus Torvalds 	return (const char *)sym->curr.val;
7861da177e4SLinus Torvalds }
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds bool sym_is_changable(struct symbol *sym)
7891da177e4SLinus Torvalds {
7901da177e4SLinus Torvalds 	return sym->visible > sym->rev_dep.tri;
7911da177e4SLinus Torvalds }
7921da177e4SLinus Torvalds 
793e66f25d7SAndi Kleen static unsigned strhash(const char *s)
794e66f25d7SAndi Kleen {
795e66f25d7SAndi Kleen 	/* fnv32 hash */
796e66f25d7SAndi Kleen 	unsigned hash = 2166136261U;
797e66f25d7SAndi Kleen 	for (; *s; s++)
798e66f25d7SAndi Kleen 		hash = (hash ^ *s) * 0x01000193;
799e66f25d7SAndi Kleen 	return hash;
800e66f25d7SAndi Kleen }
801e66f25d7SAndi Kleen 
8025a1aa8a1SRoman Zippel struct symbol *sym_lookup(const char *name, int flags)
8031da177e4SLinus Torvalds {
8041da177e4SLinus Torvalds 	struct symbol *symbol;
8051da177e4SLinus Torvalds 	char *new_name;
806e66f25d7SAndi Kleen 	int hash;
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	if (name) {
8091da177e4SLinus Torvalds 		if (name[0] && !name[1]) {
8101da177e4SLinus Torvalds 			switch (name[0]) {
8111da177e4SLinus Torvalds 			case 'y': return &symbol_yes;
8121da177e4SLinus Torvalds 			case 'm': return &symbol_mod;
8131da177e4SLinus Torvalds 			case 'n': return &symbol_no;
8141da177e4SLinus Torvalds 			}
8151da177e4SLinus Torvalds 		}
816e66f25d7SAndi Kleen 		hash = strhash(name) % SYMBOL_HASHSIZE;
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 		for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
819e66f25d7SAndi Kleen 			if (symbol->name &&
820e66f25d7SAndi Kleen 			    !strcmp(symbol->name, name) &&
8215a1aa8a1SRoman Zippel 			    (flags ? symbol->flags & flags
8225a1aa8a1SRoman Zippel 				   : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
8231da177e4SLinus Torvalds 				return symbol;
8241da177e4SLinus Torvalds 		}
825cd81fc82SMasahiro Yamada 		new_name = xstrdup(name);
8261da177e4SLinus Torvalds 	} else {
8271da177e4SLinus Torvalds 		new_name = NULL;
828e66f25d7SAndi Kleen 		hash = 0;
8291da177e4SLinus Torvalds 	}
8301da177e4SLinus Torvalds 
831177acf78SAlan Cox 	symbol = xmalloc(sizeof(*symbol));
8321da177e4SLinus Torvalds 	memset(symbol, 0, sizeof(*symbol));
8331da177e4SLinus Torvalds 	symbol->name = new_name;
8341da177e4SLinus Torvalds 	symbol->type = S_UNKNOWN;
8355a1aa8a1SRoman Zippel 	symbol->flags |= flags;
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	symbol->next = symbol_hash[hash];
8381da177e4SLinus Torvalds 	symbol_hash[hash] = symbol;
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	return symbol;
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds struct symbol *sym_find(const char *name)
8441da177e4SLinus Torvalds {
8451da177e4SLinus Torvalds 	struct symbol *symbol = NULL;
8461da177e4SLinus Torvalds 	int hash = 0;
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	if (!name)
8491da177e4SLinus Torvalds 		return NULL;
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	if (name[0] && !name[1]) {
8521da177e4SLinus Torvalds 		switch (name[0]) {
8531da177e4SLinus Torvalds 		case 'y': return &symbol_yes;
8541da177e4SLinus Torvalds 		case 'm': return &symbol_mod;
8551da177e4SLinus Torvalds 		case 'n': return &symbol_no;
8561da177e4SLinus Torvalds 		}
8571da177e4SLinus Torvalds 	}
858e66f25d7SAndi Kleen 	hash = strhash(name) % SYMBOL_HASHSIZE;
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 	for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
861e66f25d7SAndi Kleen 		if (symbol->name &&
862e66f25d7SAndi Kleen 		    !strcmp(symbol->name, name) &&
8631da177e4SLinus Torvalds 		    !(symbol->flags & SYMBOL_CONST))
8641da177e4SLinus Torvalds 				break;
8651da177e4SLinus Torvalds 	}
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	return symbol;
8681da177e4SLinus Torvalds }
8691da177e4SLinus Torvalds 
870e54e692bSArnaud Lacombe const char *sym_escape_string_value(const char *in)
871e54e692bSArnaud Lacombe {
872e54e692bSArnaud Lacombe 	const char *p;
873e54e692bSArnaud Lacombe 	size_t reslen;
874e54e692bSArnaud Lacombe 	char *res;
875e54e692bSArnaud Lacombe 	size_t l;
876e54e692bSArnaud Lacombe 
877e54e692bSArnaud Lacombe 	reslen = strlen(in) + strlen("\"\"") + 1;
878e54e692bSArnaud Lacombe 
879e54e692bSArnaud Lacombe 	p = in;
880e54e692bSArnaud Lacombe 	for (;;) {
881e54e692bSArnaud Lacombe 		l = strcspn(p, "\"\\");
882e54e692bSArnaud Lacombe 		p += l;
883e54e692bSArnaud Lacombe 
884e54e692bSArnaud Lacombe 		if (p[0] == '\0')
885e54e692bSArnaud Lacombe 			break;
886e54e692bSArnaud Lacombe 
887e54e692bSArnaud Lacombe 		reslen++;
888e54e692bSArnaud Lacombe 		p++;
889e54e692bSArnaud Lacombe 	}
890e54e692bSArnaud Lacombe 
891177acf78SAlan Cox 	res = xmalloc(reslen);
892e54e692bSArnaud Lacombe 	res[0] = '\0';
893e54e692bSArnaud Lacombe 
894e54e692bSArnaud Lacombe 	strcat(res, "\"");
895e54e692bSArnaud Lacombe 
896e54e692bSArnaud Lacombe 	p = in;
897e54e692bSArnaud Lacombe 	for (;;) {
898e54e692bSArnaud Lacombe 		l = strcspn(p, "\"\\");
899e54e692bSArnaud Lacombe 		strncat(res, p, l);
900e54e692bSArnaud Lacombe 		p += l;
901e54e692bSArnaud Lacombe 
902e54e692bSArnaud Lacombe 		if (p[0] == '\0')
903e54e692bSArnaud Lacombe 			break;
904e54e692bSArnaud Lacombe 
905e54e692bSArnaud Lacombe 		strcat(res, "\\");
906e54e692bSArnaud Lacombe 		strncat(res, p++, 1);
907e54e692bSArnaud Lacombe 	}
908e54e692bSArnaud Lacombe 
909e54e692bSArnaud Lacombe 	strcat(res, "\"");
910e54e692bSArnaud Lacombe 	return res;
911e54e692bSArnaud Lacombe }
912e54e692bSArnaud Lacombe 
913193b40aeSYann E. MORIN struct sym_match {
914193b40aeSYann E. MORIN 	struct symbol	*sym;
915193b40aeSYann E. MORIN 	off_t		so, eo;
916193b40aeSYann E. MORIN };
917193b40aeSYann E. MORIN 
918193b40aeSYann E. MORIN /* Compare matched symbols as thus:
919193b40aeSYann E. MORIN  * - first, symbols that match exactly
920193b40aeSYann E. MORIN  * - then, alphabetical sort
921193b40aeSYann E. MORIN  */
922193b40aeSYann E. MORIN static int sym_rel_comp(const void *sym1, const void *sym2)
923193b40aeSYann E. MORIN {
924508382a0SYann E. MORIN 	const struct sym_match *s1 = sym1;
925508382a0SYann E. MORIN 	const struct sym_match *s2 = sym2;
92626e933e3SYann E. MORIN 	int exact1, exact2;
927193b40aeSYann E. MORIN 
928193b40aeSYann E. MORIN 	/* Exact match:
929193b40aeSYann E. MORIN 	 * - if matched length on symbol s1 is the length of that symbol,
930193b40aeSYann E. MORIN 	 *   then this symbol should come first;
931193b40aeSYann E. MORIN 	 * - if matched length on symbol s2 is the length of that symbol,
932193b40aeSYann E. MORIN 	 *   then this symbol should come first.
933193b40aeSYann E. MORIN 	 * Note: since the search can be a regexp, both symbols may match
934193b40aeSYann E. MORIN 	 * exactly; if this is the case, we can't decide which comes first,
935193b40aeSYann E. MORIN 	 * and we fallback to sorting alphabetically.
936193b40aeSYann E. MORIN 	 */
93726e933e3SYann E. MORIN 	exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
93826e933e3SYann E. MORIN 	exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
93926e933e3SYann E. MORIN 	if (exact1 && !exact2)
940193b40aeSYann E. MORIN 		return -1;
94126e933e3SYann E. MORIN 	if (!exact1 && exact2)
942193b40aeSYann E. MORIN 		return 1;
943193b40aeSYann E. MORIN 
944193b40aeSYann E. MORIN 	/* As a fallback, sort symbols alphabetically */
945193b40aeSYann E. MORIN 	return strcmp(s1->sym->name, s2->sym->name);
946193b40aeSYann E. MORIN }
947193b40aeSYann E. MORIN 
9481da177e4SLinus Torvalds struct symbol **sym_re_search(const char *pattern)
9491da177e4SLinus Torvalds {
9501da177e4SLinus Torvalds 	struct symbol *sym, **sym_arr = NULL;
951508382a0SYann E. MORIN 	struct sym_match *sym_match_arr = NULL;
9521da177e4SLinus Torvalds 	int i, cnt, size;
9531da177e4SLinus Torvalds 	regex_t re;
954193b40aeSYann E. MORIN 	regmatch_t match[1];
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	cnt = size = 0;
9571da177e4SLinus Torvalds 	/* Skip if empty */
9581da177e4SLinus Torvalds 	if (strlen(pattern) == 0)
9591da177e4SLinus Torvalds 		return NULL;
960193b40aeSYann E. MORIN 	if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
9611da177e4SLinus Torvalds 		return NULL;
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
9641da177e4SLinus Torvalds 		if (sym->flags & SYMBOL_CONST || !sym->name)
9651da177e4SLinus Torvalds 			continue;
966193b40aeSYann E. MORIN 		if (regexec(&re, sym->name, 1, match, 0))
9671da177e4SLinus Torvalds 			continue;
9681407f97aSYann E. MORIN 		if (cnt >= size) {
969193b40aeSYann E. MORIN 			void *tmp;
9701da177e4SLinus Torvalds 			size += 16;
971508382a0SYann E. MORIN 			tmp = realloc(sym_match_arr, size * sizeof(struct sym_match));
972803b3519SYann E. MORIN 			if (!tmp)
973193b40aeSYann E. MORIN 				goto sym_re_search_free;
974193b40aeSYann E. MORIN 			sym_match_arr = tmp;
9751da177e4SLinus Torvalds 		}
976da6df879SLi Zefan 		sym_calc_value(sym);
977803b3519SYann E. MORIN 		/* As regexec returned 0, we know we have a match, so
978193b40aeSYann E. MORIN 		 * we can use match[0].rm_[se]o without further checks
979193b40aeSYann E. MORIN 		 */
980508382a0SYann E. MORIN 		sym_match_arr[cnt].so = match[0].rm_so;
981508382a0SYann E. MORIN 		sym_match_arr[cnt].eo = match[0].rm_eo;
982508382a0SYann E. MORIN 		sym_match_arr[cnt++].sym = sym;
9831da177e4SLinus Torvalds 	}
984193b40aeSYann E. MORIN 	if (sym_match_arr) {
985508382a0SYann E. MORIN 		qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp);
98688127daeSHeinrich Schuchardt 		sym_arr = malloc((cnt+1) * sizeof(struct symbol *));
987193b40aeSYann E. MORIN 		if (!sym_arr)
988193b40aeSYann E. MORIN 			goto sym_re_search_free;
989193b40aeSYann E. MORIN 		for (i = 0; i < cnt; i++)
990508382a0SYann E. MORIN 			sym_arr[i] = sym_match_arr[i].sym;
9911da177e4SLinus Torvalds 		sym_arr[cnt] = NULL;
992193b40aeSYann E. MORIN 	}
993193b40aeSYann E. MORIN sym_re_search_free:
994508382a0SYann E. MORIN 	/* sym_match_arr can be NULL if no match, but free(NULL) is OK */
995193b40aeSYann E. MORIN 	free(sym_match_arr);
9961da177e4SLinus Torvalds 	regfree(&re);
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 	return sym_arr;
9991da177e4SLinus Torvalds }
10001da177e4SLinus Torvalds 
1001d595cea6SRoman Zippel /*
1002d595cea6SRoman Zippel  * When we check for recursive dependencies we use a stack to save
1003d595cea6SRoman Zippel  * current state so we can print out relevant info to user.
1004d595cea6SRoman Zippel  * The entries are located on the call stack so no need to free memory.
10058d9dfe82SMartin Walch  * Note insert() remove() must always match to properly clear the stack.
1006d595cea6SRoman Zippel  */
1007d595cea6SRoman Zippel static struct dep_stack {
1008d595cea6SRoman Zippel 	struct dep_stack *prev, *next;
1009d595cea6SRoman Zippel 	struct symbol *sym;
1010d595cea6SRoman Zippel 	struct property *prop;
1011f498926cSMasahiro Yamada 	struct expr **expr;
1012d595cea6SRoman Zippel } *check_top;
1013d595cea6SRoman Zippel 
1014d595cea6SRoman Zippel static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
1015d595cea6SRoman Zippel {
1016d595cea6SRoman Zippel 	memset(stack, 0, sizeof(*stack));
1017d595cea6SRoman Zippel 	if (check_top)
1018d595cea6SRoman Zippel 		check_top->next = stack;
1019d595cea6SRoman Zippel 	stack->prev = check_top;
1020d595cea6SRoman Zippel 	stack->sym = sym;
1021d595cea6SRoman Zippel 	check_top = stack;
1022d595cea6SRoman Zippel }
1023d595cea6SRoman Zippel 
1024d595cea6SRoman Zippel static void dep_stack_remove(void)
1025d595cea6SRoman Zippel {
1026d595cea6SRoman Zippel 	check_top = check_top->prev;
1027d595cea6SRoman Zippel 	if (check_top)
1028d595cea6SRoman Zippel 		check_top->next = NULL;
1029d595cea6SRoman Zippel }
1030d595cea6SRoman Zippel 
1031d595cea6SRoman Zippel /*
1032d595cea6SRoman Zippel  * Called when we have detected a recursive dependency.
1033d595cea6SRoman Zippel  * check_top point to the top of the stact so we use
1034d595cea6SRoman Zippel  * the ->prev pointer to locate the bottom of the stack.
1035d595cea6SRoman Zippel  */
1036d595cea6SRoman Zippel static void sym_check_print_recursive(struct symbol *last_sym)
1037d595cea6SRoman Zippel {
1038d595cea6SRoman Zippel 	struct dep_stack *stack;
1039d595cea6SRoman Zippel 	struct symbol *sym, *next_sym;
1040d595cea6SRoman Zippel 	struct menu *menu = NULL;
1041d595cea6SRoman Zippel 	struct property *prop;
1042d595cea6SRoman Zippel 	struct dep_stack cv_stack;
1043d595cea6SRoman Zippel 
1044d595cea6SRoman Zippel 	if (sym_is_choice_value(last_sym)) {
1045d595cea6SRoman Zippel 		dep_stack_insert(&cv_stack, last_sym);
1046d595cea6SRoman Zippel 		last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
1047d595cea6SRoman Zippel 	}
1048d595cea6SRoman Zippel 
1049d595cea6SRoman Zippel 	for (stack = check_top; stack != NULL; stack = stack->prev)
1050d595cea6SRoman Zippel 		if (stack->sym == last_sym)
1051d595cea6SRoman Zippel 			break;
1052d595cea6SRoman Zippel 	if (!stack) {
1053d595cea6SRoman Zippel 		fprintf(stderr, "unexpected recursive dependency error\n");
1054d595cea6SRoman Zippel 		return;
1055d595cea6SRoman Zippel 	}
1056d595cea6SRoman Zippel 
1057d595cea6SRoman Zippel 	for (; stack; stack = stack->next) {
1058d595cea6SRoman Zippel 		sym = stack->sym;
1059d595cea6SRoman Zippel 		next_sym = stack->next ? stack->next->sym : last_sym;
1060d595cea6SRoman Zippel 		prop = stack->prop;
10613643f849SSam Ravnborg 		if (prop == NULL)
10623643f849SSam Ravnborg 			prop = stack->sym->prop;
1063d595cea6SRoman Zippel 
1064d595cea6SRoman Zippel 		/* for choice values find the menu entry (used below) */
1065d595cea6SRoman Zippel 		if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
1066d595cea6SRoman Zippel 			for (prop = sym->prop; prop; prop = prop->next) {
1067d595cea6SRoman Zippel 				menu = prop->menu;
1068d595cea6SRoman Zippel 				if (prop->menu)
1069d595cea6SRoman Zippel 					break;
1070d595cea6SRoman Zippel 			}
1071d595cea6SRoman Zippel 		}
1072d595cea6SRoman Zippel 		if (stack->sym == last_sym)
1073d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
1074d595cea6SRoman Zippel 				prop->file->name, prop->lineno);
1075e3b03bf2SMasahiro Yamada 
1076f498926cSMasahiro Yamada 		if (sym_is_choice(sym)) {
1077d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
1078d595cea6SRoman Zippel 				menu->file->name, menu->lineno,
1079d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
1080d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
1081d595cea6SRoman Zippel 		} else if (sym_is_choice_value(sym)) {
1082d595cea6SRoman Zippel 			fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
1083d595cea6SRoman Zippel 				menu->file->name, menu->lineno,
1084d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
1085d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
1086f498926cSMasahiro Yamada 		} else if (stack->expr == &sym->dir_dep.expr) {
1087f498926cSMasahiro Yamada 			fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
1088d595cea6SRoman Zippel 				prop->file->name, prop->lineno,
1089d595cea6SRoman Zippel 				sym->name ? sym->name : "<choice>",
1090d595cea6SRoman Zippel 				next_sym->name ? next_sym->name : "<choice>");
1091f498926cSMasahiro Yamada 		} else if (stack->expr == &sym->rev_dep.expr) {
1092f498926cSMasahiro Yamada 			fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
1093f498926cSMasahiro Yamada 				prop->file->name, prop->lineno,
1094f498926cSMasahiro Yamada 				sym->name ? sym->name : "<choice>",
1095f498926cSMasahiro Yamada 				next_sym->name ? next_sym->name : "<choice>");
1096f498926cSMasahiro Yamada 		} else if (stack->expr == &sym->implied.expr) {
1097f498926cSMasahiro Yamada 			fprintf(stderr, "%s:%d:\tsymbol %s is implied by %s\n",
1098f498926cSMasahiro Yamada 				prop->file->name, prop->lineno,
1099f498926cSMasahiro Yamada 				sym->name ? sym->name : "<choice>",
1100f498926cSMasahiro Yamada 				next_sym->name ? next_sym->name : "<choice>");
1101f498926cSMasahiro Yamada 		} else if (stack->expr) {
1102f498926cSMasahiro Yamada 			fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
1103f498926cSMasahiro Yamada 				prop->file->name, prop->lineno,
1104f498926cSMasahiro Yamada 				sym->name ? sym->name : "<choice>",
1105f498926cSMasahiro Yamada 				prop_get_type_name(prop->type),
1106f498926cSMasahiro Yamada 				next_sym->name ? next_sym->name : "<choice>");
1107f498926cSMasahiro Yamada 		} else {
1108f498926cSMasahiro Yamada 			fprintf(stderr, "%s:%d:\tsymbol %s %s is visible depending on %s\n",
1109f498926cSMasahiro Yamada 				prop->file->name, prop->lineno,
1110f498926cSMasahiro Yamada 				sym->name ? sym->name : "<choice>",
1111f498926cSMasahiro Yamada 				prop_get_type_name(prop->type),
1112f498926cSMasahiro Yamada 				next_sym->name ? next_sym->name : "<choice>");
1113d595cea6SRoman Zippel 		}
1114d595cea6SRoman Zippel 	}
1115d595cea6SRoman Zippel 
1116e3b03bf2SMasahiro Yamada 	fprintf(stderr,
1117e3b03bf2SMasahiro Yamada 		"For a resolution refer to Documentation/kbuild/kconfig-language.txt\n"
1118e3b03bf2SMasahiro Yamada 		"subsection \"Kconfig recursive dependency limitations\"\n"
1119e3b03bf2SMasahiro Yamada 		"\n");
1120e3b03bf2SMasahiro Yamada 
1121d595cea6SRoman Zippel 	if (check_top == &cv_stack)
1122d595cea6SRoman Zippel 		dep_stack_remove();
1123d595cea6SRoman Zippel }
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds static struct symbol *sym_check_expr_deps(struct expr *e)
11261da177e4SLinus Torvalds {
11271da177e4SLinus Torvalds 	struct symbol *sym;
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	if (!e)
11301da177e4SLinus Torvalds 		return NULL;
11311da177e4SLinus Torvalds 	switch (e->type) {
11321da177e4SLinus Torvalds 	case E_OR:
11331da177e4SLinus Torvalds 	case E_AND:
11341da177e4SLinus Torvalds 		sym = sym_check_expr_deps(e->left.expr);
11351da177e4SLinus Torvalds 		if (sym)
11361da177e4SLinus Torvalds 			return sym;
11371da177e4SLinus Torvalds 		return sym_check_expr_deps(e->right.expr);
11381da177e4SLinus Torvalds 	case E_NOT:
11391da177e4SLinus Torvalds 		return sym_check_expr_deps(e->left.expr);
11401da177e4SLinus Torvalds 	case E_EQUAL:
114131847b67SJan Beulich 	case E_GEQ:
114231847b67SJan Beulich 	case E_GTH:
114331847b67SJan Beulich 	case E_LEQ:
114431847b67SJan Beulich 	case E_LTH:
11451da177e4SLinus Torvalds 	case E_UNEQUAL:
11461da177e4SLinus Torvalds 		sym = sym_check_deps(e->left.sym);
11471da177e4SLinus Torvalds 		if (sym)
11481da177e4SLinus Torvalds 			return sym;
11491da177e4SLinus Torvalds 		return sym_check_deps(e->right.sym);
11501da177e4SLinus Torvalds 	case E_SYMBOL:
11511da177e4SLinus Torvalds 		return sym_check_deps(e->left.sym);
11521da177e4SLinus Torvalds 	default:
11531da177e4SLinus Torvalds 		break;
11541da177e4SLinus Torvalds 	}
11559e3e10c7SMasahiro Yamada 	fprintf(stderr, "Oops! How to check %d?\n", e->type);
11561da177e4SLinus Torvalds 	return NULL;
11571da177e4SLinus Torvalds }
11581da177e4SLinus Torvalds 
11595447d34bSSam Ravnborg /* return NULL when dependencies are OK */
116048981178SRoman Zippel static struct symbol *sym_check_sym_deps(struct symbol *sym)
116148981178SRoman Zippel {
116248981178SRoman Zippel 	struct symbol *sym2;
116348981178SRoman Zippel 	struct property *prop;
1164d595cea6SRoman Zippel 	struct dep_stack stack;
1165d595cea6SRoman Zippel 
1166d595cea6SRoman Zippel 	dep_stack_insert(&stack, sym);
116748981178SRoman Zippel 
1168f498926cSMasahiro Yamada 	stack.expr = &sym->dir_dep.expr;
1169f498926cSMasahiro Yamada 	sym2 = sym_check_expr_deps(sym->dir_dep.expr);
1170f498926cSMasahiro Yamada 	if (sym2)
1171f498926cSMasahiro Yamada 		goto out;
1172f498926cSMasahiro Yamada 
1173f498926cSMasahiro Yamada 	stack.expr = &sym->rev_dep.expr;
117448981178SRoman Zippel 	sym2 = sym_check_expr_deps(sym->rev_dep.expr);
117548981178SRoman Zippel 	if (sym2)
1176d595cea6SRoman Zippel 		goto out;
117748981178SRoman Zippel 
1178f498926cSMasahiro Yamada 	stack.expr = &sym->implied.expr;
11795e8c5299SMasahiro Yamada 	sym2 = sym_check_expr_deps(sym->implied.expr);
11805e8c5299SMasahiro Yamada 	if (sym2)
11815e8c5299SMasahiro Yamada 		goto out;
11825e8c5299SMasahiro Yamada 
1183f498926cSMasahiro Yamada 	stack.expr = NULL;
1184f498926cSMasahiro Yamada 
118548981178SRoman Zippel 	for (prop = sym->prop; prop; prop = prop->next) {
11865e8c5299SMasahiro Yamada 		if (prop->type == P_CHOICE || prop->type == P_SELECT ||
11875e8c5299SMasahiro Yamada 		    prop->type == P_IMPLY)
118848981178SRoman Zippel 			continue;
1189d595cea6SRoman Zippel 		stack.prop = prop;
119048981178SRoman Zippel 		sym2 = sym_check_expr_deps(prop->visible.expr);
119148981178SRoman Zippel 		if (sym2)
119248981178SRoman Zippel 			break;
119348981178SRoman Zippel 		if (prop->type != P_DEFAULT || sym_is_choice(sym))
119448981178SRoman Zippel 			continue;
1195f498926cSMasahiro Yamada 		stack.expr = &prop->expr;
119648981178SRoman Zippel 		sym2 = sym_check_expr_deps(prop->expr);
119748981178SRoman Zippel 		if (sym2)
119848981178SRoman Zippel 			break;
1199d595cea6SRoman Zippel 		stack.expr = NULL;
120048981178SRoman Zippel 	}
120148981178SRoman Zippel 
1202d595cea6SRoman Zippel out:
1203d595cea6SRoman Zippel 	dep_stack_remove();
1204d595cea6SRoman Zippel 
120548981178SRoman Zippel 	return sym2;
120648981178SRoman Zippel }
120748981178SRoman Zippel 
120848981178SRoman Zippel static struct symbol *sym_check_choice_deps(struct symbol *choice)
120948981178SRoman Zippel {
121048981178SRoman Zippel 	struct symbol *sym, *sym2;
121148981178SRoman Zippel 	struct property *prop;
121248981178SRoman Zippel 	struct expr *e;
1213d595cea6SRoman Zippel 	struct dep_stack stack;
1214d595cea6SRoman Zippel 
1215d595cea6SRoman Zippel 	dep_stack_insert(&stack, choice);
121648981178SRoman Zippel 
121748981178SRoman Zippel 	prop = sym_get_choice_prop(choice);
121848981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym)
121948981178SRoman Zippel 		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
122048981178SRoman Zippel 
122148981178SRoman Zippel 	choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
122248981178SRoman Zippel 	sym2 = sym_check_sym_deps(choice);
122348981178SRoman Zippel 	choice->flags &= ~SYMBOL_CHECK;
122448981178SRoman Zippel 	if (sym2)
122548981178SRoman Zippel 		goto out;
122648981178SRoman Zippel 
122748981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym) {
122848981178SRoman Zippel 		sym2 = sym_check_sym_deps(sym);
1229d595cea6SRoman Zippel 		if (sym2)
123048981178SRoman Zippel 			break;
123148981178SRoman Zippel 	}
123248981178SRoman Zippel out:
123348981178SRoman Zippel 	expr_list_for_each_sym(prop->expr, e, sym)
123448981178SRoman Zippel 		sym->flags &= ~SYMBOL_CHECK;
123548981178SRoman Zippel 
123648981178SRoman Zippel 	if (sym2 && sym_is_choice_value(sym2) &&
123748981178SRoman Zippel 	    prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
123848981178SRoman Zippel 		sym2 = choice;
123948981178SRoman Zippel 
1240d595cea6SRoman Zippel 	dep_stack_remove();
1241d595cea6SRoman Zippel 
124248981178SRoman Zippel 	return sym2;
124348981178SRoman Zippel }
124448981178SRoman Zippel 
12451da177e4SLinus Torvalds struct symbol *sym_check_deps(struct symbol *sym)
12461da177e4SLinus Torvalds {
12471da177e4SLinus Torvalds 	struct symbol *sym2;
12481da177e4SLinus Torvalds 	struct property *prop;
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 	if (sym->flags & SYMBOL_CHECK) {
1251d595cea6SRoman Zippel 		sym_check_print_recursive(sym);
12521da177e4SLinus Torvalds 		return sym;
12531da177e4SLinus Torvalds 	}
12543f04e7ddSDavid Gibson 	if (sym->flags & SYMBOL_CHECKED)
12553f04e7ddSDavid Gibson 		return NULL;
12561da177e4SLinus Torvalds 
125748981178SRoman Zippel 	if (sym_is_choice_value(sym)) {
1258d595cea6SRoman Zippel 		struct dep_stack stack;
1259d595cea6SRoman Zippel 
126048981178SRoman Zippel 		/* for choice groups start the check with main choice symbol */
1261d595cea6SRoman Zippel 		dep_stack_insert(&stack, sym);
126248981178SRoman Zippel 		prop = sym_get_choice_prop(sym);
126348981178SRoman Zippel 		sym2 = sym_check_deps(prop_get_symbol(prop));
1264d595cea6SRoman Zippel 		dep_stack_remove();
126548981178SRoman Zippel 	} else if (sym_is_choice(sym)) {
126648981178SRoman Zippel 		sym2 = sym_check_choice_deps(sym);
126748981178SRoman Zippel 	} else {
12681da177e4SLinus Torvalds 		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
126948981178SRoman Zippel 		sym2 = sym_check_sym_deps(sym);
12701da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_CHECK;
127148981178SRoman Zippel 	}
127248981178SRoman Zippel 
12731da177e4SLinus Torvalds 	return sym2;
12741da177e4SLinus Torvalds }
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds struct property *prop_alloc(enum prop_type type, struct symbol *sym)
12771da177e4SLinus Torvalds {
12781da177e4SLinus Torvalds 	struct property *prop;
12791da177e4SLinus Torvalds 	struct property **propp;
12801da177e4SLinus Torvalds 
1281177acf78SAlan Cox 	prop = xmalloc(sizeof(*prop));
12821da177e4SLinus Torvalds 	memset(prop, 0, sizeof(*prop));
12831da177e4SLinus Torvalds 	prop->type = type;
12841da177e4SLinus Torvalds 	prop->sym = sym;
12851da177e4SLinus Torvalds 	prop->file = current_file;
12861da177e4SLinus Torvalds 	prop->lineno = zconf_lineno();
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 	/* append property to the prop list of symbol */
12891da177e4SLinus Torvalds 	if (sym) {
12901da177e4SLinus Torvalds 		for (propp = &sym->prop; *propp; propp = &(*propp)->next)
12911da177e4SLinus Torvalds 			;
12921da177e4SLinus Torvalds 		*propp = prop;
12931da177e4SLinus Torvalds 	}
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 	return prop;
12961da177e4SLinus Torvalds }
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds struct symbol *prop_get_symbol(struct property *prop)
12991da177e4SLinus Torvalds {
13001da177e4SLinus Torvalds 	if (prop->expr && (prop->expr->type == E_SYMBOL ||
13017a962923SRoman Zippel 			   prop->expr->type == E_LIST))
13021da177e4SLinus Torvalds 		return prop->expr->left.sym;
13031da177e4SLinus Torvalds 	return NULL;
13041da177e4SLinus Torvalds }
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds const char *prop_get_type_name(enum prop_type type)
13071da177e4SLinus Torvalds {
13081da177e4SLinus Torvalds 	switch (type) {
13091da177e4SLinus Torvalds 	case P_PROMPT:
13101da177e4SLinus Torvalds 		return "prompt";
13111da177e4SLinus Torvalds 	case P_COMMENT:
13121da177e4SLinus Torvalds 		return "comment";
13131da177e4SLinus Torvalds 	case P_MENU:
13141da177e4SLinus Torvalds 		return "menu";
13151da177e4SLinus Torvalds 	case P_DEFAULT:
13161da177e4SLinus Torvalds 		return "default";
13171da177e4SLinus Torvalds 	case P_CHOICE:
13181da177e4SLinus Torvalds 		return "choice";
13191da177e4SLinus Torvalds 	case P_SELECT:
13201da177e4SLinus Torvalds 		return "select";
1321237e3ad0SNicolas Pitre 	case P_IMPLY:
1322237e3ad0SNicolas Pitre 		return "imply";
13231da177e4SLinus Torvalds 	case P_RANGE:
13241da177e4SLinus Torvalds 		return "range";
132559e89e3dSSam Ravnborg 	case P_SYMBOL:
132659e89e3dSSam Ravnborg 		return "symbol";
13271da177e4SLinus Torvalds 	case P_UNKNOWN:
13281da177e4SLinus Torvalds 		break;
13291da177e4SLinus Torvalds 	}
13301da177e4SLinus Torvalds 	return "unknown";
13311da177e4SLinus Torvalds }
1332