xref: /linux/scripts/kconfig/confdata.c (revision 84062dd3a6a045395a43de1d9adc9b8eb2d1426e)
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>
82e3646e5SRoman Zippel #include <fcntl.h>
91da177e4SLinus Torvalds #include <stdio.h>
101da177e4SLinus Torvalds #include <stdlib.h>
111da177e4SLinus Torvalds #include <string.h>
121da177e4SLinus Torvalds #include <time.h>
131da177e4SLinus Torvalds #include <unistd.h>
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #define LKC_DIRECT_LINK
161da177e4SLinus Torvalds #include "lkc.h"
171da177e4SLinus Torvalds 
18c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
19c1a0f5e3SRoman Zippel 	__attribute__ ((format (printf, 1, 2)));
20c1a0f5e3SRoman Zippel 
21c1a0f5e3SRoman Zippel static const char *conf_filename;
22c1a0f5e3SRoman Zippel static int conf_lineno, conf_warnings, conf_unsaved;
23c1a0f5e3SRoman Zippel 
241da177e4SLinus Torvalds const char conf_defname[] = "arch/$ARCH/defconfig";
251da177e4SLinus Torvalds 
26c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
27c1a0f5e3SRoman Zippel {
28c1a0f5e3SRoman Zippel 	va_list ap;
29c1a0f5e3SRoman Zippel 	va_start(ap, fmt);
30c1a0f5e3SRoman Zippel 	fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
31c1a0f5e3SRoman Zippel 	vfprintf(stderr, fmt, ap);
32c1a0f5e3SRoman Zippel 	fprintf(stderr, "\n");
33c1a0f5e3SRoman Zippel 	va_end(ap);
34c1a0f5e3SRoman Zippel 	conf_warnings++;
35c1a0f5e3SRoman Zippel }
36c1a0f5e3SRoman Zippel 
3714cdd3c4SRoman Zippel const char *conf_get_configname(void)
3814cdd3c4SRoman Zippel {
3914cdd3c4SRoman Zippel 	char *name = getenv("KCONFIG_CONFIG");
4014cdd3c4SRoman Zippel 
4114cdd3c4SRoman Zippel 	return name ? name : ".config";
4214cdd3c4SRoman Zippel }
4314cdd3c4SRoman Zippel 
4412122f62SMarkus Heidelberg const char *conf_get_autoconfig_name(void)
4512122f62SMarkus Heidelberg {
4612122f62SMarkus Heidelberg 	char *name = getenv("KCONFIG_AUTOCONFIG");
4712122f62SMarkus Heidelberg 
4812122f62SMarkus Heidelberg 	return name ? name : "include/config/auto.conf";
4912122f62SMarkus Heidelberg }
5012122f62SMarkus Heidelberg 
5148b9d03cSJ.A. Magallon static char *conf_expand_value(const char *in)
521da177e4SLinus Torvalds {
531da177e4SLinus Torvalds 	struct symbol *sym;
5448b9d03cSJ.A. Magallon 	const char *src;
551da177e4SLinus Torvalds 	static char res_value[SYMBOL_MAXLENGTH];
561da177e4SLinus Torvalds 	char *dst, name[SYMBOL_MAXLENGTH];
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 	res_value[0] = 0;
591da177e4SLinus Torvalds 	dst = name;
601da177e4SLinus Torvalds 	while ((src = strchr(in, '$'))) {
611da177e4SLinus Torvalds 		strncat(res_value, in, src - in);
621da177e4SLinus Torvalds 		src++;
631da177e4SLinus Torvalds 		dst = name;
641da177e4SLinus Torvalds 		while (isalnum(*src) || *src == '_')
651da177e4SLinus Torvalds 			*dst++ = *src++;
661da177e4SLinus Torvalds 		*dst = 0;
671da177e4SLinus Torvalds 		sym = sym_lookup(name, 0);
681da177e4SLinus Torvalds 		sym_calc_value(sym);
691da177e4SLinus Torvalds 		strcat(res_value, sym_get_string_value(sym));
701da177e4SLinus Torvalds 		in = src;
711da177e4SLinus Torvalds 	}
721da177e4SLinus Torvalds 	strcat(res_value, in);
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	return res_value;
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds char *conf_get_default_confname(void)
781da177e4SLinus Torvalds {
791da177e4SLinus Torvalds 	struct stat buf;
801da177e4SLinus Torvalds 	static char fullname[PATH_MAX+1];
811da177e4SLinus Torvalds 	char *env, *name;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	name = conf_expand_value(conf_defname);
841da177e4SLinus Torvalds 	env = getenv(SRCTREE);
851da177e4SLinus Torvalds 	if (env) {
861da177e4SLinus Torvalds 		sprintf(fullname, "%s/%s", env, name);
871da177e4SLinus Torvalds 		if (!stat(fullname, &buf))
881da177e4SLinus Torvalds 			return fullname;
891da177e4SLinus Torvalds 	}
901da177e4SLinus Torvalds 	return name;
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds 
939c900a9cSSam Ravnborg static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
949c900a9cSSam Ravnborg {
959c900a9cSSam Ravnborg 	char *p2;
969c900a9cSSam Ravnborg 
979c900a9cSSam Ravnborg 	switch (sym->type) {
989c900a9cSSam Ravnborg 	case S_TRISTATE:
999c900a9cSSam Ravnborg 		if (p[0] == 'm') {
1009c900a9cSSam Ravnborg 			sym->def[def].tri = mod;
1019c900a9cSSam Ravnborg 			sym->flags |= def_flags;
1029c900a9cSSam Ravnborg 			break;
1039c900a9cSSam Ravnborg 		}
1049c900a9cSSam Ravnborg 	case S_BOOLEAN:
1059c900a9cSSam Ravnborg 		if (p[0] == 'y') {
1069c900a9cSSam Ravnborg 			sym->def[def].tri = yes;
1079c900a9cSSam Ravnborg 			sym->flags |= def_flags;
1089c900a9cSSam Ravnborg 			break;
1099c900a9cSSam Ravnborg 		}
1109c900a9cSSam Ravnborg 		if (p[0] == 'n') {
1119c900a9cSSam Ravnborg 			sym->def[def].tri = no;
1129c900a9cSSam Ravnborg 			sym->flags |= def_flags;
1139c900a9cSSam Ravnborg 			break;
1149c900a9cSSam Ravnborg 		}
1159c900a9cSSam Ravnborg 		conf_warning("symbol value '%s' invalid for %s", p, sym->name);
1169c900a9cSSam Ravnborg 		break;
1179c900a9cSSam Ravnborg 	case S_OTHER:
1189c900a9cSSam Ravnborg 		if (*p != '"') {
1199c900a9cSSam Ravnborg 			for (p2 = p; *p2 && !isspace(*p2); p2++)
1209c900a9cSSam Ravnborg 				;
1219c900a9cSSam Ravnborg 			sym->type = S_STRING;
1229c900a9cSSam Ravnborg 			goto done;
1239c900a9cSSam Ravnborg 		}
1249c900a9cSSam Ravnborg 	case S_STRING:
1259c900a9cSSam Ravnborg 		if (*p++ != '"')
1269c900a9cSSam Ravnborg 			break;
1279c900a9cSSam Ravnborg 		for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
1289c900a9cSSam Ravnborg 			if (*p2 == '"') {
1299c900a9cSSam Ravnborg 				*p2 = 0;
1309c900a9cSSam Ravnborg 				break;
1319c900a9cSSam Ravnborg 			}
1329c900a9cSSam Ravnborg 			memmove(p2, p2 + 1, strlen(p2));
1339c900a9cSSam Ravnborg 		}
1349c900a9cSSam Ravnborg 		if (!p2) {
1359c900a9cSSam Ravnborg 			conf_warning("invalid string found");
1369c900a9cSSam Ravnborg 			return 1;
1379c900a9cSSam Ravnborg 		}
1389c900a9cSSam Ravnborg 	case S_INT:
1399c900a9cSSam Ravnborg 	case S_HEX:
1409c900a9cSSam Ravnborg 	done:
1419c900a9cSSam Ravnborg 		if (sym_string_valid(sym, p)) {
1429c900a9cSSam Ravnborg 			sym->def[def].val = strdup(p);
1439c900a9cSSam Ravnborg 			sym->flags |= def_flags;
1449c900a9cSSam Ravnborg 		} else {
1459c900a9cSSam Ravnborg 			conf_warning("symbol value '%s' invalid for %s", p, sym->name);
1469c900a9cSSam Ravnborg 			return 1;
1479c900a9cSSam Ravnborg 		}
1489c900a9cSSam Ravnborg 		break;
1499c900a9cSSam Ravnborg 	default:
1509c900a9cSSam Ravnborg 		;
1519c900a9cSSam Ravnborg 	}
1529c900a9cSSam Ravnborg 	return 0;
1539c900a9cSSam Ravnborg }
1549c900a9cSSam Ravnborg 
155669bfad9SRoman Zippel int conf_read_simple(const char *name, int def)
1561da177e4SLinus Torvalds {
1571da177e4SLinus Torvalds 	FILE *in = NULL;
1581da177e4SLinus Torvalds 	char line[1024];
1591da177e4SLinus Torvalds 	char *p, *p2;
1601da177e4SLinus Torvalds 	struct symbol *sym;
161669bfad9SRoman Zippel 	int i, def_flags;
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	if (name) {
1641da177e4SLinus Torvalds 		in = zconf_fopen(name);
1651da177e4SLinus Torvalds 	} else {
166face4374SRoman Zippel 		struct property *prop;
167face4374SRoman Zippel 
16814cdd3c4SRoman Zippel 		name = conf_get_configname();
169ddc97cacSRoman Zippel 		in = zconf_fopen(name);
170ddc97cacSRoman Zippel 		if (in)
171ddc97cacSRoman Zippel 			goto load;
172bfc10001SKarsten Wiese 		sym_add_change_count(1);
173ac1ffde1SUlf Magnusson 		if (!sym_defconfig_list) {
174ac1ffde1SUlf Magnusson 			if (modules_sym)
175ac1ffde1SUlf Magnusson 				sym_calc_value(modules_sym);
176face4374SRoman Zippel 			return 1;
177ac1ffde1SUlf Magnusson 		}
178face4374SRoman Zippel 
179face4374SRoman Zippel 		for_all_defaults(sym_defconfig_list, prop) {
180face4374SRoman Zippel 			if (expr_calc_value(prop->visible.expr) == no ||
181face4374SRoman Zippel 			    prop->expr->type != E_SYMBOL)
182face4374SRoman Zippel 				continue;
183face4374SRoman Zippel 			name = conf_expand_value(prop->expr->left.sym->name);
1841da177e4SLinus Torvalds 			in = zconf_fopen(name);
1851da177e4SLinus Torvalds 			if (in) {
1863b9fa093SArnaldo Carvalho de Melo 				printf(_("#\n"
1871da177e4SLinus Torvalds 					 "# using defaults found in %s\n"
1883b9fa093SArnaldo Carvalho de Melo 					 "#\n"), name);
189ddc97cacSRoman Zippel 				goto load;
1901da177e4SLinus Torvalds 			}
1911da177e4SLinus Torvalds 		}
1921da177e4SLinus Torvalds 	}
1931da177e4SLinus Torvalds 	if (!in)
1941da177e4SLinus Torvalds 		return 1;
1951da177e4SLinus Torvalds 
196ddc97cacSRoman Zippel load:
197c1a0f5e3SRoman Zippel 	conf_filename = name;
198c1a0f5e3SRoman Zippel 	conf_lineno = 0;
199c1a0f5e3SRoman Zippel 	conf_warnings = 0;
200c1a0f5e3SRoman Zippel 	conf_unsaved = 0;
201c1a0f5e3SRoman Zippel 
202669bfad9SRoman Zippel 	def_flags = SYMBOL_DEF << def;
2031da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
204669bfad9SRoman Zippel 		sym->flags |= SYMBOL_CHANGED;
205669bfad9SRoman Zippel 		sym->flags &= ~(def_flags|SYMBOL_VALID);
206c1a0f5e3SRoman Zippel 		if (sym_is_choice(sym))
207669bfad9SRoman Zippel 			sym->flags |= def_flags;
2081da177e4SLinus Torvalds 		switch (sym->type) {
2091da177e4SLinus Torvalds 		case S_INT:
2101da177e4SLinus Torvalds 		case S_HEX:
2111da177e4SLinus Torvalds 		case S_STRING:
212669bfad9SRoman Zippel 			if (sym->def[def].val)
213669bfad9SRoman Zippel 				free(sym->def[def].val);
2141da177e4SLinus Torvalds 		default:
215669bfad9SRoman Zippel 			sym->def[def].val = NULL;
216669bfad9SRoman Zippel 			sym->def[def].tri = no;
2171da177e4SLinus Torvalds 		}
2181da177e4SLinus Torvalds 	}
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	while (fgets(line, sizeof(line), in)) {
221c1a0f5e3SRoman Zippel 		conf_lineno++;
2221da177e4SLinus Torvalds 		sym = NULL;
2231da177e4SLinus Torvalds 		switch (line[0]) {
2241da177e4SLinus Torvalds 		case '#':
2251da177e4SLinus Torvalds 			if (memcmp(line + 2, "CONFIG_", 7))
2261da177e4SLinus Torvalds 				continue;
2271da177e4SLinus Torvalds 			p = strchr(line + 9, ' ');
2281da177e4SLinus Torvalds 			if (!p)
2291da177e4SLinus Torvalds 				continue;
2301da177e4SLinus Torvalds 			*p++ = 0;
2311da177e4SLinus Torvalds 			if (strncmp(p, "is not set", 10))
2321da177e4SLinus Torvalds 				continue;
233669bfad9SRoman Zippel 			if (def == S_DEF_USER) {
2341da177e4SLinus Torvalds 				sym = sym_find(line + 9);
235661b0680Szippel@linux-m68k.org 				if (!sym) {
236661b0680Szippel@linux-m68k.org 					sym_add_change_count(1);
237c1a0f5e3SRoman Zippel 					break;
238661b0680Szippel@linux-m68k.org 				}
239669bfad9SRoman Zippel 			} else {
240669bfad9SRoman Zippel 				sym = sym_lookup(line + 9, 0);
241669bfad9SRoman Zippel 				if (sym->type == S_UNKNOWN)
242669bfad9SRoman Zippel 					sym->type = S_BOOLEAN;
243669bfad9SRoman Zippel 			}
244669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
245d84876f9SJan Engelhardt 				conf_warning("override: reassigning to symbol %s", sym->name);
2461da177e4SLinus Torvalds 			}
2471da177e4SLinus Torvalds 			switch (sym->type) {
2481da177e4SLinus Torvalds 			case S_BOOLEAN:
2491da177e4SLinus Torvalds 			case S_TRISTATE:
250669bfad9SRoman Zippel 				sym->def[def].tri = no;
251669bfad9SRoman Zippel 				sym->flags |= def_flags;
2521da177e4SLinus Torvalds 				break;
2531da177e4SLinus Torvalds 			default:
2541da177e4SLinus Torvalds 				;
2551da177e4SLinus Torvalds 			}
2561da177e4SLinus Torvalds 			break;
2571da177e4SLinus Torvalds 		case 'C':
258c1a0f5e3SRoman Zippel 			if (memcmp(line, "CONFIG_", 7)) {
259c1a0f5e3SRoman Zippel 				conf_warning("unexpected data");
2601da177e4SLinus Torvalds 				continue;
261c1a0f5e3SRoman Zippel 			}
2621da177e4SLinus Torvalds 			p = strchr(line + 7, '=');
2631da177e4SLinus Torvalds 			if (!p)
2641da177e4SLinus Torvalds 				continue;
2651da177e4SLinus Torvalds 			*p++ = 0;
2661da177e4SLinus Torvalds 			p2 = strchr(p, '\n');
267d3660a8cSMatthew Wilcox 			if (p2) {
268d3660a8cSMatthew Wilcox 				*p2-- = 0;
269d3660a8cSMatthew Wilcox 				if (*p2 == '\r')
2701da177e4SLinus Torvalds 					*p2 = 0;
271d3660a8cSMatthew Wilcox 			}
272669bfad9SRoman Zippel 			if (def == S_DEF_USER) {
2731da177e4SLinus Torvalds 				sym = sym_find(line + 7);
274661b0680Szippel@linux-m68k.org 				if (!sym) {
275661b0680Szippel@linux-m68k.org 					sym_add_change_count(1);
276c1a0f5e3SRoman Zippel 					break;
277661b0680Szippel@linux-m68k.org 				}
278669bfad9SRoman Zippel 			} else {
279669bfad9SRoman Zippel 				sym = sym_lookup(line + 7, 0);
280669bfad9SRoman Zippel 				if (sym->type == S_UNKNOWN)
281669bfad9SRoman Zippel 					sym->type = S_OTHER;
282669bfad9SRoman Zippel 			}
283669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
284d84876f9SJan Engelhardt 				conf_warning("override: reassigning to symbol %s", sym->name);
2851da177e4SLinus Torvalds 			}
2869c900a9cSSam Ravnborg 			if (conf_set_sym_val(sym, def, def_flags, p))
287c1a0f5e3SRoman Zippel 				continue;
2881da177e4SLinus Torvalds 			break;
289d3660a8cSMatthew Wilcox 		case '\r':
2901da177e4SLinus Torvalds 		case '\n':
2911da177e4SLinus Torvalds 			break;
2921da177e4SLinus Torvalds 		default:
293c1a0f5e3SRoman Zippel 			conf_warning("unexpected data");
2941da177e4SLinus Torvalds 			continue;
2951da177e4SLinus Torvalds 		}
2961da177e4SLinus Torvalds 		if (sym && sym_is_choice_value(sym)) {
2971da177e4SLinus Torvalds 			struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
298669bfad9SRoman Zippel 			switch (sym->def[def].tri) {
2991da177e4SLinus Torvalds 			case no:
3001da177e4SLinus Torvalds 				break;
3011da177e4SLinus Torvalds 			case mod:
302669bfad9SRoman Zippel 				if (cs->def[def].tri == yes) {
303c1a0f5e3SRoman Zippel 					conf_warning("%s creates inconsistent choice state", sym->name);
304669bfad9SRoman Zippel 					cs->flags &= ~def_flags;
305c1a0f5e3SRoman Zippel 				}
3061da177e4SLinus Torvalds 				break;
3071da177e4SLinus Torvalds 			case yes:
308d84876f9SJan Engelhardt 				if (cs->def[def].tri != no)
309d84876f9SJan Engelhardt 					conf_warning("override: %s changes choice state", sym->name);
310669bfad9SRoman Zippel 				cs->def[def].val = sym;
3111da177e4SLinus Torvalds 				break;
3121da177e4SLinus Torvalds 			}
313d6ee3576SSam Ravnborg 			cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
3141da177e4SLinus Torvalds 		}
3151da177e4SLinus Torvalds 	}
3161da177e4SLinus Torvalds 	fclose(in);
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	if (modules_sym)
3191da177e4SLinus Torvalds 		sym_calc_value(modules_sym);
32090389160SRoman Zippel 	return 0;
32190389160SRoman Zippel }
32290389160SRoman Zippel 
32390389160SRoman Zippel int conf_read(const char *name)
32490389160SRoman Zippel {
3257a962923SRoman Zippel 	struct symbol *sym, *choice_sym;
32690389160SRoman Zippel 	struct property *prop;
32790389160SRoman Zippel 	struct expr *e;
328669bfad9SRoman Zippel 	int i, flags;
32990389160SRoman Zippel 
330bfc10001SKarsten Wiese 	sym_set_change_count(0);
331ddc97cacSRoman Zippel 
332669bfad9SRoman Zippel 	if (conf_read_simple(name, S_DEF_USER))
33390389160SRoman Zippel 		return 1;
33490389160SRoman Zippel 
3351da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
3361da177e4SLinus Torvalds 		sym_calc_value(sym);
337c1a0f5e3SRoman Zippel 		if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
338c1a0f5e3SRoman Zippel 			goto sym_ok;
339c1a0f5e3SRoman Zippel 		if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
340c1a0f5e3SRoman Zippel 			/* check that calculated value agrees with saved value */
341c1a0f5e3SRoman Zippel 			switch (sym->type) {
342c1a0f5e3SRoman Zippel 			case S_BOOLEAN:
343c1a0f5e3SRoman Zippel 			case S_TRISTATE:
3440c1822e6SRoman Zippel 				if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
345c1a0f5e3SRoman Zippel 					break;
346c1a0f5e3SRoman Zippel 				if (!sym_is_choice(sym))
347c1a0f5e3SRoman Zippel 					goto sym_ok;
348c1a0f5e3SRoman Zippel 			default:
3490c1822e6SRoman Zippel 				if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
350c1a0f5e3SRoman Zippel 					goto sym_ok;
351c1a0f5e3SRoman Zippel 				break;
352c1a0f5e3SRoman Zippel 			}
353c1a0f5e3SRoman Zippel 		} else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
354c1a0f5e3SRoman Zippel 			/* no previous value and not saved */
355c1a0f5e3SRoman Zippel 			goto sym_ok;
356c1a0f5e3SRoman Zippel 		conf_unsaved++;
357c1a0f5e3SRoman Zippel 		/* maybe print value in verbose mode... */
358c1a0f5e3SRoman Zippel 	sym_ok:
3591da177e4SLinus Torvalds 		if (!sym_is_choice(sym))
3601da177e4SLinus Torvalds 			continue;
361d8982ba1SRoman Zippel 		/* The choice symbol only has a set value (and thus is not new)
362d8982ba1SRoman Zippel 		 * if all its visible childs have values.
363d8982ba1SRoman Zippel 		 */
3641da177e4SLinus Torvalds 		prop = sym_get_choice_prop(sym);
365669bfad9SRoman Zippel 		flags = sym->flags;
3667a962923SRoman Zippel 		expr_list_for_each_sym(prop->expr, e, choice_sym)
3677a962923SRoman Zippel 			if (choice_sym->visible != no)
3687a962923SRoman Zippel 				flags &= choice_sym->flags;
369002d27b1SRoman Zippel 		sym->flags &= flags | ~SYMBOL_DEF_USER;
3701da177e4SLinus Torvalds 	}
3711da177e4SLinus Torvalds 
372d8982ba1SRoman Zippel 	for_all_symbols(i, sym) {
373d8982ba1SRoman Zippel 		if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
374d8982ba1SRoman Zippel 			/* Reset values of generates values, so they'll appear
375d8982ba1SRoman Zippel 			 * as new, if they should become visible, but that
376d8982ba1SRoman Zippel 			 * doesn't quite work if the Kconfig and the saved
377d8982ba1SRoman Zippel 			 * configuration disagree.
378d8982ba1SRoman Zippel 			 */
379d8982ba1SRoman Zippel 			if (sym->visible == no && !conf_unsaved)
380d8982ba1SRoman Zippel 				sym->flags &= ~SYMBOL_DEF_USER;
381d8982ba1SRoman Zippel 			switch (sym->type) {
382d8982ba1SRoman Zippel 			case S_STRING:
383d8982ba1SRoman Zippel 			case S_INT:
384d8982ba1SRoman Zippel 			case S_HEX:
385d8982ba1SRoman Zippel 				/* Reset a string value if it's out of range */
386d8982ba1SRoman Zippel 				if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
387d8982ba1SRoman Zippel 					break;
388d8982ba1SRoman Zippel 				sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
389d8982ba1SRoman Zippel 				conf_unsaved++;
390d8982ba1SRoman Zippel 				break;
391d8982ba1SRoman Zippel 			default:
392d8982ba1SRoman Zippel 				break;
393d8982ba1SRoman Zippel 			}
394d8982ba1SRoman Zippel 		}
395d8982ba1SRoman Zippel 	}
396d8982ba1SRoman Zippel 
397bfc10001SKarsten Wiese 	sym_add_change_count(conf_warnings || conf_unsaved);
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	return 0;
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds 
40249192f26SSam Ravnborg /* Write a S_STRING */
40349192f26SSam Ravnborg static void conf_write_string(bool headerfile, const char *name,
40449192f26SSam Ravnborg                               const char *str, FILE *out)
40549192f26SSam Ravnborg {
40649192f26SSam Ravnborg 	int l;
40749192f26SSam Ravnborg 	if (headerfile)
40849192f26SSam Ravnborg 		fprintf(out, "#define CONFIG_%s \"", name);
40949192f26SSam Ravnborg 	else
41049192f26SSam Ravnborg 		fprintf(out, "CONFIG_%s=\"", name);
41149192f26SSam Ravnborg 
41249192f26SSam Ravnborg 	while (1) {
41349192f26SSam Ravnborg 		l = strcspn(str, "\"\\");
41449192f26SSam Ravnborg 		if (l) {
415bf5e327aSJean Sacren 			xfwrite(str, l, 1, out);
41649192f26SSam Ravnborg 			str += l;
41749192f26SSam Ravnborg 		}
41849192f26SSam Ravnborg 		if (!*str)
41949192f26SSam Ravnborg 			break;
42049192f26SSam Ravnborg 		fprintf(out, "\\%c", *str++);
42149192f26SSam Ravnborg 	}
42249192f26SSam Ravnborg 	fputs("\"\n", out);
42349192f26SSam Ravnborg }
42449192f26SSam Ravnborg 
42549192f26SSam Ravnborg static void conf_write_symbol(struct symbol *sym, enum symbol_type type,
42649192f26SSam Ravnborg                               FILE *out, bool write_no)
42749192f26SSam Ravnborg {
42849192f26SSam Ravnborg 	const char *str;
42949192f26SSam Ravnborg 
43049192f26SSam Ravnborg 	switch (type) {
43149192f26SSam Ravnborg 	case S_BOOLEAN:
43249192f26SSam Ravnborg 	case S_TRISTATE:
43349192f26SSam Ravnborg 		switch (sym_get_tristate_value(sym)) {
43449192f26SSam Ravnborg 		case no:
43549192f26SSam Ravnborg 			if (write_no)
43649192f26SSam Ravnborg 				fprintf(out, "# CONFIG_%s is not set\n", sym->name);
43749192f26SSam Ravnborg 			break;
43849192f26SSam Ravnborg 		case mod:
43949192f26SSam Ravnborg 			fprintf(out, "CONFIG_%s=m\n", sym->name);
44049192f26SSam Ravnborg 			break;
44149192f26SSam Ravnborg 		case yes:
44249192f26SSam Ravnborg 			fprintf(out, "CONFIG_%s=y\n", sym->name);
44349192f26SSam Ravnborg 			break;
44449192f26SSam Ravnborg 		}
44549192f26SSam Ravnborg 		break;
44649192f26SSam Ravnborg 	case S_STRING:
44749192f26SSam Ravnborg 		conf_write_string(false, sym->name, sym_get_string_value(sym), out);
44849192f26SSam Ravnborg 		break;
44949192f26SSam Ravnborg 	case S_HEX:
45049192f26SSam Ravnborg 	case S_INT:
45149192f26SSam Ravnborg 		str = sym_get_string_value(sym);
45249192f26SSam Ravnborg 		fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
45349192f26SSam Ravnborg 		break;
45449192f26SSam Ravnborg 	case S_OTHER:
45549192f26SSam Ravnborg 	case S_UNKNOWN:
45649192f26SSam Ravnborg 		break;
45749192f26SSam Ravnborg 	}
45849192f26SSam Ravnborg }
45949192f26SSam Ravnborg 
4607cf3d73bSSam Ravnborg /*
4617cf3d73bSSam Ravnborg  * Write out a minimal config.
4627cf3d73bSSam Ravnborg  * All values that has default values are skipped as this is redundant.
4637cf3d73bSSam Ravnborg  */
4647cf3d73bSSam Ravnborg int conf_write_defconfig(const char *filename)
4657cf3d73bSSam Ravnborg {
4667cf3d73bSSam Ravnborg 	struct symbol *sym;
4677cf3d73bSSam Ravnborg 	struct menu *menu;
4687cf3d73bSSam Ravnborg 	FILE *out;
4697cf3d73bSSam Ravnborg 
4707cf3d73bSSam Ravnborg 	out = fopen(filename, "w");
4717cf3d73bSSam Ravnborg 	if (!out)
4727cf3d73bSSam Ravnborg 		return 1;
4737cf3d73bSSam Ravnborg 
4747cf3d73bSSam Ravnborg 	sym_clear_all_valid();
4757cf3d73bSSam Ravnborg 
4767cf3d73bSSam Ravnborg 	/* Traverse all menus to find all relevant symbols */
4777cf3d73bSSam Ravnborg 	menu = rootmenu.list;
4787cf3d73bSSam Ravnborg 
4797cf3d73bSSam Ravnborg 	while (menu != NULL)
4807cf3d73bSSam Ravnborg 	{
4817cf3d73bSSam Ravnborg 		sym = menu->sym;
4827cf3d73bSSam Ravnborg 		if (sym == NULL) {
4837cf3d73bSSam Ravnborg 			if (!menu_is_visible(menu))
4847cf3d73bSSam Ravnborg 				goto next_menu;
4857cf3d73bSSam Ravnborg 		} else if (!sym_is_choice(sym)) {
4867cf3d73bSSam Ravnborg 			sym_calc_value(sym);
4877cf3d73bSSam Ravnborg 			if (!(sym->flags & SYMBOL_WRITE))
4887cf3d73bSSam Ravnborg 				goto next_menu;
4897cf3d73bSSam Ravnborg 			sym->flags &= ~SYMBOL_WRITE;
4907cf3d73bSSam Ravnborg 			/* If we cannot change the symbol - skip */
4917cf3d73bSSam Ravnborg 			if (!sym_is_changable(sym))
4927cf3d73bSSam Ravnborg 				goto next_menu;
4937cf3d73bSSam Ravnborg 			/* If symbol equals to default value - skip */
4947cf3d73bSSam Ravnborg 			if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
4957cf3d73bSSam Ravnborg 				goto next_menu;
4967cf3d73bSSam Ravnborg 
4977cf3d73bSSam Ravnborg 			/*
4987cf3d73bSSam Ravnborg 			 * If symbol is a choice value and equals to the
4997cf3d73bSSam Ravnborg 			 * default for a choice - skip.
500*84062dd3SSam Ravnborg 			 * But only if value is bool and equal to "y" and
501*84062dd3SSam Ravnborg 			 * choice is not "optional".
502*84062dd3SSam Ravnborg 			 * (If choice is "optional" then all values can be "n")
5037cf3d73bSSam Ravnborg 			 */
5047cf3d73bSSam Ravnborg 			if (sym_is_choice_value(sym)) {
5057cf3d73bSSam Ravnborg 				struct symbol *cs;
5067cf3d73bSSam Ravnborg 				struct symbol *ds;
5077cf3d73bSSam Ravnborg 
5087cf3d73bSSam Ravnborg 				cs = prop_get_symbol(sym_get_choice_prop(sym));
5097cf3d73bSSam Ravnborg 				ds = sym_choice_default(cs);
510*84062dd3SSam Ravnborg 				if (!sym_is_optional(cs) && sym == ds) {
511801690caSSam Ravnborg 					if ((sym->type == S_BOOLEAN) &&
5127cf3d73bSSam Ravnborg 					    sym_get_tristate_value(sym) == yes)
5137cf3d73bSSam Ravnborg 						goto next_menu;
5147cf3d73bSSam Ravnborg 				}
5157cf3d73bSSam Ravnborg 			}
5167cf3d73bSSam Ravnborg 			conf_write_symbol(sym, sym->type, out, true);
5177cf3d73bSSam Ravnborg 		}
5187cf3d73bSSam Ravnborg next_menu:
5197cf3d73bSSam Ravnborg 		if (menu->list != NULL) {
5207cf3d73bSSam Ravnborg 			menu = menu->list;
5217cf3d73bSSam Ravnborg 		}
5227cf3d73bSSam Ravnborg 		else if (menu->next != NULL) {
5237cf3d73bSSam Ravnborg 			menu = menu->next;
5247cf3d73bSSam Ravnborg 		} else {
5257cf3d73bSSam Ravnborg 			while ((menu = menu->parent)) {
5267cf3d73bSSam Ravnborg 				if (menu->next != NULL) {
5277cf3d73bSSam Ravnborg 					menu = menu->next;
5287cf3d73bSSam Ravnborg 					break;
5297cf3d73bSSam Ravnborg 				}
5307cf3d73bSSam Ravnborg 			}
5317cf3d73bSSam Ravnborg 		}
5327cf3d73bSSam Ravnborg 	}
5337cf3d73bSSam Ravnborg 	fclose(out);
5347cf3d73bSSam Ravnborg 	return 0;
5357cf3d73bSSam Ravnborg }
5367cf3d73bSSam Ravnborg 
5371da177e4SLinus Torvalds int conf_write(const char *name)
5381da177e4SLinus Torvalds {
539c955ccafSRoman Zippel 	FILE *out;
5401da177e4SLinus Torvalds 	struct symbol *sym;
5411da177e4SLinus Torvalds 	struct menu *menu;
5421da177e4SLinus Torvalds 	const char *basename;
5431da177e4SLinus Torvalds 	const char *str;
54449192f26SSam Ravnborg 	char dirname[128], tmpname[128], newname[128];
54549192f26SSam Ravnborg 	enum symbol_type type;
5461da177e4SLinus Torvalds 	time_t now;
5471da177e4SLinus Torvalds 	int use_timestamp = 1;
5481da177e4SLinus Torvalds 	char *env;
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	dirname[0] = 0;
5511da177e4SLinus Torvalds 	if (name && name[0]) {
5521da177e4SLinus Torvalds 		struct stat st;
5531da177e4SLinus Torvalds 		char *slash;
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 		if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
5561da177e4SLinus Torvalds 			strcpy(dirname, name);
5571da177e4SLinus Torvalds 			strcat(dirname, "/");
55814cdd3c4SRoman Zippel 			basename = conf_get_configname();
5591da177e4SLinus Torvalds 		} else if ((slash = strrchr(name, '/'))) {
5601da177e4SLinus Torvalds 			int size = slash - name + 1;
5611da177e4SLinus Torvalds 			memcpy(dirname, name, size);
5621da177e4SLinus Torvalds 			dirname[size] = 0;
5631da177e4SLinus Torvalds 			if (slash[1])
5641da177e4SLinus Torvalds 				basename = slash + 1;
5651da177e4SLinus Torvalds 			else
56614cdd3c4SRoman Zippel 				basename = conf_get_configname();
5671da177e4SLinus Torvalds 		} else
5681da177e4SLinus Torvalds 			basename = name;
5691da177e4SLinus Torvalds 	} else
57014cdd3c4SRoman Zippel 		basename = conf_get_configname();
5711da177e4SLinus Torvalds 
57214cdd3c4SRoman Zippel 	sprintf(newname, "%s%s", dirname, basename);
57314cdd3c4SRoman Zippel 	env = getenv("KCONFIG_OVERWRITECONFIG");
57414cdd3c4SRoman Zippel 	if (!env || !*env) {
57514cdd3c4SRoman Zippel 		sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
57614cdd3c4SRoman Zippel 		out = fopen(tmpname, "w");
57714cdd3c4SRoman Zippel 	} else {
57814cdd3c4SRoman Zippel 		*tmpname = 0;
5791da177e4SLinus Torvalds 		out = fopen(newname, "w");
58014cdd3c4SRoman Zippel 	}
5811da177e4SLinus Torvalds 	if (!out)
5821da177e4SLinus Torvalds 		return 1;
58314cdd3c4SRoman Zippel 
5842244cbd8SSam Ravnborg 	sym = sym_lookup("KERNELVERSION", 0);
5851da177e4SLinus Torvalds 	sym_calc_value(sym);
5861da177e4SLinus Torvalds 	time(&now);
5871da177e4SLinus Torvalds 	env = getenv("KCONFIG_NOTIMESTAMP");
5881da177e4SLinus Torvalds 	if (env && *env)
5891da177e4SLinus Torvalds 		use_timestamp = 0;
5901da177e4SLinus Torvalds 
5913b9fa093SArnaldo Carvalho de Melo 	fprintf(out, _("#\n"
5921da177e4SLinus Torvalds 		       "# Automatically generated make config: don't edit\n"
5931da177e4SLinus Torvalds 		       "# Linux kernel version: %s\n"
5941da177e4SLinus Torvalds 		       "%s%s"
5953b9fa093SArnaldo Carvalho de Melo 		       "#\n"),
5961da177e4SLinus Torvalds 		     sym_get_string_value(sym),
5971da177e4SLinus Torvalds 		     use_timestamp ? "# " : "",
5981da177e4SLinus Torvalds 		     use_timestamp ? ctime(&now) : "");
5991da177e4SLinus Torvalds 
600b3214293SKarsten Wiese 	if (!conf_get_changed())
6011da177e4SLinus Torvalds 		sym_clear_all_valid();
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	menu = rootmenu.list;
6041da177e4SLinus Torvalds 	while (menu) {
6051da177e4SLinus Torvalds 		sym = menu->sym;
6061da177e4SLinus Torvalds 		if (!sym) {
6071da177e4SLinus Torvalds 			if (!menu_is_visible(menu))
6081da177e4SLinus Torvalds 				goto next;
6091da177e4SLinus Torvalds 			str = menu_get_prompt(menu);
6101da177e4SLinus Torvalds 			fprintf(out, "\n"
6111da177e4SLinus Torvalds 				     "#\n"
6121da177e4SLinus Torvalds 				     "# %s\n"
6131da177e4SLinus Torvalds 				     "#\n", str);
6141da177e4SLinus Torvalds 		} else if (!(sym->flags & SYMBOL_CHOICE)) {
6151da177e4SLinus Torvalds 			sym_calc_value(sym);
6161da177e4SLinus Torvalds 			if (!(sym->flags & SYMBOL_WRITE))
6171da177e4SLinus Torvalds 				goto next;
6181da177e4SLinus Torvalds 			sym->flags &= ~SYMBOL_WRITE;
6191da177e4SLinus Torvalds 			type = sym->type;
6201da177e4SLinus Torvalds 			if (type == S_TRISTATE) {
6211da177e4SLinus Torvalds 				sym_calc_value(modules_sym);
6221da177e4SLinus Torvalds 				if (modules_sym->curr.tri == no)
6231da177e4SLinus Torvalds 					type = S_BOOLEAN;
6241da177e4SLinus Torvalds 			}
62549192f26SSam Ravnborg 			/* Write config symbol to file */
62649192f26SSam Ravnborg 			conf_write_symbol(sym, type, out, true);
6271da177e4SLinus Torvalds 		}
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds next:
6301da177e4SLinus Torvalds 		if (menu->list) {
6311da177e4SLinus Torvalds 			menu = menu->list;
6321da177e4SLinus Torvalds 			continue;
6331da177e4SLinus Torvalds 		}
6341da177e4SLinus Torvalds 		if (menu->next)
6351da177e4SLinus Torvalds 			menu = menu->next;
6361da177e4SLinus Torvalds 		else while ((menu = menu->parent)) {
6371da177e4SLinus Torvalds 			if (menu->next) {
6381da177e4SLinus Torvalds 				menu = menu->next;
6391da177e4SLinus Torvalds 				break;
6401da177e4SLinus Torvalds 			}
6411da177e4SLinus Torvalds 		}
6421da177e4SLinus Torvalds 	}
6431da177e4SLinus Torvalds 	fclose(out);
64414cdd3c4SRoman Zippel 
64514cdd3c4SRoman Zippel 	if (*tmpname) {
6469a3d0fe8SSam Ravnborg 		strcat(dirname, basename);
64714cdd3c4SRoman Zippel 		strcat(dirname, ".old");
64814cdd3c4SRoman Zippel 		rename(newname, dirname);
64914cdd3c4SRoman Zippel 		if (rename(tmpname, newname))
6501da177e4SLinus Torvalds 			return 1;
65114cdd3c4SRoman Zippel 	}
6521da177e4SLinus Torvalds 
653ddc97cacSRoman Zippel 	printf(_("#\n"
654ddc97cacSRoman Zippel 		 "# configuration written to %s\n"
65514cdd3c4SRoman Zippel 		 "#\n"), newname);
656ddc97cacSRoman Zippel 
657bfc10001SKarsten Wiese 	sym_set_change_count(0);
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	return 0;
6601da177e4SLinus Torvalds }
661c955ccafSRoman Zippel 
6624356f489STrevor Keith static int conf_split_config(void)
6632e3646e5SRoman Zippel {
66412122f62SMarkus Heidelberg 	const char *name;
66512122f62SMarkus Heidelberg 	char path[128];
6662e3646e5SRoman Zippel 	char *s, *d, c;
6672e3646e5SRoman Zippel 	struct symbol *sym;
6682e3646e5SRoman Zippel 	struct stat sb;
6692e3646e5SRoman Zippel 	int res, i, fd;
6702e3646e5SRoman Zippel 
67112122f62SMarkus Heidelberg 	name = conf_get_autoconfig_name();
6722e3646e5SRoman Zippel 	conf_read_simple(name, S_DEF_AUTO);
6732e3646e5SRoman Zippel 
6742e3646e5SRoman Zippel 	if (chdir("include/config"))
6752e3646e5SRoman Zippel 		return 1;
6762e3646e5SRoman Zippel 
6772e3646e5SRoman Zippel 	res = 0;
6782e3646e5SRoman Zippel 	for_all_symbols(i, sym) {
6792e3646e5SRoman Zippel 		sym_calc_value(sym);
6802e3646e5SRoman Zippel 		if ((sym->flags & SYMBOL_AUTO) || !sym->name)
6812e3646e5SRoman Zippel 			continue;
6822e3646e5SRoman Zippel 		if (sym->flags & SYMBOL_WRITE) {
6832e3646e5SRoman Zippel 			if (sym->flags & SYMBOL_DEF_AUTO) {
6842e3646e5SRoman Zippel 				/*
6852e3646e5SRoman Zippel 				 * symbol has old and new value,
6862e3646e5SRoman Zippel 				 * so compare them...
6872e3646e5SRoman Zippel 				 */
6882e3646e5SRoman Zippel 				switch (sym->type) {
6892e3646e5SRoman Zippel 				case S_BOOLEAN:
6902e3646e5SRoman Zippel 				case S_TRISTATE:
6912e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) ==
6922e3646e5SRoman Zippel 					    sym->def[S_DEF_AUTO].tri)
6932e3646e5SRoman Zippel 						continue;
6942e3646e5SRoman Zippel 					break;
6952e3646e5SRoman Zippel 				case S_STRING:
6962e3646e5SRoman Zippel 				case S_HEX:
6972e3646e5SRoman Zippel 				case S_INT:
6982e3646e5SRoman Zippel 					if (!strcmp(sym_get_string_value(sym),
6992e3646e5SRoman Zippel 						    sym->def[S_DEF_AUTO].val))
7002e3646e5SRoman Zippel 						continue;
7012e3646e5SRoman Zippel 					break;
7022e3646e5SRoman Zippel 				default:
7032e3646e5SRoman Zippel 					break;
7042e3646e5SRoman Zippel 				}
7052e3646e5SRoman Zippel 			} else {
7062e3646e5SRoman Zippel 				/*
7072e3646e5SRoman Zippel 				 * If there is no old value, only 'no' (unset)
7082e3646e5SRoman Zippel 				 * is allowed as new value.
7092e3646e5SRoman Zippel 				 */
7102e3646e5SRoman Zippel 				switch (sym->type) {
7112e3646e5SRoman Zippel 				case S_BOOLEAN:
7122e3646e5SRoman Zippel 				case S_TRISTATE:
7132e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) == no)
7142e3646e5SRoman Zippel 						continue;
7152e3646e5SRoman Zippel 					break;
7162e3646e5SRoman Zippel 				default:
7172e3646e5SRoman Zippel 					break;
7182e3646e5SRoman Zippel 				}
7192e3646e5SRoman Zippel 			}
7202e3646e5SRoman Zippel 		} else if (!(sym->flags & SYMBOL_DEF_AUTO))
7212e3646e5SRoman Zippel 			/* There is neither an old nor a new value. */
7222e3646e5SRoman Zippel 			continue;
7232e3646e5SRoman Zippel 		/* else
7242e3646e5SRoman Zippel 		 *	There is an old value, but no new value ('no' (unset)
7252e3646e5SRoman Zippel 		 *	isn't saved in auto.conf, so the old value is always
7262e3646e5SRoman Zippel 		 *	different from 'no').
7272e3646e5SRoman Zippel 		 */
7282e3646e5SRoman Zippel 
7292e3646e5SRoman Zippel 		/* Replace all '_' and append ".h" */
7302e3646e5SRoman Zippel 		s = sym->name;
7312e3646e5SRoman Zippel 		d = path;
7322e3646e5SRoman Zippel 		while ((c = *s++)) {
7332e3646e5SRoman Zippel 			c = tolower(c);
7342e3646e5SRoman Zippel 			*d++ = (c == '_') ? '/' : c;
7352e3646e5SRoman Zippel 		}
7362e3646e5SRoman Zippel 		strcpy(d, ".h");
7372e3646e5SRoman Zippel 
7382e3646e5SRoman Zippel 		/* Assume directory path already exists. */
7392e3646e5SRoman Zippel 		fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
7402e3646e5SRoman Zippel 		if (fd == -1) {
7412e3646e5SRoman Zippel 			if (errno != ENOENT) {
7422e3646e5SRoman Zippel 				res = 1;
7432e3646e5SRoman Zippel 				break;
7442e3646e5SRoman Zippel 			}
7452e3646e5SRoman Zippel 			/*
7462e3646e5SRoman Zippel 			 * Create directory components,
7472e3646e5SRoman Zippel 			 * unless they exist already.
7482e3646e5SRoman Zippel 			 */
7492e3646e5SRoman Zippel 			d = path;
7502e3646e5SRoman Zippel 			while ((d = strchr(d, '/'))) {
7512e3646e5SRoman Zippel 				*d = 0;
7522e3646e5SRoman Zippel 				if (stat(path, &sb) && mkdir(path, 0755)) {
7532e3646e5SRoman Zippel 					res = 1;
7542e3646e5SRoman Zippel 					goto out;
7552e3646e5SRoman Zippel 				}
7562e3646e5SRoman Zippel 				*d++ = '/';
7572e3646e5SRoman Zippel 			}
7582e3646e5SRoman Zippel 			/* Try it again. */
7592e3646e5SRoman Zippel 			fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
7602e3646e5SRoman Zippel 			if (fd == -1) {
7612e3646e5SRoman Zippel 				res = 1;
7622e3646e5SRoman Zippel 				break;
7632e3646e5SRoman Zippel 			}
7642e3646e5SRoman Zippel 		}
7652e3646e5SRoman Zippel 		close(fd);
7662e3646e5SRoman Zippel 	}
7672e3646e5SRoman Zippel out:
7682e3646e5SRoman Zippel 	if (chdir("../.."))
7692e3646e5SRoman Zippel 		return 1;
7702e3646e5SRoman Zippel 
7712e3646e5SRoman Zippel 	return res;
7722e3646e5SRoman Zippel }
7732e3646e5SRoman Zippel 
774c955ccafSRoman Zippel int conf_write_autoconf(void)
775c955ccafSRoman Zippel {
776c955ccafSRoman Zippel 	struct symbol *sym;
777c955ccafSRoman Zippel 	const char *str;
77812122f62SMarkus Heidelberg 	const char *name;
779bc081dd6SMichal Marek 	FILE *out, *tristate, *out_h;
780c955ccafSRoman Zippel 	time_t now;
78149192f26SSam Ravnborg 	int i;
782c955ccafSRoman Zippel 
7832e3646e5SRoman Zippel 	sym_clear_all_valid();
7842e3646e5SRoman Zippel 
785c955ccafSRoman Zippel 	file_write_dep("include/config/auto.conf.cmd");
786c955ccafSRoman Zippel 
7872e3646e5SRoman Zippel 	if (conf_split_config())
7882e3646e5SRoman Zippel 		return 1;
7892e3646e5SRoman Zippel 
790c955ccafSRoman Zippel 	out = fopen(".tmpconfig", "w");
791c955ccafSRoman Zippel 	if (!out)
792c955ccafSRoman Zippel 		return 1;
793c955ccafSRoman Zippel 
794bc081dd6SMichal Marek 	tristate = fopen(".tmpconfig_tristate", "w");
795bc081dd6SMichal Marek 	if (!tristate) {
796bc081dd6SMichal Marek 		fclose(out);
797bc081dd6SMichal Marek 		return 1;
798bc081dd6SMichal Marek 	}
799bc081dd6SMichal Marek 
800c955ccafSRoman Zippel 	out_h = fopen(".tmpconfig.h", "w");
801c955ccafSRoman Zippel 	if (!out_h) {
802c955ccafSRoman Zippel 		fclose(out);
803bc081dd6SMichal Marek 		fclose(tristate);
804c955ccafSRoman Zippel 		return 1;
805c955ccafSRoman Zippel 	}
806c955ccafSRoman Zippel 
807c955ccafSRoman Zippel 	sym = sym_lookup("KERNELVERSION", 0);
808c955ccafSRoman Zippel 	sym_calc_value(sym);
809c955ccafSRoman Zippel 	time(&now);
810c955ccafSRoman Zippel 	fprintf(out, "#\n"
811c955ccafSRoman Zippel 		     "# Automatically generated make config: don't edit\n"
812c955ccafSRoman Zippel 		     "# Linux kernel version: %s\n"
813c955ccafSRoman Zippel 		     "# %s"
814c955ccafSRoman Zippel 		     "#\n",
815c955ccafSRoman Zippel 		     sym_get_string_value(sym), ctime(&now));
816bc081dd6SMichal Marek 	fprintf(tristate, "#\n"
817bc081dd6SMichal Marek 			  "# Automatically generated - do not edit\n"
818bc081dd6SMichal Marek 			  "\n");
819c955ccafSRoman Zippel 	fprintf(out_h, "/*\n"
820c955ccafSRoman Zippel 		       " * Automatically generated C config: don't edit\n"
821c955ccafSRoman Zippel 		       " * Linux kernel version: %s\n"
822c955ccafSRoman Zippel 		       " * %s"
823c955ccafSRoman Zippel 		       " */\n"
824c955ccafSRoman Zippel 		       "#define AUTOCONF_INCLUDED\n",
825c955ccafSRoman Zippel 		       sym_get_string_value(sym), ctime(&now));
826c955ccafSRoman Zippel 
827c955ccafSRoman Zippel 	for_all_symbols(i, sym) {
828c955ccafSRoman Zippel 		sym_calc_value(sym);
829c955ccafSRoman Zippel 		if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
830c955ccafSRoman Zippel 			continue;
83149192f26SSam Ravnborg 
83249192f26SSam Ravnborg 		/* write symbol to config file */
83349192f26SSam Ravnborg 		conf_write_symbol(sym, sym->type, out, false);
83449192f26SSam Ravnborg 
83549192f26SSam Ravnborg 		/* update autoconf and tristate files */
836c955ccafSRoman Zippel 		switch (sym->type) {
837c955ccafSRoman Zippel 		case S_BOOLEAN:
838c955ccafSRoman Zippel 		case S_TRISTATE:
839c955ccafSRoman Zippel 			switch (sym_get_tristate_value(sym)) {
840c955ccafSRoman Zippel 			case no:
841c955ccafSRoman Zippel 				break;
842c955ccafSRoman Zippel 			case mod:
843bc081dd6SMichal Marek 				fprintf(tristate, "CONFIG_%s=M\n", sym->name);
844c955ccafSRoman Zippel 				fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
845c955ccafSRoman Zippel 				break;
846c955ccafSRoman Zippel 			case yes:
847bc081dd6SMichal Marek 				if (sym->type == S_TRISTATE)
848bc081dd6SMichal Marek 					fprintf(tristate, "CONFIG_%s=Y\n",
849bc081dd6SMichal Marek 							sym->name);
850c955ccafSRoman Zippel 				fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
851c955ccafSRoman Zippel 				break;
852c955ccafSRoman Zippel 			}
853c955ccafSRoman Zippel 			break;
854c955ccafSRoman Zippel 		case S_STRING:
85549192f26SSam Ravnborg 			conf_write_string(true, sym->name, sym_get_string_value(sym), out_h);
856c955ccafSRoman Zippel 			break;
857c955ccafSRoman Zippel 		case S_HEX:
858c955ccafSRoman Zippel 			str = sym_get_string_value(sym);
859c955ccafSRoman Zippel 			if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
860c955ccafSRoman Zippel 				fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
861c955ccafSRoman Zippel 				break;
862c955ccafSRoman Zippel 			}
863c955ccafSRoman Zippel 		case S_INT:
864c955ccafSRoman Zippel 			str = sym_get_string_value(sym);
865c955ccafSRoman Zippel 			fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
866c955ccafSRoman Zippel 			break;
867c955ccafSRoman Zippel 		default:
868c955ccafSRoman Zippel 			break;
869c955ccafSRoman Zippel 		}
870c955ccafSRoman Zippel 	}
871c955ccafSRoman Zippel 	fclose(out);
872bc081dd6SMichal Marek 	fclose(tristate);
873c955ccafSRoman Zippel 	fclose(out_h);
874c955ccafSRoman Zippel 
875c955ccafSRoman Zippel 	name = getenv("KCONFIG_AUTOHEADER");
876c955ccafSRoman Zippel 	if (!name)
877264a2683SSam Ravnborg 		name = "include/generated/autoconf.h";
878c955ccafSRoman Zippel 	if (rename(".tmpconfig.h", name))
879c955ccafSRoman Zippel 		return 1;
880bc081dd6SMichal Marek 	name = getenv("KCONFIG_TRISTATE");
881bc081dd6SMichal Marek 	if (!name)
882bc081dd6SMichal Marek 		name = "include/config/tristate.conf";
883bc081dd6SMichal Marek 	if (rename(".tmpconfig_tristate", name))
884bc081dd6SMichal Marek 		return 1;
88512122f62SMarkus Heidelberg 	name = conf_get_autoconfig_name();
886c955ccafSRoman Zippel 	/*
887c955ccafSRoman Zippel 	 * This must be the last step, kbuild has a dependency on auto.conf
888c955ccafSRoman Zippel 	 * and this marks the successful completion of the previous steps.
889c955ccafSRoman Zippel 	 */
890c955ccafSRoman Zippel 	if (rename(".tmpconfig", name))
891c955ccafSRoman Zippel 		return 1;
892c955ccafSRoman Zippel 
893c955ccafSRoman Zippel 	return 0;
894c955ccafSRoman Zippel }
895b3214293SKarsten Wiese 
896bfc10001SKarsten Wiese static int sym_change_count;
8973b354c55SKarsten Wiese static void (*conf_changed_callback)(void);
898bfc10001SKarsten Wiese 
899bfc10001SKarsten Wiese void sym_set_change_count(int count)
900bfc10001SKarsten Wiese {
9013b354c55SKarsten Wiese 	int _sym_change_count = sym_change_count;
902bfc10001SKarsten Wiese 	sym_change_count = count;
9033b354c55SKarsten Wiese 	if (conf_changed_callback &&
9043b354c55SKarsten Wiese 	    (bool)_sym_change_count != (bool)count)
9053b354c55SKarsten Wiese 		conf_changed_callback();
906bfc10001SKarsten Wiese }
907bfc10001SKarsten Wiese 
908bfc10001SKarsten Wiese void sym_add_change_count(int count)
909bfc10001SKarsten Wiese {
9103b354c55SKarsten Wiese 	sym_set_change_count(count + sym_change_count);
911bfc10001SKarsten Wiese }
912bfc10001SKarsten Wiese 
913b3214293SKarsten Wiese bool conf_get_changed(void)
914b3214293SKarsten Wiese {
915b3214293SKarsten Wiese 	return sym_change_count;
916b3214293SKarsten Wiese }
9173b354c55SKarsten Wiese 
9183b354c55SKarsten Wiese void conf_set_changed_callback(void (*fn)(void))
9193b354c55SKarsten Wiese {
9203b354c55SKarsten Wiese 	conf_changed_callback = fn;
9213b354c55SKarsten Wiese }
922dc7862e5SRoman Zippel 
923a64b44eaSSam Ravnborg static void randomize_choice_values(struct symbol *csym)
924a64b44eaSSam Ravnborg {
925a64b44eaSSam Ravnborg 	struct property *prop;
926a64b44eaSSam Ravnborg 	struct symbol *sym;
927a64b44eaSSam Ravnborg 	struct expr *e;
928a64b44eaSSam Ravnborg 	int cnt, def;
929a64b44eaSSam Ravnborg 
930a64b44eaSSam Ravnborg 	/*
931a64b44eaSSam Ravnborg 	 * If choice is mod then we may have more items slected
932a64b44eaSSam Ravnborg 	 * and if no then no-one.
933a64b44eaSSam Ravnborg 	 * In both cases stop.
934a64b44eaSSam Ravnborg 	 */
935a64b44eaSSam Ravnborg 	if (csym->curr.tri != yes)
936a64b44eaSSam Ravnborg 		return;
937a64b44eaSSam Ravnborg 
938a64b44eaSSam Ravnborg 	prop = sym_get_choice_prop(csym);
939a64b44eaSSam Ravnborg 
940a64b44eaSSam Ravnborg 	/* count entries in choice block */
941a64b44eaSSam Ravnborg 	cnt = 0;
942a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym)
943a64b44eaSSam Ravnborg 		cnt++;
944a64b44eaSSam Ravnborg 
945a64b44eaSSam Ravnborg 	/*
946a64b44eaSSam Ravnborg 	 * find a random value and set it to yes,
947a64b44eaSSam Ravnborg 	 * set the rest to no so we have only one set
948a64b44eaSSam Ravnborg 	 */
949a64b44eaSSam Ravnborg 	def = (rand() % cnt);
950a64b44eaSSam Ravnborg 
951a64b44eaSSam Ravnborg 	cnt = 0;
952a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym) {
953a64b44eaSSam Ravnborg 		if (def == cnt++) {
954a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = yes;
955a64b44eaSSam Ravnborg 			csym->def[S_DEF_USER].val = sym;
956a64b44eaSSam Ravnborg 		}
957a64b44eaSSam Ravnborg 		else {
958a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = no;
959a64b44eaSSam Ravnborg 		}
960a64b44eaSSam Ravnborg 	}
961a64b44eaSSam Ravnborg 	csym->flags |= SYMBOL_DEF_USER;
962a64b44eaSSam Ravnborg 	/* clear VALID to get value calculated */
963a64b44eaSSam Ravnborg 	csym->flags &= ~(SYMBOL_VALID);
964a64b44eaSSam Ravnborg }
965a64b44eaSSam Ravnborg 
966a64b44eaSSam Ravnborg static void set_all_choice_values(struct symbol *csym)
967a64b44eaSSam Ravnborg {
968a64b44eaSSam Ravnborg 	struct property *prop;
969a64b44eaSSam Ravnborg 	struct symbol *sym;
970a64b44eaSSam Ravnborg 	struct expr *e;
971a64b44eaSSam Ravnborg 
972a64b44eaSSam Ravnborg 	prop = sym_get_choice_prop(csym);
973a64b44eaSSam Ravnborg 
974a64b44eaSSam Ravnborg 	/*
975a64b44eaSSam Ravnborg 	 * Set all non-assinged choice values to no
976a64b44eaSSam Ravnborg 	 */
977a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym) {
978a64b44eaSSam Ravnborg 		if (!sym_has_value(sym))
979a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = no;
980a64b44eaSSam Ravnborg 	}
981a64b44eaSSam Ravnborg 	csym->flags |= SYMBOL_DEF_USER;
982a64b44eaSSam Ravnborg 	/* clear VALID to get value calculated */
983a64b44eaSSam Ravnborg 	csym->flags &= ~(SYMBOL_VALID);
984a64b44eaSSam Ravnborg }
985dc7862e5SRoman Zippel 
986dc7862e5SRoman Zippel void conf_set_all_new_symbols(enum conf_def_mode mode)
987dc7862e5SRoman Zippel {
988dc7862e5SRoman Zippel 	struct symbol *sym, *csym;
989a64b44eaSSam Ravnborg 	int i, cnt;
990dc7862e5SRoman Zippel 
991dc7862e5SRoman Zippel 	for_all_symbols(i, sym) {
992dc7862e5SRoman Zippel 		if (sym_has_value(sym))
993dc7862e5SRoman Zippel 			continue;
994dc7862e5SRoman Zippel 		switch (sym_get_type(sym)) {
995dc7862e5SRoman Zippel 		case S_BOOLEAN:
996dc7862e5SRoman Zippel 		case S_TRISTATE:
997dc7862e5SRoman Zippel 			switch (mode) {
998dc7862e5SRoman Zippel 			case def_yes:
999dc7862e5SRoman Zippel 				sym->def[S_DEF_USER].tri = yes;
1000dc7862e5SRoman Zippel 				break;
1001dc7862e5SRoman Zippel 			case def_mod:
1002dc7862e5SRoman Zippel 				sym->def[S_DEF_USER].tri = mod;
1003dc7862e5SRoman Zippel 				break;
1004dc7862e5SRoman Zippel 			case def_no:
1005dc7862e5SRoman Zippel 				sym->def[S_DEF_USER].tri = no;
1006dc7862e5SRoman Zippel 				break;
1007dc7862e5SRoman Zippel 			case def_random:
10081244b41dSPeter Korsgaard 				cnt = sym_get_type(sym) == S_TRISTATE ? 3 : 2;
10091244b41dSPeter Korsgaard 				sym->def[S_DEF_USER].tri = (tristate)(rand() % cnt);
1010dc7862e5SRoman Zippel 				break;
1011dc7862e5SRoman Zippel 			default:
1012dc7862e5SRoman Zippel 				continue;
1013dc7862e5SRoman Zippel 			}
1014184832c9SSam Ravnborg 			if (!(sym_is_choice(sym) && mode == def_random))
1015dc7862e5SRoman Zippel 				sym->flags |= SYMBOL_DEF_USER;
1016dc7862e5SRoman Zippel 			break;
1017dc7862e5SRoman Zippel 		default:
1018dc7862e5SRoman Zippel 			break;
1019dc7862e5SRoman Zippel 		}
1020dc7862e5SRoman Zippel 
1021dc7862e5SRoman Zippel 	}
1022dc7862e5SRoman Zippel 
1023ce97e13eSAl Viro 	sym_clear_all_valid();
1024dc7862e5SRoman Zippel 
1025184832c9SSam Ravnborg 	/*
1026184832c9SSam Ravnborg 	 * We have different type of choice blocks.
1027184832c9SSam Ravnborg 	 * If curr.tri equal to mod then we can select several
1028184832c9SSam Ravnborg 	 * choice symbols in one block.
1029184832c9SSam Ravnborg 	 * In this case we do nothing.
1030184832c9SSam Ravnborg 	 * If curr.tri equal yes then only one symbol can be
1031184832c9SSam Ravnborg 	 * selected in a choice block and we set it to yes,
1032184832c9SSam Ravnborg 	 * and the rest to no.
1033184832c9SSam Ravnborg 	 */
1034dc7862e5SRoman Zippel 	for_all_symbols(i, csym) {
1035dc7862e5SRoman Zippel 		if (sym_has_value(csym) || !sym_is_choice(csym))
1036dc7862e5SRoman Zippel 			continue;
1037dc7862e5SRoman Zippel 
1038dc7862e5SRoman Zippel 		sym_calc_value(csym);
1039a64b44eaSSam Ravnborg 		if (mode == def_random)
1040a64b44eaSSam Ravnborg 			randomize_choice_values(csym);
1041a64b44eaSSam Ravnborg 		else
1042a64b44eaSSam Ravnborg 			set_all_choice_values(csym);
1043dc7862e5SRoman Zippel 	}
1044dc7862e5SRoman Zippel }
1045