xref: /openbmc/linux/scripts/kconfig/confdata.c (revision 67424f61f8132215514e03ce1760d30356ac56d7)
10c874100SMasahiro Yamada // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
6*67424f61SMasahiro Yamada #include <sys/mman.h>
71da177e4SLinus Torvalds #include <sys/stat.h>
81da177e4SLinus Torvalds #include <ctype.h>
994bedecaSArnaud Lacombe #include <errno.h>
102e3646e5SRoman Zippel #include <fcntl.h>
11558e78e3SMasahiro Yamada #include <limits.h>
1210a4b277SArnaud Lacombe #include <stdarg.h>
131da177e4SLinus Torvalds #include <stdio.h>
141da177e4SLinus Torvalds #include <stdlib.h>
151da177e4SLinus Torvalds #include <string.h>
161da177e4SLinus Torvalds #include <time.h>
171da177e4SLinus Torvalds #include <unistd.h>
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include "lkc.h"
201da177e4SLinus Torvalds 
210608182aSMasahiro Yamada /* return true if 'path' exists, false otherwise */
220608182aSMasahiro Yamada static bool is_present(const char *path)
230608182aSMasahiro Yamada {
240608182aSMasahiro Yamada 	struct stat st;
250608182aSMasahiro Yamada 
260608182aSMasahiro Yamada 	return !stat(path, &st);
270608182aSMasahiro Yamada }
280608182aSMasahiro Yamada 
290608182aSMasahiro Yamada /* return true if 'path' exists and it is a directory, false otherwise */
300608182aSMasahiro Yamada static bool is_dir(const char *path)
310608182aSMasahiro Yamada {
320608182aSMasahiro Yamada 	struct stat st;
330608182aSMasahiro Yamada 
340608182aSMasahiro Yamada 	if (stat(path, &st))
350608182aSMasahiro Yamada 		return 0;
360608182aSMasahiro Yamada 
370608182aSMasahiro Yamada 	return S_ISDIR(st.st_mode);
380608182aSMasahiro Yamada }
390608182aSMasahiro Yamada 
40*67424f61SMasahiro Yamada /* return true if the given two files are the same, false otherwise */
41*67424f61SMasahiro Yamada static bool is_same(const char *file1, const char *file2)
42*67424f61SMasahiro Yamada {
43*67424f61SMasahiro Yamada 	int fd1, fd2;
44*67424f61SMasahiro Yamada 	struct stat st1, st2;
45*67424f61SMasahiro Yamada 	void *map1, *map2;
46*67424f61SMasahiro Yamada 	bool ret = false;
47*67424f61SMasahiro Yamada 
48*67424f61SMasahiro Yamada 	fd1 = open(file1, O_RDONLY);
49*67424f61SMasahiro Yamada 	if (fd1 < 0)
50*67424f61SMasahiro Yamada 		return ret;
51*67424f61SMasahiro Yamada 
52*67424f61SMasahiro Yamada 	fd2 = open(file2, O_RDONLY);
53*67424f61SMasahiro Yamada 	if (fd2 < 0)
54*67424f61SMasahiro Yamada 		goto close1;
55*67424f61SMasahiro Yamada 
56*67424f61SMasahiro Yamada 	ret = fstat(fd1, &st1);
57*67424f61SMasahiro Yamada 	if (ret)
58*67424f61SMasahiro Yamada 		goto close2;
59*67424f61SMasahiro Yamada 	ret = fstat(fd2, &st2);
60*67424f61SMasahiro Yamada 	if (ret)
61*67424f61SMasahiro Yamada 		goto close2;
62*67424f61SMasahiro Yamada 
63*67424f61SMasahiro Yamada 	if (st1.st_size != st2.st_size)
64*67424f61SMasahiro Yamada 		goto close2;
65*67424f61SMasahiro Yamada 
66*67424f61SMasahiro Yamada 	map1 = mmap(NULL, st1.st_size, PROT_READ, MAP_PRIVATE, fd1, 0);
67*67424f61SMasahiro Yamada 	if (map1 == MAP_FAILED)
68*67424f61SMasahiro Yamada 		goto close2;
69*67424f61SMasahiro Yamada 
70*67424f61SMasahiro Yamada 	map2 = mmap(NULL, st2.st_size, PROT_READ, MAP_PRIVATE, fd2, 0);
71*67424f61SMasahiro Yamada 	if (map2 == MAP_FAILED)
72*67424f61SMasahiro Yamada 		goto close2;
73*67424f61SMasahiro Yamada 
74*67424f61SMasahiro Yamada 	if (bcmp(map1, map2, st1.st_size))
75*67424f61SMasahiro Yamada 		goto close2;
76*67424f61SMasahiro Yamada 
77*67424f61SMasahiro Yamada 	ret = true;
78*67424f61SMasahiro Yamada close2:
79*67424f61SMasahiro Yamada 	close(fd2);
80*67424f61SMasahiro Yamada close1:
81*67424f61SMasahiro Yamada 	close(fd1);
82*67424f61SMasahiro Yamada 
83*67424f61SMasahiro Yamada 	return ret;
84*67424f61SMasahiro Yamada }
85*67424f61SMasahiro Yamada 
860608182aSMasahiro Yamada /*
870608182aSMasahiro Yamada  * Create the parent directory of the given path.
880608182aSMasahiro Yamada  *
890608182aSMasahiro Yamada  * For example, if 'include/config/auto.conf' is given, create 'include/config'.
900608182aSMasahiro Yamada  */
910608182aSMasahiro Yamada static int make_parent_dir(const char *path)
920608182aSMasahiro Yamada {
930608182aSMasahiro Yamada 	char tmp[PATH_MAX + 1];
940608182aSMasahiro Yamada 	char *p;
950608182aSMasahiro Yamada 
960608182aSMasahiro Yamada 	strncpy(tmp, path, sizeof(tmp));
970608182aSMasahiro Yamada 	tmp[sizeof(tmp) - 1] = 0;
980608182aSMasahiro Yamada 
990608182aSMasahiro Yamada 	/* Remove the base name. Just return if nothing is left */
1000608182aSMasahiro Yamada 	p = strrchr(tmp, '/');
1010608182aSMasahiro Yamada 	if (!p)
1020608182aSMasahiro Yamada 		return 0;
1030608182aSMasahiro Yamada 	*(p + 1) = 0;
1040608182aSMasahiro Yamada 
1050608182aSMasahiro Yamada 	/* Just in case it is an absolute path */
1060608182aSMasahiro Yamada 	p = tmp;
1070608182aSMasahiro Yamada 	while (*p == '/')
1080608182aSMasahiro Yamada 		p++;
1090608182aSMasahiro Yamada 
1100608182aSMasahiro Yamada 	while ((p = strchr(p, '/'))) {
1110608182aSMasahiro Yamada 		*p = 0;
1120608182aSMasahiro Yamada 
1130608182aSMasahiro Yamada 		/* skip if the directory exists */
1140608182aSMasahiro Yamada 		if (!is_dir(tmp) && mkdir(tmp, 0755))
1150608182aSMasahiro Yamada 			return -1;
1160608182aSMasahiro Yamada 
1170608182aSMasahiro Yamada 		*p = '/';
1180608182aSMasahiro Yamada 		while (*p == '/')
1190608182aSMasahiro Yamada 			p++;
1200608182aSMasahiro Yamada 	}
1210608182aSMasahiro Yamada 
1220608182aSMasahiro Yamada 	return 0;
1230608182aSMasahiro Yamada }
1240608182aSMasahiro Yamada 
1251508fec8SMasahiro Yamada static char depfile_path[PATH_MAX];
1261508fec8SMasahiro Yamada static size_t depfile_prefix_len;
1271508fec8SMasahiro Yamada 
1281508fec8SMasahiro Yamada /* touch depfile for symbol 'name' */
1291508fec8SMasahiro Yamada static int conf_touch_dep(const char *name)
1301508fec8SMasahiro Yamada {
1311508fec8SMasahiro Yamada 	int fd, ret;
1321508fec8SMasahiro Yamada 	const char *s;
1331508fec8SMasahiro Yamada 	char *d, c;
1341508fec8SMasahiro Yamada 
1351508fec8SMasahiro Yamada 	/* check overflow: prefix + name + ".h" + '\0' must fit in buffer. */
1361508fec8SMasahiro Yamada 	if (depfile_prefix_len + strlen(name) + 3 > sizeof(depfile_path))
1371508fec8SMasahiro Yamada 		return -1;
1381508fec8SMasahiro Yamada 
1391508fec8SMasahiro Yamada 	d = depfile_path + depfile_prefix_len;
1401508fec8SMasahiro Yamada 	s = name;
1411508fec8SMasahiro Yamada 
1421508fec8SMasahiro Yamada 	while ((c = *s++))
1431508fec8SMasahiro Yamada 		*d++ = (c == '_') ? '/' : tolower(c);
1441508fec8SMasahiro Yamada 	strcpy(d, ".h");
1451508fec8SMasahiro Yamada 
1461508fec8SMasahiro Yamada 	/* Assume directory path already exists. */
1471508fec8SMasahiro Yamada 	fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1481508fec8SMasahiro Yamada 	if (fd == -1) {
1491508fec8SMasahiro Yamada 		if (errno != ENOENT)
1501508fec8SMasahiro Yamada 			return -1;
1511508fec8SMasahiro Yamada 
1521508fec8SMasahiro Yamada 		ret = make_parent_dir(depfile_path);
1531508fec8SMasahiro Yamada 		if (ret)
1541508fec8SMasahiro Yamada 			return ret;
1551508fec8SMasahiro Yamada 
1561508fec8SMasahiro Yamada 		/* Try it again. */
1571508fec8SMasahiro Yamada 		fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1581508fec8SMasahiro Yamada 		if (fd == -1)
1591508fec8SMasahiro Yamada 			return -1;
1601508fec8SMasahiro Yamada 	}
1611508fec8SMasahiro Yamada 	close(fd);
1621508fec8SMasahiro Yamada 
1631508fec8SMasahiro Yamada 	return 0;
1641508fec8SMasahiro Yamada }
1651508fec8SMasahiro Yamada 
166ad8d40cdSMichal Marek struct conf_printer {
167ad8d40cdSMichal Marek 	void (*print_symbol)(FILE *, struct symbol *, const char *, void *);
168ad8d40cdSMichal Marek 	void (*print_comment)(FILE *, const char *, void *);
169ad8d40cdSMichal Marek };
170ad8d40cdSMichal Marek 
171c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
172c1a0f5e3SRoman Zippel 	__attribute__ ((format (printf, 1, 2)));
173c1a0f5e3SRoman Zippel 
17442368c37SMichal Marek static void conf_message(const char *fmt, ...)
17542368c37SMichal Marek 	__attribute__ ((format (printf, 1, 2)));
17642368c37SMichal Marek 
177c1a0f5e3SRoman Zippel static const char *conf_filename;
17884dd95d4SMasahiro Yamada static int conf_lineno, conf_warnings;
179c1a0f5e3SRoman Zippel 
180104daea1SMasahiro Yamada const char conf_defname[] = "arch/$(ARCH)/defconfig";
1811da177e4SLinus Torvalds 
182c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
183c1a0f5e3SRoman Zippel {
184c1a0f5e3SRoman Zippel 	va_list ap;
185c1a0f5e3SRoman Zippel 	va_start(ap, fmt);
186c1a0f5e3SRoman Zippel 	fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
187c1a0f5e3SRoman Zippel 	vfprintf(stderr, fmt, ap);
188c1a0f5e3SRoman Zippel 	fprintf(stderr, "\n");
189c1a0f5e3SRoman Zippel 	va_end(ap);
190c1a0f5e3SRoman Zippel 	conf_warnings++;
191c1a0f5e3SRoman Zippel }
192c1a0f5e3SRoman Zippel 
1935accd7f3SMasahiro Yamada static void conf_default_message_callback(const char *s)
19442368c37SMichal Marek {
19542368c37SMichal Marek 	printf("#\n# ");
1965accd7f3SMasahiro Yamada 	printf("%s", s);
19742368c37SMichal Marek 	printf("\n#\n");
19842368c37SMichal Marek }
19942368c37SMichal Marek 
2005accd7f3SMasahiro Yamada static void (*conf_message_callback)(const char *s) =
20142368c37SMichal Marek 	conf_default_message_callback;
2025accd7f3SMasahiro Yamada void conf_set_message_callback(void (*fn)(const char *s))
20342368c37SMichal Marek {
20442368c37SMichal Marek 	conf_message_callback = fn;
20542368c37SMichal Marek }
20642368c37SMichal Marek 
20742368c37SMichal Marek static void conf_message(const char *fmt, ...)
20842368c37SMichal Marek {
20942368c37SMichal Marek 	va_list ap;
2105accd7f3SMasahiro Yamada 	char buf[4096];
2115accd7f3SMasahiro Yamada 
2125accd7f3SMasahiro Yamada 	if (!conf_message_callback)
2135accd7f3SMasahiro Yamada 		return;
21442368c37SMichal Marek 
21542368c37SMichal Marek 	va_start(ap, fmt);
2165accd7f3SMasahiro Yamada 
2175accd7f3SMasahiro Yamada 	vsnprintf(buf, sizeof(buf), fmt, ap);
2185accd7f3SMasahiro Yamada 	conf_message_callback(buf);
219b6a2ab2cSColin Ian King 	va_end(ap);
22042368c37SMichal Marek }
22142368c37SMichal Marek 
22214cdd3c4SRoman Zippel const char *conf_get_configname(void)
22314cdd3c4SRoman Zippel {
22414cdd3c4SRoman Zippel 	char *name = getenv("KCONFIG_CONFIG");
22514cdd3c4SRoman Zippel 
22614cdd3c4SRoman Zippel 	return name ? name : ".config";
22714cdd3c4SRoman Zippel }
22814cdd3c4SRoman Zippel 
22912122f62SMarkus Heidelberg const char *conf_get_autoconfig_name(void)
23012122f62SMarkus Heidelberg {
23112122f62SMarkus Heidelberg 	char *name = getenv("KCONFIG_AUTOCONFIG");
23212122f62SMarkus Heidelberg 
23312122f62SMarkus Heidelberg 	return name ? name : "include/config/auto.conf";
23412122f62SMarkus Heidelberg }
23512122f62SMarkus Heidelberg 
2361da177e4SLinus Torvalds char *conf_get_default_confname(void)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds 	static char fullname[PATH_MAX+1];
2391da177e4SLinus Torvalds 	char *env, *name;
2401da177e4SLinus Torvalds 
241104daea1SMasahiro Yamada 	name = expand_string(conf_defname);
2421da177e4SLinus Torvalds 	env = getenv(SRCTREE);
2431da177e4SLinus Torvalds 	if (env) {
2441da177e4SLinus Torvalds 		sprintf(fullname, "%s/%s", env, name);
2450608182aSMasahiro Yamada 		if (is_present(fullname))
2461da177e4SLinus Torvalds 			return fullname;
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds 	return name;
2491da177e4SLinus Torvalds }
2501da177e4SLinus Torvalds 
2519c900a9cSSam Ravnborg static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
2529c900a9cSSam Ravnborg {
2539c900a9cSSam Ravnborg 	char *p2;
2549c900a9cSSam Ravnborg 
2559c900a9cSSam Ravnborg 	switch (sym->type) {
2569c900a9cSSam Ravnborg 	case S_TRISTATE:
2579c900a9cSSam Ravnborg 		if (p[0] == 'm') {
2589c900a9cSSam Ravnborg 			sym->def[def].tri = mod;
2599c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2609c900a9cSSam Ravnborg 			break;
2619c900a9cSSam Ravnborg 		}
262d8fc3200SArnaud Lacombe 		/* fall through */
2639c900a9cSSam Ravnborg 	case S_BOOLEAN:
2649c900a9cSSam Ravnborg 		if (p[0] == 'y') {
2659c900a9cSSam Ravnborg 			sym->def[def].tri = yes;
2669c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2679c900a9cSSam Ravnborg 			break;
2689c900a9cSSam Ravnborg 		}
2699c900a9cSSam Ravnborg 		if (p[0] == 'n') {
2709c900a9cSSam Ravnborg 			sym->def[def].tri = no;
2719c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2729c900a9cSSam Ravnborg 			break;
2739c900a9cSSam Ravnborg 		}
27404b19b77SYann E. MORIN 		if (def != S_DEF_AUTO)
27504b19b77SYann E. MORIN 			conf_warning("symbol value '%s' invalid for %s",
27604b19b77SYann E. MORIN 				     p, sym->name);
27775f1468bSArnaud Lacombe 		return 1;
2789c900a9cSSam Ravnborg 	case S_STRING:
2799c900a9cSSam Ravnborg 		if (*p++ != '"')
2809c900a9cSSam Ravnborg 			break;
2819c900a9cSSam Ravnborg 		for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
2829c900a9cSSam Ravnborg 			if (*p2 == '"') {
2839c900a9cSSam Ravnborg 				*p2 = 0;
2849c900a9cSSam Ravnborg 				break;
2859c900a9cSSam Ravnborg 			}
2869c900a9cSSam Ravnborg 			memmove(p2, p2 + 1, strlen(p2));
2879c900a9cSSam Ravnborg 		}
2889c900a9cSSam Ravnborg 		if (!p2) {
28904b19b77SYann E. MORIN 			if (def != S_DEF_AUTO)
2909c900a9cSSam Ravnborg 				conf_warning("invalid string found");
2919c900a9cSSam Ravnborg 			return 1;
2929c900a9cSSam Ravnborg 		}
293d8fc3200SArnaud Lacombe 		/* fall through */
2949c900a9cSSam Ravnborg 	case S_INT:
2959c900a9cSSam Ravnborg 	case S_HEX:
2969c900a9cSSam Ravnborg 		if (sym_string_valid(sym, p)) {
297cd81fc82SMasahiro Yamada 			sym->def[def].val = xstrdup(p);
2989c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2999c900a9cSSam Ravnborg 		} else {
30004b19b77SYann E. MORIN 			if (def != S_DEF_AUTO)
30104b19b77SYann E. MORIN 				conf_warning("symbol value '%s' invalid for %s",
30204b19b77SYann E. MORIN 					     p, sym->name);
3039c900a9cSSam Ravnborg 			return 1;
3049c900a9cSSam Ravnborg 		}
3059c900a9cSSam Ravnborg 		break;
3069c900a9cSSam Ravnborg 	default:
3079c900a9cSSam Ravnborg 		;
3089c900a9cSSam Ravnborg 	}
3099c900a9cSSam Ravnborg 	return 0;
3109c900a9cSSam Ravnborg }
3119c900a9cSSam Ravnborg 
3121a7a8c6fSCody Schafer #define LINE_GROWTH 16
3131a7a8c6fSCody Schafer static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
3141a7a8c6fSCody Schafer {
3151a7a8c6fSCody Schafer 	char *nline;
3161a7a8c6fSCody Schafer 	size_t new_size = slen + 1;
3171a7a8c6fSCody Schafer 	if (new_size > *n) {
3181a7a8c6fSCody Schafer 		new_size += LINE_GROWTH - 1;
3191a7a8c6fSCody Schafer 		new_size *= 2;
320d717f24dSMasahiro Yamada 		nline = xrealloc(*lineptr, new_size);
3211a7a8c6fSCody Schafer 		if (!nline)
3221a7a8c6fSCody Schafer 			return -1;
3231a7a8c6fSCody Schafer 
3241a7a8c6fSCody Schafer 		*lineptr = nline;
3251a7a8c6fSCody Schafer 		*n = new_size;
3261a7a8c6fSCody Schafer 	}
3271a7a8c6fSCody Schafer 
3281a7a8c6fSCody Schafer 	(*lineptr)[slen] = c;
3291a7a8c6fSCody Schafer 
3301a7a8c6fSCody Schafer 	return 0;
3311a7a8c6fSCody Schafer }
3321a7a8c6fSCody Schafer 
3331a7a8c6fSCody Schafer static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
3341a7a8c6fSCody Schafer {
3351a7a8c6fSCody Schafer 	char *line = *lineptr;
3361a7a8c6fSCody Schafer 	size_t slen = 0;
3371a7a8c6fSCody Schafer 
3381a7a8c6fSCody Schafer 	for (;;) {
3391a7a8c6fSCody Schafer 		int c = getc(stream);
3401a7a8c6fSCody Schafer 
3411a7a8c6fSCody Schafer 		switch (c) {
3421a7a8c6fSCody Schafer 		case '\n':
3431a7a8c6fSCody Schafer 			if (add_byte(c, &line, slen, n) < 0)
3441a7a8c6fSCody Schafer 				goto e_out;
3451a7a8c6fSCody Schafer 			slen++;
3461a7a8c6fSCody Schafer 			/* fall through */
3471a7a8c6fSCody Schafer 		case EOF:
3481a7a8c6fSCody Schafer 			if (add_byte('\0', &line, slen, n) < 0)
3491a7a8c6fSCody Schafer 				goto e_out;
3501a7a8c6fSCody Schafer 			*lineptr = line;
3511a7a8c6fSCody Schafer 			if (slen == 0)
3521a7a8c6fSCody Schafer 				return -1;
3531a7a8c6fSCody Schafer 			return slen;
3541a7a8c6fSCody Schafer 		default:
3551a7a8c6fSCody Schafer 			if (add_byte(c, &line, slen, n) < 0)
3561a7a8c6fSCody Schafer 				goto e_out;
3571a7a8c6fSCody Schafer 			slen++;
3581a7a8c6fSCody Schafer 		}
3591a7a8c6fSCody Schafer 	}
3601a7a8c6fSCody Schafer 
3611a7a8c6fSCody Schafer e_out:
3621a7a8c6fSCody Schafer 	line[slen-1] = '\0';
3631a7a8c6fSCody Schafer 	*lineptr = line;
3641a7a8c6fSCody Schafer 	return -1;
3651a7a8c6fSCody Schafer }
3661a7a8c6fSCody Schafer 
367669bfad9SRoman Zippel int conf_read_simple(const char *name, int def)
3681da177e4SLinus Torvalds {
3691da177e4SLinus Torvalds 	FILE *in = NULL;
3701a7a8c6fSCody Schafer 	char   *line = NULL;
3711a7a8c6fSCody Schafer 	size_t  line_asize = 0;
3721da177e4SLinus Torvalds 	char *p, *p2;
3731da177e4SLinus Torvalds 	struct symbol *sym;
374669bfad9SRoman Zippel 	int i, def_flags;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	if (name) {
3771da177e4SLinus Torvalds 		in = zconf_fopen(name);
3781da177e4SLinus Torvalds 	} else {
379face4374SRoman Zippel 		struct property *prop;
380face4374SRoman Zippel 
38114cdd3c4SRoman Zippel 		name = conf_get_configname();
382ddc97cacSRoman Zippel 		in = zconf_fopen(name);
383ddc97cacSRoman Zippel 		if (in)
384ddc97cacSRoman Zippel 			goto load;
385bfc10001SKarsten Wiese 		sym_add_change_count(1);
3866b87b70cSAl Viro 		if (!sym_defconfig_list)
387face4374SRoman Zippel 			return 1;
388face4374SRoman Zippel 
389face4374SRoman Zippel 		for_all_defaults(sym_defconfig_list, prop) {
390face4374SRoman Zippel 			if (expr_calc_value(prop->visible.expr) == no ||
391face4374SRoman Zippel 			    prop->expr->type != E_SYMBOL)
392face4374SRoman Zippel 				continue;
393104daea1SMasahiro Yamada 			sym_calc_value(prop->expr->left.sym);
394104daea1SMasahiro Yamada 			name = sym_get_string_value(prop->expr->left.sym);
3951da177e4SLinus Torvalds 			in = zconf_fopen(name);
3961da177e4SLinus Torvalds 			if (in) {
397694c49a7SSam Ravnborg 				conf_message("using defaults found in %s",
39842368c37SMichal Marek 					 name);
399ddc97cacSRoman Zippel 				goto load;
4001da177e4SLinus Torvalds 			}
4011da177e4SLinus Torvalds 		}
4021da177e4SLinus Torvalds 	}
4031da177e4SLinus Torvalds 	if (!in)
4041da177e4SLinus Torvalds 		return 1;
4051da177e4SLinus Torvalds 
406ddc97cacSRoman Zippel load:
407c1a0f5e3SRoman Zippel 	conf_filename = name;
408c1a0f5e3SRoman Zippel 	conf_lineno = 0;
409c1a0f5e3SRoman Zippel 	conf_warnings = 0;
410c1a0f5e3SRoman Zippel 
411669bfad9SRoman Zippel 	def_flags = SYMBOL_DEF << def;
4121da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
413669bfad9SRoman Zippel 		sym->flags |= SYMBOL_CHANGED;
414669bfad9SRoman Zippel 		sym->flags &= ~(def_flags|SYMBOL_VALID);
415490f1617SYann E. MORIN 		if (sym_is_choice(sym))
416490f1617SYann E. MORIN 			sym->flags |= def_flags;
4171da177e4SLinus Torvalds 		switch (sym->type) {
4181da177e4SLinus Torvalds 		case S_INT:
4191da177e4SLinus Torvalds 		case S_HEX:
4201da177e4SLinus Torvalds 		case S_STRING:
421669bfad9SRoman Zippel 			if (sym->def[def].val)
422669bfad9SRoman Zippel 				free(sym->def[def].val);
423d8fc3200SArnaud Lacombe 			/* fall through */
4241da177e4SLinus Torvalds 		default:
425669bfad9SRoman Zippel 			sym->def[def].val = NULL;
426669bfad9SRoman Zippel 			sym->def[def].tri = no;
4271da177e4SLinus Torvalds 		}
4281da177e4SLinus Torvalds 	}
4291da177e4SLinus Torvalds 
4301a7a8c6fSCody Schafer 	while (compat_getline(&line, &line_asize, in) != -1) {
431c1a0f5e3SRoman Zippel 		conf_lineno++;
4321da177e4SLinus Torvalds 		sym = NULL;
4338baefd30SArnaud Lacombe 		if (line[0] == '#') {
434ffb5957bSArnaud Lacombe 			if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
4351da177e4SLinus Torvalds 				continue;
436ffb5957bSArnaud Lacombe 			p = strchr(line + 2 + strlen(CONFIG_), ' ');
4371da177e4SLinus Torvalds 			if (!p)
4381da177e4SLinus Torvalds 				continue;
4391da177e4SLinus Torvalds 			*p++ = 0;
4401da177e4SLinus Torvalds 			if (strncmp(p, "is not set", 10))
4411da177e4SLinus Torvalds 				continue;
442669bfad9SRoman Zippel 			if (def == S_DEF_USER) {
443ffb5957bSArnaud Lacombe 				sym = sym_find(line + 2 + strlen(CONFIG_));
444661b0680Szippel@linux-m68k.org 				if (!sym) {
445661b0680Szippel@linux-m68k.org 					sym_add_change_count(1);
44675889e9bSMasahiro Yamada 					continue;
447661b0680Szippel@linux-m68k.org 				}
448669bfad9SRoman Zippel 			} else {
449ffb5957bSArnaud Lacombe 				sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
450669bfad9SRoman Zippel 				if (sym->type == S_UNKNOWN)
451669bfad9SRoman Zippel 					sym->type = S_BOOLEAN;
452669bfad9SRoman Zippel 			}
453669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
454d84876f9SJan Engelhardt 				conf_warning("override: reassigning to symbol %s", sym->name);
4551da177e4SLinus Torvalds 			}
4561da177e4SLinus Torvalds 			switch (sym->type) {
4571da177e4SLinus Torvalds 			case S_BOOLEAN:
4581da177e4SLinus Torvalds 			case S_TRISTATE:
459669bfad9SRoman Zippel 				sym->def[def].tri = no;
460669bfad9SRoman Zippel 				sym->flags |= def_flags;
4611da177e4SLinus Torvalds 				break;
4621da177e4SLinus Torvalds 			default:
4631da177e4SLinus Torvalds 				;
4641da177e4SLinus Torvalds 			}
465ffb5957bSArnaud Lacombe 		} else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
466ffb5957bSArnaud Lacombe 			p = strchr(line + strlen(CONFIG_), '=');
4671da177e4SLinus Torvalds 			if (!p)
4681da177e4SLinus Torvalds 				continue;
4691da177e4SLinus Torvalds 			*p++ = 0;
4701da177e4SLinus Torvalds 			p2 = strchr(p, '\n');
471d3660a8cSMatthew Wilcox 			if (p2) {
472d3660a8cSMatthew Wilcox 				*p2-- = 0;
473d3660a8cSMatthew Wilcox 				if (*p2 == '\r')
4741da177e4SLinus Torvalds 					*p2 = 0;
475d3660a8cSMatthew Wilcox 			}
4762aabbed6SMasahiro Yamada 
477ffb5957bSArnaud Lacombe 			sym = sym_find(line + strlen(CONFIG_));
478661b0680Szippel@linux-m68k.org 			if (!sym) {
4792aabbed6SMasahiro Yamada 				if (def == S_DEF_AUTO)
4802aabbed6SMasahiro Yamada 					/*
4812aabbed6SMasahiro Yamada 					 * Reading from include/config/auto.conf
4822aabbed6SMasahiro Yamada 					 * If CONFIG_FOO previously existed in
4832aabbed6SMasahiro Yamada 					 * auto.conf but it is missing now,
4842aabbed6SMasahiro Yamada 					 * include/config/foo.h must be touched.
4852aabbed6SMasahiro Yamada 					 */
4862aabbed6SMasahiro Yamada 					conf_touch_dep(line + strlen(CONFIG_));
4872aabbed6SMasahiro Yamada 				else
488661b0680Szippel@linux-m68k.org 					sym_add_change_count(1);
48975889e9bSMasahiro Yamada 				continue;
490661b0680Szippel@linux-m68k.org 			}
4912aabbed6SMasahiro Yamada 
492669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
493d84876f9SJan Engelhardt 				conf_warning("override: reassigning to symbol %s", sym->name);
4941da177e4SLinus Torvalds 			}
4959c900a9cSSam Ravnborg 			if (conf_set_sym_val(sym, def, def_flags, p))
496c1a0f5e3SRoman Zippel 				continue;
4978baefd30SArnaud Lacombe 		} else {
4988baefd30SArnaud Lacombe 			if (line[0] != '\r' && line[0] != '\n')
499a4663911SPaul Bolle 				conf_warning("unexpected data: %.*s",
500a4663911SPaul Bolle 					     (int)strcspn(line, "\r\n"), line);
501a4663911SPaul Bolle 
5021da177e4SLinus Torvalds 			continue;
5031da177e4SLinus Torvalds 		}
50475889e9bSMasahiro Yamada 
5051da177e4SLinus Torvalds 		if (sym && sym_is_choice_value(sym)) {
5061da177e4SLinus Torvalds 			struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
507669bfad9SRoman Zippel 			switch (sym->def[def].tri) {
5081da177e4SLinus Torvalds 			case no:
5091da177e4SLinus Torvalds 				break;
5101da177e4SLinus Torvalds 			case mod:
511669bfad9SRoman Zippel 				if (cs->def[def].tri == yes) {
512c1a0f5e3SRoman Zippel 					conf_warning("%s creates inconsistent choice state", sym->name);
513490f1617SYann E. MORIN 					cs->flags &= ~def_flags;
514c1a0f5e3SRoman Zippel 				}
5151da177e4SLinus Torvalds 				break;
5161da177e4SLinus Torvalds 			case yes:
517d84876f9SJan Engelhardt 				if (cs->def[def].tri != no)
518d84876f9SJan Engelhardt 					conf_warning("override: %s changes choice state", sym->name);
519669bfad9SRoman Zippel 				cs->def[def].val = sym;
5201da177e4SLinus Torvalds 				break;
5211da177e4SLinus Torvalds 			}
522d6ee3576SSam Ravnborg 			cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
5231da177e4SLinus Torvalds 		}
5241da177e4SLinus Torvalds 	}
5251a7a8c6fSCody Schafer 	free(line);
5261da177e4SLinus Torvalds 	fclose(in);
52790389160SRoman Zippel 	return 0;
52890389160SRoman Zippel }
52990389160SRoman Zippel 
53090389160SRoman Zippel int conf_read(const char *name)
53190389160SRoman Zippel {
5325d09598dSArnaud Lacombe 	struct symbol *sym;
53384dd95d4SMasahiro Yamada 	int conf_unsaved = 0;
5345d09598dSArnaud Lacombe 	int i;
53590389160SRoman Zippel 
536bfc10001SKarsten Wiese 	sym_set_change_count(0);
537ddc97cacSRoman Zippel 
5386b87b70cSAl Viro 	if (conf_read_simple(name, S_DEF_USER)) {
5396b87b70cSAl Viro 		sym_calc_value(modules_sym);
54090389160SRoman Zippel 		return 1;
5416b87b70cSAl Viro 	}
5426b87b70cSAl Viro 
5436b87b70cSAl Viro 	sym_calc_value(modules_sym);
54490389160SRoman Zippel 
5451da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
5461da177e4SLinus Torvalds 		sym_calc_value(sym);
547693359f7SDirk Gouders 		if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
5485d09598dSArnaud Lacombe 			continue;
549c1a0f5e3SRoman Zippel 		if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
550c1a0f5e3SRoman Zippel 			/* check that calculated value agrees with saved value */
551c1a0f5e3SRoman Zippel 			switch (sym->type) {
552c1a0f5e3SRoman Zippel 			case S_BOOLEAN:
553c1a0f5e3SRoman Zippel 			case S_TRISTATE:
5540c1822e6SRoman Zippel 				if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
555c1a0f5e3SRoman Zippel 					break;
556c1a0f5e3SRoman Zippel 				if (!sym_is_choice(sym))
5575d09598dSArnaud Lacombe 					continue;
558d8fc3200SArnaud Lacombe 				/* fall through */
559c1a0f5e3SRoman Zippel 			default:
5600c1822e6SRoman Zippel 				if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
5615d09598dSArnaud Lacombe 					continue;
562c1a0f5e3SRoman Zippel 				break;
563c1a0f5e3SRoman Zippel 			}
564c1a0f5e3SRoman Zippel 		} else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
565c1a0f5e3SRoman Zippel 			/* no previous value and not saved */
5665d09598dSArnaud Lacombe 			continue;
567c1a0f5e3SRoman Zippel 		conf_unsaved++;
568c1a0f5e3SRoman Zippel 		/* maybe print value in verbose mode... */
5691da177e4SLinus Torvalds 	}
5701da177e4SLinus Torvalds 
571d8982ba1SRoman Zippel 	for_all_symbols(i, sym) {
572d8982ba1SRoman Zippel 		if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
573d8982ba1SRoman Zippel 			/* Reset values of generates values, so they'll appear
574d8982ba1SRoman Zippel 			 * as new, if they should become visible, but that
575d8982ba1SRoman Zippel 			 * doesn't quite work if the Kconfig and the saved
576d8982ba1SRoman Zippel 			 * configuration disagree.
577d8982ba1SRoman Zippel 			 */
578d8982ba1SRoman Zippel 			if (sym->visible == no && !conf_unsaved)
579d8982ba1SRoman Zippel 				sym->flags &= ~SYMBOL_DEF_USER;
580d8982ba1SRoman Zippel 			switch (sym->type) {
581d8982ba1SRoman Zippel 			case S_STRING:
582d8982ba1SRoman Zippel 			case S_INT:
583d8982ba1SRoman Zippel 			case S_HEX:
584d8982ba1SRoman Zippel 				/* Reset a string value if it's out of range */
585d8982ba1SRoman Zippel 				if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
586d8982ba1SRoman Zippel 					break;
587d8982ba1SRoman Zippel 				sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
588d8982ba1SRoman Zippel 				conf_unsaved++;
589d8982ba1SRoman Zippel 				break;
590d8982ba1SRoman Zippel 			default:
591d8982ba1SRoman Zippel 				break;
592d8982ba1SRoman Zippel 			}
593d8982ba1SRoman Zippel 		}
594d8982ba1SRoman Zippel 	}
595d8982ba1SRoman Zippel 
596bfc10001SKarsten Wiese 	sym_add_change_count(conf_warnings || conf_unsaved);
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	return 0;
5991da177e4SLinus Torvalds }
6001da177e4SLinus Torvalds 
601e54e692bSArnaud Lacombe /*
602e54e692bSArnaud Lacombe  * Kconfig configuration printer
603e54e692bSArnaud Lacombe  *
604e54e692bSArnaud Lacombe  * This printer is used when generating the resulting configuration after
605e54e692bSArnaud Lacombe  * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
606e54e692bSArnaud Lacombe  * passing a non-NULL argument to the printer.
607e54e692bSArnaud Lacombe  *
608e54e692bSArnaud Lacombe  */
609e54e692bSArnaud Lacombe static void
610e54e692bSArnaud Lacombe kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
61149192f26SSam Ravnborg {
61249192f26SSam Ravnborg 
6130dce6310SArnaud Lacombe 	switch (sym->type) {
61449192f26SSam Ravnborg 	case S_BOOLEAN:
61549192f26SSam Ravnborg 	case S_TRISTATE:
616e54e692bSArnaud Lacombe 		if (*value == 'n') {
617e54e692bSArnaud Lacombe 			bool skip_unset = (arg != NULL);
618e54e692bSArnaud Lacombe 
619e54e692bSArnaud Lacombe 			if (!skip_unset)
620e54e692bSArnaud Lacombe 				fprintf(fp, "# %s%s is not set\n",
621ffb5957bSArnaud Lacombe 				    CONFIG_, sym->name);
622e54e692bSArnaud Lacombe 			return;
62349192f26SSam Ravnborg 		}
62449192f26SSam Ravnborg 		break;
625e54e692bSArnaud Lacombe 	default:
62649192f26SSam Ravnborg 		break;
627e54e692bSArnaud Lacombe 	}
628e54e692bSArnaud Lacombe 
629e54e692bSArnaud Lacombe 	fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
630e54e692bSArnaud Lacombe }
631e54e692bSArnaud Lacombe 
632e54e692bSArnaud Lacombe static void
633e54e692bSArnaud Lacombe kconfig_print_comment(FILE *fp, const char *value, void *arg)
634e54e692bSArnaud Lacombe {
635e54e692bSArnaud Lacombe 	const char *p = value;
636e54e692bSArnaud Lacombe 	size_t l;
637e54e692bSArnaud Lacombe 
638e54e692bSArnaud Lacombe 	for (;;) {
639e54e692bSArnaud Lacombe 		l = strcspn(p, "\n");
640e54e692bSArnaud Lacombe 		fprintf(fp, "#");
641e54e692bSArnaud Lacombe 		if (l) {
642e54e692bSArnaud Lacombe 			fprintf(fp, " ");
64370cc01e7SPeter Foley 			xfwrite(p, l, 1, fp);
644e54e692bSArnaud Lacombe 			p += l;
645e54e692bSArnaud Lacombe 		}
646e54e692bSArnaud Lacombe 		fprintf(fp, "\n");
647e54e692bSArnaud Lacombe 		if (*p++ == '\0')
64849192f26SSam Ravnborg 			break;
649e54e692bSArnaud Lacombe 	}
650e54e692bSArnaud Lacombe }
651e54e692bSArnaud Lacombe 
652e54e692bSArnaud Lacombe static struct conf_printer kconfig_printer_cb =
653e54e692bSArnaud Lacombe {
654e54e692bSArnaud Lacombe 	.print_symbol = kconfig_print_symbol,
655e54e692bSArnaud Lacombe 	.print_comment = kconfig_print_comment,
656e54e692bSArnaud Lacombe };
657e54e692bSArnaud Lacombe 
658e54e692bSArnaud Lacombe /*
659e54e692bSArnaud Lacombe  * Header printer
660e54e692bSArnaud Lacombe  *
661e54e692bSArnaud Lacombe  * This printer is used when generating the `include/generated/autoconf.h' file.
662e54e692bSArnaud Lacombe  */
663e54e692bSArnaud Lacombe static void
664e54e692bSArnaud Lacombe header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
665e54e692bSArnaud Lacombe {
666e54e692bSArnaud Lacombe 
667e54e692bSArnaud Lacombe 	switch (sym->type) {
668e54e692bSArnaud Lacombe 	case S_BOOLEAN:
669eb4cf5a6SArnaud Lacombe 	case S_TRISTATE: {
670eb4cf5a6SArnaud Lacombe 		const char *suffix = "";
671eb4cf5a6SArnaud Lacombe 
672e54e692bSArnaud Lacombe 		switch (*value) {
673e54e692bSArnaud Lacombe 		case 'n':
6742a11c8eaSMichal Marek 			break;
675e54e692bSArnaud Lacombe 		case 'm':
676e54e692bSArnaud Lacombe 			suffix = "_MODULE";
677eb4cf5a6SArnaud Lacombe 			/* fall through */
678e54e692bSArnaud Lacombe 		default:
6792a11c8eaSMichal Marek 			fprintf(fp, "#define %s%s%s 1\n",
6802a11c8eaSMichal Marek 			    CONFIG_, sym->name, suffix);
681e54e692bSArnaud Lacombe 		}
682eb4cf5a6SArnaud Lacombe 		break;
683eb4cf5a6SArnaud Lacombe 	}
684eb4cf5a6SArnaud Lacombe 	case S_HEX: {
685eb4cf5a6SArnaud Lacombe 		const char *prefix = "";
686eb4cf5a6SArnaud Lacombe 
687eb4cf5a6SArnaud Lacombe 		if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
688eb4cf5a6SArnaud Lacombe 			prefix = "0x";
689eb4cf5a6SArnaud Lacombe 		fprintf(fp, "#define %s%s %s%s\n",
690eb4cf5a6SArnaud Lacombe 		    CONFIG_, sym->name, prefix, value);
691eb4cf5a6SArnaud Lacombe 		break;
692eb4cf5a6SArnaud Lacombe 	}
693eb4cf5a6SArnaud Lacombe 	case S_STRING:
694eb4cf5a6SArnaud Lacombe 	case S_INT:
695eb4cf5a6SArnaud Lacombe 		fprintf(fp, "#define %s%s %s\n",
696eb4cf5a6SArnaud Lacombe 		    CONFIG_, sym->name, value);
697e54e692bSArnaud Lacombe 		break;
698e54e692bSArnaud Lacombe 	default:
699e54e692bSArnaud Lacombe 		break;
700e54e692bSArnaud Lacombe 	}
701e54e692bSArnaud Lacombe 
702e54e692bSArnaud Lacombe }
703e54e692bSArnaud Lacombe 
704e54e692bSArnaud Lacombe static void
705e54e692bSArnaud Lacombe header_print_comment(FILE *fp, const char *value, void *arg)
706e54e692bSArnaud Lacombe {
707e54e692bSArnaud Lacombe 	const char *p = value;
708e54e692bSArnaud Lacombe 	size_t l;
709e54e692bSArnaud Lacombe 
710e54e692bSArnaud Lacombe 	fprintf(fp, "/*\n");
711e54e692bSArnaud Lacombe 	for (;;) {
712e54e692bSArnaud Lacombe 		l = strcspn(p, "\n");
713e54e692bSArnaud Lacombe 		fprintf(fp, " *");
714e54e692bSArnaud Lacombe 		if (l) {
715e54e692bSArnaud Lacombe 			fprintf(fp, " ");
71670cc01e7SPeter Foley 			xfwrite(p, l, 1, fp);
717e54e692bSArnaud Lacombe 			p += l;
718e54e692bSArnaud Lacombe 		}
719e54e692bSArnaud Lacombe 		fprintf(fp, "\n");
720e54e692bSArnaud Lacombe 		if (*p++ == '\0')
721e54e692bSArnaud Lacombe 			break;
722e54e692bSArnaud Lacombe 	}
723e54e692bSArnaud Lacombe 	fprintf(fp, " */\n");
724e54e692bSArnaud Lacombe }
725e54e692bSArnaud Lacombe 
726e54e692bSArnaud Lacombe static struct conf_printer header_printer_cb =
727e54e692bSArnaud Lacombe {
728e54e692bSArnaud Lacombe 	.print_symbol = header_print_symbol,
729e54e692bSArnaud Lacombe 	.print_comment = header_print_comment,
730e54e692bSArnaud Lacombe };
731e54e692bSArnaud Lacombe 
732e54e692bSArnaud Lacombe /*
733e54e692bSArnaud Lacombe  * Tristate printer
734e54e692bSArnaud Lacombe  *
735e54e692bSArnaud Lacombe  * This printer is used when generating the `include/config/tristate.conf' file.
736e54e692bSArnaud Lacombe  */
737e54e692bSArnaud Lacombe static void
738e54e692bSArnaud Lacombe tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
739e54e692bSArnaud Lacombe {
740e54e692bSArnaud Lacombe 
741e54e692bSArnaud Lacombe 	if (sym->type == S_TRISTATE && *value != 'n')
742e54e692bSArnaud Lacombe 		fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
743e54e692bSArnaud Lacombe }
744e54e692bSArnaud Lacombe 
745e54e692bSArnaud Lacombe static struct conf_printer tristate_printer_cb =
746e54e692bSArnaud Lacombe {
747e54e692bSArnaud Lacombe 	.print_symbol = tristate_print_symbol,
748e54e692bSArnaud Lacombe 	.print_comment = kconfig_print_comment,
749e54e692bSArnaud Lacombe };
750e54e692bSArnaud Lacombe 
751e54e692bSArnaud Lacombe static void conf_write_symbol(FILE *fp, struct symbol *sym,
752e54e692bSArnaud Lacombe 			      struct conf_printer *printer, void *printer_arg)
753e54e692bSArnaud Lacombe {
754e54e692bSArnaud Lacombe 	const char *str;
755e54e692bSArnaud Lacombe 
756e54e692bSArnaud Lacombe 	switch (sym->type) {
75749192f26SSam Ravnborg 	case S_UNKNOWN:
75849192f26SSam Ravnborg 		break;
759e54e692bSArnaud Lacombe 	case S_STRING:
760e54e692bSArnaud Lacombe 		str = sym_get_string_value(sym);
761e54e692bSArnaud Lacombe 		str = sym_escape_string_value(str);
762e54e692bSArnaud Lacombe 		printer->print_symbol(fp, sym, str, printer_arg);
763e54e692bSArnaud Lacombe 		free((void *)str);
764e54e692bSArnaud Lacombe 		break;
765e54e692bSArnaud Lacombe 	default:
766e54e692bSArnaud Lacombe 		str = sym_get_string_value(sym);
767e54e692bSArnaud Lacombe 		printer->print_symbol(fp, sym, str, printer_arg);
76849192f26SSam Ravnborg 	}
76949192f26SSam Ravnborg }
77049192f26SSam Ravnborg 
771e54e692bSArnaud Lacombe static void
772e54e692bSArnaud Lacombe conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
773e54e692bSArnaud Lacombe {
774e54e692bSArnaud Lacombe 	char buf[256];
775e54e692bSArnaud Lacombe 
776e54e692bSArnaud Lacombe 	snprintf(buf, sizeof(buf),
777e54e692bSArnaud Lacombe 	    "\n"
778e54e692bSArnaud Lacombe 	    "Automatically generated file; DO NOT EDIT.\n"
779e54e692bSArnaud Lacombe 	    "%s\n",
780e54e692bSArnaud Lacombe 	    rootmenu.prompt->text);
781e54e692bSArnaud Lacombe 
782e54e692bSArnaud Lacombe 	printer->print_comment(fp, buf, printer_arg);
783e54e692bSArnaud Lacombe }
784e54e692bSArnaud Lacombe 
7857cf3d73bSSam Ravnborg /*
7867cf3d73bSSam Ravnborg  * Write out a minimal config.
7877cf3d73bSSam Ravnborg  * All values that has default values are skipped as this is redundant.
7887cf3d73bSSam Ravnborg  */
7897cf3d73bSSam Ravnborg int conf_write_defconfig(const char *filename)
7907cf3d73bSSam Ravnborg {
7917cf3d73bSSam Ravnborg 	struct symbol *sym;
7927cf3d73bSSam Ravnborg 	struct menu *menu;
7937cf3d73bSSam Ravnborg 	FILE *out;
7947cf3d73bSSam Ravnborg 
7957cf3d73bSSam Ravnborg 	out = fopen(filename, "w");
7967cf3d73bSSam Ravnborg 	if (!out)
7977cf3d73bSSam Ravnborg 		return 1;
7987cf3d73bSSam Ravnborg 
7997cf3d73bSSam Ravnborg 	sym_clear_all_valid();
8007cf3d73bSSam Ravnborg 
8017cf3d73bSSam Ravnborg 	/* Traverse all menus to find all relevant symbols */
8027cf3d73bSSam Ravnborg 	menu = rootmenu.list;
8037cf3d73bSSam Ravnborg 
8047cf3d73bSSam Ravnborg 	while (menu != NULL)
8057cf3d73bSSam Ravnborg 	{
8067cf3d73bSSam Ravnborg 		sym = menu->sym;
8077cf3d73bSSam Ravnborg 		if (sym == NULL) {
8087cf3d73bSSam Ravnborg 			if (!menu_is_visible(menu))
8097cf3d73bSSam Ravnborg 				goto next_menu;
8107cf3d73bSSam Ravnborg 		} else if (!sym_is_choice(sym)) {
8117cf3d73bSSam Ravnborg 			sym_calc_value(sym);
8127cf3d73bSSam Ravnborg 			if (!(sym->flags & SYMBOL_WRITE))
8137cf3d73bSSam Ravnborg 				goto next_menu;
8147cf3d73bSSam Ravnborg 			sym->flags &= ~SYMBOL_WRITE;
8157cf3d73bSSam Ravnborg 			/* If we cannot change the symbol - skip */
8167cf3d73bSSam Ravnborg 			if (!sym_is_changable(sym))
8177cf3d73bSSam Ravnborg 				goto next_menu;
8187cf3d73bSSam Ravnborg 			/* If symbol equals to default value - skip */
8197cf3d73bSSam Ravnborg 			if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
8207cf3d73bSSam Ravnborg 				goto next_menu;
8217cf3d73bSSam Ravnborg 
8227cf3d73bSSam Ravnborg 			/*
8237cf3d73bSSam Ravnborg 			 * If symbol is a choice value and equals to the
8247cf3d73bSSam Ravnborg 			 * default for a choice - skip.
82584062dd3SSam Ravnborg 			 * But only if value is bool and equal to "y" and
82684062dd3SSam Ravnborg 			 * choice is not "optional".
82784062dd3SSam Ravnborg 			 * (If choice is "optional" then all values can be "n")
8287cf3d73bSSam Ravnborg 			 */
8297cf3d73bSSam Ravnborg 			if (sym_is_choice_value(sym)) {
8307cf3d73bSSam Ravnborg 				struct symbol *cs;
8317cf3d73bSSam Ravnborg 				struct symbol *ds;
8327cf3d73bSSam Ravnborg 
8337cf3d73bSSam Ravnborg 				cs = prop_get_symbol(sym_get_choice_prop(sym));
8347cf3d73bSSam Ravnborg 				ds = sym_choice_default(cs);
83584062dd3SSam Ravnborg 				if (!sym_is_optional(cs) && sym == ds) {
836801690caSSam Ravnborg 					if ((sym->type == S_BOOLEAN) &&
8377cf3d73bSSam Ravnborg 					    sym_get_tristate_value(sym) == yes)
8387cf3d73bSSam Ravnborg 						goto next_menu;
8397cf3d73bSSam Ravnborg 				}
8407cf3d73bSSam Ravnborg 			}
841e54e692bSArnaud Lacombe 			conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
8427cf3d73bSSam Ravnborg 		}
8437cf3d73bSSam Ravnborg next_menu:
8447cf3d73bSSam Ravnborg 		if (menu->list != NULL) {
8457cf3d73bSSam Ravnborg 			menu = menu->list;
8467cf3d73bSSam Ravnborg 		}
8477cf3d73bSSam Ravnborg 		else if (menu->next != NULL) {
8487cf3d73bSSam Ravnborg 			menu = menu->next;
8497cf3d73bSSam Ravnborg 		} else {
8507cf3d73bSSam Ravnborg 			while ((menu = menu->parent)) {
8517cf3d73bSSam Ravnborg 				if (menu->next != NULL) {
8527cf3d73bSSam Ravnborg 					menu = menu->next;
8537cf3d73bSSam Ravnborg 					break;
8547cf3d73bSSam Ravnborg 				}
8557cf3d73bSSam Ravnborg 			}
8567cf3d73bSSam Ravnborg 		}
8577cf3d73bSSam Ravnborg 	}
8587cf3d73bSSam Ravnborg 	fclose(out);
8597cf3d73bSSam Ravnborg 	return 0;
8607cf3d73bSSam Ravnborg }
8617cf3d73bSSam Ravnborg 
8621da177e4SLinus Torvalds int conf_write(const char *name)
8631da177e4SLinus Torvalds {
864c955ccafSRoman Zippel 	FILE *out;
8651da177e4SLinus Torvalds 	struct symbol *sym;
8661da177e4SLinus Torvalds 	struct menu *menu;
8671da177e4SLinus Torvalds 	const char *str;
868ceb7f329SMasahiro Yamada 	char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
8691da177e4SLinus Torvalds 	char *env;
8701da177e4SLinus Torvalds 
871ceb7f329SMasahiro Yamada 	if (!name)
872ceb7f329SMasahiro Yamada 		name = conf_get_configname();
873ceb7f329SMasahiro Yamada 
874ceb7f329SMasahiro Yamada 	if (!*name) {
875ceb7f329SMasahiro Yamada 		fprintf(stderr, "config name is empty\n");
876ceb7f329SMasahiro Yamada 		return -1;
877ceb7f329SMasahiro Yamada 	}
8781da177e4SLinus Torvalds 
8790608182aSMasahiro Yamada 	if (is_dir(name)) {
880ceb7f329SMasahiro Yamada 		fprintf(stderr, "%s: Is a directory\n", name);
881ceb7f329SMasahiro Yamada 		return -1;
882ceb7f329SMasahiro Yamada 	}
8831da177e4SLinus Torvalds 
88414cdd3c4SRoman Zippel 	env = getenv("KCONFIG_OVERWRITECONFIG");
885ceb7f329SMasahiro Yamada 	if (env && *env) {
88614cdd3c4SRoman Zippel 		*tmpname = 0;
887ceb7f329SMasahiro Yamada 		out = fopen(name, "w");
888ceb7f329SMasahiro Yamada 	} else {
889ceb7f329SMasahiro Yamada 		snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp",
890ceb7f329SMasahiro Yamada 			 name, (int)getpid());
891ceb7f329SMasahiro Yamada 		out = fopen(tmpname, "w");
89214cdd3c4SRoman Zippel 	}
8931da177e4SLinus Torvalds 	if (!out)
8941da177e4SLinus Torvalds 		return 1;
89514cdd3c4SRoman Zippel 
896e54e692bSArnaud Lacombe 	conf_write_heading(out, &kconfig_printer_cb, NULL);
8971da177e4SLinus Torvalds 
898b3214293SKarsten Wiese 	if (!conf_get_changed())
8991da177e4SLinus Torvalds 		sym_clear_all_valid();
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	menu = rootmenu.list;
9021da177e4SLinus Torvalds 	while (menu) {
9031da177e4SLinus Torvalds 		sym = menu->sym;
9041da177e4SLinus Torvalds 		if (!sym) {
9051da177e4SLinus Torvalds 			if (!menu_is_visible(menu))
9061da177e4SLinus Torvalds 				goto next;
9071da177e4SLinus Torvalds 			str = menu_get_prompt(menu);
9081da177e4SLinus Torvalds 			fprintf(out, "\n"
9091da177e4SLinus Torvalds 				     "#\n"
9101da177e4SLinus Torvalds 				     "# %s\n"
9111da177e4SLinus Torvalds 				     "#\n", str);
9121da177e4SLinus Torvalds 		} else if (!(sym->flags & SYMBOL_CHOICE)) {
9131da177e4SLinus Torvalds 			sym_calc_value(sym);
9141da177e4SLinus Torvalds 			if (!(sym->flags & SYMBOL_WRITE))
9151da177e4SLinus Torvalds 				goto next;
9161da177e4SLinus Torvalds 			sym->flags &= ~SYMBOL_WRITE;
917e54e692bSArnaud Lacombe 
918e54e692bSArnaud Lacombe 			conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
9191da177e4SLinus Torvalds 		}
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds next:
9221da177e4SLinus Torvalds 		if (menu->list) {
9231da177e4SLinus Torvalds 			menu = menu->list;
9241da177e4SLinus Torvalds 			continue;
9251da177e4SLinus Torvalds 		}
9261da177e4SLinus Torvalds 		if (menu->next)
9271da177e4SLinus Torvalds 			menu = menu->next;
9281da177e4SLinus Torvalds 		else while ((menu = menu->parent)) {
9291da177e4SLinus Torvalds 			if (menu->next) {
9301da177e4SLinus Torvalds 				menu = menu->next;
9311da177e4SLinus Torvalds 				break;
9321da177e4SLinus Torvalds 			}
9331da177e4SLinus Torvalds 		}
9341da177e4SLinus Torvalds 	}
9351da177e4SLinus Torvalds 	fclose(out);
93614cdd3c4SRoman Zippel 
93714cdd3c4SRoman Zippel 	if (*tmpname) {
938*67424f61SMasahiro Yamada 		if (is_same(name, tmpname)) {
939*67424f61SMasahiro Yamada 			conf_message("No change to %s", name);
940*67424f61SMasahiro Yamada 			unlink(tmpname);
941*67424f61SMasahiro Yamada 			sym_set_change_count(0);
942*67424f61SMasahiro Yamada 			return 0;
943*67424f61SMasahiro Yamada 		}
944*67424f61SMasahiro Yamada 
945ceb7f329SMasahiro Yamada 		snprintf(oldname, sizeof(oldname), "%s.old", name);
946ceb7f329SMasahiro Yamada 		rename(name, oldname);
947ceb7f329SMasahiro Yamada 		if (rename(tmpname, name))
9481da177e4SLinus Torvalds 			return 1;
94914cdd3c4SRoman Zippel 	}
9501da177e4SLinus Torvalds 
951ceb7f329SMasahiro Yamada 	conf_message("configuration written to %s", name);
952ddc97cacSRoman Zippel 
953bfc10001SKarsten Wiese 	sym_set_change_count(0);
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	return 0;
9561da177e4SLinus Torvalds }
957c955ccafSRoman Zippel 
958a2ff4040SMasahiro Yamada /* write a dependency file as used by kbuild to track dependencies */
959a2ff4040SMasahiro Yamada static int conf_write_dep(const char *name)
960a2ff4040SMasahiro Yamada {
961a2ff4040SMasahiro Yamada 	struct file *file;
962a2ff4040SMasahiro Yamada 	FILE *out;
963a2ff4040SMasahiro Yamada 
964a2ff4040SMasahiro Yamada 	if (!name)
965a2ff4040SMasahiro Yamada 		name = ".kconfig.d";
966a2ff4040SMasahiro Yamada 	out = fopen("..config.tmp", "w");
967a2ff4040SMasahiro Yamada 	if (!out)
968a2ff4040SMasahiro Yamada 		return 1;
969a2ff4040SMasahiro Yamada 	fprintf(out, "deps_config := \\\n");
970a2ff4040SMasahiro Yamada 	for (file = file_list; file; file = file->next) {
971a2ff4040SMasahiro Yamada 		if (file->next)
972a2ff4040SMasahiro Yamada 			fprintf(out, "\t%s \\\n", file->name);
973a2ff4040SMasahiro Yamada 		else
974a2ff4040SMasahiro Yamada 			fprintf(out, "\t%s\n", file->name);
975a2ff4040SMasahiro Yamada 	}
976a2ff4040SMasahiro Yamada 	fprintf(out, "\n%s: \\\n"
977a2ff4040SMasahiro Yamada 		     "\t$(deps_config)\n\n", conf_get_autoconfig_name());
978a2ff4040SMasahiro Yamada 
979a2ff4040SMasahiro Yamada 	env_write_dep(out, conf_get_autoconfig_name());
980a2ff4040SMasahiro Yamada 
981a2ff4040SMasahiro Yamada 	fprintf(out, "\n$(deps_config): ;\n");
982a2ff4040SMasahiro Yamada 	fclose(out);
98379123b13SMasahiro Yamada 
98479123b13SMasahiro Yamada 	if (make_parent_dir(name))
98579123b13SMasahiro Yamada 		return 1;
986a2ff4040SMasahiro Yamada 	rename("..config.tmp", name);
987a2ff4040SMasahiro Yamada 	return 0;
988a2ff4040SMasahiro Yamada }
989a2ff4040SMasahiro Yamada 
9900849d212SMasahiro Yamada static int conf_touch_deps(void)
9912e3646e5SRoman Zippel {
99212122f62SMarkus Heidelberg 	const char *name;
9932e3646e5SRoman Zippel 	struct symbol *sym;
9941508fec8SMasahiro Yamada 	int res, i;
9951508fec8SMasahiro Yamada 
9961508fec8SMasahiro Yamada 	strcpy(depfile_path, "include/config/");
9971508fec8SMasahiro Yamada 	depfile_prefix_len = strlen(depfile_path);
9982e3646e5SRoman Zippel 
99912122f62SMarkus Heidelberg 	name = conf_get_autoconfig_name();
10002e3646e5SRoman Zippel 	conf_read_simple(name, S_DEF_AUTO);
10016b87b70cSAl Viro 	sym_calc_value(modules_sym);
10022e3646e5SRoman Zippel 
10032e3646e5SRoman Zippel 	for_all_symbols(i, sym) {
10042e3646e5SRoman Zippel 		sym_calc_value(sym);
1005693359f7SDirk Gouders 		if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
10062e3646e5SRoman Zippel 			continue;
10072e3646e5SRoman Zippel 		if (sym->flags & SYMBOL_WRITE) {
10082e3646e5SRoman Zippel 			if (sym->flags & SYMBOL_DEF_AUTO) {
10092e3646e5SRoman Zippel 				/*
10102e3646e5SRoman Zippel 				 * symbol has old and new value,
10112e3646e5SRoman Zippel 				 * so compare them...
10122e3646e5SRoman Zippel 				 */
10132e3646e5SRoman Zippel 				switch (sym->type) {
10142e3646e5SRoman Zippel 				case S_BOOLEAN:
10152e3646e5SRoman Zippel 				case S_TRISTATE:
10162e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) ==
10172e3646e5SRoman Zippel 					    sym->def[S_DEF_AUTO].tri)
10182e3646e5SRoman Zippel 						continue;
10192e3646e5SRoman Zippel 					break;
10202e3646e5SRoman Zippel 				case S_STRING:
10212e3646e5SRoman Zippel 				case S_HEX:
10222e3646e5SRoman Zippel 				case S_INT:
10232e3646e5SRoman Zippel 					if (!strcmp(sym_get_string_value(sym),
10242e3646e5SRoman Zippel 						    sym->def[S_DEF_AUTO].val))
10252e3646e5SRoman Zippel 						continue;
10262e3646e5SRoman Zippel 					break;
10272e3646e5SRoman Zippel 				default:
10282e3646e5SRoman Zippel 					break;
10292e3646e5SRoman Zippel 				}
10302e3646e5SRoman Zippel 			} else {
10312e3646e5SRoman Zippel 				/*
10322e3646e5SRoman Zippel 				 * If there is no old value, only 'no' (unset)
10332e3646e5SRoman Zippel 				 * is allowed as new value.
10342e3646e5SRoman Zippel 				 */
10352e3646e5SRoman Zippel 				switch (sym->type) {
10362e3646e5SRoman Zippel 				case S_BOOLEAN:
10372e3646e5SRoman Zippel 				case S_TRISTATE:
10382e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) == no)
10392e3646e5SRoman Zippel 						continue;
10402e3646e5SRoman Zippel 					break;
10412e3646e5SRoman Zippel 				default:
10422e3646e5SRoman Zippel 					break;
10432e3646e5SRoman Zippel 				}
10442e3646e5SRoman Zippel 			}
10452e3646e5SRoman Zippel 		} else if (!(sym->flags & SYMBOL_DEF_AUTO))
10462e3646e5SRoman Zippel 			/* There is neither an old nor a new value. */
10472e3646e5SRoman Zippel 			continue;
10482e3646e5SRoman Zippel 		/* else
10492e3646e5SRoman Zippel 		 *	There is an old value, but no new value ('no' (unset)
10502e3646e5SRoman Zippel 		 *	isn't saved in auto.conf, so the old value is always
10512e3646e5SRoman Zippel 		 *	different from 'no').
10522e3646e5SRoman Zippel 		 */
10532e3646e5SRoman Zippel 
10541508fec8SMasahiro Yamada 		res = conf_touch_dep(sym->name);
10551508fec8SMasahiro Yamada 		if (res)
10562e3646e5SRoman Zippel 			return res;
10572e3646e5SRoman Zippel 	}
10582e3646e5SRoman Zippel 
10591508fec8SMasahiro Yamada 	return 0;
10601508fec8SMasahiro Yamada }
10611508fec8SMasahiro Yamada 
106200c864f8SMasahiro Yamada int conf_write_autoconf(int overwrite)
1063c955ccafSRoman Zippel {
1064c955ccafSRoman Zippel 	struct symbol *sym;
106512122f62SMarkus Heidelberg 	const char *name;
106600c864f8SMasahiro Yamada 	const char *autoconf_name = conf_get_autoconfig_name();
1067bc081dd6SMichal Marek 	FILE *out, *tristate, *out_h;
106849192f26SSam Ravnborg 	int i;
1069c955ccafSRoman Zippel 
107000c864f8SMasahiro Yamada 	if (!overwrite && is_present(autoconf_name))
107100c864f8SMasahiro Yamada 		return 0;
107200c864f8SMasahiro Yamada 
10732e3646e5SRoman Zippel 	sym_clear_all_valid();
10742e3646e5SRoman Zippel 
1075a2ff4040SMasahiro Yamada 	conf_write_dep("include/config/auto.conf.cmd");
1076c955ccafSRoman Zippel 
10770849d212SMasahiro Yamada 	if (conf_touch_deps())
10782e3646e5SRoman Zippel 		return 1;
10792e3646e5SRoman Zippel 
1080c955ccafSRoman Zippel 	out = fopen(".tmpconfig", "w");
1081c955ccafSRoman Zippel 	if (!out)
1082c955ccafSRoman Zippel 		return 1;
1083c955ccafSRoman Zippel 
1084bc081dd6SMichal Marek 	tristate = fopen(".tmpconfig_tristate", "w");
1085bc081dd6SMichal Marek 	if (!tristate) {
1086bc081dd6SMichal Marek 		fclose(out);
1087bc081dd6SMichal Marek 		return 1;
1088bc081dd6SMichal Marek 	}
1089bc081dd6SMichal Marek 
1090c955ccafSRoman Zippel 	out_h = fopen(".tmpconfig.h", "w");
1091c955ccafSRoman Zippel 	if (!out_h) {
1092c955ccafSRoman Zippel 		fclose(out);
1093bc081dd6SMichal Marek 		fclose(tristate);
1094c955ccafSRoman Zippel 		return 1;
1095c955ccafSRoman Zippel 	}
1096c955ccafSRoman Zippel 
1097e54e692bSArnaud Lacombe 	conf_write_heading(out, &kconfig_printer_cb, NULL);
1098e54e692bSArnaud Lacombe 
1099e54e692bSArnaud Lacombe 	conf_write_heading(tristate, &tristate_printer_cb, NULL);
1100e54e692bSArnaud Lacombe 
1101e54e692bSArnaud Lacombe 	conf_write_heading(out_h, &header_printer_cb, NULL);
1102c955ccafSRoman Zippel 
1103c955ccafSRoman Zippel 	for_all_symbols(i, sym) {
1104953742c8SArnaud Lacombe 		sym_calc_value(sym);
1105a9596135SPaul Gortmaker 		if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
1106953742c8SArnaud Lacombe 			continue;
1107953742c8SArnaud Lacombe 
1108a9596135SPaul Gortmaker 		/* write symbol to auto.conf, tristate and header files */
1109e54e692bSArnaud Lacombe 		conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
111049192f26SSam Ravnborg 
1111e54e692bSArnaud Lacombe 		conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
1112e54e692bSArnaud Lacombe 
1113e54e692bSArnaud Lacombe 		conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
1114c955ccafSRoman Zippel 	}
1115c955ccafSRoman Zippel 	fclose(out);
1116bc081dd6SMichal Marek 	fclose(tristate);
1117c955ccafSRoman Zippel 	fclose(out_h);
1118c955ccafSRoman Zippel 
1119c955ccafSRoman Zippel 	name = getenv("KCONFIG_AUTOHEADER");
1120c955ccafSRoman Zippel 	if (!name)
1121264a2683SSam Ravnborg 		name = "include/generated/autoconf.h";
112279123b13SMasahiro Yamada 	if (make_parent_dir(name))
112379123b13SMasahiro Yamada 		return 1;
1124c955ccafSRoman Zippel 	if (rename(".tmpconfig.h", name))
1125c955ccafSRoman Zippel 		return 1;
112679123b13SMasahiro Yamada 
1127bc081dd6SMichal Marek 	name = getenv("KCONFIG_TRISTATE");
1128bc081dd6SMichal Marek 	if (!name)
1129bc081dd6SMichal Marek 		name = "include/config/tristate.conf";
113079123b13SMasahiro Yamada 	if (make_parent_dir(name))
113179123b13SMasahiro Yamada 		return 1;
1132bc081dd6SMichal Marek 	if (rename(".tmpconfig_tristate", name))
1133bc081dd6SMichal Marek 		return 1;
113479123b13SMasahiro Yamada 
113500c864f8SMasahiro Yamada 	if (make_parent_dir(autoconf_name))
113679123b13SMasahiro Yamada 		return 1;
1137c955ccafSRoman Zippel 	/*
1138c955ccafSRoman Zippel 	 * This must be the last step, kbuild has a dependency on auto.conf
1139c955ccafSRoman Zippel 	 * and this marks the successful completion of the previous steps.
1140c955ccafSRoman Zippel 	 */
114100c864f8SMasahiro Yamada 	if (rename(".tmpconfig", autoconf_name))
1142c955ccafSRoman Zippel 		return 1;
1143c955ccafSRoman Zippel 
1144c955ccafSRoman Zippel 	return 0;
1145c955ccafSRoman Zippel }
1146b3214293SKarsten Wiese 
1147bfc10001SKarsten Wiese static int sym_change_count;
11483b354c55SKarsten Wiese static void (*conf_changed_callback)(void);
1149bfc10001SKarsten Wiese 
1150bfc10001SKarsten Wiese void sym_set_change_count(int count)
1151bfc10001SKarsten Wiese {
11523b354c55SKarsten Wiese 	int _sym_change_count = sym_change_count;
1153bfc10001SKarsten Wiese 	sym_change_count = count;
11543b354c55SKarsten Wiese 	if (conf_changed_callback &&
11553b354c55SKarsten Wiese 	    (bool)_sym_change_count != (bool)count)
11563b354c55SKarsten Wiese 		conf_changed_callback();
1157bfc10001SKarsten Wiese }
1158bfc10001SKarsten Wiese 
1159bfc10001SKarsten Wiese void sym_add_change_count(int count)
1160bfc10001SKarsten Wiese {
11613b354c55SKarsten Wiese 	sym_set_change_count(count + sym_change_count);
1162bfc10001SKarsten Wiese }
1163bfc10001SKarsten Wiese 
1164b3214293SKarsten Wiese bool conf_get_changed(void)
1165b3214293SKarsten Wiese {
1166b3214293SKarsten Wiese 	return sym_change_count;
1167b3214293SKarsten Wiese }
11683b354c55SKarsten Wiese 
11693b354c55SKarsten Wiese void conf_set_changed_callback(void (*fn)(void))
11703b354c55SKarsten Wiese {
11713b354c55SKarsten Wiese 	conf_changed_callback = fn;
11723b354c55SKarsten Wiese }
1173dc7862e5SRoman Zippel 
11743b9a19e0SYann E. MORIN static bool randomize_choice_values(struct symbol *csym)
1175a64b44eaSSam Ravnborg {
1176a64b44eaSSam Ravnborg 	struct property *prop;
1177a64b44eaSSam Ravnborg 	struct symbol *sym;
1178a64b44eaSSam Ravnborg 	struct expr *e;
1179a64b44eaSSam Ravnborg 	int cnt, def;
1180a64b44eaSSam Ravnborg 
1181a64b44eaSSam Ravnborg 	/*
1182579fb8e7SArnaud Lacombe 	 * If choice is mod then we may have more items selected
1183a64b44eaSSam Ravnborg 	 * and if no then no-one.
1184a64b44eaSSam Ravnborg 	 * In both cases stop.
1185a64b44eaSSam Ravnborg 	 */
1186a64b44eaSSam Ravnborg 	if (csym->curr.tri != yes)
11873b9a19e0SYann E. MORIN 		return false;
1188a64b44eaSSam Ravnborg 
1189a64b44eaSSam Ravnborg 	prop = sym_get_choice_prop(csym);
1190a64b44eaSSam Ravnborg 
1191a64b44eaSSam Ravnborg 	/* count entries in choice block */
1192a64b44eaSSam Ravnborg 	cnt = 0;
1193a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym)
1194a64b44eaSSam Ravnborg 		cnt++;
1195a64b44eaSSam Ravnborg 
1196a64b44eaSSam Ravnborg 	/*
1197a64b44eaSSam Ravnborg 	 * find a random value and set it to yes,
1198a64b44eaSSam Ravnborg 	 * set the rest to no so we have only one set
1199a64b44eaSSam Ravnborg 	 */
1200a64b44eaSSam Ravnborg 	def = (rand() % cnt);
1201a64b44eaSSam Ravnborg 
1202a64b44eaSSam Ravnborg 	cnt = 0;
1203a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym) {
1204a64b44eaSSam Ravnborg 		if (def == cnt++) {
1205a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = yes;
1206a64b44eaSSam Ravnborg 			csym->def[S_DEF_USER].val = sym;
1207a64b44eaSSam Ravnborg 		}
1208a64b44eaSSam Ravnborg 		else {
1209a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = no;
1210a64b44eaSSam Ravnborg 		}
1211e6abf12aSYann E. MORIN 		sym->flags |= SYMBOL_DEF_USER;
1212e6abf12aSYann E. MORIN 		/* clear VALID to get value calculated */
1213e6abf12aSYann E. MORIN 		sym->flags &= ~SYMBOL_VALID;
1214a64b44eaSSam Ravnborg 	}
1215a64b44eaSSam Ravnborg 	csym->flags |= SYMBOL_DEF_USER;
1216a64b44eaSSam Ravnborg 	/* clear VALID to get value calculated */
1217a64b44eaSSam Ravnborg 	csym->flags &= ~(SYMBOL_VALID);
12183b9a19e0SYann E. MORIN 
12193b9a19e0SYann E. MORIN 	return true;
1220a64b44eaSSam Ravnborg }
1221a64b44eaSSam Ravnborg 
1222fbe98bb9SArve Hjønnevåg void set_all_choice_values(struct symbol *csym)
1223a64b44eaSSam Ravnborg {
1224a64b44eaSSam Ravnborg 	struct property *prop;
1225a64b44eaSSam Ravnborg 	struct symbol *sym;
1226a64b44eaSSam Ravnborg 	struct expr *e;
1227a64b44eaSSam Ravnborg 
1228a64b44eaSSam Ravnborg 	prop = sym_get_choice_prop(csym);
1229a64b44eaSSam Ravnborg 
1230a64b44eaSSam Ravnborg 	/*
1231a64b44eaSSam Ravnborg 	 * Set all non-assinged choice values to no
1232a64b44eaSSam Ravnborg 	 */
1233a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym) {
1234a64b44eaSSam Ravnborg 		if (!sym_has_value(sym))
1235a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = no;
1236a64b44eaSSam Ravnborg 	}
1237a64b44eaSSam Ravnborg 	csym->flags |= SYMBOL_DEF_USER;
1238a64b44eaSSam Ravnborg 	/* clear VALID to get value calculated */
1239fbe98bb9SArve Hjønnevåg 	csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1240a64b44eaSSam Ravnborg }
1241dc7862e5SRoman Zippel 
12423b9a19e0SYann E. MORIN bool conf_set_all_new_symbols(enum conf_def_mode mode)
1243dc7862e5SRoman Zippel {
1244dc7862e5SRoman Zippel 	struct symbol *sym, *csym;
1245b92d804aSMasahiro Yamada 	int i, cnt, pby, pty, ptm;	/* pby: probability of bool     = y
1246e43956e6SYann E. MORIN 					 * pty: probability of tristate = y
1247e43956e6SYann E. MORIN 					 * ptm: probability of tristate = m
1248e43956e6SYann E. MORIN 					 */
1249e43956e6SYann E. MORIN 
1250e43956e6SYann E. MORIN 	pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1251e43956e6SYann E. MORIN 				   * below, otherwise gcc whines about
1252e43956e6SYann E. MORIN 				   * -Wmaybe-uninitialized */
1253e43956e6SYann E. MORIN 	if (mode == def_random) {
1254e43956e6SYann E. MORIN 		int n, p[3];
1255e43956e6SYann E. MORIN 		char *env = getenv("KCONFIG_PROBABILITY");
1256e43956e6SYann E. MORIN 		n = 0;
1257e43956e6SYann E. MORIN 		while( env && *env ) {
1258e43956e6SYann E. MORIN 			char *endp;
1259e43956e6SYann E. MORIN 			int tmp = strtol( env, &endp, 10 );
1260e43956e6SYann E. MORIN 			if( tmp >= 0 && tmp <= 100 ) {
1261e43956e6SYann E. MORIN 				p[n++] = tmp;
1262e43956e6SYann E. MORIN 			} else {
1263e43956e6SYann E. MORIN 				errno = ERANGE;
1264e43956e6SYann E. MORIN 				perror( "KCONFIG_PROBABILITY" );
1265e43956e6SYann E. MORIN 				exit( 1 );
1266e43956e6SYann E. MORIN 			}
1267e43956e6SYann E. MORIN 			env = (*endp == ':') ? endp+1 : endp;
1268e43956e6SYann E. MORIN 			if( n >=3 ) {
1269e43956e6SYann E. MORIN 				break;
1270e43956e6SYann E. MORIN 			}
1271e43956e6SYann E. MORIN 		}
1272e43956e6SYann E. MORIN 		switch( n ) {
1273e43956e6SYann E. MORIN 		case 1:
1274e43956e6SYann E. MORIN 			pby = p[0]; ptm = pby/2; pty = pby-ptm;
1275e43956e6SYann E. MORIN 			break;
1276e43956e6SYann E. MORIN 		case 2:
1277e43956e6SYann E. MORIN 			pty = p[0]; ptm = p[1]; pby = pty + ptm;
1278e43956e6SYann E. MORIN 			break;
1279e43956e6SYann E. MORIN 		case 3:
1280e43956e6SYann E. MORIN 			pby = p[0]; pty = p[1]; ptm = p[2];
1281e43956e6SYann E. MORIN 			break;
1282e43956e6SYann E. MORIN 		}
1283e43956e6SYann E. MORIN 
1284e43956e6SYann E. MORIN 		if( pty+ptm > 100 ) {
1285e43956e6SYann E. MORIN 			errno = ERANGE;
1286e43956e6SYann E. MORIN 			perror( "KCONFIG_PROBABILITY" );
1287e43956e6SYann E. MORIN 			exit( 1 );
1288e43956e6SYann E. MORIN 		}
1289e43956e6SYann E. MORIN 	}
12903b9a19e0SYann E. MORIN 	bool has_changed = false;
1291dc7862e5SRoman Zippel 
1292dc7862e5SRoman Zippel 	for_all_symbols(i, sym) {
1293cfa98f2eSYann E. MORIN 		if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
1294dc7862e5SRoman Zippel 			continue;
1295dc7862e5SRoman Zippel 		switch (sym_get_type(sym)) {
1296dc7862e5SRoman Zippel 		case S_BOOLEAN:
1297dc7862e5SRoman Zippel 		case S_TRISTATE:
12983b9a19e0SYann E. MORIN 			has_changed = true;
1299dc7862e5SRoman Zippel 			switch (mode) {
1300dc7862e5SRoman Zippel 			case def_yes:
1301dc7862e5SRoman Zippel 				sym->def[S_DEF_USER].tri = yes;
1302dc7862e5SRoman Zippel 				break;
1303dc7862e5SRoman Zippel 			case def_mod:
1304dc7862e5SRoman Zippel 				sym->def[S_DEF_USER].tri = mod;
1305dc7862e5SRoman Zippel 				break;
1306dc7862e5SRoman Zippel 			case def_no:
13075d2acfc7SJosh Triplett 				if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
13085d2acfc7SJosh Triplett 					sym->def[S_DEF_USER].tri = yes;
13095d2acfc7SJosh Triplett 				else
1310dc7862e5SRoman Zippel 					sym->def[S_DEF_USER].tri = no;
1311dc7862e5SRoman Zippel 				break;
1312dc7862e5SRoman Zippel 			case def_random:
1313e43956e6SYann E. MORIN 				sym->def[S_DEF_USER].tri = no;
1314e43956e6SYann E. MORIN 				cnt = rand() % 100;
1315e43956e6SYann E. MORIN 				if (sym->type == S_TRISTATE) {
1316e43956e6SYann E. MORIN 					if (cnt < pty)
1317e43956e6SYann E. MORIN 						sym->def[S_DEF_USER].tri = yes;
1318e43956e6SYann E. MORIN 					else if (cnt < (pty+ptm))
1319e43956e6SYann E. MORIN 						sym->def[S_DEF_USER].tri = mod;
1320e43956e6SYann E. MORIN 				} else if (cnt < pby)
1321e43956e6SYann E. MORIN 					sym->def[S_DEF_USER].tri = yes;
1322dc7862e5SRoman Zippel 				break;
1323dc7862e5SRoman Zippel 			default:
1324dc7862e5SRoman Zippel 				continue;
1325dc7862e5SRoman Zippel 			}
1326184832c9SSam Ravnborg 			if (!(sym_is_choice(sym) && mode == def_random))
1327dc7862e5SRoman Zippel 				sym->flags |= SYMBOL_DEF_USER;
1328dc7862e5SRoman Zippel 			break;
1329dc7862e5SRoman Zippel 		default:
1330dc7862e5SRoman Zippel 			break;
1331dc7862e5SRoman Zippel 		}
1332dc7862e5SRoman Zippel 
1333dc7862e5SRoman Zippel 	}
1334dc7862e5SRoman Zippel 
1335ce97e13eSAl Viro 	sym_clear_all_valid();
1336dc7862e5SRoman Zippel 
1337184832c9SSam Ravnborg 	/*
1338184832c9SSam Ravnborg 	 * We have different type of choice blocks.
1339579fb8e7SArnaud Lacombe 	 * If curr.tri equals to mod then we can select several
1340184832c9SSam Ravnborg 	 * choice symbols in one block.
1341184832c9SSam Ravnborg 	 * In this case we do nothing.
1342579fb8e7SArnaud Lacombe 	 * If curr.tri equals yes then only one symbol can be
1343184832c9SSam Ravnborg 	 * selected in a choice block and we set it to yes,
1344184832c9SSam Ravnborg 	 * and the rest to no.
1345184832c9SSam Ravnborg 	 */
1346fbe98bb9SArve Hjønnevåg 	if (mode != def_random) {
1347fbe98bb9SArve Hjønnevåg 		for_all_symbols(i, csym) {
1348fbe98bb9SArve Hjønnevåg 			if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1349fbe98bb9SArve Hjønnevåg 			    sym_is_choice_value(csym))
1350fbe98bb9SArve Hjønnevåg 				csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1351fbe98bb9SArve Hjønnevåg 		}
1352fbe98bb9SArve Hjønnevåg 	}
1353fbe98bb9SArve Hjønnevåg 
1354dc7862e5SRoman Zippel 	for_all_symbols(i, csym) {
1355dc7862e5SRoman Zippel 		if (sym_has_value(csym) || !sym_is_choice(csym))
1356dc7862e5SRoman Zippel 			continue;
1357dc7862e5SRoman Zippel 
1358dc7862e5SRoman Zippel 		sym_calc_value(csym);
1359a64b44eaSSam Ravnborg 		if (mode == def_random)
13603b9a19e0SYann E. MORIN 			has_changed = randomize_choice_values(csym);
13613b9a19e0SYann E. MORIN 		else {
13623b9a19e0SYann E. MORIN 			set_all_choice_values(csym);
13633b9a19e0SYann E. MORIN 			has_changed = true;
1364dc7862e5SRoman Zippel 		}
1365dc7862e5SRoman Zippel 	}
13663b9a19e0SYann E. MORIN 
13673b9a19e0SYann E. MORIN 	return has_changed;
13683b9a19e0SYann E. MORIN }
1369