xref: /linux/scripts/kconfig/confdata.c (revision 8baefd30b5b0101aa07aa75da44a9eee881eed28)
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>
101da177e4SLinus Torvalds #include <stdio.h>
111da177e4SLinus Torvalds #include <stdlib.h>
121da177e4SLinus Torvalds #include <string.h>
131da177e4SLinus Torvalds #include <time.h>
141da177e4SLinus Torvalds #include <unistd.h>
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #define LKC_DIRECT_LINK
171da177e4SLinus Torvalds #include "lkc.h"
181da177e4SLinus Torvalds 
19c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
20c1a0f5e3SRoman Zippel 	__attribute__ ((format (printf, 1, 2)));
21c1a0f5e3SRoman Zippel 
22c1a0f5e3SRoman Zippel static const char *conf_filename;
23c1a0f5e3SRoman Zippel static int conf_lineno, conf_warnings, conf_unsaved;
24c1a0f5e3SRoman Zippel 
251da177e4SLinus Torvalds const char conf_defname[] = "arch/$ARCH/defconfig";
261da177e4SLinus Torvalds 
27c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
28c1a0f5e3SRoman Zippel {
29c1a0f5e3SRoman Zippel 	va_list ap;
30c1a0f5e3SRoman Zippel 	va_start(ap, fmt);
31c1a0f5e3SRoman Zippel 	fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
32c1a0f5e3SRoman Zippel 	vfprintf(stderr, fmt, ap);
33c1a0f5e3SRoman Zippel 	fprintf(stderr, "\n");
34c1a0f5e3SRoman Zippel 	va_end(ap);
35c1a0f5e3SRoman Zippel 	conf_warnings++;
36c1a0f5e3SRoman Zippel }
37c1a0f5e3SRoman Zippel 
3814cdd3c4SRoman Zippel const char *conf_get_configname(void)
3914cdd3c4SRoman Zippel {
4014cdd3c4SRoman Zippel 	char *name = getenv("KCONFIG_CONFIG");
4114cdd3c4SRoman Zippel 
4214cdd3c4SRoman Zippel 	return name ? name : ".config";
4314cdd3c4SRoman Zippel }
4414cdd3c4SRoman Zippel 
4512122f62SMarkus Heidelberg const char *conf_get_autoconfig_name(void)
4612122f62SMarkus Heidelberg {
4712122f62SMarkus Heidelberg 	char *name = getenv("KCONFIG_AUTOCONFIG");
4812122f62SMarkus Heidelberg 
4912122f62SMarkus Heidelberg 	return name ? name : "include/config/auto.conf";
5012122f62SMarkus Heidelberg }
5112122f62SMarkus Heidelberg 
5248b9d03cSJ.A. Magallon static char *conf_expand_value(const char *in)
531da177e4SLinus Torvalds {
541da177e4SLinus Torvalds 	struct symbol *sym;
5548b9d03cSJ.A. Magallon 	const char *src;
561da177e4SLinus Torvalds 	static char res_value[SYMBOL_MAXLENGTH];
571da177e4SLinus Torvalds 	char *dst, name[SYMBOL_MAXLENGTH];
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds 	res_value[0] = 0;
601da177e4SLinus Torvalds 	dst = name;
611da177e4SLinus Torvalds 	while ((src = strchr(in, '$'))) {
621da177e4SLinus Torvalds 		strncat(res_value, in, src - in);
631da177e4SLinus Torvalds 		src++;
641da177e4SLinus Torvalds 		dst = name;
651da177e4SLinus Torvalds 		while (isalnum(*src) || *src == '_')
661da177e4SLinus Torvalds 			*dst++ = *src++;
671da177e4SLinus Torvalds 		*dst = 0;
681da177e4SLinus Torvalds 		sym = sym_lookup(name, 0);
691da177e4SLinus Torvalds 		sym_calc_value(sym);
701da177e4SLinus Torvalds 		strcat(res_value, sym_get_string_value(sym));
711da177e4SLinus Torvalds 		in = src;
721da177e4SLinus Torvalds 	}
731da177e4SLinus Torvalds 	strcat(res_value, in);
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	return res_value;
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds char *conf_get_default_confname(void)
791da177e4SLinus Torvalds {
801da177e4SLinus Torvalds 	struct stat buf;
811da177e4SLinus Torvalds 	static char fullname[PATH_MAX+1];
821da177e4SLinus Torvalds 	char *env, *name;
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	name = conf_expand_value(conf_defname);
851da177e4SLinus Torvalds 	env = getenv(SRCTREE);
861da177e4SLinus Torvalds 	if (env) {
871da177e4SLinus Torvalds 		sprintf(fullname, "%s/%s", env, name);
881da177e4SLinus Torvalds 		if (!stat(fullname, &buf))
891da177e4SLinus Torvalds 			return fullname;
901da177e4SLinus Torvalds 	}
911da177e4SLinus Torvalds 	return name;
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
949c900a9cSSam Ravnborg static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
959c900a9cSSam Ravnborg {
969c900a9cSSam Ravnborg 	char *p2;
979c900a9cSSam Ravnborg 
989c900a9cSSam Ravnborg 	switch (sym->type) {
999c900a9cSSam Ravnborg 	case S_TRISTATE:
1009c900a9cSSam Ravnborg 		if (p[0] == 'm') {
1019c900a9cSSam Ravnborg 			sym->def[def].tri = mod;
1029c900a9cSSam Ravnborg 			sym->flags |= def_flags;
1039c900a9cSSam Ravnborg 			break;
1049c900a9cSSam Ravnborg 		}
1059c900a9cSSam Ravnborg 	case S_BOOLEAN:
1069c900a9cSSam Ravnborg 		if (p[0] == 'y') {
1079c900a9cSSam Ravnborg 			sym->def[def].tri = yes;
1089c900a9cSSam Ravnborg 			sym->flags |= def_flags;
1099c900a9cSSam Ravnborg 			break;
1109c900a9cSSam Ravnborg 		}
1119c900a9cSSam Ravnborg 		if (p[0] == 'n') {
1129c900a9cSSam Ravnborg 			sym->def[def].tri = no;
1139c900a9cSSam Ravnborg 			sym->flags |= def_flags;
1149c900a9cSSam Ravnborg 			break;
1159c900a9cSSam Ravnborg 		}
1169c900a9cSSam Ravnborg 		conf_warning("symbol value '%s' invalid for %s", p, sym->name);
1179c900a9cSSam Ravnborg 		break;
1189c900a9cSSam Ravnborg 	case S_OTHER:
1199c900a9cSSam Ravnborg 		if (*p != '"') {
1209c900a9cSSam Ravnborg 			for (p2 = p; *p2 && !isspace(*p2); p2++)
1219c900a9cSSam Ravnborg 				;
1229c900a9cSSam Ravnborg 			sym->type = S_STRING;
1239c900a9cSSam Ravnborg 			goto done;
1249c900a9cSSam Ravnborg 		}
1259c900a9cSSam Ravnborg 	case S_STRING:
1269c900a9cSSam Ravnborg 		if (*p++ != '"')
1279c900a9cSSam Ravnborg 			break;
1289c900a9cSSam Ravnborg 		for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
1299c900a9cSSam Ravnborg 			if (*p2 == '"') {
1309c900a9cSSam Ravnborg 				*p2 = 0;
1319c900a9cSSam Ravnborg 				break;
1329c900a9cSSam Ravnborg 			}
1339c900a9cSSam Ravnborg 			memmove(p2, p2 + 1, strlen(p2));
1349c900a9cSSam Ravnborg 		}
1359c900a9cSSam Ravnborg 		if (!p2) {
1369c900a9cSSam Ravnborg 			conf_warning("invalid string found");
1379c900a9cSSam Ravnborg 			return 1;
1389c900a9cSSam Ravnborg 		}
1399c900a9cSSam Ravnborg 	case S_INT:
1409c900a9cSSam Ravnborg 	case S_HEX:
1419c900a9cSSam Ravnborg 	done:
1429c900a9cSSam Ravnborg 		if (sym_string_valid(sym, p)) {
1439c900a9cSSam Ravnborg 			sym->def[def].val = strdup(p);
1449c900a9cSSam Ravnborg 			sym->flags |= def_flags;
1459c900a9cSSam Ravnborg 		} else {
1469c900a9cSSam Ravnborg 			conf_warning("symbol value '%s' invalid for %s", p, sym->name);
1479c900a9cSSam Ravnborg 			return 1;
1489c900a9cSSam Ravnborg 		}
1499c900a9cSSam Ravnborg 		break;
1509c900a9cSSam Ravnborg 	default:
1519c900a9cSSam Ravnborg 		;
1529c900a9cSSam Ravnborg 	}
1539c900a9cSSam Ravnborg 	return 0;
1549c900a9cSSam Ravnborg }
1559c900a9cSSam Ravnborg 
156669bfad9SRoman Zippel int conf_read_simple(const char *name, int def)
1571da177e4SLinus Torvalds {
1581da177e4SLinus Torvalds 	FILE *in = NULL;
1591da177e4SLinus Torvalds 	char line[1024];
1601da177e4SLinus Torvalds 	char *p, *p2;
1611da177e4SLinus Torvalds 	struct symbol *sym;
162669bfad9SRoman Zippel 	int i, def_flags;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	if (name) {
1651da177e4SLinus Torvalds 		in = zconf_fopen(name);
1661da177e4SLinus Torvalds 	} else {
167face4374SRoman Zippel 		struct property *prop;
168face4374SRoman Zippel 
16914cdd3c4SRoman Zippel 		name = conf_get_configname();
170ddc97cacSRoman Zippel 		in = zconf_fopen(name);
171ddc97cacSRoman Zippel 		if (in)
172ddc97cacSRoman Zippel 			goto load;
173bfc10001SKarsten Wiese 		sym_add_change_count(1);
174ac1ffde1SUlf Magnusson 		if (!sym_defconfig_list) {
175ac1ffde1SUlf Magnusson 			if (modules_sym)
176ac1ffde1SUlf Magnusson 				sym_calc_value(modules_sym);
177face4374SRoman Zippel 			return 1;
178ac1ffde1SUlf Magnusson 		}
179face4374SRoman Zippel 
180face4374SRoman Zippel 		for_all_defaults(sym_defconfig_list, prop) {
181face4374SRoman Zippel 			if (expr_calc_value(prop->visible.expr) == no ||
182face4374SRoman Zippel 			    prop->expr->type != E_SYMBOL)
183face4374SRoman Zippel 				continue;
184face4374SRoman Zippel 			name = conf_expand_value(prop->expr->left.sym->name);
1851da177e4SLinus Torvalds 			in = zconf_fopen(name);
1861da177e4SLinus Torvalds 			if (in) {
1873b9fa093SArnaldo Carvalho de Melo 				printf(_("#\n"
1881da177e4SLinus Torvalds 					 "# using defaults found in %s\n"
1893b9fa093SArnaldo Carvalho de Melo 					 "#\n"), name);
190ddc97cacSRoman Zippel 				goto load;
1911da177e4SLinus Torvalds 			}
1921da177e4SLinus Torvalds 		}
1931da177e4SLinus Torvalds 	}
1941da177e4SLinus Torvalds 	if (!in)
1951da177e4SLinus Torvalds 		return 1;
1961da177e4SLinus Torvalds 
197ddc97cacSRoman Zippel load:
198c1a0f5e3SRoman Zippel 	conf_filename = name;
199c1a0f5e3SRoman Zippel 	conf_lineno = 0;
200c1a0f5e3SRoman Zippel 	conf_warnings = 0;
201c1a0f5e3SRoman Zippel 	conf_unsaved = 0;
202c1a0f5e3SRoman Zippel 
203669bfad9SRoman Zippel 	def_flags = SYMBOL_DEF << def;
2041da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
205669bfad9SRoman Zippel 		sym->flags |= SYMBOL_CHANGED;
206669bfad9SRoman Zippel 		sym->flags &= ~(def_flags|SYMBOL_VALID);
207c1a0f5e3SRoman Zippel 		if (sym_is_choice(sym))
208669bfad9SRoman Zippel 			sym->flags |= def_flags;
2091da177e4SLinus Torvalds 		switch (sym->type) {
2101da177e4SLinus Torvalds 		case S_INT:
2111da177e4SLinus Torvalds 		case S_HEX:
2121da177e4SLinus Torvalds 		case S_STRING:
213669bfad9SRoman Zippel 			if (sym->def[def].val)
214669bfad9SRoman Zippel 				free(sym->def[def].val);
2151da177e4SLinus Torvalds 		default:
216669bfad9SRoman Zippel 			sym->def[def].val = NULL;
217669bfad9SRoman Zippel 			sym->def[def].tri = no;
2181da177e4SLinus Torvalds 		}
2191da177e4SLinus Torvalds 	}
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	while (fgets(line, sizeof(line), in)) {
222c1a0f5e3SRoman Zippel 		conf_lineno++;
2231da177e4SLinus Torvalds 		sym = NULL;
224*8baefd30SArnaud Lacombe 		if (line[0] == '#') {
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 			}
256*8baefd30SArnaud Lacombe 		} else if (memcmp(line, "CONFIG_", 7) == 0) {
2571da177e4SLinus Torvalds 			p = strchr(line + 7, '=');
2581da177e4SLinus Torvalds 			if (!p)
2591da177e4SLinus Torvalds 				continue;
2601da177e4SLinus Torvalds 			*p++ = 0;
2611da177e4SLinus Torvalds 			p2 = strchr(p, '\n');
262d3660a8cSMatthew Wilcox 			if (p2) {
263d3660a8cSMatthew Wilcox 				*p2-- = 0;
264d3660a8cSMatthew Wilcox 				if (*p2 == '\r')
2651da177e4SLinus Torvalds 					*p2 = 0;
266d3660a8cSMatthew Wilcox 			}
267669bfad9SRoman Zippel 			if (def == S_DEF_USER) {
2681da177e4SLinus Torvalds 				sym = sym_find(line + 7);
269661b0680Szippel@linux-m68k.org 				if (!sym) {
270661b0680Szippel@linux-m68k.org 					sym_add_change_count(1);
271c1a0f5e3SRoman Zippel 					break;
272661b0680Szippel@linux-m68k.org 				}
273669bfad9SRoman Zippel 			} else {
274669bfad9SRoman Zippel 				sym = sym_lookup(line + 7, 0);
275669bfad9SRoman Zippel 				if (sym->type == S_UNKNOWN)
276669bfad9SRoman Zippel 					sym->type = S_OTHER;
277669bfad9SRoman Zippel 			}
278669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
279d84876f9SJan Engelhardt 				conf_warning("override: reassigning to symbol %s", sym->name);
2801da177e4SLinus Torvalds 			}
2819c900a9cSSam Ravnborg 			if (conf_set_sym_val(sym, def, def_flags, p))
282c1a0f5e3SRoman Zippel 				continue;
283*8baefd30SArnaud Lacombe 		} else {
284*8baefd30SArnaud Lacombe 			if (line[0] != '\r' && line[0] != '\n')
285c1a0f5e3SRoman Zippel 				conf_warning("unexpected data");
2861da177e4SLinus Torvalds 			continue;
2871da177e4SLinus Torvalds 		}
2881da177e4SLinus Torvalds 		if (sym && sym_is_choice_value(sym)) {
2891da177e4SLinus Torvalds 			struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
290669bfad9SRoman Zippel 			switch (sym->def[def].tri) {
2911da177e4SLinus Torvalds 			case no:
2921da177e4SLinus Torvalds 				break;
2931da177e4SLinus Torvalds 			case mod:
294669bfad9SRoman Zippel 				if (cs->def[def].tri == yes) {
295c1a0f5e3SRoman Zippel 					conf_warning("%s creates inconsistent choice state", sym->name);
296669bfad9SRoman Zippel 					cs->flags &= ~def_flags;
297c1a0f5e3SRoman Zippel 				}
2981da177e4SLinus Torvalds 				break;
2991da177e4SLinus Torvalds 			case yes:
300d84876f9SJan Engelhardt 				if (cs->def[def].tri != no)
301d84876f9SJan Engelhardt 					conf_warning("override: %s changes choice state", sym->name);
302669bfad9SRoman Zippel 				cs->def[def].val = sym;
3031da177e4SLinus Torvalds 				break;
3041da177e4SLinus Torvalds 			}
305d6ee3576SSam Ravnborg 			cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
3061da177e4SLinus Torvalds 		}
3071da177e4SLinus Torvalds 	}
3081da177e4SLinus Torvalds 	fclose(in);
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	if (modules_sym)
3111da177e4SLinus Torvalds 		sym_calc_value(modules_sym);
31290389160SRoman Zippel 	return 0;
31390389160SRoman Zippel }
31490389160SRoman Zippel 
31590389160SRoman Zippel int conf_read(const char *name)
31690389160SRoman Zippel {
3177a962923SRoman Zippel 	struct symbol *sym, *choice_sym;
31890389160SRoman Zippel 	struct property *prop;
31990389160SRoman Zippel 	struct expr *e;
320669bfad9SRoman Zippel 	int i, flags;
32190389160SRoman Zippel 
322bfc10001SKarsten Wiese 	sym_set_change_count(0);
323ddc97cacSRoman Zippel 
324669bfad9SRoman Zippel 	if (conf_read_simple(name, S_DEF_USER))
32590389160SRoman Zippel 		return 1;
32690389160SRoman Zippel 
3271da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
3281da177e4SLinus Torvalds 		sym_calc_value(sym);
329c1a0f5e3SRoman Zippel 		if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
330c1a0f5e3SRoman Zippel 			goto sym_ok;
331c1a0f5e3SRoman Zippel 		if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
332c1a0f5e3SRoman Zippel 			/* check that calculated value agrees with saved value */
333c1a0f5e3SRoman Zippel 			switch (sym->type) {
334c1a0f5e3SRoman Zippel 			case S_BOOLEAN:
335c1a0f5e3SRoman Zippel 			case S_TRISTATE:
3360c1822e6SRoman Zippel 				if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
337c1a0f5e3SRoman Zippel 					break;
338c1a0f5e3SRoman Zippel 				if (!sym_is_choice(sym))
339c1a0f5e3SRoman Zippel 					goto sym_ok;
340c1a0f5e3SRoman Zippel 			default:
3410c1822e6SRoman Zippel 				if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
342c1a0f5e3SRoman Zippel 					goto sym_ok;
343c1a0f5e3SRoman Zippel 				break;
344c1a0f5e3SRoman Zippel 			}
345c1a0f5e3SRoman Zippel 		} else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
346c1a0f5e3SRoman Zippel 			/* no previous value and not saved */
347c1a0f5e3SRoman Zippel 			goto sym_ok;
348c1a0f5e3SRoman Zippel 		conf_unsaved++;
349c1a0f5e3SRoman Zippel 		/* maybe print value in verbose mode... */
350c1a0f5e3SRoman Zippel 	sym_ok:
3511da177e4SLinus Torvalds 		if (!sym_is_choice(sym))
3521da177e4SLinus Torvalds 			continue;
353d8982ba1SRoman Zippel 		/* The choice symbol only has a set value (and thus is not new)
354d8982ba1SRoman Zippel 		 * if all its visible childs have values.
355d8982ba1SRoman Zippel 		 */
3561da177e4SLinus Torvalds 		prop = sym_get_choice_prop(sym);
357669bfad9SRoman Zippel 		flags = sym->flags;
3587a962923SRoman Zippel 		expr_list_for_each_sym(prop->expr, e, choice_sym)
3597a962923SRoman Zippel 			if (choice_sym->visible != no)
3607a962923SRoman Zippel 				flags &= choice_sym->flags;
361002d27b1SRoman Zippel 		sym->flags &= flags | ~SYMBOL_DEF_USER;
3621da177e4SLinus Torvalds 	}
3631da177e4SLinus Torvalds 
364d8982ba1SRoman Zippel 	for_all_symbols(i, sym) {
365d8982ba1SRoman Zippel 		if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
366d8982ba1SRoman Zippel 			/* Reset values of generates values, so they'll appear
367d8982ba1SRoman Zippel 			 * as new, if they should become visible, but that
368d8982ba1SRoman Zippel 			 * doesn't quite work if the Kconfig and the saved
369d8982ba1SRoman Zippel 			 * configuration disagree.
370d8982ba1SRoman Zippel 			 */
371d8982ba1SRoman Zippel 			if (sym->visible == no && !conf_unsaved)
372d8982ba1SRoman Zippel 				sym->flags &= ~SYMBOL_DEF_USER;
373d8982ba1SRoman Zippel 			switch (sym->type) {
374d8982ba1SRoman Zippel 			case S_STRING:
375d8982ba1SRoman Zippel 			case S_INT:
376d8982ba1SRoman Zippel 			case S_HEX:
377d8982ba1SRoman Zippel 				/* Reset a string value if it's out of range */
378d8982ba1SRoman Zippel 				if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
379d8982ba1SRoman Zippel 					break;
380d8982ba1SRoman Zippel 				sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
381d8982ba1SRoman Zippel 				conf_unsaved++;
382d8982ba1SRoman Zippel 				break;
383d8982ba1SRoman Zippel 			default:
384d8982ba1SRoman Zippel 				break;
385d8982ba1SRoman Zippel 			}
386d8982ba1SRoman Zippel 		}
387d8982ba1SRoman Zippel 	}
388d8982ba1SRoman Zippel 
389bfc10001SKarsten Wiese 	sym_add_change_count(conf_warnings || conf_unsaved);
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	return 0;
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds 
39449192f26SSam Ravnborg /* Write a S_STRING */
39549192f26SSam Ravnborg static void conf_write_string(bool headerfile, const char *name,
39649192f26SSam Ravnborg                               const char *str, FILE *out)
39749192f26SSam Ravnborg {
39849192f26SSam Ravnborg 	int l;
39949192f26SSam Ravnborg 	if (headerfile)
40049192f26SSam Ravnborg 		fprintf(out, "#define CONFIG_%s \"", name);
40149192f26SSam Ravnborg 	else
40249192f26SSam Ravnborg 		fprintf(out, "CONFIG_%s=\"", name);
40349192f26SSam Ravnborg 
40449192f26SSam Ravnborg 	while (1) {
40549192f26SSam Ravnborg 		l = strcspn(str, "\"\\");
40649192f26SSam Ravnborg 		if (l) {
407bf5e327aSJean Sacren 			xfwrite(str, l, 1, out);
40849192f26SSam Ravnborg 			str += l;
40949192f26SSam Ravnborg 		}
41049192f26SSam Ravnborg 		if (!*str)
41149192f26SSam Ravnborg 			break;
41249192f26SSam Ravnborg 		fprintf(out, "\\%c", *str++);
41349192f26SSam Ravnborg 	}
41449192f26SSam Ravnborg 	fputs("\"\n", out);
41549192f26SSam Ravnborg }
41649192f26SSam Ravnborg 
41749192f26SSam Ravnborg static void conf_write_symbol(struct symbol *sym, enum symbol_type type,
41849192f26SSam Ravnborg                               FILE *out, bool write_no)
41949192f26SSam Ravnborg {
42049192f26SSam Ravnborg 	const char *str;
42149192f26SSam Ravnborg 
42249192f26SSam Ravnborg 	switch (type) {
42349192f26SSam Ravnborg 	case S_BOOLEAN:
42449192f26SSam Ravnborg 	case S_TRISTATE:
42549192f26SSam Ravnborg 		switch (sym_get_tristate_value(sym)) {
42649192f26SSam Ravnborg 		case no:
42749192f26SSam Ravnborg 			if (write_no)
42849192f26SSam Ravnborg 				fprintf(out, "# CONFIG_%s is not set\n", sym->name);
42949192f26SSam Ravnborg 			break;
43049192f26SSam Ravnborg 		case mod:
43149192f26SSam Ravnborg 			fprintf(out, "CONFIG_%s=m\n", sym->name);
43249192f26SSam Ravnborg 			break;
43349192f26SSam Ravnborg 		case yes:
43449192f26SSam Ravnborg 			fprintf(out, "CONFIG_%s=y\n", sym->name);
43549192f26SSam Ravnborg 			break;
43649192f26SSam Ravnborg 		}
43749192f26SSam Ravnborg 		break;
43849192f26SSam Ravnborg 	case S_STRING:
43949192f26SSam Ravnborg 		conf_write_string(false, sym->name, sym_get_string_value(sym), out);
44049192f26SSam Ravnborg 		break;
44149192f26SSam Ravnborg 	case S_HEX:
44249192f26SSam Ravnborg 	case S_INT:
44349192f26SSam Ravnborg 		str = sym_get_string_value(sym);
44449192f26SSam Ravnborg 		fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
44549192f26SSam Ravnborg 		break;
44649192f26SSam Ravnborg 	case S_OTHER:
44749192f26SSam Ravnborg 	case S_UNKNOWN:
44849192f26SSam Ravnborg 		break;
44949192f26SSam Ravnborg 	}
45049192f26SSam Ravnborg }
45149192f26SSam Ravnborg 
4527cf3d73bSSam Ravnborg /*
4537cf3d73bSSam Ravnborg  * Write out a minimal config.
4547cf3d73bSSam Ravnborg  * All values that has default values are skipped as this is redundant.
4557cf3d73bSSam Ravnborg  */
4567cf3d73bSSam Ravnborg int conf_write_defconfig(const char *filename)
4577cf3d73bSSam Ravnborg {
4587cf3d73bSSam Ravnborg 	struct symbol *sym;
4597cf3d73bSSam Ravnborg 	struct menu *menu;
4607cf3d73bSSam Ravnborg 	FILE *out;
4617cf3d73bSSam Ravnborg 
4627cf3d73bSSam Ravnborg 	out = fopen(filename, "w");
4637cf3d73bSSam Ravnborg 	if (!out)
4647cf3d73bSSam Ravnborg 		return 1;
4657cf3d73bSSam Ravnborg 
4667cf3d73bSSam Ravnborg 	sym_clear_all_valid();
4677cf3d73bSSam Ravnborg 
4687cf3d73bSSam Ravnborg 	/* Traverse all menus to find all relevant symbols */
4697cf3d73bSSam Ravnborg 	menu = rootmenu.list;
4707cf3d73bSSam Ravnborg 
4717cf3d73bSSam Ravnborg 	while (menu != NULL)
4727cf3d73bSSam Ravnborg 	{
4737cf3d73bSSam Ravnborg 		sym = menu->sym;
4747cf3d73bSSam Ravnborg 		if (sym == NULL) {
4757cf3d73bSSam Ravnborg 			if (!menu_is_visible(menu))
4767cf3d73bSSam Ravnborg 				goto next_menu;
4777cf3d73bSSam Ravnborg 		} else if (!sym_is_choice(sym)) {
4787cf3d73bSSam Ravnborg 			sym_calc_value(sym);
4797cf3d73bSSam Ravnborg 			if (!(sym->flags & SYMBOL_WRITE))
4807cf3d73bSSam Ravnborg 				goto next_menu;
4817cf3d73bSSam Ravnborg 			sym->flags &= ~SYMBOL_WRITE;
4827cf3d73bSSam Ravnborg 			/* If we cannot change the symbol - skip */
4837cf3d73bSSam Ravnborg 			if (!sym_is_changable(sym))
4847cf3d73bSSam Ravnborg 				goto next_menu;
4857cf3d73bSSam Ravnborg 			/* If symbol equals to default value - skip */
4867cf3d73bSSam Ravnborg 			if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
4877cf3d73bSSam Ravnborg 				goto next_menu;
4887cf3d73bSSam Ravnborg 
4897cf3d73bSSam Ravnborg 			/*
4907cf3d73bSSam Ravnborg 			 * If symbol is a choice value and equals to the
4917cf3d73bSSam Ravnborg 			 * default for a choice - skip.
49284062dd3SSam Ravnborg 			 * But only if value is bool and equal to "y" and
49384062dd3SSam Ravnborg 			 * choice is not "optional".
49484062dd3SSam Ravnborg 			 * (If choice is "optional" then all values can be "n")
4957cf3d73bSSam Ravnborg 			 */
4967cf3d73bSSam Ravnborg 			if (sym_is_choice_value(sym)) {
4977cf3d73bSSam Ravnborg 				struct symbol *cs;
4987cf3d73bSSam Ravnborg 				struct symbol *ds;
4997cf3d73bSSam Ravnborg 
5007cf3d73bSSam Ravnborg 				cs = prop_get_symbol(sym_get_choice_prop(sym));
5017cf3d73bSSam Ravnborg 				ds = sym_choice_default(cs);
50284062dd3SSam Ravnborg 				if (!sym_is_optional(cs) && sym == ds) {
503801690caSSam Ravnborg 					if ((sym->type == S_BOOLEAN) &&
5047cf3d73bSSam Ravnborg 					    sym_get_tristate_value(sym) == yes)
5057cf3d73bSSam Ravnborg 						goto next_menu;
5067cf3d73bSSam Ravnborg 				}
5077cf3d73bSSam Ravnborg 			}
5087cf3d73bSSam Ravnborg 			conf_write_symbol(sym, sym->type, out, true);
5097cf3d73bSSam Ravnborg 		}
5107cf3d73bSSam Ravnborg next_menu:
5117cf3d73bSSam Ravnborg 		if (menu->list != NULL) {
5127cf3d73bSSam Ravnborg 			menu = menu->list;
5137cf3d73bSSam Ravnborg 		}
5147cf3d73bSSam Ravnborg 		else if (menu->next != NULL) {
5157cf3d73bSSam Ravnborg 			menu = menu->next;
5167cf3d73bSSam Ravnborg 		} else {
5177cf3d73bSSam Ravnborg 			while ((menu = menu->parent)) {
5187cf3d73bSSam Ravnborg 				if (menu->next != NULL) {
5197cf3d73bSSam Ravnborg 					menu = menu->next;
5207cf3d73bSSam Ravnborg 					break;
5217cf3d73bSSam Ravnborg 				}
5227cf3d73bSSam Ravnborg 			}
5237cf3d73bSSam Ravnborg 		}
5247cf3d73bSSam Ravnborg 	}
5257cf3d73bSSam Ravnborg 	fclose(out);
5267cf3d73bSSam Ravnborg 	return 0;
5277cf3d73bSSam Ravnborg }
5287cf3d73bSSam Ravnborg 
5291da177e4SLinus Torvalds int conf_write(const char *name)
5301da177e4SLinus Torvalds {
531c955ccafSRoman Zippel 	FILE *out;
5321da177e4SLinus Torvalds 	struct symbol *sym;
5331da177e4SLinus Torvalds 	struct menu *menu;
5341da177e4SLinus Torvalds 	const char *basename;
5351da177e4SLinus Torvalds 	const char *str;
53649192f26SSam Ravnborg 	char dirname[128], tmpname[128], newname[128];
53749192f26SSam Ravnborg 	enum symbol_type type;
5381da177e4SLinus Torvalds 	time_t now;
5391da177e4SLinus Torvalds 	int use_timestamp = 1;
5401da177e4SLinus Torvalds 	char *env;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	dirname[0] = 0;
5431da177e4SLinus Torvalds 	if (name && name[0]) {
5441da177e4SLinus Torvalds 		struct stat st;
5451da177e4SLinus Torvalds 		char *slash;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 		if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
5481da177e4SLinus Torvalds 			strcpy(dirname, name);
5491da177e4SLinus Torvalds 			strcat(dirname, "/");
55014cdd3c4SRoman Zippel 			basename = conf_get_configname();
5511da177e4SLinus Torvalds 		} else if ((slash = strrchr(name, '/'))) {
5521da177e4SLinus Torvalds 			int size = slash - name + 1;
5531da177e4SLinus Torvalds 			memcpy(dirname, name, size);
5541da177e4SLinus Torvalds 			dirname[size] = 0;
5551da177e4SLinus Torvalds 			if (slash[1])
5561da177e4SLinus Torvalds 				basename = slash + 1;
5571da177e4SLinus Torvalds 			else
55814cdd3c4SRoman Zippel 				basename = conf_get_configname();
5591da177e4SLinus Torvalds 		} else
5601da177e4SLinus Torvalds 			basename = name;
5611da177e4SLinus Torvalds 	} else
56214cdd3c4SRoman Zippel 		basename = conf_get_configname();
5631da177e4SLinus Torvalds 
56414cdd3c4SRoman Zippel 	sprintf(newname, "%s%s", dirname, basename);
56514cdd3c4SRoman Zippel 	env = getenv("KCONFIG_OVERWRITECONFIG");
56614cdd3c4SRoman Zippel 	if (!env || !*env) {
56714cdd3c4SRoman Zippel 		sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
56814cdd3c4SRoman Zippel 		out = fopen(tmpname, "w");
56914cdd3c4SRoman Zippel 	} else {
57014cdd3c4SRoman Zippel 		*tmpname = 0;
5711da177e4SLinus Torvalds 		out = fopen(newname, "w");
57214cdd3c4SRoman Zippel 	}
5731da177e4SLinus Torvalds 	if (!out)
5741da177e4SLinus Torvalds 		return 1;
57514cdd3c4SRoman Zippel 
5762244cbd8SSam Ravnborg 	sym = sym_lookup("KERNELVERSION", 0);
5771da177e4SLinus Torvalds 	sym_calc_value(sym);
5781da177e4SLinus Torvalds 	time(&now);
5791da177e4SLinus Torvalds 	env = getenv("KCONFIG_NOTIMESTAMP");
5801da177e4SLinus Torvalds 	if (env && *env)
5811da177e4SLinus Torvalds 		use_timestamp = 0;
5821da177e4SLinus Torvalds 
5833b9fa093SArnaldo Carvalho de Melo 	fprintf(out, _("#\n"
5841da177e4SLinus Torvalds 		       "# Automatically generated make config: don't edit\n"
5851da177e4SLinus Torvalds 		       "# Linux kernel version: %s\n"
5861da177e4SLinus Torvalds 		       "%s%s"
5873b9fa093SArnaldo Carvalho de Melo 		       "#\n"),
5881da177e4SLinus Torvalds 		     sym_get_string_value(sym),
5891da177e4SLinus Torvalds 		     use_timestamp ? "# " : "",
5901da177e4SLinus Torvalds 		     use_timestamp ? ctime(&now) : "");
5911da177e4SLinus Torvalds 
592b3214293SKarsten Wiese 	if (!conf_get_changed())
5931da177e4SLinus Torvalds 		sym_clear_all_valid();
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	menu = rootmenu.list;
5961da177e4SLinus Torvalds 	while (menu) {
5971da177e4SLinus Torvalds 		sym = menu->sym;
5981da177e4SLinus Torvalds 		if (!sym) {
5991da177e4SLinus Torvalds 			if (!menu_is_visible(menu))
6001da177e4SLinus Torvalds 				goto next;
6011da177e4SLinus Torvalds 			str = menu_get_prompt(menu);
6021da177e4SLinus Torvalds 			fprintf(out, "\n"
6031da177e4SLinus Torvalds 				     "#\n"
6041da177e4SLinus Torvalds 				     "# %s\n"
6051da177e4SLinus Torvalds 				     "#\n", str);
6061da177e4SLinus Torvalds 		} else if (!(sym->flags & SYMBOL_CHOICE)) {
6071da177e4SLinus Torvalds 			sym_calc_value(sym);
6081da177e4SLinus Torvalds 			if (!(sym->flags & SYMBOL_WRITE))
6091da177e4SLinus Torvalds 				goto next;
6101da177e4SLinus Torvalds 			sym->flags &= ~SYMBOL_WRITE;
6111da177e4SLinus Torvalds 			type = sym->type;
6121da177e4SLinus Torvalds 			if (type == S_TRISTATE) {
6131da177e4SLinus Torvalds 				sym_calc_value(modules_sym);
6141da177e4SLinus Torvalds 				if (modules_sym->curr.tri == no)
6151da177e4SLinus Torvalds 					type = S_BOOLEAN;
6161da177e4SLinus Torvalds 			}
61749192f26SSam Ravnborg 			/* Write config symbol to file */
61849192f26SSam Ravnborg 			conf_write_symbol(sym, type, out, true);
6191da177e4SLinus Torvalds 		}
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds next:
6221da177e4SLinus Torvalds 		if (menu->list) {
6231da177e4SLinus Torvalds 			menu = menu->list;
6241da177e4SLinus Torvalds 			continue;
6251da177e4SLinus Torvalds 		}
6261da177e4SLinus Torvalds 		if (menu->next)
6271da177e4SLinus Torvalds 			menu = menu->next;
6281da177e4SLinus Torvalds 		else while ((menu = menu->parent)) {
6291da177e4SLinus Torvalds 			if (menu->next) {
6301da177e4SLinus Torvalds 				menu = menu->next;
6311da177e4SLinus Torvalds 				break;
6321da177e4SLinus Torvalds 			}
6331da177e4SLinus Torvalds 		}
6341da177e4SLinus Torvalds 	}
6351da177e4SLinus Torvalds 	fclose(out);
63614cdd3c4SRoman Zippel 
63714cdd3c4SRoman Zippel 	if (*tmpname) {
6389a3d0fe8SSam Ravnborg 		strcat(dirname, basename);
63914cdd3c4SRoman Zippel 		strcat(dirname, ".old");
64014cdd3c4SRoman Zippel 		rename(newname, dirname);
64114cdd3c4SRoman Zippel 		if (rename(tmpname, newname))
6421da177e4SLinus Torvalds 			return 1;
64314cdd3c4SRoman Zippel 	}
6441da177e4SLinus Torvalds 
645ddc97cacSRoman Zippel 	printf(_("#\n"
646ddc97cacSRoman Zippel 		 "# configuration written to %s\n"
64714cdd3c4SRoman Zippel 		 "#\n"), newname);
648ddc97cacSRoman Zippel 
649bfc10001SKarsten Wiese 	sym_set_change_count(0);
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	return 0;
6521da177e4SLinus Torvalds }
653c955ccafSRoman Zippel 
6544356f489STrevor Keith static int conf_split_config(void)
6552e3646e5SRoman Zippel {
65612122f62SMarkus Heidelberg 	const char *name;
65712122f62SMarkus Heidelberg 	char path[128];
6582e3646e5SRoman Zippel 	char *s, *d, c;
6592e3646e5SRoman Zippel 	struct symbol *sym;
6602e3646e5SRoman Zippel 	struct stat sb;
6612e3646e5SRoman Zippel 	int res, i, fd;
6622e3646e5SRoman Zippel 
66312122f62SMarkus Heidelberg 	name = conf_get_autoconfig_name();
6642e3646e5SRoman Zippel 	conf_read_simple(name, S_DEF_AUTO);
6652e3646e5SRoman Zippel 
6662e3646e5SRoman Zippel 	if (chdir("include/config"))
6672e3646e5SRoman Zippel 		return 1;
6682e3646e5SRoman Zippel 
6692e3646e5SRoman Zippel 	res = 0;
6702e3646e5SRoman Zippel 	for_all_symbols(i, sym) {
6712e3646e5SRoman Zippel 		sym_calc_value(sym);
6722e3646e5SRoman Zippel 		if ((sym->flags & SYMBOL_AUTO) || !sym->name)
6732e3646e5SRoman Zippel 			continue;
6742e3646e5SRoman Zippel 		if (sym->flags & SYMBOL_WRITE) {
6752e3646e5SRoman Zippel 			if (sym->flags & SYMBOL_DEF_AUTO) {
6762e3646e5SRoman Zippel 				/*
6772e3646e5SRoman Zippel 				 * symbol has old and new value,
6782e3646e5SRoman Zippel 				 * so compare them...
6792e3646e5SRoman Zippel 				 */
6802e3646e5SRoman Zippel 				switch (sym->type) {
6812e3646e5SRoman Zippel 				case S_BOOLEAN:
6822e3646e5SRoman Zippel 				case S_TRISTATE:
6832e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) ==
6842e3646e5SRoman Zippel 					    sym->def[S_DEF_AUTO].tri)
6852e3646e5SRoman Zippel 						continue;
6862e3646e5SRoman Zippel 					break;
6872e3646e5SRoman Zippel 				case S_STRING:
6882e3646e5SRoman Zippel 				case S_HEX:
6892e3646e5SRoman Zippel 				case S_INT:
6902e3646e5SRoman Zippel 					if (!strcmp(sym_get_string_value(sym),
6912e3646e5SRoman Zippel 						    sym->def[S_DEF_AUTO].val))
6922e3646e5SRoman Zippel 						continue;
6932e3646e5SRoman Zippel 					break;
6942e3646e5SRoman Zippel 				default:
6952e3646e5SRoman Zippel 					break;
6962e3646e5SRoman Zippel 				}
6972e3646e5SRoman Zippel 			} else {
6982e3646e5SRoman Zippel 				/*
6992e3646e5SRoman Zippel 				 * If there is no old value, only 'no' (unset)
7002e3646e5SRoman Zippel 				 * is allowed as new value.
7012e3646e5SRoman Zippel 				 */
7022e3646e5SRoman Zippel 				switch (sym->type) {
7032e3646e5SRoman Zippel 				case S_BOOLEAN:
7042e3646e5SRoman Zippel 				case S_TRISTATE:
7052e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) == no)
7062e3646e5SRoman Zippel 						continue;
7072e3646e5SRoman Zippel 					break;
7082e3646e5SRoman Zippel 				default:
7092e3646e5SRoman Zippel 					break;
7102e3646e5SRoman Zippel 				}
7112e3646e5SRoman Zippel 			}
7122e3646e5SRoman Zippel 		} else if (!(sym->flags & SYMBOL_DEF_AUTO))
7132e3646e5SRoman Zippel 			/* There is neither an old nor a new value. */
7142e3646e5SRoman Zippel 			continue;
7152e3646e5SRoman Zippel 		/* else
7162e3646e5SRoman Zippel 		 *	There is an old value, but no new value ('no' (unset)
7172e3646e5SRoman Zippel 		 *	isn't saved in auto.conf, so the old value is always
7182e3646e5SRoman Zippel 		 *	different from 'no').
7192e3646e5SRoman Zippel 		 */
7202e3646e5SRoman Zippel 
7212e3646e5SRoman Zippel 		/* Replace all '_' and append ".h" */
7222e3646e5SRoman Zippel 		s = sym->name;
7232e3646e5SRoman Zippel 		d = path;
7242e3646e5SRoman Zippel 		while ((c = *s++)) {
7252e3646e5SRoman Zippel 			c = tolower(c);
7262e3646e5SRoman Zippel 			*d++ = (c == '_') ? '/' : c;
7272e3646e5SRoman Zippel 		}
7282e3646e5SRoman Zippel 		strcpy(d, ".h");
7292e3646e5SRoman Zippel 
7302e3646e5SRoman Zippel 		/* Assume directory path already exists. */
7312e3646e5SRoman Zippel 		fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
7322e3646e5SRoman Zippel 		if (fd == -1) {
7332e3646e5SRoman Zippel 			if (errno != ENOENT) {
7342e3646e5SRoman Zippel 				res = 1;
7352e3646e5SRoman Zippel 				break;
7362e3646e5SRoman Zippel 			}
7372e3646e5SRoman Zippel 			/*
7382e3646e5SRoman Zippel 			 * Create directory components,
7392e3646e5SRoman Zippel 			 * unless they exist already.
7402e3646e5SRoman Zippel 			 */
7412e3646e5SRoman Zippel 			d = path;
7422e3646e5SRoman Zippel 			while ((d = strchr(d, '/'))) {
7432e3646e5SRoman Zippel 				*d = 0;
7442e3646e5SRoman Zippel 				if (stat(path, &sb) && mkdir(path, 0755)) {
7452e3646e5SRoman Zippel 					res = 1;
7462e3646e5SRoman Zippel 					goto out;
7472e3646e5SRoman Zippel 				}
7482e3646e5SRoman Zippel 				*d++ = '/';
7492e3646e5SRoman Zippel 			}
7502e3646e5SRoman Zippel 			/* Try it again. */
7512e3646e5SRoman Zippel 			fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
7522e3646e5SRoman Zippel 			if (fd == -1) {
7532e3646e5SRoman Zippel 				res = 1;
7542e3646e5SRoman Zippel 				break;
7552e3646e5SRoman Zippel 			}
7562e3646e5SRoman Zippel 		}
7572e3646e5SRoman Zippel 		close(fd);
7582e3646e5SRoman Zippel 	}
7592e3646e5SRoman Zippel out:
7602e3646e5SRoman Zippel 	if (chdir("../.."))
7612e3646e5SRoman Zippel 		return 1;
7622e3646e5SRoman Zippel 
7632e3646e5SRoman Zippel 	return res;
7642e3646e5SRoman Zippel }
7652e3646e5SRoman Zippel 
766c955ccafSRoman Zippel int conf_write_autoconf(void)
767c955ccafSRoman Zippel {
768c955ccafSRoman Zippel 	struct symbol *sym;
769c955ccafSRoman Zippel 	const char *str;
77012122f62SMarkus Heidelberg 	const char *name;
771bc081dd6SMichal Marek 	FILE *out, *tristate, *out_h;
772c955ccafSRoman Zippel 	time_t now;
77349192f26SSam Ravnborg 	int i;
774c955ccafSRoman Zippel 
7752e3646e5SRoman Zippel 	sym_clear_all_valid();
7762e3646e5SRoman Zippel 
777c955ccafSRoman Zippel 	file_write_dep("include/config/auto.conf.cmd");
778c955ccafSRoman Zippel 
7792e3646e5SRoman Zippel 	if (conf_split_config())
7802e3646e5SRoman Zippel 		return 1;
7812e3646e5SRoman Zippel 
782c955ccafSRoman Zippel 	out = fopen(".tmpconfig", "w");
783c955ccafSRoman Zippel 	if (!out)
784c955ccafSRoman Zippel 		return 1;
785c955ccafSRoman Zippel 
786bc081dd6SMichal Marek 	tristate = fopen(".tmpconfig_tristate", "w");
787bc081dd6SMichal Marek 	if (!tristate) {
788bc081dd6SMichal Marek 		fclose(out);
789bc081dd6SMichal Marek 		return 1;
790bc081dd6SMichal Marek 	}
791bc081dd6SMichal Marek 
792c955ccafSRoman Zippel 	out_h = fopen(".tmpconfig.h", "w");
793c955ccafSRoman Zippel 	if (!out_h) {
794c955ccafSRoman Zippel 		fclose(out);
795bc081dd6SMichal Marek 		fclose(tristate);
796c955ccafSRoman Zippel 		return 1;
797c955ccafSRoman Zippel 	}
798c955ccafSRoman Zippel 
799c955ccafSRoman Zippel 	sym = sym_lookup("KERNELVERSION", 0);
800c955ccafSRoman Zippel 	sym_calc_value(sym);
801c955ccafSRoman Zippel 	time(&now);
802c955ccafSRoman Zippel 	fprintf(out, "#\n"
803c955ccafSRoman Zippel 		     "# Automatically generated make config: don't edit\n"
804c955ccafSRoman Zippel 		     "# Linux kernel version: %s\n"
805c955ccafSRoman Zippel 		     "# %s"
806c955ccafSRoman Zippel 		     "#\n",
807c955ccafSRoman Zippel 		     sym_get_string_value(sym), ctime(&now));
808bc081dd6SMichal Marek 	fprintf(tristate, "#\n"
809bc081dd6SMichal Marek 			  "# Automatically generated - do not edit\n"
810bc081dd6SMichal Marek 			  "\n");
811c955ccafSRoman Zippel 	fprintf(out_h, "/*\n"
812c955ccafSRoman Zippel 		       " * Automatically generated C config: don't edit\n"
813c955ccafSRoman Zippel 		       " * Linux kernel version: %s\n"
814c955ccafSRoman Zippel 		       " * %s"
815c955ccafSRoman Zippel 		       " */\n"
816c955ccafSRoman Zippel 		       "#define AUTOCONF_INCLUDED\n",
817c955ccafSRoman Zippel 		       sym_get_string_value(sym), ctime(&now));
818c955ccafSRoman Zippel 
819c955ccafSRoman Zippel 	for_all_symbols(i, sym) {
820c955ccafSRoman Zippel 		sym_calc_value(sym);
821c955ccafSRoman Zippel 		if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
822c955ccafSRoman Zippel 			continue;
82349192f26SSam Ravnborg 
82449192f26SSam Ravnborg 		/* write symbol to config file */
82549192f26SSam Ravnborg 		conf_write_symbol(sym, sym->type, out, false);
82649192f26SSam Ravnborg 
82749192f26SSam Ravnborg 		/* update autoconf and tristate files */
828c955ccafSRoman Zippel 		switch (sym->type) {
829c955ccafSRoman Zippel 		case S_BOOLEAN:
830c955ccafSRoman Zippel 		case S_TRISTATE:
831c955ccafSRoman Zippel 			switch (sym_get_tristate_value(sym)) {
832c955ccafSRoman Zippel 			case no:
833c955ccafSRoman Zippel 				break;
834c955ccafSRoman Zippel 			case mod:
835bc081dd6SMichal Marek 				fprintf(tristate, "CONFIG_%s=M\n", sym->name);
836c955ccafSRoman Zippel 				fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
837c955ccafSRoman Zippel 				break;
838c955ccafSRoman Zippel 			case yes:
839bc081dd6SMichal Marek 				if (sym->type == S_TRISTATE)
840bc081dd6SMichal Marek 					fprintf(tristate, "CONFIG_%s=Y\n",
841bc081dd6SMichal Marek 							sym->name);
842c955ccafSRoman Zippel 				fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
843c955ccafSRoman Zippel 				break;
844c955ccafSRoman Zippel 			}
845c955ccafSRoman Zippel 			break;
846c955ccafSRoman Zippel 		case S_STRING:
84749192f26SSam Ravnborg 			conf_write_string(true, sym->name, sym_get_string_value(sym), out_h);
848c955ccafSRoman Zippel 			break;
849c955ccafSRoman Zippel 		case S_HEX:
850c955ccafSRoman Zippel 			str = sym_get_string_value(sym);
851c955ccafSRoman Zippel 			if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
852c955ccafSRoman Zippel 				fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
853c955ccafSRoman Zippel 				break;
854c955ccafSRoman Zippel 			}
855c955ccafSRoman Zippel 		case S_INT:
856c955ccafSRoman Zippel 			str = sym_get_string_value(sym);
857c955ccafSRoman Zippel 			fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
858c955ccafSRoman Zippel 			break;
859c955ccafSRoman Zippel 		default:
860c955ccafSRoman Zippel 			break;
861c955ccafSRoman Zippel 		}
862c955ccafSRoman Zippel 	}
863c955ccafSRoman Zippel 	fclose(out);
864bc081dd6SMichal Marek 	fclose(tristate);
865c955ccafSRoman Zippel 	fclose(out_h);
866c955ccafSRoman Zippel 
867c955ccafSRoman Zippel 	name = getenv("KCONFIG_AUTOHEADER");
868c955ccafSRoman Zippel 	if (!name)
869264a2683SSam Ravnborg 		name = "include/generated/autoconf.h";
870c955ccafSRoman Zippel 	if (rename(".tmpconfig.h", name))
871c955ccafSRoman Zippel 		return 1;
872bc081dd6SMichal Marek 	name = getenv("KCONFIG_TRISTATE");
873bc081dd6SMichal Marek 	if (!name)
874bc081dd6SMichal Marek 		name = "include/config/tristate.conf";
875bc081dd6SMichal Marek 	if (rename(".tmpconfig_tristate", name))
876bc081dd6SMichal Marek 		return 1;
87712122f62SMarkus Heidelberg 	name = conf_get_autoconfig_name();
878c955ccafSRoman Zippel 	/*
879c955ccafSRoman Zippel 	 * This must be the last step, kbuild has a dependency on auto.conf
880c955ccafSRoman Zippel 	 * and this marks the successful completion of the previous steps.
881c955ccafSRoman Zippel 	 */
882c955ccafSRoman Zippel 	if (rename(".tmpconfig", name))
883c955ccafSRoman Zippel 		return 1;
884c955ccafSRoman Zippel 
885c955ccafSRoman Zippel 	return 0;
886c955ccafSRoman Zippel }
887b3214293SKarsten Wiese 
888bfc10001SKarsten Wiese static int sym_change_count;
8893b354c55SKarsten Wiese static void (*conf_changed_callback)(void);
890bfc10001SKarsten Wiese 
891bfc10001SKarsten Wiese void sym_set_change_count(int count)
892bfc10001SKarsten Wiese {
8933b354c55SKarsten Wiese 	int _sym_change_count = sym_change_count;
894bfc10001SKarsten Wiese 	sym_change_count = count;
8953b354c55SKarsten Wiese 	if (conf_changed_callback &&
8963b354c55SKarsten Wiese 	    (bool)_sym_change_count != (bool)count)
8973b354c55SKarsten Wiese 		conf_changed_callback();
898bfc10001SKarsten Wiese }
899bfc10001SKarsten Wiese 
900bfc10001SKarsten Wiese void sym_add_change_count(int count)
901bfc10001SKarsten Wiese {
9023b354c55SKarsten Wiese 	sym_set_change_count(count + sym_change_count);
903bfc10001SKarsten Wiese }
904bfc10001SKarsten Wiese 
905b3214293SKarsten Wiese bool conf_get_changed(void)
906b3214293SKarsten Wiese {
907b3214293SKarsten Wiese 	return sym_change_count;
908b3214293SKarsten Wiese }
9093b354c55SKarsten Wiese 
9103b354c55SKarsten Wiese void conf_set_changed_callback(void (*fn)(void))
9113b354c55SKarsten Wiese {
9123b354c55SKarsten Wiese 	conf_changed_callback = fn;
9133b354c55SKarsten Wiese }
914dc7862e5SRoman Zippel 
915a64b44eaSSam Ravnborg static void randomize_choice_values(struct symbol *csym)
916a64b44eaSSam Ravnborg {
917a64b44eaSSam Ravnborg 	struct property *prop;
918a64b44eaSSam Ravnborg 	struct symbol *sym;
919a64b44eaSSam Ravnborg 	struct expr *e;
920a64b44eaSSam Ravnborg 	int cnt, def;
921a64b44eaSSam Ravnborg 
922a64b44eaSSam Ravnborg 	/*
923a64b44eaSSam Ravnborg 	 * If choice is mod then we may have more items slected
924a64b44eaSSam Ravnborg 	 * and if no then no-one.
925a64b44eaSSam Ravnborg 	 * In both cases stop.
926a64b44eaSSam Ravnborg 	 */
927a64b44eaSSam Ravnborg 	if (csym->curr.tri != yes)
928a64b44eaSSam Ravnborg 		return;
929a64b44eaSSam Ravnborg 
930a64b44eaSSam Ravnborg 	prop = sym_get_choice_prop(csym);
931a64b44eaSSam Ravnborg 
932a64b44eaSSam Ravnborg 	/* count entries in choice block */
933a64b44eaSSam Ravnborg 	cnt = 0;
934a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym)
935a64b44eaSSam Ravnborg 		cnt++;
936a64b44eaSSam Ravnborg 
937a64b44eaSSam Ravnborg 	/*
938a64b44eaSSam Ravnborg 	 * find a random value and set it to yes,
939a64b44eaSSam Ravnborg 	 * set the rest to no so we have only one set
940a64b44eaSSam Ravnborg 	 */
941a64b44eaSSam Ravnborg 	def = (rand() % cnt);
942a64b44eaSSam Ravnborg 
943a64b44eaSSam Ravnborg 	cnt = 0;
944a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym) {
945a64b44eaSSam Ravnborg 		if (def == cnt++) {
946a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = yes;
947a64b44eaSSam Ravnborg 			csym->def[S_DEF_USER].val = sym;
948a64b44eaSSam Ravnborg 		}
949a64b44eaSSam Ravnborg 		else {
950a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = no;
951a64b44eaSSam Ravnborg 		}
952a64b44eaSSam Ravnborg 	}
953a64b44eaSSam Ravnborg 	csym->flags |= SYMBOL_DEF_USER;
954a64b44eaSSam Ravnborg 	/* clear VALID to get value calculated */
955a64b44eaSSam Ravnborg 	csym->flags &= ~(SYMBOL_VALID);
956a64b44eaSSam Ravnborg }
957a64b44eaSSam Ravnborg 
958a64b44eaSSam Ravnborg static void set_all_choice_values(struct symbol *csym)
959a64b44eaSSam Ravnborg {
960a64b44eaSSam Ravnborg 	struct property *prop;
961a64b44eaSSam Ravnborg 	struct symbol *sym;
962a64b44eaSSam Ravnborg 	struct expr *e;
963a64b44eaSSam Ravnborg 
964a64b44eaSSam Ravnborg 	prop = sym_get_choice_prop(csym);
965a64b44eaSSam Ravnborg 
966a64b44eaSSam Ravnborg 	/*
967a64b44eaSSam Ravnborg 	 * Set all non-assinged choice values to no
968a64b44eaSSam Ravnborg 	 */
969a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym) {
970a64b44eaSSam Ravnborg 		if (!sym_has_value(sym))
971a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = no;
972a64b44eaSSam Ravnborg 	}
973a64b44eaSSam Ravnborg 	csym->flags |= SYMBOL_DEF_USER;
974a64b44eaSSam Ravnborg 	/* clear VALID to get value calculated */
975a64b44eaSSam Ravnborg 	csym->flags &= ~(SYMBOL_VALID);
976a64b44eaSSam Ravnborg }
977dc7862e5SRoman Zippel 
978dc7862e5SRoman Zippel void conf_set_all_new_symbols(enum conf_def_mode mode)
979dc7862e5SRoman Zippel {
980dc7862e5SRoman Zippel 	struct symbol *sym, *csym;
981a64b44eaSSam Ravnborg 	int i, cnt;
982dc7862e5SRoman Zippel 
983dc7862e5SRoman Zippel 	for_all_symbols(i, sym) {
984dc7862e5SRoman Zippel 		if (sym_has_value(sym))
985dc7862e5SRoman Zippel 			continue;
986dc7862e5SRoman Zippel 		switch (sym_get_type(sym)) {
987dc7862e5SRoman Zippel 		case S_BOOLEAN:
988dc7862e5SRoman Zippel 		case S_TRISTATE:
989dc7862e5SRoman Zippel 			switch (mode) {
990dc7862e5SRoman Zippel 			case def_yes:
991dc7862e5SRoman Zippel 				sym->def[S_DEF_USER].tri = yes;
992dc7862e5SRoman Zippel 				break;
993dc7862e5SRoman Zippel 			case def_mod:
994dc7862e5SRoman Zippel 				sym->def[S_DEF_USER].tri = mod;
995dc7862e5SRoman Zippel 				break;
996dc7862e5SRoman Zippel 			case def_no:
997dc7862e5SRoman Zippel 				sym->def[S_DEF_USER].tri = no;
998dc7862e5SRoman Zippel 				break;
999dc7862e5SRoman Zippel 			case def_random:
10001244b41dSPeter Korsgaard 				cnt = sym_get_type(sym) == S_TRISTATE ? 3 : 2;
10011244b41dSPeter Korsgaard 				sym->def[S_DEF_USER].tri = (tristate)(rand() % cnt);
1002dc7862e5SRoman Zippel 				break;
1003dc7862e5SRoman Zippel 			default:
1004dc7862e5SRoman Zippel 				continue;
1005dc7862e5SRoman Zippel 			}
1006184832c9SSam Ravnborg 			if (!(sym_is_choice(sym) && mode == def_random))
1007dc7862e5SRoman Zippel 				sym->flags |= SYMBOL_DEF_USER;
1008dc7862e5SRoman Zippel 			break;
1009dc7862e5SRoman Zippel 		default:
1010dc7862e5SRoman Zippel 			break;
1011dc7862e5SRoman Zippel 		}
1012dc7862e5SRoman Zippel 
1013dc7862e5SRoman Zippel 	}
1014dc7862e5SRoman Zippel 
1015ce97e13eSAl Viro 	sym_clear_all_valid();
1016dc7862e5SRoman Zippel 
1017184832c9SSam Ravnborg 	/*
1018184832c9SSam Ravnborg 	 * We have different type of choice blocks.
1019184832c9SSam Ravnborg 	 * If curr.tri equal to mod then we can select several
1020184832c9SSam Ravnborg 	 * choice symbols in one block.
1021184832c9SSam Ravnborg 	 * In this case we do nothing.
1022184832c9SSam Ravnborg 	 * If curr.tri equal yes then only one symbol can be
1023184832c9SSam Ravnborg 	 * selected in a choice block and we set it to yes,
1024184832c9SSam Ravnborg 	 * and the rest to no.
1025184832c9SSam Ravnborg 	 */
1026dc7862e5SRoman Zippel 	for_all_symbols(i, csym) {
1027dc7862e5SRoman Zippel 		if (sym_has_value(csym) || !sym_is_choice(csym))
1028dc7862e5SRoman Zippel 			continue;
1029dc7862e5SRoman Zippel 
1030dc7862e5SRoman Zippel 		sym_calc_value(csym);
1031a64b44eaSSam Ravnborg 		if (mode == def_random)
1032a64b44eaSSam Ravnborg 			randomize_choice_values(csym);
1033a64b44eaSSam Ravnborg 		else
1034a64b44eaSSam Ravnborg 			set_all_choice_values(csym);
1035dc7862e5SRoman Zippel 	}
1036dc7862e5SRoman Zippel }
1037