xref: /openbmc/linux/scripts/kconfig/confdata.c (revision 2e3646e51b2d6415549b310655df63e7e0d7a080)
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>
8*2e3646e5SRoman 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_def_filename[] = ".config";
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds const char conf_defname[] = "arch/$ARCH/defconfig";
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds const char *conf_confnames[] = {
291da177e4SLinus Torvalds 	".config",
301da177e4SLinus Torvalds 	"/lib/modules/$UNAME_RELEASE/.config",
311da177e4SLinus Torvalds 	"/etc/kernel-config",
321da177e4SLinus Torvalds 	"/boot/config-$UNAME_RELEASE",
331da177e4SLinus Torvalds 	conf_defname,
341da177e4SLinus Torvalds 	NULL,
351da177e4SLinus Torvalds };
361da177e4SLinus Torvalds 
37c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
38c1a0f5e3SRoman Zippel {
39c1a0f5e3SRoman Zippel 	va_list ap;
40c1a0f5e3SRoman Zippel 	va_start(ap, fmt);
41c1a0f5e3SRoman Zippel 	fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
42c1a0f5e3SRoman Zippel 	vfprintf(stderr, fmt, ap);
43c1a0f5e3SRoman Zippel 	fprintf(stderr, "\n");
44c1a0f5e3SRoman Zippel 	va_end(ap);
45c1a0f5e3SRoman Zippel 	conf_warnings++;
46c1a0f5e3SRoman Zippel }
47c1a0f5e3SRoman Zippel 
4848b9d03cSJ.A. Magallon static char *conf_expand_value(const char *in)
491da177e4SLinus Torvalds {
501da177e4SLinus Torvalds 	struct symbol *sym;
5148b9d03cSJ.A. Magallon 	const char *src;
521da177e4SLinus Torvalds 	static char res_value[SYMBOL_MAXLENGTH];
531da177e4SLinus Torvalds 	char *dst, name[SYMBOL_MAXLENGTH];
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	res_value[0] = 0;
561da177e4SLinus Torvalds 	dst = name;
571da177e4SLinus Torvalds 	while ((src = strchr(in, '$'))) {
581da177e4SLinus Torvalds 		strncat(res_value, in, src - in);
591da177e4SLinus Torvalds 		src++;
601da177e4SLinus Torvalds 		dst = name;
611da177e4SLinus Torvalds 		while (isalnum(*src) || *src == '_')
621da177e4SLinus Torvalds 			*dst++ = *src++;
631da177e4SLinus Torvalds 		*dst = 0;
641da177e4SLinus Torvalds 		sym = sym_lookup(name, 0);
651da177e4SLinus Torvalds 		sym_calc_value(sym);
661da177e4SLinus Torvalds 		strcat(res_value, sym_get_string_value(sym));
671da177e4SLinus Torvalds 		in = src;
681da177e4SLinus Torvalds 	}
691da177e4SLinus Torvalds 	strcat(res_value, in);
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	return res_value;
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds char *conf_get_default_confname(void)
751da177e4SLinus Torvalds {
761da177e4SLinus Torvalds 	struct stat buf;
771da177e4SLinus Torvalds 	static char fullname[PATH_MAX+1];
781da177e4SLinus Torvalds 	char *env, *name;
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	name = conf_expand_value(conf_defname);
811da177e4SLinus Torvalds 	env = getenv(SRCTREE);
821da177e4SLinus Torvalds 	if (env) {
831da177e4SLinus Torvalds 		sprintf(fullname, "%s/%s", env, name);
841da177e4SLinus Torvalds 		if (!stat(fullname, &buf))
851da177e4SLinus Torvalds 			return fullname;
861da177e4SLinus Torvalds 	}
871da177e4SLinus Torvalds 	return name;
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds 
90669bfad9SRoman Zippel int conf_read_simple(const char *name, int def)
911da177e4SLinus Torvalds {
921da177e4SLinus Torvalds 	FILE *in = NULL;
931da177e4SLinus Torvalds 	char line[1024];
941da177e4SLinus Torvalds 	char *p, *p2;
951da177e4SLinus Torvalds 	struct symbol *sym;
96669bfad9SRoman Zippel 	int i, def_flags;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	if (name) {
991da177e4SLinus Torvalds 		in = zconf_fopen(name);
1001da177e4SLinus Torvalds 	} else {
1011da177e4SLinus Torvalds 		const char **names = conf_confnames;
102ddc97cacSRoman Zippel 		name = *names++;
103ddc97cacSRoman Zippel 		if (!name)
104ddc97cacSRoman Zippel 			return 1;
105ddc97cacSRoman Zippel 		in = zconf_fopen(name);
106ddc97cacSRoman Zippel 		if (in)
107ddc97cacSRoman Zippel 			goto load;
108ddc97cacSRoman Zippel 		sym_change_count++;
1091da177e4SLinus Torvalds 		while ((name = *names++)) {
1101da177e4SLinus Torvalds 			name = conf_expand_value(name);
1111da177e4SLinus Torvalds 			in = zconf_fopen(name);
1121da177e4SLinus Torvalds 			if (in) {
1133b9fa093SArnaldo Carvalho de Melo 				printf(_("#\n"
1141da177e4SLinus Torvalds 					 "# using defaults found in %s\n"
1153b9fa093SArnaldo Carvalho de Melo 					 "#\n"), name);
116ddc97cacSRoman Zippel 				goto load;
1171da177e4SLinus Torvalds 			}
1181da177e4SLinus Torvalds 		}
1191da177e4SLinus Torvalds 	}
1201da177e4SLinus Torvalds 	if (!in)
1211da177e4SLinus Torvalds 		return 1;
1221da177e4SLinus Torvalds 
123ddc97cacSRoman Zippel load:
124c1a0f5e3SRoman Zippel 	conf_filename = name;
125c1a0f5e3SRoman Zippel 	conf_lineno = 0;
126c1a0f5e3SRoman Zippel 	conf_warnings = 0;
127c1a0f5e3SRoman Zippel 	conf_unsaved = 0;
128c1a0f5e3SRoman Zippel 
129669bfad9SRoman Zippel 	def_flags = SYMBOL_DEF << def;
1301da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
131669bfad9SRoman Zippel 		sym->flags |= SYMBOL_CHANGED;
132669bfad9SRoman Zippel 		sym->flags &= ~(def_flags|SYMBOL_VALID);
133c1a0f5e3SRoman Zippel 		if (sym_is_choice(sym))
134669bfad9SRoman Zippel 			sym->flags |= def_flags;
1351da177e4SLinus Torvalds 		switch (sym->type) {
1361da177e4SLinus Torvalds 		case S_INT:
1371da177e4SLinus Torvalds 		case S_HEX:
1381da177e4SLinus Torvalds 		case S_STRING:
139669bfad9SRoman Zippel 			if (sym->def[def].val)
140669bfad9SRoman Zippel 				free(sym->def[def].val);
1411da177e4SLinus Torvalds 		default:
142669bfad9SRoman Zippel 			sym->def[def].val = NULL;
143669bfad9SRoman Zippel 			sym->def[def].tri = no;
1441da177e4SLinus Torvalds 		}
1451da177e4SLinus Torvalds 	}
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	while (fgets(line, sizeof(line), in)) {
148c1a0f5e3SRoman Zippel 		conf_lineno++;
1491da177e4SLinus Torvalds 		sym = NULL;
1501da177e4SLinus Torvalds 		switch (line[0]) {
1511da177e4SLinus Torvalds 		case '#':
1521da177e4SLinus Torvalds 			if (memcmp(line + 2, "CONFIG_", 7))
1531da177e4SLinus Torvalds 				continue;
1541da177e4SLinus Torvalds 			p = strchr(line + 9, ' ');
1551da177e4SLinus Torvalds 			if (!p)
1561da177e4SLinus Torvalds 				continue;
1571da177e4SLinus Torvalds 			*p++ = 0;
1581da177e4SLinus Torvalds 			if (strncmp(p, "is not set", 10))
1591da177e4SLinus Torvalds 				continue;
160669bfad9SRoman Zippel 			if (def == S_DEF_USER) {
1611da177e4SLinus Torvalds 				sym = sym_find(line + 9);
1621da177e4SLinus Torvalds 				if (!sym) {
163c1a0f5e3SRoman Zippel 					conf_warning("trying to assign nonexistent symbol %s", line + 9);
164c1a0f5e3SRoman Zippel 					break;
165669bfad9SRoman Zippel 				}
166669bfad9SRoman Zippel 			} else {
167669bfad9SRoman Zippel 				sym = sym_lookup(line + 9, 0);
168669bfad9SRoman Zippel 				if (sym->type == S_UNKNOWN)
169669bfad9SRoman Zippel 					sym->type = S_BOOLEAN;
170669bfad9SRoman Zippel 			}
171669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
172c1a0f5e3SRoman Zippel 				conf_warning("trying to reassign symbol %s", sym->name);
1731da177e4SLinus Torvalds 				break;
1741da177e4SLinus Torvalds 			}
1751da177e4SLinus Torvalds 			switch (sym->type) {
1761da177e4SLinus Torvalds 			case S_BOOLEAN:
1771da177e4SLinus Torvalds 			case S_TRISTATE:
178669bfad9SRoman Zippel 				sym->def[def].tri = no;
179669bfad9SRoman Zippel 				sym->flags |= def_flags;
1801da177e4SLinus Torvalds 				break;
1811da177e4SLinus Torvalds 			default:
1821da177e4SLinus Torvalds 				;
1831da177e4SLinus Torvalds 			}
1841da177e4SLinus Torvalds 			break;
1851da177e4SLinus Torvalds 		case 'C':
186c1a0f5e3SRoman Zippel 			if (memcmp(line, "CONFIG_", 7)) {
187c1a0f5e3SRoman Zippel 				conf_warning("unexpected data");
1881da177e4SLinus Torvalds 				continue;
189c1a0f5e3SRoman Zippel 			}
1901da177e4SLinus Torvalds 			p = strchr(line + 7, '=');
1911da177e4SLinus Torvalds 			if (!p)
1921da177e4SLinus Torvalds 				continue;
1931da177e4SLinus Torvalds 			*p++ = 0;
1941da177e4SLinus Torvalds 			p2 = strchr(p, '\n');
1951da177e4SLinus Torvalds 			if (p2)
1961da177e4SLinus Torvalds 				*p2 = 0;
197669bfad9SRoman Zippel 			if (def == S_DEF_USER) {
1981da177e4SLinus Torvalds 				sym = sym_find(line + 7);
1991da177e4SLinus Torvalds 				if (!sym) {
200c1a0f5e3SRoman Zippel 					conf_warning("trying to assign nonexistent symbol %s", line + 7);
201c1a0f5e3SRoman Zippel 					break;
202669bfad9SRoman Zippel 				}
203669bfad9SRoman Zippel 			} else {
204669bfad9SRoman Zippel 				sym = sym_lookup(line + 7, 0);
205669bfad9SRoman Zippel 				if (sym->type == S_UNKNOWN)
206669bfad9SRoman Zippel 					sym->type = S_OTHER;
207669bfad9SRoman Zippel 			}
208669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
209c1a0f5e3SRoman Zippel 				conf_warning("trying to reassign symbol %s", sym->name);
2101da177e4SLinus Torvalds 				break;
2111da177e4SLinus Torvalds 			}
2121da177e4SLinus Torvalds 			switch (sym->type) {
2131da177e4SLinus Torvalds 			case S_TRISTATE:
2141da177e4SLinus Torvalds 				if (p[0] == 'm') {
215669bfad9SRoman Zippel 					sym->def[def].tri = mod;
216669bfad9SRoman Zippel 					sym->flags |= def_flags;
2171da177e4SLinus Torvalds 					break;
2181da177e4SLinus Torvalds 				}
2191da177e4SLinus Torvalds 			case S_BOOLEAN:
2201da177e4SLinus Torvalds 				if (p[0] == 'y') {
221669bfad9SRoman Zippel 					sym->def[def].tri = yes;
222669bfad9SRoman Zippel 					sym->flags |= def_flags;
2231da177e4SLinus Torvalds 					break;
2241da177e4SLinus Torvalds 				}
2251da177e4SLinus Torvalds 				if (p[0] == 'n') {
226669bfad9SRoman Zippel 					sym->def[def].tri = no;
227669bfad9SRoman Zippel 					sym->flags |= def_flags;
2281da177e4SLinus Torvalds 					break;
2291da177e4SLinus Torvalds 				}
230c1a0f5e3SRoman Zippel 				conf_warning("symbol value '%s' invalid for %s", p, sym->name);
2311da177e4SLinus Torvalds 				break;
232669bfad9SRoman Zippel 			case S_OTHER:
233669bfad9SRoman Zippel 				if (*p != '"') {
234669bfad9SRoman Zippel 					for (p2 = p; *p2 && !isspace(*p2); p2++)
235669bfad9SRoman Zippel 						;
236669bfad9SRoman Zippel 					sym->type = S_STRING;
237669bfad9SRoman Zippel 					goto done;
238669bfad9SRoman Zippel 				}
2391da177e4SLinus Torvalds 			case S_STRING:
2401da177e4SLinus Torvalds 				if (*p++ != '"')
2411da177e4SLinus Torvalds 					break;
2421da177e4SLinus Torvalds 				for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
2431da177e4SLinus Torvalds 					if (*p2 == '"') {
2441da177e4SLinus Torvalds 						*p2 = 0;
2451da177e4SLinus Torvalds 						break;
2461da177e4SLinus Torvalds 					}
2471da177e4SLinus Torvalds 					memmove(p2, p2 + 1, strlen(p2));
2481da177e4SLinus Torvalds 				}
2491da177e4SLinus Torvalds 				if (!p2) {
250c1a0f5e3SRoman Zippel 					conf_warning("invalid string found");
251c1a0f5e3SRoman Zippel 					continue;
2521da177e4SLinus Torvalds 				}
2531da177e4SLinus Torvalds 			case S_INT:
2541da177e4SLinus Torvalds 			case S_HEX:
255669bfad9SRoman Zippel 			done:
2561da177e4SLinus Torvalds 				if (sym_string_valid(sym, p)) {
257669bfad9SRoman Zippel 					sym->def[def].val = strdup(p);
258669bfad9SRoman Zippel 					sym->flags |= def_flags;
2591da177e4SLinus Torvalds 				} else {
260c1a0f5e3SRoman Zippel 					conf_warning("symbol value '%s' invalid for %s", p, sym->name);
261c1a0f5e3SRoman Zippel 					continue;
2621da177e4SLinus Torvalds 				}
2631da177e4SLinus Torvalds 				break;
2641da177e4SLinus Torvalds 			default:
2651da177e4SLinus Torvalds 				;
2661da177e4SLinus Torvalds 			}
2671da177e4SLinus Torvalds 			break;
2681da177e4SLinus Torvalds 		case '\n':
2691da177e4SLinus Torvalds 			break;
2701da177e4SLinus Torvalds 		default:
271c1a0f5e3SRoman Zippel 			conf_warning("unexpected data");
2721da177e4SLinus Torvalds 			continue;
2731da177e4SLinus Torvalds 		}
2741da177e4SLinus Torvalds 		if (sym && sym_is_choice_value(sym)) {
2751da177e4SLinus Torvalds 			struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
276669bfad9SRoman Zippel 			switch (sym->def[def].tri) {
2771da177e4SLinus Torvalds 			case no:
2781da177e4SLinus Torvalds 				break;
2791da177e4SLinus Torvalds 			case mod:
280669bfad9SRoman Zippel 				if (cs->def[def].tri == yes) {
281c1a0f5e3SRoman Zippel 					conf_warning("%s creates inconsistent choice state", sym->name);
282669bfad9SRoman Zippel 					cs->flags &= ~def_flags;
283c1a0f5e3SRoman Zippel 				}
2841da177e4SLinus Torvalds 				break;
2851da177e4SLinus Torvalds 			case yes:
286669bfad9SRoman Zippel 				if (cs->def[def].tri != no) {
287c1a0f5e3SRoman Zippel 					conf_warning("%s creates inconsistent choice state", sym->name);
288669bfad9SRoman Zippel 					cs->flags &= ~def_flags;
289c1a0f5e3SRoman Zippel 				} else
290669bfad9SRoman Zippel 					cs->def[def].val = sym;
2911da177e4SLinus Torvalds 				break;
2921da177e4SLinus Torvalds 			}
293669bfad9SRoman Zippel 			cs->def[def].tri = E_OR(cs->def[def].tri, sym->def[def].tri);
2941da177e4SLinus Torvalds 		}
2951da177e4SLinus Torvalds 	}
2961da177e4SLinus Torvalds 	fclose(in);
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	if (modules_sym)
2991da177e4SLinus Torvalds 		sym_calc_value(modules_sym);
30090389160SRoman Zippel 	return 0;
30190389160SRoman Zippel }
30290389160SRoman Zippel 
30390389160SRoman Zippel int conf_read(const char *name)
30490389160SRoman Zippel {
30590389160SRoman Zippel 	struct symbol *sym;
30690389160SRoman Zippel 	struct property *prop;
30790389160SRoman Zippel 	struct expr *e;
308669bfad9SRoman Zippel 	int i, flags;
30990389160SRoman Zippel 
310ddc97cacSRoman Zippel 	sym_change_count = 0;
311ddc97cacSRoman Zippel 
312669bfad9SRoman Zippel 	if (conf_read_simple(name, S_DEF_USER))
31390389160SRoman Zippel 		return 1;
31490389160SRoman Zippel 
3151da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
3161da177e4SLinus Torvalds 		sym_calc_value(sym);
317c1a0f5e3SRoman Zippel 		if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
318c1a0f5e3SRoman Zippel 			goto sym_ok;
319c1a0f5e3SRoman Zippel 		if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
320c1a0f5e3SRoman Zippel 			/* check that calculated value agrees with saved value */
321c1a0f5e3SRoman Zippel 			switch (sym->type) {
322c1a0f5e3SRoman Zippel 			case S_BOOLEAN:
323c1a0f5e3SRoman Zippel 			case S_TRISTATE:
3240c1822e6SRoman Zippel 				if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
325c1a0f5e3SRoman Zippel 					break;
326c1a0f5e3SRoman Zippel 				if (!sym_is_choice(sym))
327c1a0f5e3SRoman Zippel 					goto sym_ok;
328c1a0f5e3SRoman Zippel 			default:
3290c1822e6SRoman Zippel 				if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
330c1a0f5e3SRoman Zippel 					goto sym_ok;
331c1a0f5e3SRoman Zippel 				break;
332c1a0f5e3SRoman Zippel 			}
333c1a0f5e3SRoman Zippel 		} else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
334c1a0f5e3SRoman Zippel 			/* no previous value and not saved */
335c1a0f5e3SRoman Zippel 			goto sym_ok;
336c1a0f5e3SRoman Zippel 		conf_unsaved++;
337c1a0f5e3SRoman Zippel 		/* maybe print value in verbose mode... */
338c1a0f5e3SRoman Zippel 	sym_ok:
3391da177e4SLinus Torvalds 		if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
3401da177e4SLinus Torvalds 			if (sym->visible == no)
341669bfad9SRoman Zippel 				sym->flags &= ~SYMBOL_DEF_USER;
3421da177e4SLinus Torvalds 			switch (sym->type) {
3431da177e4SLinus Torvalds 			case S_STRING:
3441da177e4SLinus Torvalds 			case S_INT:
3451da177e4SLinus Torvalds 			case S_HEX:
346669bfad9SRoman Zippel 				if (!sym_string_within_range(sym, sym->def[S_DEF_USER].val))
347669bfad9SRoman Zippel 					sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
3481da177e4SLinus Torvalds 			default:
3491da177e4SLinus Torvalds 				break;
3501da177e4SLinus Torvalds 			}
3511da177e4SLinus Torvalds 		}
3521da177e4SLinus Torvalds 		if (!sym_is_choice(sym))
3531da177e4SLinus Torvalds 			continue;
3541da177e4SLinus Torvalds 		prop = sym_get_choice_prop(sym);
355669bfad9SRoman Zippel 		flags = sym->flags;
3561da177e4SLinus Torvalds 		for (e = prop->expr; e; e = e->left.expr)
3571da177e4SLinus Torvalds 			if (e->right.sym->visible != no)
358669bfad9SRoman Zippel 				flags &= e->right.sym->flags;
359669bfad9SRoman Zippel 		sym->flags |= flags & SYMBOL_DEF_USER;
3601da177e4SLinus Torvalds 	}
3611da177e4SLinus Torvalds 
362ddc97cacSRoman Zippel 	sym_change_count += conf_warnings || conf_unsaved;
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	return 0;
3651da177e4SLinus Torvalds }
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds int conf_write(const char *name)
3681da177e4SLinus Torvalds {
369c955ccafSRoman Zippel 	FILE *out;
3701da177e4SLinus Torvalds 	struct symbol *sym;
3711da177e4SLinus Torvalds 	struct menu *menu;
3721da177e4SLinus Torvalds 	const char *basename;
3731da177e4SLinus Torvalds 	char dirname[128], tmpname[128], newname[128];
3741da177e4SLinus Torvalds 	int type, l;
3751da177e4SLinus Torvalds 	const char *str;
3761da177e4SLinus Torvalds 	time_t now;
3771da177e4SLinus Torvalds 	int use_timestamp = 1;
3781da177e4SLinus Torvalds 	char *env;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	dirname[0] = 0;
3811da177e4SLinus Torvalds 	if (name && name[0]) {
3821da177e4SLinus Torvalds 		struct stat st;
3831da177e4SLinus Torvalds 		char *slash;
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 		if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
3861da177e4SLinus Torvalds 			strcpy(dirname, name);
3871da177e4SLinus Torvalds 			strcat(dirname, "/");
3881da177e4SLinus Torvalds 			basename = conf_def_filename;
3891da177e4SLinus Torvalds 		} else if ((slash = strrchr(name, '/'))) {
3901da177e4SLinus Torvalds 			int size = slash - name + 1;
3911da177e4SLinus Torvalds 			memcpy(dirname, name, size);
3921da177e4SLinus Torvalds 			dirname[size] = 0;
3931da177e4SLinus Torvalds 			if (slash[1])
3941da177e4SLinus Torvalds 				basename = slash + 1;
3951da177e4SLinus Torvalds 			else
3961da177e4SLinus Torvalds 				basename = conf_def_filename;
3971da177e4SLinus Torvalds 		} else
3981da177e4SLinus Torvalds 			basename = name;
3991da177e4SLinus Torvalds 	} else
4001da177e4SLinus Torvalds 		basename = conf_def_filename;
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	sprintf(newname, "%s.tmpconfig.%d", dirname, (int)getpid());
4031da177e4SLinus Torvalds 	out = fopen(newname, "w");
4041da177e4SLinus Torvalds 	if (!out)
4051da177e4SLinus Torvalds 		return 1;
4062244cbd8SSam Ravnborg 	sym = sym_lookup("KERNELVERSION", 0);
4071da177e4SLinus Torvalds 	sym_calc_value(sym);
4081da177e4SLinus Torvalds 	time(&now);
4091da177e4SLinus Torvalds 	env = getenv("KCONFIG_NOTIMESTAMP");
4101da177e4SLinus Torvalds 	if (env && *env)
4111da177e4SLinus Torvalds 		use_timestamp = 0;
4121da177e4SLinus Torvalds 
4133b9fa093SArnaldo Carvalho de Melo 	fprintf(out, _("#\n"
4141da177e4SLinus Torvalds 		       "# Automatically generated make config: don't edit\n"
4151da177e4SLinus Torvalds 		       "# Linux kernel version: %s\n"
4161da177e4SLinus Torvalds 		       "%s%s"
4173b9fa093SArnaldo Carvalho de Melo 		       "#\n"),
4181da177e4SLinus Torvalds 		     sym_get_string_value(sym),
4191da177e4SLinus Torvalds 		     use_timestamp ? "# " : "",
4201da177e4SLinus Torvalds 		     use_timestamp ? ctime(&now) : "");
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	if (!sym_change_count)
4231da177e4SLinus Torvalds 		sym_clear_all_valid();
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	menu = rootmenu.list;
4261da177e4SLinus Torvalds 	while (menu) {
4271da177e4SLinus Torvalds 		sym = menu->sym;
4281da177e4SLinus Torvalds 		if (!sym) {
4291da177e4SLinus Torvalds 			if (!menu_is_visible(menu))
4301da177e4SLinus Torvalds 				goto next;
4311da177e4SLinus Torvalds 			str = menu_get_prompt(menu);
4321da177e4SLinus Torvalds 			fprintf(out, "\n"
4331da177e4SLinus Torvalds 				     "#\n"
4341da177e4SLinus Torvalds 				     "# %s\n"
4351da177e4SLinus Torvalds 				     "#\n", str);
4361da177e4SLinus Torvalds 		} else if (!(sym->flags & SYMBOL_CHOICE)) {
4371da177e4SLinus Torvalds 			sym_calc_value(sym);
4381da177e4SLinus Torvalds 			if (!(sym->flags & SYMBOL_WRITE))
4391da177e4SLinus Torvalds 				goto next;
4401da177e4SLinus Torvalds 			sym->flags &= ~SYMBOL_WRITE;
4411da177e4SLinus Torvalds 			type = sym->type;
4421da177e4SLinus Torvalds 			if (type == S_TRISTATE) {
4431da177e4SLinus Torvalds 				sym_calc_value(modules_sym);
4441da177e4SLinus Torvalds 				if (modules_sym->curr.tri == no)
4451da177e4SLinus Torvalds 					type = S_BOOLEAN;
4461da177e4SLinus Torvalds 			}
4471da177e4SLinus Torvalds 			switch (type) {
4481da177e4SLinus Torvalds 			case S_BOOLEAN:
4491da177e4SLinus Torvalds 			case S_TRISTATE:
4501da177e4SLinus Torvalds 				switch (sym_get_tristate_value(sym)) {
4511da177e4SLinus Torvalds 				case no:
4521da177e4SLinus Torvalds 					fprintf(out, "# CONFIG_%s is not set\n", sym->name);
4531da177e4SLinus Torvalds 					break;
4541da177e4SLinus Torvalds 				case mod:
4551da177e4SLinus Torvalds 					fprintf(out, "CONFIG_%s=m\n", sym->name);
4561da177e4SLinus Torvalds 					break;
4571da177e4SLinus Torvalds 				case yes:
4581da177e4SLinus Torvalds 					fprintf(out, "CONFIG_%s=y\n", sym->name);
4591da177e4SLinus Torvalds 					break;
4601da177e4SLinus Torvalds 				}
4611da177e4SLinus Torvalds 				break;
4621da177e4SLinus Torvalds 			case S_STRING:
4631da177e4SLinus Torvalds 				str = sym_get_string_value(sym);
4641da177e4SLinus Torvalds 				fprintf(out, "CONFIG_%s=\"", sym->name);
465c955ccafSRoman Zippel 				while (1) {
4661da177e4SLinus Torvalds 					l = strcspn(str, "\"\\");
4671da177e4SLinus Torvalds 					if (l) {
4681da177e4SLinus Torvalds 						fwrite(str, l, 1, out);
4691da177e4SLinus Torvalds 						str += l;
4701da177e4SLinus Torvalds 					}
471c955ccafSRoman Zippel 					if (!*str)
472c955ccafSRoman Zippel 						break;
473c955ccafSRoman Zippel 					fprintf(out, "\\%c", *str++);
474c955ccafSRoman Zippel 				}
4751da177e4SLinus Torvalds 				fputs("\"\n", out);
4761da177e4SLinus Torvalds 				break;
4771da177e4SLinus Torvalds 			case S_HEX:
4781da177e4SLinus Torvalds 				str = sym_get_string_value(sym);
4791da177e4SLinus Torvalds 				if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
4801da177e4SLinus Torvalds 					fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
4811da177e4SLinus Torvalds 					break;
4821da177e4SLinus Torvalds 				}
4831da177e4SLinus Torvalds 			case S_INT:
4841da177e4SLinus Torvalds 				str = sym_get_string_value(sym);
4851da177e4SLinus Torvalds 				fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
4861da177e4SLinus Torvalds 				break;
4871da177e4SLinus Torvalds 			}
4881da177e4SLinus Torvalds 		}
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	next:
4911da177e4SLinus Torvalds 		if (menu->list) {
4921da177e4SLinus Torvalds 			menu = menu->list;
4931da177e4SLinus Torvalds 			continue;
4941da177e4SLinus Torvalds 		}
4951da177e4SLinus Torvalds 		if (menu->next)
4961da177e4SLinus Torvalds 			menu = menu->next;
4971da177e4SLinus Torvalds 		else while ((menu = menu->parent)) {
4981da177e4SLinus Torvalds 			if (menu->next) {
4991da177e4SLinus Torvalds 				menu = menu->next;
5001da177e4SLinus Torvalds 				break;
5011da177e4SLinus Torvalds 			}
5021da177e4SLinus Torvalds 		}
5031da177e4SLinus Torvalds 	}
5041da177e4SLinus Torvalds 	fclose(out);
5051da177e4SLinus Torvalds 	if (!name || basename != conf_def_filename) {
5061da177e4SLinus Torvalds 		if (!name)
5071da177e4SLinus Torvalds 			name = conf_def_filename;
5081da177e4SLinus Torvalds 		sprintf(tmpname, "%s.old", name);
5091da177e4SLinus Torvalds 		rename(name, tmpname);
5101da177e4SLinus Torvalds 	}
5111da177e4SLinus Torvalds 	sprintf(tmpname, "%s%s", dirname, basename);
5121da177e4SLinus Torvalds 	if (rename(newname, tmpname))
5131da177e4SLinus Torvalds 		return 1;
5141da177e4SLinus Torvalds 
515ddc97cacSRoman Zippel 	printf(_("#\n"
516ddc97cacSRoman Zippel 		 "# configuration written to %s\n"
517ddc97cacSRoman Zippel 		 "#\n"), tmpname);
518ddc97cacSRoman Zippel 
5191da177e4SLinus Torvalds 	sym_change_count = 0;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	return 0;
5221da177e4SLinus Torvalds }
523c955ccafSRoman Zippel 
524*2e3646e5SRoman Zippel int conf_split_config(void)
525*2e3646e5SRoman Zippel {
526*2e3646e5SRoman Zippel 	char *name, path[128];
527*2e3646e5SRoman Zippel 	char *s, *d, c;
528*2e3646e5SRoman Zippel 	struct symbol *sym;
529*2e3646e5SRoman Zippel 	struct stat sb;
530*2e3646e5SRoman Zippel 	int res, i, fd;
531*2e3646e5SRoman Zippel 
532*2e3646e5SRoman Zippel 	name = getenv("KCONFIG_AUTOCONFIG");
533*2e3646e5SRoman Zippel 	if (!name)
534*2e3646e5SRoman Zippel 		name = "include/config/auto.conf";
535*2e3646e5SRoman Zippel 	conf_read_simple(name, S_DEF_AUTO);
536*2e3646e5SRoman Zippel 
537*2e3646e5SRoman Zippel 	if (chdir("include/config"))
538*2e3646e5SRoman Zippel 		return 1;
539*2e3646e5SRoman Zippel 
540*2e3646e5SRoman Zippel 	res = 0;
541*2e3646e5SRoman Zippel 	for_all_symbols(i, sym) {
542*2e3646e5SRoman Zippel 		sym_calc_value(sym);
543*2e3646e5SRoman Zippel 		if ((sym->flags & SYMBOL_AUTO) || !sym->name)
544*2e3646e5SRoman Zippel 			continue;
545*2e3646e5SRoman Zippel 		if (sym->flags & SYMBOL_WRITE) {
546*2e3646e5SRoman Zippel 			if (sym->flags & SYMBOL_DEF_AUTO) {
547*2e3646e5SRoman Zippel 				/*
548*2e3646e5SRoman Zippel 				 * symbol has old and new value,
549*2e3646e5SRoman Zippel 				 * so compare them...
550*2e3646e5SRoman Zippel 				 */
551*2e3646e5SRoman Zippel 				switch (sym->type) {
552*2e3646e5SRoman Zippel 				case S_BOOLEAN:
553*2e3646e5SRoman Zippel 				case S_TRISTATE:
554*2e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) ==
555*2e3646e5SRoman Zippel 					    sym->def[S_DEF_AUTO].tri)
556*2e3646e5SRoman Zippel 						continue;
557*2e3646e5SRoman Zippel 					break;
558*2e3646e5SRoman Zippel 				case S_STRING:
559*2e3646e5SRoman Zippel 				case S_HEX:
560*2e3646e5SRoman Zippel 				case S_INT:
561*2e3646e5SRoman Zippel 					if (!strcmp(sym_get_string_value(sym),
562*2e3646e5SRoman Zippel 						    sym->def[S_DEF_AUTO].val))
563*2e3646e5SRoman Zippel 						continue;
564*2e3646e5SRoman Zippel 					break;
565*2e3646e5SRoman Zippel 				default:
566*2e3646e5SRoman Zippel 					break;
567*2e3646e5SRoman Zippel 				}
568*2e3646e5SRoman Zippel 			} else {
569*2e3646e5SRoman Zippel 				/*
570*2e3646e5SRoman Zippel 				 * If there is no old value, only 'no' (unset)
571*2e3646e5SRoman Zippel 				 * is allowed as new value.
572*2e3646e5SRoman Zippel 				 */
573*2e3646e5SRoman Zippel 				switch (sym->type) {
574*2e3646e5SRoman Zippel 				case S_BOOLEAN:
575*2e3646e5SRoman Zippel 				case S_TRISTATE:
576*2e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) == no)
577*2e3646e5SRoman Zippel 						continue;
578*2e3646e5SRoman Zippel 					break;
579*2e3646e5SRoman Zippel 				default:
580*2e3646e5SRoman Zippel 					break;
581*2e3646e5SRoman Zippel 				}
582*2e3646e5SRoman Zippel 			}
583*2e3646e5SRoman Zippel 		} else if (!(sym->flags & SYMBOL_DEF_AUTO))
584*2e3646e5SRoman Zippel 			/* There is neither an old nor a new value. */
585*2e3646e5SRoman Zippel 			continue;
586*2e3646e5SRoman Zippel 		/* else
587*2e3646e5SRoman Zippel 		 *	There is an old value, but no new value ('no' (unset)
588*2e3646e5SRoman Zippel 		 *	isn't saved in auto.conf, so the old value is always
589*2e3646e5SRoman Zippel 		 *	different from 'no').
590*2e3646e5SRoman Zippel 		 */
591*2e3646e5SRoman Zippel 
592*2e3646e5SRoman Zippel 		/* Replace all '_' and append ".h" */
593*2e3646e5SRoman Zippel 		s = sym->name;
594*2e3646e5SRoman Zippel 		d = path;
595*2e3646e5SRoman Zippel 		while ((c = *s++)) {
596*2e3646e5SRoman Zippel 			c = tolower(c);
597*2e3646e5SRoman Zippel 			*d++ = (c == '_') ? '/' : c;
598*2e3646e5SRoman Zippel 		}
599*2e3646e5SRoman Zippel 		strcpy(d, ".h");
600*2e3646e5SRoman Zippel 
601*2e3646e5SRoman Zippel 		/* Assume directory path already exists. */
602*2e3646e5SRoman Zippel 		fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
603*2e3646e5SRoman Zippel 		if (fd == -1) {
604*2e3646e5SRoman Zippel 			if (errno != ENOENT) {
605*2e3646e5SRoman Zippel 				res = 1;
606*2e3646e5SRoman Zippel 				break;
607*2e3646e5SRoman Zippel 			}
608*2e3646e5SRoman Zippel 			/*
609*2e3646e5SRoman Zippel 			 * Create directory components,
610*2e3646e5SRoman Zippel 			 * unless they exist already.
611*2e3646e5SRoman Zippel 			 */
612*2e3646e5SRoman Zippel 			d = path;
613*2e3646e5SRoman Zippel 			while ((d = strchr(d, '/'))) {
614*2e3646e5SRoman Zippel 				*d = 0;
615*2e3646e5SRoman Zippel 				if (stat(path, &sb) && mkdir(path, 0755)) {
616*2e3646e5SRoman Zippel 					res = 1;
617*2e3646e5SRoman Zippel 					goto out;
618*2e3646e5SRoman Zippel 				}
619*2e3646e5SRoman Zippel 				*d++ = '/';
620*2e3646e5SRoman Zippel 			}
621*2e3646e5SRoman Zippel 			/* Try it again. */
622*2e3646e5SRoman Zippel 			fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
623*2e3646e5SRoman Zippel 			if (fd == -1) {
624*2e3646e5SRoman Zippel 				res = 1;
625*2e3646e5SRoman Zippel 				break;
626*2e3646e5SRoman Zippel 			}
627*2e3646e5SRoman Zippel 		}
628*2e3646e5SRoman Zippel 		close(fd);
629*2e3646e5SRoman Zippel 	}
630*2e3646e5SRoman Zippel out:
631*2e3646e5SRoman Zippel 	if (chdir("../.."))
632*2e3646e5SRoman Zippel 		return 1;
633*2e3646e5SRoman Zippel 
634*2e3646e5SRoman Zippel 	return res;
635*2e3646e5SRoman Zippel }
636*2e3646e5SRoman Zippel 
637c955ccafSRoman Zippel int conf_write_autoconf(void)
638c955ccafSRoman Zippel {
639c955ccafSRoman Zippel 	struct symbol *sym;
640c955ccafSRoman Zippel 	const char *str;
641c955ccafSRoman Zippel 	char *name;
642c955ccafSRoman Zippel 	FILE *out, *out_h;
643c955ccafSRoman Zippel 	time_t now;
644c955ccafSRoman Zippel 	int i, l;
645c955ccafSRoman Zippel 
646*2e3646e5SRoman Zippel 	sym_clear_all_valid();
647*2e3646e5SRoman Zippel 
648c955ccafSRoman Zippel 	file_write_dep("include/config/auto.conf.cmd");
649c955ccafSRoman Zippel 
650*2e3646e5SRoman Zippel 	if (conf_split_config())
651*2e3646e5SRoman Zippel 		return 1;
652*2e3646e5SRoman Zippel 
653c955ccafSRoman Zippel 	out = fopen(".tmpconfig", "w");
654c955ccafSRoman Zippel 	if (!out)
655c955ccafSRoman Zippel 		return 1;
656c955ccafSRoman Zippel 
657c955ccafSRoman Zippel 	out_h = fopen(".tmpconfig.h", "w");
658c955ccafSRoman Zippel 	if (!out_h) {
659c955ccafSRoman Zippel 		fclose(out);
660c955ccafSRoman Zippel 		return 1;
661c955ccafSRoman Zippel 	}
662c955ccafSRoman Zippel 
663c955ccafSRoman Zippel 	sym = sym_lookup("KERNELVERSION", 0);
664c955ccafSRoman Zippel 	sym_calc_value(sym);
665c955ccafSRoman Zippel 	time(&now);
666c955ccafSRoman Zippel 	fprintf(out, "#\n"
667c955ccafSRoman Zippel 		     "# Automatically generated make config: don't edit\n"
668c955ccafSRoman Zippel 		     "# Linux kernel version: %s\n"
669c955ccafSRoman Zippel 		     "# %s"
670c955ccafSRoman Zippel 		     "#\n",
671c955ccafSRoman Zippel 		     sym_get_string_value(sym), ctime(&now));
672c955ccafSRoman Zippel 	fprintf(out_h, "/*\n"
673c955ccafSRoman Zippel 		       " * Automatically generated C config: don't edit\n"
674c955ccafSRoman Zippel 		       " * Linux kernel version: %s\n"
675c955ccafSRoman Zippel 		       " * %s"
676c955ccafSRoman Zippel 		       " */\n"
677c955ccafSRoman Zippel 		       "#define AUTOCONF_INCLUDED\n",
678c955ccafSRoman Zippel 		       sym_get_string_value(sym), ctime(&now));
679c955ccafSRoman Zippel 
680c955ccafSRoman Zippel 	for_all_symbols(i, sym) {
681c955ccafSRoman Zippel 		sym_calc_value(sym);
682c955ccafSRoman Zippel 		if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
683c955ccafSRoman Zippel 			continue;
684c955ccafSRoman Zippel 		switch (sym->type) {
685c955ccafSRoman Zippel 		case S_BOOLEAN:
686c955ccafSRoman Zippel 		case S_TRISTATE:
687c955ccafSRoman Zippel 			switch (sym_get_tristate_value(sym)) {
688c955ccafSRoman Zippel 			case no:
689c955ccafSRoman Zippel 				break;
690c955ccafSRoman Zippel 			case mod:
691c955ccafSRoman Zippel 				fprintf(out, "CONFIG_%s=m\n", sym->name);
692c955ccafSRoman Zippel 				fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
693c955ccafSRoman Zippel 				break;
694c955ccafSRoman Zippel 			case yes:
695c955ccafSRoman Zippel 				fprintf(out, "CONFIG_%s=y\n", sym->name);
696c955ccafSRoman Zippel 				fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
697c955ccafSRoman Zippel 				break;
698c955ccafSRoman Zippel 			}
699c955ccafSRoman Zippel 			break;
700c955ccafSRoman Zippel 		case S_STRING:
701c955ccafSRoman Zippel 			str = sym_get_string_value(sym);
702c955ccafSRoman Zippel 			fprintf(out, "CONFIG_%s=\"", sym->name);
703c955ccafSRoman Zippel 			fprintf(out_h, "#define CONFIG_%s \"", sym->name);
704c955ccafSRoman Zippel 			while (1) {
705c955ccafSRoman Zippel 				l = strcspn(str, "\"\\");
706c955ccafSRoman Zippel 				if (l) {
707c955ccafSRoman Zippel 					fwrite(str, l, 1, out);
708c955ccafSRoman Zippel 					fwrite(str, l, 1, out_h);
709c955ccafSRoman Zippel 					str += l;
710c955ccafSRoman Zippel 				}
711c955ccafSRoman Zippel 				if (!*str)
712c955ccafSRoman Zippel 					break;
713c955ccafSRoman Zippel 				fprintf(out, "\\%c", *str);
714c955ccafSRoman Zippel 				fprintf(out_h, "\\%c", *str);
715c955ccafSRoman Zippel 				str++;
716c955ccafSRoman Zippel 			}
717c955ccafSRoman Zippel 			fputs("\"\n", out);
718c955ccafSRoman Zippel 			fputs("\"\n", out_h);
719c955ccafSRoman Zippel 			break;
720c955ccafSRoman Zippel 		case S_HEX:
721c955ccafSRoman Zippel 			str = sym_get_string_value(sym);
722c955ccafSRoman Zippel 			if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
723c955ccafSRoman Zippel 				fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
724c955ccafSRoman Zippel 				fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
725c955ccafSRoman Zippel 				break;
726c955ccafSRoman Zippel 			}
727c955ccafSRoman Zippel 		case S_INT:
728c955ccafSRoman Zippel 			str = sym_get_string_value(sym);
729c955ccafSRoman Zippel 			fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
730c955ccafSRoman Zippel 			fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
731c955ccafSRoman Zippel 			break;
732c955ccafSRoman Zippel 		default:
733c955ccafSRoman Zippel 			break;
734c955ccafSRoman Zippel 		}
735c955ccafSRoman Zippel 	}
736c955ccafSRoman Zippel 	fclose(out);
737c955ccafSRoman Zippel 	fclose(out_h);
738c955ccafSRoman Zippel 
739c955ccafSRoman Zippel 	name = getenv("KCONFIG_AUTOHEADER");
740c955ccafSRoman Zippel 	if (!name)
741c955ccafSRoman Zippel 		name = "include/linux/autoconf.h";
742c955ccafSRoman Zippel 	if (rename(".tmpconfig.h", name))
743c955ccafSRoman Zippel 		return 1;
744c955ccafSRoman Zippel 	name = getenv("KCONFIG_AUTOCONFIG");
745c955ccafSRoman Zippel 	if (!name)
746c955ccafSRoman Zippel 		name = "include/config/auto.conf";
747c955ccafSRoman Zippel 	/*
748c955ccafSRoman Zippel 	 * This must be the last step, kbuild has a dependency on auto.conf
749c955ccafSRoman Zippel 	 * and this marks the successful completion of the previous steps.
750c955ccafSRoman Zippel 	 */
751c955ccafSRoman Zippel 	if (rename(".tmpconfig", name))
752c955ccafSRoman Zippel 		return 1;
753c955ccafSRoman Zippel 
754c955ccafSRoman Zippel 	return 0;
755c955ccafSRoman Zippel }
756