xref: /linux/scripts/kconfig/confdata.c (revision b92d804a51796b77c7b2b11881b2700eaac88114)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
31da177e4SLinus Torvalds  * Released under the terms of the GNU GPL v2.0.
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <sys/stat.h>
71da177e4SLinus Torvalds #include <ctype.h>
894bedecaSArnaud Lacombe #include <errno.h>
92e3646e5SRoman Zippel #include <fcntl.h>
1010a4b277SArnaud Lacombe #include <stdarg.h>
111da177e4SLinus Torvalds #include <stdio.h>
121da177e4SLinus Torvalds #include <stdlib.h>
131da177e4SLinus Torvalds #include <string.h>
141da177e4SLinus Torvalds #include <time.h>
151da177e4SLinus Torvalds #include <unistd.h>
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include "lkc.h"
181da177e4SLinus Torvalds 
19ad8d40cdSMichal Marek struct conf_printer {
20ad8d40cdSMichal Marek 	void (*print_symbol)(FILE *, struct symbol *, const char *, void *);
21ad8d40cdSMichal Marek 	void (*print_comment)(FILE *, const char *, void *);
22ad8d40cdSMichal Marek };
23ad8d40cdSMichal Marek 
24c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
25c1a0f5e3SRoman Zippel 	__attribute__ ((format (printf, 1, 2)));
26c1a0f5e3SRoman Zippel 
2742368c37SMichal Marek static void conf_message(const char *fmt, ...)
2842368c37SMichal Marek 	__attribute__ ((format (printf, 1, 2)));
2942368c37SMichal Marek 
30c1a0f5e3SRoman Zippel static const char *conf_filename;
31c1a0f5e3SRoman Zippel static int conf_lineno, conf_warnings, conf_unsaved;
32c1a0f5e3SRoman Zippel 
331da177e4SLinus Torvalds const char conf_defname[] = "arch/$ARCH/defconfig";
341da177e4SLinus Torvalds 
35c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
36c1a0f5e3SRoman Zippel {
37c1a0f5e3SRoman Zippel 	va_list ap;
38c1a0f5e3SRoman Zippel 	va_start(ap, fmt);
39c1a0f5e3SRoman Zippel 	fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
40c1a0f5e3SRoman Zippel 	vfprintf(stderr, fmt, ap);
41c1a0f5e3SRoman Zippel 	fprintf(stderr, "\n");
42c1a0f5e3SRoman Zippel 	va_end(ap);
43c1a0f5e3SRoman Zippel 	conf_warnings++;
44c1a0f5e3SRoman Zippel }
45c1a0f5e3SRoman Zippel 
4642368c37SMichal Marek static void conf_default_message_callback(const char *fmt, va_list ap)
4742368c37SMichal Marek {
4842368c37SMichal Marek 	printf("#\n# ");
4942368c37SMichal Marek 	vprintf(fmt, ap);
5042368c37SMichal Marek 	printf("\n#\n");
5142368c37SMichal Marek }
5242368c37SMichal Marek 
5342368c37SMichal Marek static void (*conf_message_callback) (const char *fmt, va_list ap) =
5442368c37SMichal Marek 	conf_default_message_callback;
5542368c37SMichal Marek void conf_set_message_callback(void (*fn) (const char *fmt, va_list ap))
5642368c37SMichal Marek {
5742368c37SMichal Marek 	conf_message_callback = fn;
5842368c37SMichal Marek }
5942368c37SMichal Marek 
6042368c37SMichal Marek static void conf_message(const char *fmt, ...)
6142368c37SMichal Marek {
6242368c37SMichal Marek 	va_list ap;
6342368c37SMichal Marek 
6442368c37SMichal Marek 	va_start(ap, fmt);
6542368c37SMichal Marek 	if (conf_message_callback)
6642368c37SMichal Marek 		conf_message_callback(fmt, ap);
67b6a2ab2cSColin Ian King 	va_end(ap);
6842368c37SMichal Marek }
6942368c37SMichal Marek 
7014cdd3c4SRoman Zippel const char *conf_get_configname(void)
7114cdd3c4SRoman Zippel {
7214cdd3c4SRoman Zippel 	char *name = getenv("KCONFIG_CONFIG");
7314cdd3c4SRoman Zippel 
7414cdd3c4SRoman Zippel 	return name ? name : ".config";
7514cdd3c4SRoman Zippel }
7614cdd3c4SRoman Zippel 
7712122f62SMarkus Heidelberg const char *conf_get_autoconfig_name(void)
7812122f62SMarkus Heidelberg {
7912122f62SMarkus Heidelberg 	char *name = getenv("KCONFIG_AUTOCONFIG");
8012122f62SMarkus Heidelberg 
8112122f62SMarkus Heidelberg 	return name ? name : "include/config/auto.conf";
8212122f62SMarkus Heidelberg }
8312122f62SMarkus Heidelberg 
8448b9d03cSJ.A. Magallon static char *conf_expand_value(const char *in)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	struct symbol *sym;
8748b9d03cSJ.A. Magallon 	const char *src;
881da177e4SLinus Torvalds 	static char res_value[SYMBOL_MAXLENGTH];
891da177e4SLinus Torvalds 	char *dst, name[SYMBOL_MAXLENGTH];
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	res_value[0] = 0;
921da177e4SLinus Torvalds 	dst = name;
931da177e4SLinus Torvalds 	while ((src = strchr(in, '$'))) {
941da177e4SLinus Torvalds 		strncat(res_value, in, src - in);
951da177e4SLinus Torvalds 		src++;
961da177e4SLinus Torvalds 		dst = name;
971da177e4SLinus Torvalds 		while (isalnum(*src) || *src == '_')
981da177e4SLinus Torvalds 			*dst++ = *src++;
991da177e4SLinus Torvalds 		*dst = 0;
1001da177e4SLinus Torvalds 		sym = sym_lookup(name, 0);
1011da177e4SLinus Torvalds 		sym_calc_value(sym);
1021da177e4SLinus Torvalds 		strcat(res_value, sym_get_string_value(sym));
1031da177e4SLinus Torvalds 		in = src;
1041da177e4SLinus Torvalds 	}
1051da177e4SLinus Torvalds 	strcat(res_value, in);
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	return res_value;
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds char *conf_get_default_confname(void)
1111da177e4SLinus Torvalds {
1121da177e4SLinus Torvalds 	struct stat buf;
1131da177e4SLinus Torvalds 	static char fullname[PATH_MAX+1];
1141da177e4SLinus Torvalds 	char *env, *name;
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	name = conf_expand_value(conf_defname);
1171da177e4SLinus Torvalds 	env = getenv(SRCTREE);
1181da177e4SLinus Torvalds 	if (env) {
1191da177e4SLinus Torvalds 		sprintf(fullname, "%s/%s", env, name);
1201da177e4SLinus Torvalds 		if (!stat(fullname, &buf))
1211da177e4SLinus Torvalds 			return fullname;
1221da177e4SLinus Torvalds 	}
1231da177e4SLinus Torvalds 	return name;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
1269c900a9cSSam Ravnborg static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
1279c900a9cSSam Ravnborg {
1289c900a9cSSam Ravnborg 	char *p2;
1299c900a9cSSam Ravnborg 
1309c900a9cSSam Ravnborg 	switch (sym->type) {
1319c900a9cSSam Ravnborg 	case S_TRISTATE:
1329c900a9cSSam Ravnborg 		if (p[0] == 'm') {
1339c900a9cSSam Ravnborg 			sym->def[def].tri = mod;
1349c900a9cSSam Ravnborg 			sym->flags |= def_flags;
1359c900a9cSSam Ravnborg 			break;
1369c900a9cSSam Ravnborg 		}
137d8fc3200SArnaud Lacombe 		/* fall through */
1389c900a9cSSam Ravnborg 	case S_BOOLEAN:
1399c900a9cSSam Ravnborg 		if (p[0] == 'y') {
1409c900a9cSSam Ravnborg 			sym->def[def].tri = yes;
1419c900a9cSSam Ravnborg 			sym->flags |= def_flags;
1429c900a9cSSam Ravnborg 			break;
1439c900a9cSSam Ravnborg 		}
1449c900a9cSSam Ravnborg 		if (p[0] == 'n') {
1459c900a9cSSam Ravnborg 			sym->def[def].tri = no;
1469c900a9cSSam Ravnborg 			sym->flags |= def_flags;
1479c900a9cSSam Ravnborg 			break;
1489c900a9cSSam Ravnborg 		}
14904b19b77SYann E. MORIN 		if (def != S_DEF_AUTO)
15004b19b77SYann E. MORIN 			conf_warning("symbol value '%s' invalid for %s",
15104b19b77SYann E. MORIN 				     p, sym->name);
15275f1468bSArnaud Lacombe 		return 1;
1539c900a9cSSam Ravnborg 	case S_OTHER:
1549c900a9cSSam Ravnborg 		if (*p != '"') {
1559c900a9cSSam Ravnborg 			for (p2 = p; *p2 && !isspace(*p2); p2++)
1569c900a9cSSam Ravnborg 				;
1579c900a9cSSam Ravnborg 			sym->type = S_STRING;
1589c900a9cSSam Ravnborg 			goto done;
1599c900a9cSSam Ravnborg 		}
160d8fc3200SArnaud Lacombe 		/* fall through */
1619c900a9cSSam Ravnborg 	case S_STRING:
1629c900a9cSSam Ravnborg 		if (*p++ != '"')
1639c900a9cSSam Ravnborg 			break;
1649c900a9cSSam Ravnborg 		for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
1659c900a9cSSam Ravnborg 			if (*p2 == '"') {
1669c900a9cSSam Ravnborg 				*p2 = 0;
1679c900a9cSSam Ravnborg 				break;
1689c900a9cSSam Ravnborg 			}
1699c900a9cSSam Ravnborg 			memmove(p2, p2 + 1, strlen(p2));
1709c900a9cSSam Ravnborg 		}
1719c900a9cSSam Ravnborg 		if (!p2) {
17204b19b77SYann E. MORIN 			if (def != S_DEF_AUTO)
1739c900a9cSSam Ravnborg 				conf_warning("invalid string found");
1749c900a9cSSam Ravnborg 			return 1;
1759c900a9cSSam Ravnborg 		}
176d8fc3200SArnaud Lacombe 		/* fall through */
1779c900a9cSSam Ravnborg 	case S_INT:
1789c900a9cSSam Ravnborg 	case S_HEX:
1799c900a9cSSam Ravnborg 	done:
1809c900a9cSSam Ravnborg 		if (sym_string_valid(sym, p)) {
1819c900a9cSSam Ravnborg 			sym->def[def].val = strdup(p);
1829c900a9cSSam Ravnborg 			sym->flags |= def_flags;
1839c900a9cSSam Ravnborg 		} else {
18404b19b77SYann E. MORIN 			if (def != S_DEF_AUTO)
18504b19b77SYann E. MORIN 				conf_warning("symbol value '%s' invalid for %s",
18604b19b77SYann E. MORIN 					     p, sym->name);
1879c900a9cSSam Ravnborg 			return 1;
1889c900a9cSSam Ravnborg 		}
1899c900a9cSSam Ravnborg 		break;
1909c900a9cSSam Ravnborg 	default:
1919c900a9cSSam Ravnborg 		;
1929c900a9cSSam Ravnborg 	}
1939c900a9cSSam Ravnborg 	return 0;
1949c900a9cSSam Ravnborg }
1959c900a9cSSam Ravnborg 
1961a7a8c6fSCody Schafer #define LINE_GROWTH 16
1971a7a8c6fSCody Schafer static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
1981a7a8c6fSCody Schafer {
1991a7a8c6fSCody Schafer 	char *nline;
2001a7a8c6fSCody Schafer 	size_t new_size = slen + 1;
2011a7a8c6fSCody Schafer 	if (new_size > *n) {
2021a7a8c6fSCody Schafer 		new_size += LINE_GROWTH - 1;
2031a7a8c6fSCody Schafer 		new_size *= 2;
2041a7a8c6fSCody Schafer 		nline = realloc(*lineptr, new_size);
2051a7a8c6fSCody Schafer 		if (!nline)
2061a7a8c6fSCody Schafer 			return -1;
2071a7a8c6fSCody Schafer 
2081a7a8c6fSCody Schafer 		*lineptr = nline;
2091a7a8c6fSCody Schafer 		*n = new_size;
2101a7a8c6fSCody Schafer 	}
2111a7a8c6fSCody Schafer 
2121a7a8c6fSCody Schafer 	(*lineptr)[slen] = c;
2131a7a8c6fSCody Schafer 
2141a7a8c6fSCody Schafer 	return 0;
2151a7a8c6fSCody Schafer }
2161a7a8c6fSCody Schafer 
2171a7a8c6fSCody Schafer static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
2181a7a8c6fSCody Schafer {
2191a7a8c6fSCody Schafer 	char *line = *lineptr;
2201a7a8c6fSCody Schafer 	size_t slen = 0;
2211a7a8c6fSCody Schafer 
2221a7a8c6fSCody Schafer 	for (;;) {
2231a7a8c6fSCody Schafer 		int c = getc(stream);
2241a7a8c6fSCody Schafer 
2251a7a8c6fSCody Schafer 		switch (c) {
2261a7a8c6fSCody Schafer 		case '\n':
2271a7a8c6fSCody Schafer 			if (add_byte(c, &line, slen, n) < 0)
2281a7a8c6fSCody Schafer 				goto e_out;
2291a7a8c6fSCody Schafer 			slen++;
2301a7a8c6fSCody Schafer 			/* fall through */
2311a7a8c6fSCody Schafer 		case EOF:
2321a7a8c6fSCody Schafer 			if (add_byte('\0', &line, slen, n) < 0)
2331a7a8c6fSCody Schafer 				goto e_out;
2341a7a8c6fSCody Schafer 			*lineptr = line;
2351a7a8c6fSCody Schafer 			if (slen == 0)
2361a7a8c6fSCody Schafer 				return -1;
2371a7a8c6fSCody Schafer 			return slen;
2381a7a8c6fSCody Schafer 		default:
2391a7a8c6fSCody Schafer 			if (add_byte(c, &line, slen, n) < 0)
2401a7a8c6fSCody Schafer 				goto e_out;
2411a7a8c6fSCody Schafer 			slen++;
2421a7a8c6fSCody Schafer 		}
2431a7a8c6fSCody Schafer 	}
2441a7a8c6fSCody Schafer 
2451a7a8c6fSCody Schafer e_out:
2461a7a8c6fSCody Schafer 	line[slen-1] = '\0';
2471a7a8c6fSCody Schafer 	*lineptr = line;
2481a7a8c6fSCody Schafer 	return -1;
2491a7a8c6fSCody Schafer }
2501a7a8c6fSCody Schafer 
251669bfad9SRoman Zippel int conf_read_simple(const char *name, int def)
2521da177e4SLinus Torvalds {
2531da177e4SLinus Torvalds 	FILE *in = NULL;
2541a7a8c6fSCody Schafer 	char   *line = NULL;
2551a7a8c6fSCody Schafer 	size_t  line_asize = 0;
2561da177e4SLinus Torvalds 	char *p, *p2;
2571da177e4SLinus Torvalds 	struct symbol *sym;
258669bfad9SRoman Zippel 	int i, def_flags;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	if (name) {
2611da177e4SLinus Torvalds 		in = zconf_fopen(name);
2621da177e4SLinus Torvalds 	} else {
263face4374SRoman Zippel 		struct property *prop;
264face4374SRoman Zippel 
26514cdd3c4SRoman Zippel 		name = conf_get_configname();
266ddc97cacSRoman Zippel 		in = zconf_fopen(name);
267ddc97cacSRoman Zippel 		if (in)
268ddc97cacSRoman Zippel 			goto load;
269bfc10001SKarsten Wiese 		sym_add_change_count(1);
2706b87b70cSAl Viro 		if (!sym_defconfig_list)
271face4374SRoman Zippel 			return 1;
272face4374SRoman Zippel 
273face4374SRoman Zippel 		for_all_defaults(sym_defconfig_list, prop) {
274face4374SRoman Zippel 			if (expr_calc_value(prop->visible.expr) == no ||
275face4374SRoman Zippel 			    prop->expr->type != E_SYMBOL)
276face4374SRoman Zippel 				continue;
277face4374SRoman Zippel 			name = conf_expand_value(prop->expr->left.sym->name);
2781da177e4SLinus Torvalds 			in = zconf_fopen(name);
2791da177e4SLinus Torvalds 			if (in) {
28042368c37SMichal Marek 				conf_message(_("using defaults found in %s"),
28142368c37SMichal Marek 					 name);
282ddc97cacSRoman Zippel 				goto load;
2831da177e4SLinus Torvalds 			}
2841da177e4SLinus Torvalds 		}
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds 	if (!in)
2871da177e4SLinus Torvalds 		return 1;
2881da177e4SLinus Torvalds 
289ddc97cacSRoman Zippel load:
290c1a0f5e3SRoman Zippel 	conf_filename = name;
291c1a0f5e3SRoman Zippel 	conf_lineno = 0;
292c1a0f5e3SRoman Zippel 	conf_warnings = 0;
293c1a0f5e3SRoman Zippel 	conf_unsaved = 0;
294c1a0f5e3SRoman Zippel 
295669bfad9SRoman Zippel 	def_flags = SYMBOL_DEF << def;
2961da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
297669bfad9SRoman Zippel 		sym->flags |= SYMBOL_CHANGED;
298669bfad9SRoman Zippel 		sym->flags &= ~(def_flags|SYMBOL_VALID);
299490f1617SYann E. MORIN 		if (sym_is_choice(sym))
300490f1617SYann E. MORIN 			sym->flags |= def_flags;
3011da177e4SLinus Torvalds 		switch (sym->type) {
3021da177e4SLinus Torvalds 		case S_INT:
3031da177e4SLinus Torvalds 		case S_HEX:
3041da177e4SLinus Torvalds 		case S_STRING:
305669bfad9SRoman Zippel 			if (sym->def[def].val)
306669bfad9SRoman Zippel 				free(sym->def[def].val);
307d8fc3200SArnaud Lacombe 			/* fall through */
3081da177e4SLinus Torvalds 		default:
309669bfad9SRoman Zippel 			sym->def[def].val = NULL;
310669bfad9SRoman Zippel 			sym->def[def].tri = no;
3111da177e4SLinus Torvalds 		}
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 
3141a7a8c6fSCody Schafer 	while (compat_getline(&line, &line_asize, in) != -1) {
315c1a0f5e3SRoman Zippel 		conf_lineno++;
3161da177e4SLinus Torvalds 		sym = NULL;
3178baefd30SArnaud Lacombe 		if (line[0] == '#') {
318ffb5957bSArnaud Lacombe 			if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
3191da177e4SLinus Torvalds 				continue;
320ffb5957bSArnaud Lacombe 			p = strchr(line + 2 + strlen(CONFIG_), ' ');
3211da177e4SLinus Torvalds 			if (!p)
3221da177e4SLinus Torvalds 				continue;
3231da177e4SLinus Torvalds 			*p++ = 0;
3241da177e4SLinus Torvalds 			if (strncmp(p, "is not set", 10))
3251da177e4SLinus Torvalds 				continue;
326669bfad9SRoman Zippel 			if (def == S_DEF_USER) {
327ffb5957bSArnaud Lacombe 				sym = sym_find(line + 2 + strlen(CONFIG_));
328661b0680Szippel@linux-m68k.org 				if (!sym) {
329661b0680Szippel@linux-m68k.org 					sym_add_change_count(1);
3308bea7548SNaohiro Aota 					goto setsym;
331661b0680Szippel@linux-m68k.org 				}
332669bfad9SRoman Zippel 			} else {
333ffb5957bSArnaud Lacombe 				sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
334669bfad9SRoman Zippel 				if (sym->type == S_UNKNOWN)
335669bfad9SRoman Zippel 					sym->type = S_BOOLEAN;
336669bfad9SRoman Zippel 			}
337669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
338d84876f9SJan Engelhardt 				conf_warning("override: reassigning to symbol %s", sym->name);
3391da177e4SLinus Torvalds 			}
3401da177e4SLinus Torvalds 			switch (sym->type) {
3411da177e4SLinus Torvalds 			case S_BOOLEAN:
3421da177e4SLinus Torvalds 			case S_TRISTATE:
343669bfad9SRoman Zippel 				sym->def[def].tri = no;
344669bfad9SRoman Zippel 				sym->flags |= def_flags;
3451da177e4SLinus Torvalds 				break;
3461da177e4SLinus Torvalds 			default:
3471da177e4SLinus Torvalds 				;
3481da177e4SLinus Torvalds 			}
349ffb5957bSArnaud Lacombe 		} else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
350ffb5957bSArnaud Lacombe 			p = strchr(line + strlen(CONFIG_), '=');
3511da177e4SLinus Torvalds 			if (!p)
3521da177e4SLinus Torvalds 				continue;
3531da177e4SLinus Torvalds 			*p++ = 0;
3541da177e4SLinus Torvalds 			p2 = strchr(p, '\n');
355d3660a8cSMatthew Wilcox 			if (p2) {
356d3660a8cSMatthew Wilcox 				*p2-- = 0;
357d3660a8cSMatthew Wilcox 				if (*p2 == '\r')
3581da177e4SLinus Torvalds 					*p2 = 0;
359d3660a8cSMatthew Wilcox 			}
360669bfad9SRoman Zippel 			if (def == S_DEF_USER) {
361ffb5957bSArnaud Lacombe 				sym = sym_find(line + strlen(CONFIG_));
362661b0680Szippel@linux-m68k.org 				if (!sym) {
363661b0680Szippel@linux-m68k.org 					sym_add_change_count(1);
3648bea7548SNaohiro Aota 					goto setsym;
365661b0680Szippel@linux-m68k.org 				}
366669bfad9SRoman Zippel 			} else {
367ffb5957bSArnaud Lacombe 				sym = sym_lookup(line + strlen(CONFIG_), 0);
368669bfad9SRoman Zippel 				if (sym->type == S_UNKNOWN)
369669bfad9SRoman Zippel 					sym->type = S_OTHER;
370669bfad9SRoman Zippel 			}
371669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
372d84876f9SJan Engelhardt 				conf_warning("override: reassigning to symbol %s", sym->name);
3731da177e4SLinus Torvalds 			}
3749c900a9cSSam Ravnborg 			if (conf_set_sym_val(sym, def, def_flags, p))
375c1a0f5e3SRoman Zippel 				continue;
3768baefd30SArnaud Lacombe 		} else {
3778baefd30SArnaud Lacombe 			if (line[0] != '\r' && line[0] != '\n')
378a4663911SPaul Bolle 				conf_warning("unexpected data: %.*s",
379a4663911SPaul Bolle 					     (int)strcspn(line, "\r\n"), line);
380a4663911SPaul Bolle 
3811da177e4SLinus Torvalds 			continue;
3821da177e4SLinus Torvalds 		}
3838bea7548SNaohiro Aota setsym:
3841da177e4SLinus Torvalds 		if (sym && sym_is_choice_value(sym)) {
3851da177e4SLinus Torvalds 			struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
386669bfad9SRoman Zippel 			switch (sym->def[def].tri) {
3871da177e4SLinus Torvalds 			case no:
3881da177e4SLinus Torvalds 				break;
3891da177e4SLinus Torvalds 			case mod:
390669bfad9SRoman Zippel 				if (cs->def[def].tri == yes) {
391c1a0f5e3SRoman Zippel 					conf_warning("%s creates inconsistent choice state", sym->name);
392490f1617SYann E. MORIN 					cs->flags &= ~def_flags;
393c1a0f5e3SRoman Zippel 				}
3941da177e4SLinus Torvalds 				break;
3951da177e4SLinus Torvalds 			case yes:
396d84876f9SJan Engelhardt 				if (cs->def[def].tri != no)
397d84876f9SJan Engelhardt 					conf_warning("override: %s changes choice state", sym->name);
398669bfad9SRoman Zippel 				cs->def[def].val = sym;
3991da177e4SLinus Torvalds 				break;
4001da177e4SLinus Torvalds 			}
401d6ee3576SSam Ravnborg 			cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
4021da177e4SLinus Torvalds 		}
4031da177e4SLinus Torvalds 	}
4041a7a8c6fSCody Schafer 	free(line);
4051da177e4SLinus Torvalds 	fclose(in);
40690389160SRoman Zippel 	return 0;
40790389160SRoman Zippel }
40890389160SRoman Zippel 
40990389160SRoman Zippel int conf_read(const char *name)
41090389160SRoman Zippel {
4115d09598dSArnaud Lacombe 	struct symbol *sym;
4125d09598dSArnaud Lacombe 	int i;
41390389160SRoman Zippel 
414bfc10001SKarsten Wiese 	sym_set_change_count(0);
415ddc97cacSRoman Zippel 
4166b87b70cSAl Viro 	if (conf_read_simple(name, S_DEF_USER)) {
4176b87b70cSAl Viro 		sym_calc_value(modules_sym);
41890389160SRoman Zippel 		return 1;
4196b87b70cSAl Viro 	}
4206b87b70cSAl Viro 
4216b87b70cSAl Viro 	sym_calc_value(modules_sym);
42290389160SRoman Zippel 
4231da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
4241da177e4SLinus Torvalds 		sym_calc_value(sym);
425c1a0f5e3SRoman Zippel 		if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
4265d09598dSArnaud Lacombe 			continue;
427c1a0f5e3SRoman Zippel 		if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
428c1a0f5e3SRoman Zippel 			/* check that calculated value agrees with saved value */
429c1a0f5e3SRoman Zippel 			switch (sym->type) {
430c1a0f5e3SRoman Zippel 			case S_BOOLEAN:
431c1a0f5e3SRoman Zippel 			case S_TRISTATE:
4320c1822e6SRoman Zippel 				if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
433c1a0f5e3SRoman Zippel 					break;
434c1a0f5e3SRoman Zippel 				if (!sym_is_choice(sym))
4355d09598dSArnaud Lacombe 					continue;
436d8fc3200SArnaud Lacombe 				/* fall through */
437c1a0f5e3SRoman Zippel 			default:
4380c1822e6SRoman Zippel 				if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
4395d09598dSArnaud Lacombe 					continue;
440c1a0f5e3SRoman Zippel 				break;
441c1a0f5e3SRoman Zippel 			}
442c1a0f5e3SRoman Zippel 		} else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
443c1a0f5e3SRoman Zippel 			/* no previous value and not saved */
4445d09598dSArnaud Lacombe 			continue;
445c1a0f5e3SRoman Zippel 		conf_unsaved++;
446c1a0f5e3SRoman Zippel 		/* maybe print value in verbose mode... */
4471da177e4SLinus Torvalds 	}
4481da177e4SLinus Torvalds 
449d8982ba1SRoman Zippel 	for_all_symbols(i, sym) {
450d8982ba1SRoman Zippel 		if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
451d8982ba1SRoman Zippel 			/* Reset values of generates values, so they'll appear
452d8982ba1SRoman Zippel 			 * as new, if they should become visible, but that
453d8982ba1SRoman Zippel 			 * doesn't quite work if the Kconfig and the saved
454d8982ba1SRoman Zippel 			 * configuration disagree.
455d8982ba1SRoman Zippel 			 */
456d8982ba1SRoman Zippel 			if (sym->visible == no && !conf_unsaved)
457d8982ba1SRoman Zippel 				sym->flags &= ~SYMBOL_DEF_USER;
458d8982ba1SRoman Zippel 			switch (sym->type) {
459d8982ba1SRoman Zippel 			case S_STRING:
460d8982ba1SRoman Zippel 			case S_INT:
461d8982ba1SRoman Zippel 			case S_HEX:
462d8982ba1SRoman Zippel 				/* Reset a string value if it's out of range */
463d8982ba1SRoman Zippel 				if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
464d8982ba1SRoman Zippel 					break;
465d8982ba1SRoman Zippel 				sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
466d8982ba1SRoman Zippel 				conf_unsaved++;
467d8982ba1SRoman Zippel 				break;
468d8982ba1SRoman Zippel 			default:
469d8982ba1SRoman Zippel 				break;
470d8982ba1SRoman Zippel 			}
471d8982ba1SRoman Zippel 		}
472d8982ba1SRoman Zippel 	}
473d8982ba1SRoman Zippel 
474bfc10001SKarsten Wiese 	sym_add_change_count(conf_warnings || conf_unsaved);
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	return 0;
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
479e54e692bSArnaud Lacombe /*
480e54e692bSArnaud Lacombe  * Kconfig configuration printer
481e54e692bSArnaud Lacombe  *
482e54e692bSArnaud Lacombe  * This printer is used when generating the resulting configuration after
483e54e692bSArnaud Lacombe  * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
484e54e692bSArnaud Lacombe  * passing a non-NULL argument to the printer.
485e54e692bSArnaud Lacombe  *
486e54e692bSArnaud Lacombe  */
487e54e692bSArnaud Lacombe static void
488e54e692bSArnaud Lacombe kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
48949192f26SSam Ravnborg {
49049192f26SSam Ravnborg 
4910dce6310SArnaud Lacombe 	switch (sym->type) {
49249192f26SSam Ravnborg 	case S_BOOLEAN:
49349192f26SSam Ravnborg 	case S_TRISTATE:
494e54e692bSArnaud Lacombe 		if (*value == 'n') {
495e54e692bSArnaud Lacombe 			bool skip_unset = (arg != NULL);
496e54e692bSArnaud Lacombe 
497e54e692bSArnaud Lacombe 			if (!skip_unset)
498e54e692bSArnaud Lacombe 				fprintf(fp, "# %s%s is not set\n",
499ffb5957bSArnaud Lacombe 				    CONFIG_, sym->name);
500e54e692bSArnaud Lacombe 			return;
50149192f26SSam Ravnborg 		}
50249192f26SSam Ravnborg 		break;
503e54e692bSArnaud Lacombe 	default:
50449192f26SSam Ravnborg 		break;
505e54e692bSArnaud Lacombe 	}
506e54e692bSArnaud Lacombe 
507e54e692bSArnaud Lacombe 	fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
508e54e692bSArnaud Lacombe }
509e54e692bSArnaud Lacombe 
510e54e692bSArnaud Lacombe static void
511e54e692bSArnaud Lacombe kconfig_print_comment(FILE *fp, const char *value, void *arg)
512e54e692bSArnaud Lacombe {
513e54e692bSArnaud Lacombe 	const char *p = value;
514e54e692bSArnaud Lacombe 	size_t l;
515e54e692bSArnaud Lacombe 
516e54e692bSArnaud Lacombe 	for (;;) {
517e54e692bSArnaud Lacombe 		l = strcspn(p, "\n");
518e54e692bSArnaud Lacombe 		fprintf(fp, "#");
519e54e692bSArnaud Lacombe 		if (l) {
520e54e692bSArnaud Lacombe 			fprintf(fp, " ");
52170cc01e7SPeter Foley 			xfwrite(p, l, 1, fp);
522e54e692bSArnaud Lacombe 			p += l;
523e54e692bSArnaud Lacombe 		}
524e54e692bSArnaud Lacombe 		fprintf(fp, "\n");
525e54e692bSArnaud Lacombe 		if (*p++ == '\0')
52649192f26SSam Ravnborg 			break;
527e54e692bSArnaud Lacombe 	}
528e54e692bSArnaud Lacombe }
529e54e692bSArnaud Lacombe 
530e54e692bSArnaud Lacombe static struct conf_printer kconfig_printer_cb =
531e54e692bSArnaud Lacombe {
532e54e692bSArnaud Lacombe 	.print_symbol = kconfig_print_symbol,
533e54e692bSArnaud Lacombe 	.print_comment = kconfig_print_comment,
534e54e692bSArnaud Lacombe };
535e54e692bSArnaud Lacombe 
536e54e692bSArnaud Lacombe /*
537e54e692bSArnaud Lacombe  * Header printer
538e54e692bSArnaud Lacombe  *
539e54e692bSArnaud Lacombe  * This printer is used when generating the `include/generated/autoconf.h' file.
540e54e692bSArnaud Lacombe  */
541e54e692bSArnaud Lacombe static void
542e54e692bSArnaud Lacombe header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
543e54e692bSArnaud Lacombe {
544e54e692bSArnaud Lacombe 
545e54e692bSArnaud Lacombe 	switch (sym->type) {
546e54e692bSArnaud Lacombe 	case S_BOOLEAN:
547eb4cf5a6SArnaud Lacombe 	case S_TRISTATE: {
548eb4cf5a6SArnaud Lacombe 		const char *suffix = "";
549eb4cf5a6SArnaud Lacombe 
550e54e692bSArnaud Lacombe 		switch (*value) {
551e54e692bSArnaud Lacombe 		case 'n':
5522a11c8eaSMichal Marek 			break;
553e54e692bSArnaud Lacombe 		case 'm':
554e54e692bSArnaud Lacombe 			suffix = "_MODULE";
555eb4cf5a6SArnaud Lacombe 			/* fall through */
556e54e692bSArnaud Lacombe 		default:
5572a11c8eaSMichal Marek 			fprintf(fp, "#define %s%s%s 1\n",
5582a11c8eaSMichal Marek 			    CONFIG_, sym->name, suffix);
559e54e692bSArnaud Lacombe 		}
560eb4cf5a6SArnaud Lacombe 		break;
561eb4cf5a6SArnaud Lacombe 	}
562eb4cf5a6SArnaud Lacombe 	case S_HEX: {
563eb4cf5a6SArnaud Lacombe 		const char *prefix = "";
564eb4cf5a6SArnaud Lacombe 
565eb4cf5a6SArnaud Lacombe 		if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
566eb4cf5a6SArnaud Lacombe 			prefix = "0x";
567eb4cf5a6SArnaud Lacombe 		fprintf(fp, "#define %s%s %s%s\n",
568eb4cf5a6SArnaud Lacombe 		    CONFIG_, sym->name, prefix, value);
569eb4cf5a6SArnaud Lacombe 		break;
570eb4cf5a6SArnaud Lacombe 	}
571eb4cf5a6SArnaud Lacombe 	case S_STRING:
572eb4cf5a6SArnaud Lacombe 	case S_INT:
573eb4cf5a6SArnaud Lacombe 		fprintf(fp, "#define %s%s %s\n",
574eb4cf5a6SArnaud Lacombe 		    CONFIG_, sym->name, value);
575e54e692bSArnaud Lacombe 		break;
576e54e692bSArnaud Lacombe 	default:
577e54e692bSArnaud Lacombe 		break;
578e54e692bSArnaud Lacombe 	}
579e54e692bSArnaud Lacombe 
580e54e692bSArnaud Lacombe }
581e54e692bSArnaud Lacombe 
582e54e692bSArnaud Lacombe static void
583e54e692bSArnaud Lacombe header_print_comment(FILE *fp, const char *value, void *arg)
584e54e692bSArnaud Lacombe {
585e54e692bSArnaud Lacombe 	const char *p = value;
586e54e692bSArnaud Lacombe 	size_t l;
587e54e692bSArnaud Lacombe 
588e54e692bSArnaud Lacombe 	fprintf(fp, "/*\n");
589e54e692bSArnaud Lacombe 	for (;;) {
590e54e692bSArnaud Lacombe 		l = strcspn(p, "\n");
591e54e692bSArnaud Lacombe 		fprintf(fp, " *");
592e54e692bSArnaud Lacombe 		if (l) {
593e54e692bSArnaud Lacombe 			fprintf(fp, " ");
59470cc01e7SPeter Foley 			xfwrite(p, l, 1, fp);
595e54e692bSArnaud Lacombe 			p += l;
596e54e692bSArnaud Lacombe 		}
597e54e692bSArnaud Lacombe 		fprintf(fp, "\n");
598e54e692bSArnaud Lacombe 		if (*p++ == '\0')
599e54e692bSArnaud Lacombe 			break;
600e54e692bSArnaud Lacombe 	}
601e54e692bSArnaud Lacombe 	fprintf(fp, " */\n");
602e54e692bSArnaud Lacombe }
603e54e692bSArnaud Lacombe 
604e54e692bSArnaud Lacombe static struct conf_printer header_printer_cb =
605e54e692bSArnaud Lacombe {
606e54e692bSArnaud Lacombe 	.print_symbol = header_print_symbol,
607e54e692bSArnaud Lacombe 	.print_comment = header_print_comment,
608e54e692bSArnaud Lacombe };
609e54e692bSArnaud Lacombe 
610e54e692bSArnaud Lacombe /*
611e54e692bSArnaud Lacombe  * Tristate printer
612e54e692bSArnaud Lacombe  *
613e54e692bSArnaud Lacombe  * This printer is used when generating the `include/config/tristate.conf' file.
614e54e692bSArnaud Lacombe  */
615e54e692bSArnaud Lacombe static void
616e54e692bSArnaud Lacombe tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
617e54e692bSArnaud Lacombe {
618e54e692bSArnaud Lacombe 
619e54e692bSArnaud Lacombe 	if (sym->type == S_TRISTATE && *value != 'n')
620e54e692bSArnaud Lacombe 		fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
621e54e692bSArnaud Lacombe }
622e54e692bSArnaud Lacombe 
623e54e692bSArnaud Lacombe static struct conf_printer tristate_printer_cb =
624e54e692bSArnaud Lacombe {
625e54e692bSArnaud Lacombe 	.print_symbol = tristate_print_symbol,
626e54e692bSArnaud Lacombe 	.print_comment = kconfig_print_comment,
627e54e692bSArnaud Lacombe };
628e54e692bSArnaud Lacombe 
629e54e692bSArnaud Lacombe static void conf_write_symbol(FILE *fp, struct symbol *sym,
630e54e692bSArnaud Lacombe 			      struct conf_printer *printer, void *printer_arg)
631e54e692bSArnaud Lacombe {
632e54e692bSArnaud Lacombe 	const char *str;
633e54e692bSArnaud Lacombe 
634e54e692bSArnaud Lacombe 	switch (sym->type) {
63549192f26SSam Ravnborg 	case S_OTHER:
63649192f26SSam Ravnborg 	case S_UNKNOWN:
63749192f26SSam Ravnborg 		break;
638e54e692bSArnaud Lacombe 	case S_STRING:
639e54e692bSArnaud Lacombe 		str = sym_get_string_value(sym);
640e54e692bSArnaud Lacombe 		str = sym_escape_string_value(str);
641e54e692bSArnaud Lacombe 		printer->print_symbol(fp, sym, str, printer_arg);
642e54e692bSArnaud Lacombe 		free((void *)str);
643e54e692bSArnaud Lacombe 		break;
644e54e692bSArnaud Lacombe 	default:
645e54e692bSArnaud Lacombe 		str = sym_get_string_value(sym);
646e54e692bSArnaud Lacombe 		printer->print_symbol(fp, sym, str, printer_arg);
64749192f26SSam Ravnborg 	}
64849192f26SSam Ravnborg }
64949192f26SSam Ravnborg 
650e54e692bSArnaud Lacombe static void
651e54e692bSArnaud Lacombe conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
652e54e692bSArnaud Lacombe {
653e54e692bSArnaud Lacombe 	char buf[256];
654e54e692bSArnaud Lacombe 
655e54e692bSArnaud Lacombe 	snprintf(buf, sizeof(buf),
656e54e692bSArnaud Lacombe 	    "\n"
657e54e692bSArnaud Lacombe 	    "Automatically generated file; DO NOT EDIT.\n"
658e54e692bSArnaud Lacombe 	    "%s\n",
659e54e692bSArnaud Lacombe 	    rootmenu.prompt->text);
660e54e692bSArnaud Lacombe 
661e54e692bSArnaud Lacombe 	printer->print_comment(fp, buf, printer_arg);
662e54e692bSArnaud Lacombe }
663e54e692bSArnaud Lacombe 
6647cf3d73bSSam Ravnborg /*
6657cf3d73bSSam Ravnborg  * Write out a minimal config.
6667cf3d73bSSam Ravnborg  * All values that has default values are skipped as this is redundant.
6677cf3d73bSSam Ravnborg  */
6687cf3d73bSSam Ravnborg int conf_write_defconfig(const char *filename)
6697cf3d73bSSam Ravnborg {
6707cf3d73bSSam Ravnborg 	struct symbol *sym;
6717cf3d73bSSam Ravnborg 	struct menu *menu;
6727cf3d73bSSam Ravnborg 	FILE *out;
6737cf3d73bSSam Ravnborg 
6747cf3d73bSSam Ravnborg 	out = fopen(filename, "w");
6757cf3d73bSSam Ravnborg 	if (!out)
6767cf3d73bSSam Ravnborg 		return 1;
6777cf3d73bSSam Ravnborg 
6787cf3d73bSSam Ravnborg 	sym_clear_all_valid();
6797cf3d73bSSam Ravnborg 
6807cf3d73bSSam Ravnborg 	/* Traverse all menus to find all relevant symbols */
6817cf3d73bSSam Ravnborg 	menu = rootmenu.list;
6827cf3d73bSSam Ravnborg 
6837cf3d73bSSam Ravnborg 	while (menu != NULL)
6847cf3d73bSSam Ravnborg 	{
6857cf3d73bSSam Ravnborg 		sym = menu->sym;
6867cf3d73bSSam Ravnborg 		if (sym == NULL) {
6877cf3d73bSSam Ravnborg 			if (!menu_is_visible(menu))
6887cf3d73bSSam Ravnborg 				goto next_menu;
6897cf3d73bSSam Ravnborg 		} else if (!sym_is_choice(sym)) {
6907cf3d73bSSam Ravnborg 			sym_calc_value(sym);
6917cf3d73bSSam Ravnborg 			if (!(sym->flags & SYMBOL_WRITE))
6927cf3d73bSSam Ravnborg 				goto next_menu;
6937cf3d73bSSam Ravnborg 			sym->flags &= ~SYMBOL_WRITE;
6947cf3d73bSSam Ravnborg 			/* If we cannot change the symbol - skip */
6957cf3d73bSSam Ravnborg 			if (!sym_is_changable(sym))
6967cf3d73bSSam Ravnborg 				goto next_menu;
6977cf3d73bSSam Ravnborg 			/* If symbol equals to default value - skip */
6987cf3d73bSSam Ravnborg 			if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
6997cf3d73bSSam Ravnborg 				goto next_menu;
7007cf3d73bSSam Ravnborg 
7017cf3d73bSSam Ravnborg 			/*
7027cf3d73bSSam Ravnborg 			 * If symbol is a choice value and equals to the
7037cf3d73bSSam Ravnborg 			 * default for a choice - skip.
70484062dd3SSam Ravnborg 			 * But only if value is bool and equal to "y" and
70584062dd3SSam Ravnborg 			 * choice is not "optional".
70684062dd3SSam Ravnborg 			 * (If choice is "optional" then all values can be "n")
7077cf3d73bSSam Ravnborg 			 */
7087cf3d73bSSam Ravnborg 			if (sym_is_choice_value(sym)) {
7097cf3d73bSSam Ravnborg 				struct symbol *cs;
7107cf3d73bSSam Ravnborg 				struct symbol *ds;
7117cf3d73bSSam Ravnborg 
7127cf3d73bSSam Ravnborg 				cs = prop_get_symbol(sym_get_choice_prop(sym));
7137cf3d73bSSam Ravnborg 				ds = sym_choice_default(cs);
71484062dd3SSam Ravnborg 				if (!sym_is_optional(cs) && sym == ds) {
715801690caSSam Ravnborg 					if ((sym->type == S_BOOLEAN) &&
7167cf3d73bSSam Ravnborg 					    sym_get_tristate_value(sym) == yes)
7177cf3d73bSSam Ravnborg 						goto next_menu;
7187cf3d73bSSam Ravnborg 				}
7197cf3d73bSSam Ravnborg 			}
720e54e692bSArnaud Lacombe 			conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
7217cf3d73bSSam Ravnborg 		}
7227cf3d73bSSam Ravnborg next_menu:
7237cf3d73bSSam Ravnborg 		if (menu->list != NULL) {
7247cf3d73bSSam Ravnborg 			menu = menu->list;
7257cf3d73bSSam Ravnborg 		}
7267cf3d73bSSam Ravnborg 		else if (menu->next != NULL) {
7277cf3d73bSSam Ravnborg 			menu = menu->next;
7287cf3d73bSSam Ravnborg 		} else {
7297cf3d73bSSam Ravnborg 			while ((menu = menu->parent)) {
7307cf3d73bSSam Ravnborg 				if (menu->next != NULL) {
7317cf3d73bSSam Ravnborg 					menu = menu->next;
7327cf3d73bSSam Ravnborg 					break;
7337cf3d73bSSam Ravnborg 				}
7347cf3d73bSSam Ravnborg 			}
7357cf3d73bSSam Ravnborg 		}
7367cf3d73bSSam Ravnborg 	}
7377cf3d73bSSam Ravnborg 	fclose(out);
7387cf3d73bSSam Ravnborg 	return 0;
7397cf3d73bSSam Ravnborg }
7407cf3d73bSSam Ravnborg 
7411da177e4SLinus Torvalds int conf_write(const char *name)
7421da177e4SLinus Torvalds {
743c955ccafSRoman Zippel 	FILE *out;
7441da177e4SLinus Torvalds 	struct symbol *sym;
7451da177e4SLinus Torvalds 	struct menu *menu;
7461da177e4SLinus Torvalds 	const char *basename;
7471da177e4SLinus Torvalds 	const char *str;
7481408b15bSWill Newton 	char dirname[PATH_MAX+1], tmpname[PATH_MAX+1], newname[PATH_MAX+1];
7491da177e4SLinus Torvalds 	char *env;
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	dirname[0] = 0;
7521da177e4SLinus Torvalds 	if (name && name[0]) {
7531da177e4SLinus Torvalds 		struct stat st;
7541da177e4SLinus Torvalds 		char *slash;
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 		if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
7571da177e4SLinus Torvalds 			strcpy(dirname, name);
7581da177e4SLinus Torvalds 			strcat(dirname, "/");
75914cdd3c4SRoman Zippel 			basename = conf_get_configname();
7601da177e4SLinus Torvalds 		} else if ((slash = strrchr(name, '/'))) {
7611da177e4SLinus Torvalds 			int size = slash - name + 1;
7621da177e4SLinus Torvalds 			memcpy(dirname, name, size);
7631da177e4SLinus Torvalds 			dirname[size] = 0;
7641da177e4SLinus Torvalds 			if (slash[1])
7651da177e4SLinus Torvalds 				basename = slash + 1;
7661da177e4SLinus Torvalds 			else
76714cdd3c4SRoman Zippel 				basename = conf_get_configname();
7681da177e4SLinus Torvalds 		} else
7691da177e4SLinus Torvalds 			basename = name;
7701da177e4SLinus Torvalds 	} else
77114cdd3c4SRoman Zippel 		basename = conf_get_configname();
7721da177e4SLinus Torvalds 
77314cdd3c4SRoman Zippel 	sprintf(newname, "%s%s", dirname, basename);
77414cdd3c4SRoman Zippel 	env = getenv("KCONFIG_OVERWRITECONFIG");
77514cdd3c4SRoman Zippel 	if (!env || !*env) {
77614cdd3c4SRoman Zippel 		sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
77714cdd3c4SRoman Zippel 		out = fopen(tmpname, "w");
77814cdd3c4SRoman Zippel 	} else {
77914cdd3c4SRoman Zippel 		*tmpname = 0;
7801da177e4SLinus Torvalds 		out = fopen(newname, "w");
78114cdd3c4SRoman Zippel 	}
7821da177e4SLinus Torvalds 	if (!out)
7831da177e4SLinus Torvalds 		return 1;
78414cdd3c4SRoman Zippel 
785e54e692bSArnaud Lacombe 	conf_write_heading(out, &kconfig_printer_cb, NULL);
7861da177e4SLinus Torvalds 
787b3214293SKarsten Wiese 	if (!conf_get_changed())
7881da177e4SLinus Torvalds 		sym_clear_all_valid();
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	menu = rootmenu.list;
7911da177e4SLinus Torvalds 	while (menu) {
7921da177e4SLinus Torvalds 		sym = menu->sym;
7931da177e4SLinus Torvalds 		if (!sym) {
7941da177e4SLinus Torvalds 			if (!menu_is_visible(menu))
7951da177e4SLinus Torvalds 				goto next;
7961da177e4SLinus Torvalds 			str = menu_get_prompt(menu);
7971da177e4SLinus Torvalds 			fprintf(out, "\n"
7981da177e4SLinus Torvalds 				     "#\n"
7991da177e4SLinus Torvalds 				     "# %s\n"
8001da177e4SLinus Torvalds 				     "#\n", str);
8011da177e4SLinus Torvalds 		} else if (!(sym->flags & SYMBOL_CHOICE)) {
8021da177e4SLinus Torvalds 			sym_calc_value(sym);
8031da177e4SLinus Torvalds 			if (!(sym->flags & SYMBOL_WRITE))
8041da177e4SLinus Torvalds 				goto next;
8051da177e4SLinus Torvalds 			sym->flags &= ~SYMBOL_WRITE;
806e54e692bSArnaud Lacombe 
807e54e692bSArnaud Lacombe 			conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
8081da177e4SLinus Torvalds 		}
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds next:
8111da177e4SLinus Torvalds 		if (menu->list) {
8121da177e4SLinus Torvalds 			menu = menu->list;
8131da177e4SLinus Torvalds 			continue;
8141da177e4SLinus Torvalds 		}
8151da177e4SLinus Torvalds 		if (menu->next)
8161da177e4SLinus Torvalds 			menu = menu->next;
8171da177e4SLinus Torvalds 		else while ((menu = menu->parent)) {
8181da177e4SLinus Torvalds 			if (menu->next) {
8191da177e4SLinus Torvalds 				menu = menu->next;
8201da177e4SLinus Torvalds 				break;
8211da177e4SLinus Torvalds 			}
8221da177e4SLinus Torvalds 		}
8231da177e4SLinus Torvalds 	}
8241da177e4SLinus Torvalds 	fclose(out);
82514cdd3c4SRoman Zippel 
82614cdd3c4SRoman Zippel 	if (*tmpname) {
8279a3d0fe8SSam Ravnborg 		strcat(dirname, basename);
82814cdd3c4SRoman Zippel 		strcat(dirname, ".old");
82914cdd3c4SRoman Zippel 		rename(newname, dirname);
83014cdd3c4SRoman Zippel 		if (rename(tmpname, newname))
8311da177e4SLinus Torvalds 			return 1;
83214cdd3c4SRoman Zippel 	}
8331da177e4SLinus Torvalds 
83442368c37SMichal Marek 	conf_message(_("configuration written to %s"), newname);
835ddc97cacSRoman Zippel 
836bfc10001SKarsten Wiese 	sym_set_change_count(0);
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	return 0;
8391da177e4SLinus Torvalds }
840c955ccafSRoman Zippel 
8414356f489STrevor Keith static int conf_split_config(void)
8422e3646e5SRoman Zippel {
84312122f62SMarkus Heidelberg 	const char *name;
8441408b15bSWill Newton 	char path[PATH_MAX+1];
8452e3646e5SRoman Zippel 	char *s, *d, c;
8462e3646e5SRoman Zippel 	struct symbol *sym;
8472e3646e5SRoman Zippel 	struct stat sb;
8482e3646e5SRoman Zippel 	int res, i, fd;
8492e3646e5SRoman Zippel 
85012122f62SMarkus Heidelberg 	name = conf_get_autoconfig_name();
8512e3646e5SRoman Zippel 	conf_read_simple(name, S_DEF_AUTO);
8526b87b70cSAl Viro 	sym_calc_value(modules_sym);
8532e3646e5SRoman Zippel 
8542e3646e5SRoman Zippel 	if (chdir("include/config"))
8552e3646e5SRoman Zippel 		return 1;
8562e3646e5SRoman Zippel 
8572e3646e5SRoman Zippel 	res = 0;
8582e3646e5SRoman Zippel 	for_all_symbols(i, sym) {
8592e3646e5SRoman Zippel 		sym_calc_value(sym);
8602e3646e5SRoman Zippel 		if ((sym->flags & SYMBOL_AUTO) || !sym->name)
8612e3646e5SRoman Zippel 			continue;
8622e3646e5SRoman Zippel 		if (sym->flags & SYMBOL_WRITE) {
8632e3646e5SRoman Zippel 			if (sym->flags & SYMBOL_DEF_AUTO) {
8642e3646e5SRoman Zippel 				/*
8652e3646e5SRoman Zippel 				 * symbol has old and new value,
8662e3646e5SRoman Zippel 				 * so compare them...
8672e3646e5SRoman Zippel 				 */
8682e3646e5SRoman Zippel 				switch (sym->type) {
8692e3646e5SRoman Zippel 				case S_BOOLEAN:
8702e3646e5SRoman Zippel 				case S_TRISTATE:
8712e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) ==
8722e3646e5SRoman Zippel 					    sym->def[S_DEF_AUTO].tri)
8732e3646e5SRoman Zippel 						continue;
8742e3646e5SRoman Zippel 					break;
8752e3646e5SRoman Zippel 				case S_STRING:
8762e3646e5SRoman Zippel 				case S_HEX:
8772e3646e5SRoman Zippel 				case S_INT:
8782e3646e5SRoman Zippel 					if (!strcmp(sym_get_string_value(sym),
8792e3646e5SRoman Zippel 						    sym->def[S_DEF_AUTO].val))
8802e3646e5SRoman Zippel 						continue;
8812e3646e5SRoman Zippel 					break;
8822e3646e5SRoman Zippel 				default:
8832e3646e5SRoman Zippel 					break;
8842e3646e5SRoman Zippel 				}
8852e3646e5SRoman Zippel 			} else {
8862e3646e5SRoman Zippel 				/*
8872e3646e5SRoman Zippel 				 * If there is no old value, only 'no' (unset)
8882e3646e5SRoman Zippel 				 * is allowed as new value.
8892e3646e5SRoman Zippel 				 */
8902e3646e5SRoman Zippel 				switch (sym->type) {
8912e3646e5SRoman Zippel 				case S_BOOLEAN:
8922e3646e5SRoman Zippel 				case S_TRISTATE:
8932e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) == no)
8942e3646e5SRoman Zippel 						continue;
8952e3646e5SRoman Zippel 					break;
8962e3646e5SRoman Zippel 				default:
8972e3646e5SRoman Zippel 					break;
8982e3646e5SRoman Zippel 				}
8992e3646e5SRoman Zippel 			}
9002e3646e5SRoman Zippel 		} else if (!(sym->flags & SYMBOL_DEF_AUTO))
9012e3646e5SRoman Zippel 			/* There is neither an old nor a new value. */
9022e3646e5SRoman Zippel 			continue;
9032e3646e5SRoman Zippel 		/* else
9042e3646e5SRoman Zippel 		 *	There is an old value, but no new value ('no' (unset)
9052e3646e5SRoman Zippel 		 *	isn't saved in auto.conf, so the old value is always
9062e3646e5SRoman Zippel 		 *	different from 'no').
9072e3646e5SRoman Zippel 		 */
9082e3646e5SRoman Zippel 
9092e3646e5SRoman Zippel 		/* Replace all '_' and append ".h" */
9102e3646e5SRoman Zippel 		s = sym->name;
9112e3646e5SRoman Zippel 		d = path;
9122e3646e5SRoman Zippel 		while ((c = *s++)) {
9132e3646e5SRoman Zippel 			c = tolower(c);
9142e3646e5SRoman Zippel 			*d++ = (c == '_') ? '/' : c;
9152e3646e5SRoman Zippel 		}
9162e3646e5SRoman Zippel 		strcpy(d, ".h");
9172e3646e5SRoman Zippel 
9182e3646e5SRoman Zippel 		/* Assume directory path already exists. */
9192e3646e5SRoman Zippel 		fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
9202e3646e5SRoman Zippel 		if (fd == -1) {
9212e3646e5SRoman Zippel 			if (errno != ENOENT) {
9222e3646e5SRoman Zippel 				res = 1;
9232e3646e5SRoman Zippel 				break;
9242e3646e5SRoman Zippel 			}
9252e3646e5SRoman Zippel 			/*
9262e3646e5SRoman Zippel 			 * Create directory components,
9272e3646e5SRoman Zippel 			 * unless they exist already.
9282e3646e5SRoman Zippel 			 */
9292e3646e5SRoman Zippel 			d = path;
9302e3646e5SRoman Zippel 			while ((d = strchr(d, '/'))) {
9312e3646e5SRoman Zippel 				*d = 0;
9322e3646e5SRoman Zippel 				if (stat(path, &sb) && mkdir(path, 0755)) {
9332e3646e5SRoman Zippel 					res = 1;
9342e3646e5SRoman Zippel 					goto out;
9352e3646e5SRoman Zippel 				}
9362e3646e5SRoman Zippel 				*d++ = '/';
9372e3646e5SRoman Zippel 			}
9382e3646e5SRoman Zippel 			/* Try it again. */
9392e3646e5SRoman Zippel 			fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
9402e3646e5SRoman Zippel 			if (fd == -1) {
9412e3646e5SRoman Zippel 				res = 1;
9422e3646e5SRoman Zippel 				break;
9432e3646e5SRoman Zippel 			}
9442e3646e5SRoman Zippel 		}
9452e3646e5SRoman Zippel 		close(fd);
9462e3646e5SRoman Zippel 	}
9472e3646e5SRoman Zippel out:
9482e3646e5SRoman Zippel 	if (chdir("../.."))
9492e3646e5SRoman Zippel 		return 1;
9502e3646e5SRoman Zippel 
9512e3646e5SRoman Zippel 	return res;
9522e3646e5SRoman Zippel }
9532e3646e5SRoman Zippel 
954c955ccafSRoman Zippel int conf_write_autoconf(void)
955c955ccafSRoman Zippel {
956c955ccafSRoman Zippel 	struct symbol *sym;
95712122f62SMarkus Heidelberg 	const char *name;
958bc081dd6SMichal Marek 	FILE *out, *tristate, *out_h;
95949192f26SSam Ravnborg 	int i;
960c955ccafSRoman Zippel 
9612e3646e5SRoman Zippel 	sym_clear_all_valid();
9622e3646e5SRoman Zippel 
963c955ccafSRoman Zippel 	file_write_dep("include/config/auto.conf.cmd");
964c955ccafSRoman Zippel 
9652e3646e5SRoman Zippel 	if (conf_split_config())
9662e3646e5SRoman Zippel 		return 1;
9672e3646e5SRoman Zippel 
968c955ccafSRoman Zippel 	out = fopen(".tmpconfig", "w");
969c955ccafSRoman Zippel 	if (!out)
970c955ccafSRoman Zippel 		return 1;
971c955ccafSRoman Zippel 
972bc081dd6SMichal Marek 	tristate = fopen(".tmpconfig_tristate", "w");
973bc081dd6SMichal Marek 	if (!tristate) {
974bc081dd6SMichal Marek 		fclose(out);
975bc081dd6SMichal Marek 		return 1;
976bc081dd6SMichal Marek 	}
977bc081dd6SMichal Marek 
978c955ccafSRoman Zippel 	out_h = fopen(".tmpconfig.h", "w");
979c955ccafSRoman Zippel 	if (!out_h) {
980c955ccafSRoman Zippel 		fclose(out);
981bc081dd6SMichal Marek 		fclose(tristate);
982c955ccafSRoman Zippel 		return 1;
983c955ccafSRoman Zippel 	}
984c955ccafSRoman Zippel 
985e54e692bSArnaud Lacombe 	conf_write_heading(out, &kconfig_printer_cb, NULL);
986e54e692bSArnaud Lacombe 
987e54e692bSArnaud Lacombe 	conf_write_heading(tristate, &tristate_printer_cb, NULL);
988e54e692bSArnaud Lacombe 
989e54e692bSArnaud Lacombe 	conf_write_heading(out_h, &header_printer_cb, NULL);
990c955ccafSRoman Zippel 
991c955ccafSRoman Zippel 	for_all_symbols(i, sym) {
992953742c8SArnaud Lacombe 		sym_calc_value(sym);
993a9596135SPaul Gortmaker 		if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
994953742c8SArnaud Lacombe 			continue;
995953742c8SArnaud Lacombe 
996a9596135SPaul Gortmaker 		/* write symbol to auto.conf, tristate and header files */
997e54e692bSArnaud Lacombe 		conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
99849192f26SSam Ravnborg 
999e54e692bSArnaud Lacombe 		conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
1000e54e692bSArnaud Lacombe 
1001e54e692bSArnaud Lacombe 		conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
1002c955ccafSRoman Zippel 	}
1003c955ccafSRoman Zippel 	fclose(out);
1004bc081dd6SMichal Marek 	fclose(tristate);
1005c955ccafSRoman Zippel 	fclose(out_h);
1006c955ccafSRoman Zippel 
1007c955ccafSRoman Zippel 	name = getenv("KCONFIG_AUTOHEADER");
1008c955ccafSRoman Zippel 	if (!name)
1009264a2683SSam Ravnborg 		name = "include/generated/autoconf.h";
1010c955ccafSRoman Zippel 	if (rename(".tmpconfig.h", name))
1011c955ccafSRoman Zippel 		return 1;
1012bc081dd6SMichal Marek 	name = getenv("KCONFIG_TRISTATE");
1013bc081dd6SMichal Marek 	if (!name)
1014bc081dd6SMichal Marek 		name = "include/config/tristate.conf";
1015bc081dd6SMichal Marek 	if (rename(".tmpconfig_tristate", name))
1016bc081dd6SMichal Marek 		return 1;
101712122f62SMarkus Heidelberg 	name = conf_get_autoconfig_name();
1018c955ccafSRoman Zippel 	/*
1019c955ccafSRoman Zippel 	 * This must be the last step, kbuild has a dependency on auto.conf
1020c955ccafSRoman Zippel 	 * and this marks the successful completion of the previous steps.
1021c955ccafSRoman Zippel 	 */
1022c955ccafSRoman Zippel 	if (rename(".tmpconfig", name))
1023c955ccafSRoman Zippel 		return 1;
1024c955ccafSRoman Zippel 
1025c955ccafSRoman Zippel 	return 0;
1026c955ccafSRoman Zippel }
1027b3214293SKarsten Wiese 
1028bfc10001SKarsten Wiese static int sym_change_count;
10293b354c55SKarsten Wiese static void (*conf_changed_callback)(void);
1030bfc10001SKarsten Wiese 
1031bfc10001SKarsten Wiese void sym_set_change_count(int count)
1032bfc10001SKarsten Wiese {
10333b354c55SKarsten Wiese 	int _sym_change_count = sym_change_count;
1034bfc10001SKarsten Wiese 	sym_change_count = count;
10353b354c55SKarsten Wiese 	if (conf_changed_callback &&
10363b354c55SKarsten Wiese 	    (bool)_sym_change_count != (bool)count)
10373b354c55SKarsten Wiese 		conf_changed_callback();
1038bfc10001SKarsten Wiese }
1039bfc10001SKarsten Wiese 
1040bfc10001SKarsten Wiese void sym_add_change_count(int count)
1041bfc10001SKarsten Wiese {
10423b354c55SKarsten Wiese 	sym_set_change_count(count + sym_change_count);
1043bfc10001SKarsten Wiese }
1044bfc10001SKarsten Wiese 
1045b3214293SKarsten Wiese bool conf_get_changed(void)
1046b3214293SKarsten Wiese {
1047b3214293SKarsten Wiese 	return sym_change_count;
1048b3214293SKarsten Wiese }
10493b354c55SKarsten Wiese 
10503b354c55SKarsten Wiese void conf_set_changed_callback(void (*fn)(void))
10513b354c55SKarsten Wiese {
10523b354c55SKarsten Wiese 	conf_changed_callback = fn;
10533b354c55SKarsten Wiese }
1054dc7862e5SRoman Zippel 
10553b9a19e0SYann E. MORIN static bool randomize_choice_values(struct symbol *csym)
1056a64b44eaSSam Ravnborg {
1057a64b44eaSSam Ravnborg 	struct property *prop;
1058a64b44eaSSam Ravnborg 	struct symbol *sym;
1059a64b44eaSSam Ravnborg 	struct expr *e;
1060a64b44eaSSam Ravnborg 	int cnt, def;
1061a64b44eaSSam Ravnborg 
1062a64b44eaSSam Ravnborg 	/*
1063579fb8e7SArnaud Lacombe 	 * If choice is mod then we may have more items selected
1064a64b44eaSSam Ravnborg 	 * and if no then no-one.
1065a64b44eaSSam Ravnborg 	 * In both cases stop.
1066a64b44eaSSam Ravnborg 	 */
1067a64b44eaSSam Ravnborg 	if (csym->curr.tri != yes)
10683b9a19e0SYann E. MORIN 		return false;
1069a64b44eaSSam Ravnborg 
1070a64b44eaSSam Ravnborg 	prop = sym_get_choice_prop(csym);
1071a64b44eaSSam Ravnborg 
1072a64b44eaSSam Ravnborg 	/* count entries in choice block */
1073a64b44eaSSam Ravnborg 	cnt = 0;
1074a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym)
1075a64b44eaSSam Ravnborg 		cnt++;
1076a64b44eaSSam Ravnborg 
1077a64b44eaSSam Ravnborg 	/*
1078a64b44eaSSam Ravnborg 	 * find a random value and set it to yes,
1079a64b44eaSSam Ravnborg 	 * set the rest to no so we have only one set
1080a64b44eaSSam Ravnborg 	 */
1081a64b44eaSSam Ravnborg 	def = (rand() % cnt);
1082a64b44eaSSam Ravnborg 
1083a64b44eaSSam Ravnborg 	cnt = 0;
1084a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym) {
1085a64b44eaSSam Ravnborg 		if (def == cnt++) {
1086a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = yes;
1087a64b44eaSSam Ravnborg 			csym->def[S_DEF_USER].val = sym;
1088a64b44eaSSam Ravnborg 		}
1089a64b44eaSSam Ravnborg 		else {
1090a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = no;
1091a64b44eaSSam Ravnborg 		}
1092e6abf12aSYann E. MORIN 		sym->flags |= SYMBOL_DEF_USER;
1093e6abf12aSYann E. MORIN 		/* clear VALID to get value calculated */
1094e6abf12aSYann E. MORIN 		sym->flags &= ~SYMBOL_VALID;
1095a64b44eaSSam Ravnborg 	}
1096a64b44eaSSam Ravnborg 	csym->flags |= SYMBOL_DEF_USER;
1097a64b44eaSSam Ravnborg 	/* clear VALID to get value calculated */
1098a64b44eaSSam Ravnborg 	csym->flags &= ~(SYMBOL_VALID);
10993b9a19e0SYann E. MORIN 
11003b9a19e0SYann E. MORIN 	return true;
1101a64b44eaSSam Ravnborg }
1102a64b44eaSSam Ravnborg 
1103fbe98bb9SArve Hjønnevåg void set_all_choice_values(struct symbol *csym)
1104a64b44eaSSam Ravnborg {
1105a64b44eaSSam Ravnborg 	struct property *prop;
1106a64b44eaSSam Ravnborg 	struct symbol *sym;
1107a64b44eaSSam Ravnborg 	struct expr *e;
1108a64b44eaSSam Ravnborg 
1109a64b44eaSSam Ravnborg 	prop = sym_get_choice_prop(csym);
1110a64b44eaSSam Ravnborg 
1111a64b44eaSSam Ravnborg 	/*
1112a64b44eaSSam Ravnborg 	 * Set all non-assinged choice values to no
1113a64b44eaSSam Ravnborg 	 */
1114a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym) {
1115a64b44eaSSam Ravnborg 		if (!sym_has_value(sym))
1116a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = no;
1117a64b44eaSSam Ravnborg 	}
1118a64b44eaSSam Ravnborg 	csym->flags |= SYMBOL_DEF_USER;
1119a64b44eaSSam Ravnborg 	/* clear VALID to get value calculated */
1120fbe98bb9SArve Hjønnevåg 	csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1121a64b44eaSSam Ravnborg }
1122dc7862e5SRoman Zippel 
11233b9a19e0SYann E. MORIN bool conf_set_all_new_symbols(enum conf_def_mode mode)
1124dc7862e5SRoman Zippel {
1125dc7862e5SRoman Zippel 	struct symbol *sym, *csym;
1126*b92d804aSMasahiro Yamada 	int i, cnt, pby, pty, ptm;	/* pby: probability of bool     = y
1127e43956e6SYann E. MORIN 					 * pty: probability of tristate = y
1128e43956e6SYann E. MORIN 					 * ptm: probability of tristate = m
1129e43956e6SYann E. MORIN 					 */
1130e43956e6SYann E. MORIN 
1131e43956e6SYann E. MORIN 	pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1132e43956e6SYann E. MORIN 				   * below, otherwise gcc whines about
1133e43956e6SYann E. MORIN 				   * -Wmaybe-uninitialized */
1134e43956e6SYann E. MORIN 	if (mode == def_random) {
1135e43956e6SYann E. MORIN 		int n, p[3];
1136e43956e6SYann E. MORIN 		char *env = getenv("KCONFIG_PROBABILITY");
1137e43956e6SYann E. MORIN 		n = 0;
1138e43956e6SYann E. MORIN 		while( env && *env ) {
1139e43956e6SYann E. MORIN 			char *endp;
1140e43956e6SYann E. MORIN 			int tmp = strtol( env, &endp, 10 );
1141e43956e6SYann E. MORIN 			if( tmp >= 0 && tmp <= 100 ) {
1142e43956e6SYann E. MORIN 				p[n++] = tmp;
1143e43956e6SYann E. MORIN 			} else {
1144e43956e6SYann E. MORIN 				errno = ERANGE;
1145e43956e6SYann E. MORIN 				perror( "KCONFIG_PROBABILITY" );
1146e43956e6SYann E. MORIN 				exit( 1 );
1147e43956e6SYann E. MORIN 			}
1148e43956e6SYann E. MORIN 			env = (*endp == ':') ? endp+1 : endp;
1149e43956e6SYann E. MORIN 			if( n >=3 ) {
1150e43956e6SYann E. MORIN 				break;
1151e43956e6SYann E. MORIN 			}
1152e43956e6SYann E. MORIN 		}
1153e43956e6SYann E. MORIN 		switch( n ) {
1154e43956e6SYann E. MORIN 		case 1:
1155e43956e6SYann E. MORIN 			pby = p[0]; ptm = pby/2; pty = pby-ptm;
1156e43956e6SYann E. MORIN 			break;
1157e43956e6SYann E. MORIN 		case 2:
1158e43956e6SYann E. MORIN 			pty = p[0]; ptm = p[1]; pby = pty + ptm;
1159e43956e6SYann E. MORIN 			break;
1160e43956e6SYann E. MORIN 		case 3:
1161e43956e6SYann E. MORIN 			pby = p[0]; pty = p[1]; ptm = p[2];
1162e43956e6SYann E. MORIN 			break;
1163e43956e6SYann E. MORIN 		}
1164e43956e6SYann E. MORIN 
1165e43956e6SYann E. MORIN 		if( pty+ptm > 100 ) {
1166e43956e6SYann E. MORIN 			errno = ERANGE;
1167e43956e6SYann E. MORIN 			perror( "KCONFIG_PROBABILITY" );
1168e43956e6SYann E. MORIN 			exit( 1 );
1169e43956e6SYann E. MORIN 		}
1170e43956e6SYann E. MORIN 	}
11713b9a19e0SYann E. MORIN 	bool has_changed = false;
1172dc7862e5SRoman Zippel 
1173dc7862e5SRoman Zippel 	for_all_symbols(i, sym) {
1174cfa98f2eSYann E. MORIN 		if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
1175dc7862e5SRoman Zippel 			continue;
1176dc7862e5SRoman Zippel 		switch (sym_get_type(sym)) {
1177dc7862e5SRoman Zippel 		case S_BOOLEAN:
1178dc7862e5SRoman Zippel 		case S_TRISTATE:
11793b9a19e0SYann E. MORIN 			has_changed = true;
1180dc7862e5SRoman Zippel 			switch (mode) {
1181dc7862e5SRoman Zippel 			case def_yes:
1182dc7862e5SRoman Zippel 				sym->def[S_DEF_USER].tri = yes;
1183dc7862e5SRoman Zippel 				break;
1184dc7862e5SRoman Zippel 			case def_mod:
1185dc7862e5SRoman Zippel 				sym->def[S_DEF_USER].tri = mod;
1186dc7862e5SRoman Zippel 				break;
1187dc7862e5SRoman Zippel 			case def_no:
11885d2acfc7SJosh Triplett 				if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
11895d2acfc7SJosh Triplett 					sym->def[S_DEF_USER].tri = yes;
11905d2acfc7SJosh Triplett 				else
1191dc7862e5SRoman Zippel 					sym->def[S_DEF_USER].tri = no;
1192dc7862e5SRoman Zippel 				break;
1193dc7862e5SRoman Zippel 			case def_random:
1194e43956e6SYann E. MORIN 				sym->def[S_DEF_USER].tri = no;
1195e43956e6SYann E. MORIN 				cnt = rand() % 100;
1196e43956e6SYann E. MORIN 				if (sym->type == S_TRISTATE) {
1197e43956e6SYann E. MORIN 					if (cnt < pty)
1198e43956e6SYann E. MORIN 						sym->def[S_DEF_USER].tri = yes;
1199e43956e6SYann E. MORIN 					else if (cnt < (pty+ptm))
1200e43956e6SYann E. MORIN 						sym->def[S_DEF_USER].tri = mod;
1201e43956e6SYann E. MORIN 				} else if (cnt < pby)
1202e43956e6SYann E. MORIN 					sym->def[S_DEF_USER].tri = yes;
1203dc7862e5SRoman Zippel 				break;
1204dc7862e5SRoman Zippel 			default:
1205dc7862e5SRoman Zippel 				continue;
1206dc7862e5SRoman Zippel 			}
1207184832c9SSam Ravnborg 			if (!(sym_is_choice(sym) && mode == def_random))
1208dc7862e5SRoman Zippel 				sym->flags |= SYMBOL_DEF_USER;
1209dc7862e5SRoman Zippel 			break;
1210dc7862e5SRoman Zippel 		default:
1211dc7862e5SRoman Zippel 			break;
1212dc7862e5SRoman Zippel 		}
1213dc7862e5SRoman Zippel 
1214dc7862e5SRoman Zippel 	}
1215dc7862e5SRoman Zippel 
1216ce97e13eSAl Viro 	sym_clear_all_valid();
1217dc7862e5SRoman Zippel 
1218184832c9SSam Ravnborg 	/*
1219184832c9SSam Ravnborg 	 * We have different type of choice blocks.
1220579fb8e7SArnaud Lacombe 	 * If curr.tri equals to mod then we can select several
1221184832c9SSam Ravnborg 	 * choice symbols in one block.
1222184832c9SSam Ravnborg 	 * In this case we do nothing.
1223579fb8e7SArnaud Lacombe 	 * If curr.tri equals yes then only one symbol can be
1224184832c9SSam Ravnborg 	 * selected in a choice block and we set it to yes,
1225184832c9SSam Ravnborg 	 * and the rest to no.
1226184832c9SSam Ravnborg 	 */
1227fbe98bb9SArve Hjønnevåg 	if (mode != def_random) {
1228fbe98bb9SArve Hjønnevåg 		for_all_symbols(i, csym) {
1229fbe98bb9SArve Hjønnevåg 			if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1230fbe98bb9SArve Hjønnevåg 			    sym_is_choice_value(csym))
1231fbe98bb9SArve Hjønnevåg 				csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1232fbe98bb9SArve Hjønnevåg 		}
1233fbe98bb9SArve Hjønnevåg 	}
1234fbe98bb9SArve Hjønnevåg 
1235dc7862e5SRoman Zippel 	for_all_symbols(i, csym) {
1236dc7862e5SRoman Zippel 		if (sym_has_value(csym) || !sym_is_choice(csym))
1237dc7862e5SRoman Zippel 			continue;
1238dc7862e5SRoman Zippel 
1239dc7862e5SRoman Zippel 		sym_calc_value(csym);
1240a64b44eaSSam Ravnborg 		if (mode == def_random)
12413b9a19e0SYann E. MORIN 			has_changed = randomize_choice_values(csym);
12423b9a19e0SYann E. MORIN 		else {
12433b9a19e0SYann E. MORIN 			set_all_choice_values(csym);
12443b9a19e0SYann E. MORIN 			has_changed = true;
1245dc7862e5SRoman Zippel 		}
1246dc7862e5SRoman Zippel 	}
12473b9a19e0SYann E. MORIN 
12483b9a19e0SYann E. MORIN 	return has_changed;
12493b9a19e0SYann E. MORIN }
1250