xref: /openbmc/linux/scripts/kconfig/confdata.c (revision 0c1822e6991a10da6dc391f0a2e2cf5fb2e31238)
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>
81da177e4SLinus Torvalds #include <stdio.h>
91da177e4SLinus Torvalds #include <stdlib.h>
101da177e4SLinus Torvalds #include <string.h>
111da177e4SLinus Torvalds #include <time.h>
121da177e4SLinus Torvalds #include <unistd.h>
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #define LKC_DIRECT_LINK
151da177e4SLinus Torvalds #include "lkc.h"
161da177e4SLinus Torvalds 
17c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
18c1a0f5e3SRoman Zippel 	__attribute__ ((format (printf, 1, 2)));
19c1a0f5e3SRoman Zippel 
20c1a0f5e3SRoman Zippel static const char *conf_filename;
21c1a0f5e3SRoman Zippel static int conf_lineno, conf_warnings, conf_unsaved;
22c1a0f5e3SRoman Zippel 
231da177e4SLinus Torvalds const char conf_def_filename[] = ".config";
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds const char conf_defname[] = "arch/$ARCH/defconfig";
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds const char *conf_confnames[] = {
281da177e4SLinus Torvalds 	".config",
291da177e4SLinus Torvalds 	"/lib/modules/$UNAME_RELEASE/.config",
301da177e4SLinus Torvalds 	"/etc/kernel-config",
311da177e4SLinus Torvalds 	"/boot/config-$UNAME_RELEASE",
321da177e4SLinus Torvalds 	conf_defname,
331da177e4SLinus Torvalds 	NULL,
341da177e4SLinus Torvalds };
351da177e4SLinus Torvalds 
36c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
37c1a0f5e3SRoman Zippel {
38c1a0f5e3SRoman Zippel 	va_list ap;
39c1a0f5e3SRoman Zippel 	va_start(ap, fmt);
40c1a0f5e3SRoman Zippel 	fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
41c1a0f5e3SRoman Zippel 	vfprintf(stderr, fmt, ap);
42c1a0f5e3SRoman Zippel 	fprintf(stderr, "\n");
43c1a0f5e3SRoman Zippel 	va_end(ap);
44c1a0f5e3SRoman Zippel 	conf_warnings++;
45c1a0f5e3SRoman Zippel }
46c1a0f5e3SRoman Zippel 
4748b9d03cSJ.A. Magallon static char *conf_expand_value(const char *in)
481da177e4SLinus Torvalds {
491da177e4SLinus Torvalds 	struct symbol *sym;
5048b9d03cSJ.A. Magallon 	const char *src;
511da177e4SLinus Torvalds 	static char res_value[SYMBOL_MAXLENGTH];
521da177e4SLinus Torvalds 	char *dst, name[SYMBOL_MAXLENGTH];
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	res_value[0] = 0;
551da177e4SLinus Torvalds 	dst = name;
561da177e4SLinus Torvalds 	while ((src = strchr(in, '$'))) {
571da177e4SLinus Torvalds 		strncat(res_value, in, src - in);
581da177e4SLinus Torvalds 		src++;
591da177e4SLinus Torvalds 		dst = name;
601da177e4SLinus Torvalds 		while (isalnum(*src) || *src == '_')
611da177e4SLinus Torvalds 			*dst++ = *src++;
621da177e4SLinus Torvalds 		*dst = 0;
631da177e4SLinus Torvalds 		sym = sym_lookup(name, 0);
641da177e4SLinus Torvalds 		sym_calc_value(sym);
651da177e4SLinus Torvalds 		strcat(res_value, sym_get_string_value(sym));
661da177e4SLinus Torvalds 		in = src;
671da177e4SLinus Torvalds 	}
681da177e4SLinus Torvalds 	strcat(res_value, in);
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds 	return res_value;
711da177e4SLinus Torvalds }
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds char *conf_get_default_confname(void)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	struct stat buf;
761da177e4SLinus Torvalds 	static char fullname[PATH_MAX+1];
771da177e4SLinus Torvalds 	char *env, *name;
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	name = conf_expand_value(conf_defname);
801da177e4SLinus Torvalds 	env = getenv(SRCTREE);
811da177e4SLinus Torvalds 	if (env) {
821da177e4SLinus Torvalds 		sprintf(fullname, "%s/%s", env, name);
831da177e4SLinus Torvalds 		if (!stat(fullname, &buf))
841da177e4SLinus Torvalds 			return fullname;
851da177e4SLinus Torvalds 	}
861da177e4SLinus Torvalds 	return name;
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds 
8990389160SRoman Zippel int conf_read_simple(const char *name)
901da177e4SLinus Torvalds {
911da177e4SLinus Torvalds 	FILE *in = NULL;
921da177e4SLinus Torvalds 	char line[1024];
931da177e4SLinus Torvalds 	char *p, *p2;
941da177e4SLinus Torvalds 	struct symbol *sym;
951da177e4SLinus Torvalds 	int i;
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	if (name) {
981da177e4SLinus Torvalds 		in = zconf_fopen(name);
991da177e4SLinus Torvalds 	} else {
1001da177e4SLinus Torvalds 		const char **names = conf_confnames;
101ddc97cacSRoman Zippel 		name = *names++;
102ddc97cacSRoman Zippel 		if (!name)
103ddc97cacSRoman Zippel 			return 1;
104ddc97cacSRoman Zippel 		in = zconf_fopen(name);
105ddc97cacSRoman Zippel 		if (in)
106ddc97cacSRoman Zippel 			goto load;
107ddc97cacSRoman Zippel 		sym_change_count++;
1081da177e4SLinus Torvalds 		while ((name = *names++)) {
1091da177e4SLinus Torvalds 			name = conf_expand_value(name);
1101da177e4SLinus Torvalds 			in = zconf_fopen(name);
1111da177e4SLinus Torvalds 			if (in) {
1123b9fa093SArnaldo Carvalho de Melo 				printf(_("#\n"
1131da177e4SLinus Torvalds 					 "# using defaults found in %s\n"
1143b9fa093SArnaldo Carvalho de Melo 					 "#\n"), name);
115ddc97cacSRoman Zippel 				goto load;
1161da177e4SLinus Torvalds 			}
1171da177e4SLinus Torvalds 		}
1181da177e4SLinus Torvalds 	}
1191da177e4SLinus Torvalds 	if (!in)
1201da177e4SLinus Torvalds 		return 1;
1211da177e4SLinus Torvalds 
122ddc97cacSRoman Zippel load:
123c1a0f5e3SRoman Zippel 	conf_filename = name;
124c1a0f5e3SRoman Zippel 	conf_lineno = 0;
125c1a0f5e3SRoman Zippel 	conf_warnings = 0;
126c1a0f5e3SRoman Zippel 	conf_unsaved = 0;
127c1a0f5e3SRoman Zippel 
1281da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
1291da177e4SLinus Torvalds 		sym->flags |= SYMBOL_NEW | SYMBOL_CHANGED;
130c1a0f5e3SRoman Zippel 		if (sym_is_choice(sym))
131c1a0f5e3SRoman Zippel 			sym->flags &= ~SYMBOL_NEW;
1321da177e4SLinus Torvalds 		sym->flags &= ~SYMBOL_VALID;
1331da177e4SLinus Torvalds 		switch (sym->type) {
1341da177e4SLinus Torvalds 		case S_INT:
1351da177e4SLinus Torvalds 		case S_HEX:
1361da177e4SLinus Torvalds 		case S_STRING:
137*0c1822e6SRoman Zippel 			if (sym->def[S_DEF_USER].val)
138*0c1822e6SRoman Zippel 				free(sym->def[S_DEF_USER].val);
1391da177e4SLinus Torvalds 		default:
140*0c1822e6SRoman Zippel 			sym->def[S_DEF_USER].val = NULL;
141*0c1822e6SRoman Zippel 			sym->def[S_DEF_USER].tri = no;
1421da177e4SLinus Torvalds 		}
1431da177e4SLinus Torvalds 	}
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	while (fgets(line, sizeof(line), in)) {
146c1a0f5e3SRoman Zippel 		conf_lineno++;
1471da177e4SLinus Torvalds 		sym = NULL;
1481da177e4SLinus Torvalds 		switch (line[0]) {
1491da177e4SLinus Torvalds 		case '#':
1501da177e4SLinus Torvalds 			if (memcmp(line + 2, "CONFIG_", 7))
1511da177e4SLinus Torvalds 				continue;
1521da177e4SLinus Torvalds 			p = strchr(line + 9, ' ');
1531da177e4SLinus Torvalds 			if (!p)
1541da177e4SLinus Torvalds 				continue;
1551da177e4SLinus Torvalds 			*p++ = 0;
1561da177e4SLinus Torvalds 			if (strncmp(p, "is not set", 10))
1571da177e4SLinus Torvalds 				continue;
1581da177e4SLinus Torvalds 			sym = sym_find(line + 9);
1591da177e4SLinus Torvalds 			if (!sym) {
160c1a0f5e3SRoman Zippel 				conf_warning("trying to assign nonexistent symbol %s", line + 9);
161c1a0f5e3SRoman Zippel 				break;
162c1a0f5e3SRoman Zippel 			} else if (!(sym->flags & SYMBOL_NEW)) {
163c1a0f5e3SRoman Zippel 				conf_warning("trying to reassign symbol %s", sym->name);
1641da177e4SLinus Torvalds 				break;
1651da177e4SLinus Torvalds 			}
1661da177e4SLinus Torvalds 			switch (sym->type) {
1671da177e4SLinus Torvalds 			case S_BOOLEAN:
1681da177e4SLinus Torvalds 			case S_TRISTATE:
169*0c1822e6SRoman Zippel 				sym->def[S_DEF_USER].tri = no;
1701da177e4SLinus Torvalds 				sym->flags &= ~SYMBOL_NEW;
1711da177e4SLinus Torvalds 				break;
1721da177e4SLinus Torvalds 			default:
1731da177e4SLinus Torvalds 				;
1741da177e4SLinus Torvalds 			}
1751da177e4SLinus Torvalds 			break;
1761da177e4SLinus Torvalds 		case 'C':
177c1a0f5e3SRoman Zippel 			if (memcmp(line, "CONFIG_", 7)) {
178c1a0f5e3SRoman Zippel 				conf_warning("unexpected data");
1791da177e4SLinus Torvalds 				continue;
180c1a0f5e3SRoman Zippel 			}
1811da177e4SLinus Torvalds 			p = strchr(line + 7, '=');
1821da177e4SLinus Torvalds 			if (!p)
1831da177e4SLinus Torvalds 				continue;
1841da177e4SLinus Torvalds 			*p++ = 0;
1851da177e4SLinus Torvalds 			p2 = strchr(p, '\n');
1861da177e4SLinus Torvalds 			if (p2)
1871da177e4SLinus Torvalds 				*p2 = 0;
1881da177e4SLinus Torvalds 			sym = sym_find(line + 7);
1891da177e4SLinus Torvalds 			if (!sym) {
190c1a0f5e3SRoman Zippel 				conf_warning("trying to assign nonexistent symbol %s", line + 7);
191c1a0f5e3SRoman Zippel 				break;
192c1a0f5e3SRoman Zippel 			} else if (!(sym->flags & SYMBOL_NEW)) {
193c1a0f5e3SRoman Zippel 				conf_warning("trying to reassign symbol %s", sym->name);
1941da177e4SLinus Torvalds 				break;
1951da177e4SLinus Torvalds 			}
1961da177e4SLinus Torvalds 			switch (sym->type) {
1971da177e4SLinus Torvalds 			case S_TRISTATE:
1981da177e4SLinus Torvalds 				if (p[0] == 'm') {
199*0c1822e6SRoman Zippel 					sym->def[S_DEF_USER].tri = mod;
2001da177e4SLinus Torvalds 					sym->flags &= ~SYMBOL_NEW;
2011da177e4SLinus Torvalds 					break;
2021da177e4SLinus Torvalds 				}
2031da177e4SLinus Torvalds 			case S_BOOLEAN:
2041da177e4SLinus Torvalds 				if (p[0] == 'y') {
205*0c1822e6SRoman Zippel 					sym->def[S_DEF_USER].tri = yes;
2061da177e4SLinus Torvalds 					sym->flags &= ~SYMBOL_NEW;
2071da177e4SLinus Torvalds 					break;
2081da177e4SLinus Torvalds 				}
2091da177e4SLinus Torvalds 				if (p[0] == 'n') {
210*0c1822e6SRoman Zippel 					sym->def[S_DEF_USER].tri = no;
2111da177e4SLinus Torvalds 					sym->flags &= ~SYMBOL_NEW;
2121da177e4SLinus Torvalds 					break;
2131da177e4SLinus Torvalds 				}
214c1a0f5e3SRoman Zippel 				conf_warning("symbol value '%s' invalid for %s", p, sym->name);
2151da177e4SLinus Torvalds 				break;
2161da177e4SLinus Torvalds 			case S_STRING:
2171da177e4SLinus Torvalds 				if (*p++ != '"')
2181da177e4SLinus Torvalds 					break;
2191da177e4SLinus Torvalds 				for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
2201da177e4SLinus Torvalds 					if (*p2 == '"') {
2211da177e4SLinus Torvalds 						*p2 = 0;
2221da177e4SLinus Torvalds 						break;
2231da177e4SLinus Torvalds 					}
2241da177e4SLinus Torvalds 					memmove(p2, p2 + 1, strlen(p2));
2251da177e4SLinus Torvalds 				}
2261da177e4SLinus Torvalds 				if (!p2) {
227c1a0f5e3SRoman Zippel 					conf_warning("invalid string found");
228c1a0f5e3SRoman Zippel 					continue;
2291da177e4SLinus Torvalds 				}
2301da177e4SLinus Torvalds 			case S_INT:
2311da177e4SLinus Torvalds 			case S_HEX:
2321da177e4SLinus Torvalds 				if (sym_string_valid(sym, p)) {
233*0c1822e6SRoman Zippel 					sym->def[S_DEF_USER].val = strdup(p);
2341da177e4SLinus Torvalds 					sym->flags &= ~SYMBOL_NEW;
2351da177e4SLinus Torvalds 				} else {
236c1a0f5e3SRoman Zippel 					conf_warning("symbol value '%s' invalid for %s", p, sym->name);
237c1a0f5e3SRoman Zippel 					continue;
2381da177e4SLinus Torvalds 				}
2391da177e4SLinus Torvalds 				break;
2401da177e4SLinus Torvalds 			default:
2411da177e4SLinus Torvalds 				;
2421da177e4SLinus Torvalds 			}
2431da177e4SLinus Torvalds 			break;
2441da177e4SLinus Torvalds 		case '\n':
2451da177e4SLinus Torvalds 			break;
2461da177e4SLinus Torvalds 		default:
247c1a0f5e3SRoman Zippel 			conf_warning("unexpected data");
2481da177e4SLinus Torvalds 			continue;
2491da177e4SLinus Torvalds 		}
2501da177e4SLinus Torvalds 		if (sym && sym_is_choice_value(sym)) {
2511da177e4SLinus Torvalds 			struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
252*0c1822e6SRoman Zippel 			switch (sym->def[S_DEF_USER].tri) {
2531da177e4SLinus Torvalds 			case no:
2541da177e4SLinus Torvalds 				break;
2551da177e4SLinus Torvalds 			case mod:
256*0c1822e6SRoman Zippel 				if (cs->def[S_DEF_USER].tri == yes) {
257c1a0f5e3SRoman Zippel 					conf_warning("%s creates inconsistent choice state", sym->name);
258c1a0f5e3SRoman Zippel 					cs->flags |= SYMBOL_NEW;
259c1a0f5e3SRoman Zippel 				}
2601da177e4SLinus Torvalds 				break;
2611da177e4SLinus Torvalds 			case yes:
262*0c1822e6SRoman Zippel 				if (cs->def[S_DEF_USER].tri != no) {
263c1a0f5e3SRoman Zippel 					conf_warning("%s creates inconsistent choice state", sym->name);
264c1a0f5e3SRoman Zippel 					cs->flags |= SYMBOL_NEW;
265c1a0f5e3SRoman Zippel 				} else
266*0c1822e6SRoman Zippel 					cs->def[S_DEF_USER].val = sym;
2671da177e4SLinus Torvalds 				break;
2681da177e4SLinus Torvalds 			}
269*0c1822e6SRoman Zippel 			cs->def[S_DEF_USER].tri = E_OR(cs->def[S_DEF_USER].tri, sym->def[S_DEF_USER].tri);
2701da177e4SLinus Torvalds 		}
2711da177e4SLinus Torvalds 	}
2721da177e4SLinus Torvalds 	fclose(in);
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	if (modules_sym)
2751da177e4SLinus Torvalds 		sym_calc_value(modules_sym);
27690389160SRoman Zippel 	return 0;
27790389160SRoman Zippel }
27890389160SRoman Zippel 
27990389160SRoman Zippel int conf_read(const char *name)
28090389160SRoman Zippel {
28190389160SRoman Zippel 	struct symbol *sym;
28290389160SRoman Zippel 	struct property *prop;
28390389160SRoman Zippel 	struct expr *e;
28490389160SRoman Zippel 	int i;
28590389160SRoman Zippel 
286ddc97cacSRoman Zippel 	sym_change_count = 0;
287ddc97cacSRoman Zippel 
28890389160SRoman Zippel 	if (conf_read_simple(name))
28990389160SRoman Zippel 		return 1;
29090389160SRoman Zippel 
2911da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
2921da177e4SLinus Torvalds 		sym_calc_value(sym);
293c1a0f5e3SRoman Zippel 		if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
294c1a0f5e3SRoman Zippel 			goto sym_ok;
295c1a0f5e3SRoman Zippel 		if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
296c1a0f5e3SRoman Zippel 			/* check that calculated value agrees with saved value */
297c1a0f5e3SRoman Zippel 			switch (sym->type) {
298c1a0f5e3SRoman Zippel 			case S_BOOLEAN:
299c1a0f5e3SRoman Zippel 			case S_TRISTATE:
300*0c1822e6SRoman Zippel 				if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
301c1a0f5e3SRoman Zippel 					break;
302c1a0f5e3SRoman Zippel 				if (!sym_is_choice(sym))
303c1a0f5e3SRoman Zippel 					goto sym_ok;
304c1a0f5e3SRoman Zippel 			default:
305*0c1822e6SRoman Zippel 				if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
306c1a0f5e3SRoman Zippel 					goto sym_ok;
307c1a0f5e3SRoman Zippel 				break;
308c1a0f5e3SRoman Zippel 			}
309c1a0f5e3SRoman Zippel 		} else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
310c1a0f5e3SRoman Zippel 			/* no previous value and not saved */
311c1a0f5e3SRoman Zippel 			goto sym_ok;
312c1a0f5e3SRoman Zippel 		conf_unsaved++;
313c1a0f5e3SRoman Zippel 		/* maybe print value in verbose mode... */
314c1a0f5e3SRoman Zippel 	sym_ok:
3151da177e4SLinus Torvalds 		if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
3161da177e4SLinus Torvalds 			if (sym->visible == no)
3171da177e4SLinus Torvalds 				sym->flags |= SYMBOL_NEW;
3181da177e4SLinus Torvalds 			switch (sym->type) {
3191da177e4SLinus Torvalds 			case S_STRING:
3201da177e4SLinus Torvalds 			case S_INT:
3211da177e4SLinus Torvalds 			case S_HEX:
322*0c1822e6SRoman Zippel 				if (!sym_string_within_range(sym, sym->def[S_DEF_USER].val)) {
3231da177e4SLinus Torvalds 					sym->flags |= SYMBOL_NEW;
324c1a0f5e3SRoman Zippel 					sym->flags &= ~SYMBOL_VALID;
325c1a0f5e3SRoman Zippel 				}
3261da177e4SLinus Torvalds 			default:
3271da177e4SLinus Torvalds 				break;
3281da177e4SLinus Torvalds 			}
3291da177e4SLinus Torvalds 		}
3301da177e4SLinus Torvalds 		if (!sym_is_choice(sym))
3311da177e4SLinus Torvalds 			continue;
3321da177e4SLinus Torvalds 		prop = sym_get_choice_prop(sym);
3331da177e4SLinus Torvalds 		for (e = prop->expr; e; e = e->left.expr)
3341da177e4SLinus Torvalds 			if (e->right.sym->visible != no)
3351da177e4SLinus Torvalds 				sym->flags |= e->right.sym->flags & SYMBOL_NEW;
3361da177e4SLinus Torvalds 	}
3371da177e4SLinus Torvalds 
338ddc97cacSRoman Zippel 	sym_change_count += conf_warnings || conf_unsaved;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	return 0;
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds int conf_write(const char *name)
3441da177e4SLinus Torvalds {
345c955ccafSRoman Zippel 	FILE *out;
3461da177e4SLinus Torvalds 	struct symbol *sym;
3471da177e4SLinus Torvalds 	struct menu *menu;
3481da177e4SLinus Torvalds 	const char *basename;
3491da177e4SLinus Torvalds 	char dirname[128], tmpname[128], newname[128];
3501da177e4SLinus Torvalds 	int type, l;
3511da177e4SLinus Torvalds 	const char *str;
3521da177e4SLinus Torvalds 	time_t now;
3531da177e4SLinus Torvalds 	int use_timestamp = 1;
3541da177e4SLinus Torvalds 	char *env;
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	dirname[0] = 0;
3571da177e4SLinus Torvalds 	if (name && name[0]) {
3581da177e4SLinus Torvalds 		struct stat st;
3591da177e4SLinus Torvalds 		char *slash;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 		if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
3621da177e4SLinus Torvalds 			strcpy(dirname, name);
3631da177e4SLinus Torvalds 			strcat(dirname, "/");
3641da177e4SLinus Torvalds 			basename = conf_def_filename;
3651da177e4SLinus Torvalds 		} else if ((slash = strrchr(name, '/'))) {
3661da177e4SLinus Torvalds 			int size = slash - name + 1;
3671da177e4SLinus Torvalds 			memcpy(dirname, name, size);
3681da177e4SLinus Torvalds 			dirname[size] = 0;
3691da177e4SLinus Torvalds 			if (slash[1])
3701da177e4SLinus Torvalds 				basename = slash + 1;
3711da177e4SLinus Torvalds 			else
3721da177e4SLinus Torvalds 				basename = conf_def_filename;
3731da177e4SLinus Torvalds 		} else
3741da177e4SLinus Torvalds 			basename = name;
3751da177e4SLinus Torvalds 	} else
3761da177e4SLinus Torvalds 		basename = conf_def_filename;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	sprintf(newname, "%s.tmpconfig.%d", dirname, (int)getpid());
3791da177e4SLinus Torvalds 	out = fopen(newname, "w");
3801da177e4SLinus Torvalds 	if (!out)
3811da177e4SLinus Torvalds 		return 1;
3822244cbd8SSam Ravnborg 	sym = sym_lookup("KERNELVERSION", 0);
3831da177e4SLinus Torvalds 	sym_calc_value(sym);
3841da177e4SLinus Torvalds 	time(&now);
3851da177e4SLinus Torvalds 	env = getenv("KCONFIG_NOTIMESTAMP");
3861da177e4SLinus Torvalds 	if (env && *env)
3871da177e4SLinus Torvalds 		use_timestamp = 0;
3881da177e4SLinus Torvalds 
3893b9fa093SArnaldo Carvalho de Melo 	fprintf(out, _("#\n"
3901da177e4SLinus Torvalds 		       "# Automatically generated make config: don't edit\n"
3911da177e4SLinus Torvalds 		       "# Linux kernel version: %s\n"
3921da177e4SLinus Torvalds 		       "%s%s"
3933b9fa093SArnaldo Carvalho de Melo 		       "#\n"),
3941da177e4SLinus Torvalds 		     sym_get_string_value(sym),
3951da177e4SLinus Torvalds 		     use_timestamp ? "# " : "",
3961da177e4SLinus Torvalds 		     use_timestamp ? ctime(&now) : "");
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	if (!sym_change_count)
3991da177e4SLinus Torvalds 		sym_clear_all_valid();
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	menu = rootmenu.list;
4021da177e4SLinus Torvalds 	while (menu) {
4031da177e4SLinus Torvalds 		sym = menu->sym;
4041da177e4SLinus Torvalds 		if (!sym) {
4051da177e4SLinus Torvalds 			if (!menu_is_visible(menu))
4061da177e4SLinus Torvalds 				goto next;
4071da177e4SLinus Torvalds 			str = menu_get_prompt(menu);
4081da177e4SLinus Torvalds 			fprintf(out, "\n"
4091da177e4SLinus Torvalds 				     "#\n"
4101da177e4SLinus Torvalds 				     "# %s\n"
4111da177e4SLinus Torvalds 				     "#\n", str);
4121da177e4SLinus Torvalds 		} else if (!(sym->flags & SYMBOL_CHOICE)) {
4131da177e4SLinus Torvalds 			sym_calc_value(sym);
4141da177e4SLinus Torvalds 			if (!(sym->flags & SYMBOL_WRITE))
4151da177e4SLinus Torvalds 				goto next;
4161da177e4SLinus Torvalds 			sym->flags &= ~SYMBOL_WRITE;
4171da177e4SLinus Torvalds 			type = sym->type;
4181da177e4SLinus Torvalds 			if (type == S_TRISTATE) {
4191da177e4SLinus Torvalds 				sym_calc_value(modules_sym);
4201da177e4SLinus Torvalds 				if (modules_sym->curr.tri == no)
4211da177e4SLinus Torvalds 					type = S_BOOLEAN;
4221da177e4SLinus Torvalds 			}
4231da177e4SLinus Torvalds 			switch (type) {
4241da177e4SLinus Torvalds 			case S_BOOLEAN:
4251da177e4SLinus Torvalds 			case S_TRISTATE:
4261da177e4SLinus Torvalds 				switch (sym_get_tristate_value(sym)) {
4271da177e4SLinus Torvalds 				case no:
4281da177e4SLinus Torvalds 					fprintf(out, "# CONFIG_%s is not set\n", sym->name);
4291da177e4SLinus Torvalds 					break;
4301da177e4SLinus Torvalds 				case mod:
4311da177e4SLinus Torvalds 					fprintf(out, "CONFIG_%s=m\n", sym->name);
4321da177e4SLinus Torvalds 					break;
4331da177e4SLinus Torvalds 				case yes:
4341da177e4SLinus Torvalds 					fprintf(out, "CONFIG_%s=y\n", sym->name);
4351da177e4SLinus Torvalds 					break;
4361da177e4SLinus Torvalds 				}
4371da177e4SLinus Torvalds 				break;
4381da177e4SLinus Torvalds 			case S_STRING:
4391da177e4SLinus Torvalds 				str = sym_get_string_value(sym);
4401da177e4SLinus Torvalds 				fprintf(out, "CONFIG_%s=\"", sym->name);
441c955ccafSRoman Zippel 				while (1) {
4421da177e4SLinus Torvalds 					l = strcspn(str, "\"\\");
4431da177e4SLinus Torvalds 					if (l) {
4441da177e4SLinus Torvalds 						fwrite(str, l, 1, out);
4451da177e4SLinus Torvalds 						str += l;
4461da177e4SLinus Torvalds 					}
447c955ccafSRoman Zippel 					if (!*str)
448c955ccafSRoman Zippel 						break;
449c955ccafSRoman Zippel 					fprintf(out, "\\%c", *str++);
450c955ccafSRoman Zippel 				}
4511da177e4SLinus Torvalds 				fputs("\"\n", out);
4521da177e4SLinus Torvalds 				break;
4531da177e4SLinus Torvalds 			case S_HEX:
4541da177e4SLinus Torvalds 				str = sym_get_string_value(sym);
4551da177e4SLinus Torvalds 				if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
4561da177e4SLinus Torvalds 					fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
4571da177e4SLinus Torvalds 					break;
4581da177e4SLinus Torvalds 				}
4591da177e4SLinus Torvalds 			case S_INT:
4601da177e4SLinus Torvalds 				str = sym_get_string_value(sym);
4611da177e4SLinus Torvalds 				fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
4621da177e4SLinus Torvalds 				break;
4631da177e4SLinus Torvalds 			}
4641da177e4SLinus Torvalds 		}
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	next:
4671da177e4SLinus Torvalds 		if (menu->list) {
4681da177e4SLinus Torvalds 			menu = menu->list;
4691da177e4SLinus Torvalds 			continue;
4701da177e4SLinus Torvalds 		}
4711da177e4SLinus Torvalds 		if (menu->next)
4721da177e4SLinus Torvalds 			menu = menu->next;
4731da177e4SLinus Torvalds 		else while ((menu = menu->parent)) {
4741da177e4SLinus Torvalds 			if (menu->next) {
4751da177e4SLinus Torvalds 				menu = menu->next;
4761da177e4SLinus Torvalds 				break;
4771da177e4SLinus Torvalds 			}
4781da177e4SLinus Torvalds 		}
4791da177e4SLinus Torvalds 	}
4801da177e4SLinus Torvalds 	fclose(out);
4811da177e4SLinus Torvalds 	if (!name || basename != conf_def_filename) {
4821da177e4SLinus Torvalds 		if (!name)
4831da177e4SLinus Torvalds 			name = conf_def_filename;
4841da177e4SLinus Torvalds 		sprintf(tmpname, "%s.old", name);
4851da177e4SLinus Torvalds 		rename(name, tmpname);
4861da177e4SLinus Torvalds 	}
4871da177e4SLinus Torvalds 	sprintf(tmpname, "%s%s", dirname, basename);
4881da177e4SLinus Torvalds 	if (rename(newname, tmpname))
4891da177e4SLinus Torvalds 		return 1;
4901da177e4SLinus Torvalds 
491ddc97cacSRoman Zippel 	printf(_("#\n"
492ddc97cacSRoman Zippel 		 "# configuration written to %s\n"
493ddc97cacSRoman Zippel 		 "#\n"), tmpname);
494ddc97cacSRoman Zippel 
4951da177e4SLinus Torvalds 	sym_change_count = 0;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	return 0;
4981da177e4SLinus Torvalds }
499c955ccafSRoman Zippel 
500c955ccafSRoman Zippel int conf_write_autoconf(void)
501c955ccafSRoman Zippel {
502c955ccafSRoman Zippel 	struct symbol *sym;
503c955ccafSRoman Zippel 	const char *str;
504c955ccafSRoman Zippel 	char *name;
505c955ccafSRoman Zippel 	FILE *out, *out_h;
506c955ccafSRoman Zippel 	time_t now;
507c955ccafSRoman Zippel 	int i, l;
508c955ccafSRoman Zippel 
509c955ccafSRoman Zippel 	file_write_dep("include/config/auto.conf.cmd");
510c955ccafSRoman Zippel 
511c955ccafSRoman Zippel 	out = fopen(".tmpconfig", "w");
512c955ccafSRoman Zippel 	if (!out)
513c955ccafSRoman Zippel 		return 1;
514c955ccafSRoman Zippel 
515c955ccafSRoman Zippel 	out_h = fopen(".tmpconfig.h", "w");
516c955ccafSRoman Zippel 	if (!out_h) {
517c955ccafSRoman Zippel 		fclose(out);
518c955ccafSRoman Zippel 		return 1;
519c955ccafSRoman Zippel 	}
520c955ccafSRoman Zippel 
521c955ccafSRoman Zippel 	sym = sym_lookup("KERNELVERSION", 0);
522c955ccafSRoman Zippel 	sym_calc_value(sym);
523c955ccafSRoman Zippel 	time(&now);
524c955ccafSRoman Zippel 	fprintf(out, "#\n"
525c955ccafSRoman Zippel 		     "# Automatically generated make config: don't edit\n"
526c955ccafSRoman Zippel 		     "# Linux kernel version: %s\n"
527c955ccafSRoman Zippel 		     "# %s"
528c955ccafSRoman Zippel 		     "#\n",
529c955ccafSRoman Zippel 		     sym_get_string_value(sym), ctime(&now));
530c955ccafSRoman Zippel 	fprintf(out_h, "/*\n"
531c955ccafSRoman Zippel 		       " * Automatically generated C config: don't edit\n"
532c955ccafSRoman Zippel 		       " * Linux kernel version: %s\n"
533c955ccafSRoman Zippel 		       " * %s"
534c955ccafSRoman Zippel 		       " */\n"
535c955ccafSRoman Zippel 		       "#define AUTOCONF_INCLUDED\n",
536c955ccafSRoman Zippel 		       sym_get_string_value(sym), ctime(&now));
537c955ccafSRoman Zippel 
538c955ccafSRoman Zippel 	sym_clear_all_valid();
539c955ccafSRoman Zippel 
540c955ccafSRoman Zippel 	for_all_symbols(i, sym) {
541c955ccafSRoman Zippel 		sym_calc_value(sym);
542c955ccafSRoman Zippel 		if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
543c955ccafSRoman Zippel 			continue;
544c955ccafSRoman Zippel 		switch (sym->type) {
545c955ccafSRoman Zippel 		case S_BOOLEAN:
546c955ccafSRoman Zippel 		case S_TRISTATE:
547c955ccafSRoman Zippel 			switch (sym_get_tristate_value(sym)) {
548c955ccafSRoman Zippel 			case no:
549c955ccafSRoman Zippel 				break;
550c955ccafSRoman Zippel 			case mod:
551c955ccafSRoman Zippel 				fprintf(out, "CONFIG_%s=m\n", sym->name);
552c955ccafSRoman Zippel 				fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
553c955ccafSRoman Zippel 				break;
554c955ccafSRoman Zippel 			case yes:
555c955ccafSRoman Zippel 				fprintf(out, "CONFIG_%s=y\n", sym->name);
556c955ccafSRoman Zippel 				fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
557c955ccafSRoman Zippel 				break;
558c955ccafSRoman Zippel 			}
559c955ccafSRoman Zippel 			break;
560c955ccafSRoman Zippel 		case S_STRING:
561c955ccafSRoman Zippel 			str = sym_get_string_value(sym);
562c955ccafSRoman Zippel 			fprintf(out, "CONFIG_%s=\"", sym->name);
563c955ccafSRoman Zippel 			fprintf(out_h, "#define CONFIG_%s \"", sym->name);
564c955ccafSRoman Zippel 			while (1) {
565c955ccafSRoman Zippel 				l = strcspn(str, "\"\\");
566c955ccafSRoman Zippel 				if (l) {
567c955ccafSRoman Zippel 					fwrite(str, l, 1, out);
568c955ccafSRoman Zippel 					fwrite(str, l, 1, out_h);
569c955ccafSRoman Zippel 					str += l;
570c955ccafSRoman Zippel 				}
571c955ccafSRoman Zippel 				if (!*str)
572c955ccafSRoman Zippel 					break;
573c955ccafSRoman Zippel 				fprintf(out, "\\%c", *str);
574c955ccafSRoman Zippel 				fprintf(out_h, "\\%c", *str);
575c955ccafSRoman Zippel 				str++;
576c955ccafSRoman Zippel 			}
577c955ccafSRoman Zippel 			fputs("\"\n", out);
578c955ccafSRoman Zippel 			fputs("\"\n", out_h);
579c955ccafSRoman Zippel 			break;
580c955ccafSRoman Zippel 		case S_HEX:
581c955ccafSRoman Zippel 			str = sym_get_string_value(sym);
582c955ccafSRoman Zippel 			if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
583c955ccafSRoman Zippel 				fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
584c955ccafSRoman Zippel 				fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
585c955ccafSRoman Zippel 				break;
586c955ccafSRoman Zippel 			}
587c955ccafSRoman Zippel 		case S_INT:
588c955ccafSRoman Zippel 			str = sym_get_string_value(sym);
589c955ccafSRoman Zippel 			fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
590c955ccafSRoman Zippel 			fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
591c955ccafSRoman Zippel 			break;
592c955ccafSRoman Zippel 		default:
593c955ccafSRoman Zippel 			break;
594c955ccafSRoman Zippel 		}
595c955ccafSRoman Zippel 	}
596c955ccafSRoman Zippel 	fclose(out);
597c955ccafSRoman Zippel 	fclose(out_h);
598c955ccafSRoman Zippel 
599c955ccafSRoman Zippel 	name = getenv("KCONFIG_AUTOHEADER");
600c955ccafSRoman Zippel 	if (!name)
601c955ccafSRoman Zippel 		name = "include/linux/autoconf.h";
602c955ccafSRoman Zippel 	if (rename(".tmpconfig.h", name))
603c955ccafSRoman Zippel 		return 1;
604c955ccafSRoman Zippel 	name = getenv("KCONFIG_AUTOCONFIG");
605c955ccafSRoman Zippel 	if (!name)
606c955ccafSRoman Zippel 		name = "include/config/auto.conf";
607c955ccafSRoman Zippel 	/*
608c955ccafSRoman Zippel 	 * This must be the last step, kbuild has a dependency on auto.conf
609c955ccafSRoman Zippel 	 * and this marks the successful completion of the previous steps.
610c955ccafSRoman Zippel 	 */
611c955ccafSRoman Zippel 	if (rename(".tmpconfig", name))
612c955ccafSRoman Zippel 		return 1;
613c955ccafSRoman Zippel 
614c955ccafSRoman Zippel 	return 0;
615c955ccafSRoman Zippel }
616