xref: /openbmc/linux/scripts/kconfig/confdata.c (revision 78cb09078352d032b12e2af7feb9b5b7f0fa794c)
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 
667424f61SMasahiro Yamada #include <sys/mman.h>
71da177e4SLinus Torvalds #include <sys/stat.h>
8*78cb0907SBoris Kolpackov #include <sys/types.h>
91da177e4SLinus Torvalds #include <ctype.h>
1094bedecaSArnaud Lacombe #include <errno.h>
112e3646e5SRoman Zippel #include <fcntl.h>
12558e78e3SMasahiro Yamada #include <limits.h>
1310a4b277SArnaud Lacombe #include <stdarg.h>
141da177e4SLinus Torvalds #include <stdio.h>
151da177e4SLinus Torvalds #include <stdlib.h>
161da177e4SLinus Torvalds #include <string.h>
171da177e4SLinus Torvalds #include <time.h>
181da177e4SLinus Torvalds #include <unistd.h>
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include "lkc.h"
211da177e4SLinus Torvalds 
220608182aSMasahiro Yamada /* return true if 'path' exists, false otherwise */
230608182aSMasahiro Yamada static bool is_present(const char *path)
240608182aSMasahiro Yamada {
250608182aSMasahiro Yamada 	struct stat st;
260608182aSMasahiro Yamada 
270608182aSMasahiro Yamada 	return !stat(path, &st);
280608182aSMasahiro Yamada }
290608182aSMasahiro Yamada 
300608182aSMasahiro Yamada /* return true if 'path' exists and it is a directory, false otherwise */
310608182aSMasahiro Yamada static bool is_dir(const char *path)
320608182aSMasahiro Yamada {
330608182aSMasahiro Yamada 	struct stat st;
340608182aSMasahiro Yamada 
350608182aSMasahiro Yamada 	if (stat(path, &st))
360608182aSMasahiro Yamada 		return 0;
370608182aSMasahiro Yamada 
380608182aSMasahiro Yamada 	return S_ISDIR(st.st_mode);
390608182aSMasahiro Yamada }
400608182aSMasahiro Yamada 
4167424f61SMasahiro Yamada /* return true if the given two files are the same, false otherwise */
4267424f61SMasahiro Yamada static bool is_same(const char *file1, const char *file2)
4367424f61SMasahiro Yamada {
4467424f61SMasahiro Yamada 	int fd1, fd2;
4567424f61SMasahiro Yamada 	struct stat st1, st2;
4667424f61SMasahiro Yamada 	void *map1, *map2;
4767424f61SMasahiro Yamada 	bool ret = false;
4867424f61SMasahiro Yamada 
4967424f61SMasahiro Yamada 	fd1 = open(file1, O_RDONLY);
5067424f61SMasahiro Yamada 	if (fd1 < 0)
5167424f61SMasahiro Yamada 		return ret;
5267424f61SMasahiro Yamada 
5367424f61SMasahiro Yamada 	fd2 = open(file2, O_RDONLY);
5467424f61SMasahiro Yamada 	if (fd2 < 0)
5567424f61SMasahiro Yamada 		goto close1;
5667424f61SMasahiro Yamada 
5767424f61SMasahiro Yamada 	ret = fstat(fd1, &st1);
5867424f61SMasahiro Yamada 	if (ret)
5967424f61SMasahiro Yamada 		goto close2;
6067424f61SMasahiro Yamada 	ret = fstat(fd2, &st2);
6167424f61SMasahiro Yamada 	if (ret)
6267424f61SMasahiro Yamada 		goto close2;
6367424f61SMasahiro Yamada 
6467424f61SMasahiro Yamada 	if (st1.st_size != st2.st_size)
6567424f61SMasahiro Yamada 		goto close2;
6667424f61SMasahiro Yamada 
6767424f61SMasahiro Yamada 	map1 = mmap(NULL, st1.st_size, PROT_READ, MAP_PRIVATE, fd1, 0);
6867424f61SMasahiro Yamada 	if (map1 == MAP_FAILED)
6967424f61SMasahiro Yamada 		goto close2;
7067424f61SMasahiro Yamada 
7167424f61SMasahiro Yamada 	map2 = mmap(NULL, st2.st_size, PROT_READ, MAP_PRIVATE, fd2, 0);
7267424f61SMasahiro Yamada 	if (map2 == MAP_FAILED)
7367424f61SMasahiro Yamada 		goto close2;
7467424f61SMasahiro Yamada 
7567424f61SMasahiro Yamada 	if (bcmp(map1, map2, st1.st_size))
7667424f61SMasahiro Yamada 		goto close2;
7767424f61SMasahiro Yamada 
7867424f61SMasahiro Yamada 	ret = true;
7967424f61SMasahiro Yamada close2:
8067424f61SMasahiro Yamada 	close(fd2);
8167424f61SMasahiro Yamada close1:
8267424f61SMasahiro Yamada 	close(fd1);
8367424f61SMasahiro Yamada 
8467424f61SMasahiro Yamada 	return ret;
8567424f61SMasahiro Yamada }
8667424f61SMasahiro Yamada 
870608182aSMasahiro Yamada /*
880608182aSMasahiro Yamada  * Create the parent directory of the given path.
890608182aSMasahiro Yamada  *
900608182aSMasahiro Yamada  * For example, if 'include/config/auto.conf' is given, create 'include/config'.
910608182aSMasahiro Yamada  */
920608182aSMasahiro Yamada static int make_parent_dir(const char *path)
930608182aSMasahiro Yamada {
940608182aSMasahiro Yamada 	char tmp[PATH_MAX + 1];
950608182aSMasahiro Yamada 	char *p;
960608182aSMasahiro Yamada 
970608182aSMasahiro Yamada 	strncpy(tmp, path, sizeof(tmp));
980608182aSMasahiro Yamada 	tmp[sizeof(tmp) - 1] = 0;
990608182aSMasahiro Yamada 
1000608182aSMasahiro Yamada 	/* Remove the base name. Just return if nothing is left */
1010608182aSMasahiro Yamada 	p = strrchr(tmp, '/');
1020608182aSMasahiro Yamada 	if (!p)
1030608182aSMasahiro Yamada 		return 0;
1040608182aSMasahiro Yamada 	*(p + 1) = 0;
1050608182aSMasahiro Yamada 
1060608182aSMasahiro Yamada 	/* Just in case it is an absolute path */
1070608182aSMasahiro Yamada 	p = tmp;
1080608182aSMasahiro Yamada 	while (*p == '/')
1090608182aSMasahiro Yamada 		p++;
1100608182aSMasahiro Yamada 
1110608182aSMasahiro Yamada 	while ((p = strchr(p, '/'))) {
1120608182aSMasahiro Yamada 		*p = 0;
1130608182aSMasahiro Yamada 
1140608182aSMasahiro Yamada 		/* skip if the directory exists */
1150608182aSMasahiro Yamada 		if (!is_dir(tmp) && mkdir(tmp, 0755))
1160608182aSMasahiro Yamada 			return -1;
1170608182aSMasahiro Yamada 
1180608182aSMasahiro Yamada 		*p = '/';
1190608182aSMasahiro Yamada 		while (*p == '/')
1200608182aSMasahiro Yamada 			p++;
1210608182aSMasahiro Yamada 	}
1220608182aSMasahiro Yamada 
1230608182aSMasahiro Yamada 	return 0;
1240608182aSMasahiro Yamada }
1250608182aSMasahiro Yamada 
1261508fec8SMasahiro Yamada static char depfile_path[PATH_MAX];
1271508fec8SMasahiro Yamada static size_t depfile_prefix_len;
1281508fec8SMasahiro Yamada 
1291508fec8SMasahiro Yamada /* touch depfile for symbol 'name' */
1301508fec8SMasahiro Yamada static int conf_touch_dep(const char *name)
1311508fec8SMasahiro Yamada {
1321508fec8SMasahiro Yamada 	int fd, ret;
1331508fec8SMasahiro Yamada 	const char *s;
1341508fec8SMasahiro Yamada 	char *d, c;
1351508fec8SMasahiro Yamada 
1361508fec8SMasahiro Yamada 	/* check overflow: prefix + name + ".h" + '\0' must fit in buffer. */
1371508fec8SMasahiro Yamada 	if (depfile_prefix_len + strlen(name) + 3 > sizeof(depfile_path))
1381508fec8SMasahiro Yamada 		return -1;
1391508fec8SMasahiro Yamada 
1401508fec8SMasahiro Yamada 	d = depfile_path + depfile_prefix_len;
1411508fec8SMasahiro Yamada 	s = name;
1421508fec8SMasahiro Yamada 
1431508fec8SMasahiro Yamada 	while ((c = *s++))
1441508fec8SMasahiro Yamada 		*d++ = (c == '_') ? '/' : tolower(c);
1451508fec8SMasahiro Yamada 	strcpy(d, ".h");
1461508fec8SMasahiro Yamada 
1471508fec8SMasahiro Yamada 	/* Assume directory path already exists. */
1481508fec8SMasahiro Yamada 	fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1491508fec8SMasahiro Yamada 	if (fd == -1) {
1501508fec8SMasahiro Yamada 		if (errno != ENOENT)
1511508fec8SMasahiro Yamada 			return -1;
1521508fec8SMasahiro Yamada 
1531508fec8SMasahiro Yamada 		ret = make_parent_dir(depfile_path);
1541508fec8SMasahiro Yamada 		if (ret)
1551508fec8SMasahiro Yamada 			return ret;
1561508fec8SMasahiro Yamada 
1571508fec8SMasahiro Yamada 		/* Try it again. */
1581508fec8SMasahiro Yamada 		fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1591508fec8SMasahiro Yamada 		if (fd == -1)
1601508fec8SMasahiro Yamada 			return -1;
1611508fec8SMasahiro Yamada 	}
1621508fec8SMasahiro Yamada 	close(fd);
1631508fec8SMasahiro Yamada 
1641508fec8SMasahiro Yamada 	return 0;
1651508fec8SMasahiro Yamada }
1661508fec8SMasahiro Yamada 
167ad8d40cdSMichal Marek struct conf_printer {
168ad8d40cdSMichal Marek 	void (*print_symbol)(FILE *, struct symbol *, const char *, void *);
169ad8d40cdSMichal Marek 	void (*print_comment)(FILE *, const char *, void *);
170ad8d40cdSMichal Marek };
171ad8d40cdSMichal Marek 
172c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
173c1a0f5e3SRoman Zippel 	__attribute__ ((format (printf, 1, 2)));
174c1a0f5e3SRoman Zippel 
17542368c37SMichal Marek static void conf_message(const char *fmt, ...)
17642368c37SMichal Marek 	__attribute__ ((format (printf, 1, 2)));
17742368c37SMichal Marek 
178c1a0f5e3SRoman Zippel static const char *conf_filename;
17984dd95d4SMasahiro Yamada static int conf_lineno, conf_warnings;
180c1a0f5e3SRoman Zippel 
181c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
182c1a0f5e3SRoman Zippel {
183c1a0f5e3SRoman Zippel 	va_list ap;
184c1a0f5e3SRoman Zippel 	va_start(ap, fmt);
185c1a0f5e3SRoman Zippel 	fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
186c1a0f5e3SRoman Zippel 	vfprintf(stderr, fmt, ap);
187c1a0f5e3SRoman Zippel 	fprintf(stderr, "\n");
188c1a0f5e3SRoman Zippel 	va_end(ap);
189c1a0f5e3SRoman Zippel 	conf_warnings++;
190c1a0f5e3SRoman Zippel }
191c1a0f5e3SRoman Zippel 
1925accd7f3SMasahiro Yamada static void conf_default_message_callback(const char *s)
19342368c37SMichal Marek {
19442368c37SMichal Marek 	printf("#\n# ");
1955accd7f3SMasahiro Yamada 	printf("%s", s);
19642368c37SMichal Marek 	printf("\n#\n");
19742368c37SMichal Marek }
19842368c37SMichal Marek 
1995accd7f3SMasahiro Yamada static void (*conf_message_callback)(const char *s) =
20042368c37SMichal Marek 	conf_default_message_callback;
2015accd7f3SMasahiro Yamada void conf_set_message_callback(void (*fn)(const char *s))
20242368c37SMichal Marek {
20342368c37SMichal Marek 	conf_message_callback = fn;
20442368c37SMichal Marek }
20542368c37SMichal Marek 
20642368c37SMichal Marek static void conf_message(const char *fmt, ...)
20742368c37SMichal Marek {
20842368c37SMichal Marek 	va_list ap;
2095accd7f3SMasahiro Yamada 	char buf[4096];
2105accd7f3SMasahiro Yamada 
2115accd7f3SMasahiro Yamada 	if (!conf_message_callback)
2125accd7f3SMasahiro Yamada 		return;
21342368c37SMichal Marek 
21442368c37SMichal Marek 	va_start(ap, fmt);
2155accd7f3SMasahiro Yamada 
2165accd7f3SMasahiro Yamada 	vsnprintf(buf, sizeof(buf), fmt, ap);
2175accd7f3SMasahiro Yamada 	conf_message_callback(buf);
218b6a2ab2cSColin Ian King 	va_end(ap);
21942368c37SMichal Marek }
22042368c37SMichal Marek 
22114cdd3c4SRoman Zippel const char *conf_get_configname(void)
22214cdd3c4SRoman Zippel {
22314cdd3c4SRoman Zippel 	char *name = getenv("KCONFIG_CONFIG");
22414cdd3c4SRoman Zippel 
22514cdd3c4SRoman Zippel 	return name ? name : ".config";
22614cdd3c4SRoman Zippel }
22714cdd3c4SRoman Zippel 
2289b9f5948SMasahiro Yamada static const char *conf_get_autoconfig_name(void)
22912122f62SMarkus Heidelberg {
23012122f62SMarkus Heidelberg 	char *name = getenv("KCONFIG_AUTOCONFIG");
23112122f62SMarkus Heidelberg 
23212122f62SMarkus Heidelberg 	return name ? name : "include/config/auto.conf";
23312122f62SMarkus Heidelberg }
23412122f62SMarkus Heidelberg 
2359c900a9cSSam Ravnborg static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
2369c900a9cSSam Ravnborg {
2379c900a9cSSam Ravnborg 	char *p2;
2389c900a9cSSam Ravnborg 
2399c900a9cSSam Ravnborg 	switch (sym->type) {
2409c900a9cSSam Ravnborg 	case S_TRISTATE:
2419c900a9cSSam Ravnborg 		if (p[0] == 'm') {
2429c900a9cSSam Ravnborg 			sym->def[def].tri = mod;
2439c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2449c900a9cSSam Ravnborg 			break;
2459c900a9cSSam Ravnborg 		}
246d8fc3200SArnaud Lacombe 		/* fall through */
2479c900a9cSSam Ravnborg 	case S_BOOLEAN:
2489c900a9cSSam Ravnborg 		if (p[0] == 'y') {
2499c900a9cSSam Ravnborg 			sym->def[def].tri = yes;
2509c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2519c900a9cSSam Ravnborg 			break;
2529c900a9cSSam Ravnborg 		}
2539c900a9cSSam Ravnborg 		if (p[0] == 'n') {
2549c900a9cSSam Ravnborg 			sym->def[def].tri = no;
2559c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2569c900a9cSSam Ravnborg 			break;
2579c900a9cSSam Ravnborg 		}
25804b19b77SYann E. MORIN 		if (def != S_DEF_AUTO)
25904b19b77SYann E. MORIN 			conf_warning("symbol value '%s' invalid for %s",
26004b19b77SYann E. MORIN 				     p, sym->name);
26175f1468bSArnaud Lacombe 		return 1;
2629c900a9cSSam Ravnborg 	case S_STRING:
2639c900a9cSSam Ravnborg 		if (*p++ != '"')
2649c900a9cSSam Ravnborg 			break;
2659c900a9cSSam Ravnborg 		for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
2669c900a9cSSam Ravnborg 			if (*p2 == '"') {
2679c900a9cSSam Ravnborg 				*p2 = 0;
2689c900a9cSSam Ravnborg 				break;
2699c900a9cSSam Ravnborg 			}
2709c900a9cSSam Ravnborg 			memmove(p2, p2 + 1, strlen(p2));
2719c900a9cSSam Ravnborg 		}
2729c900a9cSSam Ravnborg 		if (!p2) {
27304b19b77SYann E. MORIN 			if (def != S_DEF_AUTO)
2749c900a9cSSam Ravnborg 				conf_warning("invalid string found");
2759c900a9cSSam Ravnborg 			return 1;
2769c900a9cSSam Ravnborg 		}
277d8fc3200SArnaud Lacombe 		/* fall through */
2789c900a9cSSam Ravnborg 	case S_INT:
2799c900a9cSSam Ravnborg 	case S_HEX:
2809c900a9cSSam Ravnborg 		if (sym_string_valid(sym, p)) {
281cd81fc82SMasahiro Yamada 			sym->def[def].val = xstrdup(p);
2829c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2839c900a9cSSam Ravnborg 		} else {
28404b19b77SYann E. MORIN 			if (def != S_DEF_AUTO)
28504b19b77SYann E. MORIN 				conf_warning("symbol value '%s' invalid for %s",
28604b19b77SYann E. MORIN 					     p, sym->name);
2879c900a9cSSam Ravnborg 			return 1;
2889c900a9cSSam Ravnborg 		}
2899c900a9cSSam Ravnborg 		break;
2909c900a9cSSam Ravnborg 	default:
2919c900a9cSSam Ravnborg 		;
2929c900a9cSSam Ravnborg 	}
2939c900a9cSSam Ravnborg 	return 0;
2949c900a9cSSam Ravnborg }
2959c900a9cSSam Ravnborg 
2961a7a8c6fSCody Schafer #define LINE_GROWTH 16
2971a7a8c6fSCody Schafer static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
2981a7a8c6fSCody Schafer {
2991a7a8c6fSCody Schafer 	char *nline;
3001a7a8c6fSCody Schafer 	size_t new_size = slen + 1;
3011a7a8c6fSCody Schafer 	if (new_size > *n) {
3021a7a8c6fSCody Schafer 		new_size += LINE_GROWTH - 1;
3031a7a8c6fSCody Schafer 		new_size *= 2;
304d717f24dSMasahiro Yamada 		nline = xrealloc(*lineptr, new_size);
3051a7a8c6fSCody Schafer 		if (!nline)
3061a7a8c6fSCody Schafer 			return -1;
3071a7a8c6fSCody Schafer 
3081a7a8c6fSCody Schafer 		*lineptr = nline;
3091a7a8c6fSCody Schafer 		*n = new_size;
3101a7a8c6fSCody Schafer 	}
3111a7a8c6fSCody Schafer 
3121a7a8c6fSCody Schafer 	(*lineptr)[slen] = c;
3131a7a8c6fSCody Schafer 
3141a7a8c6fSCody Schafer 	return 0;
3151a7a8c6fSCody Schafer }
3161a7a8c6fSCody Schafer 
3171a7a8c6fSCody Schafer static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
3181a7a8c6fSCody Schafer {
3191a7a8c6fSCody Schafer 	char *line = *lineptr;
3201a7a8c6fSCody Schafer 	size_t slen = 0;
3211a7a8c6fSCody Schafer 
3221a7a8c6fSCody Schafer 	for (;;) {
3231a7a8c6fSCody Schafer 		int c = getc(stream);
3241a7a8c6fSCody Schafer 
3251a7a8c6fSCody Schafer 		switch (c) {
3261a7a8c6fSCody Schafer 		case '\n':
3271a7a8c6fSCody Schafer 			if (add_byte(c, &line, slen, n) < 0)
3281a7a8c6fSCody Schafer 				goto e_out;
3291a7a8c6fSCody Schafer 			slen++;
3301a7a8c6fSCody Schafer 			/* fall through */
3311a7a8c6fSCody Schafer 		case EOF:
3321a7a8c6fSCody Schafer 			if (add_byte('\0', &line, slen, n) < 0)
3331a7a8c6fSCody Schafer 				goto e_out;
3341a7a8c6fSCody Schafer 			*lineptr = line;
3351a7a8c6fSCody Schafer 			if (slen == 0)
3361a7a8c6fSCody Schafer 				return -1;
3371a7a8c6fSCody Schafer 			return slen;
3381a7a8c6fSCody Schafer 		default:
3391a7a8c6fSCody Schafer 			if (add_byte(c, &line, slen, n) < 0)
3401a7a8c6fSCody Schafer 				goto e_out;
3411a7a8c6fSCody Schafer 			slen++;
3421a7a8c6fSCody Schafer 		}
3431a7a8c6fSCody Schafer 	}
3441a7a8c6fSCody Schafer 
3451a7a8c6fSCody Schafer e_out:
3461a7a8c6fSCody Schafer 	line[slen-1] = '\0';
3471a7a8c6fSCody Schafer 	*lineptr = line;
3481a7a8c6fSCody Schafer 	return -1;
3491a7a8c6fSCody Schafer }
3501a7a8c6fSCody Schafer 
351669bfad9SRoman Zippel int conf_read_simple(const char *name, int def)
3521da177e4SLinus Torvalds {
3531da177e4SLinus Torvalds 	FILE *in = NULL;
3541a7a8c6fSCody Schafer 	char   *line = NULL;
3551a7a8c6fSCody Schafer 	size_t  line_asize = 0;
3561da177e4SLinus Torvalds 	char *p, *p2;
3571da177e4SLinus Torvalds 	struct symbol *sym;
358669bfad9SRoman Zippel 	int i, def_flags;
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	if (name) {
3611da177e4SLinus Torvalds 		in = zconf_fopen(name);
3621da177e4SLinus Torvalds 	} else {
363face4374SRoman Zippel 		struct property *prop;
364face4374SRoman Zippel 
36514cdd3c4SRoman Zippel 		name = conf_get_configname();
366ddc97cacSRoman Zippel 		in = zconf_fopen(name);
367ddc97cacSRoman Zippel 		if (in)
368ddc97cacSRoman Zippel 			goto load;
369bfc10001SKarsten Wiese 		sym_add_change_count(1);
3706b87b70cSAl Viro 		if (!sym_defconfig_list)
371face4374SRoman Zippel 			return 1;
372face4374SRoman Zippel 
373face4374SRoman Zippel 		for_all_defaults(sym_defconfig_list, prop) {
374face4374SRoman Zippel 			if (expr_calc_value(prop->visible.expr) == no ||
375face4374SRoman Zippel 			    prop->expr->type != E_SYMBOL)
376face4374SRoman Zippel 				continue;
377104daea1SMasahiro Yamada 			sym_calc_value(prop->expr->left.sym);
378104daea1SMasahiro Yamada 			name = sym_get_string_value(prop->expr->left.sym);
3791da177e4SLinus Torvalds 			in = zconf_fopen(name);
3801da177e4SLinus Torvalds 			if (in) {
381694c49a7SSam Ravnborg 				conf_message("using defaults found in %s",
38242368c37SMichal Marek 					 name);
383ddc97cacSRoman Zippel 				goto load;
3841da177e4SLinus Torvalds 			}
3851da177e4SLinus Torvalds 		}
3861da177e4SLinus Torvalds 	}
3871da177e4SLinus Torvalds 	if (!in)
3881da177e4SLinus Torvalds 		return 1;
3891da177e4SLinus Torvalds 
390ddc97cacSRoman Zippel load:
391c1a0f5e3SRoman Zippel 	conf_filename = name;
392c1a0f5e3SRoman Zippel 	conf_lineno = 0;
393c1a0f5e3SRoman Zippel 	conf_warnings = 0;
394c1a0f5e3SRoman Zippel 
395669bfad9SRoman Zippel 	def_flags = SYMBOL_DEF << def;
3961da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
397669bfad9SRoman Zippel 		sym->flags |= SYMBOL_CHANGED;
398669bfad9SRoman Zippel 		sym->flags &= ~(def_flags|SYMBOL_VALID);
399490f1617SYann E. MORIN 		if (sym_is_choice(sym))
400490f1617SYann E. MORIN 			sym->flags |= def_flags;
4011da177e4SLinus Torvalds 		switch (sym->type) {
4021da177e4SLinus Torvalds 		case S_INT:
4031da177e4SLinus Torvalds 		case S_HEX:
4041da177e4SLinus Torvalds 		case S_STRING:
405669bfad9SRoman Zippel 			if (sym->def[def].val)
406669bfad9SRoman Zippel 				free(sym->def[def].val);
407d8fc3200SArnaud Lacombe 			/* fall through */
4081da177e4SLinus Torvalds 		default:
409669bfad9SRoman Zippel 			sym->def[def].val = NULL;
410669bfad9SRoman Zippel 			sym->def[def].tri = no;
4111da177e4SLinus Torvalds 		}
4121da177e4SLinus Torvalds 	}
4131da177e4SLinus Torvalds 
4141a7a8c6fSCody Schafer 	while (compat_getline(&line, &line_asize, in) != -1) {
415c1a0f5e3SRoman Zippel 		conf_lineno++;
4161da177e4SLinus Torvalds 		sym = NULL;
4178baefd30SArnaud Lacombe 		if (line[0] == '#') {
418ffb5957bSArnaud Lacombe 			if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
4191da177e4SLinus Torvalds 				continue;
420ffb5957bSArnaud Lacombe 			p = strchr(line + 2 + strlen(CONFIG_), ' ');
4211da177e4SLinus Torvalds 			if (!p)
4221da177e4SLinus Torvalds 				continue;
4231da177e4SLinus Torvalds 			*p++ = 0;
4241da177e4SLinus Torvalds 			if (strncmp(p, "is not set", 10))
4251da177e4SLinus Torvalds 				continue;
426669bfad9SRoman Zippel 			if (def == S_DEF_USER) {
427ffb5957bSArnaud Lacombe 				sym = sym_find(line + 2 + strlen(CONFIG_));
428661b0680Szippel@linux-m68k.org 				if (!sym) {
429661b0680Szippel@linux-m68k.org 					sym_add_change_count(1);
43075889e9bSMasahiro Yamada 					continue;
431661b0680Szippel@linux-m68k.org 				}
432669bfad9SRoman Zippel 			} else {
433ffb5957bSArnaud Lacombe 				sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
434669bfad9SRoman Zippel 				if (sym->type == S_UNKNOWN)
435669bfad9SRoman Zippel 					sym->type = S_BOOLEAN;
436669bfad9SRoman Zippel 			}
437669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
438d84876f9SJan Engelhardt 				conf_warning("override: reassigning to symbol %s", sym->name);
4391da177e4SLinus Torvalds 			}
4401da177e4SLinus Torvalds 			switch (sym->type) {
4411da177e4SLinus Torvalds 			case S_BOOLEAN:
4421da177e4SLinus Torvalds 			case S_TRISTATE:
443669bfad9SRoman Zippel 				sym->def[def].tri = no;
444669bfad9SRoman Zippel 				sym->flags |= def_flags;
4451da177e4SLinus Torvalds 				break;
4461da177e4SLinus Torvalds 			default:
4471da177e4SLinus Torvalds 				;
4481da177e4SLinus Torvalds 			}
449ffb5957bSArnaud Lacombe 		} else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
450ffb5957bSArnaud Lacombe 			p = strchr(line + strlen(CONFIG_), '=');
4511da177e4SLinus Torvalds 			if (!p)
4521da177e4SLinus Torvalds 				continue;
4531da177e4SLinus Torvalds 			*p++ = 0;
4541da177e4SLinus Torvalds 			p2 = strchr(p, '\n');
455d3660a8cSMatthew Wilcox 			if (p2) {
456d3660a8cSMatthew Wilcox 				*p2-- = 0;
457d3660a8cSMatthew Wilcox 				if (*p2 == '\r')
4581da177e4SLinus Torvalds 					*p2 = 0;
459d3660a8cSMatthew Wilcox 			}
4602aabbed6SMasahiro Yamada 
461ffb5957bSArnaud Lacombe 			sym = sym_find(line + strlen(CONFIG_));
462661b0680Szippel@linux-m68k.org 			if (!sym) {
4632aabbed6SMasahiro Yamada 				if (def == S_DEF_AUTO)
4642aabbed6SMasahiro Yamada 					/*
4652aabbed6SMasahiro Yamada 					 * Reading from include/config/auto.conf
4662aabbed6SMasahiro Yamada 					 * If CONFIG_FOO previously existed in
4672aabbed6SMasahiro Yamada 					 * auto.conf but it is missing now,
4682aabbed6SMasahiro Yamada 					 * include/config/foo.h must be touched.
4692aabbed6SMasahiro Yamada 					 */
4702aabbed6SMasahiro Yamada 					conf_touch_dep(line + strlen(CONFIG_));
4712aabbed6SMasahiro Yamada 				else
472661b0680Szippel@linux-m68k.org 					sym_add_change_count(1);
47375889e9bSMasahiro Yamada 				continue;
474661b0680Szippel@linux-m68k.org 			}
4752aabbed6SMasahiro Yamada 
476669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
477d84876f9SJan Engelhardt 				conf_warning("override: reassigning to symbol %s", sym->name);
4781da177e4SLinus Torvalds 			}
4799c900a9cSSam Ravnborg 			if (conf_set_sym_val(sym, def, def_flags, p))
480c1a0f5e3SRoman Zippel 				continue;
4818baefd30SArnaud Lacombe 		} else {
4828baefd30SArnaud Lacombe 			if (line[0] != '\r' && line[0] != '\n')
483a4663911SPaul Bolle 				conf_warning("unexpected data: %.*s",
484a4663911SPaul Bolle 					     (int)strcspn(line, "\r\n"), line);
485a4663911SPaul Bolle 
4861da177e4SLinus Torvalds 			continue;
4871da177e4SLinus Torvalds 		}
48875889e9bSMasahiro Yamada 
4891da177e4SLinus Torvalds 		if (sym && sym_is_choice_value(sym)) {
4901da177e4SLinus Torvalds 			struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
491669bfad9SRoman Zippel 			switch (sym->def[def].tri) {
4921da177e4SLinus Torvalds 			case no:
4931da177e4SLinus Torvalds 				break;
4941da177e4SLinus Torvalds 			case mod:
495669bfad9SRoman Zippel 				if (cs->def[def].tri == yes) {
496c1a0f5e3SRoman Zippel 					conf_warning("%s creates inconsistent choice state", sym->name);
497490f1617SYann E. MORIN 					cs->flags &= ~def_flags;
498c1a0f5e3SRoman Zippel 				}
4991da177e4SLinus Torvalds 				break;
5001da177e4SLinus Torvalds 			case yes:
501d84876f9SJan Engelhardt 				if (cs->def[def].tri != no)
502d84876f9SJan Engelhardt 					conf_warning("override: %s changes choice state", sym->name);
503669bfad9SRoman Zippel 				cs->def[def].val = sym;
5041da177e4SLinus Torvalds 				break;
5051da177e4SLinus Torvalds 			}
506d6ee3576SSam Ravnborg 			cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
5071da177e4SLinus Torvalds 		}
5081da177e4SLinus Torvalds 	}
5091a7a8c6fSCody Schafer 	free(line);
5101da177e4SLinus Torvalds 	fclose(in);
51190389160SRoman Zippel 	return 0;
51290389160SRoman Zippel }
51390389160SRoman Zippel 
51490389160SRoman Zippel int conf_read(const char *name)
51590389160SRoman Zippel {
5165d09598dSArnaud Lacombe 	struct symbol *sym;
51784dd95d4SMasahiro Yamada 	int conf_unsaved = 0;
5185d09598dSArnaud Lacombe 	int i;
51990389160SRoman Zippel 
520bfc10001SKarsten Wiese 	sym_set_change_count(0);
521ddc97cacSRoman Zippel 
5226b87b70cSAl Viro 	if (conf_read_simple(name, S_DEF_USER)) {
5236b87b70cSAl Viro 		sym_calc_value(modules_sym);
52490389160SRoman Zippel 		return 1;
5256b87b70cSAl Viro 	}
5266b87b70cSAl Viro 
5276b87b70cSAl Viro 	sym_calc_value(modules_sym);
52890389160SRoman Zippel 
5291da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
5301da177e4SLinus Torvalds 		sym_calc_value(sym);
531693359f7SDirk Gouders 		if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
5325d09598dSArnaud Lacombe 			continue;
533c1a0f5e3SRoman Zippel 		if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
534c1a0f5e3SRoman Zippel 			/* check that calculated value agrees with saved value */
535c1a0f5e3SRoman Zippel 			switch (sym->type) {
536c1a0f5e3SRoman Zippel 			case S_BOOLEAN:
537c1a0f5e3SRoman Zippel 			case S_TRISTATE:
538e3cd5136SMasahiro Yamada 				if (sym->def[S_DEF_USER].tri == sym_get_tristate_value(sym))
5395d09598dSArnaud Lacombe 					continue;
540e3cd5136SMasahiro Yamada 				break;
541c1a0f5e3SRoman Zippel 			default:
5420c1822e6SRoman Zippel 				if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
5435d09598dSArnaud Lacombe 					continue;
544c1a0f5e3SRoman Zippel 				break;
545c1a0f5e3SRoman Zippel 			}
546c1a0f5e3SRoman Zippel 		} else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
547c1a0f5e3SRoman Zippel 			/* no previous value and not saved */
5485d09598dSArnaud Lacombe 			continue;
549c1a0f5e3SRoman Zippel 		conf_unsaved++;
550c1a0f5e3SRoman Zippel 		/* maybe print value in verbose mode... */
5511da177e4SLinus Torvalds 	}
5521da177e4SLinus Torvalds 
553d8982ba1SRoman Zippel 	for_all_symbols(i, sym) {
554d8982ba1SRoman Zippel 		if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
555d8982ba1SRoman Zippel 			/* Reset values of generates values, so they'll appear
556d8982ba1SRoman Zippel 			 * as new, if they should become visible, but that
557d8982ba1SRoman Zippel 			 * doesn't quite work if the Kconfig and the saved
558d8982ba1SRoman Zippel 			 * configuration disagree.
559d8982ba1SRoman Zippel 			 */
560d8982ba1SRoman Zippel 			if (sym->visible == no && !conf_unsaved)
561d8982ba1SRoman Zippel 				sym->flags &= ~SYMBOL_DEF_USER;
562d8982ba1SRoman Zippel 			switch (sym->type) {
563d8982ba1SRoman Zippel 			case S_STRING:
564d8982ba1SRoman Zippel 			case S_INT:
565d8982ba1SRoman Zippel 			case S_HEX:
566d8982ba1SRoman Zippel 				/* Reset a string value if it's out of range */
567d8982ba1SRoman Zippel 				if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
568d8982ba1SRoman Zippel 					break;
569d8982ba1SRoman Zippel 				sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
570d8982ba1SRoman Zippel 				conf_unsaved++;
571d8982ba1SRoman Zippel 				break;
572d8982ba1SRoman Zippel 			default:
573d8982ba1SRoman Zippel 				break;
574d8982ba1SRoman Zippel 			}
575d8982ba1SRoman Zippel 		}
576d8982ba1SRoman Zippel 	}
577d8982ba1SRoman Zippel 
578bfc10001SKarsten Wiese 	sym_add_change_count(conf_warnings || conf_unsaved);
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	return 0;
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds 
583e54e692bSArnaud Lacombe /*
584e54e692bSArnaud Lacombe  * Kconfig configuration printer
585e54e692bSArnaud Lacombe  *
586e54e692bSArnaud Lacombe  * This printer is used when generating the resulting configuration after
587e54e692bSArnaud Lacombe  * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
588e54e692bSArnaud Lacombe  * passing a non-NULL argument to the printer.
589e54e692bSArnaud Lacombe  *
590e54e692bSArnaud Lacombe  */
591e54e692bSArnaud Lacombe static void
592e54e692bSArnaud Lacombe kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
59349192f26SSam Ravnborg {
59449192f26SSam Ravnborg 
5950dce6310SArnaud Lacombe 	switch (sym->type) {
59649192f26SSam Ravnborg 	case S_BOOLEAN:
59749192f26SSam Ravnborg 	case S_TRISTATE:
598e54e692bSArnaud Lacombe 		if (*value == 'n') {
599e54e692bSArnaud Lacombe 			bool skip_unset = (arg != NULL);
600e54e692bSArnaud Lacombe 
601e54e692bSArnaud Lacombe 			if (!skip_unset)
602e54e692bSArnaud Lacombe 				fprintf(fp, "# %s%s is not set\n",
603ffb5957bSArnaud Lacombe 				    CONFIG_, sym->name);
604e54e692bSArnaud Lacombe 			return;
60549192f26SSam Ravnborg 		}
60649192f26SSam Ravnborg 		break;
607e54e692bSArnaud Lacombe 	default:
60849192f26SSam Ravnborg 		break;
609e54e692bSArnaud Lacombe 	}
610e54e692bSArnaud Lacombe 
611e54e692bSArnaud Lacombe 	fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
612e54e692bSArnaud Lacombe }
613e54e692bSArnaud Lacombe 
614e54e692bSArnaud Lacombe static void
615e54e692bSArnaud Lacombe kconfig_print_comment(FILE *fp, const char *value, void *arg)
616e54e692bSArnaud Lacombe {
617e54e692bSArnaud Lacombe 	const char *p = value;
618e54e692bSArnaud Lacombe 	size_t l;
619e54e692bSArnaud Lacombe 
620e54e692bSArnaud Lacombe 	for (;;) {
621e54e692bSArnaud Lacombe 		l = strcspn(p, "\n");
622e54e692bSArnaud Lacombe 		fprintf(fp, "#");
623e54e692bSArnaud Lacombe 		if (l) {
624e54e692bSArnaud Lacombe 			fprintf(fp, " ");
62570cc01e7SPeter Foley 			xfwrite(p, l, 1, fp);
626e54e692bSArnaud Lacombe 			p += l;
627e54e692bSArnaud Lacombe 		}
628e54e692bSArnaud Lacombe 		fprintf(fp, "\n");
629e54e692bSArnaud Lacombe 		if (*p++ == '\0')
63049192f26SSam Ravnborg 			break;
631e54e692bSArnaud Lacombe 	}
632e54e692bSArnaud Lacombe }
633e54e692bSArnaud Lacombe 
634e54e692bSArnaud Lacombe static struct conf_printer kconfig_printer_cb =
635e54e692bSArnaud Lacombe {
636e54e692bSArnaud Lacombe 	.print_symbol = kconfig_print_symbol,
637e54e692bSArnaud Lacombe 	.print_comment = kconfig_print_comment,
638e54e692bSArnaud Lacombe };
639e54e692bSArnaud Lacombe 
640e54e692bSArnaud Lacombe /*
641e54e692bSArnaud Lacombe  * Header printer
642e54e692bSArnaud Lacombe  *
643e54e692bSArnaud Lacombe  * This printer is used when generating the `include/generated/autoconf.h' file.
644e54e692bSArnaud Lacombe  */
645e54e692bSArnaud Lacombe static void
646e54e692bSArnaud Lacombe header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
647e54e692bSArnaud Lacombe {
648e54e692bSArnaud Lacombe 
649e54e692bSArnaud Lacombe 	switch (sym->type) {
650e54e692bSArnaud Lacombe 	case S_BOOLEAN:
651eb4cf5a6SArnaud Lacombe 	case S_TRISTATE: {
652eb4cf5a6SArnaud Lacombe 		const char *suffix = "";
653eb4cf5a6SArnaud Lacombe 
654e54e692bSArnaud Lacombe 		switch (*value) {
655e54e692bSArnaud Lacombe 		case 'n':
6562a11c8eaSMichal Marek 			break;
657e54e692bSArnaud Lacombe 		case 'm':
658e54e692bSArnaud Lacombe 			suffix = "_MODULE";
659eb4cf5a6SArnaud Lacombe 			/* fall through */
660e54e692bSArnaud Lacombe 		default:
6612a11c8eaSMichal Marek 			fprintf(fp, "#define %s%s%s 1\n",
6622a11c8eaSMichal Marek 			    CONFIG_, sym->name, suffix);
663e54e692bSArnaud Lacombe 		}
664eb4cf5a6SArnaud Lacombe 		break;
665eb4cf5a6SArnaud Lacombe 	}
666eb4cf5a6SArnaud Lacombe 	case S_HEX: {
667eb4cf5a6SArnaud Lacombe 		const char *prefix = "";
668eb4cf5a6SArnaud Lacombe 
669eb4cf5a6SArnaud Lacombe 		if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
670eb4cf5a6SArnaud Lacombe 			prefix = "0x";
671eb4cf5a6SArnaud Lacombe 		fprintf(fp, "#define %s%s %s%s\n",
672eb4cf5a6SArnaud Lacombe 		    CONFIG_, sym->name, prefix, value);
673eb4cf5a6SArnaud Lacombe 		break;
674eb4cf5a6SArnaud Lacombe 	}
675eb4cf5a6SArnaud Lacombe 	case S_STRING:
676eb4cf5a6SArnaud Lacombe 	case S_INT:
677eb4cf5a6SArnaud Lacombe 		fprintf(fp, "#define %s%s %s\n",
678eb4cf5a6SArnaud Lacombe 		    CONFIG_, sym->name, value);
679e54e692bSArnaud Lacombe 		break;
680e54e692bSArnaud Lacombe 	default:
681e54e692bSArnaud Lacombe 		break;
682e54e692bSArnaud Lacombe 	}
683e54e692bSArnaud Lacombe 
684e54e692bSArnaud Lacombe }
685e54e692bSArnaud Lacombe 
686e54e692bSArnaud Lacombe static void
687e54e692bSArnaud Lacombe header_print_comment(FILE *fp, const char *value, void *arg)
688e54e692bSArnaud Lacombe {
689e54e692bSArnaud Lacombe 	const char *p = value;
690e54e692bSArnaud Lacombe 	size_t l;
691e54e692bSArnaud Lacombe 
692e54e692bSArnaud Lacombe 	fprintf(fp, "/*\n");
693e54e692bSArnaud Lacombe 	for (;;) {
694e54e692bSArnaud Lacombe 		l = strcspn(p, "\n");
695e54e692bSArnaud Lacombe 		fprintf(fp, " *");
696e54e692bSArnaud Lacombe 		if (l) {
697e54e692bSArnaud Lacombe 			fprintf(fp, " ");
69870cc01e7SPeter Foley 			xfwrite(p, l, 1, fp);
699e54e692bSArnaud Lacombe 			p += l;
700e54e692bSArnaud Lacombe 		}
701e54e692bSArnaud Lacombe 		fprintf(fp, "\n");
702e54e692bSArnaud Lacombe 		if (*p++ == '\0')
703e54e692bSArnaud Lacombe 			break;
704e54e692bSArnaud Lacombe 	}
705e54e692bSArnaud Lacombe 	fprintf(fp, " */\n");
706e54e692bSArnaud Lacombe }
707e54e692bSArnaud Lacombe 
708e54e692bSArnaud Lacombe static struct conf_printer header_printer_cb =
709e54e692bSArnaud Lacombe {
710e54e692bSArnaud Lacombe 	.print_symbol = header_print_symbol,
711e54e692bSArnaud Lacombe 	.print_comment = header_print_comment,
712e54e692bSArnaud Lacombe };
713e54e692bSArnaud Lacombe 
714e54e692bSArnaud Lacombe static void conf_write_symbol(FILE *fp, struct symbol *sym,
715e54e692bSArnaud Lacombe 			      struct conf_printer *printer, void *printer_arg)
716e54e692bSArnaud Lacombe {
717e54e692bSArnaud Lacombe 	const char *str;
718e54e692bSArnaud Lacombe 
719e54e692bSArnaud Lacombe 	switch (sym->type) {
72049192f26SSam Ravnborg 	case S_UNKNOWN:
72149192f26SSam Ravnborg 		break;
722e54e692bSArnaud Lacombe 	case S_STRING:
723e54e692bSArnaud Lacombe 		str = sym_get_string_value(sym);
724e54e692bSArnaud Lacombe 		str = sym_escape_string_value(str);
725e54e692bSArnaud Lacombe 		printer->print_symbol(fp, sym, str, printer_arg);
726e54e692bSArnaud Lacombe 		free((void *)str);
727e54e692bSArnaud Lacombe 		break;
728e54e692bSArnaud Lacombe 	default:
729e54e692bSArnaud Lacombe 		str = sym_get_string_value(sym);
730e54e692bSArnaud Lacombe 		printer->print_symbol(fp, sym, str, printer_arg);
73149192f26SSam Ravnborg 	}
73249192f26SSam Ravnborg }
73349192f26SSam Ravnborg 
734e54e692bSArnaud Lacombe static void
735e54e692bSArnaud Lacombe conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
736e54e692bSArnaud Lacombe {
737e54e692bSArnaud Lacombe 	char buf[256];
738e54e692bSArnaud Lacombe 
739e54e692bSArnaud Lacombe 	snprintf(buf, sizeof(buf),
740e54e692bSArnaud Lacombe 	    "\n"
741e54e692bSArnaud Lacombe 	    "Automatically generated file; DO NOT EDIT.\n"
742e54e692bSArnaud Lacombe 	    "%s\n",
743e54e692bSArnaud Lacombe 	    rootmenu.prompt->text);
744e54e692bSArnaud Lacombe 
745e54e692bSArnaud Lacombe 	printer->print_comment(fp, buf, printer_arg);
746e54e692bSArnaud Lacombe }
747e54e692bSArnaud Lacombe 
7487cf3d73bSSam Ravnborg /*
7497cf3d73bSSam Ravnborg  * Write out a minimal config.
7507cf3d73bSSam Ravnborg  * All values that has default values are skipped as this is redundant.
7517cf3d73bSSam Ravnborg  */
7527cf3d73bSSam Ravnborg int conf_write_defconfig(const char *filename)
7537cf3d73bSSam Ravnborg {
7547cf3d73bSSam Ravnborg 	struct symbol *sym;
7557cf3d73bSSam Ravnborg 	struct menu *menu;
7567cf3d73bSSam Ravnborg 	FILE *out;
7577cf3d73bSSam Ravnborg 
7587cf3d73bSSam Ravnborg 	out = fopen(filename, "w");
7597cf3d73bSSam Ravnborg 	if (!out)
7607cf3d73bSSam Ravnborg 		return 1;
7617cf3d73bSSam Ravnborg 
7627cf3d73bSSam Ravnborg 	sym_clear_all_valid();
7637cf3d73bSSam Ravnborg 
7647cf3d73bSSam Ravnborg 	/* Traverse all menus to find all relevant symbols */
7657cf3d73bSSam Ravnborg 	menu = rootmenu.list;
7667cf3d73bSSam Ravnborg 
7677cf3d73bSSam Ravnborg 	while (menu != NULL)
7687cf3d73bSSam Ravnborg 	{
7697cf3d73bSSam Ravnborg 		sym = menu->sym;
7707cf3d73bSSam Ravnborg 		if (sym == NULL) {
7717cf3d73bSSam Ravnborg 			if (!menu_is_visible(menu))
7727cf3d73bSSam Ravnborg 				goto next_menu;
7737cf3d73bSSam Ravnborg 		} else if (!sym_is_choice(sym)) {
7747cf3d73bSSam Ravnborg 			sym_calc_value(sym);
7757cf3d73bSSam Ravnborg 			if (!(sym->flags & SYMBOL_WRITE))
7767cf3d73bSSam Ravnborg 				goto next_menu;
7777cf3d73bSSam Ravnborg 			sym->flags &= ~SYMBOL_WRITE;
7787cf3d73bSSam Ravnborg 			/* If we cannot change the symbol - skip */
779baa23ec8SMarco Ammon 			if (!sym_is_changeable(sym))
7807cf3d73bSSam Ravnborg 				goto next_menu;
7817cf3d73bSSam Ravnborg 			/* If symbol equals to default value - skip */
7827cf3d73bSSam Ravnborg 			if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
7837cf3d73bSSam Ravnborg 				goto next_menu;
7847cf3d73bSSam Ravnborg 
7857cf3d73bSSam Ravnborg 			/*
7867cf3d73bSSam Ravnborg 			 * If symbol is a choice value and equals to the
7877cf3d73bSSam Ravnborg 			 * default for a choice - skip.
78884062dd3SSam Ravnborg 			 * But only if value is bool and equal to "y" and
78984062dd3SSam Ravnborg 			 * choice is not "optional".
79084062dd3SSam Ravnborg 			 * (If choice is "optional" then all values can be "n")
7917cf3d73bSSam Ravnborg 			 */
7927cf3d73bSSam Ravnborg 			if (sym_is_choice_value(sym)) {
7937cf3d73bSSam Ravnborg 				struct symbol *cs;
7947cf3d73bSSam Ravnborg 				struct symbol *ds;
7957cf3d73bSSam Ravnborg 
7967cf3d73bSSam Ravnborg 				cs = prop_get_symbol(sym_get_choice_prop(sym));
7977cf3d73bSSam Ravnborg 				ds = sym_choice_default(cs);
79884062dd3SSam Ravnborg 				if (!sym_is_optional(cs) && sym == ds) {
799801690caSSam Ravnborg 					if ((sym->type == S_BOOLEAN) &&
8007cf3d73bSSam Ravnborg 					    sym_get_tristate_value(sym) == yes)
8017cf3d73bSSam Ravnborg 						goto next_menu;
8027cf3d73bSSam Ravnborg 				}
8037cf3d73bSSam Ravnborg 			}
804e54e692bSArnaud Lacombe 			conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
8057cf3d73bSSam Ravnborg 		}
8067cf3d73bSSam Ravnborg next_menu:
8077cf3d73bSSam Ravnborg 		if (menu->list != NULL) {
8087cf3d73bSSam Ravnborg 			menu = menu->list;
8097cf3d73bSSam Ravnborg 		}
8107cf3d73bSSam Ravnborg 		else if (menu->next != NULL) {
8117cf3d73bSSam Ravnborg 			menu = menu->next;
8127cf3d73bSSam Ravnborg 		} else {
8137cf3d73bSSam Ravnborg 			while ((menu = menu->parent)) {
8147cf3d73bSSam Ravnborg 				if (menu->next != NULL) {
8157cf3d73bSSam Ravnborg 					menu = menu->next;
8167cf3d73bSSam Ravnborg 					break;
8177cf3d73bSSam Ravnborg 				}
8187cf3d73bSSam Ravnborg 			}
8197cf3d73bSSam Ravnborg 		}
8207cf3d73bSSam Ravnborg 	}
8217cf3d73bSSam Ravnborg 	fclose(out);
8227cf3d73bSSam Ravnborg 	return 0;
8237cf3d73bSSam Ravnborg }
8247cf3d73bSSam Ravnborg 
8251da177e4SLinus Torvalds int conf_write(const char *name)
8261da177e4SLinus Torvalds {
827c955ccafSRoman Zippel 	FILE *out;
8281da177e4SLinus Torvalds 	struct symbol *sym;
8291da177e4SLinus Torvalds 	struct menu *menu;
8301da177e4SLinus Torvalds 	const char *str;
831ceb7f329SMasahiro Yamada 	char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
8321da177e4SLinus Torvalds 	char *env;
8330c5b6c28SM. Vefa Bicakci 	int i;
834aff11cd9SAlexander Popov 	bool need_newline = false;
8351da177e4SLinus Torvalds 
836ceb7f329SMasahiro Yamada 	if (!name)
837ceb7f329SMasahiro Yamada 		name = conf_get_configname();
838ceb7f329SMasahiro Yamada 
839ceb7f329SMasahiro Yamada 	if (!*name) {
840ceb7f329SMasahiro Yamada 		fprintf(stderr, "config name is empty\n");
841ceb7f329SMasahiro Yamada 		return -1;
842ceb7f329SMasahiro Yamada 	}
8431da177e4SLinus Torvalds 
8440608182aSMasahiro Yamada 	if (is_dir(name)) {
845ceb7f329SMasahiro Yamada 		fprintf(stderr, "%s: Is a directory\n", name);
846ceb7f329SMasahiro Yamada 		return -1;
847ceb7f329SMasahiro Yamada 	}
8481da177e4SLinus Torvalds 
849580c5b3eSMasahiro Yamada 	if (make_parent_dir(name))
850580c5b3eSMasahiro Yamada 		return -1;
851580c5b3eSMasahiro Yamada 
85214cdd3c4SRoman Zippel 	env = getenv("KCONFIG_OVERWRITECONFIG");
853ceb7f329SMasahiro Yamada 	if (env && *env) {
85414cdd3c4SRoman Zippel 		*tmpname = 0;
855ceb7f329SMasahiro Yamada 		out = fopen(name, "w");
856ceb7f329SMasahiro Yamada 	} else {
857ceb7f329SMasahiro Yamada 		snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp",
858ceb7f329SMasahiro Yamada 			 name, (int)getpid());
859ceb7f329SMasahiro Yamada 		out = fopen(tmpname, "w");
86014cdd3c4SRoman Zippel 	}
8611da177e4SLinus Torvalds 	if (!out)
8621da177e4SLinus Torvalds 		return 1;
86314cdd3c4SRoman Zippel 
864e54e692bSArnaud Lacombe 	conf_write_heading(out, &kconfig_printer_cb, NULL);
8651da177e4SLinus Torvalds 
866b3214293SKarsten Wiese 	if (!conf_get_changed())
8671da177e4SLinus Torvalds 		sym_clear_all_valid();
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	menu = rootmenu.list;
8701da177e4SLinus Torvalds 	while (menu) {
8711da177e4SLinus Torvalds 		sym = menu->sym;
8721da177e4SLinus Torvalds 		if (!sym) {
8731da177e4SLinus Torvalds 			if (!menu_is_visible(menu))
8741da177e4SLinus Torvalds 				goto next;
8751da177e4SLinus Torvalds 			str = menu_get_prompt(menu);
8761da177e4SLinus Torvalds 			fprintf(out, "\n"
8771da177e4SLinus Torvalds 				     "#\n"
8781da177e4SLinus Torvalds 				     "# %s\n"
8791da177e4SLinus Torvalds 				     "#\n", str);
880aff11cd9SAlexander Popov 			need_newline = false;
8818e2442a5SMasahiro Yamada 		} else if (!(sym->flags & SYMBOL_CHOICE) &&
8828e2442a5SMasahiro Yamada 			   !(sym->flags & SYMBOL_WRITTEN)) {
8831da177e4SLinus Torvalds 			sym_calc_value(sym);
8841da177e4SLinus Torvalds 			if (!(sym->flags & SYMBOL_WRITE))
8851da177e4SLinus Torvalds 				goto next;
886aff11cd9SAlexander Popov 			if (need_newline) {
887aff11cd9SAlexander Popov 				fprintf(out, "\n");
888aff11cd9SAlexander Popov 				need_newline = false;
889aff11cd9SAlexander Popov 			}
8908e2442a5SMasahiro Yamada 			sym->flags |= SYMBOL_WRITTEN;
891e54e692bSArnaud Lacombe 			conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
8921da177e4SLinus Torvalds 		}
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds next:
8951da177e4SLinus Torvalds 		if (menu->list) {
8961da177e4SLinus Torvalds 			menu = menu->list;
8971da177e4SLinus Torvalds 			continue;
8981da177e4SLinus Torvalds 		}
8991da177e4SLinus Torvalds 		if (menu->next)
9001da177e4SLinus Torvalds 			menu = menu->next;
9011da177e4SLinus Torvalds 		else while ((menu = menu->parent)) {
902aff11cd9SAlexander Popov 			if (!menu->sym && menu_is_visible(menu) &&
903aff11cd9SAlexander Popov 			    menu != &rootmenu) {
904aff11cd9SAlexander Popov 				str = menu_get_prompt(menu);
905aff11cd9SAlexander Popov 				fprintf(out, "# end of %s\n", str);
906aff11cd9SAlexander Popov 				need_newline = true;
907aff11cd9SAlexander Popov 			}
9081da177e4SLinus Torvalds 			if (menu->next) {
9091da177e4SLinus Torvalds 				menu = menu->next;
9101da177e4SLinus Torvalds 				break;
9111da177e4SLinus Torvalds 			}
9121da177e4SLinus Torvalds 		}
9131da177e4SLinus Torvalds 	}
9141da177e4SLinus Torvalds 	fclose(out);
91514cdd3c4SRoman Zippel 
9160c5b6c28SM. Vefa Bicakci 	for_all_symbols(i, sym)
9170c5b6c28SM. Vefa Bicakci 		sym->flags &= ~SYMBOL_WRITTEN;
9180c5b6c28SM. Vefa Bicakci 
91914cdd3c4SRoman Zippel 	if (*tmpname) {
92067424f61SMasahiro Yamada 		if (is_same(name, tmpname)) {
92167424f61SMasahiro Yamada 			conf_message("No change to %s", name);
92267424f61SMasahiro Yamada 			unlink(tmpname);
92367424f61SMasahiro Yamada 			sym_set_change_count(0);
92467424f61SMasahiro Yamada 			return 0;
92567424f61SMasahiro Yamada 		}
92667424f61SMasahiro Yamada 
927ceb7f329SMasahiro Yamada 		snprintf(oldname, sizeof(oldname), "%s.old", name);
928ceb7f329SMasahiro Yamada 		rename(name, oldname);
929ceb7f329SMasahiro Yamada 		if (rename(tmpname, name))
9301da177e4SLinus Torvalds 			return 1;
93114cdd3c4SRoman Zippel 	}
9321da177e4SLinus Torvalds 
933ceb7f329SMasahiro Yamada 	conf_message("configuration written to %s", name);
934ddc97cacSRoman Zippel 
935bfc10001SKarsten Wiese 	sym_set_change_count(0);
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	return 0;
9381da177e4SLinus Torvalds }
939c955ccafSRoman Zippel 
940a2ff4040SMasahiro Yamada /* write a dependency file as used by kbuild to track dependencies */
941a2ff4040SMasahiro Yamada static int conf_write_dep(const char *name)
942a2ff4040SMasahiro Yamada {
943a2ff4040SMasahiro Yamada 	struct file *file;
944a2ff4040SMasahiro Yamada 	FILE *out;
945a2ff4040SMasahiro Yamada 
946a2ff4040SMasahiro Yamada 	out = fopen("..config.tmp", "w");
947a2ff4040SMasahiro Yamada 	if (!out)
948a2ff4040SMasahiro Yamada 		return 1;
949a2ff4040SMasahiro Yamada 	fprintf(out, "deps_config := \\\n");
950a2ff4040SMasahiro Yamada 	for (file = file_list; file; file = file->next) {
951a2ff4040SMasahiro Yamada 		if (file->next)
952a2ff4040SMasahiro Yamada 			fprintf(out, "\t%s \\\n", file->name);
953a2ff4040SMasahiro Yamada 		else
954a2ff4040SMasahiro Yamada 			fprintf(out, "\t%s\n", file->name);
955a2ff4040SMasahiro Yamada 	}
956a2ff4040SMasahiro Yamada 	fprintf(out, "\n%s: \\\n"
957a2ff4040SMasahiro Yamada 		     "\t$(deps_config)\n\n", conf_get_autoconfig_name());
958a2ff4040SMasahiro Yamada 
959a2ff4040SMasahiro Yamada 	env_write_dep(out, conf_get_autoconfig_name());
960a2ff4040SMasahiro Yamada 
961a2ff4040SMasahiro Yamada 	fprintf(out, "\n$(deps_config): ;\n");
962a2ff4040SMasahiro Yamada 	fclose(out);
96379123b13SMasahiro Yamada 
96479123b13SMasahiro Yamada 	if (make_parent_dir(name))
96579123b13SMasahiro Yamada 		return 1;
966a2ff4040SMasahiro Yamada 	rename("..config.tmp", name);
967a2ff4040SMasahiro Yamada 	return 0;
968a2ff4040SMasahiro Yamada }
969a2ff4040SMasahiro Yamada 
9700849d212SMasahiro Yamada static int conf_touch_deps(void)
9712e3646e5SRoman Zippel {
97212122f62SMarkus Heidelberg 	const char *name;
9732e3646e5SRoman Zippel 	struct symbol *sym;
9741508fec8SMasahiro Yamada 	int res, i;
9751508fec8SMasahiro Yamada 
9761508fec8SMasahiro Yamada 	strcpy(depfile_path, "include/config/");
9771508fec8SMasahiro Yamada 	depfile_prefix_len = strlen(depfile_path);
9782e3646e5SRoman Zippel 
97912122f62SMarkus Heidelberg 	name = conf_get_autoconfig_name();
9802e3646e5SRoman Zippel 	conf_read_simple(name, S_DEF_AUTO);
9816b87b70cSAl Viro 	sym_calc_value(modules_sym);
9822e3646e5SRoman Zippel 
9832e3646e5SRoman Zippel 	for_all_symbols(i, sym) {
9842e3646e5SRoman Zippel 		sym_calc_value(sym);
985693359f7SDirk Gouders 		if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
9862e3646e5SRoman Zippel 			continue;
9872e3646e5SRoman Zippel 		if (sym->flags & SYMBOL_WRITE) {
9882e3646e5SRoman Zippel 			if (sym->flags & SYMBOL_DEF_AUTO) {
9892e3646e5SRoman Zippel 				/*
9902e3646e5SRoman Zippel 				 * symbol has old and new value,
9912e3646e5SRoman Zippel 				 * so compare them...
9922e3646e5SRoman Zippel 				 */
9932e3646e5SRoman Zippel 				switch (sym->type) {
9942e3646e5SRoman Zippel 				case S_BOOLEAN:
9952e3646e5SRoman Zippel 				case S_TRISTATE:
9962e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) ==
9972e3646e5SRoman Zippel 					    sym->def[S_DEF_AUTO].tri)
9982e3646e5SRoman Zippel 						continue;
9992e3646e5SRoman Zippel 					break;
10002e3646e5SRoman Zippel 				case S_STRING:
10012e3646e5SRoman Zippel 				case S_HEX:
10022e3646e5SRoman Zippel 				case S_INT:
10032e3646e5SRoman Zippel 					if (!strcmp(sym_get_string_value(sym),
10042e3646e5SRoman Zippel 						    sym->def[S_DEF_AUTO].val))
10052e3646e5SRoman Zippel 						continue;
10062e3646e5SRoman Zippel 					break;
10072e3646e5SRoman Zippel 				default:
10082e3646e5SRoman Zippel 					break;
10092e3646e5SRoman Zippel 				}
10102e3646e5SRoman Zippel 			} else {
10112e3646e5SRoman Zippel 				/*
10122e3646e5SRoman Zippel 				 * If there is no old value, only 'no' (unset)
10132e3646e5SRoman Zippel 				 * is allowed as new value.
10142e3646e5SRoman Zippel 				 */
10152e3646e5SRoman Zippel 				switch (sym->type) {
10162e3646e5SRoman Zippel 				case S_BOOLEAN:
10172e3646e5SRoman Zippel 				case S_TRISTATE:
10182e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) == no)
10192e3646e5SRoman Zippel 						continue;
10202e3646e5SRoman Zippel 					break;
10212e3646e5SRoman Zippel 				default:
10222e3646e5SRoman Zippel 					break;
10232e3646e5SRoman Zippel 				}
10242e3646e5SRoman Zippel 			}
10252e3646e5SRoman Zippel 		} else if (!(sym->flags & SYMBOL_DEF_AUTO))
10262e3646e5SRoman Zippel 			/* There is neither an old nor a new value. */
10272e3646e5SRoman Zippel 			continue;
10282e3646e5SRoman Zippel 		/* else
10292e3646e5SRoman Zippel 		 *	There is an old value, but no new value ('no' (unset)
10302e3646e5SRoman Zippel 		 *	isn't saved in auto.conf, so the old value is always
10312e3646e5SRoman Zippel 		 *	different from 'no').
10322e3646e5SRoman Zippel 		 */
10332e3646e5SRoman Zippel 
10341508fec8SMasahiro Yamada 		res = conf_touch_dep(sym->name);
10351508fec8SMasahiro Yamada 		if (res)
10362e3646e5SRoman Zippel 			return res;
10372e3646e5SRoman Zippel 	}
10382e3646e5SRoman Zippel 
10391508fec8SMasahiro Yamada 	return 0;
10401508fec8SMasahiro Yamada }
10411508fec8SMasahiro Yamada 
104200c864f8SMasahiro Yamada int conf_write_autoconf(int overwrite)
1043c955ccafSRoman Zippel {
1044c955ccafSRoman Zippel 	struct symbol *sym;
104512122f62SMarkus Heidelberg 	const char *name;
104600c864f8SMasahiro Yamada 	const char *autoconf_name = conf_get_autoconfig_name();
10478b41fc44SMasahiro Yamada 	FILE *out, *out_h;
104849192f26SSam Ravnborg 	int i;
1049c955ccafSRoman Zippel 
105000c864f8SMasahiro Yamada 	if (!overwrite && is_present(autoconf_name))
105100c864f8SMasahiro Yamada 		return 0;
105200c864f8SMasahiro Yamada 
1053a2ff4040SMasahiro Yamada 	conf_write_dep("include/config/auto.conf.cmd");
1054c955ccafSRoman Zippel 
10550849d212SMasahiro Yamada 	if (conf_touch_deps())
10562e3646e5SRoman Zippel 		return 1;
10572e3646e5SRoman Zippel 
1058c955ccafSRoman Zippel 	out = fopen(".tmpconfig", "w");
1059c955ccafSRoman Zippel 	if (!out)
1060c955ccafSRoman Zippel 		return 1;
1061c955ccafSRoman Zippel 
1062c955ccafSRoman Zippel 	out_h = fopen(".tmpconfig.h", "w");
1063c955ccafSRoman Zippel 	if (!out_h) {
1064c955ccafSRoman Zippel 		fclose(out);
1065c955ccafSRoman Zippel 		return 1;
1066c955ccafSRoman Zippel 	}
1067c955ccafSRoman Zippel 
1068e54e692bSArnaud Lacombe 	conf_write_heading(out, &kconfig_printer_cb, NULL);
1069e54e692bSArnaud Lacombe 	conf_write_heading(out_h, &header_printer_cb, NULL);
1070c955ccafSRoman Zippel 
1071c955ccafSRoman Zippel 	for_all_symbols(i, sym) {
1072953742c8SArnaud Lacombe 		sym_calc_value(sym);
1073a9596135SPaul Gortmaker 		if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
1074953742c8SArnaud Lacombe 			continue;
1075953742c8SArnaud Lacombe 
10768b41fc44SMasahiro Yamada 		/* write symbols to auto.conf and autoconf.h */
1077e54e692bSArnaud Lacombe 		conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
1078e54e692bSArnaud Lacombe 		conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
1079c955ccafSRoman Zippel 	}
1080c955ccafSRoman Zippel 	fclose(out);
1081c955ccafSRoman Zippel 	fclose(out_h);
1082c955ccafSRoman Zippel 
1083c955ccafSRoman Zippel 	name = getenv("KCONFIG_AUTOHEADER");
1084c955ccafSRoman Zippel 	if (!name)
1085264a2683SSam Ravnborg 		name = "include/generated/autoconf.h";
108679123b13SMasahiro Yamada 	if (make_parent_dir(name))
108779123b13SMasahiro Yamada 		return 1;
1088c955ccafSRoman Zippel 	if (rename(".tmpconfig.h", name))
1089c955ccafSRoman Zippel 		return 1;
109079123b13SMasahiro Yamada 
109100c864f8SMasahiro Yamada 	if (make_parent_dir(autoconf_name))
109279123b13SMasahiro Yamada 		return 1;
1093c955ccafSRoman Zippel 	/*
1094c955ccafSRoman Zippel 	 * This must be the last step, kbuild has a dependency on auto.conf
1095c955ccafSRoman Zippel 	 * and this marks the successful completion of the previous steps.
1096c955ccafSRoman Zippel 	 */
109700c864f8SMasahiro Yamada 	if (rename(".tmpconfig", autoconf_name))
1098c955ccafSRoman Zippel 		return 1;
1099c955ccafSRoman Zippel 
1100c955ccafSRoman Zippel 	return 0;
1101c955ccafSRoman Zippel }
1102b3214293SKarsten Wiese 
1103bfc10001SKarsten Wiese static int sym_change_count;
11043b354c55SKarsten Wiese static void (*conf_changed_callback)(void);
1105bfc10001SKarsten Wiese 
1106bfc10001SKarsten Wiese void sym_set_change_count(int count)
1107bfc10001SKarsten Wiese {
11083b354c55SKarsten Wiese 	int _sym_change_count = sym_change_count;
1109bfc10001SKarsten Wiese 	sym_change_count = count;
11103b354c55SKarsten Wiese 	if (conf_changed_callback &&
11113b354c55SKarsten Wiese 	    (bool)_sym_change_count != (bool)count)
11123b354c55SKarsten Wiese 		conf_changed_callback();
1113bfc10001SKarsten Wiese }
1114bfc10001SKarsten Wiese 
1115bfc10001SKarsten Wiese void sym_add_change_count(int count)
1116bfc10001SKarsten Wiese {
11173b354c55SKarsten Wiese 	sym_set_change_count(count + sym_change_count);
1118bfc10001SKarsten Wiese }
1119bfc10001SKarsten Wiese 
1120b3214293SKarsten Wiese bool conf_get_changed(void)
1121b3214293SKarsten Wiese {
1122b3214293SKarsten Wiese 	return sym_change_count;
1123b3214293SKarsten Wiese }
11243b354c55SKarsten Wiese 
11253b354c55SKarsten Wiese void conf_set_changed_callback(void (*fn)(void))
11263b354c55SKarsten Wiese {
11273b354c55SKarsten Wiese 	conf_changed_callback = fn;
11283b354c55SKarsten Wiese }
1129dc7862e5SRoman Zippel 
11303b9a19e0SYann E. MORIN static bool randomize_choice_values(struct symbol *csym)
1131a64b44eaSSam Ravnborg {
1132a64b44eaSSam Ravnborg 	struct property *prop;
1133a64b44eaSSam Ravnborg 	struct symbol *sym;
1134a64b44eaSSam Ravnborg 	struct expr *e;
1135a64b44eaSSam Ravnborg 	int cnt, def;
1136a64b44eaSSam Ravnborg 
1137a64b44eaSSam Ravnborg 	/*
1138579fb8e7SArnaud Lacombe 	 * If choice is mod then we may have more items selected
1139a64b44eaSSam Ravnborg 	 * and if no then no-one.
1140a64b44eaSSam Ravnborg 	 * In both cases stop.
1141a64b44eaSSam Ravnborg 	 */
1142a64b44eaSSam Ravnborg 	if (csym->curr.tri != yes)
11433b9a19e0SYann E. MORIN 		return false;
1144a64b44eaSSam Ravnborg 
1145a64b44eaSSam Ravnborg 	prop = sym_get_choice_prop(csym);
1146a64b44eaSSam Ravnborg 
1147a64b44eaSSam Ravnborg 	/* count entries in choice block */
1148a64b44eaSSam Ravnborg 	cnt = 0;
1149a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym)
1150a64b44eaSSam Ravnborg 		cnt++;
1151a64b44eaSSam Ravnborg 
1152a64b44eaSSam Ravnborg 	/*
1153a64b44eaSSam Ravnborg 	 * find a random value and set it to yes,
1154a64b44eaSSam Ravnborg 	 * set the rest to no so we have only one set
1155a64b44eaSSam Ravnborg 	 */
1156a64b44eaSSam Ravnborg 	def = (rand() % cnt);
1157a64b44eaSSam Ravnborg 
1158a64b44eaSSam Ravnborg 	cnt = 0;
1159a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym) {
1160a64b44eaSSam Ravnborg 		if (def == cnt++) {
1161a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = yes;
1162a64b44eaSSam Ravnborg 			csym->def[S_DEF_USER].val = sym;
1163a64b44eaSSam Ravnborg 		}
1164a64b44eaSSam Ravnborg 		else {
1165a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = no;
1166a64b44eaSSam Ravnborg 		}
1167e6abf12aSYann E. MORIN 		sym->flags |= SYMBOL_DEF_USER;
1168e6abf12aSYann E. MORIN 		/* clear VALID to get value calculated */
1169e6abf12aSYann E. MORIN 		sym->flags &= ~SYMBOL_VALID;
1170a64b44eaSSam Ravnborg 	}
1171a64b44eaSSam Ravnborg 	csym->flags |= SYMBOL_DEF_USER;
1172a64b44eaSSam Ravnborg 	/* clear VALID to get value calculated */
1173a64b44eaSSam Ravnborg 	csym->flags &= ~(SYMBOL_VALID);
11743b9a19e0SYann E. MORIN 
11753b9a19e0SYann E. MORIN 	return true;
1176a64b44eaSSam Ravnborg }
1177a64b44eaSSam Ravnborg 
1178fbe98bb9SArve Hjønnevåg void set_all_choice_values(struct symbol *csym)
1179a64b44eaSSam Ravnborg {
1180a64b44eaSSam Ravnborg 	struct property *prop;
1181a64b44eaSSam Ravnborg 	struct symbol *sym;
1182a64b44eaSSam Ravnborg 	struct expr *e;
1183a64b44eaSSam Ravnborg 
1184a64b44eaSSam Ravnborg 	prop = sym_get_choice_prop(csym);
1185a64b44eaSSam Ravnborg 
1186a64b44eaSSam Ravnborg 	/*
1187a64b44eaSSam Ravnborg 	 * Set all non-assinged choice values to no
1188a64b44eaSSam Ravnborg 	 */
1189a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym) {
1190a64b44eaSSam Ravnborg 		if (!sym_has_value(sym))
1191a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = no;
1192a64b44eaSSam Ravnborg 	}
1193a64b44eaSSam Ravnborg 	csym->flags |= SYMBOL_DEF_USER;
1194a64b44eaSSam Ravnborg 	/* clear VALID to get value calculated */
1195fbe98bb9SArve Hjønnevåg 	csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1196a64b44eaSSam Ravnborg }
1197dc7862e5SRoman Zippel 
11983b9a19e0SYann E. MORIN bool conf_set_all_new_symbols(enum conf_def_mode mode)
1199dc7862e5SRoman Zippel {
1200dc7862e5SRoman Zippel 	struct symbol *sym, *csym;
1201b92d804aSMasahiro Yamada 	int i, cnt, pby, pty, ptm;	/* pby: probability of bool     = y
1202e43956e6SYann E. MORIN 					 * pty: probability of tristate = y
1203e43956e6SYann E. MORIN 					 * ptm: probability of tristate = m
1204e43956e6SYann E. MORIN 					 */
1205e43956e6SYann E. MORIN 
1206e43956e6SYann E. MORIN 	pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1207e43956e6SYann E. MORIN 				   * below, otherwise gcc whines about
1208e43956e6SYann E. MORIN 				   * -Wmaybe-uninitialized */
1209e43956e6SYann E. MORIN 	if (mode == def_random) {
1210e43956e6SYann E. MORIN 		int n, p[3];
1211e43956e6SYann E. MORIN 		char *env = getenv("KCONFIG_PROBABILITY");
1212e43956e6SYann E. MORIN 		n = 0;
1213e43956e6SYann E. MORIN 		while( env && *env ) {
1214e43956e6SYann E. MORIN 			char *endp;
1215e43956e6SYann E. MORIN 			int tmp = strtol( env, &endp, 10 );
1216e43956e6SYann E. MORIN 			if( tmp >= 0 && tmp <= 100 ) {
1217e43956e6SYann E. MORIN 				p[n++] = tmp;
1218e43956e6SYann E. MORIN 			} else {
1219e43956e6SYann E. MORIN 				errno = ERANGE;
1220e43956e6SYann E. MORIN 				perror( "KCONFIG_PROBABILITY" );
1221e43956e6SYann E. MORIN 				exit( 1 );
1222e43956e6SYann E. MORIN 			}
1223e43956e6SYann E. MORIN 			env = (*endp == ':') ? endp+1 : endp;
1224e43956e6SYann E. MORIN 			if( n >=3 ) {
1225e43956e6SYann E. MORIN 				break;
1226e43956e6SYann E. MORIN 			}
1227e43956e6SYann E. MORIN 		}
1228e43956e6SYann E. MORIN 		switch( n ) {
1229e43956e6SYann E. MORIN 		case 1:
1230e43956e6SYann E. MORIN 			pby = p[0]; ptm = pby/2; pty = pby-ptm;
1231e43956e6SYann E. MORIN 			break;
1232e43956e6SYann E. MORIN 		case 2:
1233e43956e6SYann E. MORIN 			pty = p[0]; ptm = p[1]; pby = pty + ptm;
1234e43956e6SYann E. MORIN 			break;
1235e43956e6SYann E. MORIN 		case 3:
1236e43956e6SYann E. MORIN 			pby = p[0]; pty = p[1]; ptm = p[2];
1237e43956e6SYann E. MORIN 			break;
1238e43956e6SYann E. MORIN 		}
1239e43956e6SYann E. MORIN 
1240e43956e6SYann E. MORIN 		if( pty+ptm > 100 ) {
1241e43956e6SYann E. MORIN 			errno = ERANGE;
1242e43956e6SYann E. MORIN 			perror( "KCONFIG_PROBABILITY" );
1243e43956e6SYann E. MORIN 			exit( 1 );
1244e43956e6SYann E. MORIN 		}
1245e43956e6SYann E. MORIN 	}
12463b9a19e0SYann E. MORIN 	bool has_changed = false;
1247dc7862e5SRoman Zippel 
1248dc7862e5SRoman Zippel 	for_all_symbols(i, sym) {
1249cfa98f2eSYann E. MORIN 		if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
1250dc7862e5SRoman Zippel 			continue;
1251dc7862e5SRoman Zippel 		switch (sym_get_type(sym)) {
1252dc7862e5SRoman Zippel 		case S_BOOLEAN:
1253dc7862e5SRoman Zippel 		case S_TRISTATE:
12543b9a19e0SYann E. MORIN 			has_changed = true;
1255dc7862e5SRoman Zippel 			switch (mode) {
1256dc7862e5SRoman Zippel 			case def_yes:
1257dc7862e5SRoman Zippel 				sym->def[S_DEF_USER].tri = yes;
1258dc7862e5SRoman Zippel 				break;
1259dc7862e5SRoman Zippel 			case def_mod:
1260dc7862e5SRoman Zippel 				sym->def[S_DEF_USER].tri = mod;
1261dc7862e5SRoman Zippel 				break;
1262dc7862e5SRoman Zippel 			case def_no:
12635d2acfc7SJosh Triplett 				if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
12645d2acfc7SJosh Triplett 					sym->def[S_DEF_USER].tri = yes;
12655d2acfc7SJosh Triplett 				else
1266dc7862e5SRoman Zippel 					sym->def[S_DEF_USER].tri = no;
1267dc7862e5SRoman Zippel 				break;
1268dc7862e5SRoman Zippel 			case def_random:
1269e43956e6SYann E. MORIN 				sym->def[S_DEF_USER].tri = no;
1270e43956e6SYann E. MORIN 				cnt = rand() % 100;
1271e43956e6SYann E. MORIN 				if (sym->type == S_TRISTATE) {
1272e43956e6SYann E. MORIN 					if (cnt < pty)
1273e43956e6SYann E. MORIN 						sym->def[S_DEF_USER].tri = yes;
1274e43956e6SYann E. MORIN 					else if (cnt < (pty+ptm))
1275e43956e6SYann E. MORIN 						sym->def[S_DEF_USER].tri = mod;
1276e43956e6SYann E. MORIN 				} else if (cnt < pby)
1277e43956e6SYann E. MORIN 					sym->def[S_DEF_USER].tri = yes;
1278dc7862e5SRoman Zippel 				break;
1279dc7862e5SRoman Zippel 			default:
1280dc7862e5SRoman Zippel 				continue;
1281dc7862e5SRoman Zippel 			}
1282184832c9SSam Ravnborg 			if (!(sym_is_choice(sym) && mode == def_random))
1283dc7862e5SRoman Zippel 				sym->flags |= SYMBOL_DEF_USER;
1284dc7862e5SRoman Zippel 			break;
1285dc7862e5SRoman Zippel 		default:
1286dc7862e5SRoman Zippel 			break;
1287dc7862e5SRoman Zippel 		}
1288dc7862e5SRoman Zippel 
1289dc7862e5SRoman Zippel 	}
1290dc7862e5SRoman Zippel 
1291ce97e13eSAl Viro 	sym_clear_all_valid();
1292dc7862e5SRoman Zippel 
1293184832c9SSam Ravnborg 	/*
1294184832c9SSam Ravnborg 	 * We have different type of choice blocks.
1295579fb8e7SArnaud Lacombe 	 * If curr.tri equals to mod then we can select several
1296184832c9SSam Ravnborg 	 * choice symbols in one block.
1297184832c9SSam Ravnborg 	 * In this case we do nothing.
1298579fb8e7SArnaud Lacombe 	 * If curr.tri equals yes then only one symbol can be
1299184832c9SSam Ravnborg 	 * selected in a choice block and we set it to yes,
1300184832c9SSam Ravnborg 	 * and the rest to no.
1301184832c9SSam Ravnborg 	 */
1302fbe98bb9SArve Hjønnevåg 	if (mode != def_random) {
1303fbe98bb9SArve Hjønnevåg 		for_all_symbols(i, csym) {
1304fbe98bb9SArve Hjønnevåg 			if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1305fbe98bb9SArve Hjønnevåg 			    sym_is_choice_value(csym))
1306fbe98bb9SArve Hjønnevåg 				csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1307fbe98bb9SArve Hjønnevåg 		}
1308fbe98bb9SArve Hjønnevåg 	}
1309fbe98bb9SArve Hjønnevåg 
1310dc7862e5SRoman Zippel 	for_all_symbols(i, csym) {
1311dc7862e5SRoman Zippel 		if (sym_has_value(csym) || !sym_is_choice(csym))
1312dc7862e5SRoman Zippel 			continue;
1313dc7862e5SRoman Zippel 
1314dc7862e5SRoman Zippel 		sym_calc_value(csym);
1315a64b44eaSSam Ravnborg 		if (mode == def_random)
1316c8fb7d7eSMasahiro Yamada 			has_changed |= randomize_choice_values(csym);
13173b9a19e0SYann E. MORIN 		else {
13183b9a19e0SYann E. MORIN 			set_all_choice_values(csym);
13193b9a19e0SYann E. MORIN 			has_changed = true;
1320dc7862e5SRoman Zippel 		}
1321dc7862e5SRoman Zippel 	}
13223b9a19e0SYann E. MORIN 
13233b9a19e0SYann E. MORIN 	return has_changed;
13243b9a19e0SYann E. MORIN }
132589b90609STetsuo Handa 
132689b90609STetsuo Handa void conf_rewrite_mod_or_yes(enum conf_def_mode mode)
132789b90609STetsuo Handa {
132889b90609STetsuo Handa 	struct symbol *sym;
132989b90609STetsuo Handa 	int i;
133089b90609STetsuo Handa 	tristate old_val = (mode == def_y2m) ? yes : mod;
133189b90609STetsuo Handa 	tristate new_val = (mode == def_y2m) ? mod : yes;
133289b90609STetsuo Handa 
133389b90609STetsuo Handa 	for_all_symbols(i, sym) {
133489b90609STetsuo Handa 		if (sym_get_type(sym) == S_TRISTATE &&
1335089b7d89STetsuo Handa 		    sym->def[S_DEF_USER].tri == old_val)
133689b90609STetsuo Handa 			sym->def[S_DEF_USER].tri = new_val;
133789b90609STetsuo Handa 	}
1338089b7d89STetsuo Handa 	sym_clear_all_valid();
133989b90609STetsuo Handa }
1340