xref: /linux/scripts/kconfig/confdata.c (revision 75889e9be78febab9991f9c07fdf09d128562bf5)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
31da177e4SLinus Torvalds  * Released under the terms of the GNU GPL v2.0.
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <sys/stat.h>
71da177e4SLinus Torvalds #include <ctype.h>
894bedecaSArnaud Lacombe #include <errno.h>
92e3646e5SRoman Zippel #include <fcntl.h>
1010a4b277SArnaud Lacombe #include <stdarg.h>
111da177e4SLinus Torvalds #include <stdio.h>
121da177e4SLinus Torvalds #include <stdlib.h>
131da177e4SLinus Torvalds #include <string.h>
141da177e4SLinus Torvalds #include <time.h>
151da177e4SLinus Torvalds #include <unistd.h>
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include "lkc.h"
181da177e4SLinus Torvalds 
190608182aSMasahiro Yamada /* return true if 'path' exists, false otherwise */
200608182aSMasahiro Yamada static bool is_present(const char *path)
210608182aSMasahiro Yamada {
220608182aSMasahiro Yamada 	struct stat st;
230608182aSMasahiro Yamada 
240608182aSMasahiro Yamada 	return !stat(path, &st);
250608182aSMasahiro Yamada }
260608182aSMasahiro Yamada 
270608182aSMasahiro Yamada /* return true if 'path' exists and it is a directory, false otherwise */
280608182aSMasahiro Yamada static bool is_dir(const char *path)
290608182aSMasahiro Yamada {
300608182aSMasahiro Yamada 	struct stat st;
310608182aSMasahiro Yamada 
320608182aSMasahiro Yamada 	if (stat(path, &st))
330608182aSMasahiro Yamada 		return 0;
340608182aSMasahiro Yamada 
350608182aSMasahiro Yamada 	return S_ISDIR(st.st_mode);
360608182aSMasahiro Yamada }
370608182aSMasahiro Yamada 
380608182aSMasahiro Yamada /*
390608182aSMasahiro Yamada  * Create the parent directory of the given path.
400608182aSMasahiro Yamada  *
410608182aSMasahiro Yamada  * For example, if 'include/config/auto.conf' is given, create 'include/config'.
420608182aSMasahiro Yamada  */
430608182aSMasahiro Yamada static int make_parent_dir(const char *path)
440608182aSMasahiro Yamada {
450608182aSMasahiro Yamada 	char tmp[PATH_MAX + 1];
460608182aSMasahiro Yamada 	char *p;
470608182aSMasahiro Yamada 
480608182aSMasahiro Yamada 	strncpy(tmp, path, sizeof(tmp));
490608182aSMasahiro Yamada 	tmp[sizeof(tmp) - 1] = 0;
500608182aSMasahiro Yamada 
510608182aSMasahiro Yamada 	/* Remove the base name. Just return if nothing is left */
520608182aSMasahiro Yamada 	p = strrchr(tmp, '/');
530608182aSMasahiro Yamada 	if (!p)
540608182aSMasahiro Yamada 		return 0;
550608182aSMasahiro Yamada 	*(p + 1) = 0;
560608182aSMasahiro Yamada 
570608182aSMasahiro Yamada 	/* Just in case it is an absolute path */
580608182aSMasahiro Yamada 	p = tmp;
590608182aSMasahiro Yamada 	while (*p == '/')
600608182aSMasahiro Yamada 		p++;
610608182aSMasahiro Yamada 
620608182aSMasahiro Yamada 	while ((p = strchr(p, '/'))) {
630608182aSMasahiro Yamada 		*p = 0;
640608182aSMasahiro Yamada 
650608182aSMasahiro Yamada 		/* skip if the directory exists */
660608182aSMasahiro Yamada 		if (!is_dir(tmp) && mkdir(tmp, 0755))
670608182aSMasahiro Yamada 			return -1;
680608182aSMasahiro Yamada 
690608182aSMasahiro Yamada 		*p = '/';
700608182aSMasahiro Yamada 		while (*p == '/')
710608182aSMasahiro Yamada 			p++;
720608182aSMasahiro Yamada 	}
730608182aSMasahiro Yamada 
740608182aSMasahiro Yamada 	return 0;
750608182aSMasahiro Yamada }
760608182aSMasahiro Yamada 
77ad8d40cdSMichal Marek struct conf_printer {
78ad8d40cdSMichal Marek 	void (*print_symbol)(FILE *, struct symbol *, const char *, void *);
79ad8d40cdSMichal Marek 	void (*print_comment)(FILE *, const char *, void *);
80ad8d40cdSMichal Marek };
81ad8d40cdSMichal Marek 
82c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
83c1a0f5e3SRoman Zippel 	__attribute__ ((format (printf, 1, 2)));
84c1a0f5e3SRoman Zippel 
8542368c37SMichal Marek static void conf_message(const char *fmt, ...)
8642368c37SMichal Marek 	__attribute__ ((format (printf, 1, 2)));
8742368c37SMichal Marek 
88c1a0f5e3SRoman Zippel static const char *conf_filename;
8984dd95d4SMasahiro Yamada static int conf_lineno, conf_warnings;
90c1a0f5e3SRoman Zippel 
91104daea1SMasahiro Yamada const char conf_defname[] = "arch/$(ARCH)/defconfig";
921da177e4SLinus Torvalds 
93c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
94c1a0f5e3SRoman Zippel {
95c1a0f5e3SRoman Zippel 	va_list ap;
96c1a0f5e3SRoman Zippel 	va_start(ap, fmt);
97c1a0f5e3SRoman Zippel 	fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
98c1a0f5e3SRoman Zippel 	vfprintf(stderr, fmt, ap);
99c1a0f5e3SRoman Zippel 	fprintf(stderr, "\n");
100c1a0f5e3SRoman Zippel 	va_end(ap);
101c1a0f5e3SRoman Zippel 	conf_warnings++;
102c1a0f5e3SRoman Zippel }
103c1a0f5e3SRoman Zippel 
1045accd7f3SMasahiro Yamada static void conf_default_message_callback(const char *s)
10542368c37SMichal Marek {
10642368c37SMichal Marek 	printf("#\n# ");
1075accd7f3SMasahiro Yamada 	printf("%s", s);
10842368c37SMichal Marek 	printf("\n#\n");
10942368c37SMichal Marek }
11042368c37SMichal Marek 
1115accd7f3SMasahiro Yamada static void (*conf_message_callback)(const char *s) =
11242368c37SMichal Marek 	conf_default_message_callback;
1135accd7f3SMasahiro Yamada void conf_set_message_callback(void (*fn)(const char *s))
11442368c37SMichal Marek {
11542368c37SMichal Marek 	conf_message_callback = fn;
11642368c37SMichal Marek }
11742368c37SMichal Marek 
11842368c37SMichal Marek static void conf_message(const char *fmt, ...)
11942368c37SMichal Marek {
12042368c37SMichal Marek 	va_list ap;
1215accd7f3SMasahiro Yamada 	char buf[4096];
1225accd7f3SMasahiro Yamada 
1235accd7f3SMasahiro Yamada 	if (!conf_message_callback)
1245accd7f3SMasahiro Yamada 		return;
12542368c37SMichal Marek 
12642368c37SMichal Marek 	va_start(ap, fmt);
1275accd7f3SMasahiro Yamada 
1285accd7f3SMasahiro Yamada 	vsnprintf(buf, sizeof(buf), fmt, ap);
1295accd7f3SMasahiro Yamada 	conf_message_callback(buf);
130b6a2ab2cSColin Ian King 	va_end(ap);
13142368c37SMichal Marek }
13242368c37SMichal Marek 
13314cdd3c4SRoman Zippel const char *conf_get_configname(void)
13414cdd3c4SRoman Zippel {
13514cdd3c4SRoman Zippel 	char *name = getenv("KCONFIG_CONFIG");
13614cdd3c4SRoman Zippel 
13714cdd3c4SRoman Zippel 	return name ? name : ".config";
13814cdd3c4SRoman Zippel }
13914cdd3c4SRoman Zippel 
14012122f62SMarkus Heidelberg const char *conf_get_autoconfig_name(void)
14112122f62SMarkus Heidelberg {
14212122f62SMarkus Heidelberg 	char *name = getenv("KCONFIG_AUTOCONFIG");
14312122f62SMarkus Heidelberg 
14412122f62SMarkus Heidelberg 	return name ? name : "include/config/auto.conf";
14512122f62SMarkus Heidelberg }
14612122f62SMarkus Heidelberg 
1471da177e4SLinus Torvalds char *conf_get_default_confname(void)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	static char fullname[PATH_MAX+1];
1501da177e4SLinus Torvalds 	char *env, *name;
1511da177e4SLinus Torvalds 
152104daea1SMasahiro Yamada 	name = expand_string(conf_defname);
1531da177e4SLinus Torvalds 	env = getenv(SRCTREE);
1541da177e4SLinus Torvalds 	if (env) {
1551da177e4SLinus Torvalds 		sprintf(fullname, "%s/%s", env, name);
1560608182aSMasahiro Yamada 		if (is_present(fullname))
1571da177e4SLinus Torvalds 			return fullname;
1581da177e4SLinus Torvalds 	}
1591da177e4SLinus Torvalds 	return name;
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds 
1629c900a9cSSam Ravnborg static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
1639c900a9cSSam Ravnborg {
1649c900a9cSSam Ravnborg 	char *p2;
1659c900a9cSSam Ravnborg 
1669c900a9cSSam Ravnborg 	switch (sym->type) {
1679c900a9cSSam Ravnborg 	case S_TRISTATE:
1689c900a9cSSam Ravnborg 		if (p[0] == 'm') {
1699c900a9cSSam Ravnborg 			sym->def[def].tri = mod;
1709c900a9cSSam Ravnborg 			sym->flags |= def_flags;
1719c900a9cSSam Ravnborg 			break;
1729c900a9cSSam Ravnborg 		}
173d8fc3200SArnaud Lacombe 		/* fall through */
1749c900a9cSSam Ravnborg 	case S_BOOLEAN:
1759c900a9cSSam Ravnborg 		if (p[0] == 'y') {
1769c900a9cSSam Ravnborg 			sym->def[def].tri = yes;
1779c900a9cSSam Ravnborg 			sym->flags |= def_flags;
1789c900a9cSSam Ravnborg 			break;
1799c900a9cSSam Ravnborg 		}
1809c900a9cSSam Ravnborg 		if (p[0] == 'n') {
1819c900a9cSSam Ravnborg 			sym->def[def].tri = no;
1829c900a9cSSam Ravnborg 			sym->flags |= def_flags;
1839c900a9cSSam Ravnborg 			break;
1849c900a9cSSam Ravnborg 		}
18504b19b77SYann E. MORIN 		if (def != S_DEF_AUTO)
18604b19b77SYann E. MORIN 			conf_warning("symbol value '%s' invalid for %s",
18704b19b77SYann E. MORIN 				     p, sym->name);
18875f1468bSArnaud Lacombe 		return 1;
1899c900a9cSSam Ravnborg 	case S_OTHER:
1909c900a9cSSam Ravnborg 		if (*p != '"') {
1919c900a9cSSam Ravnborg 			for (p2 = p; *p2 && !isspace(*p2); p2++)
1929c900a9cSSam Ravnborg 				;
1939c900a9cSSam Ravnborg 			sym->type = S_STRING;
1949c900a9cSSam Ravnborg 			goto done;
1959c900a9cSSam Ravnborg 		}
196d8fc3200SArnaud Lacombe 		/* fall through */
1979c900a9cSSam Ravnborg 	case S_STRING:
1989c900a9cSSam Ravnborg 		if (*p++ != '"')
1999c900a9cSSam Ravnborg 			break;
2009c900a9cSSam Ravnborg 		for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
2019c900a9cSSam Ravnborg 			if (*p2 == '"') {
2029c900a9cSSam Ravnborg 				*p2 = 0;
2039c900a9cSSam Ravnborg 				break;
2049c900a9cSSam Ravnborg 			}
2059c900a9cSSam Ravnborg 			memmove(p2, p2 + 1, strlen(p2));
2069c900a9cSSam Ravnborg 		}
2079c900a9cSSam Ravnborg 		if (!p2) {
20804b19b77SYann E. MORIN 			if (def != S_DEF_AUTO)
2099c900a9cSSam Ravnborg 				conf_warning("invalid string found");
2109c900a9cSSam Ravnborg 			return 1;
2119c900a9cSSam Ravnborg 		}
212d8fc3200SArnaud Lacombe 		/* fall through */
2139c900a9cSSam Ravnborg 	case S_INT:
2149c900a9cSSam Ravnborg 	case S_HEX:
2159c900a9cSSam Ravnborg 	done:
2169c900a9cSSam Ravnborg 		if (sym_string_valid(sym, p)) {
217cd81fc82SMasahiro Yamada 			sym->def[def].val = xstrdup(p);
2189c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2199c900a9cSSam Ravnborg 		} else {
22004b19b77SYann E. MORIN 			if (def != S_DEF_AUTO)
22104b19b77SYann E. MORIN 				conf_warning("symbol value '%s' invalid for %s",
22204b19b77SYann E. MORIN 					     p, sym->name);
2239c900a9cSSam Ravnborg 			return 1;
2249c900a9cSSam Ravnborg 		}
2259c900a9cSSam Ravnborg 		break;
2269c900a9cSSam Ravnborg 	default:
2279c900a9cSSam Ravnborg 		;
2289c900a9cSSam Ravnborg 	}
2299c900a9cSSam Ravnborg 	return 0;
2309c900a9cSSam Ravnborg }
2319c900a9cSSam Ravnborg 
2321a7a8c6fSCody Schafer #define LINE_GROWTH 16
2331a7a8c6fSCody Schafer static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
2341a7a8c6fSCody Schafer {
2351a7a8c6fSCody Schafer 	char *nline;
2361a7a8c6fSCody Schafer 	size_t new_size = slen + 1;
2371a7a8c6fSCody Schafer 	if (new_size > *n) {
2381a7a8c6fSCody Schafer 		new_size += LINE_GROWTH - 1;
2391a7a8c6fSCody Schafer 		new_size *= 2;
240d717f24dSMasahiro Yamada 		nline = xrealloc(*lineptr, new_size);
2411a7a8c6fSCody Schafer 		if (!nline)
2421a7a8c6fSCody Schafer 			return -1;
2431a7a8c6fSCody Schafer 
2441a7a8c6fSCody Schafer 		*lineptr = nline;
2451a7a8c6fSCody Schafer 		*n = new_size;
2461a7a8c6fSCody Schafer 	}
2471a7a8c6fSCody Schafer 
2481a7a8c6fSCody Schafer 	(*lineptr)[slen] = c;
2491a7a8c6fSCody Schafer 
2501a7a8c6fSCody Schafer 	return 0;
2511a7a8c6fSCody Schafer }
2521a7a8c6fSCody Schafer 
2531a7a8c6fSCody Schafer static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
2541a7a8c6fSCody Schafer {
2551a7a8c6fSCody Schafer 	char *line = *lineptr;
2561a7a8c6fSCody Schafer 	size_t slen = 0;
2571a7a8c6fSCody Schafer 
2581a7a8c6fSCody Schafer 	for (;;) {
2591a7a8c6fSCody Schafer 		int c = getc(stream);
2601a7a8c6fSCody Schafer 
2611a7a8c6fSCody Schafer 		switch (c) {
2621a7a8c6fSCody Schafer 		case '\n':
2631a7a8c6fSCody Schafer 			if (add_byte(c, &line, slen, n) < 0)
2641a7a8c6fSCody Schafer 				goto e_out;
2651a7a8c6fSCody Schafer 			slen++;
2661a7a8c6fSCody Schafer 			/* fall through */
2671a7a8c6fSCody Schafer 		case EOF:
2681a7a8c6fSCody Schafer 			if (add_byte('\0', &line, slen, n) < 0)
2691a7a8c6fSCody Schafer 				goto e_out;
2701a7a8c6fSCody Schafer 			*lineptr = line;
2711a7a8c6fSCody Schafer 			if (slen == 0)
2721a7a8c6fSCody Schafer 				return -1;
2731a7a8c6fSCody Schafer 			return slen;
2741a7a8c6fSCody Schafer 		default:
2751a7a8c6fSCody Schafer 			if (add_byte(c, &line, slen, n) < 0)
2761a7a8c6fSCody Schafer 				goto e_out;
2771a7a8c6fSCody Schafer 			slen++;
2781a7a8c6fSCody Schafer 		}
2791a7a8c6fSCody Schafer 	}
2801a7a8c6fSCody Schafer 
2811a7a8c6fSCody Schafer e_out:
2821a7a8c6fSCody Schafer 	line[slen-1] = '\0';
2831a7a8c6fSCody Schafer 	*lineptr = line;
2841a7a8c6fSCody Schafer 	return -1;
2851a7a8c6fSCody Schafer }
2861a7a8c6fSCody Schafer 
287669bfad9SRoman Zippel int conf_read_simple(const char *name, int def)
2881da177e4SLinus Torvalds {
2891da177e4SLinus Torvalds 	FILE *in = NULL;
2901a7a8c6fSCody Schafer 	char   *line = NULL;
2911a7a8c6fSCody Schafer 	size_t  line_asize = 0;
2921da177e4SLinus Torvalds 	char *p, *p2;
2931da177e4SLinus Torvalds 	struct symbol *sym;
294669bfad9SRoman Zippel 	int i, def_flags;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	if (name) {
2971da177e4SLinus Torvalds 		in = zconf_fopen(name);
2981da177e4SLinus Torvalds 	} else {
299face4374SRoman Zippel 		struct property *prop;
300face4374SRoman Zippel 
30114cdd3c4SRoman Zippel 		name = conf_get_configname();
302ddc97cacSRoman Zippel 		in = zconf_fopen(name);
303ddc97cacSRoman Zippel 		if (in)
304ddc97cacSRoman Zippel 			goto load;
305bfc10001SKarsten Wiese 		sym_add_change_count(1);
3066b87b70cSAl Viro 		if (!sym_defconfig_list)
307face4374SRoman Zippel 			return 1;
308face4374SRoman Zippel 
309face4374SRoman Zippel 		for_all_defaults(sym_defconfig_list, prop) {
310face4374SRoman Zippel 			if (expr_calc_value(prop->visible.expr) == no ||
311face4374SRoman Zippel 			    prop->expr->type != E_SYMBOL)
312face4374SRoman Zippel 				continue;
313104daea1SMasahiro Yamada 			sym_calc_value(prop->expr->left.sym);
314104daea1SMasahiro Yamada 			name = sym_get_string_value(prop->expr->left.sym);
3151da177e4SLinus Torvalds 			in = zconf_fopen(name);
3161da177e4SLinus Torvalds 			if (in) {
317694c49a7SSam Ravnborg 				conf_message("using defaults found in %s",
31842368c37SMichal Marek 					 name);
319ddc97cacSRoman Zippel 				goto load;
3201da177e4SLinus Torvalds 			}
3211da177e4SLinus Torvalds 		}
3221da177e4SLinus Torvalds 	}
3231da177e4SLinus Torvalds 	if (!in)
3241da177e4SLinus Torvalds 		return 1;
3251da177e4SLinus Torvalds 
326ddc97cacSRoman Zippel load:
327c1a0f5e3SRoman Zippel 	conf_filename = name;
328c1a0f5e3SRoman Zippel 	conf_lineno = 0;
329c1a0f5e3SRoman Zippel 	conf_warnings = 0;
330c1a0f5e3SRoman Zippel 
331669bfad9SRoman Zippel 	def_flags = SYMBOL_DEF << def;
3321da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
333669bfad9SRoman Zippel 		sym->flags |= SYMBOL_CHANGED;
334669bfad9SRoman Zippel 		sym->flags &= ~(def_flags|SYMBOL_VALID);
335490f1617SYann E. MORIN 		if (sym_is_choice(sym))
336490f1617SYann E. MORIN 			sym->flags |= def_flags;
3371da177e4SLinus Torvalds 		switch (sym->type) {
3381da177e4SLinus Torvalds 		case S_INT:
3391da177e4SLinus Torvalds 		case S_HEX:
3401da177e4SLinus Torvalds 		case S_STRING:
341669bfad9SRoman Zippel 			if (sym->def[def].val)
342669bfad9SRoman Zippel 				free(sym->def[def].val);
343d8fc3200SArnaud Lacombe 			/* fall through */
3441da177e4SLinus Torvalds 		default:
345669bfad9SRoman Zippel 			sym->def[def].val = NULL;
346669bfad9SRoman Zippel 			sym->def[def].tri = no;
3471da177e4SLinus Torvalds 		}
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds 
3501a7a8c6fSCody Schafer 	while (compat_getline(&line, &line_asize, in) != -1) {
351c1a0f5e3SRoman Zippel 		conf_lineno++;
3521da177e4SLinus Torvalds 		sym = NULL;
3538baefd30SArnaud Lacombe 		if (line[0] == '#') {
354ffb5957bSArnaud Lacombe 			if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
3551da177e4SLinus Torvalds 				continue;
356ffb5957bSArnaud Lacombe 			p = strchr(line + 2 + strlen(CONFIG_), ' ');
3571da177e4SLinus Torvalds 			if (!p)
3581da177e4SLinus Torvalds 				continue;
3591da177e4SLinus Torvalds 			*p++ = 0;
3601da177e4SLinus Torvalds 			if (strncmp(p, "is not set", 10))
3611da177e4SLinus Torvalds 				continue;
362669bfad9SRoman Zippel 			if (def == S_DEF_USER) {
363ffb5957bSArnaud Lacombe 				sym = sym_find(line + 2 + strlen(CONFIG_));
364661b0680Szippel@linux-m68k.org 				if (!sym) {
365661b0680Szippel@linux-m68k.org 					sym_add_change_count(1);
366*75889e9bSMasahiro Yamada 					continue;
367661b0680Szippel@linux-m68k.org 				}
368669bfad9SRoman Zippel 			} else {
369ffb5957bSArnaud Lacombe 				sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
370669bfad9SRoman Zippel 				if (sym->type == S_UNKNOWN)
371669bfad9SRoman Zippel 					sym->type = S_BOOLEAN;
372669bfad9SRoman Zippel 			}
373669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
374d84876f9SJan Engelhardt 				conf_warning("override: reassigning to symbol %s", sym->name);
3751da177e4SLinus Torvalds 			}
3761da177e4SLinus Torvalds 			switch (sym->type) {
3771da177e4SLinus Torvalds 			case S_BOOLEAN:
3781da177e4SLinus Torvalds 			case S_TRISTATE:
379669bfad9SRoman Zippel 				sym->def[def].tri = no;
380669bfad9SRoman Zippel 				sym->flags |= def_flags;
3811da177e4SLinus Torvalds 				break;
3821da177e4SLinus Torvalds 			default:
3831da177e4SLinus Torvalds 				;
3841da177e4SLinus Torvalds 			}
385ffb5957bSArnaud Lacombe 		} else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
386ffb5957bSArnaud Lacombe 			p = strchr(line + strlen(CONFIG_), '=');
3871da177e4SLinus Torvalds 			if (!p)
3881da177e4SLinus Torvalds 				continue;
3891da177e4SLinus Torvalds 			*p++ = 0;
3901da177e4SLinus Torvalds 			p2 = strchr(p, '\n');
391d3660a8cSMatthew Wilcox 			if (p2) {
392d3660a8cSMatthew Wilcox 				*p2-- = 0;
393d3660a8cSMatthew Wilcox 				if (*p2 == '\r')
3941da177e4SLinus Torvalds 					*p2 = 0;
395d3660a8cSMatthew Wilcox 			}
396669bfad9SRoman Zippel 			if (def == S_DEF_USER) {
397ffb5957bSArnaud Lacombe 				sym = sym_find(line + strlen(CONFIG_));
398661b0680Szippel@linux-m68k.org 				if (!sym) {
399661b0680Szippel@linux-m68k.org 					sym_add_change_count(1);
400*75889e9bSMasahiro Yamada 					continue;
401661b0680Szippel@linux-m68k.org 				}
402669bfad9SRoman Zippel 			} else {
403ffb5957bSArnaud Lacombe 				sym = sym_lookup(line + strlen(CONFIG_), 0);
404669bfad9SRoman Zippel 				if (sym->type == S_UNKNOWN)
405669bfad9SRoman Zippel 					sym->type = S_OTHER;
406669bfad9SRoman Zippel 			}
407669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
408d84876f9SJan Engelhardt 				conf_warning("override: reassigning to symbol %s", sym->name);
4091da177e4SLinus Torvalds 			}
4109c900a9cSSam Ravnborg 			if (conf_set_sym_val(sym, def, def_flags, p))
411c1a0f5e3SRoman Zippel 				continue;
4128baefd30SArnaud Lacombe 		} else {
4138baefd30SArnaud Lacombe 			if (line[0] != '\r' && line[0] != '\n')
414a4663911SPaul Bolle 				conf_warning("unexpected data: %.*s",
415a4663911SPaul Bolle 					     (int)strcspn(line, "\r\n"), line);
416a4663911SPaul Bolle 
4171da177e4SLinus Torvalds 			continue;
4181da177e4SLinus Torvalds 		}
419*75889e9bSMasahiro Yamada 
4201da177e4SLinus Torvalds 		if (sym && sym_is_choice_value(sym)) {
4211da177e4SLinus Torvalds 			struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
422669bfad9SRoman Zippel 			switch (sym->def[def].tri) {
4231da177e4SLinus Torvalds 			case no:
4241da177e4SLinus Torvalds 				break;
4251da177e4SLinus Torvalds 			case mod:
426669bfad9SRoman Zippel 				if (cs->def[def].tri == yes) {
427c1a0f5e3SRoman Zippel 					conf_warning("%s creates inconsistent choice state", sym->name);
428490f1617SYann E. MORIN 					cs->flags &= ~def_flags;
429c1a0f5e3SRoman Zippel 				}
4301da177e4SLinus Torvalds 				break;
4311da177e4SLinus Torvalds 			case yes:
432d84876f9SJan Engelhardt 				if (cs->def[def].tri != no)
433d84876f9SJan Engelhardt 					conf_warning("override: %s changes choice state", sym->name);
434669bfad9SRoman Zippel 				cs->def[def].val = sym;
4351da177e4SLinus Torvalds 				break;
4361da177e4SLinus Torvalds 			}
437d6ee3576SSam Ravnborg 			cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
4381da177e4SLinus Torvalds 		}
4391da177e4SLinus Torvalds 	}
4401a7a8c6fSCody Schafer 	free(line);
4411da177e4SLinus Torvalds 	fclose(in);
44290389160SRoman Zippel 	return 0;
44390389160SRoman Zippel }
44490389160SRoman Zippel 
44590389160SRoman Zippel int conf_read(const char *name)
44690389160SRoman Zippel {
4475d09598dSArnaud Lacombe 	struct symbol *sym;
44884dd95d4SMasahiro Yamada 	int conf_unsaved = 0;
4495d09598dSArnaud Lacombe 	int i;
45090389160SRoman Zippel 
451bfc10001SKarsten Wiese 	sym_set_change_count(0);
452ddc97cacSRoman Zippel 
4536b87b70cSAl Viro 	if (conf_read_simple(name, S_DEF_USER)) {
4546b87b70cSAl Viro 		sym_calc_value(modules_sym);
45590389160SRoman Zippel 		return 1;
4566b87b70cSAl Viro 	}
4576b87b70cSAl Viro 
4586b87b70cSAl Viro 	sym_calc_value(modules_sym);
45990389160SRoman Zippel 
4601da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
4611da177e4SLinus Torvalds 		sym_calc_value(sym);
462693359f7SDirk Gouders 		if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
4635d09598dSArnaud Lacombe 			continue;
464c1a0f5e3SRoman Zippel 		if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
465c1a0f5e3SRoman Zippel 			/* check that calculated value agrees with saved value */
466c1a0f5e3SRoman Zippel 			switch (sym->type) {
467c1a0f5e3SRoman Zippel 			case S_BOOLEAN:
468c1a0f5e3SRoman Zippel 			case S_TRISTATE:
4690c1822e6SRoman Zippel 				if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
470c1a0f5e3SRoman Zippel 					break;
471c1a0f5e3SRoman Zippel 				if (!sym_is_choice(sym))
4725d09598dSArnaud Lacombe 					continue;
473d8fc3200SArnaud Lacombe 				/* fall through */
474c1a0f5e3SRoman Zippel 			default:
4750c1822e6SRoman Zippel 				if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
4765d09598dSArnaud Lacombe 					continue;
477c1a0f5e3SRoman Zippel 				break;
478c1a0f5e3SRoman Zippel 			}
479c1a0f5e3SRoman Zippel 		} else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
480c1a0f5e3SRoman Zippel 			/* no previous value and not saved */
4815d09598dSArnaud Lacombe 			continue;
482c1a0f5e3SRoman Zippel 		conf_unsaved++;
483c1a0f5e3SRoman Zippel 		/* maybe print value in verbose mode... */
4841da177e4SLinus Torvalds 	}
4851da177e4SLinus Torvalds 
486d8982ba1SRoman Zippel 	for_all_symbols(i, sym) {
487d8982ba1SRoman Zippel 		if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
488d8982ba1SRoman Zippel 			/* Reset values of generates values, so they'll appear
489d8982ba1SRoman Zippel 			 * as new, if they should become visible, but that
490d8982ba1SRoman Zippel 			 * doesn't quite work if the Kconfig and the saved
491d8982ba1SRoman Zippel 			 * configuration disagree.
492d8982ba1SRoman Zippel 			 */
493d8982ba1SRoman Zippel 			if (sym->visible == no && !conf_unsaved)
494d8982ba1SRoman Zippel 				sym->flags &= ~SYMBOL_DEF_USER;
495d8982ba1SRoman Zippel 			switch (sym->type) {
496d8982ba1SRoman Zippel 			case S_STRING:
497d8982ba1SRoman Zippel 			case S_INT:
498d8982ba1SRoman Zippel 			case S_HEX:
499d8982ba1SRoman Zippel 				/* Reset a string value if it's out of range */
500d8982ba1SRoman Zippel 				if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
501d8982ba1SRoman Zippel 					break;
502d8982ba1SRoman Zippel 				sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
503d8982ba1SRoman Zippel 				conf_unsaved++;
504d8982ba1SRoman Zippel 				break;
505d8982ba1SRoman Zippel 			default:
506d8982ba1SRoman Zippel 				break;
507d8982ba1SRoman Zippel 			}
508d8982ba1SRoman Zippel 		}
509d8982ba1SRoman Zippel 	}
510d8982ba1SRoman Zippel 
511bfc10001SKarsten Wiese 	sym_add_change_count(conf_warnings || conf_unsaved);
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	return 0;
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
516e54e692bSArnaud Lacombe /*
517e54e692bSArnaud Lacombe  * Kconfig configuration printer
518e54e692bSArnaud Lacombe  *
519e54e692bSArnaud Lacombe  * This printer is used when generating the resulting configuration after
520e54e692bSArnaud Lacombe  * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
521e54e692bSArnaud Lacombe  * passing a non-NULL argument to the printer.
522e54e692bSArnaud Lacombe  *
523e54e692bSArnaud Lacombe  */
524e54e692bSArnaud Lacombe static void
525e54e692bSArnaud Lacombe kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
52649192f26SSam Ravnborg {
52749192f26SSam Ravnborg 
5280dce6310SArnaud Lacombe 	switch (sym->type) {
52949192f26SSam Ravnborg 	case S_BOOLEAN:
53049192f26SSam Ravnborg 	case S_TRISTATE:
531e54e692bSArnaud Lacombe 		if (*value == 'n') {
532e54e692bSArnaud Lacombe 			bool skip_unset = (arg != NULL);
533e54e692bSArnaud Lacombe 
534e54e692bSArnaud Lacombe 			if (!skip_unset)
535e54e692bSArnaud Lacombe 				fprintf(fp, "# %s%s is not set\n",
536ffb5957bSArnaud Lacombe 				    CONFIG_, sym->name);
537e54e692bSArnaud Lacombe 			return;
53849192f26SSam Ravnborg 		}
53949192f26SSam Ravnborg 		break;
540e54e692bSArnaud Lacombe 	default:
54149192f26SSam Ravnborg 		break;
542e54e692bSArnaud Lacombe 	}
543e54e692bSArnaud Lacombe 
544e54e692bSArnaud Lacombe 	fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
545e54e692bSArnaud Lacombe }
546e54e692bSArnaud Lacombe 
547e54e692bSArnaud Lacombe static void
548e54e692bSArnaud Lacombe kconfig_print_comment(FILE *fp, const char *value, void *arg)
549e54e692bSArnaud Lacombe {
550e54e692bSArnaud Lacombe 	const char *p = value;
551e54e692bSArnaud Lacombe 	size_t l;
552e54e692bSArnaud Lacombe 
553e54e692bSArnaud Lacombe 	for (;;) {
554e54e692bSArnaud Lacombe 		l = strcspn(p, "\n");
555e54e692bSArnaud Lacombe 		fprintf(fp, "#");
556e54e692bSArnaud Lacombe 		if (l) {
557e54e692bSArnaud Lacombe 			fprintf(fp, " ");
55870cc01e7SPeter Foley 			xfwrite(p, l, 1, fp);
559e54e692bSArnaud Lacombe 			p += l;
560e54e692bSArnaud Lacombe 		}
561e54e692bSArnaud Lacombe 		fprintf(fp, "\n");
562e54e692bSArnaud Lacombe 		if (*p++ == '\0')
56349192f26SSam Ravnborg 			break;
564e54e692bSArnaud Lacombe 	}
565e54e692bSArnaud Lacombe }
566e54e692bSArnaud Lacombe 
567e54e692bSArnaud Lacombe static struct conf_printer kconfig_printer_cb =
568e54e692bSArnaud Lacombe {
569e54e692bSArnaud Lacombe 	.print_symbol = kconfig_print_symbol,
570e54e692bSArnaud Lacombe 	.print_comment = kconfig_print_comment,
571e54e692bSArnaud Lacombe };
572e54e692bSArnaud Lacombe 
573e54e692bSArnaud Lacombe /*
574e54e692bSArnaud Lacombe  * Header printer
575e54e692bSArnaud Lacombe  *
576e54e692bSArnaud Lacombe  * This printer is used when generating the `include/generated/autoconf.h' file.
577e54e692bSArnaud Lacombe  */
578e54e692bSArnaud Lacombe static void
579e54e692bSArnaud Lacombe header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
580e54e692bSArnaud Lacombe {
581e54e692bSArnaud Lacombe 
582e54e692bSArnaud Lacombe 	switch (sym->type) {
583e54e692bSArnaud Lacombe 	case S_BOOLEAN:
584eb4cf5a6SArnaud Lacombe 	case S_TRISTATE: {
585eb4cf5a6SArnaud Lacombe 		const char *suffix = "";
586eb4cf5a6SArnaud Lacombe 
587e54e692bSArnaud Lacombe 		switch (*value) {
588e54e692bSArnaud Lacombe 		case 'n':
5892a11c8eaSMichal Marek 			break;
590e54e692bSArnaud Lacombe 		case 'm':
591e54e692bSArnaud Lacombe 			suffix = "_MODULE";
592eb4cf5a6SArnaud Lacombe 			/* fall through */
593e54e692bSArnaud Lacombe 		default:
5942a11c8eaSMichal Marek 			fprintf(fp, "#define %s%s%s 1\n",
5952a11c8eaSMichal Marek 			    CONFIG_, sym->name, suffix);
596e54e692bSArnaud Lacombe 		}
597eb4cf5a6SArnaud Lacombe 		break;
598eb4cf5a6SArnaud Lacombe 	}
599eb4cf5a6SArnaud Lacombe 	case S_HEX: {
600eb4cf5a6SArnaud Lacombe 		const char *prefix = "";
601eb4cf5a6SArnaud Lacombe 
602eb4cf5a6SArnaud Lacombe 		if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
603eb4cf5a6SArnaud Lacombe 			prefix = "0x";
604eb4cf5a6SArnaud Lacombe 		fprintf(fp, "#define %s%s %s%s\n",
605eb4cf5a6SArnaud Lacombe 		    CONFIG_, sym->name, prefix, value);
606eb4cf5a6SArnaud Lacombe 		break;
607eb4cf5a6SArnaud Lacombe 	}
608eb4cf5a6SArnaud Lacombe 	case S_STRING:
609eb4cf5a6SArnaud Lacombe 	case S_INT:
610eb4cf5a6SArnaud Lacombe 		fprintf(fp, "#define %s%s %s\n",
611eb4cf5a6SArnaud Lacombe 		    CONFIG_, sym->name, value);
612e54e692bSArnaud Lacombe 		break;
613e54e692bSArnaud Lacombe 	default:
614e54e692bSArnaud Lacombe 		break;
615e54e692bSArnaud Lacombe 	}
616e54e692bSArnaud Lacombe 
617e54e692bSArnaud Lacombe }
618e54e692bSArnaud Lacombe 
619e54e692bSArnaud Lacombe static void
620e54e692bSArnaud Lacombe header_print_comment(FILE *fp, const char *value, void *arg)
621e54e692bSArnaud Lacombe {
622e54e692bSArnaud Lacombe 	const char *p = value;
623e54e692bSArnaud Lacombe 	size_t l;
624e54e692bSArnaud Lacombe 
625e54e692bSArnaud Lacombe 	fprintf(fp, "/*\n");
626e54e692bSArnaud Lacombe 	for (;;) {
627e54e692bSArnaud Lacombe 		l = strcspn(p, "\n");
628e54e692bSArnaud Lacombe 		fprintf(fp, " *");
629e54e692bSArnaud Lacombe 		if (l) {
630e54e692bSArnaud Lacombe 			fprintf(fp, " ");
63170cc01e7SPeter Foley 			xfwrite(p, l, 1, fp);
632e54e692bSArnaud Lacombe 			p += l;
633e54e692bSArnaud Lacombe 		}
634e54e692bSArnaud Lacombe 		fprintf(fp, "\n");
635e54e692bSArnaud Lacombe 		if (*p++ == '\0')
636e54e692bSArnaud Lacombe 			break;
637e54e692bSArnaud Lacombe 	}
638e54e692bSArnaud Lacombe 	fprintf(fp, " */\n");
639e54e692bSArnaud Lacombe }
640e54e692bSArnaud Lacombe 
641e54e692bSArnaud Lacombe static struct conf_printer header_printer_cb =
642e54e692bSArnaud Lacombe {
643e54e692bSArnaud Lacombe 	.print_symbol = header_print_symbol,
644e54e692bSArnaud Lacombe 	.print_comment = header_print_comment,
645e54e692bSArnaud Lacombe };
646e54e692bSArnaud Lacombe 
647e54e692bSArnaud Lacombe /*
648e54e692bSArnaud Lacombe  * Tristate printer
649e54e692bSArnaud Lacombe  *
650e54e692bSArnaud Lacombe  * This printer is used when generating the `include/config/tristate.conf' file.
651e54e692bSArnaud Lacombe  */
652e54e692bSArnaud Lacombe static void
653e54e692bSArnaud Lacombe tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
654e54e692bSArnaud Lacombe {
655e54e692bSArnaud Lacombe 
656e54e692bSArnaud Lacombe 	if (sym->type == S_TRISTATE && *value != 'n')
657e54e692bSArnaud Lacombe 		fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
658e54e692bSArnaud Lacombe }
659e54e692bSArnaud Lacombe 
660e54e692bSArnaud Lacombe static struct conf_printer tristate_printer_cb =
661e54e692bSArnaud Lacombe {
662e54e692bSArnaud Lacombe 	.print_symbol = tristate_print_symbol,
663e54e692bSArnaud Lacombe 	.print_comment = kconfig_print_comment,
664e54e692bSArnaud Lacombe };
665e54e692bSArnaud Lacombe 
666e54e692bSArnaud Lacombe static void conf_write_symbol(FILE *fp, struct symbol *sym,
667e54e692bSArnaud Lacombe 			      struct conf_printer *printer, void *printer_arg)
668e54e692bSArnaud Lacombe {
669e54e692bSArnaud Lacombe 	const char *str;
670e54e692bSArnaud Lacombe 
671e54e692bSArnaud Lacombe 	switch (sym->type) {
67249192f26SSam Ravnborg 	case S_OTHER:
67349192f26SSam Ravnborg 	case S_UNKNOWN:
67449192f26SSam Ravnborg 		break;
675e54e692bSArnaud Lacombe 	case S_STRING:
676e54e692bSArnaud Lacombe 		str = sym_get_string_value(sym);
677e54e692bSArnaud Lacombe 		str = sym_escape_string_value(str);
678e54e692bSArnaud Lacombe 		printer->print_symbol(fp, sym, str, printer_arg);
679e54e692bSArnaud Lacombe 		free((void *)str);
680e54e692bSArnaud Lacombe 		break;
681e54e692bSArnaud Lacombe 	default:
682e54e692bSArnaud Lacombe 		str = sym_get_string_value(sym);
683e54e692bSArnaud Lacombe 		printer->print_symbol(fp, sym, str, printer_arg);
68449192f26SSam Ravnborg 	}
68549192f26SSam Ravnborg }
68649192f26SSam Ravnborg 
687e54e692bSArnaud Lacombe static void
688e54e692bSArnaud Lacombe conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
689e54e692bSArnaud Lacombe {
690e54e692bSArnaud Lacombe 	char buf[256];
691e54e692bSArnaud Lacombe 
692e54e692bSArnaud Lacombe 	snprintf(buf, sizeof(buf),
693e54e692bSArnaud Lacombe 	    "\n"
694e54e692bSArnaud Lacombe 	    "Automatically generated file; DO NOT EDIT.\n"
695e54e692bSArnaud Lacombe 	    "%s\n",
696e54e692bSArnaud Lacombe 	    rootmenu.prompt->text);
697e54e692bSArnaud Lacombe 
698e54e692bSArnaud Lacombe 	printer->print_comment(fp, buf, printer_arg);
699e54e692bSArnaud Lacombe }
700e54e692bSArnaud Lacombe 
7017cf3d73bSSam Ravnborg /*
7027cf3d73bSSam Ravnborg  * Write out a minimal config.
7037cf3d73bSSam Ravnborg  * All values that has default values are skipped as this is redundant.
7047cf3d73bSSam Ravnborg  */
7057cf3d73bSSam Ravnborg int conf_write_defconfig(const char *filename)
7067cf3d73bSSam Ravnborg {
7077cf3d73bSSam Ravnborg 	struct symbol *sym;
7087cf3d73bSSam Ravnborg 	struct menu *menu;
7097cf3d73bSSam Ravnborg 	FILE *out;
7107cf3d73bSSam Ravnborg 
7117cf3d73bSSam Ravnborg 	out = fopen(filename, "w");
7127cf3d73bSSam Ravnborg 	if (!out)
7137cf3d73bSSam Ravnborg 		return 1;
7147cf3d73bSSam Ravnborg 
7157cf3d73bSSam Ravnborg 	sym_clear_all_valid();
7167cf3d73bSSam Ravnborg 
7177cf3d73bSSam Ravnborg 	/* Traverse all menus to find all relevant symbols */
7187cf3d73bSSam Ravnborg 	menu = rootmenu.list;
7197cf3d73bSSam Ravnborg 
7207cf3d73bSSam Ravnborg 	while (menu != NULL)
7217cf3d73bSSam Ravnborg 	{
7227cf3d73bSSam Ravnborg 		sym = menu->sym;
7237cf3d73bSSam Ravnborg 		if (sym == NULL) {
7247cf3d73bSSam Ravnborg 			if (!menu_is_visible(menu))
7257cf3d73bSSam Ravnborg 				goto next_menu;
7267cf3d73bSSam Ravnborg 		} else if (!sym_is_choice(sym)) {
7277cf3d73bSSam Ravnborg 			sym_calc_value(sym);
7287cf3d73bSSam Ravnborg 			if (!(sym->flags & SYMBOL_WRITE))
7297cf3d73bSSam Ravnborg 				goto next_menu;
7307cf3d73bSSam Ravnborg 			sym->flags &= ~SYMBOL_WRITE;
7317cf3d73bSSam Ravnborg 			/* If we cannot change the symbol - skip */
7327cf3d73bSSam Ravnborg 			if (!sym_is_changable(sym))
7337cf3d73bSSam Ravnborg 				goto next_menu;
7347cf3d73bSSam Ravnborg 			/* If symbol equals to default value - skip */
7357cf3d73bSSam Ravnborg 			if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
7367cf3d73bSSam Ravnborg 				goto next_menu;
7377cf3d73bSSam Ravnborg 
7387cf3d73bSSam Ravnborg 			/*
7397cf3d73bSSam Ravnborg 			 * If symbol is a choice value and equals to the
7407cf3d73bSSam Ravnborg 			 * default for a choice - skip.
74184062dd3SSam Ravnborg 			 * But only if value is bool and equal to "y" and
74284062dd3SSam Ravnborg 			 * choice is not "optional".
74384062dd3SSam Ravnborg 			 * (If choice is "optional" then all values can be "n")
7447cf3d73bSSam Ravnborg 			 */
7457cf3d73bSSam Ravnborg 			if (sym_is_choice_value(sym)) {
7467cf3d73bSSam Ravnborg 				struct symbol *cs;
7477cf3d73bSSam Ravnborg 				struct symbol *ds;
7487cf3d73bSSam Ravnborg 
7497cf3d73bSSam Ravnborg 				cs = prop_get_symbol(sym_get_choice_prop(sym));
7507cf3d73bSSam Ravnborg 				ds = sym_choice_default(cs);
75184062dd3SSam Ravnborg 				if (!sym_is_optional(cs) && sym == ds) {
752801690caSSam Ravnborg 					if ((sym->type == S_BOOLEAN) &&
7537cf3d73bSSam Ravnborg 					    sym_get_tristate_value(sym) == yes)
7547cf3d73bSSam Ravnborg 						goto next_menu;
7557cf3d73bSSam Ravnborg 				}
7567cf3d73bSSam Ravnborg 			}
757e54e692bSArnaud Lacombe 			conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
7587cf3d73bSSam Ravnborg 		}
7597cf3d73bSSam Ravnborg next_menu:
7607cf3d73bSSam Ravnborg 		if (menu->list != NULL) {
7617cf3d73bSSam Ravnborg 			menu = menu->list;
7627cf3d73bSSam Ravnborg 		}
7637cf3d73bSSam Ravnborg 		else if (menu->next != NULL) {
7647cf3d73bSSam Ravnborg 			menu = menu->next;
7657cf3d73bSSam Ravnborg 		} else {
7667cf3d73bSSam Ravnborg 			while ((menu = menu->parent)) {
7677cf3d73bSSam Ravnborg 				if (menu->next != NULL) {
7687cf3d73bSSam Ravnborg 					menu = menu->next;
7697cf3d73bSSam Ravnborg 					break;
7707cf3d73bSSam Ravnborg 				}
7717cf3d73bSSam Ravnborg 			}
7727cf3d73bSSam Ravnborg 		}
7737cf3d73bSSam Ravnborg 	}
7747cf3d73bSSam Ravnborg 	fclose(out);
7757cf3d73bSSam Ravnborg 	return 0;
7767cf3d73bSSam Ravnborg }
7777cf3d73bSSam Ravnborg 
7781da177e4SLinus Torvalds int conf_write(const char *name)
7791da177e4SLinus Torvalds {
780c955ccafSRoman Zippel 	FILE *out;
7811da177e4SLinus Torvalds 	struct symbol *sym;
7821da177e4SLinus Torvalds 	struct menu *menu;
7831da177e4SLinus Torvalds 	const char *basename;
7841da177e4SLinus Torvalds 	const char *str;
7852ae89c7aSNathan Chancellor 	char dirname[PATH_MAX+1], tmpname[PATH_MAX+22], newname[PATH_MAX+8];
7861da177e4SLinus Torvalds 	char *env;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	dirname[0] = 0;
7891da177e4SLinus Torvalds 	if (name && name[0]) {
7901da177e4SLinus Torvalds 		char *slash;
7911da177e4SLinus Torvalds 
7920608182aSMasahiro Yamada 		if (is_dir(name)) {
7931da177e4SLinus Torvalds 			strcpy(dirname, name);
7941da177e4SLinus Torvalds 			strcat(dirname, "/");
79514cdd3c4SRoman Zippel 			basename = conf_get_configname();
7961da177e4SLinus Torvalds 		} else if ((slash = strrchr(name, '/'))) {
7971da177e4SLinus Torvalds 			int size = slash - name + 1;
7981da177e4SLinus Torvalds 			memcpy(dirname, name, size);
7991da177e4SLinus Torvalds 			dirname[size] = 0;
8001da177e4SLinus Torvalds 			if (slash[1])
8011da177e4SLinus Torvalds 				basename = slash + 1;
8021da177e4SLinus Torvalds 			else
80314cdd3c4SRoman Zippel 				basename = conf_get_configname();
8041da177e4SLinus Torvalds 		} else
8051da177e4SLinus Torvalds 			basename = name;
8061da177e4SLinus Torvalds 	} else
80714cdd3c4SRoman Zippel 		basename = conf_get_configname();
8081da177e4SLinus Torvalds 
80914cdd3c4SRoman Zippel 	sprintf(newname, "%s%s", dirname, basename);
81014cdd3c4SRoman Zippel 	env = getenv("KCONFIG_OVERWRITECONFIG");
81114cdd3c4SRoman Zippel 	if (!env || !*env) {
81214cdd3c4SRoman Zippel 		sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
81314cdd3c4SRoman Zippel 		out = fopen(tmpname, "w");
81414cdd3c4SRoman Zippel 	} else {
81514cdd3c4SRoman Zippel 		*tmpname = 0;
8161da177e4SLinus Torvalds 		out = fopen(newname, "w");
81714cdd3c4SRoman Zippel 	}
8181da177e4SLinus Torvalds 	if (!out)
8191da177e4SLinus Torvalds 		return 1;
82014cdd3c4SRoman Zippel 
821e54e692bSArnaud Lacombe 	conf_write_heading(out, &kconfig_printer_cb, NULL);
8221da177e4SLinus Torvalds 
823b3214293SKarsten Wiese 	if (!conf_get_changed())
8241da177e4SLinus Torvalds 		sym_clear_all_valid();
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	menu = rootmenu.list;
8271da177e4SLinus Torvalds 	while (menu) {
8281da177e4SLinus Torvalds 		sym = menu->sym;
8291da177e4SLinus Torvalds 		if (!sym) {
8301da177e4SLinus Torvalds 			if (!menu_is_visible(menu))
8311da177e4SLinus Torvalds 				goto next;
8321da177e4SLinus Torvalds 			str = menu_get_prompt(menu);
8331da177e4SLinus Torvalds 			fprintf(out, "\n"
8341da177e4SLinus Torvalds 				     "#\n"
8351da177e4SLinus Torvalds 				     "# %s\n"
8361da177e4SLinus Torvalds 				     "#\n", str);
8371da177e4SLinus Torvalds 		} else if (!(sym->flags & SYMBOL_CHOICE)) {
8381da177e4SLinus Torvalds 			sym_calc_value(sym);
8391da177e4SLinus Torvalds 			if (!(sym->flags & SYMBOL_WRITE))
8401da177e4SLinus Torvalds 				goto next;
8411da177e4SLinus Torvalds 			sym->flags &= ~SYMBOL_WRITE;
842e54e692bSArnaud Lacombe 
843e54e692bSArnaud Lacombe 			conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
8441da177e4SLinus Torvalds 		}
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds next:
8471da177e4SLinus Torvalds 		if (menu->list) {
8481da177e4SLinus Torvalds 			menu = menu->list;
8491da177e4SLinus Torvalds 			continue;
8501da177e4SLinus Torvalds 		}
8511da177e4SLinus Torvalds 		if (menu->next)
8521da177e4SLinus Torvalds 			menu = menu->next;
8531da177e4SLinus Torvalds 		else while ((menu = menu->parent)) {
8541da177e4SLinus Torvalds 			if (menu->next) {
8551da177e4SLinus Torvalds 				menu = menu->next;
8561da177e4SLinus Torvalds 				break;
8571da177e4SLinus Torvalds 			}
8581da177e4SLinus Torvalds 		}
8591da177e4SLinus Torvalds 	}
8601da177e4SLinus Torvalds 	fclose(out);
86114cdd3c4SRoman Zippel 
86214cdd3c4SRoman Zippel 	if (*tmpname) {
8639a3d0fe8SSam Ravnborg 		strcat(dirname, basename);
86414cdd3c4SRoman Zippel 		strcat(dirname, ".old");
86514cdd3c4SRoman Zippel 		rename(newname, dirname);
86614cdd3c4SRoman Zippel 		if (rename(tmpname, newname))
8671da177e4SLinus Torvalds 			return 1;
86814cdd3c4SRoman Zippel 	}
8691da177e4SLinus Torvalds 
870694c49a7SSam Ravnborg 	conf_message("configuration written to %s", newname);
871ddc97cacSRoman Zippel 
872bfc10001SKarsten Wiese 	sym_set_change_count(0);
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	return 0;
8751da177e4SLinus Torvalds }
876c955ccafSRoman Zippel 
877a2ff4040SMasahiro Yamada /* write a dependency file as used by kbuild to track dependencies */
878a2ff4040SMasahiro Yamada static int conf_write_dep(const char *name)
879a2ff4040SMasahiro Yamada {
880a2ff4040SMasahiro Yamada 	struct file *file;
881a2ff4040SMasahiro Yamada 	FILE *out;
882a2ff4040SMasahiro Yamada 
883a2ff4040SMasahiro Yamada 	if (!name)
884a2ff4040SMasahiro Yamada 		name = ".kconfig.d";
885a2ff4040SMasahiro Yamada 	out = fopen("..config.tmp", "w");
886a2ff4040SMasahiro Yamada 	if (!out)
887a2ff4040SMasahiro Yamada 		return 1;
888a2ff4040SMasahiro Yamada 	fprintf(out, "deps_config := \\\n");
889a2ff4040SMasahiro Yamada 	for (file = file_list; file; file = file->next) {
890a2ff4040SMasahiro Yamada 		if (file->next)
891a2ff4040SMasahiro Yamada 			fprintf(out, "\t%s \\\n", file->name);
892a2ff4040SMasahiro Yamada 		else
893a2ff4040SMasahiro Yamada 			fprintf(out, "\t%s\n", file->name);
894a2ff4040SMasahiro Yamada 	}
895a2ff4040SMasahiro Yamada 	fprintf(out, "\n%s: \\\n"
896a2ff4040SMasahiro Yamada 		     "\t$(deps_config)\n\n", conf_get_autoconfig_name());
897a2ff4040SMasahiro Yamada 
898a2ff4040SMasahiro Yamada 	env_write_dep(out, conf_get_autoconfig_name());
899a2ff4040SMasahiro Yamada 
900a2ff4040SMasahiro Yamada 	fprintf(out, "\n$(deps_config): ;\n");
901a2ff4040SMasahiro Yamada 	fclose(out);
90279123b13SMasahiro Yamada 
90379123b13SMasahiro Yamada 	if (make_parent_dir(name))
90479123b13SMasahiro Yamada 		return 1;
905a2ff4040SMasahiro Yamada 	rename("..config.tmp", name);
906a2ff4040SMasahiro Yamada 	return 0;
907a2ff4040SMasahiro Yamada }
908a2ff4040SMasahiro Yamada 
9094356f489STrevor Keith static int conf_split_config(void)
9102e3646e5SRoman Zippel {
91112122f62SMarkus Heidelberg 	const char *name;
9121408b15bSWill Newton 	char path[PATH_MAX+1];
9132e3646e5SRoman Zippel 	char *s, *d, c;
9142e3646e5SRoman Zippel 	struct symbol *sym;
9152e3646e5SRoman Zippel 	int res, i, fd;
9162e3646e5SRoman Zippel 
91712122f62SMarkus Heidelberg 	name = conf_get_autoconfig_name();
9182e3646e5SRoman Zippel 	conf_read_simple(name, S_DEF_AUTO);
9196b87b70cSAl Viro 	sym_calc_value(modules_sym);
9202e3646e5SRoman Zippel 
92179123b13SMasahiro Yamada 	if (make_parent_dir("include/config/foo.h"))
92279123b13SMasahiro Yamada 		return 1;
9232e3646e5SRoman Zippel 	if (chdir("include/config"))
9242e3646e5SRoman Zippel 		return 1;
9252e3646e5SRoman Zippel 
9262e3646e5SRoman Zippel 	res = 0;
9272e3646e5SRoman Zippel 	for_all_symbols(i, sym) {
9282e3646e5SRoman Zippel 		sym_calc_value(sym);
929693359f7SDirk Gouders 		if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
9302e3646e5SRoman Zippel 			continue;
9312e3646e5SRoman Zippel 		if (sym->flags & SYMBOL_WRITE) {
9322e3646e5SRoman Zippel 			if (sym->flags & SYMBOL_DEF_AUTO) {
9332e3646e5SRoman Zippel 				/*
9342e3646e5SRoman Zippel 				 * symbol has old and new value,
9352e3646e5SRoman Zippel 				 * so compare them...
9362e3646e5SRoman Zippel 				 */
9372e3646e5SRoman Zippel 				switch (sym->type) {
9382e3646e5SRoman Zippel 				case S_BOOLEAN:
9392e3646e5SRoman Zippel 				case S_TRISTATE:
9402e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) ==
9412e3646e5SRoman Zippel 					    sym->def[S_DEF_AUTO].tri)
9422e3646e5SRoman Zippel 						continue;
9432e3646e5SRoman Zippel 					break;
9442e3646e5SRoman Zippel 				case S_STRING:
9452e3646e5SRoman Zippel 				case S_HEX:
9462e3646e5SRoman Zippel 				case S_INT:
9472e3646e5SRoman Zippel 					if (!strcmp(sym_get_string_value(sym),
9482e3646e5SRoman Zippel 						    sym->def[S_DEF_AUTO].val))
9492e3646e5SRoman Zippel 						continue;
9502e3646e5SRoman Zippel 					break;
9512e3646e5SRoman Zippel 				default:
9522e3646e5SRoman Zippel 					break;
9532e3646e5SRoman Zippel 				}
9542e3646e5SRoman Zippel 			} else {
9552e3646e5SRoman Zippel 				/*
9562e3646e5SRoman Zippel 				 * If there is no old value, only 'no' (unset)
9572e3646e5SRoman Zippel 				 * is allowed as new value.
9582e3646e5SRoman Zippel 				 */
9592e3646e5SRoman Zippel 				switch (sym->type) {
9602e3646e5SRoman Zippel 				case S_BOOLEAN:
9612e3646e5SRoman Zippel 				case S_TRISTATE:
9622e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) == no)
9632e3646e5SRoman Zippel 						continue;
9642e3646e5SRoman Zippel 					break;
9652e3646e5SRoman Zippel 				default:
9662e3646e5SRoman Zippel 					break;
9672e3646e5SRoman Zippel 				}
9682e3646e5SRoman Zippel 			}
9692e3646e5SRoman Zippel 		} else if (!(sym->flags & SYMBOL_DEF_AUTO))
9702e3646e5SRoman Zippel 			/* There is neither an old nor a new value. */
9712e3646e5SRoman Zippel 			continue;
9722e3646e5SRoman Zippel 		/* else
9732e3646e5SRoman Zippel 		 *	There is an old value, but no new value ('no' (unset)
9742e3646e5SRoman Zippel 		 *	isn't saved in auto.conf, so the old value is always
9752e3646e5SRoman Zippel 		 *	different from 'no').
9762e3646e5SRoman Zippel 		 */
9772e3646e5SRoman Zippel 
9782e3646e5SRoman Zippel 		/* Replace all '_' and append ".h" */
9792e3646e5SRoman Zippel 		s = sym->name;
9802e3646e5SRoman Zippel 		d = path;
9812e3646e5SRoman Zippel 		while ((c = *s++)) {
9822e3646e5SRoman Zippel 			c = tolower(c);
9832e3646e5SRoman Zippel 			*d++ = (c == '_') ? '/' : c;
9842e3646e5SRoman Zippel 		}
9852e3646e5SRoman Zippel 		strcpy(d, ".h");
9862e3646e5SRoman Zippel 
9872e3646e5SRoman Zippel 		/* Assume directory path already exists. */
9882e3646e5SRoman Zippel 		fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
9892e3646e5SRoman Zippel 		if (fd == -1) {
9902e3646e5SRoman Zippel 			if (errno != ENOENT) {
9912e3646e5SRoman Zippel 				res = 1;
9922e3646e5SRoman Zippel 				break;
9932e3646e5SRoman Zippel 			}
9940608182aSMasahiro Yamada 
9950608182aSMasahiro Yamada 			if (make_parent_dir(path)) {
9962e3646e5SRoman Zippel 				res = 1;
9972e3646e5SRoman Zippel 				goto out;
9982e3646e5SRoman Zippel 			}
99979123b13SMasahiro Yamada 
10002e3646e5SRoman Zippel 			/* Try it again. */
10012e3646e5SRoman Zippel 			fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
10022e3646e5SRoman Zippel 			if (fd == -1) {
10032e3646e5SRoman Zippel 				res = 1;
10042e3646e5SRoman Zippel 				break;
10052e3646e5SRoman Zippel 			}
10062e3646e5SRoman Zippel 		}
10072e3646e5SRoman Zippel 		close(fd);
10082e3646e5SRoman Zippel 	}
10092e3646e5SRoman Zippel out:
10102e3646e5SRoman Zippel 	if (chdir("../.."))
10112e3646e5SRoman Zippel 		return 1;
10122e3646e5SRoman Zippel 
10132e3646e5SRoman Zippel 	return res;
10142e3646e5SRoman Zippel }
10152e3646e5SRoman Zippel 
101600c864f8SMasahiro Yamada int conf_write_autoconf(int overwrite)
1017c955ccafSRoman Zippel {
1018c955ccafSRoman Zippel 	struct symbol *sym;
101912122f62SMarkus Heidelberg 	const char *name;
102000c864f8SMasahiro Yamada 	const char *autoconf_name = conf_get_autoconfig_name();
1021bc081dd6SMichal Marek 	FILE *out, *tristate, *out_h;
102249192f26SSam Ravnborg 	int i;
1023c955ccafSRoman Zippel 
102400c864f8SMasahiro Yamada 	if (!overwrite && is_present(autoconf_name))
102500c864f8SMasahiro Yamada 		return 0;
102600c864f8SMasahiro Yamada 
10272e3646e5SRoman Zippel 	sym_clear_all_valid();
10282e3646e5SRoman Zippel 
1029a2ff4040SMasahiro Yamada 	conf_write_dep("include/config/auto.conf.cmd");
1030c955ccafSRoman Zippel 
10312e3646e5SRoman Zippel 	if (conf_split_config())
10322e3646e5SRoman Zippel 		return 1;
10332e3646e5SRoman Zippel 
1034c955ccafSRoman Zippel 	out = fopen(".tmpconfig", "w");
1035c955ccafSRoman Zippel 	if (!out)
1036c955ccafSRoman Zippel 		return 1;
1037c955ccafSRoman Zippel 
1038bc081dd6SMichal Marek 	tristate = fopen(".tmpconfig_tristate", "w");
1039bc081dd6SMichal Marek 	if (!tristate) {
1040bc081dd6SMichal Marek 		fclose(out);
1041bc081dd6SMichal Marek 		return 1;
1042bc081dd6SMichal Marek 	}
1043bc081dd6SMichal Marek 
1044c955ccafSRoman Zippel 	out_h = fopen(".tmpconfig.h", "w");
1045c955ccafSRoman Zippel 	if (!out_h) {
1046c955ccafSRoman Zippel 		fclose(out);
1047bc081dd6SMichal Marek 		fclose(tristate);
1048c955ccafSRoman Zippel 		return 1;
1049c955ccafSRoman Zippel 	}
1050c955ccafSRoman Zippel 
1051e54e692bSArnaud Lacombe 	conf_write_heading(out, &kconfig_printer_cb, NULL);
1052e54e692bSArnaud Lacombe 
1053e54e692bSArnaud Lacombe 	conf_write_heading(tristate, &tristate_printer_cb, NULL);
1054e54e692bSArnaud Lacombe 
1055e54e692bSArnaud Lacombe 	conf_write_heading(out_h, &header_printer_cb, NULL);
1056c955ccafSRoman Zippel 
1057c955ccafSRoman Zippel 	for_all_symbols(i, sym) {
1058953742c8SArnaud Lacombe 		sym_calc_value(sym);
1059a9596135SPaul Gortmaker 		if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
1060953742c8SArnaud Lacombe 			continue;
1061953742c8SArnaud Lacombe 
1062a9596135SPaul Gortmaker 		/* write symbol to auto.conf, tristate and header files */
1063e54e692bSArnaud Lacombe 		conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
106449192f26SSam Ravnborg 
1065e54e692bSArnaud Lacombe 		conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
1066e54e692bSArnaud Lacombe 
1067e54e692bSArnaud Lacombe 		conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
1068c955ccafSRoman Zippel 	}
1069c955ccafSRoman Zippel 	fclose(out);
1070bc081dd6SMichal Marek 	fclose(tristate);
1071c955ccafSRoman Zippel 	fclose(out_h);
1072c955ccafSRoman Zippel 
1073c955ccafSRoman Zippel 	name = getenv("KCONFIG_AUTOHEADER");
1074c955ccafSRoman Zippel 	if (!name)
1075264a2683SSam Ravnborg 		name = "include/generated/autoconf.h";
107679123b13SMasahiro Yamada 	if (make_parent_dir(name))
107779123b13SMasahiro Yamada 		return 1;
1078c955ccafSRoman Zippel 	if (rename(".tmpconfig.h", name))
1079c955ccafSRoman Zippel 		return 1;
108079123b13SMasahiro Yamada 
1081bc081dd6SMichal Marek 	name = getenv("KCONFIG_TRISTATE");
1082bc081dd6SMichal Marek 	if (!name)
1083bc081dd6SMichal Marek 		name = "include/config/tristate.conf";
108479123b13SMasahiro Yamada 	if (make_parent_dir(name))
108579123b13SMasahiro Yamada 		return 1;
1086bc081dd6SMichal Marek 	if (rename(".tmpconfig_tristate", name))
1087bc081dd6SMichal Marek 		return 1;
108879123b13SMasahiro Yamada 
108900c864f8SMasahiro Yamada 	if (make_parent_dir(autoconf_name))
109079123b13SMasahiro Yamada 		return 1;
1091c955ccafSRoman Zippel 	/*
1092c955ccafSRoman Zippel 	 * This must be the last step, kbuild has a dependency on auto.conf
1093c955ccafSRoman Zippel 	 * and this marks the successful completion of the previous steps.
1094c955ccafSRoman Zippel 	 */
109500c864f8SMasahiro Yamada 	if (rename(".tmpconfig", autoconf_name))
1096c955ccafSRoman Zippel 		return 1;
1097c955ccafSRoman Zippel 
1098c955ccafSRoman Zippel 	return 0;
1099c955ccafSRoman Zippel }
1100b3214293SKarsten Wiese 
1101bfc10001SKarsten Wiese static int sym_change_count;
11023b354c55SKarsten Wiese static void (*conf_changed_callback)(void);
1103bfc10001SKarsten Wiese 
1104bfc10001SKarsten Wiese void sym_set_change_count(int count)
1105bfc10001SKarsten Wiese {
11063b354c55SKarsten Wiese 	int _sym_change_count = sym_change_count;
1107bfc10001SKarsten Wiese 	sym_change_count = count;
11083b354c55SKarsten Wiese 	if (conf_changed_callback &&
11093b354c55SKarsten Wiese 	    (bool)_sym_change_count != (bool)count)
11103b354c55SKarsten Wiese 		conf_changed_callback();
1111bfc10001SKarsten Wiese }
1112bfc10001SKarsten Wiese 
1113bfc10001SKarsten Wiese void sym_add_change_count(int count)
1114bfc10001SKarsten Wiese {
11153b354c55SKarsten Wiese 	sym_set_change_count(count + sym_change_count);
1116bfc10001SKarsten Wiese }
1117bfc10001SKarsten Wiese 
1118b3214293SKarsten Wiese bool conf_get_changed(void)
1119b3214293SKarsten Wiese {
1120b3214293SKarsten Wiese 	return sym_change_count;
1121b3214293SKarsten Wiese }
11223b354c55SKarsten Wiese 
11233b354c55SKarsten Wiese void conf_set_changed_callback(void (*fn)(void))
11243b354c55SKarsten Wiese {
11253b354c55SKarsten Wiese 	conf_changed_callback = fn;
11263b354c55SKarsten Wiese }
1127dc7862e5SRoman Zippel 
11283b9a19e0SYann E. MORIN static bool randomize_choice_values(struct symbol *csym)
1129a64b44eaSSam Ravnborg {
1130a64b44eaSSam Ravnborg 	struct property *prop;
1131a64b44eaSSam Ravnborg 	struct symbol *sym;
1132a64b44eaSSam Ravnborg 	struct expr *e;
1133a64b44eaSSam Ravnborg 	int cnt, def;
1134a64b44eaSSam Ravnborg 
1135a64b44eaSSam Ravnborg 	/*
1136579fb8e7SArnaud Lacombe 	 * If choice is mod then we may have more items selected
1137a64b44eaSSam Ravnborg 	 * and if no then no-one.
1138a64b44eaSSam Ravnborg 	 * In both cases stop.
1139a64b44eaSSam Ravnborg 	 */
1140a64b44eaSSam Ravnborg 	if (csym->curr.tri != yes)
11413b9a19e0SYann E. MORIN 		return false;
1142a64b44eaSSam Ravnborg 
1143a64b44eaSSam Ravnborg 	prop = sym_get_choice_prop(csym);
1144a64b44eaSSam Ravnborg 
1145a64b44eaSSam Ravnborg 	/* count entries in choice block */
1146a64b44eaSSam Ravnborg 	cnt = 0;
1147a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym)
1148a64b44eaSSam Ravnborg 		cnt++;
1149a64b44eaSSam Ravnborg 
1150a64b44eaSSam Ravnborg 	/*
1151a64b44eaSSam Ravnborg 	 * find a random value and set it to yes,
1152a64b44eaSSam Ravnborg 	 * set the rest to no so we have only one set
1153a64b44eaSSam Ravnborg 	 */
1154a64b44eaSSam Ravnborg 	def = (rand() % cnt);
1155a64b44eaSSam Ravnborg 
1156a64b44eaSSam Ravnborg 	cnt = 0;
1157a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym) {
1158a64b44eaSSam Ravnborg 		if (def == cnt++) {
1159a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = yes;
1160a64b44eaSSam Ravnborg 			csym->def[S_DEF_USER].val = sym;
1161a64b44eaSSam Ravnborg 		}
1162a64b44eaSSam Ravnborg 		else {
1163a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = no;
1164a64b44eaSSam Ravnborg 		}
1165e6abf12aSYann E. MORIN 		sym->flags |= SYMBOL_DEF_USER;
1166e6abf12aSYann E. MORIN 		/* clear VALID to get value calculated */
1167e6abf12aSYann E. MORIN 		sym->flags &= ~SYMBOL_VALID;
1168a64b44eaSSam Ravnborg 	}
1169a64b44eaSSam Ravnborg 	csym->flags |= SYMBOL_DEF_USER;
1170a64b44eaSSam Ravnborg 	/* clear VALID to get value calculated */
1171a64b44eaSSam Ravnborg 	csym->flags &= ~(SYMBOL_VALID);
11723b9a19e0SYann E. MORIN 
11733b9a19e0SYann E. MORIN 	return true;
1174a64b44eaSSam Ravnborg }
1175a64b44eaSSam Ravnborg 
1176fbe98bb9SArve Hjønnevåg void set_all_choice_values(struct symbol *csym)
1177a64b44eaSSam Ravnborg {
1178a64b44eaSSam Ravnborg 	struct property *prop;
1179a64b44eaSSam Ravnborg 	struct symbol *sym;
1180a64b44eaSSam Ravnborg 	struct expr *e;
1181a64b44eaSSam Ravnborg 
1182a64b44eaSSam Ravnborg 	prop = sym_get_choice_prop(csym);
1183a64b44eaSSam Ravnborg 
1184a64b44eaSSam Ravnborg 	/*
1185a64b44eaSSam Ravnborg 	 * Set all non-assinged choice values to no
1186a64b44eaSSam Ravnborg 	 */
1187a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym) {
1188a64b44eaSSam Ravnborg 		if (!sym_has_value(sym))
1189a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = no;
1190a64b44eaSSam Ravnborg 	}
1191a64b44eaSSam Ravnborg 	csym->flags |= SYMBOL_DEF_USER;
1192a64b44eaSSam Ravnborg 	/* clear VALID to get value calculated */
1193fbe98bb9SArve Hjønnevåg 	csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1194a64b44eaSSam Ravnborg }
1195dc7862e5SRoman Zippel 
11963b9a19e0SYann E. MORIN bool conf_set_all_new_symbols(enum conf_def_mode mode)
1197dc7862e5SRoman Zippel {
1198dc7862e5SRoman Zippel 	struct symbol *sym, *csym;
1199b92d804aSMasahiro Yamada 	int i, cnt, pby, pty, ptm;	/* pby: probability of bool     = y
1200e43956e6SYann E. MORIN 					 * pty: probability of tristate = y
1201e43956e6SYann E. MORIN 					 * ptm: probability of tristate = m
1202e43956e6SYann E. MORIN 					 */
1203e43956e6SYann E. MORIN 
1204e43956e6SYann E. MORIN 	pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1205e43956e6SYann E. MORIN 				   * below, otherwise gcc whines about
1206e43956e6SYann E. MORIN 				   * -Wmaybe-uninitialized */
1207e43956e6SYann E. MORIN 	if (mode == def_random) {
1208e43956e6SYann E. MORIN 		int n, p[3];
1209e43956e6SYann E. MORIN 		char *env = getenv("KCONFIG_PROBABILITY");
1210e43956e6SYann E. MORIN 		n = 0;
1211e43956e6SYann E. MORIN 		while( env && *env ) {
1212e43956e6SYann E. MORIN 			char *endp;
1213e43956e6SYann E. MORIN 			int tmp = strtol( env, &endp, 10 );
1214e43956e6SYann E. MORIN 			if( tmp >= 0 && tmp <= 100 ) {
1215e43956e6SYann E. MORIN 				p[n++] = tmp;
1216e43956e6SYann E. MORIN 			} else {
1217e43956e6SYann E. MORIN 				errno = ERANGE;
1218e43956e6SYann E. MORIN 				perror( "KCONFIG_PROBABILITY" );
1219e43956e6SYann E. MORIN 				exit( 1 );
1220e43956e6SYann E. MORIN 			}
1221e43956e6SYann E. MORIN 			env = (*endp == ':') ? endp+1 : endp;
1222e43956e6SYann E. MORIN 			if( n >=3 ) {
1223e43956e6SYann E. MORIN 				break;
1224e43956e6SYann E. MORIN 			}
1225e43956e6SYann E. MORIN 		}
1226e43956e6SYann E. MORIN 		switch( n ) {
1227e43956e6SYann E. MORIN 		case 1:
1228e43956e6SYann E. MORIN 			pby = p[0]; ptm = pby/2; pty = pby-ptm;
1229e43956e6SYann E. MORIN 			break;
1230e43956e6SYann E. MORIN 		case 2:
1231e43956e6SYann E. MORIN 			pty = p[0]; ptm = p[1]; pby = pty + ptm;
1232e43956e6SYann E. MORIN 			break;
1233e43956e6SYann E. MORIN 		case 3:
1234e43956e6SYann E. MORIN 			pby = p[0]; pty = p[1]; ptm = p[2];
1235e43956e6SYann E. MORIN 			break;
1236e43956e6SYann E. MORIN 		}
1237e43956e6SYann E. MORIN 
1238e43956e6SYann E. MORIN 		if( pty+ptm > 100 ) {
1239e43956e6SYann E. MORIN 			errno = ERANGE;
1240e43956e6SYann E. MORIN 			perror( "KCONFIG_PROBABILITY" );
1241e43956e6SYann E. MORIN 			exit( 1 );
1242e43956e6SYann E. MORIN 		}
1243e43956e6SYann E. MORIN 	}
12443b9a19e0SYann E. MORIN 	bool has_changed = false;
1245dc7862e5SRoman Zippel 
1246dc7862e5SRoman Zippel 	for_all_symbols(i, sym) {
1247cfa98f2eSYann E. MORIN 		if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
1248dc7862e5SRoman Zippel 			continue;
1249dc7862e5SRoman Zippel 		switch (sym_get_type(sym)) {
1250dc7862e5SRoman Zippel 		case S_BOOLEAN:
1251dc7862e5SRoman Zippel 		case S_TRISTATE:
12523b9a19e0SYann E. MORIN 			has_changed = true;
1253dc7862e5SRoman Zippel 			switch (mode) {
1254dc7862e5SRoman Zippel 			case def_yes:
1255dc7862e5SRoman Zippel 				sym->def[S_DEF_USER].tri = yes;
1256dc7862e5SRoman Zippel 				break;
1257dc7862e5SRoman Zippel 			case def_mod:
1258dc7862e5SRoman Zippel 				sym->def[S_DEF_USER].tri = mod;
1259dc7862e5SRoman Zippel 				break;
1260dc7862e5SRoman Zippel 			case def_no:
12615d2acfc7SJosh Triplett 				if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
12625d2acfc7SJosh Triplett 					sym->def[S_DEF_USER].tri = yes;
12635d2acfc7SJosh Triplett 				else
1264dc7862e5SRoman Zippel 					sym->def[S_DEF_USER].tri = no;
1265dc7862e5SRoman Zippel 				break;
1266dc7862e5SRoman Zippel 			case def_random:
1267e43956e6SYann E. MORIN 				sym->def[S_DEF_USER].tri = no;
1268e43956e6SYann E. MORIN 				cnt = rand() % 100;
1269e43956e6SYann E. MORIN 				if (sym->type == S_TRISTATE) {
1270e43956e6SYann E. MORIN 					if (cnt < pty)
1271e43956e6SYann E. MORIN 						sym->def[S_DEF_USER].tri = yes;
1272e43956e6SYann E. MORIN 					else if (cnt < (pty+ptm))
1273e43956e6SYann E. MORIN 						sym->def[S_DEF_USER].tri = mod;
1274e43956e6SYann E. MORIN 				} else if (cnt < pby)
1275e43956e6SYann E. MORIN 					sym->def[S_DEF_USER].tri = yes;
1276dc7862e5SRoman Zippel 				break;
1277dc7862e5SRoman Zippel 			default:
1278dc7862e5SRoman Zippel 				continue;
1279dc7862e5SRoman Zippel 			}
1280184832c9SSam Ravnborg 			if (!(sym_is_choice(sym) && mode == def_random))
1281dc7862e5SRoman Zippel 				sym->flags |= SYMBOL_DEF_USER;
1282dc7862e5SRoman Zippel 			break;
1283dc7862e5SRoman Zippel 		default:
1284dc7862e5SRoman Zippel 			break;
1285dc7862e5SRoman Zippel 		}
1286dc7862e5SRoman Zippel 
1287dc7862e5SRoman Zippel 	}
1288dc7862e5SRoman Zippel 
1289ce97e13eSAl Viro 	sym_clear_all_valid();
1290dc7862e5SRoman Zippel 
1291184832c9SSam Ravnborg 	/*
1292184832c9SSam Ravnborg 	 * We have different type of choice blocks.
1293579fb8e7SArnaud Lacombe 	 * If curr.tri equals to mod then we can select several
1294184832c9SSam Ravnborg 	 * choice symbols in one block.
1295184832c9SSam Ravnborg 	 * In this case we do nothing.
1296579fb8e7SArnaud Lacombe 	 * If curr.tri equals yes then only one symbol can be
1297184832c9SSam Ravnborg 	 * selected in a choice block and we set it to yes,
1298184832c9SSam Ravnborg 	 * and the rest to no.
1299184832c9SSam Ravnborg 	 */
1300fbe98bb9SArve Hjønnevåg 	if (mode != def_random) {
1301fbe98bb9SArve Hjønnevåg 		for_all_symbols(i, csym) {
1302fbe98bb9SArve Hjønnevåg 			if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1303fbe98bb9SArve Hjønnevåg 			    sym_is_choice_value(csym))
1304fbe98bb9SArve Hjønnevåg 				csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1305fbe98bb9SArve Hjønnevåg 		}
1306fbe98bb9SArve Hjønnevåg 	}
1307fbe98bb9SArve Hjønnevåg 
1308dc7862e5SRoman Zippel 	for_all_symbols(i, csym) {
1309dc7862e5SRoman Zippel 		if (sym_has_value(csym) || !sym_is_choice(csym))
1310dc7862e5SRoman Zippel 			continue;
1311dc7862e5SRoman Zippel 
1312dc7862e5SRoman Zippel 		sym_calc_value(csym);
1313a64b44eaSSam Ravnborg 		if (mode == def_random)
13143b9a19e0SYann E. MORIN 			has_changed = randomize_choice_values(csym);
13153b9a19e0SYann E. MORIN 		else {
13163b9a19e0SYann E. MORIN 			set_all_choice_values(csym);
13173b9a19e0SYann E. MORIN 			has_changed = true;
1318dc7862e5SRoman Zippel 		}
1319dc7862e5SRoman Zippel 	}
13203b9a19e0SYann E. MORIN 
13213b9a19e0SYann E. MORIN 	return has_changed;
13223b9a19e0SYann E. MORIN }
1323